const __vite__mapDeps=(i,m=__vite__mapDeps,d=(m.f||(m.f=["assets/USDZLoader-DaYbmW4a.js","assets/_commonjsHelpers-gnU0ypJ3.js","assets/USDZLoader-CbwYHute.js","assets/three-Cut12-XQ.js","assets/fflate.module-Cv5q2yBc.js","assets/index-CyaBHJ-s.js","assets/index-C6Vs6abl.js"])))=>i.map(i=>d[i]);
import{i as v,_ as S}from"./index-C6Vs6abl.js";import"./three-Cut12-XQ.js";function _(u){const o=[],e=u.split(`
`).map(s=>{const n=s.indexOf("#");return n>0&&!s.substring(0,n).includes('"')?s.substring(0,n):n===0&&s.startsWith("#usda")||n===0?"":s}).join(`
`),c=/def\s+Mesh\s+"([^"]+)"\s*\{/g;let i;for(;(i=c.exec(e))!==null;){const s=i[1],n=i.index+i[0].length;let f=1,d=n;for(let m=n;m<e.length&&f>0;m++)e[m]==="{"?f++:e[m]==="}"&&f--,d=m;const a=e.substring(n,d),r=m=>{const w=m.exec(a);if(!w)return null;const x=w[1],D=[],L=/-?[\d.]+(?:e[+-]?\d+)?/gi;let b;for(;(b=L.exec(x))!==null;)D.push(parseFloat(b[0]));return D},g=r(/point3f\[\]\s+points\s*=\s*\[([\s\S]*?)\]/),l=r(/int\[\]\s+faceVertexCounts\s*=\s*\[([\s\S]*?)\]/),p=r(/int\[\]\s+faceVertexIndices\s*=\s*\[([\s\S]*?)\]/);if(!g||!p)continue;const y=r(/normal3f\[\]\s+normals\s*=\s*\[([\s\S]*?)\]/),U=r(/texCoord2f\[\]\s+(?:primvars:st|st)\s*=\s*\[([\s\S]*?)\]/);let h;if(l&&l.length>0){h=[];let m=0;for(const w of l){for(let x=1;x<w-1;x++)h.push(p[m],p[m+x],p[m+x+1]);m+=w}}else h=[...p];o.push({name:s,points:g,faceVertexCounts:l||Array(h.length/3).fill(3),faceVertexIndices:h,normals:y||void 0,uvs:U||void 0})}return o}async function $(u){const o=await v(),t=[];for(const e of u){let c=`# USD mesh: ${e.name}
`;for(let g=0;g<e.points.length;g+=3)c+=`v ${e.points[g]} ${e.points[g+1]} ${e.points[g+2]}
`;for(let g=0;g<e.faceVertexIndices.length;g+=3)c+=`f ${e.faceVertexIndices[g]+1} ${e.faceVertexIndices[g+1]+1} ${e.faceVertexIndices[g+2]+1}
`;const i=o.loadMeshFromOBJ(c);if(i<0)continue;const s=o.getVertexCount(i),n=o.getFaceCount(i),f=o.getBoundingBox(i),d=new Float32Array(o.getVertexBuffer(i)),a=new Uint32Array(o.getIndexBuffer(i)),r=new Float32Array(o.getNormalBuffer(i));t.push({handle:i,vertexCount:s,faceCount:n,boundingBox:f,buffers:{vertices:d,indices:a,normals:r}})}return t}async function V(u){const o=_(u);if(o.length===0)throw new Error("No mesh found in USDA");return $(o)}async function A(u){const o=await v(),t=[];return u.traverse(e=>{var g;const c=e;if(!c.geometry)return;const i=c.geometry;if(!((g=i.attributes)!=null&&g.position))return;const s=i.attributes.position;if(!s||s.count===0)return;const n=e;n.updateWorldMatrix(!0,!1);const f=n.matrixWorld;let d="";for(let l=0;l<s.count;l++){let p=s.getX(l),y=s.getY(l),U=s.getZ(l);const h=f.elements,m=h[0]*p+h[4]*y+h[8]*U+h[12],w=h[1]*p+h[5]*y+h[9]*U+h[13],x=h[2]*p+h[6]*y+h[10]*U+h[14];d+=`v ${m} ${w} ${x}
`}const a=i.index;if(a)for(let l=0;l<a.count;l+=3)d+=`f ${a.getX(l)+1} ${a.getX(l+1)+1} ${a.getX(l+2)+1}
`;else for(let l=0;l<s.count;l+=3)d+=`f ${l+1} ${l+2} ${l+3}
`;const r=o.loadMeshFromOBJ(d);r<0||t.push({handle:r,vertexCount:o.getVertexCount(r),faceCount:o.getFaceCount(r),boundingBox:o.getBoundingBox(r),buffers:{vertices:new Float32Array(o.getVertexBuffer(r)),indices:new Uint32Array(o.getIndexBuffer(r)),normals:new Float32Array(o.getNormalBuffer(r))}})}),t}async function F(u){const o=[];try{const{USDZLoader:t}=await S(async()=>{const{USDZLoader:n}=await import("./USDZLoader-DaYbmW4a.js").then(f=>f.U);return{USDZLoader:n}},__vite__mapDeps([0,1])),e=await S(()=>import("./three-Cut12-XQ.js"),[]),c=new e.Group,i=new t;if(await i.waitForModuleLoadingCompleted()){const n=new File([u],"model.usdz",{type:"model/vnd.usdz+zip"});await i.loadFile(n,c);const f=await A(c);if(f.length>0)return f;o.push("three-usdz-loader: WASM loaded but no mesh geometry produced")}else o.push("three-usdz-loader: WASM module init failed")}catch(t){o.push(`three-usdz-loader: ${t instanceof Error?t.message:String(t)}`)}try{const{USDZLoader:t}=await S(async()=>{const{USDZLoader:s}=await import("./USDZLoader-CbwYHute.js");return{USDZLoader:s}},__vite__mapDeps([2,3,4])),c=new t().parse(u),i=await A(c);if(i.length>0)return i;o.push("Three.js USDZLoader: no meshes extracted")}catch(t){o.push(`Three.js USDZLoader: ${t instanceof Error?t.message:String(t)}`)}try{const{convertUsdzToGlb:t}=await S(async()=>{const{convertUsdzToGlb:c}=await import("./index-CyaBHJ-s.js").then(i=>i.i);return{convertUsdzToGlb:c}},__vite__mapDeps([5,1])),e=t(new Uint8Array(u));if(e.length>500){const{GLTFLoader:c}=await S(async()=>{const{GLTFLoader:d}=await import("./index-C6Vs6abl.js").then(a=>a.G);return{GLTFLoader:d}},__vite__mapDeps([6,3])),i=new c,s=e.buffer.slice(e.byteOffset,e.byteOffset+e.byteLength),n=await new Promise((d,a)=>{i.parse(s,"",d,a)}),f=await A(n.scene);if(f.length>0)return f}o.push("usdz-to-glb: conversion produced empty mesh")}catch(t){o.push(`usdz-to-glb: ${t instanceof Error?t.message:String(t)}`)}try{const t=new DataView(u);if(t.getUint8(0)===80&&t.getUint8(1)===75){let e=0;for(;e<u.byteLength-30&&t.getUint32(e,!0)===67324752;){const i=t.getUint16(e+8,!0),s=t.getUint32(e+18,!0),n=t.getUint16(e+26,!0),f=t.getUint16(e+28,!0),d=new TextDecoder().decode(new Uint8Array(u,e+30,n)),a=e+30+n+f;if(i===0&&(d.endsWith(".usda")||d.endsWith(".usd"))){const r=new TextDecoder().decode(new Uint8Array(u,a,s));if(r.includes("def ")||r.includes("point3f"))return V(r)}for(e=a+s;e%64!==0&&e<u.byteLength;)e++}}o.push("Manual extraction: no USDA text found")}catch(t){o.push(`Manual extraction: ${t instanceof Error?t.message:String(t)}`)}throw new Error(`Failed to load USDZ file. This file uses USDC binary (Crate) format.

