@algorisys/tinyfly 0.82.0 → 0.86.2
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.
- package/README.md +11 -4
- package/lib/addons/adapters.js +363 -248
- package/lib/addons/characters.js +1652 -1470
- package/lib/addons/dom-adapter-Cphr2HZn.js +372 -0
- package/lib/addons/gsap-compat.js +197 -187
- package/lib/addons/load-scene-vp14_Jj-.js +324 -0
- package/lib/addons/scene-3d-webgl.js +237 -0
- package/lib/addons/scene-3d.js +306 -0
- package/lib/addons/shading-ByG9UXd0.js +85 -0
- package/lib/browser/tinyfly-scene-3d-webgl.iife.js +114 -0
- package/lib/browser/tinyfly-scene-3d.iife.js +3 -0
- package/lib/browser/tinyfly.iife.js +4 -4
- package/lib/browser/tinyfly.js +3028 -2480
- package/lib/browser/tinyfly.umd.js +4 -4
- package/lib/cli/tools.js +137 -22
- package/lib/embed/tinyfly-embed.iife.js +4 -4
- package/lib/embed/tinyfly-embed.js +921 -833
- package/lib/engine/tinyfly-engine.js +1022 -776
- package/lib/engine/tinyfly-engine.umd.cjs +2 -2
- package/lib/headless/headless.js +264 -7
- package/lib/player/tinyfly-player.es.js +459 -371
- package/lib/player/tinyfly-player.iife.js +2 -2
- package/lib/player/tinyfly-player.umd.js +2 -2
- package/lib/types/adapters/canvas/canvas-adapter.d.ts +36 -0
- package/lib/types/adapters/transform-3d.d.ts +99 -0
- package/lib/types/adapters/webgl/webgl-adapter.d.ts +17 -0
- package/lib/types/characters/character.d.ts +20 -1
- package/lib/types/characters/index.d.ts +2 -0
- package/lib/types/characters/rig/body-plan.d.ts +3 -1
- package/lib/types/characters/rig/skeleton.d.ts +57 -0
- package/lib/types/characters/scene-objects.d.ts +2 -0
- package/lib/types/compat/gsap/vars.d.ts +8 -0
- package/lib/types/engine/core/bake.d.ts +3 -1
- package/lib/types/engine/index.d.ts +1 -0
- package/lib/types/engine/interpolation/interpolators.d.ts +10 -3
- package/lib/types/engine/math/index.d.ts +11 -0
- package/lib/types/engine/math/mat4.d.ts +37 -0
- package/lib/types/engine/math/quat.d.ts +39 -0
- package/lib/types/engine/math/vec3.d.ts +16 -0
- package/lib/types/engine/types.d.ts +23 -2
- package/lib/types/scene-3d/camera.d.ts +27 -0
- package/lib/types/scene-3d/canvas-2d-renderer.d.ts +21 -0
- package/lib/types/scene-3d/geometry/extrude.d.ts +17 -0
- package/lib/types/scene-3d/geometry/mesh.d.ts +36 -0
- package/lib/types/scene-3d/geometry/primitives.d.ts +16 -0
- package/lib/types/scene-3d/geometry/triangulate.d.ts +24 -0
- package/lib/types/scene-3d/index.d.ts +20 -0
- package/lib/types/scene-3d/load-scene.d.ts +31 -0
- package/lib/types/scene-3d/object-kind.d.ts +54 -0
- package/lib/types/scene-3d/resolve-scene.d.ts +122 -0
- package/lib/types/scene-3d/scene-3d-adapter.d.ts +46 -0
- package/lib/types/scene-3d/scene-types.d.ts +191 -0
- package/lib/types/scene-3d/shading.d.ts +20 -0
- package/lib/types/scene-3d/validate-scene.d.ts +11 -0
- package/lib/types/scene-3d/webgl/gl-data.d.ts +28 -0
- package/lib/types/scene-3d/webgl/index.d.ts +16 -0
- package/lib/types/scene-3d/webgl/shaders.d.ts +29 -0
- package/lib/types/scene-3d/webgl/webgl2-renderer.d.ts +33 -0
- package/package.json +10 -2
- 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-ByG9UXd0.js";
|
|
4
|
+
import { f as Pt, l as St, p as At } from "./shading-ByG9UXd0.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,85 @@
|
|
|
1
|
+
import { vec3 as f } from "@algorisys/tinyfly";
|
|
2
|
+
const p = /* @__PURE__ */ new Map();
|
|
3
|
+
function b(t) {
|
|
4
|
+
const o = p.get(t);
|
|
5
|
+
if (o) return o;
|
|
6
|
+
let c = [1, 1, 1];
|
|
7
|
+
const e = t.trim().replace("#", "");
|
|
8
|
+
if (t.trim().startsWith("#") && (e.length === 3 || e.length === 6)) {
|
|
9
|
+
const n = e.length === 3 ? e.split("").map((i) => i + i).join("") : e;
|
|
10
|
+
c = [0, 2, 4].map((i) => Number.parseInt(n.slice(i, i + 2), 16) / 255);
|
|
11
|
+
} else {
|
|
12
|
+
const n = t.match(/rgba?\(\s*([\d.]+)\s*,\s*([\d.]+)\s*,\s*([\d.]+)/);
|
|
13
|
+
n && (c = [Number(n[1]) / 255, Number(n[2]) / 255, Number(n[3]) / 255]);
|
|
14
|
+
}
|
|
15
|
+
return p.size < 512 && p.set(t, c), c;
|
|
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 = (t, o, c) => {
|
|
21
|
+
const e = Math.min(1, Math.max(0, (c - t) / (o - t)));
|
|
22
|
+
return e * e * (3 - 2 * e);
|
|
23
|
+
};
|
|
24
|
+
function g(t, o, c) {
|
|
25
|
+
const e = [0, 0, 0];
|
|
26
|
+
for (const n of c) {
|
|
27
|
+
const i = b(n.color);
|
|
28
|
+
let r;
|
|
29
|
+
if (n.light === "ambient")
|
|
30
|
+
r = n.intensity;
|
|
31
|
+
else if (n.light === "directional")
|
|
32
|
+
r = n.intensity * Math.max(0, -f.dot(o, n.direction));
|
|
33
|
+
else {
|
|
34
|
+
const a = f.subtract(n.position, t), m = f.length(a), d = m > 0 ? f.scale(a, 1 / m) : o, l = n.range ? Math.max(0, 1 - m / n.range) ** 2 : 1;
|
|
35
|
+
if (r = n.intensity * Math.max(0, f.dot(o, d)) * l, n.light === "spot") {
|
|
36
|
+
const s = Math.cos(n.angle * Math.PI / 180), h = Math.cos(n.angle * 0.8 * Math.PI / 180);
|
|
37
|
+
r *= y(s, h, -f.dot(d, n.direction));
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
e[0] += i[0] * r, e[1] += i[1] * r, e[2] += i[2] * r;
|
|
41
|
+
}
|
|
42
|
+
return e;
|
|
43
|
+
}
|
|
44
|
+
function N(t, o) {
|
|
45
|
+
const c = Math.max(2, Math.round(o));
|
|
46
|
+
return Math.min(1.2, Math.ceil(Math.min(1, t) * c - 1e-9) / c);
|
|
47
|
+
}
|
|
48
|
+
function v(t, o) {
|
|
49
|
+
return t ? Math.min(1, Math.max(0, (o - t.near) / (t.far - t.near))) : 0;
|
|
50
|
+
}
|
|
51
|
+
function C(t, o, c, e) {
|
|
52
|
+
const n = t.material, i = b(t.color ?? n.color), r = n.shading ?? "lambert";
|
|
53
|
+
let a;
|
|
54
|
+
if (r === "unlit")
|
|
55
|
+
a = [...i];
|
|
56
|
+
else {
|
|
57
|
+
const l = o.length > 0 ? o : x;
|
|
58
|
+
let s;
|
|
59
|
+
if (r === "lambert") {
|
|
60
|
+
const h = [0, 0, 0];
|
|
61
|
+
for (const u of t.vertexNormals) {
|
|
62
|
+
const M = g(t.centroid, u, l);
|
|
63
|
+
h[0] += M[0] / 3, h[1] += M[1] / 3, h[2] += M[2] / 3;
|
|
64
|
+
}
|
|
65
|
+
s = h;
|
|
66
|
+
} else if (s = g(t.centroid, t.normal, l), r === "toon") {
|
|
67
|
+
const h = Math.max(s[0], s[1], s[2]), u = h > 0 ? N(h, n.bands ?? 3) / h : 0;
|
|
68
|
+
s = [s[0] * u, s[1] * u, s[2] * u];
|
|
69
|
+
}
|
|
70
|
+
a = [i[0] * s[0], i[1] * s[1], i[2] * s[2]];
|
|
71
|
+
}
|
|
72
|
+
const m = v(e, f.distance(t.centroid, c.position));
|
|
73
|
+
if (e && m > 0) {
|
|
74
|
+
const l = b(e.color);
|
|
75
|
+
a = [a[0] + (l[0] - a[0]) * m, a[1] + (l[1] - a[1]) * m, a[2] + (l[2] - a[2]) * m];
|
|
76
|
+
}
|
|
77
|
+
const d = (l) => Math.round(Math.min(1, Math.max(0, l)) * 255);
|
|
78
|
+
return { color: `rgb(${d(a[0])}, ${d(a[1])}, ${d(a[2])})`, alpha: t.opacity };
|
|
79
|
+
}
|
|
80
|
+
export {
|
|
81
|
+
v as f,
|
|
82
|
+
g as l,
|
|
83
|
+
b as p,
|
|
84
|
+
C as s
|
|
85
|
+
};
|
|
@@ -0,0 +1,114 @@
|
|
|
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,u,c,_,a,f,g,m,v,p,b]=e,A=i*l-t*n,y=i*u-o*n,E=i*c-r*n,R=t*u-o*l,L=t*c-r*l,M=o*c-r*u,F=_*v-a*m,T=_*p-f*m,C=_*b-g*m,S=a*p-f*v,I=a*b-g*v,x=f*b-g*p,O=A*x-y*I+E*S+R*C-L*T+M*F;if(Math.abs(O)<1e-12)return null;const s=1/O;return[(l*x-u*I+c*S)*s,(o*I-t*x-r*S)*s,(v*M-p*L+b*R)*s,(f*L-a*M-g*R)*s,(u*C-n*x-c*T)*s,(i*x-o*C+r*T)*s,(p*E-m*M-b*y)*s,(_*M-f*E+g*y)*s,(n*I-l*C+c*F)*s,(t*C-i*I-r*F)*s,(m*L-v*E+b*A)*s,(a*E-_*L-g*A)*s,(l*T-n*S-u*F)*s,(i*S-t*T+o*F)*s,(v*y-m*R-p*A)*s,(_*R-a*y+f*A)*s]}const N=new Map;function w(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 float u_opacity;
|
|
29
|
+
uniform int u_shading;
|
|
30
|
+
uniform float u_bands;
|
|
31
|
+
uniform vec3 u_eye;
|
|
32
|
+
uniform vec3 u_forward;
|
|
33
|
+
uniform bool u_orthographic;
|
|
34
|
+
uniform bool u_fog;
|
|
35
|
+
uniform vec3 u_fogColor;
|
|
36
|
+
uniform vec2 u_fogRange;
|
|
37
|
+
in vec3 v_world;
|
|
38
|
+
in vec3 v_normal;
|
|
39
|
+
out vec4 outColor;
|
|
40
|
+
|
|
41
|
+
vec3 lightAt(vec3 p, vec3 n) {
|
|
42
|
+
vec3 total = vec3(0.0);
|
|
43
|
+
for (int i = 0; i < MAX_LIGHTS; i++) {
|
|
44
|
+
if (i >= u_lightCount) break;
|
|
45
|
+
float amount;
|
|
46
|
+
int kind = u_lightKind[i];
|
|
47
|
+
if (kind == 0) {
|
|
48
|
+
amount = u_lightIntensity[i];
|
|
49
|
+
} else if (kind == 1) {
|
|
50
|
+
amount = u_lightIntensity[i] * max(0.0, -dot(n, u_lightDirection[i]));
|
|
51
|
+
} else {
|
|
52
|
+
vec3 toLight = u_lightPosition[i] - p;
|
|
53
|
+
float distance = length(toLight);
|
|
54
|
+
vec3 l = distance > 0.0 ? toLight / distance : n;
|
|
55
|
+
float fade = u_lightRange[i] > 0.0 ? pow(max(0.0, 1.0 - distance / u_lightRange[i]), 2.0) : 1.0;
|
|
56
|
+
amount = u_lightIntensity[i] * max(0.0, dot(n, l)) * fade;
|
|
57
|
+
if (kind == 3) amount *= smoothstep(u_lightCone[i].x, u_lightCone[i].y, -dot(l, u_lightDirection[i]));
|
|
58
|
+
}
|
|
59
|
+
total += u_lightColor[i] * amount;
|
|
60
|
+
}
|
|
61
|
+
return total;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
void main() {
|
|
65
|
+
vec3 rgb = u_color;
|
|
66
|
+
if (u_shading != 0) {
|
|
67
|
+
// Flat: the face's own normal, from the surface's slope; otherwise the smooth one.
|
|
68
|
+
vec3 n = u_shading == 1 ? normalize(cross(dFdx(v_world), dFdy(v_world))) : normalize(v_normal);
|
|
69
|
+
vec3 toEye = u_orthographic ? -u_forward : normalize(u_eye - v_world);
|
|
70
|
+
if (dot(n, toEye) < 0.0) n = -n;
|
|
71
|
+
vec3 light = lightAt(v_world, n);
|
|
72
|
+
if (u_shading == 3) {
|
|
73
|
+
// Band the brightness, not each channel, so coloured lights keep their hue.
|
|
74
|
+
float level = max(light.r, max(light.g, light.b));
|
|
75
|
+
float steps = max(2.0, floor(u_bands + 0.5));
|
|
76
|
+
float banded = min(1.2, ceil(min(1.0, level) * steps - 1e-6) / steps);
|
|
77
|
+
light = level > 0.0 ? light * (banded / level) : vec3(0.0);
|
|
78
|
+
}
|
|
79
|
+
rgb *= light;
|
|
80
|
+
}
|
|
81
|
+
if (u_fog) {
|
|
82
|
+
float amount = clamp((distance(v_world, u_eye) - u_fogRange.x) / (u_fogRange.y - u_fogRange.x), 0.0, 1.0);
|
|
83
|
+
rgb = mix(rgb, u_fogColor, amount);
|
|
84
|
+
}
|
|
85
|
+
outColor = vec4(clamp(rgb, 0.0, 1.0), u_opacity);
|
|
86
|
+
}
|
|
87
|
+
`,$=`#version 300 es
|
|
88
|
+
in vec3 a_position;
|
|
89
|
+
in vec3 a_normal;
|
|
90
|
+
uniform mat4 u_model;
|
|
91
|
+
uniform mat4 u_view;
|
|
92
|
+
uniform mat4 u_projection;
|
|
93
|
+
uniform mat3 u_normalMatrix;
|
|
94
|
+
uniform vec2 u_viewport;
|
|
95
|
+
uniform float u_width;
|
|
96
|
+
void main() {
|
|
97
|
+
mat4 viewProjection = u_projection * u_view;
|
|
98
|
+
vec4 clip = viewProjection * u_model * vec4(a_position, 1.0);
|
|
99
|
+
vec3 worldNormal = normalize(u_normalMatrix * a_normal);
|
|
100
|
+
vec4 tip = viewProjection * (u_model * vec4(a_position, 1.0) + vec4(worldNormal * 0.01, 0.0));
|
|
101
|
+
vec2 direction = tip.xy / tip.w - clip.xy / clip.w;
|
|
102
|
+
float length2 = dot(direction, direction);
|
|
103
|
+
vec2 push = length2 > 0.0 ? direction * inversesqrt(length2) : vec2(0.0);
|
|
104
|
+
clip.xy += push * u_width * 2.0 / u_viewport * clip.w;
|
|
105
|
+
gl_Position = clip;
|
|
106
|
+
}
|
|
107
|
+
`,q=`#version 300 es
|
|
108
|
+
precision highp float;
|
|
109
|
+
uniform vec4 u_ink;
|
|
110
|
+
out vec4 outColor;
|
|
111
|
+
void main() {
|
|
112
|
+
outColor = u_ink;
|
|
113
|
+
}
|
|
114
|
+
`,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(w(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:w(i??e.color),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"?w(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),u=i.camera.position,c=a=>Math.hypot(a.world[12]-u[0],a.world[13]-u[1],a.world[14]-u[2]),_=i.meshes.filter(a=>a.opacity<1).sort((a,f)=>c(f)-c(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 u=G(t.material,t.color,t.opacity);o.uniform3fv(n("u_color"),u.color),o.uniform1f(n("u_opacity"),u.opacity),o.uniform1i(n("u_shading"),u.shading),o.uniform1f(n("u_bands"),u.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"),w(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,u=t.material.outline;n.useProgram(l.program),n.enable(n.CULL_FACE),n.cullFace(n.FRONT),this.bindMesh(l,t.mesh);const c=l.uniform;n.uniformMatrix4fv(c("u_model"),!1,t.world),n.uniformMatrix4fv(c("u_view"),!1,i.camera.view),n.uniformMatrix4fv(c("u_projection"),!1,i.camera.projection),n.uniformMatrix3fv(c("u_normalMatrix"),!1,U(t.world)),n.uniform2f(c("u_viewport"),o,r),n.uniform1f(c("u_width"),u.width*(o/i.width));const[_,a,f]=w(u.color);n.uniform4f(c("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 u=o.createBuffer();o.bindBuffer(o.ARRAY_BUFFER,u),o.bufferData(o.ARRAY_BUFFER,new Float32Array(t.normals),o.STATIC_DRAW);const c=o.createBuffer();o.bindBuffer(o.ELEMENT_ARRAY_BUFFER,c),o.bufferData(o.ELEMENT_ARRAY_BUFFER,new Uint32Array(t.indices),o.STATIC_DRAW),r={position:l,normal:u,index:c},this.buffers.set(t,r)}const n=(l,u)=>{const c=o.getAttribLocation(i.program,l);c<0||(o.bindBuffer(o.ARRAY_BUFFER,u),o.enableVertexAttribArray(c),o.vertexAttribPointer(c,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,u)=>{const c=e.createShader(l);if(e.shaderSource(c,u),e.compileShader(c),!e.getShaderParameter(c,e.COMPILE_STATUS))throw new Error(`scene-3d webgl: shader failed to compile — ${e.getShaderInfoLog(c)}`);return c},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 yt(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 mt=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),f=Math.abs(n);if(l===0||f===0)return[[t,s,r,a,r,a]];const d=e*Math.PI/180,u=Math.cos(d),p=Math.sin(d),b=(t-r)/2,y=(s-a)/2,h=u*b+p*y,g=-p*b+u*y,w=h*h/(l*l)+g*g/(f*f);if(w>1){const E=Math.sqrt(w);l*=E,f*=E}const k=c===i?-1:1,x=l*l*f*f-l*l*g*g-f*f*h*h,M=l*l*g*g+f*f*h*h,A=k*Math.sqrt(Math.max(0,x/M)),m=A*l*g/f,$=-A*f*h/l,j=u*m-p*$+(t+r)/2,I=p*m+u*$+(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)),V=Math.acos(Math.max(-1,Math.min(1,gt/tt)));return E*R-Y*z<0?-V:V},S=v(1,0,(h-m)/l,(g-$)/f);let T=v((h-m)/l,(g-$)/f,(-h-m)/l,(-g-$)/f);!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))),G=T/C,Q=4/3*Math.tan(G/4),Dt=E=>{const Y=l*Math.cos(E),z=f*Math.sin(E);return[u*Y-p*z+j,p*Y+u*z+I]},Ht=E=>{const Y=-l*Math.sin(E),z=f*Math.cos(E);return[u*Y-p*z,p*Y+u*z]},Qt=[];for(let E=0;E<C;E++){const Y=S+E*G,z=Y+G,[R,gt]=Dt(Y),[tt,V]=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,V-Q*Yn,tt,V])}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 B(t,s,o){const[n,e,c,i,r,a]=o,l=[0];let f=t,d=s,u=0;for(let p=1;p<=mt;p++){const b=p/mt,y=bt(t,n,c,r,b),h=bt(s,e,i,a,b);u+=Math.hypot(y-f,h-d),l.push(u),f=y,d=h}return{subpath:0,type:"C",points:[n,e,c,i,r,a],startX:t,startY:s,endX:r,endY:a,length:u,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 f=new Set,d=y=>{l<0&&(l=0),y.subpath=l,o.push(y)};for(const{type:y,args:h}of Bt(t)){const g=y.toUpperCase(),w=y!==g,k=Vt[g];if(g==="Z"){(n!==c||e!==i)&&d(Z(n,e,c,i)),l>=0&&f.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,m=w?e:0;let $=null,j=null;switch(g){case"M":x===0?(n=M[0]+A,e=M[1]+m,c=n,i=e,(l<0||o[o.length-1]?.subpath===l)&&l++):(d(Z(n,e,M[0]+A,M[1]+m)),n=M[0]+A,e=M[1]+m);break;case"L":d(Z(n,e,M[0]+A,M[1]+m)),n=M[0]+A,e=M[1]+m;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]+m)),e=M[0]+m;break;case"C":{const I=[M[0]+A,M[1]+m,M[2]+A,M[3]+m,M[4]+A,M[5]+m];d(B(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]+m,M[2]+A,M[3]+m];d(B(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]+m):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]+m:M[1]+m;d(B(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]+m;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(B(S,T,C)),S=C[4],T=C[5];n=I,e=v;break}}r=$,a=j}}const u=o.reduce((y,h)=>y+h.length,0),p=[];for(let y=0;y<o.length;){const h=o[y].subpath;let g=y,w=0;for(;g<o.length&&o[g].subpath===h;)w+=o[g++].length;const k=o[y],x=o[g-1],M=f.has(h)||Math.abs(x.endX-k.startX)<1e-9&&Math.abs(x.endY-k.startY)<1e-9;p.push({start:y,end:g,length:w,closed:M}),y=g}const b={segments:o,totalLength:u,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 F=(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 F(s,o,t)>=0&&F(o,n,t)>=0&&F(n,s,t)>=0}function Jt(t,s){let o=0;for(let u=1;u<s.length;u++)s[u].x>s[o].x&&(o=u);const n=s[o];let e=-1,c=1/0;for(let u=0;u<t.length;u++){const p=t[u],b=t[(u+1)%t.length];if(p.y>n.y==b.y>n.y)continue;const y=p.x+(n.y-p.y)*(b.x-p.x)/(b.y-p.y);y>=n.x&&y<c&&(c=y,e=u)}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 f=1/0;for(let u=0;u<t.length;u++){const p=t[u];if(u===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<f&&(f=h,a=u)}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,u)=>c.set(d,u));const i=[...n].sort((d,u)=>Math.max(...u.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 f=a.length*a.length;for(;a.length>3&&f-- >0;){let d=!1;for(let u=0;u<a.length;u++){const p=a[(u+a.length-1)%a.length],b=a[u],y=a[(u+1)%a.length],[h,g,w]=[e[p],e[b],e[y]];if(F(h,g,w)<=0)continue;let k=!1;for(const x of a){const M=e[x];if(!(x===p||x===b||x===y||et(M,h)||et(M,g)||et(M,w))&&st(M,h,g,w)){k=!0;break}}if(!k){l.push(p,b,y),a.splice(u,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,f]=n.points;for(let d=1;d<=s;d++){const u=d/s,p=1-u,b=p*p*p*n.startX+3*p*p*u*c+3*p*u*u*r+u*u*u*l,y=p*p*p*n.startY+3*p*p*u*i+3*p*u*u*a+u*u*u*f;e.push({x:b,y:-y})}}}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),f=(c+i)/2,d=(r+a)/2,u=o.map(h=>h.map(g=>({x:(g.x-f)*l,y:(g.y-d)*l}))),p=u.map((h,g)=>u.filter((w,k)=>k!==g&&nt(h[0],w)).length),b=s.depth/2,y=-s.depth/2;return u.forEach((h,g)=>{if(p[g]%2===1)return;const w=u.filter((m,$)=>p[$]===p[g]+1&&nt(m[0],h)),{points:k,triangles:x}=wt(h,w),M=k.map(m=>n.vertex(m.x,m.y,b,0,0,1)),A=k.map(m=>n.vertex(m.x,m.y,y,0,0,-1));for(let m=0;m<x.length;m+=3)n.triangle(M[x[m]],M[x[m+1]],M[x[m+2]]),n.triangle(A[x[m]],A[x[m+2]],A[x[m+1]]);for(const m of[H(h)>=0?h:[...h].reverse(),...w.map($=>H($)<=0?$:[...$].reverse())])for(let $=0;$<m.length;$++){const j=m[$],I=m[($+1)%m.length],v=I.y-j.y,S=-(I.x-j.x),T=n.vertex(j.x,j.y,y,v,S,0),C=n.vertex(I.x,I.y,y,v,S,0),G=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,G,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,f],d]of r){const[u,p,b,y]=d.map(([h,g,w])=>n.vertex(h,g,w,a,l,f));n.quad(u,p,b,y)}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 f=l/n*_,d=Math.sin(r)*Math.sin(f),u=Math.cos(r),p=Math.sin(r)*Math.cos(f);a.push(o.vertex(d*t,u*t,p*t,d,u,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],f=c[i+1][r+1],d=c[i][r+1];i>0&&o.triangle(a,l,d),i<e-1&&o.triangle(l,f,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=f=>Array.from({length:e+1},(d,u)=>{const p=u/e*_;return n.vertex(Math.sin(p)*t,f,Math.cos(p)*t,Math.sin(p),0,Math.cos(p))}),a=r(i),l=r(c);for(let f=0;f<e;f++)n.quad(a[f],a[f+1],l[f+1],l[f]);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*_,f=(a+1)/e*_,d=(l+f)/2,u=n.vertex(Math.sin(l)*t,i,Math.cos(l)*t,Math.sin(l),r,Math.cos(l)),p=n.vertex(Math.sin(f)*t,i,Math.cos(f)*t,Math.sin(f),r,Math.cos(f)),b=n.vertex(0,c,0,Math.sin(d),r,Math.cos(d));n.triangle(u,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 f=0;f<=c;f++){const d=f/c*_,u=Math.cos(d)*Math.sin(a),p=Math.sin(d),b=Math.cos(d)*Math.cos(a),y=t+s*Math.cos(d);l.push(n.vertex(y*Math.sin(a),s*Math.sin(d),y*Math.cos(a),u,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 yt(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 W(t){return Math.hypot(t[0],t[1],t[2])}function en(t,s){return W(N(t,s))}function q(t){const s=W(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,f,d,u,p,b,y,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,m=l*b-f*p,$=l*y-d*p,j=l*h-u*p,I=f*y-d*b,v=f*h-u*b,S=d*h-u*y,T=g*S-w*v+k*I+x*j-M*$+A*m;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-y*M+h*x)*C,(d*M-f*A-u*x)*C,(r*j-c*S-a*$)*C,(s*S-n*j+e*$)*C,(y*k-p*A-h*w)*C,(l*A-d*k+u*w)*C,(c*v-i*j+a*m)*C,(o*j-s*v-e*m)*C,(p*M-b*k+h*g)*C,(f*k-l*M-u*g)*C,(i*$-c*I-r*m)*C,(s*I-o*$+n*m)*C,(b*w-p*x-y*g)*C,(l*x-f*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=W(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 yn=Math.cos(30*Math.PI/180),mn=new Map,X=t=>typeof t=="number"?t:void 0,J=(t,s)=>Array.isArray(t)&&t.length===s?t:void 0;function bn(t,s){const o=t.position??[0,0,0],n=J(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=J(s.get("quaternion"),4),i=["rotateX","rotateY","rotateZ"].map(f=>X(s.get(f)));if(c)e=Et(c);else if(i.some(f=>f!==void 0)){const f=t.rotation??[0,0,0];e=Tt(i[0]??f[0],i[1]??f[1],i[2]??f[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=J(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}`)??mn,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),f=wn(l,c.get(a),e(a),o.width/o.height),d=h=>{const g=ht(f.projection,h);return{x:(g[0]+1)/2*o.width,y:(1-g[1])/2*o.height}},u=[];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=J(g.get("target"),3)??h.target??[0,0,0],x=g.get("color");u.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),m=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 m=="string"?m:void 0,opacity:$}),p.push(...Ot(w,M,A,f,d,k,{objectId:h.id,objectIndex:g,layer:h.layer??0,color:typeof m=="string"?m:void 0,opacity:$})))});const y=[];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:f,width:o.width,height:o.height,toScreen:d});if(!k)return;const x=h.layer??0;for(const M of k.drawables??[])y.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),f,d,M.material,{objectId:h.id,objectIndex:g,layer:x,opacity:M.material.opacity??1}))}),y.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:f,lights:u,worlds:c,triangles:p,drawables:y,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 y=0;y<r;y++){const h=ht(s,[t.positions[y*3],t.positions[y*3+1],t.positions[y*3+2]]);a.push(h),l.push(ht(n.view,h))}const f=t.indices.length/3,d=[],u=[];for(let y=0;y<f;y++){const[h,g,w]=[0,1,2].map(M=>a[t.indices[y*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);u.push(O(k,x)>0)}const p=y=>-l[y][2]>=n.near,b=[];for(let y=0;y<f;y++){const h=u[y];if(!h&&!c.doubleSided)continue;const g=[t.indices[y*3],t.indices[y*3+1],t.indices[y*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[y],x),A=g.map(v=>D(xn(o,t.normals[v*3],t.normals[v*3+1],t.normals[v*3+2]),x)),m=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[y]){const S=v.across,T=S===-1||!u[S],C=!T&&y<S&&O(d[y],d[S])<yn;(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:y,part:v-1,screen:[I[0],I[v],I[v+1]],depth:$,centroid:m,normal:M,vertexNormals:A,material:c,color:i.color,opacity:i.opacity,outline:v===1?j:[]})}return b}const ft=new Map;function K(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=K(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=W(r),l=a>0?D(r,1/a):s,f=e.range?Math.max(0,1-a/e.range)**2:1;if(i=e.intensity*Math.max(0,O(s,l))*f,e.light==="spot"){const d=Math.cos(e.angle*Math.PI/180),u=Math.cos(e.angle*.8*Math.PI/180);i*=In(d,u,-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=K(t.color??e.color),i=e.shading??"lambert";let r;if(i==="unlit")r=[...c];else{const f=s.length>0?s:kn;let d;if(i==="lambert"){const u=[0,0,0];for(const p of t.vertexNormals){const b=dt(t.centroid,p,f);u[0]+=b[0]/3,u[1]+=b[1]/3,u[2]+=b[2]/3}d=u}else if(d=dt(t.centroid,t.normal,f),i==="toon"){const u=Math.max(d[0],d[1],d[2]),p=u>0?An(u,e.bands??3)/u:0;d=[d[0]*p,d[1]*p,d[2]*p]}r=[c[0]*d[0],c[1]*d[1],c[2]*d[2]]}const a=qt(n,en(t.centroid,o.position));if(n&&a>0){const f=K(n.color);r=[r[0]+(f[0]-r[0])*a,r[1]+(f[1]-r[1])*a,r[2]+(f[2]-r[2])*a]}const l=f=>Math.round(Math.min(1,Math.max(0,f))*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,f,d]=i.screen;t.globalAlpha=a,t.beginPath(),t.moveTo(l.x,l.y),t.lineTo(f.x,f.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=yt,P.orbitPosition=gn,P.parseColor=K,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||{});
|