UNPKG

videx-3d

Version:

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

888 lines (874 loc) 21.8 kB
import { EPS as at, CurveInterpolator as it } from "curve-interpolator"; import { BufferGeometry as X, BufferAttribute as U, Vector3 as v, ShaderMaterial as Y, OrthographicCamera as ct, Scene as lt, PlaneGeometry as ut, Mesh as dt, WebGLRenderTarget as ft, RGBAFormat as mt, UnsignedByteType as gt } from "three"; async function Wt(t, e, r, n) { const o = await r.get("formations", t) || []; n = n === void 0 ? -1 / 0 : n; const s = o.filter( (a) => a.stratColumnId === e && a.exit.mdMsl > n ); return s.sort( (a, i) => a.entry.mdMsl - i.entry.mdMsl || a.level - i.level ), s.length && s[0].entry.mdMsl < n && (s[0].entry.mdMsl = n), s; } function O(t, e, r) { return { mdMslFrom: e, mdMslTo: r, name: t.name, level: t.level, color: t.color, properties: { ...t.properties || {} } }; } function St(t) { if (!t.length) return []; let e = []; const r = [...t].sort((n, o) => { const s = o.level - n.level || n.entry.mdMsl - o.entry.mdMsl; if (s === 0 && (n.properties?.updated || o.properties?.updated)) { const a = n.properties?.updated ? new Date(n.properties?.updated) : null, i = o.properties?.updated ? new Date(o.properties?.updated) : null; return a === null ? 1 : i === null ? -1 : i.getTime() - a.getTime(); } return s; }); for (let n = 0; n < r.length; n++) { const o = r[n]; let s = o.entry.mdMsl; const a = [...e]; for (let i = 0; i < e.length && s < o.exit.mdMsl; i++) { const c = e[i]; if (s < c.mdMslFrom) { const l = Math.min(o.exit.mdMsl, c.mdMslFrom); a.push(O(o, s, l)); } s = Math.max(s, c.mdMslTo); } s < o.exit.mdMsl && a.push( O(o, s, o.exit.mdMsl) ), e = a.sort((i, c) => i.mdMslFrom - c.mdMslFrom); } return e; } function zt(t) { const e = [], r = [...t].sort( (s, a) => s.entry.mdMsl - a.entry.mdMsl || a.level - s.level ); let n = null, o = -1 / 0; for (let s = 0; s < r.length; s++) { const a = r[s]; a.entry.mdMsl > o && (o = a.entry.mdMsl, n && n.exit.mdMsl < o && e.push({ color: n.color, name: n.name, type: "base", mdMsl: n.exit.mdMsl, tvdMsl: n.exit.tvdMsl, level: n.level }), e.push({ color: a.color, name: a.name, type: "top", mdMsl: o, tvdMsl: a.entry.tvdMsl, level: a.level }), n = a); } return n && e.push({ color: n.color, name: n.name, type: "base", mdMsl: n.exit.mdMsl, tvdMsl: n.exit.tvdMsl, level: n.level }), e; } function B(t, e = 0, r = 1) { return t < e ? e : t > r ? r : t; } function ht(t, e, r) { return r = B(r, 0, 1), (1 - r) * t + r * e; } function pt(t, e, r) { return (t - e) / (r - e); } function Nt(t, e, r, n, o) { const s = pt(t, e, r); return ht(n, o, s); } const jt = (t) => { if (t < 0) return [0, 0, 0]; if (t > 16777.215) throw Error("Value out of range!"); const e = Math.round(t * 1e3), r = Math.floor(e / 65536), n = Math.floor(e / 256) - r * 256, o = Math.floor(e) - r * 65536 - n * 256; return [r, n, o]; }, _t = Math.PI, H = 2 * Math.PI, yt = Math.PI / 2, Gt = Math.PI / 4, qt = Math.PI / 8; function Zt(t, e) { for (; e < -Math.PI; ) e += H; for (; e > Math.PI; ) e -= H; const r = Math.atan2(t[1], t[0]), n = Math.tan(e); let o; e > -r && e <= r ? o = 1 : e > r && e <= Math.PI - r ? o = 2 : e > Math.PI - r || e <= -(Math.PI - r) ? o = 3 : o = 4; const s = [0, 0]; let a = 1, i = 1; switch (o) { case 1: i = -1; break; case 2: i = -1; break; case 3: a = -1; break; case 4: a = -1; break; } return o === 1 || o === 3 ? (s[0] += a * (t[0] / 2), s[1] += i * (t[0] / 2) * n) : (s[0] += a * (t[1] / (2 * n)), s[1] += i * (t[1] / 2)), s; } function Ot(t) { return t[0] = -t[0], t[1] = -t[1], t[2] = -t[2], t; } function Ht(t, e) { return [t[e], t[e + 1], t[e + 2]]; } function $t(t, e, r) { t[e] = r[0], t[e + 1] = r[1], t[e + 2] = r[2]; } function Qt(t, e) { return [t[0] + e[0], t[1] + e[1], t[2] + e[2]]; } function tt(t, e) { return [t[0] - e[0], t[1] - e[1], t[2] - e[2]]; } function Jt(t, e) { return t[0] = t[0] * e, t[1] = t[1] * e, t[2] = t[2] * e, t; } function et(t) { const e = t[0] ** 2 + t[1] ** 2 + t[2] ** 2; return e > 0 ? Math.sqrt(e) : 0; } function E(t, e) { return [ t[1] * e[2] - t[2] * e[1], t[2] * e[0] - t[0] * e[2], t[0] * e[1] - t[1] * e[0] ]; } function q(t, e) { return t[0] * e[0] + t[1] * e[1] + t[2] * e[2]; } function z(t) { const e = Math.sqrt(t[0] * t[0] + t[1] * t[1] + t[2] * t[2]); return e ? (t[0] = t[0] / e, t[1] = t[1] / e, t[2] = t[2] / e, t) : [0, 0, 0]; } function Kt(t, e, r = 0.5) { const n = 1 - r; return [ t[0] * n + e[0] * r, t[1] * n + e[1] * r, t[2] * n + e[2] * r ]; } function Mt(t) { return [t[0], t[1], t[2]]; } function Xt(t, e) { const r = Math.sqrt( (t[0] ** 2 + t[1] ** 2 + t[2] ** 2) * (e[0] ** 2 + e[1] ** 2 + e[2] ** 2) ); if (r === 0) return yt; const n = q(t, e) / r; return Math.acos(B(n, -1, 1)); } function At(t, e = [0, 1, 0], r = 0) { const n = Math.cos(r), o = Math.sin(r), s = 1 - n, a = t[0], i = t[1], c = t[2], l = e[0], d = e[1], f = e[2], m = s * l, u = s * d; return [ (m * l + n) * a + (m * d - o * f) * i + (m * f + o * d) * c, (m * d + o * f) * a + (u * d + n) * i + (u * f - o * l) * c, (m * f - o * d) * a + (u * f + o * l) * i + (s * f * f + n) * c ]; } function Yt(t, e) { return z(tt(e, t)); } function te(t, e) { return et(tt(e, t)); } function ee(t, e) { return [t[e], t[e + 1]]; } function ne(t, e, r) { t[e] = r[0], t[e + 1] = r[1]; } function nt(t) { const e = Math.sqrt(t[0] * t[0] + t[1] * t[1]); return e ? (t[0] = t[0] / e, t[1] = t[1] / e, t) : [0, 0]; } function $(t, e) { return [t[0] + e[0], t[1] + e[1]]; } function Z(t, e) { return [t[0] - e[0], t[1] - e[1]]; } function rt(t, e) { return t[0] * e[0] + t[1] * e[1]; } function re(t) { return t[0] = -t[0], t[1] = -t[1], t; } function se(t, e) { return t[0] = t[0] * e, t[1] = t[1] * e, t; } function bt(t) { const e = t[0] ** 2 + t[1] ** 2; return e > 0 ? Math.sqrt(e) : 0; } function oe(t, e, r = 0.5) { const n = 1 - r; return [t[0] * n + e[0] * r, t[1] * n + e[1] * r]; } function W(t, e) { return nt(Z(e, t)); } function S(t, e) { return bt(Z(e, t)); } function wt(t, e = !1) { const r = { alpha: 1, tension: 0, closed: e }, n = new it(t, r); return { getPointAt: (s) => n.getPointAt(s), getPoints: (s, a = 0, i = 1) => n.getPoints(s, null, a, i), getTangentAt: (s) => n.getTangentAt(s), getNormalAt: (s) => n.getNormalAt(s), getBoundingBox: (s = 0, a = 1) => { const i = n.getBoundingBox(s, a); return { min: i.min, max: i.max }; }, nearest: (s) => { const a = n.getNearestPosition(s); return { position: a.u, point: a.point, distance: a.distance }; }, get length() { return n.length; }, closed: e }; } function ae(t, e) { const r = e.length, n = new Array(r); if (!n.length) return []; e.forEach((l, d) => { n[d] = { curvePosition: l, position: t.getPointAt(l), tangent: t.getTangentAt(l), normal: [0, 0, 0], binormal: [0, 0, 0] }; }); let o = [0, 1, 0], s = Math.abs(n[0].tangent[1]); const a = Math.abs(n[0].tangent[0]), i = Math.abs(n[0].tangent[2]); a <= s && (s = a, o = [1, 0, 0]), i <= s && (o = [0, 0, 1]); let c = z(E(n[0].tangent, o)); n[0].normal = E(n[0].tangent, c), n[0].binormal = E(n[0].tangent, n[0].normal); for (let l = 1; l < e.length; l++) { if (c = E(n[l - 1].tangent, n[l].tangent), n[l].normal = Mt(n[l - 1].normal), et(c) > at) { z(c); const d = Math.acos( B(q(n[l - 1].tangent, n[l].tangent), -1, 1) ); n[l].normal = At(n[l - 1].normal, c, d); } n[l].binormal = E(n[l].tangent, n[l].normal); } return n; } function ie(t, e = 0, r = 1, n = 0.1, o = 0) { const s = [], a = t.length, i = Math.ceil(n * a), c = 1 / i, l = 1 / (a * 0.01); o && (o *= 0.1), s.push(e); let d = o ? t.getTangentAt(e) : null; const f = Math.floor(e * i); let m = e; for (let u = f; u <= i; u++) { const h = u * c, y = h - m, g = o ? t.getTangentAt(h) : null; h > e && h < r && (y >= l || !o || Math.abs(q(d, g)) < 1 - o) && (s.push(h), d = g, m = h); } return s.push(r), s; } function ce(t, e) { if (!e || e.length < 8) return null; const [r, , n] = e, o = new Array(e.length / 4); for (let f = 0, m = 0; f < o.length; f++, m += 4) o[f] = [ e[m] - r, -e[m + 1], n - e[m + 2] ]; const s = wt(o, !1); if (!s) return null; const a = e[3], i = e[e.length - 1], c = i - a, l = (f, m = !1) => { let u = (f - a) / c; return m && (u = B(u, 0, 1)), u < 0 || u > 1 ? null : u; }; return { id: t, curve: s, get measuredLength() { return c; }, get measuredTop() { return a; }, get measuredBottom() { return i; }, getPositionAtDepth: l, getPointAtDepth: (f, m = !1) => { const u = l(f, m); return u !== null ? s.getPointAt(u) : null; } }; } function le(t, e = (n) => n, r = 1e-7) { if (t.length <= 2) return t; let n = e(t[0]), o = [0, 0]; const s = [t[0]]; for (let a = 1; a < t.length - 1; a++) { const i = e(t[a]), c = nt(Z(i, n)); Math.abs(rt(o, c)) < 1 - r && (s.push(t[a]), o = c, n = i); } return s.push(t[t.length - 1]), s; } function ue(t, e, r = 1e3, n = 2e3, o = 0) { if (!t || t.length < 1) return null; const s = []; let a = 0; const i = [t[0][0], t[0][2]]; let c = t[0][1], l = c, d = 1; s.push(i); let f = s[s.length - 1]; for (; d < t.length; ) { const u = [t[d][0], t[d][2]]; t[d][1] > c && (c = t[d][1]), t[d][1] < l && (l = t[d][1]); const h = S(f, u); if (h < e) d === t.length - 1 && s.length > 1 && (a += h, s.push(u)), d++; else if (a += e, h > e) { const y = W(f, u), g = [ f[0] + y[0] * e, f[1] + y[1] * e ]; s.push(g), f = g; } else s.push(u), f = u, d++; } const m = s.length; if (r || a < n) { let u = 0, h = r; a + r / 2 < n && (u = (n - a) / 2, h = u); let y = [ Math.cos(o), Math.sin(o) ]; if (h) { m > 1 && (y = W( s[s.length - 2], s[s.length - 1] )); const g = s[s.length - 1], L = Math.floor(h / e), k = [ y[0] * e, y[1] * e ], P = [ g[0] + y[0] * h, g[1] + y[1] * h ]; let b = g; for (let T = 0; T < L; T++) b = $(b, k), s.push(b); S(b, P) && s.push(P); } if (u) { let g; m > 1 ? g = W(s[1], s[0]) : g = [ -Math.cos(o), -Math.sin(o) ], (a < 100 || Math.abs(rt(y, g)) > 0.95) && (g = [ -y[0], -y[1] ]); const L = Math.floor(u / e), k = [ g[0] * e, g[1] * e ], P = [ i[0] + g[0] * u, i[1] + g[1] * u ]; let b = i; for (let T = 0; T < L; T++) b = $(b, k), s.unshift(b); S( b, P ) && s.unshift(P); } } return { positions: s, top: c, bottom: l, length: a }; } function D(t) { if (t.constructor === Uint8Array) return "Uint8Array"; if (t.constructor === Uint16Array) return "Uint16Array"; if (t.constructor === Uint32Array) return "Uint32Array"; if (t.constructor === Int8Array) return "Int8Array"; if (t.constructor === Int16Array) return "Int16Array"; if (t.constructor === Int32Array) return "Int32Array"; if (t.constructor === Float32Array) return "Float32Array"; if (t.constructor === Float64Array) return "Float64Array"; throw Error("Unsupported typed array!"); } function C(t, e) { switch (e) { case "Uint8Array": return new Uint8Array(t); case "Uint16Array": return new Uint16Array(t); case "Uint32Array": return new Uint32Array(t); case "Int8Array": return new Int8Array(t); case "Int16Array": return new Int16Array(t); case "Int32Array": return new Int32Array(t); case "Float32Array": return new Float32Array(t); case "Float64Array": return new Float64Array(t); default: throw Error("Unsupported typed array!"); } } function xt(t, e = 1) { return { buffer: t.buffer, attributeType: D(t), itemSize: e }; } function de(t) { const e = [], r = { drawRange: t.drawRange || { start: 0, count: 1 / 0 }, groups: t.groups || [], attributes: {}, index: t.index, indexType: t.index ? D(t.index) : void 0, userData: t.userData ? structuredClone(t.userData) : void 0 }; for (const n in t.attributes) r.attributes[n] = xt( t.attributes[n].array, t.attributes[n].itemSize ), e.push(r.attributes[n].buffer); return [r, e]; } function vt(t) { const e = [], r = { drawRange: t.drawRange, groups: t.groups, attributes: {}, index: void 0, indexType: void 0 }, n = t.getIndex(); n && (r.index = n.array.buffer, r.indexType = D(n.array)); for (const o in t.attributes) { const s = t.getAttribute(o); r.attributes[o] = { buffer: s.array.buffer, attributeType: D(s.array), itemSize: s.itemSize }, e.push(r.attributes[o].buffer); } return t.userData && (r.userData = structuredClone(t.userData)), [r, e]; } function fe(t) { const e = {}, r = []; for (const n in t) { const o = t[n], s = vt(o); e[n] = s[0], s[1].length && r.push(...s[1]); } return { data: e, transferrables: r }; } function me(t) { const e = new X(); if (t.index) { const r = C( t.index, t.indexType || "Uint32Array" ), n = new U(r, 1); e.setIndex(n); } for (const r in t.attributes) { const n = new U( C( t.attributes[r].buffer, t.attributes[r].attributeType ), t.attributes[r].itemSize ); e.setAttribute(r, n); } return e.drawRange = t.drawRange, e.groups = t.groups, t.userData && (e.userData = t.userData), e; } function ge(t) { const e = {}; for (const r in t) { const n = new X(); if (t[r].index) { const o = C( t[r].index, t[r].indexType || "Uint32Array" ), s = new U(o, 1); n.setIndex(s); } for (const o in t[r].attributes) { const s = new U( C( t[r].attributes[o].buffer, t[r].attributes[o].attributeType ), t[r].attributes[o].itemSize ); n.setAttribute(o, s); } n.drawRange = t[r].drawRange, n.groups = t[r].groups, e[r] = n; } return e; } const N = "camera-set-position", j = "camera-focus-point"; class he extends CustomEvent { constructor(e) { super(N, { detail: e }); } } class pe extends CustomEvent { constructor(e) { super(j, { detail: e }); } } const M = new v(), p = new v(), x = new v(), I = new v(), Q = new v(); async function Pt(t, e, r) { r.normalizeRotations(); const n = Math.min(r.distance, e); return M.set(...t), r.getPosition(p), x.subVectors(M, p), e = Math.min(x.length() * 1.5, n), x.normalize(), p.copy(M).addScaledVector(x, -n), r.setLookAt( p.x, p.y, p.z, M.x, M.y, M.z, !0 ); } function Tt(t, e) { e.normalizeRotations(), I.set(...t), e.getTarget(M), Q.subVectors(M, I), e.getPosition(p), p.sub(Q), e.setPosition(p.x, p.y, p.z), e.setTarget(I.x, I.y, I.z); } class ye { controls = null; removeEventlisteners = null; constructor() { this.setControls = this.setControls.bind(this); } addEventListeners() { const e = (n) => { this.controls && Tt(n.detail, this.controls); }, r = (n) => { if (this.controls) { const o = n.detail.callback; Pt( n.detail.point, n.detail.distance || 200, this.controls ).then(() => { o && o(); }); } }; addEventListener(N, e), addEventListener(j, r), this.removeEventlisteners = () => { removeEventListener(N, e), removeEventListener(j, r); }; } async setTarget(e) { const r = this.controls; return r ? (M.set(...e), r.getPosition(p), x.subVectors(M, p), x.normalize(), r.setLookAt( p.x, p.y, p.z, M.x, M.y, M.z, !0 )) : null; } setControls(e) { this.controls = e, this.removeEventlisteners && this.removeEventlisteners(), this.addEventListeners(); } dispose() { this.controls = null, this.removeEventlisteners && this.removeEventlisteners(); } } const Et = "wellbore-selected", _ = "wellbore-added", G = "wellbore-removed"; class Me extends CustomEvent { constructor(e) { super(Et, { detail: e }); } } class Ae extends CustomEvent { constructor(e) { super(_, { detail: e }); } } class be extends CustomEvent { constructor(e) { super(G, { detail: e }); } } const J = new v(); class we { map = /* @__PURE__ */ new Map(); constructor() { this.onWellboreAdded = this.onWellboreAdded.bind(this), this.onWellboreRemoved = this.onWellboreRemoved.bind(this), addEventListener(_, this.onWellboreAdded), addEventListener(G, this.onWellboreRemoved); } onWellboreAdded(e) { this.map.set(e.detail.id, { wellboreId: e.detail.id, object: e.detail.object }); } onWellboreRemoved(e) { this.map.delete(e.detail.id); } getInfo(e) { const r = this.map.get(e); if (r) return r.object.getWorldPosition(J), { ...r, position: J.toArray() }; } dispose() { removeEventListener(_, this.onWellboreAdded), removeEventListener(G, this.onWellboreRemoved); } } function xe(t) { return t / 3.28084; } function ve(t) { return t * (Math.PI / 180); } const Pe = (t, e = 1, r = 1) => [t[0] / e * 2 - 1, -(t[1] / r) * 2 + 1], Te = (t, e = 1, r = 1) => [(t[0] + 1) / 2 * e, (1 - t[1]) / 2 * r]; var It = `#include <common> varying vec2 vUv; void main() { vUv = uv; gl_Position = vec4(position, 1.0); }`, Ft = `#include <common> #include <packing> varying vec2 vUv; uniform sampler2D depthTexture; uniform float cameraFar; uniform float cameraNear; float linearizeDepth(in float depth) { float a = cameraFar / (cameraFar - cameraNear); float b = cameraFar * cameraNear / (cameraNear - cameraFar); return a + b / depth; } float linearizeLogDepth(in float depth) { float z = exp2(depth * log2(cameraFar + 1.0)) - 1.0; return linearizeDepth(z); } void main() { float depth = texture(depthTexture, vUv).r; float viewZ; #if defined( USE_LOGARITHMIC_DEPTH_BUFFER ) || defined( USE_LOGDEPTHBUF ) viewZ = linearizeLogDepth(depth); #else viewZ = depth; #endif gl_FragColor = packDepthToRGBA(viewZ); }`; class Vt extends Y { constructor() { super({ vertexShader: It, fragmentShader: Ft, uniforms: { cameraNear: { value: 0 }, cameraFar: { value: 1 }, depthTexture: { value: null }, cameraWasPerspective: { value: null } }, toneMapped: !1, depthWrite: !1, depthTest: !1 }); } } const V = 255 / 256, R = new Float32Array([ V, V / 256, V / 256 ** 2, V / 256 ** 3 ]); function K(t, e, r, n) { return t / 255 * R[0] + e / 255 * R[1] + r / 255 * R[2] + n / 255 * R[3]; } const Rt = new ct(-1, 1, 1, -1, 0, 1), F = new Vt(), st = new lt(), Ut = new ut(2, 2), Dt = new dt(Ut, F); let w = null, A = null; st.add(Dt); async function Ee(t, e, r, n) { if (!t) return null; try { const { width: o, height: s } = t; if ((!w || !A || w.width !== o || w.height !== s) && (w?.dispose(), A = new Uint8Array(4 * o * s), w = new ft(o, s, { type: gt, format: mt, depthBuffer: !1, stencilBuffer: !1 })), F.uniforms.depthTexture.value = t, F.uniforms.cameraNear.value = r.near, F.uniforms.cameraFar.value = r.far, F.uniforms.cameraWasPerspective.value = r.isPerspectiveCamera, e.setRenderTarget(w), e.clear(), e.render(st, Rt), e.setRenderTarget(null), await e.readRenderTargetPixelsAsync( w, 0, // x 0, // y o, s, A ), n !== void 0 && Number.isFinite(n)) return K( A[n], A[n + 1], A[n + 2], A[n + 3] ) * 2 - 1; { const a = new Array(A.length / 4); for (let i = 0; i < a.length; i++) { const c = i * 4; a[i] = K( A[c], A[c + 1], A[c + 2], A[c + 3] ) * 2 - 1; } return a; } } catch (o) { return console.error(o), null; } } var Ct = `varying vec2 vUv; void main() { vUv = uv; gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0); }`, Bt = `varying vec2 vUv; void main() { gl_FragColor = vec4(vUv, 1.0, 1.0); }`; const Ie = new Y({ vertexShader: Ct, fragmentShader: Bt }); export { ht as $, Qt as A, Xt as B, pe as C, Vt as D, Mt as E, E as F, ve as G, W as H, Yt as I, S as J, te as K, rt as L, q as M, xe as N, zt as O, _t as P, ue as Q, wt as R, C as S, H as T, D as U, ee as V, Ae as W, Ht as X, pt as Y, bt as Z, et as _, Gt as a, Kt as a0, re as a1, Ot as a2, z as a3, xt as a4, fe as a5, vt as a6, de as a7, Ee as a8, Nt as a9, At as aa, se as ab, Jt as ac, Pe as ad, ne as ae, $t as af, le as ag, tt as ah, jt as ai, ge as aj, Ie as ak, yt as b, qt as c, B as d, Zt as e, Te as f, Wt as g, St as h, ce as i, be as j, N as k, j as l, oe as m, nt as n, ie as o, ae as p, he as q, Me as r, Z as s, G as t, me as u, Et as v, _ as w, ye as x, we as y, $ as z };