@algorisys/tinyfly 0.81.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.
Files changed (62) hide show
  1. package/README.md +12 -5
  2. package/lib/addons/adapters.js +363 -248
  3. package/lib/addons/characters.js +1658 -1342
  4. package/lib/addons/dom-adapter-Cphr2HZn.js +372 -0
  5. package/lib/addons/gsap-compat.js +197 -187
  6. package/lib/addons/load-scene-vp14_Jj-.js +324 -0
  7. package/lib/addons/scene-3d-webgl.js +237 -0
  8. package/lib/addons/scene-3d.js +306 -0
  9. package/lib/addons/shading-ByG9UXd0.js +85 -0
  10. package/lib/browser/tinyfly-scene-3d-webgl.iife.js +114 -0
  11. package/lib/browser/tinyfly-scene-3d.iife.js +3 -0
  12. package/lib/browser/tinyfly.iife.js +4 -4
  13. package/lib/browser/tinyfly.js +3295 -2613
  14. package/lib/browser/tinyfly.umd.js +4 -4
  15. package/lib/cli/tools.js +137 -22
  16. package/lib/embed/tinyfly-embed.iife.js +4 -4
  17. package/lib/embed/tinyfly-embed.js +921 -833
  18. package/lib/engine/tinyfly-engine.js +1022 -776
  19. package/lib/engine/tinyfly-engine.umd.cjs +2 -2
  20. package/lib/headless/headless.js +264 -7
  21. package/lib/player/tinyfly-player.es.js +459 -371
  22. package/lib/player/tinyfly-player.iife.js +2 -2
  23. package/lib/player/tinyfly-player.umd.js +2 -2
  24. package/lib/types/adapters/canvas/canvas-adapter.d.ts +36 -0
  25. package/lib/types/adapters/transform-3d.d.ts +99 -0
  26. package/lib/types/adapters/webgl/webgl-adapter.d.ts +17 -0
  27. package/lib/types/characters/character.d.ts +20 -1
  28. package/lib/types/characters/dance.d.ts +47 -0
  29. package/lib/types/characters/index.d.ts +3 -1
  30. package/lib/types/characters/rig/body-plan.d.ts +3 -1
  31. package/lib/types/characters/rig/skeleton.d.ts +57 -0
  32. package/lib/types/characters/scene-objects.d.ts +2 -0
  33. package/lib/types/characters/species/human.d.ts +8 -0
  34. package/lib/types/compat/gsap/vars.d.ts +8 -0
  35. package/lib/types/engine/core/bake.d.ts +3 -1
  36. package/lib/types/engine/index.d.ts +1 -0
  37. package/lib/types/engine/interpolation/interpolators.d.ts +10 -3
  38. package/lib/types/engine/math/index.d.ts +11 -0
  39. package/lib/types/engine/math/mat4.d.ts +37 -0
  40. package/lib/types/engine/math/quat.d.ts +39 -0
  41. package/lib/types/engine/math/vec3.d.ts +16 -0
  42. package/lib/types/engine/types.d.ts +23 -2
  43. package/lib/types/scene-3d/camera.d.ts +27 -0
  44. package/lib/types/scene-3d/canvas-2d-renderer.d.ts +21 -0
  45. package/lib/types/scene-3d/geometry/extrude.d.ts +17 -0
  46. package/lib/types/scene-3d/geometry/mesh.d.ts +36 -0
  47. package/lib/types/scene-3d/geometry/primitives.d.ts +16 -0
  48. package/lib/types/scene-3d/geometry/triangulate.d.ts +24 -0
  49. package/lib/types/scene-3d/index.d.ts +20 -0
  50. package/lib/types/scene-3d/load-scene.d.ts +31 -0
  51. package/lib/types/scene-3d/object-kind.d.ts +54 -0
  52. package/lib/types/scene-3d/resolve-scene.d.ts +122 -0
  53. package/lib/types/scene-3d/scene-3d-adapter.d.ts +46 -0
  54. package/lib/types/scene-3d/scene-types.d.ts +191 -0
  55. package/lib/types/scene-3d/shading.d.ts +20 -0
  56. package/lib/types/scene-3d/validate-scene.d.ts +11 -0
  57. package/lib/types/scene-3d/webgl/gl-data.d.ts +28 -0
  58. package/lib/types/scene-3d/webgl/index.d.ts +16 -0
  59. package/lib/types/scene-3d/webgl/shaders.d.ts +29 -0
  60. package/lib/types/scene-3d/webgl/webgl2-renderer.d.ts +33 -0
  61. package/package.json +10 -2
  62. package/lib/addons/dom-adapter-CrNwoomd.js +0 -243
