@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,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
+ };
@@ -1,4 +1,4 @@
1
- import { s as B, p as K, a as v, c as R } from "./sketch-CA8ZXhxI.js";
1
+ import { s as B, p as K, a as v, b as R } from "./sketch-CYPUbk3Q.js";
2
2
  const T = 256, N = 85.05112878, O = (a) => Math.max(-N, Math.min(N, a));
3
3
  function A(a, n) {
4
4
  const e = Math.sin(O(n) * Math.PI / 180);
@@ -0,0 +1,240 @@
1
+ import { p as _ } from "./shading-o5T_v9DT.js";
2
+ import { mat4 as h, vec3 as v } from "@algorisys/tinyfly";
3
+ const c = 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 ${c}
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 vec3 u_emissive;
31
+ uniform float u_opacity;
32
+ uniform int u_shading;
33
+ uniform float u_bands;
34
+ uniform vec3 u_eye;
35
+ uniform vec3 u_forward;
36
+ uniform bool u_orthographic;
37
+ uniform bool u_fog;
38
+ uniform vec3 u_fogColor;
39
+ uniform vec2 u_fogRange;
40
+ in vec3 v_world;
41
+ in vec3 v_normal;
42
+ out vec4 outColor;
43
+
44
+ vec3 lightAt(vec3 p, vec3 n) {
45
+ vec3 total = vec3(0.0);
46
+ for (int i = 0; i < MAX_LIGHTS; i++) {
47
+ if (i >= u_lightCount) break;
48
+ float amount;
49
+ int kind = u_lightKind[i];
50
+ if (kind == 0) {
51
+ amount = u_lightIntensity[i];
52
+ } else if (kind == 1) {
53
+ amount = u_lightIntensity[i] * max(0.0, -dot(n, u_lightDirection[i]));
54
+ } else {
55
+ vec3 toLight = u_lightPosition[i] - p;
56
+ float distance = length(toLight);
57
+ vec3 l = distance > 0.0 ? toLight / distance : n;
58
+ float fade = u_lightRange[i] > 0.0 ? pow(max(0.0, 1.0 - distance / u_lightRange[i]), 2.0) : 1.0;
59
+ amount = u_lightIntensity[i] * max(0.0, dot(n, l)) * fade;
60
+ if (kind == 3) amount *= smoothstep(u_lightCone[i].x, u_lightCone[i].y, -dot(l, u_lightDirection[i]));
61
+ }
62
+ total += u_lightColor[i] * amount;
63
+ }
64
+ return total;
65
+ }
66
+
67
+ void main() {
68
+ vec3 rgb = u_color;
69
+ if (u_shading != 0) {
70
+ // Flat: the face's own normal, from the surface's slope; otherwise the smooth one.
71
+ vec3 n = u_shading == 1 ? normalize(cross(dFdx(v_world), dFdy(v_world))) : normalize(v_normal);
72
+ vec3 toEye = u_orthographic ? -u_forward : normalize(u_eye - v_world);
73
+ if (dot(n, toEye) < 0.0) n = -n;
74
+ vec3 light = lightAt(v_world, n);
75
+ if (u_shading == 3) {
76
+ // Band the brightness, not each channel, so coloured lights keep their hue.
77
+ float level = max(light.r, max(light.g, light.b));
78
+ float steps = max(2.0, floor(u_bands + 0.5));
79
+ float banded = min(1.2, ceil(min(1.0, level) * steps - 1e-6) / steps);
80
+ light = level > 0.0 ? light * (banded / level) : vec3(0.0);
81
+ }
82
+ rgb *= light;
83
+ }
84
+ rgb += u_emissive;
85
+ if (u_fog) {
86
+ float amount = clamp((distance(v_world, u_eye) - u_fogRange.x) / (u_fogRange.y - u_fogRange.x), 0.0, 1.0);
87
+ rgb = mix(rgb, u_fogColor, amount);
88
+ }
89
+ outColor = vec4(clamp(rgb, 0.0, 1.0), u_opacity);
90
+ }
91
+ `, E = `#version 300 es
92
+ in vec3 a_position;
93
+ in vec3 a_normal;
94
+ uniform mat4 u_model;
95
+ uniform mat4 u_view;
96
+ uniform mat4 u_projection;
97
+ uniform mat3 u_normalMatrix;
98
+ uniform vec2 u_viewport;
99
+ uniform float u_width;
100
+ void main() {
101
+ mat4 viewProjection = u_projection * u_view;
102
+ vec4 clip = viewProjection * u_model * vec4(a_position, 1.0);
103
+ vec3 worldNormal = normalize(u_normalMatrix * a_normal);
104
+ vec4 tip = viewProjection * (u_model * vec4(a_position, 1.0) + vec4(worldNormal * 0.01, 0.0));
105
+ vec2 direction = tip.xy / tip.w - clip.xy / clip.w;
106
+ float length2 = dot(direction, direction);
107
+ vec2 push = length2 > 0.0 ? direction * inversesqrt(length2) : vec2(0.0);
108
+ clip.xy += push * u_width * 2.0 / u_viewport * clip.w;
109
+ gl_Position = clip;
110
+ }
111
+ `, y = `#version 300 es
112
+ precision highp float;
113
+ uniform vec4 u_ink;
114
+ out vec4 outColor;
115
+ void main() {
116
+ outColor = u_ink;
117
+ }
118
+ `, R = [
119
+ { id: "default-ambient", light: "ambient", color: "#ffffff", intensity: 0.35, position: [0, 0, 0], direction: [0, -1, 0], angle: 30 },
120
+ { id: "default-key", light: "directional", color: "#ffffff", intensity: 0.8, position: [3, 5, 4], direction: v.normalize([-3, -5, -4]), angle: 30 }
121
+ ];
122
+ function F(a) {
123
+ const t = (a.length > 0 ? a : R).slice(0, c), i = {
124
+ count: t.length,
125
+ kind: new Int32Array(c),
126
+ color: new Float32Array(c * 3),
127
+ intensity: new Float32Array(c),
128
+ position: new Float32Array(c * 3),
129
+ direction: new Float32Array(c * 3),
130
+ range: new Float32Array(c),
131
+ cone: new Float32Array(c * 2)
132
+ };
133
+ return t.forEach((o, n) => {
134
+ 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);
135
+ }), i;
136
+ }
137
+ function L(a, t, i) {
138
+ return {
139
+ color: _(t ?? a.color),
140
+ emissive: a.emissive ? _(a.emissive) : [0, 0, 0],
141
+ opacity: i,
142
+ shading: p[a.shading ?? "lambert"],
143
+ bands: a.bands ?? 3
144
+ };
145
+ }
146
+ function m(a) {
147
+ const t = h.transpose(h.invert(a) ?? h.identity());
148
+ return new Float32Array([t[0], t[1], t[2], t[4], t[5], t[6], t[8], t[9], t[10]]);
149
+ }
150
+ class C {
151
+ gl;
152
+ surface;
153
+ outline;
154
+ buffers = /* @__PURE__ */ new WeakMap();
155
+ overlay;
156
+ constructor(t, i = {}) {
157
+ this.gl = t, this.overlay = i.overlay, this.surface = g(t, A, b), this.outline = g(t, E, y);
158
+ }
159
+ render(t) {
160
+ const i = this.gl, o = i.drawingBufferWidth, n = i.drawingBufferHeight;
161
+ i.viewport(0, 0, o, n);
162
+ const e = t.background && t.background !== "transparent" ? _(t.background) : null;
163
+ 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);
164
+ const l = t.meshes.filter((r) => r.opacity >= 1), s = t.camera.position, u = (r) => Math.hypot(r.world[12] - s[0], r.world[13] - s[1], r.world[14] - s[2]), d = t.meshes.filter((r) => r.opacity < 1).sort((r, f) => u(f) - u(r) || r.objectIndex - f.objectIndex);
165
+ i.disable(i.BLEND), i.depthMask(!0);
166
+ for (const r of l) r.material.outline && this.drawOutline(t, r, o, n);
167
+ for (const r of l) this.drawSurface(t, r);
168
+ i.enable(i.BLEND), i.blendFunc(i.SRC_ALPHA, i.ONE_MINUS_SRC_ALPHA), i.depthMask(!1);
169
+ for (const r of d) this.drawSurface(t, r);
170
+ if (i.depthMask(!0), i.disable(i.BLEND), this.overlay && t.drawables.length > 0) {
171
+ const r = this.overlay;
172
+ 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);
173
+ for (const f of t.drawables)
174
+ r.save(), f.draw(r, t), r.restore();
175
+ r.restore();
176
+ } else this.overlay && this.overlay.clearRect(0, 0, this.overlay.canvas.width, this.overlay.canvas.height);
177
+ }
178
+ drawSurface(t, i) {
179
+ const o = this.gl, n = this.surface;
180
+ 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);
181
+ const e = n.uniform;
182
+ 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, m(i.world));
183
+ const l = F(t.lights);
184
+ o.uniform1i(e("u_lightCount"), l.count), o.uniform1iv(e("u_lightKind"), l.kind), o.uniform3fv(e("u_lightColor"), l.color), o.uniform1fv(e("u_lightIntensity"), l.intensity), o.uniform3fv(e("u_lightPosition"), l.position), o.uniform3fv(e("u_lightDirection"), l.direction), o.uniform1fv(e("u_lightRange"), l.range), o.uniform2fv(e("u_lightCone"), l.cone);
185
+ const s = L(i.material, i.color, i.opacity);
186
+ o.uniform3fv(e("u_color"), s.color), o.uniform3fv(e("u_emissive"), s.emissive), o.uniform1f(e("u_opacity"), s.opacity), o.uniform1i(e("u_shading"), s.shading), o.uniform1f(e("u_bands"), s.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);
187
+ }
188
+ drawOutline(t, i, o, n) {
189
+ const e = this.gl, l = this.outline, s = i.material.outline;
190
+ e.useProgram(l.program), e.enable(e.CULL_FACE), e.cullFace(e.FRONT), this.bindMesh(l, i.mesh);
191
+ const u = l.uniform;
192
+ 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, m(i.world)), e.uniform2f(u("u_viewport"), o, n), e.uniform1f(u("u_width"), s.width * (o / t.width));
193
+ const [d, r, f] = _(s.color);
194
+ 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);
195
+ }
196
+ /** Upload a mesh once; bind its buffers to the program's attributes. */
197
+ bindMesh(t, i) {
198
+ const o = this.gl;
199
+ let n = this.buffers.get(i);
200
+ if (!n) {
201
+ const l = o.createBuffer();
202
+ o.bindBuffer(o.ARRAY_BUFFER, l), o.bufferData(o.ARRAY_BUFFER, new Float32Array(i.positions), o.STATIC_DRAW);
203
+ const s = o.createBuffer();
204
+ o.bindBuffer(o.ARRAY_BUFFER, s), o.bufferData(o.ARRAY_BUFFER, new Float32Array(i.normals), o.STATIC_DRAW);
205
+ const u = o.createBuffer();
206
+ o.bindBuffer(o.ELEMENT_ARRAY_BUFFER, u), o.bufferData(o.ELEMENT_ARRAY_BUFFER, new Uint32Array(i.indices), o.STATIC_DRAW), n = { position: l, normal: s, index: u }, this.buffers.set(i, n);
207
+ }
208
+ const e = (l, s) => {
209
+ const u = o.getAttribLocation(t.program, l);
210
+ u < 0 || (o.bindBuffer(o.ARRAY_BUFFER, s), o.enableVertexAttribArray(u), o.vertexAttribPointer(u, 3, o.FLOAT, !1, 0, 0));
211
+ };
212
+ e("a_position", n.position), e("a_normal", n.normal), o.bindBuffer(o.ELEMENT_ARRAY_BUFFER, n.index);
213
+ }
214
+ /** Release the programs (mesh buffers go with their meshes). */
215
+ destroy() {
216
+ this.gl.deleteProgram(this.surface.program), this.gl.deleteProgram(this.outline.program);
217
+ }
218
+ }
219
+ function g(a, t, i) {
220
+ const o = (l, s) => {
221
+ const u = a.createShader(l);
222
+ if (a.shaderSource(u, s), a.compileShader(u), !a.getShaderParameter(u, a.COMPILE_STATUS)) throw new Error(`scene-3d webgl: shader failed to compile — ${a.getShaderInfoLog(u)}`);
223
+ return u;
224
+ }, n = a.createProgram();
225
+ if (a.attachShader(n, o(a.VERTEX_SHADER, t)), a.attachShader(n, o(a.FRAGMENT_SHADER, i)), a.linkProgram(n), !a.getProgramParameter(n, a.LINK_STATUS)) throw new Error(`scene-3d webgl: program failed to link — ${a.getProgramInfoLog(n)}`);
226
+ const e = /* @__PURE__ */ new Map();
227
+ return {
228
+ program: n,
229
+ uniform: (l) => (e.has(l) || e.set(l, a.getUniformLocation(n, l)), e.get(l))
230
+ };
231
+ }
232
+ export {
233
+ w as LIGHT_CODE,
234
+ c as MAX_LIGHTS,
235
+ p as SHADING_CODE,
236
+ C as WebGL2Renderer,
237
+ m as normalMatrix3,
238
+ F as packLights,
239
+ L as surfaceUniforms
240
+ };