@glyphcss/core 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +110 -0
- package/dist/index.cjs +3 -0
- package/dist/index.d.cts +1181 -0
- package/dist/index.d.ts +1181 -0
- package/dist/index.js +3 -0
- package/package.json +52 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Layoutit
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
> **Status: pre-1.0. APIs may still change before a stable 1.0 release.**
|
|
2
|
+
|
|
3
|
+
# @glyphcss/core
|
|
4
|
+
|
|
5
|
+
Framework-agnostic math, parsers, and helpers for ASCII polygon-mesh rendering. Zero browser globals: runs in Node, workers, or any JS environment.
|
|
6
|
+
|
|
7
|
+
This package contains the entire non-rendering side of glyphcss: OBJ / glTF / GLB / MagicaVoxel parsers, polygon normalization, coplanar merge, Lambert lighting, isometric camera state, and all shared TypeScript types.
|
|
8
|
+
|
|
9
|
+
## When to use directly
|
|
10
|
+
|
|
11
|
+
Most users install `@glyphcss/react`, `@glyphcss/vue`, or `glyphcss` (vanilla). Those packages include `@glyphcss/core` as a transitive runtime dependency and re-export its public types and functions, so you never need to write `import ... from "@glyphcss/core"` in application code.
|
|
12
|
+
|
|
13
|
+
Install `@glyphcss/core` directly when you:
|
|
14
|
+
|
|
15
|
+
- Build custom rendering outside React / Vue / vanilla (e.g., a Svelte wrapper, a server-side OBJ validator, a CLI mesh processor).
|
|
16
|
+
- Want only the parsers / math without any rendering layer.
|
|
17
|
+
- Are writing a glyphcss plugin or tooling that must remain framework-neutral.
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
npm install @glyphcss/core
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Public surface
|
|
24
|
+
|
|
25
|
+
### Types
|
|
26
|
+
|
|
27
|
+
| Type | Description |
|
|
28
|
+
|---|---|
|
|
29
|
+
| `Vec2` | `[number, number]`: 2D point or UV coordinate |
|
|
30
|
+
| `Vec3` | `[number, number, number]`: 3D point or direction |
|
|
31
|
+
| `Polygon` | Single renderable polygon: `vertices`, optional `color`, `texture`, `uvs`, `data` |
|
|
32
|
+
| `GlyphcssDirectionalLight` | Directional light: `direction`, optional `color`, optional `intensity` |
|
|
33
|
+
| `GlyphcssAmbientLight` | Ambient fill light: optional `color`, optional `intensity` |
|
|
34
|
+
| `ParseResult` | Unified parser return: `polygons`, `objectUrls`, `dispose()`, `warnings` |
|
|
35
|
+
| `ObjParseOptions` | Options for `parseObj` |
|
|
36
|
+
| `GltfParseOptions` | Options for `parseGltf` |
|
|
37
|
+
| `VoxParseOptions` | Options for `parseVox` |
|
|
38
|
+
| `MtlParseResult` | `{ colors, textures }` from `parseMtl` |
|
|
39
|
+
| `NormalizeResult` | `{ polygons, warnings }` from `normalizePolygons` |
|
|
40
|
+
| `CameraState` | Camera target, angles, zoom, and dolly distance |
|
|
41
|
+
| `CameraHandle` | Mutable camera object from `createIsometricCamera` |
|
|
42
|
+
| `AutoRotateOption` | `boolean | number | { axis, speed, pauseOnInteraction }` |
|
|
43
|
+
|
|
44
|
+
### Functions
|
|
45
|
+
|
|
46
|
+
| Function | Description |
|
|
47
|
+
|---|---|
|
|
48
|
+
| `normalizePolygons(input)` | Validates polygons. Drops degenerate ones, auto-triangulates non-coplanar N-gons, strips mismatched UVs. Returns `{ polygons, warnings }`. |
|
|
49
|
+
| `mergePolygons(polygons)` | Coplanar same-material adjacent merge. Reduces rendered polygon count on flat surfaces. |
|
|
50
|
+
| `optimizeMeshPolygons(polygons, options?)` | Applies lossless or lossy mesh-resolution optimization; defaults to `meshResolution: "lossy"`. |
|
|
51
|
+
| `computeSceneBbox(polygons)` | Computes min/max bounds across all polygon vertices. |
|
|
52
|
+
| `createIsometricCamera(initial?)` | Creates a mutable camera handle with `state`, `update(partial)`, and `getStyle()`. |
|
|
53
|
+
| `parseObj(text, options?)` | Parses OBJ text into `ParseResult`. Supports UV (`vt`), materials, `map_Kd` textures. |
|
|
54
|
+
| `parseMtl(text)` | Parses MTL text into `{ colors, textures }`. |
|
|
55
|
+
| `parseGltf(buffer, options?)` | Parses GLB or glTF `ArrayBuffer` into `ParseResult`. Extracts embedded textures as blob URLs. |
|
|
56
|
+
| `parseVox(buffer, options?)` | Parses MagicaVoxel `.vox` `ArrayBuffer` into `ParseResult`. Face-culls interior voxel faces. |
|
|
57
|
+
| `loadMesh(url, options?)` | Fetches a URL, dispatches to the right parser by extension (`.obj`, `.glb`, `.gltf`, `.vox`). Returns `Promise<ParseResult>`. |
|
|
58
|
+
| `parseColor(input)` | Parse any CSS color string to `{ r, g, b, a }`. |
|
|
59
|
+
| `shadeColor(input, lambert, ...)` | Apply Lambert shading factor to a color. |
|
|
60
|
+
| `computeShapeLighting(normal, baseColor, light?)` | Compute shaded color for a polygon face given a directional light and surface normal. |
|
|
61
|
+
|
|
62
|
+
## Examples
|
|
63
|
+
|
|
64
|
+
### Parse an OBJ file
|
|
65
|
+
|
|
66
|
+
```ts
|
|
67
|
+
import { parseObj } from "@glyphcss/core";
|
|
68
|
+
|
|
69
|
+
const text = await fetch("/cottage.obj").then(r => r.text());
|
|
70
|
+
const { polygons, warnings, dispose } = parseObj(text, {
|
|
71
|
+
targetSize: 40,
|
|
72
|
+
defaultColor: "#cccccc",
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
console.log(polygons.length, "polygons");
|
|
76
|
+
warnings.forEach(w => console.warn(w));
|
|
77
|
+
|
|
78
|
+
dispose();
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
### Normalize a polygon list
|
|
82
|
+
|
|
83
|
+
```ts
|
|
84
|
+
import { normalizePolygons } from "@glyphcss/core";
|
|
85
|
+
import type { Polygon } from "@glyphcss/core";
|
|
86
|
+
|
|
87
|
+
const raw: Polygon[] = [
|
|
88
|
+
{ vertices: [[0,0,0], [1,0,0], [0,1,0]], color: "#f00" },
|
|
89
|
+
{ vertices: [[0,0,0], [0,0,0], [0,0,0]] }, // degenerate: will be dropped
|
|
90
|
+
{ vertices: [[0,0,0], [1,0,0], [0.5,1,0], [0.5,1,0.1]] }, // non-coplanar quad → triangulated
|
|
91
|
+
];
|
|
92
|
+
|
|
93
|
+
const { polygons, warnings } = normalizePolygons(raw);
|
|
94
|
+
console.log(polygons.length); // 2
|
|
95
|
+
warnings.forEach(w => console.warn(w));
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
### Merge coplanar polygons
|
|
99
|
+
|
|
100
|
+
```ts
|
|
101
|
+
import { parseGltf, mergePolygons } from "@glyphcss/core";
|
|
102
|
+
|
|
103
|
+
const buf = await fetch("/cottage.glb").then(r => r.arrayBuffer());
|
|
104
|
+
const { polygons, dispose } = parseGltf(buf, { targetSize: 60 });
|
|
105
|
+
|
|
106
|
+
const merged = mergePolygons(polygons);
|
|
107
|
+
console.log(`${polygons.length} triangles → ${merged.length} merged polygons`);
|
|
108
|
+
|
|
109
|
+
dispose();
|
|
110
|
+
```
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
"use strict";var zn=Object.defineProperty;var yo=Object.getOwnPropertyDescriptor;var bo=Object.getOwnPropertyNames;var Po=Object.prototype.hasOwnProperty;var Mo=(e,n)=>{for(var r in n)zn(e,r,{get:n[r],enumerable:!0})},vo=(e,n,r,t)=>{if(n&&typeof n=="object"||typeof n=="function")for(let o of bo(n))!Po.call(e,o)&&o!==r&&zn(e,o,{get:()=>n[o],enumerable:!(t=yo(n,o))||t.enumerable});return e};var Ao=e=>vo(zn({},"__esModule",{value:!0}),e);var Ac={};Mo(Ac,{BASE_TILE:()=>Zt,CAMERA_BACKFACE_CULL_EPS:()=>We,DEFAULT_CAMERA_STATE:()=>Ce,DEFAULT_PROJECTION:()=>Et,LoopOnce:()=>Br,LoopPingPong:()=>Xr,LoopRepeat:()=>jr,QUAT_IDENTITY:()=>Bt,VOXEL_CAMERA_CULL_AXIS_EPS:()=>at,VOXEL_CAMERA_CULL_NORMAL_LIMIT:()=>lt,arrowPolygons:()=>xt,axesHelperPolygons:()=>dt,bakeSolidTextureSampledPolygons:()=>lo,bakeSolidTextureSamples:()=>Tn,buildSceneContext:()=>Ut,cameraCullNormalGroups:()=>mt,cameraCullNormalGroupsFromPolygons:()=>zr,cameraCullNormalKey:()=>ft,cameraCullVisibleSignature:()=>Fr,cameraFacingDepth:()=>ut,clampChannel:()=>xe,computeSceneBbox:()=>jn,computeShapeLighting:()=>Nt,coverPlanarPolygons:()=>sn,createGlyphcssAnimationMixer:()=>Yr,createIsometricCamera:()=>qt,cullInteriorPolygons:()=>be,dedupeOverlappingPolygons:()=>tn,eulerXYZFromQuat:()=>Ht,findOverlappingPolygonDuplicates:()=>Hn,formatColor:()=>Ge,inverseRotateVec3:()=>$t,isAxisAlignedSurfaceNormal:()=>gt,isVoxelCameraCullableNormalGroups:()=>Ur,loadMesh:()=>go,mergePolygons:()=>se,normalFacesCamera:()=>bn,normalizeInvertMultiplier:()=>Qt,normalizePolygons:()=>en,octahedronPolygons:()=>Pt,optimizeMeshPolygons:()=>gn,parseColor:()=>Ae,parseGltf:()=>Sn,parseHexColor:()=>Un,parseMtl:()=>vn,parseObj:()=>Mn,parsePureColor:()=>Qe,parseRgbColor:()=>Fn,parseVox:()=>wn,planePolygons:()=>bt,polygonCssSurfaceNormal:()=>yn,polygonFaces:()=>Ft,polygonFacesCamera:()=>Dr,project:()=>po,quatFromAxisAngle:()=>Xt,quatFromEulerXYZ:()=>Yt,quatMultiply:()=>jt,ringPolygons:()=>ht,ringQuadPolygons:()=>yt,rotateVec3:()=>De,shadeColor:()=>_t,trianglesToFeatureEdges:()=>ho});module.exports=Ao(Ac);var Et="cubic";function Un(e){let n=e.replace("#","");if(n.length===3){let r=parseInt(n[0]+n[0],16),t=parseInt(n[1]+n[1],16),o=parseInt(n[2]+n[2],16);return Number.isNaN(r)||Number.isNaN(t)||Number.isNaN(o)?null:{rgb:[r,t,o],alpha:1}}if(n.length===6){let r=parseInt(n.slice(0,2),16),t=parseInt(n.slice(2,4),16),o=parseInt(n.slice(4,6),16);return Number.isNaN(r)||Number.isNaN(t)||Number.isNaN(o)?null:{rgb:[r,t,o],alpha:1}}return null}function Fn(e){let n=e.match(/rgba?\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*(?:,\s*([\d.]+)\s*)?\)/i);return n?{rgb:[Number(n[1]),Number(n[2]),Number(n[3])],alpha:n[4]?Number(n[4]):1}:null}function Qe(e){if(!e)return null;let n=e.trim(),r=Un(n);return r||Fn(n)}function xe(e){return Math.max(0,Math.min(255,Math.round(e)))}function Ge(e){let[n,r,t]=e.rgb.map(xe);return e.alpha<1?`rgba(${n}, ${r}, ${t}, ${e.alpha})`:`rgb(${n}, ${r}, ${t})`}var Bn={rgb:[204,204,204],alpha:1},Ot=new Map;function Ae(e){if(!e)return null;let n=e.trim(),r=Ot.get(n);if(r)return r;let t=Qe(n);return t?(Ot.set(n,t),t):null}function _t(e,n){let r=Ae(e)??Bn,t=[xe(r.rgb[0]+n),xe(r.rgb[1]+n),xe(r.rgb[2]+n)];return Ge({rgb:t,alpha:r.alpha})}var $n={direction:[0,0,-1],color:"#ffffff",intensity:1},Rt={color:"#ffffff",intensity:.4};function Lt(e){let n=Math.hypot(e[0],e[1],e[2]);return n<1e-12?[0,0,0]:[e[0]/n,e[1]/n,e[2]/n]}function kt(e,n,r){let t=Ae(n)??Bn;return e*(t.rgb[r]/255)}function Nt(e,n,r,t){let o=Ae(n)??Bn,s=Lt(r?.direction??$n.direction),i=r?.color??$n.color,l=Math.max(0,r?.intensity??$n.intensity),c=t?.color??Rt.color,a=Math.max(0,t?.intensity??Rt.intensity),u=Lt(e),f=Math.max(0,-(u[0]*s[0]+u[1]*s[1]+u[2]*s[2])),g=l*f,m=[0,0,0];for(let p=0;p<3;p=p+1){let d=o.rgb[p],y=kt(d,c,p)*a,b=kt(d,i,p)*g;m[p]=xe(y+b)}return Ge({rgb:m,alpha:o.alpha})}var Gt="#cccccc",qe=(e,n)=>[e[0]-n[0],e[1]-n[1],e[2]-n[2]],Dt=(e,n)=>[e[1]*n[2]-e[2]*n[1],e[2]*n[0]-e[0]*n[2],e[0]*n[1]-e[1]*n[0]],zt=(e,n)=>e[0]*n[0]+e[1]*n[1]+e[2]*n[2],Je=e=>Math.hypot(e[0],e[1],e[2]);function Co(e){let n=1/0,r=1/0,t=1/0,o=-1/0,s=-1/0,i=-1/0;for(let u of e)u[0]<n&&(n=u[0]),u[0]>o&&(o=u[0]),u[1]<r&&(r=u[1]),u[1]>s&&(s=u[1]),u[2]<t&&(t=u[2]),u[2]>i&&(i=u[2]);let l=o-n,c=s-r,a=i-t;return Math.hypot(l,c,a)}function Vo(){let e=globalThis;if(e.__POLYCSS_DEV__===!0)return!0;if(e.__POLYCSS_DEV__===!1)return!1;let n=e.process?.env?.NODE_ENV;return typeof n=="string"?n!=="production":!1}function en(e){let n=[],r=[];if(!e||e.length===0)return{polygons:n,warnings:r};for(let t=0;t<e.length;t++){let o=e[t];if(!o||!Array.isArray(o.vertices)){r.push(`Polygon ${t}: missing vertices, dropped`);continue}let s=o.vertices;if(s.length<3){r.push(`Polygon ${t}: ${s.length} vertices (need >= 3), dropped`);continue}let i=qe(s[1],s[0]),l=qe(s[2],s[0]),c=Dt(i,l),a=Je(c);if(s.length===3){if(a<1e-12){Je(i)<1e-12||Je(l)<1e-12?r.push(`Polygon ${t}: zero-area triangle (coincident vertices), dropped`):r.push(`Polygon ${t}: vertices collinear, dropped`);continue}}else if(a<1e-12){r.push(`Polygon ${t}: first 3 vertices collinear, dropped`);continue}let u=[{...o,vertices:s.slice()}];if(s.length>=4){let f=[c[0]/a,c[1]/a,c[2]/a],g=zt(f,s[0]),m=Co(s),p=Math.max(1e-6,m*.001),d=0;for(let y=3;y<s.length;y++){let b=Math.abs(zt(f,s[y])-g);b>d&&(d=b)}if(d>p){let y=[],b=o.uvs&&o.uvs.length===s.length?o.uvs:void 0;for(let M=1;M<s.length-1;M++){let v=[s[0].slice(),s[M].slice(),s[M+1].slice()],P=Dt(qe(v[1],v[0]),qe(v[2],v[0]));if(Je(P)<1e-12)continue;let x={...o,vertices:v};b&&(x.uvs=[b[0].slice(),b[M].slice(),b[M+1].slice()]),y.push(x)}if(y.length===0){r.push(`Polygon ${t}: ${s.length} non-coplanar vertices, fan-triangulation produced no valid triangles, dropped`);continue}r.push(`Polygon ${t}: ${s.length} non-coplanar vertices, fan-triangulated to ${y.length} triangles`),u=y}}for(let f of u){let g=So(f,t,r);g&&n.push(g)}}return{polygons:n,warnings:r}}function So(e,n,r){let t={vertices:e.vertices},o=e.texture;if(typeof o=="string"&&o===""&&(o=void 0),e.color!==void 0&&o!==void 0&&Vo()&&r.push(`Polygon ${n}: color and texture both set; texture wins`),e.color!==void 0&&(Ae(e.color)?t.color=e.color:(r.push(`Polygon ${n}: invalid color "${e.color}", replaced with ${Gt}`),t.color=Gt)),o!==void 0&&(t.texture=o),e.uvs!==void 0&&(!Array.isArray(e.uvs)||e.uvs.length!==e.vertices.length?r.push(`Polygon ${n}: uvs length ${Array.isArray(e.uvs)?e.uvs.length:"?"} != vertices length ${e.vertices.length}, uvs stripped`):t.uvs=e.uvs),e.data!==void 0&&e.data!==null&&typeof e.data=="object"){let s={},i=!1;for(let l of Object.keys(e.data)){let c=e.data[l];typeof c=="string"||typeof c=="number"||typeof c=="boolean"?s[l]=c:(i=!0,r.push(`Polygon ${n}: data["${l}"] has non-primitive value, key dropped`))}Object.keys(s).length>0&&(t.data=s)}return t}function jn(e){if(!e||e.length===0)return{min:[0,0,0],max:[0,0,0]};let n=1/0,r=1/0,t=1/0,o=-1/0,s=-1/0,i=-1/0,l=!1;for(let c of e)if(c?.vertices)for(let a of c.vertices)a&&(l=!0,a[0]<n&&(n=a[0]),a[0]>o&&(o=a[0]),a[1]<r&&(r=a[1]),a[1]>s&&(s=a[1]),a[2]<t&&(t=a[2]),a[2]>i&&(i=a[2]));return l?{min:[n,r,t],max:[o,s,i]}:{min:[0,0,0],max:[0,0,0]}}function Ut(e){let n=e.polygons??[],r,t;if(e.skipNormalize)r=n,t=[];else{let l=en(n);r=l.polygons,t=l.warnings}let o=jn(r),s=[o.max[0]-o.min[0],o.max[1]-o.min[1],o.max[2]-o.min[2]];return{context:{polygons:r,sceneBbox:o,warnings:t},dimensions:{sceneBbox:o,size:s},warnings:t}}function Ft(e){return!e.vertices||e.vertices.length<3?[]:[{v:e.vertices.map(n=>[n[0],n[1],n[2]]),color:e.color}]}function De(e,n,r,t){let o=n*Math.PI/180,s=r*Math.PI/180,i=t*Math.PI/180,[l,c,a]=e;if(i!==0){let u=Math.cos(i),f=Math.sin(i);[l,c]=[l*u-c*f,l*f+c*u]}if(s!==0){let u=Math.cos(s),f=Math.sin(s);[l,a]=[l*u+a*f,-l*f+a*u]}if(o!==0){let u=Math.cos(o),f=Math.sin(o);[c,a]=[c*u-a*f,c*f+a*u]}return[l,c,a]}function $t(e,n){let r=-n[0]*Math.PI/180,t=-n[1]*Math.PI/180,o=-n[2]*Math.PI/180,[s,i,l]=e;if(r!==0){let c=Math.cos(r),a=Math.sin(r);[i,l]=[i*c-l*a,i*a+l*c]}if(t!==0){let c=Math.cos(t),a=Math.sin(t);[s,l]=[s*c+l*a,-s*a+l*c]}if(o!==0){let c=Math.cos(o),a=Math.sin(o);[s,i]=[s*c-i*a,s*a+i*c]}return[s,i,l]}var Xn=Math.PI/180,ze=180/Math.PI,Bt=[1,0,0,0];function jt(e,n){let[r,t,o,s]=e,[i,l,c,a]=n;return[r*i-t*l-o*c-s*a,r*l+t*i+o*a-s*c,r*c-t*a+o*i+s*l,r*a+t*c-o*l+s*i]}function Xt(e,n){let r=n*.5,t=Math.sin(r);return[Math.cos(r),e[0]*t,e[1]*t,e[2]*t]}function Yt(e){let n=e[0]*Xn,r=e[1]*Xn,t=e[2]*Xn,o=Math.cos(n*.5),s=Math.sin(n*.5),i=Math.cos(r*.5),l=Math.sin(r*.5),c=Math.cos(t*.5),a=Math.sin(t*.5);return[o*i*c-s*l*a,s*i*c+o*l*a,o*l*c-s*i*a,o*i*a+s*l*c]}function Ht(e){let[n,r,t,o]=e,s=2*(r*o+n*t),i=2*(t*o-n*r),l=1-2*(r*r+t*t),c=2*(r*t-n*o),a=1-2*(t*t+o*o),u=Math.max(-1,Math.min(1,s)),f=Math.asin(u);return Math.abs(u)<.99999?[Math.atan2(-i,l)*ze,f*ze,Math.atan2(-c,a)*ze]:[Math.atan2(2*(t*o+n*r),1-2*(r*r+o*o))*ze,f*ze,0]}var Zt=50;function Qt(e){if(typeof e=="number")return e===0?void 0:e<0?-1:1;if(typeof e=="boolean")return e?-1:1}var Ce={target:[0,0,0],rotX:65,rotY:45,zoom:.65,distance:0},Kt=100,Wt=1e4,Oe=e=>Math.round(e*Kt)/Kt,To=e=>Math.round(e*Wt)/Wt;function qt(e={}){let n={target:e.target??[...Ce.target],rotX:e.rotX??Ce.rotX,rotY:e.rotY??Ce.rotY,zoom:e.zoom??Ce.zoom,distance:e.distance??Ce.distance};function r(o){o.target!==void 0&&(n.target=[Oe(o.target[0]),Oe(o.target[1]),Oe(o.target[2])]),o.rotX!==void 0&&(n.rotX=Oe(o.rotX)),o.rotY!==void 0&&(n.rotY=Oe(o.rotY)),o.zoom!==void 0&&(n.zoom=To(o.zoom)),o.distance!==void 0&&(n.distance=Oe(o.distance))}function t(o={}){let i=(o.cols??0)*50,l=(o.rows??0)*50,[c,a,u]=n.target,f=a*50,g=c*50,m=u*50;return{transform:`${n.distance!==0?` translateZ(${-n.distance}px)`:""}scale(${n.zoom}) rotateX(${n.rotX}deg) rotate(${n.rotY}deg) translate3d(${-f}px, ${-g}px, ${-m}px)`,width:`${i}px`,height:`${l}px`}}return{state:n,update:r,getStyle:t}}var ye=(e,n)=>[e[0]-n[0],e[1]-n[1],e[2]-n[2]],nn=(e,n)=>[e[1]*n[2]-e[2]*n[1],e[2]*n[0]-e[0]*n[2],e[0]*n[1]-e[1]*n[0]],Ue=(e,n)=>e[0]*n[0]+e[1]*n[1]+e[2]*n[2],Yn=e=>Math.hypot(e[0],e[1],e[2]),he=(e,n)=>e[0]===n[0]&&e[1]===n[1]&&e[2]===n[2];function wo(e,n){let r=`${e[0]},${e[1]},${e[2]}`,t=`${n[0]},${n[1]},${n[2]}`;return r<t?`${r}|${t}`:`${t}|${r}`}function er(e){if(e.length<3)return null;let n=ye(e[1],e[0]),r=ye(e[2],e[0]),t=nn(n,r),o=Yn(t);if(o<1e-12)return null;let s=[t[0]/o,t[1]/o,t[2]/o],i=Ue(s,e[0]);return{normal:s,d:i}}function Io(e,n){return Ue(e.normal,n.normal)<1-.001?!1:Math.abs(e.d-n.d)<.05}function Eo(e,n){return Ue(e.normal,n.normal)<1-.001?!1:Math.abs(e.d-n.d)<.001}function Oo(e,n,r,t){let o=e.vertices.findIndex(V=>he(V,r)),s=e.vertices.findIndex(V=>he(V,t)),i=n.vertices.findIndex(V=>he(V,r)),l=n.vertices.findIndex(V=>he(V,t));if(o<0||s<0||i<0||l<0)return null;let c=e.vertices.length,a=n.vertices.length,u=(o+1)%c===s,f=(i+1)%a===l;if(u===f)return null;let g=u?s:o,m=u?o:s,p=f?l:i,d=f?i:l,y=!!(e.uvs&&n.uvs),b=[],M=y?[]:void 0,v=g;for(;b.push(e.vertices[v]),M&&M.push(e.uvs[v]),v!==m;)v=(v+1)%c;for(v=p;b.push(n.vertices[v]),M&&M.push(n.uvs[v]),v!==d;)v=(v+1)%a;let P=[],x=M?[]:void 0;for(let V=0;V<b.length;V++)(P.length===0||!he(b[V],P[P.length-1]))&&(P.push(b[V]),x&&M&&x.push(M[V]));if(P.length>1&&he(P[0],P[P.length-1])&&(P.pop(),x?.pop()),y)return Jt(P,x);let A=[],T=x?[]:void 0;for(let V=0;V<P.length;V++){let w=P[(V-1+P.length)%P.length],O=P[V],L=P[(V+1)%P.length],U=nn(ye(O,w),ye(L,w));Yn(U)>1e-9&&(A.push(O),T&&x&&T.push(x[V]))}return A.length<3?null:Jt(A,T)}function Ro(e,n){let r=e.length,t=0;for(let o=0;o<r;o++){let s=e[o],i=e[(o+1)%r],l=e[(o+2)%r],c=ye(i,s),a=ye(l,i),u=Ue(nn(c,a),n);if(Math.abs(u)<1e-9)continue;let f=u>0?1:-1;if(t===0)t=f;else if(f!==t)return!1}return!0}function Lo(e,n){if(e.length<3)return!1;let r=er(e);if(!r)return!1;for(let t of e)if(Math.abs(Ue(r.normal,t)-r.d)>n)return!1;return!0}function ko(e){return e.map(n=>({vertices:n.vertices.map(r=>[...r]),uvs:n.uvs.map(r=>[...r])}))}function _o(e,n){let r=[];for(let t=1;t<e.length-1;t++)r.push({vertices:[[...e[0]],[...e[t]],[...e[t+1]]],uvs:[[...n[0]],[...n[t]],[...n[t+1]]]});return r}function Jt(e,n){for(let r=0;r<e.length;r++){let t=e[r],o=e[(r+1)%e.length],s=e[(r+2)%e.length];if(!(Yn(nn(ye(o,t),ye(s,t)))<=1e-9))return r===0?{vertices:e,uvs:n}:{vertices:[...e.slice(r),...e.slice(0,r)],uvs:n?[...n.slice(r),...n.slice(0,r)]:void 0}}return{vertices:e,uvs:n}}function se(e){let n=[],r=[];for(let o of e??[]){if(!o||!o.vertices||o.vertices.length<3){o&&n.push(o);continue}let s=o.vertices.map(a=>[a[0],a[1],a[2]]),i=er(s);if(!i){n.push(o);continue}let l=o.texture&&o.uvs&&o.uvs.length===s.length?o.uvs.map(a=>[a[0],a[1]]):void 0,c=o.texture&&l?o.textureTriangles?.length?ko(o.textureTriangles):_o(s,l):void 0;r.push({vertices:s,uvs:l,color:o.color??"#cccccc",texture:o.texture,textureTriangles:c,normal:i.normal,d:i.d,alive:!0,data:o.data})}let t=()=>{let o=new Map;for(let l=0;l<r.length;l++){let c=r[l];if(!c.alive)continue;let a=c.vertices.length;for(let u=0;u<a;u++){let f=c.vertices[u],g=c.vertices[(u+1)%a],m=wo(f,g),p=o.get(m);p||(p=[],o.set(m,p)),p.push(l)}}let s=!1,i=(l,c)=>{for(let a=0;a<l.vertices.length;a++){let u=l.vertices[a],f=l.vertices[(a+1)%l.vertices.length];for(let g=0;g<c.vertices.length;g++){let m=c.vertices[g],p=c.vertices[(g+1)%c.vertices.length];if(he(u,p)&&he(f,m))return[u,f]}}return null};for(let[,l]of o){if(l.length<2)continue;let[c,a]=l;if(c===a)continue;let u=r[c],f=r[a];if(!u.alive||!f.alive||u.color!==f.color||u.texture!==f.texture)continue;let g=!!(u.texture||f.texture);if(g&&(!u.textureTriangles||!f.textureTriangles)||!!u.uvs!=!!f.uvs||(g?!Eo(u,f):!Io(u,f)))continue;let m=i(u,f);if(!m)continue;let[p,d]=m,y=Oo(u,f,p,d);y&&Lo(y.vertices,.001)&&Ro(y.vertices,u.normal)&&(u.vertices=y.vertices,u.uvs=y.uvs,u.textureTriangles=g?[...u.textureTriangles??[],...f.textureTriangles??[]]:void 0,f.alive=!1,s=!0)}return s};for(;t(););for(let o of r){if(!o.alive)continue;let s={vertices:o.vertices,color:o.color};o.texture&&(s.texture=o.texture),o.uvs&&(s.uvs=o.uvs),o.textureTriangles?.length&&(s.textureTriangles=o.textureTriangles),o.data&&(s.data=o.data),n.push(s)}return n}var No=(e,n)=>[e[0]-n[0],e[1]-n[1],e[2]-n[2]],Go=(e,n)=>[e[1]*n[2]-e[2]*n[1],e[2]*n[0]-e[0]*n[2],e[0]*n[1]-e[1]*n[0]],fe=(e,n)=>e[0]*n[0]+e[1]*n[1]+e[2]*n[2];function Do(e,n){let r=e.vertices;if(!r||r.length<3)return null;let t=0,o=0,s=0;for(let d=0;d<r.length;d++){let y=r[d],b=r[(d+1)%r.length];t+=(y[1]-b[1])*(y[2]+b[2]),o+=(y[2]-b[2])*(y[0]+b[0]),s+=(y[0]-b[0])*(y[1]+b[1])}let i=Math.hypot(t,o,s);if(i<1e-12)return null;let l=[t/i,o/i,s/i],c=0,a=0,u=0;for(let d of r)c+=d[0],a+=d[1],u+=d[2];let f=1/r.length,g=[c*f,a*f,u*f],m=fe(l,g),p=i*.5;return{index:n,polygon:e,normal:l,d:m,centroid:g,area:p,local2D:null,bbox2D:null,basis:null}}function zo(e){let n=Math.abs(e[0])<.9?[1,0,0]:[0,1,0],r=n[0]-e[0]*fe(n,e),t=n[1]-e[1]*fe(n,e),o=n[2]-e[2]*fe(n,e),s=Math.hypot(r,t,o);r/=s,t/=s,o/=s;let i=[r,t,o],l=Go(e,i);return{u:i,v:l}}function Uo(e){if(e.local2D)return;let n=zo(e.normal),r=[],t=1/0,o=1/0,s=-1/0,i=-1/0;for(let l of e.polygon.vertices){let c=fe(l,n.u),a=fe(l,n.v);r.push([c,a]),c<t&&(t=c),c>s&&(s=c),a<o&&(o=a),a>i&&(i=a)}e.local2D=r,e.bbox2D={min:[t,o],max:[s,i]},e.basis=n}function Fo(e,n){let r=[],t=1/0,o=1/0,s=-1/0,i=-1/0;for(let l of e.vertices){let c=fe(l,n.u),a=fe(l,n.v);r.push([c,a]),c<t&&(t=c),c>s&&(s=c),a<o&&(o=a),a>i&&(i=a)}return{local2D:r,bbox2D:{min:[t,o],max:[s,i]}}}function $o(e,n){return e.max[0]>=n.min[0]&&e.min[0]<=n.max[0]&&e.max[1]>=n.min[1]&&e.min[1]<=n.max[1]}function nr(e,n){let r=!1;for(let t=0,o=n.length-1;t<n.length;o=t++){let s=n[t],i=n[o];s[1]>e[1]!=i[1]>e[1]&&e[0]<(i[0]-s[0])*(e[1]-s[1])/(i[1]-s[1]+1e-30)+s[0]&&(r=!r)}return r}function tr(e){let n=0,r=0;for(let t of e)n+=t[0],r+=t[1];return[n/e.length,r/e.length]}function Bo(e,n){let r=tr(e),t=tr(n),o=1e-4,s=0;for(let a of e){let u=[a[0]+(r[0]-a[0])*o,a[1]+(r[1]-a[1])*o];nr(u,n)&&s++}let i=0;for(let a of n){let u=[a[0]+(t[0]-a[0])*o,a[1]+(t[1]-a[1])*o];nr(u,e)&&i++}let l=s/e.length,c=i/n.length;return Math.max(l,c)}function jo(e,n){let r=e.normal[0],t=e.normal[1],o=e.normal[2],s=Math.abs(r),i=Math.abs(t),l=Math.abs(o),c=r;i>s&&i>l?c=t:l>s&&l>i&&(c=o),c<0&&(r=-r,t=-t,o=-o);let a=Math.round(r/.05),u=Math.round(t/.05),f=Math.round(o/.05),g=e.d*(e.normal[0]===r&&e.normal[1]===t&&e.normal[2]===o?1:-1),m=Math.round(g/(n*2));return`${a},${u},${f}|${m}`}function Xo(e,n,r,t){let o=fe(e.normal,n.normal);if(Math.abs(o)<1-r)return!1;let s=o>0?1:-1;return Math.abs(e.d-s*n.d)<t}function rr(e,n){let r=No(n,e.centroid);return fe(e.normal,r)>0}function Hn(e,n){if(!e||e.length<2)return new Set;let r=n?.normalTolerance??.001,t=n?.distanceTolerance??.05,o=n?.overlapFraction??.7,s=[];for(let m=0;m<e.length;m++){let p=Do(e[m],m);p&&s.push(p)}if(s.length<2)return new Set;let i=0,l=0,c=0,a=0;for(let m of s)i+=m.centroid[0]*m.area,l+=m.centroid[1]*m.area,c+=m.centroid[2]*m.area,a+=m.area;let u=a>0?[i/a,l/a,c/a]:[0,0,0],f=new Map;for(let m of s){let p=jo(m,t),d=f.get(p);d||(d=[],f.set(p,d)),d.push(m)}let g=new Set;for(let m of f.values())if(!(m.length<2))for(let p=0;p<m.length;p++){let d=m[p];if(!g.has(d.index))for(let y=p+1;y<m.length;y++){let b=m[y];if(g.has(b.index)||!Xo(d,b,r,t))continue;Uo(d);let M=Fo(b.polygon,d.basis);if(!$o(d.bbox2D,M.bbox2D)||Bo(d.local2D,M.local2D)<o)continue;let P=rr(d,u),x=rr(b,u),A;if(P&&!x?A=d:x&&!P?A=b:A=d.area<b.area?d:b,g.add(A.index),A===d)break}}return g}function tn(e,n){if(!e||e.length<2)return e??[];let r=Hn(e,n);if(r.size===0)return e;let t=[];for(let o=0;o<e.length;o++)r.has(o)||t.push(e[o]);return t}var Yo=4,Ho=8,Ko=.001,Wo=1e-9,Zo=1e6,Qo=1e6,Ve=(e,n)=>[e[0]-n[0],e[1]-n[1],e[2]-n[2]],Zn=(e,n)=>[e[0]+n[0],e[1]+n[1],e[2]+n[2]],Be=(e,n)=>[e[0]*n,e[1]*n,e[2]*n],Se=(e,n)=>e[0]*n[0]+e[1]*n[1]+e[2]*n[2],ar=(e,n)=>[e[1]*n[2]-e[2]*n[1],e[2]*n[0]-e[0]*n[2],e[0]*n[1]-e[1]*n[0]],lr=e=>Math.hypot(e[0],e[1],e[2]);function je(e){let n=lr(e);return n<=Wo?null:[e[0]/n,e[1]/n,e[2]/n]}function $e(e,n){return Math.round(e*n)/n}function le(e){return $e(e,Zo)}function Kn(e){return $e(e,Qo)}function Wn(e,n){return Math.abs(e[0]-n[0])<=1e-7&&Math.abs(e[1]-n[1])<=1e-7}function ge(e){return`${le(e[0])},${le(e[1])}`}function qo(e){let n=ge(e.a),r=ge(e.b);return n<r?`${n}|${r}`:`${r}|${n}`}function Re(e){return`${e[0]},${e[1]},${e[2]}`}function ur(e,n){let r=Re(e),t=Re(n);return r<t?`${r}|${t}`:`${t}|${r}`}function Qn(e){return e?Object.keys(e).sort().map(n=>`${n}:${String(e[n])}`).join("|"):""}function or(e){return[e.color??"#cccccc",e.texture??"",e.uvs?"uv":"plain",Qn(e.data)].join("|")}function fr(e){let n=e.vertices;if(!n||n.length<3)return null;let r=n[0],t=[0,0,0];for(let s=1;s<n.length-1;s++)t=Zn(t,ar(Ve(n[s],r),Ve(n[s+1],r)));let o=je(t);return o?{normal:o,d:Se(o,r)}:null}function Jo(e,n,r){return Se(e.normal,n.normal)>1-r&&Math.abs(e.d-n.d)<=r}function es(e){return!e.texture&&!e.uvs&&!e.textureTriangles}function ns(e,n){let r=n[e[0]]?.data,t=Qn(r);for(let o of e)if(Qn(n[o].data)!==t)return;return r?{...r}:void 0}function ts(e,n){let r=e.map(c=>es(c)?fr(c):null),t=r.map(Boolean),o=new Map;for(let c=0;c<e.length;c++){if(!r[c])continue;let a=e[c].vertices;for(let u=0;u<a.length;u++){let f=ur(a[u],a[(u+1)%a.length]),g=o.get(f),m={polygon:c,edge:u};g?g.push(m):o.set(f,[m])}}let s=e.map(()=>new Set);for(let c of o.values())if(!(c.length<2))for(let a=0;a<c.length;a++)for(let u=a+1;u<c.length;u++){let f=c[a].polygon,g=c[u].polygon,m=r[f],p=r[g];!m||!p||or(e[f])===or(e[g])&&Jo(m,p,n)&&(s[f].add(g),s[g].add(f))}let i=new Set,l=[];for(let c=0;c<e.length;c++){if(!t[c]||i.has(c))continue;let a=[],u=[c];for(i.add(c);u.length>0;){let f=u.shift();a.push(f);for(let g of s[f])i.has(g)||(i.add(g),u.push(g))}l.push(a)}return{groups:l}}function rs(e,n){let r=new Set(e),t=new Map;for(let s of e){let i=n[s].vertices;for(let l=0;l<i.length;l++){let c=ur(i[l],i[(l+1)%i.length]),a=t.get(c),u={polygon:s,edge:l};a?a.push(u):t.set(c,[u])}}let o=[];for(let s of t.values()){let i=s.filter(l=>r.has(l.polygon));if(i.length===1){let l=i[0],c=n[l.polygon].vertices;o.push({a:c[l.edge],b:c[(l.edge+1)%c.length]})}else if(i.length!==2)return null}return o}function os(e){let n=new Map;for(let r of e)n.set(Re(r.a),(n.get(Re(r.a))??0)+1),n.set(Re(r.b),(n.get(Re(r.b))??0)+1);for(let r of n.values())if(r%2!==0)return!1;return!0}function mr(e){let n=[e[0],e[1],e[2]],r=Math.abs(n[0])>=Math.abs(n[1])&&Math.abs(n[0])>=Math.abs(n[2])?0:Math.abs(n[1])>=Math.abs(n[2])?1:2;return n[r]<0?[-n[0],-n[1],-n[2]]:n}function ss(e){let n=mr(e);return`${$e(n[0],1e3)},${$e(n[1],1e3)},${$e(n[2],1e3)}`}function is(e){let n=Math.abs(e[0])<.9?[1,0,0]:[0,1,0],r=Ve(n,Be(e,Se(n,e)));return je(r)??[1,0,0]}function cs(e,n,r){let t=new Map;for(let s of e){let i=Ve(s.b,s.a),l=Ve(i,Be(n,Se(i,n))),c=je(l);if(!c)continue;let a=mr(c),u=ss(a),f=lr(l),g=t.get(u);g?g.weight+=f:t.set(u,{axis:a,weight:f})}let o=[...t.values()].sort((s,i)=>i.weight-s.weight).slice(0,r).map(s=>s.axis);return o.length===0&&o.push(is(n)),o}function sr(e,n,r,t){let o=Ve(e,n);return[le(Se(o,r)),le(Se(o,t))]}function as(e){let n=[...e].sort((t,o)=>t-o),r=[];for(let t of n)(r.length===0||Math.abs(t-r[r.length-1])>1e-7)&&r.push(t);return r}function Fe(e,n){let[r,t]=e.a,[o,s]=e.b,i=(n-r)/(o-r);return le(t+(s-t)*i)}function qn(e){let n=[];for(let t of e)(n.length===0||Math.abs(t[0]-n[n.length-1][0])>1e-7||Math.abs(t[1]-n[n.length-1][1])>1e-7)&&n.push(t);n.length>1&&Math.abs(n[0][0]-n[n.length-1][0])<=1e-7&&Math.abs(n[0][1]-n[n.length-1][1])<=1e-7&&n.pop();let r=[];for(let t=0;t<n.length;t++){let o=n[(t-1+n.length)%n.length],s=n[t],i=n[(t+1)%n.length],l=(s[0]-o[0])*(i[1]-s[1])-(s[1]-o[1])*(i[0]-s[0]);Math.abs(l)>1e-8&&r.push(s)}return r}function on(e){let n=0;for(let r=0;r<e.length;r++){let t=e[r],o=e[(r+1)%e.length];n+=t[0]*o[1]-o[0]*t[1]}return n/2}function ir(e){let n=1/0,r=1/0,t=-1/0,o=-1/0;for(let[s,i]of e)n=Math.min(n,s),r=Math.min(r,i),t=Math.max(t,s),o=Math.max(o,i);return{minX:n,minY:r,maxX:t,maxY:o}}function ls(e,n){let r=ir(e),t=ir(n);return!(r.maxX<t.minX-1e-7||t.maxX<r.minX-1e-7||r.maxY<t.minY-1e-7||t.maxY<r.minY-1e-7)}function rn(e){return Math.abs(on(e))}function us(e){if(e.length<3)return!1;let n=0;for(let r=0;r<e.length;r++){let t=e[r],o=e[(r+1)%e.length],s=e[(r+2)%e.length],i=(o[0]-t[0])*(s[1]-o[1])-(o[1]-t[1])*(s[0]-o[0]);if(Math.abs(i)<=1e-8)continue;let l=i>0?1:-1;if(n===0)n=l;else if(n!==l)return!1}return!0}function fs(e,n,r){let t=r[0]-n[0],o=r[1]-n[1],s=e[0]-n[0],i=e[1]-n[1],l=t*i-o*s,c=Math.hypot(t,o);if(c<=1e-9||Math.abs(l)>Math.max(1e-8,c*1e-8))return!1;let a=s*t+i*o;return a>=-1e-8&&a<=t*t+o*o+1e-8}function ms(e,n,r){let t=r[0]-n[0],o=r[1]-n[1],s=t*t+o*o;return s<=1e-12?0:((e[0]-n[0])*t+(e[1]-n[1])*o)/s}function cr(e,n){let r=[];for(let t=0;t<e.length;t++){let o=e[t],s=e[(t+1)%e.length],i=[{t:0,point:o},{t:1,point:s}];for(let c of n)Wn(c,o)||Wn(c,s)||fs(c,o,s)&&i.push({t:ms(c,o,s),point:c});i.sort((c,a)=>c.t-a.t);let l=[];for(let c of i)l.some(a=>Math.abs(a.t-c.t)<=1e-8||Wn(a.point,c.point))||l.push(c);for(let c=0;c<l.length-1;c++){let a=l[c].point,u=l[c+1].point;Math.hypot(u[0]-a[0],u[1]-a[1])<=1e-8||r.push({a:[le(a[0]),le(a[1])],b:[le(u[0]),le(u[1])]})}}return r}function gs(e,n){if(!ls(e,n))return null;let r=[...cr(e,n),...cr(n,e)],t=new Map;for(let d of r){let y=qo(d),b=t.get(y);b?b.push(d):t.set(y,[d])}let o=!1,s=[];for(let d of t.values()){if(d.length===1){s.push(d[0]);continue}o=!0;let y=d.filter(M=>ge(M.a)<ge(M.b)).length,b=d.length-y;if(y!==b)return null}if(!o||s.length<3)return null;let i=new Map;for(let d of s){let y=ge(d.a);if(i.has(y))return null;i.set(y,d)}let l=s[0],c=ge(l.a),a=[],u=new Set,f=c;for(let d=0;d<=s.length;d++){let y=i.get(f);if(!y)return null;let b=`${ge(y.a)}>${ge(y.b)}`;if(u.has(b))return null;if(u.add(b),a.push(y.a),f=ge(y.b),f===c)break}if(f!==c||u.size!==s.length)return null;let g=qn(a);if(g.length<3||!us(g))return null;let m=rn(g),p=rn(e)+rn(n);return Math.abs(m-p)>Math.max(1e-5,p*1e-5)?null:on(g)>=0?g:[...g].reverse()}function ps(e){let n=e.map(qn).filter(t=>t.length>=3&&rn(t)>1e-8),r=!0;for(;r;){r=!1;for(let t=0;t<n.length;t++){for(let o=t+1;o<n.length;o++){let s=gs(n[t],n[o]);if(s){n[t]=s,n.splice(o,1),r=!0;break}}if(r)break}}return n}function ds(e,n,r){let t=new Map;return o=>{let s=le(o[0]),i=le(o[1]),l=`${s},${i}`,c=t.get(l);if(c)return[c[0],c[1],c[2]];let a=Zn(e,Zn(Be(n,s),Be(r,i))),u=[Kn(a[0]),Kn(a[1]),Kn(a[2])];return t.set(l,u),[u[0],u[1],u[2]]}}function xs(e,n,r,t,o){let s=r[0]?.a;if(!s)return null;let i=je(Ve(o,Be(t,Se(o,t))));if(!i)return null;let l=je(ar(t,i));if(!l)return null;let c=[],a=[];for(let y of r){let b=sr(y.a,s,i,l),M=sr(y.b,s,i,l);Math.hypot(b[0]-M[0],b[1]-M[1])<=1e-7||(c.push({a:b,b:M}),a.push(b[0],M[0]))}let u=as(a);if(c.length<3||u.length<2)return null;let f=n[e[0]].color,g=ns(e,n),m=[];for(let y=0;y<u.length-1;y++){let b=u[y],M=u[y+1];if(M-b<=1e-7)continue;let v=(b+M)/2,P=c.filter(x=>{let A=Math.min(x.a[0],x.b[0]),T=Math.max(x.a[0],x.b[0]);return A<v&&v<T&&Math.abs(x.a[0]-x.b[0])>1e-7}).map(x=>({segment:x,yMid:Fe(x,v)})).sort((x,A)=>x.yMid-A.yMid);if(P.length!==0){if(P.length%2!==0)return null;for(let x=0;x<P.length;x+=2){let A=P[x].segment,T=P[x+1].segment,V=Fe(A,b),w=Fe(A,M),O=Fe(T,b),L=Fe(T,M),U=qn([[b,V],[M,w],[M,L],[b,O]]);if(U.length<3||Math.abs(on(U))<=1e-8)continue;let R=on(U)>0?U:[...U].reverse();m.push(R)}}}if(m.length===0)return null;let p=ds(s,i,l),d=ps(m).map(y=>({vertices:y.map(p),...f?{color:f}:{},...g?{data:g}:{}}));return se(d)}function hs(e,n,r){let t=rs(e,n);if(!t||t.length<3||!os(t))return null;let o=fr(n[e[0]]);if(!o)return null;let s=null;for(let i of cs(t,o.normal,r)){let l=xs(e,n,t,o.normal,i);l&&(!s||l.length<s.length)&&(s=l)}return!s||s.length>=e.length?null:s}function sn(e,n={}){let r=n.minGroupPolygons??Yo,t=n.maxCandidateAxes??Ho,o=n.planeEpsilon??Ko,s=e??[];if(s.length<r)return s;let{groups:i}=ts(s,o);if(i.length===0)return s;let l=new Map,c=new Set;for(let u of i){if(u.length<r)continue;let f=hs(u,s,t);if(f){l.set(u[0],f);for(let g of u)c.add(g)}}if(l.size===0)return s;let a=[];for(let u=0;u<s.length;u++){let f=l.get(u);if(f){a.push(...f);continue}c.has(u)||a.push(s[u])}return a}function ys(e){let n=e.vertices;if(!n||n.length<3)return null;let r=0,t=0,o=0;for(let[R,D,k]of n)r+=R,t+=D,o+=k;let s=1/n.length;r*=s,t*=s,o*=s;let i=n[0],l=n[1],c=n[2],a=l[0]-i[0],u=l[1]-i[1],f=l[2]-i[2],g=c[0]-i[0],m=c[1]-i[1],p=c[2]-i[2],d=u*p-f*m,y=f*g-a*p,b=a*m-u*g,M=Math.hypot(d,y,b);if(M<1e-9)return null;d/=M,y/=M,b/=M;let v=n.length-2,P=new Float64Array(v*9),x=0;for(let R=1;R<n.length-1;R++){let D=n[0],k=n[R],_=n[R+1];P[x++]=D[0],P[x++]=D[1],P[x++]=D[2],P[x++]=k[0],P[x++]=k[1],P[x++]=k[2],P[x++]=_[0],P[x++]=_[1],P[x++]=_[2]}let A=0,T=1/0,V=1/0,w=1/0,O=-1/0,L=-1/0,U=-1/0;for(let[R,D,k]of n){let _=R-r,E=D-t,C=k-o,h=_*_+E*E+C*C;h>A&&(A=h),R<T&&(T=R),R>O&&(O=R),D<V&&(V=D),D>L&&(L=D),k<w&&(w=k),k>U&&(U=k)}return{centroid:[r,t,o],normal:[d,y,b],vertices:n,triFlat:P,bcx:r,bcy:t,bcz:o,br2:A,minX:T,minY:V,minZ:w,maxX:O,maxY:L,maxZ:U}}function bs(e,n,r,t,o,s,i,l){let c=i[l],a=i[l+1],u=i[l+2],f=i[l+3]-c,g=i[l+4]-a,m=i[l+5]-u,p=i[l+6]-c,d=i[l+7]-a,y=i[l+8]-u,b=o*y-s*d,M=s*p-t*y,v=t*d-o*p,P=f*b+g*M+m*v;if(P>-1e-9&&P<1e-9)return!1;let x=1/P,A=e-c,T=n-a,V=r-u,w=x*(A*b+T*M+V*v);if(w<0||w>1)return!1;let O=T*m-V*g,L=V*f-A*m,U=A*g-T*f,R=x*(t*O+o*L+s*U);return R<0||w+R>1?!1:x*(p*O+d*L+y*U)>.001}function Ps(e,n,r,t,o,s,i){let l=i.bcx-e,c=i.bcy-n,a=i.bcz-r,u=l*t+c*o+a*s,f=l-u*t,g=c-u*o,m=a-u*s;if(f*f+g*g+m*m>i.br2)return!1;let p=i.triFlat,d=p.length;for(let y=0;y<d;y+=9)if(bs(e,n,r,t,o,s,p,y))return!0;return!1}var Xe=9,Ms=6,ne=12;function Jn(e,n,r,t,o,s){let i=t-e,l=o-n,c=s-r;return i*l+l*c+c*i}function vs(e){let n=[];for(let A=0;A<e.length;A++)e[A]&&n.push(A);let r=n.length,t=new Int32Array(r);for(let A=0;A<r;A++)t[A]=n[A];let o=new Float64Array(r),s=new Float64Array(r),i=new Float64Array(r);for(let A=0;A<r;A++){let T=e[t[A]];o[A]=(T.minX+T.maxX)*.5,s[A]=(T.minY+T.maxY)*.5,i[A]=(T.minZ+T.maxZ)*.5}let l=2*Math.max(1,r)+1,c=new Float64Array(l*Xe),a=0,u=new Float64Array(ne),f=new Float64Array(ne),g=new Float64Array(ne),m=new Float64Array(ne),p=new Float64Array(ne),d=new Float64Array(ne),y=new Int32Array(ne),b=new Float64Array(ne-1),M=new Int32Array(ne-1),v=new Float64Array(ne-1),P=new Int32Array(ne-1);function x(A,T){let V=a++,w=V*Xe,O=T-A,L=1/0,U=1/0,R=1/0,D=-1/0,k=-1/0,_=-1/0;for(let $=A;$<T;$++){let Y=e[t[$]];Y.minX<L&&(L=Y.minX),Y.maxX>D&&(D=Y.maxX),Y.minY<U&&(U=Y.minY),Y.maxY>k&&(k=Y.maxY),Y.minZ<R&&(R=Y.minZ),Y.maxZ>_&&(_=Y.maxZ)}if(c[w]=L,c[w+1]=U,c[w+2]=R,c[w+3]=D,c[w+4]=k,c[w+5]=_,O<=Ms)return c[w+6]=1,c[w+7]=A,c[w+8]=T,V;let E=1/0,C=1/0,h=1/0,S=-1/0,I=-1/0,G=-1/0;for(let $=A;$<T;$++)o[$]<E&&(E=o[$]),o[$]>S&&(S=o[$]),s[$]<C&&(C=s[$]),s[$]>I&&(I=s[$]),i[$]<h&&(h=i[$]),i[$]>G&&(G=i[$]);let N=S-E,F=I-C,z=G-h;if(N===0&&F===0&&z===0)return c[w+6]=1,c[w+7]=A,c[w+8]=T,V;let X=Jn(L,U,R,D,k,_),H=X>0?1/X:0,K=O+1,Z=0,J=0;for(let $=0;$<3;$++){let Y=$===0?E:$===1?C:h,ie=$===0?N:$===1?F:z;if(ie===0)continue;let de=$===0?o:$===1?s:i,ve=ne/ie;u.fill(1/0),f.fill(1/0),g.fill(1/0),m.fill(-1/0),p.fill(-1/0),d.fill(-1/0),y.fill(0);for(let B=A;B<T;B++){let j=(de[B]-Y)*ve|0;j>=ne&&(j=ne-1);let ce=e[t[B]];ce.minX<u[j]&&(u[j]=ce.minX),ce.maxX>m[j]&&(m[j]=ce.maxX),ce.minY<f[j]&&(f[j]=ce.minY),ce.maxY>p[j]&&(p[j]=ce.maxY),ce.minZ<g[j]&&(g[j]=ce.minZ),ce.maxZ>d[j]&&(d[j]=ce.maxZ),y[j]++}let Ie=1/0,Ee=1/0,In=1/0,En=-1/0,On=-1/0,Rn=-1/0,wt=0;for(let B=0;B<ne-1;B++)u[B]<Ie&&(Ie=u[B]),m[B]>En&&(En=m[B]),f[B]<Ee&&(Ee=f[B]),p[B]>On&&(On=p[B]),g[B]<In&&(In=g[B]),d[B]>Rn&&(Rn=d[B]),wt+=y[B],b[B]=Jn(Ie,Ee,In,En,On,Rn),M[B]=wt;let Ln=1/0,kn=1/0,_n=1/0,Nn=-1/0,Gn=-1/0,Dn=-1/0,It=0;for(let B=ne-2;B>=0;B--){let j=B+1;u[j]<Ln&&(Ln=u[j]),m[j]>Nn&&(Nn=m[j]),f[j]<kn&&(kn=f[j]),p[j]>Gn&&(Gn=p[j]),g[j]<_n&&(_n=g[j]),d[j]>Dn&&(Dn=d[j]),It+=y[j],v[B]=Jn(Ln,kn,_n,Nn,Gn,Dn),P[B]=It}for(let B=0;B<ne-1;B++){if(M[B]===0||P[B]===0)continue;let j=.125+(b[B]*M[B]+v[B]*P[B])*H;j<K&&(K=j,Z=$,J=Y+(B+1)/ve)}}let oe=Z===0?o:Z===1?s:i,Q=A,q=T-1;for(;Q<=q;)if(oe[Q]<J)Q++;else{let $=t[Q];t[Q]=t[q],t[q]=$;let Y=o[Q];o[Q]=o[q],o[q]=Y;let ie=s[Q];s[Q]=s[q],s[q]=ie;let de=i[Q];i[Q]=i[q],i[q]=de,q--}let ae=Q;(ae===A||ae===T)&&(ae=A+T>>1),c[w+6]=0;let re=x(A,ae),W=x(ae,T);return c[V*Xe+7]=re,c[V*Xe+8]=W,V}return r>0&&x(0,r),{data:c,nodeCount:a,polyIndices:t,meta:e}}function gr(e,n,r,t,o,s,i,l,c){if(l.nodeCount===0)return!1;let{data:a,polyIndices:u,meta:f}=l,g=t!==0?1/t:t>=0?1/0:-1/0,m=o!==0?1/o:o>=0?1/0:-1/0,p=s!==0?1/s:s>=0?1/0:-1/0,d=0;for(c[d++]=0;d>0;){let b=c[--d]*Xe,M=(a[b]-e)*g,v=(a[b+3]-e)*g,P=M<v?M:v,x=M<v?v:M,A=(a[b+1]-n)*m,T=(a[b+4]-n)*m,V=A<T?A:T,w=A<T?T:A;if(P>w||V>x)continue;V>P&&(P=V),w<x&&(x=w);let O=(a[b+2]-r)*p,L=(a[b+5]-r)*p,U=O<L?O:L,R=O<L?L:O;if(!(P>R||U>x)&&(R<x&&(x=R),!(x<.001)))if(a[b+6]===1){let D=a[b+7]|0,k=a[b+8]|0;for(let _=D;_<k;_++){let E=u[_];if(E===i)continue;let C=f[E];if(C&&Ps(e,n,r,t,o,s,C))return!0}}else c[d++]=a[b+7]|0,c[d++]=a[b+8]|0}return!1}function As(e){let n=(1+Math.sqrt(5))/2,r=new Float64Array(e*3);for(let t=0;t<e;t++){let o=(t+.5)/e,s=Math.sqrt(Math.max(0,1-o*o)),i=2*Math.PI*(t/n);r[t*3]=s*Math.cos(i),r[t*3+1]=s*Math.sin(i),r[t*3+2]=o}return r}function Cs(e){let n=Math.abs(e[0])>.9?0:1,r=Math.abs(e[0])>.9?1:0,t=r*e[2],o=-n*e[2],s=n*e[1]-r*e[0],i=Math.hypot(t,o,s);t/=i,o/=i,s/=i;let l=e[1]*s-e[2]*o,c=e[2]*t-e[0]*s,a=e[0]*o-e[1]*t;return{ux:t,uy:o,uz:s,vx:l,vy:c,vz:a}}function be(e,n){let r=n?.samples??8;if(e.length<4||r<1)return e;let t=e.map(ys),o=As(r),s=[],i=vs(t),l=new Int32Array(Math.max(64,i.nodeCount)),c=192,a=new Float64Array(c);for(let u=0;u<e.length;u++){let f=t[u];if(!f){s.push(e[u]);continue}let g=f.normal[0],m=f.normal[1],p=f.normal[2],d=.001*g,y=.001*m,b=.001*p;{let k=f.centroid[0]+d,_=f.centroid[1]+y,E=f.centroid[2]+b;if(!gr(k,_,E,g,m,p,u,i,l)){s.push(e[u]);continue}}let{ux:M,uy:v,uz:P,vx:x,vy:A,vz:T}=Cs(f.normal),V=f.centroid[0],w=f.centroid[1],O=f.centroid[2],L=f.vertices,U=L.length,R=0;a[R++]=V+d,a[R++]=w+y,a[R++]=O+b;for(let k=0;k<U;k++){let _=L[k];a[R++]=_[0]+(V-_[0])*.08+d,a[R++]=_[1]+(w-_[1])*.08+y,a[R++]=_[2]+(O-_[2])*.08+b}for(let k=0;k<U;k++){let _=L[k],E=L[(k+1)%U],C=(_[0]+E[0])*.5,h=(_[1]+E[1])*.5,S=(_[2]+E[2])*.5;a[R++]=C+(V-C)*.08+d,a[R++]=h+(w-h)*.08+y,a[R++]=S+(O-S)*.08+b}let D=!1;e:for(let k=0;k<o.length;k+=3){let _=o[k],E=o[k+1],C=o[k+2],h=_*M+E*x+C*g,S=_*v+E*A+C*m,I=_*P+E*T+C*p;for(let G=0;G<R;G+=3){let N=a[G],F=a[G+1],z=a[G+2];if(!gr(N,F,z,h,S,I,u,i,l)){D=!0;break e}}}D&&s.push(e[u])}return s}var Vs=3,Ss=.03,Ts=.02,Ye={maxAngleDeg:Vs,maxPlaneDisplacement:Ss,maxBoundaryDisplacement:Ts,isolatedPairs:!1},Le={maxAngleDeg:15,maxPlaneDisplacement:.35,maxBoundaryDisplacement:.0725,isolatedPairs:!0},an=[{maxAngleDeg:15,maxPlaneDisplacement:.35,maxBoundaryDisplacement:.02},{maxAngleDeg:15,maxPlaneDisplacement:.35,maxBoundaryDisplacement:.0725},{maxAngleDeg:45,maxPlaneDisplacement:1,maxBoundaryDisplacement:.0725}],ws=[2,4,6,8,12],Is=300,Es=.3,Os=300,Rs=16,Ls=.015,pr=2.6,ks=3,_s=96,Te={minGroupPolygons:2,maxCandidateAxes:24},Ns={maxGap:0,internalBoundaryLength:0,excessBoundaryLength:0};function gn(e,n={}){let r=n.meshResolution??"lossy",t=et(e,!1),o={baseline:t},s=t,i=te(t),l=(a,u=te(a))=>u>=i?!1:(s=a,i=u,!0),c=cn(t,n.rectCover);if(c!==t&&l(c),r==="lossy"&&n.approximateMerge!==!1){let a=Ks(e),u=[],f=me(a,s,Le.maxBoundaryDisplacement).metrics,g=n.approximateMerge===void 0||n.approximateMerge===!0,m=(M,v=!0)=>!Ys(a,M.metrics,M.tolerance,v?f:null),p=(M,v)=>{l(M,v)},d=(M,v,P=Le.maxBoundaryDisplacement)=>{!g||v>i+br(i)||u.push({polygons:M,cost:v,maxBoundaryDisplacement:P})},y=dr(n.approximateMerge,g?t:void 0);for(let M=0;M<y.length;M++){let v=y[M],P=et(e,v,o),x=te(P),A=null,T=()=>(A??(A=me(a,P,v.maxBoundaryDisplacement)),A),V=!0;if(g||v.guard){let R=T();V=m(R,!!v.allowReferenceCracks)}if(!V&&x<i)continue;V&&(p(P,x),d(P,x,v.maxBoundaryDisplacement));let w=cn(P,n.rectCover),O=te(w),L=null,U=()=>(L??(L=me(a,w,v.maxBoundaryDisplacement)),L);if(w!==P&&O<i){let R=!0;(g||v.guard)&&(R=m(U(),!!v.allowReferenceCracks)),R&&(p(w,O),d(w,O,v.maxBoundaryDisplacement))}}if(g)for(let M of Gs(e)){let v={baseline:se(be(M))},P=te(v.baseline),x=!0,A=null,T=()=>(A??(A=me(a,v.baseline,Le.maxBoundaryDisplacement)),A);P<i&&(x=m(T())),x&&(p(v.baseline,P),d(v.baseline,P));let V=cn(v.baseline,n.rectCover);if(V!==v.baseline){let w=te(V);(w>=i||m(me(a,V,Le.maxBoundaryDisplacement)))&&(p(V,w),d(V,w))}for(let w of dr(n.approximateMerge,v.baseline)){let O=et(M,w,v),L=te(O),U=null,R=()=>(U??(U=me(a,O,w.maxBoundaryDisplacement)),U),D=!0;if(g||w.guard){let h=R();D=m(h,!!w.allowReferenceCracks)}if(!D&&L<i)continue;D&&(p(O,L),d(O,L,w.maxBoundaryDisplacement));let k=cn(O,n.rectCover),_=te(k),E=null,C=()=>(E??(E=me(a,k,w.maxBoundaryDisplacement)),E);if(k!==O&&_<i){let h=!0;(g||w.guard)&&(h=m(C(),!!w.allowReferenceCracks)),h&&(p(k,_),d(k,_,w.maxBoundaryDisplacement))}}}if(g)for(let M of an){let v=Vr({...M,isolatedPairs:!0}),P=ci(s,v);if(P===s)continue;let x=te(P);if(x>=i)continue;let A=me(a,P,v.maxBoundaryDisplacement);m(A)&&(p(P,x),d(P,x,v.maxBoundaryDisplacement))}let b=Bs(u,s,i,M=>(M.metrics??(M.metrics=me(a,M.polygons,M.maxBoundaryDisplacement).metrics),M.metrics),()=>me(a,s,Le.maxBoundaryDisplacement).metrics);b&&(s=b.polygons,i=b.cost)}return s}function Gs(e){let n=Ds(e);if(n.eligiblePolygons<24||n.colorCount<8)return[];let r=[],t=new Set;for(let o of ws){let s=zs(e,o,n.colorCount);if(!s)continue;let i=Fs(s);t.has(i)||(t.add(i),r.push(s))}return r}function Ds(e){let n=new Set,r=0;for(let t of e)t.texture||t.material?.texture||t.uvs||t.textureTriangles?.length||yr(t.color)&&(r+=1,n.add(t.color??"#cccccc"));return{eligiblePolygons:r,colorCount:n.size}}function zs(e,n,r){let t=!1,o=new Set,s=e.map(i=>{if(i.texture||i.material?.texture||i.uvs||i.textureTriangles?.length)return i;let l=yr(i.color);if(!l)return i;let c=Us([Math.round(l[0]/n)*n,Math.round(l[1]/n)*n,Math.round(l[2]/n)*n]);return o.add(c),c===i.color?i:(t=!0,{...i,color:c})});return!t||o.size>=r?null:s}function yr(e){let n=e??"#cccccc",r=/^#([0-9a-f])([0-9a-f])([0-9a-f])$/i.exec(n);if(r)return[parseInt(r[1]+r[1],16),parseInt(r[2]+r[2],16),parseInt(r[3]+r[3],16)];let t=/^#([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})$/i.exec(n);return t?[parseInt(t[1],16),parseInt(t[2],16),parseInt(t[3],16)]:null}function Us(e){return`#${e.map(n=>Math.max(0,Math.min(255,Math.round(n))).toString(16).padStart(2,"0")).join("")}`}function Fs(e){return e.map(n=>n.color??"").join("|")}function dr(e,n){if(e&&e!==!0)return typeof e.isolatedPairs=="boolean"?[{...e,guard:e.isolatedPairs===!1}]:[{...e,isolatedPairs:!0,guard:!1},{...e,isolatedPairs:!1,guard:!0}];if(n&&$s(n))return[{...an[0],isolatedPairs:!0,guard:!1,allowReferenceCracks:!0}];let r=[],t=new Set,o=n&&n.length>Os?[!0]:[!0,!1];for(let s=0;s<an.length;s++){let i=an[s];for(let l of o){let c={...i,isolatedPairs:l,guard:s>0||l===!1,allowReferenceCracks:!0},a=[c.maxAngleDeg,c.maxPlaneDisplacement,c.maxBoundaryDisplacement,c.isolatedPairs].join("|");t.has(a)||(t.add(a),r.push(c))}}return r.length===0?[{...Le,guard:!1}]:r}function $s(e){return e.length<Is?!1:Ar(e)/e.length<=Es}function te(e){let n=0;for(let r of e){let t=r.vertices.length,o=t<=4?0:Math.min(4,t-4)*.12,s=r.texture||r.material?.texture||r.textureTriangles?.length?.15:0;n+=1+o+s}return n}function br(e){return Math.max(Rs,e*Ls)}function Bs(e,n,r,t,o){if(e.length===0)return null;let s=br(r),i=e.find(u=>u.polygons===n),l=null,c=null,a=null;for(let u of e){if(u.polygons===n||u.cost>r+s)continue;let f=t(u);l??(l=i?t(i):o()),Xs(f,l)&&(!c||!a||js(u,f,c,a)<0)&&(c=u,a=f)}return c}function js(e,n,r,t){return n.maxGap-t.maxGap||n.internalBoundaryLength-t.internalBoundaryLength||n.excessBoundaryLength-t.excessBoundaryLength||e.cost-r.cost}function Xs(e,n){let r=Math.max(5e-4,n.maxGap*.02);if(e.maxGap<n.maxGap-r)return!0;if(e.maxGap>n.maxGap+r)return!1;let t=Math.max(8,n.internalBoundaryLength*.01);if(e.internalBoundaryLength<n.internalBoundaryLength-t)return!0;if(e.internalBoundaryLength>n.internalBoundaryLength+t)return!1;let o=Math.max(8,n.excessBoundaryLength*.01);return e.excessBoundaryLength<n.excessBoundaryLength-o}function Ys(e,n,r,t=null){if(!t)return n.internalBoundaryLength>0||n.excessBoundaryLength>r;let o=Math.max(r*.1,1e-6),s=t.maxGap+o,i=r<=.08?Math.max(s,Math.min(r*.75,.04)):s,l=Math.max(r*2,t.internalBoundaryLength*.15),c=Math.max(r*2,t.excessBoundaryLength*.15);return n.maxGap>i||n.internalBoundaryLength>t.internalBoundaryLength+l||n.excessBoundaryLength>t.excessBoundaryLength+c}function Hs(e,n,r=0,t=tt(e,r)){let o=e.edges,s=Pr(n),i=tt(e,r),l=t>0?Zs(e,t):null,c={...Ns,excessBoundaryLength:Math.max(0,s.boundaryLength-o.boundaryLength)};for(let a of s.boundarySegments){let u=ke(a.a,a.b);if(o.boundaryKeys.has(u))continue;if(o.internalKeys.has(u)){c.internalBoundaryLength+=pe(a.a,a.b);continue}let f=l?Js(a,l,t):null;f!==null&&(c.maxGap=Math.max(c.maxGap,f),c.internalBoundaryLength+=pe(a.a,a.b))}return{metrics:c,tolerance:i}}function me(e,n,r=0){return Hs(e,n,r,Ws(e,r))}function Ks(e){let n=Qs(e),r=n>0?Math.min(.08,Math.max(.001,n*.001)):0;return{edges:Pr(e),baseTolerance:r,polygonCount:e.length,indexes:new Map}}function tt(e,n=0){return Math.max(e.baseTolerance,n*1.05)}function Ws(e,n=0){return Math.max(tt(e,n),e.baseTolerance*pr,n*pr)}function Zs(e,n){let r=n.toFixed(6),t=e.indexes.get(r);if(t)return t;let o=qs(e.edges.internalSegments,n);return e.indexes.set(r,o),o}function Pr(e){let n=new Map;for(let l of e)for(let c=0;c<l.vertices.length;c++){let a=l.vertices[c],u=l.vertices[(c+1)%l.vertices.length],f=ke(a,u),g=n.get(f);g?g.count+=1:n.set(f,{count:1,a,b:u})}let r=new Set,t=new Set,o=[],s=[],i=0;for(let[l,c]of n){let a={a:c.a,b:c.b};c.count===1?(r.add(l),o.push(a),i+=pe(a.a,a.b)):(t.add(l),s.push(a))}return{boundaryKeys:r,internalKeys:t,boundarySegments:o,internalSegments:s,boundaryLength:i}}function Qs(e){let n=1/0,r=1/0,t=1/0,o=-1/0,s=-1/0,i=-1/0;for(let l of e)for(let[c,a,u]of l.vertices)n=Math.min(n,c),r=Math.min(r,a),t=Math.min(t,u),o=Math.max(o,c),s=Math.max(s,a),i=Math.max(i,u);return Number.isFinite(n)?Math.hypot(o-n,s-r,i-t):0}function qs(e,n){let r=Math.max(n*2,1e-6),t=new Map;for(let o of e){let[s,i,l]=Mr(o,r),c=vr(s,i,l),a=t.get(c);a?a.push(o):t.set(c,[o])}return{cellSize:r,cells:t}}function Js(e,n,r){let[t,o,s]=Mr(e,n.cellSize),i=null;for(let l=-1;l<=1;l++)for(let c=-1;c<=1;c++)for(let a=-1;a<=1;a++){let u=n.cells.get(vr(t+l,o+c,s+a));if(u)for(let f of u){let g=ei(e,f);g<=r&&(i=i===null?g:Math.min(i,g))}}return i}function Mr(e,n){return[Math.floor((e.a[0]+e.b[0])/2/n),Math.floor((e.a[1]+e.b[1])/2/n),Math.floor((e.a[2]+e.b[2])/2/n)]}function vr(e,n,r){return`${e},${n},${r}`}function ei(e,n){return Math.min(Math.max(pe(e.a,n.a),pe(e.b,n.b)),Math.max(pe(e.a,n.b),pe(e.b,n.a)))}function cn(e,n){if(n===!1)return e;let r=ni(e,n);if(!r)return e;let t=sn(e,r);return t.length<e.length?t:e}function ni(e,n){if(n&&n!==!0)return n;let r=e.length;return r>2200?null:r>1200?{...Te,maxCandidateAxes:Math.min(Te.maxCandidateAxes??24,2)}:r>300&&Ar(e)<=_s?{...Te,maxCandidateAxes:Math.min(Te.maxCandidateAxes??24,2)}:r>900?{...Te,maxCandidateAxes:Math.min(Te.maxCandidateAxes??24,4)}:Te}function Ar(e){let n=0;for(let r of e)r.vertices.length===3&&(n+=1);return n}function et(e,n,r){let t=tn(e),o=r?.baseline??se(be(t));if(!n)return o;let s=n===!0?Ye:Vr(n);if(s.isolatedPairs){let l=ri(ti(t,r),s),c=se(l);return c.length<o.length?c:o}let i=se(be(hi(t,s,r)));return i.length<o.length?i:o}function Cr(e,n){return n?(n.snapped||(n.snapped=un(e)),n.snapped):un(e)}function ti(e,n){return n?(n.snappedInterior||(n.snappedInterior=be(Cr(e,n))),n.snappedInterior):be(un(e))}function Vr(e){return{maxAngleDeg:e.maxAngleDeg??Ye.maxAngleDeg,maxPlaneDisplacement:e.maxPlaneDisplacement??Ye.maxPlaneDisplacement,maxBoundaryDisplacement:e.maxBoundaryDisplacement??Ye.maxBoundaryDisplacement,isolatedPairs:e.isolatedPairs??Ye.isolatedPairs}}function ri(e,n){let r=e.map(f=>{let g=Ke(f);return g?{polygon:f,normal:g.normal,area:g.area,materialKey:st(f)}:null}),t=new Map;for(let f=0;f<e.length;f++){let g=e[f];if(!(g.vertices.length!==3||!r[f]))for(let m=0;m<g.vertices.length;m++){let p=ke(g.vertices[m],g.vertices[(m+1)%g.vertices.length]),d=t.get(p);d?d.push(f):t.set(p,[f])}}let o=[];for(let f of t.values()){if(f.length!==2)continue;let[g,m]=f,p=xi(g,m,e,r,n);p&&o.push(p)}let s=new Set,i=new Map,l=new Set,c=oi(o),a=pn(c.flatMap(f=>f.vertexMoves));for(let f of c){s.add(f.a),s.add(f.b);let g=Math.min(f.a,f.b);i.set(g,f.polygon),l.add(Math.max(f.a,f.b))}let u=[];for(let f=0;f<e.length;f++){let g=i.get(f);if(g){u.push(g);continue}l.has(f)||u.push(e[f])}return a.size>0?dn(u,a):u}function oi(e){return e.length>3e3?si(e):ii(e)}function si(e){let n=new Map;for(let s of e)n.set(s.a,(n.get(s.a)??0)+1),n.set(s.b,(n.get(s.b)??0)+1);let r=[...e].sort((s,i)=>{let l=(n.get(s.a)??0)+(n.get(s.b)??0),c=(n.get(i.a)??0)+(n.get(i.b)??0);return l-c||s.score-i.score}),t=new Set,o=[];for(let s of r)t.has(s.a)||t.has(s.b)||(t.add(s.a),t.add(s.b),o.push(s));return o}function ii(e){let n=new Map;for(let a=0;a<e.length;a++){let u=e[a],f=n.get(u.a);f?f.push(a):n.set(u.a,[a]);let g=n.get(u.b);g?g.push(a):n.set(u.b,[a])}let r=[],t=new Array(e.length).fill(!0),o=new Map,s=new rt;for(let[a,u]of n)o.set(a,u.length);let i=a=>(o.get(a.a)??0)+(o.get(a.b)??0),l=a=>{let u=e[a];s.push({degree:i(u),score:u.score,index:a})},c=(a,u)=>{if(!t[a])return;t[a]=!1;let f=e[a];for(let g of[f.a,f.b])o.set(g,(o.get(g)??0)-1),u.add(g)};for(let a=0;a<e.length;a++)l(a);for(;s.size()>0;){let a=s.pop();if(!t[a.index])continue;let u=e[a.index];if(i(u)!==a.degree){l(a.index);continue}r.push(u);let g=new Set;for(let m of[u.a,u.b])for(let p of n.get(m)??[])c(p,g);for(let m of g)for(let p of n.get(m)??[])t[p]&&l(p)}return r}var rt=class{constructor(){this.items=[]}size(){return this.items.length}push(n){this.items.push(n);let r=this.items.length-1;for(;r>0;){let t=r-1>>1;if(nt(this.items[t],this.items[r])<=0)break;[this.items[t],this.items[r]]=[this.items[r],this.items[t]],r=t}}pop(){if(this.items.length===0)return null;let n=this.items[0],r=this.items.pop();if(this.items.length>0){this.items[0]=r;let t=0;for(;;){let o=t*2+1,s=o+1,i=t;if(o<this.items.length&&nt(this.items[o],this.items[i])<0&&(i=o),s<this.items.length&&nt(this.items[s],this.items[i])<0&&(i=s),i===t)break;[this.items[t],this.items[i]]=[this.items[i],this.items[t]],t=i}}return n}};function nt(e,n){return e.degree-n.degree||e.score-n.score||e.index-n.index}function ci(e,n){let r=e,t=te(r),o=fi(r);for(let s=0;s<ks;s++){let i=ai(r,n,o);if(!i)break;let l=te(i.polygons);if(l>=t)break;r=i.polygons,t=l,o=i.origins}return r===e?e:r}function ai(e,n,r){let t=e.map(g=>{let m=Ke(g);return m?{polygon:g,normal:m.normal,area:m.area,materialKey:st(g)}:null}),o=new Map;for(let g=0;g<e.length;g++){let m=e[g];if(!(!t[g]||m.vertices.length<3))for(let p=0;p<m.vertices.length;p++){let d=ke(m.vertices[p],m.vertices[(p+1)%m.vertices.length]),y=o.get(d);y?y.push({polygon:g,edge:p}):o.set(d,[{polygon:g,edge:p}])}}let s=[];for(let g of o.values()){if(g.length!==2)continue;let[m,p]=g,d=li(m.polygon,m.edge,p.polygon,p.edge,e,t,n);d&&s.push(d)}if(s.length===0)return null;s.sort((g,m)=>m.score-g.score);let i=new Set,l=[];for(let g of s)i.has(g.a)||i.has(g.b)||(i.add(g.a),i.add(g.b),l.push(g));if(l.length===0)return null;let c=pn(l.flatMap(g=>g.vertexMoves));if(!ui(c,r,n.maxBoundaryDisplacement))return null;let a=dn(e,c),u=mi(e,c,r),f=se(a);return te(f)<te(e)?{polygons:f,origins:gi(f,u)}:null}function li(e,n,r,t,o,s,i){let l=o[e],c=o[r],a=s[e],u=s[r];if(!a||!u||l.vertices.length===3&&c.vertices.length===3||!Rr(l,c,a,u))return null;let f=Math.abs(ue(a.normal,u.normal)),g=Math.cos(i.maxAngleDeg*Math.PI/180);if(f<g)return null;let m=xr(l,c,n)??xr(c,l,t);if(!m||m.length<4||m.length>10)return null;let p=Tr(m);if(!p)return null;let d=0,y=0;for(let V of m){let w=Math.abs(xn(V,p));d=Math.max(d,w),y+=w*w}if(d>Math.min(i.maxPlaneDisplacement,i.maxBoundaryDisplacement))return null;let b=m.map(V=>hn(V,p));if(!wr(b,p.normal))return null;let M=Ke({vertices:b});if(!M||ue(M.normal,a.normal)<.2||ue(M.normal,u.normal)<.2)return null;let v=[...m.map((V,w)=>({key:ee(V),target:b[w]})),...Sr([l,c],p)],P=dn([l,c],pn(v)),x=te([l,c]),A=te(se(P));if(A>=x)return null;let T=x-A-(y/m.length+d*.25+(1-f)*.1);return T<=0?null:{a:e,b:r,vertexMoves:v,score:T}}function xr(e,n,r){let t=e.vertices,o=n.vertices,s=t[r],i=t[(r+1)%t.length],l=-1;for(let u=0;u<o.length;u++)if(Pe(o[u],i)&&Pe(o[(u+1)%o.length],s)){l=u;break}if(l<0)return null;let c=[],a=(r+1)%t.length;for(c.push(t[a]);a!==r;)a=(a+1)%t.length,c.push(t[a]);for(a=(l+2)%o.length;a!==l;){let u=o[a];Pe(u,c[c.length-1])||c.push(u),a=(a+1)%o.length}return c.length>1&&Pe(c[0],c[c.length-1])&&c.pop(),c}function ui(e,n,r){for(let[t,o]of e){let s=pi(t),i=n.get(t)??(s?[s]:[]);if(i.length===0)return!1;for(let l of i)if(pe(l,o)>r+1e-6)return!1}return!0}function fi(e){let n=new Map;for(let r of e){for(let t of r.vertices)ln(n,ee(t),t);for(let t of r.textureTriangles??[])for(let o of t.vertices)ln(n,ee(o),o)}return n}function mi(e,n,r){let t=new Map;for(let o of e){let s=[...o.vertices,...(o.textureTriangles??[]).flatMap(i=>i.vertices)];for(let i of s){let l=ee(i),c=n.get(l)??i,a=ee(c);for(let u of r.get(l)??[i])ln(t,a,u)}}return t}function gi(e,n){let r=new Map;for(let t of e){let o=[...t.vertices,...(t.textureTriangles??[]).flatMap(s=>s.vertices)];for(let s of o){let i=ee(s);for(let l of n.get(i)??[s])ln(r,i,l)}}return r}function ln(e,n,r){let t=e.get(n);if(!t){e.set(n,[r]);return}let o=ee(r);t.some(s=>ee(s)===o)||t.push(r)}function pi(e){let n=e.split(",").map(Number);return n.length===3&&n.every(Number.isFinite)?[n[0],n[1],n[2]]:null}function pn(e){let n=new Map;for(let t of e){let o=n.get(t.key);o?(o.x+=t.target[0],o.y+=t.target[1],o.z+=t.target[2],o.count+=1):n.set(t.key,{x:t.target[0],y:t.target[1],z:t.target[2],count:1})}let r=new Map;for(let[t,o]of n)r.set(t,[o.x/o.count,o.y/o.count,o.z/o.count]);return r}function di(e,n){let r=[];for(let t=0;t<e.length;t++){let o=e[t].vertices,s=n[t]?.vertices;if(!s||s.length!==o.length)continue;for(let c=0;c<o.length;c++)r.push({key:ee(o[c]),target:s[c]});let i=e[t].textureTriangles??[],l=n[t]?.textureTriangles??[];for(let c=0;c<i.length;c++){let a=l[c];if(a)for(let u=0;u<i[c].vertices.length;u++)r.push({key:ee(i[c].vertices[u]),target:a.vertices[u]})}}return r}function Sr(e,n){let r=[];for(let t of e)for(let o of t.textureTriangles??[])for(let s of o.vertices)r.push({key:ee(s),target:hn(s,n)});return r}function dn(e,n){return e.map(r=>{let t=!1,o=l=>{let c=n.get(ee(l));return c?(t=!0,c):l},s=r.vertices.map(o),i=ot(r.textureTriangles,o);return t?{...r,vertices:s,...i?{textureTriangles:i}:{}}:r})}function xi(e,n,r,t,o){let s=r[e],i=r[n],l=t[e],c=t[n];if(!l||!c||s.vertices.length!==3||i.vertices.length!==3||!Rr(s,i,l,c))return null;let a=ct(s,i);if(!a)return null;let[u,f,g,m]=a;if((g+1)%i.vertices.length===m)return null;let d=Math.abs(ue(l.normal,c.normal)),y=Math.cos(o.maxAngleDeg*Math.PI/180);if(d<y)return null;let b=(f+1)%s.vertices.length,M=3-g-m,v=[s.vertices[f],s.vertices[b],s.vertices[u],i.vertices[M]],P=Tr(v);if(!P)return null;let x=0,A=0;for(let O of v){let L=Math.abs(xn(O,P));x=Math.max(x,L),A+=L*L}if(x>Math.min(o.maxPlaneDisplacement,o.maxBoundaryDisplacement))return null;let T=v.map(O=>hn(O,P));if(!wr(T,P.normal))return null;let V=Ke({vertices:T});if(!V||ue(V.normal,l.normal)<.2||ue(V.normal,c.normal)<.2)return null;let w={vertices:v,color:s.color,...s.data?{data:{...s.data}}:{}};if(it(s,i)&&s.uvs&&i.uvs&&s.texture){w.texture=s.texture,w.uvs=[[...s.uvs[f]],[...s.uvs[b]],[...s.uvs[u]],[...i.uvs[M]]];let O=yi([s,i]);O?.length&&(w.textureTriangles=O)}return{a:e,b:n,polygon:w,vertexMoves:[...v.map((O,L)=>({key:ee(O),target:T[L]})),...Sr([s,i],P)],score:A/v.length+x*.25+(1-d)*.1}}function Tr(e){if(e.length<3)return null;let n=0,r=0,t=0,o=0,s=0,i=0;for(let c=0;c<e.length;c++){let a=e[c],u=e[(c+1)%e.length];n+=(a[1]-u[1])*(a[2]+u[2]),r+=(a[2]-u[2])*(a[0]+u[0]),t+=(a[0]-u[0])*(a[1]+u[1]),o+=a[0],s+=a[1],i+=a[2]}let l=Gr([n,r,t]);return l?{normal:l,point:[o/e.length,s/e.length,i/e.length]}:null}function wr(e,n){let r=0;for(let t=0;t<e.length;t++){let o=e[t],s=e[(t+1)%e.length],i=e[(t+2)%e.length],l=ue(Nr(He(s,o),He(i,s)),n);if(Math.abs(l)<=1e-9)continue;let c=l>0?1:-1;if(r===0)r=c;else if(r!==c)return!1}return!0}function hi(e,n,r){let t=Cr(e,r),o=Ci(t,n);if(o<=0)return t;let s=t.map(m=>{let p=Ke(m);return p?{polygon:m,normal:p.normal,area:p.area,materialKey:st(m)}:null}),i=Ti(t,s),l=new Set,c=Array(t.length),a=[],u=(m,p)=>{c[m]=p};for(let m=0;m<t.length;m++){let p=s[m];if(l.has(m))continue;if(!p){u(m,t[m]);continue}let d=Ii(m,s,i,l,o,n);for(let b of d)l.add(b);if(d.length<2){u(m,t[m]);continue}let y=Mi(d,t,s,i,o,n);a.push(...y.vertexMoves);for(let b of d)u(b,y.polygons.get(b)??t[b])}let f=c.flatMap(m=>m?[m]:[]),g=a.length>0?dn(f,pn(a)):f;return un(g)}function un(e){let n=Or(e),r=1e-4;if(n<=0)return e;let t=Vi(n),o=Si(r);return e.map(s=>{let i=f=>t.snap(f),l=s.vertices.map(i),c=s.uvs&&s.uvs.length===s.vertices.length?s.uvs.map(f=>o.snap(f)):void 0,a=ot(s.textureTriangles,i),u={...s,vertices:l,...c?{uvs:c}:{},...a?{textureTriangles:a}:{}};return{...u,...u.texture?{textureTriangles:Ir(u)}:{}}})}function Ir(e){if(e.texture){if(e.textureTriangles?.length)return Pi(e.textureTriangles);if(e.uvs&&e.uvs.length===e.vertices.length)return bi(e.vertices,e.uvs)}}function yi(e){let n=e.flatMap(r=>Ir(r)??[]);return n.length>0?n:void 0}function bi(e,n){let r=[];for(let t=1;t<e.length-1;t++)r.push({vertices:[[...e[0]],[...e[t]],[...e[t+1]]],uvs:[[...n[0]],[...n[t]],[...n[t+1]]]});return r}function Pi(e){return e.map(n=>({vertices:n.vertices.map(r=>[...r]),uvs:n.uvs.map(r=>[...r])}))}function ot(e,n){if(e?.length)return e.map(r=>({vertices:r.vertices.map(n),uvs:r.uvs.map(t=>[...t])}))}function Mi(e,n,r,t,o,s){let i=Er(e,n,r,o,s);return i?Ai(i):vi(e,n,r,t,o,s)}function vi(e,n,r,t,o,s){let i=new Set(e),l=[];for(let f of e)for(let g of t.get(f)??[]){if(f>=g||!i.has(g))continue;let m=Er([f,g],n,r,o,s);m&&l.push(m)}l.sort((f,g)=>g.score-f.score);let c=new Set,a=new Map,u=[];for(let f of l)if(!f.indices.some(g=>c.has(g))){u.push(...f.vertexMoves);for(let g=0;g<f.indices.length;g++){let m=f.indices[g];c.add(m),a.set(m,n[m])}}return{polygons:a,vertexMoves:u}}function Ai(e){let n=new Map;for(let r=0;r<e.indices.length;r++)n.set(e.indices[r],e.source[r]);return{polygons:n,vertexMoves:e.vertexMoves}}function Er(e,n,r,t,o){let s=Lr(e,r);if(!s||!kr(e,r,s,t,o))return null;let i=e.map(u=>n[u]),l=i.map(u=>Ri(u,s)),c=te(se(i)),a=te(se(l));return a>=c?null:{indices:e,source:i,projected:l,vertexMoves:di(i,l),score:c-a}}function Ci(e,n){return Or(e)<=0?0:n.maxPlaneDisplacement}function Or(e){let n=1/0,r=1/0,t=1/0,o=-1/0,s=-1/0,i=-1/0;for(let c of e)for(let[a,u,f]of c.vertices)n=Math.min(n,a),r=Math.min(r,u),t=Math.min(t,f),o=Math.max(o,a),s=Math.max(s,u),i=Math.max(i,f);if(!Number.isFinite(n))return 0;let l=Math.hypot(o-n,s-r,i-t);return l<=0?0:Math.min(.025,Math.max(1e-4,l*25e-5))}function Vi(e){let n=new Map,r=o=>Math.floor(o/e),t=(o,s,i)=>`${o},${s},${i}`;return{snap(o){let s=r(o[0]),i=r(o[1]),l=r(o[2]);for(let f=-1;f<=1;f++)for(let g=-1;g<=1;g++)for(let m=-1;m<=1;m++){let p=n.get(t(s+f,i+g,l+m));if(p){for(let d of p)if(pe(o,d)<=e)return[d[0],d[1],d[2]]}}let c=[o[0],o[1],o[2]],a=t(s,i,l),u=n.get(a);return u?u.push(c):n.set(a,[c]),c}}}function Si(e){let n=new Map,r=o=>Math.floor(o/e),t=(o,s)=>`${o},${s}`;return{snap(o){let s=r(o[0]),i=r(o[1]);for(let u=-1;u<=1;u++)for(let f=-1;f<=1;f++){let g=n.get(t(s+u,i+f));if(g){for(let m of g)if(Math.hypot(o[0]-m[0],o[1]-m[1])<=e)return[m[0],m[1]]}}let l=[o[0],o[1]],c=t(s,i),a=n.get(c);return a?a.push(l):n.set(c,[l]),l}}}function st(e){return`${e.color??"#cccccc"}|${e.texture??""}|${e.uvs?"uv":"plain"}`}function Ke(e){let n=e.vertices;if(!n||n.length<3)return null;let r=0,t=0,o=0,s=n[0];for(let l=1;l<n.length-1;l++){let c=He(n[l],s),a=He(n[l+1],s),u=Nr(c,a);r+=u[0],t+=u[1],o+=u[2]}let i=Math.hypot(r,t,o);return i<=1e-10?null:{normal:[r/i,t/i,o/i],area:i/2}}function Ti(e,n){let r=new Map,t=new Map;for(let o=0;o<e.length;o++){let s=e[o];if(!(!n[o]||s.vertices.length<3))for(let i=0;i<s.vertices.length;i++){let l=ke(s.vertices[i],s.vertices[(i+1)%s.vertices.length]),c=r.get(l);c?c.push(o):r.set(l,[o])}}for(let o of r.values())for(let s=0;s<o.length;s++)for(let i=s+1;i<o.length;i++){let l=o[s],c=o[i];wi(e[l],e[c],n[l],n[c])&&(hr(t,l,c),hr(t,c,l))}return t}function wi(e,n,r,t){if(!r||!t||r.materialKey!==t.materialKey||!!e.uvs!=!!n.uvs)return!1;if(fn(e)||fn(n))return it(e,n);if(!e.uvs||!n.uvs)return!0;let o=ct(e,n);if(!o)return!1;let[s,i,l,c]=o;return mn(e.uvs[s],n.uvs[l])&&mn(e.uvs[i],n.uvs[c])}function Rr(e,n,r,t){return r.materialKey!==t.materialKey?!1:fn(e)||fn(n)?it(e,n):!e.uvs&&!n.uvs&&!e.textureTriangles?.length&&!n.textureTriangles?.length}function fn(e){return!!(e.texture||e.material?.texture||e.textureTriangles?.length)}function it(e,n){if(!e.texture||!n.texture||e.texture!==n.texture||e.material?.texture||n.material?.texture||!e.uvs||!n.uvs||e.uvs.length!==e.vertices.length||n.uvs.length!==n.vertices.length)return!1;let r=ct(e,n);if(!r)return!1;let[t,o,s,i]=r;return mn(e.uvs[t],n.uvs[s])&&mn(e.uvs[o],n.uvs[i])}function hr(e,n,r){let t=e.get(n);t?t.add(r):e.set(n,new Set([r]))}function Ii(e,n,r,t,o,s){let i=[e],l=new Set([e]),c=[e];for(;c.length>0;){let a=c.shift();for(let u of r.get(a)??[]){if(t.has(u)||l.has(u))continue;let f=n[u],g=n[e];!f||!g||f.materialKey===g.materialKey&&Ei([...i,u],n,o,s)&&(i.push(u),l.add(u),c.push(u))}}return i}function Ei(e,n,r,t){let o=Lr(e,n);return!!o&&kr(e,n,o,r,t)}function Lr(e,n){let r=n[e[0]];if(!r)return null;let t=0,o=0,s=0,i=0,l=0,c=0,a=0;for(let m of e){let p=n[m];if(!p)return null;let d=ue(r.normal,p.normal)<0?-1:1,y=Math.max(p.area,1e-6);t+=p.normal[0]*d*y,o+=p.normal[1]*d*y,s+=p.normal[2]*d*y;for(let b of p.polygon.vertices)i+=b[0],l+=b[1],c+=b[2],a+=1}let u=Gr([t,o,s]);if(!u||a===0)return null;let f=_r(e,n),g=Oi(e,n,u,f);if(g){let m=(g.min+g.max)/2;return{normal:u,point:[u[0]*m,u[1]*m,u[2]*m]}}return{normal:u,point:[i/a,l/a,c/a]}}function Oi(e,n,r,t){let o=1/0,s=-1/0;for(let i of e){let l=n[i];if(l)for(let c of l.polygon.vertices){if(!t.has(ee(c)))continue;let a=ue(c,r);o=Math.min(o,a),s=Math.max(s,a)}}return Number.isFinite(o)&&Number.isFinite(s)?{min:o,max:s}:null}function kr(e,n,r,t,o){let s=Math.cos(o.maxAngleDeg*Math.PI/180),i=_r(e,n);for(let l of e){let c=n[l];if(!c||Math.abs(ue(c.normal,r.normal))<s)return!1;for(let a of c.polygon.vertices){let u=i.has(ee(a))?o.maxBoundaryDisplacement:t;if(Math.abs(xn(a,r))>u)return!1}}return!0}function _r(e,n){let r=new Map;for(let o of e){let s=n[o];if(!s)continue;let i=s.polygon.vertices;for(let l=0;l<i.length;l++){let c=i[l],a=i[(l+1)%i.length],u=ke(c,a),f=r.get(u);f?f.count+=1:r.set(u,{count:1,a:c,b:a})}}let t=new Set;for(let o of r.values())o.count===1&&(t.add(ee(o.a)),t.add(ee(o.b)));return t}function Ri(e,n){let r=o=>hn(o,n),t=ot(e.textureTriangles,r);return{...e,vertices:e.vertices.map(r),...t?{textureTriangles:t}:{}}}function ct(e,n){for(let r=0;r<e.vertices.length;r++){let t=(r+1)%e.vertices.length;for(let o=0;o<n.vertices.length;o++){let s=(o+1)%n.vertices.length;if(Pe(e.vertices[r],n.vertices[o])&&Pe(e.vertices[t],n.vertices[s]))return[r,t,o,s];if(Pe(e.vertices[r],n.vertices[s])&&Pe(e.vertices[t],n.vertices[o]))return[r,t,s,o]}}return null}function ke(e,n){let r=ee(e),t=ee(n);return r<t?`${r}|${t}`:`${t}|${r}`}function ee(e){return`${e[0]},${e[1]},${e[2]}`}function Pe(e,n){return e[0]===n[0]&&e[1]===n[1]&&e[2]===n[2]}function mn(e,n){return Math.abs(e[0]-n[0])<=1e-4&&Math.abs(e[1]-n[1])<=1e-4}function He(e,n){return[e[0]-n[0],e[1]-n[1],e[2]-n[2]]}function Nr(e,n){return[e[1]*n[2]-e[2]*n[1],e[2]*n[0]-e[0]*n[2],e[0]*n[1]-e[1]*n[0]]}function ue(e,n){return e[0]*n[0]+e[1]*n[1]+e[2]*n[2]}function pe(e,n){return Math.hypot(e[0]-n[0],e[1]-n[1],e[2]-n[2])}function Gr(e){let n=Math.hypot(e[0],e[1],e[2]);return n<=1e-10?null:[e[0]/n,e[1]/n,e[2]/n]}function xn(e,n){return ue(He(e,n.point),n.normal)}function hn(e,n){let r=xn(e,n);return[e[0]-n.normal[0]*r,e[1]-n.normal[1]*r,e[2]-n.normal[2]*r]}var We=1e-5,at=.001,lt=6;function yn(e){let n=e.vertices;if(n.length<3)return null;let r=n[0],t=0,o=0,s=0;for(let l=1;l+1<n.length;l++){let c=n[l],a=n[l+1],u=c[1]-r[1],f=c[0]-r[0],g=c[2]-r[2],m=a[1]-r[1],p=a[0]-r[0],d=a[2]-r[2];t-=f*d-g*p,o-=g*m-u*d,s-=u*p-f*m}let i=Math.hypot(t,o,s);return i<1e-9?null:[t/i,o/i,s/i]}function ut(e,n){let r=n.meshRotation,t=r?De(e,r[0]??0,r[1]??0,r[2]??0):e;return De(t,n.rotX,0,n.rotY)[2]}function bn(e,n,r=We){return ut(e,n)>r}function Dr(e,n,r=We){let t=yn(e);return t===null||bn(t,n,r)}function ft(e){return`${e[0].toFixed(4)},${e[1].toFixed(4)},${e[2].toFixed(4)}`}function mt(e){let n=new Map;for(let r of e){if(!r)continue;let t=ft(r);n.has(t)||n.set(t,r)}return Array.from(n,([r,t])=>({key:r,normal:t}))}function zr(e){return mt(e.map(yn))}function gt(e,n=at){let r=Math.abs(e[0]),t=Math.abs(e[1]),o=Math.abs(e[2]),s=Math.max(r,t,o);return s>1-n&&r+t+o-s<n}function Ur(e){return e.length<=lt&&e.every(({normal:n})=>gt(n))}function Fr(e,n,r=We){let t=[];for(let{key:o,normal:s}of e)bn(s,n,r)&&t.push(o);return t.sort(),t.join("|")}function pt(e,n,r,t,o){let s=(p,d,y)=>{let b=[0,0,0];return b[e]=p,b[(e+1)%3]=d,b[(e+2)%3]=y,b},i=s(n,-t,-t),l=s(n,t,-t),c=s(n,t,t),a=s(n,-t,t),u=s(r,-t,-t),f=s(r,t,-t),g=s(r,t,t),m=s(r,-t,t);return[{vertices:[i,l,c,a],color:o},{vertices:[u,f,g,m],color:o},{vertices:[i,l,f,u],color:o},{vertices:[l,c,g,f],color:o},{vertices:[c,a,m,g],color:o},{vertices:[a,i,u,m],color:o}]}function dt(e={}){let n=e.size??5,r=e.thickness??.025,t=e.negative??!1,o=e.xColor??"#ff3a3a",s=e.yColor??"#3aff3a",i=e.zColor??"#3a8aff",l=n*r/2,c=t?-n:0;return[...pt(0,c,n,l,o),...pt(1,c,n,l,s),...pt(2,c,n,l,i)]}function $r(e,n,r,t){let o=[0,0,0];return o[e]=n,o[(e+1)%3]=r,o[(e+2)%3]=t,o}function Li(e,n,r,t,o){let s=(p,d,y)=>$r(e,p,d,y),i=s(n,-t,-t),l=s(n,t,-t),c=s(n,t,t),a=s(n,-t,t),u=s(r,-t,-t),f=s(r,t,-t),g=s(r,t,t),m=s(r,-t,t);return[{vertices:[i,l,c,a],color:o},{vertices:[u,f,g,m],color:o},{vertices:[i,l,f,u],color:o},{vertices:[l,c,g,f],color:o},{vertices:[c,a,m,g],color:o},{vertices:[a,i,u,m],color:o}]}function ki(e,n,r,t,o){let s=(f,g,m)=>$r(e,f,g,m),i=s(n,-t,-t),l=s(n,t,-t),c=s(n,t,t),a=s(n,-t,t),u=s(r,0,0);return[{vertices:[i,l,c,a],color:o},{vertices:[i,l,u],color:o},{vertices:[l,c,u],color:o},{vertices:[c,a,u],color:o},{vertices:[a,i,u],color:o}]}function _i(e){return e.map(n=>({...n,vertices:[...n.vertices].reverse()}))}function xt(e){let n=e.axis,r=e.sign??1,t=e.shaftLength??4,o=e.shaftHalfThickness??.05,s=e.headLength??.8,i=e.headHalfThickness??.2,l=e.color??"#ffffff",c=e.shaft??!0,a=t*r,u=Math.min(0,a),f=Math.max(0,a),g=a,m=(t+s)*r,p=ki(n,g,m,i,l),d=r===-1?_i(p):p;return c?[...Li(n,u,f,o,l),...d]:d}function Pn(e,n,r){let t=[0,0,0],o=(e+1)%3,s=(e+2)%3;return t[o]=Math.cos(r)*n,t[s]=Math.sin(r)*n,t}function ht(e){let n=e.axis,r=e.radius,t=e.halfThickness??Math.max(.05,r*.04),o=e.segments??32,s=e.color??"#ffffff",i=r-t,l=r+t,c=[];for(let a=0;a<o;a++){let u=a/o*Math.PI*2,f=(a+1)/o*Math.PI*2,g=Pn(n,i,u),m=Pn(n,i,f),p=Pn(n,l,u),d=Pn(n,l,f);c.push({vertices:[g,p,d,m],color:s})}return c}function yt(e){let n=e.axis,r=e.outerRadius,t=e.color??"#ffffff",o=(n+1)%3,s=(n+2)%3,i=(l,c)=>{let a=[0,0,0];return a[n]=0,a[o]=l,a[s]=c,a};return[{vertices:[i(-r,-r),i(r,-r),i(r,r),i(-r,r)],color:t}]}function bt(e){let n=e.axis,r=e.size??.4,t=e.offset??r*2,o=typeof t=="number"?t:t[0],s=typeof t=="number"?t:t[1],i=e.along??0,l=e.color??"#ffffff",c=(n+1)%3,a=(n+2)%3,u=(f,g)=>{let m=[0,0,0];return m[n]=i,m[c]=o+f,m[a]=s+g,m};return[{vertices:[u(-r,-r),u(r,-r),u(r,r),u(-r,r)],color:l}]}function Pt(e){let{center:n,size:r,color:t="#ffffff"}=e,[o,s,i]=n,l=[[o+r,s,i],[o-r,s,i],[o,s+r,i],[o,s-r,i],[o,s,i+r],[o,s,i-r]];return[[0,2,4],[2,1,4],[1,3,4],[3,0,4],[2,0,5],[1,2,5],[3,1,5],[0,3,5]].map(a=>({vertices:[l[a[0]],l[a[1]],l[a[2]]],color:t}))}var Br=2200,jr=2201,Xr=2202;function Ni(e,n){let r=0,t=1,o=1,s=2201,i=1/0,l=0,c=!1,a=null,u=!0,f=!1,g={clampWhenFinished:!1,get timeScale(){return o},set timeScale(m){o=m},get weight(){return t},set weight(m){t=m},get time(){return r},set time(m){r=m},get isRunning(){return c},get enabled(){return u},set enabled(m){u=m},get paused(){return f},set paused(m){f=m},play(){return c=!0,g},stop(){return c=!1,r=0,l=0,a=null,g},reset(){return r=0,l=0,g},fadeIn(m){return a={from:0,to:1,elapsed:0,duration:m},t=0,g},fadeOut(m){return a={from:t,to:0,elapsed:0,duration:m},g},crossFadeTo(m,p){return g.fadeOut(p),m.fadeIn(p),g},crossFadeFrom(m,p){return m.fadeOut(p),g.fadeIn(p),g},setLoop(m,p){return s=m,i=p,g},setEffectiveTimeScale(m){return o=m,g},setEffectiveWeight(m){return t=m,g}};return g._internal={get clip(){return e},get time(){return r},get weight(){return t},get enabled(){return u},get paused(){return f},get running(){return c},get loopMode(){return s},get repetitions(){return i},get completedReps(){return l},set completedReps(m){l=m},get clampWhenFinished(){return g.clampWhenFinished},get timeScale(){return o},get fade(){return a},advance(m){if(!c)return;if(a){a.elapsed+=m;let d=a.duration>0?Math.min(a.elapsed/a.duration,1):1;if(t=a.from+(a.to-a.from)*d,d>=1&&(t=a.to,a=null,t<=0)){c=!1;return}}if(f)return;let p=e.duration;if(!(p<=0)){if(r+=m*o,s===2200)r>=p&&(r=g.clampWhenFinished?p:0,c=!1);else if(s===2201){if(r>=p){let d=r%p,y=Math.floor(r/p);l+=y,r=d,i!==1/0&&l>=i&&(r=g.clampWhenFinished?p:0,c=!1)}}else if(p>0){let d=p*2,y=r%d;if(r>=d){let b=Math.floor(r/d);l+=b,i!==1/0&&l>=i?(r=g.clampWhenFinished?p:0,c=!1):r=y}}}},sampleTime(){if(s===2202){let m=e.duration,p=m*2,d=r%p;return d<=m?d:p-d}return r}},g}function Gi(e){return e._internal}function Mt(e,n){return typeof n=="number"?e[n]:e.find(r=>r.name===n)}function Yr(e,n){let r=new Map;function t(a){let u=Mt(n.clips,a);if(!u)throw new Error(`GlyphcssAnimationMixer: no clip found for key "${a}". Available: ${n.clips.map(g=>g.name).join(", ")}`);let f=r.get(u.index);return f||(f=Ni(u,n),r.set(u.index,f)),f}function o(a){let u=Mt(n.clips,a);return u?r.get(u.index)??null:null}function s(a){let u=[];for(let d of r.values()){let y=Gi(d);y.advance(a),y.running&&y.enabled&&u.push({internal:y,clip:y.clip})}if(u.length===0)return;if(u.length===1){let{internal:d,clip:y}=u[0],b=n.sample(y.name,d.sampleTime());e.setPolygons(b);return}let f=0;for(let{internal:d}of u)f+=d.weight;if(f<=0)return;let g=u.map(({internal:d,clip:y})=>({polygons:n.sample(y.name,d.sampleTime()),weight:d.weight/f})),m=g[0].polygons;if(m.length===0)return;let p=m.map((d,y)=>{let b=d.vertices.map((M,v)=>{let P=0,x=0,A=0;for(let{polygons:T,weight:V}of g){let w=T[y];if(!w)continue;let O=w.vertices[v];O&&(P+=O[0]*V,x+=O[1]*V,A+=O[2]*V)}return[P,x,A]});return{...d,vertices:b}});e.setPolygons(p)}function i(){for(let a of r.values())a.stop()}function l(a){let u=Mt(n.clips,a);u&&r.delete(u.index)}function c(){r.clear()}return{clipAction:t,existingAction:o,update:s,stopAllAction:i,uncacheClip:l,uncacheRoot:c}}var Di=/^[0-9A-Fa-f]{6}$/,zi=["#3b82f6","#ef4444","#22c55e","#eab308","#a855f7","#06b6d4","#f97316","#ec4899"];function Mn(e,n){let r=n?.targetSize??60,t=n?.gridShift??1,o=n?.defaultColor??"#888888",s=n?.palette??zi,i=n?.materialColors??{},l=n?.materialTextures??{},c=[],a=[],u=[],f=[],g=new Map,m=o,p,d=n?.includeObjects?new Set(n.includeObjects):null,y=n?.excludeObjects?new Set(n.excludeObjects):null,b=null,M=()=>b===null?d===null:!(d&&!d.has(b)||y&&y.has(b)),v=E=>E in i?i[E]:Di.test(E)?`#${E}`:(g.has(E)||(g.set(E,s[f.length%s.length]),f.push(E)),g.get(E)),P=e.split(`
|
|
2
|
+
`);for(let E of P)if(!(E.length===0||E.charCodeAt(0)===35)){if(E.startsWith("v ")){let C=E.trim().split(/\s+/);c.push([parseFloat(C[1]),parseFloat(C[2]),parseFloat(C[3])])}else if(E.startsWith("vt ")){let C=E.trim().split(/\s+/);a.push([parseFloat(C[1]),parseFloat(C[2])])}else if(E.startsWith("o "))b=E.trim().slice(2).trim();else if(E.startsWith("usemtl ")){let C=E.trim().split(/\s+/)[1];m=v(C),p=l[C]}else if(E.startsWith("f ")){if(!M())continue;let C=E.trim().split(/\s+/).slice(1),h=[],S=[];for(let I of C){let G=I.split("/");h.push(parseInt(G[0],10)-1);let N=G[1];if(N&&N.length>0){let F=parseInt(N,10)-1;S.push(Number.isFinite(F)?F:null)}else S.push(null)}u.push({idx:h,uvIdx:S,color:m,texture:p})}}if(c.length===0||u.length===0)return Ui(f,e.length);let x=new Set;for(let E of u)for(let C of E.idx)x.add(C);let A=1/0,T=1/0,V=1/0,w=-1/0,O=-1/0,L=-1/0;for(let E of x){let C=c[E];C&&(C[0]<A&&(A=C[0]),C[0]>w&&(w=C[0]),C[1]<T&&(T=C[1]),C[1]>O&&(O=C[1]),C[2]<V&&(V=C[2]),C[2]>L&&(L=C[2]))}let U=Math.max(w-A,O-T,L-V),R=U>0?r/U:1,D=E=>Math.round(E*1e3)/1e3,k=c.map(([E,C,h])=>[D((h-V)*R+t),D((E-A)*R+t),D((C-T)*R+t)]),_=[];for(let{idx:E,uvIdx:C,color:h,texture:S}of u)for(let I=1;I<E.length-1;I++){let G=E[0],N=E[I],F=E[I+1],z=k[G],X=k[N],H=k[F];if(!z||!X||!H||z[0]===X[0]&&z[1]===X[1]&&z[2]===X[2]||z[0]===H[0]&&z[1]===H[1]&&z[2]===H[2]||X[0]===H[0]&&X[1]===H[1]&&X[2]===H[2])continue;let K;if(S){let J=C[0],oe=C[I],Q=C[I+1];if(J!=null&&oe!=null&&Q!=null){let q=a[J],ae=a[oe],re=a[Q];q&&ae&&re&&(K=[q,ae,re])}}let Z={vertices:[z,X,H],color:h};S&&(Z.texture=S),K&&(Z.uvs=K),_.push(Z)}return{polygons:_,objectUrls:[],dispose:()=>{},warnings:[],metadata:{triangleCount:_.length,materials:f,sourceBytes:e.length}}}function Ui(e,n){return{polygons:[],objectUrls:[],dispose:()=>{},warnings:[],metadata:{triangleCount:0,materials:e,sourceBytes:n}}}var vt=e=>Math.round(Math.max(0,Math.min(1,e))*255).toString(16).padStart(2,"0");function vn(e){let n={},r={},t=null;for(let o of e.split(`
|
|
3
|
+
`)){let s=o.trim();if(!(s.length===0||s.charCodeAt(0)===35)){if(s.startsWith("newmtl ")){t=s.slice(7).trim();continue}if(t){if(s.startsWith("Kd ")){let i=s.split(/\s+/),l=parseFloat(i[1]),c=parseFloat(i[2]),a=parseFloat(i[3]);Number.isFinite(l)&&Number.isFinite(c)&&Number.isFinite(a)&&(n[t]=`#${vt(l)}${vt(c)}${vt(a)}`)}else if(s.startsWith("map_Kd ")){let i=s.split(/\s+/),l=i[i.length-1]?.replace(/\\+/g,"/");l&&l!=="map_Kd"&&(r[t]=l)}}}}return{colors:n,textures:r}}var Qr={5120:1,5121:1,5122:2,5123:2,5125:4,5126:4},qr={SCALAR:1,VEC2:2,VEC3:3,VEC4:4,MAT2:4,MAT3:9,MAT4:16};function Jr(e){let n=globalThis.TextDecoder;return new n().decode(e)}function Fi(e){let n=e.indexOf(",");if(n<0)throw new Error("parseGltf: malformed data: URI");let r=e.slice(5,n),t=e.slice(n+1);if(!r.includes(";base64")){let i=decodeURIComponent(t),l=new Uint8Array(i.length);for(let c=0;c<i.length;c++)l[c]=i.charCodeAt(c)&255;return l}let o=globalThis.atob(t),s=new Uint8Array(o.length);for(let i=0;i<o.length;i++)s[i]=o.charCodeAt(i);return s}function $i(e,n){let r=e.buffers?.[0];if(!r)throw new Error("parseGltf: JSON doc has no buffers[0]");let t=r.uri;if(!t)throw new Error("parseGltf: JSON doc buffer has no uri (and there's no GLB BIN chunk)");if(t.startsWith("data:"))return Fi(t);if(n){let o=n(t);if(o instanceof Uint8Array)return o;throw new Error("parseGltf: resolveBuffer returned a Promise; use parseGltf via async if your buffers are external")}throw new Error(`parseGltf: external buffer URI "${t}" \u2014 provide options.resolveBuffer`)}function Bi(e){let n=new DataView(e);if(n.getUint32(0,!0)!==1179937895)throw new Error("parseGltf: not a GLB (bad magic)");let r=n.getUint32(4,!0);if(r!==2)throw new Error(`parseGltf: only glTF v2 supported (got v${r})`);let t=12,o=null,s=null;for(;t<e.byteLength;){let i=n.getUint32(t,!0),l=n.getUint32(t+4,!0),c=t+8;if(l===1313821514){let a=new Uint8Array(e,c,i);o=JSON.parse(Jr(a))}else l===5130562&&(s=new Uint8Array(e,c,i));t=c+i}if(!o)throw new Error("parseGltf: no JSON chunk in GLB");return{doc:o,bin:s}}function At(e,n,r){let t=e.accessors?.[r],o=e.bufferViews?.[t?.bufferView??-1];if(!t||!o)throw new Error(`parseGltf: bad accessor/bufferView ${r}`);let s=Qr[t.componentType],i=qr[t.type];if(!s||!i)throw new Error(`parseGltf: unsupported accessor type ${t.type}/${t.componentType}`);let l=(o.byteOffset??0)+(t.byteOffset??0),c=t.count*i,a=n.buffer.slice(n.byteOffset+l,n.byteOffset+l+c*s),u;switch(t.componentType){case 5126:u=new Float32Array(a);break;case 5123:u=new Uint16Array(a);break;case 5125:u=new Uint32Array(a);break;case 5121:u=new Uint8Array(a);break;default:throw new Error(`parseGltf: unhandled componentType ${t.componentType}`)}return{array:u,count:t.count,componentCount:i}}function ji(e,n,r){switch(r){case 5120:return e.getInt8(n);case 5121:return e.getUint8(n);case 5122:return e.getInt16(n,!0);case 5123:return e.getUint16(n,!0);case 5125:return e.getUint32(n,!0);case 5126:return e.getFloat32(n,!0);default:throw new Error(`parseGltf: unhandled componentType ${r}`)}}function Xi(e,n){switch(n){case 5120:return Math.max(e/127,-1);case 5121:return e/255;case 5122:return Math.max(e/32767,-1);case 5123:return e/65535;default:return e}}function Vn(e,n,r){let t=e.accessors?.[r],o=e.bufferViews?.[t?.bufferView??-1];if(!t||!o)throw new Error(`parseGltf: bad accessor/bufferView ${r}`);let s=Qr[t.componentType],i=qr[t.type];if(!s||!i)throw new Error(`parseGltf: unsupported accessor type ${t.type}/${t.componentType}`);let l=n.byteOffset+(o.byteOffset??0)+(t.byteOffset??0),c=o.byteStride??s*i,a=new DataView(n.buffer),u=new Array(t.count*i),f=0;for(let g=0;g<t.count;g++){let m=l+g*c;for(let p=0;p<i;p++){let d=ji(a,m+p*s,t.componentType);u[f++]=t.normalized?Xi(d,t.componentType):d}}return{values:u,count:t.count,componentCount:i}}function Yi(e,n,r){let t=[],o=[],s=globalThis;for(let i of e.images??[]){if(i.uri){if(r&&!i.uri.startsWith("data:"))try{t.push(new s.URL(i.uri,r).href)}catch{t.push(i.uri)}else t.push(i.uri);continue}if(i.bufferView!==void 0){let l=e.bufferViews?.[i.bufferView];if(!l){t.push("");continue}let c=l.byteOffset??0,a=n.subarray(c,c+l.byteLength),u=i.mimeType??"image/png",f=new s.Blob([a],{type:u}),g=s.URL.createObjectURL(f);t.push(g),o.push(g)}else t.push("")}return{urls:t,objectUrls:o}}function Hi(e,n){let r=new Map,t=e.materials??[];for(let o=0;o<t.length;o++){let s=t[o].pbrMetallicRoughness?.baseColorTexture?.index;if(s===void 0)continue;let i=e.textures?.[s]?.source;if(i===void 0)continue;let l=n[i];l&&r.set(o,l)}return r}function Ki(e,n){let r=e?.pbrMetallicRoughness?.baseColorFactor;if(!r||r.length<3)return n;let t=l=>Math.max(0,Math.min(1,l)),o=l=>Math.round(t(l)*255).toString(16).padStart(2,"0"),s=l=>Math.round(t(l)*255),i=t(r[3]??1);return i<1?`rgba(${s(r[0])}, ${s(r[1])}, ${s(r[2])}, ${Math.round(i*1e3)/1e3})`:`#${o(r[0])}${o(r[1])}${o(r[2])}`}var _e=[1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1];function Ct(e,n){let r=new Array(16);for(let t=0;t<4;t++)for(let o=0;o<4;o++)r[o*4+t]=e[0+t]*n[o*4+0]+e[4+t]*n[o*4+1]+e[8+t]*n[o*4+2]+e[12+t]*n[o*4+3];return r}function Cn(e,n){return[e[0]*n[0]+e[4]*n[1]+e[8]*n[2]+e[12],e[1]*n[0]+e[5]*n[1]+e[9]*n[2]+e[13],e[2]*n[0]+e[6]*n[1]+e[10]*n[2]+e[14]]}function eo(e,n,r){let t=e?.[0]??0,o=e?.[1]??0,s=e?.[2]??0,i=n?.[0]??0,l=n?.[1]??0,c=n?.[2]??0,a=n?.[3]??1,u=r?.[0]??1,f=r?.[1]??1,g=r?.[2]??1,m=i+i,p=l+l,d=c+c,y=i*m,b=i*p,M=i*d,v=l*p,P=l*d,x=c*d,A=a*m,T=a*p,V=a*d;return[(1-(v+x))*u,(b+V)*u,(M-T)*u,0,(b-V)*f,(1-(y+x))*f,(P+A)*f,0,(M+T)*g,(P-A)*g,(1-(y+v))*g,0,t,o,s,1]}function Wi(e){return e.matrix&&e.matrix.length===16?e.matrix.slice():eo(e.translation,e.rotation,e.scale)}function Zi(e,n){return[e[0]+n[0],e[1]+n[1],e[2]+n[2]]}function Hr(e,n){return[e[0]*n,e[1]*n,e[2]*n]}function no(e,n,r){let t=new Array(Math.min(e.length,n.length));for(let o=0;o<t.length;o++)t[o]=e[o]+(n[o]-e[o])*r;return t}function Ze(e){let n=Math.hypot(e[0]??0,e[1]??0,e[2]??0,e[3]??1)||1;return[(e[0]??0)/n,(e[1]??0)/n,(e[2]??0)/n,(e[3]??1)/n]}function Qi(e,n,r){let t=Ze(e),o=Ze(n),s=t[0]*o[0]+t[1]*o[1]+t[2]*o[2]+t[3]*o[3];if(s<0&&(s=-s,o=[-o[0],-o[1],-o[2],-o[3]]),s>.9995)return Ze(no(t,o,r));let i=Math.acos(Math.max(-1,Math.min(1,s))),l=Math.sin(i),c=Math.sin((1-r)*i)/l,a=Math.sin(r*i)/l;return Ze([t[0]*c+o[0]*a,t[1]*c+o[1]*a,t[2]*c+o[2]*a,t[3]*c+o[3]*a])}function qi(e){return{translation:e?.translation?.slice()??[0,0,0],rotation:e?.rotation?.slice()??[0,0,0,1],scale:e?.scale?.slice()??[1,1,1],matrix:e?.matrix&&e.matrix.length===16?e.matrix.slice():void 0}}function Kr(e){return e.matrix?e.matrix.slice():eo(e.translation,e.rotation,e.scale)}function Ji(e){let n=e.scene??0,r=e.scenes?.[n]?.nodes;return r&&r.length>0?r:[]}function Wr(e,n){let r=e.nodes??[],t=new Array(r.length),o=new Set,s=(l,c)=>{if(l<0||l>=r.length)return;let a=Ct(c,n[l]??_e);t[l]=a,o.add(l);for(let u of r[l].children??[])s(u,a)},i=Ji(e);if(i.length>0)for(let l of i)s(l,_e);for(let l=0;l<r.length;l++)o.has(l)||s(l,_e);return t}function Zr(e,n,r,t,o){if(r===void 0)return;let{values:s,count:i,componentCount:l}=Vn(e,n,r);if(i!==o||l<1)return;let c=[];for(let a=0;a<i;a++){let u=[];for(let f=0;f<t;f++)u.push(s[a*l+f]??0);c.push(u)}return c}function ec(e,n,r,t){if(r===void 0)return Array.from({length:t},()=>_e.slice());let{values:o,componentCount:s,count:i}=Vn(e,n,r);if(s!==16)throw new Error(`parseGltf: inverseBindMatrices accessor ${r} is not MAT4`);let l=[];for(let c=0;c<t;c++){let a=Math.min(c,i-1);l.push(o.slice(a*16,a*16+16))}return l}function An(e,n){let r=e.componentCount,t=e.interpolation==="CUBICSPLINE"?(n*3+1)*r:n*r;return e.output.slice(t,t+r)}function nc(e,n,r){let t=e.input;if(t.length===0)return[];if(t.length===1||n<=t[0])return An(e,0);let o=t.length-1;if(n>=t[o])return An(e,o);let s=0,i=o;for(;s+1<i;){let g=s+i>>1;t[g]<=n?s=g:i=g}let l=t[s],c=t[s+1],a=c>l?(n-l)/(c-l):0,u=An(e,s),f=An(e,s+1);return e.interpolation==="STEP"?u:r==="rotation"?Qi(u,f,a):no(u,f,a)}function tc(e,n,r,t){let o=e.animations??[];if(o.length===0||r.length===0)return;let s=(e.nodes??[]).map(m=>qi(m)),i=s.map(Kr),l=Wr(e,i),c=(e.skins??[]).map(m=>({joints:m.joints??[],inverseBindMatrices:ec(e,n,m.inverseBindMatrices,m.joints?.length??0)})),a=[];for(let m=0;m<o.length;m++){let p=o[m],d=(p.samplers??[]).map(M=>{let v=Vn(e,n,M.input),P=Vn(e,n,M.output);return{input:v.values,output:P.values,componentCount:P.componentCount,interpolation:M.interpolation??"LINEAR"}}),y=[];for(let M of p.channels??[]){let v=M.target.node,P=M.target.path,x=d[M.sampler];v===void 0||!P||!x||P==="weights"||y.push({sampler:x,targetNode:v,path:P})}let b=y.reduce((M,v)=>{let P=v.sampler.input;return Math.max(M,P[P.length-1]??0)},0);a.push({info:{index:m,name:p.name??`animation_${m}`,duration:b,channelCount:y.length},channels:y})}let u=a.map(m=>m.info);if(u.length===0)return;let f=(m,p,d,y,b,M)=>{let v=t(m),P=t(p),x=t(d);if(v[0]===P[0]&&v[1]===P[1]&&v[2]===P[2]||v[0]===x[0]&&v[1]===x[1]&&v[2]===x[2]||P[0]===x[0]&&P[1]===x[1]&&P[2]===x[2])return null;let A={vertices:[v,P,x],color:y};return b&&(A.texture=b),M&&(A.uvs=M),A};return{clips:u,sample:(m,p)=>{let d=typeof m=="number"?a[m]:a.find(x=>x.info.name===m);if(!d)return[];let y=d.info.duration,b=y>0?(p%y+y)%y:Math.max(0,p),M=s.map(x=>({translation:x.translation.slice(),rotation:x.rotation.slice(),scale:x.scale.slice(),matrix:x.matrix?x.matrix.slice():void 0}));for(let x of d.channels){let A=M[x.targetNode];if(!A)continue;let T=nc(x.sampler,b,x.path);A.matrix=void 0,x.path==="translation"?A.translation=T.slice(0,3):x.path==="rotation"?A.rotation=Ze(T.slice(0,4)):x.path==="scale"&&(A.scale=T.slice(0,3))}let v=Wr(e,M.map(Kr)),P=[];for(let x of r){let A=[];if(x.skinIndex!==void 0&&x.joints&&x.weights&&c[x.skinIndex]){let T=c[x.skinIndex];for(let V=0;V<x.positions.length;V++){let w=x.positions[V],O=[0,0,0],L=0,U=x.joints[V]??[],R=x.weights[V]??[];for(let D=0;D<4;D++){let k=R[D]??0;if(k<=0)continue;let _=Math.round(U[D]??0),E=T.joints[_],C=v[E],h=T.inverseBindMatrices[_];if(!C||!h)continue;let S=Ct(C,h);O=Zi(O,Hr(Cn(S,w),k)),L+=k}A.push(L>0?Hr(O,1/L):Cn(x.meshBindWorld,w))}}else{let T=x.meshNode!==null?v[x.meshNode]??x.meshBindWorld:x.meshBindWorld;for(let V of x.positions)A.push(Cn(T,V))}for(let T=0;T+2<x.indices.length;T+=3){let V=x.indices[T],w=x.indices[T+1],O=x.indices[T+2],L=A[V],U=A[w],R=A[O];if(!L||!U||!R)continue;let D;if(x.uvs&&x.texture){let _=x.uvs[V],E=x.uvs[w],C=x.uvs[O];_&&E&&C&&(D=[_,E,C])}let k=f(L,U,R,x.color,x.texture,D);k&&P.push(k)}}return P}}}function Sn(e,n){let r=n?.targetSize??60,t=n?.gridShift??1,o=n?.defaultColor??"#888888",s=n?.materialColors??{},i=e instanceof Uint8Array?e.buffer.slice(e.byteOffset,e.byteOffset+e.byteLength):e,l=i.byteLength,c,a;if(i.byteLength>=4&&new DataView(i).getUint32(0,!0)===1179937895){let h=Bi(i);if(c=h.doc,!h.bin)throw new Error("parseGltf: GLB has no binary chunk");a=h.bin}else c=JSON.parse(Jr(new Uint8Array(i))),a=$i(c,n?.resolveBuffer);let{urls:u,objectUrls:f}=Yi(c,a,n?.baseUrl),g=Hi(c,u),m=[],p=[],d=(c.meshes??[]).map((h,S)=>h.name??`mesh_${S}`),y=(c.materials??[]).map((h,S)=>h.name??`material_${S}`);function b(h,S,I){let G=c.meshes?.[h];if(G)for(let N of G.primitives){if((N.mode??4)!==4)continue;let z=N.material!==void 0?c.materials?.[N.material]?.name:void 0,H=(z?s[z]:void 0)??Ki(N.material!==void 0?c.materials?.[N.material]:void 0,o),K=N.material!==void 0?g.get(N.material):void 0,{array:Z,count:J}=At(c,a,N.attributes.POSITION);if(!(Z instanceof Float32Array))continue;let oe=[],Q=[];for(let W=0;W<J;W++){let $=[Z[W*3],Z[W*3+1],Z[W*3+2]];oe.push($),Q.push(Cn(S,$))}let q=null,ae=N.attributes.TEXCOORD_0;if(K&&ae!==void 0){let{array:W,count:$}=At(c,a,ae);q=[];let Y=1;W instanceof Uint8Array?Y=1/255:W instanceof Uint16Array&&(Y=1/65535);for(let ie=0;ie<$;ie++){let de=W[ie*2]*Y,ve=W[ie*2+1]*Y;q.push([de,1-ve])}}let re;if(N.indices!==void 0){let{array:W,count:$}=At(c,a,N.indices);re=[];for(let Y=0;Y<$;Y++)re.push(Number(W[Y]))}else re=Q.map((W,$)=>$);if((c.animations?.length??0)>0){let W=Zr(c,a,N.attributes.JOINTS_0,4,J),$=Zr(c,a,N.attributes.WEIGHTS_0,4,J);p.push({meshNode:I,meshBindWorld:S,skinIndex:I!==null?c.nodes?.[I]?.skin:void 0,positions:oe,indices:re,color:H,texture:K,uvs:q??void 0,joints:W,weights:$})}for(let W=0;W+2<re.length;W+=3){let $=Q[re[W]],Y=Q[re[W+1]],ie=Q[re[W+2]];if(!$||!Y||!ie)continue;let de;if(q&&K){let ve=q[re[W]],Ie=q[re[W+1]],Ee=q[re[W+2]];ve&&Ie&&Ee&&(de=[ve,Ie,Ee])}m.push({v0:$,v1:Y,v2:ie,color:H,texture:K,uvs:de})}}}function M(h,S){let I=c.nodes?.[h];if(!I)return;let G=Ct(S,Wi(I));typeof I.mesh=="number"&&b(I.mesh,G,h);for(let N of I.children??[])M(N,G)}let v=c.scene??0,P=c.scenes?.[v]?.nodes;if(P&&P.length>0)for(let h of P)M(h,_e);else for(let h=0;h<(c.meshes?.length??0);h++)b(h,_e,null);let x=rc(f);if(m.length===0)return{polygons:[],objectUrls:f,dispose:x,warnings:[],metadata:{triangleCount:0,meshes:d,materials:y,sourceBytes:l}};let A=1/0,T=1/0,V=1/0,w=-1/0,O=-1/0,L=-1/0;for(let h of m)for(let S of[h.v0,h.v1,h.v2])S[0]<A&&(A=S[0]),S[0]>w&&(w=S[0]),S[1]<T&&(T=S[1]),S[1]>O&&(O=S[1]),S[2]<V&&(V=S[2]),S[2]>L&&(L=S[2]);let U=Math.max(w-A,O-T,L-V),R=U>0?r/U:1,D=h=>Math.round(h*1e3)/1e3,_=(n?.upAxis??"y")==="z"?([h,S,I])=>[D((h-A)*R+t),D((S-T)*R+t),D((I-V)*R+t)]:([h,S,I])=>[D((I-V)*R+t),D((h-A)*R+t),D((S-T)*R+t)],E=tc(c,a,p,_),C=[];for(let h of m){let S=_(h.v0),I=_(h.v1),G=_(h.v2);if(S[0]===I[0]&&S[1]===I[1]&&S[2]===I[2]||S[0]===G[0]&&S[1]===G[1]&&S[2]===G[2]||I[0]===G[0]&&I[1]===G[1]&&I[2]===G[2])continue;let N={vertices:[S,I,G],color:h.color};h.texture&&(N.texture=h.texture),h.uvs&&(N.uvs=h.uvs),C.push(N)}return{polygons:C,animation:E,objectUrls:f,dispose:x,warnings:[],metadata:{triangleCount:C.length,meshes:d,materials:y,animations:E?.clips,sourceBytes:l}}}function rc(e){let n=!1;return()=>{if(n)return;n=!0;let r=globalThis.URL;if(r?.revokeObjectURL)for(let t of e)try{r.revokeObjectURL(t)}catch{}}}function Vt(e){return e.material?.texture??e.texture}function oc(){let e=globalThis;return typeof e.Image!="function"||typeof e.document?.createElement!="function"?null:{Image:e.Image,createCanvas(){return e.document.createElement("canvas")}}}function sc(e,n){return new Promise((r,t)=>{let o=new n,s=!1,i=l=>{s||(s=!0,l())};o.decoding="async",o.onload=()=>i(()=>r(o)),o.onerror=()=>i(()=>t(new Error(`texture load failed: ${e}`))),o.src=e,typeof o.decode=="function"&&o.decode().then(()=>i(()=>r(o)),()=>{})})}async function ic(e,n,r){try{let t=await sc(e,n.Image),o=Math.max(0,Math.floor(t.naturalWidth||t.width||0)),s=Math.max(0,Math.floor(t.naturalHeight||t.height||0));if(o<=0||s<=0||o*s>r)return null;let i=n.createCanvas();i.width=o,i.height=s;let l=i.getContext("2d",{willReadFrequently:!0});if(!l)return null;l.drawImage(t,0,0,o,s);let c=l.getImageData(0,0,o,s).data;return{width:o,height:s,data:c,lowDetail:cc(o,s,c)}}catch{return null}}function to(e,n,r,t,o,s){let i=(t*n+r)*4,l=(s*n+o)*4;return Math.max(Math.abs((e[i]??0)-(e[l]??0)),Math.abs((e[i+1]??0)-(e[l+1]??0)),Math.abs((e[i+2]??0)-(e[l+2]??0)))}function cc(e,n,r){let t=Math.max(1,Math.floor(Math.max(e,n)/128)),o=0,s=0,i=0;for(let l=0;l<n;l+=t)for(let c=0;c<e;c+=t){if(c+t<e){let a=to(r,e,c,l,c+t,l);i+=a,o++,a>32&&s++}if(l+t<n){let a=to(r,e,c,l,c,l+t);i+=a,o++,a>32&&s++}}return o>0&&s/o<=.045&&i/o<=10}function ro(e,n,r){return Math.max(n,Math.min(r,e))}function ac(e,n){let r=n[0],t=1-n[1];if(!Number.isFinite(r)||!Number.isFinite(t))return null;let o=ro(Math.floor(r*e.width),0,e.width-1),i=(ro(Math.floor(t*e.height),0,e.height-1)*e.width+o)*4;return{r:e.data[i]??0,g:e.data[i+1]??0,b:e.data[i+2]??0,a:e.data[i+3]??255}}function Me(e,n,r,t,o,s){return[e[0]*t+n[0]*o+r[0]*s,e[1]*t+n[1]*o+r[1]*s]}function lc(e){let[n,r,t]=e;return[Me(n,r,t,1/3,1/3,1/3),Me(n,r,t,.8,.1,.1),Me(n,r,t,.1,.8,.1),Me(n,r,t,.1,.1,.8),Me(n,r,t,.45,.45,.1),Me(n,r,t,.45,.1,.45),Me(n,r,t,.1,.45,.45)]}function uc(e){let[n,r,t]=e,o=lc(e);for(let s=1;s<6;s++)for(let i=1;i<6-s;i++){let l=6-s-i;l<=0||o.push(Me(n,r,t,s/6,i/6,l/6))}return o}function co(e){if(e.textureTriangles?.length)return e.textureTriangles;let n=e.uvs;if(!n||n.length!==e.vertices.length||n.length<3)return[];let r=[];for(let t=1;t+1<n.length;t++)r.push({uvs:[n[0],n[t],n[t+1]]});return r}function oo(e,n,r){return Math.abs(e.r-n.r)<=r&&Math.abs(e.g-n.g)<=r&&Math.abs(e.b-n.b)<=r&&Math.abs(e.a-n.a)<=r}function so(e){let n=t=>Math.round(Math.max(0,Math.min(255,t))).toString(16).padStart(2,"0");if(e.a>=255)return`#${n(e.r)}${n(e.g)}${n(e.b)}`;let r=Math.round(Math.max(0,Math.min(255,e.a))/255*1e3)/1e3;return`rgba(${Math.round(e.r)}, ${Math.round(e.g)}, ${Math.round(e.b)}, ${r})`}function fc(){return{min:{r:255,g:255,b:255,a:255},max:{r:0,g:0,b:0,a:0},sum:{r:0,g:0,b:0,a:0},count:0}}function mc(e,n){e.min.r=Math.min(e.min.r,n.r),e.min.g=Math.min(e.min.g,n.g),e.min.b=Math.min(e.min.b,n.b),e.min.a=Math.min(e.min.a,n.a),e.max.r=Math.max(e.max.r,n.r),e.max.g=Math.max(e.max.g,n.g),e.max.b=Math.max(e.max.b,n.b),e.max.a=Math.max(e.max.a,n.a),e.sum.r+=n.r,e.sum.g+=n.g,e.sum.b+=n.b,e.sum.a+=n.a,e.count++}function io(e){return{r:e.sum.r/e.count,g:e.sum.g/e.count,b:e.sum.b/e.count,a:e.sum.a/e.count}}function gc(e,n,r,t){let o=co(e);if(o.length===0||!t&&!n.lowDetail)return null;let s=fc();for(let i of o)for(let l of uc(i.uvs)){let c=ac(n,l);if(!c)return null;mc(s,c)}return s.count===0?null:oo(s.min,s.max,r)?so(io(s)):t||!oo(s.min,s.max,32)?null:so(io(s))}function pc(e,n){let{texture:r,material:t,uvs:o,textureTriangles:s,...i}=e;return{...i,color:n}}async function ao(e,n={}){if(n.enabled===!1)return null;let r=oc();if(!r)return null;let t=e.filter(c=>Vt(c)&&co(c).length>0);if(t.length===0)return null;let o=n.maxTexturePixels??16777216,s=new Map;await Promise.all(Array.from(new Set(t.map(c=>Vt(c)))).map(async c=>{s.set(c,await ic(c,r,o))}));let i=n.colorTolerance??2,l=n.colorTolerance!==void 0;return{bake(c){let a=!1,u=c.map(f=>{let g=Vt(f);if(!g)return f;let m=s.get(g);if(!m)return f;let p=gc(f,m,i,l);return p?(a=!0,pc(f,p)):f});return{polygons:a?u:c,changed:a}}}}async function lo(e,n={}){let r=await ao(e,n);return r?r.bake(e).polygons:e}async function Tn(e,n={}){let r=await ao(e.polygons,n);if(!r)return e;let t=r.bake(e.polygons);return!t.changed&&!e.animation?e:{...e,polygons:t.polygons,animation:e.animation?{...e.animation,sample(o,s){return r.bake(e.animation.sample(o,s)).polygons}}:e.animation}}var dc=[0,4294967295,4291624959,4288282623,4284940287,4281597951,4278255615,4294954239,4291611903,4288269567,4284927231,4281584895,4278242559,4294941183,4291598847,4288256511,4284914175,4281571839,4278229503,4294928127,4291585791,4288243455,4284901119,4281558783,4278216447,4294915071,4291572735,4288230399,4284888063,4281545727,4278203391,4294902015,4291559679,4288217343,4284875007,4281532671,4278190335,4294967244,4291624908,4288282572,4284940236,4281597900,4278255564,4294954188,4291611852,4288269516,4284927180,4281584844,4278242508,4294941132,4291598796,4288256460,4284914124,4281571788,4278229452,4294928076,4291585740,4288243404,4284901068,4281558732,4278216396,4294915020,4291572684,4288230348,4284888012,4281545676,4278203340,4294901964,4291559628,4288217292,4284874956,4281532620,4278190284,4294967193,4291624857,4288282521,4284940185,4281597849,4278255513,4294954137,4291611801,4288269465,4284927129,4281584793,4278242457,4294941081,4291598745,4288256409,4284914073,4281571737,4278229401,4294928025,4291585689,4288243353,4284901017,4281558681,4278216345,4294914969,4291572633,4288230297,4284887961,4281545625,4278203289,4294901913,4291559577,4288217241,4284874905,4281532569,4278190233,4294967142,4291624806,4288282470,4284940134,4281597798,4278255462,4294954086,4291611750,4288269414,4284927078,4281584742,4278242406,4294941030,4291598694,4288256358,4284914022,4281571686,4278229350,4294927974,4291585638,4288243302,4284900966,4281558630,4278216294,4294914918,4291572582,4288230246,4284887910,4281545574,4278203238,4294901862,4291559526,4288217190,4284874854,4281532518,4278190182,4294967091,4291624755,4288282419,4284940083,4281597747,4278255411,4294954035,4291611699,4288269363,4284927027,4281584691,4278242355,4294940979,4291598643,4288256307,4284913971,4281571635,4278229299,4294927923,4291585587,4288243251,4284900915,4281558579,4278216243,4294914867,4291572531,4288230195,4284887859,4281545523,4278203187,4294901811,4291559475,4288217139,4284874803,4281532467,4278190131,4294967040,4291624704,4288282368,4284940032,4281597696,4278255360,4294953984,4291611648,4288269312,4284926976,4281584640,4278242304,4294940928,4291598592,4288256256,4284913920,4281571584,4278229248,4294927872,4291585536,4288243200,4284900864,4281558528,4278216192,4294914816,4291572480,4288230144,4284887808,4281545472,4278203136,4294901760,4291559424,4288217088,4284874752,4281532416,4278190318,4278190301,4278190267,4278190250,4278190216,4278190199,4278190165,4278190148,4278190114,4278190097,4278251008,4278246656,4278237952,4278233600,4278224896,4278220544,4278211840,4278207488,4278198784,4278194432,4293787648,4292673536,4290445312,4289331200,4287102976,4285988864,4283760640,4282646528,4280418304,4279304192,4293848814,4292730333,4290493371,4289374890,4287137928,4286019447,4283782485,4282664004,4280427042,4279308561];function xc(e){let n=e>>0&255,r=e>>8&255,t=e>>16&255;return[n,r,t]}function St(e){return(e&255).toString(16).padStart(2,"0")}function fo(e,n,r){return`#${St(e)}${St(n)}${St(r)}`}function hc(e,n,r,t){if(t>=255)return fo(e,n,r);let o=Math.round(Math.max(0,Math.min(255,t))/255*1e3)/1e3;return`rgba(${e}, ${n}, ${r}, ${o})`}var yc=542658390;function wn(e,n){let r=n?.targetSize??60,t=n?.gridShift??0,o=e.byteLength;if(e.byteLength<8)return Ne(o,["parseVox: buffer too small to be a valid .vox file"]);let s=new DataView(e);if(s.getUint32(0,!0)!==yc)return Ne(o,["parseVox: not a .vox file (bad magic)"]);if(e.byteLength<20)return Ne(o,["parseVox: buffer too small for MAIN chunk"]);if(uo(s,8)!=="MAIN")return Ne(o,["parseVox: expected MAIN chunk at offset 8"]);let a=20+s.getUint32(12,!0),u=a+s.getUint32(16,!0),f=[],g=[],m=null;for(;a<u&&a+12<=e.byteLength;){let C=uo(s,a),h=s.getUint32(a+4,!0),S=s.getUint32(a+8,!0),I=a+12,G=I+h+S;if(C==="SIZE"){if(h>=12&&I+12<=e.byteLength){let N=s.getUint32(I,!0),F=s.getUint32(I+4,!0),z=s.getUint32(I+8,!0);f.push({sx:N,sy:F,sz:z})}}else if(C==="XYZI"){if(h>=4&&I+4<=e.byteLength){let N=s.getUint32(I,!0),F=[],z=Math.min(N,Math.floor((h-4)/4));for(let X=0;X<z;X++){let H=I+4+X*4;F.push({x:s.getUint8(H),y:s.getUint8(H+1),z:s.getUint8(H+2),colorIndex:s.getUint8(H+3)})}g.push(F)}}else if(C==="RGBA"&&h>=1024&&I+1024<=e.byteLength){m=[];for(let N=0;N<256;N++){let F=I+N*4,z=s.getUint8(F),X=s.getUint8(F+1),H=s.getUint8(F+2),K=s.getUint8(F+3);m.push(hc(z,X,H,K))}}a=G}let p=[],d=new Set;for(let C of g)for(let h of C){if(h.colorIndex===0)continue;let S=`${h.x},${h.y},${h.z}`;d.has(S)||(d.add(S),p.push(h))}if(p.length===0)return Ne(o,[]);let y=C=>{let h=C-1;if(m!==null)return m[h]??"#888888";let S=dc[C]??0,[I,G,N]=xc(S);return fo(I,G,N)},b=(C,h,S)=>d.has(`${C},${h},${S}`),M=new Map,v=(C,h,S,I,G)=>{let N=`${C}:${h}:${G}`,F=M.get(N);F||(F=new Set,M.set(N,F)),F.add(`${S},${I}`)};for(let C of p){let{x:h,y:S,z:I}=C,G=y(C.colorIndex);b(h+1,S,I)||v(0,h+1,S,I,G),b(h-1,S,I)||v(1,h,S,I,G),b(h,S+1,I)||v(2,S+1,h,I,G),b(h,S-1,I)||v(3,S,h,I,G),b(h,S,I+1)||v(4,I+1,h,S,G),b(h,S,I-1)||v(5,I,h,S,G)}function P(C){let h=new Set,S=[],I=1/0,G=-1/0,N=1/0,F=-1/0;for(let z of C){let[X,H]=z.split(","),K=+X,Z=+H;K<I&&(I=K),K>G&&(G=K),Z<N&&(N=Z),Z>F&&(F=Z)}for(let z=N;z<=F;z++)for(let X=I;X<=G;X++){let H=`${X},${z}`;if(!C.has(H)||h.has(H))continue;let K=1;for(;X+K<=G;){let J=`${X+K},${z}`;if(!C.has(J)||h.has(J))break;K++}let Z=1;e:for(;z+Z<=F;){for(let J=0;J<K;J++){let oe=`${X+J},${z+Z}`;if(!C.has(oe)||h.has(oe))break e}Z++}for(let J=0;J<Z;J++)for(let oe=0;oe<K;oe++)h.add(`${X+oe},${z+J}`);S.push({u:X,v:z,w:K,h:Z})}return S}let x=(C,h,S,I,G,N)=>{let F=S+G,z=I+N;switch(C){case 0:return[[h,S,I],[h,F,I],[h,F,z],[h,S,z]];case 1:return[[h,F,I],[h,S,I],[h,S,z],[h,F,z]];case 2:return[[S,h,I],[S,h,z],[F,h,z],[F,h,I]];case 3:return[[F,h,I],[F,h,z],[S,h,z],[S,h,I]];case 4:return[[S,I,h],[F,I,h],[F,z,h],[S,z,h]];default:return[[S,z,h],[F,z,h],[F,I,h],[S,I,h]]}},A=[];for(let[C,h]of M){let S=C.indexOf(":"),I=C.indexOf(":",S+1),G=+C.slice(0,S),N=+C.slice(S+1,I),F=C.slice(I+1);for(let{u:z,v:X,w:H,h:K}of P(h))A.push({vertices:x(G,N,z,X,H,K),color:F})}if(A.length===0)return Ne(o,[]);let T=1/0,V=1/0,w=1/0,O=-1/0,L=-1/0,U=-1/0;for(let C of A)for(let h of C.vertices)h[0]<T&&(T=h[0]),h[0]>O&&(O=h[0]),h[1]<V&&(V=h[1]),h[1]>L&&(L=h[1]),h[2]<w&&(w=h[2]),h[2]>U&&(U=h[2]);let R=Math.max(O-T,L-V,U-w),D=R>0?r/R:1,k=C=>Math.round(C*1e3)/1e3,_=C=>[k((C[0]-T)*D+t),k((C[1]-V)*D+t),k((C[2]-w)*D+t)],E=A.map(({vertices:C,color:h})=>({vertices:C.map(_),color:h}));return{polygons:E,objectUrls:[],dispose:()=>{},warnings:[],metadata:{triangleCount:E.length,sourceBytes:o,voxelCount:p.length}}}function uo(e,n){return String.fromCharCode(e.getUint8(n))+String.fromCharCode(e.getUint8(n+1))+String.fromCharCode(e.getUint8(n+2))+String.fromCharCode(e.getUint8(n+3))}function Ne(e,n){return{polygons:[],objectUrls:[],dispose:()=>{},warnings:n,metadata:{triangleCount:0,sourceBytes:e}}}var we="loadMesh";function Tt(e,n){let r=gn(e.polygons,{meshResolution:n?.meshResolution});return r.length===e.polygons.length?e:{...e,polygons:r}}async function mo(e,n){let r=n?.solidTextureSamples;return r===!1?e:Tn(e,typeof r=="object"?r:void 0)}function bc(e){let n=e.split("?")[0].split("#")[0],r=n.lastIndexOf(".");return r<0?"":n.slice(r+1).toLowerCase()}async function go(e,n){let r=bc(e);if(r==="mtl")throw new Error(`${we}: .mtl is a material file, not a mesh \u2014 use parseMtl directly`);let t=globalThis.fetch;if(!t)throw new Error(`${we}: no fetch() in this environment`);let o=n?.baseUrl??e;if(r==="obj"){let s=await t(e);if(!s.ok)throw new Error(`${we}: ${e} \u2192 ${s.status}`);let i=await s.text(),l=n?.objOptions;if(n?.mtlUrl){let a=await t(n.mtlUrl);if(!a.ok)throw new Error(`${we}: ${n.mtlUrl} \u2192 ${a.status}`);let u=await a.text(),{colors:f,textures:g}=vn(u),m={},p=globalThis.URL,d=globalThis.document?.baseURI,y=n.mtlUrl;if(p&&d)try{y=new p(n.mtlUrl,d).toString()}catch{}for(let[b,M]of Object.entries(g)){if(p)try{m[b]=new p(M,y).toString();continue}catch{}let v=n.mtlUrl.lastIndexOf("/"),P=v>=0?n.mtlUrl.slice(0,v+1):"";m[b]=M.startsWith("/")||/^https?:\/\//.test(M)?M:P+M}l={...l??{},materialColors:{...f,...l?.materialColors??{}},materialTextures:{...m,...l?.materialTextures??{}}}}let c=Mn(i,l);return Tt(await mo(c,n),n)}if(r==="glb"||r==="gltf"){let s=await t(e);if(!s.ok)throw new Error(`${we}: ${e} \u2192 ${s.status}`);let i=await s.arrayBuffer(),l=Sn(i,{baseUrl:o,...n?.gltfOptions??{}});return Tt(await mo(l,n),n)}if(r==="vox"){let s=await t(e);if(!s.ok)throw new Error(`${we}: ${e} \u2192 ${s.status}`);let i=await s.arrayBuffer();return Tt(wn(i,n?.voxOptions),n)}throw new Error(`${we}: unsupported extension ".${r}" (supported: obj, glb, gltf, vox)`)}function po(e,n,r,t,o=.5,s=.5,i=.4){let l=4/(e[2]+3),c=Math.min(n,r)*i,a=n*o+e[0]*c*t*l,u=r*s-e[1]*c*l;return[a,u,e[2]]}function Pc(e){let[n,r,t]=[e.vertices[0],e.vertices[1],e.vertices[2]],o=r[0]-n[0],s=r[1]-n[1],i=r[2]-n[2],l=t[0]-n[0],c=t[1]-n[1],a=t[2]-n[2];return[s*a-i*c,i*l-o*a,o*c-s*l]}function Mc(e,n){let r=Math.hypot(e[0],e[1],e[2]),t=Math.hypot(n[0],n[1],n[2]);return r===0||t===0?1:(e[0]*n[0]+e[1]*n[1]+e[2]*n[2])/(r*t)}function xo(e,n){let r=`${e[0]},${e[1]},${e[2]}`,t=`${n[0]},${n[1]},${n[2]}`;return r<t?`${r}|${t}`:`${t}|${r}`}function vc(e,n=0){if(n<=0){let i=new Set,l=[];for(let c of e){let a=[[0,1],[1,2],[2,0]];for(let[u,f]of a){let g=c.vertices[u],m=c.vertices[f],p=xo(g,m);if(i.has(p))continue;i.add(p);let d={from:g,to:m,weight:2};c.color&&(d.color=c.color),l.push(d)}}return l}let r=Math.cos(n*Math.PI/180),t=new Map,o=[[0,1],[1,2],[2,0]];for(let i of e){let l=Pc(i);for(let[c,a]of o){let u=i.vertices[c],f=i.vertices[a],g=xo(u,f),m=t.get(g);m?m.normals.push(l):t.set(g,{normals:[l],from:u,to:f,color:i.color})}}let s=[];for(let{normals:i,from:l,to:c,color:a}of t.values()){if(i.length<2){let f={from:l,to:c,weight:2};a&&(f.color=a),s.push(f);continue}let u=!1;e:for(let f=0;f<i.length;f++)for(let g=f+1;g<i.length;g++)if(Mc(i[f],i[g])<r){u=!0;break e}if(u){let f={from:l,to:c,weight:2};a&&(f.color=a),s.push(f)}}return s}function ho(e,n=0){return vc(e,n)}0&&(module.exports={BASE_TILE,CAMERA_BACKFACE_CULL_EPS,DEFAULT_CAMERA_STATE,DEFAULT_PROJECTION,LoopOnce,LoopPingPong,LoopRepeat,QUAT_IDENTITY,VOXEL_CAMERA_CULL_AXIS_EPS,VOXEL_CAMERA_CULL_NORMAL_LIMIT,arrowPolygons,axesHelperPolygons,bakeSolidTextureSampledPolygons,bakeSolidTextureSamples,buildSceneContext,cameraCullNormalGroups,cameraCullNormalGroupsFromPolygons,cameraCullNormalKey,cameraCullVisibleSignature,cameraFacingDepth,clampChannel,computeSceneBbox,computeShapeLighting,coverPlanarPolygons,createGlyphcssAnimationMixer,createIsometricCamera,cullInteriorPolygons,dedupeOverlappingPolygons,eulerXYZFromQuat,findOverlappingPolygonDuplicates,formatColor,inverseRotateVec3,isAxisAlignedSurfaceNormal,isVoxelCameraCullableNormalGroups,loadMesh,mergePolygons,normalFacesCamera,normalizeInvertMultiplier,normalizePolygons,octahedronPolygons,optimizeMeshPolygons,parseColor,parseGltf,parseHexColor,parseMtl,parseObj,parsePureColor,parseRgbColor,parseVox,planePolygons,polygonCssSurfaceNormal,polygonFaces,polygonFacesCamera,project,quatFromAxisAngle,quatFromEulerXYZ,quatMultiply,ringPolygons,ringQuadPolygons,rotateVec3,shadeColor,trianglesToFeatureEdges});
|