@margoengine/labels 0.0.0-stage → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,54 @@
1
+ import { type Backend, type ScreenPoint } from '@margoengine/math/mat4';
2
+ /** One posed primitive: stride-20 interleaved vertices (xyz first), triangle indices and the node's world matrix. */
3
+ export interface HotspotSnapshot {
4
+ vertices: ArrayLike<number>;
5
+ indices: ArrayLike<number>;
6
+ /** Column-major 4x4 world matrix of the primitive's node. */
7
+ matrix: ArrayLike<number>;
8
+ primitiveIndex?: number;
9
+ }
10
+ /** Anything that poses a mesh on demand: `Skin.picking` from `@margoengine/animation`, or a morph or physics source with the same method. */
11
+ export interface HotspotSource {
12
+ posePickingGeometry(): readonly HotspotSnapshot[];
13
+ }
14
+ /** Where a hotspot sits: a triangle of a primitive and weights of its three corners. */
15
+ export interface HotspotOptions {
16
+ /** Index of the primitive (default 0). */
17
+ primitiveIndex?: number;
18
+ /** Triangle index: its corners are `indices[3 * face .. 3 * face + 2]`. */
19
+ face: number;
20
+ /** Weights of the triangle's first, second and third corner; must be 0 or more and sum to 1 (within 1e-3). Default the centre. */
21
+ barycentric?: readonly [number, number, number];
22
+ }
23
+ /**
24
+ * A fixed point inside a triangle of a deformed mesh (skinned or morphed): its world position is the barycentric
25
+ * blend of the triangle's three posed corners, so it follows the surface as the pose changes. `project` gives its
26
+ * screen position for a label. The blend P = wa*A + wb*B + wc*C is the standard affine combination of a triangle's
27
+ * corners (Moebius, 1827, "Der barycentrische Calcul").
28
+ */
29
+ export declare class Hotspot {
30
+ private readonly source;
31
+ readonly primitiveIndex: number;
32
+ readonly face: number;
33
+ readonly barycentric: readonly [number, number, number];
34
+ private readonly point;
35
+ constructor(source: HotspotSource, options: HotspotOptions);
36
+ /** A hotspot where a pick hit the mesh: takes `primitiveIndex`, `faceIndex` and `barycoord` from a `Raycaster` hit. */
37
+ static fromHit(source: HotspotSource, hit: {
38
+ primitiveIndex?: number;
39
+ faceIndex: number;
40
+ barycoord: ArrayLike<number>;
41
+ }): Hotspot;
42
+ private snapshot;
43
+ /** World position at the current pose. `out` is written and returned (default a new array). */
44
+ worldPosition(out?: [number, number, number]): [number, number, number];
45
+ /**
46
+ * Screen position in CSS pixels at the current pose. `matrix` is the camera's view-projection (`camera.matrix`).
47
+ * The returned point is reused by the next call.
48
+ */
49
+ project(matrix: ArrayLike<number>, width: number, height: number, backend?: Backend): ScreenPoint;
50
+ /** Moves a label to the hotspot's current world position; call it each frame after the pose updates. */
51
+ pin(label: {
52
+ setPosition(position: [number, number, number]): unknown;
53
+ }): void;
54
+ }
@@ -0,0 +1,6 @@
1
+ export { LabelRenderer, Label, CSS3DObject } from './renderer.js';
2
+ export type { LabelOptions, CSS3DOptions, LabelRendererOptions, LabelView, LabelInfo, OccludeHook, CSS3DOccludeHook, DistanceScale, LabelLayout, LayoutContext, } from './renderer.js';
3
+ export { lookAtView, cssPerspective, cameraCSSTransform, orthographicCSSTransform, objectCSSTransform, cssPointToWorld, composeMatrix, boxAnchorPoint, } from './projection.js';
4
+ export type { Backend, Vec3, BoxAnchorSpec, BoxFace } from './projection.js';
5
+ export { Hotspot } from './hotspot.js';
6
+ export type { HotspotSource, HotspotSnapshot, HotspotOptions } from './hotspot.js';
package/dist/index.js ADDED
@@ -0,0 +1,566 @@
1
+ // @margo/core-built 1.0.0
2
+ import {coreRequirement as __margoCoreRequirement,assertCore as __margoAssertCore} from '@margoengine/core/extension';
3
+ const r=(engine,version="1.0.0",source=import.meta.url)=>__margoRuntime(engine,version,source);
4
+ import { n as e, r as t, t as n } from "./errors-52P7hKm0.js";
5
+ import { engineRuntime as __margoRuntime, meshRuntime as i } from "@margoengine/core/extension";
6
+ import { createScreenPoint as a, isOrthographic as o, projectPoint as s } from "@margoengine/math/mat4";
7
+ //#region src/projection.ts
8
+ var c = 1e6;
9
+ function l(e) {
10
+ let t = Math.round(e * c) / c;
11
+ return t === 0 ? "0" : String(t);
12
+ }
13
+ function u(e) {
14
+ let t = l(e[0]);
15
+ for (let n = 1; n < 16; n++) t += `,${l(e[n])}`;
16
+ return t;
17
+ }
18
+ function d(e, t, n = /* @__PURE__ */ new Float64Array(16), r = [
19
+ 0,
20
+ 1,
21
+ 0
22
+ ]) {
23
+ let i = e[0] - t[0], a = e[1] - t[1], o = e[2] - t[2], s = Math.hypot(i, a, o);
24
+ if (s === 0) throw RangeError("Camera position equals target; move the camera or its target apart");
25
+ i /= s, a /= s, o /= s;
26
+ let c = r[1] * o - r[2] * a, l = r[2] * i - r[0] * o, u = r[0] * a - r[1] * i, d = Math.hypot(c, l, u);
27
+ if (d === 0) throw RangeError("Camera is parallel to its up vector; choose an up vector not along the view direction");
28
+ c /= d, l /= d, u /= d;
29
+ let f = a * u - o * l, p = o * c - i * u, m = i * l - a * c;
30
+ return n[0] = c, n[1] = f, n[2] = i, n[3] = 0, n[4] = l, n[5] = p, n[6] = a, n[7] = 0, n[8] = u, n[9] = m, n[10] = o, n[11] = 0, n[12] = -(c * e[0] + l * e[1] + u * e[2]), n[13] = -(f * e[0] + p * e[1] + m * e[2]), n[14] = -(i * e[0] + a * e[1] + o * e[2]), n[15] = 1, n;
31
+ }
32
+ var f = (e, t) => t / 2 / Math.tan(e * Math.PI / 360);
33
+ function p(e, t, n, r) {
34
+ let i = "";
35
+ for (let t = 0; t < 16; t++) i += (t ? "," : "") + l(t % 4 == 1 ? -e[t] : e[t]);
36
+ return `translateZ(${l(t)}px) matrix3d(${i}) translate(${l(n / 2)}px,${l(r / 2)}px)`;
37
+ }
38
+ function m(e, t, n, r = "webgpu") {
39
+ let i = t / 2, a = n / 2, o = Math.hypot(e[1], e[5], e[9]), s = Math.hypot(e[2], e[6], e[10]) || 1, c = (r === "webgl2" ? -a : a) * o / s, d = [
40
+ 0,
41
+ 0,
42
+ 0,
43
+ 0,
44
+ 0,
45
+ 0,
46
+ 0,
47
+ 0,
48
+ 0,
49
+ 0,
50
+ 0,
51
+ 0,
52
+ 0,
53
+ 0,
54
+ 0,
55
+ 1
56
+ ];
57
+ for (let t = 0; t < 4; t++) {
58
+ let n = t * 4;
59
+ d[n] = e[n] * i, d[n + 1] = -e[n + 1] * a, d[n + 2] = e[n + 2] * c;
60
+ }
61
+ return `matrix3d(${u(d)}) translate(${l(i)}px,${l(a)}px)`;
62
+ }
63
+ function h(e, t = 1) {
64
+ let n = "";
65
+ for (let r = 0; r < 16; r++) {
66
+ let i = r >> 2, a = i === 3 ? 1 : i === 1 ? -t : t;
67
+ n += (r ? "," : "") + l(e[r] * a);
68
+ }
69
+ return `translate(-50%,-50%) matrix3d(${n})`;
70
+ }
71
+ function g(e, t, n, r) {
72
+ let i = n * t, a = -r * t;
73
+ return [
74
+ e[0] * i + e[4] * a + e[12],
75
+ e[1] * i + e[5] * a + e[13],
76
+ e[2] * i + e[6] * a + e[14]
77
+ ];
78
+ }
79
+ function _(e, t = [
80
+ 0,
81
+ 0,
82
+ 0,
83
+ 1
84
+ ], n = [
85
+ 1,
86
+ 1,
87
+ 1
88
+ ], r = /* @__PURE__ */ new Float64Array(16)) {
89
+ let i = Math.hypot(t[0], t[1], t[2], t[3]) || 1, a = t[0] / i, o = t[1] / i, s = t[2] / i, c = t[3] / i, l = n[0], u = n[1], d = n[2];
90
+ return r[0] = (1 - 2 * (o * o + s * s)) * l, r[1] = 2 * (a * o + c * s) * l, r[2] = 2 * (a * s - c * o) * l, r[3] = 0, r[4] = 2 * (a * o - c * s) * u, r[5] = (1 - 2 * (a * a + s * s)) * u, r[6] = 2 * (o * s + c * a) * u, r[7] = 0, r[8] = 2 * (a * s + c * o) * d, r[9] = 2 * (o * s - c * a) * d, r[10] = (1 - 2 * (a * a + o * o)) * d, r[11] = 0, r[12] = e[0], r[13] = e[1], r[14] = e[2], r[15] = 1, r;
91
+ }
92
+ var v = {
93
+ left: [
94
+ -1,
95
+ 0,
96
+ 0
97
+ ],
98
+ right: [
99
+ 1,
100
+ 0,
101
+ 0
102
+ ],
103
+ bottom: [
104
+ 0,
105
+ -1,
106
+ 0
107
+ ],
108
+ top: [
109
+ 0,
110
+ 1,
111
+ 0
112
+ ],
113
+ back: [
114
+ 0,
115
+ 0,
116
+ -1
117
+ ],
118
+ front: [
119
+ 0,
120
+ 0,
121
+ 1
122
+ ],
123
+ center: [
124
+ 0,
125
+ 0,
126
+ 0
127
+ ]
128
+ };
129
+ function y(t, n, r = "center", i, a = [
130
+ 0,
131
+ 0,
132
+ 0
133
+ ]) {
134
+ let o = typeof r == "string" ? v[r] : r;
135
+ if (!o || o.length !== 3 || !o.every((e) => e === -1 || e === 0 || e === 1) || ![...t, ...n].every(Number.isFinite) || t.length !== 3 || n.length !== 3 || t[0] > n[0] || t[1] > n[1] || t[2] > n[2]) throw e("boxAnchorPoint needs finite min <= max and an anchor of a face name, center, or three numbers from -1, 0 and 1; fix the arguments", "labels");
136
+ let s = (t[0] + n[0] + o[0] * (n[0] - t[0])) / 2, c = (t[1] + n[1] + o[1] * (n[1] - t[1])) / 2, l = (t[2] + n[2] + o[2] * (n[2] - t[2])) / 2;
137
+ if (!i) return a[0] = s, a[1] = c, a[2] = l, a;
138
+ let u = i[3] * s + i[7] * c + i[11] * l + i[15] || 1;
139
+ return a[0] = (i[0] * s + i[4] * c + i[8] * l + i[12]) / u, a[1] = (i[1] * s + i[5] * c + i[9] * l + i[13]) / u, a[2] = (i[2] * s + i[6] * c + i[10] * l + i[14]) / u, a;
140
+ }
141
+ //#endregion
142
+ //#region ../shared/visibility.js
143
+ function b(e, t) {
144
+ for (let n = t; n >= 0; n = e.parents?.[n] ?? -1) if (e.hidden[n]) return !0;
145
+ return !1;
146
+ }
147
+ //#endregion
148
+ //#region src/renderer.ts
149
+ var x = (e, t, n, r) => {
150
+ t[n] !== r && (t[n] = r, e.style[n] = r);
151
+ }, S = (e) => `${Math.round(e * 100) / 100}px`, C = (t) => {
152
+ if (typeof t != "number" || Number.isNaN(t)) throw e("priority must be a number", "labels");
153
+ return t;
154
+ }, w = (e, t) => {
155
+ if (!e || e.length < 3 || ![
156
+ e[0],
157
+ e[1],
158
+ e[2]
159
+ ].every(Number.isFinite)) throw RangeError(`${t} must be three finite numbers`);
160
+ return [
161
+ e[0],
162
+ e[1],
163
+ e[2]
164
+ ];
165
+ }, T = class {
166
+ constructor(e, t, n, r, i) {
167
+ if (this.onScreen = !1, this.distance = NaN, this.cache = {}, this.measured = null, !t || typeof t != "object" || !("style" in t)) throw TypeError("Expected an HTMLElement");
168
+ this.owner = e, this.element = t, this.meshValue = n ?? null, this.followVisibility = r ?? !0, this.visible = i ?? !0;
169
+ }
170
+ get mesh() {
171
+ return this.meshValue;
172
+ }
173
+ remeasure() {
174
+ return this.measured = null, this;
175
+ }
176
+ remove() {
177
+ this.owner?.remove(this);
178
+ }
179
+ }, E = class extends T {
180
+ constructor(t, n, r) {
181
+ super(t, n, r.mesh, r.followVisibility, r.visible), this.scale = 1, this.placedAnchor = [.5, .5], this.layoutHidden = !1, this.layoutAnchor = null, this.screen = a(), this.world = [
182
+ 0,
183
+ 0,
184
+ 0
185
+ ], this.placement = (/* @__PURE__ */ new Float64Array(6)).fill(NaN), this.positionValue = w(r.position ?? [
186
+ 0,
187
+ 0,
188
+ 0
189
+ ], "position"), this.offsetValue = w(r.offset ?? [
190
+ 0,
191
+ 0,
192
+ 0
193
+ ], "offset"), this.offsetSpace = r.offsetSpace ?? "local", this.anchor = [r.anchor?.[0] ?? .5, r.anchor?.[1] ?? .5];
194
+ let i = r.screenOffset ?? [0, 0];
195
+ if (!i || !Number.isFinite(i[0]) || !Number.isFinite(i[1])) throw e("screenOffset must be two finite numbers", "labels");
196
+ this.screenOffset = [i[0], i[1]], this.frustumCulling = r.frustumCulling ?? !0, this.distanceScale = r.distanceScale ?? null, this.sort = r.sort ?? !0, this.priorityValue = C(r.priority ?? 0), this.occlude = r.occlude ?? null, n.style.pointerEvents = r.pointerEvents ?? "none";
197
+ }
198
+ get priority() {
199
+ return this.priorityValue;
200
+ }
201
+ set priority(e) {
202
+ this.priorityValue = C(e);
203
+ }
204
+ get position() {
205
+ return this.positionValue;
206
+ }
207
+ get offset() {
208
+ return this.offsetValue;
209
+ }
210
+ setMesh(e) {
211
+ return this.meshValue = e, this;
212
+ }
213
+ setPosition(e) {
214
+ return this.positionValue = w(e, "position"), this.meshValue = null, this;
215
+ }
216
+ setOffset(e) {
217
+ return this.offsetValue = w(e, "offset"), this;
218
+ }
219
+ };
220
+ Object.defineProperty(E.prototype, "position", { set: t });
221
+ var D = class extends T {
222
+ constructor(e, t, n) {
223
+ if (super(e, t, n.mesh, n.followVisibility, n.visible), this.clipped = !1, this.matrix = new Float64Array([
224
+ 1,
225
+ 0,
226
+ 0,
227
+ 0,
228
+ 0,
229
+ 1,
230
+ 0,
231
+ 0,
232
+ 0,
233
+ 0,
234
+ 1,
235
+ 0,
236
+ 0,
237
+ 0,
238
+ 0,
239
+ 1
240
+ ]), this.own = new Float64Array([
241
+ 1,
242
+ 0,
243
+ 0,
244
+ 0,
245
+ 0,
246
+ 1,
247
+ 0,
248
+ 0,
249
+ 0,
250
+ 0,
251
+ 1,
252
+ 0,
253
+ 0,
254
+ 0,
255
+ 0,
256
+ 1
257
+ ]), this.pixelScale = n.pixelScale ?? .01, !Number.isFinite(this.pixelScale) || this.pixelScale <= 0) throw RangeError("pixelScale must be > 0");
258
+ this.occlude = n.occlude ?? null, n.matrix ? this.setMatrix(n.matrix) : _(w(n.position ?? [
259
+ 0,
260
+ 0,
261
+ 0
262
+ ], "position"), n.quaternion ?? [
263
+ 0,
264
+ 0,
265
+ 0,
266
+ 1
267
+ ], [
268
+ 1,
269
+ 1,
270
+ 1
271
+ ], this.own), t.style.pointerEvents = n.pointerEvents ?? "none";
272
+ }
273
+ setMesh(e) {
274
+ return this.meshValue = e, this;
275
+ }
276
+ setMatrix(e) {
277
+ if (!e || e.length < 16) throw RangeError("Expected a 16-element matrix");
278
+ for (let t = 0; t < 16; t++) if (!Number.isFinite(e[t])) throw RangeError("Matrix must be finite");
279
+ for (let t = 0; t < 16; t++) this.own[t] = e[t];
280
+ return this.meshValue = null, this;
281
+ }
282
+ setPose(e, t = [
283
+ 0,
284
+ 0,
285
+ 0,
286
+ 1
287
+ ]) {
288
+ return _(w(e, "position"), t, [
289
+ 1,
290
+ 1,
291
+ 1
292
+ ], this.own), this.meshValue = null, this;
293
+ }
294
+ get ownMatrix() {
295
+ return this.own;
296
+ }
297
+ }, O = class t {coreRequirement=__margoCoreRequirement("1.0.0",import.meta.url);
298
+ static attach(e, i) {__margoAssertCore(e,__margoCoreRequirement("1.0.0",import.meta.url));
299
+ let a = i?.name ?? "margoengine-labels";
300
+ if (r(e).plugins.has(a)) throw n("state", `A plugin named "${a}" is already attached; reuse its LabelRenderer or pass an unused name in LabelRenderer.attach(engine, { name: 'annotations' })`);
301
+ let o = new t(i);
302
+ return e.use(o), o;
303
+ }
304
+ constructor(t = {}) {
305
+ this.root = null, this.engine = null, this.labels = /* @__PURE__ */ new Set(), this.objects = /* @__PURE__ */ new Set(), this.viewer = null, this.cameraElement = null, this.rootCache = {}, this.viewerCache = {}, this.cameraCache = {}, this.cssView = /* @__PURE__ */ new Float64Array(16), this.point = a(), this.depths = /* @__PURE__ */ new Float64Array(4), this.visibleLabels = [], this.shownLabels = [], this.layoutContext = {
306
+ width: 0,
307
+ height: 0,
308
+ root: null,
309
+ size: (e) => this.measure(e),
310
+ hide: (e) => {
311
+ e.layoutHidden = !0;
312
+ },
313
+ place: (t, n) => {
314
+ if (!n || !Number.isFinite(n[0]) || !Number.isFinite(n[1])) throw e("place(label, anchor) takes two finite numbers", "labels");
315
+ t.layoutAnchor = [n[0], n[1]];
316
+ }
317
+ }, this.world = [
318
+ 0,
319
+ 0,
320
+ 0
321
+ ], this.left = 0, this.top = 0, this.rootScale = 1, this.writes = 0, this.options = t, this.view = t.view ?? null, this.name = t.name ?? "margoengine-labels", this.order = t.order ?? 1e3, this.occlude = t.occlude ?? null, this.layout = t.layout ?? null;
322
+ }
323
+ setup(e) {
324
+ this.engine = e;
325
+ let t = e.canvas, n = t.ownerDocument, r = n.createElement("div");
326
+ this.options.className && (r.className = this.options.className);
327
+ let i = r.style;
328
+ i.position = "absolute", i.pointerEvents = "none", i.overflow = "hidden", i.zIndex = String(this.options.zIndex ?? 1), i.left = "0px", i.top = "0px", i.margin = "0", i.padding = "0", i.transformOrigin = "0 0", r.dataset.margoengineLabels = "";
329
+ let a = this.options.parent ?? t.parentElement ?? n.body;
330
+ a === t.parentElement ? a.insertBefore(r, t.nextSibling) : a.appendChild(r), this.root = r, this.left = 0, this.top = 0, this.rootScale = 1;
331
+ for (let e of this.labels) this.mount(e);
332
+ for (let e of this.objects) this.mount(e);
333
+ }
334
+ registerPasses(e) {
335
+ e.add({
336
+ name: this.name,
337
+ order: this.order,
338
+ execute: () => this.update()
339
+ });
340
+ }
341
+ add(e, t = {}) {
342
+ let n = new E(this, e, t);
343
+ return this.labels.add(n), this.mount(n), n;
344
+ }
345
+ add3D(e, t = {}) {
346
+ let n = new D(this, e, t);
347
+ return this.objects.add(n), this.mount(n), n;
348
+ }
349
+ remove(e) {
350
+ e.owner === this && (e.owner = null, this.labels.delete(e), this.objects.delete(e), e.element.remove());
351
+ }
352
+ get size() {
353
+ return this.labels.size + this.objects.size;
354
+ }
355
+ update() {
356
+ let e = this.engine, t = this.root;
357
+ if (this.writes = 0, !e || e.disposed || !t) return;
358
+ let n = e.canvas, a = this.view, c = a?.camera ?? e.camera, l = e.backend, u = n.clientWidth || n.width, d = n.clientHeight || n.height, [f, p, m, h] = a?.viewport ?? [
359
+ 0,
360
+ 0,
361
+ 1,
362
+ 1
363
+ ], g = a?.pixels ? u / n.width : u, _ = a?.pixels ? d / n.height : d, v = m * g, y = h * _, x = r(e), S = x.store, C = S.matrices;
364
+ this.syncRoot(n, f * g, p * _, v, y);
365
+ let w = this.visibleLabels, T = this.shownLabels;
366
+ w.length = 0, T.length = 0;
367
+ let [E, D, O] = c.position, k = c.matrix, A = o(k) ? 1 / (Math.hypot(k[1], k[5], k[9]) * Math.tan(c.fov * Math.PI / 360)) : 0;
368
+ for (let e of this.labels) {
369
+ let t = e.visible, n = e.mesh, r = e.world;
370
+ if (t && n) {
371
+ let a = i(n).id;
372
+ if (x.meshes.get(a) !== n || e.followVisibility && b(S, a)) t = !1;
373
+ else {
374
+ let t = a * 16, [n, i, o] = e.offset;
375
+ e.offsetSpace === "world" ? (r[0] = C[t + 12] + n, r[1] = C[t + 13] + i, r[2] = C[t + 14] + o) : (r[0] = C[t] * n + C[t + 4] * i + C[t + 8] * o + C[t + 12], r[1] = C[t + 1] * n + C[t + 5] * i + C[t + 9] * o + C[t + 13], r[2] = C[t + 2] * n + C[t + 6] * i + C[t + 10] * o + C[t + 14]);
376
+ }
377
+ } else if (t) {
378
+ let t = e.position, n = e.offset;
379
+ r[0] = t[0] + n[0], r[1] = t[1] + n[1], r[2] = t[2] + n[2];
380
+ }
381
+ let a = null;
382
+ if (t && (a = s(c.matrix, r[0], r[1], r[2], v, y, l, e.screen), (!a.inFront || e.frustumCulling && !a.inFrustum) && (t = !1)), t) {
383
+ e.distance = Math.hypot(r[0] - E, r[1] - D, r[2] - O);
384
+ let n = e.occlude ?? this.occlude;
385
+ n && n(e, {
386
+ x: a.x,
387
+ y: a.y,
388
+ depth: a.depth,
389
+ distance: e.distance,
390
+ world: [
391
+ r[0],
392
+ r[1],
393
+ r[2]
394
+ ]
395
+ }) && (t = !1);
396
+ }
397
+ if (e.onScreen = t, e.layoutHidden = !1, e.layoutAnchor = null, !t) {
398
+ this.set(e.element, e.cache, "display", "none");
399
+ continue;
400
+ }
401
+ let o = 1;
402
+ if (e.distanceScale !== null) {
403
+ let t = typeof e.distanceScale == "number" ? { reference: e.distanceScale } : e.distanceScale;
404
+ o = Math.round(Math.min(t.max ?? Infinity, Math.max(t.min ?? 0, t.reference / Math.max(A || e.distance, 1e-6))) * 1e4) / 1e4;
405
+ }
406
+ e.scale = o, T.push(e);
407
+ }
408
+ let j = this.layout;
409
+ if (j) {
410
+ let e = this.layoutContext;
411
+ e.width = v, e.height = y, e.root = t, j.arrange(T, e);
412
+ }
413
+ for (let e of T) {
414
+ if (e.layoutHidden) {
415
+ e.onScreen = !1, this.set(e.element, e.cache, "display", "none");
416
+ continue;
417
+ }
418
+ let t = e.layoutAnchor ?? e.anchor, n = e.placement, r = Math.round((e.screen.x + e.screenOffset[0]) * 100) / 100, i = Math.round((e.screen.y + e.screenOffset[1]) * 100) / 100, a = t[0], o = t[1], s = +(e.distanceScale !== null || e.scale !== 1), c = s ? e.scale : 0;
419
+ e.placedAnchor[0] = a, e.placedAnchor[1] = o, (n[0] !== r || n[1] !== i || n[2] !== s || n[3] !== c || n[4] !== a || n[5] !== o) && (n[0] = r, n[1] = i, n[2] = s, n[3] = c, n[4] = a, n[5] = o, this.set(e.element, e.cache, "transform", `translate(${r}px,${i}px)${s ? ` scale(${c})` : ""} translate(${-a * 100}%,${-o * 100}%)`)), this.set(e.element, e.cache, "display", ""), e.sort && w.push(e);
420
+ }
421
+ w.sort((e, t) => t.distance - e.distance);
422
+ for (let e = 0; e < w.length; e++) this.set(w[e].element, w[e].cache, "zIndex", String(e + 1));
423
+ this.objects.size && this.update3D(c, v, y, x, l);
424
+ }
425
+ update3D(e, t, n, r, a) {
426
+ let c = r.store, l = c.matrices, u = this.viewer, g = this.cameraElement, _ = o(e.matrix), v = f(e.fov, n);
427
+ this.set(u, this.viewerCache, "perspective", _ ? "none" : S(v)), this.set(g, this.cameraCache, "width", S(t)), this.set(g, this.cameraCache, "height", S(n));
428
+ let y = _ ? this.cssView : d(e.position, e.target, this.cssView, e.up);
429
+ this.set(g, this.cameraCache, "transform", _ ? m(e.matrix, t, n, a) : p(y, v, t, n));
430
+ for (let o of this.objects) {
431
+ let u = o.visible, d = o.mesh, f = o.matrix;
432
+ if (u && d) {
433
+ let e = i(d).id;
434
+ if (r.meshes.get(e) !== d || o.followVisibility && b(c, e)) u = !1;
435
+ else for (let t = 0; t < 16; t++) f[t] = l[e * 16 + t];
436
+ } else u && f.set(o.ownMatrix);
437
+ let p = "";
438
+ if (u) {
439
+ let [r, i] = this.measure(o), c = o.pixelScale, l = this.depths, d = Infinity, m = -Infinity;
440
+ for (let o = 0; o < 4; o++) {
441
+ let u = (o === 1 || o === 2 ? .5 : -.5) * r * c, p = (o >= 2 ? -.5 : .5) * i * c, h = f[0] * u + f[4] * p + f[12], g = f[1] * u + f[5] * p + f[13], v = f[2] * u + f[6] * p + f[14], b = _ ? s(e.matrix, h, g, v, t, n, a, this.point).depth : -(y[2] * h + y[6] * g + y[10] * v + y[14]) - e.near;
442
+ l[o] = b, d = Math.min(d, b), m = Math.max(m, b);
443
+ }
444
+ m > 0 ? d < 0 && (p = this.nearClip(r, i)) : u = !1, o.distance = Math.hypot(f[12] - e.position[0], f[13] - e.position[1], f[14] - e.position[2]);
445
+ }
446
+ if (u && o.occlude) {
447
+ let r = s(e.matrix, f[12], f[13], f[14], t, n, a, this.point);
448
+ u = !o.occlude(o, {
449
+ x: r.x,
450
+ y: r.y,
451
+ depth: r.depth,
452
+ distance: o.distance,
453
+ world: [
454
+ f[12],
455
+ f[13],
456
+ f[14]
457
+ ]
458
+ });
459
+ }
460
+ o.onScreen = u, o.clipped = u && p !== "", u ? ((p || o.cache.clipPath) && this.set(o.element, o.cache, "clipPath", p), this.set(o.element, o.cache, "transform", h(f, o.pixelScale)), this.set(o.element, o.cache, "display", "")) : this.set(o.element, o.cache, "display", "none");
461
+ }
462
+ }
463
+ dispose() {
464
+ let e = this.engine;
465
+ this.engine = null, e && !e.disposed && e.sceneOf(this)?.removePlugin(this.name);
466
+ for (let e of [...this.labels, ...this.objects]) this.remove(e);
467
+ this.root?.remove(), this.root = null, this.viewer = null, this.cameraElement = null;
468
+ for (let e of [
469
+ this.rootCache,
470
+ this.viewerCache,
471
+ this.cameraCache
472
+ ]) for (let t of Object.keys(e)) delete e[t];
473
+ this.engine = null;
474
+ }
475
+ [Symbol.dispose ?? Symbol()]() {
476
+ this.dispose();
477
+ }
478
+ measure(e) {
479
+ return e.measured ? e.measured : (this.set(e.element, e.cache, "display", ""), e.measured = [e.element.offsetWidth || 0, e.element.offsetHeight || 0]);
480
+ }
481
+ nearClip(e, t) {
482
+ let n = this.depths, r = "";
483
+ for (let i = 0; i < 4; i++) {
484
+ let a = i + 1 & 3, o = n[i], s = n[a], c = i === 1 || i === 2 ? e : 0, l = i >= 2 ? t : 0, u = a === 1 || a === 2 ? e : 0, d = a >= 2 ? t : 0;
485
+ if (o >= 0 && (r += `${r ? "," : ""}${S(c)} ${S(l)}`), o >= 0 != s >= 0) {
486
+ let e = o / (o - s);
487
+ r += `${r ? "," : ""}${S(c + (u - c) * e)} ${S(l + (d - l) * e)}`;
488
+ }
489
+ }
490
+ return `polygon(${r})`;
491
+ }
492
+ set(e, t, n, r) {
493
+ t[n] !== r && (x(e, t, n, r), this.writes++);
494
+ }
495
+ syncRoot(e, t, n, r, i) {
496
+ let a = this.root;
497
+ this.set(a, this.rootCache, "width", S(r)), this.set(a, this.rootCache, "height", S(i));
498
+ let o = e.getBoundingClientRect(), s = a.getBoundingClientRect(), c = Number.parseFloat(this.rootCache.width), l = s.width > 0 && c > 0 ? s.width / (c * this.rootScale) : 1, u = a.parentElement === e.parentElement, d = !u && o.width > 0 && e.offsetWidth > 0 ? o.width / e.offsetWidth : l, f = u ? 1 : Math.round(d / l * 1e6) / 1e6, p = (o.left + (e.clientLeft + t) * d - s.left) / l, m = (o.top + (e.clientTop + n) * d - s.top) / l;
499
+ Math.abs(p) > .01 && (this.left += p), Math.abs(m) > .01 && (this.top += m), this.set(a, this.rootCache, "left", S(this.left)), this.set(a, this.rootCache, "top", S(this.top)), this.rootScale = f, (f !== 1 || this.rootCache.transform) && this.set(a, this.rootCache, "transform", f === 1 ? "" : `scale(${f})`);
500
+ }
501
+ mount(e) {
502
+ let t = this.root;
503
+ if (!t) return;
504
+ let n = e.element, r = n.style;
505
+ if (r.position = "absolute", r.left = "0px", r.top = "0px", e instanceof E) r.transformOrigin = "0 0", x(n, e.cache, "display", "none"), t.appendChild(n);
506
+ else {
507
+ if (!this.viewer) {
508
+ let e = t.ownerDocument, n = e.createElement("div"), r = e.createElement("div");
509
+ n.style.position = "absolute", n.style.left = "0px", n.style.top = "0px", n.style.width = "100%", n.style.height = "100%", n.style.overflow = "hidden", n.style.pointerEvents = "none", r.style.position = "absolute", r.style.left = "0px", r.style.top = "0px", r.style.transformStyle = "preserve-3d", r.style.pointerEvents = "none", n.appendChild(r), t.insertBefore(n, t.firstChild), this.viewer = n, this.cameraElement = r;
510
+ }
511
+ x(n, e.cache, "display", "none"), this.cameraElement.appendChild(n);
512
+ }
513
+ }
514
+ }, k = class t {
515
+ constructor(t, n) {
516
+ this.source = t, this.point = a();
517
+ let r = n.barycentric ?? [
518
+ 1 / 3,
519
+ 1 / 3,
520
+ 1 / 3
521
+ ];
522
+ if (!Number.isInteger(n.face) || n.face < 0 || r.length !== 3 || !r.every((e) => Number.isFinite(e) && e >= -1e-6) || Math.abs(r[0] + r[1] + r[2] - 1) > .001) throw e("Hotspot needs an integer face of 0 or more and three barycentric weights of 0 or more that sum to 1; fix the options", "labels");
523
+ this.primitiveIndex = n.primitiveIndex ?? 0, this.face = n.face, this.barycentric = [
524
+ r[0],
525
+ r[1],
526
+ r[2]
527
+ ], this.snapshot();
528
+ }
529
+ static fromHit(e, n) {
530
+ return new t(e, {
531
+ primitiveIndex: n.primitiveIndex,
532
+ face: n.faceIndex,
533
+ barycentric: [
534
+ n.barycoord[0],
535
+ n.barycoord[1],
536
+ n.barycoord[2]
537
+ ]
538
+ });
539
+ }
540
+ snapshot() {
541
+ let t = this.source.posePickingGeometry().find((e, t) => (e.primitiveIndex ?? t) === this.primitiveIndex);
542
+ if (!t || this.face * 3 + 2 >= t.indices.length) throw e("Hotspot primitive or face is not in the posed mesh; pass a primitiveIndex and face that exist", "labels");
543
+ return t;
544
+ }
545
+ worldPosition(e = [
546
+ 0,
547
+ 0,
548
+ 0
549
+ ]) {
550
+ let t = this.snapshot(), n = t.matrix, r = 0, i = 0, a = 0;
551
+ for (let e = 0; e < 3; e++) {
552
+ let n = t.indices[this.face * 3 + e] * 20, o = this.barycentric[e];
553
+ r += o * t.vertices[n], i += o * t.vertices[n + 1], a += o * t.vertices[n + 2];
554
+ }
555
+ return e[0] = n[0] * r + n[4] * i + n[8] * a + n[12], e[1] = n[1] * r + n[5] * i + n[9] * a + n[13], e[2] = n[2] * r + n[6] * i + n[10] * a + n[14], e;
556
+ }
557
+ project(e, t, n, r = "webgpu") {
558
+ let [i, a, o] = this.worldPosition();
559
+ return s(e, i, a, o, t, n, r, this.point);
560
+ }
561
+ pin(e) {
562
+ e.setPosition(this.worldPosition());
563
+ }
564
+ };
565
+ //#endregion
566
+ export { D as CSS3DObject, k as Hotspot, E as Label, O as LabelRenderer, y as boxAnchorPoint, p as cameraCSSTransform, _ as composeMatrix, f as cssPerspective, g as cssPointToWorld, d as lookAtView, h as objectCSSTransform, m as orthographicCSSTransform };