UNPKG

react-three-nurbs

Version:

A React component library for rendering NURBS (Non-Uniform Rational B-Spline) surfaces in Three.js. Built with React Three Fiber, this library provides an easy way to create and visualize complex curved surfaces in your 3D applications.

824 lines (821 loc) 27 kB
import Ye, { useMemo as ge, useState as mr, useEffect as hr, isValidElement as Ee, Children as br } from "react"; import { Vector3 as yr, BufferGeometry as gr, Float32BufferAttribute as $e, MeshPhongMaterial as Er, DoubleSide as Le, Color as _r, Mesh as Rr } from "three"; import ue from "verb-nurbs"; import { Line as Sr } from "@react-three/drei"; import { useThree as Cr } from "@react-three/fiber"; var _e = { exports: {} }, ee = {}; /** * @license React * react-jsx-runtime.production.min.js * * Copyright (c) Facebook, Inc. and its affiliates. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ var Me; function Tr() { if (Me) return ee; Me = 1; var T = Ye, S = Symbol.for("react.element"), d = Symbol.for("react.fragment"), h = Object.prototype.hasOwnProperty, k = T.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.ReactCurrentOwner, A = { key: !0, ref: !0, __self: !0, __source: !0 }; function E(P, s, _) { var f, u = {}, R = null, F = null; _ !== void 0 && (R = "" + _), s.key !== void 0 && (R = "" + s.key), s.ref !== void 0 && (F = s.ref); for (f in s) h.call(s, f) && !A.hasOwnProperty(f) && (u[f] = s[f]); if (P && P.defaultProps) for (f in s = P.defaultProps, s) u[f] === void 0 && (u[f] = s[f]); return { $$typeof: S, type: P, key: R, ref: F, props: u, _owner: k.current }; } return ee.Fragment = d, ee.jsx = E, ee.jsxs = E, ee; } var re = {}; /** * @license React * react-jsx-runtime.development.js * * Copyright (c) Facebook, Inc. and its affiliates. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ var Ne; function wr() { return Ne || (Ne = 1, process.env.NODE_ENV !== "production" && function() { var T = Ye, S = Symbol.for("react.element"), d = Symbol.for("react.portal"), h = Symbol.for("react.fragment"), k = Symbol.for("react.strict_mode"), A = Symbol.for("react.profiler"), E = Symbol.for("react.provider"), P = Symbol.for("react.context"), s = Symbol.for("react.forward_ref"), _ = Symbol.for("react.suspense"), f = Symbol.for("react.suspense_list"), u = Symbol.for("react.memo"), R = Symbol.for("react.lazy"), F = Symbol.for("react.offscreen"), C = Symbol.iterator, b = "@@iterator"; function g(e) { if (e === null || typeof e != "object") return null; var r = C && e[C] || e[b]; return typeof r == "function" ? r : null; } var p = T.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED; function c(e) { { for (var r = arguments.length, t = new Array(r > 1 ? r - 1 : 0), n = 1; n < r; n++) t[n - 1] = arguments[n]; W("error", e, t); } } function W(e, r, t) { { var n = p.ReactDebugCurrentFrame, i = n.getStackAddendum(); i !== "" && (r += "%s", t = t.concat([i])); var l = t.map(function(o) { return String(o); }); l.unshift("Warning: " + r), Function.prototype.apply.call(console[e], console, l); } } var H = !1, U = !1, ce = !1, le = !1, w = !1, v; v = Symbol.for("react.module.reference"); function $(e) { return !!(typeof e == "string" || typeof e == "function" || e === h || e === A || w || e === k || e === _ || e === f || le || e === F || H || U || ce || typeof e == "object" && e !== null && (e.$$typeof === R || e.$$typeof === u || e.$$typeof === E || e.$$typeof === P || e.$$typeof === s || // This needs to include all possible module reference object // types supported by any Flight configuration anywhere since // we don't know which Flight build this will end up being used // with. e.$$typeof === v || e.getModuleId !== void 0)); } function M(e, r, t) { var n = e.displayName; if (n) return n; var i = r.displayName || r.name || ""; return i !== "" ? t + "(" + i + ")" : t; } function B(e) { return e.displayName || "Context"; } function j(e) { if (e == null) return null; if (typeof e.tag == "number" && c("Received an unexpected object in getComponentNameFromType(). This is likely a bug in React. Please file an issue."), typeof e == "function") return e.displayName || e.name || null; if (typeof e == "string") return e; switch (e) { case h: return "Fragment"; case d: return "Portal"; case A: return "Profiler"; case k: return "StrictMode"; case _: return "Suspense"; case f: return "SuspenseList"; } if (typeof e == "object") switch (e.$$typeof) { case P: var r = e; return B(r) + ".Consumer"; case E: var t = e; return B(t._context) + ".Provider"; case s: return M(e, e.render, "ForwardRef"); case u: var n = e.displayName || null; return n !== null ? n : j(e.type) || "Memo"; case R: { var i = e, l = i._payload, o = i._init; try { return j(o(l)); } catch { return null; } } } return null; } var N = Object.assign, z = 0, Z, Y, L, I, te, ne, q; function J() { } J.__reactDisabledLog = !0; function Ke() { { if (z === 0) { Z = console.log, Y = console.info, L = console.warn, I = console.error, te = console.group, ne = console.groupCollapsed, q = console.groupEnd; var e = { configurable: !0, enumerable: !0, value: J, writable: !0 }; Object.defineProperties(console, { info: e, log: e, warn: e, error: e, group: e, groupCollapsed: e, groupEnd: e }); } z++; } } function Be() { { if (z--, z === 0) { var e = { configurable: !0, enumerable: !0, writable: !0 }; Object.defineProperties(console, { log: N({}, e, { value: Z }), info: N({}, e, { value: Y }), warn: N({}, e, { value: L }), error: N({}, e, { value: I }), group: N({}, e, { value: te }), groupCollapsed: N({}, e, { value: ne }), groupEnd: N({}, e, { value: q }) }); } z < 0 && c("disabledDepth fell below zero. This is a bug in React. Please file an issue."); } } var fe = p.ReactCurrentDispatcher, ve; function ae(e, r, t) { { if (ve === void 0) try { throw Error(); } catch (i) { var n = i.stack.trim().match(/\n( *(at )?)/); ve = n && n[1] || ""; } return ` ` + ve + e; } } var de = !1, oe; { var qe = typeof WeakMap == "function" ? WeakMap : Map; oe = new qe(); } function Re(e, r) { if (!e || de) return ""; { var t = oe.get(e); if (t !== void 0) return t; } var n; de = !0; var i = Error.prepareStackTrace; Error.prepareStackTrace = void 0; var l; l = fe.current, fe.current = null, Ke(); try { if (r) { var o = function() { throw Error(); }; if (Object.defineProperty(o.prototype, "props", { set: function() { throw Error(); } }), typeof Reflect == "object" && Reflect.construct) { try { Reflect.construct(o, []); } catch (x) { n = x; } Reflect.construct(e, [], o); } else { try { o.call(); } catch (x) { n = x; } e.call(o.prototype); } } else { try { throw Error(); } catch (x) { n = x; } e(); } } catch (x) { if (x && n && typeof x.stack == "string") { for (var a = x.stack.split(` `), O = n.stack.split(` `), m = a.length - 1, y = O.length - 1; m >= 1 && y >= 0 && a[m] !== O[y]; ) y--; for (; m >= 1 && y >= 0; m--, y--) if (a[m] !== O[y]) { if (m !== 1 || y !== 1) do if (m--, y--, y < 0 || a[m] !== O[y]) { var D = ` ` + a[m].replace(" at new ", " at "); return e.displayName && D.includes("<anonymous>") && (D = D.replace("<anonymous>", e.displayName)), typeof e == "function" && oe.set(e, D), D; } while (m >= 1 && y >= 0); break; } } } finally { de = !1, fe.current = l, Be(), Error.prepareStackTrace = i; } var X = e ? e.displayName || e.name : "", G = X ? ae(X) : ""; return typeof e == "function" && oe.set(e, G), G; } function Ue(e, r, t) { return Re(e, !1); } function ze(e) { var r = e.prototype; return !!(r && r.isReactComponent); } function ie(e, r, t) { if (e == null) return ""; if (typeof e == "function") return Re(e, ze(e)); if (typeof e == "string") return ae(e); switch (e) { case _: return ae("Suspense"); case f: return ae("SuspenseList"); } if (typeof e == "object") switch (e.$$typeof) { case s: return Ue(e.render); case u: return ie(e.type, r, t); case R: { var n = e, i = n._payload, l = n._init; try { return ie(l(i), r, t); } catch { } } } return ""; } var Q = Object.prototype.hasOwnProperty, Se = {}, Ce = p.ReactDebugCurrentFrame; function se(e) { if (e) { var r = e._owner, t = ie(e.type, e._source, r ? r.type : null); Ce.setExtraStackFrame(t); } else Ce.setExtraStackFrame(null); } function Ge(e, r, t, n, i) { { var l = Function.call.bind(Q); for (var o in e) if (l(e, o)) { var a = void 0; try { if (typeof e[o] != "function") { var O = Error((n || "React class") + ": " + t + " type `" + o + "` is invalid; it must be a function, usually from the `prop-types` package, but received `" + typeof e[o] + "`.This often happens because of typos such as `PropTypes.function` instead of `PropTypes.func`."); throw O.name = "Invariant Violation", O; } a = e[o](r, o, n, t, null, "SECRET_DO_NOT_PASS_THIS_OR_YOU_WILL_BE_FIRED"); } catch (m) { a = m; } a && !(a instanceof Error) && (se(i), c("%s: type specification of %s `%s` is invalid; the type checker function must return `null` or an `Error` but returned a %s. You may have forgotten to pass an argument to the type checker creator (arrayOf, instanceOf, objectOf, oneOf, oneOfType, and shape all require an argument).", n || "React class", t, o, typeof a), se(null)), a instanceof Error && !(a.message in Se) && (Se[a.message] = !0, se(i), c("Failed %s type: %s", t, a.message), se(null)); } } } var Je = Array.isArray; function pe(e) { return Je(e); } function Ve(e) { { var r = typeof Symbol == "function" && Symbol.toStringTag, t = r && e[Symbol.toStringTag] || e.constructor.name || "Object"; return t; } } function Xe(e) { try { return Te(e), !1; } catch { return !0; } } function Te(e) { return "" + e; } function we(e) { if (Xe(e)) return c("The provided key is an unsupported type %s. This value must be coerced to a string before before using it here.", Ve(e)), Te(e); } var je = p.ReactCurrentOwner, He = { key: !0, ref: !0, __self: !0, __source: !0 }, Oe, Pe; function Ze(e) { if (Q.call(e, "ref")) { var r = Object.getOwnPropertyDescriptor(e, "ref").get; if (r && r.isReactWarning) return !1; } return e.ref !== void 0; } function Qe(e) { if (Q.call(e, "key")) { var r = Object.getOwnPropertyDescriptor(e, "key").get; if (r && r.isReactWarning) return !1; } return e.key !== void 0; } function er(e, r) { typeof e.ref == "string" && je.current; } function rr(e, r) { { var t = function() { Oe || (Oe = !0, c("%s: `key` is not a prop. Trying to access it will result in `undefined` being returned. If you need to access the same value within the child component, you should pass it as a different prop. (https://reactjs.org/link/special-props)", r)); }; t.isReactWarning = !0, Object.defineProperty(e, "key", { get: t, configurable: !0 }); } } function tr(e, r) { { var t = function() { Pe || (Pe = !0, c("%s: `ref` is not a prop. Trying to access it will result in `undefined` being returned. If you need to access the same value within the child component, you should pass it as a different prop. (https://reactjs.org/link/special-props)", r)); }; t.isReactWarning = !0, Object.defineProperty(e, "ref", { get: t, configurable: !0 }); } } var nr = function(e, r, t, n, i, l, o) { var a = { // This tag allows us to uniquely identify this as a React Element $$typeof: S, // Built-in properties that belong on the element type: e, key: r, ref: t, props: o, // Record the component responsible for creating this element. _owner: l }; return a._store = {}, Object.defineProperty(a._store, "validated", { configurable: !1, enumerable: !1, writable: !0, value: !1 }), Object.defineProperty(a, "_self", { configurable: !1, enumerable: !1, writable: !1, value: n }), Object.defineProperty(a, "_source", { configurable: !1, enumerable: !1, writable: !1, value: i }), Object.freeze && (Object.freeze(a.props), Object.freeze(a)), a; }; function ar(e, r, t, n, i) { { var l, o = {}, a = null, O = null; t !== void 0 && (we(t), a = "" + t), Qe(r) && (we(r.key), a = "" + r.key), Ze(r) && (O = r.ref, er(r, i)); for (l in r) Q.call(r, l) && !He.hasOwnProperty(l) && (o[l] = r[l]); if (e && e.defaultProps) { var m = e.defaultProps; for (l in m) o[l] === void 0 && (o[l] = m[l]); } if (a || O) { var y = typeof e == "function" ? e.displayName || e.name || "Unknown" : e; a && rr(o, y), O && tr(o, y); } return nr(e, a, O, i, n, je.current, o); } } var me = p.ReactCurrentOwner, xe = p.ReactDebugCurrentFrame; function V(e) { if (e) { var r = e._owner, t = ie(e.type, e._source, r ? r.type : null); xe.setExtraStackFrame(t); } else xe.setExtraStackFrame(null); } var he; he = !1; function be(e) { return typeof e == "object" && e !== null && e.$$typeof === S; } function ke() { { if (me.current) { var e = j(me.current.type); if (e) return ` Check the render method of \`` + e + "`."; } return ""; } } function or(e) { return ""; } var Ae = {}; function ir(e) { { var r = ke(); if (!r) { var t = typeof e == "string" ? e : e.displayName || e.name; t && (r = ` Check the top-level render call using <` + t + ">."); } return r; } } function Fe(e, r) { { if (!e._store || e._store.validated || e.key != null) return; e._store.validated = !0; var t = ir(r); if (Ae[t]) return; Ae[t] = !0; var n = ""; e && e._owner && e._owner !== me.current && (n = " It was passed a child from " + j(e._owner.type) + "."), V(e), c('Each child in a list should have a unique "key" prop.%s%s See https://reactjs.org/link/warning-keys for more information.', t, n), V(null); } } function De(e, r) { { if (typeof e != "object") return; if (pe(e)) for (var t = 0; t < e.length; t++) { var n = e[t]; be(n) && Fe(n, r); } else if (be(e)) e._store && (e._store.validated = !0); else if (e) { var i = g(e); if (typeof i == "function" && i !== e.entries) for (var l = i.call(e), o; !(o = l.next()).done; ) be(o.value) && Fe(o.value, r); } } } function sr(e) { { var r = e.type; if (r == null || typeof r == "string") return; var t; if (typeof r == "function") t = r.propTypes; else if (typeof r == "object" && (r.$$typeof === s || // Note: Memo only checks outer props here. // Inner props are checked in the reconciler. r.$$typeof === u)) t = r.propTypes; else return; if (t) { var n = j(r); Ge(t, e.props, "prop", n, e); } else if (r.PropTypes !== void 0 && !he) { he = !0; var i = j(r); c("Component %s declared `PropTypes` instead of `propTypes`. Did you misspell the property assignment?", i || "Unknown"); } typeof r.getDefaultProps == "function" && !r.getDefaultProps.isReactClassApproved && c("getDefaultProps is only used on classic React.createClass definitions. Use a static property named `defaultProps` instead."); } } function ur(e) { { for (var r = Object.keys(e.props), t = 0; t < r.length; t++) { var n = r[t]; if (n !== "children" && n !== "key") { V(e), c("Invalid prop `%s` supplied to `React.Fragment`. React.Fragment can only have `key` and `children` props.", n), V(null); break; } } e.ref !== null && (V(e), c("Invalid attribute `ref` supplied to `React.Fragment`."), V(null)); } } var Ie = {}; function We(e, r, t, n, i, l) { { var o = $(e); if (!o) { var a = ""; (e === void 0 || typeof e == "object" && e !== null && Object.keys(e).length === 0) && (a += " You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports."); var O = or(); O ? a += O : a += ke(); var m; e === null ? m = "null" : pe(e) ? m = "array" : e !== void 0 && e.$$typeof === S ? (m = "<" + (j(e.type) || "Unknown") + " />", a = " Did you accidentally export a JSX literal instead of a component?") : m = typeof e, c("React.jsx: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: %s.%s", m, a); } var y = ar(e, r, t, i, l); if (y == null) return y; if (o) { var D = r.children; if (D !== void 0) if (n) if (pe(D)) { for (var X = 0; X < D.length; X++) De(D[X], e); Object.freeze && Object.freeze(D); } else c("React.jsx: Static children should always be an array. You are likely explicitly calling React.jsxs or React.jsxDEV. Use the Babel transform instead."); else De(D, e); } if (Q.call(r, "key")) { var G = j(e), x = Object.keys(r).filter(function(pr) { return pr !== "key"; }), ye = x.length > 0 ? "{key: someKey, " + x.join(": ..., ") + ": ...}" : "{key: someKey}"; if (!Ie[G + ye]) { var dr = x.length > 0 ? "{" + x.join(": ..., ") + ": ...}" : "{}"; c(`A props object containing a "key" prop is being spread into JSX: let props = %s; <%s {...props} /> React keys must be passed directly to JSX without using spread: let props = %s; <%s key={someKey} {...props} />`, ye, G, dr, G), Ie[G + ye] = !0; } } return e === h ? ur(y) : sr(y), y; } } function cr(e, r, t) { return We(e, r, t, !0); } function lr(e, r, t) { return We(e, r, t, !1); } var fr = lr, vr = cr; re.Fragment = h, re.jsx = fr, re.jsxs = vr; }()), re; } process.env.NODE_ENV === "production" ? _e.exports = Tr() : _e.exports = wr(); var K = _e.exports; const jr = ({ points: T, degree: S = 3, weights: d, knots: h, resolution: k = 50, color: A = "black", segments: E, dashed: P = !1, vertexColors: s, ..._ }) => { const f = ge(() => { if (!h) return console.error("NurbsCurve requires knots to be provided"), []; try { const u = Array(T.length).fill(1), R = ue.geom.NurbsCurve.byKnotsControlPointsWeights( S, h, T, d ?? u ), F = k; return Array.from({ length: F + 1 }, (C, b) => { const g = b / F, p = R.point(g); return new yr(p[0], p[1], p[2]); }); } catch (u) { return console.error("Error creating NURBS curve:", u), []; } }, [T, S, d, h, k]); return f.length === 0 ? null : /* @__PURE__ */ K.jsx( Sr, { points: f, color: A, segments: E, dashed: P, vertexColors: s, ..._ } ); }; function Fr({ controlPoints: T, weights: S, degreeU: d, degreeV: h, color: k = "#ffffff", wireframe: A = !1, children: E, ...P }) { const { scene: s } = Cr(), [_, f] = mr(null); return hr(() => { const u = T.length + d + 1, R = T[0].length + h + 1, F = Array(u).fill(0).map((w, v) => v < d + 1 ? 0 : v >= u - d - 1 ? 1 : (v - d) / (u - 2 * d - 1)), C = Array(R).fill(0).map((w, v) => v < h + 1 ? 0 : v >= R - h - 1 ? 1 : (v - h) / (R - 2 * h - 1)), b = ue.geom.NurbsSurface.byKnotsControlPointsWeights( d, h, F, C, T, S ), g = new gr(), p = [], c = [], W = [], H = 20, U = 20, ce = 1 / H, le = 1 / U; for (let w = 0; w <= H; w++) for (let v = 0; v <= U; v++) { const $ = w * ce, M = v * le, B = b.point($, M), j = 1e-4, N = b.point($ + j, M), z = b.point($, M + j), Z = b.point($, M), Y = N.map((q, J) => (q - Z[J]) / j), L = z.map((q, J) => (q - Z[J]) / j), I = [ Y[1] * L[2] - Y[2] * L[1], Y[2] * L[0] - Y[0] * L[2], Y[0] * L[1] - Y[1] * L[0] ], te = Math.sqrt( I[0] * I[0] + I[1] * I[1] + I[2] * I[2] ), ne = I.map((q) => q / te); p.push(...B), c.push(...ne); } for (let w = 0; w < H; w++) for (let v = 0; v < U; v++) { const $ = w * (U + 1) + v, M = $ + 1, B = (w + 1) * (U + 1) + v, j = B + 1; W.push($, M, B), W.push(M, j, B); } if (g.setAttribute("position", new $e(p, 3)), g.setAttribute("normal", new $e(c, 3)), g.setIndex(W), f(g), !E) { const w = new Er({ color: new _r(k), side: Le, wireframe: A }), v = new Rr(g, w); return s.add(v), () => { s.remove(v), g.dispose(), w.dispose(); }; } return () => { g.dispose(); }; }, [ T, S, d, h, k, A, s, E ]), _ && E ? !Ee(E) || !E.type.toString().includes("Material") ? (console.warn("NurbsSurface children must be a material component"), null) : /* @__PURE__ */ K.jsxs("mesh", { ...P, children: [ /* @__PURE__ */ K.jsx("primitive", { object: _, attach: "geometry" }), E ] }) : null; } const Dr = ({ degreeV: T = 3, resolutionU: S = 20, resolutionV: d = 20, color: h = "#ff0000", wireframe: k = !1, children: A }) => { const { curveChildren: E, materialChild: P } = ge(() => { const _ = []; let f = null; return br.forEach(A, (u) => { Ee(u) && u.type === jr ? _.push(u) : Ee(u) && u.type && u.type.prototype && "isMaterial" in u.type.prototype && (f = u); }), { curveChildren: _, materialChild: f }; }, [A]), s = ge(() => { if (E.length < 2) return console.error("LoftedSurface requires at least 2 NurbsCurve children"), null; try { const _ = E.map((C) => { const { points: b, degree: g = 3, weights: p, knots: c } = C.props, W = Array(b.length).fill(1); return ue.geom.NurbsCurve.byKnotsControlPointsWeights( g, c, b, p ?? W ); }), f = ue.geom.NurbsSurface.byLoftingCurves( _, T ), u = [], R = [], F = []; for (let C = 0; C <= S; C++) for (let b = 0; b <= d; b++) { const g = C / S, p = b / d, c = f.point(g, p); u.push(c[0], c[1], c[2]), F.push(g, p); } for (let C = 0; C < S; C++) for (let b = 0; b < d; b++) { const g = C * (d + 1) + b, p = g + 1, c = (C + 1) * (d + 1) + b, W = c + 1; R.push(g, p, c), R.push(p, W, c); } return { vertices: u, indices: R, uvs: F }; } catch (_) { return console.error("Error creating lofted surface:", _), null; } }, [E, T, S, d]); return s ? /* @__PURE__ */ K.jsxs("mesh", { children: [ /* @__PURE__ */ K.jsxs("bufferGeometry", { children: [ /* @__PURE__ */ K.jsx( "bufferAttribute", { attach: "attributes-position", count: s.vertices.length / 3, array: new Float32Array(s.vertices), itemSize: 3 } ), /* @__PURE__ */ K.jsx( "bufferAttribute", { attach: "attributes-uv", count: s.uvs.length / 2, array: new Float32Array(s.uvs), itemSize: 2 } ), /* @__PURE__ */ K.jsx( "bufferAttribute", { attach: "index", count: s.indices.length, array: new Uint32Array(s.indices), itemSize: 1 } ) ] }), P || /* @__PURE__ */ K.jsx("meshStandardMaterial", { color: h, wireframe: k, side: Le }) ] }) : null; }; export { Dr as LoftedSurface, jr as NurbsCurve, Fr as NurbsSurface };