@algorisys/tinyfly 0.100.0 → 0.101.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 (58) hide show
  1. package/README.md +5 -5
  2. package/bin/tinyfly.mjs +7 -3
  3. package/docs/character-appearance.md +26 -0
  4. package/docs/editor-guide.md +1 -1
  5. package/docs/examples.md +1 -1
  6. package/docs/getting-started.md +1 -1
  7. package/docs/gsap-compat.md +1 -1
  8. package/docs/llm-guide.md +14 -2
  9. package/docs/maps.md +2 -2
  10. package/docs/scene-3d.md +23 -4
  11. package/docs/teaching.md +1 -1
  12. package/docs/video-rendering.md +154 -0
  13. package/lib/addons/adapters.js +387 -333
  14. package/lib/addons/characters.js +4467 -4448
  15. package/lib/addons/load-scene-oWNsYYC1.js +372 -0
  16. package/lib/addons/scene-3d-webgl.js +1 -1
  17. package/lib/addons/scene-3d.js +323 -277
  18. package/lib/addons/shading-0lPgnXmD.js +96 -0
  19. package/lib/browser/tinyfly-scene-3d.iife.js +2 -2
  20. package/lib/browser/tinyfly.iife.js +14 -14
  21. package/lib/browser/tinyfly.js +5888 -5511
  22. package/lib/browser/tinyfly.umd.js +14 -14
  23. package/lib/engine/tinyfly-engine.js +1389 -1031
  24. package/lib/engine/tinyfly-engine.umd.cjs +2 -2
  25. package/lib/headless/headless.js +39 -0
  26. package/lib/types/adapters/canvas/impossible.d.ts +15 -0
  27. package/lib/types/adapters/canvas/index.d.ts +3 -0
  28. package/lib/types/adapters/canvas/nested-zoom.d.ts +40 -0
  29. package/lib/types/adapters/canvas/zoom-blur.d.ts +49 -0
  30. package/lib/types/characters/character.d.ts +23 -3
  31. package/lib/types/characters/head/face.d.ts +19 -2
  32. package/lib/types/engine/authoring/beat-loop.d.ts +46 -0
  33. package/lib/types/engine/authoring/curve.d.ts +26 -0
  34. package/lib/types/engine/authoring/index.d.ts +3 -0
  35. package/lib/types/engine/authoring/periodic.d.ts +89 -0
  36. package/lib/types/engine/path/goo.d.ts +103 -0
  37. package/lib/types/engine/path/impossible.d.ts +92 -0
  38. package/lib/types/engine/path/index.d.ts +6 -0
  39. package/lib/types/engine/path/similarity.d.ts +48 -0
  40. package/lib/types/headless/index.d.ts +1 -0
  41. package/lib/types/headless/loop-audio.d.ts +21 -0
  42. package/lib/types/headless/node-audio.d.ts +20 -0
  43. package/lib/types/headless/video-scene.d.ts +9 -0
  44. package/lib/types/scene-3d/camera.d.ts +11 -2
  45. package/lib/types/scene-3d/geometry/mesh.d.ts +2 -0
  46. package/lib/types/scene-3d/geometry/primitives.d.ts +7 -0
  47. package/lib/types/scene-3d/impossible-3d.d.ts +29 -0
  48. package/lib/types/scene-3d/index.d.ts +4 -3
  49. package/lib/types/scene-3d/resolve-scene.d.ts +13 -1
  50. package/lib/types/scene-3d/scene-3d-adapter.d.ts +2 -0
  51. package/lib/types/scene-3d/scene-types.d.ts +29 -4
  52. package/lib/types/scene-3d/shading.d.ts +2 -0
  53. package/llms-full.txt +226 -15
  54. package/llms.txt +29 -29
  55. package/package.json +1 -1
  56. package/skills/tinyfly/SKILL.md +7 -0
  57. package/lib/addons/load-scene-CQvcFWGJ.js +0 -331
  58. package/lib/addons/shading-o5T_v9DT.js +0 -89