@@ -0,0 +1,324 @@
1
+ import { parsePath as B } from "@algorisys/tinyfly";
2
+ class j {
3
+ positions = [];
4
+ normals = [];
5
+ indices = [];
6
+ /** Add a vertex; returns its index. */
7
+ vertex(i, c, n, e, a, t) {
8
+ const o = Math.hypot(e, a, t) || 1;
9
+ return this.positions.push(i, c, n), this.normals.push(e / o, a / o, t / o), this.positions.length / 3 - 1;
10
+ }
11
+ /** Add a triangle, counter-clockwise seen from its front. */
12
+ triangle(i, c, n) {
13
+ this.indices.push(i, c, n);
14
+ }
15
+ /** Add a quad a-b-c-d (counter-clockwise) as two triangles. */
16
+ quad(i, c, n, e) {
17
+ this.indices.push(i, c, n, i, n, e);
18
+ }
19
+ build() {
20
+ return { positions: this.positions, normals: this.normals, indices: this.indices };
21
+ }
22
+ }
23
+ function C(s) {
24
+ const i = /* @__PURE__ */ new Map(), c = [];
25
+ for (let e = 0; e < s.positions.length / 3; e++) {
26
+ const a = [0, 1, 2].map((t) => Math.round(s.positions[e * 3 + t] * 1e6)).join(",");
27
+ i.has(a) || i.set(a, e), c.push(i.get(a));
28
+ }
29
+ const n = /* @__PURE__ */ new Map();
30
+ for (let e = 0; e < s.indices.length / 3; e++)
31
+ for (let a = 0; a < 3; a++) {
32
+ const t = c[s.indices[e * 3 + a]], o = c[s.indices[e * 3 + (a + 1) % 3]];
33
+ if (t === o) continue;
34
+ const r = t < o ? `${t}-${o}` : `${o}-${t}`, l = n.get(r);
35
+ l ? l.faces.push(e) : n.set(r, { a: Math.min(t, o), b: Math.max(t, o), faces: [e] });
36
+ }
37
+ return [...n.values()];
38
+ }
39
+ function I(s) {
40
+ let i = 0;
41
+ for (let c = 0; c < s.length; c++) {
42
+ const n = s[c], e = s[(c + 1) % s.length];
43
+ i += n.x * e.y - e.x * n.y;
44
+ }
45
+ return i;
46
+ }
47
+ function _(s, i) {
48
+ let c = !1;
49
+ for (let n = 0, e = i.length - 1; n < i.length; e = n++) {
50
+ const a = i[n], t = i[e];
51
+ a.y > s.y != t.y > s.y && s.x < (t.x - a.x) * (s.y - a.y) / (t.y - a.y) + a.x && (c = !c);
52
+ }
53
+ return c;
54
+ }
55
+ const X = (s, i, c) => (i.x - s.x) * (c.y - s.y) - (i.y - s.y) * (c.x - s.x), Y = (s, i) => s.x === i.x && s.y === i.y;
56
+ function P(s, i, c, n) {
57
+ return X(i, c, s) >= 0 && X(c, n, s) >= 0 && X(n, i, s) >= 0;
58
+ }
59
+ function H(s, i) {
60
+ let c = 0;
61
+ for (let h = 1; h < i.length; h++) i[h].x > i[c].x && (c = h);
62
+ const n = i[c];
63
+ let e = -1, a = 1 / 0;
64
+ for (let h = 0; h < s.length; h++) {
65
+ const f = s[h], x = s[(h + 1) % s.length];
66
+ if (f.y > n.y == x.y > n.y) continue;
67
+ const g = f.x + (n.y - f.y) * (x.x - f.x) / (x.y - f.y);
68
+ g >= n.x && g < a && (a = g, e = h);
69
+ }
70
+ if (e < 0) return s;
71
+ const t = s[e], o = s[(e + 1) % s.length];
72
+ let r = t.x > o.x ? e : (e + 1) % s.length;
73
+ const l = { x: a, y: n.y };
74
+ let d = 1 / 0;
75
+ for (let h = 0; h < s.length; h++) {
76
+ const f = s[h];
77
+ if (h === r || f.x < n.x) continue;
78
+ const x = s[r];
79
+ if (!(P(f, n, l, x) || P(f, n, x, l))) continue;
80
+ const p = Math.abs(Math.atan2(f.y - n.y, f.x - n.x));
81
+ p < d && (d = p, r = h);
82
+ }
83
+ const u = i.slice(c).concat(i.slice(0, c));
84
+ return [...s.slice(0, r + 1), ...u, n, s[r], ...s.slice(r + 1)];
85
+ }
86
+ function U(s, i = []) {
87
+ const c = I(s) >= 0 ? s : [...s].reverse(), n = i.map((u) => I(u) <= 0 ? u : [...u].reverse()), e = [...c, ...n.flat()], a = /* @__PURE__ */ new Map();
88
+ e.forEach((u, h) => a.set(u, h));
89
+ const t = [...n].sort((u, h) => Math.max(...h.map((f) => f.x)) - Math.max(...u.map((f) => f.x)));
90
+ let o = c;
91
+ for (const u of t) o = H(o, u);
92
+ const r = o.map((u) => a.get(u)), l = [];
93
+ let d = r.length * r.length;
94
+ for (; r.length > 3 && d-- > 0; ) {
95
+ let u = !1;
96
+ for (let h = 0; h < r.length; h++) {
97
+ const f = r[(h + r.length - 1) % r.length], x = r[h], g = r[(h + 1) % r.length], [p, M, b] = [e[f], e[x], e[g]];
98
+ if (X(p, M, b) <= 0) continue;
99
+ let $ = !1;
100
+ for (const y of r) {
101
+ const w = e[y];
102
+ if (!(y === f || y === x || y === g || Y(w, p) || Y(w, M) || Y(w, b)) && P(w, p, M, b)) {
103
+ $ = !0;
104
+ break;
105
+ }
106
+ }
107
+ if (!$) {
108
+ l.push(f, x, g), r.splice(h, 1), u = !0;
109
+ break;
110
+ }
111
+ }
112
+ u || (l.push(r[r.length - 1], r[0], r[1]), r.splice(0, 1));
113
+ }
114
+ return r.length === 3 && l.push(r[0], r[1], r[2]), { points: e, triangles: l };
115
+ }
116
+ function F(s, i) {
117
+ const c = /* @__PURE__ */ new Map();
118
+ for (const n of B(s).segments) {
119
+ c.has(n.subpath) || c.set(n.subpath, [{ x: n.startX, y: -n.startY }]);
120
+ const e = c.get(n.subpath);
121
+ if (n.type === "L")
122
+ e.push({ x: n.endX, y: -n.endY });
123
+ else {
124
+ const [a, t, o, r, l, d] = n.points;
125
+ for (let u = 1; u <= i; u++) {
126
+ const h = u / i, f = 1 - h, x = f * f * f * n.startX + 3 * f * f * h * a + 3 * f * h * h * o + h * h * h * l, g = f * f * f * n.startY + 3 * f * f * h * t + 3 * f * h * h * r + h * h * h * d;
127
+ e.push({ x, y: -g });
128
+ }
129
+ }
130
+ }
131
+ return [...c.values()].map((n) => n.filter((e, a) => a === 0 || e.x !== n[a - 1].x || e.y !== n[a - 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(I(n)) > 1e-12);
132
+ }
133
+ function W(s, i) {
134
+ const c = F(s, Math.max(1, Math.round(i.curveSegments ?? 12))), n = new j();
135
+ if (c.length === 0) return n.build();
136
+ const e = c.flat(), a = Math.min(...e.map((p) => p.x)), t = Math.max(...e.map((p) => p.x)), o = Math.min(...e.map((p) => p.y)), r = Math.max(...e.map((p) => p.y)), l = (i.width ?? 1) / Math.max(t - a, 1e-9), d = (a + t) / 2, u = (o + r) / 2, h = c.map((p) => p.map((M) => ({ x: (M.x - d) * l, y: (M.y - u) * l }))), f = h.map((p, M) => h.filter((b, $) => $ !== M && _(p[0], b)).length), x = i.depth / 2, g = -i.depth / 2;
137
+ return h.forEach((p, M) => {
138
+ if (f[M] % 2 === 1) return;
139
+ const b = h.filter((m, v) => f[v] === f[M] + 1 && _(m[0], p)), { points: $, triangles: y } = U(p, b), w = $.map((m) => n.vertex(m.x, m.y, x, 0, 0, 1)), E = $.map((m) => n.vertex(m.x, m.y, g, 0, 0, -1));
140
+ for (let m = 0; m < y.length; m += 3)
141
+ n.triangle(w[y[m]], w[y[m + 1]], w[y[m + 2]]), n.triangle(E[y[m]], E[y[m + 2]], E[y[m + 1]]);
142
+ for (const m of [I(p) >= 0 ? p : [...p].reverse(), ...b.map((v) => I(v) <= 0 ? v : [...v].reverse())])
143
+ for (let v = 0; v < m.length; v++) {
144
+ const q = m[v], z = m[(v + 1) % m.length], A = z.y - q.y, S = -(z.x - q.x), D = n.vertex(q.x, q.y, g, A, S, 0), L = n.vertex(z.x, z.y, g, A, S, 0), O = n.vertex(z.x, z.y, x, A, S, 0), T = n.vertex(q.x, q.y, x, A, S, 0);
145
+ n.quad(D, L, O, T);
146
+ }
147
+ }), n.build();
148
+ }
149
+ const k = Math.PI * 2;
150
+ function Z([s, i, c]) {
151
+ const n = new j(), e = s / 2, a = i / 2, t = c / 2, o = [
152
+ [[0, 0, 1], [[-e, -a, t], [e, -a, t], [e, a, t], [-e, a, t]]],
153
+ [[0, 0, -1], [[e, -a, -t], [-e, -a, -t], [-e, a, -t], [e, a, -t]]],
154
+ [[1, 0, 0], [[e, -a, t], [e, -a, -t], [e, a, -t], [e, a, t]]],
155
+ [[-1, 0, 0], [[-e, -a, -t], [-e, -a, t], [-e, a, t], [-e, a, -t]]],
156
+ [[0, 1, 0], [[-e, a, t], [e, a, t], [e, a, -t], [-e, a, -t]]],
157
+ [[0, -1, 0], [[-e, -a, -t], [e, -a, -t], [e, -a, t], [-e, -a, t]]]
158
+ ];
159
+ for (const [[r, l, d], u] of o) {
160
+ const [h, f, x, g] = u.map(([p, M, b]) => n.vertex(p, M, b, r, l, d));
161
+ n.quad(h, f, x, g);
162
+ }
163
+ return n.build();
164
+ }
165
+ function G([s, i], c = 1) {
166
+ const n = new j(), e = Math.max(1, Math.round(c)), a = [];
167
+ for (let t = 0; t <= e; t++) {
168
+ const o = i / 2 - i * t / e;
169
+ a.push(Array.from({ length: e + 1 }, (r, l) => n.vertex(-s / 2 + s * l / e, 0, o, 0, 1, 0)));
170
+ }
171
+ for (let t = 0; t < e; t++)
172
+ for (let o = 0; o < e; o++) n.quad(a[t][o], a[t][o + 1], a[t + 1][o + 1], a[t + 1][o]);
173
+ return n.build();
174
+ }
175
+ function J(s, i = 16) {
176
+ const c = new j(), n = Math.max(6, Math.round(i)), e = Math.max(3, Math.round(n / 2)), a = [];
177
+ for (let t = 0; t <= e; t++) {
178
+ const o = t / e * Math.PI, r = [];
179
+ for (let l = 0; l <= n; l++) {
180
+ const d = l / n * k, u = Math.sin(o) * Math.sin(d), h = Math.cos(o), f = Math.sin(o) * Math.cos(d);
181
+ r.push(c.vertex(u * s, h * s, f * s, u, h, f));
182
+ }
183
+ a.push(r);
184
+ }
185
+ for (let t = 0; t < e; t++)
186
+ for (let o = 0; o < n; o++) {
187
+ const r = a[t][o], l = a[t + 1][o], d = a[t + 1][o + 1], u = a[t][o + 1];
188
+ t > 0 && c.triangle(r, l, u), t < e - 1 && c.triangle(l, d, u);
189
+ }
190
+ return c.build();
191
+ }
192
+ function N(s, i, c, n, e) {
193
+ const a = s.vertex(0, c, 0, 0, n, 0), t = Array.from({ length: e }, (o, r) => {
194
+ const l = r / e * k;
195
+ return s.vertex(Math.sin(l) * i, c, Math.cos(l) * i, 0, n, 0);
196
+ });
197
+ for (let o = 0; o < e; o++) {
198
+ const r = t[o], l = t[(o + 1) % e];
199
+ n === 1 ? s.triangle(a, r, l) : s.triangle(a, l, r);
200
+ }
201
+ }
202
+ function K(s, i, c = 24) {
203
+ const n = new j(), e = Math.max(6, Math.round(c)), a = i / 2, t = -i / 2, o = (d) => Array.from({ length: e + 1 }, (u, h) => {
204
+ const f = h / e * k;
205
+ return n.vertex(Math.sin(f) * s, d, Math.cos(f) * s, Math.sin(f), 0, Math.cos(f));
206
+ }), r = o(t), l = o(a);
207
+ for (let d = 0; d < e; d++) n.quad(r[d], r[d + 1], l[d + 1], l[d]);
208
+ return N(n, s, a, 1, e), N(n, s, t, -1, e), n.build();
209
+ }
210
+ function Q(s, i, c = 24) {
211
+ const n = new j(), e = Math.max(6, Math.round(c)), a = i / 2, t = -i / 2, o = s / i;
212
+ for (let r = 0; r < e; r++) {
213
+ const l = r / e * k, d = (r + 1) / e * k, u = (l + d) / 2, h = n.vertex(Math.sin(l) * s, t, Math.cos(l) * s, Math.sin(l), o, Math.cos(l)), f = n.vertex(Math.sin(d) * s, t, Math.cos(d) * s, Math.sin(d), o, Math.cos(d)), x = n.vertex(0, a, 0, Math.sin(u), o, Math.cos(u));
214
+ n.triangle(h, f, x);
215
+ }
216
+ return N(n, s, t, -1, e), n.build();
217
+ }
218
+ function R(s, i, c = 24) {
219
+ const n = new j(), e = Math.max(6, Math.round(c)), a = Math.max(4, Math.round(e / 2)), t = [];
220
+ for (let o = 0; o <= e; o++) {
221
+ const r = o / e * k, l = [];
222
+ for (let d = 0; d <= a; d++) {
223
+ const u = d / a * k, h = Math.cos(u) * Math.sin(r), f = Math.sin(u), x = Math.cos(u) * Math.cos(r), g = s + i * Math.cos(u);
224
+ l.push(n.vertex(g * Math.sin(r), i * Math.sin(u), g * Math.cos(r), h, f, x));
225
+ }
226
+ t.push(l);
227
+ }
228
+ for (let o = 0; o < e; o++)
229
+ for (let r = 0; r < a; r++) n.quad(t[o][r], t[o + 1][r], t[o + 1][r + 1], t[o][r + 1]);
230
+ return n.build();
231
+ }
232
+ function V(s) {
233
+ switch (s.type) {
234
+ case "box":
235
+ return Z(s.size);
236
+ case "plane":
237
+ return G(s.size, s.segments);
238
+ case "sphere":
239
+ return J(s.radius, s.segments);
240
+ case "cylinder":
241
+ return K(s.radius, s.height, s.segments);
242
+ case "cone":
243
+ return Q(s.radius, s.height, s.segments);
244
+ case "torus":
245
+ return R(s.radius, s.tube, s.segments);
246
+ case "extrude":
247
+ return W(s.path, { depth: s.depth, width: s.width, curveSegments: s.curveSegments });
248
+ }
249
+ }
250
+ const tt = [
251
+ { name: "position", vector: "position", components: ["x", "y", "z"] },
252
+ { name: "rotation", vector: "quaternion", components: ["rotateX", "rotateY", "rotateZ"] }
253
+ ];
254
+ function nt(s, i = []) {
255
+ const c = [], n = /* @__PURE__ */ new Map();
256
+ for (const t of s.objects) {
257
+ if (t.id ? n.has(t.id) && c.push(`two objects share the id "${t.id}"`) : c.push("an object has no id"), t.parent !== void 0 && !n.has(t.parent) && c.push(`"${t.id}" has parent "${t.parent}", which must be an earlier object`), t.quaternion && t.quaternion.length !== 4 && c.push(`"${t.id}": a quaternion has four numbers`), t.kind === "mesh") {
258
+ s.materials?.[t.material] || c.push(`"${t.id}" uses material "${t.material}", which is not in materials`);
259
+ const o = t.geometry, r = o.type === "box" || o.type === "plane" ? o.size : o.type === "torus" ? [o.radius, o.tube] : o.type === "sphere" ? [o.radius] : o.type === "extrude" ? [o.depth, o.width ?? 1] : [o.radius, o.height];
260
+ o.type === "extrude" && !(typeof o.path == "string" && /^\s*[Mm]/.test(o.path)) && c.push(`"${t.id}": an extrude needs a path that starts with M`), r.every((l) => Number.isFinite(l) && l > 0) || c.push(`"${t.id}": a ${o.type}'s sizes must be positive`);
261
+ }
262
+ t.kind === "camera" && (t.near > 0 && t.far > t.near || c.push(`camera "${t.id}": near must be above 0 and far beyond it`), t.projection === "perspective" && !(t.fov > 0 && t.fov < 180) && c.push(`camera "${t.id}": fov is between 0 and 180 degrees`), t.projection === "orthographic" && !(t.height > 0) && c.push(`camera "${t.id}": height must be positive`)), t.id && n.set(t.id, t);
263
+ }
264
+ const e = n.get(s.camera);
265
+ e ? e.kind !== "camera" && c.push(`the scene's camera "${s.camera}" is a ${e.kind}, not a camera`) : c.push(`the scene's camera "${s.camera}" is not one of its objects`), s.fog && !(s.fog.far > s.fog.near) && c.push("fog: far must be beyond near");
266
+ const a = /* @__PURE__ */ new Map();
267
+ for (const t of i) {
268
+ if (!t.target.startsWith(`${s.id}/`)) continue;
269
+ const o = t.target.slice(s.id.length + 1);
270
+ a.has(o) || a.set(o, /* @__PURE__ */ new Set()), a.get(o).add(t.property);
271
+ }
272
+ for (const [t, o] of a) {
273
+ n.has(t) || c.push(`a track animates "${s.id}/${t}", which is not in the scene`);
274
+ for (const r of tt)
275
+ o.has(r.vector) && r.components.some((l) => o.has(l)) && c.push(`"${t}": its ${r.name} is animated both by ${r.vector} and by ${r.components.join(" / ")}; use one`);
276
+ }
277
+ return c;
278
+ }
279
+ function st(s) {
280
+ const i = Array.from({ length: s.indices.length / 3 }, () => []);
281
+ for (const c of C(s))
282
+ for (const n of c.faces) {
283
+ const e = c.faces.find((a) => a !== n) ?? -1;
284
+ i[n].push({ a: c.a, b: c.b, across: e });
285
+ }
286
+ return { ...s, faceEdges: i };
287
+ }
288
+ function at(s, i = {}) {
289
+ const c = new Map((i.kinds ?? []).map((t) => [t.kind, t])), n = nt(s);
290
+ for (const t of s.objects) {
291
+ if (et.has(t.kind)) continue;
292
+ const o = c.get(t.kind);
293
+ o ? n.push(...(o.validate?.(t) ?? []).map((r) => `"${t.id}": ${r}`)) : n.push(`"${t.id}" is a ${t.kind}: load the scene with that kind (for characters: loadScene3D(scene, { kinds: [characterObjects] }), from @algorisys/tinyfly/characters)`);
294
+ }
295
+ if (n.length > 0) throw new Error(`scene-3d: "${s.id}" is not valid:
296
+ - ${n.join(`
297
+ - `)}`);
298
+ const e = /* @__PURE__ */ new Map(), a = /* @__PURE__ */ new Map();
299
+ for (const t of s.objects) {
300
+ t.kind === "mesh" && e.set(t.id, st(V(t.geometry)));
301
+ const o = c.get(t.kind);
302
+ o?.prepare && a.set(t.id, o.prepare(t));
303
+ }
304
+ return { scene: s, meshes: e, kinds: c, prepared: a };
305
+ }
306
+ const et = /* @__PURE__ */ new Set(["group", "mesh", "camera", "light"]);
307
+ export {
308
+ j as M,
309
+ K as a,
310
+ Z as b,
311
+ Q as c,
312
+ _ as d,
313
+ W as e,
314
+ st as f,
315
+ V as g,
316
+ J as h,
317
+ U as i,
318
+ at as l,
319
+ C as m,
320
+ G as p,
321
+ I as s,
322
+ R as t,
323
+ nt as v
324
+ };
@@ -0,0 +1,237 @@
1
+ import { p as _ } from "./shading-ByG9UXd0.js";
2
+ import { mat4 as h, vec3 as v } from "@algorisys/tinyfly";
3
+ const s = 8, p = { unlit: 0, flat: 1, lambert: 2, toon: 3 }, w = { ambient: 0, directional: 1, point: 2, spot: 3 }, A = `#version 300 es
4
+ in vec3 a_position;
5
+ in vec3 a_normal;
6
+ uniform mat4 u_model;
7
+ uniform mat4 u_view;
8
+ uniform mat4 u_projection;
9
+ uniform mat3 u_normalMatrix;
10
+ out vec3 v_world;
11
+ out vec3 v_normal;
12
+ void main() {
13
+ vec4 world = u_model * vec4(a_position, 1.0);
14
+ v_world = world.xyz;
15
+ v_normal = u_normalMatrix * a_normal;
16
+ gl_Position = u_projection * u_view * world;
17
+ }
18
+ `, b = `#version 300 es
19
+ precision highp float;
20
+ #define MAX_LIGHTS ${s}
21
+ uniform int u_lightCount;
22
+ uniform int u_lightKind[MAX_LIGHTS];
23
+ uniform vec3 u_lightColor[MAX_LIGHTS];
24
+ uniform float u_lightIntensity[MAX_LIGHTS];
25
+ uniform vec3 u_lightPosition[MAX_LIGHTS];
26
+ uniform vec3 u_lightDirection[MAX_LIGHTS];
27
+ uniform float u_lightRange[MAX_LIGHTS];
28
+ uniform vec2 u_lightCone[MAX_LIGHTS];
29
+ uniform vec3 u_color;
30
+ uniform float u_opacity;
31
+ uniform int u_shading;
32
+ uniform float u_bands;
33
+ uniform vec3 u_eye;
34
+ uniform vec3 u_forward;
35
+ uniform bool u_orthographic;
36
+ uniform bool u_fog;
37
+ uniform vec3 u_fogColor;
38
+ uniform vec2 u_fogRange;
39
+ in vec3 v_world;
40
+ in vec3 v_normal;
41
+ out vec4 outColor;
42
+
43
+ vec3 lightAt(vec3 p, vec3 n) {
44
+ vec3 total = vec3(0.0);
45
+ for (int i = 0; i < MAX_LIGHTS; i++) {
46
+ if (i >= u_lightCount) break;
47
+ float amount;
48
+ int kind = u_lightKind[i];
49
+ if (kind == 0) {
50
+ amount = u_lightIntensity[i];
51
+ } else if (kind == 1) {
52
+ amount = u_lightIntensity[i] * max(0.0, -dot(n, u_lightDirection[i]));
53
+ } else {
54
+ vec3 toLight = u_lightPosition[i] - p;
55
+ float distance = length(toLight);
56
+ vec3 l = distance > 0.0 ? toLight / distance : n;
57
+ float fade = u_lightRange[i] > 0.0 ? pow(max(0.0, 1.0 - distance / u_lightRange[i]), 2.0) : 1.0;
58
+ amount = u_lightIntensity[i] * max(0.0, dot(n, l)) * fade;
59
+ if (kind == 3) amount *= smoothstep(u_lightCone[i].x, u_lightCone[i].y, -dot(l, u_lightDirection[i]));
60
+ }
61
+ total += u_lightColor[i] * amount;
62
+ }
63
+ return total;
64
+ }
65
+
66
+ void main() {
67
+ vec3 rgb = u_color;
68
+ if (u_shading != 0) {
69
+ // Flat: the face's own normal, from the surface's slope; otherwise the smooth one.
70
+ vec3 n = u_shading == 1 ? normalize(cross(dFdx(v_world), dFdy(v_world))) : normalize(v_normal);
71
+ vec3 toEye = u_orthographic ? -u_forward : normalize(u_eye - v_world);
72
+ if (dot(n, toEye) < 0.0) n = -n;
73
+ vec3 light = lightAt(v_world, n);
74
+ if (u_shading == 3) {
75
+ // Band the brightness, not each channel, so coloured lights keep their hue.
76
+ float level = max(light.r, max(light.g, light.b));
77
+ float steps = max(2.0, floor(u_bands + 0.5));
78
+ float banded = min(1.2, ceil(min(1.0, level) * steps - 1e-6) / steps);
79
+ light = level > 0.0 ? light * (banded / level) : vec3(0.0);
80
+ }
81
+ rgb *= light;
82
+ }
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
+ `, E = `#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
+ `, y = `#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
+ `, R = [
117
+ { id: "default-ambient", light: "ambient", color: "#ffffff", intensity: 0.35, position: [0, 0, 0], direction: [0, -1, 0], angle: 30 },
118
+ { id: "default-key", light: "directional", color: "#ffffff", intensity: 0.8, position: [3, 5, 4], direction: v.normalize([-3, -5, -4]), angle: 30 }
119
+ ];
120
+ function F(l) {
121
+ const t = (l.length > 0 ? l : R).slice(0, s), i = {
122
+ count: t.length,
123
+ kind: new Int32Array(s),
124
+ color: new Float32Array(s * 3),
125
+ intensity: new Float32Array(s),
126
+ position: new Float32Array(s * 3),
127
+ direction: new Float32Array(s * 3),
128
+ range: new Float32Array(s),
129
+ cone: new Float32Array(s * 2)
130
+ };
131
+ return t.forEach((o, n) => {
132
+ i.kind[n] = w[o.light], i.color.set(_(o.color), n * 3), i.intensity[n] = o.intensity, i.position.set(o.position, n * 3), i.direction.set(o.direction, n * 3), i.range[n] = o.range ?? 0, i.cone[n * 2] = Math.cos(o.angle * Math.PI / 180), i.cone[n * 2 + 1] = Math.cos(o.angle * 0.8 * Math.PI / 180);
133
+ }), i;
134
+ }
135
+ function L(l, t, i) {
136
+ return {
137
+ color: _(t ?? l.color),
138
+ opacity: i,
139
+ shading: p[l.shading ?? "lambert"],
140
+ bands: l.bands ?? 3
141
+ };
142
+ }
143
+ function g(l) {
144
+ const t = h.transpose(h.invert(l) ?? h.identity());
145
+ return new Float32Array([t[0], t[1], t[2], t[4], t[5], t[6], t[8], t[9], t[10]]);
146
+ }
147
+ class C {
148
+ gl;
149
+ surface;
150
+ outline;
151
+ buffers = /* @__PURE__ */ new WeakMap();
152
+ overlay;
153
+ constructor(t, i = {}) {
154
+ this.gl = t, this.overlay = i.overlay, this.surface = m(t, A, b), this.outline = m(t, E, y);
155
+ }
156
+ render(t) {
157
+ const i = this.gl, o = i.drawingBufferWidth, n = i.drawingBufferHeight;
158
+ i.viewport(0, 0, o, n);
159
+ const e = t.background && t.background !== "transparent" ? _(t.background) : null;
160
+ i.clearColor(e?.[0] ?? 0, e?.[1] ?? 0, e?.[2] ?? 0, e ? 1 : 0), i.clearDepth(1), i.clear(i.COLOR_BUFFER_BIT | i.DEPTH_BUFFER_BIT), i.enable(i.DEPTH_TEST), i.depthFunc(i.LEQUAL), i.enable(i.CULL_FACE), i.frontFace(i.CCW);
161
+ const a = t.meshes.filter((r) => r.opacity >= 1), c = t.camera.position, u = (r) => Math.hypot(r.world[12] - c[0], r.world[13] - c[1], r.world[14] - c[2]), d = t.meshes.filter((r) => r.opacity < 1).sort((r, f) => u(f) - u(r) || r.objectIndex - f.objectIndex);
162
+ i.disable(i.BLEND), i.depthMask(!0);
163
+ for (const r of a) r.material.outline && this.drawOutline(t, r, o, n);
164
+ for (const r of a) this.drawSurface(t, r);
165
+ i.enable(i.BLEND), i.blendFunc(i.SRC_ALPHA, i.ONE_MINUS_SRC_ALPHA), i.depthMask(!1);
166
+ for (const r of d) this.drawSurface(t, r);
167
+ if (i.depthMask(!0), i.disable(i.BLEND), this.overlay && t.drawables.length > 0) {
168
+ const r = this.overlay;
169
+ r.save(), r.setTransform(r.canvas.width / t.width, 0, 0, r.canvas.height / t.height, 0, 0), r.clearRect(0, 0, t.width, t.height);
170
+ for (const f of t.drawables)
171
+ r.save(), f.draw(r, t), r.restore();
172
+ r.restore();
173
+ } else this.overlay && this.overlay.clearRect(0, 0, this.overlay.canvas.width, this.overlay.canvas.height);
174
+ }
175
+ drawSurface(t, i) {
176
+ const o = this.gl, n = this.surface;
177
+ o.useProgram(n.program), i.material.doubleSided ? o.disable(o.CULL_FACE) : (o.enable(o.CULL_FACE), o.cullFace(o.BACK)), this.bindMesh(n, i.mesh);
178
+ const e = n.uniform;
179
+ o.uniformMatrix4fv(e("u_model"), !1, i.world), o.uniformMatrix4fv(e("u_view"), !1, t.camera.view), o.uniformMatrix4fv(e("u_projection"), !1, t.camera.projection), o.uniformMatrix3fv(e("u_normalMatrix"), !1, g(i.world));
180
+ const a = F(t.lights);
181
+ o.uniform1i(e("u_lightCount"), a.count), o.uniform1iv(e("u_lightKind"), a.kind), o.uniform3fv(e("u_lightColor"), a.color), o.uniform1fv(e("u_lightIntensity"), a.intensity), o.uniform3fv(e("u_lightPosition"), a.position), o.uniform3fv(e("u_lightDirection"), a.direction), o.uniform1fv(e("u_lightRange"), a.range), o.uniform2fv(e("u_lightCone"), a.cone);
182
+ const c = L(i.material, i.color, i.opacity);
183
+ o.uniform3fv(e("u_color"), c.color), o.uniform1f(e("u_opacity"), c.opacity), o.uniform1i(e("u_shading"), c.shading), o.uniform1f(e("u_bands"), c.bands), o.uniform3fv(e("u_eye"), t.camera.position), o.uniform3fv(e("u_forward"), t.camera.forward), o.uniform1i(e("u_orthographic"), t.camera.orthographic ? 1 : 0), o.uniform1i(e("u_fog"), t.fog ? 1 : 0), t.fog && (o.uniform3fv(e("u_fogColor"), _(t.fog.color)), o.uniform2f(e("u_fogRange"), t.fog.near, t.fog.far)), o.drawElements(o.TRIANGLES, i.mesh.indices.length, o.UNSIGNED_INT, 0);
184
+ }
185
+ drawOutline(t, i, o, n) {
186
+ const e = this.gl, a = this.outline, c = i.material.outline;
187
+ e.useProgram(a.program), e.enable(e.CULL_FACE), e.cullFace(e.FRONT), this.bindMesh(a, i.mesh);
188
+ const u = a.uniform;
189
+ e.uniformMatrix4fv(u("u_model"), !1, i.world), e.uniformMatrix4fv(u("u_view"), !1, t.camera.view), e.uniformMatrix4fv(u("u_projection"), !1, t.camera.projection), e.uniformMatrix3fv(u("u_normalMatrix"), !1, g(i.world)), e.uniform2f(u("u_viewport"), o, n), e.uniform1f(u("u_width"), c.width * (o / t.width));
190
+ const [d, r, f] = _(c.color);
191
+ e.uniform4f(u("u_ink"), d, r, f, 1), e.drawElements(e.TRIANGLES, i.mesh.indices.length, e.UNSIGNED_INT, 0), e.cullFace(e.BACK);
192
+ }
193
+ /** Upload a mesh once; bind its buffers to the program's attributes. */
194
+ bindMesh(t, i) {
195
+ const o = this.gl;
196
+ let n = this.buffers.get(i);
197
+ if (!n) {
198
+ const a = o.createBuffer();
199
+ o.bindBuffer(o.ARRAY_BUFFER, a), o.bufferData(o.ARRAY_BUFFER, new Float32Array(i.positions), o.STATIC_DRAW);
200
+ const c = o.createBuffer();
201
+ o.bindBuffer(o.ARRAY_BUFFER, c), o.bufferData(o.ARRAY_BUFFER, new Float32Array(i.normals), o.STATIC_DRAW);
202
+ const u = o.createBuffer();
203
+ o.bindBuffer(o.ELEMENT_ARRAY_BUFFER, u), o.bufferData(o.ELEMENT_ARRAY_BUFFER, new Uint32Array(i.indices), o.STATIC_DRAW), n = { position: a, normal: c, index: u }, this.buffers.set(i, n);
204
+ }
205
+ const e = (a, c) => {
206
+ const u = o.getAttribLocation(t.program, a);
207
+ u < 0 || (o.bindBuffer(o.ARRAY_BUFFER, c), o.enableVertexAttribArray(u), o.vertexAttribPointer(u, 3, o.FLOAT, !1, 0, 0));
208
+ };
209
+ e("a_position", n.position), e("a_normal", n.normal), o.bindBuffer(o.ELEMENT_ARRAY_BUFFER, n.index);
210
+ }
211
+ /** Release the programs (mesh buffers go with their meshes). */
212
+ destroy() {
213
+ this.gl.deleteProgram(this.surface.program), this.gl.deleteProgram(this.outline.program);
214
+ }
215
+ }
216
+ function m(l, t, i) {
217
+ const o = (a, c) => {
218
+ const u = l.createShader(a);
219
+ if (l.shaderSource(u, c), l.compileShader(u), !l.getShaderParameter(u, l.COMPILE_STATUS)) throw new Error(`scene-3d webgl: shader failed to compile — ${l.getShaderInfoLog(u)}`);
220
+ return u;
221
+ }, n = l.createProgram();
222
+ if (l.attachShader(n, o(l.VERTEX_SHADER, t)), l.attachShader(n, o(l.FRAGMENT_SHADER, i)), l.linkProgram(n), !l.getProgramParameter(n, l.LINK_STATUS)) throw new Error(`scene-3d webgl: program failed to link — ${l.getProgramInfoLog(n)}`);
223
+ const e = /* @__PURE__ */ new Map();
224
+ return {
225
+ program: n,
226
+ uniform: (a) => (e.has(a) || e.set(a, l.getUniformLocation(n, a)), e.get(a))
227
+ };
228
+ }
229
+ export {
230
+ w as LIGHT_CODE,
231
+ s as MAX_LIGHTS,
232
+ p as SHADING_CODE,
233
+ C as WebGL2Renderer,
234
+ g as normalMatrix3,
235
+ F as packLights,
236
+ L as surfaceUniforms
237
+ };