To open this file, try one of:
1. Convert to GLB using Blender (File > Import > USD, then File > Export > GLB)
2. Use Apple Reality Converter to export as USDA text format
3. Use an online USDZ to GLB converter

Technical details:
`+o.join(`
`))}async function E(u){const o=await v(),t=new Float32Array(o.getVertexBuffer(u)),e=new Uint32Array(o.getIndexBuffer(u)),c=new Float32Array(o.getNormalBuffer(u)),s=o.meshHasUVs(u)?new Float32Array(o.getUVBuffer(u)):null;let n=`#usda 1.0
`;n+=`(
    defaultPrim = "Root"
    upAxis = "Y"
)

`,n+=`def Xform "Root"
{
`,n+=`    def Mesh "Mesh"
    {
`;const f=e.length/3;n+="        int[] faceVertexCounts = ["+Array(f).fill(3).join(", ")+`]
`,n+="        int[] faceVertexIndices = ["+Array.from(e).join(", ")+`]
`,n+="        point3f[] points = [";const d=[];for(let a=0;a<t.length;a+=3)d.push(`(${t[a]}, ${t[a+1]}, ${t[a+2]})`);if(n+=d.join(", "),n+=`]
`,c.length>0){n+="        normal3f[] normals = [";const a=[];for(let r=0;r<c.length;r+=3)a.push(`(${c[r]}, ${c[r+1]}, ${c[r+2]})`);n+=a.join(", "),n+=`]
`}if(s&&s.length>0){n+="        texCoord2f[] primvars:st = [";const a=[];for(let r=0;r<s.length;r+=2)a.push(`(${s[r]}, ${s[r+1]})`);n+=a.join(", "),n+=`]
`}return n+=`    }
}
`,n}export{E as exportUSDA,V as loadUSDA,F as loadUSDZ,_ as parseUSDA};
//# sourceMappingURL=usdLoader-Blb4juHb.js.map
