UNPKG

videx-3d

Version:

React 3D component library designed for sub surface visualizations in the browser

592 lines (591 loc) 21.2 kB
import { BufferGeometry as K, BufferAttribute as I } from "three"; import { lerp as Z } from "three/src/math/MathUtils.js"; import { d as k, P as j, o as $, p as O, a3 as D, E as S, F as tt, aa as st, ag as nt, J as et } from "./chunk-iY0wQ9Z6.js"; function B(l, t, s) { if (t[0] === s[0]) return s[1]; const n = s[0] - t[0], e = k((l - t[0]) / n, 0, 1); return Z(t[1], s[1], e); } function it(l, t, s, n, e, a, h, c) { const o = []; if (t === "none" || a.length === 0) o.push([s, e], [n, e]); else { let r = [0, e], d = [1, e]; const _ = a.findIndex((m) => m[0] > s); _ === -1 ? r = a[a.length - 1] : (_ > 0 && (r = a[_ - 1]), d = a[_]); const v = t === "linear" ? B(s, r, d) : r[1]; o.push([s, v]); for (let m = _; m >= 0 && m < a.length; m++) { const w = a[m]; if (w[0] < n) o.push(w); else { t === "linear" ? o.push([ n, B(n, o[o.length - 1], w) ]) : o.push([n, o[o.length - 1][1]]); break; } } o[o.length - 1][0] < n && (t === "linear" ? o.push([ n, B(n, o[o.length - 1], [1, e]) ]) : o.push([n, o[o.length - 1][1]])); } const f = [], p = $( l, s, n, h, c ); let g = 0, i = p[g]; for (let r = 0; r < o.length - 1; r++) { const d = r + 1, [_, v] = o[r], [m, w] = o[d], A = (m - _) * l.length, T = w - v, P = Math.atan2(T, A); for (f.push([ _, v, t === "linear" ? P : 0 ]); i <= _ && g < p.length - 1; ) i = p[++g]; for (; i < m && g < p.length; ) { const b = t === "linear" ? B(i, o[r], o[d]) : v; f.push([ i, b, t === "linear" ? P : 0 ]), i = p[g++]; } if (d < o.length) { if (t === "linear") f.push([ m, w, t === "linear" ? P : 0 ]); else if (t === "stepped") { const b = P < 0 ? -j / 2 : j / 2; f.push( [m, v, 0], [m, v, b], [m, w, b] ); } } d === o.length - 1 && t === "none" && f.push([m, w, P]); } const u = O( l, f.map((r) => r[0]) ); return f.map((r, d) => ({ radius: r[1], theta: r[2], ...u[d] })); } function X(l, t, s = !0, n, e = 0) { let a = 0, h = 0; const c = [], o = [], f = n.computeNormals ? [] : null, p = n.computeUvs ? [] : null, g = s ? [-l.tangent[0], -l.tangent[1], -l.tangent[2]] : l.tangent; c.push(...l.position), a++, f && f.push(...g), p && p.push(0.5, 0.5); for (let i = 0; i <= t; i++) { const u = i / t * j * 2, r = Math.sin(u), d = -Math.cos(u), _ = D([ d * l.normal[0] + r * l.binormal[0], d * l.normal[1] + r * l.binormal[1], d * l.normal[2] + r * l.binormal[2] ]); if (c.push( l.position[0] + l.radius * _[0], l.position[1] + l.radius * _[1], l.position[2] + l.radius * _[2] ), a++, f && f.push(...g), p) { const v = [(d + 1) / 2, (r + 1) / 2]; s && (v[0] = 1 - v[0]), p.push(...v); } } for (let i = 1; i <= t; i++) { let r, d; s ? (r = i + 0, d = i + 0 + 1) : (r = i + 0 + 1, d = i + 0), o.push(r + e, d + e, 0 + e), h += 3; } return { vertices: c, indices: o, normals: f, uvs: p, vertexCount: a, indexCount: h }; } function Y(l, t, s, n = !0, e, a = 0) { let h = 0, c = 0; const o = [], f = [], p = e.computeNormals ? [] : null, g = e.computeUvs ? [] : null, i = n ? [ -l.tangent[0], -l.tangent[1], -l.tangent[2] ] : l.tangent, u = t.radius / l.radius; for (let r = 0; r <= s; r++) { const d = r / s * j * 2, _ = Math.sin(d), v = -Math.cos(d), m = D([ v * l.normal[0] + _ * l.binormal[0], v * l.normal[1] + _ * l.binormal[1], v * l.normal[2] + _ * l.binormal[2] ]); if (o.push( l.position[0] + l.radius * m[0], l.position[1] + l.radius * m[1], l.position[2] + l.radius * m[2] ), h++, o.push( t.position[0] + t.radius * m[0], t.position[1] + t.radius * m[1], t.position[2] + t.radius * m[2] ), h++, p && p.push(...i, ...i), g) { const w = [(v + 1) / 2, (_ + 1) / 2], q = [ (v * u + 1) / 2, (_ * u + 1) / 2 ]; n && (w[0] = 1 - w[0], q[0] = 1 - q[0]), g.push(...w, ...q); } } for (let r = 0; r < s; r++) { const d = r * 2, _ = d + 1, v = d + 2, m = d + 3; n ? f.push( d + a, v + a, _ + a, _ + a, v + a, m + a ) : f.push( v + a, d + a, _ + a, _ + a, m + a, v + a ), c += 6; } return { vertices: o, indices: f, normals: p, uvs: g, vertexCount: h, indexCount: c }; } function y(l, t, s, n, e = 0) { let a = 0, h = 0; const c = [], o = [], f = n.computeNormals ? [] : null, p = n.computeUvs ? [] : null, g = (i) => { for (let u = 0; u <= t; u++) { const r = u / t * j * 2, d = Math.sin(r), _ = -Math.cos(r), v = D([ _ * i.normal[0] + d * i.binormal[0], _ * i.normal[1] + d * i.binormal[1], _ * i.normal[2] + d * i.binormal[2] ]), m = [ i.position[0] + i.radius * v[0], i.position[1] + i.radius * v[1], i.position[2] + i.radius * v[2] ]; if (f) { let w = S(v); if (i.theta) { const q = D( tt(i.tangent, v) ); w = st(v, q, i.theta); } f.push(...w); } c.push(...m), a++; } }; for (let i = 0; i < l.length; i++) g(l[i]); if (s && g(l[0]), p) for (let i = 0; i < l.length; i++) for (let u = 0; u <= t; u++) p.push(u / t, i / (l.length - 1)); for (let i = 1; i < l.length; i++) for (let u = 1; u <= t; u++) { const r = (t + 1) * (i - 1) + (u - 1), d = (t + 1) * i + (u - 1), _ = (t + 1) * i + u, v = (t + 1) * (i - 1) + u; o.push(r + e, d + e, v + e), o.push(d + e, _ + e, v + e), h += 6; } return { vertices: c, indices: o, normals: f, uvs: p, vertexCount: a, indexCount: h }; } function ct(l, t = {}) { const s = k(t.from || 0, 0, 1), n = k(t.to || 1); if (n < s) throw Error('Value of "from" must be less than the value of "to"!'); const e = new K(), a = t.radius || 1, h = t.radiusModifier?.steps || [], c = t.radialSegments || 8, o = t.startCap || !1, f = t.endCap || !1, p = l.closed; h.sort((b, M) => b[0] - M[0]); const g = t.segmentsPerMeter || 0.1, i = t.radiusModifier?.type || "none", u = k( t.simplificationThreshold || 0, 0, 1 ), r = it( l, i, s, n, a, h, g, u ), d = y( r, c, p, t ); let _ = null, v = null, m = null, w = d.vertexCount, q = 0; t.addGroups && (e.addGroup(q, d.indexCount, e.groups.length), q += d.indexCount); let A = null; (t.innerRadius || t.thickness) && (A = r.map((b) => ({ ...b, radius: t.innerRadius || b.radius - t.thickness, theta: b.theta - j })), _ = y( A, c, p, t, w ), w += _.vertexCount, t.addGroups && (e.addGroup( q, _.indexCount, e.groups.length ), q += _.indexCount)), o && (!p || s > 0 || n < 1) && (A ? v = Y( r[0], A[0], c, !0, t, w ) : v = X( r[0], c, !0, t, w ), w += v.vertexCount, t.addGroups && (e.addGroup( q, v.indexCount, e.groups.length ), q += v.indexCount)), f && (!p || s > 0 || n < 1) && (A ? m = Y( r[r.length - 1], A[A.length - 1], c, !1, t, w ) : m = X( r[r.length - 1], c, !1, t, w ), w += m.vertexCount, t.addGroups && (e.addGroup(q, m.indexCount, e.groups.length), q += m.indexCount)); let T = d.vertices, P = d.indices; if (_ && (T = T.concat(_.vertices), P = P.concat(_.indices.reverse())), v && (T = T.concat(v.vertices), P = P.concat(v.indices)), m && (T = T.concat(m.vertices), P = P.concat(m.indices)), e.setAttribute( "position", new I(Float32Array.from(T), 3) ), t.computeNormals) { let b = d.normals; _ && (b = b.concat(_.normals)), v && (b = b.concat(v.normals)), m && (b = b.concat(m.normals)), e.setAttribute( "normal", new I(Float32Array.from(b), 3) ); } if (t.computeLengths || t.computeCurveNormals || t.computeCurveTangents || t.computeCurveBinormals || t.computeRelativeLengths) { const b = t.computeLengths ? [] : null, M = t.computeRelativeLengths ? [] : null, L = t.computeCurveNormals ? [] : null, R = t.computeCurveTangents ? [] : null, z = t.computeCurveBinormals ? [] : null, F = l.length; for (let C = 0; C < r.length; C++) for (let G = 0; G <= c; G++) b && b.push(r[C].curvePosition * F), M && M.push( (r[C].curvePosition - s) * F ), L && L.push(...r[C].normal), R && R.push(...r[C].tangent), z && z.push(...r[C].binormal); if (_ && A) for (let C = 0; C < A.length; C++) for (let G = 0; G <= c; G++) b && b.push(A[C].curvePosition * F), M && M.push( (r[C].curvePosition - s) * F ), L && L.push(...A[C].normal), R && R.push(...A[C].tangent), z && z.push(...A[C].binormal); if (v) for (let C = 0; C < v.vertexCount; C++) b && b.push(s * F), M && M.push(0), L && L.push(...r[0].normal), R && R.push(...r[0].tangent), z && z.push(...r[0].binormal); if (m) for (let C = 0; C < m.vertexCount; C++) b && b.push(n * F), M && M.push(F), L && L.push(...r[r.length - 1].normal), R && R.push(...r[r.length - 1].tangent), z && z.push(...r[r.length - 1].binormal); b && e.setAttribute( "curveLength", new I(Float32Array.from(b), 1) ), M && e.setAttribute( "curveRelativeLength", new I(Float32Array.from(M), 1) ), L && e.setAttribute( "curveNormal", new I(Float32Array.from(L), 3) ), R && e.setAttribute( "curveTangent", new I(Float32Array.from(R), 3) ), z && e.setAttribute( "curveBinormal", new I(Float32Array.from(z), 3) ); } if (t.computeUvs) { let b = d.uvs; _ && (b = b.concat(_.uvs)), v && (b = b.concat(v.uvs)), m && (b = b.concat(m.uvs)), e.setAttribute("uv", new I(Float32Array.from(b), 2)); } return e.setIndex(new I(Uint32Array.from(P), 1)), e; } const J = -1e3; class ot { data; width; height; coords = []; // vertex coordinates (x, y) triangles = []; // mesh triangle indices nullValue; _queue = []; _queueIndices = []; _errors = []; _halfedges = []; _candidates = []; _invalidPoints; _rms = []; _pending = []; _pendingLen = 0; _rmsSum = 0; constructor(t, s, n = -1) { this.data = t, this.width = s, this.height = this.data.length / s, this.nullValue = n, this._invalidPoints = /* @__PURE__ */ new Set(); const e = this.width - 1, a = this.height - 1, h = this._addPoint(0, 0), c = this._addPoint(e, 0), o = this._addPoint(0, a), f = this._addPoint(e, a), p = this._addTriangle(f, h, o, -1, -1, -1); this._addTriangle(h, f, c, p, -1, -1), this._flush(); } // refine the mesh until its maximum error gets below the given one run(t = 1) { for (; this.getMaxError() > t; ) this.refine(); } // Removes triangles where one or more vertices contains a null value (nullValue) removeInvalidTriangles() { const t = []; for (let s = 0; s < this.triangles.length; s += 3) { const n = this.triangles[s], e = this.triangles[s + 1], a = this.triangles[s + 2]; !this._invalidPoints.has(n) && !this._invalidPoints.has(e) && !this._invalidPoints.has(a) && t.push( this.triangles[s], this.triangles[s + 1], this.triangles[s + 2] ); } this.triangles = t; } // refine the mesh with a single point refine() { this._step(), this._flush(); } // max error of the current mesh getMaxError() { return this._errors[0]; } // root-mean-square deviation of the current mesh getRMSD() { return this._rmsSum > 0 ? Math.sqrt(this._rmsSum / (this.width * this.height)) : 0; } // height value at a given position heightAt(t, s) { const n = this.data[this.width * s + t]; return n === this.nullValue ? J : n; } // rasterize a triangle, find its max error, and queue it for processing _findCandidate(t, s, n, e, a, h, c) { const o = Math.min(t, n, a), f = Math.min(s, e, h), p = Math.max(t, n, a), g = Math.max(s, e, h); let i = N(n, e, a, h, o, f), u = N(a, h, t, s, o, f), r = N(t, s, n, e, o, f); const d = e - s, _ = t - n, v = h - e, m = n - a, w = s - h, q = a - t, A = N(t, s, n, e, a, h), T = this.heightAt(t, s) / A, P = this.heightAt(n, e) / A, b = this.heightAt(a, h) / A; let M = 0, L = 0, R = 0, z = 0; for (let F = f; F <= g; F++) { let C = 0; i < 0 && v !== 0 && (C = Math.max(C, Math.floor(-i / v))), u < 0 && w !== 0 && (C = Math.max(C, Math.floor(-u / w))), r < 0 && d !== 0 && (C = Math.max(C, Math.floor(-r / d))); let G = i + v * C, E = u + w * C, x = r + d * C, H = !1; for (let U = o + C; U <= p; U++) { if (G >= 0 && E >= 0 && x >= 0) { H = !0; const Q = T * G + P * E + b * x, W = this.heightAt(U, F), V = Math.abs(Q - W); z += V * V, V > M && (M = V, L = U, R = F); } else if (H) break; G += v, E += w, x += d; } i += m, u += q, r += _; } (L === t && R === s || L === n && R === e || L === a && R === h) && (M = 0), this._candidates[2 * c] = L, this._candidates[2 * c + 1] = R, this._rms[c] = z, this._queuePush(c, M, z); } // process the next triangle in the queue, splitting it with a new point _step() { const t = this._queuePop(), s = t * 3 + 0, n = t * 3 + 1, e = t * 3 + 2, a = this.triangles[s], h = this.triangles[n], c = this.triangles[e], o = this.coords[2 * a], f = this.coords[2 * a + 1], p = this.coords[2 * h], g = this.coords[2 * h + 1], i = this.coords[2 * c], u = this.coords[2 * c + 1], r = this._candidates[2 * t], d = this._candidates[2 * t + 1], _ = this._addPoint(r, d); if (N(o, f, p, g, r, d) === 0) this._handleCollinear(_, s); else if (N(p, g, i, u, r, d) === 0) this._handleCollinear(_, n); else if (N(i, u, o, f, r, d) === 0) this._handleCollinear(_, e); else { const v = this._halfedges[s], m = this._halfedges[n], w = this._halfedges[e], q = this._addTriangle(a, h, _, v, -1, -1, s), A = this._addTriangle(h, c, _, m, -1, q + 1), T = this._addTriangle(c, a, _, w, q + 2, A + 1); this._legalize(q), this._legalize(A), this._legalize(T); } } _addPoint(t, s) { const n = this.coords.length >> 1; return this.coords.push(t, s), this.heightAt(t, s) === J && this._invalidPoints.add(n), n; } _addTriangle(t, s, n, e, a, h, c = this.triangles.length) { const o = c / 3; return this.triangles[c + 0] = t, this.triangles[c + 1] = s, this.triangles[c + 2] = n, this._halfedges[c + 0] = e, this._halfedges[c + 1] = a, this._halfedges[c + 2] = h, e >= 0 && (this._halfedges[e] = c + 0), a >= 0 && (this._halfedges[a] = c + 1), h >= 0 && (this._halfedges[h] = c + 2), this._candidates[2 * o + 0] = 0, this._candidates[2 * o + 1] = 0, this._queueIndices[o] = -1, this._rms[o] = 0, this._pending[this._pendingLen++] = o, c; } _flush() { const t = this.coords; for (let s = 0; s < this._pendingLen; s++) { const n = this._pending[s], e = 2 * this.triangles[n * 3 + 0], a = 2 * this.triangles[n * 3 + 1], h = 2 * this.triangles[n * 3 + 2]; this._findCandidate( t[e], t[e + 1], t[a], t[a + 1], t[h], t[h + 1], n ); } this._pendingLen = 0; } _legalize(t) { const s = this._halfedges[t]; if (s < 0) return; const n = t - t % 3, e = s - s % 3, a = n + (t + 1) % 3, h = n + (t + 2) % 3, c = e + (s + 2) % 3, o = e + (s + 1) % 3, f = this.triangles[h], p = this.triangles[t], g = this.triangles[a], i = this.triangles[c], u = this.coords; if (!rt( u[2 * f], u[2 * f + 1], u[2 * p], u[2 * p + 1], u[2 * g], u[2 * g + 1], u[2 * i], u[2 * i + 1] )) return; const r = this._halfedges[a], d = this._halfedges[h], _ = this._halfedges[c], v = this._halfedges[o]; this._queueRemove(n / 3), this._queueRemove(e / 3); const m = this._addTriangle(f, i, g, -1, _, r, n), w = this._addTriangle(i, f, p, m, d, v, e); this._legalize(m + 1), this._legalize(w + 2); } _handleCollinear(t, s) { const n = s - s % 3, e = n + (s + 1) % 3, a = n + (s + 2) % 3, h = this.triangles[a], c = this.triangles[s], o = this.triangles[e], f = this._halfedges[e], p = this._halfedges[a], g = this._halfedges[s]; if (g < 0) { const T = this._addTriangle(t, h, c, -1, p, -1, n), P = this._addTriangle(h, t, o, T, -1, f); this._legalize(T + 1), this._legalize(P + 2); return; } const i = g - g % 3, u = i + (g + 2) % 3, r = i + (g + 1) % 3, d = this.triangles[u], _ = this._halfedges[u], v = this._halfedges[r]; this._queueRemove(i / 3); const m = this._addTriangle(h, c, t, p, -1, -1, n), w = this._addTriangle(c, d, t, v, -1, m + 1, i), q = this._addTriangle(d, o, t, _, -1, w + 1), A = this._addTriangle(o, h, t, f, m + 2, q + 1); this._legalize(m), this._legalize(w), this._legalize(q), this._legalize(A); } // priority queue methods _queuePush(t, s, n) { const e = this._queue.length; this._queueIndices[t] = e, this._queue.push(t), this._errors.push(s), this._rmsSum += n, this._queueUp(e); } _queuePop() { const t = this._queue.length - 1; return this._queueSwap(0, t), this._queueDown(0, t), this._queuePopBack(); } _queuePopBack() { const t = this._queue.pop(); return this._errors.pop(), this._rmsSum -= this._rms[t], this._queueIndices[t] = -1, t; } _queueRemove(t) { const s = this._queueIndices[t]; if (s < 0) { const e = this._pending.indexOf(t); if (e !== -1) this._pending[e] = this._pending[--this._pendingLen]; else throw new Error("Broken triangulation (something went wrong)."); return; } const n = this._queue.length - 1; n !== s && (this._queueSwap(s, n), this._queueDown(s, n) || this._queueUp(s)), this._queuePopBack(); } _queueLess(t, s) { return this._errors[t] > this._errors[s]; } _queueSwap(t, s) { const n = this._queue[t], e = this._queue[s]; this._queue[t] = e, this._queue[s] = n, this._queueIndices[n] = s, this._queueIndices[e] = t; const a = this._errors[t]; this._errors[t] = this._errors[s], this._errors[s] = a; } _queueUp(t) { let s = t; for (; ; ) { const n = s - 1 >> 1; if (n === s || !this._queueLess(s, n)) break; this._queueSwap(n, s), s = n; } } _queueDown(t, s) { let n = t; for (; ; ) { const e = 2 * n + 1; if (e >= s || e < 0) break; const a = e + 1; let h = e; if (a < s && this._queueLess(a, e) && (h = a), !this._queueLess(h, n)) break; this._queueSwap(n, h), n = h; } return n > t; } } function N(l, t, s, n, e, a) { return (s - e) * (t - a) - (n - a) * (l - e); } function rt(l, t, s, n, e, a, h, c) { const o = l - h, f = t - c, p = s - h, g = n - c, i = e - h, u = a - c, r = o * o + f * f, d = p * p + g * g, _ = i * i + u * u; return o * (g * _ - d * u) - f * (p * _ - d * i) + r * (p * u - g * i) < 0; } function ut(l, t, s) { const n = nt(l), e = new Array(n.length); let a = null; for (let g = 0; g < n.length; g++) a !== null ? e[g] = e[a] + et(n[a], n[g]) : e[g] = 0, a = g; const h = e[e.length - 1], c = new K(), o = new Float32Array(n.length * 2 * 3), f = new Uint32Array((n.length - 1) * 6), p = new Float32Array(n.length * 2 * 2); for (let g = 0; g < n.length; g++) { const i = g * 6; o[i] = n[g][0], o[i + 1] = t, o[i + 2] = n[g][1], o[i + 3] = n[g][0], o[i + 4] = s, o[i + 5] = n[g][1]; const u = g * 4; p[u] = e[g] / h, p[u + 1] = 1, p[u + 2] = e[g] / h, p[u + 3] = 0; const r = g * 2; g < n.length - 1 && (f[i] = r, f[i + 1] = r + 1, f[i + 2] = r + 2, f[i + 3] = r + 2, f[i + 4] = r + 1, f[i + 5] = r + 3); } return c.setAttribute("position", new I(o, 3)), c.setAttribute("uv", new I(p, 2)), c.setIndex(new I(f, 1)), c.computeVertexNormals(), c; } function dt(l, t, s = 1, n = 1, e = -1, a = 5) { const h = t, c = l.length / h; console.time("delatin"); const o = new ot(l, h, e); o.run(a), o.removeInvalidTriangles(), console.timeEnd("delatin"); const f = new Float32Array(o.coords.length * 1.5), p = new Float32Array(o.coords.length); for (let i = 0, u = 0; i < o.coords.length; i += 2) p[i] = o.coords[i] / (h - 1), p[i + 1] = 1 - o.coords[i + 1] / (c - 1), u = i * 1.5, f[u] = o.coords[i] * s, f[u + 1] = o.heightAt(o.coords[i], o.coords[i + 1]), f[u + 2] = o.coords[i + 1] * n; const g = new Uint32Array(o.triangles); return console.log(o.triangles.length / 3), { positions: f, uvs: p, indices: g }; } function ft(l) { let t = 0, s, n; if (!l || l.length === 0) return t; for (s = 0; s < l.length; s++) n = l.charCodeAt(s), t = (t << 5) - t + n, t |= 0; return t; } function gt(l) { return l.replace(/(^|\s)\S/g, (t) => t.toUpperCase()); } export { ot as D, ut as a, ct as b, ft as c, dt as d, gt as t };