@algorisys/tinyfly 0.82.0 → 0.87.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.
Files changed (71) hide show
  1. package/README.md +13 -4
  2. package/bin/tinyfly.mjs +44 -6
  3. package/lib/addons/adapters.js +411 -248
  4. package/lib/addons/characters.js +1652 -1470
  5. package/lib/addons/dom-adapter-Cphr2HZn.js +372 -0
  6. package/lib/addons/gsap-compat.js +197 -187
  7. package/lib/addons/load-scene-vp14_Jj-.js +324 -0
  8. package/lib/addons/maps.js +1 -1
  9. package/lib/addons/scene-3d-webgl.js +240 -0
  10. package/lib/addons/scene-3d.js +306 -0
  11. package/lib/addons/shading-o5T_v9DT.js +89 -0
  12. package/lib/addons/{sketch-CA8ZXhxI.js → sketch-CYPUbk3Q.js} +2 -2
  13. package/lib/browser/tinyfly-scene-3d-webgl.iife.js +116 -0
  14. package/lib/browser/tinyfly-scene-3d.iife.js +3 -0
  15. package/lib/browser/tinyfly.iife.js +4 -4
  16. package/lib/browser/tinyfly.js +4333 -3726
  17. package/lib/browser/tinyfly.umd.js +4 -4
  18. package/lib/cli/tools.js +137 -22
  19. package/lib/embed/tinyfly-embed.iife.js +4 -4
  20. package/lib/embed/tinyfly-embed.js +921 -833
  21. package/lib/engine/tinyfly-engine.js +1022 -776
  22. package/lib/engine/tinyfly-engine.umd.cjs +2 -2
  23. package/lib/headless/headless.js +410 -8
  24. package/lib/player/tinyfly-player.es.js +459 -371
  25. package/lib/player/tinyfly-player.iife.js +2 -2
  26. package/lib/player/tinyfly-player.umd.js +2 -2
  27. package/lib/types/adapters/canvas/bloom.d.ts +33 -0
  28. package/lib/types/adapters/canvas/canvas-adapter.d.ts +36 -0
  29. package/lib/types/adapters/canvas/index.d.ts +2 -0
  30. package/lib/types/adapters/canvas/offscreen.d.ts +13 -0
  31. package/lib/types/adapters/transform-3d.d.ts +99 -0
  32. package/lib/types/adapters/webgl/webgl-adapter.d.ts +17 -0
  33. package/lib/types/browser/index.d.ts +2 -0
  34. package/lib/types/characters/character.d.ts +20 -1
  35. package/lib/types/characters/index.d.ts +2 -0
  36. package/lib/types/characters/rig/body-plan.d.ts +3 -1
  37. package/lib/types/characters/rig/skeleton.d.ts +57 -0
  38. package/lib/types/characters/scene-objects.d.ts +2 -0
  39. package/lib/types/compat/gsap/vars.d.ts +8 -0
  40. package/lib/types/engine/core/bake.d.ts +3 -1
  41. package/lib/types/engine/index.d.ts +1 -0
  42. package/lib/types/engine/interpolation/interpolators.d.ts +10 -3
  43. package/lib/types/engine/math/index.d.ts +11 -0
  44. package/lib/types/engine/math/mat4.d.ts +37 -0
  45. package/lib/types/engine/math/quat.d.ts +39 -0
  46. package/lib/types/engine/math/vec3.d.ts +16 -0
  47. package/lib/types/engine/types.d.ts +23 -2
  48. package/lib/types/headless/index.d.ts +1 -0
  49. package/lib/types/headless/loop-check.d.ts +41 -0
  50. package/lib/types/headless/node-video.d.ts +8 -0
  51. package/lib/types/headless/video-scene.d.ts +7 -1
  52. package/lib/types/scene-3d/camera.d.ts +27 -0
  53. package/lib/types/scene-3d/canvas-2d-renderer.d.ts +21 -0
  54. package/lib/types/scene-3d/geometry/extrude.d.ts +17 -0
  55. package/lib/types/scene-3d/geometry/mesh.d.ts +36 -0
  56. package/lib/types/scene-3d/geometry/primitives.d.ts +16 -0
  57. package/lib/types/scene-3d/geometry/triangulate.d.ts +24 -0
  58. package/lib/types/scene-3d/index.d.ts +20 -0
  59. package/lib/types/scene-3d/load-scene.d.ts +31 -0
  60. package/lib/types/scene-3d/object-kind.d.ts +54 -0
  61. package/lib/types/scene-3d/resolve-scene.d.ts +122 -0
  62. package/lib/types/scene-3d/scene-3d-adapter.d.ts +46 -0
  63. package/lib/types/scene-3d/scene-types.d.ts +192 -0
  64. package/lib/types/scene-3d/shading.d.ts +20 -0
  65. package/lib/types/scene-3d/validate-scene.d.ts +11 -0
  66. package/lib/types/scene-3d/webgl/gl-data.d.ts +29 -0
  67. package/lib/types/scene-3d/webgl/index.d.ts +16 -0
  68. package/lib/types/scene-3d/webgl/shaders.d.ts +29 -0
  69. package/lib/types/scene-3d/webgl/webgl2-renderer.d.ts +33 -0
  70. package/package.json +10 -2
  71. package/lib/addons/dom-adapter-CrNwoomd.js +0 -243
