@selvajs/visualization 1.6.0-beta.5 → 1.6.0-beta.7

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/dist/parse.d.cts CHANGED
@@ -128,6 +128,8 @@ interface MeshMetadata {
128
128
  indexStart: number;
129
129
  /** Arbitrary key-value pairs from the GH Metadata input. */
130
130
  metadata?: Record<string, string>;
131
+ /** The writer's closed-solid verdict (see `isClosedSolid`); absent means compute it here. */
132
+ closed?: boolean;
131
133
  }
132
134
  interface MaterialGroup {
133
135
  materialId: number;
package/dist/parse.d.ts CHANGED
@@ -128,6 +128,8 @@ interface MeshMetadata {
128
128
  indexStart: number;
129
129
  /** Arbitrary key-value pairs from the GH Metadata input. */
130
130
  metadata?: Record<string, string>;
131
+ /** The writer's closed-solid verdict (see `isClosedSolid`); absent means compute it here. */
132
+ closed?: boolean;
131
133
  }
132
134
  interface MaterialGroup {
133
135
  materialId: number;
package/dist/parse.js CHANGED
@@ -1,5 +1,5 @@
1
- import{a as e,g as t,h as n,i as r,l as i,n as a,o,p as s,t as c,u as l}from"./gpu-dispose-gTGxGTL4.js";import{a as u,o as d}from"./gpu-ownership-DcHWbATk.js";import*as f from"three";import{Line2 as p}from"three/addons/lines/Line2.js";import{LineGeometry as m}from"three/addons/lines/LineGeometry.js";import{LineMaterial as h}from"three/addons/lines/LineMaterial.js";import{inflate as g,inflateSync as _}from"fflate";function v(){let e=globalThis.Buffer;return typeof e==`function`?e:void 0}function y(e){let t=e.replace(/[\t\n\f\r ]/g,``);return t.length%4==0&&(t=t.replace(/={1,2}$/,``)),t.length%4==1?null:t}function b(e){let r=y(e),i=r===null?null:x(r);if(i===null)throw new t(`Invalid base64 input.`,n.ENCODING_ERROR,{context:{inputLength:e.length}});return i}function x(e){let r=v();if(r){if(r.byteLength(e,`utf8`)!==e.length||e.includes(`-`)||e.includes(`_`))return null;let t=r.from(e,`base64`);return t.length===Math.floor(e.length*3/4)?t.byteOffset===0&&t.buffer.byteLength===t.length?new Uint8Array(t.buffer,0,t.length):new Uint8Array(t):null}if(typeof globalThis.atob==`function`){let t;try{t=globalThis.atob(e)}catch{return null}let n=new Uint8Array(t.length);for(let e=0;e<t.length;e++)n[e]=t.charCodeAt(e)&255;return n}throw new t(`Base64 decoding not supported in this environment.`,n.INVALID_STATE,{context:{environmentInfo:`atob or Buffer not available`}})}function S(e){return e.map(e=>{let t=e.clone(!0),n=[];e.traverse(e=>n.push(e));let r=0;return t.traverse(e=>{let t=n[r++],i=e;t.geometry&&(i.geometry=t.geometry.clone())}),t})}function C(e){e.forEach(e=>c(e,{materials:!1}))}const w={clone:S,release:C};function T(e,t){let n=t??1;return{color:new f.Color(e??`#ffffff`),transparent:n<1,opacity:n}}function E(e){let t=D(e);if(!t)return null;let n=new m;n.setPositions(t);let r=T(e.color,e.opacity),i=new h({color:r.color}),a=i;a.linewidth=e.width??2,a.transparent=r.transparent,a.opacity=r.opacity;let o=new p(n,i);return o.computeLineDistances(),o.name=e.name,o.userData={source:`compute`,id:e.id,layer:e.layer,kind:`curve`,metadata:e.metadata},o}function D(e){if(!e.points)throw new t(`Curve display item '${e.id}' has no tessellated points. It was produced by an outdated Display component — upgrade it in Grasshopper (Solution → Upgrade obsolete components) and re-save the definition.`,n.INVALID_CONFIG,{context:{itemId:e.id,kind:e.kind}});return e.points.length>=6?e.points:null}function O(e){let{position:t}=e;if(!t||typeof t.X!=`number`||!Number.isFinite(t.X)||typeof t.Y!=`number`||!Number.isFinite(t.Y)||typeof t.Z!=`number`||!Number.isFinite(t.Z))return s().warn(`Skipping point display item with missing or non-finite position (id: ${String(e.id)}).`),null;let n=new f.BufferGeometry;n.setAttribute(`position`,new f.Float32BufferAttribute([t.X,t.Y,t.Z],3));let r=new f.PointsMaterial({...T(e.color,e.opacity),size:6,sizeAttenuation:!1}),i=new f.Points(n,r);return i.name=e.name,i.userData={source:`compute`,id:e.id,layer:e.layer,kind:`point`,metadata:e.metadata},i}function k(e){if(!e||e.length===0)return[];let t=[];for(let n of e)switch(n.kind){case`curve`:{let e=E(n);e&&t.push(e);break}case`point`:{let e=O(n);e&&t.push(e);break}default:{let e=n;s().warn(`Skipping unknown display item kind: ${String(e.kind)}`);break}}return t}const A=new Uint16Array(new Uint8Array([1,0]).buffer)[0]===1;function j(e){return typeof e==`string`?b(e):e instanceof Uint8Array?e:new Uint8Array(e)}function M(e){return e.byteLength>=8&&new DataView(e.buffer,e.byteOffset,8).getUint32(0,!0)===1515605075}function N(e){return M(e)?new DataView(e.buffer,e.byteOffset,8).getUint32(4,!0):0}function P(e){let t=new DataView(e.buffer,e.byteOffset,e.byteLength).getUint32(4,!0),n=e.subarray(8),r=Math.max(n.byteLength*1032+1024,1<<20);if(t>r)throw z(`SLVZ header declares an implausible uncompressed length`,{uncompressedLen:t,deflatedBytes:n.byteLength,maxPlausibleLen:r});return{deflated:n,uncompressedLen:t}}function F(e,t){return z(`Failed to inflate SLVZ blob: ${e instanceof Error?e.message:String(e)}`,{uncompressedLen:t.uncompressedLen,deflatedBytes:t.deflated.byteLength})}function ee(e,t){if(e.byteLength!==t.uncompressedLen)throw z(`SLVZ payload inflated to a different size than the header declares.`,{declaredLen:t.uncompressedLen,actualLen:e.byteLength,deflatedBytes:t.deflated.byteLength});return e}function te(e){if(!M(e))return e;let t=P(e),n;try{n=_(t.deflated,{out:new Uint8Array(t.uncompressedLen+1)})}catch(e){throw F(e,t)}return ee(n,t)}function ne(e){return new Promise((t,n)=>{let r=P(e);try{g(r.deflated,{size:r.uncompressedLen+1},(e,i)=>{if(e)n(F(e,r));else try{t(ee(i,r))}catch(e){n(e)}})}catch(e){n(F(e,r))}})}function I(e){if(typeof TextDecoder<`u`)return new TextDecoder(`utf-8`).decode(e);if(globalThis.Buffer!==void 0)return globalThis.Buffer.from(e).toString(`utf-8`);throw new t(`No UTF-8 decoder available in this environment.`,n.INVALID_STATE)}function re(e,t,n){if(n===0)return/* @__PURE__ */ new Int16Array;if(t%2==0)return new Int16Array(e,t,n);let r=new Uint8Array(n*2);return r.set(new Uint8Array(e,t,n*2)),new Int16Array(r.buffer)}function ie(e,t,n){if(n===0)return/* @__PURE__ */ new Float32Array;if(t%4==0)return new Float32Array(e,t,n);let r=new Uint8Array(n*4);return r.set(new Uint8Array(e,t,n*4)),new Float32Array(r.buffer)}function L(e,t,n){if(n===0)return/* @__PURE__ */ new Uint16Array;if(t%2==0)return new Uint16Array(e,t,n);let r=new Uint8Array(n*2);return r.set(new Uint8Array(e,t,n*2)),new Uint16Array(r.buffer)}function ae(e,t,n){if(n===0)return/* @__PURE__ */ new Uint32Array;if(t%4==0)return new Uint32Array(e,t,n);let r=new Uint8Array(n*4);return r.set(new Uint8Array(e,t,n*4)),new Uint32Array(r.buffer)}function oe(e,t){if(e.length!==0&&!(e instanceof Uint16Array&&t>65535)){for(let n=0;n<e.length;n++)if(e[n]>=t)throw z(`Index out of range of vertexCount.`,{indexPosition:n,indexValue:e[n],vertexCount:t})}}function R(e){return e>>>1^-(e&1)}function se(e){let t=new Int16Array(e.length),n=0,r=0,i=0;for(let a=0;a<e.length;a+=3)n=n+R(e[a])<<16>>16,r=r+R(e[a+1])<<16>>16,i=i+R(e[a+2])<<16>>16,t[a]=n,t[a+1]=r,t[a+2]=i;return t}function ce(e,t){let n=new Int16Array(t*3),r=t,i=0,a=0,o=0;for(let t=0;t<r;t++)i=i+R(e[t]|e[r*3+t]<<8)<<16>>16,a=a+R(e[r+t]|e[r*4+t]<<8)<<16>>16,o=o+R(e[r*2+t]|e[r*5+t]<<8)<<16>>16,n[t*3]=i,n[t*3+1]=a,n[t*3+2]=o;return n}function le(e,t){let n=new Uint16Array(t),r=0;for(let i=0;i<t;i++)r=r+R(e[i]|e[t+i]<<8)&65535,n[i]=r;return n}function ue(e,t){let n=new Uint32Array(t),r=0;for(let i=0;i<t;i++){let a=(e[i]|e[t+i]<<8|e[t*2+i]<<16|e[t*3+i]<<24)>>>0;r=r+R(a)>>>0,n[i]=r}return n}function de(e){let t=new Uint16Array(e.length),n=0;for(let r=0;r<e.length;r++)n=n+R(e[r])&65535,t[r]=n;return t}function fe(e){let t=new Uint32Array(e.length),n=0;for(let r=0;r<e.length;r++)n=n+R(e[r])>>>0,t[r]=n;return t}function z(e,r){return new t(e,n.VALIDATION_ERROR,{context:r})}function pe(e,t,n,r,i,a=!1){if(n+36>e.byteLength)throw z(`Insufficient data to read UV chunk header.`,{expectedBytes:36,availableBytes:e.byteLength-n,offset:n});let o=t.getUint32(n,!0);n+=4;let s=t.getFloat64(n,!0);n+=8;let c=t.getFloat64(n,!0);n+=8;let l=t.getFloat64(n,!0);n+=8;let u=t.getFloat64(n,!0);n+=8;let d=r*2,f=o===1,p=d*(f?4:2);if(n+p>e.byteLength)throw z(`Insufficient data to read UV chunk.`,{expectedBytes:p,availableBytes:e.byteLength-n,offset:n,uvFormat:o,vertexCount:r});let m=e.byteOffset+n,h;if(f)h=ie(e.buffer,m,d).slice();else if(a){let t=e.subarray(n,n+p),i=r;h=new Float32Array(d);let a=0,o=0;for(let e=0;e<i;e++)a=a+R(t[e]|t[i*2+e]<<8)&65535,o=o+R(t[i+e]|t[i*3+e]<<8)&65535,h[e*2]=s+a*l,h[e*2+1]=c+o*u}else{let t=L(e.buffer,m,d);h=new Float32Array(d);let n=0,r=0;for(let e=0;e<d;e+=2)i?(n=n+R(t[e])&65535,r=r+R(t[e+1])&65535):(n=t[e],r=t[e+1]),h[e]=s+n*l,h[e+1]=c+r*u}return{uvs:h,offset:n+p}}function me(e,t,n,r){let i=n*3;if(t+i>e.byteLength)throw z(`Insufficient data to read vertex-color chunk.`,{expectedBytes:i,availableBytes:e.byteLength-t,offset:t,vertexCount:n});let a=e.subarray(t,t+i);if(!r)return a.slice();let o=new Uint8Array(i),s=0,c=0,l=0;for(let e=0;e<i;e+=3)s=s+R(a[e])&255,c=c+R(a[e+1])&255,l=l+R(a[e+2])&255,o[e]=s,o[e+1]=c,o[e+2]=l;return o}function he(e){return e.byteLength>=4&&new DataView(e.buffer,e.byteOffset,4).getUint32(0,!0)===1297501267}function ge(e){let t=_e(e),n=null,r=null,i=null,a=[];for(let{type:e,payload:o}of t)switch(e){case 1297040711:n=o;break;case 1279410516:r=te(o);break;case 1280590157:i=I(o);break;case 1381516628:a.push(o)}if(n===null)throw z(`SLVM container has no GEOM chunk.`,{chunkCount:t.length});if(r===null)throw z(`SLVM container has no TABL chunk.`,{chunkCount:t.length});let{groups:o}=ye(r),s=be(i,a);return{geometryBlob:n,metadata:{materials:s,groups:o}}}function _e(e){let t=new DataView(e.buffer,e.byteOffset,e.byteLength);if(e.byteLength<12||t.getUint32(0,!0)!==1297501267)throw z(`Not an SLVM container (bad magic).`,{byteLength:e.byteLength});let n=t.getUint32(4,!0);if(n!==3)throw z(`Unsupported SLVM version: ${n}`,{expectedVersion:3});let r=t.getUint32(8,!0),i=[],a=12;for(let n=0;n<r;n++){if(a+8>e.byteLength)throw z(`Truncated SLVM chunk header.`,{offset:a,chunkIndex:n});let r=t.getUint32(a,!0),o=t.getUint32(a+4,!0);if(a+=8,a+o>e.byteLength)throw z(`Truncated SLVM chunk payload.`,{offset:a,chunkIndex:n,byteLen:o});i.push({type:r,payload:e.subarray(a,a+o)}),a+=o+(4-o%4)%4}return i}function B(e){let t=0,n=0;for(;;){if(e.pos>=e.bytes.byteLength||n>28)throw z(`Malformed varint in SLVM table.`,{pos:e.pos});let r=e.bytes[e.pos++];if(t|=(r&127)<<n,!(r&128))return t>>>0;n+=7}}function ve(e,t,n){let r=e.bytes[e.pos++],i=Array(t);switch(r){case 0:i.fill(``);break;case 1:for(let e=0;e<t;e++)i[e]=String(e+1);break;case 2:for(let r=0;r<t;r++)i[r]=n[B(e)]??``;break;default:throw z(`Unknown SLVM string column mode: ${r}`,{pos:e.pos})}return i}function ye(e){let t={bytes:e,pos:0},n=B(t),r=B(t),i=B(t),a=n+r+i,o=B(t),s=Array(o);for(let n=0;n<o;n++){let r=B(t);s[n]=I(e.subarray(t.pos,t.pos+r)),t.pos+=r}let c=Array(n),l=Array(n);for(let e=0;e<n;e++)c[e]=B(t),l[e]=B(t);for(let e=0;e<r;e++)B(t);let u=B(t),d=[];for(let e=0;e<u;e++)d.push({materialId:B(t),meshCount:B(t)});let f=ve(t,a,s),p=ve(t,a,s),m=Array(a),h=B(t);for(let e=0;e<h;e++){let e=s[B(t)]??``,n=B(t),r=Array(n),i=0;for(let e=0;e<n;e++)i+=B(t),r[e]=i;for(let i=0;i<n;i++){let n=s[B(t)]??``,a=r[i];(m[a]??={})[e]=n}}let g=d.length>0||n===0?d:[{materialId:0,meshCount:n}],_=[],v=0,y=0,b=0;for(let e of g){let t=[];for(let r=0;r<e.meshCount&&v<n;r++,v++){let e=m[v],n=e?.id;e!==void 0&&delete e.id,t.push({id:n,name:f[v],layer:p[v],vertexCount:c[v],indexCount:l[v]*3,vertexStart:y,indexStart:b,metadata:e??{}}),y+=c[v],b+=l[v]*3}_.push({materialId:e.materialId,meshes:t})}return{groups:_}}function be(e,t){if(e===null)return[];let n;try{n=JSON.parse(e).materials??[]}catch(t){throw z(`Failed to parse SLVM materials JSON: ${t instanceof Error?t.message:String(t)}`,{byteLength:e.length})}for(let e of n)for(let n of xe){let r=e[n];if(r?.startsWith(`slvm:tex:`)){let i=t[Number(r.slice(9))];i!==void 0&&(e[n]=Se(i))}}return n}const xe=[`map`,`roughnessMap`,`normalMap`];function Se(e){let t={bytes:e,pos:0},n=B(t),r=I(e.subarray(t.pos,t.pos+n)),i=e.subarray(t.pos+n),a=``,o=32768;for(let e=0;e<i.byteLength;e+=o)a+=String.fromCharCode(...i.subarray(e,e+o));return`data:${r};base64,${btoa(a)}`}function Ce(e){let t;t=e.isFloat32?e.vertexData:e.planarByteSplit?ce(e.vertexData,e.vertexCount):e.deltaEncoded?se(e.vertexData):e.vertexData;let n;if(e.planarByteSplit){let t=e.indexData;n=e.uint16Indices?le(t,t.length/2):ue(t,t.length/4)}else n=e.deltaEncoded?e.indexData instanceof Uint16Array?de(e.indexData):fe(e.indexData):e.indexData;return oe(n,e.vertexCount),{metadata:e.metadata,flags:e.flags,vertices:t,indices:n,origin:e.origin,scale:e.scale,uvs:e.uvs,colors:e.colors}}function V(e){if(!A)throw new t(`SLVA parsing requires a little-endian host: the zero-copy geometry readers view the wire bytes in host byte order.`,n.ENVIRONMENT_ERROR);let r=j(e);if(he(r)){let e=ge(r),t=V(e.geometryBlob);return t.metadata=e.metadata,t}let i=te(r),a=new DataView(i.buffer,i.byteOffset,i.byteLength);if(i.byteLength<12)throw z(`Blob too small to contain SLVA header.`,{expectedBytes:12,availableBytes:i.byteLength});let o=0,s=a.getUint32(o,!0);if(o+=4,s!==1096174675)throw z(`Invalid SLVA magic: 0x${s.toString(16)}`,{expectedMagic:`0x41564c53`,actualMagic:`0x${s.toString(16)}`});let c=a.getUint32(o,!0);if(o+=4,c<1||c>4)throw z(`Unsupported SLVA version: ${c}`,{minSupportedVersion:1,maxSupportedVersion:4,actualVersion:c});let l=a.getUint32(o,!0);if(o+=4,o+l>i.byteLength)throw z(`Insufficient data to read metadata JSON.`,{expectedBytes:l,availableBytes:i.byteLength-o,offset:o});let u=i.subarray(o,o+l);o+=l;let d;try{d=l===0?{}:JSON.parse(I(u))}catch(e){throw z(`Failed to parse metadata JSON: ${e instanceof Error?e.message:String(e)}`,{metadataLen:l})}if(o+56>i.byteLength)throw z(`Insufficient data to read geometry header.`,{expectedBytes:56,availableBytes:i.byteLength-o,offset:o});let f=a.getUint32(o,!0);o+=4;let p=a.getFloat64(o,!0);o+=8;let m=a.getFloat64(o,!0);o+=8;let h=a.getFloat64(o,!0);o+=8;let g=a.getFloat64(o,!0);o+=8;let _=a.getFloat64(o,!0);o+=8;let v=a.getFloat64(o,!0);o+=8;let y=a.getUint32(o,!0);o+=4;let b=!!(f&1),x=!!(f&4),S=!!(f&32),C=y*3,w=C*(b?4:2);if(o+w>i.byteLength)throw z(`Insufficient data to read vertices.`,{expectedBytes:w,availableBytes:i.byteLength-o,offset:o,useFloat32:b,vertexCount:y});let T=i.byteOffset+o,E;if(E=b?ie(i.buffer,T,C):S?i.subarray(o,o+w):x?L(i.buffer,T,C):re(i.buffer,T,C),o+=w,o+4>i.byteLength)throw z(`Insufficient data to read index count.`,{expectedBytes:4,availableBytes:i.byteLength-o,offset:o});let D=a.getUint32(o,!0);o+=4;let O=!!(f&2),k=D*(O?2:4);if(o+k>i.byteLength)throw z(`Insufficient data to read indices.`,{expectedBytes:k,availableBytes:i.byteLength-o,offset:o,indexCount:D,useUint16Indices:O});let M=S?i.subarray(o,o+k):O?L(i.buffer,i.byteOffset+o,D):ae(i.buffer,i.byteOffset+o,D);o+=k;let N=null;if(f&8){let e=pe(i,a,o,y,x,S);N=e.uvs,o=e.offset}let P=null;return f&16&&(P=me(i,o,y,x)),{metadata:d,flags:f,vertexData:E,indexData:M,isFloat32:b,deltaEncoded:x,planarByteSplit:S,uint16Indices:O,vertexCount:y,origin:[p,m,h],scale:[g,_,v],uvs:N,colors:P}}function we(e,t,n){if(n.length===0)return!1;let r=e=>{let t=16;for(;t<e*2;)t*=2;return t},i=e.length/3,a=new Int32Array(i),o=new Int32Array(i),s=/* @__PURE__ */ new Int32Array,c=/* @__PURE__ */ new Int32Array,l=/* @__PURE__ */ new Int32Array,u=/* @__PURE__ */ new Int32Array,d=/* @__PURE__ */ new Int32Array,f=/* @__PURE__ */ new Int32Array,p=/* @__PURE__ */ new Int32Array,m=/* @__PURE__ */ new Int32Array,h=/* @__PURE__ */ new Int32Array,g=(e,t)=>e.length>=t?e:new Int32Array(t),_=(n,i,_)=>{if(_<12||_%3!=0)return!1;let v=2*n+1,y=v+1,b=0,x=0;for(let n=0;n<_;n++){let r=t[i+n];a[r]!==v&&(a[r]=v,x++,b=Math.max(b,Math.abs(e[r*3]),Math.abs(e[r*3+1]),Math.abs(e[r*3+2])))}if(!(b>0)||!Number.isFinite(b))return!1;let S=1/(2*2**(Math.floor(Math.log2(b))-23)),C=r(x),w=C-1;s=g(s,C),s.fill(-1,0,C),c=g(c,C),l=g(l,C),u=g(u,C);let T=(e,t,n)=>{let r=Math.imul(e,2654435761)^Math.imul(t,2246822519)^Math.imul(n,3266489917);for(r=(r^r>>>15)&w;s[r]!==-1&&(c[r]!==e||l[r]!==t||u[r]!==n);)r=r+1&w;return r},E=(e,t)=>{let n=e-t;return n>.25?t+1:n<-.25?t-1:t};d=g(d,_),f=g(f,x);let D=0;for(let n=0;n<_;n++){let r=t[i+n];if(a[r]===y){d[n]=o[r];continue}let p=e[r*3]*S,m=e[r*3+1]*S,h=e[r*3+2]*S,g=Math.round(p),_=Math.round(m),v=Math.round(h),b=E(p,g),x=E(m,_),C=E(h,v),w=-1,O=-1;for(let e=0;e<8&&w===-1;e++){if(e&1&&b===g||e&2&&x===_||e&4&&C===v)continue;let t=T(e&1?b:g,e&2?x:_,e&4?C:v);e===0&&(O=t),s[t]!==-1&&(w=s[t])}if(w===-1){let e=O;w=D++,s[e]=w,c[e]=g,l[e]=_,u[e]=v,f[w]=r}a[r]=y,o[r]=w,d[n]=w}p=g(p,D+1),p.fill(0,0,D+1);let O=0;for(let e=0;e<_;e+=3){let t=d[e],n=d[e+1],r=d[e+2];t!==n&&n!==r&&r!==t&&(p[t+1]+=1,p[n+1]+=1,p[r+1]+=1,O++)}if(O<4)return!1;for(let e=0;e<D;e++)p[e+1]+=p[e];m=g(m,p[D]),h=g(h,D),h.set(p.subarray(0,D));for(let e=0;e<_;e+=3){let t=d[e],n=d[e+1],r=d[e+2];t!==n&&n!==r&&r!==t&&(m[h[t]]=n,h[t]+=1,m[h[n]]=r,h[n]+=1,m[h[r]]=t,h[r]+=1)}for(let e=0;e<D;e++){let t=p[e],n=p[e+1];for(let r=t;r<n;r++){let n=m[r];for(let e=t;e<r;e++)if(m[e]===n)return!1;let i=!1;for(let t=p[n],r=p[n+1];t<r&&!i;t++)i=m[t]===e;if(!i)return!1}}let k=f[d[0]]*3,A=e[k],j=e[k+1],M=e[k+2],N=0;for(let t=0;t<_;t+=3){let n=f[d[t]]*3,r=f[d[t+1]]*3,i=f[d[t+2]]*3,a=e[n]-A,o=e[n+1]-j,s=e[n+2]-M,c=e[r]-A,l=e[r+1]-j,u=e[r+2]-M,p=e[i]-A,m=e[i+1]-j,h=e[i+2]-M;N+=a*(l*h-u*m)+o*(u*p-c*h)+s*(c*m-l*p)}return N>0},v=0;for(let e=0;e<n.length;e++){let t=n[e];if(!_(e,v,t))return!1;v+=t}return!0}function Te(e,t,n,r){let i=Math.cos(30*Math.PI/180),a=e.length/3,o=t.length,s=o/3;{let i=new Uint32Array(e.buffer,e.byteOffset,e.length),s=n?new Uint32Array(n.buffer,n.byteOffset,n.length):null,c=(e,t)=>e[t]===2147483648?0:e[t],l=16;for(;l<a*2;)l*=2;let u=l-1,d=new Int32Array(l).fill(-1),f=new Uint32Array(a),p=!1;for(let e=0;e<a;e++){let t=c(i,e*3),n=c(i,e*3+1),a=c(i,e*3+2),o=Math.imul(t,2654435761)^Math.imul(n,2246822519)^Math.imul(a,3266489917);for(o=(o^o>>>15)&u;;){let l=d[o];if(l===-1){d[o]=e,f[e]=e;break}if(c(i,l*3)===t&&c(i,l*3+1)===n&&c(i,l*3+2)===a&&(!r||r[l*3]===r[e*3]&&r[l*3+1]===r[e*3+1]&&r[l*3+2]===r[e*3+2])&&(!s||s[l*2]===s[e*2]&&s[l*2+1]===s[e*2+1])){f[e]=l,p=!0;break}o=o+1&u}}if(p)for(let e=0;e<o;e++)t[e]=f[t[e]]}let c=new Float32Array(s*3),l=new Float32Array(s);for(let n=0;n<s;n++){let r=t[n*3]*3,i=t[n*3+1]*3,a=t[n*3+2]*3,o=e[i]-e[r],s=e[i+1]-e[r+1],u=e[i+2]-e[r+2],d=e[a]-e[r],f=e[a+1]-e[r+1],p=e[a+2]-e[r+2],m=s*p-u*f,h=u*d-o*p,g=o*f-s*d,_=Math.sqrt(m*m+h*h+g*g);_!==0&&(c[n*3]=m/_,c[n*3+1]=h/_,c[n*3+2]=g/_,l[n]=_)}let u=new Uint32Array(a+1);for(let e=0;e<o;e++)u[t[e]+1]+=1;for(let e=0;e<a;e++)u[e+1]+=u[e];let d=new Uint32Array(o),f=u.slice(0,a);for(let e=0;e<o;e++){let n=t[e];d[f[n]]=e,f[n]+=1}let p=0;for(let e=0;e<a;e++)p=Math.max(p,u[e+1]-u[e]);let m=new Uint32Array(o),h=new Uint32Array(a),g=new Float32Array(Math.max(1,p)*3);for(let e=0;e<a;e++){let t=0;for(let n=u[e];n<u[e+1];n++){let e=d[n],r=Math.floor(e/3)*3,a=c[r],o=c[r+1],s=c[r+2],l=-1;if(a===0&&o===0&&s===0)l=0;else for(let e=0;e<t;e++)if(g[e*3]*a+g[e*3+1]*o+g[e*3+2]*s>=i){l=e;break}l===-1&&(l=t,g[t*3]=a,g[t*3+1]=o,g[t*3+2]=s,t++),m[e]=l}h[e]=Math.max(1,t)}let _=new Uint32Array(a+1);for(let e=0;e<a;e++)_[e+1]=_[e]+h[e]-1;let v=_[a],y=a+v,b=e,x=n,S=r;if(v>0){b=new Float32Array(y*3),b.set(e),x=n?new Float32Array(y*2):null,x&&n&&x.set(n),S=r?new Uint8Array(y*3):null,S&&r&&S.set(r);for(let t=0;t<a;t++)for(let i=1;i<h[t];i++){let o=a+_[t]+i-1;b[o*3]=e[t*3],b[o*3+1]=e[t*3+1],b[o*3+2]=e[t*3+2],x&&n&&(x[o*2]=n[t*2],x[o*2+1]=n[t*2+1]),S&&r&&(S[o*3]=r[t*3],S[o*3+1]=r[t*3+1],S[o*3+2]=r[t*3+2])}}let C=new Float32Array(y*3);for(let e=0;e<s;e++){let n=l[e],r=c[e*3]*n,i=c[e*3+1]*n,o=c[e*3+2]*n;for(let n=e*3;n<e*3+3;n++){let e=t[n],s=m[n];s!==0&&(e=a+_[e]+s-1,t[n]=e),C[e*3]+=r,C[e*3+1]+=i,C[e*3+2]+=o}}for(let e=0;e<C.length;e+=3){let t=C[e],n=C[e+1],r=C[e+2],i=Math.sqrt(t*t+n*n+r*r)||1;C[e]=t/i,C[e+1]=n/i,C[e+2]=r/i}return{positions:b,normals:C,indices:t,uvs:x,colors:S}}function Ee(e,t,n,r){let i=e.length/3,a=new Float32Array(i*3),o=new Float32Array(i*3);for(let t=0;t<n.length;t+=3){let i=n[t],s=n[t+1],c=n[t+2],l=e[s*3]-e[i*3],u=e[s*3+1]-e[i*3+1],d=e[s*3+2]-e[i*3+2],f=e[c*3]-e[i*3],p=e[c*3+1]-e[i*3+1],m=e[c*3+2]-e[i*3+2],h=u*m-d*p,g=d*f-l*m,_=l*p-u*f,v=Math.sqrt(h*h+g*g+_*_);if(v===0)continue;let y=(p*_-m*g)/v,b=(m*h-f*_)/v,x=(f*g-p*h)/v,S=(g*d-_*u)/v,C=(_*l-h*d)/v,w=(h*u-g*l)/v,T=r[s*2]-r[i*2],E=r[c*2]-r[i*2],D=r[s*2+1]-r[i*2+1],O=r[c*2+1]-r[i*2+1],k=T*y+E*S,A=T*b+E*C,j=T*x+E*w,M=D*y+O*S,N=D*b+O*C,P=D*x+O*w;for(let e=0;e<3;e++){let r=n[t+e];a[r*3]+=k,a[r*3+1]+=A,a[r*3+2]+=j,o[r*3]+=M,o[r*3+1]+=N,o[r*3+2]+=P}}let s=new Float32Array(i*4);for(let e=0;e<i;e++){let n=t[e*3],r=t[e*3+1],i=t[e*3+2],c=a[e*3]*n+a[e*3+1]*r+a[e*3+2]*i,l=a[e*3]-c*n,u=a[e*3+1]-c*r,d=a[e*3+2]-c*i,f=Math.sqrt(l*l+u*u+d*d);if(!(f>1e-12)){let e=+(Math.abs(n)<.9),t=1-e;l=t*i,u=-e*i,d=e*r-t*n,f=Math.sqrt(l*l+u*u+d*d)||1}l/=f,u/=f,d/=f;let p=r*d-i*u,m=i*l-n*d,h=n*u-r*l,g=p*o[e*3]+m*o[e*3+1]+h*o[e*3+2];s[e*4]=l,s[e*4+1]=u,s[e*4+2]=d,s[e*4+3]=g<0?-1:1}return s}function H(e,t){let n=we(e.positions,e.indices,t),r=Te(e.positions,e.indices,e.uvs,e.colors),i=r.uvs?Ee(r.positions,r.normals,r.indices,r.uvs):null;return{...r,closed:n,tangents:i}}function De(e){let{isFloat32:t,deltaEncoded:n,planarByteSplit:r,uint16Indices:i,origin:a,scale:o,uvs:s,colors:c,jobs:l}=e,u=e=>e>>>1^-(e&1),d;if(t)d=e.vertexData;else{let t;if(r){let n=e.vertexData,r=n.length/6;t=new Int16Array(r*3);let i=0,a=0,o=0;for(let e=0;e<r;e++)i=i+u(n[e]|n[r*3+e]<<8)<<16>>16,a=a+u(n[r+e]|n[r*4+e]<<8)<<16>>16,o=o+u(n[r*2+e]|n[r*5+e]<<8)<<16>>16,t[e*3]=i,t[e*3+1]=a,t[e*3+2]=o}else if(n){let n=e.vertexData;t=new Int16Array(n.length);let r=0,i=0,a=0;for(let e=0;e<n.length;e+=3)r=r+u(n[e])<<16>>16,i=i+u(n[e+1])<<16>>16,a=a+u(n[e+2])<<16>>16,t[e]=r,t[e+1]=i,t[e+2]=a}else t=e.vertexData;d=new Float32Array(t.length);let i=a[0],s=a[1],c=a[2],l=o[0],f=o[1],p=o[2];for(let e=0;e<t.length;e+=3)d[e]=i+(t[e]+32767)*l,d[e+1]=s+(t[e+1]+32767)*f,d[e+2]=c+(t[e+2]+32767)*p}let f;if(r){let t=e.indexData;if(i){let e=t.length/2,n=new Uint16Array(e),r=0;for(let i=0;i<e;i++)r=r+u(t[i]|t[e+i]<<8)&65535,n[i]=r;f=n}else{let e=t.length/4,n=new Uint32Array(e),r=0;for(let i=0;i<e;i++){let a=(t[i]|t[e+i]<<8|t[e*2+i]<<16|t[e*3+i]<<24)>>>0;r=r+u(a)>>>0,n[i]=r}f=n}}else if(n){let t=e.indexData;if(t instanceof Uint16Array){let e=new Uint16Array(t.length),n=0;for(let r=0;r<t.length;r++)n=n+u(t[r])&65535,e[r]=n;f=e}else{let e=new Uint32Array(t.length),n=0;for(let r=0;r<t.length;r++)n=n+u(t[r])>>>0,e[r]=n;f=e}}else f=e.indexData;let p=d.length/3;for(let e=0;e<f.length;e++)if(f[e]>=p)throw Error(`Index ${f[e]} out of range of vertexCount ${p}`);let m=[];for(let e of l){let t=0,n=0;for(let r of e.windows)t+=r.vertexCount,n+=r.indexCount;let r=new Float32Array(t*3),i=new Uint32Array(n),a=s?new Float32Array(t*2):null,o=c?new Uint8Array(t*3):null,l=0,u=0;for(let t of e.windows){let e=t.vertexStart*3;r.set(d.subarray(e,e+t.vertexCount*3),l*3),a&&s&&a.set(s.subarray(t.vertexStart*2,(t.vertexStart+t.vertexCount)*2),l*2),o&&c&&o.set(c.subarray(e,e+t.vertexCount*3),l*3);let n=t.vertexStart,p=t.vertexStart+t.vertexCount,m=l-t.vertexStart;for(let e=0;e<t.indexCount;e++){let r=f[t.indexStart+e];if(r<n||r>=p)throw Error(`Index ${r} outside vertex window [${n}, ${p})`);i[u+e]=r+m}l+=t.vertexCount,u+=t.indexCount}m.push({positions:r,indices:i,uvs:a,colors:o})}return m}function Oe(){return[`const assemble = ${De.toString()};`,`const isClosedSolid = ${we.toString()};`,`const splitCreases = ${Te.toString()};`,`const grainTangents = ${Ee.toString()};`,`self.onmessage = (event) => {`,` const { id, input } = event.data;`,` try {`,` const geometries = assemble(input).map((g, i) => {`,` const counts = input.jobs[i].windows.map((w) => w.indexCount);`,` const closed = isClosedSolid(g.positions, g.indices, counts);`,` const c = splitCreases(g.positions, g.indices, g.uvs, g.colors);`,` const tangents = c.uvs ? grainTangents(c.positions, c.normals, c.indices, c.uvs) : null;`,` return { ...c, closed, tangents };`,` });`,` const transfer = [];`,` for (const g of geometries) {`,` transfer.push(g.positions.buffer, g.normals.buffer, g.indices.buffer);`,` if (g.uvs) transfer.push(g.uvs.buffer);`,` if (g.tangents) transfer.push(g.tangents.buffer);`,` if (g.colors) transfer.push(g.colors.buffer);`,` }`,` self.postMessage({ id, geometries }, transfer);`,` } catch (error) {`,` self.postMessage({ id, error: String((error && error.message) || error) });`,` }`,`};`].join(`
2
- `)}let U;const W=/* @__PURE__ */ new Map;let ke=1;function Ae(){if(U!==void 0)return U;if(typeof Worker>`u`||typeof Blob>`u`||typeof URL>`u`||typeof URL.createObjectURL!=`function`)return U=null,null;try{let e=URL.createObjectURL(new Blob([Oe()],{type:`text/javascript`})),t=new Worker(e);t.onmessage=e=>{let{id:t,geometries:n,error:r}=e.data,i=W.get(t);i&&(W.delete(t),n?i.resolve(n):i.reject(Error(r??`mesh assembly failed in worker`)))},t.onerror=()=>{for(let e of W.values())e.reject(/* @__PURE__ */ Error(`mesh assembly worker crashed`));W.clear(),t.terminate(),U=null},U=t}catch{U=null}return U}function je(e,t,n){return new Promise((r,i)=>{let a=ke++;W.set(a,{resolve:r,reject:i}),e.postMessage({id:a,input:t},n)})}let Me=1;function Ne(e){Me=Math.max(1,e)}a(Ne);function G(e,t,n,r){typeof document<`u`&&new f.TextureLoader().load(t,t=>{if(t.colorSpace=n===`map`?f.SRGBColorSpace:f.NoColorSpace,t.anisotropy=Me,r){let e=t.image,n=e?.width&&e.height?e.height/e.width:1;t.wrapS=f.RepeatWrapping,t.wrapT=f.RepeatWrapping,t.repeat.set(1/r,1/(r*n))}e[n]=t,e.needsUpdate=!0},void 0,e=>{s().warn(`Failed to load material ${n} ${t}:`,e)})}const Pe={brushed:{fibres:[{pitch:.04,length:[2,12],depth:.3},{pitch:.25,length:[5,40],depth:.18}],bands:[]},rolled:{fibres:[{pitch:.3,length:[80,600],depth:.07}],bands:[{width:20,length:1500,depth:.05}]}},Fe=.35,K=e=>e.toFixed(4);function Ie(e,t){let n=Pe[e];if(!n||!(t>0))return null;let r=n.fibres.map((e,t)=>`selvaFibres( vUv, footprint, ${K(e.pitch)}, vec2( ${K(e.length[0])}, ${K(e.length[1])} ), ${K(e.depth)}, ${K(t*31+3)} )`).join(` + `)||`0.0`,i=n.bands.map(e=>`selvaBand( vUv, footprint, vec2( ${K(e.length)}, ${K(e.width)} ), ${K(e.depth)} )`).join(` + `)||`0.0`;return{key:`finish:${e}:${K(t)}`,patch:e=>{e.fragmentShader=e.fragmentShader.replace(`#include <common>`,`#include <common>
1
+ import{a as e,g as t,h as n,i as r,l as i,n as a,o,p as s,t as c,u as l}from"./gpu-dispose-gTGxGTL4.js";import{a as u,o as d}from"./gpu-ownership-DcHWbATk.js";import*as f from"three";import{Line2 as p}from"three/addons/lines/Line2.js";import{LineGeometry as m}from"three/addons/lines/LineGeometry.js";import{LineMaterial as h}from"three/addons/lines/LineMaterial.js";import{inflate as g,inflateSync as _}from"fflate";function v(){let e=globalThis.Buffer;return typeof e==`function`?e:void 0}function y(e){let t=e.replace(/[\t\n\f\r ]/g,``);return t.length%4==0&&(t=t.replace(/={1,2}$/,``)),t.length%4==1?null:t}function b(e){let r=y(e),i=r===null?null:x(r);if(i===null)throw new t(`Invalid base64 input.`,n.ENCODING_ERROR,{context:{inputLength:e.length}});return i}function x(e){let r=v();if(r){if(r.byteLength(e,`utf8`)!==e.length||e.includes(`-`)||e.includes(`_`))return null;let t=r.from(e,`base64`);return t.length===Math.floor(e.length*3/4)?t.byteOffset===0&&t.buffer.byteLength===t.length?new Uint8Array(t.buffer,0,t.length):new Uint8Array(t):null}if(typeof globalThis.atob==`function`){let t;try{t=globalThis.atob(e)}catch{return null}let n=new Uint8Array(t.length);for(let e=0;e<t.length;e++)n[e]=t.charCodeAt(e)&255;return n}throw new t(`Base64 decoding not supported in this environment.`,n.INVALID_STATE,{context:{environmentInfo:`atob or Buffer not available`}})}function S(e){return e.map(e=>{let t=e.clone(!0),n=[];e.traverse(e=>n.push(e));let r=0;return t.traverse(e=>{let t=n[r++],i=e;t.geometry&&(i.geometry=t.geometry.clone())}),t})}function C(e){e.forEach(e=>c(e,{materials:!1}))}const w={clone:S,release:C};function T(e,t){let n=t??1;return{color:new f.Color(e??`#ffffff`),transparent:n<1,opacity:n}}function E(e){let t=D(e);if(!t)return null;let n=new m;n.setPositions(t);let r=T(e.color,e.opacity),i=new h({color:r.color}),a=i;a.linewidth=e.width??2,a.transparent=r.transparent,a.opacity=r.opacity;let o=new p(n,i);return o.computeLineDistances(),o.name=e.name,o.userData={source:`compute`,id:e.id,layer:e.layer,kind:`curve`,metadata:e.metadata},o}function D(e){if(!e.points)throw new t(`Curve display item '${e.id}' has no tessellated points. It was produced by an outdated Display component — upgrade it in Grasshopper (Solution → Upgrade obsolete components) and re-save the definition.`,n.INVALID_CONFIG,{context:{itemId:e.id,kind:e.kind}});return e.points.length>=6?e.points:null}function O(e){let{position:t}=e;if(!t||typeof t.X!=`number`||!Number.isFinite(t.X)||typeof t.Y!=`number`||!Number.isFinite(t.Y)||typeof t.Z!=`number`||!Number.isFinite(t.Z))return s().warn(`Skipping point display item with missing or non-finite position (id: ${String(e.id)}).`),null;let n=new f.BufferGeometry;n.setAttribute(`position`,new f.Float32BufferAttribute([t.X,t.Y,t.Z],3));let r=new f.PointsMaterial({...T(e.color,e.opacity),size:6,sizeAttenuation:!1}),i=new f.Points(n,r);return i.name=e.name,i.userData={source:`compute`,id:e.id,layer:e.layer,kind:`point`,metadata:e.metadata},i}function k(e){if(!e||e.length===0)return[];let t=[];for(let n of e)switch(n.kind){case`curve`:{let e=E(n);e&&t.push(e);break}case`point`:{let e=O(n);e&&t.push(e);break}default:{let e=n;s().warn(`Skipping unknown display item kind: ${String(e.kind)}`);break}}return t}const A=new Uint16Array(new Uint8Array([1,0]).buffer)[0]===1;function j(e){return typeof e==`string`?b(e):e instanceof Uint8Array?e:new Uint8Array(e)}function M(e){return e.byteLength>=8&&new DataView(e.buffer,e.byteOffset,8).getUint32(0,!0)===1515605075}function N(e){return M(e)?new DataView(e.buffer,e.byteOffset,8).getUint32(4,!0):0}function P(e){let t=new DataView(e.buffer,e.byteOffset,e.byteLength).getUint32(4,!0),n=e.subarray(8),r=Math.max(n.byteLength*1032+1024,1<<20);if(t>r)throw B(`SLVZ header declares an implausible uncompressed length`,{uncompressedLen:t,deflatedBytes:n.byteLength,maxPlausibleLen:r});return{deflated:n,uncompressedLen:t}}function F(e,t){return B(`Failed to inflate SLVZ blob: ${e instanceof Error?e.message:String(e)}`,{uncompressedLen:t.uncompressedLen,deflatedBytes:t.deflated.byteLength})}function ee(e,t){if(e.byteLength!==t.uncompressedLen)throw B(`SLVZ payload inflated to a different size than the header declares.`,{declaredLen:t.uncompressedLen,actualLen:e.byteLength,deflatedBytes:t.deflated.byteLength});return e}function I(e){if(!M(e))return e;let t=P(e),n;try{n=_(t.deflated,{out:new Uint8Array(t.uncompressedLen+1)})}catch(e){throw F(e,t)}return ee(n,t)}function te(e){return new Promise((t,n)=>{let r=P(e);try{g(r.deflated,{size:r.uncompressedLen+1},(e,i)=>{if(e)n(F(e,r));else try{t(ee(i,r))}catch(e){n(e)}})}catch(e){n(F(e,r))}})}function L(e){if(typeof TextDecoder<`u`)return new TextDecoder(`utf-8`).decode(e);if(globalThis.Buffer!==void 0)return globalThis.Buffer.from(e).toString(`utf-8`);throw new t(`No UTF-8 decoder available in this environment.`,n.INVALID_STATE)}function ne(e,t,n){if(n===0)return/* @__PURE__ */ new Int16Array;if(t%2==0)return new Int16Array(e,t,n);let r=new Uint8Array(n*2);return r.set(new Uint8Array(e,t,n*2)),new Int16Array(r.buffer)}function re(e,t,n){if(n===0)return/* @__PURE__ */ new Float32Array;if(t%4==0)return new Float32Array(e,t,n);let r=new Uint8Array(n*4);return r.set(new Uint8Array(e,t,n*4)),new Float32Array(r.buffer)}function R(e,t,n){if(n===0)return/* @__PURE__ */ new Uint16Array;if(t%2==0)return new Uint16Array(e,t,n);let r=new Uint8Array(n*2);return r.set(new Uint8Array(e,t,n*2)),new Uint16Array(r.buffer)}function ie(e,t,n){if(n===0)return/* @__PURE__ */ new Uint32Array;if(t%4==0)return new Uint32Array(e,t,n);let r=new Uint8Array(n*4);return r.set(new Uint8Array(e,t,n*4)),new Uint32Array(r.buffer)}function ae(e,t){if(e.length!==0&&!(e instanceof Uint16Array&&t>65535)){for(let n=0;n<e.length;n++)if(e[n]>=t)throw B(`Index out of range of vertexCount.`,{indexPosition:n,indexValue:e[n],vertexCount:t})}}function z(e){return e>>>1^-(e&1)}function oe(e){let t=new Int16Array(e.length),n=0,r=0,i=0;for(let a=0;a<e.length;a+=3)n=n+z(e[a])<<16>>16,r=r+z(e[a+1])<<16>>16,i=i+z(e[a+2])<<16>>16,t[a]=n,t[a+1]=r,t[a+2]=i;return t}function se(e,t){let n=new Int16Array(t*3),r=t,i=0,a=0,o=0;for(let t=0;t<r;t++)i=i+z(e[t]|e[r*3+t]<<8)<<16>>16,a=a+z(e[r+t]|e[r*4+t]<<8)<<16>>16,o=o+z(e[r*2+t]|e[r*5+t]<<8)<<16>>16,n[t*3]=i,n[t*3+1]=a,n[t*3+2]=o;return n}function ce(e,t){let n=new Uint16Array(t),r=0;for(let i=0;i<t;i++)r=r+z(e[i]|e[t+i]<<8)&65535,n[i]=r;return n}function le(e,t){let n=new Uint32Array(t),r=0;for(let i=0;i<t;i++){let a=(e[i]|e[t+i]<<8|e[t*2+i]<<16|e[t*3+i]<<24)>>>0;r=r+z(a)>>>0,n[i]=r}return n}function ue(e){let t=new Uint16Array(e.length),n=0;for(let r=0;r<e.length;r++)n=n+z(e[r])&65535,t[r]=n;return t}function de(e){let t=new Uint32Array(e.length),n=0;for(let r=0;r<e.length;r++)n=n+z(e[r])>>>0,t[r]=n;return t}function B(e,r){return new t(e,n.VALIDATION_ERROR,{context:r})}function fe(e,t,n,r,i,a=!1){if(n+36>e.byteLength)throw B(`Insufficient data to read UV chunk header.`,{expectedBytes:36,availableBytes:e.byteLength-n,offset:n});let o=t.getUint32(n,!0);n+=4;let s=t.getFloat64(n,!0);n+=8;let c=t.getFloat64(n,!0);n+=8;let l=t.getFloat64(n,!0);n+=8;let u=t.getFloat64(n,!0);n+=8;let d=r*2,f=o===1,p=d*(f?4:2);if(n+p>e.byteLength)throw B(`Insufficient data to read UV chunk.`,{expectedBytes:p,availableBytes:e.byteLength-n,offset:n,uvFormat:o,vertexCount:r});let m=e.byteOffset+n,h;if(f)h=re(e.buffer,m,d).slice();else if(a){let t=e.subarray(n,n+p),i=r;h=new Float32Array(d);let a=0,o=0;for(let e=0;e<i;e++)a=a+z(t[e]|t[i*2+e]<<8)&65535,o=o+z(t[i+e]|t[i*3+e]<<8)&65535,h[e*2]=s+a*l,h[e*2+1]=c+o*u}else{let t=R(e.buffer,m,d);h=new Float32Array(d);let n=0,r=0;for(let e=0;e<d;e+=2)i?(n=n+z(t[e])&65535,r=r+z(t[e+1])&65535):(n=t[e],r=t[e+1]),h[e]=s+n*l,h[e+1]=c+r*u}return{uvs:h,offset:n+p}}function pe(e,t,n,r){let i=n*3;if(t+i>e.byteLength)throw B(`Insufficient data to read vertex-color chunk.`,{expectedBytes:i,availableBytes:e.byteLength-t,offset:t,vertexCount:n});let a=e.subarray(t,t+i);if(!r)return a.slice();let o=new Uint8Array(i),s=0,c=0,l=0;for(let e=0;e<i;e+=3)s=s+z(a[e])&255,c=c+z(a[e+1])&255,l=l+z(a[e+2])&255,o[e]=s,o[e+1]=c,o[e+2]=l;return o}function me(e){return e.byteLength>=4&&new DataView(e.buffer,e.byteOffset,4).getUint32(0,!0)===1297501267}function he(e){let t=ge(e),n=null,r=null,i=null,a=null,o=[];for(let{type:e,payload:s}of t)switch(e){case 1297040711:n=s;break;case 1279410516:r=I(s);break;case 1280590157:i=L(s);break;case 1381516628:o.push(s);break;case 1146309699:a=I(s)}if(n===null)throw B(`SLVM container has no GEOM chunk.`,{chunkCount:t.length});if(r===null)throw B(`SLVM container has no TABL chunk.`,{chunkCount:t.length});let{groups:s}=ve(r,a),c=ye(i,o);return{geometryBlob:n,metadata:{materials:c,groups:s}}}function ge(e){let t=new DataView(e.buffer,e.byteOffset,e.byteLength);if(e.byteLength<12||t.getUint32(0,!0)!==1297501267)throw B(`Not an SLVM container (bad magic).`,{byteLength:e.byteLength});let n=t.getUint32(4,!0);if(n!==3)throw B(`Unsupported SLVM version: ${n}`,{expectedVersion:3});let r=t.getUint32(8,!0),i=[],a=12;for(let n=0;n<r;n++){if(a+8>e.byteLength)throw B(`Truncated SLVM chunk header.`,{offset:a,chunkIndex:n});let r=t.getUint32(a,!0),o=t.getUint32(a+4,!0);if(a+=8,a+o>e.byteLength)throw B(`Truncated SLVM chunk payload.`,{offset:a,chunkIndex:n,byteLen:o});i.push({type:r,payload:e.subarray(a,a+o)}),a+=o+(4-o%4)%4}return i}function V(e){let t=0,n=0;for(;;){if(e.pos>=e.bytes.byteLength||n>28)throw B(`Malformed varint in SLVM table.`,{pos:e.pos});let r=e.bytes[e.pos++];if(t|=(r&127)<<n,!(r&128))return t>>>0;n+=7}}function _e(e,t,n){let r=e.bytes[e.pos++],i=Array(t);switch(r){case 0:i.fill(``);break;case 1:for(let e=0;e<t;e++)i[e]=String(e+1);break;case 2:for(let r=0;r<t;r++)i[r]=n[V(e)]??``;break;default:throw B(`Unknown SLVM string column mode: ${r}`,{pos:e.pos})}return i}function ve(e,t){let n={bytes:e,pos:0},r=V(n),i=V(n),a=V(n),o=r+i+a,s=V(n),c=Array(s);for(let t=0;t<s;t++){let r=V(n);c[t]=L(e.subarray(n.pos,n.pos+r)),n.pos+=r}let l=Array(r),u=Array(r);for(let e=0;e<r;e++)l[e]=V(n),u[e]=V(n);for(let e=0;e<i;e++)V(n);let d=V(n),f=[];for(let e=0;e<d;e++)f.push({materialId:V(n),meshCount:V(n)});let p=_e(n,o,c),m=_e(n,o,c),h=Array(o),g=V(n);for(let e=0;e<g;e++){let e=c[V(n)]??``,t=V(n),r=Array(t),i=0;for(let e=0;e<t;e++)i+=V(n),r[e]=i;for(let i=0;i<t;i++){let t=c[V(n)]??``,a=r[i];(h[a]??={})[e]=t}}let _=f.length>0||r===0?f:[{materialId:0,meshCount:r}],v=[],y=0,b=0,x=0;for(let e of _){let n=[];for(let i=0;i<e.meshCount&&y<r;i++,y++){let e=h[y],r=e?.id;e!==void 0&&delete e.id;let i={id:r,name:p[y],layer:m[y],vertexCount:l[y],indexCount:u[y]*3,vertexStart:b,indexStart:x,metadata:e??{}},a=t?.[y];(a===1||a===2)&&(i.closed=a===2),n.push(i),b+=l[y],x+=u[y]*3}v.push({materialId:e.materialId,meshes:n})}return{groups:v}}function ye(e,t){if(e===null)return[];let n;try{n=JSON.parse(e).materials??[]}catch(t){throw B(`Failed to parse SLVM materials JSON: ${t instanceof Error?t.message:String(t)}`,{byteLength:e.length})}for(let e of n)for(let n of be){let r=e[n];if(r?.startsWith(`slvm:tex:`)){let i=t[Number(r.slice(9))];i!==void 0&&(e[n]=xe(i))}}return n}const be=[`map`,`roughnessMap`,`normalMap`];function xe(e){let t={bytes:e,pos:0},n=V(t),r=L(e.subarray(t.pos,t.pos+n)),i=e.subarray(t.pos+n),a=``,o=32768;for(let e=0;e<i.byteLength;e+=o)a+=String.fromCharCode(...i.subarray(e,e+o));return`data:${r};base64,${btoa(a)}`}function Se(e){let t;t=e.isFloat32?e.vertexData:e.planarByteSplit?se(e.vertexData,e.vertexCount):e.deltaEncoded?oe(e.vertexData):e.vertexData;let n;if(e.planarByteSplit){let t=e.indexData;n=e.uint16Indices?ce(t,t.length/2):le(t,t.length/4)}else n=e.deltaEncoded?e.indexData instanceof Uint16Array?ue(e.indexData):de(e.indexData):e.indexData;return ae(n,e.vertexCount),{metadata:e.metadata,flags:e.flags,vertices:t,indices:n,origin:e.origin,scale:e.scale,uvs:e.uvs,colors:e.colors}}function H(e){if(!A)throw new t(`SLVA parsing requires a little-endian host: the zero-copy geometry readers view the wire bytes in host byte order.`,n.ENVIRONMENT_ERROR);let r=j(e);if(me(r)){let e=he(r),t=H(e.geometryBlob);return t.metadata=e.metadata,t}let i=I(r),a=new DataView(i.buffer,i.byteOffset,i.byteLength);if(i.byteLength<12)throw B(`Blob too small to contain SLVA header.`,{expectedBytes:12,availableBytes:i.byteLength});let o=0,s=a.getUint32(o,!0);if(o+=4,s!==1096174675)throw B(`Invalid SLVA magic: 0x${s.toString(16)}`,{expectedMagic:`0x41564c53`,actualMagic:`0x${s.toString(16)}`});let c=a.getUint32(o,!0);if(o+=4,c<1||c>4)throw B(`Unsupported SLVA version: ${c}`,{minSupportedVersion:1,maxSupportedVersion:4,actualVersion:c});let l=a.getUint32(o,!0);if(o+=4,o+l>i.byteLength)throw B(`Insufficient data to read metadata JSON.`,{expectedBytes:l,availableBytes:i.byteLength-o,offset:o});let u=i.subarray(o,o+l);o+=l;let d;try{d=l===0?{}:JSON.parse(L(u))}catch(e){throw B(`Failed to parse metadata JSON: ${e instanceof Error?e.message:String(e)}`,{metadataLen:l})}if(o+56>i.byteLength)throw B(`Insufficient data to read geometry header.`,{expectedBytes:56,availableBytes:i.byteLength-o,offset:o});let f=a.getUint32(o,!0);o+=4;let p=a.getFloat64(o,!0);o+=8;let m=a.getFloat64(o,!0);o+=8;let h=a.getFloat64(o,!0);o+=8;let g=a.getFloat64(o,!0);o+=8;let _=a.getFloat64(o,!0);o+=8;let v=a.getFloat64(o,!0);o+=8;let y=a.getUint32(o,!0);o+=4;let b=!!(f&1),x=!!(f&4),S=!!(f&32),C=y*3,w=C*(b?4:2);if(o+w>i.byteLength)throw B(`Insufficient data to read vertices.`,{expectedBytes:w,availableBytes:i.byteLength-o,offset:o,useFloat32:b,vertexCount:y});let T=i.byteOffset+o,E;if(E=b?re(i.buffer,T,C):S?i.subarray(o,o+w):x?R(i.buffer,T,C):ne(i.buffer,T,C),o+=w,o+4>i.byteLength)throw B(`Insufficient data to read index count.`,{expectedBytes:4,availableBytes:i.byteLength-o,offset:o});let D=a.getUint32(o,!0);o+=4;let O=!!(f&2),k=D*(O?2:4);if(o+k>i.byteLength)throw B(`Insufficient data to read indices.`,{expectedBytes:k,availableBytes:i.byteLength-o,offset:o,indexCount:D,useUint16Indices:O});let M=S?i.subarray(o,o+k):O?R(i.buffer,i.byteOffset+o,D):ie(i.buffer,i.byteOffset+o,D);o+=k;let N=null;if(f&8){let e=fe(i,a,o,y,x,S);N=e.uvs,o=e.offset}let P=null;return f&16&&(P=pe(i,o,y,x)),{metadata:d,flags:f,vertexData:E,indexData:M,isFloat32:b,deltaEncoded:x,planarByteSplit:S,uint16Indices:O,vertexCount:y,origin:[p,m,h],scale:[g,_,v],uvs:N,colors:P}}function Ce(e,t,n){if(n.length===0)return!1;let r=e=>{let t=16;for(;t<e*2;)t*=2;return t},i=e.length/3,a=new Int32Array(i),o=new Int32Array(i),s=/* @__PURE__ */ new Int32Array,c=/* @__PURE__ */ new Int32Array,l=/* @__PURE__ */ new Int32Array,u=/* @__PURE__ */ new Int32Array,d=/* @__PURE__ */ new Int32Array,f=/* @__PURE__ */ new Int32Array,p=/* @__PURE__ */ new Int32Array,m=/* @__PURE__ */ new Int32Array,h=/* @__PURE__ */ new Int32Array,g=(e,t)=>e.length>=t?e:new Int32Array(t),_=(n,i,_)=>{if(_<12||_%3!=0)return!1;let v=2*n+1,y=v+1,b=0,x=0;for(let n=0;n<_;n++){let r=t[i+n];a[r]!==v&&(a[r]=v,x++,b=Math.max(b,Math.abs(e[r*3]),Math.abs(e[r*3+1]),Math.abs(e[r*3+2])))}if(!(b>0)||!Number.isFinite(b))return!1;let S=1/(2*2**(Math.floor(Math.log2(b))-23)),C=r(x),w=C-1;s=g(s,C),s.fill(-1,0,C),c=g(c,C),l=g(l,C),u=g(u,C);let T=(e,t,n)=>{let r=Math.imul(e,2654435761)^Math.imul(t,2246822519)^Math.imul(n,3266489917);for(r=(r^r>>>15)&w;s[r]!==-1&&(c[r]!==e||l[r]!==t||u[r]!==n);)r=r+1&w;return r},E=(e,t)=>{let n=e-t;return n>.25?t+1:n<-.25?t-1:t};d=g(d,_),f=g(f,x);let D=0;for(let n=0;n<_;n++){let r=t[i+n];if(a[r]===y){d[n]=o[r];continue}let p=e[r*3]*S,m=e[r*3+1]*S,h=e[r*3+2]*S,g=Math.round(p),_=Math.round(m),v=Math.round(h),b=E(p,g),x=E(m,_),C=E(h,v),w=-1,O=-1;for(let e=0;e<8&&w===-1;e++){if(e&1&&b===g||e&2&&x===_||e&4&&C===v)continue;let t=T(e&1?b:g,e&2?x:_,e&4?C:v);e===0&&(O=t),s[t]!==-1&&(w=s[t])}if(w===-1){let e=O;w=D++,s[e]=w,c[e]=g,l[e]=_,u[e]=v,f[w]=r}a[r]=y,o[r]=w,d[n]=w}p=g(p,D+1),p.fill(0,0,D+1);let O=0;for(let e=0;e<_;e+=3){let t=d[e],n=d[e+1],r=d[e+2];t!==n&&n!==r&&r!==t&&(p[t+1]+=1,p[n+1]+=1,p[r+1]+=1,O++)}if(O<4)return!1;for(let e=0;e<D;e++)p[e+1]+=p[e];m=g(m,p[D]),h=g(h,D),h.set(p.subarray(0,D));for(let e=0;e<_;e+=3){let t=d[e],n=d[e+1],r=d[e+2];t!==n&&n!==r&&r!==t&&(m[h[t]]=n,h[t]+=1,m[h[n]]=r,h[n]+=1,m[h[r]]=t,h[r]+=1)}for(let e=0;e<D;e++){let t=p[e],n=p[e+1];for(let r=t;r<n;r++){let n=m[r];for(let e=t;e<r;e++)if(m[e]===n)return!1;let i=!1;for(let t=p[n],r=p[n+1];t<r&&!i;t++)i=m[t]===e;if(!i)return!1}}let k=f[d[0]]*3,A=e[k],j=e[k+1],M=e[k+2],N=0;for(let t=0;t<_;t+=3){let n=f[d[t]]*3,r=f[d[t+1]]*3,i=f[d[t+2]]*3,a=e[n]-A,o=e[n+1]-j,s=e[n+2]-M,c=e[r]-A,l=e[r+1]-j,u=e[r+2]-M,p=e[i]-A,m=e[i+1]-j,h=e[i+2]-M;N+=a*(l*h-u*m)+o*(u*p-c*h)+s*(c*m-l*p)}return N>0},v=0;for(let e=0;e<n.length;e++){let t=n[e];if(!_(e,v,t))return!1;v+=t}return!0}function we(e,t,n,r){let i=Math.cos(30*Math.PI/180),a=e.length/3,o=t.length,s=o/3,c=/* @__PURE__ */ new Uint32Array;{let i=new Uint32Array(e.buffer,e.byteOffset,e.length),s=n?new Uint32Array(n.buffer,n.byteOffset,n.length):null,l=(e,t)=>e[t]===2147483648?0:e[t],u=16;for(;u<a*2;)u*=2;let d=u-1,f=new Int32Array(u).fill(-1),p=new Uint32Array(a),m=!1;for(let e=0;e<a;e++){let t=l(i,e*3),n=l(i,e*3+1),a=l(i,e*3+2),o=Math.imul(t,2654435761)^Math.imul(n,2246822519)^Math.imul(a,3266489917);for(o=(o^o>>>15)&d;;){let c=f[o];if(c===-1){f[o]=e,p[e]=e;break}if(l(i,c*3)===t&&l(i,c*3+1)===n&&l(i,c*3+2)===a&&(!r||r[c*3]===r[e*3]&&r[c*3+1]===r[e*3+1]&&r[c*3+2]===r[e*3+2])&&(!s||s[c*2]===s[e*2]&&s[c*2+1]===s[e*2+1])){p[e]=c,m=!0;break}o=o+1&d}}if(m){for(let e=0;e<o;e++)t[e]=p[t[e]];let e=0;for(let t=0;t<a;t++)p[t]!==t&&e++;c=new Uint32Array(e);for(let e=0,t=0;e<a;e++)p[e]!==e&&(c[t++]=e)}}let l=new Float32Array(s*3),u=new Float32Array(s);for(let n=0;n<s;n++){let r=t[n*3]*3,i=t[n*3+1]*3,a=t[n*3+2]*3,o=e[i]-e[r],s=e[i+1]-e[r+1],c=e[i+2]-e[r+2],d=e[a]-e[r],f=e[a+1]-e[r+1],p=e[a+2]-e[r+2],m=s*p-c*f,h=c*d-o*p,g=o*f-s*d,_=Math.sqrt(m*m+h*h+g*g);_!==0&&(l[n*3]=m/_,l[n*3+1]=h/_,l[n*3+2]=g/_,u[n]=_)}let d=new Uint32Array(a+1);for(let e=0;e<o;e++)d[t[e]+1]+=1;for(let e=0;e<a;e++)d[e+1]+=d[e];let f=new Uint32Array(o),p=d.slice(0,a);for(let e=0;e<o;e++){let n=t[e];f[p[n]]=e,p[n]+=1}let m=0;for(let e=0;e<a;e++)m=Math.max(m,d[e+1]-d[e]);let h=new Uint32Array(o),g=new Uint32Array(a),_=new Float32Array(Math.max(1,m)*3);for(let e=0;e<a;e++){let t=0;for(let n=d[e];n<d[e+1];n++){let e=f[n],r=Math.floor(e/3)*3,a=l[r],o=l[r+1],s=l[r+2],c=-1;if(a===0&&o===0&&s===0)c=0;else for(let e=0;e<t;e++)if(_[e*3]*a+_[e*3+1]*o+_[e*3+2]*s>=i){c=e;break}c===-1&&(c=t,_[t*3]=a,_[t*3+1]=o,_[t*3+2]=s,t++),h[e]=c}g[e]=Math.max(1,t)}let v=new Uint32Array(a+1);for(let e=0;e<a;e++)v[e+1]=v[e]+g[e]-1;let y=v[a],b=a+Math.max(0,y-c.length),x=new Uint32Array(y);for(let e=0;e<y;e++)x[e]=e<c.length?c[e]:a+e-c.length;let S=e,C=n,w=r;if(y>0){S=new Float32Array(b*3),S.set(e),C=n?new Float32Array(b*2):null,C&&n&&C.set(n),w=r?new Uint8Array(b*3):null,w&&r&&w.set(r);for(let t=0;t<a;t++)for(let i=1;i<g[t];i++){let a=x[v[t]+i-1];S[a*3]=e[t*3],S[a*3+1]=e[t*3+1],S[a*3+2]=e[t*3+2],C&&n&&(C[a*2]=n[t*2],C[a*2+1]=n[t*2+1]),w&&r&&(w[a*3]=r[t*3],w[a*3+1]=r[t*3+1],w[a*3+2]=r[t*3+2])}}let T=new Float32Array(b*3);for(let e=0;e<s;e++){let n=u[e],r=l[e*3]*n,i=l[e*3+1]*n,a=l[e*3+2]*n;for(let n=e*3;n<e*3+3;n++){let e=t[n],o=h[n];o!==0&&(e=x[v[e]+o-1],t[n]=e),T[e*3]+=r,T[e*3+1]+=i,T[e*3+2]+=a}}for(let e=0;e<T.length;e+=3){let t=T[e],n=T[e+1],r=T[e+2],i=Math.sqrt(t*t+n*n+r*r)||1;T[e]=t/i,T[e+1]=n/i,T[e+2]=r/i}return{positions:S,normals:T,indices:t,uvs:C,colors:w}}function Te(e,t,n,r){let i=e.length/3,a=new Float32Array(i*3),o=new Float32Array(i*3);for(let t=0;t<n.length;t+=3){let i=n[t],s=n[t+1],c=n[t+2],l=e[s*3]-e[i*3],u=e[s*3+1]-e[i*3+1],d=e[s*3+2]-e[i*3+2],f=e[c*3]-e[i*3],p=e[c*3+1]-e[i*3+1],m=e[c*3+2]-e[i*3+2],h=u*m-d*p,g=d*f-l*m,_=l*p-u*f,v=Math.sqrt(h*h+g*g+_*_);if(v===0)continue;let y=(p*_-m*g)/v,b=(m*h-f*_)/v,x=(f*g-p*h)/v,S=(g*d-_*u)/v,C=(_*l-h*d)/v,w=(h*u-g*l)/v,T=r[s*2]-r[i*2],E=r[c*2]-r[i*2],D=r[s*2+1]-r[i*2+1],O=r[c*2+1]-r[i*2+1],k=T*y+E*S,A=T*b+E*C,j=T*x+E*w,M=D*y+O*S,N=D*b+O*C,P=D*x+O*w;for(let e=0;e<3;e++){let r=n[t+e];a[r*3]+=k,a[r*3+1]+=A,a[r*3+2]+=j,o[r*3]+=M,o[r*3+1]+=N,o[r*3+2]+=P}}let s=new Float32Array(i*4);for(let e=0;e<i;e++){let n=t[e*3],r=t[e*3+1],i=t[e*3+2],c=a[e*3]*n+a[e*3+1]*r+a[e*3+2]*i,l=a[e*3]-c*n,u=a[e*3+1]-c*r,d=a[e*3+2]-c*i,f=Math.sqrt(l*l+u*u+d*d);if(!(f>1e-12)){let e=+(Math.abs(n)<.9),t=1-e;l=t*i,u=-e*i,d=e*r-t*n,f=Math.sqrt(l*l+u*u+d*d)||1}l/=f,u/=f,d/=f;let p=r*d-i*u,m=i*l-n*d,h=n*u-r*l,g=p*o[e*3]+m*o[e*3+1]+h*o[e*3+2];s[e*4]=l,s[e*4+1]=u,s[e*4+2]=d,s[e*4+3]=g<0?-1:1}return s}function U(e,t){let n=t.every(e=>typeof e.closed==`boolean`)?t.length>0&&t.every(e=>e.closed):Ce(e.positions,e.indices,t.map(e=>e.indexCount)),r=we(e.positions,e.indices,e.uvs,e.colors),i=r.uvs?Te(r.positions,r.normals,r.indices,r.uvs):null;return{...r,closed:n,tangents:i}}function Ee(e){let{isFloat32:t,deltaEncoded:n,planarByteSplit:r,uint16Indices:i,origin:a,scale:o,uvs:s,colors:c,jobs:l}=e,u=e=>e>>>1^-(e&1),d;if(t)d=e.vertexData;else{let t;if(r){let n=e.vertexData,r=n.length/6;t=new Int16Array(r*3);let i=0,a=0,o=0;for(let e=0;e<r;e++)i=i+u(n[e]|n[r*3+e]<<8)<<16>>16,a=a+u(n[r+e]|n[r*4+e]<<8)<<16>>16,o=o+u(n[r*2+e]|n[r*5+e]<<8)<<16>>16,t[e*3]=i,t[e*3+1]=a,t[e*3+2]=o}else if(n){let n=e.vertexData;t=new Int16Array(n.length);let r=0,i=0,a=0;for(let e=0;e<n.length;e+=3)r=r+u(n[e])<<16>>16,i=i+u(n[e+1])<<16>>16,a=a+u(n[e+2])<<16>>16,t[e]=r,t[e+1]=i,t[e+2]=a}else t=e.vertexData;d=new Float32Array(t.length);let i=a[0],s=a[1],c=a[2],l=o[0],f=o[1],p=o[2];for(let e=0;e<t.length;e+=3)d[e]=i+(t[e]+32767)*l,d[e+1]=s+(t[e+1]+32767)*f,d[e+2]=c+(t[e+2]+32767)*p}let f;if(r){let t=e.indexData;if(i){let e=t.length/2,n=new Uint16Array(e),r=0;for(let i=0;i<e;i++)r=r+u(t[i]|t[e+i]<<8)&65535,n[i]=r;f=n}else{let e=t.length/4,n=new Uint32Array(e),r=0;for(let i=0;i<e;i++){let a=(t[i]|t[e+i]<<8|t[e*2+i]<<16|t[e*3+i]<<24)>>>0;r=r+u(a)>>>0,n[i]=r}f=n}}else if(n){let t=e.indexData;if(t instanceof Uint16Array){let e=new Uint16Array(t.length),n=0;for(let r=0;r<t.length;r++)n=n+u(t[r])&65535,e[r]=n;f=e}else{let e=new Uint32Array(t.length),n=0;for(let r=0;r<t.length;r++)n=n+u(t[r])>>>0,e[r]=n;f=e}}else f=e.indexData;let p=d.length/3;for(let e=0;e<f.length;e++)if(f[e]>=p)throw Error(`Index ${f[e]} out of range of vertexCount ${p}`);let m=[];for(let e of l){let t=0,n=0;for(let r of e.windows)t+=r.vertexCount,n+=r.indexCount;let r=new Float32Array(t*3),i=new Uint32Array(n),a=s?new Float32Array(t*2):null,o=c?new Uint8Array(t*3):null,l=0,u=0;for(let t of e.windows){let e=t.vertexStart*3;r.set(d.subarray(e,e+t.vertexCount*3),l*3),a&&s&&a.set(s.subarray(t.vertexStart*2,(t.vertexStart+t.vertexCount)*2),l*2),o&&c&&o.set(c.subarray(e,e+t.vertexCount*3),l*3);let n=t.vertexStart,p=t.vertexStart+t.vertexCount,m=l-t.vertexStart;for(let e=0;e<t.indexCount;e++){let r=f[t.indexStart+e];if(r<n||r>=p)throw Error(`Index ${r} outside vertex window [${n}, ${p})`);i[u+e]=r+m}l+=t.vertexCount,u+=t.indexCount}m.push({positions:r,indices:i,uvs:a,colors:o})}return m}function De(){return[`const assemble = ${Ee.toString()};`,`const isClosedSolid = ${Ce.toString()};`,`const splitCreases = ${we.toString()};`,`const grainTangents = ${Te.toString()};`,`self.onmessage = (event) => {`,` const { id, input } = event.data;`,` try {`,` const geometries = assemble(input).map((g, i) => {`,` const windows = input.jobs[i].windows;`,` const known = windows.every((w) => typeof w.closed === 'boolean');`,` const closed = known`,` ? windows.length > 0 && windows.every((w) => w.closed)`,` : isClosedSolid(g.positions, g.indices, windows.map((w) => w.indexCount));`,` const c = splitCreases(g.positions, g.indices, g.uvs, g.colors);`,` const tangents = c.uvs ? grainTangents(c.positions, c.normals, c.indices, c.uvs) : null;`,` return { ...c, closed, tangents };`,` });`,` const transfer = [];`,` for (const g of geometries) {`,` transfer.push(g.positions.buffer, g.normals.buffer, g.indices.buffer);`,` if (g.uvs) transfer.push(g.uvs.buffer);`,` if (g.tangents) transfer.push(g.tangents.buffer);`,` if (g.colors) transfer.push(g.colors.buffer);`,` }`,` self.postMessage({ id, geometries }, transfer);`,` } catch (error) {`,` self.postMessage({ id, error: String((error && error.message) || error) });`,` }`,`};`].join(`
2
+ `)}let W;const G=/* @__PURE__ */ new Map;let Oe=1;function ke(){if(W!==void 0)return W;if(typeof Worker>`u`||typeof Blob>`u`||typeof URL>`u`||typeof URL.createObjectURL!=`function`)return W=null,null;try{let e=URL.createObjectURL(new Blob([De()],{type:`text/javascript`})),t=new Worker(e);t.onmessage=e=>{let{id:t,geometries:n,error:r}=e.data,i=G.get(t);i&&(G.delete(t),n?i.resolve(n):i.reject(Error(r??`mesh assembly failed in worker`)))},t.onerror=()=>{for(let e of G.values())e.reject(/* @__PURE__ */ Error(`mesh assembly worker crashed`));G.clear(),t.terminate(),W=null},W=t}catch{W=null}return W}function Ae(e,t,n){return new Promise((r,i)=>{let a=Oe++;G.set(a,{resolve:r,reject:i}),e.postMessage({id:a,input:t},n)})}let je=1;function Me(e){je=Math.max(1,e)}a(Me);function K(e,t,n,r){typeof document<`u`&&new f.TextureLoader().load(t,t=>{if(t.colorSpace=n===`map`?f.SRGBColorSpace:f.NoColorSpace,t.anisotropy=je,r){let e=t.image,n=e?.width&&e.height?e.height/e.width:1;t.wrapS=f.RepeatWrapping,t.wrapT=f.RepeatWrapping,t.repeat.set(1/r,1/(r*n))}e[n]=t,e.needsUpdate=!0},void 0,e=>{s().warn(`Failed to load material ${n} ${t}:`,e)})}const Ne={brushed:{fibres:[{pitch:.04,length:[2,12],depth:.3},{pitch:.25,length:[5,40],depth:.18}],bands:[]},rolled:{fibres:[{pitch:.3,length:[80,600],depth:.07}],bands:[{width:20,length:1500,depth:.05}]}},Pe=.35,q=e=>e.toFixed(4);function Fe(e,t){let n=Ne[e];if(!n||!(t>0))return null;let r=n.fibres.map((e,t)=>`selvaFibres( vUv, footprint, ${q(e.pitch)}, vec2( ${q(e.length[0])}, ${q(e.length[1])} ), ${q(e.depth)}, ${q(t*31+3)} )`).join(` + `)||`0.0`,i=n.bands.map(e=>`selvaBand( vUv, footprint, vec2( ${q(e.length)}, ${q(e.width)} ), ${q(e.depth)} )`).join(` + `)||`0.0`;return{key:`finish:${e}:${q(t)}`,patch:e=>{e.fragmentShader=e.fragmentShader.replace(`#include <common>`,`#include <common>
3
3
  // Hash without sine: sin() loses precision on the large arguments mm UVs produce.
4
4
  float selvaHash( vec2 p ) {
5
5
  vec3 p3 = fract( vec3( p.xyx ) * 0.1031 );
@@ -34,9 +34,9 @@ import{a as e,g as t,h as n,i as r,l as i,n as a,o,p as s,t as c,u as l}from"./g
34
34
  float selvaFibres( vec2 uv, float footprint, float pitch, vec2 len, float depth, float seed ) {
35
35
  float level = clamp( log2( footprint / pitch ) + 0.5, 0.0, 14.0 );
36
36
  float l0 = floor( level );
37
- float a = selvaFibreRow( uv, pitch * exp2( l0 ), len, seed + 7.0 * l0 ) * exp2( -${K(Fe)} * l0 );
37
+ float a = selvaFibreRow( uv, pitch * exp2( l0 ), len, seed + 7.0 * l0 ) * exp2( -${q(Pe)} * l0 );
38
38
  float b = selvaFibreRow( uv, pitch * exp2( l0 + 1.0 ), len, seed + 7.0 * l0 + 7.0 )
39
- * exp2( -${K(Fe)} * ( l0 + 1.0 ) );
39
+ * exp2( -${q(Pe)} * ( l0 + 1.0 ) );
40
40
  return depth * mix( a, b, level - l0 );
41
41
  }
42
42
  float selvaBand( vec2 uv, float footprint, vec2 size, float depth ) {
@@ -44,8 +44,8 @@ import{a as e,g as t,h as n,i as r,l as i,n as a,o,p as s,t as c,u as l}from"./g
44
44
  return depth * fade * ( selvaNoise( uv / size ) * 2.0 - 1.0 );
45
45
  }`).replace(`#include <color_fragment>`,`#include <color_fragment>
46
46
  float footprint = max( fwidth( vUv.y ), 1e-6 );
47
- float selvaFibre = ( ${r} ) * ${K(t)};
48
- float selvaStreak = selvaFibre + ( ${i} ) * ${K(t)};
47
+ float selvaFibre = ( ${r} ) * ${q(t)};
48
+ float selvaStreak = selvaFibre + ( ${i} ) * ${q(t)};
49
49
  diffuseColor.rgb *= 1.0 + 0.5 * selvaStreak;`).replace(`#include <roughnessmap_fragment>`,`#include <roughnessmap_fragment>
50
50
  roughnessFactor = clamp( roughnessFactor * ( 1.0 + selvaStreak ), 0.0, 1.0 );`).replace(`#include <normal_fragment_maps>`,`#include <normal_fragment_maps>
51
51
  #ifdef USE_ANISOTROPY
@@ -57,12 +57,12 @@ import{a as e,g as t,h as n,i as r,l as i,n as a,o,p as s,t as c,u as l}from"./g
57
57
  vec3 selvaAcross = cross( selvaQ1, normal ) * dFdx( vUv ).y + cross( normal, selvaQ0 ) * dFdy( vUv ).y;
58
58
  selvaAcross *= inversesqrt( max( dot( selvaAcross, selvaAcross ), 1e-12 ) );
59
59
  #endif
60
- normal = normalize( normal + ${K(.6)} * selvaFibre * selvaAcross );`)}}}function Le(e,t){return t?.appearance?.cullBackfaces?!0:(t?.closed??!1)&&!e.transparent&&e.opacity>=1}function Re(e,t){let n=/* @__PURE__ */ new Map;return(r,i)=>{let a=e[r],o=Le(a,{...t,closed:i}),s=`${r}:${o}`,c=n.get(s);return c||(c=ze(a,{...t,closed:o}),n.set(s,c)),c}}function ze(e,t){let n=o(e.color),r=t?.vertexColors??!1,a=t?.appearance,s=new f.MeshPhysicalMaterial({color:n,metalness:e.metalness,roughness:e.roughness,opacity:e.opacity,transparent:e.transparent,vertexColors:r,side:Le(e,t)?f.FrontSide:f.DoubleSide,polygonOffset:!0,polygonOffsetFactor:.25,polygonOffsetUnits:0,depthWrite:!0,depthTest:!0});e.envMapIntensity==null?a?.envMapIntensity!=null&&(s.envMapIntensity=l(a.envMapIntensity,e.metalness)):(s.envMapIntensity=e.envMapIntensity,s.userData[i]=e.envMapIntensity),e.clearcoat!=null&&(s.clearcoat=e.clearcoat,s.clearcoatRoughness=e.clearcoatRoughness??0),e.anisotropy!=null&&e.anisotropy>0&&t?.hasUvs&&(s.anisotropy=e.anisotropy,s.anisotropyRotation=e.anisotropyRotation??0),e.transmission!=null&&(s.transmission=e.transmission,s.thickness=0),e.ior!=null&&(s.ior=e.ior),r&&Ve(s);let c=e.finish&&t?.hasUvs?Ie(e.finish,e.finishStrength??1):null;c&&(s.defines={...s.defines,USE_UV:``},Be(s,c.key,c.patch));let{mapSize:u}=e;return e.map&&G(s,e.map,`map`,u),e.roughnessMap&&G(s,e.roughnessMap,`roughnessMap`,u),e.normalMap&&G(s,e.normalMap,`normalMap`,u),s}function Be(e,t,n){let r=e.onBeforeCompile,i=e.customProgramCacheKey.bind(e);e.onBeforeCompile=(t,i)=>{r.call(e,t,i),n(t)},e.customProgramCacheKey=()=>`${i()}|${t}`}function Ve(e){Be(e,`srgb-vertex-colors`,e=>{e.vertexShader=e.vertexShader.replace(`#include <color_vertex>`,`#include <color_vertex>
60
+ normal = normalize( normal + ${q(.6)} * selvaFibre * selvaAcross );`)}}}function Ie(e,t){return t?.appearance?.cullBackfaces?!0:(t?.closed??!1)&&!e.transparent&&e.opacity>=1}function Le(e,t){let n=/* @__PURE__ */ new Map;return(r,i)=>{let a=e[r],o=Ie(a,{...t,closed:i}),s=`${r}:${o}`,c=n.get(s);return c||(c=Re(a,{...t,closed:o}),n.set(s,c)),c}}function Re(e,t){let n=o(e.color),r=t?.vertexColors??!1,a=t?.appearance,s=new f.MeshPhysicalMaterial({color:n,metalness:e.metalness,roughness:e.roughness,opacity:e.opacity,transparent:e.transparent,vertexColors:r,side:Ie(e,t)?f.FrontSide:f.DoubleSide,polygonOffset:!0,polygonOffsetFactor:.25,polygonOffsetUnits:0,depthWrite:!0,depthTest:!0});e.envMapIntensity==null?a?.envMapIntensity!=null&&(s.envMapIntensity=l(a.envMapIntensity,e.metalness)):(s.envMapIntensity=e.envMapIntensity,s.userData[i]=e.envMapIntensity),e.clearcoat!=null&&(s.clearcoat=e.clearcoat,s.clearcoatRoughness=e.clearcoatRoughness??0),e.anisotropy!=null&&e.anisotropy>0&&t?.hasUvs&&(s.anisotropy=e.anisotropy,s.anisotropyRotation=e.anisotropyRotation??0),e.transmission!=null&&(s.transmission=e.transmission,s.thickness=0),e.ior!=null&&(s.ior=e.ior),r&&Be(s);let c=e.finish&&t?.hasUvs?Fe(e.finish,e.finishStrength??1):null;c&&(s.defines={...s.defines,USE_UV:``},ze(s,c.key,c.patch));let{mapSize:u}=e;return e.map&&K(s,e.map,`map`,u),e.roughnessMap&&K(s,e.roughnessMap,`roughnessMap`,u),e.normalMap&&K(s,e.normalMap,`normalMap`,u),s}function ze(e,t,n){let r=e.onBeforeCompile,i=e.customProgramCacheKey.bind(e);e.onBeforeCompile=(t,i)=>{r.call(e,t,i),n(t)},e.customProgramCacheKey=()=>`${i()}|${t}`}function Be(e){ze(e,`srgb-vertex-colors`,e=>{e.vertexShader=e.vertexShader.replace(`#include <color_vertex>`,`#include <color_vertex>
61
61
  #if defined( USE_COLOR ) || defined( USE_COLOR_ALPHA )
62
62
  vColor.rgb = mix(
63
63
  vColor.rgb / 12.92,
64
64
  pow( ( vColor.rgb + 0.055 ) / 1.055, vec3( 2.4 ) ),
65
65
  step( vec3( 0.04045 ), vColor.rgb )
66
66
  );
67
- #endif`)})}function q(e,r){return new t(e,n.VALIDATION_ERROR,{context:r})}function J(e,t,n,r){for(let i of e){if(!Number.isInteger(i.materialId)||i.materialId<0||i.materialId>=t)throw q(`Group materialId out of range of the materials array.`,{materialId:i.materialId,materialCount:t});for(let e of i.meshes){let t={vertexStart:e.vertexStart,vertexCount:e.vertexCount,indexStart:e.indexStart,indexCount:e.indexCount};for(let[n,r]of Object.entries(t))if(!Number.isInteger(r)||r<0)throw q(`Mesh metadata field "${n}" must be a non-negative integer.`,{meshName:e.name,field:n,value:r});if(e.vertexStart+e.vertexCount>n)throw q(`Mesh vertex window exceeds the batch vertex buffer.`,{meshName:e.name,vertexStart:e.vertexStart,vertexCount:e.vertexCount,totalVertexCount:n});if(e.indexStart+e.indexCount>r)throw q(`Mesh index window exceeds the batch index buffer.`,{meshName:e.name,indexStart:e.indexStart,indexCount:e.indexCount,totalIndexCount:r})}}}function He(e,t){return q(`Index references a vertex outside its mesh's vertex window.`,{meshName:t.name,indexValue:e,vertexStart:t.vertexStart,vertexCount:t.vertexCount})}function Ue(e,t,n){let r=new Float32Array(e.length),i=t[0],a=t[1],o=t[2],s=n[0],c=n[1],l=n[2];for(let t=0;t<e.length;t+=3)r[t]=i+(e[t]+32767)*s,r[t+1]=a+(e[t+1]+32767)*c,r[t+2]=o+(e[t+2]+32767)*l;return r}function We(e){let t=/* @__PURE__ */ new Map;for(let n of e.meshes){let e=n.layer??``,r=t.get(e);r||(r=[],t.set(e,r)),r.push(n)}return[...t.values()].map(t=>({materialId:e.materialId,meshes:t}))}function Ge(e){let t=/* @__PURE__ */ new Map;for(let n of e){let e=t.get(n.materialId);e?e.push(...n.meshes):t.set(n.materialId,[...n.meshes])}return[...t].flatMap(([e,t])=>We({materialId:e,meshes:t}))}function Ke(e,t,n,r,i=null,a=null){let o=0,s=0;for(let t of e.meshes)o+=t.vertexCount,s+=t.indexCount;let c=new Float32Array(o*3),l=new Uint32Array(s),u=i?new Float32Array(o*2):null,d=a?new Uint8Array(o*3):null,f=0,p=0;for(let r of e.meshes){let e=r.vertexStart*3,o=r.vertexCount*3;if(c.set(t.subarray(e,e+o),f*3),u&&i){let e=r.vertexStart*2;u.set(i.subarray(e,e+r.vertexCount*2),f*2)}d&&a&&d.set(a.subarray(e,e+o),f*3);let s=n.subarray(r.indexStart,r.indexStart+r.indexCount),m=f-r.vertexStart,h=r.vertexStart,g=r.vertexStart+r.vertexCount;for(let e=0;e<s.length;e++){let t=s[e];if(t<h||t>=g)throw He(t,r);l[p+e]=t+m}f+=r.vertexCount,p+=r.indexCount}let m=H({positions:c,indices:l,uvs:u,colors:d},e.meshes.map(e=>e.indexCount));return Je(Y(m),e,r(e.materialId,m.closed))}function Y(e){let t=new f.BufferGeometry;return t.setAttribute(`position`,new f.BufferAttribute(e.positions,3)),t.setAttribute(`normal`,new f.BufferAttribute(e.normals,3)),t.setIndex(new f.BufferAttribute(e.indices,1)),e.uvs&&t.setAttribute(`uv`,new f.BufferAttribute(e.uvs,2)),e.tangents&&t.setAttribute(`tangent`,new f.BufferAttribute(e.tangents,4)),e.colors&&t.setAttribute(`color`,new f.BufferAttribute(e.colors,3,!0)),t}function qe(e){let t=[],n=0;for(let r of e.meshes)t.push({trackingKey:r.id,name:r.name,layer:r.layer,metadata:r.metadata??{},indexStart:n,indexCount:r.indexCount}),n+=r.indexCount;return t}function Je(e,t,n){let r=new f.Mesh(e,n),i=t.meshes[0],a=t.meshes.map(e=>e.name).filter(e=>e&&e.length>0);return r.name=a.length>0?a[0]:`merged_material_${t.materialId}`,r.castShadow=!0,r.receiveShadow=!0,r.userData={source:`compute`,name:r.name,layer:i?.layer??``,metadata:i?.metadata??{},members:qe(t)},r}function Ye(e,t,n,r,i=null,a=null){let o=[];for(let s of e.meshes){let c=s.vertexStart*3,l=s.vertexCount*3,u=t.slice(c,c+l),d=n.subarray(s.indexStart,s.indexStart+s.indexCount),f=new Uint32Array(d.length),p=s.vertexStart,m=s.vertexStart+s.vertexCount;for(let e=0;e<d.length;e++){let t=d[e];if(t<p||t>=m)throw He(t,s);f[e]=t-p}let h=s.vertexStart*2,g=H({positions:u,indices:f,uvs:i?i.slice(h,h+s.vertexCount*2):null,colors:a?a.slice(c,c+l):null},[f.length]);o.push(Xe(Y(g),s,r(e.materialId,g.closed)))}return o}function Xe(e,t,n){let r=new f.Mesh(e,n);return r.name=t.name,r.userData={source:`compute`,name:t.name,layer:t.layer??``,trackingKey:t.id,metadata:t.metadata??{}},r.castShadow=!0,r.receiveShadow=!0,r}const Ze=268435456,X=/* @__PURE__ */ new Map;let Z=0;function Qe(e,t){let n=nt(t),r=rt(e,n),i=X.get(r);return!i||!it(i.jobShape,n)||!at(i.geometryBytes,e)?null:(X.delete(r),X.set(r,i),i)}function $e(e){let{geometries:t,...n}=e;return{...n,buffers:t.map(Y),closed:t.map(e=>e.closed)}}function et(e,t,n){let r=$e(n),i=e.byteLength;for(let e of n.geometries)i+=e.positions.byteLength+e.normals.byteLength+e.indices.byteLength,i+=(e.uvs?.byteLength??0)+(e.colors?.byteLength??0)+(e.tangents?.byteLength??0);if(i>Ze)return r;let a=nt(t),o=rt(e,a),s=X.get(o);s&&(X.delete(o),Q(s)),r.buffers.forEach(u),X.set(o,{...r,geometryBytes:e.slice(),jobShape:a,bytes:i}),Z+=i;for(let[e,t]of X){if(Z<=Ze)break;X.delete(e),Q(t)}return r}function tt(){X.forEach(Q),X.clear()}function Q(e){Z-=e.bytes;for(let t of e.buffers)d(t),t.dispose()}function nt(e){let t=0;for(let n of e)t+=2+4*n.windows.length;let n=new Int32Array(t),r=0;for(let t of e){n[r++]=+(t.kind===`merged`),n[r++]=t.windows.length;for(let e of t.windows)n[r++]=e.vertexStart,n[r++]=e.vertexCount,n[r++]=e.indexStart,n[r++]=e.indexCount}return n}function rt(e,t){let n=e.length,r=Math.max(1,Math.floor(n/1024)),i=2166136261;for(let t=0;t<n;t+=r)i=Math.imul(i^e[t],16777619);let a=2166136261;for(let e=0;e<t.length;e++)a=Math.imul(a^t[e],16777619);return`${n}:${i>>>0}:${t.length}:${a>>>0}`}function it(e,t){if(e.length!==t.length)return!1;for(let n=0;n<e.length;n++)if(e[n]!==t[n])return!1;return!0}function at(e,t){if(e.length!==t.length)return!1;let n=0;if(e.byteOffset%4==0&&t.byteOffset%4==0){let r=e.length>>>2,i=new Uint32Array(e.buffer,e.byteOffset,r),a=new Uint32Array(t.buffer,t.byteOffset,r);for(;n<r;n++)if(i[n]!==a[n])return!1;n=r<<2}for(;n<e.length;n++)if(e[n]!==t[n])return!1;return!0}async function ot(e,t,n){let{mergeByMaterial:r=!0,debug:i=!1,material:a}=t??{},{parseTime:o=0,perfStart:s=i?performance.now():0}=n??{};if(!e.compressedData)return[];let c=performance.now(),l={mergeByMaterial:r,debug:i,material:a,fallback:{materials:e.materials,groups:e.groups}},u=await dt(e.compressedData,l);return u.meshes?u.meshes:await ht(u,l)||ut(Ce(u.raw),{mergeByMaterial:r,debug:i,material:a,parseTime:o,decodeTime:performance.now()-c,perfStart:s,blobBytes:i?yt(e.compressedData):0,fallback:{materials:e.materials,groups:e.groups}})}async function st(e,t){let{mergeByMaterial:n=!0,debug:r=!1,material:i}=t??{},a=r?performance.now():0,o=performance.now(),s={mergeByMaterial:n,debug:r,material:i},c=await dt(e,s);if(c.meshes)return c.meshes;let l=await ht(c,s);if(l)return l;let u=Ce(c.raw),d=performance.now()-o,f=e.byteLength;return ut(u,{mergeByMaterial:n,debug:r,material:i,parseTime:0,decodeTime:d,perfStart:a,blobBytes:f})}let ct=!1;function lt(e,t){if(e||ct)return;let n=0;for(let e of t)n+=e.meshes.length;n<1e3||(ct=!0,s().warn(`Parsing ${n} meshes with mergeByMaterial: false — each becomes its own THREE object, and render cost scales with object count. Drop the option to merge by material (the default); per-object identity survives it via userData.members.`))}function ut(e,t){let{mergeByMaterial:n,debug:r,material:i,parseTime:a,decodeTime:o,perfStart:c,blobBytes:l,fallback:u}=t,d=e.metadata.materials??u?.materials??[],f=e.metadata.groups??u?.groups??[],p=!!(e.flags&1);J(f,d.length,e.vertices.length/3,e.indices.length);let m=p?e.vertices:Ue(e.vertices,e.origin,e.scale);if(r){let t=e.vertices.byteLength+e.indices.byteLength;s().debug(`Mesh Batch Stats:`),s().debug(` Materials: ${d.length} | Groups: ${f.length}`),s().debug(` Vertices: ${e.vertices.length/3} | Indices: ${e.indices.length}`),s().debug(` Format: ${p?`float32`:`int16 quantized`}`),s().debug(` Blob: ${(l/1024/1024).toFixed(2)} MB | Geometry on wire: ${(t/1024/1024).toFixed(2)} MB`)}lt(n,f);let h=performance.now(),g=Re(d,{vertexColors:e.colors!=null,hasUvs:e.uvs!=null,appearance:i}),_=[],v=n?Ge(f):f;for(let t of v)if(n&&t.meshes.length>1){let n=Ke(t,m,e.indices,g,e.uvs,e.colors);_.push(n)}else{let n=Ye(t,m,e.indices,g,e.uvs,e.colors);_.push(...n)}let y=performance.now()-h;if(r){let e=performance.now()-c;s().debug(`Performance:`),a>0&&s().debug(` Parse JSON: ${a.toFixed(2)}ms`),s().debug(` Decode binary: ${o.toFixed(2)}ms`),s().debug(` Create Meshes: ${y.toFixed(2)}ms`),s().debug(` Total: ${e.toFixed(2)}ms`)}return Promise.resolve(_)}async function dt(e,t){let n=j(e);if(!he(n))return{raw:V(n),geometryBytes:null,source:n,reparse:()=>V(n)};let r=ge(n),i=pt(r,t);if(i)return{meshes:i};let a=r.geometryBlob,o=()=>{let e=V(a);return e.metadata=r.metadata,e},s=await ft(r),c=s?V(s):o();return c.metadata=r.metadata,{raw:c,geometryBytes:a,source:a,reparse:o}}async function ft(e){if(N(e.geometryBlob)<4194304||!Ae())return null;try{return await ne(e.geometryBlob)}catch{return null}}function pt(e,t){let n=e.metadata.materials??t.fallback?.materials??[],r=e.metadata.groups??t.fallback?.groups??[],{jobs:i,jobRefs:a}=mt(r,t.mergeByMaterial),o=Qe(e.geometryBlob,i);return o?(J(r,n.length,o.vertexCount,o.indexCount),vt(o,a,n,t.material)):null}function mt(e,t){let n=e=>({vertexStart:e.vertexStart,vertexCount:e.vertexCount,indexStart:e.indexStart,indexCount:e.indexCount}),r=[],i=[],a=t?Ge(e):e;for(let e of a)if(t&&e.meshes.length>1)r.push({kind:`merged`,windows:e.meshes.map(n)}),i.push({kind:`merged`,group:e});else for(let t of e.meshes)r.push({kind:`single`,windows:[n(t)]}),i.push({kind:`single`,group:e,meshMeta:t});return{jobs:r,jobRefs:i}}async function ht(e,t){let{raw:n,geometryBytes:r}=e,i=n.planarByteSplit?n.indexData.length/(n.uint16Indices?2:4):n.indexData.length;if(i/3<5e4)return null;let a=n.metadata.materials??t.fallback?.materials??[],o=n.metadata.groups??t.fallback?.groups??[];J(o,a.length,n.vertexCount,i);let{jobs:c,jobRefs:l}=mt(o,t.mergeByMaterial),u=await _t(n,c,e.source);if(!u)return gt(n,e.source)&&(e.raw=e.reparse()),null;let d={geometries:u,hasUvs:n.uvs!=null,hasColors:n.colors!=null,vertexCount:n.vertexCount,indexCount:i},f=r?et(r,c,d):$e(d);return t.debug&&s().debug(`Mesh batch assembled: ${u.length} geometries, ${i/3} triangles`),vt(f,l,a,t.material)}function gt(e,t){return e.vertexData.buffer!==t.buffer&&e.indexData.buffer!==t.buffer}async function _t(e,t,n){let r=Ae(),i={vertexData:e.vertexData,isFloat32:e.isFloat32,deltaEncoded:e.deltaEncoded,planarByteSplit:e.planarByteSplit,uint16Indices:e.uint16Indices,origin:e.origin,scale:e.scale,indexData:e.indexData,uvs:e.uvs,colors:e.colors,jobs:t};if(!r)return De(i).map((e,n)=>H(e,t[n].windows.map(e=>e.indexCount)));gt(e,n)||(i.vertexData=e.vertexData.slice(),i.indexData=e.indexData.slice(),i.uvs=e.uvs?.slice()??null,i.colors=e.colors?.slice()??null);let a=/* @__PURE__ */ new Set([i.vertexData.buffer,i.indexData.buffer]);i.uvs&&a.add(i.uvs.buffer),i.colors&&a.add(i.colors.buffer);try{let e=await je(r,i,[...a]);return e.length===t.length?e:null}catch(e){return s().warn(`Mesh assembly worker failed; falling back to main-thread parse.`,e),null}}function vt(e,t,n,r){let i=Re(n,{vertexColors:e.hasColors,hasUvs:e.hasUvs,appearance:r}),a=[];for(let n=0;n<e.buffers.length;n++){let r=e.buffers[n],o=t[n],s=i(o.group.materialId,e.closed[n]),c=o.kind===`merged`?Je(r,o.group,s):Xe(r,o.meshMeta,s);a.push(c)}return a}function yt(e){return Math.floor(e.length*3/4)}const $={Angstroms:1e-10,Nanometers:1e-9,Microns:1e-6,Millimeters:.001,Centimeters:.01,Decimeters:.1,Meters:1,Dekameters:10,Hectometers:100,Kilometers:1e3,Megameters:1e6,Gigameters:1e9,Microinches:2.54e-8,Mils:254e-7,Inches:.0254,Feet:.3048,Yards:.9144,Miles:1609.344,NauticalMiles:1852};function bt(e){let t=e.split(`.`);return t.includes(`Display`)||t.includes(`DisplayBatch`)}const xt=/* @__PURE__ */ new Set;async function St(e,t){let n=performance.now(),r=[],{allowScaling:i=!0,allowAutoPosition:a=!1,groundAxis:o=`z`,debug:s=!1,parsing:c={}}=t??{};try{return await wt(e,r,i?Ct(e.modelunits):1,c,s),a&&Ot(r,o),r}catch(e){throw kt(e,r),e}finally{s&&jt(n)}}function Ct(e){let t=$[e];return t===void 0?(xt.has(e)||(xt.add(e),s().warn(`Unknown Rhino model unit "${e}" — geometry will not be scaled (factor 1). Known units: ${Object.keys($).join(`, `)}.`)),1):t}async function wt(e,t,n,r,i){for(let a of e.values){let e=a.InnerTree;for(let a in e){let o=e[a];o&&await Tt(o,t,n,r,i)}}}async function Tt(e,t,n,r,i){for(let a of e){if(!bt(a.type))continue;let e={debug:!1,...r,mergeByMaterial:r.mergeByMaterial??!0},o=Et(a.data);if(!o){s().error(`Error parsing display batch envelope: invalid JSON`);continue}let c=await ot(o,e),l=k(o.items),u=[...c,...l];if(n!==1)for(let e of u)e.scale.set(n,n,n);t.push(...u),i&&s().debug(`Extracted ${c.length} meshes and ${l.length} items from batch`)}}function Et(e){return typeof e==`string`?Dt(e):e}function Dt(e){try{return JSON.parse(e)}catch{return}}function Ot(t,n){if(t.length===0)return;let i=e(t);r(t,i.min[n],n)}function kt(e,t){s().error(`An unexpected error occurred:`,e),At(t)}function At(e){for(let t of e)c(t)}function jt(e){let t=performance.now()-e;s().info(`Time to process meshes:`,`${t.toFixed(2)}ms`)}export{$ as SCALE_FACTORS,tt as clearGeometryCache,St as getThreeObjectsFromComputeResponse,w as meshPolicy,k as parseDisplayItems,st as parseMeshBatchBlob,ot as parseMeshBatchObject,Ne as setTextureAnisotropy};
67
+ #endif`)})}function J(e,r){return new t(e,n.VALIDATION_ERROR,{context:r})}function Y(e,t,n,r){for(let i of e){if(!Number.isInteger(i.materialId)||i.materialId<0||i.materialId>=t)throw J(`Group materialId out of range of the materials array.`,{materialId:i.materialId,materialCount:t});for(let e of i.meshes){let t={vertexStart:e.vertexStart,vertexCount:e.vertexCount,indexStart:e.indexStart,indexCount:e.indexCount};for(let[n,r]of Object.entries(t))if(!Number.isInteger(r)||r<0)throw J(`Mesh metadata field "${n}" must be a non-negative integer.`,{meshName:e.name,field:n,value:r});if(e.vertexStart+e.vertexCount>n)throw J(`Mesh vertex window exceeds the batch vertex buffer.`,{meshName:e.name,vertexStart:e.vertexStart,vertexCount:e.vertexCount,totalVertexCount:n});if(e.indexStart+e.indexCount>r)throw J(`Mesh index window exceeds the batch index buffer.`,{meshName:e.name,indexStart:e.indexStart,indexCount:e.indexCount,totalIndexCount:r})}}}function Ve(e,t){return J(`Index references a vertex outside its mesh's vertex window.`,{meshName:t.name,indexValue:e,vertexStart:t.vertexStart,vertexCount:t.vertexCount})}function He(e,t,n){let r=new Float32Array(e.length),i=t[0],a=t[1],o=t[2],s=n[0],c=n[1],l=n[2];for(let t=0;t<e.length;t+=3)r[t]=i+(e[t]+32767)*s,r[t+1]=a+(e[t+1]+32767)*c,r[t+2]=o+(e[t+2]+32767)*l;return r}function Ue(e){let t=/* @__PURE__ */ new Map;for(let n of e.meshes){let e=n.layer??``,r=t.get(e);r||(r=[],t.set(e,r)),r.push(n)}return[...t.values()].map(t=>({materialId:e.materialId,meshes:t}))}function We(e){let t=/* @__PURE__ */ new Map;for(let n of e){let e=t.get(n.materialId);if(e)for(let t of n.meshes)e.push(t);else t.set(n.materialId,n.meshes.slice())}return[...t].flatMap(([e,t])=>Ue({materialId:e,meshes:t}))}function Ge(e,t,n,r,i=null,a=null){let o=0,s=0;for(let t of e.meshes)o+=t.vertexCount,s+=t.indexCount;let c=new Float32Array(o*3),l=new Uint32Array(s),u=i?new Float32Array(o*2):null,d=a?new Uint8Array(o*3):null,f=0,p=0;for(let r of e.meshes){let e=r.vertexStart*3,o=r.vertexCount*3;if(c.set(t.subarray(e,e+o),f*3),u&&i){let e=r.vertexStart*2;u.set(i.subarray(e,e+r.vertexCount*2),f*2)}d&&a&&d.set(a.subarray(e,e+o),f*3);let s=n.subarray(r.indexStart,r.indexStart+r.indexCount),m=f-r.vertexStart,h=r.vertexStart,g=r.vertexStart+r.vertexCount;for(let e=0;e<s.length;e++){let t=s[e];if(t<h||t>=g)throw Ve(t,r);l[p+e]=t+m}f+=r.vertexCount,p+=r.indexCount}let m=U({positions:c,indices:l,uvs:u,colors:d},e.meshes);return qe(X(m),e,r(e.materialId,m.closed))}function X(e){let t=new f.BufferGeometry;return t.setAttribute(`position`,new f.BufferAttribute(e.positions,3)),t.setAttribute(`normal`,new f.BufferAttribute(e.normals,3)),t.setIndex(new f.BufferAttribute(e.indices,1)),e.uvs&&t.setAttribute(`uv`,new f.BufferAttribute(e.uvs,2)),e.tangents&&t.setAttribute(`tangent`,new f.BufferAttribute(e.tangents,4)),e.colors&&t.setAttribute(`color`,new f.BufferAttribute(e.colors,3,!0)),t}function Ke(e){let t=[],n=0;for(let r of e.meshes)t.push({trackingKey:r.id,name:r.name,layer:r.layer,metadata:r.metadata??{},indexStart:n,indexCount:r.indexCount}),n+=r.indexCount;return t}function qe(e,t,n){let r=new f.Mesh(e,n),i=t.meshes[0],a=t.meshes.map(e=>e.name).filter(e=>e&&e.length>0);return r.name=a.length>0?a[0]:`merged_material_${t.materialId}`,r.castShadow=!0,r.receiveShadow=!0,r.userData={source:`compute`,name:r.name,layer:i?.layer??``,metadata:i?.metadata??{},members:Ke(t)},r}function Je(e,t,n,r,i=null,a=null){let o=[];for(let s of e.meshes){let c=s.vertexStart*3,l=s.vertexCount*3,u=t.slice(c,c+l),d=n.subarray(s.indexStart,s.indexStart+s.indexCount),f=new Uint32Array(d.length),p=s.vertexStart,m=s.vertexStart+s.vertexCount;for(let e=0;e<d.length;e++){let t=d[e];if(t<p||t>=m)throw Ve(t,s);f[e]=t-p}let h=s.vertexStart*2,g=U({positions:u,indices:f,uvs:i?i.slice(h,h+s.vertexCount*2):null,colors:a?a.slice(c,c+l):null},[s]);o.push(Ye(X(g),s,r(e.materialId,g.closed)))}return o}function Ye(e,t,n){let r=new f.Mesh(e,n);return r.name=t.name,r.userData={source:`compute`,name:t.name,layer:t.layer??``,trackingKey:t.id,metadata:t.metadata??{}},r.castShadow=!0,r.receiveShadow=!0,r}const Xe=268435456,Z=/* @__PURE__ */ new Map;let Q=0;function Ze(e,t){let n=tt(t),r=nt(e,n),i=Z.get(r);return!i||!rt(i.jobShape,n)||!it(i.geometryBytes,e)?null:(Z.delete(r),Z.set(r,i),i)}function Qe(e){let{geometries:t,...n}=e;return{...n,buffers:t.map(X),closed:t.map(e=>e.closed)}}function $e(e,t,n){let r=Qe(n),i=e.byteLength;for(let e of n.geometries)i+=e.positions.byteLength+e.normals.byteLength+e.indices.byteLength,i+=(e.uvs?.byteLength??0)+(e.colors?.byteLength??0)+(e.tangents?.byteLength??0);if(i>Xe)return r;let a=tt(t),o=nt(e,a),s=Z.get(o);s&&(Z.delete(o),$(s)),r.buffers.forEach(u),Z.set(o,{...r,geometryBytes:e.slice(),jobShape:a,bytes:i}),Q+=i;for(let[e,t]of Z){if(Q<=Xe)break;Z.delete(e),$(t)}return r}function et(){Z.forEach($),Z.clear()}function $(e){Q-=e.bytes;for(let t of e.buffers)d(t),t.dispose()}function tt(e){let t=0;for(let n of e)t+=2+4*n.windows.length;let n=new Int32Array(t),r=0;for(let t of e){n[r++]=+(t.kind===`merged`),n[r++]=t.windows.length;for(let e of t.windows)n[r++]=e.vertexStart,n[r++]=e.vertexCount,n[r++]=e.indexStart,n[r++]=e.indexCount}return n}function nt(e,t){let n=e.length,r=Math.max(1,Math.floor(n/1024)),i=2166136261;for(let t=0;t<n;t+=r)i=Math.imul(i^e[t],16777619);let a=2166136261;for(let e=0;e<t.length;e++)a=Math.imul(a^t[e],16777619);return`${n}:${i>>>0}:${t.length}:${a>>>0}`}function rt(e,t){if(e.length!==t.length)return!1;for(let n=0;n<e.length;n++)if(e[n]!==t[n])return!1;return!0}function it(e,t){if(e.length!==t.length)return!1;let n=0;if(e.byteOffset%4==0&&t.byteOffset%4==0){let r=e.length>>>2,i=new Uint32Array(e.buffer,e.byteOffset,r),a=new Uint32Array(t.buffer,t.byteOffset,r);for(;n<r;n++)if(i[n]!==a[n])return!1;n=r<<2}for(;n<e.length;n++)if(e[n]!==t[n])return!1;return!0}async function at(e,t,n){let{mergeByMaterial:r=!0,debug:i=!1,material:a}=t??{},{parseTime:o=0,perfStart:s=i?performance.now():0}=n??{};if(!e.compressedData)return[];let c=performance.now(),l={mergeByMaterial:r,debug:i,material:a,fallback:{materials:e.materials,groups:e.groups}},u=await ut(e.compressedData,l);return u.meshes?u.meshes:await mt(u,l)||lt(Se(u.raw),{mergeByMaterial:r,debug:i,material:a,parseTime:o,decodeTime:performance.now()-c,perfStart:s,blobBytes:i?vt(e.compressedData):0,fallback:{materials:e.materials,groups:e.groups}})}async function ot(e,t){let{mergeByMaterial:n=!0,debug:r=!1,material:i}=t??{},a=r?performance.now():0,o=performance.now(),s={mergeByMaterial:n,debug:r,material:i},c=await ut(e,s);if(c.meshes)return c.meshes;let l=await mt(c,s);if(l)return l;let u=Se(c.raw),d=performance.now()-o,f=e.byteLength;return lt(u,{mergeByMaterial:n,debug:r,material:i,parseTime:0,decodeTime:d,perfStart:a,blobBytes:f})}let st=!1;function ct(e,t){if(e||st)return;let n=0;for(let e of t)n+=e.meshes.length;n<1e3||(st=!0,s().warn(`Parsing ${n} meshes with mergeByMaterial: false — each becomes its own THREE object, and render cost scales with object count. Drop the option to merge by material (the default); per-object identity survives it via userData.members.`))}function lt(e,t){let{mergeByMaterial:n,debug:r,material:i,parseTime:a,decodeTime:o,perfStart:c,blobBytes:l,fallback:u}=t,d=e.metadata.materials??u?.materials??[],f=e.metadata.groups??u?.groups??[],p=!!(e.flags&1);Y(f,d.length,e.vertices.length/3,e.indices.length);let m=p?e.vertices:He(e.vertices,e.origin,e.scale);if(r){let t=e.vertices.byteLength+e.indices.byteLength;s().debug(`Mesh Batch Stats:`),s().debug(` Materials: ${d.length} | Groups: ${f.length}`),s().debug(` Vertices: ${e.vertices.length/3} | Indices: ${e.indices.length}`),s().debug(` Format: ${p?`float32`:`int16 quantized`}`),s().debug(` Blob: ${(l/1024/1024).toFixed(2)} MB | Geometry on wire: ${(t/1024/1024).toFixed(2)} MB`)}ct(n,f);let h=performance.now(),g=Le(d,{vertexColors:e.colors!=null,hasUvs:e.uvs!=null,appearance:i}),_=[],v=n?We(f):f;for(let t of v)if(n&&t.meshes.length>1){let n=Ge(t,m,e.indices,g,e.uvs,e.colors);_.push(n)}else{let n=Je(t,m,e.indices,g,e.uvs,e.colors);for(let e of n)_.push(e)}let y=performance.now()-h;if(r){let e=performance.now()-c;s().debug(`Performance:`),a>0&&s().debug(` Parse JSON: ${a.toFixed(2)}ms`),s().debug(` Decode binary: ${o.toFixed(2)}ms`),s().debug(` Create Meshes: ${y.toFixed(2)}ms`),s().debug(` Total: ${e.toFixed(2)}ms`)}return Promise.resolve(_)}async function ut(e,t){let n=j(e);if(!me(n))return{raw:H(n),geometryBytes:null,source:n,reparse:()=>H(n)};let r=he(n),i=ft(r,t);if(i)return{meshes:i};let a=r.geometryBlob,o=()=>{let e=H(a);return e.metadata=r.metadata,e},s=await dt(r),c=s?H(s):o();return c.metadata=r.metadata,{raw:c,geometryBytes:a,source:a,reparse:o}}async function dt(e){if(N(e.geometryBlob)<4194304||!ke())return null;try{return await te(e.geometryBlob)}catch{return null}}function ft(e,t){let n=e.metadata.materials??t.fallback?.materials??[],r=e.metadata.groups??t.fallback?.groups??[],{jobs:i,jobRefs:a}=pt(r,t.mergeByMaterial),o=Ze(e.geometryBlob,i);return o?(Y(r,n.length,o.vertexCount,o.indexCount),_t(o,a,n,t.material)):null}function pt(e,t){let n=e=>({vertexStart:e.vertexStart,vertexCount:e.vertexCount,indexStart:e.indexStart,indexCount:e.indexCount,closed:e.closed}),r=[],i=[],a=t?We(e):e;for(let e of a)if(t&&e.meshes.length>1)r.push({kind:`merged`,windows:e.meshes.map(n)}),i.push({kind:`merged`,group:e});else for(let t of e.meshes)r.push({kind:`single`,windows:[n(t)]}),i.push({kind:`single`,group:e,meshMeta:t});return{jobs:r,jobRefs:i}}async function mt(e,t){let{raw:n,geometryBytes:r}=e,i=n.planarByteSplit?n.indexData.length/(n.uint16Indices?2:4):n.indexData.length;if(i/3<5e4)return null;let a=n.metadata.materials??t.fallback?.materials??[],o=n.metadata.groups??t.fallback?.groups??[];Y(o,a.length,n.vertexCount,i);let{jobs:c,jobRefs:l}=pt(o,t.mergeByMaterial),u=await gt(n,c,e.source);if(!u)return ht(n,e.source)&&(e.raw=e.reparse()),null;let d={geometries:u,hasUvs:n.uvs!=null,hasColors:n.colors!=null,vertexCount:n.vertexCount,indexCount:i},f=r?$e(r,c,d):Qe(d);return t.debug&&s().debug(`Mesh batch assembled: ${u.length} geometries, ${i/3} triangles`),_t(f,l,a,t.material)}function ht(e,t){return e.vertexData.buffer!==t.buffer&&e.indexData.buffer!==t.buffer}async function gt(e,t,n){let r=ke(),i={vertexData:e.vertexData,isFloat32:e.isFloat32,deltaEncoded:e.deltaEncoded,planarByteSplit:e.planarByteSplit,uint16Indices:e.uint16Indices,origin:e.origin,scale:e.scale,indexData:e.indexData,uvs:e.uvs,colors:e.colors,jobs:t};if(!r)return Ee(i).map((e,n)=>U(e,t[n].windows));ht(e,n)||(i.vertexData=e.vertexData.slice(),i.indexData=e.indexData.slice(),i.uvs=e.uvs?.slice()??null,i.colors=e.colors?.slice()??null);let a=/* @__PURE__ */ new Set([i.vertexData.buffer,i.indexData.buffer]);i.uvs&&a.add(i.uvs.buffer),i.colors&&a.add(i.colors.buffer);try{let e=await Ae(r,i,[...a]);return e.length===t.length?e:null}catch(e){return s().warn(`Mesh assembly worker failed; falling back to main-thread parse.`,e),null}}function _t(e,t,n,r){let i=Le(n,{vertexColors:e.hasColors,hasUvs:e.hasUvs,appearance:r}),a=[];for(let n=0;n<e.buffers.length;n++){let r=e.buffers[n],o=t[n],s=i(o.group.materialId,e.closed[n]),c=o.kind===`merged`?qe(r,o.group,s):Ye(r,o.meshMeta,s);a.push(c)}return a}function vt(e){return Math.floor(e.length*3/4)}const yt={Angstroms:1e-10,Nanometers:1e-9,Microns:1e-6,Millimeters:.001,Centimeters:.01,Decimeters:.1,Meters:1,Dekameters:10,Hectometers:100,Kilometers:1e3,Megameters:1e6,Gigameters:1e9,Microinches:2.54e-8,Mils:254e-7,Inches:.0254,Feet:.3048,Yards:.9144,Miles:1609.344,NauticalMiles:1852};function bt(e){let t=e.split(`.`);return t.includes(`Display`)||t.includes(`DisplayBatch`)}const xt=/* @__PURE__ */ new Set;async function St(e,t){let n=performance.now(),r=[],{allowScaling:i=!0,allowAutoPosition:a=!1,groundAxis:o=`z`,debug:s=!1,parsing:c={}}=t??{};try{return await wt(e,r,i?Ct(e.modelunits):1,c,s),a&&Ot(r,o),r}catch(e){throw kt(e,r),e}finally{s&&jt(n)}}function Ct(e){let t=yt[e];return t===void 0?(xt.has(e)||(xt.add(e),s().warn(`Unknown Rhino model unit "${e}" — geometry will not be scaled (factor 1). Known units: ${Object.keys(yt).join(`, `)}.`)),1):t}async function wt(e,t,n,r,i){for(let a of e.values){let e=a.InnerTree;for(let a in e){let o=e[a];o&&await Tt(o,t,n,r,i)}}}async function Tt(e,t,n,r,i){for(let a of e){if(!bt(a.type))continue;let e={debug:!1,...r,mergeByMaterial:r.mergeByMaterial??!0},o=Et(a.data);if(!o){s().error(`Error parsing display batch envelope: invalid JSON`);continue}let c=await at(o,e),l=k(o.items),u=[...c,...l];if(n!==1)for(let e of u)e.scale.set(n,n,n);for(let e of u)t.push(e);i&&s().debug(`Extracted ${c.length} meshes and ${l.length} items from batch`)}}function Et(e){return typeof e==`string`?Dt(e):e}function Dt(e){try{return JSON.parse(e)}catch{return}}function Ot(t,n){if(t.length===0)return;let i=e(t);r(t,i.min[n],n)}function kt(e,t){s().error(`An unexpected error occurred:`,e),At(t)}function At(e){for(let t of e)c(t)}function jt(e){let t=performance.now()-e;s().info(`Time to process meshes:`,`${t.toFixed(2)}ms`)}export{yt as SCALE_FACTORS,et as clearGeometryCache,St as getThreeObjectsFromComputeResponse,w as meshPolicy,k as parseDisplayItems,ot as parseMeshBatchBlob,at as parseMeshBatchObject,Me as setTextureAnisotropy};
68
68
  //# sourceMappingURL=parse.js.map