@@ -0,0 +1,96 @@
1
+ import { vec3 as f } from "@algorisys/tinyfly";
2
+ const b = /* @__PURE__ */ new Map();
3
+ function d(n) {
4
+ const i = b.get(n);
5
+ if (i) return i;
6
+ let a = [1, 1, 1];
7
+ const e = n.trim().replace("#", "");
8
+ if (n.trim().startsWith("#") && (e.length === 3 || e.length === 6)) {
9
+ const t = e.length === 3 ? e.split("").map((s) => s + s).join("") : e;
10
+ a = [0, 2, 4].map((s) => Number.parseInt(t.slice(s, s + 2), 16) / 255);
11
+ } else {
12
+ const t = n.match(/rgba?\(\s*([\d.]+)\s*,\s*([\d.]+)\s*,\s*([\d.]+)/);
13
+ t && (a = [Number(t[1]) / 255, Number(t[2]) / 255, Number(t[3]) / 255]);
14
+ }
15
+ return b.size < 512 && b.set(n, a), a;
16
+ }
17
+ const y = [
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
+ ], v = (n, i, a) => {
21
+ const e = Math.min(1, Math.max(0, (a - n) / (i - n)));
22
+ return e * e * (3 - 2 * e);
23
+ };
24
+ function g(n, i, a) {
25
+ const e = [0, 0, 0];
26
+ for (const t of a) {
27
+ const s = d(t.color);
28
+ let c;
29
+ if (t.light === "ambient")
30
+ c = t.intensity;
31
+ else if (t.light === "directional")
32
+ c = t.intensity * Math.max(0, -f.dot(i, t.direction));
33
+ else {
34
+ const o = f.subtract(t.position, n), m = f.length(o), u = m > 0 ? f.scale(o, 1 / m) : i, r = t.range ? Math.max(0, 1 - m / t.range) ** 2 : 1;
35
+ if (c = t.intensity * Math.max(0, f.dot(i, u)) * r, t.light === "spot") {
36
+ const l = Math.cos(t.angle * Math.PI / 180), h = Math.cos(t.angle * 0.8 * Math.PI / 180);
37
+ c *= v(l, h, -f.dot(u, t.direction));
38
+ }
39
+ }
40
+ e[0] += s[0] * c, e[1] += s[1] * c, e[2] += s[2] * c;
41
+ }
42
+ return e;
43
+ }
44
+ function $(n, i) {
45
+ const a = Math.max(2, Math.round(i));
46
+ return Math.min(1.2, Math.ceil(Math.min(1, n) * a - 1e-9) / a);
47
+ }
48
+ function x(n, i) {
49
+ return n ? Math.min(1, Math.max(0, (i - n.near) / (n.far - n.near))) : 0;
50
+ }
51
+ function N(n, i, a) {
52
+ const e = x(i, a);
53
+ if (!i || e <= 0) return n;
54
+ const t = d(n), s = d(i.color), c = (o) => Math.round(Math.min(1, Math.max(0, o)) * 255);
55
+ return `rgb(${c(t[0] + (s[0] - t[0]) * e)}, ${c(t[1] + (s[1] - t[1]) * e)}, ${c(t[2] + (s[2] - t[2]) * e)})`;
56
+ }
57
+ function A(n, i, a, e) {
58
+ const t = n.material, s = d(n.color ?? t.color), c = t.shading ?? "lambert";
59
+ let o;
60
+ if (c === "unlit")
61
+ o = [...s];
62
+ else {
63
+ const r = i.length > 0 ? i : y;
64
+ let l;
65
+ if (c === "lambert") {
66
+ const h = [0, 0, 0];
67
+ for (const M of n.vertexNormals) {
68
+ const p = g(n.centroid, M, r);
69
+ h[0] += p[0] / 3, h[1] += p[1] / 3, h[2] += p[2] / 3;
70
+ }
71
+ l = h;
72
+ } else if (l = g(n.centroid, n.normal, r), c === "toon") {
73
+ const h = Math.max(l[0], l[1], l[2]), M = h > 0 ? $(h, t.bands ?? 3) / h : 0;
74
+ l = [l[0] * M, l[1] * M, l[2] * M];
75
+ }
76
+ o = [s[0] * l[0], s[1] * l[1], s[2] * l[2]];
77
+ }
78
+ if (t.emissive) {
79
+ const r = d(t.emissive);
80
+ o = [o[0] + r[0], o[1] + r[1], o[2] + r[2]];
81
+ }
82
+ const m = x(e, f.distance(n.centroid, a.position));
83
+ if (e && m > 0) {
84
+ const r = d(e.color);
85
+ o = [o[0] + (r[0] - o[0]) * m, o[1] + (r[1] - o[1]) * m, o[2] + (r[2] - o[2]) * m];
86
+ }
87
+ const u = (r) => Math.round(Math.min(1, Math.max(0, r)) * 255);
88
+ return { color: `rgb(${u(o[0])}, ${u(o[1])}, ${u(o[2])})`, alpha: n.opacity };
89
+ }
90
+ export {
91
+ x as f,
92
+ N as i,
93
+ g as l,
94
+ d as p,
95
+ A as s
96
+ };
@@ -1,3 +1,3 @@
1
- (function(T){"use strict";const Gt=[{name:"position",vector:"position",components:["x","y","z"]},{name:"rotation",vector:"quaternion",components:["rotateX","rotateY","rotateZ"]}];function wt(t,s=[]){const i=[],n=new Map;for(const o of t.objects){if(o.id?n.has(o.id)&&i.push(`two objects share the id "${o.id}"`):i.push("an object has no id"),o.parent!==void 0&&!n.has(o.parent)&&i.push(`"${o.id}" has parent "${o.parent}", which must be an earlier object`),o.quaternion&&o.quaternion.length!==4&&i.push(`"${o.id}": a quaternion has four numbers`),o.kind==="mesh"){t.materials?.[o.material]||i.push(`"${o.id}" uses material "${o.material}", which is not in materials`);const r=o.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))&&i.push(`"${o.id}": an extrude needs a path that starts with M`),a.every(h=>Number.isFinite(h)&&h>0)||i.push(`"${o.id}": a ${r.type}'s sizes must be positive`)}if(o.kind==="line"||o.kind==="trail"){o.width!==void 0&&!(o.width>0)&&i.push(`"${o.id}": width must be positive`);for(const r of["taper","fade","opacity"]){const a=o[r];a!==void 0&&!(a>=0&&a<=1)&&i.push(`"${o.id}": ${r} is between 0 and 1`)}}o.kind==="line"&&(Array.isArray(o.points)&&o.points.length>=2&&o.points.every(a=>Array.isArray(a)&&a.length===3&&a.every(Number.isFinite))||i.push(`"${o.id}": a line needs two or more points of three numbers`)),o.kind==="trail"&&(o.length>0||i.push(`"${o.id}": a trail's length (ms) must be positive`),(o.follow===o.id||!t.objects.some(r=>r.id===o.follow))&&i.push(`"${o.id}" follows "${o.follow}", which is not another object in the scene`),o.samples!==void 0&&!(o.samples>=2)&&i.push(`"${o.id}": a trail needs at least 2 samples`)),o.kind==="camera"&&(o.near>0&&o.far>o.near||i.push(`camera "${o.id}": near must be above 0 and far beyond it`),o.projection==="perspective"&&!(o.fov>0&&o.fov<180)&&i.push(`camera "${o.id}": fov is between 0 and 180 degrees`),o.projection==="orthographic"&&!(o.height>0)&&i.push(`camera "${o.id}": height must be positive`)),o.id&&n.set(o.id,o)}const e=n.get(t.camera);e?e.kind!=="camera"&&i.push(`the scene's camera "${t.camera}" is a ${e.kind}, not a camera`):i.push(`the scene's camera "${t.camera}" is not one of its objects`),t.fog&&!(t.fog.far>t.fog.near)&&i.push("fog: far must be beyond near");const c=new Map;for(const o of s){if(!o.target.startsWith(`${t.id}/`))continue;const r=o.target.slice(t.id.length+1);c.has(r)||c.set(r,new Set),c.get(r).add(o.property)}for(const[o,r]of c){n.has(o)||i.push(`a track animates "${t.id}/${o}", which is not in the scene`);for(const a of Gt)r.has(a.vector)&&a.components.some(h=>r.has(h))&&i.push(`"${o}": its ${a.name} is animated both by ${a.vector} and by ${a.components.join(" / ")}; use one`)}return i}class z{positions=[];normals=[];indices=[];vertex(s,i,n,e,c,o){const r=Math.hypot(e,c,o)||1;return this.positions.push(s,i,n),this.normals.push(e/r,c/r,o/r),this.positions.length/3-1}triangle(s,i,n){this.indices.push(s,i,n)}quad(s,i,n,e){this.indices.push(s,i,n,s,n,e)}build(){return{positions:this.positions,normals:this.normals,indices:this.indices}}}function vt(t){const s=new Map,i=[];for(let e=0;e<t.positions.length/3;e++){const c=[0,1,2].map(o=>Math.round(t.positions[e*3+o]*1e6)).join(",");s.has(c)||s.set(c,e),i.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 o=i[t.indices[e*3+c]],r=i[t.indices[e*3+(c+1)%3]];if(o===r)continue;const a=o<r?`${o}-${r}`:`${r}-${o}`,h=n.get(a);h?h.faces.push(e):n.set(a,{a:Math.min(o,r),b:Math.max(o,r),faces:[e]})}return[...n.values()]}const xt=32,Bt=256,F=new Map,Jt=/[MmLlHhVvCcSsQqTtAaZz]/,Kt=/^[-+]?(?:\d+\.?\d*|\.\d+)(?:[eE][-+]?\d+)?/,tn={M:2,L:2,H:1,V:1,C:6,S:4,Q:4,T:2,A:7,Z:0};function nn(t){const s=[];let i=0,n=null;const e=()=>{for(;i<t.length&&/[\s,]/.test(t[i]);)i++};for(;i<t.length&&(e(),!(i>=t.length));){const c=t[i];if(Jt.test(c)){n={type:c,args:[]},s.push(n),i++;continue}if(!n)break;const o=n.type==="A"||n.type==="a",r=n.args.length%7;if(o&&(r===3||r===4)){if(c!=="0"&&c!=="1")break;n.args.push(c==="1"?1:0),i++;continue}const a=Kt.exec(t.slice(i));if(!a)break;n.args.push(parseFloat(a[0])),i+=a[0].length}return s}function en(t,s,i,n,e,c,o,r,a){if(t===r&&s===a)return[];let h=Math.abs(i),d=Math.abs(n);if(h===0||d===0)return[[t,s,r,a,r,a]];const u=e*Math.PI/180,f=Math.cos(u),p=Math.sin(u),M=(t-r)/2,g=(s-a)/2,m=f*M+p*g,l=-p*M+f*g,w=m*m/(h*h)+l*l/(d*d);if(w>1){const X=Math.sqrt(w);h*=X,d*=X}const A=c===o?-1:1,b=h*h*d*d-h*h*l*l-d*d*m*m,y=h*h*l*l+d*d*m*m,x=A*Math.sqrt(Math.max(0,b/y)),v=x*h*l/d,I=-x*d*m/h,E=f*v-p*I+(t+r)/2,S=p*v+f*I+(s+a)/2,k=(X,j,N,U)=>{const bt=X*N+j*U,nt=Math.sqrt((X*X+j*j)*(N*N+U*U)),B=Math.acos(Math.max(-1,Math.min(1,bt/nt)));return X*U-j*N<0?-B:B},P=k(1,0,(m-v)/h,(l-I)/d);let $=k((m-v)/h,(l-I)/d,(-m-v)/h,(-l-I)/d);!o&&$>0&&($-=2*Math.PI),o&&$<0&&($+=2*Math.PI);const C=Math.max(1,Math.ceil(Math.abs($)/(Math.PI/2))),G=$/C,R=4/3*Math.tan(G/4),Vt=X=>{const j=h*Math.cos(X),N=d*Math.sin(X);return[f*j-p*N+E,p*j+f*N+S]},Wt=X=>{const j=-h*Math.sin(X),N=d*Math.cos(X);return[f*j-p*N,p*j+f*N]},Zt=[];for(let X=0;X<C;X++){const j=P+X*G,N=j+G,[U,bt]=Vt(j),[nt,B]=X===C-1?[r,a]:Vt(N),[Dn,Hn]=Wt(j),[Qn,Rn]=Wt(N);Zt.push([U+R*Dn,bt+R*Hn,nt-R*Qn,B-R*Rn,nt,B])}return Zt}function kt(t,s,i,n,e){const c=1-e;return c*c*c*t+3*c*c*e*s+3*c*e*e*i+e*e*e*n}function V(t,s,i,n){return{subpath:0,type:"L",points:[i,n],startX:t,startY:s,endX:i,endY:n,length:Math.hypot(i-t,n-s)}}function J(t,s,i){const[n,e,c,o,r,a]=i,h=[0];let d=t,u=s,f=0;for(let p=1;p<=xt;p++){const M=p/xt,g=kt(t,n,c,r,M),m=kt(s,e,o,a,M);f+=Math.hypot(g-d,m-u),h.push(f),d=g,u=m}return{subpath:0,type:"C",points:[n,e,c,o,r,a],startX:t,startY:s,endX:r,endY:a,length:f,lengths:h}}function sn(t){const s=F.get(t);if(s)return s;const i=[];let n=0,e=0,c=0,o=0,r=null,a=null,h=-1;const d=new Set,u=g=>{h<0&&(h=0),g.subpath=h,i.push(g)};for(const{type:g,args:m}of nn(t)){const l=g.toUpperCase(),w=g!==l,A=tn[l];if(l==="Z"){(n!==c||e!==o)&&u(V(n,e,c,o)),h>=0&&d.add(h),n=c,e=o,r=a=null;continue}for(let b=0;b+A<=m.length;b+=A){const y=m.slice(b,b+A),x=w?n:0,v=w?e:0;let I=null,E=null;switch(l){case"M":b===0?(n=y[0]+x,e=y[1]+v,c=n,o=e,(h<0||i[i.length-1]?.subpath===h)&&h++):(u(V(n,e,y[0]+x,y[1]+v)),n=y[0]+x,e=y[1]+v);break;case"L":u(V(n,e,y[0]+x,y[1]+v)),n=y[0]+x,e=y[1]+v;break;case"H":u(V(n,e,y[0]+x,e)),n=y[0]+x;break;case"V":u(V(n,e,n,y[0]+v)),e=y[0]+v;break;case"C":{const S=[y[0]+x,y[1]+v,y[2]+x,y[3]+v,y[4]+x,y[5]+v];u(J(n,e,S)),I=[S[2],S[3]],n=S[4],e=S[5];break}case"S":{const[S,k]=r?[2*n-r[0],2*e-r[1]]:[n,e],P=[S,k,y[0]+x,y[1]+v,y[2]+x,y[3]+v];u(J(n,e,P)),I=[P[2],P[3]],n=P[4],e=P[5];break}case"Q":case"T":{let S=n,k=e;l==="Q"?(S=y[0]+x,k=y[1]+v):a&&(S=2*n-a[0],k=2*e-a[1]);const P=l==="Q"?y[2]+x:y[0]+x,$=l==="Q"?y[3]+v:y[1]+v;u(J(n,e,[n+2/3*(S-n),e+2/3*(k-e),P+2/3*(S-P),$+2/3*(k-$),P,$])),E=[S,k],n=P,e=$;break}case"A":{const S=y[5]+x,k=y[6]+v;let P=n,$=e;for(const C of en(n,e,y[0],y[1],y[2],y[3],y[4],S,k))u(J(P,$,C)),P=C[4],$=C[5];n=S,e=k;break}}r=I,a=E}}const f=i.reduce((g,m)=>g+m.length,0),p=[];for(let g=0;g<i.length;){const m=i[g].subpath;let l=g,w=0;for(;l<i.length&&i[l].subpath===m;)w+=i[l++].length;const A=i[g],b=i[l-1],y=d.has(m)||Math.abs(b.endX-A.startX)<1e-9&&Math.abs(b.endY-A.startY)<1e-9;p.push({start:g,end:l,length:w,closed:y}),g=l}const M={segments:i,totalLength:f,subpaths:p};return F.size>=Bt&&F.delete(F.keys().next().value),F.set(t,M),M}function Q(t){let s=0;for(let i=0;i<t.length;i++){const n=t[i],e=t[(i+1)%t.length];s+=n.x*e.y-e.x*n.y}return s}function et(t,s){let i=!1;for(let n=0,e=s.length-1;n<s.length;e=n++){const c=s[n],o=s[e];c.y>t.y!=o.y>t.y&&t.x<(o.x-c.x)*(t.y-c.y)/(o.y-c.y)+c.x&&(i=!i)}return i}const K=(t,s,i)=>(s.x-t.x)*(i.y-t.y)-(s.y-t.y)*(i.x-t.x),st=(t,s)=>t.x===s.x&&t.y===s.y;function ot(t,s,i,n){return K(s,i,t)>=0&&K(i,n,t)>=0&&K(n,s,t)>=0}function on(t,s){let i=0;for(let f=1;f<s.length;f++)s[f].x>s[i].x&&(i=f);const n=s[i];let e=-1,c=1/0;for(let f=0;f<t.length;f++){const p=t[f],M=t[(f+1)%t.length];if(p.y>n.y==M.y>n.y)continue;const g=p.x+(n.y-p.y)*(M.x-p.x)/(M.y-p.y);g>=n.x&&g<c&&(c=g,e=f)}if(e<0)return t;const o=t[e],r=t[(e+1)%t.length];let a=o.x>r.x?e:(e+1)%t.length;const h={x:c,y:n.y};let d=1/0;for(let f=0;f<t.length;f++){const p=t[f];if(f===a||p.x<n.x)continue;const M=t[a];if(!(ot(p,n,h,M)||ot(p,n,M,h)))continue;const m=Math.abs(Math.atan2(p.y-n.y,p.x-n.x));m<d&&(d=m,a=f)}const u=s.slice(i).concat(s.slice(0,i));return[...t.slice(0,a+1),...u,n,t[a],...t.slice(a+1)]}function At(t,s=[]){const i=Q(t)>=0?t:[...t].reverse(),n=s.map(u=>Q(u)<=0?u:[...u].reverse()),e=[...i,...n.flat()],c=new Map;e.forEach((u,f)=>c.set(u,f));const o=[...n].sort((u,f)=>Math.max(...f.map(p=>p.x))-Math.max(...u.map(p=>p.x)));let r=i;for(const u of o)r=on(r,u);const a=r.map(u=>c.get(u)),h=[];let d=a.length*a.length;for(;a.length>3&&d-- >0;){let u=!1;for(let f=0;f<a.length;f++){const p=a[(f+a.length-1)%a.length],M=a[f],g=a[(f+1)%a.length],[m,l,w]=[e[p],e[M],e[g]];if(K(m,l,w)<=0)continue;let A=!1;for(const b of a){const y=e[b];if(!(b===p||b===M||b===g||st(y,m)||st(y,l)||st(y,w))&&ot(y,m,l,w)){A=!0;break}}if(!A){h.push(p,M,g),a.splice(f,1),u=!0;break}}u||(h.push(a[a.length-1],a[0],a[1]),a.splice(0,1))}return a.length===3&&h.push(a[0],a[1],a[2]),{points:e,triangles:h}}function cn(t,s){const i=new Map;for(const n of sn(t).segments){i.has(n.subpath)||i.set(n.subpath,[{x:n.startX,y:-n.startY}]);const e=i.get(n.subpath);if(n.type==="L")e.push({x:n.endX,y:-n.endY});else{const[c,o,r,a,h,d]=n.points;for(let u=1;u<=s;u++){const f=u/s,p=1-f,M=p*p*p*n.startX+3*p*p*f*c+3*p*f*f*r+f*f*f*h,g=p*p*p*n.startY+3*p*p*f*o+3*p*f*f*a+f*f*f*d;e.push({x:M,y:-g})}}}return[...i.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(Q(n))>1e-12)}function $t(t,s){const i=cn(t,Math.max(1,Math.round(s.curveSegments??12))),n=new z;if(i.length===0)return n.build();const e=i.flat(),c=Math.min(...e.map(m=>m.x)),o=Math.max(...e.map(m=>m.x)),r=Math.min(...e.map(m=>m.y)),a=Math.max(...e.map(m=>m.y)),h=(s.width??1)/Math.max(o-c,1e-9),d=(c+o)/2,u=(r+a)/2,f=i.map(m=>m.map(l=>({x:(l.x-d)*h,y:(l.y-u)*h}))),p=f.map((m,l)=>f.filter((w,A)=>A!==l&&et(m[0],w)).length),M=s.depth/2,g=-s.depth/2;return f.forEach((m,l)=>{if(p[l]%2===1)return;const w=f.filter((v,I)=>p[I]===p[l]+1&&et(v[0],m)),{points:A,triangles:b}=At(m,w),y=A.map(v=>n.vertex(v.x,v.y,M,0,0,1)),x=A.map(v=>n.vertex(v.x,v.y,g,0,0,-1));for(let v=0;v<b.length;v+=3)n.triangle(y[b[v]],y[b[v+1]],y[b[v+2]]),n.triangle(x[b[v]],x[b[v+2]],x[b[v+1]]);for(const v of[Q(m)>=0?m:[...m].reverse(),...w.map(I=>Q(I)<=0?I:[...I].reverse())])for(let I=0;I<v.length;I++){const E=v[I],S=v[(I+1)%v.length],k=S.y-E.y,P=-(S.x-E.x),$=n.vertex(E.x,E.y,g,k,P,0),C=n.vertex(S.x,S.y,g,k,P,0),G=n.vertex(S.x,S.y,M,k,P,0),R=n.vertex(E.x,E.y,M,k,P,0);n.quad($,C,G,R)}}),n.build()}const q=Math.PI*2;function It([t,s,i]){const n=new z,e=t/2,c=s/2,o=i/2,r=[[[0,0,1],[[-e,-c,o],[e,-c,o],[e,c,o],[-e,c,o]]],[[0,0,-1],[[e,-c,-o],[-e,-c,-o],[-e,c,-o],[e,c,-o]]],[[1,0,0],[[e,-c,o],[e,-c,-o],[e,c,-o],[e,c,o]]],[[-1,0,0],[[-e,-c,-o],[-e,-c,o],[-e,c,o],[-e,c,-o]]],[[0,1,0],[[-e,c,o],[e,c,o],[e,c,-o],[-e,c,-o]]],[[0,-1,0],[[-e,-c,-o],[e,-c,-o],[e,-c,o],[-e,-c,o]]]];for(const[[a,h,d],u]of r){const[f,p,M,g]=u.map(([m,l,w])=>n.vertex(m,l,w,a,h,d));n.quad(f,p,M,g)}return n.build()}function St([t,s],i=1){const n=new z,e=Math.max(1,Math.round(i)),c=[];for(let o=0;o<=e;o++){const r=s/2-s*o/e;c.push(Array.from({length:e+1},(a,h)=>n.vertex(-t/2+t*h/e,0,r,0,1,0)))}for(let o=0;o<e;o++)for(let r=0;r<e;r++)n.quad(c[o][r],c[o][r+1],c[o+1][r+1],c[o+1][r]);return n.build()}function Pt(t,s=16){const i=new z,n=Math.max(6,Math.round(s)),e=Math.max(3,Math.round(n/2)),c=[];for(let o=0;o<=e;o++){const r=o/e*Math.PI,a=[];for(let h=0;h<=n;h++){const d=h/n*q,u=Math.sin(r)*Math.sin(d),f=Math.cos(r),p=Math.sin(r)*Math.cos(d);a.push(i.vertex(u*t,f*t,p*t,u,f,p))}c.push(a)}for(let o=0;o<e;o++)for(let r=0;r<n;r++){const a=c[o][r],h=c[o+1][r],d=c[o+1][r+1],u=c[o][r+1];o>0&&i.triangle(a,h,u),o<e-1&&i.triangle(h,d,u)}return i.build()}function it(t,s,i,n,e){const c=t.vertex(0,i,0,0,n,0),o=Array.from({length:e},(r,a)=>{const h=a/e*q;return t.vertex(Math.sin(h)*s,i,Math.cos(h)*s,0,n,0)});for(let r=0;r<e;r++){const a=o[r],h=o[(r+1)%e];n===1?t.triangle(c,a,h):t.triangle(c,h,a)}}function Tt(t,s,i=24){const n=new z,e=Math.max(6,Math.round(i)),c=s/2,o=-s/2,r=d=>Array.from({length:e+1},(u,f)=>{const p=f/e*q;return n.vertex(Math.sin(p)*t,d,Math.cos(p)*t,Math.sin(p),0,Math.cos(p))}),a=r(o),h=r(c);for(let d=0;d<e;d++)n.quad(a[d],a[d+1],h[d+1],h[d]);return it(n,t,c,1,e),it(n,t,o,-1,e),n.build()}function Ct(t,s,i=24){const n=new z,e=Math.max(6,Math.round(i)),c=s/2,o=-s/2,r=t/s;for(let a=0;a<e;a++){const h=a/e*q,d=(a+1)/e*q,u=(h+d)/2,f=n.vertex(Math.sin(h)*t,o,Math.cos(h)*t,Math.sin(h),r,Math.cos(h)),p=n.vertex(Math.sin(d)*t,o,Math.cos(d)*t,Math.sin(d),r,Math.cos(d)),M=n.vertex(0,c,0,Math.sin(u),r,Math.cos(u));n.triangle(f,p,M)}return it(n,t,o,-1,e),n.build()}function Et(t,s,i=24){const n=new z,e=Math.max(6,Math.round(i)),c=Math.max(4,Math.round(e/2)),o=[];for(let r=0;r<=e;r++){const a=r/e*q,h=[];for(let d=0;d<=c;d++){const u=d/c*q,f=Math.cos(u)*Math.sin(a),p=Math.sin(u),M=Math.cos(u)*Math.cos(a),g=t+s*Math.cos(u);h.push(n.vertex(g*Math.sin(a),s*Math.sin(u),g*Math.cos(a),f,p,M))}o.push(h)}for(let r=0;r<e;r++)for(let a=0;a<c;a++)n.quad(o[r][a],o[r+1][a],o[r+1][a+1],o[r][a+1]);return n.build()}function Xt(t){switch(t.type){case"box":return It(t.size);case"plane":return St(t.size,t.segments);case"sphere":return Pt(t.radius,t.segments);case"cylinder":return Tt(t.radius,t.height,t.segments);case"cone":return Ct(t.radius,t.height,t.segments);case"torus":return Et(t.radius,t.tube,t.segments);case"extrude":return $t(t.path,{depth:t.depth,width:t.width,curveSegments:t.curveSegments})}}function Yt(t){const s=Array.from({length:t.indices.length/3},()=>[]);for(const i of vt(t))for(const n of i.faces){const e=i.faces.find(c=>c!==n)??-1;s[n].push({a:i.a,b:i.b,across:e})}return{...t,faceEdges:s}}function rn(t,s={}){const i=new Map((s.kinds??[]).map(o=>[o.kind,o])),n=wt(t);for(const o of t.objects){if(an.has(o.kind))continue;const r=i.get(o.kind);r?n.push(...(r.validate?.(o)??[]).map(a=>`"${o.id}": ${a}`)):n.push(`"${o.id}" is a ${o.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:
1
+ (function(T){"use strict";const nn=[{name:"position",vector:"position",components:["x","y","z"]},{name:"rotation",vector:"quaternion",components:["rotateX","rotateY","rotateZ"]}];function xt(t,e=[]){const s=[],n=new Map;for(const i of t.objects){if(i.id?n.has(i.id)&&s.push(`two objects share the id "${i.id}"`):s.push("an object has no id"),i.parent!==void 0&&!n.has(i.parent)&&s.push(`"${i.id}" has parent "${i.parent}", which must be an earlier object`),i.quaternion&&i.quaternion.length!==4&&s.push(`"${i.id}": a quaternion has four numbers`),i.kind==="mesh"){t.materials?.[i.material]||s.push(`"${i.id}" uses material "${i.material}", which is not in materials`);const r=i.geometry;if(r.type==="mesh"){const a=(r.positions?.length??0)/3;(!Array.isArray(r.positions)||r.positions.length%3!==0||a<3)&&s.push(`"${i.id}": a mesh needs positions as x, y, z runs for at least three vertices`),!Array.isArray(r.indices)||r.indices.length%3!==0||r.indices.length<3?s.push(`"${i.id}": a mesh needs indices in triples`):r.indices.every(l=>Number.isInteger(l)&&l>=0&&l<a)||s.push(`"${i.id}": a mesh index is out of range`),r.normals&&r.normals.length!==(r.positions?.length??0)&&s.push(`"${i.id}": a mesh's normals must match its positions`),r.colors&&r.colors.length!==(r.indices?.length??0)/3&&r.colors.length!==a&&s.push(`"${i.id}": a mesh's colors are one per face or one per vertex`)}else{const 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))&&s.push(`"${i.id}": an extrude needs a path that starts with M`),a.every(l=>Number.isFinite(l)&&l>0)||s.push(`"${i.id}": a ${r.type}'s sizes must be positive`)}}if(i.kind==="line"||i.kind==="trail"){i.width!==void 0&&!(i.width>0)&&s.push(`"${i.id}": width must be positive`);for(const r of["taper","fade","opacity"]){const a=i[r];a!==void 0&&!(a>=0&&a<=1)&&s.push(`"${i.id}": ${r} is between 0 and 1`)}}i.kind==="line"&&(Array.isArray(i.points)&&i.points.length>=2&&i.points.every(a=>Array.isArray(a)&&a.length===3&&a.every(Number.isFinite))||s.push(`"${i.id}": a line needs two or more points of three numbers`)),i.kind==="trail"&&(i.length>0||s.push(`"${i.id}": a trail's length (ms) must be positive`),(i.follow===i.id||!t.objects.some(r=>r.id===i.follow))&&s.push(`"${i.id}" follows "${i.follow}", which is not another object in the scene`),i.samples!==void 0&&!(i.samples>=2)&&s.push(`"${i.id}": a trail needs at least 2 samples`)),i.kind==="camera"&&(i.near>0&&i.far>i.near||s.push(`camera "${i.id}": near must be above 0 and far beyond it`),i.projection==="perspective"&&!(i.fov>0&&i.fov<180)&&s.push(`camera "${i.id}": fov is between 0 and 180 degrees`),i.projection==="orthographic"&&!(i.height>0)&&s.push(`camera "${i.id}": height must be positive`)),i.id&&n.set(i.id,i)}const o=n.get(t.camera);o?o.kind!=="camera"&&s.push(`the scene's camera "${t.camera}" is a ${o.kind}, not a camera`):s.push(`the scene's camera "${t.camera}" is not one of its objects`),t.fog&&!(t.fog.far>t.fog.near)&&s.push("fog: far must be beyond near");const c=new Map;for(const i of e){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)||s.push(`a track animates "${t.id}/${i}", which is not in the scene`);for(const a of nn)r.has(a.vector)&&a.components.some(l=>r.has(l))&&s.push(`"${i}": its ${a.name} is animated both by ${a.vector} and by ${a.components.join(" / ")}; use one`)}return s}class O{positions=[];normals=[];indices=[];vertex(e,s,n,o,c,i){const r=Math.hypot(o,c,i)||1;return this.positions.push(e,s,n),this.normals.push(o/r,c/r,i/r),this.positions.length/3-1}triangle(e,s,n){this.indices.push(e,s,n)}quad(e,s,n,o){this.indices.push(e,s,n,e,n,o)}build(){return{positions:this.positions,normals:this.normals,indices:this.indices}}}function kt(t){const e=new Map,s=[];for(let o=0;o<t.positions.length/3;o++){const c=[0,1,2].map(i=>Math.round(t.positions[o*3+i]*1e6)).join(",");e.has(c)||e.set(c,o),s.push(e.get(c))}const n=new Map;for(let o=0;o<t.indices.length/3;o++)for(let c=0;c<3;c++){const i=s[t.indices[o*3+c]],r=s[t.indices[o*3+(c+1)%3]];if(i===r)continue;const a=i<r?`${i}-${r}`:`${r}-${i}`,l=n.get(a);l?l.faces.push(o):n.set(a,{a:Math.min(i,r),b:Math.max(i,r),faces:[o]})}return[...n.values()]}const $t=32,en=256,U=new Map,on=/[MmLlHhVvCcSsQqTtAaZz]/,sn=/^[-+]?(?:\d+\.?\d*|\.\d+)(?:[eE][-+]?\d+)?/,cn={M:2,L:2,H:1,V:1,C:6,S:4,Q:4,T:2,A:7,Z:0};function rn(t){const e=[];let s=0,n=null;const o=()=>{for(;s<t.length&&/[\s,]/.test(t[s]);)s++};for(;s<t.length&&(o(),!(s>=t.length));){const c=t[s];if(on.test(c)){n={type:c,args:[]},e.push(n),s++;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),s++;continue}const a=sn.exec(t.slice(s));if(!a)break;n.args.push(parseFloat(a[0])),s+=a[0].length}return e}function an(t,e,s,n,o,c,i,r,a){if(t===r&&e===a)return[];let l=Math.abs(s),h=Math.abs(n);if(l===0||h===0)return[[t,e,r,a,r,a]];const u=o*Math.PI/180,d=Math.cos(u),p=Math.sin(u),m=(t-r)/2,g=(e-a)/2,y=d*m+p*g,x=-p*m+d*g,f=y*y/(l*l)+x*x/(h*h);if(f>1){const j=Math.sqrt(f);l*=j,h*=j}const v=c===i?-1:1,w=l*l*h*h-l*l*x*x-h*h*y*y,M=l*l*x*x+h*h*y*y,$=v*Math.sqrt(Math.max(0,w/M)),b=$*l*x/h,P=-$*h*y/l,E=d*b-p*P+(t+r)/2,S=p*b+d*P+(e+a)/2,A=(j,N,X,V)=>{const vt=j*X+N*V,ot=Math.sqrt((j*j+N*N)*(X*X+V*V)),B=Math.acos(Math.max(-1,Math.min(1,vt/ot)));return j*V-N*X<0?-B:B},k=A(1,0,(y-b)/l,(x-P)/h);let I=A((y-b)/l,(x-P)/h,(-y-b)/l,(-x-P)/h);!i&&I>0&&(I-=2*Math.PI),i&&I<0&&(I+=2*Math.PI);const C=Math.max(1,Math.ceil(Math.abs(I)/(Math.PI/2))),F=I/C,R=4/3*Math.tan(F/4),Jt=j=>{const N=l*Math.cos(j),X=h*Math.sin(j);return[d*N-p*X+E,p*N+d*X+S]},Kt=j=>{const N=-l*Math.sin(j),X=h*Math.cos(j);return[d*N-p*X,p*N+d*X]},tn=[];for(let j=0;j<C;j++){const N=k+j*F,X=N+F,[V,vt]=Jt(N),[ot,B]=j===C-1?[r,a]:Jt(X),[Wn,Zn]=Kt(N),[Gn,Bn]=Kt(X);tn.push([V+R*Wn,vt+R*Zn,ot-R*Gn,B-R*Bn,ot,B])}return tn}function At(t,e,s,n,o){const c=1-o;return c*c*c*t+3*c*c*o*e+3*c*o*o*s+o*o*o*n}function W(t,e,s,n){return{subpath:0,type:"L",points:[s,n],startX:t,startY:e,endX:s,endY:n,length:Math.hypot(s-t,n-e)}}function J(t,e,s){const[n,o,c,i,r,a]=s,l=[0];let h=t,u=e,d=0;for(let p=1;p<=$t;p++){const m=p/$t,g=At(t,n,c,r,m),y=At(e,o,i,a,m);d+=Math.hypot(g-h,y-u),l.push(d),h=g,u=y}return{subpath:0,type:"C",points:[n,o,c,i,r,a],startX:t,startY:e,endX:r,endY:a,length:d,lengths:l}}function ln(t){const e=U.get(t);if(e)return e;const s=[];let n=0,o=0,c=0,i=0,r=null,a=null,l=-1;const h=new Set,u=g=>{l<0&&(l=0),g.subpath=l,s.push(g)};for(const{type:g,args:y}of rn(t)){const x=g.toUpperCase(),f=g!==x,v=cn[x];if(x==="Z"){(n!==c||o!==i)&&u(W(n,o,c,i)),l>=0&&h.add(l),n=c,o=i,r=a=null;continue}for(let w=0;w+v<=y.length;w+=v){const M=y.slice(w,w+v),$=f?n:0,b=f?o:0;let P=null,E=null;switch(x){case"M":w===0?(n=M[0]+$,o=M[1]+b,c=n,i=o,(l<0||s[s.length-1]?.subpath===l)&&l++):(u(W(n,o,M[0]+$,M[1]+b)),n=M[0]+$,o=M[1]+b);break;case"L":u(W(n,o,M[0]+$,M[1]+b)),n=M[0]+$,o=M[1]+b;break;case"H":u(W(n,o,M[0]+$,o)),n=M[0]+$;break;case"V":u(W(n,o,n,M[0]+b)),o=M[0]+b;break;case"C":{const S=[M[0]+$,M[1]+b,M[2]+$,M[3]+b,M[4]+$,M[5]+b];u(J(n,o,S)),P=[S[2],S[3]],n=S[4],o=S[5];break}case"S":{const[S,A]=r?[2*n-r[0],2*o-r[1]]:[n,o],k=[S,A,M[0]+$,M[1]+b,M[2]+$,M[3]+b];u(J(n,o,k)),P=[k[2],k[3]],n=k[4],o=k[5];break}case"Q":case"T":{let S=n,A=o;x==="Q"?(S=M[0]+$,A=M[1]+b):a&&(S=2*n-a[0],A=2*o-a[1]);const k=x==="Q"?M[2]+$:M[0]+$,I=x==="Q"?M[3]+b:M[1]+b;u(J(n,o,[n+2/3*(S-n),o+2/3*(A-o),k+2/3*(S-k),I+2/3*(A-I),k,I])),E=[S,A],n=k,o=I;break}case"A":{const S=M[5]+$,A=M[6]+b;let k=n,I=o;for(const C of an(n,o,M[0],M[1],M[2],M[3],M[4],S,A))u(J(k,I,C)),k=C[4],I=C[5];n=S,o=A;break}}r=P,a=E}}const d=s.reduce((g,y)=>g+y.length,0),p=[];for(let g=0;g<s.length;){const y=s[g].subpath;let x=g,f=0;for(;x<s.length&&s[x].subpath===y;)f+=s[x++].length;const v=s[g],w=s[x-1],M=h.has(y)||Math.abs(w.endX-v.startX)<1e-9&&Math.abs(w.endY-v.startY)<1e-9;p.push({start:g,end:x,length:f,closed:M}),g=x}const m={segments:s,totalLength:d,subpaths:p};return U.size>=en&&U.delete(U.keys().next().value),U.set(t,m),m}function Q(t){let e=0;for(let s=0;s<t.length;s++){const n=t[s],o=t[(s+1)%t.length];e+=n.x*o.y-o.x*n.y}return e}function st(t,e){let s=!1;for(let n=0,o=e.length-1;n<e.length;o=n++){const c=e[n],i=e[o];c.y>t.y!=i.y>t.y&&t.x<(i.x-c.x)*(t.y-c.y)/(i.y-c.y)+c.x&&(s=!s)}return s}const K=(t,e,s)=>(e.x-t.x)*(s.y-t.y)-(e.y-t.y)*(s.x-t.x),it=(t,e)=>t.x===e.x&&t.y===e.y;function ct(t,e,s,n){return K(e,s,t)>=0&&K(s,n,t)>=0&&K(n,e,t)>=0}function hn(t,e){let s=0;for(let d=1;d<e.length;d++)e[d].x>e[s].x&&(s=d);const n=e[s];let o=-1,c=1/0;for(let d=0;d<t.length;d++){const p=t[d],m=t[(d+1)%t.length];if(p.y>n.y==m.y>n.y)continue;const g=p.x+(n.y-p.y)*(m.x-p.x)/(m.y-p.y);g>=n.x&&g<c&&(c=g,o=d)}if(o<0)return t;const i=t[o],r=t[(o+1)%t.length];let a=i.x>r.x?o:(o+1)%t.length;const l={x:c,y:n.y};let h=1/0;for(let d=0;d<t.length;d++){const p=t[d];if(d===a||p.x<n.x)continue;const m=t[a];if(!(ct(p,n,l,m)||ct(p,n,m,l)))continue;const y=Math.abs(Math.atan2(p.y-n.y,p.x-n.x));y<h&&(h=y,a=d)}const u=e.slice(s).concat(e.slice(0,s));return[...t.slice(0,a+1),...u,n,t[a],...t.slice(a+1)]}function It(t,e=[]){const s=Q(t)>=0?t:[...t].reverse(),n=e.map(u=>Q(u)<=0?u:[...u].reverse()),o=[...s,...n.flat()],c=new Map;o.forEach((u,d)=>c.set(u,d));const i=[...n].sort((u,d)=>Math.max(...d.map(p=>p.x))-Math.max(...u.map(p=>p.x)));let r=s;for(const u of i)r=hn(r,u);const a=r.map(u=>c.get(u)),l=[];let h=a.length*a.length;for(;a.length>3&&h-- >0;){let u=!1;for(let d=0;d<a.length;d++){const p=a[(d+a.length-1)%a.length],m=a[d],g=a[(d+1)%a.length],[y,x,f]=[o[p],o[m],o[g]];if(K(y,x,f)<=0)continue;let v=!1;for(const w of a){const M=o[w];if(!(w===p||w===m||w===g||it(M,y)||it(M,x)||it(M,f))&&ct(M,y,x,f)){v=!0;break}}if(!v){l.push(p,m,g),a.splice(d,1),u=!0;break}}u||(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:o,triangles:l}}function un(t,e){const s=new Map;for(const n of ln(t).segments){s.has(n.subpath)||s.set(n.subpath,[{x:n.startX,y:-n.startY}]);const o=s.get(n.subpath);if(n.type==="L")o.push({x:n.endX,y:-n.endY});else{const[c,i,r,a,l,h]=n.points;for(let u=1;u<=e;u++){const d=u/e,p=1-d,m=p*p*p*n.startX+3*p*p*d*c+3*p*d*d*r+d*d*d*l,g=p*p*p*n.startY+3*p*p*d*i+3*p*d*d*a+d*d*d*h;o.push({x:m,y:-g})}}}return[...s.values()].map(n=>n.filter((o,c)=>c===0||o.x!==n[c-1].x||o.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(Q(n))>1e-12)}function St(t,e){const s=un(t,Math.max(1,Math.round(e.curveSegments??12))),n=new O;if(s.length===0)return n.build();const o=s.flat(),c=Math.min(...o.map(y=>y.x)),i=Math.max(...o.map(y=>y.x)),r=Math.min(...o.map(y=>y.y)),a=Math.max(...o.map(y=>y.y)),l=(e.width??1)/Math.max(i-c,1e-9),h=(c+i)/2,u=(r+a)/2,d=s.map(y=>y.map(x=>({x:(x.x-h)*l,y:(x.y-u)*l}))),p=d.map((y,x)=>d.filter((f,v)=>v!==x&&st(y[0],f)).length),m=e.depth/2,g=-e.depth/2;return d.forEach((y,x)=>{if(p[x]%2===1)return;const f=d.filter((b,P)=>p[P]===p[x]+1&&st(b[0],y)),{points:v,triangles:w}=It(y,f),M=v.map(b=>n.vertex(b.x,b.y,m,0,0,1)),$=v.map(b=>n.vertex(b.x,b.y,g,0,0,-1));for(let b=0;b<w.length;b+=3)n.triangle(M[w[b]],M[w[b+1]],M[w[b+2]]),n.triangle($[w[b]],$[w[b+2]],$[w[b+1]]);for(const b of[Q(y)>=0?y:[...y].reverse(),...f.map(P=>Q(P)<=0?P:[...P].reverse())])for(let P=0;P<b.length;P++){const E=b[P],S=b[(P+1)%b.length],A=S.y-E.y,k=-(S.x-E.x),I=n.vertex(E.x,E.y,g,A,k,0),C=n.vertex(S.x,S.y,g,A,k,0),F=n.vertex(S.x,S.y,m,A,k,0),R=n.vertex(E.x,E.y,m,A,k,0);n.quad(I,C,F,R)}}),n.build()}const q=Math.PI*2;function Pt([t,e,s]){const n=new O,o=t/2,c=e/2,i=s/2,r=[[[0,0,1],[[-o,-c,i],[o,-c,i],[o,c,i],[-o,c,i]]],[[0,0,-1],[[o,-c,-i],[-o,-c,-i],[-o,c,-i],[o,c,-i]]],[[1,0,0],[[o,-c,i],[o,-c,-i],[o,c,-i],[o,c,i]]],[[-1,0,0],[[-o,-c,-i],[-o,-c,i],[-o,c,i],[-o,c,-i]]],[[0,1,0],[[-o,c,i],[o,c,i],[o,c,-i],[-o,c,-i]]],[[0,-1,0],[[-o,-c,-i],[o,-c,-i],[o,-c,i],[-o,-c,i]]]];for(const[[a,l,h],u]of r){const[d,p,m,g]=u.map(([y,x,f])=>n.vertex(y,x,f,a,l,h));n.quad(d,p,m,g)}return n.build()}function Ct([t,e],s=1){const n=new O,o=Math.max(1,Math.round(s)),c=[];for(let i=0;i<=o;i++){const r=e/2-e*i/o;c.push(Array.from({length:o+1},(a,l)=>n.vertex(-t/2+t*l/o,0,r,0,1,0)))}for(let i=0;i<o;i++)for(let r=0;r<o;r++)n.quad(c[i][r],c[i][r+1],c[i+1][r+1],c[i+1][r]);return n.build()}function Tt(t,e=16){const s=new O,n=Math.max(6,Math.round(e)),o=Math.max(3,Math.round(n/2)),c=[];for(let i=0;i<=o;i++){const r=i/o*Math.PI,a=[];for(let l=0;l<=n;l++){const h=l/n*q,u=Math.sin(r)*Math.sin(h),d=Math.cos(r),p=Math.sin(r)*Math.cos(h);a.push(s.vertex(u*t,d*t,p*t,u,d,p))}c.push(a)}for(let i=0;i<o;i++)for(let r=0;r<n;r++){const a=c[i][r],l=c[i+1][r],h=c[i+1][r+1],u=c[i][r+1];i>0&&s.triangle(a,l,u),i<o-1&&s.triangle(l,h,u)}return s.build()}function rt(t,e,s,n,o){const c=t.vertex(0,s,0,0,n,0),i=Array.from({length:o},(r,a)=>{const l=a/o*q;return t.vertex(Math.sin(l)*e,s,Math.cos(l)*e,0,n,0)});for(let r=0;r<o;r++){const a=i[r],l=i[(r+1)%o];n===1?t.triangle(c,a,l):t.triangle(c,l,a)}}function Et(t,e,s=24){const n=new O,o=Math.max(6,Math.round(s)),c=e/2,i=-e/2,r=h=>Array.from({length:o+1},(u,d)=>{const p=d/o*q;return n.vertex(Math.sin(p)*t,h,Math.cos(p)*t,Math.sin(p),0,Math.cos(p))}),a=r(i),l=r(c);for(let h=0;h<o;h++)n.quad(a[h],a[h+1],l[h+1],l[h]);return rt(n,t,c,1,o),rt(n,t,i,-1,o),n.build()}function zt(t,e,s=24){const n=new O,o=Math.max(6,Math.round(s)),c=e/2,i=-e/2,r=t/e;for(let a=0;a<o;a++){const l=a/o*q,h=(a+1)/o*q,u=(l+h)/2,d=n.vertex(Math.sin(l)*t,i,Math.cos(l)*t,Math.sin(l),r,Math.cos(l)),p=n.vertex(Math.sin(h)*t,i,Math.cos(h)*t,Math.sin(h),r,Math.cos(h)),m=n.vertex(0,c,0,Math.sin(u),r,Math.cos(u));n.triangle(d,p,m)}return rt(n,t,i,-1,o),n.build()}function jt(t,e,s=24){const n=new O,o=Math.max(6,Math.round(s)),c=Math.max(4,Math.round(o/2)),i=[];for(let r=0;r<=o;r++){const a=r/o*q,l=[];for(let h=0;h<=c;h++){const u=h/c*q,d=Math.cos(u)*Math.sin(a),p=Math.sin(u),m=Math.cos(u)*Math.cos(a),g=t+e*Math.cos(u);l.push(n.vertex(g*Math.sin(a),e*Math.sin(u),g*Math.cos(a),d,p,m))}i.push(l)}for(let r=0;r<o;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 Nt(t){switch(t.type){case"box":return Pt(t.size);case"plane":return Ct(t.size,t.segments);case"sphere":return Tt(t.radius,t.segments);case"cylinder":return Et(t.radius,t.height,t.segments);case"cone":return zt(t.radius,t.height,t.segments);case"torus":return jt(t.radius,t.tube,t.segments);case"extrude":return St(t.path,{depth:t.depth,width:t.width,curveSegments:t.curveSegments});case"mesh":return Xt(t)}}function Xt(t){const e=[...t.positions],s=[...t.indices],n=e.length/3,o=s.length/3;let c;if(t.normals&&t.normals.length===e.length)c=[...t.normals];else{c=new Array(e.length).fill(0);for(let r=0;r<o;r++){const[a,l,h]=[s[r*3],s[r*3+1],s[r*3+2]],u=C=>[e[C*3],e[C*3+1],e[C*3+2]],[d,p,m]=u(a),[g,y,x]=u(l),[f,v,w]=u(h),M=g-d,$=y-p,b=x-m,P=f-d,E=v-p,S=w-m,A=$*S-b*E,k=b*P-M*S,I=M*E-$*P;for(const C of[a,l,h])c[C*3]+=A,c[C*3+1]+=k,c[C*3+2]+=I}for(let r=0;r<n;r++){const a=Math.hypot(c[r*3],c[r*3+1],c[r*3+2])||1;c[r*3]/=a,c[r*3+1]/=a,c[r*3+2]/=a}}let i;if(t.colors&&t.colors.length===o)i=[...t.colors];else if(t.colors&&t.colors.length===n){i=[];for(let r=0;r<o;r++){const a=[0,1,2].map(h=>dn(t.colors[s[r*3+h]])),l=[0,1,2].map(h=>Math.round((a[0][h]+a[1][h]+a[2][h])/3));i.push(`#${l.map(h=>h.toString(16).padStart(2,"0")).join("")}`)}}return i?{positions:e,normals:c,indices:s,faceColors:i}:{positions:e,normals:c,indices:s}}function dn(t){const e=t.trim().replace("#",""),s=e.length===3?e.split("").map(o=>o+o).join(""):e,n=parseInt(s,16);return Number.isFinite(n)&&s.length===6?[n>>16&255,n>>8&255,n&255]:[0,0,0]}function Yt(t){const e=Array.from({length:t.indices.length/3},()=>[]);for(const s of kt(t))for(const n of s.faces){const o=s.faces.find(c=>c!==n)??-1;e[n].push({a:s.a,b:s.b,across:o})}return{...t,faceEdges:e}}function fn(t,e={}){const s=new Map((e.kinds??[]).map(i=>[i.kind,i])),n=xt(t);for(const i of t.objects){if(pn.has(i.kind))continue;const r=s.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
2
  - ${n.join(`
3
- - `)}`);const e=new Map,c=new Map;for(const o of t.objects){o.kind==="mesh"&&e.set(o.id,Yt(Xt(o.geometry)));const r=i.get(o.kind);r?.prepare&&c.set(o.id,r.prepare(o))}return{scene:t,meshes:e,kinds:i,prepared:c}}const an=new Set(["group","mesh","camera","light","line","trail"]);function ct(t,s){return[t[0]+s[0],t[1]+s[1],t[2]+s[2]]}function L(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 rt(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 hn(t,s){return W(L(t,s))}function _(t){const s=W(t);return s===0?[0,0,0]:D(t,1/s)}function at(t,s,i){return[t[0]+(s[0]-t[0])*i,t[1]+(s[1]-t[1])*i,t[2]+(s[2]-t[2])*i]}const ln=Math.PI/180;function ht(){return[0,0,0,1]}function lt(t,s){const i=Math.hypot(t[0],t[1],t[2]);if(i===0)return ht();const n=s*ln/2,e=Math.sin(n)/i;return[t[0]*e,t[1]*e,t[2]*e,Math.cos(n)]}function jt(t,s,i){return Nt(Nt(lt([0,1,0],s),lt([1,0,0],t)),lt([0,0,1],i))}function Nt(t,s){const[i,n,e,c]=t,[o,r,a,h]=s;return[c*o+i*h+n*a-e*r,c*r-i*a+n*h+e*o,c*a+i*r-n*o+e*h,c*h-i*o-n*r-e*a]}function dn(t){return Math.hypot(t[0],t[1],t[2],t[3])}function Lt(t){const s=dn(t);return s===0?ht():[t[0]/s,t[1]/s,t[2]/s,t[3]/s]}const un=Math.PI/180;function dt(){return[1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1]}function zt(t,s){const i=new Array(16);for(let n=0;n<4;n++)for(let e=0;e<4;e++){let c=0;for(let o=0;o<4;o++)c+=t[o*4+e]*s[n*4+o];i[n*4+e]=c}return i}function fn(t){const[s,i,n,e]=t;return[1-2*(i*i+n*n),2*(s*i+n*e),2*(s*n-i*e),0,2*(s*i-n*e),1-2*(s*s+n*n),2*(i*n+s*e),0,2*(s*n+i*e),2*(i*n-s*e),1-2*(s*s+i*i),0,0,0,0,1]}function pn(t,s,i){const n=fn(s);for(let e=0;e<3;e++)n[e]*=i[0],n[4+e]*=i[1],n[8+e]*=i[2];return n[12]=t[0],n[13]=t[1],n[14]=t[2],n}function gn(t){const s=new Array(16);for(let i=0;i<4;i++)for(let n=0;n<4;n++)s[n*4+i]=t[i*4+n];return s}function Ot(t){const[s,i,n,e,c,o,r,a,h,d,u,f,p,M,g,m]=t,l=s*o-i*c,w=s*r-n*c,A=s*a-e*c,b=i*r-n*o,y=i*a-e*o,x=n*a-e*r,v=h*M-d*p,I=h*g-u*p,E=h*m-f*p,S=d*g-u*M,k=d*m-f*M,P=u*m-f*g,$=l*P-w*k+A*S+b*E-y*I+x*v;if(Math.abs($)<1e-12)return null;const C=1/$;return[(o*P-r*k+a*S)*C,(n*k-i*P-e*S)*C,(M*x-g*y+m*b)*C,(u*y-d*x-f*b)*C,(r*E-c*P-a*I)*C,(s*P-n*E+e*I)*C,(g*A-p*x-m*w)*C,(h*x-u*A+f*w)*C,(c*k-o*E+a*v)*C,(i*E-s*k-e*v)*C,(p*y-M*A+m*l)*C,(d*A-h*y-f*l)*C,(o*I-c*S-r*v)*C,(s*S-i*I+n*v)*C,(M*w-p*b-g*l)*C,(h*b-d*w+u*l)*C]}function Mn(t,s,i,n){const e=1/Math.tan(t*un/2),c=1/(i-n);return[e/s,0,0,0,0,e,0,0,0,0,(n+i)*c,-1,0,0,2*n*i*c,0]}function yn(t,s,i,n,e,c){const o=1/(s-t),r=1/(n-i),a=1/(c-e);return[2*o,0,0,0,0,2*r,0,0,0,0,-2*a,0,-(s+t)*o,-(n+i)*r,-(c+e)*a,1]}function mn(t,s,i=[0,1,0]){const n=_(L(t,s)),e=_(rt(i,n)),c=rt(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 Z(t,s){const i=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?[i,n,e]:[i/c,n/c,e/c]}function _t(t,s){const i=_(L(s,t)),n=Math.abs(i[1])>.9999?[0,0,-1]:[0,1,0];return mn(t,s,n)}function bn(t,s,i,n){const e=s*Math.PI/180,c=i*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 wn(t,s,i){const n=L(t,s),e=W(n);if(e===0)return t;const c=Math.max(.001,e-i);return ct(s,D(n,c/e))}function ut(t,s){if(t.projection==="perspective")return Mn(t.fov,s,t.near,t.far);const i=t.height/2;return yn(-i*s,i*s,-i,i,t.near,t.far)}function vn(t,s,i){const n=Math.max(2,Math.round(i.samples??32)),e=Math.max(0,i.length),c=i.since!==void 0?Math.max(s-e,i.since):s-e;if(c>=s)return[];const o=i.period,r=[];for(let a=0;a<n;a++){const h=c+(s-c)*a/(n-1),d=o&&o>0&&a<n-1?(h%o+o)%o:h;r.push({at:t(d),time:h,age:e>0?(s-h)/e:0})}return r}const xn=2.5;function kn(t){const s=[],i=[],n=t.length,e=(c,o)=>{for(let r=c+o;r>=0&&r<n;r+=o){const a=(t[r].x-t[c].x)*o,h=(t[r].y-t[c].y)*o,d=Math.hypot(a,h);if(d>1e-9)return[a/d,h/d]}return null};for(let c=0;c<n;c++){const o=t[c],r=e(c,-1),a=e(c,1),h=r??a??[1,0],d=a??r??[1,0];let u=h[0]+d[0],f=h[1]+d[1];const p=Math.hypot(u,f);p<1e-9?(u=h[0],f=h[1]):(u/=p,f/=p);const M=u*h[0]+f*h[1],g=Math.min(xn,1/Math.max(M,1e-6)),m=o.width/2*g;s.push({x:o.x-f*m,y:o.y+u*m}),i.push({x:o.x+f*m,y:o.y-u*m})}return{left:s,right:i}}function An(t){const s=t.length;if(s<2)return null;const i=t[s-1];for(let n=s-2;n>=0;n--){const e=i.x-t[n].x,c=i.y-t[n].y,o=Math.hypot(e,c);if(o>1e-9)return{x:i.x,y:i.y,radius:i.width/2,start:Math.atan2(e/o,-c/o)}}return null}const ft=new Map;function H(t){const s=ft.get(t);if(s)return s;let i=[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;i=[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&&(i=[Number(e[1])/255,Number(e[2])/255,Number(e[3])/255])}return ft.size<512&&ft.set(t,i),i}const $n=[{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:_([-3,-5,-4]),angle:30}],In=(t,s,i)=>{const n=Math.min(1,Math.max(0,(i-t)/(s-t)));return n*n*(3-2*n)};function pt(t,s,i){const n=[0,0,0];for(const e of i){const c=H(e.color);let o;if(e.light==="ambient")o=e.intensity;else if(e.light==="directional")o=e.intensity*Math.max(0,-O(s,e.direction));else{const r=L(e.position,t),a=W(r),h=a>0?D(r,1/a):s,d=e.range?Math.max(0,1-a/e.range)**2:1;if(o=e.intensity*Math.max(0,O(s,h))*d,e.light==="spot"){const u=Math.cos(e.angle*Math.PI/180),f=Math.cos(e.angle*.8*Math.PI/180);o*=In(u,f,-O(h,e.direction))}}n[0]+=c[0]*o,n[1]+=c[1]*o,n[2]+=c[2]*o}return n}function Sn(t,s){const i=Math.max(2,Math.round(s));return Math.min(1.2,Math.ceil(Math.min(1,t)*i-1e-9)/i)}function gt(t,s){return t?Math.min(1,Math.max(0,(s-t.near)/(t.far-t.near))):0}function qt(t,s,i,n){const e=t.material,c=H(t.color??e.color),o=e.shading??"lambert";let r;if(o==="unlit")r=[...c];else{const d=s.length>0?s:$n;let u;if(o==="lambert"){const f=[0,0,0];for(const p of t.vertexNormals){const M=pt(t.centroid,p,d);f[0]+=M[0]/3,f[1]+=M[1]/3,f[2]+=M[2]/3}u=f}else if(u=pt(t.centroid,t.normal,d),o==="toon"){const f=Math.max(u[0],u[1],u[2]),p=f>0?Sn(f,e.bands??3)/f:0;u=[u[0]*p,u[1]*p,u[2]*p]}r=[c[0]*u[0],c[1]*u[1],c[2]*u[2]]}if(e.emissive){const d=H(e.emissive);r=[r[0]+d[0],r[1]+d[1],r[2]+d[2]]}const a=gt(n,hn(t.centroid,i.position));if(n&&a>0){const d=H(n.color);r=[r[0]+(d[0]-r[0])*a,r[1]+(d[1]-r[1])*a,r[2]+(d[2]-r[2])*a]}const h=d=>Math.round(Math.min(1,Math.max(0,d))*255);return{color:`rgb(${h(r[0])}, ${h(r[1])}, ${h(r[2])})`,alpha:t.opacity}}function Pn(t,s,i,n){const{camera:e}=n,c=e.near,o=t.map(M=>Z(e.view,M)),r=e.projection[5]*n.height/2,a=(M,g)=>i.width*(1-i.taper*M)*r/(e.orthographic?1:Math.max(g,c)),h=H(i.color),d=[];let u=[];const f=M=>-M[2]>=c;for(let M=0;M<o.length;M++){const g=o[M];f(g)?(u.length===0&&M>0&&!f(o[M-1])&&u.push(Dt(o[M-1],g,s[M-1],s[M],c)),u.push({view:g,age:s[M]})):u.length>0&&(u.push(Dt(o[M-1],g,s[M-1],s[M],c)),d.push(u),u=[])}u.length>0&&d.push(u);const p=[];for(const M of d){if(M.length<2)continue;const g=M.map(b=>-b.view[2]),l=M.map(b=>n.toScreen(b.view)).map((b,y)=>({x:b.x,y:b.y,width:a(M[y].age,g[y])})),w=kn(l),A=M[M.length-1].age===0?An(l):null;for(let b=0;b+1<M.length;b++){const y=(g[b]+g[b+1])/2;if(y>e.far)continue;let x=i.opacity*(1-i.fade*((M[b].age+M[b+1].age)/2));const v=at(M[b].view,M[b+1].view,.5),I=gt(n.fog,W(v));let E=h;if(n.fog&&I>0)if(i.additive)x*=1-I;else{const $=H(n.fog.color);E=[h[0]+($[0]-h[0])*I,h[1]+($[1]-h[1])*I,h[2]+($[2]-h[2])*I]}if(x<=0)continue;const S=`rgb(${E.map($=>Math.round(Math.min(1,Math.max(0,$))*255)).join(", ")})`,k=[w.left[b],w.left[b+1],w.right[b+1],w.right[b]],P=b+2===M.length?A:null;p.push({depth:y,draw($){i.additive&&($.globalCompositeOperation="lighter"),$.globalAlpha=Math.min(1,x),$.fillStyle=S,$.beginPath(),$.moveTo(k[0].x,k[0].y),$.lineTo(k[1].x,k[1].y),P&&$.arc(P.x,P.y,P.radius,P.start,P.start-Math.PI,!0),$.lineTo(k[2].x,k[2].y),$.lineTo(k[3].x,k[3].y),$.closePath(),$.fill(),!i.additive&&x>=1&&($.strokeStyle=S,$.lineWidth=.75,$.stroke())}})}}return p}function Dt(t,s,i,n,e){const c=(-e-t[2])/(s[2]-t[2]);return{view:at(t,s,c),age:i+(n-i)*c}}const Tn=Math.cos(30*Math.PI/180),Ht=new Map,Y=t=>typeof t=="number"?t:void 0,tt=(t,s)=>Array.isArray(t)&&t.length===s?t:void 0;function Qt(t,s){const i=t.position??[0,0,0],n=tt(s.get("position"),3)??[Y(s.get("x"))??i[0],Y(s.get("y"))??i[1],Y(s.get("z"))??i[2]];let e;const c=tt(s.get("quaternion"),4),o=["rotateX","rotateY","rotateZ"].map(d=>Y(s.get(d)));if(c)e=Lt(c);else if(o.some(d=>d!==void 0)){const d=t.rotation??[0,0,0];e=jt(o[0]??d[0],o[1]??d[1],o[2]??d[2])}else t.quaternion?e=Lt(t.quaternion):e=t.rotation?jt(...t.rotation):ht();const r=typeof t.scale=="number"?[t.scale,t.scale,t.scale]:t.scale??[1,1,1],a=Y(s.get("scale")),h=[Y(s.get("scaleX"))??a??r[0],Y(s.get("scaleY"))??a??r[1],Y(s.get("scaleZ"))??a??r[2]];return pn(n,e,h)}const Mt=t=>[t[12],t[13],t[14]];function Cn(t,s,i,n){const e=Mt(s),c=tt(i.get("lookAt"),3)??t.lookAt,o=c?_t(e,c):Ot(s)??dt(),r=t.projection==="perspective"?ut({...t,fov:Y(i.get("fov"))??t.fov},n):ut({...t,height:Y(i.get("height"))??t.height},n);return{id:t.id,view:o,projection:r,position:e,forward:_([-o[2],-o[6],-o[10]]),orthographic:t.projection==="orthographic",near:t.near,far:t.far}}function Rt(t){return gn(Ot(t)??dt())}function En(t,s,i,n){return _([t[0]*s+t[4]*i+t[8]*n,t[1]*s+t[5]*i+t[9]*n,t[2]*s+t[6]*i+t[10]*n])}function Xn(t,s){const i=e=>-e[2]>=s,n=[];for(let e=0;e<t.length;e++){const c=t[e],o=t[(e+1)%t.length];if(i(c)&&n.push(c),i(c)!==i(o)){const r=(-s-c[2])/(o[2]-c[2]);n.push(at(c,o,r))}}return n}function yt(t,s=new Map,i){const{scene:n}=t,e=l=>s.get(`${n.id}/${l}`)??Ht,c=new Map,o=new Map;for(const l of n.objects){const w=e(l.id),A=Qt(l,w),b=l.parent?c.get(l.parent):void 0;c.set(l.id,b?zt(b,A):A);const y=Y(w.get("visible"))!==void 0?Y(w.get("visible"))>.5:l.visible!==!1;o.set(l.id,y&&(l.parent?o.get(l.parent)!==!1:!0))}const r=s.get(n.id)?.get("activeCamera"),a=typeof r=="string"&&n.objects.some(l=>l.id===r&&l.kind==="camera")?r:n.camera,h=n.objects.find(l=>l.id===a),d=Cn(h,c.get(a),e(a),i.width/i.height),u=l=>{const w=Z(d.projection,l);return{x:(w[0]+1)/2*i.width,y:(1-w[1])/2*i.height}},f=[];for(const l of n.objects){if(l.kind!=="light"||!o.get(l.id))continue;const w=e(l.id),A=Mt(c.get(l.id)),b=tt(w.get("target"),3)??l.target??[0,0,0],y=w.get("color");f.push({id:l.id,light:l.light,color:typeof y=="string"?y:l.color,intensity:Y(w.get("intensity"))??l.intensity,position:A,direction:_(L(b,A)),range:l.range,angle:l.angle??30})}const p=[],M=[];n.objects.forEach((l,w)=>{if(l.kind!=="mesh"||!o.get(l.id))return;const A=t.meshes.get(l.id),b=n.materials?.[l.material];if(!A||!b)return;const y=e(l.id),x=c.get(l.id),v=Rt(x),I=y.get("color"),E=(Y(y.get("opacity"))??1)*(b.opacity??1);E<=0||(M.push({objectId:l.id,objectIndex:w,mesh:A,world:x,material:b,color:typeof I=="string"?I:void 0,opacity:E}),p.push(...Ft(A,x,v,d,u,b,{objectId:l.id,objectIndex:w,layer:l.layer??0,color:typeof I=="string"?I:void 0,opacity:E})))});const g=[];n.objects.forEach((l,w)=>{const A=t.kinds.get(l.kind);if(!A||!o.get(l.id))return;const b=A.resolve({object:l,prepared:t.prepared.get(l.id),values:e(l.id),world:c.get(l.id),camera:d,lights:f,fog:n.fog,width:i.width,height:i.height,toScreen:u});if(!b)return;const y=l.layer??0;for(const x of b.drawables??[])g.push({...x,objectId:l.id,objectIndex:w,layer:y});for(const x of b.meshes??[])M.push({objectId:l.id,objectIndex:w,mesh:x.mesh,world:x.world,material:x.material,opacity:x.material.opacity??1}),p.push(...Ft(x.mesh,x.world,Rt(x.world),d,u,x.material,{objectId:l.id,objectIndex:w,layer:y,opacity:x.material.opacity??1}))});const m={camera:d,height:i.height,fog:n.fog,toScreen:u};return n.objects.forEach((l,w)=>{if(l.kind!=="line"&&l.kind!=="trail"||!o.get(l.id))return;const A=e(l.id),b=l.kind==="line"?jn(l,c.get(l.id)):Nn(l,n,s,i);if(!b)return;const y=Yn(l,A);if(y.opacity<=0||y.width<=0)return;const x=l.layer??0;for(const v of Pn(b.points,b.ages,y,m))g.push({...v,objectId:l.id,objectIndex:w,layer:x})}),g.sort((l,w)=>l.layer-w.layer||w.depth-l.depth||l.objectIndex-w.objectIndex),p.sort((l,w)=>l.layer-w.layer||w.depth-l.depth||l.objectIndex-w.objectIndex||l.face-w.face||l.part-w.part),{id:n.id,width:i.width,height:i.height,background:n.background,fog:n.fog,camera:d,lights:f,worlds:c,triangles:p,drawables:g,meshes:M,time:i.time??0}}function Yn(t,s){const i=s.get("color"),n=t.kind==="trail";return{color:typeof i=="string"?i:t.color,width:Y(s.get("width"))??t.width??.05,taper:t.taper??(n?1:0),fade:t.fade??(n?1:0),opacity:(Y(s.get("opacity"))??1)*(t.opacity??1),additive:t.blend==="add"}}function jn(t,s){const n=(t.closed?[...t.points,t.points[0]]:t.points).map(e=>Z(s,e));return{points:n,ages:n.map((e,c)=>1-c/(n.length-1))}}function Nn(t,s,i,n){if(!n.valuesAt)return null;const e=n.time??0,c=Y(i.get(`${s.id}/${t.id}`)?.get("length"))??t.length,o=vn(r=>Mt(Ut(s,t.follow,r===e?i:n.valuesAt(r))),e,{length:c,samples:t.samples??32,period:t.period});return o.length>=2?{points:o.map(r=>r.at),ages:o.map(r=>r.age)}:null}function Ut(t,s,i){const n=t.objects.find(c=>c.id===s);if(!n)return dt();const e=Qt(n,i.get(`${t.id}/${s}`)??Ht);return n.parent?zt(Ut(t,n.parent,i),e):e}function Ft(t,s,i,n,e,c,o){const r=t.positions.length/3,a=[],h=[];for(let g=0;g<r;g++){const m=Z(s,[t.positions[g*3],t.positions[g*3+1],t.positions[g*3+2]]);a.push(m),h.push(Z(n.view,m))}const d=t.indices.length/3,u=[],f=[];for(let g=0;g<d;g++){const[m,l,w]=[0,1,2].map(y=>a[t.indices[g*3+y]]),A=_(rt(L(l,m),L(w,m)));u.push(A);const b=n.orthographic?D(n.forward,-1):L(n.position,m);f.push(O(A,b)>0)}const p=g=>-h[g][2]>=n.near,M=[];for(let g=0;g<d;g++){const m=f[g];if(!m&&!c.doubleSided)continue;const l=[t.indices[g*3],t.indices[g*3+1],t.indices[g*3+2]],w=l.map(k=>h[k]);if(w.every(k=>-k[2]>n.far))continue;const A=Xn(w,n.near);if(A.length<3)continue;const b=m?1:-1,y=D(u[g],b),x=l.map(k=>D(En(i,t.normals[k*3],t.normals[k*3+1],t.normals[k*3+2]),b)),v=D(ct(ct(a[l[0]],a[l[1]]),a[l[2]]),1/3),I=-(w[0][2]+w[1][2]+w[2][2])/3,E=[];if(c.outline&&m)for(const k of t.faceEdges[g]){const P=k.across,$=P===-1||!f[P],C=c.creases!==!1&&!$&&g<P&&O(u[g],u[P])<Tn;($||C)&&p(k.a)&&p(k.b)&&E.push([e(h[k.a]),e(h[k.b])])}const S=A.map(e);for(let k=1;k+1<S.length;k++)M.push({objectId:o.objectId,objectIndex:o.objectIndex,layer:o.layer,face:g,part:k-1,screen:[S[0],S[k],S[k+1]],depth:I,centroid:v,normal:y,vertexNormals:x,material:c,color:o.color,opacity:o.opacity,outline:k===1?E:[]})}return M}class Ln{ctx;constructor(s){this.ctx=s}render(s){mt(this.ctx,s)}}function mt(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 i=0;const n=(o,r)=>{for(;i<s.drawables.length;){const a=s.drawables[i];if(a.layer>o||a.layer===o&&a.depth<=r)break;t.save(),a.draw(t,s),t.restore(),i++}};let e=[];const c=o=>{if(e.length===0)return;const r=[];for(const a of e)o&&o.objectId===a.objectId&&o.layer===a.layer&&a.depth-o.depth<=zn*a.depth?r.push(a):On(t,a.triangle);e=r};for(const o of s.triangles){c(o),n(o.layer,o.depth);const{color:r,alpha:a}=qt(o,s.lights,s.camera,s.fog),[h,d,u]=o.screen;t.globalAlpha=a,t.beginPath(),t.moveTo(h.x,h.y),t.lineTo(d.x,d.y),t.lineTo(u.x,u.y),t.closePath(),t.fillStyle=r,t.fill(),a>=1&&(t.strokeStyle=r,t.lineWidth=.75,t.stroke()),o.material.outline&&o.outline.length>0&&e.push({objectId:o.objectId,layer:o.layer,depth:o.depth,triangle:o})}c(),n(1/0,-1/0),t.restore()}const zn=.04;function On(t,s){const i=s.material.outline;t.globalAlpha=s.opacity,t.strokeStyle=i.color,t.lineWidth=i.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 _n{renderer;width;height;scenes=new Map;values=new Map;valuesAt;constructor(s,i){this.renderer=s,this.width=i.width,this.height=i.height,this.valuesAt=i.valuesAt}registerScene(s){this.scenes.set(s.scene.id,s)}unregisterScene(s){this.scenes.delete(s)}resize(s,i){this.width=s,this.height=i}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(yt(s,this.values,{width:this.width,height:this.height,time:this.time,valuesAt:this.valuesAt}))}}function qn(t,s,i,n){const e=n.stateAt,c=n.valuesAt??(e&&(o=>e(o).values));mt(t,yt(s,i??new Map,{width:n.width,height:n.height,time:n.time,valuesAt:c}))}T.Canvas2DRenderer=Ln,T.MeshBuilder=z,T.Scene3DAdapter=_n,T.boxMesh=It,T.coneMesh=Ct,T.cylinderMesh=Tt,T.dollyPosition=wn,T.drawResolvedScene=mt,T.drawScene3D=qn,T.extrudeMesh=$t,T.fogAmount=gt,T.geometryMesh=Xt,T.lightAt=pt,T.loadScene3D=rn,T.lookAtView=_t,T.meshEdges=vt,T.orbitPosition=bn,T.parseColor=H,T.planeMesh=St,T.pointInPolygon=et,T.prepareMesh=Yt,T.projectionMatrix=ut,T.resolveScene3D=yt,T.shadeTriangle=qt,T.signedArea=Q,T.sphereMesh=Pt,T.torusMesh=Et,T.triangulate=At,T.validateScene3D=wt,Object.defineProperty(T,Symbol.toStringTag,{value:"Module"})})(this.tinyfly=this.tinyfly||{});
3
+ - `)}`);const o=new Map,c=new Map;for(const i of t.objects){i.kind==="mesh"&&o.set(i.id,Yt(Nt(i.geometry)));const r=s.get(i.kind);r?.prepare&&c.set(i.id,r.prepare(i))}return{scene:t,meshes:o,kinds:s,prepared:c}}const pn=new Set(["group","mesh","camera","light","line","trail"]);function at(t,e){return[t[0]+e[0],t[1]+e[1],t[2]+e[2]]}function Y(t,e){return[t[0]-e[0],t[1]-e[1],t[2]-e[2]]}function H(t,e){return[t[0]*e,t[1]*e,t[2]*e]}function D(t,e){return t[0]*e[0]+t[1]*e[1]+t[2]*e[2]}function lt(t,e){return[t[1]*e[2]-t[2]*e[1],t[2]*e[0]-t[0]*e[2],t[0]*e[1]-t[1]*e[0]]}function Z(t){return Math.hypot(t[0],t[1],t[2])}function Lt(t,e){return Z(Y(t,e))}function _(t){const e=Z(t);return e===0?[0,0,0]:H(t,1/e)}function ht(t,e,s){return[t[0]+(e[0]-t[0])*s,t[1]+(e[1]-t[1])*s,t[2]+(e[2]-t[2])*s]}const gn=Math.PI/180;function ut(){return[0,0,0,1]}function tt(t,e){const s=Math.hypot(t[0],t[1],t[2]);if(s===0)return ut();const n=e*gn/2,o=Math.sin(n)/s;return[t[0]*o,t[1]*o,t[2]*o,Math.cos(n)]}function Ot(t,e,s){return Dt(Dt(tt([0,1,0],e),tt([1,0,0],t)),tt([0,0,1],s))}function Dt(t,e){const[s,n,o,c]=t,[i,r,a,l]=e;return[c*i+s*l+n*a-o*r,c*r-s*a+n*l+o*i,c*a+s*r-n*i+o*l,c*l-s*i-n*r-o*a]}function Mn(t){return Math.hypot(t[0],t[1],t[2],t[3])}function _t(t){const e=Mn(t);return e===0?ut():[t[0]/e,t[1]/e,t[2]/e,t[3]/e]}const mn=Math.PI/180;function dt(){return[1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1]}function ft(t,e){const s=new Array(16);for(let n=0;n<4;n++)for(let o=0;o<4;o++){let c=0;for(let i=0;i<4;i++)c+=t[i*4+o]*e[n*4+i];s[n*4+o]=c}return s}function qt(t){const[e,s,n,o]=t;return[1-2*(s*s+n*n),2*(e*s+n*o),2*(e*n-s*o),0,2*(e*s-n*o),1-2*(e*e+n*n),2*(s*n+e*o),0,2*(e*n+s*o),2*(s*n-e*o),1-2*(e*e+s*s),0,0,0,0,1]}function yn(t,e,s){const n=qt(e);for(let o=0;o<3;o++)n[o]*=s[0],n[4+o]*=s[1],n[8+o]*=s[2];return n[12]=t[0],n[13]=t[1],n[14]=t[2],n}function bn(t){const e=new Array(16);for(let s=0;s<4;s++)for(let n=0;n<4;n++)e[n*4+s]=t[s*4+n];return e}function Ht(t){const[e,s,n,o,c,i,r,a,l,h,u,d,p,m,g,y]=t,x=e*i-s*c,f=e*r-n*c,v=e*a-o*c,w=s*r-n*i,M=s*a-o*i,$=n*a-o*r,b=l*m-h*p,P=l*g-u*p,E=l*y-d*p,S=h*g-u*m,A=h*y-d*m,k=u*y-d*g,I=x*k-f*A+v*S+w*E-M*P+$*b;if(Math.abs(I)<1e-12)return null;const C=1/I;return[(i*k-r*A+a*S)*C,(n*A-s*k-o*S)*C,(m*$-g*M+y*w)*C,(u*M-h*$-d*w)*C,(r*E-c*k-a*P)*C,(e*k-n*E+o*P)*C,(g*v-p*$-y*f)*C,(l*$-u*v+d*f)*C,(c*A-i*E+a*b)*C,(s*E-e*A-o*b)*C,(p*M-m*v+y*x)*C,(h*v-l*M-d*x)*C,(i*P-c*S-r*b)*C,(e*S-s*P+n*b)*C,(m*f-p*w-g*x)*C,(l*w-h*f+u*x)*C]}function wn(t,e,s,n){const o=1/Math.tan(t*mn/2),c=1/(s-n);return[o/e,0,0,0,0,o,0,0,0,0,(n+s)*c,-1,0,0,2*n*s*c,0]}function vn(t,e,s,n,o,c){const i=1/(e-t),r=1/(n-s),a=1/(c-o);return[2*i,0,0,0,0,2*r,0,0,0,0,-2*a,0,-(e+t)*i,-(n+s)*r,-(c+o)*a,1]}function xn(t,e,s=[0,1,0]){const n=_(Y(t,e)),o=_(lt(s,n)),c=lt(n,o);return[o[0],c[0],n[0],0,o[1],c[1],n[1],0,o[2],c[2],n[2],0,-D(o,t),-D(c,t),-D(n,t),1]}function G(t,e){const s=t[0]*e[0]+t[4]*e[1]+t[8]*e[2]+t[12],n=t[1]*e[0]+t[5]*e[1]+t[9]*e[2]+t[13],o=t[2]*e[0]+t[6]*e[1]+t[10]*e[2]+t[14],c=t[3]*e[0]+t[7]*e[1]+t[11]*e[2]+t[15];return c===1||c===0?[s,n,o]:[s/c,n/c,o/c]}function Ft(t,e,s={}){const n=_(Y(e,t)),o=s.up??(Math.abs(n[1])>.9999?[0,0,-1]:[0,1,0]);return pt(xn(t,e,o),s.roll??0)}function pt(t,e){return e?ft(qt(tt([0,0,1],-e)),t):t}function kn(t,e,s,n){const o=e*Math.PI/180,c=s*Math.PI/180;return[t[0]+n*Math.cos(c)*Math.sin(o),t[1]+n*Math.sin(c),t[2]+n*Math.cos(c)*Math.cos(o)]}function $n(t,e,s){const n=Y(t,e),o=Z(n);if(o===0)return t;const c=Math.max(.001,o-s);return at(e,H(n,c/o))}function gt(t,e){if(t.projection==="perspective")return wn(t.fov,e,t.near,t.far);const s=t.height/2;return vn(-s*e,s*e,-s,s,t.near,t.far)}function An(t,e,s){const n=Math.max(2,Math.round(s.samples??32)),o=Math.max(0,s.length),c=s.since!==void 0?Math.max(e-o,s.since):e-o;if(c>=e)return[];const i=s.period,r=[];for(let a=0;a<n;a++){const l=c+(e-c)*a/(n-1),h=i&&i>0&&a<n-1?(l%i+i)%i:l;r.push({at:t(h),time:l,age:o>0?(e-l)/o:0})}return r}const In=2.5;function Sn(t){const e=[],s=[],n=t.length,o=(c,i)=>{for(let r=c+i;r>=0&&r<n;r+=i){const a=(t[r].x-t[c].x)*i,l=(t[r].y-t[c].y)*i,h=Math.hypot(a,l);if(h>1e-9)return[a/h,l/h]}return null};for(let c=0;c<n;c++){const i=t[c],r=o(c,-1),a=o(c,1),l=r??a??[1,0],h=a??r??[1,0];let u=l[0]+h[0],d=l[1]+h[1];const p=Math.hypot(u,d);p<1e-9?(u=l[0],d=l[1]):(u/=p,d/=p);const m=u*l[0]+d*l[1],g=Math.min(In,1/Math.max(m,1e-6)),y=i.width/2*g;e.push({x:i.x-d*y,y:i.y+u*y}),s.push({x:i.x+d*y,y:i.y-u*y})}return{left:e,right:s}}function Pn(t){const e=t.length;if(e<2)return null;const s=t[e-1];for(let n=e-2;n>=0;n--){const o=s.x-t[n].x,c=s.y-t[n].y,i=Math.hypot(o,c);if(i>1e-9)return{x:s.x,y:s.y,radius:s.width/2,start:Math.atan2(o/i,-c/i)}}return null}const Mt=new Map;function L(t){const e=Mt.get(t);if(e)return e;let s=[1,1,1];const n=t.trim().replace("#","");if(t.trim().startsWith("#")&&(n.length===3||n.length===6)){const o=n.length===3?n.split("").map(c=>c+c).join(""):n;s=[0,2,4].map(c=>Number.parseInt(o.slice(c,c+2),16)/255)}else{const o=t.match(/rgba?\(\s*([\d.]+)\s*,\s*([\d.]+)\s*,\s*([\d.]+)/);o&&(s=[Number(o[1])/255,Number(o[2])/255,Number(o[3])/255])}return Mt.size<512&&Mt.set(t,s),s}const Cn=[{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:_([-3,-5,-4]),angle:30}],Tn=(t,e,s)=>{const n=Math.min(1,Math.max(0,(s-t)/(e-t)));return n*n*(3-2*n)};function mt(t,e,s){const n=[0,0,0];for(const o of s){const c=L(o.color);let i;if(o.light==="ambient")i=o.intensity;else if(o.light==="directional")i=o.intensity*Math.max(0,-D(e,o.direction));else{const r=Y(o.position,t),a=Z(r),l=a>0?H(r,1/a):e,h=o.range?Math.max(0,1-a/o.range)**2:1;if(i=o.intensity*Math.max(0,D(e,l))*h,o.light==="spot"){const u=Math.cos(o.angle*Math.PI/180),d=Math.cos(o.angle*.8*Math.PI/180);i*=Tn(u,d,-D(l,o.direction))}}n[0]+=c[0]*i,n[1]+=c[1]*i,n[2]+=c[2]*i}return n}function En(t,e){const s=Math.max(2,Math.round(e));return Math.min(1.2,Math.ceil(Math.min(1,t)*s-1e-9)/s)}function nt(t,e){return t?Math.min(1,Math.max(0,(e-t.near)/(t.far-t.near))):0}function Qt(t,e,s){const n=nt(e,s);if(!e||n<=0)return t;const o=L(t),c=L(e.color),i=r=>Math.round(Math.min(1,Math.max(0,r))*255);return`rgb(${i(o[0]+(c[0]-o[0])*n)}, ${i(o[1]+(c[1]-o[1])*n)}, ${i(o[2]+(c[2]-o[2])*n)})`}function Rt(t,e,s,n){const o=t.material,c=L(t.color??o.color),i=o.shading??"lambert";let r;if(i==="unlit")r=[...c];else{const h=e.length>0?e:Cn;let u;if(i==="lambert"){const d=[0,0,0];for(const p of t.vertexNormals){const m=mt(t.centroid,p,h);d[0]+=m[0]/3,d[1]+=m[1]/3,d[2]+=m[2]/3}u=d}else if(u=mt(t.centroid,t.normal,h),i==="toon"){const d=Math.max(u[0],u[1],u[2]),p=d>0?En(d,o.bands??3)/d:0;u=[u[0]*p,u[1]*p,u[2]*p]}r=[c[0]*u[0],c[1]*u[1],c[2]*u[2]]}if(o.emissive){const h=L(o.emissive);r=[r[0]+h[0],r[1]+h[1],r[2]+h[2]]}const a=nt(n,Lt(t.centroid,s.position));if(n&&a>0){const h=L(n.color);r=[r[0]+(h[0]-r[0])*a,r[1]+(h[1]-r[1])*a,r[2]+(h[2]-r[2])*a]}const l=h=>Math.round(Math.min(1,Math.max(0,h))*255);return{color:`rgb(${l(r[0])}, ${l(r[1])}, ${l(r[2])})`,alpha:t.opacity}}function zn(t,e,s,n){const{camera:o}=n,c=o.near,i=t.map(m=>G(o.view,m)),r=o.projection[5]*n.height/2,a=(m,g)=>s.width*(1-s.taper*m)*r/(o.orthographic?1:Math.max(g,c)),l=L(s.color),h=[];let u=[];const d=m=>-m[2]>=c;for(let m=0;m<i.length;m++){const g=i[m];d(g)?(u.length===0&&m>0&&!d(i[m-1])&&u.push(Vt(i[m-1],g,e[m-1],e[m],c)),u.push({view:g,age:e[m]})):u.length>0&&(u.push(Vt(i[m-1],g,e[m-1],e[m],c)),h.push(u),u=[])}u.length>0&&h.push(u);const p=[];for(const m of h){if(m.length<2)continue;const g=m.map(w=>-w.view[2]),x=m.map(w=>n.toScreen(w.view)).map((w,M)=>({x:w.x,y:w.y,width:a(m[M].age,g[M])})),f=Sn(x),v=m[m.length-1].age===0?Pn(x):null;for(let w=0;w+1<m.length;w++){const M=(g[w]+g[w+1])/2;if(M>o.far)continue;let $=s.opacity*(1-s.fade*((m[w].age+m[w+1].age)/2));const b=ht(m[w].view,m[w+1].view,.5),P=nt(n.fog,Z(b));let E=l;if(n.fog&&P>0)if(s.additive)$*=1-P;else{const I=L(n.fog.color);E=[l[0]+(I[0]-l[0])*P,l[1]+(I[1]-l[1])*P,l[2]+(I[2]-l[2])*P]}if($<=0)continue;const S=`rgb(${E.map(I=>Math.round(Math.min(1,Math.max(0,I))*255)).join(", ")})`,A=[f.left[w],f.left[w+1],f.right[w+1],f.right[w]],k=w+2===m.length?v:null;p.push({depth:M,draw(I){s.additive&&(I.globalCompositeOperation="lighter"),I.globalAlpha=Math.min(1,$),I.fillStyle=S,I.beginPath(),I.moveTo(A[0].x,A[0].y),I.lineTo(A[1].x,A[1].y),k&&I.arc(k.x,k.y,k.radius,k.start,k.start-Math.PI,!0),I.lineTo(A[2].x,A[2].y),I.lineTo(A[3].x,A[3].y),I.closePath(),I.fill(),!s.additive&&$>=1&&(I.strokeStyle=S,I.lineWidth=.75,I.stroke())}})}}return p}function Vt(t,e,s,n,o){const c=(-o-t[2])/(e[2]-t[2]);return{view:ht(t,e,c),age:s+(n-s)*c}}const jn=Math.cos(30*Math.PI/180),Ut=new Map,z=t=>typeof t=="number"?t:void 0,et=(t,e)=>Array.isArray(t)&&t.length===e?t:void 0;function Wt(t,e){const s=t.position??[0,0,0],n=et(e.get("position"),3)??[z(e.get("x"))??s[0],z(e.get("y"))??s[1],z(e.get("z"))??s[2]];let o;const c=et(e.get("quaternion"),4),i=["rotateX","rotateY","rotateZ"].map(h=>z(e.get(h)));if(c)o=_t(c);else if(i.some(h=>h!==void 0)){const h=t.rotation??[0,0,0];o=Ot(i[0]??h[0],i[1]??h[1],i[2]??h[2])}else t.quaternion?o=_t(t.quaternion):o=t.rotation?Ot(...t.rotation):ut();const r=typeof t.scale=="number"?[t.scale,t.scale,t.scale]:t.scale??[1,1,1],a=z(e.get("scale")),l=[z(e.get("scaleX"))??a??r[0],z(e.get("scaleY"))??a??r[1],z(e.get("scaleZ"))??a??r[2]];return yn(n,o,l)}const yt=t=>[t[12],t[13],t[14]];function Nn(t,e,s,n){const o=yt(e),c=et(s.get("lookAt"),3)??t.lookAt,i=z(s.get("roll"))??t.roll??0,r=c?Ft(o,c,{up:t.up,roll:i}):pt(Ht(e)??dt(),i),a=t.projection==="perspective"?gt({...t,fov:z(s.get("fov"))??t.fov},n):gt({...t,height:z(s.get("height"))??t.height},n);return{id:t.id,view:r,projection:a,position:o,forward:_([-r[2],-r[6],-r[10]]),orthographic:t.projection==="orthographic",near:t.near,far:t.far}}function Zt(t){return bn(Ht(t)??dt())}function Xn(t,e,s,n){return _([t[0]*e+t[4]*s+t[8]*n,t[1]*e+t[5]*s+t[9]*n,t[2]*e+t[6]*s+t[10]*n])}function Yn(t,e){const s=o=>-o[2]>=e,n=[];for(let o=0;o<t.length;o++){const c=t[o],i=t[(o+1)%t.length];if(s(c)&&n.push(c),s(c)!==s(i)){const r=(-e-c[2])/(i[2]-c[2]);n.push(ht(c,i,r))}}return n}function Ln(t,e){const s=z(e.get("outlineWidth"))??t.outline?.width,n=e.get("outlineColor"),o=(typeof n=="string"?n:void 0)??t.outline?.color??"#000000";return s!==void 0&&s>0?{width:s,color:o}:void 0}function On(t,e){return!t||t.to<=t.from?1:Math.min(1,Math.max(0,(e-t.from)/(t.to-t.from)))}function bt(t,e=new Map,s){const{scene:n}=t,o=f=>e.get(`${n.id}/${f}`)??Ut,c=s.nearFade??n.nearFade,i=new Map,r=new Map;for(const f of n.objects){const v=o(f.id),w=Wt(f,v),M=f.parent?i.get(f.parent):void 0;i.set(f.id,M?ft(M,w):w);const $=z(v.get("visible"))!==void 0?z(v.get("visible"))>.5:f.visible!==!1;r.set(f.id,$&&(f.parent?r.get(f.parent)!==!1:!0))}const a=e.get(n.id)?.get("activeCamera"),l=typeof a=="string"&&n.objects.some(f=>f.id===a&&f.kind==="camera")?a:n.camera,h=n.objects.find(f=>f.id===l),u=Nn(h,i.get(l),o(l),s.width/s.height),d=f=>{const v=G(u.projection,f);return{x:(v[0]+1)/2*s.width,y:(1-v[1])/2*s.height}},p=[];for(const f of n.objects){if(f.kind!=="light"||!r.get(f.id))continue;const v=o(f.id),w=yt(i.get(f.id)),M=et(v.get("target"),3)??f.target??[0,0,0],$=v.get("color");p.push({id:f.id,light:f.light,color:typeof $=="string"?$:f.color,intensity:z(v.get("intensity"))??f.intensity,position:w,direction:_(Y(M,w)),range:f.range,angle:f.angle??30})}const m=[],g=[];n.objects.forEach((f,v)=>{if(f.kind!=="mesh"||!r.get(f.id))return;const w=t.meshes.get(f.id),M=n.materials?.[f.material];if(!w||!M)return;const $=o(f.id),b=i.get(f.id),P=Zt(b),E=$.get("color"),S=(z($.get("opacity"))??1)*(M.opacity??1);if(S<=0)return;const A=Ln(M,$),k=A===M.outline||A&&M.outline&&A.width===M.outline.width&&A.color===M.outline.color?M:{...M,outline:A};g.push({objectId:f.id,objectIndex:v,mesh:w,world:b,material:k,color:typeof E=="string"?E:void 0,opacity:S}),m.push(...Bt(w,b,P,u,d,k,{objectId:f.id,objectIndex:v,layer:f.layer??0,color:typeof E=="string"?E:void 0,opacity:S,ink:A,nearFade:c}))});const y=[];n.objects.forEach((f,v)=>{const w=t.kinds.get(f.kind);if(!w||!r.get(f.id))return;const M=w.resolve({object:f,prepared:t.prepared.get(f.id),values:o(f.id),world:i.get(f.id),camera:u,lights:p,fog:n.fog,width:s.width,height:s.height,toScreen:d});if(!M)return;const $=f.layer??0;for(const b of M.drawables??[])y.push({...b,objectId:f.id,objectIndex:v,layer:$});for(const b of M.meshes??[])g.push({objectId:f.id,objectIndex:v,mesh:b.mesh,world:b.world,material:b.material,opacity:b.material.opacity??1}),m.push(...Bt(b.mesh,b.world,Zt(b.world),u,d,b.material,{objectId:f.id,objectIndex:v,layer:$,opacity:b.material.opacity??1,ink:b.material.outline,nearFade:c}))});const x={camera:u,height:s.height,fog:n.fog,toScreen:d};return n.objects.forEach((f,v)=>{if(f.kind!=="line"&&f.kind!=="trail"||!r.get(f.id))return;const w=o(f.id),M=f.kind==="line"?_n(f,i.get(f.id)):qn(f,n,e,s);if(!M)return;const $=Dn(f,w);if($.opacity<=0||$.width<=0)return;const b=f.layer??0;for(const P of zn(M.points,M.ages,$,x))y.push({...P,objectId:f.id,objectIndex:v,layer:b})}),y.sort((f,v)=>f.layer-v.layer||v.depth-f.depth||f.objectIndex-v.objectIndex),m.sort((f,v)=>f.layer-v.layer||v.depth-f.depth||f.objectIndex-v.objectIndex||f.face-v.face||f.part-v.part),{id:n.id,width:s.width,height:s.height,background:n.background,fog:n.fog,camera:u,lights:p,worlds:i,triangles:m,drawables:y,meshes:g,time:s.time??0}}function Dn(t,e){const s=e.get("color"),n=t.kind==="trail";return{color:typeof s=="string"?s:t.color,width:z(e.get("width"))??t.width??.05,taper:t.taper??(n?1:0),fade:t.fade??(n?1:0),opacity:(z(e.get("opacity"))??1)*(t.opacity??1),additive:t.blend==="add"}}function _n(t,e){const n=(t.closed?[...t.points,t.points[0]]:t.points).map(o=>G(e,o));return{points:n,ages:n.map((o,c)=>1-c/(n.length-1))}}function qn(t,e,s,n){if(!n.valuesAt)return null;const o=n.time??0,c=z(s.get(`${e.id}/${t.id}`)?.get("length"))??t.length,i=An(r=>yt(Gt(e,t.follow,r===o?s:n.valuesAt(r))),o,{length:c,samples:t.samples??32,period:t.period??n.period});return i.length>=2?{points:i.map(r=>r.at),ages:i.map(r=>r.age)}:null}function Gt(t,e,s){const n=t.objects.find(c=>c.id===e);if(!n)return dt();const o=Wt(n,s.get(`${t.id}/${e}`)??Ut);return n.parent?ft(Gt(t,n.parent,s),o):o}function Bt(t,e,s,n,o,c,i){const r=t.positions.length/3,a=[],l=[];for(let g=0;g<r;g++){const y=G(e,[t.positions[g*3],t.positions[g*3+1],t.positions[g*3+2]]);a.push(y),l.push(G(n.view,y))}const h=t.indices.length/3,u=[],d=[];for(let g=0;g<h;g++){const[y,x,f]=[0,1,2].map(M=>a[t.indices[g*3+M]]),v=_(lt(Y(x,y),Y(f,y)));u.push(v);const w=n.orthographic?H(n.forward,-1):Y(n.position,y);d.push(D(v,w)>0)}const p=g=>-l[g][2]>=n.near,m=[];for(let g=0;g<h;g++){const y=d[g];if(!y&&!c.doubleSided)continue;const x=[t.indices[g*3],t.indices[g*3+1],t.indices[g*3+2]],f=x.map(k=>l[k]);if(f.every(k=>-k[2]>n.far))continue;const v=Yn(f,n.near);if(v.length<3)continue;const w=y?1:-1,M=H(u[g],w),$=x.map(k=>H(Xn(s,t.normals[k*3],t.normals[k*3+1],t.normals[k*3+2]),w)),b=H(at(at(a[x[0]],a[x[1]]),a[x[2]]),1/3),P=-(f[0][2]+f[1][2]+f[2][2])/3,E=[];if(i.ink&&y)for(const k of t.faceEdges[g]){const I=k.across,C=I===-1||!d[I],F=c.creases!==!1&&!C&&g<I&&D(u[g],u[I])<jn;(C||F)&&p(k.a)&&p(k.b)&&E.push([o(l[k.a]),o(l[k.b])])}const S=i.opacity*On(i.nearFade,P);if(S<=0)continue;const A=v.map(o);for(let k=1;k+1<A.length;k++)m.push({objectId:i.objectId,objectIndex:i.objectIndex,layer:i.layer,face:g,part:k-1,screen:[A[0],A[k],A[k+1]],depth:P,centroid:b,normal:M,vertexNormals:$,material:c,color:i.color??t.faceColors?.[g],opacity:S,outline:k===1?E:[],ink:i.ink})}return m}class Hn{ctx;constructor(e){this.ctx=e}render(e){wt(this.ctx,e)}}function wt(t,e){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";let s=0;const n=(i,r)=>{for(;s<e.drawables.length;){const a=e.drawables[s];if(a.layer>i||a.layer===i&&a.depth<=r)break;t.save(),a.draw(t,e),t.restore(),s++}};let o=[];const c=i=>{if(o.length===0)return;const r=[];for(const a of o)i&&i.objectId===a.objectId&&i.layer===a.layer&&a.depth-i.depth<=Fn*a.depth?r.push(a):Qn(t,a.triangle,e);o=r};for(const i of e.triangles){c(i),n(i.layer,i.depth);const{color:r,alpha:a}=Rt(i,e.lights,e.camera,e.fog),[l,h,u]=i.screen;t.globalAlpha=a,t.beginPath(),t.moveTo(l.x,l.y),t.lineTo(h.x,h.y),t.lineTo(u.x,u.y),t.closePath(),t.fillStyle=r,t.fill(),a>=1&&(t.strokeStyle=r,t.lineWidth=.75,t.stroke()),i.ink&&i.outline.length>0&&o.push({objectId:i.objectId,layer:i.layer,depth:i.depth,triangle:i})}c(),n(1/0,-1/0),t.restore()}const Fn=.04;function Qn(t,e,s){const n=e.ink;t.globalAlpha=e.opacity,t.strokeStyle=Qt(n.color,s.fog,Lt(e.centroid,s.camera.position)),t.lineWidth=n.width,t.beginPath();for(const[o,c]of e.outline)t.moveTo(o.x,o.y),t.lineTo(c.x,c.y);t.stroke()}class Rn{renderer;width;height;scenes=new Map;values=new Map;valuesAt;constructor(e,s){this.renderer=e,this.width=s.width,this.height=s.height,this.valuesAt=s.valuesAt}registerScene(e){this.scenes.set(e.scene.id,e)}unregisterScene(e){this.scenes.delete(e)}resize(e,s){this.width=e,this.height=s}applyState(e){this.values=e.values,this.time=e.currentTime??this.time}time=0;render(){for(const e of this.scenes.values())this.renderer.render(bt(e,this.values,{width:this.width,height:this.height,time:this.time,valuesAt:this.valuesAt}))}}function Vn(t,e,s,n){const o=n.stateAt,c=n.valuesAt??(o&&(i=>o(i).values));wt(t,bt(e,s??new Map,{width:n.width,height:n.height,time:n.time,period:n.period,valuesAt:c}))}function Un(t={}){const e=t.id??"penrose",s=t.length??4,n=t.thickness??.8,o=t.material??"penrose",[c,i,r]=t.position??[0,0,0],a=n/2,l=(m,g,y)=>({id:`${e}-${m}`,kind:"mesh",geometry:{type:"box",size:[y[0]-g[0],y[1]-g[1],y[2]-g[2]]},material:o,position:[c+(g[0]+y[0])/2,i+(g[1]+y[1])/2,r+(g[2]+y[2])/2]}),h=[l("x",[-a,-a,-a],[s-a,a,a]),l("y",[s-a,-a,-a],[s+a,s-a,a]),l("z",[s-a,s-a,-a],[s+a,s+a,s])],u=[c+2*s/3,i+s/3,r+s/3],d=(t.distance??20)/Math.sqrt(3),p={id:`${e}-camera`,kind:"camera",projection:"orthographic",height:s*1.6,near:.1,far:(t.distance??20)*3,position:[u[0]+d,u[1]+d,u[2]+d],lookAt:u};return{objects:h,camera:p}}T.Canvas2DRenderer=Hn,T.MeshBuilder=O,T.Scene3DAdapter=Rn,T.boxMesh=Pt,T.coneMesh=zt,T.cylinderMesh=Et,T.dollyPosition=$n,T.drawResolvedScene=wt,T.drawScene3D=Vn,T.extrudeMesh=St,T.fogAmount=nt,T.geometryMesh=Nt,T.inkColor=Qt,T.lightAt=mt,T.loadScene3D=fn,T.lookAtView=Ft,T.meshEdges=kt,T.orbitPosition=kn,T.parseColor=L,T.penroseTriangle3D=Un,T.planeMesh=Ct,T.pointInPolygon=st,T.prepareMesh=Yt,T.projectionMatrix=gt,T.rawMesh=Xt,T.resolveScene3D=bt,T.rollView=pt,T.shadeTriangle=Rt,T.signedArea=Q,T.sphereMesh=Tt,T.torusMesh=jt,T.triangulate=It,T.validateScene3D=xt,Object.defineProperty(T,Symbol.toStringTag,{value:"Module"})})(this.tinyfly=this.tinyfly||{});