@@ -0,0 +1,306 @@
1
+ import { M as at, b as ct, c as lt, a as dt, e as ht, g as gt, l as pt, m as ft, p as ut, d as yt, f as wt, s as mt, h as vt, t as Mt, i as bt, v as kt } from "./load-scene-vp14_Jj-.js";
2
+ import { vec3 as h, mat4 as b, quat as T } from "@algorisys/tinyfly";
3
+ import { s as B } from "./shading-o5T_v9DT.js";
4
+ import { f as Pt, l as St, p as At } from "./shading-o5T_v9DT.js";
5
+ function F(t, e) {
6
+ const i = h.normalize(h.subtract(e, t)), n = Math.abs(i[1]) > 0.9999 ? [0, 0, -1] : [0, 1, 0];
7
+ return b.lookAt(t, e, n);
8
+ }
9
+ function tt(t, e, i, n) {
10
+ const r = e * Math.PI / 180, c = i * Math.PI / 180;
11
+ return [t[0] + n * Math.cos(c) * Math.sin(r), t[1] + n * Math.sin(c), t[2] + n * Math.cos(c) * Math.cos(r)];
12
+ }
13
+ function et(t, e, i) {
14
+ const n = h.subtract(t, e), r = h.length(n);
15
+ if (r === 0) return t;
16
+ const c = Math.max(1e-3, r - i);
17
+ return h.add(e, h.scale(n, c / r));
18
+ }
19
+ function Y(t, e) {
20
+ if (t.projection === "perspective") return b.perspective(t.fov, e, t.near, t.far);
21
+ const i = t.height / 2;
22
+ return b.orthographic(-i * e, i * e, -i, i, t.near, t.far);
23
+ }
24
+ const H = Math.cos(30 * Math.PI / 180), J = /* @__PURE__ */ new Map(), v = (t) => typeof t == "number" ? t : void 0, z = (t, e) => Array.isArray(t) && t.length === e ? t : void 0;
25
+ function K(t, e) {
26
+ const i = t.position ?? [0, 0, 0], n = z(e.get("position"), 3) ?? [
27
+ v(e.get("x")) ?? i[0],
28
+ v(e.get("y")) ?? i[1],
29
+ v(e.get("z")) ?? i[2]
30
+ ];
31
+ let r;
32
+ const c = z(e.get("quaternion"), 4), s = ["rotateX", "rotateY", "rotateZ"].map((u) => v(e.get(u)));
33
+ if (c) r = T.normalize(c);
34
+ else if (s.some((u) => u !== void 0)) {
35
+ const u = t.rotation ?? [0, 0, 0];
36
+ r = T.fromEuler(s[0] ?? u[0], s[1] ?? u[1], s[2] ?? u[2]);
37
+ } else t.quaternion ? r = T.normalize(t.quaternion) : r = t.rotation ? T.fromEuler(...t.rotation) : T.identity();
38
+ const g = typeof t.scale == "number" ? [t.scale, t.scale, t.scale] : t.scale ?? [1, 1, 1], l = v(e.get("scale")), M = [
39
+ v(e.get("scaleX")) ?? l ?? g[0],
40
+ v(e.get("scaleY")) ?? l ?? g[1],
41
+ v(e.get("scaleZ")) ?? l ?? g[2]
42
+ ];
43
+ return b.compose(n, r, M);
44
+ }
45
+ const X = (t) => [t[12], t[13], t[14]];
46
+ function L(t, e, i, n) {
47
+ const r = X(e), c = z(i.get("lookAt"), 3) ?? t.lookAt, s = c ? F(r, c) : b.invert(e) ?? b.identity(), g = t.projection === "perspective" ? Y({ ...t, fov: v(i.get("fov")) ?? t.fov }, n) : Y({ ...t, height: v(i.get("height")) ?? t.height }, n);
48
+ return {
49
+ id: t.id,
50
+ view: s,
51
+ projection: g,
52
+ position: r,
53
+ forward: h.normalize([-s[2], -s[6], -s[10]]),
54
+ orthographic: t.projection === "orthographic",
55
+ near: t.near,
56
+ far: t.far
57
+ };
58
+ }
59
+ function j(t) {
60
+ return b.transpose(b.invert(t) ?? b.identity());
61
+ }
62
+ function Q(t, e, i, n) {
63
+ return h.normalize([t[0] * e + t[4] * i + t[8] * n, t[1] * e + t[5] * i + t[9] * n, t[2] * e + t[6] * i + t[10] * n]);
64
+ }
65
+ function U(t, e) {
66
+ const i = (r) => -r[2] >= e, n = [];
67
+ for (let r = 0; r < t.length; r++) {
68
+ const c = t[r], s = t[(r + 1) % t.length];
69
+ if (i(c) && n.push(c), i(c) !== i(s)) {
70
+ const g = (-e - c[2]) / (s[2] - c[2]);
71
+ n.push(h.lerp(c, s, g));
72
+ }
73
+ }
74
+ return n;
75
+ }
76
+ function Z(t, e = /* @__PURE__ */ new Map(), i) {
77
+ const { scene: n } = t, r = (o) => e.get(`${n.id}/${o}`) ?? J, c = /* @__PURE__ */ new Map(), s = /* @__PURE__ */ new Map();
78
+ for (const o of n.objects) {
79
+ const a = r(o.id), p = K(o, a), y = o.parent ? c.get(o.parent) : void 0;
80
+ c.set(o.id, y ? b.multiply(y, p) : p);
81
+ const m = v(a.get("visible")) !== void 0 ? v(a.get("visible")) > 0.5 : o.visible !== !1;
82
+ s.set(o.id, m && (o.parent ? s.get(o.parent) !== !1 : !0));
83
+ }
84
+ const g = e.get(n.id)?.get("activeCamera"), l = typeof g == "string" && n.objects.some((o) => o.id === g && o.kind === "camera") ? g : n.camera, M = n.objects.find((o) => o.id === l), u = L(M, c.get(l), r(l), i.width / i.height), k = (o) => {
85
+ const a = b.transformPoint(u.projection, o);
86
+ return { x: (a[0] + 1) / 2 * i.width, y: (1 - a[1]) / 2 * i.height };
87
+ }, P = [];
88
+ for (const o of n.objects) {
89
+ if (o.kind !== "light" || !s.get(o.id)) continue;
90
+ const a = r(o.id), p = X(c.get(o.id)), y = z(a.get("target"), 3) ?? o.target ?? [0, 0, 0], m = a.get("color");
91
+ P.push({
92
+ id: o.id,
93
+ light: o.light,
94
+ color: typeof m == "string" ? m : o.color,
95
+ intensity: v(a.get("intensity")) ?? o.intensity,
96
+ position: p,
97
+ direction: h.normalize(h.subtract(y, p)),
98
+ range: o.range,
99
+ angle: o.angle ?? 30
100
+ });
101
+ }
102
+ const I = [], S = [];
103
+ n.objects.forEach((o, a) => {
104
+ if (o.kind !== "mesh" || !s.get(o.id)) return;
105
+ const p = t.meshes.get(o.id), y = n.materials?.[o.material];
106
+ if (!p || !y) return;
107
+ const m = r(o.id), w = c.get(o.id), N = j(w), A = m.get("color"), E = (v(m.get("opacity")) ?? 1) * (y.opacity ?? 1);
108
+ E <= 0 || (S.push({ objectId: o.id, objectIndex: a, mesh: p, world: w, material: y, color: typeof A == "string" ? A : void 0, opacity: E }), I.push(...W(p, w, N, u, k, y, {
109
+ objectId: o.id,
110
+ objectIndex: a,
111
+ layer: o.layer ?? 0,
112
+ color: typeof A == "string" ? A : void 0,
113
+ opacity: E
114
+ })));
115
+ });
116
+ const d = [];
117
+ return n.objects.forEach((o, a) => {
118
+ const p = t.kinds.get(o.kind);
119
+ if (!p || !s.get(o.id)) return;
120
+ const y = p.resolve({
121
+ object: o,
122
+ prepared: t.prepared.get(o.id),
123
+ values: r(o.id),
124
+ world: c.get(o.id),
125
+ camera: u,
126
+ width: i.width,
127
+ height: i.height,
128
+ toScreen: k
129
+ });
130
+ if (!y) return;
131
+ const m = o.layer ?? 0;
132
+ for (const w of y.drawables ?? []) d.push({ ...w, objectId: o.id, objectIndex: a, layer: m });
133
+ for (const w of y.meshes ?? [])
134
+ S.push({ objectId: o.id, objectIndex: a, mesh: w.mesh, world: w.world, material: w.material, opacity: w.material.opacity ?? 1 }), I.push(...W(w.mesh, w.world, j(w.world), u, k, w.material, {
135
+ objectId: o.id,
136
+ objectIndex: a,
137
+ layer: m,
138
+ opacity: w.material.opacity ?? 1
139
+ }));
140
+ }), d.sort((o, a) => o.layer - a.layer || a.depth - o.depth || o.objectIndex - a.objectIndex), I.sort((o, a) => o.layer - a.layer || a.depth - o.depth || o.objectIndex - a.objectIndex || o.face - a.face || o.part - a.part), {
141
+ id: n.id,
142
+ width: i.width,
143
+ height: i.height,
144
+ background: n.background,
145
+ fog: n.fog,
146
+ camera: u,
147
+ lights: P,
148
+ worlds: c,
149
+ triangles: I,
150
+ drawables: d,
151
+ meshes: S,
152
+ time: i.time ?? 0
153
+ };
154
+ }
155
+ function W(t, e, i, n, r, c, s) {
156
+ const g = t.positions.length / 3, l = [], M = [];
157
+ for (let d = 0; d < g; d++) {
158
+ const o = b.transformPoint(e, [t.positions[d * 3], t.positions[d * 3 + 1], t.positions[d * 3 + 2]]);
159
+ l.push(o), M.push(b.transformPoint(n.view, o));
160
+ }
161
+ const u = t.indices.length / 3, k = [], P = [];
162
+ for (let d = 0; d < u; d++) {
163
+ const [o, a, p] = [0, 1, 2].map((w) => l[t.indices[d * 3 + w]]), y = h.normalize(h.cross(h.subtract(a, o), h.subtract(p, o)));
164
+ k.push(y);
165
+ const m = n.orthographic ? h.scale(n.forward, -1) : h.subtract(n.position, o);
166
+ P.push(h.dot(y, m) > 0);
167
+ }
168
+ const I = (d) => -M[d][2] >= n.near, S = [];
169
+ for (let d = 0; d < u; d++) {
170
+ const o = P[d];
171
+ if (!o && !c.doubleSided) continue;
172
+ const a = [t.indices[d * 3], t.indices[d * 3 + 1], t.indices[d * 3 + 2]], p = a.map((f) => M[f]);
173
+ if (p.every((f) => -f[2] > n.far)) continue;
174
+ const y = U(p, n.near);
175
+ if (y.length < 3) continue;
176
+ const m = o ? 1 : -1, w = h.scale(k[d], m), N = a.map((f) => h.scale(Q(i, t.normals[f * 3], t.normals[f * 3 + 1], t.normals[f * 3 + 2]), m)), A = h.scale(h.add(h.add(l[a[0]], l[a[1]]), l[a[2]]), 1 / 3), E = -(p[0][2] + p[1][2] + p[2][2]) / 3, O = [];
177
+ if (c.outline && o)
178
+ for (const f of t.faceEdges[d]) {
179
+ const D = f.across, R = D === -1 || !P[D], $ = !R && d < D && h.dot(k[d], k[D]) < H;
180
+ (R || $) && I(f.a) && I(f.b) && O.push([r(M[f.a]), r(M[f.b])]);
181
+ }
182
+ const C = y.map(r);
183
+ for (let f = 1; f + 1 < C.length; f++)
184
+ S.push({
185
+ objectId: s.objectId,
186
+ objectIndex: s.objectIndex,
187
+ layer: s.layer,
188
+ face: d,
189
+ part: f - 1,
190
+ screen: [C[0], C[f], C[f + 1]],
191
+ depth: E,
192
+ centroid: A,
193
+ normal: w,
194
+ vertexNormals: N,
195
+ material: c,
196
+ color: s.color,
197
+ opacity: s.opacity,
198
+ outline: f === 1 ? O : []
199
+ });
200
+ }
201
+ return S;
202
+ }
203
+ class ot {
204
+ ctx;
205
+ constructor(e) {
206
+ this.ctx = e;
207
+ }
208
+ render(e) {
209
+ _(this.ctx, e);
210
+ }
211
+ }
212
+ function _(t, e) {
213
+ t.save(), e.background && e.background !== "transparent" && (t.fillStyle = e.background, t.fillRect(0, 0, e.width, e.height)), t.lineJoin = "round", t.lineCap = "round";
214
+ let i = 0;
215
+ const n = (s, g) => {
216
+ for (; i < e.drawables.length; ) {
217
+ const l = e.drawables[i];
218
+ if (l.layer > s || l.layer === s && l.depth <= g) break;
219
+ t.save(), l.draw(t, e), t.restore(), i++;
220
+ }
221
+ };
222
+ let r = [];
223
+ const c = (s) => {
224
+ if (r.length === 0) return;
225
+ const g = [];
226
+ for (const l of r)
227
+ s && s.objectId === l.objectId && s.layer === l.layer && l.depth - s.depth <= V * l.depth ? g.push(l) : G(t, l.triangle);
228
+ r = g;
229
+ };
230
+ for (const s of e.triangles) {
231
+ c(s), n(s.layer, s.depth);
232
+ const { color: g, alpha: l } = B(s, e.lights, e.camera, e.fog), [M, u, k] = s.screen;
233
+ t.globalAlpha = l, t.beginPath(), t.moveTo(M.x, M.y), t.lineTo(u.x, u.y), t.lineTo(k.x, k.y), t.closePath(), t.fillStyle = g, t.fill(), l >= 1 && (t.strokeStyle = g, t.lineWidth = 0.75, t.stroke()), s.material.outline && s.outline.length > 0 && r.push({ objectId: s.objectId, layer: s.layer, depth: s.depth, triangle: s });
234
+ }
235
+ c(), n(1 / 0, -1 / 0), t.restore();
236
+ }
237
+ const V = 0.04;
238
+ function G(t, e) {
239
+ const i = e.material.outline;
240
+ t.globalAlpha = e.opacity, t.strokeStyle = i.color, t.lineWidth = i.width, t.beginPath();
241
+ for (const [n, r] of e.outline)
242
+ t.moveTo(n.x, n.y), t.lineTo(r.x, r.y);
243
+ t.stroke();
244
+ }
245
+ class nt {
246
+ renderer;
247
+ width;
248
+ height;
249
+ scenes = /* @__PURE__ */ new Map();
250
+ values = /* @__PURE__ */ new Map();
251
+ constructor(e, i) {
252
+ this.renderer = e, this.width = i.width, this.height = i.height;
253
+ }
254
+ /** Add a scene; its tracks address `<scene.id>/<objectId>`. */
255
+ registerScene(e) {
256
+ this.scenes.set(e.scene.id, e);
257
+ }
258
+ unregisterScene(e) {
259
+ this.scenes.delete(e);
260
+ }
261
+ resize(e, i) {
262
+ this.width = e, this.height = i;
263
+ }
264
+ applyState(e) {
265
+ this.values = e.values, this.time = e.currentTime ?? this.time;
266
+ }
267
+ time = 0;
268
+ render() {
269
+ for (const e of this.scenes.values())
270
+ this.renderer.render(Z(e, this.values, { width: this.width, height: this.height, time: this.time }));
271
+ }
272
+ }
273
+ function it(t, e, i, n) {
274
+ _(t, Z(e, i ?? /* @__PURE__ */ new Map(), { width: n.width, height: n.height, time: n.time }));
275
+ }
276
+ export {
277
+ ot as Canvas2DRenderer,
278
+ at as MeshBuilder,
279
+ nt as Scene3DAdapter,
280
+ ct as boxMesh,
281
+ lt as coneMesh,
282
+ dt as cylinderMesh,
283
+ et as dollyPosition,
284
+ _ as drawResolvedScene,
285
+ it as drawScene3D,
286
+ ht as extrudeMesh,
287
+ Pt as fogAmount,
288
+ gt as geometryMesh,
289
+ St as lightAt,
290
+ pt as loadScene3D,
291
+ F as lookAtView,
292
+ ft as meshEdges,
293
+ tt as orbitPosition,
294
+ At as parseColor,
295
+ ut as planeMesh,
296
+ yt as pointInPolygon,
297
+ wt as prepareMesh,
298
+ Y as projectionMatrix,
299
+ Z as resolveScene3D,
300
+ B as shadeTriangle,
301
+ mt as signedArea,
302
+ vt as sphereMesh,
303
+ Mt as torusMesh,
304
+ bt as triangulate,
305
+ kt as validateScene3D
306
+ };
@@ -0,0 +1,89 @@
1
+ import { vec3 as f } from "@algorisys/tinyfly";
2
+ const b = /* @__PURE__ */ new Map();
3
+ function M(n) {
4
+ const i = b.get(n);
5
+ if (i) return i;
6
+ let r = [1, 1, 1];
7
+ const o = n.trim().replace("#", "");
8
+ if (n.trim().startsWith("#") && (o.length === 3 || o.length === 6)) {
9
+ const t = o.length === 3 ? o.split("").map((s) => s + s).join("") : o;
10
+ r = [0, 2, 4].map((s) => Number.parseInt(t.slice(s, s + 2), 16) / 255);
11
+ } else {
12
+ const t = n.match(/rgba?\(\s*([\d.]+)\s*,\s*([\d.]+)\s*,\s*([\d.]+)/);
13
+ t && (r = [Number(t[1]) / 255, Number(t[2]) / 255, Number(t[3]) / 255]);
14
+ }
15
+ return b.size < 512 && b.set(n, r), r;
16
+ }
17
+ const x = [
18
+ { id: "default-ambient", light: "ambient", color: "#ffffff", intensity: 0.35, position: [0, 0, 0], direction: [0, -1, 0], angle: 30 },
19
+ { id: "default-key", light: "directional", color: "#ffffff", intensity: 0.8, position: [3, 5, 4], direction: f.normalize([-3, -5, -4]), angle: 30 }
20
+ ], y = (n, i, r) => {
21
+ const o = Math.min(1, Math.max(0, (r - n) / (i - n)));
22
+ return o * o * (3 - 2 * o);
23
+ };
24
+ function g(n, i, r) {
25
+ const o = [0, 0, 0];
26
+ for (const t of r) {
27
+ const s = M(t.color);
28
+ let l;
29
+ if (t.light === "ambient")
30
+ l = t.intensity;
31
+ else if (t.light === "directional")
32
+ l = t.intensity * Math.max(0, -f.dot(i, t.direction));
33
+ else {
34
+ const e = f.subtract(t.position, n), m = f.length(e), d = m > 0 ? f.scale(e, 1 / m) : i, a = t.range ? Math.max(0, 1 - m / t.range) ** 2 : 1;
35
+ if (l = t.intensity * Math.max(0, f.dot(i, d)) * a, t.light === "spot") {
36
+ const c = Math.cos(t.angle * Math.PI / 180), h = Math.cos(t.angle * 0.8 * Math.PI / 180);
37
+ l *= y(c, h, -f.dot(d, t.direction));
38
+ }
39
+ }
40
+ o[0] += s[0] * l, o[1] += s[1] * l, o[2] += s[2] * l;
41
+ }
42
+ return o;
43
+ }
44
+ function v(n, i) {
45
+ const r = Math.max(2, Math.round(i));
46
+ return Math.min(1.2, Math.ceil(Math.min(1, n) * r - 1e-9) / r);
47
+ }
48
+ function N(n, i) {
49
+ return n ? Math.min(1, Math.max(0, (i - n.near) / (n.far - n.near))) : 0;
50
+ }
51
+ function C(n, i, r, o) {
52
+ const t = n.material, s = M(n.color ?? t.color), l = t.shading ?? "lambert";
53
+ let e;
54
+ if (l === "unlit")
55
+ e = [...s];
56
+ else {
57
+ const a = i.length > 0 ? i : x;
58
+ let c;
59
+ if (l === "lambert") {
60
+ const h = [0, 0, 0];
61
+ for (const u of n.vertexNormals) {
62
+ const p = g(n.centroid, u, a);
63
+ h[0] += p[0] / 3, h[1] += p[1] / 3, h[2] += p[2] / 3;
64
+ }
65
+ c = h;
66
+ } else if (c = g(n.centroid, n.normal, a), l === "toon") {
67
+ const h = Math.max(c[0], c[1], c[2]), u = h > 0 ? v(h, t.bands ?? 3) / h : 0;
68
+ c = [c[0] * u, c[1] * u, c[2] * u];
69
+ }
70
+ e = [s[0] * c[0], s[1] * c[1], s[2] * c[2]];
71
+ }
72
+ if (t.emissive) {
73
+ const a = M(t.emissive);
74
+ e = [e[0] + a[0], e[1] + a[1], e[2] + a[2]];
75
+ }
76
+ const m = N(o, f.distance(n.centroid, r.position));
77
+ if (o && m > 0) {
78
+ const a = M(o.color);
79
+ e = [e[0] + (a[0] - e[0]) * m, e[1] + (a[1] - e[1]) * m, e[2] + (a[2] - e[2]) * m];
80
+ }
81
+ const d = (a) => Math.round(Math.min(1, Math.max(0, a)) * 255);
82
+ return { color: `rgb(${d(e[0])}, ${d(e[1])}, ${d(e[2])})`, alpha: n.opacity };
83
+ }
84
+ export {
85
+ N as f,
86
+ g as l,
87
+ M as p,
88
+ C as s
89
+ };
@@ -121,8 +121,8 @@ function W(t, n) {
121
121
  }
122
122
  export {
123
123
  E as a,
124
- F as b,
125
- p as c,
124
+ p as b,
125
+ F as c,
126
126
  $ as p,
127
127
  B as s
128
128
  };
@@ -0,0 +1,116 @@
1
+ (function(h){"use strict";function j(e,i){return[e[0]*i,e[1]*i,e[2]*i]}function X(e){return Math.hypot(e[0],e[1],e[2])}function B(e){const i=X(e);return i===0?[0,0,0]:j(e,1/i)}function W(){return[1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1]}function Y(e){const i=new Array(16);for(let t=0;t<4;t++)for(let o=0;o<4;o++)i[o*4+t]=e[t*4+o];return i}function z(e){const[i,t,o,r,n,l,s,u,_,a,f,m,v,p,b,w]=e,A=i*l-t*n,y=i*s-o*n,E=i*u-r*n,R=t*s-o*l,L=t*u-r*l,M=o*u-r*s,F=_*p-a*v,T=_*b-f*v,C=_*w-m*v,S=a*b-f*p,I=a*w-m*p,x=f*w-m*b,O=A*x-y*I+E*S+R*C-L*T+M*F;if(Math.abs(O)<1e-12)return null;const c=1/O;return[(l*x-s*I+u*S)*c,(o*I-t*x-r*S)*c,(p*M-b*L+w*R)*c,(f*L-a*M-m*R)*c,(s*C-n*x-u*T)*c,(i*x-o*C+r*T)*c,(b*E-v*M-w*y)*c,(_*M-f*E+m*y)*c,(n*I-l*C+u*F)*c,(t*C-i*I-r*F)*c,(v*L-p*E+w*A)*c,(a*E-_*L-m*A)*c,(l*T-n*S-s*F)*c,(i*S-t*T+o*F)*c,(p*y-v*R-b*A)*c,(_*R-a*y+f*A)*c]}const N=new Map;function g(e){const i=N.get(e);if(i)return i;let t=[1,1,1];const o=e.trim().replace("#","");if(e.trim().startsWith("#")&&(o.length===3||o.length===6)){const r=o.length===3?o.split("").map(n=>n+n).join(""):o;t=[0,2,4].map(n=>Number.parseInt(r.slice(n,n+2),16)/255)}else{const r=e.match(/rgba?\(\s*([\d.]+)\s*,\s*([\d.]+)\s*,\s*([\d.]+)/);r&&(t=[Number(r[1])/255,Number(r[2])/255,Number(r[3])/255])}return N.size<512&&N.set(e,t),t}B([-3,-5,-4]);const d=8,P={unlit:0,flat:1,lambert:2,toon:3},k={ambient:0,directional:1,point:2,spot:3},K=`#version 300 es
2
+ in vec3 a_position;
3
+ in vec3 a_normal;
4
+ uniform mat4 u_model;
5
+ uniform mat4 u_view;
6
+ uniform mat4 u_projection;
7
+ uniform mat3 u_normalMatrix;
8
+ out vec3 v_world;
9
+ out vec3 v_normal;
10
+ void main() {
11
+ vec4 world = u_model * vec4(a_position, 1.0);
12
+ v_world = world.xyz;
13
+ v_normal = u_normalMatrix * a_normal;
14
+ gl_Position = u_projection * u_view * world;
15
+ }
16
+ `,V=`#version 300 es
17
+ precision highp float;
18
+ #define MAX_LIGHTS ${d}
19
+ uniform int u_lightCount;
20
+ uniform int u_lightKind[MAX_LIGHTS];
21
+ uniform vec3 u_lightColor[MAX_LIGHTS];
22
+ uniform float u_lightIntensity[MAX_LIGHTS];
23
+ uniform vec3 u_lightPosition[MAX_LIGHTS];
24
+ uniform vec3 u_lightDirection[MAX_LIGHTS];
25
+ uniform float u_lightRange[MAX_LIGHTS];
26
+ uniform vec2 u_lightCone[MAX_LIGHTS];
27
+ uniform vec3 u_color;
28
+ uniform vec3 u_emissive;
29
+ uniform float u_opacity;
30
+ uniform int u_shading;
31
+ uniform float u_bands;
32
+ uniform vec3 u_eye;
33
+ uniform vec3 u_forward;
34
+ uniform bool u_orthographic;
35
+ uniform bool u_fog;
36
+ uniform vec3 u_fogColor;
37
+ uniform vec2 u_fogRange;
38
+ in vec3 v_world;
39
+ in vec3 v_normal;
40
+ out vec4 outColor;
41
+
42
+ vec3 lightAt(vec3 p, vec3 n) {
43
+ vec3 total = vec3(0.0);
44
+ for (int i = 0; i < MAX_LIGHTS; i++) {
45
+ if (i >= u_lightCount) break;
46
+ float amount;
47
+ int kind = u_lightKind[i];
48
+ if (kind == 0) {
49
+ amount = u_lightIntensity[i];
50
+ } else if (kind == 1) {
51
+ amount = u_lightIntensity[i] * max(0.0, -dot(n, u_lightDirection[i]));
52
+ } else {
53
+ vec3 toLight = u_lightPosition[i] - p;
54
+ float distance = length(toLight);
55
+ vec3 l = distance > 0.0 ? toLight / distance : n;
56
+ float fade = u_lightRange[i] > 0.0 ? pow(max(0.0, 1.0 - distance / u_lightRange[i]), 2.0) : 1.0;
57
+ amount = u_lightIntensity[i] * max(0.0, dot(n, l)) * fade;
58
+ if (kind == 3) amount *= smoothstep(u_lightCone[i].x, u_lightCone[i].y, -dot(l, u_lightDirection[i]));
59
+ }
60
+ total += u_lightColor[i] * amount;
61
+ }
62
+ return total;
63
+ }
64
+
65
+ void main() {
66
+ vec3 rgb = u_color;
67
+ if (u_shading != 0) {
68
+ // Flat: the face's own normal, from the surface's slope; otherwise the smooth one.
69
+ vec3 n = u_shading == 1 ? normalize(cross(dFdx(v_world), dFdy(v_world))) : normalize(v_normal);
70
+ vec3 toEye = u_orthographic ? -u_forward : normalize(u_eye - v_world);
71
+ if (dot(n, toEye) < 0.0) n = -n;
72
+ vec3 light = lightAt(v_world, n);
73
+ if (u_shading == 3) {
74
+ // Band the brightness, not each channel, so coloured lights keep their hue.
75
+ float level = max(light.r, max(light.g, light.b));
76
+ float steps = max(2.0, floor(u_bands + 0.5));
77
+ float banded = min(1.2, ceil(min(1.0, level) * steps - 1e-6) / steps);
78
+ light = level > 0.0 ? light * (banded / level) : vec3(0.0);
79
+ }
80
+ rgb *= light;
81
+ }
82
+ rgb += u_emissive;
83
+ if (u_fog) {
84
+ float amount = clamp((distance(v_world, u_eye) - u_fogRange.x) / (u_fogRange.y - u_fogRange.x), 0.0, 1.0);
85
+ rgb = mix(rgb, u_fogColor, amount);
86
+ }
87
+ outColor = vec4(clamp(rgb, 0.0, 1.0), u_opacity);
88
+ }
89
+ `,$=`#version 300 es
90
+ in vec3 a_position;
91
+ in vec3 a_normal;
92
+ uniform mat4 u_model;
93
+ uniform mat4 u_view;
94
+ uniform mat4 u_projection;
95
+ uniform mat3 u_normalMatrix;
96
+ uniform vec2 u_viewport;
97
+ uniform float u_width;
98
+ void main() {
99
+ mat4 viewProjection = u_projection * u_view;
100
+ vec4 clip = viewProjection * u_model * vec4(a_position, 1.0);
101
+ vec3 worldNormal = normalize(u_normalMatrix * a_normal);
102
+ vec4 tip = viewProjection * (u_model * vec4(a_position, 1.0) + vec4(worldNormal * 0.01, 0.0));
103
+ vec2 direction = tip.xy / tip.w - clip.xy / clip.w;
104
+ float length2 = dot(direction, direction);
105
+ vec2 push = length2 > 0.0 ? direction * inversesqrt(length2) : vec2(0.0);
106
+ clip.xy += push * u_width * 2.0 / u_viewport * clip.w;
107
+ gl_Position = clip;
108
+ }
109
+ `,q=`#version 300 es
110
+ precision highp float;
111
+ uniform vec4 u_ink;
112
+ out vec4 outColor;
113
+ void main() {
114
+ outColor = u_ink;
115
+ }
116
+ `,Q=[{id:"default-ambient",light:"ambient",color:"#ffffff",intensity:.35,position:[0,0,0],direction:[0,-1,0],angle:30},{id:"default-key",light:"directional",color:"#ffffff",intensity:.8,position:[3,5,4],direction:B([-3,-5,-4]),angle:30}];function D(e){const i=(e.length>0?e:Q).slice(0,d),t={count:i.length,kind:new Int32Array(d),color:new Float32Array(d*3),intensity:new Float32Array(d),position:new Float32Array(d*3),direction:new Float32Array(d*3),range:new Float32Array(d),cone:new Float32Array(d*2)};return i.forEach((o,r)=>{t.kind[r]=k[o.light],t.color.set(g(o.color),r*3),t.intensity[r]=o.intensity,t.position.set(o.position,r*3),t.direction.set(o.direction,r*3),t.range[r]=o.range??0,t.cone[r*2]=Math.cos(o.angle*Math.PI/180),t.cone[r*2+1]=Math.cos(o.angle*.8*Math.PI/180)}),t}function G(e,i,t){return{color:g(i??e.color),emissive:e.emissive?g(e.emissive):[0,0,0],opacity:t,shading:P[e.shading??"lambert"],bands:e.bands??3}}function U(e){const i=Y(z(e)??W());return new Float32Array([i[0],i[1],i[2],i[4],i[5],i[6],i[8],i[9],i[10]])}class J{gl;surface;outline;buffers=new WeakMap;overlay;constructor(i,t={}){this.gl=i,this.overlay=t.overlay,this.surface=H(i,K,V),this.outline=H(i,$,q)}render(i){const t=this.gl,o=t.drawingBufferWidth,r=t.drawingBufferHeight;t.viewport(0,0,o,r);const n=i.background&&i.background!=="transparent"?g(i.background):null;t.clearColor(n?.[0]??0,n?.[1]??0,n?.[2]??0,n?1:0),t.clearDepth(1),t.clear(t.COLOR_BUFFER_BIT|t.DEPTH_BUFFER_BIT),t.enable(t.DEPTH_TEST),t.depthFunc(t.LEQUAL),t.enable(t.CULL_FACE),t.frontFace(t.CCW);const l=i.meshes.filter(a=>a.opacity>=1),s=i.camera.position,u=a=>Math.hypot(a.world[12]-s[0],a.world[13]-s[1],a.world[14]-s[2]),_=i.meshes.filter(a=>a.opacity<1).sort((a,f)=>u(f)-u(a)||a.objectIndex-f.objectIndex);t.disable(t.BLEND),t.depthMask(!0);for(const a of l)a.material.outline&&this.drawOutline(i,a,o,r);for(const a of l)this.drawSurface(i,a);t.enable(t.BLEND),t.blendFunc(t.SRC_ALPHA,t.ONE_MINUS_SRC_ALPHA),t.depthMask(!1);for(const a of _)this.drawSurface(i,a);if(t.depthMask(!0),t.disable(t.BLEND),this.overlay&&i.drawables.length>0){const a=this.overlay;a.save(),a.setTransform(a.canvas.width/i.width,0,0,a.canvas.height/i.height,0,0),a.clearRect(0,0,i.width,i.height);for(const f of i.drawables)a.save(),f.draw(a,i),a.restore();a.restore()}else this.overlay&&this.overlay.clearRect(0,0,this.overlay.canvas.width,this.overlay.canvas.height)}drawSurface(i,t){const o=this.gl,r=this.surface;o.useProgram(r.program),t.material.doubleSided?o.disable(o.CULL_FACE):(o.enable(o.CULL_FACE),o.cullFace(o.BACK)),this.bindMesh(r,t.mesh);const n=r.uniform;o.uniformMatrix4fv(n("u_model"),!1,t.world),o.uniformMatrix4fv(n("u_view"),!1,i.camera.view),o.uniformMatrix4fv(n("u_projection"),!1,i.camera.projection),o.uniformMatrix3fv(n("u_normalMatrix"),!1,U(t.world));const l=D(i.lights);o.uniform1i(n("u_lightCount"),l.count),o.uniform1iv(n("u_lightKind"),l.kind),o.uniform3fv(n("u_lightColor"),l.color),o.uniform1fv(n("u_lightIntensity"),l.intensity),o.uniform3fv(n("u_lightPosition"),l.position),o.uniform3fv(n("u_lightDirection"),l.direction),o.uniform1fv(n("u_lightRange"),l.range),o.uniform2fv(n("u_lightCone"),l.cone);const s=G(t.material,t.color,t.opacity);o.uniform3fv(n("u_color"),s.color),o.uniform3fv(n("u_emissive"),s.emissive),o.uniform1f(n("u_opacity"),s.opacity),o.uniform1i(n("u_shading"),s.shading),o.uniform1f(n("u_bands"),s.bands),o.uniform3fv(n("u_eye"),i.camera.position),o.uniform3fv(n("u_forward"),i.camera.forward),o.uniform1i(n("u_orthographic"),i.camera.orthographic?1:0),o.uniform1i(n("u_fog"),i.fog?1:0),i.fog&&(o.uniform3fv(n("u_fogColor"),g(i.fog.color)),o.uniform2f(n("u_fogRange"),i.fog.near,i.fog.far)),o.drawElements(o.TRIANGLES,t.mesh.indices.length,o.UNSIGNED_INT,0)}drawOutline(i,t,o,r){const n=this.gl,l=this.outline,s=t.material.outline;n.useProgram(l.program),n.enable(n.CULL_FACE),n.cullFace(n.FRONT),this.bindMesh(l,t.mesh);const u=l.uniform;n.uniformMatrix4fv(u("u_model"),!1,t.world),n.uniformMatrix4fv(u("u_view"),!1,i.camera.view),n.uniformMatrix4fv(u("u_projection"),!1,i.camera.projection),n.uniformMatrix3fv(u("u_normalMatrix"),!1,U(t.world)),n.uniform2f(u("u_viewport"),o,r),n.uniform1f(u("u_width"),s.width*(o/i.width));const[_,a,f]=g(s.color);n.uniform4f(u("u_ink"),_,a,f,1),n.drawElements(n.TRIANGLES,t.mesh.indices.length,n.UNSIGNED_INT,0),n.cullFace(n.BACK)}bindMesh(i,t){const o=this.gl;let r=this.buffers.get(t);if(!r){const l=o.createBuffer();o.bindBuffer(o.ARRAY_BUFFER,l),o.bufferData(o.ARRAY_BUFFER,new Float32Array(t.positions),o.STATIC_DRAW);const s=o.createBuffer();o.bindBuffer(o.ARRAY_BUFFER,s),o.bufferData(o.ARRAY_BUFFER,new Float32Array(t.normals),o.STATIC_DRAW);const u=o.createBuffer();o.bindBuffer(o.ELEMENT_ARRAY_BUFFER,u),o.bufferData(o.ELEMENT_ARRAY_BUFFER,new Uint32Array(t.indices),o.STATIC_DRAW),r={position:l,normal:s,index:u},this.buffers.set(t,r)}const n=(l,s)=>{const u=o.getAttribLocation(i.program,l);u<0||(o.bindBuffer(o.ARRAY_BUFFER,s),o.enableVertexAttribArray(u),o.vertexAttribPointer(u,3,o.FLOAT,!1,0,0))};n("a_position",r.position),n("a_normal",r.normal),o.bindBuffer(o.ELEMENT_ARRAY_BUFFER,r.index)}destroy(){this.gl.deleteProgram(this.surface.program),this.gl.deleteProgram(this.outline.program)}}function H(e,i,t){const o=(l,s)=>{const u=e.createShader(l);if(e.shaderSource(u,s),e.compileShader(u),!e.getShaderParameter(u,e.COMPILE_STATUS))throw new Error(`scene-3d webgl: shader failed to compile — ${e.getShaderInfoLog(u)}`);return u},r=e.createProgram();if(e.attachShader(r,o(e.VERTEX_SHADER,i)),e.attachShader(r,o(e.FRAGMENT_SHADER,t)),e.linkProgram(r),!e.getProgramParameter(r,e.LINK_STATUS))throw new Error(`scene-3d webgl: program failed to link — ${e.getProgramInfoLog(r)}`);const n=new Map;return{program:r,uniform:l=>(n.has(l)||n.set(l,e.getUniformLocation(r,l)),n.get(l))}}h.LIGHT_CODE=k,h.MAX_LIGHTS=d,h.SHADING_CODE=P,h.WebGL2Renderer=J,h.normalMatrix3=U,h.packLights=D,h.surfaceUniforms=G,Object.defineProperty(h,Symbol.toStringTag,{value:"Module"})})(this.tinyfly=this.tinyfly||{});
@@ -0,0 +1,3 @@
1
+ (function(P){"use strict";const Rt=[{name:"position",vector:"position",components:["x","y","z"]},{name:"rotation",vector:"quaternion",components:["rotateX","rotateY","rotateZ"]}];function Mt(t,s=[]){const o=[],n=new Map;for(const i of t.objects){if(i.id?n.has(i.id)&&o.push(`two objects share the id "${i.id}"`):o.push("an object has no id"),i.parent!==void 0&&!n.has(i.parent)&&o.push(`"${i.id}" has parent "${i.parent}", which must be an earlier object`),i.quaternion&&i.quaternion.length!==4&&o.push(`"${i.id}": a quaternion has four numbers`),i.kind==="mesh"){t.materials?.[i.material]||o.push(`"${i.id}" uses material "${i.material}", which is not in materials`);const r=i.geometry,a=r.type==="box"||r.type==="plane"?r.size:r.type==="torus"?[r.radius,r.tube]:r.type==="sphere"?[r.radius]:r.type==="extrude"?[r.depth,r.width??1]:[r.radius,r.height];r.type==="extrude"&&!(typeof r.path=="string"&&/^\s*[Mm]/.test(r.path))&&o.push(`"${i.id}": an extrude needs a path that starts with M`),a.every(l=>Number.isFinite(l)&&l>0)||o.push(`"${i.id}": a ${r.type}'s sizes must be positive`)}i.kind==="camera"&&(i.near>0&&i.far>i.near||o.push(`camera "${i.id}": near must be above 0 and far beyond it`),i.projection==="perspective"&&!(i.fov>0&&i.fov<180)&&o.push(`camera "${i.id}": fov is between 0 and 180 degrees`),i.projection==="orthographic"&&!(i.height>0)&&o.push(`camera "${i.id}": height must be positive`)),i.id&&n.set(i.id,i)}const e=n.get(t.camera);e?e.kind!=="camera"&&o.push(`the scene's camera "${t.camera}" is a ${e.kind}, not a camera`):o.push(`the scene's camera "${t.camera}" is not one of its objects`),t.fog&&!(t.fog.far>t.fog.near)&&o.push("fog: far must be beyond near");const c=new Map;for(const i of s){if(!i.target.startsWith(`${t.id}/`))continue;const r=i.target.slice(t.id.length+1);c.has(r)||c.set(r,new Set),c.get(r).add(i.property)}for(const[i,r]of c){n.has(i)||o.push(`a track animates "${t.id}/${i}", which is not in the scene`);for(const a of Rt)r.has(a.vector)&&a.components.some(l=>r.has(l))&&o.push(`"${i}": its ${a.name} is animated both by ${a.vector} and by ${a.components.join(" / ")}; use one`)}return o}class L{positions=[];normals=[];indices=[];vertex(s,o,n,e,c,i){const r=Math.hypot(e,c,i)||1;return this.positions.push(s,o,n),this.normals.push(e/r,c/r,i/r),this.positions.length/3-1}triangle(s,o,n){this.indices.push(s,o,n)}quad(s,o,n,e){this.indices.push(s,o,n,s,n,e)}build(){return{positions:this.positions,normals:this.normals,indices:this.indices}}}function mt(t){const s=new Map,o=[];for(let e=0;e<t.positions.length/3;e++){const c=[0,1,2].map(i=>Math.round(t.positions[e*3+i]*1e6)).join(",");s.has(c)||s.set(c,e),o.push(s.get(c))}const n=new Map;for(let e=0;e<t.indices.length/3;e++)for(let c=0;c<3;c++){const i=o[t.indices[e*3+c]],r=o[t.indices[e*3+(c+1)%3]];if(i===r)continue;const a=i<r?`${i}-${r}`:`${r}-${i}`,l=n.get(a);l?l.faces.push(e):n.set(a,{a:Math.min(i,r),b:Math.max(i,r),faces:[e]})}return[...n.values()]}const yt=32,Ut=256,U=new Map,Zt=/[MmLlHhVvCcSsQqTtAaZz]/,Gt=/^[-+]?(?:\d+\.?\d*|\.\d+)(?:[eE][-+]?\d+)?/,Vt={M:2,L:2,H:1,V:1,C:6,S:4,Q:4,T:2,A:7,Z:0};function Bt(t){const s=[];let o=0,n=null;const e=()=>{for(;o<t.length&&/[\s,]/.test(t[o]);)o++};for(;o<t.length&&(e(),!(o>=t.length));){const c=t[o];if(Zt.test(c)){n={type:c,args:[]},s.push(n),o++;continue}if(!n)break;const i=n.type==="A"||n.type==="a",r=n.args.length%7;if(i&&(r===3||r===4)){if(c!=="0"&&c!=="1")break;n.args.push(c==="1"?1:0),o++;continue}const a=Gt.exec(t.slice(o));if(!a)break;n.args.push(parseFloat(a[0])),o+=a[0].length}return s}function Ft(t,s,o,n,e,c,i,r,a){if(t===r&&s===a)return[];let l=Math.abs(o),u=Math.abs(n);if(l===0||u===0)return[[t,s,r,a,r,a]];const d=e*Math.PI/180,f=Math.cos(d),p=Math.sin(d),b=(t-r)/2,m=(s-a)/2,h=f*b+p*m,g=-p*b+f*m,w=h*h/(l*l)+g*g/(u*u);if(w>1){const E=Math.sqrt(w);l*=E,u*=E}const k=c===i?-1:1,x=l*l*u*u-l*l*g*g-u*u*h*h,M=l*l*g*g+u*u*h*h,A=k*Math.sqrt(Math.max(0,x/M)),y=A*l*g/u,$=-A*u*h/l,j=f*y-p*$+(t+r)/2,I=p*y+f*$+(s+a)/2,v=(E,Y,z,R)=>{const gt=E*z+Y*R,tt=Math.sqrt((E*E+Y*Y)*(z*z+R*R)),B=Math.acos(Math.max(-1,Math.min(1,gt/tt)));return E*R-Y*z<0?-B:B},S=v(1,0,(h-y)/l,(g-$)/u);let T=v((h-y)/l,(g-$)/u,(-h-y)/l,(-g-$)/u);!i&&T>0&&(T-=2*Math.PI),i&&T<0&&(T+=2*Math.PI);const C=Math.max(1,Math.ceil(Math.abs(T)/(Math.PI/2))),V=T/C,Q=4/3*Math.tan(V/4),Dt=E=>{const Y=l*Math.cos(E),z=u*Math.sin(E);return[f*Y-p*z+j,p*Y+f*z+I]},Ht=E=>{const Y=-l*Math.sin(E),z=u*Math.cos(E);return[f*Y-p*z,p*Y+f*z]},Qt=[];for(let E=0;E<C;E++){const Y=S+E*V,z=Y+V,[R,gt]=Dt(Y),[tt,B]=E===C-1?[r,a]:Dt(z),[jn,En]=Ht(Y),[Xn,Yn]=Ht(z);Qt.push([R+Q*jn,gt+Q*En,tt-Q*Xn,B-Q*Yn,tt,B])}return Qt}function bt(t,s,o,n,e){const c=1-e;return c*c*c*t+3*c*c*e*s+3*c*e*e*o+e*e*e*n}function Z(t,s,o,n){return{subpath:0,type:"L",points:[o,n],startX:t,startY:s,endX:o,endY:n,length:Math.hypot(o-t,n-s)}}function F(t,s,o){const[n,e,c,i,r,a]=o,l=[0];let u=t,d=s,f=0;for(let p=1;p<=yt;p++){const b=p/yt,m=bt(t,n,c,r,b),h=bt(s,e,i,a,b);f+=Math.hypot(m-u,h-d),l.push(f),u=m,d=h}return{subpath:0,type:"C",points:[n,e,c,i,r,a],startX:t,startY:s,endX:r,endY:a,length:f,lengths:l}}function Wt(t){const s=U.get(t);if(s)return s;const o=[];let n=0,e=0,c=0,i=0,r=null,a=null,l=-1;const u=new Set,d=m=>{l<0&&(l=0),m.subpath=l,o.push(m)};for(const{type:m,args:h}of Bt(t)){const g=m.toUpperCase(),w=m!==g,k=Vt[g];if(g==="Z"){(n!==c||e!==i)&&d(Z(n,e,c,i)),l>=0&&u.add(l),n=c,e=i,r=a=null;continue}for(let x=0;x+k<=h.length;x+=k){const M=h.slice(x,x+k),A=w?n:0,y=w?e:0;let $=null,j=null;switch(g){case"M":x===0?(n=M[0]+A,e=M[1]+y,c=n,i=e,(l<0||o[o.length-1]?.subpath===l)&&l++):(d(Z(n,e,M[0]+A,M[1]+y)),n=M[0]+A,e=M[1]+y);break;case"L":d(Z(n,e,M[0]+A,M[1]+y)),n=M[0]+A,e=M[1]+y;break;case"H":d(Z(n,e,M[0]+A,e)),n=M[0]+A;break;case"V":d(Z(n,e,n,M[0]+y)),e=M[0]+y;break;case"C":{const I=[M[0]+A,M[1]+y,M[2]+A,M[3]+y,M[4]+A,M[5]+y];d(F(n,e,I)),$=[I[2],I[3]],n=I[4],e=I[5];break}case"S":{const[I,v]=r?[2*n-r[0],2*e-r[1]]:[n,e],S=[I,v,M[0]+A,M[1]+y,M[2]+A,M[3]+y];d(F(n,e,S)),$=[S[2],S[3]],n=S[4],e=S[5];break}case"Q":case"T":{let I=n,v=e;g==="Q"?(I=M[0]+A,v=M[1]+y):a&&(I=2*n-a[0],v=2*e-a[1]);const S=g==="Q"?M[2]+A:M[0]+A,T=g==="Q"?M[3]+y:M[1]+y;d(F(n,e,[n+2/3*(I-n),e+2/3*(v-e),S+2/3*(I-S),T+2/3*(v-T),S,T])),j=[I,v],n=S,e=T;break}case"A":{const I=M[5]+A,v=M[6]+y;let S=n,T=e;for(const C of Ft(n,e,M[0],M[1],M[2],M[3],M[4],I,v))d(F(S,T,C)),S=C[4],T=C[5];n=I,e=v;break}}r=$,a=j}}const f=o.reduce((m,h)=>m+h.length,0),p=[];for(let m=0;m<o.length;){const h=o[m].subpath;let g=m,w=0;for(;g<o.length&&o[g].subpath===h;)w+=o[g++].length;const k=o[m],x=o[g-1],M=u.has(h)||Math.abs(x.endX-k.startX)<1e-9&&Math.abs(x.endY-k.startY)<1e-9;p.push({start:m,end:g,length:w,closed:M}),m=g}const b={segments:o,totalLength:f,subpaths:p};return U.size>=Ut&&U.delete(U.keys().next().value),U.set(t,b),b}function H(t){let s=0;for(let o=0;o<t.length;o++){const n=t[o],e=t[(o+1)%t.length];s+=n.x*e.y-e.x*n.y}return s}function nt(t,s){let o=!1;for(let n=0,e=s.length-1;n<s.length;e=n++){const c=s[n],i=s[e];c.y>t.y!=i.y>t.y&&t.x<(i.x-c.x)*(t.y-c.y)/(i.y-c.y)+c.x&&(o=!o)}return o}const W=(t,s,o)=>(s.x-t.x)*(o.y-t.y)-(s.y-t.y)*(o.x-t.x),et=(t,s)=>t.x===s.x&&t.y===s.y;function st(t,s,o,n){return W(s,o,t)>=0&&W(o,n,t)>=0&&W(n,s,t)>=0}function Jt(t,s){let o=0;for(let f=1;f<s.length;f++)s[f].x>s[o].x&&(o=f);const n=s[o];let e=-1,c=1/0;for(let f=0;f<t.length;f++){const p=t[f],b=t[(f+1)%t.length];if(p.y>n.y==b.y>n.y)continue;const m=p.x+(n.y-p.y)*(b.x-p.x)/(b.y-p.y);m>=n.x&&m<c&&(c=m,e=f)}if(e<0)return t;const i=t[e],r=t[(e+1)%t.length];let a=i.x>r.x?e:(e+1)%t.length;const l={x:c,y:n.y};let u=1/0;for(let f=0;f<t.length;f++){const p=t[f];if(f===a||p.x<n.x)continue;const b=t[a];if(!(st(p,n,l,b)||st(p,n,b,l)))continue;const h=Math.abs(Math.atan2(p.y-n.y,p.x-n.x));h<u&&(u=h,a=f)}const d=s.slice(o).concat(s.slice(0,o));return[...t.slice(0,a+1),...d,n,t[a],...t.slice(a+1)]}function wt(t,s=[]){const o=H(t)>=0?t:[...t].reverse(),n=s.map(d=>H(d)<=0?d:[...d].reverse()),e=[...o,...n.flat()],c=new Map;e.forEach((d,f)=>c.set(d,f));const i=[...n].sort((d,f)=>Math.max(...f.map(p=>p.x))-Math.max(...d.map(p=>p.x)));let r=o;for(const d of i)r=Jt(r,d);const a=r.map(d=>c.get(d)),l=[];let u=a.length*a.length;for(;a.length>3&&u-- >0;){let d=!1;for(let f=0;f<a.length;f++){const p=a[(f+a.length-1)%a.length],b=a[f],m=a[(f+1)%a.length],[h,g,w]=[e[p],e[b],e[m]];if(W(h,g,w)<=0)continue;let k=!1;for(const x of a){const M=e[x];if(!(x===p||x===b||x===m||et(M,h)||et(M,g)||et(M,w))&&st(M,h,g,w)){k=!0;break}}if(!k){l.push(p,b,m),a.splice(f,1),d=!0;break}}d||(l.push(a[a.length-1],a[0],a[1]),a.splice(0,1))}return a.length===3&&l.push(a[0],a[1],a[2]),{points:e,triangles:l}}function Kt(t,s){const o=new Map;for(const n of Wt(t).segments){o.has(n.subpath)||o.set(n.subpath,[{x:n.startX,y:-n.startY}]);const e=o.get(n.subpath);if(n.type==="L")e.push({x:n.endX,y:-n.endY});else{const[c,i,r,a,l,u]=n.points;for(let d=1;d<=s;d++){const f=d/s,p=1-f,b=p*p*p*n.startX+3*p*p*f*c+3*p*f*f*r+f*f*f*l,m=p*p*p*n.startY+3*p*p*f*i+3*p*f*f*a+f*f*f*u;e.push({x:b,y:-m})}}}return[...o.values()].map(n=>n.filter((e,c)=>c===0||e.x!==n[c-1].x||e.y!==n[c-1].y)).map(n=>n.length>1&&n[0].x===n[n.length-1].x&&n[0].y===n[n.length-1].y?n.slice(0,-1):n).filter(n=>n.length>=3&&Math.abs(H(n))>1e-12)}function xt(t,s){const o=Kt(t,Math.max(1,Math.round(s.curveSegments??12))),n=new L;if(o.length===0)return n.build();const e=o.flat(),c=Math.min(...e.map(h=>h.x)),i=Math.max(...e.map(h=>h.x)),r=Math.min(...e.map(h=>h.y)),a=Math.max(...e.map(h=>h.y)),l=(s.width??1)/Math.max(i-c,1e-9),u=(c+i)/2,d=(r+a)/2,f=o.map(h=>h.map(g=>({x:(g.x-u)*l,y:(g.y-d)*l}))),p=f.map((h,g)=>f.filter((w,k)=>k!==g&&nt(h[0],w)).length),b=s.depth/2,m=-s.depth/2;return f.forEach((h,g)=>{if(p[g]%2===1)return;const w=f.filter((y,$)=>p[$]===p[g]+1&&nt(y[0],h)),{points:k,triangles:x}=wt(h,w),M=k.map(y=>n.vertex(y.x,y.y,b,0,0,1)),A=k.map(y=>n.vertex(y.x,y.y,m,0,0,-1));for(let y=0;y<x.length;y+=3)n.triangle(M[x[y]],M[x[y+1]],M[x[y+2]]),n.triangle(A[x[y]],A[x[y+2]],A[x[y+1]]);for(const y of[H(h)>=0?h:[...h].reverse(),...w.map($=>H($)<=0?$:[...$].reverse())])for(let $=0;$<y.length;$++){const j=y[$],I=y[($+1)%y.length],v=I.y-j.y,S=-(I.x-j.x),T=n.vertex(j.x,j.y,m,v,S,0),C=n.vertex(I.x,I.y,m,v,S,0),V=n.vertex(I.x,I.y,b,v,S,0),Q=n.vertex(j.x,j.y,b,v,S,0);n.quad(T,C,V,Q)}}),n.build()}const _=Math.PI*2;function vt([t,s,o]){const n=new L,e=t/2,c=s/2,i=o/2,r=[[[0,0,1],[[-e,-c,i],[e,-c,i],[e,c,i],[-e,c,i]]],[[0,0,-1],[[e,-c,-i],[-e,-c,-i],[-e,c,-i],[e,c,-i]]],[[1,0,0],[[e,-c,i],[e,-c,-i],[e,c,-i],[e,c,i]]],[[-1,0,0],[[-e,-c,-i],[-e,-c,i],[-e,c,i],[-e,c,-i]]],[[0,1,0],[[-e,c,i],[e,c,i],[e,c,-i],[-e,c,-i]]],[[0,-1,0],[[-e,-c,-i],[e,-c,-i],[e,-c,i],[-e,-c,i]]]];for(const[[a,l,u],d]of r){const[f,p,b,m]=d.map(([h,g,w])=>n.vertex(h,g,w,a,l,u));n.quad(f,p,b,m)}return n.build()}function kt([t,s],o=1){const n=new L,e=Math.max(1,Math.round(o)),c=[];for(let i=0;i<=e;i++){const r=s/2-s*i/e;c.push(Array.from({length:e+1},(a,l)=>n.vertex(-t/2+t*l/e,0,r,0,1,0)))}for(let i=0;i<e;i++)for(let r=0;r<e;r++)n.quad(c[i][r],c[i][r+1],c[i+1][r+1],c[i+1][r]);return n.build()}function It(t,s=16){const o=new L,n=Math.max(6,Math.round(s)),e=Math.max(3,Math.round(n/2)),c=[];for(let i=0;i<=e;i++){const r=i/e*Math.PI,a=[];for(let l=0;l<=n;l++){const u=l/n*_,d=Math.sin(r)*Math.sin(u),f=Math.cos(r),p=Math.sin(r)*Math.cos(u);a.push(o.vertex(d*t,f*t,p*t,d,f,p))}c.push(a)}for(let i=0;i<e;i++)for(let r=0;r<n;r++){const a=c[i][r],l=c[i+1][r],u=c[i+1][r+1],d=c[i][r+1];i>0&&o.triangle(a,l,d),i<e-1&&o.triangle(l,u,d)}return o.build()}function ot(t,s,o,n,e){const c=t.vertex(0,o,0,0,n,0),i=Array.from({length:e},(r,a)=>{const l=a/e*_;return t.vertex(Math.sin(l)*s,o,Math.cos(l)*s,0,n,0)});for(let r=0;r<e;r++){const a=i[r],l=i[(r+1)%e];n===1?t.triangle(c,a,l):t.triangle(c,l,a)}}function At(t,s,o=24){const n=new L,e=Math.max(6,Math.round(o)),c=s/2,i=-s/2,r=u=>Array.from({length:e+1},(d,f)=>{const p=f/e*_;return n.vertex(Math.sin(p)*t,u,Math.cos(p)*t,Math.sin(p),0,Math.cos(p))}),a=r(i),l=r(c);for(let u=0;u<e;u++)n.quad(a[u],a[u+1],l[u+1],l[u]);return ot(n,t,c,1,e),ot(n,t,i,-1,e),n.build()}function $t(t,s,o=24){const n=new L,e=Math.max(6,Math.round(o)),c=s/2,i=-s/2,r=t/s;for(let a=0;a<e;a++){const l=a/e*_,u=(a+1)/e*_,d=(l+u)/2,f=n.vertex(Math.sin(l)*t,i,Math.cos(l)*t,Math.sin(l),r,Math.cos(l)),p=n.vertex(Math.sin(u)*t,i,Math.cos(u)*t,Math.sin(u),r,Math.cos(u)),b=n.vertex(0,c,0,Math.sin(d),r,Math.cos(d));n.triangle(f,p,b)}return ot(n,t,i,-1,e),n.build()}function St(t,s,o=24){const n=new L,e=Math.max(6,Math.round(o)),c=Math.max(4,Math.round(e/2)),i=[];for(let r=0;r<=e;r++){const a=r/e*_,l=[];for(let u=0;u<=c;u++){const d=u/c*_,f=Math.cos(d)*Math.sin(a),p=Math.sin(d),b=Math.cos(d)*Math.cos(a),m=t+s*Math.cos(d);l.push(n.vertex(m*Math.sin(a),s*Math.sin(d),m*Math.cos(a),f,p,b))}i.push(l)}for(let r=0;r<e;r++)for(let a=0;a<c;a++)n.quad(i[r][a],i[r+1][a],i[r+1][a+1],i[r][a+1]);return n.build()}function Pt(t){switch(t.type){case"box":return vt(t.size);case"plane":return kt(t.size,t.segments);case"sphere":return It(t.radius,t.segments);case"cylinder":return At(t.radius,t.height,t.segments);case"cone":return $t(t.radius,t.height,t.segments);case"torus":return St(t.radius,t.tube,t.segments);case"extrude":return xt(t.path,{depth:t.depth,width:t.width,curveSegments:t.curveSegments})}}function Ct(t){const s=Array.from({length:t.indices.length/3},()=>[]);for(const o of mt(t))for(const n of o.faces){const e=o.faces.find(c=>c!==n)??-1;s[n].push({a:o.a,b:o.b,across:e})}return{...t,faceEdges:s}}function tn(t,s={}){const o=new Map((s.kinds??[]).map(i=>[i.kind,i])),n=Mt(t);for(const i of t.objects){if(nn.has(i.kind))continue;const r=o.get(i.kind);r?n.push(...(r.validate?.(i)??[]).map(a=>`"${i.id}": ${a}`)):n.push(`"${i.id}" is a ${i.kind}: load the scene with that kind (for characters: loadScene3D(scene, { kinds: [characterObjects] }), from @algorisys/tinyfly/characters)`)}if(n.length>0)throw new Error(`scene-3d: "${t.id}" is not valid:
2
+ - ${n.join(`
3
+ - `)}`);const e=new Map,c=new Map;for(const i of t.objects){i.kind==="mesh"&&e.set(i.id,Ct(Pt(i.geometry)));const r=o.get(i.kind);r?.prepare&&c.set(i.id,r.prepare(i))}return{scene:t,meshes:e,kinds:o,prepared:c}}const nn=new Set(["group","mesh","camera","light"]);function it(t,s){return[t[0]+s[0],t[1]+s[1],t[2]+s[2]]}function N(t,s){return[t[0]-s[0],t[1]-s[1],t[2]-s[2]]}function D(t,s){return[t[0]*s,t[1]*s,t[2]*s]}function O(t,s){return t[0]*s[0]+t[1]*s[1]+t[2]*s[2]}function ct(t,s){return[t[1]*s[2]-t[2]*s[1],t[2]*s[0]-t[0]*s[2],t[0]*s[1]-t[1]*s[0]]}function J(t){return Math.hypot(t[0],t[1],t[2])}function en(t,s){return J(N(t,s))}function q(t){const s=J(t);return s===0?[0,0,0]:D(t,1/s)}function sn(t,s,o){return[t[0]+(s[0]-t[0])*o,t[1]+(s[1]-t[1])*o,t[2]+(s[2]-t[2])*o]}const on=Math.PI/180;function rt(){return[0,0,0,1]}function at(t,s){const o=Math.hypot(t[0],t[1],t[2]);if(o===0)return rt();const n=s*on/2,e=Math.sin(n)/o;return[t[0]*e,t[1]*e,t[2]*e,Math.cos(n)]}function Tt(t,s,o){return jt(jt(at([0,1,0],s),at([1,0,0],t)),at([0,0,1],o))}function jt(t,s){const[o,n,e,c]=t,[i,r,a,l]=s;return[c*i+o*l+n*a-e*r,c*r-o*a+n*l+e*i,c*a+o*r-n*i+e*l,c*l-o*i-n*r-e*a]}function cn(t){return Math.hypot(t[0],t[1],t[2],t[3])}function Et(t){const s=cn(t);return s===0?rt():[t[0]/s,t[1]/s,t[2]/s,t[3]/s]}const rn=Math.PI/180;function Xt(){return[1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1]}function an(t,s){const o=new Array(16);for(let n=0;n<4;n++)for(let e=0;e<4;e++){let c=0;for(let i=0;i<4;i++)c+=t[i*4+e]*s[n*4+i];o[n*4+e]=c}return o}function hn(t){const[s,o,n,e]=t;return[1-2*(o*o+n*n),2*(s*o+n*e),2*(s*n-o*e),0,2*(s*o-n*e),1-2*(s*s+n*n),2*(o*n+s*e),0,2*(s*n+o*e),2*(o*n-s*e),1-2*(s*s+o*o),0,0,0,0,1]}function ln(t,s,o){const n=hn(s);for(let e=0;e<3;e++)n[e]*=o[0],n[4+e]*=o[1],n[8+e]*=o[2];return n[12]=t[0],n[13]=t[1],n[14]=t[2],n}function un(t){const s=new Array(16);for(let o=0;o<4;o++)for(let n=0;n<4;n++)s[n*4+o]=t[o*4+n];return s}function Yt(t){const[s,o,n,e,c,i,r,a,l,u,d,f,p,b,m,h]=t,g=s*i-o*c,w=s*r-n*c,k=s*a-e*c,x=o*r-n*i,M=o*a-e*i,A=n*a-e*r,y=l*b-u*p,$=l*m-d*p,j=l*h-f*p,I=u*m-d*b,v=u*h-f*b,S=d*h-f*m,T=g*S-w*v+k*I+x*j-M*$+A*y;if(Math.abs(T)<1e-12)return null;const C=1/T;return[(i*S-r*v+a*I)*C,(n*v-o*S-e*I)*C,(b*A-m*M+h*x)*C,(d*M-u*A-f*x)*C,(r*j-c*S-a*$)*C,(s*S-n*j+e*$)*C,(m*k-p*A-h*w)*C,(l*A-d*k+f*w)*C,(c*v-i*j+a*y)*C,(o*j-s*v-e*y)*C,(p*M-b*k+h*g)*C,(u*k-l*M-f*g)*C,(i*$-c*I-r*y)*C,(s*I-o*$+n*y)*C,(b*w-p*x-m*g)*C,(l*x-u*w+d*g)*C]}function fn(t,s,o,n){const e=1/Math.tan(t*rn/2),c=1/(o-n);return[e/s,0,0,0,0,e,0,0,0,0,(n+o)*c,-1,0,0,2*n*o*c,0]}function dn(t,s,o,n,e,c){const i=1/(s-t),r=1/(n-o),a=1/(c-e);return[2*i,0,0,0,0,2*r,0,0,0,0,-2*a,0,-(s+t)*i,-(n+o)*r,-(c+e)*a,1]}function pn(t,s,o=[0,1,0]){const n=q(N(t,s)),e=q(ct(o,n)),c=ct(n,e);return[e[0],c[0],n[0],0,e[1],c[1],n[1],0,e[2],c[2],n[2],0,-O(e,t),-O(c,t),-O(n,t),1]}function ht(t,s){const o=t[0]*s[0]+t[4]*s[1]+t[8]*s[2]+t[12],n=t[1]*s[0]+t[5]*s[1]+t[9]*s[2]+t[13],e=t[2]*s[0]+t[6]*s[1]+t[10]*s[2]+t[14],c=t[3]*s[0]+t[7]*s[1]+t[11]*s[2]+t[15];return c===1||c===0?[o,n,e]:[o/c,n/c,e/c]}function zt(t,s){const o=q(N(s,t)),n=Math.abs(o[1])>.9999?[0,0,-1]:[0,1,0];return pn(t,s,n)}function gn(t,s,o,n){const e=s*Math.PI/180,c=o*Math.PI/180;return[t[0]+n*Math.cos(c)*Math.sin(e),t[1]+n*Math.sin(c),t[2]+n*Math.cos(c)*Math.cos(e)]}function Mn(t,s,o){const n=N(t,s),e=J(n);if(e===0)return t;const c=Math.max(.001,e-o);return it(s,D(n,c/e))}function lt(t,s){if(t.projection==="perspective")return fn(t.fov,s,t.near,t.far);const o=t.height/2;return dn(-o*s,o*s,-o,o,t.near,t.far)}const mn=Math.cos(30*Math.PI/180),yn=new Map,X=t=>typeof t=="number"?t:void 0,K=(t,s)=>Array.isArray(t)&&t.length===s?t:void 0;function bn(t,s){const o=t.position??[0,0,0],n=K(s.get("position"),3)??[X(s.get("x"))??o[0],X(s.get("y"))??o[1],X(s.get("z"))??o[2]];let e;const c=K(s.get("quaternion"),4),i=["rotateX","rotateY","rotateZ"].map(u=>X(s.get(u)));if(c)e=Et(c);else if(i.some(u=>u!==void 0)){const u=t.rotation??[0,0,0];e=Tt(i[0]??u[0],i[1]??u[1],i[2]??u[2])}else t.quaternion?e=Et(t.quaternion):e=t.rotation?Tt(...t.rotation):rt();const r=typeof t.scale=="number"?[t.scale,t.scale,t.scale]:t.scale??[1,1,1],a=X(s.get("scale")),l=[X(s.get("scaleX"))??a??r[0],X(s.get("scaleY"))??a??r[1],X(s.get("scaleZ"))??a??r[2]];return ln(n,e,l)}const Nt=t=>[t[12],t[13],t[14]];function wn(t,s,o,n){const e=Nt(s),c=K(o.get("lookAt"),3)??t.lookAt,i=c?zt(e,c):Yt(s)??Xt(),r=t.projection==="perspective"?lt({...t,fov:X(o.get("fov"))??t.fov},n):lt({...t,height:X(o.get("height"))??t.height},n);return{id:t.id,view:i,projection:r,position:e,forward:q([-i[2],-i[6],-i[10]]),orthographic:t.projection==="orthographic",near:t.near,far:t.far}}function Lt(t){return un(Yt(t)??Xt())}function xn(t,s,o,n){return q([t[0]*s+t[4]*o+t[8]*n,t[1]*s+t[5]*o+t[9]*n,t[2]*s+t[6]*o+t[10]*n])}function vn(t,s){const o=e=>-e[2]>=s,n=[];for(let e=0;e<t.length;e++){const c=t[e],i=t[(e+1)%t.length];if(o(c)&&n.push(c),o(c)!==o(i)){const r=(-s-c[2])/(i[2]-c[2]);n.push(sn(c,i,r))}}return n}function ut(t,s=new Map,o){const{scene:n}=t,e=h=>s.get(`${n.id}/${h}`)??yn,c=new Map,i=new Map;for(const h of n.objects){const g=e(h.id),w=bn(h,g),k=h.parent?c.get(h.parent):void 0;c.set(h.id,k?an(k,w):w);const x=X(g.get("visible"))!==void 0?X(g.get("visible"))>.5:h.visible!==!1;i.set(h.id,x&&(h.parent?i.get(h.parent)!==!1:!0))}const r=s.get(n.id)?.get("activeCamera"),a=typeof r=="string"&&n.objects.some(h=>h.id===r&&h.kind==="camera")?r:n.camera,l=n.objects.find(h=>h.id===a),u=wn(l,c.get(a),e(a),o.width/o.height),d=h=>{const g=ht(u.projection,h);return{x:(g[0]+1)/2*o.width,y:(1-g[1])/2*o.height}},f=[];for(const h of n.objects){if(h.kind!=="light"||!i.get(h.id))continue;const g=e(h.id),w=Nt(c.get(h.id)),k=K(g.get("target"),3)??h.target??[0,0,0],x=g.get("color");f.push({id:h.id,light:h.light,color:typeof x=="string"?x:h.color,intensity:X(g.get("intensity"))??h.intensity,position:w,direction:q(N(k,w)),range:h.range,angle:h.angle??30})}const p=[],b=[];n.objects.forEach((h,g)=>{if(h.kind!=="mesh"||!i.get(h.id))return;const w=t.meshes.get(h.id),k=n.materials?.[h.material];if(!w||!k)return;const x=e(h.id),M=c.get(h.id),A=Lt(M),y=x.get("color"),$=(X(x.get("opacity"))??1)*(k.opacity??1);$<=0||(b.push({objectId:h.id,objectIndex:g,mesh:w,world:M,material:k,color:typeof y=="string"?y:void 0,opacity:$}),p.push(...Ot(w,M,A,u,d,k,{objectId:h.id,objectIndex:g,layer:h.layer??0,color:typeof y=="string"?y:void 0,opacity:$})))});const m=[];return n.objects.forEach((h,g)=>{const w=t.kinds.get(h.kind);if(!w||!i.get(h.id))return;const k=w.resolve({object:h,prepared:t.prepared.get(h.id),values:e(h.id),world:c.get(h.id),camera:u,width:o.width,height:o.height,toScreen:d});if(!k)return;const x=h.layer??0;for(const M of k.drawables??[])m.push({...M,objectId:h.id,objectIndex:g,layer:x});for(const M of k.meshes??[])b.push({objectId:h.id,objectIndex:g,mesh:M.mesh,world:M.world,material:M.material,opacity:M.material.opacity??1}),p.push(...Ot(M.mesh,M.world,Lt(M.world),u,d,M.material,{objectId:h.id,objectIndex:g,layer:x,opacity:M.material.opacity??1}))}),m.sort((h,g)=>h.layer-g.layer||g.depth-h.depth||h.objectIndex-g.objectIndex),p.sort((h,g)=>h.layer-g.layer||g.depth-h.depth||h.objectIndex-g.objectIndex||h.face-g.face||h.part-g.part),{id:n.id,width:o.width,height:o.height,background:n.background,fog:n.fog,camera:u,lights:f,worlds:c,triangles:p,drawables:m,meshes:b,time:o.time??0}}function Ot(t,s,o,n,e,c,i){const r=t.positions.length/3,a=[],l=[];for(let m=0;m<r;m++){const h=ht(s,[t.positions[m*3],t.positions[m*3+1],t.positions[m*3+2]]);a.push(h),l.push(ht(n.view,h))}const u=t.indices.length/3,d=[],f=[];for(let m=0;m<u;m++){const[h,g,w]=[0,1,2].map(M=>a[t.indices[m*3+M]]),k=q(ct(N(g,h),N(w,h)));d.push(k);const x=n.orthographic?D(n.forward,-1):N(n.position,h);f.push(O(k,x)>0)}const p=m=>-l[m][2]>=n.near,b=[];for(let m=0;m<u;m++){const h=f[m];if(!h&&!c.doubleSided)continue;const g=[t.indices[m*3],t.indices[m*3+1],t.indices[m*3+2]],w=g.map(v=>l[v]);if(w.every(v=>-v[2]>n.far))continue;const k=vn(w,n.near);if(k.length<3)continue;const x=h?1:-1,M=D(d[m],x),A=g.map(v=>D(xn(o,t.normals[v*3],t.normals[v*3+1],t.normals[v*3+2]),x)),y=D(it(it(a[g[0]],a[g[1]]),a[g[2]]),1/3),$=-(w[0][2]+w[1][2]+w[2][2])/3,j=[];if(c.outline&&h)for(const v of t.faceEdges[m]){const S=v.across,T=S===-1||!f[S],C=!T&&m<S&&O(d[m],d[S])<mn;(T||C)&&p(v.a)&&p(v.b)&&j.push([e(l[v.a]),e(l[v.b])])}const I=k.map(e);for(let v=1;v+1<I.length;v++)b.push({objectId:i.objectId,objectIndex:i.objectIndex,layer:i.layer,face:m,part:v-1,screen:[I[0],I[v],I[v+1]],depth:$,centroid:y,normal:M,vertexNormals:A,material:c,color:i.color,opacity:i.opacity,outline:v===1?j:[]})}return b}const ft=new Map;function G(t){const s=ft.get(t);if(s)return s;let o=[1,1,1];const n=t.trim().replace("#","");if(t.trim().startsWith("#")&&(n.length===3||n.length===6)){const e=n.length===3?n.split("").map(c=>c+c).join(""):n;o=[0,2,4].map(c=>Number.parseInt(e.slice(c,c+2),16)/255)}else{const e=t.match(/rgba?\(\s*([\d.]+)\s*,\s*([\d.]+)\s*,\s*([\d.]+)/);e&&(o=[Number(e[1])/255,Number(e[2])/255,Number(e[3])/255])}return ft.size<512&&ft.set(t,o),o}const kn=[{id:"default-ambient",light:"ambient",color:"#ffffff",intensity:.35,position:[0,0,0],direction:[0,-1,0],angle:30},{id:"default-key",light:"directional",color:"#ffffff",intensity:.8,position:[3,5,4],direction:q([-3,-5,-4]),angle:30}],In=(t,s,o)=>{const n=Math.min(1,Math.max(0,(o-t)/(s-t)));return n*n*(3-2*n)};function dt(t,s,o){const n=[0,0,0];for(const e of o){const c=G(e.color);let i;if(e.light==="ambient")i=e.intensity;else if(e.light==="directional")i=e.intensity*Math.max(0,-O(s,e.direction));else{const r=N(e.position,t),a=J(r),l=a>0?D(r,1/a):s,u=e.range?Math.max(0,1-a/e.range)**2:1;if(i=e.intensity*Math.max(0,O(s,l))*u,e.light==="spot"){const d=Math.cos(e.angle*Math.PI/180),f=Math.cos(e.angle*.8*Math.PI/180);i*=In(d,f,-O(l,e.direction))}}n[0]+=c[0]*i,n[1]+=c[1]*i,n[2]+=c[2]*i}return n}function An(t,s){const o=Math.max(2,Math.round(s));return Math.min(1.2,Math.ceil(Math.min(1,t)*o-1e-9)/o)}function qt(t,s){return t?Math.min(1,Math.max(0,(s-t.near)/(t.far-t.near))):0}function _t(t,s,o,n){const e=t.material,c=G(t.color??e.color),i=e.shading??"lambert";let r;if(i==="unlit")r=[...c];else{const u=s.length>0?s:kn;let d;if(i==="lambert"){const f=[0,0,0];for(const p of t.vertexNormals){const b=dt(t.centroid,p,u);f[0]+=b[0]/3,f[1]+=b[1]/3,f[2]+=b[2]/3}d=f}else if(d=dt(t.centroid,t.normal,u),i==="toon"){const f=Math.max(d[0],d[1],d[2]),p=f>0?An(f,e.bands??3)/f:0;d=[d[0]*p,d[1]*p,d[2]*p]}r=[c[0]*d[0],c[1]*d[1],c[2]*d[2]]}if(e.emissive){const u=G(e.emissive);r=[r[0]+u[0],r[1]+u[1],r[2]+u[2]]}const a=qt(n,en(t.centroid,o.position));if(n&&a>0){const u=G(n.color);r=[r[0]+(u[0]-r[0])*a,r[1]+(u[1]-r[1])*a,r[2]+(u[2]-r[2])*a]}const l=u=>Math.round(Math.min(1,Math.max(0,u))*255);return{color:`rgb(${l(r[0])}, ${l(r[1])}, ${l(r[2])})`,alpha:t.opacity}}class $n{ctx;constructor(s){this.ctx=s}render(s){pt(this.ctx,s)}}function pt(t,s){t.save(),s.background&&s.background!=="transparent"&&(t.fillStyle=s.background,t.fillRect(0,0,s.width,s.height)),t.lineJoin="round",t.lineCap="round";let o=0;const n=(i,r)=>{for(;o<s.drawables.length;){const a=s.drawables[o];if(a.layer>i||a.layer===i&&a.depth<=r)break;t.save(),a.draw(t,s),t.restore(),o++}};let e=[];const c=i=>{if(e.length===0)return;const r=[];for(const a of e)i&&i.objectId===a.objectId&&i.layer===a.layer&&a.depth-i.depth<=Sn*a.depth?r.push(a):Pn(t,a.triangle);e=r};for(const i of s.triangles){c(i),n(i.layer,i.depth);const{color:r,alpha:a}=_t(i,s.lights,s.camera,s.fog),[l,u,d]=i.screen;t.globalAlpha=a,t.beginPath(),t.moveTo(l.x,l.y),t.lineTo(u.x,u.y),t.lineTo(d.x,d.y),t.closePath(),t.fillStyle=r,t.fill(),a>=1&&(t.strokeStyle=r,t.lineWidth=.75,t.stroke()),i.material.outline&&i.outline.length>0&&e.push({objectId:i.objectId,layer:i.layer,depth:i.depth,triangle:i})}c(),n(1/0,-1/0),t.restore()}const Sn=.04;function Pn(t,s){const o=s.material.outline;t.globalAlpha=s.opacity,t.strokeStyle=o.color,t.lineWidth=o.width,t.beginPath();for(const[n,e]of s.outline)t.moveTo(n.x,n.y),t.lineTo(e.x,e.y);t.stroke()}class Cn{renderer;width;height;scenes=new Map;values=new Map;constructor(s,o){this.renderer=s,this.width=o.width,this.height=o.height}registerScene(s){this.scenes.set(s.scene.id,s)}unregisterScene(s){this.scenes.delete(s)}resize(s,o){this.width=s,this.height=o}applyState(s){this.values=s.values,this.time=s.currentTime??this.time}time=0;render(){for(const s of this.scenes.values())this.renderer.render(ut(s,this.values,{width:this.width,height:this.height,time:this.time}))}}function Tn(t,s,o,n){pt(t,ut(s,o??new Map,{width:n.width,height:n.height,time:n.time}))}P.Canvas2DRenderer=$n,P.MeshBuilder=L,P.Scene3DAdapter=Cn,P.boxMesh=vt,P.coneMesh=$t,P.cylinderMesh=At,P.dollyPosition=Mn,P.drawResolvedScene=pt,P.drawScene3D=Tn,P.extrudeMesh=xt,P.fogAmount=qt,P.geometryMesh=Pt,P.lightAt=dt,P.loadScene3D=tn,P.lookAtView=zt,P.meshEdges=mt,P.orbitPosition=gn,P.parseColor=G,P.planeMesh=kt,P.pointInPolygon=nt,P.prepareMesh=Ct,P.projectionMatrix=lt,P.resolveScene3D=ut,P.shadeTriangle=_t,P.signedArea=H,P.sphereMesh=It,P.torusMesh=St,P.triangulate=wt,P.validateScene3D=Mt,Object.defineProperty(P,Symbol.toStringTag,{value:"Module"})})(this.tinyfly=this.tinyfly||{});