import{$ as e,At as t,Bt as n,C as r,Ct as i,Dt as a,E as o,Et as s,Ht as c,K as l,Mt as u,Ot as d,Pt as f,S as p,St as m,bt as h,c as g,d as _,dt as v,et as y,f as b,ft as x,h as S,ht as C,it as w,l as T,o as E,ot as D,p as O,pt as k,r as A,u as j,v as M,wt as N,x as P,yt as F,zt as I}from"./three.module-BFsVh7r1.js";import{t as L}from"./fflate.module-BIQmJeC9.js";var R=/^def\s+(?:(\w+)\s+)?"?([^"]+)"?$/,z=/^string\s+(\w+)$/,B=/^(?:uniform\s+)?(\w+(?:\[\])?)\s+(.+)$/,V=class{parseText(e){e=this._preprocess(e);let t={},n=e.split(` `),r=null,i=t,a=[t];for(let e of n)if(e.includes(`=`)){let t=this._findAssignmentOperator(e);if(t===-1){r=e.trim();continue}let n=e.slice(0,t).trim(),o=e.slice(t+1).trim();if(o.endsWith(`{`)){let e={};a.push(e),i[n]=e,i=e}else if(o.endsWith(`(`)){let e=o.slice(0,-1);i[n]=e;let t={};a.push(t),i=t}else i[n]=o}else if(e.includes(`:`)&&!e.includes(`=`)){let t=e.indexOf(`:`),n=e.slice(0,t).trim(),r=e.slice(t+1).trim();/^[\d.]+$/.test(n)&&(i[n]=r)}else if(e.endsWith(`{`)){let e=i[r]||{};a.push(e),i[r]=e,i=e}else if(e.endsWith(`}`)){if(a.pop(),a.length===0)continue;i=a[a.length-1]}else if(e.endsWith(`(`)){let t={};a.push(t),r=e.split(`(`)[0].trim()||r,i[r]=t,i=t}else e.endsWith(`)`)?(a.pop(),i=a[a.length-1]):e.trim()&&(r=e.trim());return t}_preprocess(e){e=this._stripBlockComments(e),e=this._collapseTripleQuotedStrings(e);let t=e.split(` `),n=[],r=!1,i=0,a=0,o=``;for(let e=0;e0?a++:e===`)`&&i>0&&a--;i===0&&a===0&&(n.push(o),o=``,r=!1)}else{if(c.includes(`=`)){let e=this._findAssignmentOperator(c);if(e!==-1){let t=c.slice(e+1).trim(),n=0,s=0;for(let e of t)e===`[`?n++:e===`]`&&s++;if(n>s){r=!0,i=n-s,a=0,o=c;continue}}}n.push(c)}}return n.join(` `)}_stripBlockComments(e){let t=``,n=0;for(;n{let i=[];for(let o in e){if(o===`#usda 1.0`||o===`variants`)continue;let s=o.match(R);if(s){let c=s[1]||``,l=s[2],u=t===`/`?`/`+l:t+`/`+l;i.push(l);let d={typeName:c},f=e[o];this._extractPrimData(f,u,d,n,r),n[u]={specType:r.Prim,fields:d},a(f,u)}}i.length>0&&n[t]&&(n[t].fields.primChildren=i)};return a(t,`/`),{specsByPath:n}}_extractPrimData(e,t,n,r,i){if(!(!e||typeof e!=`object`))for(let a in e){if(a.startsWith(`def `))continue;if(a===`prepend references`){n.references=[e[a]];continue}if(a===`payload`){n.payload=e[a];continue}if(a===`variants`){let t={},r=e[a];for(let e in r){let n=e.match(z);if(n){let i=n[1];t[i]=r[e].replace(/"/g,``)}}Object.keys(t).length>0&&(n.variantSelection=t);continue}if(a.startsWith(`rel `)){let n=a.slice(4),o=t+`.`+n,s=e[a].replace(/[<>]/g,``);r[o]={specType:i.Relationship,fields:{targetPaths:[s]}};continue}if(a.includes(`xformOpOrder`)){n.xformOpOrder=e[a].replace(/[\[\]]/g,``).split(`,`).map(e=>e.trim().replace(/"/g,``));continue}let o=a.match(B);if(o){let n=o[1],s=o[2],c=e[a];if(s.endsWith(`.connect`)){let e=s.slice(0,-8),a=t+`.`+e,o=String(c).trim();o.startsWith(`<`)&&(o=o.slice(1)),o.endsWith(`>`)&&(o=o.slice(0,-1)),r[a]||(r[a]={specType:i.Attribute,fields:{typeName:n}}),r[a].fields.connectionPaths=[o];continue}if(s.endsWith(`.timeSamples`)&&typeof c==`object`){let e=s.slice(0,-12),a=t+`.`+e,o=[],l=[];for(let e in c){let t=parseFloat(e);isNaN(t)||(o.push(t),l.push(this._parseAttributeValue(n,c[e])))}let u=o.map((e,t)=>({t:e,v:l[t]})).sort((e,t)=>e.t-t.t);r[a]={specType:i.Attribute,fields:{timeSamples:{times:u.map(e=>e.t),values:u.map(e=>e.v)},typeName:n}}}else{let e=this._parseAttributeValue(n,c),a=t+`.`+s;r[a]={specType:i.Attribute,fields:{default:e,typeName:n}}}}}}_parseAttributeValue(e,t){if(t==null)return;let n=String(t).trim();if(e.endsWith(`[]`))try{let e=n.replace(/\(/g,`[`).replace(/\)/g,`]`);e.endsWith(`,`)&&(e=e.slice(0,-1));let t=JSON.parse(e);return Array.isArray(t)&&Array.isArray(t[0])?t.flat():t}catch{return n.replace(/[\[\]]/g,``).split(`,`).map(e=>{let t=e.trim(),n=parseFloat(t);return isNaN(n)?t.replace(/"/g,``):n})}if(e.includes(`3`)||e.includes(`2`)||e.includes(`4`))return n.replace(/[()]/g,``).split(`,`).map(e=>parseFloat(e.trim()));if(e.startsWith(`quat`)){let e=n.replace(/[()]/g,``).split(`,`).map(e=>parseFloat(e.trim()));return[e[1],e[2],e[3],e[0]]}return e.includes(`matrix`)?n.replace(/[()]/g,``).split(`,`).map(e=>parseFloat(e.trim())):e===`float`||e===`double`||e===`int`?parseFloat(n):e===`string`||e===`token`?this._parseString(n):e===`asset`?n.replace(/@/g,``).replace(/"/g,``):this._parseString(n)}_parseString(e){(e.startsWith(`"`)&&e.endsWith(`"`)||e.startsWith(`'`)&&e.endsWith(`'`))&&(e=e.slice(1,-1));let t=``,n=0;for(;nn)break;let s=o>>4;if(s===15){let r;do{if(t>=n)break;r=e[t++],s+=r}while(r===255&&t0){t+s>n&&(s=n-t);for(let n=0;n=a);n++)r[i++]=e[t++]}if(t>=n||t+2>n)break;let c=e[t++]|e[t++]<<8;if(c===0)break;let l=(o&15)+4;if(l===19){let r;do{if(t>=n)break;r=e[t++],l+=r}while(r===255&&t=a);e++)r[i++]=r[u+e]}return i}function J(e,t){let n=new Uint8Array(t),r=e[0];if(r===0)return q(e,1,e.length,n,0,t),n;{let i=1,a=[];for(let t=0;t>>0;a.push(t),i+=4}let o=i,s=0;for(let i=0;i>3)+4;return ne(J(new Uint8Array(e),n),t)}function ne(e,t){let n=new DataView(e.buffer,e.byteOffset,e.byteLength),r=0,i=n.getInt32(r,!0);r+=4;let a=t*2+7>>3,o=r,s=r+a,c=new Int32Array(t),l=0,u=o,d=s;for(let r=0;r>e*2&3,o=0;switch(t){case 0:o=i;break;case 1:o=n.getInt8(d),d+=1;break;case 2:o=n.getInt16(d,!0),d+=2;break;case 3:o=n.getInt32(d,!0),d+=4;break}l+=o,c[r]=l}}return c}var re=class{constructor(e){this.buffer=e,this.view=new DataView(e),this.offset=0}seek(e){this.offset=e}tell(){return this.offset}readUint8(){let e=this.view.getUint8(this.offset);return this.offset+=1,e}readInt8(){let e=this.view.getInt8(this.offset);return this.offset+=1,e}readUint16(){let e=this.view.getUint16(this.offset,!0);return this.offset+=2,e}readInt16(){let e=this.view.getInt16(this.offset,!0);return this.offset+=2,e}readUint32(){let e=this.view.getUint32(this.offset,!0);return this.offset+=4,e}readInt32(){let e=this.view.getInt32(this.offset,!0);return this.offset+=4,e}readUint64(){let e=this.view.getUint32(this.offset,!0),t=this.view.getUint32(this.offset+4,!0);return this.offset+=8,t*4294967296+e}readInt64(){let e=this.view.getUint32(this.offset,!0),t=this.view.getInt32(this.offset+4,!0);return this.offset+=8,t*4294967296+e}readFloat32(){let e=this.view.getFloat32(this.offset,!0);return this.offset+=4,e}readFloat64(){let e=this.view.getFloat64(this.offset,!0);return this.offset+=8,e}readBytes(e){let t=new Uint8Array(this.buffer,this.offset,e);return this.offset+=e,t}readString(e){let t=this.readBytes(e),n=0;for(;n>16&255}get payload(){return this.lo+(this.hi&65535)*4294967296}getInlinedValue(){return this.lo}},ie=class{parseData(e){this.buffer=e instanceof ArrayBuffer?e:e.buffer,this.reader=new re(this.buffer),this.version={major:0,minor:0,patch:0},this._conversionBuffer=new ArrayBuffer(4),this._conversionView=new DataView(this._conversionBuffer),this._readBootstrap(),this._readTOC(),this._readTokens(),this._readStrings(),this._readFields(),this._readFieldSets(),this._readPaths(),this._readSpecs(),this.specsByPath={};for(let e of this.specs){let t=this.paths[e.pathIndex];if(!t)continue;let n=this._getFieldsForSpec(e);this.specsByPath[t]={specType:e.specType,fields:n}}return{specsByPath:this.specsByPath}}_readBootstrap(){let e=this.reader;if(e.seek(0),e.readString(8)!==`PXR-USDC`)throw Error(`Not a valid USDC file`);this.version.major=e.readUint8(),this.version.minor=e.readUint8(),this.version.patch=e.readUint8(),e.readBytes(5),this.tocOffset=e.readUint64()}_readTOC(){let e=this.reader;e.seek(this.tocOffset);let t=e.readUint64();this.sections={};for(let n=0;n1e3)return;let r=n.readUint32(),i=n.readUint32(),a=n.readUint8(),o=(a&1)!=0,s=(a&2)!=0,c=(a&4)!=0,l;if(e===``)l=`/`;else{let t=this.tokens[i]||``;l=c?e+`.`+t:e===`/`?`/`+t:e+`/`+t}if(this.paths[r]=l,o&&s){let r=n.readUint64();this._readPathsRecursive(l,t+1),n.seek(r),this._readPathsRecursive(e,t+1)}else o?this._readPathsRecursive(l,t+1):s&&this._readPathsRecursive(e,t+1)}_buildPathsFromCompressed(e,t,n){let r=(i,a)=>{let o=i;for(;o0||l===-1,f=l>=0;if(d)f&&r(i+l,a),a=u;else if(!f)break}};r(0,``)}_readSpecs(){let e=this.sections.SPECS;if(!e)return;let t=this.reader;if(t.seek(e.start),this.specs=[],this.version.major===0&&this.version.minor<4){let n=this.version.minor===0&&this.version.patch===1?16:12,r=Math.floor(e.size/n);for(let e=0;e=this.buffer.byteLength)throw RangeError(`USDCParser: Invalid payload offset `+i+` for type `+t+`.`);let a=this.reader.tell();this.reader.seek(i);let o;return o=n?this._readArrayValue(e):this._readScalarValue(t),this.reader.seek(a),o}_readInlinedValue(e){let t=e.typeEnum,n=e.getInlinedValue(),r=this._conversionView;switch(t){case G.Bool:return n!==0;case G.UChar:return n&255;case G.Int:case G.UInt:return n;case G.Float:return r.setUint32(0,n,!0),r.getFloat32(0,!0);case G.Double:return r.setUint32(0,n,!0),r.getFloat32(0,!0);case G.Token:return this.tokens[n]||``;case G.String:return this.tokens[this.strings[n]]||``;case G.AssetPath:return this.tokens[n]||``;case G.Specifier:return n;case G.Permission:case G.Variability:return n;case G.Vec2h:return r.setUint32(0,n,!0),[this._halfToFloat(r.getUint16(0,!0)),this._halfToFloat(r.getUint16(2,!0))];case G.Vec2f:case G.Vec2i:return r.setUint32(0,n,!0),[r.getInt8(0),r.getInt8(1)];case G.Vec3f:case G.Vec3i:return r.setUint32(0,n,!0),[r.getInt8(0),r.getInt8(1),r.getInt8(2)];case G.Vec4f:case G.Vec4i:return r.setUint32(0,n,!0),[r.getInt8(0),r.getInt8(1),r.getInt8(2),r.getInt8(3)];case G.Matrix2d:return r.setUint32(0,n,!0),[r.getInt8(0),0,0,r.getInt8(1)];case G.Matrix3d:return r.setUint32(0,n,!0),[r.getInt8(0),0,0,0,r.getInt8(1),0,0,0,r.getInt8(2)];case G.Matrix4d:return r.setUint32(0,n,!0),[r.getInt8(0),0,0,0,0,r.getInt8(1),0,0,0,0,r.getInt8(2),0,0,0,0,r.getInt8(3)];default:return n}}_readTimeSamples(e){let t=this.reader,n=e.payload,r=t.tell();t.seek(n);let i=t.tell(),a=t.readInt64();t.seek(i+a);let o=new X(t.readUint32(),t.readUint32()),s=this._readValue(o),c=i+a+8;t.seek(c);let l=t.tell(),u=t.readInt64();t.seek(l+u);let d=t.readUint64(),f=[];for(let e=0;e{let e=t.readUint64(),n=[];for(let r=0;r0?d:l&&l.length>0?l:f&&f.length>0?f:u&&u.length>0?u:null}case G.VariantSelectionMap:{let e=t.readUint64(),n={};for(let r=0;r2147483647)throw RangeError(`USDCParser: Array size `+i+` exceeds implementation limits.`);if(i===0)return[];if(r)return this._readCompressedArray(n,i);switch(n){case G.Int:{let e=new Int32Array(i);for(let n=0;n>15,n=(e&31744)>>10,r=e&1023;return n===0?r===0?t?-0:0:(t?-1:1)*W*(r/1024):n===31?r?NaN:t?-1/0:1/0:(t?-1:1)*U[n]*(1+r/1024)}_getFieldsForSpec(e){let t={},n=e.fieldSetIndex,r=0;for(;n0){let n=new e,r=new e,i=null;for(let e=0;e0){let t=e.slice(0,n),r=e.slice(n+1);this.childrenByPath.has(t)||this.childrenByPath.set(t,[]),this.childrenByPath.get(t).push({name:r,path:e})}else if(n===0&&e.length>1){let t=e.slice(1);this.childrenByPath.has(`/`)||this.childrenByPath.set(`/`,[]),this.childrenByPath.get(`/`).push({name:t,path:e})}let r=t.fields.typeName;if(r===`Material`){let t=e.split(`/`),n=t.length>1?`/`+t[1]:`/`;this.materialsByRoot.has(n)||this.materialsByRoot.set(n,[]),this.materialsByRoot.get(n).push(e)}if(r===`Shader`&&n>0){let t=e.slice(0,n);for(;t.length>0;){let n=this.specsByPath[t];if(n&&n.specType===Z.Prim&&n.fields.typeName===`Material`){this.shadersByMaterialPath.has(t)||this.shadersByMaterialPath.set(t,[]),this.shadersByMaterialPath.get(t).push(e);break}let r=t.lastIndexOf(`/`);if(r<=0)break;t=t.slice(0,r)}}if(r===`GeomSubset`&&n>0){let t=e.slice(0,n);this.geomSubsetsByMeshPath.has(t)||this.geomSubsetsByMeshPath.set(t,[]),this.geomSubsetsByMeshPath.get(t).push(e)}}else if(t.specType===Z.Attribute||t.specType===Z.Relationship){let n=e.lastIndexOf(`.`);if(n>0){let r=e.slice(0,n),i=e.slice(n+1);this.attributesByPrimPath.has(r)||this.attributesByPrimPath.set(r,new Map),this.attributesByPrimPath.get(r).set(i,t)}}}}_isDirectChild(e,t,n){if(!t.startsWith(n))return!1;let r=t.slice(n.length);return r.length===0||r.startsWith(`{`)?!1:!r.includes(`/`)}_buildHierarchy(e,t){let n=[],r=new Set,i=this.childrenByPath.get(t);if(i)for(let e of i)r.has(e.path)||(r.add(e.path),n.push(e));let a=this._getVariantPaths(t);for(let e of a){let t=this.childrenByPath.get(e);if(t)for(let e of t)r.has(e.path)||(r.add(e.path),n.push(e))}for(let{name:t,path:r}of n){let n=this.specsByPath[r];if(!n||n.specType!==Z.Prim)continue;let i=n.fields.typeName,a=this._getReferences(n);if(a.length>0){let o=this._getLocalVariantSelections(n.fields),s=[];for(let e of a){let t=this._resolveReference(e,o);t&&s.push(t)}if(s.length>0){let a=this._getAttributes(r);if(s.length===1){let o=this._findSingleMesh(s[0]);if(o&&(i===`Xform`||!i)){o.name=t,this.applyTransform(o,n.fields,a),this._applyMaterialBinding(o,r),e.add(o),this._buildHierarchy(o,r);continue}}let o=new v;o.name=t,this.applyTransform(o,n.fields,a);for(let e of s)for(;e.children.length>0;)o.add(e.children[0]);e.add(o),this._buildHierarchy(o,r);continue}}if(i===`SkelRoot`){let i=new v;i.name=t,i.userData.isSkelRoot=!0;let a=this._getAttributes(r);this.applyTransform(i,n.fields,a),e.add(i),this._buildHierarchy(i,r)}else if(i===`Skeleton`){let t=this._buildSkeleton(r);t&&(this.skeletons[r]=t),this._buildHierarchy(e,r)}else if(i!==`SkelAnimation`){if(i===`Mesh`){let t=this._buildMesh(r,n);t&&(e.add(t),this._buildHierarchy(t,r))}else if(i===`Camera`){let i=this._buildCamera(r);i.name=t;let a=this._getAttributes(r);this.applyTransform(i,n.fields,a),e.add(i),this._buildHierarchy(i,r)}else if(i===`DistantLight`||i===`SphereLight`||i===`RectLight`||i===`DiskLight`){let a=this._buildLight(r,i);a.name=t;let o=this._getAttributes(r);this.applyTransform(a,n.fields,o),e.add(a),this._buildHierarchy(a,r)}else if(i===`Cube`||i===`Sphere`||i===`Cylinder`||i===`Cone`||i===`Capsule`){let t=this._buildGeomPrimitive(r,n,i);t&&(e.add(t),this._buildHierarchy(t,r))}else if(!(i===`Material`||i===`Shader`||i===`GeomSubset`)){let i=new v;i.name=t;let a=this._getAttributes(r);this.applyTransform(i,n.fields,a),e.add(i),this._buildHierarchy(i,r)}}}}_getVariantPaths(e){let t=this.specsByPath[e],n=t?.fields?.variantSetChildren,r=[];if(!n||n.length===0)return r;for(let i of n){let n=this.externalVariantSelections[i]||null;if(!n){let e=t.fields.variantSelection;n=e?e[i]:null}if(!n){let t=e+`/{`+i+`=}`,r=this.specsByPath[t];r?.fields?.variantChildren&&(n=r.fields.variantChildren[0])}if(n){let t=e+`/{`+i+`=`+n+`}`;r.push(t)}}return r}_resolveFilePath(e){let t=e;return t.startsWith(`./`)&&(t=t.slice(2)),this.basePath?this.basePath+`/`+t:t}_resolveReference(e,t={}){if(!e)return null;let n=e.match(/@([^@]+)@(?:<([^>]+)>)?/);if(!n)return null;let i=n[1],a=n[2],s=this._resolveFilePath(i),c={...t,...this.externalVariantSelections},l=this.assets[s];if(!l)return null;if(l.specsByPath){let e=new r(this.manager),t=this._getBasePath(s),n=e.compose(l,this.assets,c,t);if(a){let e=a.split(`/`).pop(),t=null;for(let r of n.children)if(r.name===e){t=r;break}if(t){n.remove(t);let e=new o;return e.add(t),e}}return n}return l.isGroup||l.isObject3D?l.clone():null}_findSingleMesh(e){for(let t of e.children)if(t.isMesh)return e.remove(t),t;if(e.children.length===1){let t=e.children[0];if(t.children&&t.children.length===1){let e=t.children[0];if(e.isMesh&&!this._hasNonIdentityTransform(t))return t.remove(e),e}}return null}_hasNonIdentityTransform(e){let t=e.position,n=e.rotation,r=e.scale,i=t.x!==0||t.y!==0||t.z!==0,a=n.x!==0||n.y!==0||n.z!==0,o=r.x!==1||r.y!==1||r.z!==1;return i||a||o}_getBasePath(e){let t=e.lastIndexOf(`/`);return t>=0?e.slice(0,t):``}_getLocalVariantSelections(e){let t={};if(e.variantSelection)for(let n in e.variantSelection)t[n]=e.variantSelection[n];return t}_getReferences(e){let t=[];if(e.fields.references&&e.fields.references.length>0){let n=e.fields.references[0];if(typeof n==`string`){let e=n.matchAll(/@([^@]+)@(?:<([^>]+)>)?/g);for(let n of e)t.push(n[0])}else n.assetPath&&t.push(`@`+n.assetPath+`@`)}if(t.length===0&&e.fields.payload){let n=e.fields.payload;typeof n==`string`?t.push(n):n.assetPath&&t.push(`@`+n.assetPath+`@`)}return t}_getAttributes(e){let t={};this._collectAttributesFromPath(e,t);let n=e.match(ae);if(n){let r=n[1],i=n[4],a=this._getVariantPaths(r);for(let n of a){if(e.startsWith(n))continue;let r=n+`/`+i;this._collectAttributesFromPath(r,t)}}else{let n=e.split(`/`);for(let e=1;e0){let r=n.indexOf(0);t[e]=r>=0?i[r]:i[0]}}r.fields?.elementSize!==void 0&&(t[e+`:elementSize`]=r.fields.elementSize),e.startsWith(`primvars:`)&&r.fields?.typeName!==void 0&&(t[e+`:typeName`]=r.fields.typeName)}}_buildGeomPrimitive(e,n,r){let i=this._getAttributes(e),a=e.split(`/`).pop(),o;switch(r){case`Cube`:{let e=i.size||2;o=new g(e,e,e);break}case`Sphere`:o=new t(i.radius||1,32,16);break;case`Cylinder`:{let e=i.height||2,t=i.radius||1;o=new M(t,t,e,32);break}case`Cone`:{let e=i.height||2;o=new S(i.radius||1,e,32);break}case`Capsule`:{let e=i.height||1;o=new _(i.radius||.5,e,16,32);break}}let s=i.axis||`Z`;s===`X`?o.rotateZ(-Math.PI/2):s===`Z`&&o.rotateX(Math.PI/2);let c=this._buildMaterial(e,n.fields),l=new y(o,c);return l.name=a,this.applyTransform(l,n.fields,i),l}_buildMesh(e,t){let n=this._getAttributes(e),r=n[`primvars:skel:jointIndices`],i=n[`primvars:skel:jointWeights`],a=r&&i&&r.length>0&&i.length>0,o=this._getGeomSubsets(e),s,c;if(o.length>0){s=this._buildGeometryWithSubsets(n,o,a);let r=this._getMaterialPath(e,t.fields);c=o.map(e=>{let t=e.materialPath||r;return this._buildMaterialForPath(t)})}else s=this._buildGeometry(e,n,a),c=this._buildMaterial(e,t.fields);let l=n[`primvars:displayColor`];if(l&&l.length>=3){let e=e=>{e.color&&e.color.r===1&&e.color.g===1&&e.color.b===1&&!e.map&&e.color.setRGB(l[0],l[1],l[2],N)};Array.isArray(c)?c.forEach(e):e(c)}let u=n[`primvars:displayOpacity`];if(u&&u.length===1&&o.length===0){let e=u[0],t=t=>{e<1&&t.opacity===1&&t.transparent===!1&&(t.opacity=e,t.transparent=!0)};Array.isArray(c)?c.forEach(t):t(c)}let f;if(a){f=new d(s,c);let t=this.specsByPath[e+`.skel:skeleton`];t||=this.specsByPath[e+`.rel skel:skeleton`];let r=null;t&&(t.fields.targetPaths&&t.fields.targetPaths.length>0?r=t.fields.targetPaths[0]:t.fields.default&&(r=t.fields.default.replace(/<|>/g,``)));let i=n[`skel:joints`],a=n[`primvars:skel:geomBindTransform`];this.skinnedMeshes.push({mesh:f,skeletonPath:r,path:e,localJoints:i,geomBindTransform:a})}else f=new y(s,c);return f.name=e.split(`/`).pop(),this.applyTransform(f,t.fields,n),f}_buildCamera(e){let t=this._getAttributes(e),n=t.projection,r=typeof n==`string`?n.toLowerCase():Q.projection,i=t.clippingRange||Q.clippingRange,a=Math.max(2**-52,this._parseNumber(i[0],Q.clippingRange[0])),o=Math.max(a+2**-52,this._parseNumber(i[1],Q.clippingRange[1])),s=this._parseNumber(t.horizontalAperture,Q.horizontalAperture),c=this._parseNumber(t.verticalAperture,Q.verticalAperture),l=this._parseNumber(t.horizontalApertureOffset,Q.horizontalApertureOffset),u=this._parseNumber(t.verticalApertureOffset,Q.verticalApertureOffset),d=this._parseNumber(t.focalLength,Q.focalLength),f=this._parseNumber(t.focusDistance,Q.focusDistance),p=this._parseNumber(t.fStop,Q.fStop),m;if(r===`orthographic`){let e=s/10,t=c/10,n=l/10,r=u/10;m=new x(n-e*.5,n+e*.5,r+t*.5,r-t*.5,a,o)}else{let e=Math.max(2**-52,c),t=Math.max(2**-52,d),n=s/e;m=new k(2*Math.atan(e/(2*t))*180/Math.PI,n,a,o),m.filmGauge=Math.max(s,c),m.filmOffset=l,m.focus=f,m.setFocalLength(t),u!==0&&(m.userData.verticalApertureOffset=u)}return m.userData.fStop=p,m.userData.usdProjection=r,m}_buildLight(e,t){let n=this._getAttributes(e),r=this._parseNumber(n[`inputs:intensity`],1),i=n[`inputs:color`]||[1,1,1],a=n[`inputs:enableColorTemperature`]===!0,o=this._parseNumber(n[`inputs:colorTemperature`],6500),s=new O(i[0],i[1],i[2]);if(a){let e=this._colorTemperature(o);s.multiply(e)}let c;switch(t){case`DistantLight`:c=new P(s,r);break;case`SphereLight`:{let e=this._parseNumber(n[`shaping:cone:angle`],0);c=e>0?new u(s,r,0,e*Math.PI/180,this._parseNumber(n[`shaping:cone:softness`],0)):new C(s,r);break}case`RectLight`:c=new m(s,r,this._parseNumber(n[`inputs:width`],1),this._parseNumber(n[`inputs:height`],1));break;case`DiskLight`:{let e=this._parseNumber(n[`inputs:radius`],.5)*2;c=new m(s,r,e,e);break}}return c}_colorTemperature(e){let t=e/100,n,r,i;return t<=66?(n=1,r=.3900815787690196*Math.log(t)-.6318414437886275):(n=1.292936186062745*(t-60)**-.1332047592,r=1.1298908608952942*(t-60)**-.0755148492),i=t>=66?1:t<=19?0:.543206789110196*Math.log(t-10)-1.19625408914,new O(Math.min(Math.max(n,0),1),Math.min(Math.max(r,0),1),Math.min(Math.max(i,0),1))}_parseNumber(e,t){let n=Number(e);return Number.isFinite(n)?n:t}_getGeomSubsets(e){let t=[],n=this.geomSubsetsByMeshPath.get(e);if(!n)return t;for(let e of n){let n=this._getAttributes(e).indices;if(!n||n.length===0)continue;let r=this._getMaterialBindingTarget(e);t.push({name:e.split(`/`).pop(),indices:n,materialPath:r})}return t}_getMaterialBindingTarget(e){let t=e+`.material:binding`,n=this.specsByPath[t];if(n?.fields?.targetPaths?.length>0)return n.fields.targetPaths[0];let r=e.split(`/`);for(let e=1;e0)return r.fields.targetPaths[0]}}return null}_buildGeometry(e,t,n=!1){let r=new j,i=t.points;if(!i||i.length===0)return r;let a=t.faceVertexIndices,o=t.faceVertexCounts,s=t[`primvars:arnold:polygon_holes`],c=this._buildHoleMap(s),l=a,u=null;if(o&&o.length>0){let e=this._triangulateIndicesWithPattern(a,o,i,c);l=e.indices,u=e.pattern}let d=i;l&&l.length>0&&(d=this._expandAttribute(i,l,3)),r.setAttribute(`position`,new T(new Float32Array(d),3));let f=t.normals||t[`primvars:normals`],p=t[`normals:indices`]||t[`primvars:normals:indices`];if(f&&f.length>0){let e=f;if(p&&p.length>0&&u){let t=this._applyTriangulationPattern(p,u);e=this._expandAttribute(f,t,3)}else if(f.length===i.length)l&&l.length>0&&(e=this._expandAttribute(f,l,3));else if(u){let t=this._applyTriangulationPattern(Array.from({length:f.length/3},(e,t)=>t),u);e=this._expandAttribute(f,t,3)}r.setAttribute(`normal`,new T(new Float32Array(e),3))}else{let e=this._computeVertexNormals(i,l);r.setAttribute(`normal`,new T(new Float32Array(this._expandAttribute(e,l,3)),3))}let{uvs:m,uvIndices:h}=this._findUVPrimvar(t),g=a?a.length:0;if(m&&m.length>0){let e=m;if(h&&h.length>0&&u){let t=this._applyTriangulationPattern(h,u);e=this._expandAttribute(m,t,2)}else if(l&&m.length/2==i.length/3)e=this._expandAttribute(m,l,2);else if(u&&m.length/2===g){let t=this._applyTriangulationPattern(Array.from({length:g},(e,t)=>t),u);e=this._expandAttribute(m,t,2)}r.setAttribute(`uv`,new T(new Float32Array(e),2))}let{uvs2:_,uv2Indices:v}=this._findUV2Primvar(t);if(_&&_.length>0){let e=_;if(v&&v.length>0&&u){let t=this._applyTriangulationPattern(v,u);e=this._expandAttribute(_,t,2)}else if(l&&_.length/2==i.length/3)e=this._expandAttribute(_,l,2);else if(u&&_.length/2===g){let t=this._applyTriangulationPattern(Array.from({length:g},(e,t)=>t),u);e=this._expandAttribute(_,t,2)}r.setAttribute(`uv1`,new T(new Float32Array(e),2))}if(n){let e=t[`primvars:skel:jointIndices`],n=t[`primvars:skel:jointWeights`],i=t[`primvars:skel:jointIndices:elementSize`]||4;if(e&&n){let t=d.length/3,a,o;l&&l.length>0?(a=this._expandAttribute(e,l,i),o=this._expandAttribute(n,l,i)):(a=e,o=n);let s=new Uint16Array(t*4),c=new Float32Array(t*4);this._selectTopWeights(a,o,i,t,s,c),r.setAttribute(`skinIndex`,new T(s,4)),r.setAttribute(`skinWeight`,new T(c,4))}}return r}_buildGeometryWithSubsets(e,t,n=!1){let r=new j,i=e.points;if(!i||i.length===0)return r;let a=e.faceVertexIndices,o=e.faceVertexCounts;if(!o||o.length===0)return r;let s=e[`primvars:arnold:polygon_holes`],c=this._buildHoleMap(s),l=c.holeFaces,u=c.parentToHoles,{uvs:d,uvIndices:f}=this._findUVPrimvar(e),{uvs2:p,uv2Indices:m}=this._findUV2Primvar(e),h=e.normals||e[`primvars:normals`],g=e[`normals:indices`]||e[`primvars:normals:indices`],_=n?e[`primvars:skel:jointIndices`]:null,v=n?e[`primvars:skel:jointWeights`]:null,y=e[`primvars:skel:jointIndices:elementSize`]||4,b=[],x=0;for(let e=0;e0){let e=t;for(let t of n)e+=o[t];x+=e-2}else t>=3&&(x+=t-2)}let S=new Int32Array(x).fill(-1);for(let e=0;e=o.length)continue;let i=b[r],a=o[r]-2;for(let t=0;te.subset-t.subset);let w=[],E=C.length>0?C[0].subset:-1,D=0;for(let e=0;e=0&&w.push({start:D*3,count:(e-D)*3,materialIndex:E}),E=C[e].subset,D=e);E>=0&&C.length>D&&w.push({start:D*3,count:(C.length-D)*3,materialIndex:E});for(let e of w)r.addGroup(e.start,e.count,e.materialIndex);let{indices:O,pattern:k}=this._triangulateIndicesWithPattern(a,o,i,c),A=o.reduce((e,t)=>e+t,0),M=d&&!f&&d.length/2===A||p&&!m&&p.length/2===A?this._applyTriangulationPattern(Array.from({length:A},(e,t)=>t),k):null,N=f?this._applyTriangulationPattern(f,k):d&&d.length/2===A?M:null,P=m?this._applyTriangulationPattern(m,k):p&&p.length/2===A?M:null,F=h&&g&&g.length>0,I=h&&h.length/3===A,L=F?this._applyTriangulationPattern(g,k):I?this._applyTriangulationPattern(Array.from({length:A},(e,t)=>t),k):null,R=!h&&O.length>0?this._computeVertexNormals(i,O):null,z=x*3,B=new Float32Array(z*3),V=d?new Float32Array(z*2):null,H=p?new Float32Array(z*2):null,U=h||R?new Float32Array(z*3):null,W=_?new Uint16Array(z*y):null,G=v?new Float32Array(z*y):null;for(let e=0;ea&&(a=e,i=s)}if(i!==e){let t=o[e];o[e]=o[i],o[i]=t}}let c=0;for(let e=0;e<4;e++)c+=t[r+o[e]]||0;for(let n=0;n<4;n++){let l=o[n];c>0?(i[s*4+n]=e[r+l]||0,a[s*4+n]=(t[r+l]||0)/c):(i[s*4+n]=0,a[s*4+n]=0)}}}_findUVPrimvar(e){for(let t in e){if(!t.startsWith(`primvars:`)||t.endsWith(`:typeName`)||t.endsWith(`:elementSize`)||t.endsWith(`:indices`)||t.includes(`skel:`))continue;let n=e[t+`:typeName`];if(n&&n.includes(`texCoord`))return{uvs:e[t],uvIndices:e[t+`:indices`]}}return{uvs:e[`primvars:st`]||e[`primvars:UVMap`],uvIndices:e[`primvars:st:indices`]}}_findUV2Primvar(e){return{uvs2:e[`primvars:st1`],uv2Indices:e[`primvars:st1:indices`]}}_buildHoleMap(e){if(!e||e.length===0)return{parentToHoles:new Map,holeFaces:new Set};let t=new Map,n=new Set;for(let r=0;r0&&n&&n.length>0){let r=new Map,c=[];for(let t=0;t4)if(n&&n.length>0){let t=[];for(let n=0;n.9?o.set(1,0,0):o.set(0,1,0),c.crossVectors(a,o).normalize(),o.crossVectors(c,a).normalize();for(let e of i)r.push(new I(e.dot(o),e.dot(c)));let l=s.triangulateShape(r,[]),u=[];for(let t of l)u.push([e[t[0]],e[t[1]],e[t[2]]]);return u}_triangulateNGonWithHoles(e,t,r){let i=[];for(let t of e)i.push(new n(r[t*3],r[t*3+1],r[t*3+2]));let a=new n;for(let e=0;e.9?o.set(1,0,0):o.set(0,1,0),c.crossVectors(a,o).normalize(),o.crossVectors(c,a).normalize();let l=[];for(let e of i)l.push(new I(e.dot(o),e.dot(c)));let u=[];for(let e of t){let t=[];for(let i of e){let e=new n(r[i*3],r[i*3+1],r[i*3+2]);t.push(new I(e.dot(o),e.dot(c)))}u.push(t)}let d=[...e];for(let e of t)d.push(...e);let f=s.triangulateShape(l,u),p=[];for(let e of f)p.push([d[e[0]],d[e[1]],d[e[2]]]);return p}_triangulateIndices(e,t){let n=[],r=0;for(let i=0;i4)for(let t=1;t0&&(r[e*3]/=a,r[e*3+1]/=a,r[e*3+2]/=a)}return r}_getMaterialPath(e,t){let n=null,r=t[`material:binding`];return r&&(n=Array.isArray(r)?r[0]:r),n||=this._getMaterialBindingTarget(e),n}_buildMaterial(e,t){let n=new w,r=null,i=t[`material:binding`];if(i&&(r=Array.isArray(i)?i[0]:i),r||=this._getMaterialBindingTarget(e),!r){let t=[],n=e+`/`;for(let e in this.specsByPath){if(!e.startsWith(n)||!e.endsWith(`.material:binding`))continue;let r=this.specsByPath[e];if(!r)continue;let i=r.fields.targetPaths;i&&i.length>0&&t.push(i[0])}t.length>0&&(r=this._pickBestMaterial(t))}if(!r){let t=`/`+e.split(`/`)[1],n=this.materialsByRoot.get(t);if(n){for(let e of n)if(e.startsWith(t+`/Looks/`)||e.startsWith(t+`/Materials/`)){r=e;break}}}return r&&this._applyMaterial(n,r),n}_buildMaterialForPath(e){let t=new w;return e&&this._applyMaterial(t,e),t}_applyMaterialBinding(e,t){let n=t+`.material:binding`,r=this.specsByPath[n];if(!r)return;let i=null,a=r.fields?.targetPaths||r.fields?.default;if(a&&(i=Array.isArray(a)?a[0]:a),!i)return;i=String(i).replace(/^<|>$/g,``);let o=new w;this._applyMaterial(o,i),e.material=o}_pickBestMaterial(e){for(let t of e){let e=this.shadersByMaterialPath.get(t);if(e)for(let n of e){let e=this._getAttributes(n);if(e[`info:id`]===`UsdUVTexture`&&e[`inputs:file`])return t}}return e[0]}_applyMaterial(e,t){if(!this.specsByPath[t])return;let n=this.shadersByMaterialPath.get(t);if(n)for(let t of n){let n=this.specsByPath[t];if(!n)continue;let r=this._getAttributes(t)[`info:id`]||n.fields[`info:id`];r===`UsdPreviewSurface`||r===`ND_UsdPreviewSurface_surfaceshader`?this._applyPreviewSurface(e,t):r===`arnold:openpbr_surface`&&this._applyOpenPBRSurface(e,t)}}_applyTextureOrValue(e,t,n,r,i,a,o,s){let c=t+`.`+r,l=this.specsByPath[c];if(l&&l.fields.connectionPaths&&l.fields.connectionPaths.length>0){let t=s===this._getTextureFromOpenPBRConnection?l.fields.connectionPaths:[l.fields.connectionPaths[0]];for(let n of t){let t=s.call(this,n);if(t)return t.colorSpace=a,e[i]=t,!0}}return n[r]!==void 0&&o&&o(n[r]),!1}_applyPreviewSurface(e,t){let n=this._getAttributes(t),r=(r,i,a,o)=>this._applyTextureOrValue(e,t,n,r,i,a,o,this._getTextureFromConnection),i=e=>{let n=t+`.`+e;return this.specsByPath[n]};if(r(`inputs:diffuseColor`,`map`,N,t=>{Array.isArray(t)&&t.length>=3&&e.color.setRGB(t[0],t[1],t[2],N)}),e.map&&e.map.userData.scale){let t=e.map.userData.scale;Array.isArray(t)&&t.length>=3&&e.color.setRGB(t[0],t[1],t[2],N)}if(r(`inputs:emissiveColor`,`emissiveMap`,N,t=>{Array.isArray(t)&&t.length>=3&&e.emissive.setRGB(t[0],t[1],t[2],N)}),e.emissiveMap)if(e.emissiveMap.userData.scale){let t=e.emissiveMap.userData.scale;Array.isArray(t)&&t.length>=3&&e.emissive.setRGB(t[0],t[1],t[2],N)}else e.emissive.set(16777215);if(r(`inputs:normal`,`normalMap`,``,null),e.normalMap&&e.normalMap.userData.scale){let t=e.normalMap.userData.scale;e.normalScale=new I(t[0],t[1])}if(r(`inputs:roughness`,`roughnessMap`,``,t=>{e.roughness=t})&&(e.roughness=1),r(`inputs:metallic`,`metalnessMap`,``,t=>{e.metalness=t})&&(e.metalness=1),r(`inputs:occlusion`,`aoMap`,``,null),n[`inputs:ior`]!==void 0&&(e.ior=n[`inputs:ior`]),r(`inputs:specularColor`,`specularColorMap`,N,t=>{Array.isArray(t)&&t.length>=3&&e.specularColor.setRGB(t[0],t[1],t[2],N)}),e.specularColorMap&&e.specularColorMap.userData.scale){let t=e.specularColorMap.userData.scale;Array.isArray(t)&&t.length>=3&&e.specularColor.setRGB(t[0],t[1],t[2],N)}n[`inputs:clearcoat`]!==void 0&&(e.clearcoat=n[`inputs:clearcoat`]),n[`inputs:clearcoatRoughness`]!==void 0&&(e.clearcoatRoughness=n[`inputs:clearcoatRoughness`]);let a=n[`inputs:opacityThreshold`]===void 0?0:n[`inputs:opacityThreshold`];if(i(`inputs:opacity`)?.fields?.connectionPaths?.length>0)a>0?(e.alphaTest=a,e.transparent=!1):e.transparent=!0;else{let t=n[`inputs:opacity`]===void 0?1:n[`inputs:opacity`];t<1&&(e.transparent=!0,e.opacity=t)}}_applyOpenPBRSurface(e,t){let n=this._getAttributes(t),r=(r,i,a,o)=>this._applyTextureOrValue(e,t,n,r,i,a,o,this._getTextureFromOpenPBRConnection);if(r(`inputs:base_color`,`map`,N,t=>{Array.isArray(t)&&t.length>=3&&e.color.setRGB(t[0],t[1],t[2],N)}),e.map&&e.map.userData.scale){let t=e.map.userData.scale;Array.isArray(t)&&t.length>=3&&e.color.setRGB(t[0],t[1],t[2],N)}r(`inputs:base_metalness`,`metalnessMap`,``,t=>{typeof t==`number`&&(e.metalness=t)}),r(`inputs:specular_roughness`,`roughnessMap`,``,t=>{typeof t==`number`&&(e.roughness=t)});let i=r(`inputs:emission_color`,`emissiveMap`,N,t=>{Array.isArray(t)&&t.length>=3&&e.emissive.setRGB(t[0],t[1],t[2],N)}),a=n[`inputs:emission_luminance`];a!==void 0&&a>0&&(i?e.emissiveIntensity=a:e.emissive.multiplyScalar(a));let o=n[`inputs:transmission_weight`];if(o!==void 0&&o>0){e.transmission=o;let t=n[`inputs:transmission_depth`];t!==void 0&&(e.thickness=t);let r=n[`inputs:transmission_color`];r!==void 0&&Array.isArray(r)&&(e.attenuationColor.setRGB(r[0],r[1],r[2]),e.attenuationDistance=t||1)}let s=n[`inputs:geometry_opacity`];s!==void 0&&s<1&&(e.opacity=s,e.transparent=!0);let c=n[`inputs:specular_ior`];c!==void 0&&(e.ior=c);let l=n[`inputs:coat_weight`];if(l!==void 0&&l>0){e.clearcoat=l;let t=n[`inputs:coat_roughness`];t!==void 0&&(e.clearcoatRoughness=t)}let u=n[`inputs:thin_film_weight`];if(u!==void 0&&u>0){e.iridescence=u;let t=n[`inputs:thin_film_ior`];t!==void 0&&(e.iridescenceIOR=t);let r=n[`inputs:thin_film_thickness`];if(r!==void 0){let t=r*1e3;e.iridescenceThicknessRange=[t,t]}}let d=n[`inputs:specular_weight`];d!==void 0&&(e.specularIntensity=d);let f=n[`inputs:specular_color`];f!==void 0&&Array.isArray(f)&&e.specularColor.setRGB(f[0],f[1],f[2]);let p=n[`inputs:specular_roughness_anisotropy`];p!==void 0&&p>0&&(e.anisotropy=p),r(`inputs:geometry_normal`,`normalMap`,``,null)}_getTextureFromOpenPBRConnection(e){let t=e.replace(/<|>/g,``),n=t.split(`.`)[0],r=this.specsByPath[n];if(!r)return null;let i=this._getAttributes(n),a=i[`info:id`]||r.fields[`info:id`];if(r.fields.typeName===`NodeGraph`){let e=t.split(`.`)[1],r=n+`.`+e,i=this.specsByPath[r];return i?.fields?.connectionPaths?.length>0?this._getTextureFromOpenPBRConnection(i.fields.connectionPaths[0]):null}if(a===`arnold:image`){let e=i[`inputs:filename`];return e?this._loadTextureFromPath(e):null}if(a&&a.startsWith(`ND_image_`)){let e=i[`inputs:file`];return e?this._loadTextureFromPath(e):null}if(a===`MayaND_fileTexture_color4`){let e=n+`.inputs:inColor`,t=this.specsByPath[e];return t?.fields?.connectionPaths?.length>0?this._getTextureFromOpenPBRConnection(t.fields.connectionPaths[0]):null}if(a&&a.startsWith(`ND_convert_`)){let e=n+`.inputs:in`,t=this.specsByPath[e];return t?.fields?.connectionPaths?.length>0?this._getTextureFromOpenPBRConnection(t.fields.connectionPaths[0]):null}if(a===`arnold:bump2d`){let e=n+`.inputs:bump_map`,t=this.specsByPath[e];return t?.fields?.connectionPaths?.length>0?this._getTextureFromOpenPBRConnection(t.fields.connectionPaths[0]):null}if(a===`arnold:color_correct`){let e=n+`.inputs:input`,t=this.specsByPath[e];return t?.fields?.connectionPaths?.length>0?this._getTextureFromOpenPBRConnection(t.fields.connectionPaths[0]):null}let o=n.substring(0,n.lastIndexOf(`/`));if(o){let e=this.specsByPath[o];if(e){let t=this._getAttributes(o);if((t[`info:id`]||e.fields[`info:id`])===`arnold:image`){let e=t[`inputs:filename`];if(e)return this._loadTextureFromPath(e)}}}return null}_loadTextureFromPath(e){if(!e)return null;if(this.textureCache[e])return this.textureCache[e];let t=this._loadTexture(e,null,null);return t&&(this.textureCache[e]=t),t}_getTextureFromConnection(e){let t=e.split(`.`)[0],n=this.specsByPath[t];if(!n)return null;let r=this._getAttributes(t);if((r[`info:id`]||n.fields[`info:id`])!==`UsdUVTexture`)return null;let i=r[`inputs:file`];if(!i)return null;let a=null,o=0,s=t+`.inputs:st`,c=this.specsByPath[s];if(c?.fields?.connectionPaths?.length>0){let e=c.fields.connectionPaths[0].replace(/<|>/g,``).split(`.`)[0],t=this.specsByPath[e];if(t){let n=this._getAttributes(e),r=n[`info:id`]||t.fields[`info:id`];if(r===`UsdTransform2d`){a=n;let t=e+`.inputs:in`,r=this.specsByPath[t];if(r?.fields?.connectionPaths?.length>0){let e=r.fields.connectionPaths[0].replace(/<|>/g,``).split(`.`)[0],t=this._getAttributes(e)[`inputs:varname`];t===`st1`?o=1:t===`st2`&&(o=2)}}else if(r===`UsdPrimvarReader_float2`){let e=n[`inputs:varname`];e===`st1`?o=1:e===`st2`&&(o=2)}}}let l=r[`inputs:scale`],u=r[`inputs:bias`],d=i;if(l&&(d+=`:s`+l.join(`,`)),u&&(d+=`:b`+u.join(`,`)),this.textureCache[d])return this.textureCache[d];let f=this._loadTexture(i,r,a);return f&&(l&&(f.userData.scale=l),u&&(f.userData.bias=u),o!==0&&(f.channel=o),this.textureCache[d]=f),f}_applyTextureTransforms(e,t){if(!t)return;let n=t[`inputs:scale`];n&&Array.isArray(n)&&n.length>=2&&e.repeat.set(n[0],n[1]);let r=t[`inputs:translation`];r&&Array.isArray(r)&&r.length>=2&&e.offset.set(r[0],r[1]);let i=t[`inputs:rotation`];typeof i==`number`&&(e.rotation=i*Math.PI/180)}_loadTexture(e,t,n){let r=e;r.startsWith(`@`)&&(r=r.slice(1)),r.endsWith(`@`)&&(r=r.slice(0,-1));let i=this._resolveFilePath(r),a=this.assets[i];if(a||=this.assets[r],!a){let e=r.split(`/`).pop();for(let r in this.assets)if(r.endsWith(e)||r.endsWith(`/`+e))return this._createTextureFromData(this.assets[r],t,n);if(this.manager){let r=this.manager.resolveURL(e);if(r!==e)return this._createTextureFromData(r,t,n)}return console.warn(`USDLoader: Texture not found:`,r),null}return this._createTextureFromData(a,t,n)}_createTextureFromData(e,t,n){if(!e)return null;let r=this,i=new f,a;if(typeof e==`string`)a=e;else if(e instanceof Uint8Array||e instanceof ArrayBuffer){let t=new Blob([e]);a=URL.createObjectURL(t)}else return null;let o=new Image;return o.onload=function(){i.image=o,t&&(i.wrapS=r._getWrapMode(t[`inputs:wrapS`]),i.wrapT=r._getWrapMode(t[`inputs:wrapT`])),r._applyTextureTransforms(i,n),i.needsUpdate=!0,typeof e!=`string`&&URL.revokeObjectURL(a)},o.src=a,i}_getWrapMode(e){return e===`repeat`?i:e===`mirror`?D:e===`clamp`?b:i}_buildSkeleton(t){let n=this._getAttributes(t),r=n.joints;if(!r||r.length===0)return null;let i=n.bindTransforms,o=n.restTransforms,s=this._flattenMatrixArray(i,r.length),c=this._flattenMatrixArray(o,r.length),l=[],u={},d=[];for(let t=0;t=(t+1)*16){let n=new e,r=s.slice(t*16,(t+1)*16);n.set(r[0],r[4],r[8],r[12],r[1],r[5],r[9],r[13],r[2],r[6],r[10],r[14],r[3],r[7],r[11],r[15]);let i=n.clone().invert();d.push(i)}else d.push(new e)}for(let e=0;e1){let n=u[t.slice(0,-1).join(`/`)];n&&n.bone.add(l[e])}}if(c&&c.length>=r.length*16)for(let t=0;t!e.parent||!e.parent.isBone),p=this.specsByPath[t+`.skel:animationSource`],m=null;return p&&p.fields.targetPaths&&p.fields.targetPaths.length>0&&(m=p.fields.targetPaths[0]),{skeleton:new a(l,d),joints:r,rootBones:f,animationPath:m,path:t}}_bindSkeletons(){for(let t of this.skinnedMeshes){let{mesh:n,skeletonPath:r,localJoints:i,geomBindTransform:a}=t,o=null;if(r&&this.skeletons[r]&&(o=this.skeletons[r]),!o){for(let e in this.skeletons)if(r&&(r.includes(e)||e.includes(r))){o=this.skeletons[e];break}}if(!o){let e=Object.keys(this.skeletons);e.length>0&&(o=this.skeletons[e[0]])}if(!o){console.warn(`USDComposer: No skeleton found for skinned mesh`,n.name);continue}let{skeleton:s,rootBones:c,joints:l}=o;if(i&&i.length>0){let e=n.geometry.attributes.skinIndex;if(e){let t=[];for(let e=0;e=0?r:0}let n=e.array;for(let e=0;e0&&e.push(new A(`TransformAnimation`,-1,t)),e}_buildTransformAnimations(){let t=[];for(let r in this.specsByPath){let i=this.specsByPath[r];if(i.specType!==Z.Prim)continue;let a=i.fields?.typeName;if(a!==`Xform`&&a!==`Scope`&&a!==`Mesh`)continue;let o=r.split(`/`).pop(),s=r+`.xformOp:orient`,l=this.specsByPath[s];if(l?.fields?.timeSamples){let{times:e,values:n}=l.fields.timeSamples,r=[],i=[];for(let t=0;t0&&t.push(new h(o+`.quaternion`,new Float32Array(r),new Float32Array(i)))}let u=r+`.xformOp:rotateXYZ`,d=this.specsByPath[u];if(d?.fields?.timeSamples){let{times:e,values:n}=d.fields.timeSamples,r=[],i=[],a=new p,s=new F;for(let t=0;t0&&t.push(new h(o+`.quaternion`,new Float32Array(r),new Float32Array(i)))}let f=r+`.xformOp:translate`,m=this.specsByPath[f];if(m?.fields?.timeSamples){let{times:e,values:n}=m.fields.timeSamples,r=[],i=[];for(let t=0;t0&&t.push(new c(o+`.position`,new Float32Array(r),new Float32Array(i)))}let g=r+`.xformOp:scale`,_=this.specsByPath[g];if(_?.fields?.timeSamples){let{times:e,values:n}=_.fields.timeSamples,r=[],i=[];for(let t=0;t0&&t.push(new c(o+`.scale`,new Float32Array(r),new Float32Array(i)))}let v=i.fields?.properties||[];for(let i of v){if(!i.startsWith(`xformOp:transform`))continue;let a=r+`.`+i,s=this.specsByPath[a];if(!s?.fields?.timeSamples)continue;let{times:l,values:u}=s.fields.timeSamples,d=[],f=[],p=[],m=[],g=[],_=[],v=new e,y=new n,b=new F,x=new n;for(let e=0;e0&&(t.push(new c(o+`.position`,new Float32Array(d),new Float32Array(f))),t.push(new h(o+`.quaternion`,new Float32Array(p),new Float32Array(m))),t.push(new c(o+`.scale`,new Float32Array(g),new Float32Array(_))));break}}return t}_buildAnimationClip(e){let t=this._getAttributes(e).joints;if(!t||t.length===0)return null;let n=[],r=this._getTimeSampledAttribute(e,`rotations`);if(r&&r.times&&r.values){let{times:e,values:i}=r;for(let r=0;r0&&n.push(new h(a+`.quaternion`,new Float32Array(o),new Float32Array(s)))}}let i=this._getTimeSampledAttribute(e,`translations`);if(i&&i.times&&i.values){let{times:e,values:r}=i;for(let i=0;i0&&n.push(new c(a+`.position`,new Float32Array(o),new Float32Array(s)))}}let a=this._getTimeSampledAttribute(e,`scales`);if(a&&a.times&&a.values){let{times:e,values:r}=a;for(let i=0;i0&&n.push(new c(a+`.scale`,new Float32Array(o),new Float32Array(s)))}}return n.length===0?null:new A(e.split(`/`).pop(),-1,n)}_getTimeSampledAttribute(e,t){let n=e+`.`+t,r=this.specsByPath[n];if(r&&r.fields.timeSamples){let e=r.fields.timeSamples;if(e.times&&e.values)return e}return null}_flattenMatrixArray(e,t){if(!e||e.length===0)return null;if(typeof e[0]==`number`)return e;let n=[];for(let r=0;rt?``:e.slice(t+1).toLowerCase()}function o(e){let o={};for(let c in e){let l=e[c],u=a(c);if(u===`png`||u===`jpg`||u===`jpeg`||u===`avif`){o[c]=l;continue}u!==`usd`&&u!==`usda`&&u!==`usdc`||(s(l)?o[c]=n.parseData(i(l)):o[c]=t.parseData(r.decode(l)))}return o}function s(e){let t=new Uint8Array([80,88,82,45,85,83,68,67]),n=e instanceof Uint8Array?e:new Uint8Array(e);if(n.byteLength=0?n.slice(0,o):``;if(r===`usda`)return{file:e[n],filename:n,basePath:c};if(r===`usdc`)i=!0;else if(r===`usd`)if(s(e[n]))i=!0;else return{file:e[n],filename:n,basePath:c};return i?{file:e[n],filename:n,basePath:c}:{file:void 0,filename:``,basePath:``}}let l=this;if(typeof e==`string`){let n=new $(l.manager),r=t.parseData(e);return n.compose(r,{})}if(s(e)){let t=new $(l.manager),r=n.parseData(i(e));return t.compose(r,{})}let u=new Uint8Array(e);if(u[0]===80&&u[1]===75){let e=L(u),t=o(e),{file:n,filename:r,basePath:i}=c(e);if(!n)throw Error(`USDLoader: Invalid USDZ package. The first ZIP entry must be a USD layer (.usd/.usda/.usdc).`);let a=new $(l.manager),s=t[r];if(!s)throw Error(`USDLoader: Failed to parse root layer "`+r+`".`);return a.compose(s,t,{},i)}let d=new $(l.manager),f=r.decode(u),p=t.parseData(f);return d.compose(p,{})}};export{oe as USDLoader};