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

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.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{inflateSync as g}from"fflate";function _(){let e=globalThis.Buffer;return typeof e==`function`?e:void 0}function v(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 y(e){let r=v(e),i=r===null?null:b(r);if(i===null)throw new t(`Invalid base64 input.`,n.ENCODING_ERROR,{context:{inputLength:e.length}});return i}function b(e){let r=_();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 x(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 S(e){e.forEach(e=>c(e,{materials:!1}))}const C={clone:x,release:S};function w(e,t){let n=t??1;return{color:new f.Color(e??`#ffffff`),transparent:n<1,opacity:n}}function T(e){let t=E(e);if(!t)return null;let n=new m;n.setPositions(t);let r=w(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 E(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 D(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({...w(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 O(e){if(!e||e.length===0)return[];let t=[];for(let n of e)switch(n.kind){case`curve`:{let e=T(n);e&&t.push(e);break}case`point`:{let e=D(n);e&&t.push(e);break}default:{let e=n;s().warn(`Skipping unknown display item kind: ${String(e.kind)}`);break}}return t}const k=new Uint16Array(new Uint8Array([1,0]).buffer)[0]===1;function A(e){return typeof e==`string`?y(e):e instanceof Uint8Array?e:new Uint8Array(e)}function j(e){if(e.byteLength<8)return e;let t=new DataView(e.buffer,e.byteOffset,e.byteLength);if(t.getUint32(0,!0)!==1515605075)return e;let n=t.getUint32(4,!0),r=e.subarray(8),i=Math.max(r.byteLength*1032+1024,1<<20);if(n>i)throw z(`SLVZ header declares an implausible uncompressed length`,{uncompressedLen:n,deflatedBytes:r.byteLength,maxPlausibleLen:i});let a;try{a=g(r,{out:new Uint8Array(n+1)})}catch(e){throw z(`Failed to inflate SLVZ blob: ${e instanceof Error?e.message:String(e)}`,{uncompressedLen:n,deflatedBytes:r.byteLength})}if(a.byteLength!==n)throw z(`SLVZ payload inflated to a different size than the header declares.`,{declaredLen:n,actualLen:a.byteLength,deflatedBytes:r.byteLength});return a}function M(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 ee(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 N(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 P(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 te(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 F(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 I(e){return e>>>1^-(e&1)}function L(e){let t=new Int16Array(e.length),n=0,r=0,i=0;for(let a=0;a<e.length;a+=3)n=n+I(e[a])<<16>>16,r=r+I(e[a+1])<<16>>16,i=i+I(e[a+2])<<16>>16,t[a]=n,t[a+1]=r,t[a+2]=i;return t}function R(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+I(e[t]|e[r*3+t]<<8)<<16>>16,a=a+I(e[r+t]|e[r*4+t]<<8)<<16>>16,o=o+I(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 ne(e,t){let n=new Uint16Array(t),r=0;for(let i=0;i<t;i++)r=r+I(e[i]|e[t+i]<<8)&65535,n[i]=r;return n}function re(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+I(a)>>>0,n[i]=r}return n}function ie(e){let t=new Uint16Array(e.length),n=0;for(let r=0;r<e.length;r++)n=n+I(e[r])&65535,t[r]=n;return t}function ae(e){let t=new Uint32Array(e.length),n=0;for(let r=0;r<e.length;r++)n=n+I(e[r])>>>0,t[r]=n;return t}function z(e,r){return new t(e,n.VALIDATION_ERROR,{context:r})}function oe(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=N(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+I(t[e]|t[i*2+e]<<8)&65535,o=o+I(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=P(e.buffer,m,d);h=new Float32Array(d);let n=0,r=0;for(let e=0;e<d;e+=2)i?(n=n+I(t[e])&65535,r=r+I(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 se(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+I(a[e])&255,c=c+I(a[e+1])&255,l=l+I(a[e+2])&255,o[e]=s,o[e+1]=c,o[e+2]=l;return o}function ce(e){return e.byteLength>=4&&new DataView(e.buffer,e.byteOffset,4).getUint32(0,!0)===1297501267}function le(e){let t=ue(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=j(o);break;case 1280590157:i=M(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}=fe(r),s=pe(i,a);return{geometryBlob:n,metadata:{materials:s,groups:o}}}function ue(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 de(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 fe(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]=M(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=de(t,a,s),p=de(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 pe(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 me){let r=e[n];if(r?.startsWith(`slvm:tex:`)){let i=t[Number(r.slice(9))];i!==void 0&&(e[n]=he(i))}}return n}const me=[`map`,`roughnessMap`,`normalMap`];function he(e){let t={bytes:e,pos:0},n=B(t),r=M(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 ge(e){let t;t=e.isFloat32?e.vertexData:e.planarByteSplit?R(e.vertexData,e.vertexCount):e.deltaEncoded?L(e.vertexData):e.vertexData;let n;if(e.planarByteSplit){let t=e.indexData;n=e.uint16Indices?ne(t,t.length/2):re(t,t.length/4)}else n=e.deltaEncoded?e.indexData instanceof Uint16Array?ie(e.indexData):ae(e.indexData):e.indexData;return F(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(!k)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=A(e);if(ce(r)){let e=le(r),t=V(e.geometryBlob);return t.metadata=e.metadata,t}let i=j(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(M(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?N(i.buffer,T,C):S?i.subarray(o,o+w):x?P(i.buffer,T,C):ee(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),F=D*(O?2:4);if(o+F>i.byteLength)throw z(`Insufficient data to read indices.`,{expectedBytes:F,availableBytes:i.byteLength-o,offset:o,indexCount:D,useUint16Indices:O});let I=S?i.subarray(o,o+F):O?P(i.buffer,i.byteOffset+o,D):te(i.buffer,i.byteOffset+o,D);o+=F;let L=null;if(f&8){let e=oe(i,a,o,y,x,S);L=e.uvs,o=e.offset}let R=null;return f&16&&(R=se(i,o,y,x)),{metadata:d,flags:f,vertexData:E,indexData:I,isFloat32:b,deltaEncoded:x,planarByteSplit:S,uint16Indices:O,vertexCount:y,origin:[p,m,h],scale:[g,_,v],uvs:L,colors:R}}function _e(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=(n,i,s)=>{if(s<12||s%3!=0)return!1;let c=2*n+1,l=c+1,u=0,d=0;for(let n=0;n<s;n++){let r=t[i+n];a[r]!==c&&(a[r]=c,d++,u=Math.max(u,Math.abs(e[r*3]),Math.abs(e[r*3+1]),Math.abs(e[r*3+2])))}if(!(u>0)||!Number.isFinite(u))return!1;let f=1/(2*2**(Math.floor(Math.log2(u))-23)),p=r(d),m=p-1,h=new Int32Array(p).fill(-1),g=new Int32Array(p),_=new Int32Array(p),v=new Int32Array(p),y=(e,t,n)=>{let r=Math.imul(e,2654435761)^Math.imul(t,2246822519)^Math.imul(n,3266489917);for(r=(r^r>>>15)&m;h[r]!==-1&&(g[r]!==e||_[r]!==t||v[r]!==n);)r=r+1&m;return r},b=(e,t)=>{let n=e-t;return n>.25?t+1:n<-.25?t-1:t},x=new Int32Array(s),S=new Int32Array(d),C=0;for(let n=0;n<s;n++){let r=t[i+n];if(a[r]===l){x[n]=o[r];continue}let s=e[r*3]*f,c=e[r*3+1]*f,u=e[r*3+2]*f,d=Math.round(s),p=Math.round(c),m=Math.round(u),w=b(s,d),T=b(c,p),E=b(u,m),D=-1;for(let e=0;e<8&&D===-1;e++){if(e&1&&w===d||e&2&&T===p||e&4&&E===m)continue;let t=y(e&1?w:d,e&2?T:p,e&4?E:m);h[t]!==-1&&(D=h[t])}if(D===-1){let e=y(d,p,m);D=C++,h[e]=D,g[e]=d,_[e]=p,v[e]=m,S[D]=r}a[r]=l,o[r]=D,x[n]=D}let w=new Int32Array(C+1),T=0;for(let e=0;e<s;e+=3){let t=x[e],n=x[e+1],r=x[e+2];t!==n&&n!==r&&r!==t&&(w[t+1]+=1,w[n+1]+=1,w[r+1]+=1,T++)}if(T<4)return!1;for(let e=0;e<C;e++)w[e+1]+=w[e];let E=new Int32Array(w[C]),D=w.slice(0,C);for(let e=0;e<s;e+=3){let t=x[e],n=x[e+1],r=x[e+2];t!==n&&n!==r&&r!==t&&(E[D[t]]=n,D[t]+=1,E[D[n]]=r,D[n]+=1,E[D[r]]=t,D[r]+=1)}for(let e=0;e<C;e++){let t=w[e],n=w[e+1];for(let r=t;r<n;r++){let n=E[r];for(let e=t;e<r;e++)if(E[e]===n)return!1;let i=!1;for(let t=w[n],r=w[n+1];t<r&&!i;t++)i=E[t]===e;if(!i)return!1}}let O=S[x[0]]*3,k=e[O],A=e[O+1],j=e[O+2],M=0;for(let t=0;t<s;t+=3){let n=S[x[t]]*3,r=S[x[t+1]]*3,i=S[x[t+2]]*3,a=e[n]-k,o=e[n+1]-A,s=e[n+2]-j,c=e[r]-k,l=e[r+1]-A,u=e[r+2]-j,d=e[i]-k,f=e[i+1]-A,p=e[i+2]-j;M+=a*(l*p-u*f)+o*(u*d-c*p)+s*(c*f-l*d)}return M>0},c=0;for(let e=0;e<n.length;e++){let t=n[e];if(!s(e,c,t))return!1;c+=t}return!0}function ve(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=(e,t)=>{for(let n=0;n<3;n++)if(c(i,e*3+n)!==c(i,t*3+n)||r&&r[e*3+n]!==r[t*3+n])return!1;return!s||s[e*2]===s[t*2]&&s[e*2+1]===s[t*2+1]},u=16;for(;u<a*2;)u*=2;let d=u-1,f=new Int32Array(u).fill(-1),p=new Uint32Array(a);for(let e=0;e<a;e++){let t=Math.imul(c(i,e*3),2654435761)^Math.imul(c(i,e*3+1),2246822519)^Math.imul(c(i,e*3+2),3266489917);for(t=(t^t>>>15)&d;f[t]!==-1&&!l(f[t],e);)t=t+1&d;f[t]===-1&&(f[t]=e),p[e]=f[t]}for(let e=0;e<o;e++)t[e]=p[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<o;e++){let n=t[e],r=m[e],i=r===0?n:a+_[n]+r-1;t[e]=i;let o=Math.floor(e/3)*3,s=l[o/3];C[i*3]+=c[o]*s,C[i*3+1]+=c[o+1]*s,C[i*3+2]+=c[o+2]*s}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 ye(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,ee=D*b+O*C,N=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]+=ee,o[r*3+2]+=N}}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=_e(e.positions,e.indices,t),r=ve(e.positions,e.indices,e.uvs,e.colors),i=r.uvs?ye(r.positions,r.normals,r.indices,r.uvs):null;return{...r,closed:n,tangents:i}}function be(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 xe(){return[`const assemble = ${be.toString()};`,`const isClosedSolid = ${_e.toString()};`,`const splitCreases = ${ve.toString()};`,`const grainTangents = ${ye.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 Se=1;function Ce(){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([xe()],{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 we(e,t,n){return new Promise((r,i)=>{let a=Se++;W.set(a,{resolve:r,reject:i}),e.postMessage({id:a,input:t},n)})}let Te=1;function Ee(e){Te=Math.max(1,e)}a(Ee);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=Te,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 De={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}]}},Oe=.35,K=e=>e.toFixed(4);function ke(e,t){let n=De[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 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>
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(Oe)} * l0 );
37
+ float a = selvaFibreRow( uv, pitch * exp2( l0 ), len, seed + 7.0 * l0 ) * exp2( -${K(Fe)} * l0 );
38
38
  float b = selvaFibreRow( uv, pitch * exp2( l0 + 1.0 ), len, seed + 7.0 * l0 + 7.0 )
39
- * exp2( -${K(Oe)} * ( l0 + 1.0 ) );
39
+ * exp2( -${K(Fe)} * ( 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 ) {
@@ -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 Ae(e,t){return t?.appearance?.cullBackfaces?!0:(t?.closed??!1)&&!e.transparent&&e.opacity>=1}function je(e,t){let n=/* @__PURE__ */ new Map;return(r,i)=>{let a=e[r],o=Ae(a,{...t,closed:i}),s=`${r}:${o}`,c=n.get(s);return c||(c=Me(a,{...t,closed:o}),n.set(s,c)),c}}function Me(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:Ae(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&&Pe(s);let c=e.finish&&t?.hasUvs?ke(e.finish,e.finishStrength??1):null;c&&(s.defines={...s.defines,USE_UV:``},Ne(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 Ne(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 Pe(e){Ne(e,`srgb-vertex-colors`,e=>{e.vertexShader=e.vertexShader.replace(`#include <color_vertex>`,`#include <color_vertex>
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>
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 Fe(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 Ie(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 Le(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 Re(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])=>Le({materialId:e,meshes:t}))}function ze(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 Fe(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 Ve(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 Be(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 Ve(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:Be(t)},r}function He(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 Fe(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(Ue(Y(g),s,r(e.materialId,g.closed)))}return o}function Ue(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 We=268435456,X=/* @__PURE__ */ new Map;let Z=0;function Ge(e,t){let n=Ye(t),r=Xe(e,n),i=X.get(r);return!i||!Ze(i.jobShape,n)||!Qe(i.geometryBytes,e)?null:(X.delete(r),X.set(r,i),i)}function Ke(e){let{geometries:t,...n}=e;return{...n,buffers:t.map(Y),closed:t.map(e=>e.closed)}}function qe(e,t,n){let r=Ke(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>We)return r;let a=Ye(t),o=Xe(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<=We)break;X.delete(e),Q(t)}return r}function Je(){X.forEach(Q),X.clear()}function Q(e){Z-=e.bytes;for(let t of e.buffers)d(t),t.dispose()}function Ye(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 Xe(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 Ze(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 Qe(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 $e(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=it(e.compressedData,l);if(u.meshes)return u.meshes;let d=u.raw;return await st(d,l,u.geometryBytes)||rt(ge(d),{mergeByMaterial:r,debug:i,material:a,parseTime:o,decodeTime:performance.now()-c,perfStart:s,blobBytes:i?ut(e.compressedData):0,fallback:{materials:e.materials,groups:e.groups}})}async function et(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=it(e,s);if(c.meshes)return c.meshes;let l=c.raw,u=await st(l,s,c.geometryBytes);if(u)return u;let d=ge(l),f=performance.now()-o,p=e.byteLength;return rt(d,{mergeByMaterial:n,debug:r,material:i,parseTime:0,decodeTime:f,perfStart:a,blobBytes:p})}let tt=!1;function nt(e,t){if(e||tt)return;let n=0;for(let e of t)n+=e.meshes.length;n<1e3||(tt=!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 rt(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:Ie(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`)}nt(n,f);let h=performance.now(),g=je(d,{vertexColors:e.colors!=null,hasUvs:e.uvs!=null,appearance:i}),_=[],v=n?Re(f):f;for(let t of v)if(n&&t.meshes.length>1){let n=ze(t,m,e.indices,g,e.uvs,e.colors);_.push(n)}else{let n=He(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(_)}function it(e,t){let n=A(e);if(!ce(n))return{raw:V(n),geometryBytes:null};let r=le(n),i=at(r,t);if(i)return{meshes:i};let a=V(r.geometryBlob);return a.metadata=r.metadata,{raw:a,geometryBytes:r.geometryBlob}}function at(e,t){let n=e.metadata.materials??t.fallback?.materials??[],r=e.metadata.groups??t.fallback?.groups??[],{jobs:i,jobRefs:a}=ot(r,t.mergeByMaterial),o=Ge(e.geometryBlob,i);return o?(J(r,n.length,o.vertexCount,o.indexCount),lt(o,a,n,t.material)):null}function ot(e,t){let n=e=>({vertexStart:e.vertexStart,vertexCount:e.vertexCount,indexStart:e.indexStart,indexCount:e.indexCount}),r=[],i=[],a=t?Re(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 st(e,t,n){let r=e.planarByteSplit?e.indexData.length/(e.uint16Indices?2:4):e.indexData.length;if(r/3<5e4)return null;let i=e.metadata.materials??t.fallback?.materials??[],a=e.metadata.groups??t.fallback?.groups??[];J(a,i.length,e.vertexCount,r);let{jobs:o,jobRefs:c}=ot(a,t.mergeByMaterial),l=await ct(e,o);if(!l)return null;let u={geometries:l,hasUvs:e.uvs!=null,hasColors:e.colors!=null,vertexCount:e.vertexCount,indexCount:r},d=n?qe(n,o,u):Ke(u);return t.debug&&s().debug(`Mesh batch assembled: ${l.length} geometries, ${r/3} triangles`),lt(d,c,i,t.material)}async function ct(e,t){let n={vertexData:e.vertexData.slice(),isFloat32:e.isFloat32,deltaEncoded:e.deltaEncoded,planarByteSplit:e.planarByteSplit,uint16Indices:e.uint16Indices,origin:e.origin,scale:e.scale,indexData:e.indexData.slice(),uvs:e.uvs?.slice()??null,colors:e.colors?.slice()??null,jobs:t},r=Ce();if(!r)return be(n).map((e,n)=>H(e,t[n].windows.map(e=>e.indexCount)));let i=[n.vertexData.buffer,n.indexData.buffer];n.uvs&&i.push(n.uvs.buffer),n.colors&&i.push(n.colors.buffer);try{let e=await we(r,n,i);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 lt(e,t,n,r){let i=je(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`?Ve(r,o.group,s):Ue(r,o.meshMeta,s);a.push(c)}return a}function ut(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 dt(e){let t=e.split(`.`);return t.includes(`Display`)||t.includes(`DisplayBatch`)}const ft=/* @__PURE__ */ new Set;async function pt(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 ht(e,r,i?mt(e.modelunits):1,c,s),a&&yt(r,o),r}catch(e){throw bt(e,r),e}finally{s&&St(n)}}function mt(e){let t=$[e];return t===void 0?(ft.has(e)||(ft.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 ht(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 gt(o,t,n,r,i)}}}async function gt(e,t,n,r,i){for(let a of e){if(!dt(a.type))continue;let e={debug:!1,...r,mergeByMaterial:r.mergeByMaterial??!0},o=_t(a.data);if(!o){s().error(`Error parsing display batch envelope: invalid JSON`);continue}let c=await $e(o,e),l=O(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 _t(e){return typeof e==`string`?vt(e):e}function vt(e){try{return JSON.parse(e)}catch{return}}function yt(t,n){if(t.length===0)return;let i=e(t);r(t,i.min[n],n)}function bt(e,t){s().error(`An unexpected error occurred:`,e),xt(t)}function xt(e){for(let t of e)c(t)}function St(e){let t=performance.now()-e;s().info(`Time to process meshes:`,`${t.toFixed(2)}ms`)}export{$ as SCALE_FACTORS,Je as clearGeometryCache,pt as getThreeObjectsFromComputeResponse,C as meshPolicy,O as parseDisplayItems,et as parseMeshBatchBlob,$e as parseMeshBatchObject,Ee as setTextureAnisotropy};
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};
68
68
  //# sourceMappingURL=parse.js.map