rayzee 7.6.0 → 7.7.0
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/README.md +1 -1
- package/dist/rayzee.es.js +104 -105
- package/dist/rayzee.es.js.map +1 -1
- package/dist/rayzee.umd.js +3 -3
- package/dist/rayzee.umd.js.map +1 -1
- package/package.json +1 -1
- package/src/Processor/ShaderBuilder.js +0 -3
- package/src/Stages/PathTracer.js +0 -3
- package/src/TSL/FinalWriteKernel.js +5 -2
package/dist/rayzee.umd.js
CHANGED
|
@@ -491,7 +491,7 @@
|
|
|
491
491
|
let p = o + xu * x + yv * y + z0 * z;
|
|
492
492
|
return vec4f( p, 1.0f / S );
|
|
493
493
|
}
|
|
494
|
-
`),qr=class{constructor(){this.prevColorTexNode=null,this.prevNormalDepthTexNode=null,this.prevAlbedoTexNode=null,this._sceneTextureNodes=null}updateSceneTextures(e){let t=this._sceneTextureNodes,n=e.environment,r=e.materialData;n.environmentTexture&&t.envTex&&(t.envTex.value=n.environmentTexture),r.albedoMaps&&t.albedoMapsTex&&(t.albedoMapsTex.value=r.albedoMaps),r.normalMaps&&t.normalMapsTex&&(t.normalMapsTex.value=r.normalMaps),r.bumpMaps&&t.bumpMapsTex&&(t.bumpMapsTex.value=r.bumpMaps),r.metalnessMaps&&t.metalnessMapsTex&&(t.metalnessMapsTex.value=r.metalnessMaps),r.roughnessMaps&&t.roughnessMapsTex&&(t.roughnessMapsTex.value=r.roughnessMaps),r.emissiveMaps&&t.emissiveMapsTex&&(t.emissiveMapsTex.value=r.emissiveMaps),r.displacementMaps&&t.displacementMapsTex&&(t.displacementMapsTex.value=r.displacementMaps),e.goboMaps&&t.goboMapsTex&&(t.goboMapsTex.value=e.goboMaps),e.iesProfiles&&t.iesProfilesTex&&(t.iesProfilesTex.value=e.iesProfiles),console.log(`ShaderBuilder: Scene textures updated in-place`)}updateGoboMaps(e){let t=this._sceneTextureNodes;!t||!t.goboMapsTex||e&&(t.goboMapsTex.value=e)}updateIESProfiles(e){let t=this._sceneTextureNodes;!t||!t.iesProfilesTex||e&&(t.iesProfilesTex.value=e)}getSceneTextureNodes(){return this._sceneTextureNodes}createSceneTextureNodes(e,t){let i=e.triangleStorageNode,a=e.bvhStorageNode,o=e.materialData.materialStorageNode,s=e.lightStorageNode;gr(e.uniforms.get(`enableAlphaShadows`));let c=(0,n.texture)(e.environment.environmentTexture),l=t.getReadTextures();this.prevColorTexNode=(0,n.texture)(l.color),this.prevNormalDepthTexNode=(0,n.texture)(l.normalDepth),this.prevAlbedoTexNode=(0,n.texture)(l.albedo);let u=()=>{let e=new r.DataArrayTexture(new Uint8Array([255,255,255,255]),1,1,1);return e.minFilter=r.LinearFilter,e.magFilter=r.LinearFilter,e.generateMipmaps=!1,e.needsUpdate=!0,(0,n.texture)(e)},d=e.materialData,f=d.albedoMaps?(0,n.texture)(d.albedoMaps):u(),p=d.normalMaps?(0,n.texture)(d.normalMaps):u(),m=d.bumpMaps?(0,n.texture)(d.bumpMaps):u(),h=d.metalnessMaps?(0,n.texture)(d.metalnessMaps):u(),g=d.roughnessMaps?(0,n.texture)(d.roughnessMaps):u(),_=d.emissiveMaps?(0,n.texture)(d.emissiveMaps):u(),v=d.displacementMaps?(0,n.texture)(d.displacementMaps):u(),y=e.goboMaps?(0,n.texture)(e.goboMaps):u();Lr(y);let b=e.iesProfiles?(0,n.texture)(e.iesProfiles):u();Vr(b),hr(f);let x={triStorage:i,bvhStorage:a,matStorage:o,lightBufferStorage:s,envTex:c,albedoMapsTex:f,normalMapsTex:p,bumpMapsTex:m,metalnessMapsTex:h,roughnessMapsTex:g,emissiveMapsTex:_,displacementMapsTex:v,goboMapsTex:y,iesProfilesTex:b};return this._sceneTextureNodes=x,x}dispose(){this.prevColorTexNode=null,this.prevNormalDepthTexNode=null,this.prevAlbedoTexNode=null,this._sceneTextureNodes=null}},Jr=class{constructor(e,t){this.traversalCost=e,this.intersectionCost=t,this.maxTreeletLeaves=7,this.minImprovement=.02,this.topologyCache=new Map;for(let e=3;e<=this.maxTreeletLeaves;e++)this.topologyCache.set(e,this.generateTopologies(e));this.stats={treeletsProcessed:0,treeletsImproved:0,totalSAHImprovement:0,averageSAHImprovement:0,optimizationTime:0}}generateTopologies(e){if(e===1)return[0];if(e===2)return[[0,1]];let t=[];for(let n=1;n<e;n++){let r=this.generateTopologies(n),i=this.generateTopologies(e-n);for(let e of r)for(let r of i)t.push([e,this.offsetTopology(r,n)])}return t}offsetTopology(e,t){return typeof e==`number`?e+t:[this.offsetTopology(e[0],t),this.offsetTopology(e[1],t)]}optimizeBVH(e){let t=performance.now();this.stats={treeletsProcessed:0,treeletsImproved:0,totalSAHImprovement:0,averageSAHImprovement:0,optimizationTime:0};let n=this.identifyTreeletRoots(e);for(let e=0;e<n.length;e++){if(performance.now()-t>3e4){console.warn(`TreeletOptimizer: timeout after ${e}/${n.length} treelets`);break}this.optimizeTreelet(n[e])}return this.stats.optimizationTime=performance.now()-t,this.stats.averageSAHImprovement=this.stats.treeletsProcessed>0?this.stats.totalSAHImprovement/this.stats.treeletsProcessed:0,e}identifyTreeletRoots(e){let t=[],n=new Set,r=[{node:e,visited:!1}];for(;r.length>0;){let e=r[r.length-1];if(e.visited){r.pop();let i=e.node;if(i.triangleCount>0||n.has(i))continue;let a=this.countLeaves(i);a>=3&&a<=this.maxTreeletLeaves&&(t.push(i),this.markSubtree(i,n))}else{e.visited=!0;let t=e.node;if(t.triangleCount>0)continue;t.rightChild&&r.push({node:t.rightChild,visited:!1}),t.leftChild&&r.push({node:t.leftChild,visited:!1})}}return t}countLeaves(e){return e?e.triangleCount>0?1:this.countLeaves(e.leftChild)+this.countLeaves(e.rightChild):0}markSubtree(e,t){e&&(t.add(e),!(e.triangleCount>0)&&(this.markSubtree(e.leftChild,t),this.markSubtree(e.rightChild,t)))}optimizeTreelet(e){let t=[];this.extractLeaves(e,t);let n=t.length;if(n<3||n>this.maxTreeletLeaves)return;this.stats.treeletsProcessed++;let r=this.evaluateSubtreeSAH(e),i=this.topologyCache.get(n);if(!i||i.length===0)return;let a=r,o=null,s=null;if(n<=5){let e=this.generatePermutations(n);for(let n of i)for(let r of e){let e=this.evaluateTopology(n,t,r);e<a&&(a=e,o=n,s=r)}}else{let e=Array.from({length:n},(e,t)=>t);for(let n of i){let r=this.evaluateTopology(n,t,e);r<a&&(a=r,o=n,s=e);let i=this.greedySwapOptimize(n,t,e,r);i.cost<a&&(a=i.cost,o=n,s=i.perm)}}let c=(r-a)/r;o&&c>this.minImprovement&&(this.reconstructTreelet(e,o,t,s),this.stats.treeletsImproved++,this.stats.totalSAHImprovement+=c)}extractLeaves(e,t){if(e){if(e.triangleCount>0){t.push({minX:e.minX,minY:e.minY,minZ:e.minZ,maxX:e.maxX,maxY:e.maxY,maxZ:e.maxZ,triangleOffset:e.triangleOffset,triangleCount:e.triangleCount});return}this.extractLeaves(e.leftChild,t),this.extractLeaves(e.rightChild,t)}}evaluateSubtreeSAH(e){if(!e)return 0;if(e.triangleCount>0)return this.surfaceAreaFlat(e.minX,e.minY,e.minZ,e.maxX,e.maxY,e.maxZ)*e.triangleCount*this.intersectionCost;let t=this.evaluateSubtreeSAH(e.leftChild),n=this.evaluateSubtreeSAH(e.rightChild);return this.surfaceAreaFlat(e.minX,e.minY,e.minZ,e.maxX,e.maxY,e.maxZ)*this.traversalCost+t+n}evaluateTopology(e,t,n){return this.evalTopoRecursive(e,t,n).cost}evalTopoRecursive(e,t,n){if(typeof e==`number`){let r=t[n[e]];return{cost:this.surfaceAreaFlat(r.minX,r.minY,r.minZ,r.maxX,r.maxY,r.maxZ)*r.triangleCount*this.intersectionCost,minX:r.minX,minY:r.minY,minZ:r.minZ,maxX:r.maxX,maxY:r.maxY,maxZ:r.maxZ}}let r=this.evalTopoRecursive(e[0],t,n),i=this.evalTopoRecursive(e[1],t,n),a=Math.min(r.minX,i.minX),o=Math.min(r.minY,i.minY),s=Math.min(r.minZ,i.minZ),c=Math.max(r.maxX,i.maxX),l=Math.max(r.maxY,i.maxY),u=Math.max(r.maxZ,i.maxZ);return{cost:this.surfaceAreaFlat(a,o,s,c,l,u)*this.traversalCost+r.cost+i.cost,minX:a,minY:o,minZ:s,maxX:c,maxY:l,maxZ:u}}surfaceAreaFlat(e,t,n,r,i,a){let o=r-e,s=i-t,c=a-n;return 2*(o*s+s*c+c*o)}generatePermutations(e){let t=[],n=Array.from({length:e},(e,t)=>t),r=i=>{if(i===e){t.push([...n]);return}for(let t=i;t<e;t++)[n[i],n[t]]=[n[t],n[i]],r(i+1),[n[i],n[t]]=[n[t],n[i]]};return r(0),t}greedySwapOptimize(e,t,n,r){let i=[...n],a=r,o=!0;for(;o;){o=!1;for(let n=0;n<i.length-1;n++)for(let r=n+1;r<i.length;r++){[i[n],i[r]]=[i[r],i[n]];let s=this.evaluateTopology(e,t,i);s<a?(a=s,o=!0):[i[n],i[r]]=[i[r],i[n]]}}return{perm:i,cost:a}}reconstructTreelet(e,t,n,r){let i=this.buildSubtree(t,n,r);e.minX=i.minX,e.minY=i.minY,e.minZ=i.minZ,e.maxX=i.maxX,e.maxY=i.maxY,e.maxZ=i.maxZ,e.leftChild=i.leftChild,e.rightChild=i.rightChild,e.triangleOffset=i.triangleOffset,e.triangleCount=i.triangleCount}buildSubtree(e,t,n){if(typeof e==`number`){let r=t[n[e]],i=new Yr;return i.minX=r.minX,i.minY=r.minY,i.minZ=r.minZ,i.maxX=r.maxX,i.maxY=r.maxY,i.maxZ=r.maxZ,i.triangleOffset=r.triangleOffset,i.triangleCount=r.triangleCount,i}let r=this.buildSubtree(e[0],t,n),i=this.buildSubtree(e[1],t,n),a=new Yr;return a.leftChild=r,a.rightChild=i,a.minX=Math.min(r.minX,i.minX),a.minY=Math.min(r.minY,i.minY),a.minZ=Math.min(r.minZ,i.minZ),a.maxX=Math.max(r.maxX,i.maxX),a.maxY=Math.max(r.maxY,i.maxY),a.maxZ=Math.max(r.maxZ,i.maxZ),a}setTreeletSize(e){this.maxTreeletLeaves=Math.max(3,Math.min(7,e));for(let e=3;e<=this.maxTreeletLeaves;e++)this.topologyCache.has(e)||this.topologyCache.set(e,this.generateTopologies(e))}setMinImprovement(e){this.minImprovement=Math.max(.001,e)}setMaxTreelets(){}getStatistics(){return{...this.stats}}},Yr=class{constructor(){this.minX=0,this.minY=0,this.minZ=0,this.maxX=0,this.maxY=0,this.maxZ=0,this.leftChild=null,this.rightChild=null,this.triangleOffset=0,this.triangleCount=0}},Xr=class{constructor(e,t){this.traversalCost=e,this.intersectionCost=t,this.batchSizeRatio=.02,this.maxIterations=2,this.timeBudgetMs=15e3,this.stats={reinsertionsApplied:0,iterations:0,timeMs:0}}setBatchSizeRatio(e){this.batchSizeRatio=Math.max(.005,Math.min(.1,e))}setMaxIterations(e){this.maxIterations=Math.max(1,Math.min(5,e))}getStatistics(){return{...this.stats}}surfaceArea(e){let t=e.maxX-e.minX,n=e.maxY-e.minY,r=e.maxZ-e.minZ;return t*n+n*r+r*t}buildParentMap(e){let t=new Map;t.set(e,{parent:null,isLeft:!1});let n=[e];for(;n.length>0;){let e=n.pop();e.triangleCount>0||(e.leftChild&&(t.set(e.leftChild,{parent:e,isLeft:!0}),n.push(e.leftChild)),e.rightChild&&(t.set(e.rightChild,{parent:e,isLeft:!1}),n.push(e.rightChild)))}return t}findCandidates(e,t,n){let r=[],i=[e];for(;i.length>0;){let a=i.pop();if(a!==e&&n.get(a).parent!==e){let e=this.surfaceArea(a);r.length<t?(r.push({node:a,cost:e}),r.length===t&&this._heapify(r)):e>r[0].cost&&(r[0]={node:a,cost:e},this._siftDown(r,0))}a.triangleCount===0&&(a.leftChild&&i.push(a.leftChild),a.rightChild&&i.push(a.rightChild))}return r}_heapify(e){for(let t=(e.length>>1)-1;t>=0;t--)this._siftDown(e,t)}_siftDown(e,t){let n=e.length;for(;;){let r=t,i=2*t+1,a=2*t+2;if(i<n&&e[i].cost<e[r].cost&&(r=i),a<n&&e[a].cost<e[r].cost&&(r=a),r===t)break;let o=e[t];e[t]=e[r],e[r]=o,t=r}}findReinsertion(e,t,n){let r=n.get(e),i=r.parent;if(!i)return null;let a=r.isLeft?i.rightChild:i.leftChild,o=this.surfaceArea(e),s=this.surfaceArea(i),c=null,l=0,u=s,d=a.minX,f=a.minY,p=a.minZ,m=a.maxX,h=a.maxY,g=a.maxZ,_=a,v=i,y=[];do{for(y.length=0,y.push(u,_);y.length>0;){let t=y.pop(),n=y.pop();if(n-o<=l)continue;let r=Math.min(t.minX,e.minX),i=Math.min(t.minY,e.minY),a=Math.min(t.minZ,e.minZ),s=Math.max(t.maxX,e.maxX),u=Math.max(t.maxY,e.maxY),d=Math.max(t.maxZ,e.maxZ),f=s-r,p=u-i,m=d-a,h=n-(f*p+p*m+m*f);if(h>l&&(c=t,l=h),t.triangleCount===0&&t.leftChild&&t.rightChild){let e=h+this.surfaceArea(t);y.push(e,t.leftChild),y.push(e,t.rightChild)}}let t=n.get(v);if(!t||t.parent===null)break;if(v!==i){d=Math.min(d,_.minX),f=Math.min(f,_.minY),p=Math.min(p,_.minZ),m=Math.max(m,_.maxX),h=Math.max(h,_.maxY),g=Math.max(g,_.maxZ);let e=m-d,t=h-f,n=g-p,r=e*t+t*n+n*e;u+=this.surfaceArea(v)-r}let r=t.parent;_=t.isLeft?r.rightChild:r.leftChild,v=r}while(n.get(v).parent!==null);return c===a||c===i?null:c?{from:e,to:c,areaDiff:l}:null}getConflicts(e,t,n){let r=n.get(e);return[t,e,r.isLeft?r.parent.rightChild:r.parent.leftChild,n.get(t).parent,r.parent]}reinsertNode(e,t,n){let r=n.get(e),i=r.parent,a=r.isLeft?i.rightChild:i.leftChild,o=n.get(i),s=o.parent,c=n.get(t),l=c.parent;o.isLeft?s.leftChild=a:s.rightChild=a,i.leftChild=e,i.rightChild=t,i.triangleOffset=0,i.triangleCount=0,i.minX=Math.min(e.minX,t.minX),i.minY=Math.min(e.minY,t.minY),i.minZ=Math.min(e.minZ,t.minZ),i.maxX=Math.max(e.maxX,t.maxX),i.maxY=Math.max(e.maxY,t.maxY),i.maxZ=Math.max(e.maxZ,t.maxZ),c.isLeft?l.leftChild=i:l.rightChild=i,n.set(a,{parent:s,isLeft:o.isLeft}),n.set(i,{parent:l,isLeft:c.isLeft}),n.set(e,{parent:i,isLeft:!0}),n.set(t,{parent:i,isLeft:!1}),this.refitFrom(s,n),this.refitFrom(l,n)}refitFrom(e,t){let n=e;for(;n;){if(n.triangleCount===0&&n.leftChild&&n.rightChild){let e=n.leftChild,t=n.rightChild;n.minX=Math.min(e.minX,t.minX),n.minY=Math.min(e.minY,t.minY),n.minZ=Math.min(e.minZ,t.minZ),n.maxX=Math.max(e.maxX,t.maxX),n.maxY=Math.max(e.maxY,t.maxY),n.maxZ=Math.max(e.maxZ,t.maxZ)}let e=t.get(n);n=e?e.parent:null}}optimizeBVH(e,t){let n=performance.now();this.stats={reinsertionsApplied:0,iterations:0,timeMs:0};for(let r=0;r<this.maxIterations&&!(performance.now()-n>this.timeBudgetMs);r++){let i=this.buildParentMap(e),a=i.size,o=Math.max(1,Math.floor(a*this.batchSizeRatio));t&&t(`Reinsertion iter ${r+1}/${this.maxIterations}: selecting ${o} candidates`);let s=this.findCandidates(e,o,i),c=[];for(let t=0;t<s.length&&!(performance.now()-n>this.timeBudgetMs);t++){let n=this.findReinsertion(s[t].node,e,i);n&&n.areaDiff>0&&c.push(n)}c.sort((e,t)=>t.areaDiff-e.areaDiff);let l=new Set,u=0;for(let e of c){let t=this.getConflicts(e.from,e.to,i);if(!t.some(e=>l.has(e))){for(let e of t)l.add(e);this.reinsertNode(e.from,e.to,i),u++}}if(this.stats.reinsertionsApplied+=u,this.stats.iterations=r+1,t&&t(`Reinsertion iter ${r+1}: applied ${u} reinsertions`),u===0)break}return this.stats.timeMs=performance.now()-n,this.stats}},Zr=``+(typeof document>`u`&&typeof location>`u`?require(`url`).pathToFileURL(__dirname+`/assets/BVHWorker-CNJ0UBQz.js`).href:new URL(`assets/BVHWorker-CNJ0UBQz.js`,typeof document>`u`?location.href:document.currentScript&&document.currentScript.tagName.toUpperCase()===`SCRIPT`&&document.currentScript.src||document.baseURI).href),Qr={FLOATS_PER_TRIANGLE:32,POSITION_A_OFFSET:0,POSITION_B_OFFSET:4,POSITION_C_OFFSET:8,NORMAL_A_OFFSET:12,NORMAL_B_OFFSET:16,NORMAL_C_OFFSET:20,UV_AB_OFFSET:24,UV_C_MAT_OFFSET:28},$r=Qr.FLOATS_PER_TRIANGLE,ei=class{constructor(){this.minX=0,this.minY=0,this.minZ=0,this.maxX=0,this.maxY=0,this.maxZ=0,this.leftChild=null,this.rightChild=null,this.triangleOffset=0,this.triangleCount=0}},ti=class{constructor(){this.useWorker=!0,this.maxLeafSize=8,this.numBins=32,this.minBins=8,this.maxBins=64,this.totalNodes=0,this.processedTriangles=0,this.totalTriangles=0,this.lastProgressUpdate=0,this.progressUpdateInterval=100,this.traversalCost=1,this.intersectionCost=2.5,this.useMortonCodes=!0,this.mortonBits=10,this.mortonClusterThreshold=128,this.enableObjectMedianFallback=!0,this.enableSpatialMedianFallback=!0,this.splitStats={sahSplits:0,objectMedianSplits:0,spatialMedianSplits:0,failedSplits:0,avgBinsUsed:0,totalSplitAttempts:0,mortonSortTime:0,totalBuildTime:0,treeletOptimizationTime:0,treeletsProcessed:0,treeletsImproved:0,averageSAHImprovement:0,reinsertionOptimizationTime:0,reinsertionsApplied:0,reinsertionIterations:0},this.enableTreeletOptimization=!0,this.treeletSize=5,this.treeletOptimizationPasses=1,this.treeletMinImprovement=.02,this.maxTreeletDepth=3,this.maxTreeletsPerScene=20,this.treeletComplexityThreshold=5e4,this.enableReinsertionOptimization=!0,this.reinsertionBatchSizeRatio=.02,this.reinsertionMaxIterations=2,this.initializeBinArrays(),this._partResult={mid:0,lMinX:0,lMinY:0,lMinZ:0,lMaxX:0,lMaxY:0,lMaxZ:0,rMinX:0,rMinY:0,rMinZ:0,rMaxX:0,rMaxY:0,rMaxZ:0},this.centroids=null,this.bMin=null,this.bMax=null,this.indices=null,this.mortonCodes=null,this.triangles=null,this.reorderedTriangleData=null}initializeBinArrays(){let e=this.maxBins;this.binBoundsMin=new Float32Array(e*3),this.binBoundsMax=new Float32Array(e*3),this.binCounts=new Uint32Array(e),this.leftPrefixMin=new Float32Array(e*3),this.leftPrefixMax=new Float32Array(e*3),this.leftPrefixCount=new Uint32Array(e),this.rightPrefixMin=new Float32Array(e*3),this.rightPrefixMax=new Float32Array(e*3),this.rightPrefixCount=new Uint32Array(e)}getOptimalBinCount(e){return e<=16?this.minBins:e<=64?16:e<=256?32:e<=1024?48:this.maxBins}setAdaptiveBinConfig(e){e.minBins!==void 0&&(this.minBins=Math.max(4,e.minBins)),e.maxBins!==void 0&&(this.maxBins=Math.min(128,e.maxBins)),e.baseBins!==void 0&&(this.numBins=e.baseBins),e.maxBins!==void 0&&this.initializeBinArrays()}setMortonConfig(e){e.enabled!==void 0&&(this.useMortonCodes=e.enabled),e.bits!==void 0&&(this.mortonBits=Math.max(6,Math.min(10,e.bits))),e.threshold!==void 0&&(this.mortonClusterThreshold=Math.max(16,e.threshold))}setFallbackConfig(e){e.objectMedian!==void 0&&(this.enableObjectMedianFallback=e.objectMedian),e.spatialMedian!==void 0&&(this.enableSpatialMedianFallback=e.spatialMedian)}setTreeletConfig(e){e.enabled!==void 0&&(this.enableTreeletOptimization=e.enabled),e.size!==void 0&&(this.treeletSize=Math.max(3,Math.min(12,e.size))),e.passes!==void 0&&(this.treeletOptimizationPasses=Math.max(1,Math.min(3,e.passes))),e.minImprovement!==void 0&&(this.treeletMinImprovement=Math.max(.001,e.minImprovement))}disableTreeletOptimization(){this.enableTreeletOptimization=!1}setReinsertionConfig(e){e.enabled!==void 0&&(this.enableReinsertionOptimization=e.enabled),e.batchSizeRatio!==void 0&&(this.reinsertionBatchSizeRatio=Math.max(.005,Math.min(.1,e.batchSizeRatio))),e.maxIterations!==void 0&&(this.reinsertionMaxIterations=Math.max(1,Math.min(5,e.maxIterations)))}initializeTriangleArrays(){let e=this.totalTriangles,t=this.triangles,n=Qr.POSITION_A_OFFSET,r=Qr.POSITION_B_OFFSET,i=Qr.POSITION_C_OFFSET;for(let a=0;a<e;a++){let e=a*$r,o=t[e+n],s=t[e+n+1],c=t[e+n+2],l=t[e+r],u=t[e+r+1],d=t[e+r+2],f=t[e+i],p=t[e+i+1],m=t[e+i+2],h=a*3;this.centroids[h]=(o+l+f)/3,this.centroids[h+1]=(s+u+p)/3,this.centroids[h+2]=(c+d+m)/3,this.bMin[h]=o<l?o<f?o:f:l<f?l:f,this.bMin[h+1]=s<u?s<p?s:p:u<p?u:p,this.bMin[h+2]=c<d?c<m?c:m:d<m?d:m,this.bMax[h]=o>l?o>f?o:f:l>f?l:f,this.bMax[h+1]=s>u?s>p?s:p:u>p?u:p,this.bMax[h+2]=c>d?c>m?c:m:d>m?d:m,this.indices[a]=a}}expandBits(e){return e=e*65537&4278190335,e=e*257&251719695,e=e*17&3272356035,e=e*5&1227133513,e}morton3D(e,t,n){return(this.expandBits(n)<<2)+(this.expandBits(t)<<1)+this.expandBits(e)}computeMortonCodeForIndex(e,t,n,r,i,a,o){let s=this.centroids,c=e*3,l=(1<<this.mortonBits)-1,u=i>0?(s[c]-t)/i:0,d=a>0?(s[c+1]-n)/a:0,f=o>0?(s[c+2]-r)/o:0,p=Math.max(0,Math.min(l,Math.floor(u*l))),m=Math.max(0,Math.min(l,Math.floor(d*l))),h=Math.max(0,Math.min(l,Math.floor(f*l)));return this.morton3D(p,m,h)}sortTrianglesByMortonCode(){let e=this.totalTriangles;if(!this.useMortonCodes||e<this.mortonClusterThreshold)return;let t=performance.now(),n=this.centroids,r=this.indices,i=1/0,a=1/0,o=1/0,s=-1/0,c=-1/0,l=-1/0;for(let t=0;t<e;t++){let e=r[t]*3,u=n[e],d=n[e+1],f=n[e+2];u<i&&(i=u),d<a&&(a=d),f<o&&(o=f),u>s&&(s=u),d>c&&(c=d),f>l&&(l=f)}let u=s-i,d=c-a,f=l-o,p=this.mortonCodes,m=(1<<this.mortonBits)-1,h=u>0?m/u:0,g=d>0?m/d:0,_=f>0?m/f:0;for(let t=0;t<e;t++){let e=r[t],s=e*3,c=(n[s]-i)*h,l=(n[s+1]-a)*g,u=(n[s+2]-o)*_;c=c<0?0:(c>m?m:c)|0,l=l<0?0:(l>m?m:l)|0,u=u<0?0:(u>m?m:u)|0,c=c*65537&4278190335,c=c*257&251719695,c=c*17&3272356035,c=c*5&1227133513,l=l*65537&4278190335,l=l*257&251719695,l=l*17&3272356035,l=l*5&1227133513,u=u*65537&4278190335,u=u*257&251719695,u=u*17&3272356035,u=u*5&1227133513,p[e]=(u<<2)+(l<<1)+c}let v=new Uint32Array(e),y=new Uint32Array(256);for(let t=0;t<32;t+=8){y.fill(0);for(let n=0;n<e;n++)y[p[r[n]]>>>t&255]++;let n=0;for(let e=0;e<256;e++){let t=y[e];y[e]=n,n+=t}for(let n=0;n<e;n++){let e=p[r[n]]>>>t&255;v[y[e]++]=r[n]}r.set(v)}this.splitStats.mortonSortTime+=performance.now()-t}build(e,t=30,n=null){return this.totalTriangles=e.byteLength/($r*4),this.processedTriangles=0,this.lastProgressUpdate=performance.now(),this.useWorker&&typeof Worker<`u`?new Promise((r,i)=>{let a=a=>{let o=this.totalTriangles,s=typeof SharedArrayBuffer<`u`;console.log(`[BVHBuilder] SharedArrayBuffer: ${s?`enabled`:`unavailable (using transfer fallback)`}`);let c=s?new SharedArrayBuffer(o*$r*4):null;a.onmessage=e=>{let{bvhData:t,triangles:o,originalToBvh:s,error:l,progress:u,treeletStats:d}=e.data;if(l){a.terminate(),i(Error(l));return}if(u!==void 0&&n){n(u);return}d&&(this.splitStats=d),a.terminate(),r({bvhData:t,bvhRoot:!0,reorderedTriangles:c?new Float32Array(c):o,originalToBvh:s||null})},a.onerror=e=>{a.terminate(),i(e)};let l=e.buffer,u={triangleData:l,triangleByteOffset:e.byteOffset,triangleByteLength:e.byteLength,triangleCount:o,depth:t,reportProgress:!!n,sharedReorderBuffer:c,treeletOptimization:{enabled:this.enableTreeletOptimization,size:this.treeletSize,passes:this.treeletOptimizationPasses,minImprovement:this.treeletMinImprovement},reinsertionOptimization:{enabled:this.enableReinsertionOptimization,batchSizeRatio:this.reinsertionBatchSizeRatio,maxIterations:this.reinsertionMaxIterations}};a.postMessage(u,[l])};try{a(new Worker(Zr,{type:`module`}))}catch(i){i.name===`SecurityError`?Je(Zr).then(a).catch(()=>{console.warn(`Worker fetch fallback failed, using synchronous build`),r(this._buildSyncAndFlatten(e,t,n))}):(console.warn(`Worker creation failed, falling back to synchronous build:`,i),r(this._buildSyncAndFlatten(e,t,n)))}}):new Promise(r=>{r(this._buildSyncAndFlatten(e,t,n))})}_buildSyncAndFlatten(e,t,n){let r=this.buildSync(e,t,n);return{bvhData:this.flattenBVH(r),bvhRoot:!0,reorderedTriangles:this.reorderedTriangleData||null,originalToBvh:this.originalToBvhMap||null}}buildSync(e,t=30,n=null,r=null){let i=performance.now();this.totalNodes=0,this.processedTriangles=0,this.triangles=e,this.totalTriangles=e.byteLength/($r*4),this.lastProgressUpdate=performance.now(),this.splitStats={sahSplits:0,objectMedianSplits:0,spatialMedianSplits:0,failedSplits:0,avgBinsUsed:0,totalSplitAttempts:0,mortonSortTime:0,totalBuildTime:0,treeletOptimizationTime:0,treeletsProcessed:0,treeletsImproved:0,averageSAHImprovement:0,reinsertionOptimizationTime:0,reinsertionsApplied:0,reinsertionIterations:0,saOrderTime:0,initTime:0,sahBuildTime:0,reorderTime:0};let a=this.totalTriangles,o=performance.now();this.centroids=new Float32Array(a*3),this.bMin=new Float32Array(a*3),this.bMax=new Float32Array(a*3),this.indices=new Uint32Array(a),this.mortonCodes=new Uint32Array(a),this.initializeTriangleArrays(),this.splitStats.initTime=performance.now()-o,this.sortTrianglesByMortonCode();let s=performance.now(),c=this.buildNodeRecursive(0,a,t,n);if(this.splitStats.sahBuildTime=performance.now()-s,this.enableTreeletOptimization&&this.totalTriangles>1e3){let e=this.totalTriangles>this.treeletComplexityThreshold,t=e?3:this.treeletSize,r=e?10:this.maxTreeletsPerScene,i=new Jr(this.traversalCost,this.intersectionCost);i.setTreeletSize(t),i.setMinImprovement(this.treeletMinImprovement),i.setMaxTreelets(r);let a=performance.now();for(let e=0;e<this.treeletOptimizationPasses;e++){let t=n?t=>{n(`Treelet optimization pass ${e+1}/${this.treeletOptimizationPasses}: ${t}`)}:null;try{i.optimizeBVH(c,t)}catch(t){console.error(`TreeletOptimizer: Error in pass ${e+1}:`,t);break}let r=i.getStatistics(),o=performance.now()-a;if(r.treeletsImproved===0&&e>0||o>15e3)break}let o=performance.now()-a;this.splitStats.treeletOptimizationTime=o;let s=i.getStatistics();this.splitStats.treeletsProcessed=s.treeletsProcessed,this.splitStats.treeletsImproved=s.treeletsImproved,this.splitStats.averageSAHImprovement=s.averageSAHImprovement}if(this.enableReinsertionOptimization&&this.totalTriangles>1e3){let e=new Xr(this.traversalCost,this.intersectionCost);e.setBatchSizeRatio(this.reinsertionBatchSizeRatio),e.setMaxIterations(this.reinsertionMaxIterations);let t=n?e=>{n(e)}:null;try{e.optimizeBVH(c,t)}catch(e){console.error(`ReinsertionOptimizer: Error:`,e)}let r=e.getStatistics();this.splitStats.reinsertionOptimizationTime=r.timeMs,this.splitStats.reinsertionsApplied=r.reinsertionsApplied,this.splitStats.reinsertionIterations=r.iterations}let l=performance.now();this.applySAOrdering(c),this.splitStats.saOrderTime=performance.now()-l;let u=performance.now(),d=this.triangles,f=r||new Float32Array(a*$r);for(let e=0;e<a;e++){let t=this.indices[e]*$r,n=e*$r;f.set(d.subarray(t,t+$r),n)}this.reorderedTriangleData=f;let p=new Uint32Array(a);for(let e=0;e<a;e++)p[this.indices[e]]=e;this.originalToBvhMap=p,this.splitStats.reorderTime=performance.now()-u,this.splitStats.totalBuildTime=performance.now()-i;let m=this.splitStats.totalBuildTime,h=this.splitStats;return console.log(`[BVH] ${a.toLocaleString()} tris → ${this.totalNodes} nodes in ${Math.round(m)}ms | SAH ${h.sahSplits} objMed ${h.objectMedianSplits} spatMed ${h.spatialMedianSplits} failed ${h.failedSplits}`+(h.treeletsProcessed?` | treelets ${h.treeletsImproved}/${h.treeletsProcessed} improved`:``)+(h.reinsertionsApplied?` | reinsertions ${h.reinsertionsApplied}`:``)),n&&n(100),this.centroids=null,this.bMin=null,this.bMax=null,this.mortonCodes=null,c}updateProgress(e,t){if(!t)return;this.processedTriangles+=e;let n=performance.now();n-this.lastProgressUpdate<this.progressUpdateInterval||(this.lastProgressUpdate=n,t(Math.min(Math.floor(this.processedTriangles/this.totalTriangles*100),99)))}buildNodeRecursiveToDepth(e,t,n,r,i,a,o,s,c,l,u){let d=new ei;this.totalNodes++;let f=t-e;if(a===void 0?this.updateNodeBounds(d,e,t):(d.minX=a,d.minY=o,d.minZ=s,d.maxX=c,d.maxY=l,d.maxZ=u),f<=this.maxLeafSize||n<=0)return d.triangleOffset=e,d.triangleCount=f,this.updateProgress(f,i),d;if(r<=0&&f>this.maxLeafSize*16){let r=this.frontierTasks.length;return d.triangleOffset=e,d.triangleCount=f,d.isFrontier=!0,d.frontierTaskId=r,this.frontierTasks.push({taskId:r,start:e,end:t,depth:n,preMinX:d.minX,preMinY:d.minY,preMinZ:d.minZ,preMaxX:d.maxX,preMaxY:d.maxY,preMaxZ:d.maxZ}),d}let p=this.findBestSplitPositionSAH(e,t,d);if(!p.success){if(this.splitStats.failedSplits++,r>0||f<=this.maxLeafSize*16)return d.triangleOffset=e,d.triangleCount=f,this.updateProgress(f,i),d;let a=this.frontierTasks.length;return d.triangleOffset=e,d.triangleCount=f,d.isFrontier=!0,d.frontierTaskId=a,this.frontierTasks.push({taskId:a,start:e,end:t,depth:n,preMinX:d.minX,preMinY:d.minY,preMinZ:d.minZ,preMaxX:d.maxX,preMaxY:d.maxY,preMaxZ:d.maxZ}),d}p.method===`SAH`?this.splitStats.sahSplits++:p.method===`object_median`?this.splitStats.objectMedianSplits++:p.method===`spatial_median`&&this.splitStats.spatialMedianSplits++,this.partitionWithBounds(e,t,p.axis,p.pos);let m=this._partResult,h=m.mid,g=m.lMinX,_=m.lMinY,v=m.lMinZ,y=m.lMaxX,b=m.lMaxY,x=m.lMaxZ,S=m.rMinX,C=m.rMinY,w=m.rMinZ,T=m.rMaxX,E=m.rMaxY,D=m.rMaxZ;return h===e||h===t?(d.triangleOffset=e,d.triangleCount=f,this.updateProgress(f,i),d):(d.leftChild=this.buildNodeRecursiveToDepth(e,h,n-1,r-1,i,g,_,v,y,b,x),d.rightChild=this.buildNodeRecursiveToDepth(h,t,n-1,r-1,i,S,C,w,T,E,D),d)}buildNodeRecursive(e,t,n,r,i,a,o,s,c,l){let u=new ei;this.totalNodes++;let d=t-e;if(i===void 0?this.updateNodeBounds(u,e,t):(u.minX=i,u.minY=a,u.minZ=o,u.maxX=s,u.maxY=c,u.maxZ=l),d<=this.maxLeafSize||n<=0)return u.triangleOffset=e,u.triangleCount=d,this.updateProgress(d,r),u;let f=this.findBestSplitPositionSAH(e,t,u);if(!f.success)return this.splitStats.failedSplits++,u.triangleOffset=e,u.triangleCount=d,this.updateProgress(d,r),u;f.method===`SAH`?this.splitStats.sahSplits++:f.method===`object_median`?this.splitStats.objectMedianSplits++:f.method===`spatial_median`&&this.splitStats.spatialMedianSplits++,this.partitionWithBounds(e,t,f.axis,f.pos);let p=this._partResult,m=p.mid,h=p.lMinX,g=p.lMinY,_=p.lMinZ,v=p.lMaxX,y=p.lMaxY,b=p.lMaxZ,x=p.rMinX,S=p.rMinY,C=p.rMinZ,w=p.rMaxX,T=p.rMaxY,E=p.rMaxZ;return m===e||m===t?(u.triangleOffset=e,u.triangleCount=d,this.updateProgress(d,r),u):(u.leftChild=this.buildNodeRecursive(e,m,n-1,r,h,g,_,v,y,b),u.rightChild=this.buildNodeRecursive(m,t,n-1,r,x,S,C,w,T,E),u)}partitionWithBounds(e,t,n,r){let i=this.indices,a=this.centroids,o=this.bMin,s=this.bMax,c=e,l=t-1,u=1/0,d=1/0,f=1/0,p=-1/0,m=-1/0,h=-1/0,g=1/0,_=1/0,v=1/0,y=-1/0,b=-1/0,x=-1/0;for(;c<=l;){let e=i[c],t=e*3;a[t+n]<=r?(o[t]<u&&(u=o[t]),o[t+1]<d&&(d=o[t+1]),o[t+2]<f&&(f=o[t+2]),s[t]>p&&(p=s[t]),s[t+1]>m&&(m=s[t+1]),s[t+2]>h&&(h=s[t+2]),c++):(o[t]<g&&(g=o[t]),o[t+1]<_&&(_=o[t+1]),o[t+2]<v&&(v=o[t+2]),s[t]>y&&(y=s[t]),s[t+1]>b&&(b=s[t+1]),s[t+2]>x&&(x=s[t+2]),i[c]=i[l],i[l]=e,l--)}let S=this._partResult;return S.mid=c,S.lMinX=u,S.lMinY=d,S.lMinZ=f,S.lMaxX=p,S.lMaxY=m,S.lMaxZ=h,S.rMinX=g,S.rMinY=_,S.rMinZ=v,S.rMaxX=y,S.rMaxY=b,S.rMaxZ=x,S}updateNodeBounds(e,t,n){let r=1/0,i=1/0,a=1/0,o=-1/0,s=-1/0,c=-1/0,l=this.indices,u=this.bMin,d=this.bMax;for(let e=t;e<n;e++){let t=l[e]*3;u[t]<r&&(r=u[t]),u[t+1]<i&&(i=u[t+1]),u[t+2]<a&&(a=u[t+2]),d[t]>o&&(o=d[t]),d[t+1]>s&&(s=d[t+1]),d[t+2]>c&&(c=d[t+2])}e.minX=r,e.minY=i,e.minZ=a,e.maxX=o,e.maxY=s,e.maxZ=c}findBestSplitPositionSAH(e,t,n){let r=1/0,i=-1,a=0,o=this.computeSurfaceAreaFlat(n.minX,n.minY,n.minZ,n.maxX,n.maxY,n.maxZ),s=t-e,c=this.intersectionCost*s,l=this.getOptimalBinCount(s);this.splitStats.totalSplitAttempts++,this.splitStats.avgBinsUsed=(this.splitStats.avgBinsUsed*(this.splitStats.totalSplitAttempts-1)+l)/this.splitStats.totalSplitAttempts;let u=this.indices,d=this.centroids,f=this.bMin,p=this.bMax,m=this.binBoundsMin,h=this.binBoundsMax,g=this.binCounts,_=this.leftPrefixMin,v=this.leftPrefixMax,y=this.leftPrefixCount,b=this.rightPrefixMin,x=this.rightPrefixMax,S=this.rightPrefixCount,C=1/0,w=-1/0,T=1/0,E=-1/0,D=1/0,O=-1/0;for(let n=e;n<t;n++){let e=u[n]*3,t=d[e],r=d[e+1],i=d[e+2];t<C&&(C=t),t>w&&(w=t),r<T&&(T=r),r>E&&(E=r),i<D&&(D=i),i>O&&(O=i)}let k=[C,T,D],A=[w,E,O];for(let n=0;n<3;n++){let s=k[n],C=A[n];if(C-s<1e-6)continue;for(let e=0;e<l;e++){g[e]=0;let t=e*3;m[t]=1/0,m[t+1]=1/0,m[t+2]=1/0,h[t]=-1/0,h[t+1]=-1/0,h[t+2]=-1/0}let w=l/(C-s);for(let r=e;r<t;r++){let e=u[r],t=d[e*3+n],i=Math.floor((t-s)*w);i>=l&&(i=l-1),g[i]++;let a=i*3,o=e*3;f[o]<m[a]&&(m[a]=f[o]),f[o+1]<m[a+1]&&(m[a+1]=f[o+1]),f[o+2]<m[a+2]&&(m[a+2]=f[o+2]),p[o]>h[a]&&(h[a]=p[o]),p[o+1]>h[a+1]&&(h[a+1]=p[o+1]),p[o+2]>h[a+2]&&(h[a+2]=p[o+2])}y[0]=g[0],_[0]=m[0],_[1]=m[1],_[2]=m[2],v[0]=h[0],v[1]=h[1],v[2]=h[2];for(let e=1;e<l;e++){let t=e*3,n=(e-1)*3;y[e]=y[e-1]+g[e];let r=_[n],i=m[t],a=_[n+1],o=m[t+1],s=_[n+2],c=m[t+2];_[t]=r<i?r:i,_[t+1]=a<o?a:o,_[t+2]=s<c?s:c;let l=v[n],u=h[t],d=v[n+1],f=h[t+1],p=v[n+2],b=h[t+2];v[t]=l>u?l:u,v[t+1]=d>f?d:f,v[t+2]=p>b?p:b}let T=l-1,E=T*3;S[T]=g[T],b[E]=m[E],b[E+1]=m[E+1],b[E+2]=m[E+2],x[E]=h[E],x[E+1]=h[E+1],x[E+2]=h[E+2];for(let e=T-1;e>=0;e--){let t=e*3,n=(e+1)*3;S[e]=S[e+1]+g[e];let r=b[n],i=m[t],a=b[n+1],o=m[t+1],s=b[n+2],c=m[t+2];b[t]=r<i?r:i,b[t+1]=a<o?a:o,b[t+2]=s<c?s:c;let l=x[n],u=h[t],d=x[n+1],f=h[t+1],p=x[n+2],_=h[t+2];x[t]=l>u?l:u,x[t+1]=d>f?d:f,x[t+2]=p>_?p:_}for(let e=1;e<l;e++){let t=(e-1)*3,u=e*3,d=y[e-1],f=S[e];if(d===0||f===0)continue;let p=v[t]-_[t],m=v[t+1]-_[t+1],h=v[t+2]-_[t+2],g=2*(p*m+m*h+h*p),w=x[u]-b[u],T=x[u+1]-b[u+1],E=x[u+2]-b[u+2],D=2*(w*T+T*E+E*w),O=this.traversalCost+g/o*d*this.intersectionCost+D/o*f*this.intersectionCost;O<r&&O<c&&(r=O,i=n,a=s+(C-s)*e/l)}}return i===-1?this.enableObjectMedianFallback?this.findObjectMedianSplit(e,t):this.enableSpatialMedianFallback?this.findSpatialMedianSplit(e,t):{success:!1,method:`fallbacks_disabled`}:{success:!0,axis:i,pos:a,method:`SAH`,binsUsed:l}}findObjectMedianSplit(e,t){let n=this.indices,r=this.centroids,i=-1,a=-1;for(let o=0;o<3;o++){let s=1/0,c=-1/0;for(let i=e;i<t;i++){let e=r[n[i]*3+o];e<s&&(s=e),e>c&&(c=e)}let l=c-s;l>a&&(a=l,i=o)}if(i===-1||a<1e-10)return this.enableSpatialMedianFallback?this.findSpatialMedianSplit(e,t):{success:!1,method:`object_median_failed`};let o=t-e,s=e+Math.floor(o/2);this.quickselect(e,t,s,i);let c=r[n[s]*3+i],l=!0;for(let e=s+1;e<t;e++)if(r[n[e]*3+i]>c){l=!1;break}if(l){let a=-1/0;for(let t=e;t<s;t++){let e=r[n[t]*3+i];e>a&&(a=e)}if(a<c)c=(a+c)*.5;else return this.enableSpatialMedianFallback?this.findSpatialMedianSplit(e,t):{success:!1,method:`object_median_degenerate`}}return{success:!0,axis:i,pos:c,method:`object_median`}}findSpatialMedianSplit(e,t){let n=this.indices,r=this.centroids,i=this.bMin,a=this.bMax,o=-1,s=-1,c=0,l=0;for(let r=0;r<3;r++){let u=1/0,d=-1/0;for(let o=e;o<t;o++){let e=n[o]*3+r;i[e]<u&&(u=i[e]),a[e]>d&&(d=a[e])}let f=d-u;f>s&&(s=f,o=r,c=u,l=d)}if(o===-1||s<1e-12)return{success:!1,method:`spatial_median_failed`};let u=(c+l)*.5,d=t-e,f=0;for(let i=e;i<t;i++)r[n[i]*3+o]<=u&&f++;if(f===0||f===d){let i=e+Math.floor(d/2);this.quickselect(e,t,i,o);let a=r[n[i]*3+o],s=!0;for(let i=e;i<t;i++)if(r[n[i]*3+o]!==a){s=!1;break}if(s)return{success:!1,method:`spatial_median_degenerate`};let c=-1/0;for(let t=e;t<i;t++){let e=r[n[t]*3+o];e>c&&(c=e)}if(c<a)u=(c+a)*.5;else{let e=1/0;for(let a=i+1;a<t;a++){let t=r[n[a]*3+o];t<e&&(e=t)}u=(a+e)*.5}}return{success:!0,axis:o,pos:u,method:`spatial_median`}}quickselect(e,t,n,r){let i=this.indices,a=this.centroids,o=e,s=t-1;for(;o<s;){let e=o+s>>>1,t=a[i[o]*3+r],c=a[i[e]*3+r],l=a[i[s]*3+r];if(t>c){let t=i[o];i[o]=i[e],i[e]=t}if(t>l){let e=i[o];i[o]=i[s],i[s]=e}if(c>l){let t=i[e];i[e]=i[s],i[s]=t}let u=a[i[e]*3+r],d=o,f=s;for(;d<=f;){for(;a[i[d]*3+r]<u;)d++;for(;a[i[f]*3+r]>u;)f--;if(d<=f){let e=i[d];i[d]=i[f],i[f]=e,d++,f--}}f<n&&(o=d),d>n&&(s=f)}}applySAOrdering(e){if(!e||!e.leftChild)return;let t=[e],n=[];for(;t.length>0;){let e=t.pop();!e.leftChild||!e.rightChild||(n.push(e),t.push(e.leftChild),t.push(e.rightChild))}for(let e=n.length-1;e>=0;e--){let t=n[e],r=t.leftChild,i=t.rightChild,a=r.maxX-r.minX,o=r.maxY-r.minY,s=r.maxZ-r.minZ,c=i.maxX-i.minX,l=i.maxY-i.minY,u=i.maxZ-i.minZ;c*l+l*u+u*c>a*o+o*s+s*a&&(t.leftChild=i,t.rightChild=r)}}flattenBVH(e){let t=[],n=[e];for(;n.length>0;){let e=n.pop();e._flatIndex=t.length,t.push(e),e.rightChild&&n.push(e.rightChild),e.leftChild&&n.push(e.leftChild)}let r=new Float32Array(t.length*16);for(let e=0;e<t.length;e++){let n=t[e],i=e*16;if(n.leftChild){let e=n.leftChild,t=n.rightChild;r[i]=e.minX,r[i+1]=e.minY,r[i+2]=e.minZ,r[i+3]=e._flatIndex,r[i+4]=e.maxX,r[i+5]=e.maxY,r[i+6]=e.maxZ,r[i+7]=t._flatIndex,r[i+8]=t.minX,r[i+9]=t.minY,r[i+10]=t.minZ,r[i+12]=t.maxX,r[i+13]=t.maxY,r[i+14]=t.maxZ}else r[i]=n.triangleOffset,r[i+1]=n.triangleCount,r[i+3]=-1}return r}flattenBVHWithFrontier(e){let t=[],n=[e];for(;n.length>0;){let e=n.pop();e._flatIndex=t.length,t.push(e),e.rightChild&&n.push(e.rightChild),e.leftChild&&n.push(e.leftChild)}let r=new Float32Array(t.length*16),i=[];for(let e=0;e<t.length;e++){let n=t[e],a=e*16;if(n.leftChild){let e=n.leftChild,t=n.rightChild;r[a]=e.minX,r[a+1]=e.minY,r[a+2]=e.minZ,r[a+3]=e._flatIndex,r[a+4]=e.maxX,r[a+5]=e.maxY,r[a+6]=e.maxZ,r[a+7]=t._flatIndex,r[a+8]=t.minX,r[a+9]=t.minY,r[a+10]=t.minZ,r[a+12]=t.maxX,r[a+13]=t.maxY,r[a+14]=t.maxZ}else if(n.isFrontier){let t=n.frontierTaskId;r[a]=n.triangleOffset,r[a+1]=n.triangleCount,r[a+2]=t,r[a+3]=-2,i.push({taskId:t,flatIndex:e})}else r[a]=n.triangleOffset,r[a+1]=n.triangleCount,r[a+3]=-1}return{flatData:r,frontierMap:i,nodeCount:t.length}}assembleParallelBVH(e,t,n,r){let i=[...r].sort((e,t)=>e.taskId-t.taskId),a=t;for(let e=0;e<i.length;e++)a+=i[e].nodeCount;let o=new Float32Array(a*16);o.set(e);let s=new Map;for(let e of n)s.set(e.taskId,e.flatIndex);let c=t;for(let e=0;e<i.length;e++){let t=i[e],n=t.flatData,r=t.nodeCount,a=c*16;o.set(n,a);for(let e=0;e<r;e++){let t=a+e*16;o[t+3]!==-1&&(o[t+3]+=c,o[t+7]+=c)}let l=s.get(t.taskId);if(l!==void 0){let e=l*16,t=a;for(let n=0;n<16;n++)o[e+n]=o[t+n]}c+=r}return o}computeSurfaceAreaFlat(e,t,n,r,i,a){let o=r-e,s=i-t,c=a-n;return 2*(o*s+s*c+c*o)}},Q={FLOATS_PER_TRIANGLE:32,POSITION_A_OFFSET:0,POSITION_B_OFFSET:4,POSITION_C_OFFSET:8,NORMAL_A_OFFSET:12,NORMAL_B_OFFSET:16,NORMAL_C_OFFSET:20},ni=Q.FLOATS_PER_TRIANGLE,ri=16,ii=-1,ai=-2,oi=class{constructor(){this._bounds=null,this._boundsNodeCount=0}updateTrianglePositions(e,t,n){let r=n.length;for(let i=0;i<r;i++){let r=n[i],a=i*ni,o=r*9,s=t[o],c=t[o+1],l=t[o+2],u=t[o+3],d=t[o+4],f=t[o+5],p=t[o+6],m=t[o+7],h=t[o+8];e[a+Q.POSITION_A_OFFSET]=s,e[a+Q.POSITION_A_OFFSET+1]=c,e[a+Q.POSITION_A_OFFSET+2]=l,e[a+Q.POSITION_B_OFFSET]=u,e[a+Q.POSITION_B_OFFSET+1]=d,e[a+Q.POSITION_B_OFFSET+2]=f,e[a+Q.POSITION_C_OFFSET]=p,e[a+Q.POSITION_C_OFFSET+1]=m,e[a+Q.POSITION_C_OFFSET+2]=h;let g=u-s,_=d-c,v=f-l,y=p-s,b=m-c,x=h-l,S=_*x-v*b,C=v*y-g*x,w=g*b-_*y;e[a+Q.NORMAL_A_OFFSET]=S,e[a+Q.NORMAL_A_OFFSET+1]=C,e[a+Q.NORMAL_A_OFFSET+2]=w,e[a+Q.NORMAL_B_OFFSET]=S,e[a+Q.NORMAL_B_OFFSET+1]=C,e[a+Q.NORMAL_B_OFFSET+2]=w,e[a+Q.NORMAL_C_OFFSET]=S,e[a+Q.NORMAL_C_OFFSET+1]=C,e[a+Q.NORMAL_C_OFFSET+2]=w}}refitRange(e,t,n,r){r>this._boundsNodeCount&&(this._bounds=new Float32Array(r*6),this._boundsNodeCount=r);let i=this._bounds,a=n+r;for(let r=a-1;r>=n;r--){let a=r*ri,o=(r-n)*6;if(e[a+3]===ii){let n=e[a],r=e[a+1],s=1/0,c=1/0,l=1/0,u=-1/0,d=-1/0,f=-1/0;for(let e=0;e<r;e++){let r=(n+e)*ni,i=t[r+Q.POSITION_A_OFFSET],a=t[r+Q.POSITION_A_OFFSET+1],o=t[r+Q.POSITION_A_OFFSET+2],p=t[r+Q.POSITION_B_OFFSET],m=t[r+Q.POSITION_B_OFFSET+1],h=t[r+Q.POSITION_B_OFFSET+2],g=t[r+Q.POSITION_C_OFFSET],_=t[r+Q.POSITION_C_OFFSET+1],v=t[r+Q.POSITION_C_OFFSET+2];s=Math.min(s,i,p,g),c=Math.min(c,a,m,_),l=Math.min(l,o,h,v),u=Math.max(u,i,p,g),d=Math.max(d,a,m,_),f=Math.max(f,o,h,v)}i[o]=s,i[o+1]=c,i[o+2]=l,i[o+3]=u,i[o+4]=d,i[o+5]=f}else{let t=e[a+3],r=e[a+7],s=(t-n)*6,c=(r-n)*6,l=i[s],u=i[s+1],d=i[s+2],f=i[s+3],p=i[s+4],m=i[s+5],h=i[c],g=i[c+1],_=i[c+2],v=i[c+3],y=i[c+4],b=i[c+5];e[a]=l,e[a+1]=u,e[a+2]=d,e[a+4]=f,e[a+5]=p,e[a+6]=m,e[a+8]=h,e[a+9]=g,e[a+10]=_,e[a+12]=v,e[a+13]=y,e[a+14]=b,i[o]=Math.min(l,h),i[o+1]=Math.min(u,g),i[o+2]=Math.min(d,_),i[o+3]=Math.max(f,v),i[o+4]=Math.max(p,y),i[o+5]=Math.max(m,b)}}}refit(e,t,n){n!==this._boundsNodeCount&&(this._bounds=new Float32Array(n*6),this._boundsNodeCount=n);let r=this._bounds;for(let i=n-1;i>=0;i--){let n=i*ri,a=i*6,o=e[n+3];if(o===ii){let i=e[n],o=e[n+1],s=1/0,c=1/0,l=1/0,u=-1/0,d=-1/0,f=-1/0;for(let e=0;e<o;e++){let n=(i+e)*ni,r=t[n+Q.POSITION_A_OFFSET],a=t[n+Q.POSITION_A_OFFSET+1],o=t[n+Q.POSITION_A_OFFSET+2],p=t[n+Q.POSITION_B_OFFSET],m=t[n+Q.POSITION_B_OFFSET+1],h=t[n+Q.POSITION_B_OFFSET+2],g=t[n+Q.POSITION_C_OFFSET],_=t[n+Q.POSITION_C_OFFSET+1],v=t[n+Q.POSITION_C_OFFSET+2];s=Math.min(s,r,p,g),c=Math.min(c,a,m,_),l=Math.min(l,o,h,v),u=Math.max(u,r,p,g),d=Math.max(d,a,m,_),f=Math.max(f,o,h,v)}r[a]=s,r[a+1]=c,r[a+2]=l,r[a+3]=u,r[a+4]=d,r[a+5]=f}else if(o===ai){let t=e[n]*6;r[a]=r[t],r[a+1]=r[t+1],r[a+2]=r[t+2],r[a+3]=r[t+3],r[a+4]=r[t+4],r[a+5]=r[t+5]}else{let t=e[n+3],i=e[n+7],o=t*6,s=i*6,c=r[o],l=r[o+1],u=r[o+2],d=r[o+3],f=r[o+4],p=r[o+5],m=r[s],h=r[s+1],g=r[s+2],_=r[s+3],v=r[s+4],y=r[s+5];e[n]=c,e[n+1]=l,e[n+2]=u,e[n+4]=d,e[n+5]=f,e[n+6]=p,e[n+8]=m,e[n+9]=h,e[n+10]=g,e[n+12]=_,e[n+13]=v,e[n+14]=y,r[a]=Math.min(c,m),r[a+1]=Math.min(l,h),r[a+2]=Math.min(u,g),r[a+3]=Math.max(d,_),r[a+4]=Math.max(f,v),r[a+5]=Math.max(p,y)}}}},si=``+(typeof document>`u`&&typeof location>`u`?require(`url`).pathToFileURL(__dirname+`/assets/BVHSubtreeWorker-D9GImjGj.js`).href:new URL(`assets/BVHSubtreeWorker-D9GImjGj.js`,typeof document>`u`?location.href:document.currentScript&&document.currentScript.tagName.toUpperCase()===`SCRIPT`&&document.currentScript.src||document.baseURI).href),ci=32,li=5e4,ui=8;function di(e,t,n,r){let i=e.byteLength/(ci*4),a=Math.min(navigator.hardwareConcurrency||4,ui),o=Math.ceil(Math.log2(a*2.5+1));return console.log(`[ParallelBVH] Parallel build: ${i.toLocaleString()} triangles, ${a} workers, parallelDepth=${o}`),new Promise((s,c)=>{(async()=>{let c=new SharedArrayBuffer(e.byteLength);new Float32Array(c).set(e);let l=new SharedArrayBuffer(i*3*4),u=new SharedArrayBuffer(i*3*4),d=new SharedArrayBuffer(i*3*4),f=new SharedArrayBuffer(i*4),p=new SharedArrayBuffer(i*4),m=new SharedArrayBuffer(i*ci*4),h;try{h=new Worker(Zr,{type:`module`})}catch(e){if(e.name!==`SecurityError`)throw e;h=await Je(Zr)}let g=null,_=[h],v={id:null},y=!1,b=()=>{v.id&&=(clearTimeout(v.id),null);for(let e of _)try{e.terminate()}catch{}},x=e=>{if(y)return;y=!0,console.warn(`[ParallelBVH] Parallel build failed (${e}), falling back to single worker`),b();let i=new ArrayBuffer(c.byteLength);new Float32Array(i).set(new Float32Array(c)),s(pi(new Float32Array(i),t,n,r))};h.onerror=e=>{x(`coordinator error: ${e.message}`)},h.onmessage=e=>{let t=e.data;if(t.error||t.type===`error`){x(t.error);return}if(t.type===`progress`&&n){n(Math.floor(t.progress*.3));return}if(t.type===`phase1Result`){g=t.splitStats,fi(t,a,c,l,u,d,f,m,i,n,h,_,b,x,s,v,r).catch(e=>x(e.message));return}if(t.type===`assembleResult`){y=!0,b();let e=new Float32Array(m);s({bvhData:t.bvhData,bvhRoot:!0,reorderedTriangles:e,originalToBvh:t.originalToBvh||null,splitStats:g||{}});return}},n&&n(0),h.postMessage({type:`buildPhase1`,sharedTriangleData:c,sharedCentroids:l,sharedBMin:u,sharedBMax:d,sharedIndices:f,sharedMortonCodes:p,triangleCount:i,depth:t,parallelDepth:o,reportProgress:!!n,treeletOptimization:r.treeletOptimization})})().catch(e=>{console.warn(`[ParallelBVH] Parallel build setup failed:`,e),c(e)})})}async function fi(e,t,n,r,i,a,o,s,c,l,u,d,f,p,m,h,g){let{topFlatData:_,topNodeCount:v,frontierTasks:y,frontierMap:b}=e;if(!y||y.length===0){console.log(`[ParallelBVH] No frontier tasks, assembling with top-level tree only`),u.postMessage({type:`assemble`,topFlatData:_,topNodeCount:v,frontierMap:[],subtreeResults:[],sharedTriangleData:n,sharedIndices:o,sharedReorderBuffer:s,triangleCount:c},[_.buffer]);return}console.log(`[ParallelBVH] Phase 2: distributing ${y.length} tasks across ${t} workers`);let x=[...y].sort((e,t)=>t.end-t.start-(e.end-e.start)),S=Array.from({length:Math.min(t,x.length)},()=>[]),C=Array(S.length).fill(0);for(let e of x){let t=0;for(let e=1;e<C.length;e++)C[e]<C[t]&&(t=e);S[t].push(e),C[t]+=e.end-e.start}let w=[],T=0,E=y.length,D=y.reduce((e,t)=>e+(t.end-t.start),0),O=0;h.id=setTimeout(()=>{p(`Phase 2 timeout (30s)`)},3e4);let k=()=>{clearTimeout(h.id),h.id=null,l&&l(85);let t=[];e.topFlatData&&e.topFlatData.buffer&&t.push(e.topFlatData.buffer);for(let e of w)e.flatData&&e.flatData.buffer&&t.push(e.flatData.buffer);u.postMessage({type:`assemble`,topFlatData:e.topFlatData,topNodeCount:v,frontierMap:b,subtreeResults:w,sharedTriangleData:n,sharedIndices:o,sharedReorderBuffer:s,triangleCount:c},t)},A=S.length;for(let e=0;e<A;e++){let t=S[e];if(t.length===0)continue;let s;try{s=new Worker(si,{type:`module`})}catch(e){if(e.name!==`SecurityError`)throw e;s=await Je(si)}d.push(s),s.onerror=e=>{p(`subtree worker error: ${e.message}`)},s.onmessage=e=>{let t=e.data;if(t.type===`error`){p(`subtree task ${t.taskId} error: ${t.error}`);return}if(t.type===`progress`&&l){let e=30+Math.floor(O/D*55);l(Math.min(e,85));return}if(t.type===`subtreeResult`){w.push({taskId:t.taskId,flatData:t.flatData,nodeCount:t.nodeCount});let e=y.find(e=>e.taskId===t.taskId);if(e&&(O+=e.end-e.start),T++,l){let e=30+Math.floor(O/D*55);l(Math.min(e,85))}if(T===E){for(let e of d)if(e!==u)try{e.terminate()}catch{}k()}}},s.postMessage({tasks:t,sharedTriangleData:n,sharedCentroids:r,sharedBMin:i,sharedBMax:a,sharedIndices:o,triangleCount:c,maxLeafSize:g.maxLeafSize,numBins:g.numBins,maxBins:g.maxBins,minBins:g.minBins,treeletConfig:g.treeletOptimization,reinsertionConfig:g.reinsertionOptimization,reportProgress:!!l})}}function pi(e,t,n,r){return new Promise((i,a)=>{(async()=>{let o;try{o=new Worker(Zr,{type:`module`})}catch(e){if(e.name!==`SecurityError`)throw e;o=await Je(Zr)}let s=e.byteLength/(ci*4),c=typeof SharedArrayBuffer<`u`?new SharedArrayBuffer(s*ci*4):null;o.onmessage=e=>{let{bvhData:t,triangles:r,originalToBvh:s,error:l,progress:u,treeletStats:d}=e.data;if(l){o.terminate(),a(Error(l));return}if(u!==void 0&&n){n(u);return}o.terminate(),i({bvhData:t,bvhRoot:!0,reorderedTriangles:c?new Float32Array(c):r,originalToBvh:s||null,splitStats:d||{}})},o.onerror=e=>{o.terminate(),a(e)};let l=e.buffer;o.postMessage({triangleData:l,triangleByteOffset:e.byteOffset,triangleByteLength:e.byteLength,triangleCount:s,depth:t,reportProgress:!!n,sharedReorderBuffer:c,treeletOptimization:r.treeletOptimization,reinsertionOptimization:r.reinsertionOptimization},[l])})().catch(e=>{console.warn(`[ParallelBVH] Single worker fallback failed:`,e),a(e)})})}function mi(e){return typeof Worker<`u`&&typeof SharedArrayBuffer<`u`&&e>=li}var hi=16,gi=class{constructor(){this.minX=0,this.minY=0,this.minZ=0,this.maxX=0,this.maxY=0,this.maxZ=0,this.leftChild=null,this.rightChild=null,this.entryIndex=-1}},_i=class{constructor(){this._flatBuffer=null,this._flatBufferCapacity=0}build(e){if(e.length===0)return{root:null,nodeCount:0};let t=[];for(let n=0;n<e.length;n++)t.push(n);let n=this._buildRecursive(e,t);return{root:n,nodeCount:this._countNodes(n)}}_buildRecursive(e,t){let n=new gi;if(t.length===1){let r=e[t[0]].worldAABB;return n.minX=r.minX,n.minY=r.minY,n.minZ=r.minZ,n.maxX=r.maxX,n.maxY=r.maxY,n.maxZ=r.maxZ,n.entryIndex=t[0],n}let r=1/0,i=1/0,a=1/0,o=-1/0,s=-1/0,c=-1/0;for(let n of t){let t=e[n].worldAABB;t.minX<r&&(r=t.minX),t.minY<i&&(i=t.minY),t.minZ<a&&(a=t.minZ),t.maxX>o&&(o=t.maxX),t.maxY>s&&(s=t.maxY),t.maxZ>c&&(c=t.maxZ)}if(n.minX=r,n.minY=i,n.minZ=a,n.maxX=o,n.maxY=s,n.maxZ=c,t.length===2)return n.leftChild=this._buildRecursive(e,[t[0]]),n.rightChild=this._buildRecursive(e,[t[1]]),n;let l=this._surfaceArea(r,i,a,o,s,c),u=1/0,d=0,f=0;if(l>0&&isFinite(l))for(let n=0;n<3;n++){let r=t.slice().sort((t,r)=>{let i=e[t].worldAABB,a=e[r].worldAABB;return this._centroid(i,n)-this._centroid(a,n)});for(let t=1;t<r.length;t++){let i=this._computeGroupAABB(e,r,0,t),a=this._computeGroupAABB(e,r,t,r.length),o=this._surfaceArea(i.minX,i.minY,i.minZ,i.maxX,i.maxY,i.maxZ),s=this._surfaceArea(a.minX,a.minY,a.minZ,a.maxX,a.maxY,a.maxZ),c=1+(o*t+s*(r.length-t))/l;c<u&&(u=c,d=n,f=t)}}if(f<=0||f>=t.length){d=0;let e=o-r,n=s-i,l=c-a;n>e&&n>l?d=1:l>e&&(d=2),f=t.length>>1}let p=t.slice().sort((t,n)=>this._centroid(e[t].worldAABB,d)-this._centroid(e[n].worldAABB,d)),m=p.slice(0,f),h=p.slice(f);return n.leftChild=this._buildRecursive(e,m),n.rightChild=this._buildRecursive(e,h),n}flatten(e,t){if(!e)return new Float32Array;let n=[],r=[e];for(;r.length>0;){let e=r.pop();e._flatIndex=n.length,n.push(e),e.rightChild&&r.push(e.rightChild),e.leftChild&&r.push(e.leftChild)}let i=n.length*hi;i>this._flatBufferCapacity&&(this._flatBuffer=new Float32Array(i),this._flatBufferCapacity=i);let a=this._flatBuffer;a.fill(0,0,i);for(let e=0;e<n.length;e++){let r=n[e],i=e*hi;if(r.leftChild){let e=r.leftChild,t=r.rightChild;a[i]=e.minX,a[i+1]=e.minY,a[i+2]=e.minZ,a[i+3]=e._flatIndex,a[i+4]=e.maxX,a[i+5]=e.maxY,a[i+6]=e.maxZ,a[i+7]=t._flatIndex,a[i+8]=t.minX,a[i+9]=t.minY,a[i+10]=t.minZ,a[i+12]=t.maxX,a[i+13]=t.maxY,a[i+14]=t.maxZ}else{let n=t[r.entryIndex];a[i]=n.blasOffset,a[i+1]=r.entryIndex,a[i+2]=n.visible===!1?0:1,a[i+3]=Ne.BLAS_POINTER_LEAF,n.tlasLeafIndex=e}}return a.subarray(0,i)}_centroid(e,t){return t===0?(e.minX+e.maxX)*.5:t===1?(e.minY+e.maxY)*.5:(e.minZ+e.maxZ)*.5}_surfaceArea(e,t,n,r,i,a){let o=r-e,s=i-t,c=a-n;return 2*(o*s+s*c+c*o)}_computeGroupAABB(e,t,n,r){let i=1/0,a=1/0,o=1/0,s=-1/0,c=-1/0,l=-1/0;for(let u=n;u<r;u++){let n=e[t[u]].worldAABB;n.minX<i&&(i=n.minX),n.minY<a&&(a=n.minY),n.minZ<o&&(o=n.minZ),n.maxX>s&&(s=n.maxX),n.maxY>c&&(c=n.maxY),n.maxZ>l&&(l=n.maxZ)}return{minX:i,minY:a,minZ:o,maxX:s,maxY:c,maxZ:l}}_countNodes(e){if(!e)return 0;let t=0,n=[e];for(;n.length>0;){let e=n.pop();t++,e.leftChild&&n.push(e.leftChild),e.rightChild&&n.push(e.rightChild)}return t}},vi=class{constructor(){this.entries=[],this.totalBLASNodes=0,this.tlasNodeCount=0}allocate(e){this.entries=Array(e).fill(null)}setEntry({meshIndex:e,blasNodeCount:t,triOffset:n,triCount:r,originalToBvhMap:i,bvhData:a}){this.entries[e]={meshIndex:e,blasOffset:0,blasNodeCount:t,triOffset:n,triCount:r,worldAABB:null,originalToBvhMap:i,bvhData:a,visible:!0,tlasLeafIndex:-1}}setVisibility(e,t){let n=this.entries[e];n&&(n.visible=t)}computeAABBs(e){for(let t of this.entries)t&&(t.worldAABB=this._readRootAABB(t.bvhData,t,e))}recomputeAABB(e,t,n){let r=this.entries[e],i=t.subarray(r.blasOffset*16,r.blasOffset*16+16);r.worldAABB=this._readRootAABB(i,r,n)}_readRootAABB(e,t,n){if(e[3]===-1)return this._computeAABBFromTriangles(t,n);let r=e[0],i=e[1],a=e[2],o=e[4],s=e[5],c=e[6],l=e[8],u=e[9],d=e[10],f=e[12],p=e[13],m=e[14];return{minX:Math.min(r,l),minY:Math.min(i,u),minZ:Math.min(a,d),maxX:Math.max(o,f),maxY:Math.max(s,p),maxZ:Math.max(c,m)}}_computeAABBFromTriangles(e,t){let n=G.FLOATS_PER_TRIANGLE,r=1/0,i=1/0,a=1/0,o=-1/0,s=-1/0,c=-1/0;for(let l=0;l<e.triCount;l++){let u=(e.triOffset+l)*n;for(let e=0;e<=8;e+=4){let n=t[u+e],l=t[u+e+1],d=t[u+e+2];n<r&&(r=n),l<i&&(i=l),d<a&&(a=d),n>o&&(o=n),l>s&&(s=l),d>c&&(c=d)}}return{minX:r,minY:i,minZ:a,maxX:o,maxY:s,maxZ:c}}assignOffsets(e){this.tlasNodeCount=e;let t=e;for(let e of this.entries)e&&(e.blasOffset=t,t+=e.blasNodeCount);this.totalBLASNodes=t-e}get totalNodeCount(){return this.tlasNodeCount+this.totalBLASNodes}get count(){return this.entries.length}clear(){this.entries=[],this.totalBLASNodes=0,this.tlasNodeCount=0}},yi=``+(typeof document>`u`&&typeof location>`u`?require(`url`).pathToFileURL(__dirname+`/assets/TexturesWorker-6mlBy4Dm.js`).href:new URL(`assets/TexturesWorker-6mlBy4Dm.js`,typeof document>`u`?location.href:document.currentScript&&document.currentScript.tagName.toUpperCase()===`SCRIPT`&&document.currentScript.src||document.baseURI).href),bi=class{constructor(){this.canvasContextPairs=[],this.maxPoolSize=Pe.CANVAS_POOL_SIZE}getCanvasWithContext(e,t,n=!1,r={}){let i={willReadFrequently:!0,alpha:!0,desynchronized:!0,...r},a=this.canvasContextPairs.pop();if(!a){let r;r=n&&typeof OffscreenCanvas<`u`?new OffscreenCanvas(e,t):document.createElement(`canvas`);let o=r.getContext(`2d`,i);a={canvas:r,context:o}}return a.canvas.width=e,a.canvas.height=t,a}releaseCanvasWithContext(e){this.canvasContextPairs.length<this.maxPoolSize&&(e.context.globalAlpha=1,e.context.globalCompositeOperation=`source-over`,e.context.imageSmoothingEnabled=!0,e.context.clearRect(0,0,e.canvas.width,e.canvas.height),e.canvas.width=1,e.canvas.height=1,this.canvasContextPairs.push(e))}getCanvas(e,t,n=!1){return this.getCanvasWithContext(e,t,n).canvas}getContext(e,t={}){let n=this.canvasContextPairs.find(t=>t.canvas===e);return n?n.context:e.getContext(`2d`,{willReadFrequently:!0,alpha:!0,desynchronized:!0,...t})}dispose(){this.canvasContextPairs=[]}},xi=class{constructor(e={}){this.pools=new Map,this.memoryUsage=0,this.maxMemoryUsage=e.maxMemory||Re.MAX_BUFFER_MEMORY,this.allocatedBuffers=new WeakMap,this.sizeStrategy=e.sizeStrategy||`adaptive`}getOptimalSize(e){switch(this.sizeStrategy){case`exact`:return e;case`power2`:return 2**Math.ceil(Math.log2(e));default:return e<1024?e:e<1024*1024?Math.ceil(e/1024)*1024:2**Math.ceil(Math.log2(e))}}getBuffer(e,t=Float32Array){let n=this.getOptimalSize(e),r=`${t.name}-${n}`,i=(this.pools.get(r)||[]).pop();if(!i)try{i=new t(n),this.memoryUsage+=i.byteLength,this.allocatedBuffers.set(i,!0)}catch{this.cleanup();try{i=new t(n),this.memoryUsage+=i.byteLength,this.allocatedBuffers.set(i,!0)}catch(e){let r=n*t.BYTES_PER_ELEMENT/(1024*1024),i=this.memoryUsage/(1024*1024);throw Error(`Buffer allocation failed: requested ${r.toFixed(1)}MB, current usage: ${i.toFixed(1)}MB, max: ${(this.maxMemoryUsage/(1024*1024)).toFixed(1)}MB. Original error: ${e.message}`)}}return this.memoryUsage>this.maxMemoryUsage*Re.CLEANUP_THRESHOLD&&this.cleanup(),this.checkMemoryHealth(),i.byteOffset+e*t.BYTES_PER_ELEMENT>i.buffer.byteLength&&(i=new t(e),this.memoryUsage+=i.byteLength),new t(i.buffer,i.byteOffset,e)}releaseBuffer(e,t=Float32Array){let n=(e.buffer.byteLength-e.byteOffset)/t.BYTES_PER_ELEMENT,r=this.getOptimalSize(n),i=`${t.name}-${r}`,a=this.pools.get(i)||[];a.length<Pe.BUFFER_POOL_SIZE?(a.push(new t(e.buffer,e.byteOffset,n)),this.pools.set(i,a)):this.allocatedBuffers.has(e)&&(this.memoryUsage-=e.byteLength,this.allocatedBuffers.delete(e))}cleanup(){let e=Array.from(this.pools.entries());e.slice(0,Math.floor(e.length/2)).forEach(([e,t])=>{t.forEach(e=>{this.allocatedBuffers.has(e)&&(this.memoryUsage-=e.byteLength,this.allocatedBuffers.delete(e))}),this.pools.delete(e)})}dispose(){this.pools.forEach(e=>{e.forEach(e=>{this.allocatedBuffers.has(e)&&this.allocatedBuffers.delete(e)})}),this.pools.clear(),this.memoryUsage=0}getMemoryStats(){return{currentUsage:this.memoryUsage,maxUsage:this.maxMemoryUsage,utilizationPercentage:this.memoryUsage/this.maxMemoryUsage*100,poolCount:this.pools.size,allocatedBufferCount:this.allocatedBuffers&&this.allocatedBuffers.size||0}}checkMemoryHealth(){let e=this.getMemoryStats();return e.utilizationPercentage>90?(console.warn(`Memory pool critical: ${e.utilizationPercentage.toFixed(1)}% used (${(e.currentUsage/(1024*1024)).toFixed(1)}MB / ${(e.maxUsage/(1024*1024)).toFixed(1)}MB)`),`critical`):e.utilizationPercentage>70?(console.warn(`Memory pool high: ${e.utilizationPercentage.toFixed(1)}% used (${(e.currentUsage/(1024*1024)).toFixed(1)}MB / ${(e.maxUsage/(1024*1024)).toFixed(1)}MB)`),`high`):`normal`}},Si=class{constructor(e=Pe.CACHE_SIZE_LIMIT){this.cache=new Map,this.accessOrder=[],this.maxSize=e}generateHash(e){let t=``;for(let n of e)if(n?.image){let e=n.image.width||0,r=n.image.height||0,i=n.image.src||n.uuid||``,a=n.flipY===!1?`n`:`f`;t+=`${e}x${r}_${i.slice(-8)}_${a}_`}return t+e.length}get(e){if(this.cache.has(e)){let t=this.accessOrder.indexOf(e);return t>-1&&this.accessOrder.splice(t,1),this.accessOrder.push(e),this.cache.get(e)}return null}set(e,t){if(this.cache.has(e)){let t=this.accessOrder.indexOf(e);t>-1&&this.accessOrder.splice(t,1)}else this.cache.size>=this.maxSize&&this.evictLRU();this.cache.set(e,t),this.accessOrder.push(e)}evictLRU(){if(this.accessOrder.length>0){let e=this.accessOrder.shift(),t=this.cache.get(e);t&&t.dispose&&t.dispose(),this.cache.delete(e)}}dispose(){this.cache.forEach(e=>{e&&e.dispose&&e.dispose()}),this.cache.clear(),this.accessOrder=[]}},Ci=class{constructor(e={}){this.useWorkers=typeof Worker<`u`,this.maxConcurrentWorkers=Pe.MAX_CONCURRENT_WORKERS,this.activeWorkers=0,this.maxTextureSize=this._clampTextureSize(e.maxTextureSize??Pe.DEFAULT_MAX_TEXTURE_SIZE),this.canvasPool=new bi,this.bufferPool=new xi({maxMemory:e.maxBufferMemory||Re.MAX_BUFFER_MEMORY,sizeStrategy:e.bufferSizeStrategy||`adaptive`}),this.textureCache=new Si,this.capabilities=this.detectCapabilities(),this.optimalMethod=this.selectOptimalMethod()}_clampTextureSize(e){let t=Math.floor(Number(e)||Pe.DEFAULT_MAX_TEXTURE_SIZE);return Math.max(Pe.MIN_TEXTURE_WIDTH,Math.min(t,Pe.MAX_TEXTURE_SIZE))}setMaxTextureSize(e){let t=this._clampTextureSize(e);return t===this.maxTextureSize?t:(this.maxTextureSize=t,this.textureCache?.dispose(),this.textureCache=new Si,this.maxTextureSize)}detectCapabilities(){return{offscreenCanvas:typeof OffscreenCanvas<`u`,imageBitmap:typeof createImageBitmap<`u`,workers:typeof Worker<`u`,hardwareConcurrency:navigator.hardwareConcurrency||4}}selectOptimalMethod(){return this.capabilities.workers&&this.capabilities.offscreenCanvas?`worker-offscreen`:this.capabilities.imageBitmap?`imageBitmap`:`canvas`}async createTexturesToDataTexture(e){if(!e||e.length===0)return null;let t=this.textureCache.generateHash(e),n=this.textureCache.get(t);if(n)return n;let{normalized:r,bitmapsToClose:i}=await this._normalizeTexturesForProcessing(e),a=this.selectProcessingStrategy(r),o;try{switch(a.method){case`worker-direct`:o=await this.processWithWorkerDirect(r);break;case`worker-chunked`:o=await this.processWithWorkerChunked(r,a.chunkSize);break;case`main-batch`:o=await this.processOnMainThreadBatch(r,a.batchSize);break;case`main-streaming`:o=await this.processOnMainThreadStreaming(r);break;default:o=await this.processOnMainThreadSync(r)}return o&&this.textureCache.set(t,o),o}catch(e){return console.warn(`Texture processing failed, trying fallback:`,e),await this.processOnMainThreadSync(r)}finally{for(let e of i)e.close()}}selectProcessingStrategy(e){let t=e.reduce((e,t)=>e+(t.image?.width||0)*(t.image?.height||0),0),n=t*4;return this.capabilities.workers&&n>Re.MAX_TEXTURE_MEMORY?{method:`worker-chunked`,chunkSize:Math.max(1,Math.floor(e.length/4))}:this.capabilities.workers&&t>2097152?{method:`worker-direct`}:t>524288?{method:`main-batch`,batchSize:Math.min(4,e.length)}:e.length>8?{method:`main-streaming`}:{method:`main-sync`}}async processWithWorkerDirect(e){for(;this.activeWorkers>=this.maxConcurrentWorkers;)await new Promise(e=>setTimeout(e,10));this.activeWorkers++;try{let t;try{t=new Worker(yi,{type:`module`})}catch(e){if(e.name!==`SecurityError`)throw e;t=await Je(yi)}let n=await this.prepareTexturesForWorkerDirect(e),r=await new Promise((e,r)=>{t.onmessage=t=>{t.data.error?r(Error(t.data.error)):e(t.data)},t.onerror=r;let i=[];n.forEach(e=>{e.data instanceof ArrayBuffer?i.push(e.data):e.bitmap&&i.push(e.bitmap)}),t.postMessage({textures:n,maxTextureSize:this.maxTextureSize,method:`direct-transfer`},i)});return t.terminate(),this.createDataArrayTextureFromResult(r)}finally{this.activeWorkers--}}async prepareTexturesForWorkerDirect(e){let t=[];for(let n of e){if(!n?.image)continue;let e=n.flipY!==!1;try{if(typeof createImageBitmap<`u`&&n.image instanceof HTMLImageElement){let r=await createImageBitmap(n.image,{imageOrientation:e?`flipY`:`none`});t.push({bitmap:r,width:n.image.width,height:n.image.height,isDirect:!0})}else{let r=n.image.width,i=n.image.height,a=await createImageBitmap(n.image,{imageOrientation:e?`flipY`:`none`}),o=this.canvasPool.getCanvasWithContext(r,i);o.context.drawImage(a,0,0),a.close();let s=o.context.getImageData(0,0,r,i);t.push({data:s.data.buffer,width:r,height:i,isImageData:!0}),this.canvasPool.releaseCanvasWithContext(o)}}catch(e){console.warn(`Failed to prepare texture for worker:`,e)}}return t}async processWithWorkerChunked(e,t){let n=[];for(let r=0;r<e.length;r+=t){let i=e.slice(r,r+t),a=await this.processWithWorkerDirect(i);n.push(a)}return this.combineTextureResults(n)}async processOnMainThreadBatch(e,t){let n=e.filter(e=>e?.image);if(n.length===0)return this.createFallbackTexture();let{maxWidth:r,maxHeight:i}=this.calculateOptimalDimensions(n),a=n.length,o=this.bufferPool.getBuffer(r*i*a*4,Uint8Array);for(let e=0;e<n.length;e+=t){let a=Math.min(e+t,n.length),s=[];for(let t=e;t<a;t++){let e=n[t],a=e.flipY!==!1,o=createImageBitmap(e.image,{resizeWidth:r,resizeHeight:i,resizeQuality:`high`,imageOrientation:a?`flipY`:`none`});s.push(o.then(e=>({bitmap:e,index:t})))}let c=await Promise.all(s),l=this.canvasPool.getCanvasWithContext(r,i);l.context.imageSmoothingEnabled=!1;for(let{bitmap:e,index:t}of c){l.context.clearRect(0,0,r,i),l.context.drawImage(e,0,0);let n=l.context.getImageData(0,0,r,i),a=r*i*4*t;o.set(n.data,a),e.close()}this.canvasPool.releaseCanvasWithContext(l)}return this.createDataArrayTextureFromBuffer(o,r,i,a)}async processOnMainThreadStreaming(e){let t=e.filter(e=>e?.image);if(t.length===0)return this.createFallbackTexture();let{maxWidth:n,maxHeight:r}=this.calculateOptimalDimensions(t),i=t.length,a=this.bufferPool.getBuffer(n*r*i*4,Uint8Array),o=this.canvasPool.getCanvasWithContext(n,r);o.context.imageSmoothingEnabled=!0,o.context.imageSmoothingQuality=`high`;for(let e=0;e<t.length;e++){let i=t[e],s=await createImageBitmap(i.image,{resizeWidth:n,resizeHeight:r,resizeQuality:`high`,imageOrientation:i.flipY===!1?`none`:`flipY`});o.context.clearRect(0,0,n,r),o.context.drawImage(s,0,0),s.close();let c=o.context.getImageData(0,0,n,r),l=n*r*4*e;a.set(c.data,l),e%Re.STREAM_BATCH_SIZE===0&&await new Promise(e=>setTimeout(e,0))}return this.canvasPool.releaseCanvasWithContext(o),this.createDataArrayTextureFromBuffer(a,n,r,i)}async processOnMainThreadSync(e){let t=e.filter(e=>e?.image);if(t.length===0)return this.createFallbackTexture();let{maxWidth:n,maxHeight:r}=this.calculateOptimalDimensions(t),i=t.length,a=this.bufferPool.getBuffer(n*r*i*4,Uint8Array),o=this.canvasPool.getCanvasWithContext(n,r);o.context.imageSmoothingEnabled=!0,o.context.imageSmoothingQuality=`high`;for(let e=0;e<t.length;e++){let i=t[e],s=await createImageBitmap(i.image,{resizeWidth:n,resizeHeight:r,resizeQuality:`high`,imageOrientation:i.flipY===!1?`none`:`flipY`});o.context.clearRect(0,0,n,r),o.context.drawImage(s,0,0),s.close();let c=o.context.getImageData(0,0,n,r),l=n*r*4*e;a.set(c.data,l)}return this.canvasPool.releaseCanvasWithContext(o),this.createDataArrayTextureFromBuffer(a,n,r,i)}createMaterialRawData(e){let t=K.FLOATS_PER_MATERIAL,n=e.length,r=n*t,i=new Float32Array(r);for(let r=0;r<n;r++){let n=e[r],a=r*t,o=n.mapMatrix??Fe,s=n.normalMapMatrices??Fe,c=n.roughnessMapMatrices??Fe,l=n.metalnessMapMatrices??Fe,u=n.emissiveMapMatrices??Fe,d=n.bumpMapMatrices??Fe,f=n.displacementMapMatrices??Fe,p=[n.ior,n.transmission,n.thickness,n.emissiveIntensity,n.attenuationColor.r,n.attenuationColor.g,n.attenuationColor.b,n.attenuationDistance,n.opacity,n.side,n.transparent,n.alphaTest,n.alphaMode,n.depthWrite,n.normalScale?.x??1,n.normalScale?.y??1,n.color.r,n.color.g,n.color.b,n.metalness,n.emissive.r,n.emissive.g,n.emissive.b,n.roughness,n.map,n.normalMap,n.roughnessMap,n.metalnessMap,n.emissiveMap,n.bumpMap,n.clearcoat,n.clearcoatRoughness,n.dispersion,n.visible,n.sheen,n.sheenRoughness,n.sheenColor.r,n.sheenColor.g,n.sheenColor.b,1,n.specularIntensity,n.specularColor.r,n.specularColor.g,n.specularColor.b,n.iridescence,n.iridescenceIOR,n.iridescenceThicknessRange[0],n.iridescenceThicknessRange[1],n.bumpScale,n.displacementScale,n.displacementMap,0,o[0],o[1],o[2],o[3],o[4],o[5],o[6],1,s[0],s[1],s[2],s[3],s[4],s[5],s[6],1,c[0],c[1],c[2],c[3],c[4],c[5],c[6],1,l[0],l[1],l[2],l[3],l[4],l[5],l[6],1,u[0],u[1],u[2],u[3],u[4],u[5],u[6],1,d[0],d[1],d[2],d[3],d[4],d[5],d[6],1,f[0],f[1],f[2],f[3],f[4],f[5],f[6],1,n.subsurfaceColor?.r??1,n.subsurfaceColor?.g??1,n.subsurfaceColor?.b??1,n.subsurface??0,n.subsurfaceRadius?.[0]??1,n.subsurfaceRadius?.[1]??.2,n.subsurfaceRadius?.[2]??.1,n.subsurfaceRadiusScale??1,n.subsurfaceAnisotropy??0,0,0,0];i.set(p,a)}return i}createBVHRawData(e){let t=[],n=e=>{let r=t.length;if(t.push(e),e.leftChild){let t=n(e.leftChild),r=n(e.rightChild);e.leftChild=t,e.rightChild=r}return r};n(e);let r=Pe.VEC4_PER_BVH_NODE*Pe.FLOATS_PER_VEC4,i=t.length*r,a=new Float32Array(i);for(let e=0;e<t.length;e++){let n=e*r,i=t[e];if(i.leftChild!==null){let e=i.leftChild,r=i.rightChild,o=t[e],s=t[r];a[n]=o.boundsMin.x,a[n+1]=o.boundsMin.y,a[n+2]=o.boundsMin.z,a[n+3]=e,a[n+4]=o.boundsMax.x,a[n+5]=o.boundsMax.y,a[n+6]=o.boundsMax.z,a[n+7]=r,a[n+8]=s.boundsMin.x,a[n+9]=s.boundsMin.y,a[n+10]=s.boundsMin.z,a[n+12]=s.boundsMax.x,a[n+13]=s.boundsMax.y,a[n+14]=s.boundsMax.z}else a[n]=i.triangleOffset,a[n+1]=i.triangleCount,a[n+3]=-1}return a}async createMaterialTextures(e){let{materials:t,maps:n,normalMaps:i,bumpMaps:a,roughnessMaps:o,metalnessMaps:s,emissiveMaps:c,displacementMaps:l}=e;console.log(`[TextureCreator] Creating material textures only`);let u=performance.now();try{if(!t||t.length===0)throw Error(`No materials provided for texture creation`);this.textureCache.dispose(),this.textureCache=new Si;let e=[];n&&n.length>0&&e.push(this.createTexturesToDataTexture(n).then(e=>({type:`albedo`,texture:e})).catch(e=>(console.warn(`Failed to create albedo textures:`,e),{type:`albedo`,texture:null}))),i&&i.length>0&&e.push(this.createTexturesToDataTexture(i).then(e=>({type:`normal`,texture:e})).catch(e=>(console.warn(`Failed to create normal textures:`,e),{type:`normal`,texture:null}))),a&&a.length>0&&e.push(this.createTexturesToDataTexture(a).then(e=>({type:`bump`,texture:e})).catch(e=>(console.warn(`Failed to create bump textures:`,e),{type:`bump`,texture:null}))),o&&o.length>0&&e.push(this.createTexturesToDataTexture(o).then(e=>({type:`roughness`,texture:e})).catch(e=>(console.warn(`Failed to create roughness textures:`,e),{type:`roughness`,texture:null}))),s&&s.length>0&&e.push(this.createTexturesToDataTexture(s).then(e=>({type:`metalness`,texture:e})).catch(e=>(console.warn(`Failed to create metalness textures:`,e),{type:`metalness`,texture:null}))),c&&c.length>0&&e.push(this.createTexturesToDataTexture(c).then(e=>({type:`emissive`,texture:e})).catch(e=>(console.warn(`Failed to create emissive textures:`,e),{type:`emissive`,texture:null}))),l&&l.length>0&&e.push(this.createTexturesToDataTexture(l).then(e=>({type:`displacement`,texture:e})).catch(e=>(console.warn(`Failed to create displacement textures:`,e),{type:`displacement`,texture:null})));let d=await Promise.allSettled(e),f={};d.forEach(e=>{if(e.status===`fulfilled`&&e.value){let{type:t,texture:n}=e.value;if(n)switch(t){case`albedo`:n.colorSpace=r.SRGBColorSpace,f.albedoTexture=n;break;case`normal`:f.normalTexture=n;break;case`bump`:f.bumpTexture=n;break;case`roughness`:f.roughnessTexture=n;break;case`metalness`:f.metalnessTexture=n;break;case`emissive`:n.colorSpace=r.SRGBColorSpace,f.emissiveTexture=n;break;case`displacement`:f.displacementTexture=n;break}}});let p=performance.now()-u;return console.log(`[TextureCreator] Material texture creation complete (${p.toFixed(2)}ms)`),f}catch(e){throw console.error(`[TextureCreator] Material texture creation error:`,e),Error(`Material texture creation failed: ${e.message}`)}}calculateOptimalDimensions(e){let t=0,n=0;for(let r of e)t=Math.max(t,r.image.width),n=Math.max(n,r.image.height);for(t=2**Math.ceil(Math.log2(t)),n=2**Math.ceil(Math.log2(n));t>this.maxTextureSize||n>this.maxTextureSize;)t=Math.max(1,Math.floor(t/2)),n=Math.max(1,Math.floor(n/2));return{maxWidth:t,maxHeight:n}}createDataArrayTextureFromResult(e){let t=(e.data instanceof ArrayBuffer,new Uint8Array(e.data));return this.createDataArrayTextureFromBuffer(t,e.width,e.height,e.depth)}createDataArrayTextureFromBuffer(e,t,n,i){let a=new r.DataArrayTexture(e,t,n,i);a.minFilter=r.LinearFilter,a.magFilter=r.LinearFilter,a.format=r.RGBAFormat,a.type=r.UnsignedByteType,a.needsUpdate=!0,a.generateMipmaps=!1,a.userData={buffer:e,bufferType:Uint8Array};let o=a.dispose.bind(a);return a.dispose=()=>{a.userData.buffer&&(this.bufferPool.releaseBuffer(a.userData.buffer,a.userData.bufferType),a.userData.buffer=null),o()},a}async _normalizeTexturesForProcessing(e){let t=[],n=[],i=[];for(let n of e)if(n?.image){if(n.isCompressedTexture&&n.format===r.RGBAFormat&&n.mipmaps?.[0]?.data){let e=n.mipmaps[0],r=t.length;t.push(null),i.push({index:r,flipY:n.flipY,promise:wi(e.data,e.width,e.height)});continue}if(n.isCompressedTexture){console.warn(`[TextureCreator] GPU-compressed texture in mixed group — using placeholder`),t.push(null);continue}if(n.image.data&&!(n.image instanceof HTMLImageElement)&&!(n.image instanceof HTMLCanvasElement)&&!(typeof ImageBitmap<`u`&&n.image instanceof ImageBitmap)){let e=t.length;t.push(null),i.push({index:e,flipY:n.flipY,promise:wi(n.image.data,n.image.width,n.image.height)});continue}t.push(n)}if(i.length>0){let e=await Promise.allSettled(i.map(e=>e.promise));for(let r=0;r<i.length;r++){let{index:a,flipY:o}=i[r],s=e[r];if(s.status===`fulfilled`){let e=s.value;n.push(e),t[a]={image:e,flipY:o}}else console.warn(`[TextureCreator] Failed to create ImageBitmap:`,s.reason)}}for(let e=0;e<t.length;e++)if(t[e]===null){let r=new Uint8ClampedArray([255,255,255,255]),i=await createImageBitmap(new ImageData(r,1,1));n.push(i),t[e]={image:i,flipY:!1}}return{normalized:t,bitmapsToClose:n}}createFallbackTexture(){let e=new r.DataArrayTexture(new Uint8Array([255,255,255,255]),1,1,1);return e.minFilter=r.LinearFilter,e.magFilter=r.LinearFilter,e.format=r.RGBAFormat,e.type=r.UnsignedByteType,e.needsUpdate=!0,e.generateMipmaps=!1,e}combineTextureResults(e){return e[0]}dispose(){this.canvasPool.dispose(),this.bufferPool.dispose(),this.textureCache.dispose()}};function wi(e,t,n){let r=new Uint8ClampedArray(e.buffer,e.byteOffset,e.byteLength);return createImageBitmap(new ImageData(r,t,n))}var Ti=128,Ei=class{constructor(){this._vectorPool={vec3:Array(9).fill().map(()=>new r.Vector3),vec2:[,,,,,,].fill().map(()=>new r.Vector2)},this._matrixPool={mat3:new r.Matrix3,mat4:new r.Matrix4},this.resetArrays(),this.triangleCount=0,this.currentTriangleIndex=0}_getVec3(e=0){return this._vectorPool.vec3[e%this._vectorPool.vec3.length]}_getVec2(e=0){return this._vectorPool.vec2[e%this._vectorPool.vec2.length]}extract(e){return this.resetArrays(),this._triangleCapacity=1024,this.triangleData=new Float32Array(this._triangleCapacity*G.FLOATS_PER_TRIANGLE),this.currentTriangleIndex=0,this.traverseObject(e),this.logStats(),this.getExtractedData()}_ensureCapacity(e){if(e<=this._triangleCapacity)return;let t=this._triangleCapacity;for(;t<e;)t*=2;let n=new Float32Array(t*G.FLOATS_PER_TRIANGLE);n.set(this.triangleData),this.triangleData=n,this._triangleCapacity=t}traverseObject(e){if(e.isMesh?this.processMesh(e):e.isDirectionalLight?this.directionalLights.push(e):e.isCamera&&this.cameras.push(e),e.children)for(let t of e.children)this.traverseObject(t)}processMesh(e){if(!e.geometry||!e.material){console.warn(`Skipping mesh with missing geometry or material:`,e);return}let t=this.processMaterial(e.material);e.userData.materialIndex=t;let n=this.meshes.length;this.meshes.push(e),e.userData.meshIndex=n;let r=this.currentTriangleIndex;this.extractGeometry(e,t,n),this.meshTriangleRanges.push({start:r,count:this.currentTriangleIndex-r})}processMaterial(e){let t=this._materialUuidMap.get(e.uuid)??-1;if(t===-1){e.depthWrite===!1&&(e.depthWrite=!0,console.warn(`Depth write is disabled in material, enabling it for rastered rendering`));let n=this.createMaterialObject(e);this.materials.push(n),t=this.materials.length-1,this._materialUuidMap.set(e.uuid,t),n.clearcoat>0&&(this.sceneFeatures.hasClearcoat=!0),n.transmission>0&&(this.sceneFeatures.hasTransmission=!0),n.dispersion>0&&(this.sceneFeatures.hasDispersion=!0),n.iridescence>0&&(this.sceneFeatures.hasIridescence=!0),n.sheen>0&&(this.sceneFeatures.hasSheen=!0),(n.transparent||n.opacity<1||n.alphaTest>0)&&(this.sceneFeatures.hasTransparency=!0),n.subsurface>0&&(this.sceneFeatures.hasSubsurface=!0),[n.clearcoat>0,n.transmission>0,n.iridescence>0,n.sheen>0].filter(Boolean).length>=2&&(this.sceneFeatures.hasMultiLobeMaterials=!0)}return t}getMaterialAlphaMode(e){if(e.userData?.gltfExtensions?.KHR_materials_unlit?.alphaMode){let t=e.userData.gltfExtensions.KHR_materials_unlit.alphaMode;return t===`BLEND`?2:+(t===`MASK`)}return e.alphaTest>0?1:e.transparent&&e.opacity<1||e.map&&e.map.format===r.RGBAFormat&&e.transparent?2:0}getMaterialType(e){return e.isMeshPhysicalMaterial?`physical`:e.isMeshStandardMaterial?`standard`:e.isMeshPhongMaterial?`phong`:e.isMeshLambertMaterial?`lambert`:e.isMeshBasicMaterial?`basic`:e.isMeshToonMaterial?`toon`:`unknown`}getPhysicalDefaults(){return{emissive:new r.Color(0,0,0),emissiveIntensity:1,roughness:1,metalness:0,ior:1.5,opacity:1,transmission:0,thickness:.1,attenuationColor:new r.Color(16777215),attenuationDistance:1/0,dispersion:0,sheen:0,sheenRoughness:1,sheenColor:new r.Color(0),specularIntensity:1,specularColor:new r.Color(16777215),clearcoat:0,clearcoatRoughness:0,iridescence:0,iridescenceIOR:1.3,iridescenceThicknessRange:[100,400],normalScale:{x:1,y:1},bumpScale:1,displacementScale:1,alphaTest:0,subsurface:0,subsurfaceColor:new r.Color(16777215),subsurfaceRadius:[1,.2,.1],subsurfaceRadiusScale:1,subsurfaceAnisotropy:0}}mapLegacyMaterialToPhysical(e,t){let n={};switch(t){case`basic`:n.emissive=e.color.clone(),n.emissiveIntensity=1,n.color=new r.Color(0),n.roughness=1,n.metalness=0;break;case`lambert`:n.roughness=1,n.metalness=0,n.specularIntensity=0;break;case`phong`:{let t=e.shininess||30;n.roughness=Math.sqrt(2/(t+2)),n.metalness=0}if(e.specular){let t=e.specular.r*.299+e.specular.g*.587+e.specular.b*.114;n.specularIntensity=Math.min(t*2,1),n.specularColor=e.specular.clone()}break;case`toon`:n.roughness=.9,n.metalness=0;break;case`standard`:case`physical`:break}return n}createMaterialObject(e){let t=this.getPhysicalDefaults(),n=this.getMaterialType(e),i=this.mapLegacyMaterialToPhysical(e,n),a=(e.metalness??i.metalness??0)>.1,o=t.ior;a?o=2.5:e.transmission>0&&(o=1.5);let s=e.color||new r.Color(16777215);return n===`basic`&&!e.map&&(s=new r.Color(0)),{uuid:e.uuid,color:s,emissive:i.emissive??e.emissive??t.emissive,emissiveIntensity:i.emissiveIntensity??e.emissiveIntensity??t.emissiveIntensity,roughness:Math.max(.05,i.roughness??e.roughness??t.roughness),metalness:i.metalness??e.metalness??t.metalness,ior:e.ior??o,opacity:e.opacity??t.opacity,transmission:e.transmission??t.transmission,thickness:e.thickness??t.thickness,attenuationColor:e.attenuationColor??t.attenuationColor,attenuationDistance:e.attenuationDistance??t.attenuationDistance,dispersion:e.dispersion??t.dispersion,sheen:e.sheen??t.sheen,sheenRoughness:e.sheenRoughness??t.sheenRoughness,sheenColor:e.sheenColor??t.sheenColor,clearcoat:e.clearcoat??t.clearcoat,clearcoatRoughness:e.clearcoatRoughness??t.clearcoatRoughness,iridescence:e.iridescence??t.iridescence,iridescenceIOR:e.iridescenceIOR??t.iridescenceIOR,iridescenceThicknessRange:e.iridescenceThicknessRange??t.iridescenceThicknessRange,subsurface:e.subsurface??t.subsurface,subsurfaceColor:e.subsurfaceColor??t.subsurfaceColor,subsurfaceRadius:e.subsurfaceRadius??t.subsurfaceRadius,subsurfaceRadiusScale:e.subsurfaceRadiusScale??t.subsurfaceRadiusScale,subsurfaceAnisotropy:e.subsurfaceAnisotropy??t.subsurfaceAnisotropy,specularIntensity:i.specularIntensity??e.specularIntensity??t.specularIntensity,specularColor:i.specularColor??e.specularColor??t.specularColor,normalScale:e.normalScale??t.normalScale,bumpScale:e.bumpScale??t.bumpScale,displacementScale:e.displacementScale??t.displacementScale,transparent:+!!e.transparent,alphaTest:e.alphaTest??t.alphaTest,alphaMode:this.getMaterialAlphaMode(e),side:this.getMaterialSide(e),depthWrite:e.depthWrite??!0?1:0,map:this.processTexture(e.map,this.maps),normalMap:this.processTexture(e.normalMap,this.normalMaps),bumpMap:this.processTexture(e.bumpMap,this.bumpMaps),roughnessMap:this.processTexture(e.roughnessMap,this.roughnessMaps),metalnessMap:this.processTexture(e.metalnessMap,this.metalnessMaps),emissiveMap:this.processTexture(e.emissiveMap,this.emissiveMaps),displacementMap:this.processTexture(e.displacementMap,this.displacementMaps),clearcoatMap:this.processTexture(e.clearcoatMap,[]),clearcoatRoughnessMap:this.processTexture(e.clearcoatRoughnessMap,[]),transmissionMap:this.processTexture(e.transmissionMap,[]),thicknessMap:this.processTexture(e.thicknessMap,[]),sheenColorMap:this.processTexture(e.sheenColorMap,[]),sheenRoughnessMap:this.processTexture(e.sheenRoughnessMap,[]),specularIntensityMap:this.processTexture(e.specularIntensityMap,[]),specularColorMap:this.processTexture(e.specularColorMap,[]),iridescenceMap:this.processTexture(e.iridescenceMap,[]),iridescenceThicknessMap:this.processTexture(e.iridescenceThicknessMap,[]),mapMatrix:this.getTextureMatrix(e.map),normalMapMatrices:this.getTextureMatrix(e.normalMap),bumpMapMatrices:this.getTextureMatrix(e.bumpMap),roughnessMapMatrices:this.getTextureMatrix(e.roughnessMap),metalnessMapMatrices:this.getTextureMatrix(e.metalnessMap),emissiveMapMatrices:this.getTextureMatrix(e.emissiveMap),displacementMapMatrices:this.getTextureMatrix(e.displacementMap),originalType:n}}getTextureMatrix(e){return e?(e.updateMatrix(),e.matrix.elements):new r.Matrix3().elements}getMaterialSide(e){if(e.transmission>0)return 2;switch(e.side){case r.FrontSide:return 0;case r.BackSide:return 1;case r.DoubleSide:return 2;default:return 0}}processTexture(e,t){if(!e)return-1;let n=this._textureIndexCache.get(t);n||(n=new Map,this._textureIndexCache.set(t,n));let r=e.source.uuid,i=n.get(r);if(i!==void 0)return i;if(t.length<Ti){t.push(e);let i=t.length-1;return n.set(r,i),i}return-1}extractGeometry(e,t,n){e.updateMatrix(),e.updateMatrixWorld();let r=e.geometry;r.attributes.normal||r.computeVertexNormals();let i=r.attributes.position,a=r.attributes.normal,o=r.attributes.uv,s=r.index?r.index.array:null;this._matrixPool.mat4.copy(e.matrixWorld),this._matrixPool.mat3.getNormalMatrix(this._matrixPool.mat4);let c=s?s.length/3:i.count/3;this.extractTrianglesInBatch(i,a,o,s,c,t,n)}extractTrianglesInBatch(e,t,n,r,i,a,o){for(;this.materialTriangleCounts.length<=a;)this.materialTriangleCounts.push(0);this.materialTriangleCounts[a]+=i;let s=this._getVec3(0),c=this._getVec3(1),l=this._getVec3(2),u=this._getVec3(3),d=this._getVec3(4),f=this._getVec3(5),p=this._getVec2(0),m=this._getVec2(1),h=this._getVec2(2);this._ensureCapacity(this.currentTriangleIndex+i);for(let g=0;g<i;g++){let i=g*3,_=r?r[i+0]:i+0,v=r?r[i+1]:i+1,y=r?r[i+2]:i+2;this.getVertex(e,_,s),this.getVertex(e,v,c),this.getVertex(e,y,l),this.getVertex(t,_,u),this.getVertex(t,v,d),this.getVertex(t,y,f),n?(this.getVertex(n,_,p),this.getVertex(n,v,m),this.getVertex(n,y,h)):(p.set(0,0),m.set(0,0),h.set(0,0)),s.applyMatrix4(this._matrixPool.mat4),c.applyMatrix4(this._matrixPool.mat4),l.applyMatrix4(this._matrixPool.mat4),u.applyMatrix3(this._matrixPool.mat3).normalize(),d.applyMatrix3(this._matrixPool.mat3).normalize(),f.applyMatrix3(this._matrixPool.mat3).normalize(),this.packTriangleDataTextureFormat(this.currentTriangleIndex,s,c,l,u,d,f,p,m,h,a,o),this.currentTriangleIndex++}}packTriangleDataTextureFormat(e,t,n,r,i,a,o,s,c,l,u,d){let f=e*G.FLOATS_PER_TRIANGLE;this.triangleData[f+G.POSITION_A_OFFSET+0]=t.x,this.triangleData[f+G.POSITION_A_OFFSET+1]=t.y,this.triangleData[f+G.POSITION_A_OFFSET+2]=t.z,this.triangleData[f+G.POSITION_A_OFFSET+3]=0,this.triangleData[f+G.POSITION_B_OFFSET+0]=n.x,this.triangleData[f+G.POSITION_B_OFFSET+1]=n.y,this.triangleData[f+G.POSITION_B_OFFSET+2]=n.z,this.triangleData[f+G.POSITION_B_OFFSET+3]=0,this.triangleData[f+G.POSITION_C_OFFSET+0]=r.x,this.triangleData[f+G.POSITION_C_OFFSET+1]=r.y,this.triangleData[f+G.POSITION_C_OFFSET+2]=r.z,this.triangleData[f+G.POSITION_C_OFFSET+3]=0,this.triangleData[f+G.NORMAL_A_OFFSET+0]=i.x,this.triangleData[f+G.NORMAL_A_OFFSET+1]=i.y,this.triangleData[f+G.NORMAL_A_OFFSET+2]=i.z;{let e=this.materials[u],t=e&&!(e.alphaMode|0)&&!(e.transparent|0)&&(e.transmission||0)===0&&(e.opacity??1)>=1?1:0;this.triangleData[f+G.NORMAL_A_OFFSET+3]=t}this.triangleData[f+G.NORMAL_B_OFFSET+0]=a.x,this.triangleData[f+G.NORMAL_B_OFFSET+1]=a.y,this.triangleData[f+G.NORMAL_B_OFFSET+2]=a.z,this.triangleData[f+G.NORMAL_B_OFFSET+3]=0,this.triangleData[f+G.NORMAL_C_OFFSET+0]=o.x,this.triangleData[f+G.NORMAL_C_OFFSET+1]=o.y,this.triangleData[f+G.NORMAL_C_OFFSET+2]=o.z,this.triangleData[f+G.NORMAL_C_OFFSET+3]=this.materials[u]?.side??0,this.triangleData[f+G.UV_AB_OFFSET+0]=s.x,this.triangleData[f+G.UV_AB_OFFSET+1]=s.y,this.triangleData[f+G.UV_AB_OFFSET+2]=c.x,this.triangleData[f+G.UV_AB_OFFSET+3]=c.y,this.triangleData[f+G.UV_C_MAT_OFFSET+0]=l.x,this.triangleData[f+G.UV_C_MAT_OFFSET+1]=l.y,this.triangleData[f+G.UV_C_MAT_OFFSET+2]=u,this.triangleData[f+G.UV_C_MAT_OFFSET+3]=d}getTriangleData(){return this.triangleData?this.triangleData.subarray(0,this.currentTriangleIndex*G.FLOATS_PER_TRIANGLE):null}getTriangleCount(){return this.currentTriangleIndex}getVertex(e,t,n){return e.itemSize===2?(n.x=e.getX(t),n.y=e.getY(t)):e.itemSize>=3&&(n.x=e.getX(t),n.y=e.getY(t),n.z=e.getZ(t)),n}logStats(){let e=this.currentTriangleIndex*G.FLOATS_PER_TRIANGLE*4;console.log(`materials:`,this.materials.length),console.log(`triangles:`,this.currentTriangleIndex),console.log(`triangle data size (MB):`,(e/(1024*1024)).toFixed(2)),console.log(`maps:`,this.maps.length)}extractMaterialsOnly(e){return this.resetArrays(),this._traverseMaterialsOnly(e),this.getExtractedData()}_traverseMaterialsOnly(e){if(e.isMesh&&e.geometry&&e.material){let t=this.processMaterial(e.material);e.userData.materialIndex=t;let n=this.meshes.length;this.meshes.push(e),e.userData.meshIndex=n}else e.isDirectionalLight?this.directionalLights.push(e):e.isCamera&&this.cameras.push(e);if(e.children)for(let t of e.children)this._traverseMaterialsOnly(t)}resetArrays(){this.triangleData=null,this.triangleCount=0,this.currentTriangleIndex=0,this.materials=[],this.materialTriangleCounts=[],this.meshes=[],this.meshTriangleRanges=[],this.maps=[],this.normalMaps=[],this.bumpMaps=[],this.metalnessMaps=[],this.emissiveMaps=[],this.roughnessMaps=[],this.displacementMaps=[],this.directionalLights=[],this.cameras=[],this._materialUuidMap=new Map,this._textureIndexCache=new WeakMap,this.sceneFeatures={hasClearcoat:!1,hasTransmission:!1,hasDispersion:!1,hasIridescence:!1,hasSheen:!1,hasTransparency:!1,hasSubsurface:!1,hasMultiLobeMaterials:!1,hasMRTOutputs:!0}}getExtractedData(){return{triangleData:this.getTriangleData(),triangleCount:this.getTriangleCount(),materials:this.materials,materialTriangleCounts:this.materialTriangleCounts,meshes:this.meshes,meshTriangleRanges:this.meshTriangleRanges,maps:this.maps,normalMaps:this.normalMaps,bumpMaps:this.bumpMaps,metalnessMaps:this.metalnessMaps,emissiveMaps:this.emissiveMaps,roughnessMaps:this.roughnessMaps,displacementMaps:this.displacementMaps,directionalLights:this.directionalLights,cameras:this.cameras,sceneFeatures:this.sceneFeatures}}},Di=class{constructor(){this.maxLeafSize=8}build(e){let t=e.length;if(t===0){let e=new Float32Array(16);return e[7]=1,e[14]=1,e[15]=-1,{nodeData:e,nodeCount:1,sortedPerm:new Int32Array,bitTrails:new Float32Array}}let n=new Int32Array(t);for(let e=0;e<t;e++)n[e]=e;let r=2*t+4,i=new Float32Array(r*16),a=0,o=new Float32Array(t),s=(t,r,c,l)=>{let u=a++,d=u*16,f=1/0,p=1/0,m=1/0,h=-1/0,g=-1/0,_=-1/0,v=0,y=1/0,b=1/0,x=1/0,S=-1/0,C=-1/0,w=-1/0;for(let i=t;i<r;i++){let t=e[n[i]];f=Math.min(f,t.bMinX),p=Math.min(p,t.bMinY),m=Math.min(m,t.bMinZ),h=Math.max(h,t.bMaxX),g=Math.max(g,t.bMaxY),_=Math.max(_,t.bMaxZ),v+=t.power,y=Math.min(y,t.cx),b=Math.min(b,t.cy),x=Math.min(x,t.cz),S=Math.max(S,t.cx),C=Math.max(C,t.cy),w=Math.max(w,t.cz)}i[d+0]=f,i[d+1]=p,i[d+2]=m,i[d+3]=v,i[d+4]=h,i[d+5]=g,i[d+6]=_;let T=r-t,E;if(T<=this.maxLeafSize){i[d+7]=1,i[d+8]=t,i[d+9]=T,i[d+10]=0,i[d+11]=0,E=null;for(let i=t;i<r;i++){let t=e[n[i]];E=ki(E,Oi(t)),o[i]=c}E||={ax:0,ay:0,az:1,cosO:-1}}else{let a=S-y,o=C-b,u=w-x,f;f=a>=o&&a>=u?0:o>=u?1:2;let p=[`cx`,`cy`,`cz`][f],m=t+r>>1;this._nthElement(n,e,t,r,m,p),i[d+7]=0;let h=s(t,m,c,l+1),g=s(m,r,c+2**l,l+1);i[d+8]=h.nodeIndex,i[d+9]=g.nodeIndex,i[d+10]=0,i[d+11]=0,E=ki(h.cone,g.cone)}return i[d+12]=E.ax,i[d+13]=E.ay,i[d+14]=E.az,i[d+15]=E.cosO,{nodeIndex:u,cone:E}};s(0,t,0,0);let c=new Int32Array(t);for(let e=0;e<t;e++)c[e]=n[e];let l=new Float32Array(a*16);return l.set(i.subarray(0,a*16)),console.log(`[LightBVHBuilder] Built BVH: ${a} nodes for ${t} emissive triangles`),{nodeData:l,nodeCount:a,sortedPerm:c,bitTrails:o}}_nthElement(e,t,n,r,i,a){for(;n<r-1;){let o=n+r>>1,s=t[e[o]][a],c=e[o];e[o]=e[r-1],e[r-1]=c;let l=n;for(let i=n;i<r-1;i++)t[e[i]][a]<s&&(c=e[i],e[i]=e[l],e[l]=c,l++);if(c=e[l],e[l]=e[r-1],e[r-1]=c,l===i)return;l<i?n=l+1:r=l}}};function Oi(e){return e.twoSided?{ax:e.nx,ay:e.ny,az:e.nz,cosO:-1}:{ax:e.nx,ay:e.ny,az:e.nz,cosO:1}}function ki(e,t){if(!e)return t;if(!t||e.cosO<=-1)return e;if(t.cosO<=-1)return t;let n=Math.min(Math.max(e.cosO,-1),1),r=Math.min(Math.max(t.cosO,-1),1),i=Math.acos(n),a=Math.acos(r),o=Math.min(Math.max(e.ax*t.ax+e.ay*t.ay+e.az*t.az,-1),1),s=Math.acos(o);if(Math.min(s+a,Math.PI)<=i)return e;if(Math.min(s+i,Math.PI)<=a)return t;let c=(i+a+s)*.5;if(c>=Math.PI)return{ax:e.ax,ay:e.ay,az:e.az,cosO:-1};let l=c-i,u=e.ay*t.az-e.az*t.ay,d=e.az*t.ax-e.ax*t.az,f=e.ax*t.ay-e.ay*t.ax,p=Math.sqrt(u*u+d*d+f*f);if(p<1e-8)return{ax:e.ax,ay:e.ay,az:e.az,cosO:-1};u/=p,d/=p,f/=p;let m=Math.cos(l),h=Math.sin(l),g=u*e.ax+d*e.ay+f*e.az,_=d*e.az-f*e.ay,v=f*e.ax-u*e.az,y=u*e.ay-d*e.ax,b=e.ax*m+_*h+u*g*(1-m),x=e.ay*m+v*h+d*g*(1-m),S=e.az*m+y*h+f*g*(1-m),C=Math.sqrt(b*b+x*x+S*S)||1;return{ax:b/C,ay:x/C,az:S/C,cosO:Math.cos(c)}}var Ai=class{constructor(){this.emissiveTriangles=[],this.emissiveCount=0,this.totalEmissivePower=0,this.emissiveIndicesArray=null,this.emissivePowerArray=null,this.cdfArray=null,this.lightBVHNodeData=null,this.lightBVHNodeCount=0,this.emissiveBitTrailMap=null,this._totalTriangleCount=0}extractEmissiveTriangles(e,t,n){console.log(`[EmissiveTriangleBuilder] Extracting emissive triangles...`),this.emissiveTriangles=[],this.totalEmissivePower=0,this._totalTriangleCount=n;let r=G.FLOATS_PER_TRIANGLE,i=G.UV_C_MAT_OFFSET+2;for(let a=0;a<n;a++){let n=a*r,o=Math.floor(e[n+i]),s=t[o];if(!s)continue;let c=s.emissive||{r:0,g:0,b:0},l=s.emissiveIntensity||0;if(l>0&&(c.r>0||c.g>0||c.b>0)){let t=e[n+0],r=e[n+1],i=e[n+2],u=e[n+4],d=e[n+5],f=e[n+6],p=e[n+8],m=e[n+9],h=e[n+10],g=this._calculateTriangleArea(t,r,i,u,d,f,p,m,h),_=(.2126*c.r+.7152*c.g+.0722*c.b)*l*g,v=(d-r)*(h-i)-(f-i)*(m-r),y=(f-i)*(p-t)-(u-t)*(h-i),b=(u-t)*(m-r)-(d-r)*(p-t),x=Math.sqrt(v*v+y*y+b*b)||1,S=s.side===1?-1:1;v=v/x*S,y=y/x*S,b=b/x*S;let C=s.side===2,w=(t+u+p)/3,T=(r+d+m)/3,E=(i+f+h)/3,D=Math.min(t,u,p),O=Math.min(r,d,m),k=Math.min(i,f,h),A=Math.max(t,u,p),j=Math.max(r,d,m),M=Math.max(i,f,h);this.emissiveTriangles.push({triangleIndex:a,materialIndex:o,power:_,area:g,emissive:{r:c.r,g:c.g,b:c.b},emissiveIntensity:l,cx:w,cy:T,cz:E,bMinX:D,bMinY:O,bMinZ:k,bMaxX:A,bMaxY:j,bMaxZ:M,nx:v,ny:y,nz:b,twoSided:C}),this.totalEmissivePower+=_}}return this.emissiveCount=this.emissiveTriangles.length,console.log(`[EmissiveTriangleBuilder] Found ${this.emissiveCount} emissive triangles (${(this.emissiveCount/n*100).toFixed(2)}%)`),console.log(`[EmissiveTriangleBuilder] Total emissive power: ${this.totalEmissivePower.toFixed(2)}`),this._buildDataArrays(),this.emissiveCount}_calculateTriangleArea(e,t,n,r,i,a,o,s,c){let l=r-e,u=i-t,d=a-n,f=o-e,p=s-t,m=c-n,h=u*m-d*p,g=d*f-l*m,_=l*p-u*f;return Math.sqrt(h*h+g*g+_*_)*.5}_buildDataArrays(){this.emissiveIndicesArray=new Int32Array(this.emissiveCount),this.emissivePowerArray=new Float32Array(this.emissiveCount);for(let e=0;e<this.emissiveCount;e++)this.emissiveIndicesArray[e]=this.emissiveTriangles[e].triangleIndex,this.emissivePowerArray[e]=this.emissiveTriangles[e].power;this._buildCDF()}_buildCDF(){if(this.emissiveCount===0){this.cdfArray=new Float32Array(1),this.cdfArray[0]=0;return}this.cdfArray=new Float32Array(this.emissiveCount);let e=0;for(let t=0;t<this.emissiveCount;t++)e+=this.emissivePowerArray[t],this.cdfArray[t]=e;if(e>0)for(let t=0;t<this.emissiveCount;t++)this.cdfArray[t]/=e}sampleCDF(e){if(this.emissiveCount===0)return-1;if(this.emissiveCount===1)return 0;let t=0,n=this.emissiveCount-1;for(;t<n;){let r=Math.floor((t+n)/2);this.cdfArray[r]<e?t=r+1:n=r}return t}getGPUData(){return{emissiveIndices:this.emissiveIndicesArray,emissivePower:this.emissivePowerArray,emissiveCDF:this.cdfArray,emissiveCount:this.emissiveCount,totalPower:this.totalEmissivePower}}createEmissiveTexture(){if(this.emissiveCount===0)return new r.DataTexture(new Float32Array(4),1,1,r.RGBAFormat,r.FloatType);let e=new Float32Array(this.emissiveCount*4);for(let t=0;t<this.emissiveCount;t++){let n=t*4;e[n+0]=this.emissiveIndicesArray[t],e[n+1]=this.emissivePowerArray[t],e[n+2]=this.cdfArray[t],e[n+3]=0}let t=Math.ceil(Math.sqrt(this.emissiveCount)),n=Math.ceil(this.emissiveCount/t),i=t*n*4,a=new Float32Array(i);a.set(e);let o=new r.DataTexture(a,t,n,r.RGBAFormat,r.FloatType);return o.needsUpdate=!0,o.generateMipmaps=!1,o.minFilter=r.NearestFilter,o.magFilter=r.NearestFilter,console.log(`[EmissiveTriangleBuilder] Created ${t}x${n} emissive texture (${this.emissiveCount} emissives)`),o}createEmissiveRawData(){if(this.emissiveCount===0)return new Float32Array(8);let e=new Float32Array(this.emissiveCount*8);for(let t=0;t<this.emissiveCount;t++){let n=this.emissiveTriangles[t],r=t*8;e[r+0]=n.triangleIndex,e[r+1]=n.power,e[r+2]=this.cdfArray[t],e[r+3]=this.totalEmissivePower>0?n.power/this.totalEmissivePower:0,e[r+4]=n.emissive.r*n.emissiveIntensity,e[r+5]=n.emissive.g*n.emissiveIntensity,e[r+6]=n.emissive.b*n.emissiveIntensity,e[r+7]=n.area}return console.log(`[EmissiveTriangleBuilder] Created emissive raw data: ${this.emissiveCount} entries (${e.byteLength} bytes)`),e}getStats(){if(this.emissiveCount===0)return{count:0,totalPower:0,averagePower:0,minPower:0,maxPower:0};let e=1/0,t=-1/0;for(let n=0;n<this.emissiveCount;n++){let r=this.emissivePowerArray[n];e=Math.min(e,r),t=Math.max(t,r)}return{count:this.emissiveCount,totalPower:this.totalEmissivePower,averagePower:this.totalEmissivePower/this.emissiveCount,minPower:e,maxPower:t}}updateMaterialEmissive(e,t,n,r,i){let a=t.emissive||{r:0,g:0,b:0},o=t.emissiveIntensity||0,s=o>0&&(a.r>0||a.g>0||a.b>0);if(s!==this.emissiveTriangles.some(t=>t.materialIndex===e))return this.extractEmissiveTriangles(n,r,i),!0;if(!s)return!1;let c=.2126*a.r+.7152*a.g+.0722*a.b;this.totalEmissivePower=0;for(let t=0;t<this.emissiveCount;t++){let n=this.emissiveTriangles[t];n.materialIndex===e&&(n.power=c*o*n.area,n.emissive={r:a.r,g:a.g,b:a.b},n.emissiveIntensity=o,this.emissivePowerArray[t]=n.power),this.totalEmissivePower+=this.emissiveTriangles[t].power}return this._buildCDF(),!0}buildLightBVH(){if(this.emissiveCount===0)return this.lightBVHNodeData=new Float32Array(16),this.lightBVHNodeData[7]=1,this.lightBVHNodeData[14]=1,this.lightBVHNodeData[15]=-1,this.lightBVHNodeCount=1,this.emissiveBitTrailMap=new Float32Array(Math.max(this._totalTriangleCount,1)).fill(-1),1;let{nodeData:e,nodeCount:t,sortedPerm:n,bitTrails:r}=new Di().build(this.emissiveTriangles);this.lightBVHNodeData=e,this.lightBVHNodeCount=t,this._rebuildSortedEmissiveData(n),this.emissiveBitTrailMap=new Float32Array(Math.max(this._totalTriangleCount,1)).fill(-1);for(let e=0;e<n.length;e++){let t=this.emissiveTriangles[n[e]].triangleIndex;this.emissiveBitTrailMap[t]=r[e]}return t}_rebuildSortedEmissiveData(e){let t=e.length;this.emissiveIndicesArray=new Int32Array(t),this.emissivePowerArray=new Float32Array(t);for(let n=0;n<t;n++){let t=e[n];this.emissiveIndicesArray[n]=this.emissiveTriangles[t].triangleIndex,this.emissivePowerArray[n]=this.emissiveTriangles[t].power}this._buildCDF();let n=new Float32Array(t*8);for(let r=0;r<t;r++){let t=e[r],i=this.emissiveTriangles[t],a=r*8;n[a+0]=i.triangleIndex,n[a+1]=i.power,n[a+2]=this.cdfArray[r],n[a+3]=this.totalEmissivePower>0?i.power/this.totalEmissivePower:0,n[a+4]=i.emissive.r*i.emissiveIntensity,n[a+5]=i.emissive.g*i.emissiveIntensity,n[a+6]=i.emissive.b*i.emissiveIntensity,n[a+7]=i.area}this.emissiveTriangleData=n,console.log(`[EmissiveTriangleBuilder] Rebuilt sorted emissive data: ${t} entries`)}clear(){this.emissiveTriangles=[],this.emissiveCount=0,this.totalEmissivePower=0,this.emissiveIndicesArray=null,this.emissivePowerArray=null,this.cdfArray=null,this.lightBVHNodeData=null,this.lightBVHNodeCount=0,this.emissiveBitTrailMap=null,this._totalTriangleCount=0}},ji=class{constructor(e=`Build`){this.label=e,this.entries=new Map,this.order=[],this.totalStart=performance.now()}start(e){return this.entries.set(e,{start:performance.now(),end:null}),this.order.includes(e)||this.order.push(e),this}end(e){let t=this.entries.get(e);return t&&(t.end=performance.now(),t.duration=t.end-t.start),this}getDuration(e){return this.entries.get(e)?.duration??0}print(){let e=performance.now()-this.totalStart,t=this.order.map(e=>{let t=this.entries.get(e)?.duration??0;return t>=1?`${e} ${Math.round(t)}ms`:null}).filter(Boolean);return console.log(`[${this.label}] ${Math.round(e)}ms`+(t.length?` | ${t.join(` · `)}`:``)),{steps:Object.fromEntries(this.order.map(e=>[e,Math.round(this.entries.get(e)?.duration??0)])),total:Math.round(e)}}},Mi=``+(typeof document>`u`&&typeof location>`u`?require(`url`).pathToFileURL(__dirname+`/assets/BVHRefitWorker-GkmNJYvb.js`).href:new URL(`assets/BVHRefitWorker-GkmNJYvb.js`,typeof document>`u`?location.href:document.currentScript&&document.currentScript.tagName.toUpperCase()===`SCRIPT`&&document.currentScript.src||document.baseURI).href),Ni=class{constructor(e={}){this.config={useWorkers:!0,bvhDepth:30,maxLeafSize:4,verbose:!1,useFloat32Array:!0,textureQuality:`adaptive`,maxTextureSize:Pe.DEFAULT_MAX_TEXTURE_SIZE,enableTextureCache:!0,maxConcurrentTextureTasks:Math.min(navigator.hardwareConcurrency||4,6),treeletSize:7,treeletOptimizationPasses:1,treeletMinImprovement:.01,...e},this.triangleData=null,this.triangleCount=0,this.materials=[],this.maps=[],this.normalMaps=[],this.bumpMaps=[],this.roughnessMaps=[],this.metalnessMaps=[],this.emissiveMaps=[],this.displacementMaps=[],this.directionalLights=[],this.cameras=[],this.spheres=[],this.bvhRoot=null,this.bvhData=null,this.materialData=null,this.instanceTable=null,this.originalToBvhMap=null,this._refitWorker=null,this._refitSharedBuffers=null,this._rebuildGeneration=0,this._pendingRebuilds=new Map,this.albedoTextures=null,this.normalTextures=null,this.bumpTextures=null,this.roughnessTextures=null,this.metalnessTextures=null,this.emissiveTextures=null,this.displacementTextures=null,this.emissiveTriangleData=null,this.emissiveTriangleCount=0,this.lightBVHNodeData=null,this.lightBVHNodeCount=0,this.emissiveBitTrailMap=null,this._initProcessors(),this.isProcessing=!1,this.processingStage=null,this.performanceMetrics={textureCreationTime:0,geometryExtractionTime:0,bvhBuildTime:0,totalProcessingTime:0}}setMaxTextureSize(e){return this.config.maxTextureSize=e,this.textureCreator?.setMaxTextureSize(e)}_initProcessors(){this.geometryExtractor=new Ei,this.bvhBuilder=new ti,this.bvhBuilder.maxLeafSize=this.config.maxLeafSize,this.bvhBuilder.setTreeletConfig({enabled:this.config.enableTreeletOptimization,size:this.config.treeletSize,passes:this.config.treeletOptimizationPasses,minImprovement:this.config.treeletMinImprovement}),this.textureCreator=new Ci({maxTextureSize:this.config.maxTextureSize}),this.emissiveTriangleBuilder=new Ai,this.tlasBuilder=new _i}_log(e,t){this.config.verbose&&console.log(`[SceneProcessor] ${e}`,t||``)}async buildBVH(e){if(this.isProcessing)throw Error(`Already processing a scene. Call dispose() first.`);this.isProcessing=!0,this.processingStage=`init`;let t=new ji(`SceneProcessor (${e.name||`scene`})`);try{this._reset(),this._log(`Starting scene processing`),this.processingStage=`extraction`,t.start(`Geometry extraction`),await this._extractGeometry(e),t.end(`Geometry extraction`),this.performanceMetrics.geometryExtractionTime=t.getDuration(`Geometry extraction`),this.processingStage=`bvh`,t.start(`BVH construction (worker)`),t.start(`Material textures (parallel)`);let n=!1,r=this._buildBVH().then(()=>t.end(`BVH construction (worker)`)),i=this._createMaterialTextures().then(()=>{t.end(`Material textures (parallel)`),n=!0});return await r,R({status:`Building light data...`,progress:77}),t.start(`Emissive extraction + Light BVH`),this._buildEmissiveData(),t.end(`Emissive extraction + Light BVH`),n||R({status:`Processing material textures...`,progress:80}),await i,this.performanceMetrics.bvhBuildTime=t.getDuration(`BVH construction (worker)`),this.performanceMetrics.textureCreationTime=t.getDuration(`Material textures (parallel)`),this.processingStage=`finalize`,t.start(`BVH data packing`),this.bvhRoot&&!this.bvhData&&(this.bvhData=this.textureCreator.createBVHRawData(this.bvhRoot)),t.end(`BVH data packing`),this.spheres=this._createSpheres(),this.performanceMetrics.totalProcessingTime=performance.now()-t.totalStart,t.print(),this.processingStage=`complete`,R({status:`Scene data ready`,progress:85}),this}catch(e){throw this.processingStage=`error`,console.error(`[SceneProcessor] Processing error:`,e),R({status:`Error: ${e.message}`,progress:100}),e}finally{this.isProcessing=!1}}async _extractGeometry(e){R({isLoading:!0,title:`Processing`,status:`Extracting geometry...`,progress:15}),await new Promise(e=>setTimeout(e,0)),this._log(`Extracting geometry`);let t=performance.now();try{let n=this.geometryExtractor.extract(e);this.triangleData=n.triangleData,this.triangleCount=n.triangleCount,this._log(`Using Float32Array format: ${this.triangleCount} triangles, ${(this.triangleData.byteLength/(1024*1024)).toFixed(2)}MB`),this.materials=n.materials,this.materialCount=this.materials.length,this.materialTriangleCounts=n.materialTriangleCounts,this.meshes=n.meshes,this.meshTriangleRanges=n.meshTriangleRanges,this.maps=n.maps,this.normalMaps=n.normalMaps,this.bumpMaps=n.bumpMaps,this.roughnessMaps=n.roughnessMaps,this.metalnessMaps=n.metalnessMaps,this.emissiveMaps=n.emissiveMaps,this.displacementMaps=n.displacementMaps,this.directionalLights=n.directionalLights,this.cameras=n.cameras,this.sceneFeatures=n.sceneFeatures;let r=performance.now()-t;this._log(`Geometry extraction complete (${r.toFixed(2)}ms)`,{triangleCount:this.triangleCount,materials:this.materials.length}),R({status:`Extracted ${this.triangleCount.toLocaleString()} triangles`,progress:25})}catch(e){throw console.error(`[SceneProcessor] Geometry extraction error:`,e),R({status:`Extraction error: ${e.message}`,progress:25}),e}}async _buildBVH(){if(R({status:`Building BVH...`,progress:25}),this.triangleCount===0)throw Error(`No triangles to build BVH from`);this._log(`Building two-level BVH (TLAS/BLAS)`);let e=performance.now();try{let t=G.FLOATS_PER_TRIANGLE,n=this.meshTriangleRanges;if(!n||n.length===0)throw Error(`No mesh triangle ranges available for TLAS/BLAS build`);this.instanceTable=new vi,this.instanceTable.allocate(n.length);let r=n.length,i=this.config.enableTreeletOptimization,a=[],o=[];for(let e=0;e<r;e++){let t=n[e];t.count!==0&&(t.count>=2e5&&mi(t.count)?o.push({m:e,range:t}):a.push({m:e,range:t}))}let s={depth:this.config.bvhDepth,treeletOptimization:{enabled:i!==!1,size:this.config.treeletSize,passes:this.config.treeletOptimizationPasses,minImprovement:this.config.treeletMinImprovement},reinsertionOptimization:{enabled:this.bvhBuilder.enableReinsertionOptimization,batchSizeRatio:this.bvhBuilder.reinsertionBatchSizeRatio,maxIterations:this.bvhBuilder.reinsertionMaxIterations}},c=a.length+o.length,l=this._buildBLASesWithPool(a,s,e=>{R({status:`Building BLAS ${e+o.length}/${c}...`,progress:25+Math.floor(e/c*45)})}),u=o.map(({m:e,range:n})=>di(this.triangleData.slice(n.start*t,(n.start+n.count)*t),this.config.bvhDepth,null,{maxLeafSize:this.bvhBuilder.maxLeafSize,numBins:this.bvhBuilder.numBins,maxBins:this.bvhBuilder.maxBins,minBins:this.bvhBuilder.minBins,...s}).then(t=>({m:e,range:n,result:t}))),[d,f]=await Promise.all([l,Promise.all(u)]);for(let{m:e,range:n,result:r}of[...d,...f])r.reorderedTriangles&&this.triangleData.set(r.reorderedTriangles,n.start*t),this.instanceTable.setEntry({meshIndex:e,blasNodeCount:r.bvhData.length/16,triOffset:n.start,triCount:n.count,originalToBvhMap:r.originalToBvh||null,bvhData:r.bvhData});R({status:`Built all BLASes`,progress:70}),R({status:`Building TLAS...`,progress:72});let p=this.instanceTable.entries.filter(e=>e!==null);this.instanceTable.computeAABBs(this.triangleData);let{root:m,nodeCount:h}=this.tlasBuilder.build(p);this.instanceTable.assignOffsets(h);let g=this.instanceTable.totalNodeCount,_=this.tlasBuilder.flatten(m,p);this.bvhData=new Float32Array(g*16),this.bvhData.set(_);for(let e of p){let t=e.blasOffset*16;this.bvhData.set(e.bvhData,t),this._offsetBLASInPlace(t,e.bvhData.length/16,e.blasOffset,e.triOffset)}this._buildGlobalOriginalToBvhMap();for(let e of p)e.originalToBvhMap=null,e.bvhData=null;this.bvhRoot=!0,this._disposeRefitWorker();let v=performance.now()-e;this._log(`BVH complete: ${p.length} mesh(es), ${this.bvhData.length/16} nodes (${v.toFixed(2)}ms)`),R({status:`BVH construction complete`,progress:75})}catch(e){throw console.error(`[SceneProcessor] BVH building error:`,e),R({status:`BVH error: ${e.message}`,progress:75}),e}}_offsetBLASInPlace(e,t,n,r){for(let i=0;i<t;i++){let t=e+i*16;this.bvhData[t+3]===-1?this.bvhData[t]+=r:(this.bvhData[t+3]+=n,this.bvhData[t+7]+=n)}}_buildBLASesWithPool(e,t,n){if(e.length===0)return Promise.resolve([]);let r=G.FLOATS_PER_TRIANGLE,i=Math.min(e.length,this.config.maxConcurrentTextureTasks||4),a=[],o=0,s=0;return new Promise((c,l)=>{let u=[],d=n=>{if(o>=e.length){n.terminate(),u.splice(u.indexOf(n),1),u.length===0&&c(a);return}let{m:i,range:s}=e[o++],l=this.triangleData.slice(s.start*r,(s.start+s.count)*r),d=s.count,f=d<=500?{...t.treeletOptimization,enabled:!1}:t.treeletOptimization;n._currentTask={m:i,range:s},n.postMessage({triangleData:l.buffer,triangleByteOffset:l.byteOffset,triangleByteLength:l.byteLength,triangleCount:d,depth:t.depth,reportProgress:!1,sharedReorderBuffer:null,treeletOptimization:f,reinsertionOptimization:t.reinsertionOptimization},[l.buffer])},f=(e,t)=>{let r=t.data;if(r.error){u.forEach(e=>e.terminate()),l(Error(r.error));return}if(r.progress!==void 0)return;let{m:i,range:o}=e._currentTask;a.push({m:i,range:o,result:{bvhData:r.bvhData,reorderedTriangles:r.triangles||null,originalToBvh:r.originalToBvh||null}}),s++,n?.(s),d(e)};(async()=>{for(let e=0;e<i;e++){let e;try{e=new Worker(Zr,{type:`module`})}catch(t){if(t.name!==`SecurityError`){l(t);return}e=await Je(Zr)}e.onmessage=t=>f(e,t),e.onerror=e=>{u.forEach(e=>e.terminate()),l(e)},u.push(e),d(e)}})().catch(l)})}_buildGlobalOriginalToBvhMap(){this.originalToBvhMap=new Uint32Array(this.triangleCount);for(let e of this.instanceTable.entries){if(!e)continue;let t=new Uint32Array(e.triCount);if(e.originalToBvhMap)for(let n=0;n<e.triCount;n++){let r=e.originalToBvhMap[n];this.originalToBvhMap[e.triOffset+n]=e.triOffset+r,t[r]=n}else for(let n=0;n<e.triCount;n++)this.originalToBvhMap[e.triOffset+n]=e.triOffset+n,t[n]=n;e.bvhToOriginal=t}}async _createMaterialTextures(){this._log(`Creating material textures (parallel with BVH)`);try{this.materials?.length&&(this.materialData=this.textureCreator.createMaterialRawData(this.materials));let e=[{data:this.maps,prop:`albedoTextures`},{data:this.normalMaps,prop:`normalTextures`},{data:this.bumpMaps,prop:`bumpTextures`},{data:this.roughnessMaps,prop:`roughnessTextures`},{data:this.metalnessMaps,prop:`metalnessTextures`},{data:this.emissiveMaps,prop:`emissiveTextures`},{data:this.displacementMaps,prop:`displacementTextures`}];await Promise.all(e.filter(({data:e})=>e?.length>0).map(({data:e,prop:t})=>this.textureCreator.createTexturesToDataTexture(e).then(e=>{this[t]=e}))),this._log(`Material textures complete`,{materialData:!!this.materialData})}catch(e){throw console.error(`[SceneProcessor] Texture creation error:`,e),e}}_buildEmissiveData(){this.emissiveTriangleCount=this.emissiveTriangleBuilder.extractEmissiveTriangles(this.triangleData,this.materials,this.triangleCount),this.emissiveTriangleData=this.emissiveTriangleBuilder.createEmissiveRawData(),this.emissiveTotalPower=this.emissiveTriangleBuilder.totalEmissivePower,this._log(`Emissive triangle extraction complete`,this.emissiveTriangleBuilder.getStats()),this.emissiveTriangleBuilder.buildLightBVH(),this.lightBVHNodeData=this.emissiveTriangleBuilder.lightBVHNodeData,this.lightBVHNodeCount=this.emissiveTriangleBuilder.lightBVHNodeCount,this.emissiveTriangleData=this.emissiveTriangleBuilder.emissiveTriangleData||this.emissiveTriangleData,this.emissiveBitTrailMap=this.emissiveTriangleBuilder.emissiveBitTrailMap}_createSpheres(){return[]}_reset(){this._disposeTextures(),this.triangles=[],this.triangleData=null,this.triangleCount=0,this.materials=[],this.meshTriangleRanges=null,this.maps=[],this.normalMaps=[],this.bumpMaps=[],this.roughnessMaps=[],this.metalnessMaps=[],this.emissiveMaps=[],this.displacementMaps=[],this.directionalLights=[],this.cameras=[],this.spheres=[],this.bvhRoot=null,this.bvhData=null,this.instanceTable=null,this.lightBVHNodeData=null,this.lightBVHNodeCount=0,this.emissiveBitTrailMap=null,this.performanceMetrics={textureCreationTime:0,geometryExtractionTime:0,bvhBuildTime:0,totalProcessingTime:0}}_disposeTextures(){[`albedoTextures`,`normalTextures`,`bumpTextures`,`roughnessTextures`,`metalnessTextures`,`emissiveTextures`,`displacementTextures`].forEach(e=>{this[e]&&(typeof this[e].dispose==`function`&&this[e].dispose(),this[e]=null)})}async rebuildMaterials(e){if(this.isProcessing)throw Error(`Already processing. Cannot rebuild materials during processing.`);this._log(`Rebuilding materials and textures`);let t=performance.now();try{this.isProcessing=!0;let n=this.geometryExtractor.extractMaterialsOnly(e);this._disposeMaterialTextures(),this.materials=n.materials,this.materialCount=this.materials.length,this.meshes=n.meshes,this.maps=n.maps,this.normalMaps=n.normalMaps,this.bumpMaps=n.bumpMaps,this.roughnessMaps=n.roughnessMaps,this.metalnessMaps=n.metalnessMaps,this.emissiveMaps=n.emissiveMaps,this.displacementMaps=n.displacementMaps,this.sceneFeatures=n.sceneFeatures;let r={materials:this.materials,triangles:this.triangleData,maps:this.maps,normalMaps:this.normalMaps,bumpMaps:this.bumpMaps,roughnessMaps:this.roughnessMaps,metalnessMaps:this.metalnessMaps,emissiveMaps:this.emissiveMaps,displacementMaps:this.displacementMaps,bvhRoot:this.bvhRoot},i=await this.textureCreator.createMaterialTextures(r);this.materialData=this.textureCreator.createMaterialRawData(this.materials),this.albedoTextures=i.albedoTexture,this.normalTextures=i.normalTexture,this.bumpTextures=i.bumpTexture,this.roughnessTextures=i.roughnessTexture,this.metalnessTextures=i.metalnessTexture,this.emissiveTextures=i.emissiveTexture,this.displacementTextures=i.displacementTexture;let a=performance.now()-t;return this._log(`Material rebuild complete (${a.toFixed(2)}ms)`,{materials:this.materials.length,textures:this.maps.length}),this}catch(e){throw console.error(`[SceneProcessor] Material rebuild error:`,e),e}finally{this.isProcessing=!1}}_disposeMaterialTextures(){[`albedoTextures`,`normalTextures`,`bumpTextures`,`roughnessTextures`,`metalnessTextures`,`emissiveTextures`,`displacementTextures`].forEach(e=>{if(this[e])try{typeof this[e].dispose==`function`&&this[e].dispose()}catch(t){console.warn(`[SceneProcessor] Error disposing ${e}:`,t)}finally{this[e]=null}}),this.textureCreator&&this.textureCreator.textureCache&&(this.textureCreator.textureCache.dispose(),this.textureCreator.textureCache=new this.textureCreator.textureCache.constructor)}getStatistics(){let e={triangleCount:this.triangleCount,materialCount:this.materials.length,textureCount:this.maps.length,lightCount:this.directionalLights.length,cameraCount:this.cameras.length,processingComplete:this.processingStage===`complete`,hasBVH:!!this.bvhRoot,hasTextures:!!this.materialData&&!!this.bvhData,useFloat32Array:this.config.useFloat32Array,triangleDataSize:this.triangleData?(this.triangleData.byteLength/(1024*1024)).toFixed(2)+`MB`:`0MB`};return this.performanceMetrics.totalProcessingTime>0&&(e.performance={totalTime:this.performanceMetrics.totalProcessingTime,textureTime:this.performanceMetrics.textureCreationTime,bvhTime:this.performanceMetrics.bvhBuildTime,extractionTime:this.performanceMetrics.geometryExtractionTime,texturePercentage:(this.performanceMetrics.textureCreationTime/this.performanceMetrics.totalProcessingTime*100).toFixed(1)+`%`}),this.textureCreator&&this.textureCreator.capabilities&&(e.textureCapabilities=this.textureCreator.capabilities),e}updateConfig(e){Object.assign(this.config,e),this.bvhBuilder&&(this.bvhBuilder.maxLeafSize=this.config.maxLeafSize,this.bvhBuilder.setTreeletConfig({enabled:this.config.enableTreeletOptimization,size:this.config.treeletSize,passes:this.config.treeletOptimizationPasses,minImprovement:this.config.treeletMinImprovement})),this._log(`Configuration updated`,this.config)}async refitBVH(e,t){if(!this.bvhData||!this.triangleData||!this.originalToBvhMap)throw Error(`No BVH data available for refit. Run buildBVH() first.`);if(!this._refitWorker)try{this._refitWorker=new Worker(Mi,{type:`module`})}catch(e){if(e.name!==`SecurityError`)throw e;this._refitWorker=await Je(Mi)}if(!this._refitSharedBuffers){let t=new SharedArrayBuffer(this.bvhData.byteLength),n=new SharedArrayBuffer(this.triangleData.byteLength),r=new SharedArrayBuffer(e.byteLength),i=new Float32Array(t),a=new Float32Array(n);i.set(this.bvhData),a.set(this.triangleData),this.bvhData=i,this.triangleData=a;let o=this.originalToBvhMap.length,s=new Uint32Array(o);for(let e=0;e<o;e++)s[this.originalToBvhMap[e]]=e;this._refitSharedBuffers={bvhBuf:t,triBuf:n,posBuf:r,posView:new Float32Array(r)},this._refitWorker.postMessage({type:`init`,sharedBvhBuf:t,sharedTriBuf:n,sharedPosBuf:r,bvhToOriginal:s},[s.buffer])}return this._refitSharedBuffers.posView.set(e),new Promise((e,n)=>{this._refitWorker.onmessage=r=>{let i=r.data;i.type===`refitComplete`?(t&&this._patchSmoothNormals(t),e({refitTimeMs:i.refitTimeMs})):i.type===`error`&&n(Error(i.error))},this._refitWorker.postMessage({type:`refit`})})}_patchSmoothNormals(e){this._patchNormalsRange(e,0,this.originalToBvhMap.length)}refitBLASes(e,t,n){if(!this.instanceTable||!this.bvhData||!this.triangleData)throw Error(`No TLAS/BLAS data available. Run buildBVH() first.`);let r=performance.now();this._blasRefitter||=new oi;for(let r of e){let e=this.instanceTable.entries[r];e&&(this._updateMeshTrianglePositions(e,t),n&&this._patchMeshSmoothNormals(e,n),this._blasRefitter.refitRange(this.bvhData,this.triangleData,e.blasOffset,e.blasNodeCount),this.instanceTable.recomputeAABB(r,this.bvhData,this.triangleData))}return this._refitTLAS(),{refitTimeMs:performance.now()-r}}computeBLASDirtyRanges(e){let t=G.FLOATS_PER_TRIANGLE,n=[],r=[];for(let i of e){let e=this.instanceTable.entries[i];e&&(n.push({offset:e.triOffset*t,count:e.triCount*t}),r.push({offset:e.blasOffset*16,count:e.blasNodeCount*16}))}return r.push({offset:0,count:this.instanceTable.tlasNodeCount*16}),{triRanges:n,bvhRanges:r}}uploadToPathTracer(e,t,n,r){return this.triangleData?(e.setTriangleData(this.triangleData,this.triangleCount),this.bvhData?(e.setBVHData(this.bvhData),e.setInstanceTable(this.instanceTable),this.materialData?e.materialData.setMaterialData(this.materialData):console.warn(`SceneProcessor: No material data, using defaults`),r&&e.environment.setEnvironmentTexture(r),e.materialData.setMaterialTextures({albedoMaps:this.albedoTextures,normalMaps:this.normalTextures,bumpMaps:this.bumpTextures,roughnessMaps:this.roughnessTextures,metalnessMaps:this.metalnessTextures,emissiveMaps:this.emissiveTextures,displacementMaps:this.displacementTextures}),this.emissiveTriangleData&&e.setEmissiveTriangleData(this.emissiveTriangleData,this.emissiveTriangleCount,this.emissiveTotalPower,this.emissiveBitTrailMap),this.lightBVHNodeData&&e.setLightBVHData(this.lightBVHNodeData,this.lightBVHNodeCount),t.transferSceneLights(n),!0):(console.error(`SceneProcessor: Failed to get BVH data`),!1)):(console.error(`SceneProcessor: Failed to get triangle data`),!1)}updateMaterialEmissive(e,t,n){if(!this.emissiveTriangleBuilder)return null;let r=this.materials[e];return!r||(t===`emissive`?r.emissive=n:t===`emissiveIntensity`&&(r.emissiveIntensity=n),!this.emissiveTriangleBuilder.updateMaterialEmissive(e,r,this.triangleData,this.materials,this.triangleCount))?null:(this.emissiveTriangleBuilder.buildLightBVH(),this.lightBVHNodeData=this.emissiveTriangleBuilder.lightBVHNodeData,this.lightBVHNodeCount=this.emissiveTriangleBuilder.lightBVHNodeCount,this.emissiveTriangleData=this.emissiveTriangleBuilder.emissiveTriangleData,this.emissiveBitTrailMap=this.emissiveTriangleBuilder.emissiveBitTrailMap,this.emissiveTriangleCount=this.emissiveTriangleBuilder.emissiveCount,this.emissiveTotalPower=this.emissiveTriangleBuilder.totalEmissivePower,{rawData:this.emissiveTriangleBuilder.emissiveTriangleData,emissiveCount:this.emissiveTriangleBuilder.emissiveCount,totalPower:this.emissiveTriangleBuilder.totalEmissivePower,bitTrailMap:this.emissiveBitTrailMap,lightBVHNodeData:this.lightBVHNodeData,lightBVHNodeCount:this.lightBVHNodeCount})}_updateMeshTrianglePositions(e,t){let n=G.FLOATS_PER_TRIANGLE,r=G.POSITION_A_OFFSET,i=G.POSITION_B_OFFSET,a=G.POSITION_C_OFFSET,o=G.NORMAL_A_OFFSET,s=G.NORMAL_B_OFFSET,c=G.NORMAL_C_OFFSET,l=e.bvhToOriginal;for(let u=0;u<e.triCount;u++){let d=l[u],f=(e.triOffset+u)*n,p=(e.triOffset+d)*9,m=t[p],h=t[p+1],g=t[p+2],_=t[p+3],v=t[p+4],y=t[p+5],b=t[p+6],x=t[p+7],S=t[p+8];this.triangleData[f+r]=m,this.triangleData[f+r+1]=h,this.triangleData[f+r+2]=g,this.triangleData[f+i]=_,this.triangleData[f+i+1]=v,this.triangleData[f+i+2]=y,this.triangleData[f+a]=b,this.triangleData[f+a+1]=x,this.triangleData[f+a+2]=S;let C=_-m,w=v-h,T=y-g,E=b-m,D=x-h,O=S-g,k=w*O-T*D,A=T*E-C*O,j=C*D-w*E;this.triangleData[f+o]=k,this.triangleData[f+o+1]=A,this.triangleData[f+o+2]=j,this.triangleData[f+s]=k,this.triangleData[f+s+1]=A,this.triangleData[f+s+2]=j,this.triangleData[f+c]=k,this.triangleData[f+c+1]=A,this.triangleData[f+c+2]=j}}_patchMeshSmoothNormals(e,t){this._patchNormalsRange(t,e.triOffset,e.triCount)}_patchNormalsRange(e,t,n){let r=G.FLOATS_PER_TRIANGLE,i=G.NORMAL_A_OFFSET,a=G.NORMAL_B_OFFSET,o=G.NORMAL_C_OFFSET;for(let s=0;s<n;s++){let n=t+s,c=this.originalToBvhMap[n]*r,l=n*9;this.triangleData[c+i]=e[l],this.triangleData[c+i+1]=e[l+1],this.triangleData[c+i+2]=e[l+2],this.triangleData[c+a]=e[l+3],this.triangleData[c+a+1]=e[l+4],this.triangleData[c+a+2]=e[l+5],this.triangleData[c+o]=e[l+6],this.triangleData[c+o+1]=e[l+7],this.triangleData[c+o+2]=e[l+8]}}_refitTLAS(){let e=this.instanceTable.tlasNodeCount;(!this._tlasBounds||this._tlasBounds.length<e*6)&&(this._tlasBounds=new Float32Array(e*6)),this._blasOffsetMap||=new Map,this._blasOffsetMap.clear();for(let e of this.instanceTable.entries)e&&this._blasOffsetMap.set(e.blasOffset,e);for(let t=e-1;t>=0;t--){let e=t*16,n=this.bvhData[e+3];if(n===-2){let n=this.bvhData[e],r=this._blasOffsetMap.get(n);if(r&&r.worldAABB){let e=t*6;this._tlasBounds[e]=r.worldAABB.minX,this._tlasBounds[e+1]=r.worldAABB.minY,this._tlasBounds[e+2]=r.worldAABB.minZ,this._tlasBounds[e+3]=r.worldAABB.maxX,this._tlasBounds[e+4]=r.worldAABB.maxY,this._tlasBounds[e+5]=r.worldAABB.maxZ}}else if(n>=0){let n=this.bvhData[e+3],r=this.bvhData[e+7],i=n*6,a=r*6,o=this._tlasBounds;this.bvhData[e]=o[i],this.bvhData[e+1]=o[i+1],this.bvhData[e+2]=o[i+2],this.bvhData[e+4]=o[i+3],this.bvhData[e+5]=o[i+4],this.bvhData[e+6]=o[i+5],this.bvhData[e+8]=o[a],this.bvhData[e+9]=o[a+1],this.bvhData[e+10]=o[a+2],this.bvhData[e+12]=o[a+3],this.bvhData[e+13]=o[a+4],this.bvhData[e+14]=o[a+5];let s=t*6;o[s]=Math.min(o[i],o[a]),o[s+1]=Math.min(o[i+1],o[a+1]),o[s+2]=Math.min(o[i+2],o[a+2]),o[s+3]=Math.max(o[i+3],o[a+3]),o[s+4]=Math.max(o[i+4],o[a+4]),o[s+5]=Math.max(o[i+5],o[a+5])}}}scheduleBackgroundRebuild(e,t){if(!this.instanceTable||!this.triangleData)return;let n=G.FLOATS_PER_TRIANGLE;this._rebuildGeneration++;let r=this._rebuildGeneration,i=(e,i,a)=>{let o=this.triangleData.slice(i.triOffset*n,(i.triOffset+i.triCount)*n);this._pendingRebuilds.set(e,a),a.onmessage=n=>{let o=n.data;if(a.terminate(),this._pendingRebuilds.delete(e),o.error){console.error(`Background BLAS rebuild error (mesh ${e}):`,o.error);return}r===this._rebuildGeneration&&this._swapBLAS(e,i,o,t)},a.onerror=t=>{console.error(`Background BLAS rebuild worker error (mesh ${e}):`,t),a.terminate(),this._pendingRebuilds.delete(e)};let s=i.triCount>500;a.postMessage({triangleData:o.buffer,triangleByteOffset:o.byteOffset,triangleByteLength:o.byteLength,triangleCount:i.triCount,depth:this.config.bvhDepth,reportProgress:!1,sharedReorderBuffer:null,treeletOptimization:{enabled:s,size:this.config.treeletSize,passes:this.config.treeletOptimizationPasses,minImprovement:this.config.treeletMinImprovement},reinsertionOptimization:{enabled:this.bvhBuilder.enableReinsertionOptimization,batchSizeRatio:this.bvhBuilder.reinsertionBatchSizeRatio,maxIterations:this.bvhBuilder.reinsertionMaxIterations}},[o.buffer])};for(let t of e){let e=this.instanceTable.entries[t];if(!e)continue;let n=this._pendingRebuilds.get(t);n&&n.terminate();let r;try{r=new Worker(Zr,{type:`module`}),i(t,e,r)}catch(n){if(n.name!==`SecurityError`)throw n;Je(Zr).then(n=>i(t,e,n))}}}_swapBLAS(e,t,n,r){let i=n.bvhData,a=i.length/16;if(a!==t.blasNodeCount){console.warn(`Background rebuild: node count mismatch for mesh ${e} (${a} vs ${t.blasNodeCount}), skipping swap`);return}let o=t.blasOffset*16;this.bvhData.set(i,o),this._offsetBLASInPlace(o,a,t.blasOffset,t.triOffset);let s=G.FLOATS_PER_TRIANGLE,c=n.triangles;c&&this.triangleData.set(c,t.triOffset*s);let l=n.originalToBvh;if(l){for(let e=0;e<t.triCount;e++)this.originalToBvhMap[t.triOffset+e]=t.triOffset+l[e];let e=new Uint32Array(t.triCount);for(let n=0;n<t.triCount;n++)e[l[n]]=n;t.bvhToOriginal=e}this.instanceTable.recomputeAABB(e,this.bvhData,this.triangleData),this._refitTLAS(),this._log(`Background BLAS rebuild complete for mesh ${e}`),r?.()}cancelBackgroundRebuilds(){for(let e of this._pendingRebuilds.values())e.terminate();this._pendingRebuilds.clear()}_disposeRefitWorker(){this._refitWorker&&=(this._refitWorker.terminate(),null),this._refitSharedBuffers=null,this.cancelBackgroundRebuilds()}dispose(){this._log(`Disposing resources`),this._disposeRefitWorker(),this._disposeTextures(),this._reset(),this.textureCreator&&=(this.textureCreator.dispose(),null),this.geometryExtractor=null,this.bvhBuilder=null,this.tlasBuilder=null,this._blasRefitter=null}},Pi=[[1619.19106,-.00205010916,5.02995757],[2488.45471,-.00111330907,3.22621544],[3341.43193,-.000486551192,1.76486769],[4094.61742,-.000127446582,.725731635],[4670.28036,291258199e-13,.126703442],[4595.09185,287495649e-13,.15034502],[3787.1745,935907826e-14,.399075871]],Fi=[[-488.999748,.000604330754,-.0755807526],[-755.994277,.000316730098,.478306139],[-1023.63977,.00012022347,.936662319],[-1265.71316,487340896e-14,1.27054498],[-1425.29332,-401150431e-13,1.43972784],[-1175.54822,-216378048e-13,1.30408023],[-500.799571,-459832026e-14,1.09098763]],Ii=[[596945309e-19,-4.85742887e-8,-970622247e-13,-.00407936148],[240430366e-19,5.55021075e-8,-.000198503712,.0289312858],[-140949732e-19,1.89878968e-7,-.000356632824,.0910767778],[-361460868e-19,2.84822009e-7,-.000493211319,.15672344],[-197075738e-19,1.75359352e-7,-.000250542825,-.0222783266],[-161997957e-21,-1.64216008e-8,.000386216271,-.738077418],[672650283e-21,-2.73078809e-8,.000424098264,-.752335691]];function Li(e){let t,n,r;if(e>=12e3)t=.8262954810464208,n=.9945080501520986,r=1.566307710274283;else if(e<800)t=5.413294490189271,n=-.20319390035873933,r=-.0822535242887164;else{let i=e>=6365?6:e>=3315?5:e>=1902?4:e>=1449?3:e>=1167?2:+(e>=965),a=Pi[i],o=Fi[i],s=Ii[i],c=1/e;t=a[0]*c+a[1]*e+a[2],n=o[0]*c+o[1]*e+o[2],r=((s[0]*e+s[1])*e+s[2])*e+s[3]}return[Math.max(0,t),Math.max(0,n),Math.max(0,r)]}var Ri=1/(4*Math.PI);function zi(e){let t=e.userData||{},n=Number.isFinite(t.exposure)?t.exposure:0,r=e.intensity*2**n,i=e.color.r,a=e.color.g,o=e.color.b;if(t.useTemperature){let[e,n,r]=Li(t.temperature??6500);i*=e,a*=n,o*=r}return{r:i,g:a,b:o,intensity:r}}var Bi=class{constructor(){this.lightData={directional:[],rectArea:[],point:[],spot:[]},this.directionalLightCache=[],this.areaLightCache=[],this.pointLightCache=[],this.spotLightCache=[]}clear(){this.lightData.directional=[],this.lightData.rectArea=[],this.lightData.point=[],this.lightData.spot=[],this.directionalLightCache=[],this.areaLightCache=[],this.pointLightCache=[],this.spotLightCache=[]}calculateLightImportance(e,t=`directional`){let n=.2126*e.color.r+.7152*e.color.g+.0722*e.color.b,r=e.intensity*n;if(t===`area`){let t=e.width*e.height;r*=Math.sqrt(t)}else if(t===`point`)r*=Math.sqrt(e.distance||100);else if(t===`spot`){let t=Math.sin(e.angle||Math.PI/4);r*=Math.sqrt(e.distance||100)*t}return r}addDirectionalLight(e){if(e.intensity<=0)return;e.updateMatrixWorld();let t=e.getWorldPosition(new r.Vector3),n;if(e.target){e.target.updateMatrixWorld();let i=e.target.getWorldPosition(new r.Vector3);n=t.sub(i).normalize()}else n=t.normalize();let i=this.calculateLightImportance(e,`directional`),a=e.userData.angle||e.angle||0,o=zi(e),s=e.userData?.gobo,c=s&&Number.isInteger(s.index)?s.index:-1,l=s&&typeof s.intensity==`number`?s.intensity:1,u=s&&s.inverted?-Math.abs(l):Math.abs(l),d=s&&typeof s.scale==`number`?s.scale:5;this.directionalLightCache.push({data:[n.x,n.y,n.z,o.r,o.g,o.b,o.intensity,a,c,u,d,0],importance:i,light:e})}addRectAreaLight(e){if(e.intensity<=0)return;e.updateMatrixWorld();let t=e.getWorldPosition(new r.Vector3),n=e.getWorldQuaternion(new r.Quaternion),i=e.getWorldScale(new r.Vector3),a=e.width*i.x*.5,o=e.height*i.y*.5,s=new r.Vector3(a,0,0).applyQuaternion(n),c=new r.Vector3(0,-o,0).applyQuaternion(n),l=this.calculateLightImportance(e,`area`),u=e.userData?.normalize??!0?1:0,d=Number.isFinite(e.userData?.spread)?e.userData.spread:Math.PI,f=+(e.userData?.shape===`ellipse`||e.userData?.shape===`disk`||e.userData?.shape===1),p=zi(e);this.areaLightCache.push({data:[t.x,t.y,t.z,s.x,s.y,s.z,c.x,c.y,c.z,p.r,p.g,p.b,p.intensity,u,d,f],importance:l,light:e})}addPointLight(e){if(e.intensity<=0)return;e.updateMatrixWorld();let t=e.getWorldPosition(new r.Vector3),n=this.calculateLightImportance(e,`point`),i=zi(e);this.pointLightCache.push({data:[t.x,t.y,t.z,i.r,i.g,i.b,i.intensity*Ri,e.distance||0,e.decay===void 0?2:e.decay],importance:n,light:e})}addSpotLight(e){if(e.intensity<=0)return;e.updateMatrixWorld();let t=e.getWorldPosition(new r.Vector3),n=(e.target?e.target.getWorldPosition(new r.Vector3):new r.Vector3(0,0,-1)).sub(t).normalize(),i=this.calculateLightImportance(e,`spot`),a=e.userData?.gobo,o=a&&Number.isInteger(a.index)?a.index:-1,s=a&&typeof a.intensity==`number`?a.intensity:1,c=a&&a.inverted?-Math.abs(s):Math.abs(s),l=e.userData?.ies,u=l&&Number.isInteger(l.index)?l.index:-1,d=l&&typeof l.intensity==`number`?l.intensity:1,f=zi(e);this.spotLightCache.push({data:[t.x,t.y,t.z,n.x,n.y,n.z,f.r,f.g,f.b,f.intensity*Ri,e.angle||Math.PI/4,e.penumbra||0,e.distance||0,e.decay===void 0?2:e.decay,o,c,u,d,0,0],importance:i,light:e})}preprocessLights(){this.directionalLightCache.sort((e,t)=>t.importance-e.importance),this.areaLightCache.sort((e,t)=>t.importance-e.importance),this.pointLightCache.sort((e,t)=>t.importance-e.importance),this.spotLightCache.sort((e,t)=>t.importance-e.importance),this.lightData.directional=[],this.lightData.rectArea=[],this.lightData.point=[],this.lightData.spot=[],this.directionalLightCache.forEach(e=>{this.lightData.directional.push(...e.data)}),this.areaLightCache.forEach(e=>{this.lightData.rectArea.push(...e.data)}),this.pointLightCache.forEach(e=>{this.lightData.point.push(...e.data)}),this.spotLightCache.forEach(e=>{this.lightData.spot.push(...e.data)}),this.areaLightCache.length>0&&console.log(`Preprocessed ${this.areaLightCache.length} area lights by importance`),this.pointLightCache.length>0&&console.log(`Preprocessed ${this.pointLightCache.length} point lights by importance`),this.spotLightCache.length>0&&console.log(`Preprocessed ${this.spotLightCache.length} spot lights by importance`)}updateShaderUniforms(e){let t=Math.floor(this.lightData.directional.length/12),n=Math.floor(this.lightData.rectArea.length/16),r=Math.floor(this.lightData.point.length/9),i=Math.floor(this.lightData.spot.length/20);e.defines.MAX_DIRECTIONAL_LIGHTS=t,e.defines.MAX_AREA_LIGHTS=n,e.defines.MAX_POINT_LIGHTS=r,e.defines.MAX_SPOT_LIGHTS=i,e.uniforms.directionalLights.value=new Float32Array(this.lightData.directional),e.uniforms.areaLights.value=new Float32Array(this.lightData.rectArea),e.uniforms.pointLights.value=new Float32Array(this.lightData.point),e.uniforms.spotLights.value=new Float32Array(this.lightData.spot),e.needsUpdate=!0}processSceneLights(e,t){this.clear(),e.traverse(e=>{e.isDirectionalLight?this.addDirectionalLight(e):e.isRectAreaLight?this.addRectAreaLight(e):e.isPointLight?this.addPointLight(e):e.isSpotLight&&this.addSpotLight(e)}),this.preprocessLights(),this.updateShaderUniforms(t)}getLightStatistics(){return{directionalLights:this.directionalLightCache.map(e=>({intensity:e.light.intensity,importance:e.importance,color:e.light.color})),areaLights:this.areaLightCache.map(e=>({intensity:e.light.intensity,importance:e.importance,color:e.light.color,size:e.light.width*e.light.height}))}}},Vi={stbnScalarAtlas:`https://assets.rayzee.atulmourya.com/noise/stbn_scalar_atlas.png`,stbnVec2Atlas:`https://assets.rayzee.atulmourya.com/noise/stbn_vec2_atlas.png`,ortRuntimeUrl:`https://cdn.jsdelivr.net/npm/onnxruntime-web@1.24.3/dist/ort.webgpu.bundle.min.mjs`,ortWasmPaths:`https://cdn.jsdelivr.net/npm/onnxruntime-web@1.24.3/dist/`,dracoDecoderPath:`https://www.gstatic.com/draco/v1/decoders/`,ktx2TranscoderPath:`https://cdn.jsdelivr.net/npm/three@0.183.2/examples/jsm/libs/basis/`,oidnWeightsBaseUrl:`https://cdn.jsdelivr.net/npm/denoiser/tzas/`,upscalerModelBaseUrl:`https://huggingface.co/notaneimu/onnx-image-models/resolve/main/`,cacheNamespace:`rayzee`};function Hi(e){e&&Object.assign(Vi,e)}function Ui(){return{...Vi}}var Wi=4,Gi=class extends ge{constructor(e,t,n,i={}){super(`PathTracer`,{...i,executionMode:he.ALWAYS});let a=i.width||1920,o=i.height||1080;this.camera=n,this.width=a,this.height=o,this.renderer=e,this.scene=t,this.sdfs=new Ni,this.lightSerializer=new Bi,this.accumulationEnabled=!0,this.isComplete=!1,this.cameras=[],this.performanceMonitor=z(),this.completionThreshold=0,this.renderLimitMode=`frames`,this._initDataTextures(),this.storageTextures=new Be(0,0),this.uniforms=new q(a,o),this._defineUniformGetters(),this.materialData=new qe(this.sdfs),this.materialData.callbacks.onReset=()=>this.reset(),this.materialData.callbacks.getTriangleData=()=>({array:this.triangleStorageAttr?.array,count:this.triangleCount}),this.materialData.callbacks.onTriangleDataChanged=()=>{this.triangleStorageAttr&&(this.triangleStorageAttr.needsUpdate=!0)},this.environment=new Ct(this.scene,this.uniforms),this.environment.callbacks.onReset=()=>this.reset(),this.environment.callbacks.getSceneTextureNodes=()=>this.shaderBuilder.getSceneTextureNodes(),this.shaderBuilder=new qr,this._initRenderingState(),this.setupBlueNoise(),this.tempVector2=new r.Vector2,this.lastCameraMatrix=new r.Matrix4,this.lastProjectionMatrix=new r.Matrix4,this.lastRenderMode=-1,this.renderModeChangeTimeout=null,this.renderModeChangeDelay=50,this.pendingRenderMode=null,this.lastInteractionModeState=!1,this.cameraChanged=!1,this.updateCompletionThreshold()}_initDataTextures(){this.triangleStorageAttr=null,this.triangleStorageNode=null,this.triangleCount=0,this.bvhStorageAttr=null,this.bvhStorageNode=null,this.bvhNodeCount=0,this.directionalLightsData=null,this.pointLightsData=null,this.spotLightsData=null,this.areaLightsData=null,this.goboMaps=null,this.iesProfiles=null,this.stbnScalarTexture=null,this.stbnVec2Texture=null,this.lightStorageAttr=new t.StorageInstancedBufferAttribute(new Float32Array(16),4),this.lightStorageNode=(0,n.storage)(this.lightStorageAttr,`vec4`,1).toReadOnly(),this._lbvhDataCache=null,this._emissiveDataCache=null,this._bitTrailMapCache=null,this._instanceTable=null,this.spheres=[]}_defineUniformGetters(){let e=this.uniforms;for(let t of e.keys())Object.defineProperty(this,t,{get:()=>e.get(t),configurable:!0});let t=e.getLightBufferNodes();for(let[e,n]of Object.entries(t))Object.defineProperty(this,`${e}LightsBufferNode`,{get:()=>n,configurable:!0})}_initRenderingState(){this.isReady=!1,this.frameCount=0}_initCameraOptimizer(){let e=this;this.cameraOptimizer=new _e(this.renderer,{uniforms:{maxBounceCount:{get value(){return e.maxBounces.value},set value(t){e.maxBounces.value=t}},useEnvMapIS:{get value(){return e.useEnvMapIS.value},set value(t){e.useEnvMapIS.value=t}},enableAccumulation:{get value(){return e.enableAccumulation.value},set value(t){e.enableAccumulation.value=t}},enableEmissiveTriangleSampling:{get value(){return e.enableEmissiveTriangleSampling.value},set value(t){e.enableEmissiveTriangleSampling.value=t}},cameraIsMoving:{get value(){return e.cameraIsMoving.value},set value(t){e.cameraIsMoving.value=t}}}},{enabled:B.interactionModeEnabled,qualitySettings:{maxBounceCount:1,useEnvMapIS:!1,enableAccumulation:!1,enableEmissiveTriangleSampling:!1},onReset:()=>{this.reset(),this.emit(`pathtracer:viewpointChanged`)}})}setupBlueNoise(){let e=new r.TextureLoader;e.setCrossOrigin(`anonymous`);let t=e=>(e.minFilter=r.NearestFilter,e.magFilter=r.NearestFilter,e.wrapS=r.RepeatWrapping,e.wrapT=r.RepeatWrapping,e.generateMipmaps=!1,e),{stbnScalarAtlas:n,stbnVec2Atlas:i}=Ui();e.load(n,e=>{this.stbnScalarTexture=t(e),N.value=e,console.log(`PathTracer: STBN scalar atlas loaded ${e.image.width}x${e.image.height}`)}),e.load(i,e=>{this.stbnVec2Texture=t(e),P.value=e,console.log(`PathTracer: STBN vec2 atlas loaded ${e.image.width}x${e.image.height}`)})}setupEventListeners(){this.on(`pipeline:reset`,()=>{this.reset()}),this.on(`pipeline:resize`,e=>{e&&e.width&&e.height&&this.setSize(e.width,e.height)}),this.on(`pathtracer:setCompletionThreshold`,e=>{e&&e.threshold!==void 0&&(this.completionThreshold=e.threshold)})}async build(e){this.dispose(),this.scene=e,await this.sdfs.buildBVH(e),this.cameras=this.sdfs.cameras,this.materialData.injectMaterialFeatureDefines(),this.updateSceneUniforms(),this.updateLights(),this._initCameraOptimizer(),this.setupMaterial()}updateSceneUniforms(){this.setTriangleData(this.sdfs.triangleData,this.sdfs.triangleCount),this.setBVHData(this.sdfs.bvhData),this.setInstanceTable(this.sdfs.instanceTable),this.materialData.setMaterialData(this.sdfs.materialData),this.materialData.loadTexturesFromSdfs(),this.sdfs.emissiveTriangleData?this.setEmissiveTriangleData(this.sdfs.emissiveTriangleData,this.sdfs.emissiveTriangleCount||0):this.emissiveTriangleCount.value=0,this.sdfs.lightBVHNodeData?this.setLightBVHData(this.sdfs.lightBVHNodeData,this.sdfs.lightBVHNodeCount||0):this.lightBVHNodeCount.value=0,this._meshRefs=this._collectMeshRefs(this.scene),this.setMeshVisibilityData(this._meshRefs),this.spheres=this.sdfs.spheres||[]}updateLights(){let e={uniforms:{directionalLights:{value:null},pointLights:{value:null},spotLights:{value:null},areaLights:{value:null}},defines:{}};if(this.lightSerializer.processSceneLights(this.scene,e),this.directionalLightsData=e.uniforms.directionalLights.value,this.pointLightsData=e.uniforms.pointLights.value,this.spotLightsData=e.uniforms.spotLights.value,this.areaLightsData=e.uniforms.areaLights.value,this.hasSun.value){let t=this.environment.envParams.skySunIntensity*950,n={intensity:t,color:{r:1,g:1,b:1},userData:{angle:this.sunAngularSize.value},updateMatrixWorld:()=>{},getWorldPosition:e=>{let t=this.sunDirection.value;return e.set(t.x,t.y,t.z).multiplyScalar(1e10)}};this.lightSerializer.addDirectionalLight(n),this.lightSerializer.preprocessLights(),this.lightSerializer.updateShaderUniforms(e),this.directionalLightsData=e.uniforms.directionalLights.value,console.log(`Sun added as directional light (intensity: ${t.toFixed(2)})`)}this._updateLightBufferNodes()}_updateLightBufferNodes(){this.directionalLightsData&&this.directionalLightsData.length>0?(this.directionalLightsBufferNode.array=Array.from(this.directionalLightsData),this.numDirectionalLights.value=Math.floor(this.directionalLightsData.length/12)):this.numDirectionalLights.value=0,this.areaLightsData&&this.areaLightsData.length>0?(this.areaLightsBufferNode.array=Array.from(this.areaLightsData),this.numAreaLights.value=Math.floor(this.areaLightsData.length/16)):this.numAreaLights.value=0,this.pointLightsData&&this.pointLightsData.length>0?(this.pointLightsBufferNode.array=Array.from(this.pointLightsData),this.numPointLights.value=Math.floor(this.pointLightsData.length/9)):this.numPointLights.value=0,this.spotLightsData&&this.spotLightsData.length>0?(this.spotLightsBufferNode.array=Array.from(this.spotLightsData),this.numSpotLights.value=Math.floor(this.spotLightsData.length/20)):this.numSpotLights.value=0}reset(){this.frameCount=0,this.frame.value=0,this.hasPreviousAccumulated.value=0,this.storageTextures.currentTarget=0,this.updateCompletionThreshold(),this.isComplete=!1,this.performanceMonitor?.reset(),this.lastRenderMode=-1,this.lastInteractionModeState=!1}setSize(e,t){this.width=e,this.height=t,this.resolution.value.set(e,t),this.createStorageTextures(e,t)}setAccumulationEnabled(e){this.accumulationEnabled=e,this.enableAccumulation.value=+!!e}enterInteractionMode(){this.cameraOptimizer?.enterInteractionMode()}setInteractionModeEnabled(e){this.cameraOptimizer?.setInteractionModeEnabled(e)}get interactionMode(){return this.cameraOptimizer?.isInInteractionMode()??!1}setTriangleData(e,r){if(!e)return;let i=e.length/4;this.triangleStorageNode?(this.triangleStorageAttr=new t.StorageInstancedBufferAttribute(e,4),this.triangleStorageNode.value=this.triangleStorageAttr,this.triangleStorageNode.bufferCount=i):(this.triangleStorageAttr=new t.StorageInstancedBufferAttribute(e,4),this.triangleStorageNode=(0,n.storage)(this.triangleStorageAttr,`vec4`,i).toReadOnly()),this.triangleCount=r,console.log(`PathTracer: ${this.triangleCount} triangles (storage buffer)`)}setBVHData(e){if(!e)return;let r=e.length/4;this.bvhStorageNode?(this.bvhStorageAttr=new t.StorageInstancedBufferAttribute(e,4),this.bvhStorageNode.value=this.bvhStorageAttr,this.bvhStorageNode.bufferCount=r):(this.bvhStorageAttr=new t.StorageInstancedBufferAttribute(e,4),this.bvhStorageNode=(0,n.storage)(this.bvhStorageAttr,`vec4`,r).toReadOnly()),this.bvhNodeCount=Math.floor(r/Wi),console.log(`PathTracer: ${this.bvhNodeCount} BVH nodes (storage buffer)`)}setInstanceTable(e){this._instanceTable=e}setMeshVisibilityData(e){if(!(!e||e.length===0||!this._instanceTable)){for(let t=0;t<e.length;t++)this._patchTLASLeafVisibility(t,this._isWorldVisible(e[t]));this.bvhStorageAttr&&(this.bvhStorageAttr.needsUpdate=!0)}}updateMeshVisibility(e,t){this._patchTLASLeafVisibility(e,t)&&this.bvhStorageAttr&&(this.bvhStorageAttr.needsUpdate=!0)}updateAllMeshVisibility(){if(!(!this._meshRefs||!this._instanceTable)){for(let e=0;e<this._meshRefs.length;e++)this._patchTLASLeafVisibility(e,this._isWorldVisible(this._meshRefs[e]));this.bvhStorageAttr&&(this.bvhStorageAttr.needsUpdate=!0)}}_patchTLASLeafVisibility(e,t){let n=this._instanceTable?.entries?.[e];return!n||n.tlasLeafIndex<0||!this.bvhStorageAttr?!1:(n.visible=t,this.bvhStorageAttr.array[n.tlasLeafIndex*16+2]=+!!t,!0)}_collectMeshRefs(e){if(!e)return[];let t=[];return e.traverse(e=>{e.isMesh&&e.userData.meshIndex!==void 0&&(t[e.userData.meshIndex]=e)}),t}_isWorldVisible(e){for(;e;){if(!e.visible)return!1;e=e.parent}return!0}_updateStorageBuffer(e,t){e&&(e.array.set(t),e.needsUpdate=!0)}updateTriangleData(e){this._updateStorageBuffer(this.triangleStorageAttr,e)}updateBVHData(e){this._updateStorageBuffer(this.bvhStorageAttr,e)}updateBufferRanges(e,t){if(this.triangleStorageAttr&&e.length>0){this.triangleStorageAttr.clearUpdateRanges();for(let t of e)this.triangleStorageAttr.addUpdateRange(t.offset,t.count);this.triangleStorageAttr.version++}if(this.bvhStorageAttr&&t.length>0){this.bvhStorageAttr.clearUpdateRanges();for(let e of t)this.bvhStorageAttr.addUpdateRange(e.offset,e.count);this.bvhStorageAttr.version++}}createStorageTextures(e,t){this.storageTextures.writeColor?this.storageTextures.setSize(e,t):this.storageTextures.create(e,t),this.resolution.value.set(e,t)}setupMaterial(){if(this.cameraOptimizer||this._initCameraOptimizer(),!this.triangleStorageNode){console.error(`PathTracer: Triangle data required`);return}if(!this.bvhStorageNode){console.error(`PathTracer: BVH data required`);return}if(this.isReady&&this.shaderBuilder.getSceneTextureNodes()){this.shaderBuilder.updateSceneTextures(this);return}this._ensureStorageTextures(),this.shaderBuilder.createSceneTextureNodes(this,this.storageTextures),this.isReady=!0}_ensureStorageTextures(){let e=this.renderer.domElement,t=Math.max(1,e.width||this.width),n=Math.max(1,e.height||this.height);this.storageTextures.ensureSize(t,n)&&this.resolution.value.set(t,n)}_handleResize(){let{width:e,height:t}=this.renderer.domElement;(e!==this.storageTextures.renderWidth||t!==this.storageTextures.renderHeight)&&(this.createStorageTextures(e,t),this.frameCount=0),this.resolution.value.set(e,t)}_matricesApproxEqual(e,t,n=1e-10){let r=e.elements,i=t.elements;for(let e=0;e<16;e++)if(Math.abs(r[e]-i[e])>n)return!1;return!0}_updateCameraUniforms(){return!this._matricesApproxEqual(this.lastCameraMatrix,this.camera.matrixWorld)||!this._matricesApproxEqual(this.lastProjectionMatrix,this.camera.projectionMatrixInverse)?(this.cameraWorldMatrix.value.copy(this.camera.matrixWorld),this.cameraViewMatrix.value.copy(this.camera.matrixWorldInverse),this.cameraProjectionMatrix.value.copy(this.camera.projectionMatrix),this.cameraProjectionMatrixInverse.value.copy(this.camera.projectionMatrixInverse),this.lastCameraMatrix.copy(this.camera.matrixWorld),this.lastProjectionMatrix.copy(this.camera.projectionMatrixInverse),!0):!1}_updateAccumulationUniforms(e,t){let n=this.cameraOptimizer?.isInInteractionMode()??!1;this.lastInteractionModeState=n,this.accumulationEnabled?n?(this.accumulationAlpha.value=1,this.hasPreviousAccumulated.value=0):(this.accumulationAlpha.value=Oe(e),this.hasPreviousAccumulated.value=+(e>0)):(this.accumulationAlpha.value=1,this.hasPreviousAccumulated.value=0)}_publishTexturesToContext(e,t){e.setTexture(`pathtracer:color`,t.color),e.setTexture(`pathtracer:normalDepth`,t.normalDepth),e.setTexture(`pathtracer:albedo`,t.albedo),e.setState(`interactionMode`,this.cameraOptimizer?.isInInteractionMode()??!1),e.setState(`renderMode`,this.renderMode.value)}_emitStateEvents(){this.emit(`pathtracer:frameComplete`,{frame:this.frameCount,isComplete:this.isComplete}),this.cameraChanged&&=(this.emit(`camera:moved`),!1)}updateCompletionThreshold(){let e=this.renderMode.value,t=this.maxSamples.value;this.renderLimitMode===`time`?this.completionThreshold=1/0:this.completionThreshold=De(e,t)}setRenderLimitMode(e){this.renderLimitMode=e,this.updateCompletionThreshold()}manageASVGFForRenderMode(e){e!==this.lastRenderMode&&(this.renderModeChangeTimeout&&clearTimeout(this.renderModeChangeTimeout),this.pendingRenderMode=e,this.renderModeChangeTimeout=setTimeout(()=>{this.pendingRenderMode!==null&&this.pendingRenderMode!==this.lastRenderMode&&(this.lastRenderMode=this.pendingRenderMode,this._onRenderModeChanged(this.pendingRenderMode)),this.renderModeChangeTimeout=null,this.pendingRenderMode=null},this.renderModeChangeDelay)),this._handleFullQuadASVGF()}_onRenderModeChanged(e){e===1?this.emit(`asvgf:updateParameters`,{enableDebug:!1,temporalAlpha:.15}):this.emit(`asvgf:updateParameters`,{temporalAlpha:.1}),this.emit(`asvgf:reset`)}_handleFullQuadASVGF(){this.emit(`asvgf:setTemporal`,{enabled:!0})}setUniform(e,t){this.uniforms.set(e,t)}setBlueNoiseTexture(e){this.stbnScalarTexture=e,e&&(N.value=e)}_rebuildLightBuffer(){let e=this._lbvhDataCache,n=this._emissiveDataCache,r=this._bitTrailMapCache,i=e?e.length:0,a=n?n.length:0,o=r?Math.ceil(r.length/4)*4:0,s=Math.max(i+a+o,4),c=new Float32Array(s);e&&c.set(e,0),n&&c.set(n,i),r&&c.set(r,i+a),this.lightStorageAttr=new t.StorageInstancedBufferAttribute(c,4),this.lightStorageNode.value=this.lightStorageAttr,this.lightStorageNode.bufferCount=c.length/4,this.emissiveVec4Offset.value=(this.lightBVHNodeCount.value||0)*4,this.reverseMapVec4Offset.value=(i+a)/4}setEmissiveTriangleData(e,t,n=0,r=null){e&&(this._emissiveDataCache=e,r&&(this._bitTrailMapCache=r),this.emissiveTriangleCount.value=t,this.emissiveTotalPower.value=n,this._rebuildLightBuffer(),console.log(`PathTracer: ${t} emissive triangles, totalPower=${n.toFixed(4)} (storage buffer)`))}setLightBVHData(e,t){e&&(this._lbvhDataCache=e,this.lightBVHNodeCount.value=t,this._rebuildLightBuffer(),console.log(`PathTracer: Light BVH ${t} nodes`))}updateUniforms(e){let t=!1;for(let[n,r]of Object.entries(e))this[n]&&this[n].value!==void 0&&this[n].value!==r&&(this[n].value=r,t=!0);t&&this.reset()}async rebuildMaterials(e){if(!this.sdfs)throw Error(`Scene not built yet. Call build() first.`);try{console.log(`PathTracer: Starting material rebuild...`),await this.sdfs.rebuildMaterials(e),this.updateSceneUniforms(),this.shaderBuilder.updateSceneTextures(this),this.updateLights(),this.reset(),console.log(`PathTracer materials rebuilt successfully`)}catch(e){console.error(`Error rebuilding PathTracer materials:`,e);try{console.warn(`Attempting recovery by resetting path tracer...`),this.reset()}catch(e){console.error(`Recovery failed:`,e)}throw e}}dispose(){this.renderModeChangeTimeout&&=(clearTimeout(this.renderModeChangeTimeout),null),this.cameraOptimizer?.dispose(),this.materialData?.dispose(),this.environment?.dispose(),this.shaderBuilder?.dispose(),this.uniforms?.dispose(),this.storageTextures?.dispose(),this.stbnScalarTexture?.dispose(),this.stbnVec2Texture?.dispose(),this.placeholderTexture?.dispose(),this.triangleStorageAttr=null,this.triangleStorageNode=null,this.bvhStorageAttr=null,this.bvhStorageNode=null,this.placeholderTexture=null,this.isReady=!1}},Ki={ORIGIN_META:0,DIR_FLAGS:1,THROUGHPUT_PDF:2,RADIANCE_ALPHA:3,MEDIUM_STACK:4,MEDIUM_SIGMA_A:5,SSS_SIGMA_S:6},qi={DIST_TRI_BARY:0,NORMAL_MAT:1},Ji=0,$=(e,t)=>t===0?e:e.add(t*Ji),Yi=class{static requiredCapacity(e){return Math.ceil(e*1.25)}constructor(e=0){this.capacity=0,this._attrs={},this.rayBuffer=null,this.rngBuffer=null,this.hitBuffer=null,e>0&&this.allocate(e)}allocate(e){this.dispose();let r=Math.ceil(e*1.25);this.capacity=r,Ji=r;let i=r*7,a=new t.StorageInstancedBufferAttribute(new Float32Array(i*4),4);this._attrs.ray=a,this.rayBuffer={rw:(0,n.storage)(a,`vec4`),ro:(0,n.storage)(a,`vec4`).toReadOnly()};let o=new t.StorageInstancedBufferAttribute(new Uint32Array(r),1);this._attrs.rng=o,this.rngBuffer={rw:(0,n.storage)(o,`uint`),ro:(0,n.storage)(o,`uint`).toReadOnly()};let s=r*2,c=new t.StorageInstancedBufferAttribute(new Float32Array(s*4),4);this._attrs.hit=c,this.hitBuffer={rw:(0,n.storage)(c,`vec4`),ro:(0,n.storage)(c,`vec4`).toReadOnly()};let l=(i*16+r*4+s*16)/(1024*1024);console.log(`PackedRayBuffer: capacity=${r}, total=${l.toFixed(1)} MB (ray=${(i*16/1048576).toFixed(0)}MB hit=${(s*16/1048576).toFixed(0)}MB) [SoA ray/hit]`)}resize(e){return Math.ceil(e*1.25)<=this.capacity&&this.capacity>0?!1:(this.allocate(e),!0)}dispose(){this._attrs={},this.rayBuffer=null,this.rngBuffer=null,this.hitBuffer=null,this.capacity=0}},Xi=(e,t)=>e.element($(t,Ki.ORIGIN_META)).xyz,Zi=(e,t)=>e.element($(t,Ki.DIR_FLAGS)).xyz,Qi=(e,t)=>(0,n.floatBitsToUint)(e.element($(t,Ki.DIR_FLAGS)).w),$i=(e,t)=>e.element($(t,Ki.THROUGHPUT_PDF)).xyz,ea=(e,t)=>e.element($(t,Ki.THROUGHPUT_PDF)).w,ta=(e,t)=>e.element($(t,Ki.RADIANCE_ALPHA)),na=e=>(0,n.uint)(e).mul(1),ra=(e,t,r,i,a)=>e.element(na(t)).assign((0,n.uvec4)((0,n.packSnorm2x16)((0,n.vec2)(r.x,r.y)),(0,n.packSnorm2x16)((0,n.vec2)(r.z,i)),(0,n.packUnorm2x16)((0,n.vec2)(a.x,a.y)),(0,n.packUnorm2x16)((0,n.vec2)(a.z,0)))),ia=(e,t)=>e.element(na(t)),aa=e=>{let t=(0,n.unpackSnorm2x16)(e.x),r=(0,n.unpackSnorm2x16)(e.y);return(0,n.vec4)((0,n.vec3)(t.x,t.y,r.x).mul(.5).add(.5),r.y)},oa=e=>(0,n.vec3)((0,n.unpackUnorm2x16)(e.z),(0,n.unpackUnorm2x16)(e.w).x),sa=(e,t,r,i,a)=>e.element($(t,Ki.ORIGIN_META)).assign((0,n.vec4)(r,(0,n.uintBitsToFloat)((0,n.uint)(i).bitOr((0,n.uint)(a).shiftLeft(8))))),ca=(e,t,r,i)=>e.element($(t,Ki.DIR_FLAGS)).assign((0,n.vec4)(r,(0,n.uintBitsToFloat)(i))),la=(e,t,r,i)=>e.element($(t,Ki.THROUGHPUT_PDF)).assign((0,n.vec4)(r,i)),ua=(e,t,n)=>e.element($(t,Ki.RADIANCE_ALPHA)).assign(n),da=(e,t)=>e.element($(t,qi.DIST_TRI_BARY)).x,fa=(e,t)=>(0,n.floatBitsToUint)(e.element($(t,qi.DIST_TRI_BARY)).y),pa=(e,t)=>e.element($(t,qi.DIST_TRI_BARY)).zw,ma=(e,t)=>e.element($(t,qi.NORMAL_MAT)).xyz,ha=(e,t)=>(0,n.uint)((0,n.floatBitsToUint)(e.element($(t,qi.NORMAL_MAT)).w).bitAnd(65535)),ga=(e,t,r,i,a,o,s,c,l)=>{e.element($(t,qi.DIST_TRI_BARY)).assign((0,n.vec4)(r,(0,n.uintBitsToFloat)(i),a,o)),e.element($(t,qi.NORMAL_MAT)).assign((0,n.vec4)(s,(0,n.uintBitsToFloat)(c.bitOr(l.shiftLeft(16)))))},_a=(e,t)=>{let r=e.element($(t,Ki.MEDIUM_STACK)),i=(0,n.floatBitsToUint)(r.x);return{stackDepth:i.bitAnd(255),transTraversals:i.shiftRight(8).bitAnd(255),wavelength:i.shiftRight(16).bitAnd(65535),ior1:r.y,ior2:r.z,ior3:r.w}},va=(e,t,r,i,a,o,s,c=(0,n.uint)(0))=>e.element($(t,Ki.MEDIUM_STACK)).assign((0,n.vec4)((0,n.uintBitsToFloat)(r.bitOr(i.shiftLeft(8)).bitOr(c.shiftLeft(16))),a,o,s)),ya=(e,t)=>e.element($(t,Ki.MEDIUM_SIGMA_A)).xyz,ba=(e,t,r)=>e.element($(t,Ki.MEDIUM_SIGMA_A)).assign((0,n.vec4)(r,0)),xa=(e,t)=>(0,n.int)((0,n.floatBitsToUint)(e.element($(t,Ki.ORIGIN_META)).w).bitAnd(255)),Sa=(e,t)=>(0,n.int)((0,n.floatBitsToUint)(e.element($(t,Ki.ORIGIN_META)).w).shiftRight(8).bitAnd(255)),Ca=(e,t)=>{let n=e.element($(t,Ki.SSS_SIGMA_S));return{sigmaS:n.xyz,g:n.w}},wa=(e,t,r,i)=>e.element($(t,Ki.SSS_SIGMA_S)).assign((0,n.vec4)(r,i)),Ta={ACTIVE_RAY_COUNT:0,ENTERING_COUNT:1,COUNT:2},Ea={BOUNCE_MASK:255,ACTIVE:256,SPECULAR:512,INSIDE_MEDIUM:1024,RAY_TYPE_SHIFT:11,RAY_TYPE_MASK:63488,HAS_HIT_OPAQUE:65536,AUX_LOCKED:1<<17,REDIRECTED:1<<18},Da=class{constructor(e=0){this.capacity=0,this.counters=null,this.activeIndices=null,this.activeIndicesRO=null,this.sortedIndices=null,this.sortedIndicesRO=null,this.sortGlobalHistogram=null,this.pingPong=0,e>0&&this.allocate(e)}allocate(e){this.dispose(),this.capacity=e,this._countersAttr=new t.StorageInstancedBufferAttribute(new Uint32Array(Ta.COUNT),1),this.counters=(0,n.storage)(this._countersAttr,`uint`).toAtomic(),this.MAX_BOUNCE_SNAPSHOTS=32,this._bounceCountsAttr=new t.StorageInstancedBufferAttribute(new Uint32Array(this.MAX_BOUNCE_SNAPSHOTS),1),this.bounceCounts=(0,n.storage)(this._bounceCountsAttr,`uint`);let r=new t.StorageInstancedBufferAttribute(new Uint32Array(e),1),i=new t.StorageInstancedBufferAttribute(new Uint32Array(e),1);this._attrA=r,this._attrB=i,this.activeIndices={a:(0,n.storage)(r,`uint`),b:(0,n.storage)(i,`uint`)},this.activeIndicesRO={a:(0,n.storage)(r,`uint`).toReadOnly(),b:(0,n.storage)(i,`uint`).toReadOnly()};let a=new t.StorageInstancedBufferAttribute(new Uint32Array(e),1);this._sortAttr=a,this.sortedIndices=(0,n.storage)(a,`uint`),this.sortedIndicesRO=(0,n.storage)(a,`uint`).toReadOnly(),this._sortGlobalHistAttr=new t.StorageInstancedBufferAttribute(new Uint32Array(256),1),this.sortGlobalHistogram=(0,n.storage)(this._sortGlobalHistAttr,`uint`).toAtomic(),this.pingPong=0;let o=Ta.COUNT*4+e*4*3;console.log(`QueueManager: Allocated capacity=${e}, total=${(o/(1024*1024)).toFixed(1)} MB`)}resize(e){return e<=this.capacity&&this.capacity>0?!1:(this.allocate(e),!0)}getCounters(){return this.counters}getActiveReadRO(){return this.pingPong===0?this.activeIndicesRO.a:this.activeIndicesRO.b}getActiveRead(){return this.pingPong===0?this.activeIndices.a:this.activeIndices.b}getActiveWrite(){return this.pingPong===0?this.activeIndices.b:this.activeIndices.a}getSortedRW(){return this.sortedIndices}getSortedRO(){return this.sortedIndicesRO}getSortGlobalHistogram(){return this.sortGlobalHistogram}getCountersAttribute(){return this._countersAttr}getBounceCounts(){return this.bounceCounts}getBounceCountsAttribute(){return this._bounceCountsAttr}swap(){this.pingPong=1-this.pingPong}resetPingPong(){this.pingPong=0}dispose(){this.counters=null,this.activeIndices=null,this.activeIndicesRO=null,this.sortedIndices=null,this.sortedIndicesRO=null,this.sortGlobalHistogram=null,this.capacity=0}},Oa={[r.RGBAFormat]:4,[r.RGBFormat]:3,[r.RGFormat]:2,[r.RedFormat]:1},ka={[r.FloatType]:4,[r.HalfFloatType]:2,[r.UnsignedByteType]:1,[r.ByteType]:1,[r.UnsignedShortType]:2,[r.ShortType]:2,[r.UnsignedIntType]:4,[r.IntType]:4};function Aa(e){return(Oa[e.format]??4)*(ka[e.type]??4)}function ja(e){return e?.array?.byteLength||0}function Ma(e){if(!e)return 0;if(e.isRenderTarget){let t=e.textures?.length?e.textures:[e.texture],n=e.width||0,r=e.height||0,i=e.depth||1,a=0;for(let e of t)e&&(a+=n*r*i*Aa(e));return a}let t=e.image||{},n=t.width??e.width??0,r=t.height??e.height??0,i=t.depth??1;return n*r*i*Aa(e)}var Na=class{constructor(e=null){this._providers=[],this._renderer=e,this.current=0,this.peak=0,this.byCategory={}}setRenderer(e){this._renderer=e}register(e,t){this._providers.push({category:e,fn:t})}measure(){let e=new WeakSet,t={},n=0;for(let{category:r,fn:i}of this._providers){let a;try{a=i()}catch{a=null}if(!a)continue;let o=0;for(let t of Array.isArray(a)?a:[a])o+=this._resourceBytes(t,e);t[r]=(t[r]||0)+o,n+=o}return this.byCategory=t,this.current=n,n>this.peak&&(this.peak=n),{current:n,peak:this.peak,byCategory:t}}_resourceBytes(e,t){return e?typeof e.bytes==`number`&&!e.isTexture&&!e.isRenderTarget?e.bytes:e.array&&e.array.byteLength!=null?t.has(e.array)?0:(t.add(e.array),e.array.byteLength):e.isRenderTarget||e.isTexture?t.has(e)?0:(t.add(e),this._residentTextureBytes(e)):0:0}_isResident(e){let t=this._renderer?.backend;if(!t||typeof t.get!=`function`)return!0;if(typeof t.has==`function`&&!t.has(e))return!1;let n=t.get(e);return!!(n&&(n.texture||n.gpuTexture))}_residentTextureBytes(e){if(e.isRenderTarget){let t=e.textures?.length?e.textures:[e.texture],n=e.width||0,r=e.height||0,i=e.depth||1,a=0;for(let e of t)e&&this._isResident(e)&&(a+=n*r*i*Aa(e));return a}return this._isResident(e)?Ma(e):0}resetPeak(){this.peak=this.current}getReport(){let e=e=>(e/1048576).toFixed(1),t=Object.entries(this.byCategory).map(([t,n])=>`${t}=${e(n)}`);return`VRAM current=${e(this.current)}MB peak=${e(this.peak)}MB [${t.join(` `)}]`}},Pa={generate:[16,16,1],extend:[256,1,1],shade:[256,1,1],connect:[256,1,1],accumulate:[256,1,1],compact:[256,1,1],resetCounters:[1,1,1],finalWrite:[16,16,1]},Fa=class{constructor(e){this.renderer=e,this.kernels=new Map,this.workgroupSizes=new Map,this.timing=new Map,this.profiling=!1,this.profile=new Map;for(let[e,t]of Object.entries(Pa))this.workgroupSizes.set(e,t)}register(e,t){this.kernels.set(e,t),this.timing.set(e,{compiledOnce:!1,lastDispatchMs:0})}dispatch(e){let t=this.kernels.get(e);if(!t)throw Error(`KernelManager: Unknown kernel '${e}'`);let n=this.timing.get(e);if(n&&!n.compiledOnce){let r=performance.now();this.renderer.compute(t);let i=performance.now();n.compiledOnce=!0,n.lastDispatchMs=i-r,console.log(`[Wavefront] Kernel '${e}' first dispatch (includes compilation): ${(i-r).toFixed(1)}ms`)}else if(this.profiling){let n=performance.now();this.renderer.compute(t);let r=performance.now(),i=this.profile.get(e);i||(i={calls:0,totalMs:0},this.profile.set(e,i)),i.calls++,i.totalMs+=r-n}else this.renderer.compute(t)}setDispatchCount(e,t){let n=this.kernels.get(e);n&&(n.dispatchSize=t)}calcScreenDispatch(e,t,n){let r=this.workgroupSizes.get(n)||[16,16,1];return[Math.ceil(e/r[0]),Math.ceil(t/r[1]),1]}calcRayDispatch(e,t){let n=this.workgroupSizes.get(t)||[256,1,1];return[Math.ceil(e/n[0]),1,1]}getWorkgroupSize(e){return this.workgroupSizes.get(e)||[256,1,1]}has(e){return this.kernels.has(e)}get(e){return this.kernels.get(e)}getTimingReport(){let e={};for(let[t,n]of this.timing)e[t]={...n};return e}enableProfiling(e){this.profiling=e,e&&this.profile.clear()}getProfileReport(){let e=[],t=0;for(let[n,{calls:r,totalMs:i}]of this.profile)t+=i,e.push({name:n,calls:r,totalMs:+i.toFixed(2),avgMs:+(i/r).toFixed(3)});return e.sort((e,t)=>t.totalMs-e.totalMs),e.push({name:`TOTAL`,calls:e.reduce((e,t)=>e+t.calls,0),totalMs:+t.toFixed(2),avgMs:null}),e}dispose(){this.kernels.clear(),this.timing.clear(),this.profile.clear()}},Ia=32,La=512,Ra=4,za=8,Ba=1e8,Va=()=>(0,n.array)(`int`,Ia).toVar(),Ha=(0,n.wgslFn)(`
|
|
494
|
+
`),qr=class{constructor(){this.prevColorTexNode=null,this.prevAlbedoTexNode=null,this._sceneTextureNodes=null}updateSceneTextures(e){let t=this._sceneTextureNodes,n=e.environment,r=e.materialData;n.environmentTexture&&t.envTex&&(t.envTex.value=n.environmentTexture),r.albedoMaps&&t.albedoMapsTex&&(t.albedoMapsTex.value=r.albedoMaps),r.normalMaps&&t.normalMapsTex&&(t.normalMapsTex.value=r.normalMaps),r.bumpMaps&&t.bumpMapsTex&&(t.bumpMapsTex.value=r.bumpMaps),r.metalnessMaps&&t.metalnessMapsTex&&(t.metalnessMapsTex.value=r.metalnessMaps),r.roughnessMaps&&t.roughnessMapsTex&&(t.roughnessMapsTex.value=r.roughnessMaps),r.emissiveMaps&&t.emissiveMapsTex&&(t.emissiveMapsTex.value=r.emissiveMaps),r.displacementMaps&&t.displacementMapsTex&&(t.displacementMapsTex.value=r.displacementMaps),e.goboMaps&&t.goboMapsTex&&(t.goboMapsTex.value=e.goboMaps),e.iesProfiles&&t.iesProfilesTex&&(t.iesProfilesTex.value=e.iesProfiles),console.log(`ShaderBuilder: Scene textures updated in-place`)}updateGoboMaps(e){let t=this._sceneTextureNodes;!t||!t.goboMapsTex||e&&(t.goboMapsTex.value=e)}updateIESProfiles(e){let t=this._sceneTextureNodes;!t||!t.iesProfilesTex||e&&(t.iesProfilesTex.value=e)}getSceneTextureNodes(){return this._sceneTextureNodes}createSceneTextureNodes(e,t){let i=e.triangleStorageNode,a=e.bvhStorageNode,o=e.materialData.materialStorageNode,s=e.lightStorageNode;gr(e.uniforms.get(`enableAlphaShadows`));let c=(0,n.texture)(e.environment.environmentTexture),l=t.getReadTextures();this.prevColorTexNode=(0,n.texture)(l.color),this.prevAlbedoTexNode=(0,n.texture)(l.albedo);let u=()=>{let e=new r.DataArrayTexture(new Uint8Array([255,255,255,255]),1,1,1);return e.minFilter=r.LinearFilter,e.magFilter=r.LinearFilter,e.generateMipmaps=!1,e.needsUpdate=!0,(0,n.texture)(e)},d=e.materialData,f=d.albedoMaps?(0,n.texture)(d.albedoMaps):u(),p=d.normalMaps?(0,n.texture)(d.normalMaps):u(),m=d.bumpMaps?(0,n.texture)(d.bumpMaps):u(),h=d.metalnessMaps?(0,n.texture)(d.metalnessMaps):u(),g=d.roughnessMaps?(0,n.texture)(d.roughnessMaps):u(),_=d.emissiveMaps?(0,n.texture)(d.emissiveMaps):u(),v=d.displacementMaps?(0,n.texture)(d.displacementMaps):u(),y=e.goboMaps?(0,n.texture)(e.goboMaps):u();Lr(y);let b=e.iesProfiles?(0,n.texture)(e.iesProfiles):u();Vr(b),hr(f);let x={triStorage:i,bvhStorage:a,matStorage:o,lightBufferStorage:s,envTex:c,albedoMapsTex:f,normalMapsTex:p,bumpMapsTex:m,metalnessMapsTex:h,roughnessMapsTex:g,emissiveMapsTex:_,displacementMapsTex:v,goboMapsTex:y,iesProfilesTex:b};return this._sceneTextureNodes=x,x}dispose(){this.prevColorTexNode=null,this.prevAlbedoTexNode=null,this._sceneTextureNodes=null}},Jr=class{constructor(e,t){this.traversalCost=e,this.intersectionCost=t,this.maxTreeletLeaves=7,this.minImprovement=.02,this.topologyCache=new Map;for(let e=3;e<=this.maxTreeletLeaves;e++)this.topologyCache.set(e,this.generateTopologies(e));this.stats={treeletsProcessed:0,treeletsImproved:0,totalSAHImprovement:0,averageSAHImprovement:0,optimizationTime:0}}generateTopologies(e){if(e===1)return[0];if(e===2)return[[0,1]];let t=[];for(let n=1;n<e;n++){let r=this.generateTopologies(n),i=this.generateTopologies(e-n);for(let e of r)for(let r of i)t.push([e,this.offsetTopology(r,n)])}return t}offsetTopology(e,t){return typeof e==`number`?e+t:[this.offsetTopology(e[0],t),this.offsetTopology(e[1],t)]}optimizeBVH(e){let t=performance.now();this.stats={treeletsProcessed:0,treeletsImproved:0,totalSAHImprovement:0,averageSAHImprovement:0,optimizationTime:0};let n=this.identifyTreeletRoots(e);for(let e=0;e<n.length;e++){if(performance.now()-t>3e4){console.warn(`TreeletOptimizer: timeout after ${e}/${n.length} treelets`);break}this.optimizeTreelet(n[e])}return this.stats.optimizationTime=performance.now()-t,this.stats.averageSAHImprovement=this.stats.treeletsProcessed>0?this.stats.totalSAHImprovement/this.stats.treeletsProcessed:0,e}identifyTreeletRoots(e){let t=[],n=new Set,r=[{node:e,visited:!1}];for(;r.length>0;){let e=r[r.length-1];if(e.visited){r.pop();let i=e.node;if(i.triangleCount>0||n.has(i))continue;let a=this.countLeaves(i);a>=3&&a<=this.maxTreeletLeaves&&(t.push(i),this.markSubtree(i,n))}else{e.visited=!0;let t=e.node;if(t.triangleCount>0)continue;t.rightChild&&r.push({node:t.rightChild,visited:!1}),t.leftChild&&r.push({node:t.leftChild,visited:!1})}}return t}countLeaves(e){return e?e.triangleCount>0?1:this.countLeaves(e.leftChild)+this.countLeaves(e.rightChild):0}markSubtree(e,t){e&&(t.add(e),!(e.triangleCount>0)&&(this.markSubtree(e.leftChild,t),this.markSubtree(e.rightChild,t)))}optimizeTreelet(e){let t=[];this.extractLeaves(e,t);let n=t.length;if(n<3||n>this.maxTreeletLeaves)return;this.stats.treeletsProcessed++;let r=this.evaluateSubtreeSAH(e),i=this.topologyCache.get(n);if(!i||i.length===0)return;let a=r,o=null,s=null;if(n<=5){let e=this.generatePermutations(n);for(let n of i)for(let r of e){let e=this.evaluateTopology(n,t,r);e<a&&(a=e,o=n,s=r)}}else{let e=Array.from({length:n},(e,t)=>t);for(let n of i){let r=this.evaluateTopology(n,t,e);r<a&&(a=r,o=n,s=e);let i=this.greedySwapOptimize(n,t,e,r);i.cost<a&&(a=i.cost,o=n,s=i.perm)}}let c=(r-a)/r;o&&c>this.minImprovement&&(this.reconstructTreelet(e,o,t,s),this.stats.treeletsImproved++,this.stats.totalSAHImprovement+=c)}extractLeaves(e,t){if(e){if(e.triangleCount>0){t.push({minX:e.minX,minY:e.minY,minZ:e.minZ,maxX:e.maxX,maxY:e.maxY,maxZ:e.maxZ,triangleOffset:e.triangleOffset,triangleCount:e.triangleCount});return}this.extractLeaves(e.leftChild,t),this.extractLeaves(e.rightChild,t)}}evaluateSubtreeSAH(e){if(!e)return 0;if(e.triangleCount>0)return this.surfaceAreaFlat(e.minX,e.minY,e.minZ,e.maxX,e.maxY,e.maxZ)*e.triangleCount*this.intersectionCost;let t=this.evaluateSubtreeSAH(e.leftChild),n=this.evaluateSubtreeSAH(e.rightChild);return this.surfaceAreaFlat(e.minX,e.minY,e.minZ,e.maxX,e.maxY,e.maxZ)*this.traversalCost+t+n}evaluateTopology(e,t,n){return this.evalTopoRecursive(e,t,n).cost}evalTopoRecursive(e,t,n){if(typeof e==`number`){let r=t[n[e]];return{cost:this.surfaceAreaFlat(r.minX,r.minY,r.minZ,r.maxX,r.maxY,r.maxZ)*r.triangleCount*this.intersectionCost,minX:r.minX,minY:r.minY,minZ:r.minZ,maxX:r.maxX,maxY:r.maxY,maxZ:r.maxZ}}let r=this.evalTopoRecursive(e[0],t,n),i=this.evalTopoRecursive(e[1],t,n),a=Math.min(r.minX,i.minX),o=Math.min(r.minY,i.minY),s=Math.min(r.minZ,i.minZ),c=Math.max(r.maxX,i.maxX),l=Math.max(r.maxY,i.maxY),u=Math.max(r.maxZ,i.maxZ);return{cost:this.surfaceAreaFlat(a,o,s,c,l,u)*this.traversalCost+r.cost+i.cost,minX:a,minY:o,minZ:s,maxX:c,maxY:l,maxZ:u}}surfaceAreaFlat(e,t,n,r,i,a){let o=r-e,s=i-t,c=a-n;return 2*(o*s+s*c+c*o)}generatePermutations(e){let t=[],n=Array.from({length:e},(e,t)=>t),r=i=>{if(i===e){t.push([...n]);return}for(let t=i;t<e;t++)[n[i],n[t]]=[n[t],n[i]],r(i+1),[n[i],n[t]]=[n[t],n[i]]};return r(0),t}greedySwapOptimize(e,t,n,r){let i=[...n],a=r,o=!0;for(;o;){o=!1;for(let n=0;n<i.length-1;n++)for(let r=n+1;r<i.length;r++){[i[n],i[r]]=[i[r],i[n]];let s=this.evaluateTopology(e,t,i);s<a?(a=s,o=!0):[i[n],i[r]]=[i[r],i[n]]}}return{perm:i,cost:a}}reconstructTreelet(e,t,n,r){let i=this.buildSubtree(t,n,r);e.minX=i.minX,e.minY=i.minY,e.minZ=i.minZ,e.maxX=i.maxX,e.maxY=i.maxY,e.maxZ=i.maxZ,e.leftChild=i.leftChild,e.rightChild=i.rightChild,e.triangleOffset=i.triangleOffset,e.triangleCount=i.triangleCount}buildSubtree(e,t,n){if(typeof e==`number`){let r=t[n[e]],i=new Yr;return i.minX=r.minX,i.minY=r.minY,i.minZ=r.minZ,i.maxX=r.maxX,i.maxY=r.maxY,i.maxZ=r.maxZ,i.triangleOffset=r.triangleOffset,i.triangleCount=r.triangleCount,i}let r=this.buildSubtree(e[0],t,n),i=this.buildSubtree(e[1],t,n),a=new Yr;return a.leftChild=r,a.rightChild=i,a.minX=Math.min(r.minX,i.minX),a.minY=Math.min(r.minY,i.minY),a.minZ=Math.min(r.minZ,i.minZ),a.maxX=Math.max(r.maxX,i.maxX),a.maxY=Math.max(r.maxY,i.maxY),a.maxZ=Math.max(r.maxZ,i.maxZ),a}setTreeletSize(e){this.maxTreeletLeaves=Math.max(3,Math.min(7,e));for(let e=3;e<=this.maxTreeletLeaves;e++)this.topologyCache.has(e)||this.topologyCache.set(e,this.generateTopologies(e))}setMinImprovement(e){this.minImprovement=Math.max(.001,e)}setMaxTreelets(){}getStatistics(){return{...this.stats}}},Yr=class{constructor(){this.minX=0,this.minY=0,this.minZ=0,this.maxX=0,this.maxY=0,this.maxZ=0,this.leftChild=null,this.rightChild=null,this.triangleOffset=0,this.triangleCount=0}},Xr=class{constructor(e,t){this.traversalCost=e,this.intersectionCost=t,this.batchSizeRatio=.02,this.maxIterations=2,this.timeBudgetMs=15e3,this.stats={reinsertionsApplied:0,iterations:0,timeMs:0}}setBatchSizeRatio(e){this.batchSizeRatio=Math.max(.005,Math.min(.1,e))}setMaxIterations(e){this.maxIterations=Math.max(1,Math.min(5,e))}getStatistics(){return{...this.stats}}surfaceArea(e){let t=e.maxX-e.minX,n=e.maxY-e.minY,r=e.maxZ-e.minZ;return t*n+n*r+r*t}buildParentMap(e){let t=new Map;t.set(e,{parent:null,isLeft:!1});let n=[e];for(;n.length>0;){let e=n.pop();e.triangleCount>0||(e.leftChild&&(t.set(e.leftChild,{parent:e,isLeft:!0}),n.push(e.leftChild)),e.rightChild&&(t.set(e.rightChild,{parent:e,isLeft:!1}),n.push(e.rightChild)))}return t}findCandidates(e,t,n){let r=[],i=[e];for(;i.length>0;){let a=i.pop();if(a!==e&&n.get(a).parent!==e){let e=this.surfaceArea(a);r.length<t?(r.push({node:a,cost:e}),r.length===t&&this._heapify(r)):e>r[0].cost&&(r[0]={node:a,cost:e},this._siftDown(r,0))}a.triangleCount===0&&(a.leftChild&&i.push(a.leftChild),a.rightChild&&i.push(a.rightChild))}return r}_heapify(e){for(let t=(e.length>>1)-1;t>=0;t--)this._siftDown(e,t)}_siftDown(e,t){let n=e.length;for(;;){let r=t,i=2*t+1,a=2*t+2;if(i<n&&e[i].cost<e[r].cost&&(r=i),a<n&&e[a].cost<e[r].cost&&(r=a),r===t)break;let o=e[t];e[t]=e[r],e[r]=o,t=r}}findReinsertion(e,t,n){let r=n.get(e),i=r.parent;if(!i)return null;let a=r.isLeft?i.rightChild:i.leftChild,o=this.surfaceArea(e),s=this.surfaceArea(i),c=null,l=0,u=s,d=a.minX,f=a.minY,p=a.minZ,m=a.maxX,h=a.maxY,g=a.maxZ,_=a,v=i,y=[];do{for(y.length=0,y.push(u,_);y.length>0;){let t=y.pop(),n=y.pop();if(n-o<=l)continue;let r=Math.min(t.minX,e.minX),i=Math.min(t.minY,e.minY),a=Math.min(t.minZ,e.minZ),s=Math.max(t.maxX,e.maxX),u=Math.max(t.maxY,e.maxY),d=Math.max(t.maxZ,e.maxZ),f=s-r,p=u-i,m=d-a,h=n-(f*p+p*m+m*f);if(h>l&&(c=t,l=h),t.triangleCount===0&&t.leftChild&&t.rightChild){let e=h+this.surfaceArea(t);y.push(e,t.leftChild),y.push(e,t.rightChild)}}let t=n.get(v);if(!t||t.parent===null)break;if(v!==i){d=Math.min(d,_.minX),f=Math.min(f,_.minY),p=Math.min(p,_.minZ),m=Math.max(m,_.maxX),h=Math.max(h,_.maxY),g=Math.max(g,_.maxZ);let e=m-d,t=h-f,n=g-p,r=e*t+t*n+n*e;u+=this.surfaceArea(v)-r}let r=t.parent;_=t.isLeft?r.rightChild:r.leftChild,v=r}while(n.get(v).parent!==null);return c===a||c===i?null:c?{from:e,to:c,areaDiff:l}:null}getConflicts(e,t,n){let r=n.get(e);return[t,e,r.isLeft?r.parent.rightChild:r.parent.leftChild,n.get(t).parent,r.parent]}reinsertNode(e,t,n){let r=n.get(e),i=r.parent,a=r.isLeft?i.rightChild:i.leftChild,o=n.get(i),s=o.parent,c=n.get(t),l=c.parent;o.isLeft?s.leftChild=a:s.rightChild=a,i.leftChild=e,i.rightChild=t,i.triangleOffset=0,i.triangleCount=0,i.minX=Math.min(e.minX,t.minX),i.minY=Math.min(e.minY,t.minY),i.minZ=Math.min(e.minZ,t.minZ),i.maxX=Math.max(e.maxX,t.maxX),i.maxY=Math.max(e.maxY,t.maxY),i.maxZ=Math.max(e.maxZ,t.maxZ),c.isLeft?l.leftChild=i:l.rightChild=i,n.set(a,{parent:s,isLeft:o.isLeft}),n.set(i,{parent:l,isLeft:c.isLeft}),n.set(e,{parent:i,isLeft:!0}),n.set(t,{parent:i,isLeft:!1}),this.refitFrom(s,n),this.refitFrom(l,n)}refitFrom(e,t){let n=e;for(;n;){if(n.triangleCount===0&&n.leftChild&&n.rightChild){let e=n.leftChild,t=n.rightChild;n.minX=Math.min(e.minX,t.minX),n.minY=Math.min(e.minY,t.minY),n.minZ=Math.min(e.minZ,t.minZ),n.maxX=Math.max(e.maxX,t.maxX),n.maxY=Math.max(e.maxY,t.maxY),n.maxZ=Math.max(e.maxZ,t.maxZ)}let e=t.get(n);n=e?e.parent:null}}optimizeBVH(e,t){let n=performance.now();this.stats={reinsertionsApplied:0,iterations:0,timeMs:0};for(let r=0;r<this.maxIterations&&!(performance.now()-n>this.timeBudgetMs);r++){let i=this.buildParentMap(e),a=i.size,o=Math.max(1,Math.floor(a*this.batchSizeRatio));t&&t(`Reinsertion iter ${r+1}/${this.maxIterations}: selecting ${o} candidates`);let s=this.findCandidates(e,o,i),c=[];for(let t=0;t<s.length&&!(performance.now()-n>this.timeBudgetMs);t++){let n=this.findReinsertion(s[t].node,e,i);n&&n.areaDiff>0&&c.push(n)}c.sort((e,t)=>t.areaDiff-e.areaDiff);let l=new Set,u=0;for(let e of c){let t=this.getConflicts(e.from,e.to,i);if(!t.some(e=>l.has(e))){for(let e of t)l.add(e);this.reinsertNode(e.from,e.to,i),u++}}if(this.stats.reinsertionsApplied+=u,this.stats.iterations=r+1,t&&t(`Reinsertion iter ${r+1}: applied ${u} reinsertions`),u===0)break}return this.stats.timeMs=performance.now()-n,this.stats}},Zr=``+(typeof document>`u`&&typeof location>`u`?require(`url`).pathToFileURL(__dirname+`/assets/BVHWorker-CNJ0UBQz.js`).href:new URL(`assets/BVHWorker-CNJ0UBQz.js`,typeof document>`u`?location.href:document.currentScript&&document.currentScript.tagName.toUpperCase()===`SCRIPT`&&document.currentScript.src||document.baseURI).href),Qr={FLOATS_PER_TRIANGLE:32,POSITION_A_OFFSET:0,POSITION_B_OFFSET:4,POSITION_C_OFFSET:8,NORMAL_A_OFFSET:12,NORMAL_B_OFFSET:16,NORMAL_C_OFFSET:20,UV_AB_OFFSET:24,UV_C_MAT_OFFSET:28},$r=Qr.FLOATS_PER_TRIANGLE,ei=class{constructor(){this.minX=0,this.minY=0,this.minZ=0,this.maxX=0,this.maxY=0,this.maxZ=0,this.leftChild=null,this.rightChild=null,this.triangleOffset=0,this.triangleCount=0}},ti=class{constructor(){this.useWorker=!0,this.maxLeafSize=8,this.numBins=32,this.minBins=8,this.maxBins=64,this.totalNodes=0,this.processedTriangles=0,this.totalTriangles=0,this.lastProgressUpdate=0,this.progressUpdateInterval=100,this.traversalCost=1,this.intersectionCost=2.5,this.useMortonCodes=!0,this.mortonBits=10,this.mortonClusterThreshold=128,this.enableObjectMedianFallback=!0,this.enableSpatialMedianFallback=!0,this.splitStats={sahSplits:0,objectMedianSplits:0,spatialMedianSplits:0,failedSplits:0,avgBinsUsed:0,totalSplitAttempts:0,mortonSortTime:0,totalBuildTime:0,treeletOptimizationTime:0,treeletsProcessed:0,treeletsImproved:0,averageSAHImprovement:0,reinsertionOptimizationTime:0,reinsertionsApplied:0,reinsertionIterations:0},this.enableTreeletOptimization=!0,this.treeletSize=5,this.treeletOptimizationPasses=1,this.treeletMinImprovement=.02,this.maxTreeletDepth=3,this.maxTreeletsPerScene=20,this.treeletComplexityThreshold=5e4,this.enableReinsertionOptimization=!0,this.reinsertionBatchSizeRatio=.02,this.reinsertionMaxIterations=2,this.initializeBinArrays(),this._partResult={mid:0,lMinX:0,lMinY:0,lMinZ:0,lMaxX:0,lMaxY:0,lMaxZ:0,rMinX:0,rMinY:0,rMinZ:0,rMaxX:0,rMaxY:0,rMaxZ:0},this.centroids=null,this.bMin=null,this.bMax=null,this.indices=null,this.mortonCodes=null,this.triangles=null,this.reorderedTriangleData=null}initializeBinArrays(){let e=this.maxBins;this.binBoundsMin=new Float32Array(e*3),this.binBoundsMax=new Float32Array(e*3),this.binCounts=new Uint32Array(e),this.leftPrefixMin=new Float32Array(e*3),this.leftPrefixMax=new Float32Array(e*3),this.leftPrefixCount=new Uint32Array(e),this.rightPrefixMin=new Float32Array(e*3),this.rightPrefixMax=new Float32Array(e*3),this.rightPrefixCount=new Uint32Array(e)}getOptimalBinCount(e){return e<=16?this.minBins:e<=64?16:e<=256?32:e<=1024?48:this.maxBins}setAdaptiveBinConfig(e){e.minBins!==void 0&&(this.minBins=Math.max(4,e.minBins)),e.maxBins!==void 0&&(this.maxBins=Math.min(128,e.maxBins)),e.baseBins!==void 0&&(this.numBins=e.baseBins),e.maxBins!==void 0&&this.initializeBinArrays()}setMortonConfig(e){e.enabled!==void 0&&(this.useMortonCodes=e.enabled),e.bits!==void 0&&(this.mortonBits=Math.max(6,Math.min(10,e.bits))),e.threshold!==void 0&&(this.mortonClusterThreshold=Math.max(16,e.threshold))}setFallbackConfig(e){e.objectMedian!==void 0&&(this.enableObjectMedianFallback=e.objectMedian),e.spatialMedian!==void 0&&(this.enableSpatialMedianFallback=e.spatialMedian)}setTreeletConfig(e){e.enabled!==void 0&&(this.enableTreeletOptimization=e.enabled),e.size!==void 0&&(this.treeletSize=Math.max(3,Math.min(12,e.size))),e.passes!==void 0&&(this.treeletOptimizationPasses=Math.max(1,Math.min(3,e.passes))),e.minImprovement!==void 0&&(this.treeletMinImprovement=Math.max(.001,e.minImprovement))}disableTreeletOptimization(){this.enableTreeletOptimization=!1}setReinsertionConfig(e){e.enabled!==void 0&&(this.enableReinsertionOptimization=e.enabled),e.batchSizeRatio!==void 0&&(this.reinsertionBatchSizeRatio=Math.max(.005,Math.min(.1,e.batchSizeRatio))),e.maxIterations!==void 0&&(this.reinsertionMaxIterations=Math.max(1,Math.min(5,e.maxIterations)))}initializeTriangleArrays(){let e=this.totalTriangles,t=this.triangles,n=Qr.POSITION_A_OFFSET,r=Qr.POSITION_B_OFFSET,i=Qr.POSITION_C_OFFSET;for(let a=0;a<e;a++){let e=a*$r,o=t[e+n],s=t[e+n+1],c=t[e+n+2],l=t[e+r],u=t[e+r+1],d=t[e+r+2],f=t[e+i],p=t[e+i+1],m=t[e+i+2],h=a*3;this.centroids[h]=(o+l+f)/3,this.centroids[h+1]=(s+u+p)/3,this.centroids[h+2]=(c+d+m)/3,this.bMin[h]=o<l?o<f?o:f:l<f?l:f,this.bMin[h+1]=s<u?s<p?s:p:u<p?u:p,this.bMin[h+2]=c<d?c<m?c:m:d<m?d:m,this.bMax[h]=o>l?o>f?o:f:l>f?l:f,this.bMax[h+1]=s>u?s>p?s:p:u>p?u:p,this.bMax[h+2]=c>d?c>m?c:m:d>m?d:m,this.indices[a]=a}}expandBits(e){return e=e*65537&4278190335,e=e*257&251719695,e=e*17&3272356035,e=e*5&1227133513,e}morton3D(e,t,n){return(this.expandBits(n)<<2)+(this.expandBits(t)<<1)+this.expandBits(e)}computeMortonCodeForIndex(e,t,n,r,i,a,o){let s=this.centroids,c=e*3,l=(1<<this.mortonBits)-1,u=i>0?(s[c]-t)/i:0,d=a>0?(s[c+1]-n)/a:0,f=o>0?(s[c+2]-r)/o:0,p=Math.max(0,Math.min(l,Math.floor(u*l))),m=Math.max(0,Math.min(l,Math.floor(d*l))),h=Math.max(0,Math.min(l,Math.floor(f*l)));return this.morton3D(p,m,h)}sortTrianglesByMortonCode(){let e=this.totalTriangles;if(!this.useMortonCodes||e<this.mortonClusterThreshold)return;let t=performance.now(),n=this.centroids,r=this.indices,i=1/0,a=1/0,o=1/0,s=-1/0,c=-1/0,l=-1/0;for(let t=0;t<e;t++){let e=r[t]*3,u=n[e],d=n[e+1],f=n[e+2];u<i&&(i=u),d<a&&(a=d),f<o&&(o=f),u>s&&(s=u),d>c&&(c=d),f>l&&(l=f)}let u=s-i,d=c-a,f=l-o,p=this.mortonCodes,m=(1<<this.mortonBits)-1,h=u>0?m/u:0,g=d>0?m/d:0,_=f>0?m/f:0;for(let t=0;t<e;t++){let e=r[t],s=e*3,c=(n[s]-i)*h,l=(n[s+1]-a)*g,u=(n[s+2]-o)*_;c=c<0?0:(c>m?m:c)|0,l=l<0?0:(l>m?m:l)|0,u=u<0?0:(u>m?m:u)|0,c=c*65537&4278190335,c=c*257&251719695,c=c*17&3272356035,c=c*5&1227133513,l=l*65537&4278190335,l=l*257&251719695,l=l*17&3272356035,l=l*5&1227133513,u=u*65537&4278190335,u=u*257&251719695,u=u*17&3272356035,u=u*5&1227133513,p[e]=(u<<2)+(l<<1)+c}let v=new Uint32Array(e),y=new Uint32Array(256);for(let t=0;t<32;t+=8){y.fill(0);for(let n=0;n<e;n++)y[p[r[n]]>>>t&255]++;let n=0;for(let e=0;e<256;e++){let t=y[e];y[e]=n,n+=t}for(let n=0;n<e;n++){let e=p[r[n]]>>>t&255;v[y[e]++]=r[n]}r.set(v)}this.splitStats.mortonSortTime+=performance.now()-t}build(e,t=30,n=null){return this.totalTriangles=e.byteLength/($r*4),this.processedTriangles=0,this.lastProgressUpdate=performance.now(),this.useWorker&&typeof Worker<`u`?new Promise((r,i)=>{let a=a=>{let o=this.totalTriangles,s=typeof SharedArrayBuffer<`u`;console.log(`[BVHBuilder] SharedArrayBuffer: ${s?`enabled`:`unavailable (using transfer fallback)`}`);let c=s?new SharedArrayBuffer(o*$r*4):null;a.onmessage=e=>{let{bvhData:t,triangles:o,originalToBvh:s,error:l,progress:u,treeletStats:d}=e.data;if(l){a.terminate(),i(Error(l));return}if(u!==void 0&&n){n(u);return}d&&(this.splitStats=d),a.terminate(),r({bvhData:t,bvhRoot:!0,reorderedTriangles:c?new Float32Array(c):o,originalToBvh:s||null})},a.onerror=e=>{a.terminate(),i(e)};let l=e.buffer,u={triangleData:l,triangleByteOffset:e.byteOffset,triangleByteLength:e.byteLength,triangleCount:o,depth:t,reportProgress:!!n,sharedReorderBuffer:c,treeletOptimization:{enabled:this.enableTreeletOptimization,size:this.treeletSize,passes:this.treeletOptimizationPasses,minImprovement:this.treeletMinImprovement},reinsertionOptimization:{enabled:this.enableReinsertionOptimization,batchSizeRatio:this.reinsertionBatchSizeRatio,maxIterations:this.reinsertionMaxIterations}};a.postMessage(u,[l])};try{a(new Worker(Zr,{type:`module`}))}catch(i){i.name===`SecurityError`?Je(Zr).then(a).catch(()=>{console.warn(`Worker fetch fallback failed, using synchronous build`),r(this._buildSyncAndFlatten(e,t,n))}):(console.warn(`Worker creation failed, falling back to synchronous build:`,i),r(this._buildSyncAndFlatten(e,t,n)))}}):new Promise(r=>{r(this._buildSyncAndFlatten(e,t,n))})}_buildSyncAndFlatten(e,t,n){let r=this.buildSync(e,t,n);return{bvhData:this.flattenBVH(r),bvhRoot:!0,reorderedTriangles:this.reorderedTriangleData||null,originalToBvh:this.originalToBvhMap||null}}buildSync(e,t=30,n=null,r=null){let i=performance.now();this.totalNodes=0,this.processedTriangles=0,this.triangles=e,this.totalTriangles=e.byteLength/($r*4),this.lastProgressUpdate=performance.now(),this.splitStats={sahSplits:0,objectMedianSplits:0,spatialMedianSplits:0,failedSplits:0,avgBinsUsed:0,totalSplitAttempts:0,mortonSortTime:0,totalBuildTime:0,treeletOptimizationTime:0,treeletsProcessed:0,treeletsImproved:0,averageSAHImprovement:0,reinsertionOptimizationTime:0,reinsertionsApplied:0,reinsertionIterations:0,saOrderTime:0,initTime:0,sahBuildTime:0,reorderTime:0};let a=this.totalTriangles,o=performance.now();this.centroids=new Float32Array(a*3),this.bMin=new Float32Array(a*3),this.bMax=new Float32Array(a*3),this.indices=new Uint32Array(a),this.mortonCodes=new Uint32Array(a),this.initializeTriangleArrays(),this.splitStats.initTime=performance.now()-o,this.sortTrianglesByMortonCode();let s=performance.now(),c=this.buildNodeRecursive(0,a,t,n);if(this.splitStats.sahBuildTime=performance.now()-s,this.enableTreeletOptimization&&this.totalTriangles>1e3){let e=this.totalTriangles>this.treeletComplexityThreshold,t=e?3:this.treeletSize,r=e?10:this.maxTreeletsPerScene,i=new Jr(this.traversalCost,this.intersectionCost);i.setTreeletSize(t),i.setMinImprovement(this.treeletMinImprovement),i.setMaxTreelets(r);let a=performance.now();for(let e=0;e<this.treeletOptimizationPasses;e++){let t=n?t=>{n(`Treelet optimization pass ${e+1}/${this.treeletOptimizationPasses}: ${t}`)}:null;try{i.optimizeBVH(c,t)}catch(t){console.error(`TreeletOptimizer: Error in pass ${e+1}:`,t);break}let r=i.getStatistics(),o=performance.now()-a;if(r.treeletsImproved===0&&e>0||o>15e3)break}let o=performance.now()-a;this.splitStats.treeletOptimizationTime=o;let s=i.getStatistics();this.splitStats.treeletsProcessed=s.treeletsProcessed,this.splitStats.treeletsImproved=s.treeletsImproved,this.splitStats.averageSAHImprovement=s.averageSAHImprovement}if(this.enableReinsertionOptimization&&this.totalTriangles>1e3){let e=new Xr(this.traversalCost,this.intersectionCost);e.setBatchSizeRatio(this.reinsertionBatchSizeRatio),e.setMaxIterations(this.reinsertionMaxIterations);let t=n?e=>{n(e)}:null;try{e.optimizeBVH(c,t)}catch(e){console.error(`ReinsertionOptimizer: Error:`,e)}let r=e.getStatistics();this.splitStats.reinsertionOptimizationTime=r.timeMs,this.splitStats.reinsertionsApplied=r.reinsertionsApplied,this.splitStats.reinsertionIterations=r.iterations}let l=performance.now();this.applySAOrdering(c),this.splitStats.saOrderTime=performance.now()-l;let u=performance.now(),d=this.triangles,f=r||new Float32Array(a*$r);for(let e=0;e<a;e++){let t=this.indices[e]*$r,n=e*$r;f.set(d.subarray(t,t+$r),n)}this.reorderedTriangleData=f;let p=new Uint32Array(a);for(let e=0;e<a;e++)p[this.indices[e]]=e;this.originalToBvhMap=p,this.splitStats.reorderTime=performance.now()-u,this.splitStats.totalBuildTime=performance.now()-i;let m=this.splitStats.totalBuildTime,h=this.splitStats;return console.log(`[BVH] ${a.toLocaleString()} tris → ${this.totalNodes} nodes in ${Math.round(m)}ms | SAH ${h.sahSplits} objMed ${h.objectMedianSplits} spatMed ${h.spatialMedianSplits} failed ${h.failedSplits}`+(h.treeletsProcessed?` | treelets ${h.treeletsImproved}/${h.treeletsProcessed} improved`:``)+(h.reinsertionsApplied?` | reinsertions ${h.reinsertionsApplied}`:``)),n&&n(100),this.centroids=null,this.bMin=null,this.bMax=null,this.mortonCodes=null,c}updateProgress(e,t){if(!t)return;this.processedTriangles+=e;let n=performance.now();n-this.lastProgressUpdate<this.progressUpdateInterval||(this.lastProgressUpdate=n,t(Math.min(Math.floor(this.processedTriangles/this.totalTriangles*100),99)))}buildNodeRecursiveToDepth(e,t,n,r,i,a,o,s,c,l,u){let d=new ei;this.totalNodes++;let f=t-e;if(a===void 0?this.updateNodeBounds(d,e,t):(d.minX=a,d.minY=o,d.minZ=s,d.maxX=c,d.maxY=l,d.maxZ=u),f<=this.maxLeafSize||n<=0)return d.triangleOffset=e,d.triangleCount=f,this.updateProgress(f,i),d;if(r<=0&&f>this.maxLeafSize*16){let r=this.frontierTasks.length;return d.triangleOffset=e,d.triangleCount=f,d.isFrontier=!0,d.frontierTaskId=r,this.frontierTasks.push({taskId:r,start:e,end:t,depth:n,preMinX:d.minX,preMinY:d.minY,preMinZ:d.minZ,preMaxX:d.maxX,preMaxY:d.maxY,preMaxZ:d.maxZ}),d}let p=this.findBestSplitPositionSAH(e,t,d);if(!p.success){if(this.splitStats.failedSplits++,r>0||f<=this.maxLeafSize*16)return d.triangleOffset=e,d.triangleCount=f,this.updateProgress(f,i),d;let a=this.frontierTasks.length;return d.triangleOffset=e,d.triangleCount=f,d.isFrontier=!0,d.frontierTaskId=a,this.frontierTasks.push({taskId:a,start:e,end:t,depth:n,preMinX:d.minX,preMinY:d.minY,preMinZ:d.minZ,preMaxX:d.maxX,preMaxY:d.maxY,preMaxZ:d.maxZ}),d}p.method===`SAH`?this.splitStats.sahSplits++:p.method===`object_median`?this.splitStats.objectMedianSplits++:p.method===`spatial_median`&&this.splitStats.spatialMedianSplits++,this.partitionWithBounds(e,t,p.axis,p.pos);let m=this._partResult,h=m.mid,g=m.lMinX,_=m.lMinY,v=m.lMinZ,y=m.lMaxX,b=m.lMaxY,x=m.lMaxZ,S=m.rMinX,C=m.rMinY,w=m.rMinZ,T=m.rMaxX,E=m.rMaxY,D=m.rMaxZ;return h===e||h===t?(d.triangleOffset=e,d.triangleCount=f,this.updateProgress(f,i),d):(d.leftChild=this.buildNodeRecursiveToDepth(e,h,n-1,r-1,i,g,_,v,y,b,x),d.rightChild=this.buildNodeRecursiveToDepth(h,t,n-1,r-1,i,S,C,w,T,E,D),d)}buildNodeRecursive(e,t,n,r,i,a,o,s,c,l){let u=new ei;this.totalNodes++;let d=t-e;if(i===void 0?this.updateNodeBounds(u,e,t):(u.minX=i,u.minY=a,u.minZ=o,u.maxX=s,u.maxY=c,u.maxZ=l),d<=this.maxLeafSize||n<=0)return u.triangleOffset=e,u.triangleCount=d,this.updateProgress(d,r),u;let f=this.findBestSplitPositionSAH(e,t,u);if(!f.success)return this.splitStats.failedSplits++,u.triangleOffset=e,u.triangleCount=d,this.updateProgress(d,r),u;f.method===`SAH`?this.splitStats.sahSplits++:f.method===`object_median`?this.splitStats.objectMedianSplits++:f.method===`spatial_median`&&this.splitStats.spatialMedianSplits++,this.partitionWithBounds(e,t,f.axis,f.pos);let p=this._partResult,m=p.mid,h=p.lMinX,g=p.lMinY,_=p.lMinZ,v=p.lMaxX,y=p.lMaxY,b=p.lMaxZ,x=p.rMinX,S=p.rMinY,C=p.rMinZ,w=p.rMaxX,T=p.rMaxY,E=p.rMaxZ;return m===e||m===t?(u.triangleOffset=e,u.triangleCount=d,this.updateProgress(d,r),u):(u.leftChild=this.buildNodeRecursive(e,m,n-1,r,h,g,_,v,y,b),u.rightChild=this.buildNodeRecursive(m,t,n-1,r,x,S,C,w,T,E),u)}partitionWithBounds(e,t,n,r){let i=this.indices,a=this.centroids,o=this.bMin,s=this.bMax,c=e,l=t-1,u=1/0,d=1/0,f=1/0,p=-1/0,m=-1/0,h=-1/0,g=1/0,_=1/0,v=1/0,y=-1/0,b=-1/0,x=-1/0;for(;c<=l;){let e=i[c],t=e*3;a[t+n]<=r?(o[t]<u&&(u=o[t]),o[t+1]<d&&(d=o[t+1]),o[t+2]<f&&(f=o[t+2]),s[t]>p&&(p=s[t]),s[t+1]>m&&(m=s[t+1]),s[t+2]>h&&(h=s[t+2]),c++):(o[t]<g&&(g=o[t]),o[t+1]<_&&(_=o[t+1]),o[t+2]<v&&(v=o[t+2]),s[t]>y&&(y=s[t]),s[t+1]>b&&(b=s[t+1]),s[t+2]>x&&(x=s[t+2]),i[c]=i[l],i[l]=e,l--)}let S=this._partResult;return S.mid=c,S.lMinX=u,S.lMinY=d,S.lMinZ=f,S.lMaxX=p,S.lMaxY=m,S.lMaxZ=h,S.rMinX=g,S.rMinY=_,S.rMinZ=v,S.rMaxX=y,S.rMaxY=b,S.rMaxZ=x,S}updateNodeBounds(e,t,n){let r=1/0,i=1/0,a=1/0,o=-1/0,s=-1/0,c=-1/0,l=this.indices,u=this.bMin,d=this.bMax;for(let e=t;e<n;e++){let t=l[e]*3;u[t]<r&&(r=u[t]),u[t+1]<i&&(i=u[t+1]),u[t+2]<a&&(a=u[t+2]),d[t]>o&&(o=d[t]),d[t+1]>s&&(s=d[t+1]),d[t+2]>c&&(c=d[t+2])}e.minX=r,e.minY=i,e.minZ=a,e.maxX=o,e.maxY=s,e.maxZ=c}findBestSplitPositionSAH(e,t,n){let r=1/0,i=-1,a=0,o=this.computeSurfaceAreaFlat(n.minX,n.minY,n.minZ,n.maxX,n.maxY,n.maxZ),s=t-e,c=this.intersectionCost*s,l=this.getOptimalBinCount(s);this.splitStats.totalSplitAttempts++,this.splitStats.avgBinsUsed=(this.splitStats.avgBinsUsed*(this.splitStats.totalSplitAttempts-1)+l)/this.splitStats.totalSplitAttempts;let u=this.indices,d=this.centroids,f=this.bMin,p=this.bMax,m=this.binBoundsMin,h=this.binBoundsMax,g=this.binCounts,_=this.leftPrefixMin,v=this.leftPrefixMax,y=this.leftPrefixCount,b=this.rightPrefixMin,x=this.rightPrefixMax,S=this.rightPrefixCount,C=1/0,w=-1/0,T=1/0,E=-1/0,D=1/0,O=-1/0;for(let n=e;n<t;n++){let e=u[n]*3,t=d[e],r=d[e+1],i=d[e+2];t<C&&(C=t),t>w&&(w=t),r<T&&(T=r),r>E&&(E=r),i<D&&(D=i),i>O&&(O=i)}let k=[C,T,D],A=[w,E,O];for(let n=0;n<3;n++){let s=k[n],C=A[n];if(C-s<1e-6)continue;for(let e=0;e<l;e++){g[e]=0;let t=e*3;m[t]=1/0,m[t+1]=1/0,m[t+2]=1/0,h[t]=-1/0,h[t+1]=-1/0,h[t+2]=-1/0}let w=l/(C-s);for(let r=e;r<t;r++){let e=u[r],t=d[e*3+n],i=Math.floor((t-s)*w);i>=l&&(i=l-1),g[i]++;let a=i*3,o=e*3;f[o]<m[a]&&(m[a]=f[o]),f[o+1]<m[a+1]&&(m[a+1]=f[o+1]),f[o+2]<m[a+2]&&(m[a+2]=f[o+2]),p[o]>h[a]&&(h[a]=p[o]),p[o+1]>h[a+1]&&(h[a+1]=p[o+1]),p[o+2]>h[a+2]&&(h[a+2]=p[o+2])}y[0]=g[0],_[0]=m[0],_[1]=m[1],_[2]=m[2],v[0]=h[0],v[1]=h[1],v[2]=h[2];for(let e=1;e<l;e++){let t=e*3,n=(e-1)*3;y[e]=y[e-1]+g[e];let r=_[n],i=m[t],a=_[n+1],o=m[t+1],s=_[n+2],c=m[t+2];_[t]=r<i?r:i,_[t+1]=a<o?a:o,_[t+2]=s<c?s:c;let l=v[n],u=h[t],d=v[n+1],f=h[t+1],p=v[n+2],b=h[t+2];v[t]=l>u?l:u,v[t+1]=d>f?d:f,v[t+2]=p>b?p:b}let T=l-1,E=T*3;S[T]=g[T],b[E]=m[E],b[E+1]=m[E+1],b[E+2]=m[E+2],x[E]=h[E],x[E+1]=h[E+1],x[E+2]=h[E+2];for(let e=T-1;e>=0;e--){let t=e*3,n=(e+1)*3;S[e]=S[e+1]+g[e];let r=b[n],i=m[t],a=b[n+1],o=m[t+1],s=b[n+2],c=m[t+2];b[t]=r<i?r:i,b[t+1]=a<o?a:o,b[t+2]=s<c?s:c;let l=x[n],u=h[t],d=x[n+1],f=h[t+1],p=x[n+2],_=h[t+2];x[t]=l>u?l:u,x[t+1]=d>f?d:f,x[t+2]=p>_?p:_}for(let e=1;e<l;e++){let t=(e-1)*3,u=e*3,d=y[e-1],f=S[e];if(d===0||f===0)continue;let p=v[t]-_[t],m=v[t+1]-_[t+1],h=v[t+2]-_[t+2],g=2*(p*m+m*h+h*p),w=x[u]-b[u],T=x[u+1]-b[u+1],E=x[u+2]-b[u+2],D=2*(w*T+T*E+E*w),O=this.traversalCost+g/o*d*this.intersectionCost+D/o*f*this.intersectionCost;O<r&&O<c&&(r=O,i=n,a=s+(C-s)*e/l)}}return i===-1?this.enableObjectMedianFallback?this.findObjectMedianSplit(e,t):this.enableSpatialMedianFallback?this.findSpatialMedianSplit(e,t):{success:!1,method:`fallbacks_disabled`}:{success:!0,axis:i,pos:a,method:`SAH`,binsUsed:l}}findObjectMedianSplit(e,t){let n=this.indices,r=this.centroids,i=-1,a=-1;for(let o=0;o<3;o++){let s=1/0,c=-1/0;for(let i=e;i<t;i++){let e=r[n[i]*3+o];e<s&&(s=e),e>c&&(c=e)}let l=c-s;l>a&&(a=l,i=o)}if(i===-1||a<1e-10)return this.enableSpatialMedianFallback?this.findSpatialMedianSplit(e,t):{success:!1,method:`object_median_failed`};let o=t-e,s=e+Math.floor(o/2);this.quickselect(e,t,s,i);let c=r[n[s]*3+i],l=!0;for(let e=s+1;e<t;e++)if(r[n[e]*3+i]>c){l=!1;break}if(l){let a=-1/0;for(let t=e;t<s;t++){let e=r[n[t]*3+i];e>a&&(a=e)}if(a<c)c=(a+c)*.5;else return this.enableSpatialMedianFallback?this.findSpatialMedianSplit(e,t):{success:!1,method:`object_median_degenerate`}}return{success:!0,axis:i,pos:c,method:`object_median`}}findSpatialMedianSplit(e,t){let n=this.indices,r=this.centroids,i=this.bMin,a=this.bMax,o=-1,s=-1,c=0,l=0;for(let r=0;r<3;r++){let u=1/0,d=-1/0;for(let o=e;o<t;o++){let e=n[o]*3+r;i[e]<u&&(u=i[e]),a[e]>d&&(d=a[e])}let f=d-u;f>s&&(s=f,o=r,c=u,l=d)}if(o===-1||s<1e-12)return{success:!1,method:`spatial_median_failed`};let u=(c+l)*.5,d=t-e,f=0;for(let i=e;i<t;i++)r[n[i]*3+o]<=u&&f++;if(f===0||f===d){let i=e+Math.floor(d/2);this.quickselect(e,t,i,o);let a=r[n[i]*3+o],s=!0;for(let i=e;i<t;i++)if(r[n[i]*3+o]!==a){s=!1;break}if(s)return{success:!1,method:`spatial_median_degenerate`};let c=-1/0;for(let t=e;t<i;t++){let e=r[n[t]*3+o];e>c&&(c=e)}if(c<a)u=(c+a)*.5;else{let e=1/0;for(let a=i+1;a<t;a++){let t=r[n[a]*3+o];t<e&&(e=t)}u=(a+e)*.5}}return{success:!0,axis:o,pos:u,method:`spatial_median`}}quickselect(e,t,n,r){let i=this.indices,a=this.centroids,o=e,s=t-1;for(;o<s;){let e=o+s>>>1,t=a[i[o]*3+r],c=a[i[e]*3+r],l=a[i[s]*3+r];if(t>c){let t=i[o];i[o]=i[e],i[e]=t}if(t>l){let e=i[o];i[o]=i[s],i[s]=e}if(c>l){let t=i[e];i[e]=i[s],i[s]=t}let u=a[i[e]*3+r],d=o,f=s;for(;d<=f;){for(;a[i[d]*3+r]<u;)d++;for(;a[i[f]*3+r]>u;)f--;if(d<=f){let e=i[d];i[d]=i[f],i[f]=e,d++,f--}}f<n&&(o=d),d>n&&(s=f)}}applySAOrdering(e){if(!e||!e.leftChild)return;let t=[e],n=[];for(;t.length>0;){let e=t.pop();!e.leftChild||!e.rightChild||(n.push(e),t.push(e.leftChild),t.push(e.rightChild))}for(let e=n.length-1;e>=0;e--){let t=n[e],r=t.leftChild,i=t.rightChild,a=r.maxX-r.minX,o=r.maxY-r.minY,s=r.maxZ-r.minZ,c=i.maxX-i.minX,l=i.maxY-i.minY,u=i.maxZ-i.minZ;c*l+l*u+u*c>a*o+o*s+s*a&&(t.leftChild=i,t.rightChild=r)}}flattenBVH(e){let t=[],n=[e];for(;n.length>0;){let e=n.pop();e._flatIndex=t.length,t.push(e),e.rightChild&&n.push(e.rightChild),e.leftChild&&n.push(e.leftChild)}let r=new Float32Array(t.length*16);for(let e=0;e<t.length;e++){let n=t[e],i=e*16;if(n.leftChild){let e=n.leftChild,t=n.rightChild;r[i]=e.minX,r[i+1]=e.minY,r[i+2]=e.minZ,r[i+3]=e._flatIndex,r[i+4]=e.maxX,r[i+5]=e.maxY,r[i+6]=e.maxZ,r[i+7]=t._flatIndex,r[i+8]=t.minX,r[i+9]=t.minY,r[i+10]=t.minZ,r[i+12]=t.maxX,r[i+13]=t.maxY,r[i+14]=t.maxZ}else r[i]=n.triangleOffset,r[i+1]=n.triangleCount,r[i+3]=-1}return r}flattenBVHWithFrontier(e){let t=[],n=[e];for(;n.length>0;){let e=n.pop();e._flatIndex=t.length,t.push(e),e.rightChild&&n.push(e.rightChild),e.leftChild&&n.push(e.leftChild)}let r=new Float32Array(t.length*16),i=[];for(let e=0;e<t.length;e++){let n=t[e],a=e*16;if(n.leftChild){let e=n.leftChild,t=n.rightChild;r[a]=e.minX,r[a+1]=e.minY,r[a+2]=e.minZ,r[a+3]=e._flatIndex,r[a+4]=e.maxX,r[a+5]=e.maxY,r[a+6]=e.maxZ,r[a+7]=t._flatIndex,r[a+8]=t.minX,r[a+9]=t.minY,r[a+10]=t.minZ,r[a+12]=t.maxX,r[a+13]=t.maxY,r[a+14]=t.maxZ}else if(n.isFrontier){let t=n.frontierTaskId;r[a]=n.triangleOffset,r[a+1]=n.triangleCount,r[a+2]=t,r[a+3]=-2,i.push({taskId:t,flatIndex:e})}else r[a]=n.triangleOffset,r[a+1]=n.triangleCount,r[a+3]=-1}return{flatData:r,frontierMap:i,nodeCount:t.length}}assembleParallelBVH(e,t,n,r){let i=[...r].sort((e,t)=>e.taskId-t.taskId),a=t;for(let e=0;e<i.length;e++)a+=i[e].nodeCount;let o=new Float32Array(a*16);o.set(e);let s=new Map;for(let e of n)s.set(e.taskId,e.flatIndex);let c=t;for(let e=0;e<i.length;e++){let t=i[e],n=t.flatData,r=t.nodeCount,a=c*16;o.set(n,a);for(let e=0;e<r;e++){let t=a+e*16;o[t+3]!==-1&&(o[t+3]+=c,o[t+7]+=c)}let l=s.get(t.taskId);if(l!==void 0){let e=l*16,t=a;for(let n=0;n<16;n++)o[e+n]=o[t+n]}c+=r}return o}computeSurfaceAreaFlat(e,t,n,r,i,a){let o=r-e,s=i-t,c=a-n;return 2*(o*s+s*c+c*o)}},Q={FLOATS_PER_TRIANGLE:32,POSITION_A_OFFSET:0,POSITION_B_OFFSET:4,POSITION_C_OFFSET:8,NORMAL_A_OFFSET:12,NORMAL_B_OFFSET:16,NORMAL_C_OFFSET:20},ni=Q.FLOATS_PER_TRIANGLE,ri=16,ii=-1,ai=-2,oi=class{constructor(){this._bounds=null,this._boundsNodeCount=0}updateTrianglePositions(e,t,n){let r=n.length;for(let i=0;i<r;i++){let r=n[i],a=i*ni,o=r*9,s=t[o],c=t[o+1],l=t[o+2],u=t[o+3],d=t[o+4],f=t[o+5],p=t[o+6],m=t[o+7],h=t[o+8];e[a+Q.POSITION_A_OFFSET]=s,e[a+Q.POSITION_A_OFFSET+1]=c,e[a+Q.POSITION_A_OFFSET+2]=l,e[a+Q.POSITION_B_OFFSET]=u,e[a+Q.POSITION_B_OFFSET+1]=d,e[a+Q.POSITION_B_OFFSET+2]=f,e[a+Q.POSITION_C_OFFSET]=p,e[a+Q.POSITION_C_OFFSET+1]=m,e[a+Q.POSITION_C_OFFSET+2]=h;let g=u-s,_=d-c,v=f-l,y=p-s,b=m-c,x=h-l,S=_*x-v*b,C=v*y-g*x,w=g*b-_*y;e[a+Q.NORMAL_A_OFFSET]=S,e[a+Q.NORMAL_A_OFFSET+1]=C,e[a+Q.NORMAL_A_OFFSET+2]=w,e[a+Q.NORMAL_B_OFFSET]=S,e[a+Q.NORMAL_B_OFFSET+1]=C,e[a+Q.NORMAL_B_OFFSET+2]=w,e[a+Q.NORMAL_C_OFFSET]=S,e[a+Q.NORMAL_C_OFFSET+1]=C,e[a+Q.NORMAL_C_OFFSET+2]=w}}refitRange(e,t,n,r){r>this._boundsNodeCount&&(this._bounds=new Float32Array(r*6),this._boundsNodeCount=r);let i=this._bounds,a=n+r;for(let r=a-1;r>=n;r--){let a=r*ri,o=(r-n)*6;if(e[a+3]===ii){let n=e[a],r=e[a+1],s=1/0,c=1/0,l=1/0,u=-1/0,d=-1/0,f=-1/0;for(let e=0;e<r;e++){let r=(n+e)*ni,i=t[r+Q.POSITION_A_OFFSET],a=t[r+Q.POSITION_A_OFFSET+1],o=t[r+Q.POSITION_A_OFFSET+2],p=t[r+Q.POSITION_B_OFFSET],m=t[r+Q.POSITION_B_OFFSET+1],h=t[r+Q.POSITION_B_OFFSET+2],g=t[r+Q.POSITION_C_OFFSET],_=t[r+Q.POSITION_C_OFFSET+1],v=t[r+Q.POSITION_C_OFFSET+2];s=Math.min(s,i,p,g),c=Math.min(c,a,m,_),l=Math.min(l,o,h,v),u=Math.max(u,i,p,g),d=Math.max(d,a,m,_),f=Math.max(f,o,h,v)}i[o]=s,i[o+1]=c,i[o+2]=l,i[o+3]=u,i[o+4]=d,i[o+5]=f}else{let t=e[a+3],r=e[a+7],s=(t-n)*6,c=(r-n)*6,l=i[s],u=i[s+1],d=i[s+2],f=i[s+3],p=i[s+4],m=i[s+5],h=i[c],g=i[c+1],_=i[c+2],v=i[c+3],y=i[c+4],b=i[c+5];e[a]=l,e[a+1]=u,e[a+2]=d,e[a+4]=f,e[a+5]=p,e[a+6]=m,e[a+8]=h,e[a+9]=g,e[a+10]=_,e[a+12]=v,e[a+13]=y,e[a+14]=b,i[o]=Math.min(l,h),i[o+1]=Math.min(u,g),i[o+2]=Math.min(d,_),i[o+3]=Math.max(f,v),i[o+4]=Math.max(p,y),i[o+5]=Math.max(m,b)}}}refit(e,t,n){n!==this._boundsNodeCount&&(this._bounds=new Float32Array(n*6),this._boundsNodeCount=n);let r=this._bounds;for(let i=n-1;i>=0;i--){let n=i*ri,a=i*6,o=e[n+3];if(o===ii){let i=e[n],o=e[n+1],s=1/0,c=1/0,l=1/0,u=-1/0,d=-1/0,f=-1/0;for(let e=0;e<o;e++){let n=(i+e)*ni,r=t[n+Q.POSITION_A_OFFSET],a=t[n+Q.POSITION_A_OFFSET+1],o=t[n+Q.POSITION_A_OFFSET+2],p=t[n+Q.POSITION_B_OFFSET],m=t[n+Q.POSITION_B_OFFSET+1],h=t[n+Q.POSITION_B_OFFSET+2],g=t[n+Q.POSITION_C_OFFSET],_=t[n+Q.POSITION_C_OFFSET+1],v=t[n+Q.POSITION_C_OFFSET+2];s=Math.min(s,r,p,g),c=Math.min(c,a,m,_),l=Math.min(l,o,h,v),u=Math.max(u,r,p,g),d=Math.max(d,a,m,_),f=Math.max(f,o,h,v)}r[a]=s,r[a+1]=c,r[a+2]=l,r[a+3]=u,r[a+4]=d,r[a+5]=f}else if(o===ai){let t=e[n]*6;r[a]=r[t],r[a+1]=r[t+1],r[a+2]=r[t+2],r[a+3]=r[t+3],r[a+4]=r[t+4],r[a+5]=r[t+5]}else{let t=e[n+3],i=e[n+7],o=t*6,s=i*6,c=r[o],l=r[o+1],u=r[o+2],d=r[o+3],f=r[o+4],p=r[o+5],m=r[s],h=r[s+1],g=r[s+2],_=r[s+3],v=r[s+4],y=r[s+5];e[n]=c,e[n+1]=l,e[n+2]=u,e[n+4]=d,e[n+5]=f,e[n+6]=p,e[n+8]=m,e[n+9]=h,e[n+10]=g,e[n+12]=_,e[n+13]=v,e[n+14]=y,r[a]=Math.min(c,m),r[a+1]=Math.min(l,h),r[a+2]=Math.min(u,g),r[a+3]=Math.max(d,_),r[a+4]=Math.max(f,v),r[a+5]=Math.max(p,y)}}}},si=``+(typeof document>`u`&&typeof location>`u`?require(`url`).pathToFileURL(__dirname+`/assets/BVHSubtreeWorker-D9GImjGj.js`).href:new URL(`assets/BVHSubtreeWorker-D9GImjGj.js`,typeof document>`u`?location.href:document.currentScript&&document.currentScript.tagName.toUpperCase()===`SCRIPT`&&document.currentScript.src||document.baseURI).href),ci=32,li=5e4,ui=8;function di(e,t,n,r){let i=e.byteLength/(ci*4),a=Math.min(navigator.hardwareConcurrency||4,ui),o=Math.ceil(Math.log2(a*2.5+1));return console.log(`[ParallelBVH] Parallel build: ${i.toLocaleString()} triangles, ${a} workers, parallelDepth=${o}`),new Promise((s,c)=>{(async()=>{let c=new SharedArrayBuffer(e.byteLength);new Float32Array(c).set(e);let l=new SharedArrayBuffer(i*3*4),u=new SharedArrayBuffer(i*3*4),d=new SharedArrayBuffer(i*3*4),f=new SharedArrayBuffer(i*4),p=new SharedArrayBuffer(i*4),m=new SharedArrayBuffer(i*ci*4),h;try{h=new Worker(Zr,{type:`module`})}catch(e){if(e.name!==`SecurityError`)throw e;h=await Je(Zr)}let g=null,_=[h],v={id:null},y=!1,b=()=>{v.id&&=(clearTimeout(v.id),null);for(let e of _)try{e.terminate()}catch{}},x=e=>{if(y)return;y=!0,console.warn(`[ParallelBVH] Parallel build failed (${e}), falling back to single worker`),b();let i=new ArrayBuffer(c.byteLength);new Float32Array(i).set(new Float32Array(c)),s(pi(new Float32Array(i),t,n,r))};h.onerror=e=>{x(`coordinator error: ${e.message}`)},h.onmessage=e=>{let t=e.data;if(t.error||t.type===`error`){x(t.error);return}if(t.type===`progress`&&n){n(Math.floor(t.progress*.3));return}if(t.type===`phase1Result`){g=t.splitStats,fi(t,a,c,l,u,d,f,m,i,n,h,_,b,x,s,v,r).catch(e=>x(e.message));return}if(t.type===`assembleResult`){y=!0,b();let e=new Float32Array(m);s({bvhData:t.bvhData,bvhRoot:!0,reorderedTriangles:e,originalToBvh:t.originalToBvh||null,splitStats:g||{}});return}},n&&n(0),h.postMessage({type:`buildPhase1`,sharedTriangleData:c,sharedCentroids:l,sharedBMin:u,sharedBMax:d,sharedIndices:f,sharedMortonCodes:p,triangleCount:i,depth:t,parallelDepth:o,reportProgress:!!n,treeletOptimization:r.treeletOptimization})})().catch(e=>{console.warn(`[ParallelBVH] Parallel build setup failed:`,e),c(e)})})}async function fi(e,t,n,r,i,a,o,s,c,l,u,d,f,p,m,h,g){let{topFlatData:_,topNodeCount:v,frontierTasks:y,frontierMap:b}=e;if(!y||y.length===0){console.log(`[ParallelBVH] No frontier tasks, assembling with top-level tree only`),u.postMessage({type:`assemble`,topFlatData:_,topNodeCount:v,frontierMap:[],subtreeResults:[],sharedTriangleData:n,sharedIndices:o,sharedReorderBuffer:s,triangleCount:c},[_.buffer]);return}console.log(`[ParallelBVH] Phase 2: distributing ${y.length} tasks across ${t} workers`);let x=[...y].sort((e,t)=>t.end-t.start-(e.end-e.start)),S=Array.from({length:Math.min(t,x.length)},()=>[]),C=Array(S.length).fill(0);for(let e of x){let t=0;for(let e=1;e<C.length;e++)C[e]<C[t]&&(t=e);S[t].push(e),C[t]+=e.end-e.start}let w=[],T=0,E=y.length,D=y.reduce((e,t)=>e+(t.end-t.start),0),O=0;h.id=setTimeout(()=>{p(`Phase 2 timeout (30s)`)},3e4);let k=()=>{clearTimeout(h.id),h.id=null,l&&l(85);let t=[];e.topFlatData&&e.topFlatData.buffer&&t.push(e.topFlatData.buffer);for(let e of w)e.flatData&&e.flatData.buffer&&t.push(e.flatData.buffer);u.postMessage({type:`assemble`,topFlatData:e.topFlatData,topNodeCount:v,frontierMap:b,subtreeResults:w,sharedTriangleData:n,sharedIndices:o,sharedReorderBuffer:s,triangleCount:c},t)},A=S.length;for(let e=0;e<A;e++){let t=S[e];if(t.length===0)continue;let s;try{s=new Worker(si,{type:`module`})}catch(e){if(e.name!==`SecurityError`)throw e;s=await Je(si)}d.push(s),s.onerror=e=>{p(`subtree worker error: ${e.message}`)},s.onmessage=e=>{let t=e.data;if(t.type===`error`){p(`subtree task ${t.taskId} error: ${t.error}`);return}if(t.type===`progress`&&l){let e=30+Math.floor(O/D*55);l(Math.min(e,85));return}if(t.type===`subtreeResult`){w.push({taskId:t.taskId,flatData:t.flatData,nodeCount:t.nodeCount});let e=y.find(e=>e.taskId===t.taskId);if(e&&(O+=e.end-e.start),T++,l){let e=30+Math.floor(O/D*55);l(Math.min(e,85))}if(T===E){for(let e of d)if(e!==u)try{e.terminate()}catch{}k()}}},s.postMessage({tasks:t,sharedTriangleData:n,sharedCentroids:r,sharedBMin:i,sharedBMax:a,sharedIndices:o,triangleCount:c,maxLeafSize:g.maxLeafSize,numBins:g.numBins,maxBins:g.maxBins,minBins:g.minBins,treeletConfig:g.treeletOptimization,reinsertionConfig:g.reinsertionOptimization,reportProgress:!!l})}}function pi(e,t,n,r){return new Promise((i,a)=>{(async()=>{let o;try{o=new Worker(Zr,{type:`module`})}catch(e){if(e.name!==`SecurityError`)throw e;o=await Je(Zr)}let s=e.byteLength/(ci*4),c=typeof SharedArrayBuffer<`u`?new SharedArrayBuffer(s*ci*4):null;o.onmessage=e=>{let{bvhData:t,triangles:r,originalToBvh:s,error:l,progress:u,treeletStats:d}=e.data;if(l){o.terminate(),a(Error(l));return}if(u!==void 0&&n){n(u);return}o.terminate(),i({bvhData:t,bvhRoot:!0,reorderedTriangles:c?new Float32Array(c):r,originalToBvh:s||null,splitStats:d||{}})},o.onerror=e=>{o.terminate(),a(e)};let l=e.buffer;o.postMessage({triangleData:l,triangleByteOffset:e.byteOffset,triangleByteLength:e.byteLength,triangleCount:s,depth:t,reportProgress:!!n,sharedReorderBuffer:c,treeletOptimization:r.treeletOptimization,reinsertionOptimization:r.reinsertionOptimization},[l])})().catch(e=>{console.warn(`[ParallelBVH] Single worker fallback failed:`,e),a(e)})})}function mi(e){return typeof Worker<`u`&&typeof SharedArrayBuffer<`u`&&e>=li}var hi=16,gi=class{constructor(){this.minX=0,this.minY=0,this.minZ=0,this.maxX=0,this.maxY=0,this.maxZ=0,this.leftChild=null,this.rightChild=null,this.entryIndex=-1}},_i=class{constructor(){this._flatBuffer=null,this._flatBufferCapacity=0}build(e){if(e.length===0)return{root:null,nodeCount:0};let t=[];for(let n=0;n<e.length;n++)t.push(n);let n=this._buildRecursive(e,t);return{root:n,nodeCount:this._countNodes(n)}}_buildRecursive(e,t){let n=new gi;if(t.length===1){let r=e[t[0]].worldAABB;return n.minX=r.minX,n.minY=r.minY,n.minZ=r.minZ,n.maxX=r.maxX,n.maxY=r.maxY,n.maxZ=r.maxZ,n.entryIndex=t[0],n}let r=1/0,i=1/0,a=1/0,o=-1/0,s=-1/0,c=-1/0;for(let n of t){let t=e[n].worldAABB;t.minX<r&&(r=t.minX),t.minY<i&&(i=t.minY),t.minZ<a&&(a=t.minZ),t.maxX>o&&(o=t.maxX),t.maxY>s&&(s=t.maxY),t.maxZ>c&&(c=t.maxZ)}if(n.minX=r,n.minY=i,n.minZ=a,n.maxX=o,n.maxY=s,n.maxZ=c,t.length===2)return n.leftChild=this._buildRecursive(e,[t[0]]),n.rightChild=this._buildRecursive(e,[t[1]]),n;let l=this._surfaceArea(r,i,a,o,s,c),u=1/0,d=0,f=0;if(l>0&&isFinite(l))for(let n=0;n<3;n++){let r=t.slice().sort((t,r)=>{let i=e[t].worldAABB,a=e[r].worldAABB;return this._centroid(i,n)-this._centroid(a,n)});for(let t=1;t<r.length;t++){let i=this._computeGroupAABB(e,r,0,t),a=this._computeGroupAABB(e,r,t,r.length),o=this._surfaceArea(i.minX,i.minY,i.minZ,i.maxX,i.maxY,i.maxZ),s=this._surfaceArea(a.minX,a.minY,a.minZ,a.maxX,a.maxY,a.maxZ),c=1+(o*t+s*(r.length-t))/l;c<u&&(u=c,d=n,f=t)}}if(f<=0||f>=t.length){d=0;let e=o-r,n=s-i,l=c-a;n>e&&n>l?d=1:l>e&&(d=2),f=t.length>>1}let p=t.slice().sort((t,n)=>this._centroid(e[t].worldAABB,d)-this._centroid(e[n].worldAABB,d)),m=p.slice(0,f),h=p.slice(f);return n.leftChild=this._buildRecursive(e,m),n.rightChild=this._buildRecursive(e,h),n}flatten(e,t){if(!e)return new Float32Array;let n=[],r=[e];for(;r.length>0;){let e=r.pop();e._flatIndex=n.length,n.push(e),e.rightChild&&r.push(e.rightChild),e.leftChild&&r.push(e.leftChild)}let i=n.length*hi;i>this._flatBufferCapacity&&(this._flatBuffer=new Float32Array(i),this._flatBufferCapacity=i);let a=this._flatBuffer;a.fill(0,0,i);for(let e=0;e<n.length;e++){let r=n[e],i=e*hi;if(r.leftChild){let e=r.leftChild,t=r.rightChild;a[i]=e.minX,a[i+1]=e.minY,a[i+2]=e.minZ,a[i+3]=e._flatIndex,a[i+4]=e.maxX,a[i+5]=e.maxY,a[i+6]=e.maxZ,a[i+7]=t._flatIndex,a[i+8]=t.minX,a[i+9]=t.minY,a[i+10]=t.minZ,a[i+12]=t.maxX,a[i+13]=t.maxY,a[i+14]=t.maxZ}else{let n=t[r.entryIndex];a[i]=n.blasOffset,a[i+1]=r.entryIndex,a[i+2]=n.visible===!1?0:1,a[i+3]=Ne.BLAS_POINTER_LEAF,n.tlasLeafIndex=e}}return a.subarray(0,i)}_centroid(e,t){return t===0?(e.minX+e.maxX)*.5:t===1?(e.minY+e.maxY)*.5:(e.minZ+e.maxZ)*.5}_surfaceArea(e,t,n,r,i,a){let o=r-e,s=i-t,c=a-n;return 2*(o*s+s*c+c*o)}_computeGroupAABB(e,t,n,r){let i=1/0,a=1/0,o=1/0,s=-1/0,c=-1/0,l=-1/0;for(let u=n;u<r;u++){let n=e[t[u]].worldAABB;n.minX<i&&(i=n.minX),n.minY<a&&(a=n.minY),n.minZ<o&&(o=n.minZ),n.maxX>s&&(s=n.maxX),n.maxY>c&&(c=n.maxY),n.maxZ>l&&(l=n.maxZ)}return{minX:i,minY:a,minZ:o,maxX:s,maxY:c,maxZ:l}}_countNodes(e){if(!e)return 0;let t=0,n=[e];for(;n.length>0;){let e=n.pop();t++,e.leftChild&&n.push(e.leftChild),e.rightChild&&n.push(e.rightChild)}return t}},vi=class{constructor(){this.entries=[],this.totalBLASNodes=0,this.tlasNodeCount=0}allocate(e){this.entries=Array(e).fill(null)}setEntry({meshIndex:e,blasNodeCount:t,triOffset:n,triCount:r,originalToBvhMap:i,bvhData:a}){this.entries[e]={meshIndex:e,blasOffset:0,blasNodeCount:t,triOffset:n,triCount:r,worldAABB:null,originalToBvhMap:i,bvhData:a,visible:!0,tlasLeafIndex:-1}}setVisibility(e,t){let n=this.entries[e];n&&(n.visible=t)}computeAABBs(e){for(let t of this.entries)t&&(t.worldAABB=this._readRootAABB(t.bvhData,t,e))}recomputeAABB(e,t,n){let r=this.entries[e],i=t.subarray(r.blasOffset*16,r.blasOffset*16+16);r.worldAABB=this._readRootAABB(i,r,n)}_readRootAABB(e,t,n){if(e[3]===-1)return this._computeAABBFromTriangles(t,n);let r=e[0],i=e[1],a=e[2],o=e[4],s=e[5],c=e[6],l=e[8],u=e[9],d=e[10],f=e[12],p=e[13],m=e[14];return{minX:Math.min(r,l),minY:Math.min(i,u),minZ:Math.min(a,d),maxX:Math.max(o,f),maxY:Math.max(s,p),maxZ:Math.max(c,m)}}_computeAABBFromTriangles(e,t){let n=G.FLOATS_PER_TRIANGLE,r=1/0,i=1/0,a=1/0,o=-1/0,s=-1/0,c=-1/0;for(let l=0;l<e.triCount;l++){let u=(e.triOffset+l)*n;for(let e=0;e<=8;e+=4){let n=t[u+e],l=t[u+e+1],d=t[u+e+2];n<r&&(r=n),l<i&&(i=l),d<a&&(a=d),n>o&&(o=n),l>s&&(s=l),d>c&&(c=d)}}return{minX:r,minY:i,minZ:a,maxX:o,maxY:s,maxZ:c}}assignOffsets(e){this.tlasNodeCount=e;let t=e;for(let e of this.entries)e&&(e.blasOffset=t,t+=e.blasNodeCount);this.totalBLASNodes=t-e}get totalNodeCount(){return this.tlasNodeCount+this.totalBLASNodes}get count(){return this.entries.length}clear(){this.entries=[],this.totalBLASNodes=0,this.tlasNodeCount=0}},yi=``+(typeof document>`u`&&typeof location>`u`?require(`url`).pathToFileURL(__dirname+`/assets/TexturesWorker-6mlBy4Dm.js`).href:new URL(`assets/TexturesWorker-6mlBy4Dm.js`,typeof document>`u`?location.href:document.currentScript&&document.currentScript.tagName.toUpperCase()===`SCRIPT`&&document.currentScript.src||document.baseURI).href),bi=class{constructor(){this.canvasContextPairs=[],this.maxPoolSize=Pe.CANVAS_POOL_SIZE}getCanvasWithContext(e,t,n=!1,r={}){let i={willReadFrequently:!0,alpha:!0,desynchronized:!0,...r},a=this.canvasContextPairs.pop();if(!a){let r;r=n&&typeof OffscreenCanvas<`u`?new OffscreenCanvas(e,t):document.createElement(`canvas`);let o=r.getContext(`2d`,i);a={canvas:r,context:o}}return a.canvas.width=e,a.canvas.height=t,a}releaseCanvasWithContext(e){this.canvasContextPairs.length<this.maxPoolSize&&(e.context.globalAlpha=1,e.context.globalCompositeOperation=`source-over`,e.context.imageSmoothingEnabled=!0,e.context.clearRect(0,0,e.canvas.width,e.canvas.height),e.canvas.width=1,e.canvas.height=1,this.canvasContextPairs.push(e))}getCanvas(e,t,n=!1){return this.getCanvasWithContext(e,t,n).canvas}getContext(e,t={}){let n=this.canvasContextPairs.find(t=>t.canvas===e);return n?n.context:e.getContext(`2d`,{willReadFrequently:!0,alpha:!0,desynchronized:!0,...t})}dispose(){this.canvasContextPairs=[]}},xi=class{constructor(e={}){this.pools=new Map,this.memoryUsage=0,this.maxMemoryUsage=e.maxMemory||Re.MAX_BUFFER_MEMORY,this.allocatedBuffers=new WeakMap,this.sizeStrategy=e.sizeStrategy||`adaptive`}getOptimalSize(e){switch(this.sizeStrategy){case`exact`:return e;case`power2`:return 2**Math.ceil(Math.log2(e));default:return e<1024?e:e<1024*1024?Math.ceil(e/1024)*1024:2**Math.ceil(Math.log2(e))}}getBuffer(e,t=Float32Array){let n=this.getOptimalSize(e),r=`${t.name}-${n}`,i=(this.pools.get(r)||[]).pop();if(!i)try{i=new t(n),this.memoryUsage+=i.byteLength,this.allocatedBuffers.set(i,!0)}catch{this.cleanup();try{i=new t(n),this.memoryUsage+=i.byteLength,this.allocatedBuffers.set(i,!0)}catch(e){let r=n*t.BYTES_PER_ELEMENT/(1024*1024),i=this.memoryUsage/(1024*1024);throw Error(`Buffer allocation failed: requested ${r.toFixed(1)}MB, current usage: ${i.toFixed(1)}MB, max: ${(this.maxMemoryUsage/(1024*1024)).toFixed(1)}MB. Original error: ${e.message}`)}}return this.memoryUsage>this.maxMemoryUsage*Re.CLEANUP_THRESHOLD&&this.cleanup(),this.checkMemoryHealth(),i.byteOffset+e*t.BYTES_PER_ELEMENT>i.buffer.byteLength&&(i=new t(e),this.memoryUsage+=i.byteLength),new t(i.buffer,i.byteOffset,e)}releaseBuffer(e,t=Float32Array){let n=(e.buffer.byteLength-e.byteOffset)/t.BYTES_PER_ELEMENT,r=this.getOptimalSize(n),i=`${t.name}-${r}`,a=this.pools.get(i)||[];a.length<Pe.BUFFER_POOL_SIZE?(a.push(new t(e.buffer,e.byteOffset,n)),this.pools.set(i,a)):this.allocatedBuffers.has(e)&&(this.memoryUsage-=e.byteLength,this.allocatedBuffers.delete(e))}cleanup(){let e=Array.from(this.pools.entries());e.slice(0,Math.floor(e.length/2)).forEach(([e,t])=>{t.forEach(e=>{this.allocatedBuffers.has(e)&&(this.memoryUsage-=e.byteLength,this.allocatedBuffers.delete(e))}),this.pools.delete(e)})}dispose(){this.pools.forEach(e=>{e.forEach(e=>{this.allocatedBuffers.has(e)&&this.allocatedBuffers.delete(e)})}),this.pools.clear(),this.memoryUsage=0}getMemoryStats(){return{currentUsage:this.memoryUsage,maxUsage:this.maxMemoryUsage,utilizationPercentage:this.memoryUsage/this.maxMemoryUsage*100,poolCount:this.pools.size,allocatedBufferCount:this.allocatedBuffers&&this.allocatedBuffers.size||0}}checkMemoryHealth(){let e=this.getMemoryStats();return e.utilizationPercentage>90?(console.warn(`Memory pool critical: ${e.utilizationPercentage.toFixed(1)}% used (${(e.currentUsage/(1024*1024)).toFixed(1)}MB / ${(e.maxUsage/(1024*1024)).toFixed(1)}MB)`),`critical`):e.utilizationPercentage>70?(console.warn(`Memory pool high: ${e.utilizationPercentage.toFixed(1)}% used (${(e.currentUsage/(1024*1024)).toFixed(1)}MB / ${(e.maxUsage/(1024*1024)).toFixed(1)}MB)`),`high`):`normal`}},Si=class{constructor(e=Pe.CACHE_SIZE_LIMIT){this.cache=new Map,this.accessOrder=[],this.maxSize=e}generateHash(e){let t=``;for(let n of e)if(n?.image){let e=n.image.width||0,r=n.image.height||0,i=n.image.src||n.uuid||``,a=n.flipY===!1?`n`:`f`;t+=`${e}x${r}_${i.slice(-8)}_${a}_`}return t+e.length}get(e){if(this.cache.has(e)){let t=this.accessOrder.indexOf(e);return t>-1&&this.accessOrder.splice(t,1),this.accessOrder.push(e),this.cache.get(e)}return null}set(e,t){if(this.cache.has(e)){let t=this.accessOrder.indexOf(e);t>-1&&this.accessOrder.splice(t,1)}else this.cache.size>=this.maxSize&&this.evictLRU();this.cache.set(e,t),this.accessOrder.push(e)}evictLRU(){if(this.accessOrder.length>0){let e=this.accessOrder.shift(),t=this.cache.get(e);t&&t.dispose&&t.dispose(),this.cache.delete(e)}}dispose(){this.cache.forEach(e=>{e&&e.dispose&&e.dispose()}),this.cache.clear(),this.accessOrder=[]}},Ci=class{constructor(e={}){this.useWorkers=typeof Worker<`u`,this.maxConcurrentWorkers=Pe.MAX_CONCURRENT_WORKERS,this.activeWorkers=0,this.maxTextureSize=this._clampTextureSize(e.maxTextureSize??Pe.DEFAULT_MAX_TEXTURE_SIZE),this.canvasPool=new bi,this.bufferPool=new xi({maxMemory:e.maxBufferMemory||Re.MAX_BUFFER_MEMORY,sizeStrategy:e.bufferSizeStrategy||`adaptive`}),this.textureCache=new Si,this.capabilities=this.detectCapabilities(),this.optimalMethod=this.selectOptimalMethod()}_clampTextureSize(e){let t=Math.floor(Number(e)||Pe.DEFAULT_MAX_TEXTURE_SIZE);return Math.max(Pe.MIN_TEXTURE_WIDTH,Math.min(t,Pe.MAX_TEXTURE_SIZE))}setMaxTextureSize(e){let t=this._clampTextureSize(e);return t===this.maxTextureSize?t:(this.maxTextureSize=t,this.textureCache?.dispose(),this.textureCache=new Si,this.maxTextureSize)}detectCapabilities(){return{offscreenCanvas:typeof OffscreenCanvas<`u`,imageBitmap:typeof createImageBitmap<`u`,workers:typeof Worker<`u`,hardwareConcurrency:navigator.hardwareConcurrency||4}}selectOptimalMethod(){return this.capabilities.workers&&this.capabilities.offscreenCanvas?`worker-offscreen`:this.capabilities.imageBitmap?`imageBitmap`:`canvas`}async createTexturesToDataTexture(e){if(!e||e.length===0)return null;let t=this.textureCache.generateHash(e),n=this.textureCache.get(t);if(n)return n;let{normalized:r,bitmapsToClose:i}=await this._normalizeTexturesForProcessing(e),a=this.selectProcessingStrategy(r),o;try{switch(a.method){case`worker-direct`:o=await this.processWithWorkerDirect(r);break;case`worker-chunked`:o=await this.processWithWorkerChunked(r,a.chunkSize);break;case`main-batch`:o=await this.processOnMainThreadBatch(r,a.batchSize);break;case`main-streaming`:o=await this.processOnMainThreadStreaming(r);break;default:o=await this.processOnMainThreadSync(r)}return o&&this.textureCache.set(t,o),o}catch(e){return console.warn(`Texture processing failed, trying fallback:`,e),await this.processOnMainThreadSync(r)}finally{for(let e of i)e.close()}}selectProcessingStrategy(e){let t=e.reduce((e,t)=>e+(t.image?.width||0)*(t.image?.height||0),0),n=t*4;return this.capabilities.workers&&n>Re.MAX_TEXTURE_MEMORY?{method:`worker-chunked`,chunkSize:Math.max(1,Math.floor(e.length/4))}:this.capabilities.workers&&t>2097152?{method:`worker-direct`}:t>524288?{method:`main-batch`,batchSize:Math.min(4,e.length)}:e.length>8?{method:`main-streaming`}:{method:`main-sync`}}async processWithWorkerDirect(e){for(;this.activeWorkers>=this.maxConcurrentWorkers;)await new Promise(e=>setTimeout(e,10));this.activeWorkers++;try{let t;try{t=new Worker(yi,{type:`module`})}catch(e){if(e.name!==`SecurityError`)throw e;t=await Je(yi)}let n=await this.prepareTexturesForWorkerDirect(e),r=await new Promise((e,r)=>{t.onmessage=t=>{t.data.error?r(Error(t.data.error)):e(t.data)},t.onerror=r;let i=[];n.forEach(e=>{e.data instanceof ArrayBuffer?i.push(e.data):e.bitmap&&i.push(e.bitmap)}),t.postMessage({textures:n,maxTextureSize:this.maxTextureSize,method:`direct-transfer`},i)});return t.terminate(),this.createDataArrayTextureFromResult(r)}finally{this.activeWorkers--}}async prepareTexturesForWorkerDirect(e){let t=[];for(let n of e){if(!n?.image)continue;let e=n.flipY!==!1;try{if(typeof createImageBitmap<`u`&&n.image instanceof HTMLImageElement){let r=await createImageBitmap(n.image,{imageOrientation:e?`flipY`:`none`});t.push({bitmap:r,width:n.image.width,height:n.image.height,isDirect:!0})}else{let r=n.image.width,i=n.image.height,a=await createImageBitmap(n.image,{imageOrientation:e?`flipY`:`none`}),o=this.canvasPool.getCanvasWithContext(r,i);o.context.drawImage(a,0,0),a.close();let s=o.context.getImageData(0,0,r,i);t.push({data:s.data.buffer,width:r,height:i,isImageData:!0}),this.canvasPool.releaseCanvasWithContext(o)}}catch(e){console.warn(`Failed to prepare texture for worker:`,e)}}return t}async processWithWorkerChunked(e,t){let n=[];for(let r=0;r<e.length;r+=t){let i=e.slice(r,r+t),a=await this.processWithWorkerDirect(i);n.push(a)}return this.combineTextureResults(n)}async processOnMainThreadBatch(e,t){let n=e.filter(e=>e?.image);if(n.length===0)return this.createFallbackTexture();let{maxWidth:r,maxHeight:i}=this.calculateOptimalDimensions(n),a=n.length,o=this.bufferPool.getBuffer(r*i*a*4,Uint8Array);for(let e=0;e<n.length;e+=t){let a=Math.min(e+t,n.length),s=[];for(let t=e;t<a;t++){let e=n[t],a=e.flipY!==!1,o=createImageBitmap(e.image,{resizeWidth:r,resizeHeight:i,resizeQuality:`high`,imageOrientation:a?`flipY`:`none`});s.push(o.then(e=>({bitmap:e,index:t})))}let c=await Promise.all(s),l=this.canvasPool.getCanvasWithContext(r,i);l.context.imageSmoothingEnabled=!1;for(let{bitmap:e,index:t}of c){l.context.clearRect(0,0,r,i),l.context.drawImage(e,0,0);let n=l.context.getImageData(0,0,r,i),a=r*i*4*t;o.set(n.data,a),e.close()}this.canvasPool.releaseCanvasWithContext(l)}return this.createDataArrayTextureFromBuffer(o,r,i,a)}async processOnMainThreadStreaming(e){let t=e.filter(e=>e?.image);if(t.length===0)return this.createFallbackTexture();let{maxWidth:n,maxHeight:r}=this.calculateOptimalDimensions(t),i=t.length,a=this.bufferPool.getBuffer(n*r*i*4,Uint8Array),o=this.canvasPool.getCanvasWithContext(n,r);o.context.imageSmoothingEnabled=!0,o.context.imageSmoothingQuality=`high`;for(let e=0;e<t.length;e++){let i=t[e],s=await createImageBitmap(i.image,{resizeWidth:n,resizeHeight:r,resizeQuality:`high`,imageOrientation:i.flipY===!1?`none`:`flipY`});o.context.clearRect(0,0,n,r),o.context.drawImage(s,0,0),s.close();let c=o.context.getImageData(0,0,n,r),l=n*r*4*e;a.set(c.data,l),e%Re.STREAM_BATCH_SIZE===0&&await new Promise(e=>setTimeout(e,0))}return this.canvasPool.releaseCanvasWithContext(o),this.createDataArrayTextureFromBuffer(a,n,r,i)}async processOnMainThreadSync(e){let t=e.filter(e=>e?.image);if(t.length===0)return this.createFallbackTexture();let{maxWidth:n,maxHeight:r}=this.calculateOptimalDimensions(t),i=t.length,a=this.bufferPool.getBuffer(n*r*i*4,Uint8Array),o=this.canvasPool.getCanvasWithContext(n,r);o.context.imageSmoothingEnabled=!0,o.context.imageSmoothingQuality=`high`;for(let e=0;e<t.length;e++){let i=t[e],s=await createImageBitmap(i.image,{resizeWidth:n,resizeHeight:r,resizeQuality:`high`,imageOrientation:i.flipY===!1?`none`:`flipY`});o.context.clearRect(0,0,n,r),o.context.drawImage(s,0,0),s.close();let c=o.context.getImageData(0,0,n,r),l=n*r*4*e;a.set(c.data,l)}return this.canvasPool.releaseCanvasWithContext(o),this.createDataArrayTextureFromBuffer(a,n,r,i)}createMaterialRawData(e){let t=K.FLOATS_PER_MATERIAL,n=e.length,r=n*t,i=new Float32Array(r);for(let r=0;r<n;r++){let n=e[r],a=r*t,o=n.mapMatrix??Fe,s=n.normalMapMatrices??Fe,c=n.roughnessMapMatrices??Fe,l=n.metalnessMapMatrices??Fe,u=n.emissiveMapMatrices??Fe,d=n.bumpMapMatrices??Fe,f=n.displacementMapMatrices??Fe,p=[n.ior,n.transmission,n.thickness,n.emissiveIntensity,n.attenuationColor.r,n.attenuationColor.g,n.attenuationColor.b,n.attenuationDistance,n.opacity,n.side,n.transparent,n.alphaTest,n.alphaMode,n.depthWrite,n.normalScale?.x??1,n.normalScale?.y??1,n.color.r,n.color.g,n.color.b,n.metalness,n.emissive.r,n.emissive.g,n.emissive.b,n.roughness,n.map,n.normalMap,n.roughnessMap,n.metalnessMap,n.emissiveMap,n.bumpMap,n.clearcoat,n.clearcoatRoughness,n.dispersion,n.visible,n.sheen,n.sheenRoughness,n.sheenColor.r,n.sheenColor.g,n.sheenColor.b,1,n.specularIntensity,n.specularColor.r,n.specularColor.g,n.specularColor.b,n.iridescence,n.iridescenceIOR,n.iridescenceThicknessRange[0],n.iridescenceThicknessRange[1],n.bumpScale,n.displacementScale,n.displacementMap,0,o[0],o[1],o[2],o[3],o[4],o[5],o[6],1,s[0],s[1],s[2],s[3],s[4],s[5],s[6],1,c[0],c[1],c[2],c[3],c[4],c[5],c[6],1,l[0],l[1],l[2],l[3],l[4],l[5],l[6],1,u[0],u[1],u[2],u[3],u[4],u[5],u[6],1,d[0],d[1],d[2],d[3],d[4],d[5],d[6],1,f[0],f[1],f[2],f[3],f[4],f[5],f[6],1,n.subsurfaceColor?.r??1,n.subsurfaceColor?.g??1,n.subsurfaceColor?.b??1,n.subsurface??0,n.subsurfaceRadius?.[0]??1,n.subsurfaceRadius?.[1]??.2,n.subsurfaceRadius?.[2]??.1,n.subsurfaceRadiusScale??1,n.subsurfaceAnisotropy??0,0,0,0];i.set(p,a)}return i}createBVHRawData(e){let t=[],n=e=>{let r=t.length;if(t.push(e),e.leftChild){let t=n(e.leftChild),r=n(e.rightChild);e.leftChild=t,e.rightChild=r}return r};n(e);let r=Pe.VEC4_PER_BVH_NODE*Pe.FLOATS_PER_VEC4,i=t.length*r,a=new Float32Array(i);for(let e=0;e<t.length;e++){let n=e*r,i=t[e];if(i.leftChild!==null){let e=i.leftChild,r=i.rightChild,o=t[e],s=t[r];a[n]=o.boundsMin.x,a[n+1]=o.boundsMin.y,a[n+2]=o.boundsMin.z,a[n+3]=e,a[n+4]=o.boundsMax.x,a[n+5]=o.boundsMax.y,a[n+6]=o.boundsMax.z,a[n+7]=r,a[n+8]=s.boundsMin.x,a[n+9]=s.boundsMin.y,a[n+10]=s.boundsMin.z,a[n+12]=s.boundsMax.x,a[n+13]=s.boundsMax.y,a[n+14]=s.boundsMax.z}else a[n]=i.triangleOffset,a[n+1]=i.triangleCount,a[n+3]=-1}return a}async createMaterialTextures(e){let{materials:t,maps:n,normalMaps:i,bumpMaps:a,roughnessMaps:o,metalnessMaps:s,emissiveMaps:c,displacementMaps:l}=e;console.log(`[TextureCreator] Creating material textures only`);let u=performance.now();try{if(!t||t.length===0)throw Error(`No materials provided for texture creation`);this.textureCache.dispose(),this.textureCache=new Si;let e=[];n&&n.length>0&&e.push(this.createTexturesToDataTexture(n).then(e=>({type:`albedo`,texture:e})).catch(e=>(console.warn(`Failed to create albedo textures:`,e),{type:`albedo`,texture:null}))),i&&i.length>0&&e.push(this.createTexturesToDataTexture(i).then(e=>({type:`normal`,texture:e})).catch(e=>(console.warn(`Failed to create normal textures:`,e),{type:`normal`,texture:null}))),a&&a.length>0&&e.push(this.createTexturesToDataTexture(a).then(e=>({type:`bump`,texture:e})).catch(e=>(console.warn(`Failed to create bump textures:`,e),{type:`bump`,texture:null}))),o&&o.length>0&&e.push(this.createTexturesToDataTexture(o).then(e=>({type:`roughness`,texture:e})).catch(e=>(console.warn(`Failed to create roughness textures:`,e),{type:`roughness`,texture:null}))),s&&s.length>0&&e.push(this.createTexturesToDataTexture(s).then(e=>({type:`metalness`,texture:e})).catch(e=>(console.warn(`Failed to create metalness textures:`,e),{type:`metalness`,texture:null}))),c&&c.length>0&&e.push(this.createTexturesToDataTexture(c).then(e=>({type:`emissive`,texture:e})).catch(e=>(console.warn(`Failed to create emissive textures:`,e),{type:`emissive`,texture:null}))),l&&l.length>0&&e.push(this.createTexturesToDataTexture(l).then(e=>({type:`displacement`,texture:e})).catch(e=>(console.warn(`Failed to create displacement textures:`,e),{type:`displacement`,texture:null})));let d=await Promise.allSettled(e),f={};d.forEach(e=>{if(e.status===`fulfilled`&&e.value){let{type:t,texture:n}=e.value;if(n)switch(t){case`albedo`:n.colorSpace=r.SRGBColorSpace,f.albedoTexture=n;break;case`normal`:f.normalTexture=n;break;case`bump`:f.bumpTexture=n;break;case`roughness`:f.roughnessTexture=n;break;case`metalness`:f.metalnessTexture=n;break;case`emissive`:n.colorSpace=r.SRGBColorSpace,f.emissiveTexture=n;break;case`displacement`:f.displacementTexture=n;break}}});let p=performance.now()-u;return console.log(`[TextureCreator] Material texture creation complete (${p.toFixed(2)}ms)`),f}catch(e){throw console.error(`[TextureCreator] Material texture creation error:`,e),Error(`Material texture creation failed: ${e.message}`)}}calculateOptimalDimensions(e){let t=0,n=0;for(let r of e)t=Math.max(t,r.image.width),n=Math.max(n,r.image.height);for(t=2**Math.ceil(Math.log2(t)),n=2**Math.ceil(Math.log2(n));t>this.maxTextureSize||n>this.maxTextureSize;)t=Math.max(1,Math.floor(t/2)),n=Math.max(1,Math.floor(n/2));return{maxWidth:t,maxHeight:n}}createDataArrayTextureFromResult(e){let t=(e.data instanceof ArrayBuffer,new Uint8Array(e.data));return this.createDataArrayTextureFromBuffer(t,e.width,e.height,e.depth)}createDataArrayTextureFromBuffer(e,t,n,i){let a=new r.DataArrayTexture(e,t,n,i);a.minFilter=r.LinearFilter,a.magFilter=r.LinearFilter,a.format=r.RGBAFormat,a.type=r.UnsignedByteType,a.needsUpdate=!0,a.generateMipmaps=!1,a.userData={buffer:e,bufferType:Uint8Array};let o=a.dispose.bind(a);return a.dispose=()=>{a.userData.buffer&&(this.bufferPool.releaseBuffer(a.userData.buffer,a.userData.bufferType),a.userData.buffer=null),o()},a}async _normalizeTexturesForProcessing(e){let t=[],n=[],i=[];for(let n of e)if(n?.image){if(n.isCompressedTexture&&n.format===r.RGBAFormat&&n.mipmaps?.[0]?.data){let e=n.mipmaps[0],r=t.length;t.push(null),i.push({index:r,flipY:n.flipY,promise:wi(e.data,e.width,e.height)});continue}if(n.isCompressedTexture){console.warn(`[TextureCreator] GPU-compressed texture in mixed group — using placeholder`),t.push(null);continue}if(n.image.data&&!(n.image instanceof HTMLImageElement)&&!(n.image instanceof HTMLCanvasElement)&&!(typeof ImageBitmap<`u`&&n.image instanceof ImageBitmap)){let e=t.length;t.push(null),i.push({index:e,flipY:n.flipY,promise:wi(n.image.data,n.image.width,n.image.height)});continue}t.push(n)}if(i.length>0){let e=await Promise.allSettled(i.map(e=>e.promise));for(let r=0;r<i.length;r++){let{index:a,flipY:o}=i[r],s=e[r];if(s.status===`fulfilled`){let e=s.value;n.push(e),t[a]={image:e,flipY:o}}else console.warn(`[TextureCreator] Failed to create ImageBitmap:`,s.reason)}}for(let e=0;e<t.length;e++)if(t[e]===null){let r=new Uint8ClampedArray([255,255,255,255]),i=await createImageBitmap(new ImageData(r,1,1));n.push(i),t[e]={image:i,flipY:!1}}return{normalized:t,bitmapsToClose:n}}createFallbackTexture(){let e=new r.DataArrayTexture(new Uint8Array([255,255,255,255]),1,1,1);return e.minFilter=r.LinearFilter,e.magFilter=r.LinearFilter,e.format=r.RGBAFormat,e.type=r.UnsignedByteType,e.needsUpdate=!0,e.generateMipmaps=!1,e}combineTextureResults(e){return e[0]}dispose(){this.canvasPool.dispose(),this.bufferPool.dispose(),this.textureCache.dispose()}};function wi(e,t,n){let r=new Uint8ClampedArray(e.buffer,e.byteOffset,e.byteLength);return createImageBitmap(new ImageData(r,t,n))}var Ti=128,Ei=class{constructor(){this._vectorPool={vec3:Array(9).fill().map(()=>new r.Vector3),vec2:[,,,,,,].fill().map(()=>new r.Vector2)},this._matrixPool={mat3:new r.Matrix3,mat4:new r.Matrix4},this.resetArrays(),this.triangleCount=0,this.currentTriangleIndex=0}_getVec3(e=0){return this._vectorPool.vec3[e%this._vectorPool.vec3.length]}_getVec2(e=0){return this._vectorPool.vec2[e%this._vectorPool.vec2.length]}extract(e){return this.resetArrays(),this._triangleCapacity=1024,this.triangleData=new Float32Array(this._triangleCapacity*G.FLOATS_PER_TRIANGLE),this.currentTriangleIndex=0,this.traverseObject(e),this.logStats(),this.getExtractedData()}_ensureCapacity(e){if(e<=this._triangleCapacity)return;let t=this._triangleCapacity;for(;t<e;)t*=2;let n=new Float32Array(t*G.FLOATS_PER_TRIANGLE);n.set(this.triangleData),this.triangleData=n,this._triangleCapacity=t}traverseObject(e){if(e.isMesh?this.processMesh(e):e.isDirectionalLight?this.directionalLights.push(e):e.isCamera&&this.cameras.push(e),e.children)for(let t of e.children)this.traverseObject(t)}processMesh(e){if(!e.geometry||!e.material){console.warn(`Skipping mesh with missing geometry or material:`,e);return}let t=this.processMaterial(e.material);e.userData.materialIndex=t;let n=this.meshes.length;this.meshes.push(e),e.userData.meshIndex=n;let r=this.currentTriangleIndex;this.extractGeometry(e,t,n),this.meshTriangleRanges.push({start:r,count:this.currentTriangleIndex-r})}processMaterial(e){let t=this._materialUuidMap.get(e.uuid)??-1;if(t===-1){e.depthWrite===!1&&(e.depthWrite=!0,console.warn(`Depth write is disabled in material, enabling it for rastered rendering`));let n=this.createMaterialObject(e);this.materials.push(n),t=this.materials.length-1,this._materialUuidMap.set(e.uuid,t),n.clearcoat>0&&(this.sceneFeatures.hasClearcoat=!0),n.transmission>0&&(this.sceneFeatures.hasTransmission=!0),n.dispersion>0&&(this.sceneFeatures.hasDispersion=!0),n.iridescence>0&&(this.sceneFeatures.hasIridescence=!0),n.sheen>0&&(this.sceneFeatures.hasSheen=!0),(n.transparent||n.opacity<1||n.alphaTest>0)&&(this.sceneFeatures.hasTransparency=!0),n.subsurface>0&&(this.sceneFeatures.hasSubsurface=!0),[n.clearcoat>0,n.transmission>0,n.iridescence>0,n.sheen>0].filter(Boolean).length>=2&&(this.sceneFeatures.hasMultiLobeMaterials=!0)}return t}getMaterialAlphaMode(e){if(e.userData?.gltfExtensions?.KHR_materials_unlit?.alphaMode){let t=e.userData.gltfExtensions.KHR_materials_unlit.alphaMode;return t===`BLEND`?2:+(t===`MASK`)}return e.alphaTest>0?1:e.transparent&&e.opacity<1||e.map&&e.map.format===r.RGBAFormat&&e.transparent?2:0}getMaterialType(e){return e.isMeshPhysicalMaterial?`physical`:e.isMeshStandardMaterial?`standard`:e.isMeshPhongMaterial?`phong`:e.isMeshLambertMaterial?`lambert`:e.isMeshBasicMaterial?`basic`:e.isMeshToonMaterial?`toon`:`unknown`}getPhysicalDefaults(){return{emissive:new r.Color(0,0,0),emissiveIntensity:1,roughness:1,metalness:0,ior:1.5,opacity:1,transmission:0,thickness:.1,attenuationColor:new r.Color(16777215),attenuationDistance:1/0,dispersion:0,sheen:0,sheenRoughness:1,sheenColor:new r.Color(0),specularIntensity:1,specularColor:new r.Color(16777215),clearcoat:0,clearcoatRoughness:0,iridescence:0,iridescenceIOR:1.3,iridescenceThicknessRange:[100,400],normalScale:{x:1,y:1},bumpScale:1,displacementScale:1,alphaTest:0,subsurface:0,subsurfaceColor:new r.Color(16777215),subsurfaceRadius:[1,.2,.1],subsurfaceRadiusScale:1,subsurfaceAnisotropy:0}}mapLegacyMaterialToPhysical(e,t){let n={};switch(t){case`basic`:n.emissive=e.color.clone(),n.emissiveIntensity=1,n.color=new r.Color(0),n.roughness=1,n.metalness=0;break;case`lambert`:n.roughness=1,n.metalness=0,n.specularIntensity=0;break;case`phong`:{let t=e.shininess||30;n.roughness=Math.sqrt(2/(t+2)),n.metalness=0}if(e.specular){let t=e.specular.r*.299+e.specular.g*.587+e.specular.b*.114;n.specularIntensity=Math.min(t*2,1),n.specularColor=e.specular.clone()}break;case`toon`:n.roughness=.9,n.metalness=0;break;case`standard`:case`physical`:break}return n}createMaterialObject(e){let t=this.getPhysicalDefaults(),n=this.getMaterialType(e),i=this.mapLegacyMaterialToPhysical(e,n),a=(e.metalness??i.metalness??0)>.1,o=t.ior;a?o=2.5:e.transmission>0&&(o=1.5);let s=e.color||new r.Color(16777215);return n===`basic`&&!e.map&&(s=new r.Color(0)),{uuid:e.uuid,color:s,emissive:i.emissive??e.emissive??t.emissive,emissiveIntensity:i.emissiveIntensity??e.emissiveIntensity??t.emissiveIntensity,roughness:Math.max(.05,i.roughness??e.roughness??t.roughness),metalness:i.metalness??e.metalness??t.metalness,ior:e.ior??o,opacity:e.opacity??t.opacity,transmission:e.transmission??t.transmission,thickness:e.thickness??t.thickness,attenuationColor:e.attenuationColor??t.attenuationColor,attenuationDistance:e.attenuationDistance??t.attenuationDistance,dispersion:e.dispersion??t.dispersion,sheen:e.sheen??t.sheen,sheenRoughness:e.sheenRoughness??t.sheenRoughness,sheenColor:e.sheenColor??t.sheenColor,clearcoat:e.clearcoat??t.clearcoat,clearcoatRoughness:e.clearcoatRoughness??t.clearcoatRoughness,iridescence:e.iridescence??t.iridescence,iridescenceIOR:e.iridescenceIOR??t.iridescenceIOR,iridescenceThicknessRange:e.iridescenceThicknessRange??t.iridescenceThicknessRange,subsurface:e.subsurface??t.subsurface,subsurfaceColor:e.subsurfaceColor??t.subsurfaceColor,subsurfaceRadius:e.subsurfaceRadius??t.subsurfaceRadius,subsurfaceRadiusScale:e.subsurfaceRadiusScale??t.subsurfaceRadiusScale,subsurfaceAnisotropy:e.subsurfaceAnisotropy??t.subsurfaceAnisotropy,specularIntensity:i.specularIntensity??e.specularIntensity??t.specularIntensity,specularColor:i.specularColor??e.specularColor??t.specularColor,normalScale:e.normalScale??t.normalScale,bumpScale:e.bumpScale??t.bumpScale,displacementScale:e.displacementScale??t.displacementScale,transparent:+!!e.transparent,alphaTest:e.alphaTest??t.alphaTest,alphaMode:this.getMaterialAlphaMode(e),side:this.getMaterialSide(e),depthWrite:e.depthWrite??!0?1:0,map:this.processTexture(e.map,this.maps),normalMap:this.processTexture(e.normalMap,this.normalMaps),bumpMap:this.processTexture(e.bumpMap,this.bumpMaps),roughnessMap:this.processTexture(e.roughnessMap,this.roughnessMaps),metalnessMap:this.processTexture(e.metalnessMap,this.metalnessMaps),emissiveMap:this.processTexture(e.emissiveMap,this.emissiveMaps),displacementMap:this.processTexture(e.displacementMap,this.displacementMaps),clearcoatMap:this.processTexture(e.clearcoatMap,[]),clearcoatRoughnessMap:this.processTexture(e.clearcoatRoughnessMap,[]),transmissionMap:this.processTexture(e.transmissionMap,[]),thicknessMap:this.processTexture(e.thicknessMap,[]),sheenColorMap:this.processTexture(e.sheenColorMap,[]),sheenRoughnessMap:this.processTexture(e.sheenRoughnessMap,[]),specularIntensityMap:this.processTexture(e.specularIntensityMap,[]),specularColorMap:this.processTexture(e.specularColorMap,[]),iridescenceMap:this.processTexture(e.iridescenceMap,[]),iridescenceThicknessMap:this.processTexture(e.iridescenceThicknessMap,[]),mapMatrix:this.getTextureMatrix(e.map),normalMapMatrices:this.getTextureMatrix(e.normalMap),bumpMapMatrices:this.getTextureMatrix(e.bumpMap),roughnessMapMatrices:this.getTextureMatrix(e.roughnessMap),metalnessMapMatrices:this.getTextureMatrix(e.metalnessMap),emissiveMapMatrices:this.getTextureMatrix(e.emissiveMap),displacementMapMatrices:this.getTextureMatrix(e.displacementMap),originalType:n}}getTextureMatrix(e){return e?(e.updateMatrix(),e.matrix.elements):new r.Matrix3().elements}getMaterialSide(e){if(e.transmission>0)return 2;switch(e.side){case r.FrontSide:return 0;case r.BackSide:return 1;case r.DoubleSide:return 2;default:return 0}}processTexture(e,t){if(!e)return-1;let n=this._textureIndexCache.get(t);n||(n=new Map,this._textureIndexCache.set(t,n));let r=e.source.uuid,i=n.get(r);if(i!==void 0)return i;if(t.length<Ti){t.push(e);let i=t.length-1;return n.set(r,i),i}return-1}extractGeometry(e,t,n){e.updateMatrix(),e.updateMatrixWorld();let r=e.geometry;r.attributes.normal||r.computeVertexNormals();let i=r.attributes.position,a=r.attributes.normal,o=r.attributes.uv,s=r.index?r.index.array:null;this._matrixPool.mat4.copy(e.matrixWorld),this._matrixPool.mat3.getNormalMatrix(this._matrixPool.mat4);let c=s?s.length/3:i.count/3;this.extractTrianglesInBatch(i,a,o,s,c,t,n)}extractTrianglesInBatch(e,t,n,r,i,a,o){for(;this.materialTriangleCounts.length<=a;)this.materialTriangleCounts.push(0);this.materialTriangleCounts[a]+=i;let s=this._getVec3(0),c=this._getVec3(1),l=this._getVec3(2),u=this._getVec3(3),d=this._getVec3(4),f=this._getVec3(5),p=this._getVec2(0),m=this._getVec2(1),h=this._getVec2(2);this._ensureCapacity(this.currentTriangleIndex+i);for(let g=0;g<i;g++){let i=g*3,_=r?r[i+0]:i+0,v=r?r[i+1]:i+1,y=r?r[i+2]:i+2;this.getVertex(e,_,s),this.getVertex(e,v,c),this.getVertex(e,y,l),this.getVertex(t,_,u),this.getVertex(t,v,d),this.getVertex(t,y,f),n?(this.getVertex(n,_,p),this.getVertex(n,v,m),this.getVertex(n,y,h)):(p.set(0,0),m.set(0,0),h.set(0,0)),s.applyMatrix4(this._matrixPool.mat4),c.applyMatrix4(this._matrixPool.mat4),l.applyMatrix4(this._matrixPool.mat4),u.applyMatrix3(this._matrixPool.mat3).normalize(),d.applyMatrix3(this._matrixPool.mat3).normalize(),f.applyMatrix3(this._matrixPool.mat3).normalize(),this.packTriangleDataTextureFormat(this.currentTriangleIndex,s,c,l,u,d,f,p,m,h,a,o),this.currentTriangleIndex++}}packTriangleDataTextureFormat(e,t,n,r,i,a,o,s,c,l,u,d){let f=e*G.FLOATS_PER_TRIANGLE;this.triangleData[f+G.POSITION_A_OFFSET+0]=t.x,this.triangleData[f+G.POSITION_A_OFFSET+1]=t.y,this.triangleData[f+G.POSITION_A_OFFSET+2]=t.z,this.triangleData[f+G.POSITION_A_OFFSET+3]=0,this.triangleData[f+G.POSITION_B_OFFSET+0]=n.x,this.triangleData[f+G.POSITION_B_OFFSET+1]=n.y,this.triangleData[f+G.POSITION_B_OFFSET+2]=n.z,this.triangleData[f+G.POSITION_B_OFFSET+3]=0,this.triangleData[f+G.POSITION_C_OFFSET+0]=r.x,this.triangleData[f+G.POSITION_C_OFFSET+1]=r.y,this.triangleData[f+G.POSITION_C_OFFSET+2]=r.z,this.triangleData[f+G.POSITION_C_OFFSET+3]=0,this.triangleData[f+G.NORMAL_A_OFFSET+0]=i.x,this.triangleData[f+G.NORMAL_A_OFFSET+1]=i.y,this.triangleData[f+G.NORMAL_A_OFFSET+2]=i.z;{let e=this.materials[u],t=e&&!(e.alphaMode|0)&&!(e.transparent|0)&&(e.transmission||0)===0&&(e.opacity??1)>=1?1:0;this.triangleData[f+G.NORMAL_A_OFFSET+3]=t}this.triangleData[f+G.NORMAL_B_OFFSET+0]=a.x,this.triangleData[f+G.NORMAL_B_OFFSET+1]=a.y,this.triangleData[f+G.NORMAL_B_OFFSET+2]=a.z,this.triangleData[f+G.NORMAL_B_OFFSET+3]=0,this.triangleData[f+G.NORMAL_C_OFFSET+0]=o.x,this.triangleData[f+G.NORMAL_C_OFFSET+1]=o.y,this.triangleData[f+G.NORMAL_C_OFFSET+2]=o.z,this.triangleData[f+G.NORMAL_C_OFFSET+3]=this.materials[u]?.side??0,this.triangleData[f+G.UV_AB_OFFSET+0]=s.x,this.triangleData[f+G.UV_AB_OFFSET+1]=s.y,this.triangleData[f+G.UV_AB_OFFSET+2]=c.x,this.triangleData[f+G.UV_AB_OFFSET+3]=c.y,this.triangleData[f+G.UV_C_MAT_OFFSET+0]=l.x,this.triangleData[f+G.UV_C_MAT_OFFSET+1]=l.y,this.triangleData[f+G.UV_C_MAT_OFFSET+2]=u,this.triangleData[f+G.UV_C_MAT_OFFSET+3]=d}getTriangleData(){return this.triangleData?this.triangleData.subarray(0,this.currentTriangleIndex*G.FLOATS_PER_TRIANGLE):null}getTriangleCount(){return this.currentTriangleIndex}getVertex(e,t,n){return e.itemSize===2?(n.x=e.getX(t),n.y=e.getY(t)):e.itemSize>=3&&(n.x=e.getX(t),n.y=e.getY(t),n.z=e.getZ(t)),n}logStats(){let e=this.currentTriangleIndex*G.FLOATS_PER_TRIANGLE*4;console.log(`materials:`,this.materials.length),console.log(`triangles:`,this.currentTriangleIndex),console.log(`triangle data size (MB):`,(e/(1024*1024)).toFixed(2)),console.log(`maps:`,this.maps.length)}extractMaterialsOnly(e){return this.resetArrays(),this._traverseMaterialsOnly(e),this.getExtractedData()}_traverseMaterialsOnly(e){if(e.isMesh&&e.geometry&&e.material){let t=this.processMaterial(e.material);e.userData.materialIndex=t;let n=this.meshes.length;this.meshes.push(e),e.userData.meshIndex=n}else e.isDirectionalLight?this.directionalLights.push(e):e.isCamera&&this.cameras.push(e);if(e.children)for(let t of e.children)this._traverseMaterialsOnly(t)}resetArrays(){this.triangleData=null,this.triangleCount=0,this.currentTriangleIndex=0,this.materials=[],this.materialTriangleCounts=[],this.meshes=[],this.meshTriangleRanges=[],this.maps=[],this.normalMaps=[],this.bumpMaps=[],this.metalnessMaps=[],this.emissiveMaps=[],this.roughnessMaps=[],this.displacementMaps=[],this.directionalLights=[],this.cameras=[],this._materialUuidMap=new Map,this._textureIndexCache=new WeakMap,this.sceneFeatures={hasClearcoat:!1,hasTransmission:!1,hasDispersion:!1,hasIridescence:!1,hasSheen:!1,hasTransparency:!1,hasSubsurface:!1,hasMultiLobeMaterials:!1,hasMRTOutputs:!0}}getExtractedData(){return{triangleData:this.getTriangleData(),triangleCount:this.getTriangleCount(),materials:this.materials,materialTriangleCounts:this.materialTriangleCounts,meshes:this.meshes,meshTriangleRanges:this.meshTriangleRanges,maps:this.maps,normalMaps:this.normalMaps,bumpMaps:this.bumpMaps,metalnessMaps:this.metalnessMaps,emissiveMaps:this.emissiveMaps,roughnessMaps:this.roughnessMaps,displacementMaps:this.displacementMaps,directionalLights:this.directionalLights,cameras:this.cameras,sceneFeatures:this.sceneFeatures}}},Di=class{constructor(){this.maxLeafSize=8}build(e){let t=e.length;if(t===0){let e=new Float32Array(16);return e[7]=1,e[14]=1,e[15]=-1,{nodeData:e,nodeCount:1,sortedPerm:new Int32Array,bitTrails:new Float32Array}}let n=new Int32Array(t);for(let e=0;e<t;e++)n[e]=e;let r=2*t+4,i=new Float32Array(r*16),a=0,o=new Float32Array(t),s=(t,r,c,l)=>{let u=a++,d=u*16,f=1/0,p=1/0,m=1/0,h=-1/0,g=-1/0,_=-1/0,v=0,y=1/0,b=1/0,x=1/0,S=-1/0,C=-1/0,w=-1/0;for(let i=t;i<r;i++){let t=e[n[i]];f=Math.min(f,t.bMinX),p=Math.min(p,t.bMinY),m=Math.min(m,t.bMinZ),h=Math.max(h,t.bMaxX),g=Math.max(g,t.bMaxY),_=Math.max(_,t.bMaxZ),v+=t.power,y=Math.min(y,t.cx),b=Math.min(b,t.cy),x=Math.min(x,t.cz),S=Math.max(S,t.cx),C=Math.max(C,t.cy),w=Math.max(w,t.cz)}i[d+0]=f,i[d+1]=p,i[d+2]=m,i[d+3]=v,i[d+4]=h,i[d+5]=g,i[d+6]=_;let T=r-t,E;if(T<=this.maxLeafSize){i[d+7]=1,i[d+8]=t,i[d+9]=T,i[d+10]=0,i[d+11]=0,E=null;for(let i=t;i<r;i++){let t=e[n[i]];E=ki(E,Oi(t)),o[i]=c}E||={ax:0,ay:0,az:1,cosO:-1}}else{let a=S-y,o=C-b,u=w-x,f;f=a>=o&&a>=u?0:o>=u?1:2;let p=[`cx`,`cy`,`cz`][f],m=t+r>>1;this._nthElement(n,e,t,r,m,p),i[d+7]=0;let h=s(t,m,c,l+1),g=s(m,r,c+2**l,l+1);i[d+8]=h.nodeIndex,i[d+9]=g.nodeIndex,i[d+10]=0,i[d+11]=0,E=ki(h.cone,g.cone)}return i[d+12]=E.ax,i[d+13]=E.ay,i[d+14]=E.az,i[d+15]=E.cosO,{nodeIndex:u,cone:E}};s(0,t,0,0);let c=new Int32Array(t);for(let e=0;e<t;e++)c[e]=n[e];let l=new Float32Array(a*16);return l.set(i.subarray(0,a*16)),console.log(`[LightBVHBuilder] Built BVH: ${a} nodes for ${t} emissive triangles`),{nodeData:l,nodeCount:a,sortedPerm:c,bitTrails:o}}_nthElement(e,t,n,r,i,a){for(;n<r-1;){let o=n+r>>1,s=t[e[o]][a],c=e[o];e[o]=e[r-1],e[r-1]=c;let l=n;for(let i=n;i<r-1;i++)t[e[i]][a]<s&&(c=e[i],e[i]=e[l],e[l]=c,l++);if(c=e[l],e[l]=e[r-1],e[r-1]=c,l===i)return;l<i?n=l+1:r=l}}};function Oi(e){return e.twoSided?{ax:e.nx,ay:e.ny,az:e.nz,cosO:-1}:{ax:e.nx,ay:e.ny,az:e.nz,cosO:1}}function ki(e,t){if(!e)return t;if(!t||e.cosO<=-1)return e;if(t.cosO<=-1)return t;let n=Math.min(Math.max(e.cosO,-1),1),r=Math.min(Math.max(t.cosO,-1),1),i=Math.acos(n),a=Math.acos(r),o=Math.min(Math.max(e.ax*t.ax+e.ay*t.ay+e.az*t.az,-1),1),s=Math.acos(o);if(Math.min(s+a,Math.PI)<=i)return e;if(Math.min(s+i,Math.PI)<=a)return t;let c=(i+a+s)*.5;if(c>=Math.PI)return{ax:e.ax,ay:e.ay,az:e.az,cosO:-1};let l=c-i,u=e.ay*t.az-e.az*t.ay,d=e.az*t.ax-e.ax*t.az,f=e.ax*t.ay-e.ay*t.ax,p=Math.sqrt(u*u+d*d+f*f);if(p<1e-8)return{ax:e.ax,ay:e.ay,az:e.az,cosO:-1};u/=p,d/=p,f/=p;let m=Math.cos(l),h=Math.sin(l),g=u*e.ax+d*e.ay+f*e.az,_=d*e.az-f*e.ay,v=f*e.ax-u*e.az,y=u*e.ay-d*e.ax,b=e.ax*m+_*h+u*g*(1-m),x=e.ay*m+v*h+d*g*(1-m),S=e.az*m+y*h+f*g*(1-m),C=Math.sqrt(b*b+x*x+S*S)||1;return{ax:b/C,ay:x/C,az:S/C,cosO:Math.cos(c)}}var Ai=class{constructor(){this.emissiveTriangles=[],this.emissiveCount=0,this.totalEmissivePower=0,this.emissiveIndicesArray=null,this.emissivePowerArray=null,this.cdfArray=null,this.lightBVHNodeData=null,this.lightBVHNodeCount=0,this.emissiveBitTrailMap=null,this._totalTriangleCount=0}extractEmissiveTriangles(e,t,n){console.log(`[EmissiveTriangleBuilder] Extracting emissive triangles...`),this.emissiveTriangles=[],this.totalEmissivePower=0,this._totalTriangleCount=n;let r=G.FLOATS_PER_TRIANGLE,i=G.UV_C_MAT_OFFSET+2;for(let a=0;a<n;a++){let n=a*r,o=Math.floor(e[n+i]),s=t[o];if(!s)continue;let c=s.emissive||{r:0,g:0,b:0},l=s.emissiveIntensity||0;if(l>0&&(c.r>0||c.g>0||c.b>0)){let t=e[n+0],r=e[n+1],i=e[n+2],u=e[n+4],d=e[n+5],f=e[n+6],p=e[n+8],m=e[n+9],h=e[n+10],g=this._calculateTriangleArea(t,r,i,u,d,f,p,m,h),_=(.2126*c.r+.7152*c.g+.0722*c.b)*l*g,v=(d-r)*(h-i)-(f-i)*(m-r),y=(f-i)*(p-t)-(u-t)*(h-i),b=(u-t)*(m-r)-(d-r)*(p-t),x=Math.sqrt(v*v+y*y+b*b)||1,S=s.side===1?-1:1;v=v/x*S,y=y/x*S,b=b/x*S;let C=s.side===2,w=(t+u+p)/3,T=(r+d+m)/3,E=(i+f+h)/3,D=Math.min(t,u,p),O=Math.min(r,d,m),k=Math.min(i,f,h),A=Math.max(t,u,p),j=Math.max(r,d,m),M=Math.max(i,f,h);this.emissiveTriangles.push({triangleIndex:a,materialIndex:o,power:_,area:g,emissive:{r:c.r,g:c.g,b:c.b},emissiveIntensity:l,cx:w,cy:T,cz:E,bMinX:D,bMinY:O,bMinZ:k,bMaxX:A,bMaxY:j,bMaxZ:M,nx:v,ny:y,nz:b,twoSided:C}),this.totalEmissivePower+=_}}return this.emissiveCount=this.emissiveTriangles.length,console.log(`[EmissiveTriangleBuilder] Found ${this.emissiveCount} emissive triangles (${(this.emissiveCount/n*100).toFixed(2)}%)`),console.log(`[EmissiveTriangleBuilder] Total emissive power: ${this.totalEmissivePower.toFixed(2)}`),this._buildDataArrays(),this.emissiveCount}_calculateTriangleArea(e,t,n,r,i,a,o,s,c){let l=r-e,u=i-t,d=a-n,f=o-e,p=s-t,m=c-n,h=u*m-d*p,g=d*f-l*m,_=l*p-u*f;return Math.sqrt(h*h+g*g+_*_)*.5}_buildDataArrays(){this.emissiveIndicesArray=new Int32Array(this.emissiveCount),this.emissivePowerArray=new Float32Array(this.emissiveCount);for(let e=0;e<this.emissiveCount;e++)this.emissiveIndicesArray[e]=this.emissiveTriangles[e].triangleIndex,this.emissivePowerArray[e]=this.emissiveTriangles[e].power;this._buildCDF()}_buildCDF(){if(this.emissiveCount===0){this.cdfArray=new Float32Array(1),this.cdfArray[0]=0;return}this.cdfArray=new Float32Array(this.emissiveCount);let e=0;for(let t=0;t<this.emissiveCount;t++)e+=this.emissivePowerArray[t],this.cdfArray[t]=e;if(e>0)for(let t=0;t<this.emissiveCount;t++)this.cdfArray[t]/=e}sampleCDF(e){if(this.emissiveCount===0)return-1;if(this.emissiveCount===1)return 0;let t=0,n=this.emissiveCount-1;for(;t<n;){let r=Math.floor((t+n)/2);this.cdfArray[r]<e?t=r+1:n=r}return t}getGPUData(){return{emissiveIndices:this.emissiveIndicesArray,emissivePower:this.emissivePowerArray,emissiveCDF:this.cdfArray,emissiveCount:this.emissiveCount,totalPower:this.totalEmissivePower}}createEmissiveTexture(){if(this.emissiveCount===0)return new r.DataTexture(new Float32Array(4),1,1,r.RGBAFormat,r.FloatType);let e=new Float32Array(this.emissiveCount*4);for(let t=0;t<this.emissiveCount;t++){let n=t*4;e[n+0]=this.emissiveIndicesArray[t],e[n+1]=this.emissivePowerArray[t],e[n+2]=this.cdfArray[t],e[n+3]=0}let t=Math.ceil(Math.sqrt(this.emissiveCount)),n=Math.ceil(this.emissiveCount/t),i=t*n*4,a=new Float32Array(i);a.set(e);let o=new r.DataTexture(a,t,n,r.RGBAFormat,r.FloatType);return o.needsUpdate=!0,o.generateMipmaps=!1,o.minFilter=r.NearestFilter,o.magFilter=r.NearestFilter,console.log(`[EmissiveTriangleBuilder] Created ${t}x${n} emissive texture (${this.emissiveCount} emissives)`),o}createEmissiveRawData(){if(this.emissiveCount===0)return new Float32Array(8);let e=new Float32Array(this.emissiveCount*8);for(let t=0;t<this.emissiveCount;t++){let n=this.emissiveTriangles[t],r=t*8;e[r+0]=n.triangleIndex,e[r+1]=n.power,e[r+2]=this.cdfArray[t],e[r+3]=this.totalEmissivePower>0?n.power/this.totalEmissivePower:0,e[r+4]=n.emissive.r*n.emissiveIntensity,e[r+5]=n.emissive.g*n.emissiveIntensity,e[r+6]=n.emissive.b*n.emissiveIntensity,e[r+7]=n.area}return console.log(`[EmissiveTriangleBuilder] Created emissive raw data: ${this.emissiveCount} entries (${e.byteLength} bytes)`),e}getStats(){if(this.emissiveCount===0)return{count:0,totalPower:0,averagePower:0,minPower:0,maxPower:0};let e=1/0,t=-1/0;for(let n=0;n<this.emissiveCount;n++){let r=this.emissivePowerArray[n];e=Math.min(e,r),t=Math.max(t,r)}return{count:this.emissiveCount,totalPower:this.totalEmissivePower,averagePower:this.totalEmissivePower/this.emissiveCount,minPower:e,maxPower:t}}updateMaterialEmissive(e,t,n,r,i){let a=t.emissive||{r:0,g:0,b:0},o=t.emissiveIntensity||0,s=o>0&&(a.r>0||a.g>0||a.b>0);if(s!==this.emissiveTriangles.some(t=>t.materialIndex===e))return this.extractEmissiveTriangles(n,r,i),!0;if(!s)return!1;let c=.2126*a.r+.7152*a.g+.0722*a.b;this.totalEmissivePower=0;for(let t=0;t<this.emissiveCount;t++){let n=this.emissiveTriangles[t];n.materialIndex===e&&(n.power=c*o*n.area,n.emissive={r:a.r,g:a.g,b:a.b},n.emissiveIntensity=o,this.emissivePowerArray[t]=n.power),this.totalEmissivePower+=this.emissiveTriangles[t].power}return this._buildCDF(),!0}buildLightBVH(){if(this.emissiveCount===0)return this.lightBVHNodeData=new Float32Array(16),this.lightBVHNodeData[7]=1,this.lightBVHNodeData[14]=1,this.lightBVHNodeData[15]=-1,this.lightBVHNodeCount=1,this.emissiveBitTrailMap=new Float32Array(Math.max(this._totalTriangleCount,1)).fill(-1),1;let{nodeData:e,nodeCount:t,sortedPerm:n,bitTrails:r}=new Di().build(this.emissiveTriangles);this.lightBVHNodeData=e,this.lightBVHNodeCount=t,this._rebuildSortedEmissiveData(n),this.emissiveBitTrailMap=new Float32Array(Math.max(this._totalTriangleCount,1)).fill(-1);for(let e=0;e<n.length;e++){let t=this.emissiveTriangles[n[e]].triangleIndex;this.emissiveBitTrailMap[t]=r[e]}return t}_rebuildSortedEmissiveData(e){let t=e.length;this.emissiveIndicesArray=new Int32Array(t),this.emissivePowerArray=new Float32Array(t);for(let n=0;n<t;n++){let t=e[n];this.emissiveIndicesArray[n]=this.emissiveTriangles[t].triangleIndex,this.emissivePowerArray[n]=this.emissiveTriangles[t].power}this._buildCDF();let n=new Float32Array(t*8);for(let r=0;r<t;r++){let t=e[r],i=this.emissiveTriangles[t],a=r*8;n[a+0]=i.triangleIndex,n[a+1]=i.power,n[a+2]=this.cdfArray[r],n[a+3]=this.totalEmissivePower>0?i.power/this.totalEmissivePower:0,n[a+4]=i.emissive.r*i.emissiveIntensity,n[a+5]=i.emissive.g*i.emissiveIntensity,n[a+6]=i.emissive.b*i.emissiveIntensity,n[a+7]=i.area}this.emissiveTriangleData=n,console.log(`[EmissiveTriangleBuilder] Rebuilt sorted emissive data: ${t} entries`)}clear(){this.emissiveTriangles=[],this.emissiveCount=0,this.totalEmissivePower=0,this.emissiveIndicesArray=null,this.emissivePowerArray=null,this.cdfArray=null,this.lightBVHNodeData=null,this.lightBVHNodeCount=0,this.emissiveBitTrailMap=null,this._totalTriangleCount=0}},ji=class{constructor(e=`Build`){this.label=e,this.entries=new Map,this.order=[],this.totalStart=performance.now()}start(e){return this.entries.set(e,{start:performance.now(),end:null}),this.order.includes(e)||this.order.push(e),this}end(e){let t=this.entries.get(e);return t&&(t.end=performance.now(),t.duration=t.end-t.start),this}getDuration(e){return this.entries.get(e)?.duration??0}print(){let e=performance.now()-this.totalStart,t=this.order.map(e=>{let t=this.entries.get(e)?.duration??0;return t>=1?`${e} ${Math.round(t)}ms`:null}).filter(Boolean);return console.log(`[${this.label}] ${Math.round(e)}ms`+(t.length?` | ${t.join(` · `)}`:``)),{steps:Object.fromEntries(this.order.map(e=>[e,Math.round(this.entries.get(e)?.duration??0)])),total:Math.round(e)}}},Mi=``+(typeof document>`u`&&typeof location>`u`?require(`url`).pathToFileURL(__dirname+`/assets/BVHRefitWorker-GkmNJYvb.js`).href:new URL(`assets/BVHRefitWorker-GkmNJYvb.js`,typeof document>`u`?location.href:document.currentScript&&document.currentScript.tagName.toUpperCase()===`SCRIPT`&&document.currentScript.src||document.baseURI).href),Ni=class{constructor(e={}){this.config={useWorkers:!0,bvhDepth:30,maxLeafSize:4,verbose:!1,useFloat32Array:!0,textureQuality:`adaptive`,maxTextureSize:Pe.DEFAULT_MAX_TEXTURE_SIZE,enableTextureCache:!0,maxConcurrentTextureTasks:Math.min(navigator.hardwareConcurrency||4,6),treeletSize:7,treeletOptimizationPasses:1,treeletMinImprovement:.01,...e},this.triangleData=null,this.triangleCount=0,this.materials=[],this.maps=[],this.normalMaps=[],this.bumpMaps=[],this.roughnessMaps=[],this.metalnessMaps=[],this.emissiveMaps=[],this.displacementMaps=[],this.directionalLights=[],this.cameras=[],this.spheres=[],this.bvhRoot=null,this.bvhData=null,this.materialData=null,this.instanceTable=null,this.originalToBvhMap=null,this._refitWorker=null,this._refitSharedBuffers=null,this._rebuildGeneration=0,this._pendingRebuilds=new Map,this.albedoTextures=null,this.normalTextures=null,this.bumpTextures=null,this.roughnessTextures=null,this.metalnessTextures=null,this.emissiveTextures=null,this.displacementTextures=null,this.emissiveTriangleData=null,this.emissiveTriangleCount=0,this.lightBVHNodeData=null,this.lightBVHNodeCount=0,this.emissiveBitTrailMap=null,this._initProcessors(),this.isProcessing=!1,this.processingStage=null,this.performanceMetrics={textureCreationTime:0,geometryExtractionTime:0,bvhBuildTime:0,totalProcessingTime:0}}setMaxTextureSize(e){return this.config.maxTextureSize=e,this.textureCreator?.setMaxTextureSize(e)}_initProcessors(){this.geometryExtractor=new Ei,this.bvhBuilder=new ti,this.bvhBuilder.maxLeafSize=this.config.maxLeafSize,this.bvhBuilder.setTreeletConfig({enabled:this.config.enableTreeletOptimization,size:this.config.treeletSize,passes:this.config.treeletOptimizationPasses,minImprovement:this.config.treeletMinImprovement}),this.textureCreator=new Ci({maxTextureSize:this.config.maxTextureSize}),this.emissiveTriangleBuilder=new Ai,this.tlasBuilder=new _i}_log(e,t){this.config.verbose&&console.log(`[SceneProcessor] ${e}`,t||``)}async buildBVH(e){if(this.isProcessing)throw Error(`Already processing a scene. Call dispose() first.`);this.isProcessing=!0,this.processingStage=`init`;let t=new ji(`SceneProcessor (${e.name||`scene`})`);try{this._reset(),this._log(`Starting scene processing`),this.processingStage=`extraction`,t.start(`Geometry extraction`),await this._extractGeometry(e),t.end(`Geometry extraction`),this.performanceMetrics.geometryExtractionTime=t.getDuration(`Geometry extraction`),this.processingStage=`bvh`,t.start(`BVH construction (worker)`),t.start(`Material textures (parallel)`);let n=!1,r=this._buildBVH().then(()=>t.end(`BVH construction (worker)`)),i=this._createMaterialTextures().then(()=>{t.end(`Material textures (parallel)`),n=!0});return await r,R({status:`Building light data...`,progress:77}),t.start(`Emissive extraction + Light BVH`),this._buildEmissiveData(),t.end(`Emissive extraction + Light BVH`),n||R({status:`Processing material textures...`,progress:80}),await i,this.performanceMetrics.bvhBuildTime=t.getDuration(`BVH construction (worker)`),this.performanceMetrics.textureCreationTime=t.getDuration(`Material textures (parallel)`),this.processingStage=`finalize`,t.start(`BVH data packing`),this.bvhRoot&&!this.bvhData&&(this.bvhData=this.textureCreator.createBVHRawData(this.bvhRoot)),t.end(`BVH data packing`),this.spheres=this._createSpheres(),this.performanceMetrics.totalProcessingTime=performance.now()-t.totalStart,t.print(),this.processingStage=`complete`,R({status:`Scene data ready`,progress:85}),this}catch(e){throw this.processingStage=`error`,console.error(`[SceneProcessor] Processing error:`,e),R({status:`Error: ${e.message}`,progress:100}),e}finally{this.isProcessing=!1}}async _extractGeometry(e){R({isLoading:!0,title:`Processing`,status:`Extracting geometry...`,progress:15}),await new Promise(e=>setTimeout(e,0)),this._log(`Extracting geometry`);let t=performance.now();try{let n=this.geometryExtractor.extract(e);this.triangleData=n.triangleData,this.triangleCount=n.triangleCount,this._log(`Using Float32Array format: ${this.triangleCount} triangles, ${(this.triangleData.byteLength/(1024*1024)).toFixed(2)}MB`),this.materials=n.materials,this.materialCount=this.materials.length,this.materialTriangleCounts=n.materialTriangleCounts,this.meshes=n.meshes,this.meshTriangleRanges=n.meshTriangleRanges,this.maps=n.maps,this.normalMaps=n.normalMaps,this.bumpMaps=n.bumpMaps,this.roughnessMaps=n.roughnessMaps,this.metalnessMaps=n.metalnessMaps,this.emissiveMaps=n.emissiveMaps,this.displacementMaps=n.displacementMaps,this.directionalLights=n.directionalLights,this.cameras=n.cameras,this.sceneFeatures=n.sceneFeatures;let r=performance.now()-t;this._log(`Geometry extraction complete (${r.toFixed(2)}ms)`,{triangleCount:this.triangleCount,materials:this.materials.length}),R({status:`Extracted ${this.triangleCount.toLocaleString()} triangles`,progress:25})}catch(e){throw console.error(`[SceneProcessor] Geometry extraction error:`,e),R({status:`Extraction error: ${e.message}`,progress:25}),e}}async _buildBVH(){if(R({status:`Building BVH...`,progress:25}),this.triangleCount===0)throw Error(`No triangles to build BVH from`);this._log(`Building two-level BVH (TLAS/BLAS)`);let e=performance.now();try{let t=G.FLOATS_PER_TRIANGLE,n=this.meshTriangleRanges;if(!n||n.length===0)throw Error(`No mesh triangle ranges available for TLAS/BLAS build`);this.instanceTable=new vi,this.instanceTable.allocate(n.length);let r=n.length,i=this.config.enableTreeletOptimization,a=[],o=[];for(let e=0;e<r;e++){let t=n[e];t.count!==0&&(t.count>=2e5&&mi(t.count)?o.push({m:e,range:t}):a.push({m:e,range:t}))}let s={depth:this.config.bvhDepth,treeletOptimization:{enabled:i!==!1,size:this.config.treeletSize,passes:this.config.treeletOptimizationPasses,minImprovement:this.config.treeletMinImprovement},reinsertionOptimization:{enabled:this.bvhBuilder.enableReinsertionOptimization,batchSizeRatio:this.bvhBuilder.reinsertionBatchSizeRatio,maxIterations:this.bvhBuilder.reinsertionMaxIterations}},c=a.length+o.length,l=this._buildBLASesWithPool(a,s,e=>{R({status:`Building BLAS ${e+o.length}/${c}...`,progress:25+Math.floor(e/c*45)})}),u=o.map(({m:e,range:n})=>di(this.triangleData.slice(n.start*t,(n.start+n.count)*t),this.config.bvhDepth,null,{maxLeafSize:this.bvhBuilder.maxLeafSize,numBins:this.bvhBuilder.numBins,maxBins:this.bvhBuilder.maxBins,minBins:this.bvhBuilder.minBins,...s}).then(t=>({m:e,range:n,result:t}))),[d,f]=await Promise.all([l,Promise.all(u)]);for(let{m:e,range:n,result:r}of[...d,...f])r.reorderedTriangles&&this.triangleData.set(r.reorderedTriangles,n.start*t),this.instanceTable.setEntry({meshIndex:e,blasNodeCount:r.bvhData.length/16,triOffset:n.start,triCount:n.count,originalToBvhMap:r.originalToBvh||null,bvhData:r.bvhData});R({status:`Built all BLASes`,progress:70}),R({status:`Building TLAS...`,progress:72});let p=this.instanceTable.entries.filter(e=>e!==null);this.instanceTable.computeAABBs(this.triangleData);let{root:m,nodeCount:h}=this.tlasBuilder.build(p);this.instanceTable.assignOffsets(h);let g=this.instanceTable.totalNodeCount,_=this.tlasBuilder.flatten(m,p);this.bvhData=new Float32Array(g*16),this.bvhData.set(_);for(let e of p){let t=e.blasOffset*16;this.bvhData.set(e.bvhData,t),this._offsetBLASInPlace(t,e.bvhData.length/16,e.blasOffset,e.triOffset)}this._buildGlobalOriginalToBvhMap();for(let e of p)e.originalToBvhMap=null,e.bvhData=null;this.bvhRoot=!0,this._disposeRefitWorker();let v=performance.now()-e;this._log(`BVH complete: ${p.length} mesh(es), ${this.bvhData.length/16} nodes (${v.toFixed(2)}ms)`),R({status:`BVH construction complete`,progress:75})}catch(e){throw console.error(`[SceneProcessor] BVH building error:`,e),R({status:`BVH error: ${e.message}`,progress:75}),e}}_offsetBLASInPlace(e,t,n,r){for(let i=0;i<t;i++){let t=e+i*16;this.bvhData[t+3]===-1?this.bvhData[t]+=r:(this.bvhData[t+3]+=n,this.bvhData[t+7]+=n)}}_buildBLASesWithPool(e,t,n){if(e.length===0)return Promise.resolve([]);let r=G.FLOATS_PER_TRIANGLE,i=Math.min(e.length,this.config.maxConcurrentTextureTasks||4),a=[],o=0,s=0;return new Promise((c,l)=>{let u=[],d=n=>{if(o>=e.length){n.terminate(),u.splice(u.indexOf(n),1),u.length===0&&c(a);return}let{m:i,range:s}=e[o++],l=this.triangleData.slice(s.start*r,(s.start+s.count)*r),d=s.count,f=d<=500?{...t.treeletOptimization,enabled:!1}:t.treeletOptimization;n._currentTask={m:i,range:s},n.postMessage({triangleData:l.buffer,triangleByteOffset:l.byteOffset,triangleByteLength:l.byteLength,triangleCount:d,depth:t.depth,reportProgress:!1,sharedReorderBuffer:null,treeletOptimization:f,reinsertionOptimization:t.reinsertionOptimization},[l.buffer])},f=(e,t)=>{let r=t.data;if(r.error){u.forEach(e=>e.terminate()),l(Error(r.error));return}if(r.progress!==void 0)return;let{m:i,range:o}=e._currentTask;a.push({m:i,range:o,result:{bvhData:r.bvhData,reorderedTriangles:r.triangles||null,originalToBvh:r.originalToBvh||null}}),s++,n?.(s),d(e)};(async()=>{for(let e=0;e<i;e++){let e;try{e=new Worker(Zr,{type:`module`})}catch(t){if(t.name!==`SecurityError`){l(t);return}e=await Je(Zr)}e.onmessage=t=>f(e,t),e.onerror=e=>{u.forEach(e=>e.terminate()),l(e)},u.push(e),d(e)}})().catch(l)})}_buildGlobalOriginalToBvhMap(){this.originalToBvhMap=new Uint32Array(this.triangleCount);for(let e of this.instanceTable.entries){if(!e)continue;let t=new Uint32Array(e.triCount);if(e.originalToBvhMap)for(let n=0;n<e.triCount;n++){let r=e.originalToBvhMap[n];this.originalToBvhMap[e.triOffset+n]=e.triOffset+r,t[r]=n}else for(let n=0;n<e.triCount;n++)this.originalToBvhMap[e.triOffset+n]=e.triOffset+n,t[n]=n;e.bvhToOriginal=t}}async _createMaterialTextures(){this._log(`Creating material textures (parallel with BVH)`);try{this.materials?.length&&(this.materialData=this.textureCreator.createMaterialRawData(this.materials));let e=[{data:this.maps,prop:`albedoTextures`},{data:this.normalMaps,prop:`normalTextures`},{data:this.bumpMaps,prop:`bumpTextures`},{data:this.roughnessMaps,prop:`roughnessTextures`},{data:this.metalnessMaps,prop:`metalnessTextures`},{data:this.emissiveMaps,prop:`emissiveTextures`},{data:this.displacementMaps,prop:`displacementTextures`}];await Promise.all(e.filter(({data:e})=>e?.length>0).map(({data:e,prop:t})=>this.textureCreator.createTexturesToDataTexture(e).then(e=>{this[t]=e}))),this._log(`Material textures complete`,{materialData:!!this.materialData})}catch(e){throw console.error(`[SceneProcessor] Texture creation error:`,e),e}}_buildEmissiveData(){this.emissiveTriangleCount=this.emissiveTriangleBuilder.extractEmissiveTriangles(this.triangleData,this.materials,this.triangleCount),this.emissiveTriangleData=this.emissiveTriangleBuilder.createEmissiveRawData(),this.emissiveTotalPower=this.emissiveTriangleBuilder.totalEmissivePower,this._log(`Emissive triangle extraction complete`,this.emissiveTriangleBuilder.getStats()),this.emissiveTriangleBuilder.buildLightBVH(),this.lightBVHNodeData=this.emissiveTriangleBuilder.lightBVHNodeData,this.lightBVHNodeCount=this.emissiveTriangleBuilder.lightBVHNodeCount,this.emissiveTriangleData=this.emissiveTriangleBuilder.emissiveTriangleData||this.emissiveTriangleData,this.emissiveBitTrailMap=this.emissiveTriangleBuilder.emissiveBitTrailMap}_createSpheres(){return[]}_reset(){this._disposeTextures(),this.triangles=[],this.triangleData=null,this.triangleCount=0,this.materials=[],this.meshTriangleRanges=null,this.maps=[],this.normalMaps=[],this.bumpMaps=[],this.roughnessMaps=[],this.metalnessMaps=[],this.emissiveMaps=[],this.displacementMaps=[],this.directionalLights=[],this.cameras=[],this.spheres=[],this.bvhRoot=null,this.bvhData=null,this.instanceTable=null,this.lightBVHNodeData=null,this.lightBVHNodeCount=0,this.emissiveBitTrailMap=null,this.performanceMetrics={textureCreationTime:0,geometryExtractionTime:0,bvhBuildTime:0,totalProcessingTime:0}}_disposeTextures(){[`albedoTextures`,`normalTextures`,`bumpTextures`,`roughnessTextures`,`metalnessTextures`,`emissiveTextures`,`displacementTextures`].forEach(e=>{this[e]&&(typeof this[e].dispose==`function`&&this[e].dispose(),this[e]=null)})}async rebuildMaterials(e){if(this.isProcessing)throw Error(`Already processing. Cannot rebuild materials during processing.`);this._log(`Rebuilding materials and textures`);let t=performance.now();try{this.isProcessing=!0;let n=this.geometryExtractor.extractMaterialsOnly(e);this._disposeMaterialTextures(),this.materials=n.materials,this.materialCount=this.materials.length,this.meshes=n.meshes,this.maps=n.maps,this.normalMaps=n.normalMaps,this.bumpMaps=n.bumpMaps,this.roughnessMaps=n.roughnessMaps,this.metalnessMaps=n.metalnessMaps,this.emissiveMaps=n.emissiveMaps,this.displacementMaps=n.displacementMaps,this.sceneFeatures=n.sceneFeatures;let r={materials:this.materials,triangles:this.triangleData,maps:this.maps,normalMaps:this.normalMaps,bumpMaps:this.bumpMaps,roughnessMaps:this.roughnessMaps,metalnessMaps:this.metalnessMaps,emissiveMaps:this.emissiveMaps,displacementMaps:this.displacementMaps,bvhRoot:this.bvhRoot},i=await this.textureCreator.createMaterialTextures(r);this.materialData=this.textureCreator.createMaterialRawData(this.materials),this.albedoTextures=i.albedoTexture,this.normalTextures=i.normalTexture,this.bumpTextures=i.bumpTexture,this.roughnessTextures=i.roughnessTexture,this.metalnessTextures=i.metalnessTexture,this.emissiveTextures=i.emissiveTexture,this.displacementTextures=i.displacementTexture;let a=performance.now()-t;return this._log(`Material rebuild complete (${a.toFixed(2)}ms)`,{materials:this.materials.length,textures:this.maps.length}),this}catch(e){throw console.error(`[SceneProcessor] Material rebuild error:`,e),e}finally{this.isProcessing=!1}}_disposeMaterialTextures(){[`albedoTextures`,`normalTextures`,`bumpTextures`,`roughnessTextures`,`metalnessTextures`,`emissiveTextures`,`displacementTextures`].forEach(e=>{if(this[e])try{typeof this[e].dispose==`function`&&this[e].dispose()}catch(t){console.warn(`[SceneProcessor] Error disposing ${e}:`,t)}finally{this[e]=null}}),this.textureCreator&&this.textureCreator.textureCache&&(this.textureCreator.textureCache.dispose(),this.textureCreator.textureCache=new this.textureCreator.textureCache.constructor)}getStatistics(){let e={triangleCount:this.triangleCount,materialCount:this.materials.length,textureCount:this.maps.length,lightCount:this.directionalLights.length,cameraCount:this.cameras.length,processingComplete:this.processingStage===`complete`,hasBVH:!!this.bvhRoot,hasTextures:!!this.materialData&&!!this.bvhData,useFloat32Array:this.config.useFloat32Array,triangleDataSize:this.triangleData?(this.triangleData.byteLength/(1024*1024)).toFixed(2)+`MB`:`0MB`};return this.performanceMetrics.totalProcessingTime>0&&(e.performance={totalTime:this.performanceMetrics.totalProcessingTime,textureTime:this.performanceMetrics.textureCreationTime,bvhTime:this.performanceMetrics.bvhBuildTime,extractionTime:this.performanceMetrics.geometryExtractionTime,texturePercentage:(this.performanceMetrics.textureCreationTime/this.performanceMetrics.totalProcessingTime*100).toFixed(1)+`%`}),this.textureCreator&&this.textureCreator.capabilities&&(e.textureCapabilities=this.textureCreator.capabilities),e}updateConfig(e){Object.assign(this.config,e),this.bvhBuilder&&(this.bvhBuilder.maxLeafSize=this.config.maxLeafSize,this.bvhBuilder.setTreeletConfig({enabled:this.config.enableTreeletOptimization,size:this.config.treeletSize,passes:this.config.treeletOptimizationPasses,minImprovement:this.config.treeletMinImprovement})),this._log(`Configuration updated`,this.config)}async refitBVH(e,t){if(!this.bvhData||!this.triangleData||!this.originalToBvhMap)throw Error(`No BVH data available for refit. Run buildBVH() first.`);if(!this._refitWorker)try{this._refitWorker=new Worker(Mi,{type:`module`})}catch(e){if(e.name!==`SecurityError`)throw e;this._refitWorker=await Je(Mi)}if(!this._refitSharedBuffers){let t=new SharedArrayBuffer(this.bvhData.byteLength),n=new SharedArrayBuffer(this.triangleData.byteLength),r=new SharedArrayBuffer(e.byteLength),i=new Float32Array(t),a=new Float32Array(n);i.set(this.bvhData),a.set(this.triangleData),this.bvhData=i,this.triangleData=a;let o=this.originalToBvhMap.length,s=new Uint32Array(o);for(let e=0;e<o;e++)s[this.originalToBvhMap[e]]=e;this._refitSharedBuffers={bvhBuf:t,triBuf:n,posBuf:r,posView:new Float32Array(r)},this._refitWorker.postMessage({type:`init`,sharedBvhBuf:t,sharedTriBuf:n,sharedPosBuf:r,bvhToOriginal:s},[s.buffer])}return this._refitSharedBuffers.posView.set(e),new Promise((e,n)=>{this._refitWorker.onmessage=r=>{let i=r.data;i.type===`refitComplete`?(t&&this._patchSmoothNormals(t),e({refitTimeMs:i.refitTimeMs})):i.type===`error`&&n(Error(i.error))},this._refitWorker.postMessage({type:`refit`})})}_patchSmoothNormals(e){this._patchNormalsRange(e,0,this.originalToBvhMap.length)}refitBLASes(e,t,n){if(!this.instanceTable||!this.bvhData||!this.triangleData)throw Error(`No TLAS/BLAS data available. Run buildBVH() first.`);let r=performance.now();this._blasRefitter||=new oi;for(let r of e){let e=this.instanceTable.entries[r];e&&(this._updateMeshTrianglePositions(e,t),n&&this._patchMeshSmoothNormals(e,n),this._blasRefitter.refitRange(this.bvhData,this.triangleData,e.blasOffset,e.blasNodeCount),this.instanceTable.recomputeAABB(r,this.bvhData,this.triangleData))}return this._refitTLAS(),{refitTimeMs:performance.now()-r}}computeBLASDirtyRanges(e){let t=G.FLOATS_PER_TRIANGLE,n=[],r=[];for(let i of e){let e=this.instanceTable.entries[i];e&&(n.push({offset:e.triOffset*t,count:e.triCount*t}),r.push({offset:e.blasOffset*16,count:e.blasNodeCount*16}))}return r.push({offset:0,count:this.instanceTable.tlasNodeCount*16}),{triRanges:n,bvhRanges:r}}uploadToPathTracer(e,t,n,r){return this.triangleData?(e.setTriangleData(this.triangleData,this.triangleCount),this.bvhData?(e.setBVHData(this.bvhData),e.setInstanceTable(this.instanceTable),this.materialData?e.materialData.setMaterialData(this.materialData):console.warn(`SceneProcessor: No material data, using defaults`),r&&e.environment.setEnvironmentTexture(r),e.materialData.setMaterialTextures({albedoMaps:this.albedoTextures,normalMaps:this.normalTextures,bumpMaps:this.bumpTextures,roughnessMaps:this.roughnessTextures,metalnessMaps:this.metalnessTextures,emissiveMaps:this.emissiveTextures,displacementMaps:this.displacementTextures}),this.emissiveTriangleData&&e.setEmissiveTriangleData(this.emissiveTriangleData,this.emissiveTriangleCount,this.emissiveTotalPower,this.emissiveBitTrailMap),this.lightBVHNodeData&&e.setLightBVHData(this.lightBVHNodeData,this.lightBVHNodeCount),t.transferSceneLights(n),!0):(console.error(`SceneProcessor: Failed to get BVH data`),!1)):(console.error(`SceneProcessor: Failed to get triangle data`),!1)}updateMaterialEmissive(e,t,n){if(!this.emissiveTriangleBuilder)return null;let r=this.materials[e];return!r||(t===`emissive`?r.emissive=n:t===`emissiveIntensity`&&(r.emissiveIntensity=n),!this.emissiveTriangleBuilder.updateMaterialEmissive(e,r,this.triangleData,this.materials,this.triangleCount))?null:(this.emissiveTriangleBuilder.buildLightBVH(),this.lightBVHNodeData=this.emissiveTriangleBuilder.lightBVHNodeData,this.lightBVHNodeCount=this.emissiveTriangleBuilder.lightBVHNodeCount,this.emissiveTriangleData=this.emissiveTriangleBuilder.emissiveTriangleData,this.emissiveBitTrailMap=this.emissiveTriangleBuilder.emissiveBitTrailMap,this.emissiveTriangleCount=this.emissiveTriangleBuilder.emissiveCount,this.emissiveTotalPower=this.emissiveTriangleBuilder.totalEmissivePower,{rawData:this.emissiveTriangleBuilder.emissiveTriangleData,emissiveCount:this.emissiveTriangleBuilder.emissiveCount,totalPower:this.emissiveTriangleBuilder.totalEmissivePower,bitTrailMap:this.emissiveBitTrailMap,lightBVHNodeData:this.lightBVHNodeData,lightBVHNodeCount:this.lightBVHNodeCount})}_updateMeshTrianglePositions(e,t){let n=G.FLOATS_PER_TRIANGLE,r=G.POSITION_A_OFFSET,i=G.POSITION_B_OFFSET,a=G.POSITION_C_OFFSET,o=G.NORMAL_A_OFFSET,s=G.NORMAL_B_OFFSET,c=G.NORMAL_C_OFFSET,l=e.bvhToOriginal;for(let u=0;u<e.triCount;u++){let d=l[u],f=(e.triOffset+u)*n,p=(e.triOffset+d)*9,m=t[p],h=t[p+1],g=t[p+2],_=t[p+3],v=t[p+4],y=t[p+5],b=t[p+6],x=t[p+7],S=t[p+8];this.triangleData[f+r]=m,this.triangleData[f+r+1]=h,this.triangleData[f+r+2]=g,this.triangleData[f+i]=_,this.triangleData[f+i+1]=v,this.triangleData[f+i+2]=y,this.triangleData[f+a]=b,this.triangleData[f+a+1]=x,this.triangleData[f+a+2]=S;let C=_-m,w=v-h,T=y-g,E=b-m,D=x-h,O=S-g,k=w*O-T*D,A=T*E-C*O,j=C*D-w*E;this.triangleData[f+o]=k,this.triangleData[f+o+1]=A,this.triangleData[f+o+2]=j,this.triangleData[f+s]=k,this.triangleData[f+s+1]=A,this.triangleData[f+s+2]=j,this.triangleData[f+c]=k,this.triangleData[f+c+1]=A,this.triangleData[f+c+2]=j}}_patchMeshSmoothNormals(e,t){this._patchNormalsRange(t,e.triOffset,e.triCount)}_patchNormalsRange(e,t,n){let r=G.FLOATS_PER_TRIANGLE,i=G.NORMAL_A_OFFSET,a=G.NORMAL_B_OFFSET,o=G.NORMAL_C_OFFSET;for(let s=0;s<n;s++){let n=t+s,c=this.originalToBvhMap[n]*r,l=n*9;this.triangleData[c+i]=e[l],this.triangleData[c+i+1]=e[l+1],this.triangleData[c+i+2]=e[l+2],this.triangleData[c+a]=e[l+3],this.triangleData[c+a+1]=e[l+4],this.triangleData[c+a+2]=e[l+5],this.triangleData[c+o]=e[l+6],this.triangleData[c+o+1]=e[l+7],this.triangleData[c+o+2]=e[l+8]}}_refitTLAS(){let e=this.instanceTable.tlasNodeCount;(!this._tlasBounds||this._tlasBounds.length<e*6)&&(this._tlasBounds=new Float32Array(e*6)),this._blasOffsetMap||=new Map,this._blasOffsetMap.clear();for(let e of this.instanceTable.entries)e&&this._blasOffsetMap.set(e.blasOffset,e);for(let t=e-1;t>=0;t--){let e=t*16,n=this.bvhData[e+3];if(n===-2){let n=this.bvhData[e],r=this._blasOffsetMap.get(n);if(r&&r.worldAABB){let e=t*6;this._tlasBounds[e]=r.worldAABB.minX,this._tlasBounds[e+1]=r.worldAABB.minY,this._tlasBounds[e+2]=r.worldAABB.minZ,this._tlasBounds[e+3]=r.worldAABB.maxX,this._tlasBounds[e+4]=r.worldAABB.maxY,this._tlasBounds[e+5]=r.worldAABB.maxZ}}else if(n>=0){let n=this.bvhData[e+3],r=this.bvhData[e+7],i=n*6,a=r*6,o=this._tlasBounds;this.bvhData[e]=o[i],this.bvhData[e+1]=o[i+1],this.bvhData[e+2]=o[i+2],this.bvhData[e+4]=o[i+3],this.bvhData[e+5]=o[i+4],this.bvhData[e+6]=o[i+5],this.bvhData[e+8]=o[a],this.bvhData[e+9]=o[a+1],this.bvhData[e+10]=o[a+2],this.bvhData[e+12]=o[a+3],this.bvhData[e+13]=o[a+4],this.bvhData[e+14]=o[a+5];let s=t*6;o[s]=Math.min(o[i],o[a]),o[s+1]=Math.min(o[i+1],o[a+1]),o[s+2]=Math.min(o[i+2],o[a+2]),o[s+3]=Math.max(o[i+3],o[a+3]),o[s+4]=Math.max(o[i+4],o[a+4]),o[s+5]=Math.max(o[i+5],o[a+5])}}}scheduleBackgroundRebuild(e,t){if(!this.instanceTable||!this.triangleData)return;let n=G.FLOATS_PER_TRIANGLE;this._rebuildGeneration++;let r=this._rebuildGeneration,i=(e,i,a)=>{let o=this.triangleData.slice(i.triOffset*n,(i.triOffset+i.triCount)*n);this._pendingRebuilds.set(e,a),a.onmessage=n=>{let o=n.data;if(a.terminate(),this._pendingRebuilds.delete(e),o.error){console.error(`Background BLAS rebuild error (mesh ${e}):`,o.error);return}r===this._rebuildGeneration&&this._swapBLAS(e,i,o,t)},a.onerror=t=>{console.error(`Background BLAS rebuild worker error (mesh ${e}):`,t),a.terminate(),this._pendingRebuilds.delete(e)};let s=i.triCount>500;a.postMessage({triangleData:o.buffer,triangleByteOffset:o.byteOffset,triangleByteLength:o.byteLength,triangleCount:i.triCount,depth:this.config.bvhDepth,reportProgress:!1,sharedReorderBuffer:null,treeletOptimization:{enabled:s,size:this.config.treeletSize,passes:this.config.treeletOptimizationPasses,minImprovement:this.config.treeletMinImprovement},reinsertionOptimization:{enabled:this.bvhBuilder.enableReinsertionOptimization,batchSizeRatio:this.bvhBuilder.reinsertionBatchSizeRatio,maxIterations:this.bvhBuilder.reinsertionMaxIterations}},[o.buffer])};for(let t of e){let e=this.instanceTable.entries[t];if(!e)continue;let n=this._pendingRebuilds.get(t);n&&n.terminate();let r;try{r=new Worker(Zr,{type:`module`}),i(t,e,r)}catch(n){if(n.name!==`SecurityError`)throw n;Je(Zr).then(n=>i(t,e,n))}}}_swapBLAS(e,t,n,r){let i=n.bvhData,a=i.length/16;if(a!==t.blasNodeCount){console.warn(`Background rebuild: node count mismatch for mesh ${e} (${a} vs ${t.blasNodeCount}), skipping swap`);return}let o=t.blasOffset*16;this.bvhData.set(i,o),this._offsetBLASInPlace(o,a,t.blasOffset,t.triOffset);let s=G.FLOATS_PER_TRIANGLE,c=n.triangles;c&&this.triangleData.set(c,t.triOffset*s);let l=n.originalToBvh;if(l){for(let e=0;e<t.triCount;e++)this.originalToBvhMap[t.triOffset+e]=t.triOffset+l[e];let e=new Uint32Array(t.triCount);for(let n=0;n<t.triCount;n++)e[l[n]]=n;t.bvhToOriginal=e}this.instanceTable.recomputeAABB(e,this.bvhData,this.triangleData),this._refitTLAS(),this._log(`Background BLAS rebuild complete for mesh ${e}`),r?.()}cancelBackgroundRebuilds(){for(let e of this._pendingRebuilds.values())e.terminate();this._pendingRebuilds.clear()}_disposeRefitWorker(){this._refitWorker&&=(this._refitWorker.terminate(),null),this._refitSharedBuffers=null,this.cancelBackgroundRebuilds()}dispose(){this._log(`Disposing resources`),this._disposeRefitWorker(),this._disposeTextures(),this._reset(),this.textureCreator&&=(this.textureCreator.dispose(),null),this.geometryExtractor=null,this.bvhBuilder=null,this.tlasBuilder=null,this._blasRefitter=null}},Pi=[[1619.19106,-.00205010916,5.02995757],[2488.45471,-.00111330907,3.22621544],[3341.43193,-.000486551192,1.76486769],[4094.61742,-.000127446582,.725731635],[4670.28036,291258199e-13,.126703442],[4595.09185,287495649e-13,.15034502],[3787.1745,935907826e-14,.399075871]],Fi=[[-488.999748,.000604330754,-.0755807526],[-755.994277,.000316730098,.478306139],[-1023.63977,.00012022347,.936662319],[-1265.71316,487340896e-14,1.27054498],[-1425.29332,-401150431e-13,1.43972784],[-1175.54822,-216378048e-13,1.30408023],[-500.799571,-459832026e-14,1.09098763]],Ii=[[596945309e-19,-4.85742887e-8,-970622247e-13,-.00407936148],[240430366e-19,5.55021075e-8,-.000198503712,.0289312858],[-140949732e-19,1.89878968e-7,-.000356632824,.0910767778],[-361460868e-19,2.84822009e-7,-.000493211319,.15672344],[-197075738e-19,1.75359352e-7,-.000250542825,-.0222783266],[-161997957e-21,-1.64216008e-8,.000386216271,-.738077418],[672650283e-21,-2.73078809e-8,.000424098264,-.752335691]];function Li(e){let t,n,r;if(e>=12e3)t=.8262954810464208,n=.9945080501520986,r=1.566307710274283;else if(e<800)t=5.413294490189271,n=-.20319390035873933,r=-.0822535242887164;else{let i=e>=6365?6:e>=3315?5:e>=1902?4:e>=1449?3:e>=1167?2:+(e>=965),a=Pi[i],o=Fi[i],s=Ii[i],c=1/e;t=a[0]*c+a[1]*e+a[2],n=o[0]*c+o[1]*e+o[2],r=((s[0]*e+s[1])*e+s[2])*e+s[3]}return[Math.max(0,t),Math.max(0,n),Math.max(0,r)]}var Ri=1/(4*Math.PI);function zi(e){let t=e.userData||{},n=Number.isFinite(t.exposure)?t.exposure:0,r=e.intensity*2**n,i=e.color.r,a=e.color.g,o=e.color.b;if(t.useTemperature){let[e,n,r]=Li(t.temperature??6500);i*=e,a*=n,o*=r}return{r:i,g:a,b:o,intensity:r}}var Bi=class{constructor(){this.lightData={directional:[],rectArea:[],point:[],spot:[]},this.directionalLightCache=[],this.areaLightCache=[],this.pointLightCache=[],this.spotLightCache=[]}clear(){this.lightData.directional=[],this.lightData.rectArea=[],this.lightData.point=[],this.lightData.spot=[],this.directionalLightCache=[],this.areaLightCache=[],this.pointLightCache=[],this.spotLightCache=[]}calculateLightImportance(e,t=`directional`){let n=.2126*e.color.r+.7152*e.color.g+.0722*e.color.b,r=e.intensity*n;if(t===`area`){let t=e.width*e.height;r*=Math.sqrt(t)}else if(t===`point`)r*=Math.sqrt(e.distance||100);else if(t===`spot`){let t=Math.sin(e.angle||Math.PI/4);r*=Math.sqrt(e.distance||100)*t}return r}addDirectionalLight(e){if(e.intensity<=0)return;e.updateMatrixWorld();let t=e.getWorldPosition(new r.Vector3),n;if(e.target){e.target.updateMatrixWorld();let i=e.target.getWorldPosition(new r.Vector3);n=t.sub(i).normalize()}else n=t.normalize();let i=this.calculateLightImportance(e,`directional`),a=e.userData.angle||e.angle||0,o=zi(e),s=e.userData?.gobo,c=s&&Number.isInteger(s.index)?s.index:-1,l=s&&typeof s.intensity==`number`?s.intensity:1,u=s&&s.inverted?-Math.abs(l):Math.abs(l),d=s&&typeof s.scale==`number`?s.scale:5;this.directionalLightCache.push({data:[n.x,n.y,n.z,o.r,o.g,o.b,o.intensity,a,c,u,d,0],importance:i,light:e})}addRectAreaLight(e){if(e.intensity<=0)return;e.updateMatrixWorld();let t=e.getWorldPosition(new r.Vector3),n=e.getWorldQuaternion(new r.Quaternion),i=e.getWorldScale(new r.Vector3),a=e.width*i.x*.5,o=e.height*i.y*.5,s=new r.Vector3(a,0,0).applyQuaternion(n),c=new r.Vector3(0,-o,0).applyQuaternion(n),l=this.calculateLightImportance(e,`area`),u=e.userData?.normalize??!0?1:0,d=Number.isFinite(e.userData?.spread)?e.userData.spread:Math.PI,f=+(e.userData?.shape===`ellipse`||e.userData?.shape===`disk`||e.userData?.shape===1),p=zi(e);this.areaLightCache.push({data:[t.x,t.y,t.z,s.x,s.y,s.z,c.x,c.y,c.z,p.r,p.g,p.b,p.intensity,u,d,f],importance:l,light:e})}addPointLight(e){if(e.intensity<=0)return;e.updateMatrixWorld();let t=e.getWorldPosition(new r.Vector3),n=this.calculateLightImportance(e,`point`),i=zi(e);this.pointLightCache.push({data:[t.x,t.y,t.z,i.r,i.g,i.b,i.intensity*Ri,e.distance||0,e.decay===void 0?2:e.decay],importance:n,light:e})}addSpotLight(e){if(e.intensity<=0)return;e.updateMatrixWorld();let t=e.getWorldPosition(new r.Vector3),n=(e.target?e.target.getWorldPosition(new r.Vector3):new r.Vector3(0,0,-1)).sub(t).normalize(),i=this.calculateLightImportance(e,`spot`),a=e.userData?.gobo,o=a&&Number.isInteger(a.index)?a.index:-1,s=a&&typeof a.intensity==`number`?a.intensity:1,c=a&&a.inverted?-Math.abs(s):Math.abs(s),l=e.userData?.ies,u=l&&Number.isInteger(l.index)?l.index:-1,d=l&&typeof l.intensity==`number`?l.intensity:1,f=zi(e);this.spotLightCache.push({data:[t.x,t.y,t.z,n.x,n.y,n.z,f.r,f.g,f.b,f.intensity*Ri,e.angle||Math.PI/4,e.penumbra||0,e.distance||0,e.decay===void 0?2:e.decay,o,c,u,d,0,0],importance:i,light:e})}preprocessLights(){this.directionalLightCache.sort((e,t)=>t.importance-e.importance),this.areaLightCache.sort((e,t)=>t.importance-e.importance),this.pointLightCache.sort((e,t)=>t.importance-e.importance),this.spotLightCache.sort((e,t)=>t.importance-e.importance),this.lightData.directional=[],this.lightData.rectArea=[],this.lightData.point=[],this.lightData.spot=[],this.directionalLightCache.forEach(e=>{this.lightData.directional.push(...e.data)}),this.areaLightCache.forEach(e=>{this.lightData.rectArea.push(...e.data)}),this.pointLightCache.forEach(e=>{this.lightData.point.push(...e.data)}),this.spotLightCache.forEach(e=>{this.lightData.spot.push(...e.data)}),this.areaLightCache.length>0&&console.log(`Preprocessed ${this.areaLightCache.length} area lights by importance`),this.pointLightCache.length>0&&console.log(`Preprocessed ${this.pointLightCache.length} point lights by importance`),this.spotLightCache.length>0&&console.log(`Preprocessed ${this.spotLightCache.length} spot lights by importance`)}updateShaderUniforms(e){let t=Math.floor(this.lightData.directional.length/12),n=Math.floor(this.lightData.rectArea.length/16),r=Math.floor(this.lightData.point.length/9),i=Math.floor(this.lightData.spot.length/20);e.defines.MAX_DIRECTIONAL_LIGHTS=t,e.defines.MAX_AREA_LIGHTS=n,e.defines.MAX_POINT_LIGHTS=r,e.defines.MAX_SPOT_LIGHTS=i,e.uniforms.directionalLights.value=new Float32Array(this.lightData.directional),e.uniforms.areaLights.value=new Float32Array(this.lightData.rectArea),e.uniforms.pointLights.value=new Float32Array(this.lightData.point),e.uniforms.spotLights.value=new Float32Array(this.lightData.spot),e.needsUpdate=!0}processSceneLights(e,t){this.clear(),e.traverse(e=>{e.isDirectionalLight?this.addDirectionalLight(e):e.isRectAreaLight?this.addRectAreaLight(e):e.isPointLight?this.addPointLight(e):e.isSpotLight&&this.addSpotLight(e)}),this.preprocessLights(),this.updateShaderUniforms(t)}getLightStatistics(){return{directionalLights:this.directionalLightCache.map(e=>({intensity:e.light.intensity,importance:e.importance,color:e.light.color})),areaLights:this.areaLightCache.map(e=>({intensity:e.light.intensity,importance:e.importance,color:e.light.color,size:e.light.width*e.light.height}))}}},Vi={stbnScalarAtlas:`https://assets.rayzee.atulmourya.com/noise/stbn_scalar_atlas.png`,stbnVec2Atlas:`https://assets.rayzee.atulmourya.com/noise/stbn_vec2_atlas.png`,ortRuntimeUrl:`https://cdn.jsdelivr.net/npm/onnxruntime-web@1.24.3/dist/ort.webgpu.bundle.min.mjs`,ortWasmPaths:`https://cdn.jsdelivr.net/npm/onnxruntime-web@1.24.3/dist/`,dracoDecoderPath:`https://www.gstatic.com/draco/v1/decoders/`,ktx2TranscoderPath:`https://cdn.jsdelivr.net/npm/three@0.183.2/examples/jsm/libs/basis/`,oidnWeightsBaseUrl:`https://cdn.jsdelivr.net/npm/denoiser/tzas/`,upscalerModelBaseUrl:`https://huggingface.co/notaneimu/onnx-image-models/resolve/main/`,cacheNamespace:`rayzee`};function Hi(e){e&&Object.assign(Vi,e)}function Ui(){return{...Vi}}var Wi=4,Gi=class extends ge{constructor(e,t,n,i={}){super(`PathTracer`,{...i,executionMode:he.ALWAYS});let a=i.width||1920,o=i.height||1080;this.camera=n,this.width=a,this.height=o,this.renderer=e,this.scene=t,this.sdfs=new Ni,this.lightSerializer=new Bi,this.accumulationEnabled=!0,this.isComplete=!1,this.cameras=[],this.performanceMonitor=z(),this.completionThreshold=0,this.renderLimitMode=`frames`,this._initDataTextures(),this.storageTextures=new Be(0,0),this.uniforms=new q(a,o),this._defineUniformGetters(),this.materialData=new qe(this.sdfs),this.materialData.callbacks.onReset=()=>this.reset(),this.materialData.callbacks.getTriangleData=()=>({array:this.triangleStorageAttr?.array,count:this.triangleCount}),this.materialData.callbacks.onTriangleDataChanged=()=>{this.triangleStorageAttr&&(this.triangleStorageAttr.needsUpdate=!0)},this.environment=new Ct(this.scene,this.uniforms),this.environment.callbacks.onReset=()=>this.reset(),this.environment.callbacks.getSceneTextureNodes=()=>this.shaderBuilder.getSceneTextureNodes(),this.shaderBuilder=new qr,this._initRenderingState(),this.setupBlueNoise(),this.tempVector2=new r.Vector2,this.lastCameraMatrix=new r.Matrix4,this.lastProjectionMatrix=new r.Matrix4,this.lastRenderMode=-1,this.renderModeChangeTimeout=null,this.renderModeChangeDelay=50,this.pendingRenderMode=null,this.lastInteractionModeState=!1,this.cameraChanged=!1,this.updateCompletionThreshold()}_initDataTextures(){this.triangleStorageAttr=null,this.triangleStorageNode=null,this.triangleCount=0,this.bvhStorageAttr=null,this.bvhStorageNode=null,this.bvhNodeCount=0,this.directionalLightsData=null,this.pointLightsData=null,this.spotLightsData=null,this.areaLightsData=null,this.goboMaps=null,this.iesProfiles=null,this.stbnScalarTexture=null,this.stbnVec2Texture=null,this.lightStorageAttr=new t.StorageInstancedBufferAttribute(new Float32Array(16),4),this.lightStorageNode=(0,n.storage)(this.lightStorageAttr,`vec4`,1).toReadOnly(),this._lbvhDataCache=null,this._emissiveDataCache=null,this._bitTrailMapCache=null,this._instanceTable=null,this.spheres=[]}_defineUniformGetters(){let e=this.uniforms;for(let t of e.keys())Object.defineProperty(this,t,{get:()=>e.get(t),configurable:!0});let t=e.getLightBufferNodes();for(let[e,n]of Object.entries(t))Object.defineProperty(this,`${e}LightsBufferNode`,{get:()=>n,configurable:!0})}_initRenderingState(){this.isReady=!1,this.frameCount=0}_initCameraOptimizer(){let e=this;this.cameraOptimizer=new _e(this.renderer,{uniforms:{maxBounceCount:{get value(){return e.maxBounces.value},set value(t){e.maxBounces.value=t}},useEnvMapIS:{get value(){return e.useEnvMapIS.value},set value(t){e.useEnvMapIS.value=t}},enableAccumulation:{get value(){return e.enableAccumulation.value},set value(t){e.enableAccumulation.value=t}},enableEmissiveTriangleSampling:{get value(){return e.enableEmissiveTriangleSampling.value},set value(t){e.enableEmissiveTriangleSampling.value=t}},cameraIsMoving:{get value(){return e.cameraIsMoving.value},set value(t){e.cameraIsMoving.value=t}}}},{enabled:B.interactionModeEnabled,qualitySettings:{maxBounceCount:1,useEnvMapIS:!1,enableAccumulation:!1,enableEmissiveTriangleSampling:!1},onReset:()=>{this.reset(),this.emit(`pathtracer:viewpointChanged`)}})}setupBlueNoise(){let e=new r.TextureLoader;e.setCrossOrigin(`anonymous`);let t=e=>(e.minFilter=r.NearestFilter,e.magFilter=r.NearestFilter,e.wrapS=r.RepeatWrapping,e.wrapT=r.RepeatWrapping,e.generateMipmaps=!1,e),{stbnScalarAtlas:n,stbnVec2Atlas:i}=Ui();e.load(n,e=>{this.stbnScalarTexture=t(e),N.value=e,console.log(`PathTracer: STBN scalar atlas loaded ${e.image.width}x${e.image.height}`)}),e.load(i,e=>{this.stbnVec2Texture=t(e),P.value=e,console.log(`PathTracer: STBN vec2 atlas loaded ${e.image.width}x${e.image.height}`)})}setupEventListeners(){this.on(`pipeline:reset`,()=>{this.reset()}),this.on(`pipeline:resize`,e=>{e&&e.width&&e.height&&this.setSize(e.width,e.height)}),this.on(`pathtracer:setCompletionThreshold`,e=>{e&&e.threshold!==void 0&&(this.completionThreshold=e.threshold)})}async build(e){this.dispose(),this.scene=e,await this.sdfs.buildBVH(e),this.cameras=this.sdfs.cameras,this.materialData.injectMaterialFeatureDefines(),this.updateSceneUniforms(),this.updateLights(),this._initCameraOptimizer(),this.setupMaterial()}updateSceneUniforms(){this.setTriangleData(this.sdfs.triangleData,this.sdfs.triangleCount),this.setBVHData(this.sdfs.bvhData),this.setInstanceTable(this.sdfs.instanceTable),this.materialData.setMaterialData(this.sdfs.materialData),this.materialData.loadTexturesFromSdfs(),this.sdfs.emissiveTriangleData?this.setEmissiveTriangleData(this.sdfs.emissiveTriangleData,this.sdfs.emissiveTriangleCount||0):this.emissiveTriangleCount.value=0,this.sdfs.lightBVHNodeData?this.setLightBVHData(this.sdfs.lightBVHNodeData,this.sdfs.lightBVHNodeCount||0):this.lightBVHNodeCount.value=0,this._meshRefs=this._collectMeshRefs(this.scene),this.setMeshVisibilityData(this._meshRefs),this.spheres=this.sdfs.spheres||[]}updateLights(){let e={uniforms:{directionalLights:{value:null},pointLights:{value:null},spotLights:{value:null},areaLights:{value:null}},defines:{}};if(this.lightSerializer.processSceneLights(this.scene,e),this.directionalLightsData=e.uniforms.directionalLights.value,this.pointLightsData=e.uniforms.pointLights.value,this.spotLightsData=e.uniforms.spotLights.value,this.areaLightsData=e.uniforms.areaLights.value,this.hasSun.value){let t=this.environment.envParams.skySunIntensity*950,n={intensity:t,color:{r:1,g:1,b:1},userData:{angle:this.sunAngularSize.value},updateMatrixWorld:()=>{},getWorldPosition:e=>{let t=this.sunDirection.value;return e.set(t.x,t.y,t.z).multiplyScalar(1e10)}};this.lightSerializer.addDirectionalLight(n),this.lightSerializer.preprocessLights(),this.lightSerializer.updateShaderUniforms(e),this.directionalLightsData=e.uniforms.directionalLights.value,console.log(`Sun added as directional light (intensity: ${t.toFixed(2)})`)}this._updateLightBufferNodes()}_updateLightBufferNodes(){this.directionalLightsData&&this.directionalLightsData.length>0?(this.directionalLightsBufferNode.array=Array.from(this.directionalLightsData),this.numDirectionalLights.value=Math.floor(this.directionalLightsData.length/12)):this.numDirectionalLights.value=0,this.areaLightsData&&this.areaLightsData.length>0?(this.areaLightsBufferNode.array=Array.from(this.areaLightsData),this.numAreaLights.value=Math.floor(this.areaLightsData.length/16)):this.numAreaLights.value=0,this.pointLightsData&&this.pointLightsData.length>0?(this.pointLightsBufferNode.array=Array.from(this.pointLightsData),this.numPointLights.value=Math.floor(this.pointLightsData.length/9)):this.numPointLights.value=0,this.spotLightsData&&this.spotLightsData.length>0?(this.spotLightsBufferNode.array=Array.from(this.spotLightsData),this.numSpotLights.value=Math.floor(this.spotLightsData.length/20)):this.numSpotLights.value=0}reset(){this.frameCount=0,this.frame.value=0,this.hasPreviousAccumulated.value=0,this.storageTextures.currentTarget=0,this.updateCompletionThreshold(),this.isComplete=!1,this.performanceMonitor?.reset(),this.lastRenderMode=-1,this.lastInteractionModeState=!1}setSize(e,t){this.width=e,this.height=t,this.resolution.value.set(e,t),this.createStorageTextures(e,t)}setAccumulationEnabled(e){this.accumulationEnabled=e,this.enableAccumulation.value=+!!e}enterInteractionMode(){this.cameraOptimizer?.enterInteractionMode()}setInteractionModeEnabled(e){this.cameraOptimizer?.setInteractionModeEnabled(e)}get interactionMode(){return this.cameraOptimizer?.isInInteractionMode()??!1}setTriangleData(e,r){if(!e)return;let i=e.length/4;this.triangleStorageNode?(this.triangleStorageAttr=new t.StorageInstancedBufferAttribute(e,4),this.triangleStorageNode.value=this.triangleStorageAttr,this.triangleStorageNode.bufferCount=i):(this.triangleStorageAttr=new t.StorageInstancedBufferAttribute(e,4),this.triangleStorageNode=(0,n.storage)(this.triangleStorageAttr,`vec4`,i).toReadOnly()),this.triangleCount=r,console.log(`PathTracer: ${this.triangleCount} triangles (storage buffer)`)}setBVHData(e){if(!e)return;let r=e.length/4;this.bvhStorageNode?(this.bvhStorageAttr=new t.StorageInstancedBufferAttribute(e,4),this.bvhStorageNode.value=this.bvhStorageAttr,this.bvhStorageNode.bufferCount=r):(this.bvhStorageAttr=new t.StorageInstancedBufferAttribute(e,4),this.bvhStorageNode=(0,n.storage)(this.bvhStorageAttr,`vec4`,r).toReadOnly()),this.bvhNodeCount=Math.floor(r/Wi),console.log(`PathTracer: ${this.bvhNodeCount} BVH nodes (storage buffer)`)}setInstanceTable(e){this._instanceTable=e}setMeshVisibilityData(e){if(!(!e||e.length===0||!this._instanceTable)){for(let t=0;t<e.length;t++)this._patchTLASLeafVisibility(t,this._isWorldVisible(e[t]));this.bvhStorageAttr&&(this.bvhStorageAttr.needsUpdate=!0)}}updateMeshVisibility(e,t){this._patchTLASLeafVisibility(e,t)&&this.bvhStorageAttr&&(this.bvhStorageAttr.needsUpdate=!0)}updateAllMeshVisibility(){if(!(!this._meshRefs||!this._instanceTable)){for(let e=0;e<this._meshRefs.length;e++)this._patchTLASLeafVisibility(e,this._isWorldVisible(this._meshRefs[e]));this.bvhStorageAttr&&(this.bvhStorageAttr.needsUpdate=!0)}}_patchTLASLeafVisibility(e,t){let n=this._instanceTable?.entries?.[e];return!n||n.tlasLeafIndex<0||!this.bvhStorageAttr?!1:(n.visible=t,this.bvhStorageAttr.array[n.tlasLeafIndex*16+2]=+!!t,!0)}_collectMeshRefs(e){if(!e)return[];let t=[];return e.traverse(e=>{e.isMesh&&e.userData.meshIndex!==void 0&&(t[e.userData.meshIndex]=e)}),t}_isWorldVisible(e){for(;e;){if(!e.visible)return!1;e=e.parent}return!0}_updateStorageBuffer(e,t){e&&(e.array.set(t),e.needsUpdate=!0)}updateTriangleData(e){this._updateStorageBuffer(this.triangleStorageAttr,e)}updateBVHData(e){this._updateStorageBuffer(this.bvhStorageAttr,e)}updateBufferRanges(e,t){if(this.triangleStorageAttr&&e.length>0){this.triangleStorageAttr.clearUpdateRanges();for(let t of e)this.triangleStorageAttr.addUpdateRange(t.offset,t.count);this.triangleStorageAttr.version++}if(this.bvhStorageAttr&&t.length>0){this.bvhStorageAttr.clearUpdateRanges();for(let e of t)this.bvhStorageAttr.addUpdateRange(e.offset,e.count);this.bvhStorageAttr.version++}}createStorageTextures(e,t){this.storageTextures.writeColor?this.storageTextures.setSize(e,t):this.storageTextures.create(e,t),this.resolution.value.set(e,t)}setupMaterial(){if(this.cameraOptimizer||this._initCameraOptimizer(),!this.triangleStorageNode){console.error(`PathTracer: Triangle data required`);return}if(!this.bvhStorageNode){console.error(`PathTracer: BVH data required`);return}if(this.isReady&&this.shaderBuilder.getSceneTextureNodes()){this.shaderBuilder.updateSceneTextures(this);return}this._ensureStorageTextures(),this.shaderBuilder.createSceneTextureNodes(this,this.storageTextures),this.isReady=!0}_ensureStorageTextures(){let e=this.renderer.domElement,t=Math.max(1,e.width||this.width),n=Math.max(1,e.height||this.height);this.storageTextures.ensureSize(t,n)&&this.resolution.value.set(t,n)}_handleResize(){let{width:e,height:t}=this.renderer.domElement;(e!==this.storageTextures.renderWidth||t!==this.storageTextures.renderHeight)&&(this.createStorageTextures(e,t),this.frameCount=0),this.resolution.value.set(e,t)}_matricesApproxEqual(e,t,n=1e-10){let r=e.elements,i=t.elements;for(let e=0;e<16;e++)if(Math.abs(r[e]-i[e])>n)return!1;return!0}_updateCameraUniforms(){return!this._matricesApproxEqual(this.lastCameraMatrix,this.camera.matrixWorld)||!this._matricesApproxEqual(this.lastProjectionMatrix,this.camera.projectionMatrixInverse)?(this.cameraWorldMatrix.value.copy(this.camera.matrixWorld),this.cameraViewMatrix.value.copy(this.camera.matrixWorldInverse),this.cameraProjectionMatrix.value.copy(this.camera.projectionMatrix),this.cameraProjectionMatrixInverse.value.copy(this.camera.projectionMatrixInverse),this.lastCameraMatrix.copy(this.camera.matrixWorld),this.lastProjectionMatrix.copy(this.camera.projectionMatrixInverse),!0):!1}_updateAccumulationUniforms(e,t){let n=this.cameraOptimizer?.isInInteractionMode()??!1;this.lastInteractionModeState=n,this.accumulationEnabled?n?(this.accumulationAlpha.value=1,this.hasPreviousAccumulated.value=0):(this.accumulationAlpha.value=Oe(e),this.hasPreviousAccumulated.value=+(e>0)):(this.accumulationAlpha.value=1,this.hasPreviousAccumulated.value=0)}_publishTexturesToContext(e,t){e.setTexture(`pathtracer:color`,t.color),e.setTexture(`pathtracer:normalDepth`,t.normalDepth),e.setTexture(`pathtracer:albedo`,t.albedo),e.setState(`interactionMode`,this.cameraOptimizer?.isInInteractionMode()??!1),e.setState(`renderMode`,this.renderMode.value)}_emitStateEvents(){this.emit(`pathtracer:frameComplete`,{frame:this.frameCount,isComplete:this.isComplete}),this.cameraChanged&&=(this.emit(`camera:moved`),!1)}updateCompletionThreshold(){let e=this.renderMode.value,t=this.maxSamples.value;this.renderLimitMode===`time`?this.completionThreshold=1/0:this.completionThreshold=De(e,t)}setRenderLimitMode(e){this.renderLimitMode=e,this.updateCompletionThreshold()}manageASVGFForRenderMode(e){e!==this.lastRenderMode&&(this.renderModeChangeTimeout&&clearTimeout(this.renderModeChangeTimeout),this.pendingRenderMode=e,this.renderModeChangeTimeout=setTimeout(()=>{this.pendingRenderMode!==null&&this.pendingRenderMode!==this.lastRenderMode&&(this.lastRenderMode=this.pendingRenderMode,this._onRenderModeChanged(this.pendingRenderMode)),this.renderModeChangeTimeout=null,this.pendingRenderMode=null},this.renderModeChangeDelay)),this._handleFullQuadASVGF()}_onRenderModeChanged(e){e===1?this.emit(`asvgf:updateParameters`,{enableDebug:!1,temporalAlpha:.15}):this.emit(`asvgf:updateParameters`,{temporalAlpha:.1}),this.emit(`asvgf:reset`)}_handleFullQuadASVGF(){this.emit(`asvgf:setTemporal`,{enabled:!0})}setUniform(e,t){this.uniforms.set(e,t)}setBlueNoiseTexture(e){this.stbnScalarTexture=e,e&&(N.value=e)}_rebuildLightBuffer(){let e=this._lbvhDataCache,n=this._emissiveDataCache,r=this._bitTrailMapCache,i=e?e.length:0,a=n?n.length:0,o=r?Math.ceil(r.length/4)*4:0,s=Math.max(i+a+o,4),c=new Float32Array(s);e&&c.set(e,0),n&&c.set(n,i),r&&c.set(r,i+a),this.lightStorageAttr=new t.StorageInstancedBufferAttribute(c,4),this.lightStorageNode.value=this.lightStorageAttr,this.lightStorageNode.bufferCount=c.length/4,this.emissiveVec4Offset.value=(this.lightBVHNodeCount.value||0)*4,this.reverseMapVec4Offset.value=(i+a)/4}setEmissiveTriangleData(e,t,n=0,r=null){e&&(this._emissiveDataCache=e,r&&(this._bitTrailMapCache=r),this.emissiveTriangleCount.value=t,this.emissiveTotalPower.value=n,this._rebuildLightBuffer(),console.log(`PathTracer: ${t} emissive triangles, totalPower=${n.toFixed(4)} (storage buffer)`))}setLightBVHData(e,t){e&&(this._lbvhDataCache=e,this.lightBVHNodeCount.value=t,this._rebuildLightBuffer(),console.log(`PathTracer: Light BVH ${t} nodes`))}updateUniforms(e){let t=!1;for(let[n,r]of Object.entries(e))this[n]&&this[n].value!==void 0&&this[n].value!==r&&(this[n].value=r,t=!0);t&&this.reset()}async rebuildMaterials(e){if(!this.sdfs)throw Error(`Scene not built yet. Call build() first.`);try{console.log(`PathTracer: Starting material rebuild...`),await this.sdfs.rebuildMaterials(e),this.updateSceneUniforms(),this.shaderBuilder.updateSceneTextures(this),this.updateLights(),this.reset(),console.log(`PathTracer materials rebuilt successfully`)}catch(e){console.error(`Error rebuilding PathTracer materials:`,e);try{console.warn(`Attempting recovery by resetting path tracer...`),this.reset()}catch(e){console.error(`Recovery failed:`,e)}throw e}}dispose(){this.renderModeChangeTimeout&&=(clearTimeout(this.renderModeChangeTimeout),null),this.cameraOptimizer?.dispose(),this.materialData?.dispose(),this.environment?.dispose(),this.shaderBuilder?.dispose(),this.uniforms?.dispose(),this.storageTextures?.dispose(),this.stbnScalarTexture?.dispose(),this.stbnVec2Texture?.dispose(),this.placeholderTexture?.dispose(),this.triangleStorageAttr=null,this.triangleStorageNode=null,this.bvhStorageAttr=null,this.bvhStorageNode=null,this.placeholderTexture=null,this.isReady=!1}},Ki={ORIGIN_META:0,DIR_FLAGS:1,THROUGHPUT_PDF:2,RADIANCE_ALPHA:3,MEDIUM_STACK:4,MEDIUM_SIGMA_A:5,SSS_SIGMA_S:6},qi={DIST_TRI_BARY:0,NORMAL_MAT:1},Ji=0,$=(e,t)=>t===0?e:e.add(t*Ji),Yi=class{static requiredCapacity(e){return Math.ceil(e*1.25)}constructor(e=0){this.capacity=0,this._attrs={},this.rayBuffer=null,this.rngBuffer=null,this.hitBuffer=null,e>0&&this.allocate(e)}allocate(e){this.dispose();let r=Math.ceil(e*1.25);this.capacity=r,Ji=r;let i=r*7,a=new t.StorageInstancedBufferAttribute(new Float32Array(i*4),4);this._attrs.ray=a,this.rayBuffer={rw:(0,n.storage)(a,`vec4`),ro:(0,n.storage)(a,`vec4`).toReadOnly()};let o=new t.StorageInstancedBufferAttribute(new Uint32Array(r),1);this._attrs.rng=o,this.rngBuffer={rw:(0,n.storage)(o,`uint`),ro:(0,n.storage)(o,`uint`).toReadOnly()};let s=r*2,c=new t.StorageInstancedBufferAttribute(new Float32Array(s*4),4);this._attrs.hit=c,this.hitBuffer={rw:(0,n.storage)(c,`vec4`),ro:(0,n.storage)(c,`vec4`).toReadOnly()};let l=(i*16+r*4+s*16)/(1024*1024);console.log(`PackedRayBuffer: capacity=${r}, total=${l.toFixed(1)} MB (ray=${(i*16/1048576).toFixed(0)}MB hit=${(s*16/1048576).toFixed(0)}MB) [SoA ray/hit]`)}resize(e){return Math.ceil(e*1.25)<=this.capacity&&this.capacity>0?!1:(this.allocate(e),!0)}dispose(){this._attrs={},this.rayBuffer=null,this.rngBuffer=null,this.hitBuffer=null,this.capacity=0}},Xi=(e,t)=>e.element($(t,Ki.ORIGIN_META)).xyz,Zi=(e,t)=>e.element($(t,Ki.DIR_FLAGS)).xyz,Qi=(e,t)=>(0,n.floatBitsToUint)(e.element($(t,Ki.DIR_FLAGS)).w),$i=(e,t)=>e.element($(t,Ki.THROUGHPUT_PDF)).xyz,ea=(e,t)=>e.element($(t,Ki.THROUGHPUT_PDF)).w,ta=(e,t)=>e.element($(t,Ki.RADIANCE_ALPHA)),na=e=>(0,n.uint)(e).mul(1),ra=(e,t,r,i,a)=>e.element(na(t)).assign((0,n.uvec4)((0,n.packSnorm2x16)((0,n.vec2)(r.x,r.y)),(0,n.packSnorm2x16)((0,n.vec2)(r.z,i)),(0,n.packUnorm2x16)((0,n.vec2)(a.x,a.y)),(0,n.packUnorm2x16)((0,n.vec2)(a.z,0)))),ia=(e,t)=>e.element(na(t)),aa=e=>{let t=(0,n.unpackSnorm2x16)(e.x),r=(0,n.unpackSnorm2x16)(e.y);return(0,n.vec4)((0,n.vec3)(t.x,t.y,r.x).mul(.5).add(.5),r.y)},oa=e=>(0,n.vec3)((0,n.unpackUnorm2x16)(e.z),(0,n.unpackUnorm2x16)(e.w).x),sa=(e,t,r,i,a)=>e.element($(t,Ki.ORIGIN_META)).assign((0,n.vec4)(r,(0,n.uintBitsToFloat)((0,n.uint)(i).bitOr((0,n.uint)(a).shiftLeft(8))))),ca=(e,t,r,i)=>e.element($(t,Ki.DIR_FLAGS)).assign((0,n.vec4)(r,(0,n.uintBitsToFloat)(i))),la=(e,t,r,i)=>e.element($(t,Ki.THROUGHPUT_PDF)).assign((0,n.vec4)(r,i)),ua=(e,t,n)=>e.element($(t,Ki.RADIANCE_ALPHA)).assign(n),da=(e,t)=>e.element($(t,qi.DIST_TRI_BARY)).x,fa=(e,t)=>(0,n.floatBitsToUint)(e.element($(t,qi.DIST_TRI_BARY)).y),pa=(e,t)=>e.element($(t,qi.DIST_TRI_BARY)).zw,ma=(e,t)=>e.element($(t,qi.NORMAL_MAT)).xyz,ha=(e,t)=>(0,n.uint)((0,n.floatBitsToUint)(e.element($(t,qi.NORMAL_MAT)).w).bitAnd(65535)),ga=(e,t,r,i,a,o,s,c,l)=>{e.element($(t,qi.DIST_TRI_BARY)).assign((0,n.vec4)(r,(0,n.uintBitsToFloat)(i),a,o)),e.element($(t,qi.NORMAL_MAT)).assign((0,n.vec4)(s,(0,n.uintBitsToFloat)(c.bitOr(l.shiftLeft(16)))))},_a=(e,t)=>{let r=e.element($(t,Ki.MEDIUM_STACK)),i=(0,n.floatBitsToUint)(r.x);return{stackDepth:i.bitAnd(255),transTraversals:i.shiftRight(8).bitAnd(255),wavelength:i.shiftRight(16).bitAnd(65535),ior1:r.y,ior2:r.z,ior3:r.w}},va=(e,t,r,i,a,o,s,c=(0,n.uint)(0))=>e.element($(t,Ki.MEDIUM_STACK)).assign((0,n.vec4)((0,n.uintBitsToFloat)(r.bitOr(i.shiftLeft(8)).bitOr(c.shiftLeft(16))),a,o,s)),ya=(e,t)=>e.element($(t,Ki.MEDIUM_SIGMA_A)).xyz,ba=(e,t,r)=>e.element($(t,Ki.MEDIUM_SIGMA_A)).assign((0,n.vec4)(r,0)),xa=(e,t)=>(0,n.int)((0,n.floatBitsToUint)(e.element($(t,Ki.ORIGIN_META)).w).bitAnd(255)),Sa=(e,t)=>(0,n.int)((0,n.floatBitsToUint)(e.element($(t,Ki.ORIGIN_META)).w).shiftRight(8).bitAnd(255)),Ca=(e,t)=>{let n=e.element($(t,Ki.SSS_SIGMA_S));return{sigmaS:n.xyz,g:n.w}},wa=(e,t,r,i)=>e.element($(t,Ki.SSS_SIGMA_S)).assign((0,n.vec4)(r,i)),Ta={ACTIVE_RAY_COUNT:0,ENTERING_COUNT:1,COUNT:2},Ea={BOUNCE_MASK:255,ACTIVE:256,SPECULAR:512,INSIDE_MEDIUM:1024,RAY_TYPE_SHIFT:11,RAY_TYPE_MASK:63488,HAS_HIT_OPAQUE:65536,AUX_LOCKED:1<<17,REDIRECTED:1<<18},Da=class{constructor(e=0){this.capacity=0,this.counters=null,this.activeIndices=null,this.activeIndicesRO=null,this.sortedIndices=null,this.sortedIndicesRO=null,this.sortGlobalHistogram=null,this.pingPong=0,e>0&&this.allocate(e)}allocate(e){this.dispose(),this.capacity=e,this._countersAttr=new t.StorageInstancedBufferAttribute(new Uint32Array(Ta.COUNT),1),this.counters=(0,n.storage)(this._countersAttr,`uint`).toAtomic(),this.MAX_BOUNCE_SNAPSHOTS=32,this._bounceCountsAttr=new t.StorageInstancedBufferAttribute(new Uint32Array(this.MAX_BOUNCE_SNAPSHOTS),1),this.bounceCounts=(0,n.storage)(this._bounceCountsAttr,`uint`);let r=new t.StorageInstancedBufferAttribute(new Uint32Array(e),1),i=new t.StorageInstancedBufferAttribute(new Uint32Array(e),1);this._attrA=r,this._attrB=i,this.activeIndices={a:(0,n.storage)(r,`uint`),b:(0,n.storage)(i,`uint`)},this.activeIndicesRO={a:(0,n.storage)(r,`uint`).toReadOnly(),b:(0,n.storage)(i,`uint`).toReadOnly()};let a=new t.StorageInstancedBufferAttribute(new Uint32Array(e),1);this._sortAttr=a,this.sortedIndices=(0,n.storage)(a,`uint`),this.sortedIndicesRO=(0,n.storage)(a,`uint`).toReadOnly(),this._sortGlobalHistAttr=new t.StorageInstancedBufferAttribute(new Uint32Array(256),1),this.sortGlobalHistogram=(0,n.storage)(this._sortGlobalHistAttr,`uint`).toAtomic(),this.pingPong=0;let o=Ta.COUNT*4+e*4*3;console.log(`QueueManager: Allocated capacity=${e}, total=${(o/(1024*1024)).toFixed(1)} MB`)}resize(e){return e<=this.capacity&&this.capacity>0?!1:(this.allocate(e),!0)}getCounters(){return this.counters}getActiveReadRO(){return this.pingPong===0?this.activeIndicesRO.a:this.activeIndicesRO.b}getActiveRead(){return this.pingPong===0?this.activeIndices.a:this.activeIndices.b}getActiveWrite(){return this.pingPong===0?this.activeIndices.b:this.activeIndices.a}getSortedRW(){return this.sortedIndices}getSortedRO(){return this.sortedIndicesRO}getSortGlobalHistogram(){return this.sortGlobalHistogram}getCountersAttribute(){return this._countersAttr}getBounceCounts(){return this.bounceCounts}getBounceCountsAttribute(){return this._bounceCountsAttr}swap(){this.pingPong=1-this.pingPong}resetPingPong(){this.pingPong=0}dispose(){this.counters=null,this.activeIndices=null,this.activeIndicesRO=null,this.sortedIndices=null,this.sortedIndicesRO=null,this.sortGlobalHistogram=null,this.capacity=0}},Oa={[r.RGBAFormat]:4,[r.RGBFormat]:3,[r.RGFormat]:2,[r.RedFormat]:1},ka={[r.FloatType]:4,[r.HalfFloatType]:2,[r.UnsignedByteType]:1,[r.ByteType]:1,[r.UnsignedShortType]:2,[r.ShortType]:2,[r.UnsignedIntType]:4,[r.IntType]:4};function Aa(e){return(Oa[e.format]??4)*(ka[e.type]??4)}function ja(e){return e?.array?.byteLength||0}function Ma(e){if(!e)return 0;if(e.isRenderTarget){let t=e.textures?.length?e.textures:[e.texture],n=e.width||0,r=e.height||0,i=e.depth||1,a=0;for(let e of t)e&&(a+=n*r*i*Aa(e));return a}let t=e.image||{},n=t.width??e.width??0,r=t.height??e.height??0,i=t.depth??1;return n*r*i*Aa(e)}var Na=class{constructor(e=null){this._providers=[],this._renderer=e,this.current=0,this.peak=0,this.byCategory={}}setRenderer(e){this._renderer=e}register(e,t){this._providers.push({category:e,fn:t})}measure(){let e=new WeakSet,t={},n=0;for(let{category:r,fn:i}of this._providers){let a;try{a=i()}catch{a=null}if(!a)continue;let o=0;for(let t of Array.isArray(a)?a:[a])o+=this._resourceBytes(t,e);t[r]=(t[r]||0)+o,n+=o}return this.byCategory=t,this.current=n,n>this.peak&&(this.peak=n),{current:n,peak:this.peak,byCategory:t}}_resourceBytes(e,t){return e?typeof e.bytes==`number`&&!e.isTexture&&!e.isRenderTarget?e.bytes:e.array&&e.array.byteLength!=null?t.has(e.array)?0:(t.add(e.array),e.array.byteLength):e.isRenderTarget||e.isTexture?t.has(e)?0:(t.add(e),this._residentTextureBytes(e)):0:0}_isResident(e){let t=this._renderer?.backend;if(!t||typeof t.get!=`function`)return!0;if(typeof t.has==`function`&&!t.has(e))return!1;let n=t.get(e);return!!(n&&(n.texture||n.gpuTexture))}_residentTextureBytes(e){if(e.isRenderTarget){let t=e.textures?.length?e.textures:[e.texture],n=e.width||0,r=e.height||0,i=e.depth||1,a=0;for(let e of t)e&&this._isResident(e)&&(a+=n*r*i*Aa(e));return a}return this._isResident(e)?Ma(e):0}resetPeak(){this.peak=this.current}getReport(){let e=e=>(e/1048576).toFixed(1),t=Object.entries(this.byCategory).map(([t,n])=>`${t}=${e(n)}`);return`VRAM current=${e(this.current)}MB peak=${e(this.peak)}MB [${t.join(` `)}]`}},Pa={generate:[16,16,1],extend:[256,1,1],shade:[256,1,1],connect:[256,1,1],accumulate:[256,1,1],compact:[256,1,1],resetCounters:[1,1,1],finalWrite:[16,16,1]},Fa=class{constructor(e){this.renderer=e,this.kernels=new Map,this.workgroupSizes=new Map,this.timing=new Map,this.profiling=!1,this.profile=new Map;for(let[e,t]of Object.entries(Pa))this.workgroupSizes.set(e,t)}register(e,t){this.kernels.set(e,t),this.timing.set(e,{compiledOnce:!1,lastDispatchMs:0})}dispatch(e){let t=this.kernels.get(e);if(!t)throw Error(`KernelManager: Unknown kernel '${e}'`);let n=this.timing.get(e);if(n&&!n.compiledOnce){let r=performance.now();this.renderer.compute(t);let i=performance.now();n.compiledOnce=!0,n.lastDispatchMs=i-r,console.log(`[Wavefront] Kernel '${e}' first dispatch (includes compilation): ${(i-r).toFixed(1)}ms`)}else if(this.profiling){let n=performance.now();this.renderer.compute(t);let r=performance.now(),i=this.profile.get(e);i||(i={calls:0,totalMs:0},this.profile.set(e,i)),i.calls++,i.totalMs+=r-n}else this.renderer.compute(t)}setDispatchCount(e,t){let n=this.kernels.get(e);n&&(n.dispatchSize=t)}calcScreenDispatch(e,t,n){let r=this.workgroupSizes.get(n)||[16,16,1];return[Math.ceil(e/r[0]),Math.ceil(t/r[1]),1]}calcRayDispatch(e,t){let n=this.workgroupSizes.get(t)||[256,1,1];return[Math.ceil(e/n[0]),1,1]}getWorkgroupSize(e){return this.workgroupSizes.get(e)||[256,1,1]}has(e){return this.kernels.has(e)}get(e){return this.kernels.get(e)}getTimingReport(){let e={};for(let[t,n]of this.timing)e[t]={...n};return e}enableProfiling(e){this.profiling=e,e&&this.profile.clear()}getProfileReport(){let e=[],t=0;for(let[n,{calls:r,totalMs:i}]of this.profile)t+=i,e.push({name:n,calls:r,totalMs:+i.toFixed(2),avgMs:+(i/r).toFixed(3)});return e.sort((e,t)=>t.totalMs-e.totalMs),e.push({name:`TOTAL`,calls:e.reduce((e,t)=>e+t.calls,0),totalMs:+t.toFixed(2),avgMs:null}),e}dispose(){this.kernels.clear(),this.timing.clear(),this.profile.clear()}},Ia=32,La=512,Ra=4,za=8,Ba=1e8,Va=()=>(0,n.array)(`int`,Ia).toVar(),Ha=(0,n.wgslFn)(`
|
|
495
495
|
fn RayTriangleGeometry( rayOrigin: vec3f, rayDir: vec3f, pA: vec3f, pB: vec3f, pC: vec3f, closestHitDst: f32, woopParams: vec4f ) -> vec4f {
|
|
496
496
|
|
|
497
497
|
// Returns vec4(t, u, v, hit) where hit > 0.5 means intersection.
|
|
@@ -652,7 +652,7 @@
|
|
|
652
652
|
if ( isNan || isInf ) { return vec3f( 1.0f, 0.0f, 0.0f ); }
|
|
653
653
|
return vec3f( 0.0f );
|
|
654
654
|
}
|
|
655
|
-
`);function vs(e){let{rayBufferRO:t,gBufferRO:r,writeColorTex:i,writeNDTex:a,writeAlbedoTex:o,resolution:s,frame:c,enableAccumulation:l,hasPreviousAccumulated:u,accumulationAlpha:d,cameraIsMoving:f,transparentBackground:p,prevAccumTexture:m,
|
|
655
|
+
`);function vs(e){let{rayBufferRO:t,gBufferRO:r,writeColorTex:i,writeNDTex:a,writeAlbedoTex:o,resolution:s,frame:c,enableAccumulation:l,hasPreviousAccumulated:u,accumulationAlpha:d,cameraIsMoving:f,transparentBackground:p,prevAccumTexture:m,prevAlbedoTexture:h,renderWidth:g,renderHeight:_,visMode:v,auxGBufferEnabled:y}=e,b=y.greaterThan((0,n.uint)(0));return(0,n.Fn)(()=>{let e=(0,n.int)(n.workgroupId.x).mul(16).add((0,n.int)(n.localId.x)),y=(0,n.int)(n.workgroupId.y).mul(16).add((0,n.int)(n.localId.y));(0,n.If)(e.lessThan(g).and(y.lessThan(_)),()=>{let g=(0,n.uint)(y.mul((0,n.int)(s.x)).add(e)),_=ta(t,g),x=_.xyz.toVar(),S=(0,n.select)(p,_.w,(0,n.float)(1)).toVar(),C=(0,n.vec4)(0).toVar(),w=(0,n.vec4)(0).xyz.toVar();(0,n.If)(b,()=>{let e=ia(r,g);C.assign(aa(e)),w.assign((0,n.vec4)(oa(e),0).xyz)});let T=(0,n.vec2)((0,n.float)(e).add(.5),(0,n.float)(y).add(.5)).div(s);(0,n.If)(l.and(f.not()).and(c.greaterThan((0,n.uint)(0))).and(u).and(v.notEqual((0,n.int)(11))),()=>{let e=(0,n.texture)(m,T,0).toVar();x.assign((0,n.mix)(e.xyz,_.xyz,d)),(0,n.If)(b,()=>{w.assign((0,n.mix)((0,n.texture)(h,T,0).xyz,w,d))}),(0,n.If)(p,()=>{S.assign((0,n.mix)(e.w,_.w,d))})}),(0,n.If)(v.equal((0,n.int)(11)),()=>{x.assign(_s(x))});let E=(0,n.uvec2)((0,n.uint)(e),(0,n.uint)(y));(0,n.textureStore)(i,E,(0,n.vec4)(x,S)).toWriteOnly(),(0,n.If)(b,()=>{(0,n.textureStore)(a,E,C).toWriteOnly(),(0,n.textureStore)(o,E,(0,n.vec4)(w,1)).toWriteOnly()})})})}var ys=(0,n.wgslFn)(`
|
|
656
656
|
fn visualizeDepth( depth: f32 ) -> vec3f {
|
|
657
657
|
return vec3f( 1.0f - depth );
|
|
658
658
|
}
|
|
@@ -666,7 +666,7 @@
|
|
|
666
666
|
let ndcDepth = clipPos.z / clipPos.w * 0.5f + 0.5f;
|
|
667
667
|
return clamp( ndcDepth, 0.0f, 1.0f );
|
|
668
668
|
}
|
|
669
|
-
`),Ss=(0,n.Fn)(([e,t,r,i,a,o,s,c,l,u,d,f,p,m,h,g,_,v,y,b,x,S])=>{let C=(0,n.vec4)(1,0,1,1).toVar(),w=wt({origin:e,direction:t}),T=Ot.wrap(Ga(w,r,i).toVar());return(0,n.If)(u.equal((0,n.int)(7)),()=>{let e=(0,n.float)(T.triTests).div(d);C.assign((0,n.select)(e.lessThan(1),(0,n.vec4)((0,n.vec3)(e),1),(0,n.vec4)(1,0,0,1)))}),(0,n.If)(u.equal((0,n.int)(8)),()=>{let e=(0,n.float)(T.boxTests).div(d);C.assign((0,n.select)(e.lessThan(1),(0,n.vec4)((0,n.vec3)(e),1),(0,n.vec4)(1,0,0,1)))}),(0,n.If)(u.equal((0,n.int)(10)),()=>{(0,n.If)(l,()=>{let e=eo({tex:o,samp:(0,n.sampler)(o),direction:t,environmentMatrix:s,environmentIntensity:c,enableEnvironmentLight:l}).toVar(),r=(0,n.dot)(e.xyz,qt).toVar(),i=r,a=(0,n.max)(d.mul(.1),.001),u=r.div(a),m=(0,n.log)(r.add(1e-6)).add(10).div(10),h=(0,n.select)(d.greaterThan(1),u,m).toVar(),g=(0,n.vec3)(0).toVar();(0,n.If)(h.lessThan(.2),()=>{g.assign((0,n.mix)((0,n.vec3)(0,0,0),(0,n.vec3)(0,0,.5),h.mul(5)))}).ElseIf(h.lessThan(.4),()=>{g.assign((0,n.mix)((0,n.vec3)(0,0,.5),(0,n.vec3)(0,0,1),h.sub(.2).mul(5)))}).ElseIf(h.lessThan(.6),()=>{g.assign((0,n.mix)((0,n.vec3)(0,0,1),(0,n.vec3)(0,1,0),h.sub(.4).mul(5)))}).ElseIf(h.lessThan(.8),()=>{g.assign((0,n.mix)((0,n.vec3)(0,1,0),(0,n.vec3)(1,1,0),h.sub(.6).mul(5)))}).ElseIf(h.lessThan(1),()=>{g.assign((0,n.mix)((0,n.vec3)(1,1,0),(0,n.vec3)(1,0,0),h.sub(.8).mul(5)))}).Else(()=>{g.assign((0,n.mix)((0,n.vec3)(1,0,0),(0,n.vec3)(1,1,1),(0,n.min)(h.sub(1),1)))});let _=f.div(p);(0,n.If)(_.x.lessThan(.1).and(_.y.lessThan(.1)),()=>{let e=i.mul(1);g.assign((0,n.vec3)(e))}).ElseIf(_.x.greaterThan(.9).and(_.y.lessThan(.1)),()=>{g.assign(e.xyz.mul(1))}),C.assign((0,n.vec4)(g,1))}).Else(()=>{C.assign((0,n.vec4)(1,0,1,1))})}),(0,n.If)(u.equal((0,n.int)(4)),()=>{(0,n.If)(T.didHit.not(),()=>{C.assign((0,n.vec4)(0,0,0,1))}).Else(()=>{let t=Tt.wrap(un(T.materialIndex,a)).toVar(),r=Pt.wrap(dr(m,h,g,_,v,y,t,T.uv,T.normal)).toVar().emissive.toVar(),i=(0,n.length)(r).toVar();(0,n.If)(i.greaterThan(0),()=>{let t=r.div((0,n.max)(i.mul(.1),.001)).toVar(),a=(0,n.length)(e.sub(T.hitPoint)),o=(0,n.clamp)((0,n.float)(1).sub(a.div(10)),0,1),s=(0,n.mix)(t,t.mul((0,n.vec3)(1,.8,.6)),o.mul(.3));C.assign((0,n.vec4)(s,1))}).Else(()=>{C.assign((0,n.vec4)(0,0,.1,1))})})}),(0,n.If)(u.equal((0,n.int)(1)),()=>{(0,n.If)(T.didHit.not(),()=>{C.assign((0,n.vec4)(.5,.5,1,1))}).Else(()=>{let e=Tt.wrap(un(T.materialIndex,a)).toVar(),t=(0,n.normalize)(Pt.wrap(dr(m,h,g,_,v,y,e,T.uv,T.normal)).toVar().normal);C.assign((0,n.vec4)(bs({normal:t}),1))})}),(0,n.If)(u.equal((0,n.int)(2)),()=>{(0,n.If)(T.didHit.not(),()=>{C.assign((0,n.vec4)((0,n.vec3)(1),1))}).Else(()=>{let e=xs({worldPos:T.hitPoint,cameraProjectionMatrix:b,cameraViewMatrix:x});C.assign((0,n.vec4)(ys({depth:e}),1))})}),(0,n.If)(u.equal((0,n.int)(3)),()=>{(0,n.If)(T.didHit.not(),()=>{C.assign((0,n.vec4)(0,0,0,1))}).Else(()=>{let e=Tt.wrap(un(T.materialIndex,a)).toVar(),t=Pt.wrap(dr(m,h,g,_,v,y,e,T.uv,T.normal)).toVar().albedo.rgb;C.assign((0,n.vec4)(t,1))})}),(0,n.If)(u.equal((0,n.int)(5)),()=>{(0,n.If)(T.didHit.not(),()=>{C.assign((0,n.vec4)(0,0,0,1))}).Else(()=>{let e=Tt.wrap(un(T.materialIndex,a)).toVar(),t=Pt.wrap(dr(m,h,g,_,v,y,e,T.uv,T.normal)).toVar(),u=t.albedo.rgb,d=(0,n.normalize)(t.normal).toVar(),p=ne({state:re({seed:(0,n.uint)(f.x).mul((0,n.uint)(1973)).add((0,n.uint)(f.y).mul((0,n.uint)(9277))).add(S.mul((0,n.uint)(26699)))})}).toVar(),b=Pn({N:d,xi:(0,n.vec2)(I(p).toVar(),I(p).toVar())}).toVar(),x=wt({origin:T.hitPoint.add(d.mul(.001)),direction:b}),w=Ot.wrap(Ga(x,r,i).toVar()),E=(0,n.vec3)(0).toVar();(0,n.If)(w.didHit.not(),()=>{(0,n.If)(l,()=>{E.assign(eo({tex:o,samp:(0,n.sampler)(o),direction:b,environmentMatrix:s,environmentIntensity:c,enableEnvironmentLight:l}).xyz)})}).Else(()=>{let e=Tt.wrap(un(w.materialIndex,a)).toVar(),t=Pt.wrap(dr(m,h,g,_,v,y,e,w.uv,w.normal)).toVar();E.assign(t.emissive),(0,n.If)(l,()=>{let e=(0,n.normalize)(t.normal).toVar(),r=eo({tex:o,samp:(0,n.sampler)(o),direction:e,environmentMatrix:s,environmentIntensity:c,enableEnvironmentLight:l}).xyz;E.addAssign(t.albedo.rgb.mul(r))})});let D=u.mul(E);C.assign((0,n.vec4)(D,1))})}),(0,n.If)(u.equal((0,n.int)(6)),()=>{(0,n.If)(T.didHit,()=>{let e=T.normal.toVar(),r=(0,n.normalize)(t.sub(e.mul((0,n.dot)(t,e).mul(2)))).toVar(),i=eo({tex:o,samp:(0,n.sampler)(o),direction:r,environmentMatrix:s,environmentIntensity:c,enableEnvironmentLight:l}).xyz,a=i.div(i.add(1));C.assign((0,n.vec4)(a,1))}).Else(()=>{let e=eo({tex:o,samp:(0,n.sampler)(o),direction:t,environmentMatrix:s,environmentIntensity:c,enableEnvironmentLight:l}).xyz,r=e.div(e.add(1));C.assign((0,n.vec4)(r,1))})}),C});function Cs(e){let{writeColorTex:t,writeNDTex:r,writeAlbedoTex:i,resolution:a,renderWidth:o,renderHeight:s,cameraWorldMatrix:c,cameraProjectionMatrixInverse:l,cameraProjectionMatrix:u,cameraViewMatrix:d,enableDOF:f,focalLength:p,aperture:m,focusDistance:h,sceneScale:g,apertureScale:_,anamorphicRatio:v,bvhBuffer:y,triangleBuffer:b,materialBuffer:x,envTexture:S,environmentMatrix:C,environmentIntensity:w,enableEnvironmentLight:T,visMode:E,debugVisScale:D,albedoMaps:O,normalMaps:k,bumpMaps:A,metalnessMaps:j,roughnessMaps:M,emissiveMaps:N,frame:P}=e;return(0,n.Fn)(()=>{let e=(0,n.int)(n.workgroupId.x).mul(16).add((0,n.int)(n.localId.x)),F=(0,n.int)(n.workgroupId.y).mul(16).add((0,n.int)(n.localId.y));(0,n.If)(e.lessThan(o).and(F.lessThan(s)),()=>{let o=(0,n.vec2)((0,n.float)(e).add(.5),(0,n.float)(F).add(.5)),s=ne({state:(0,n.uint)(F.mul((0,n.int)(a.x)).add(e)).add((0,n.uint)(1))}).toVar(),ee=o.div(a).mul(2).sub(1).toVar();ee.y.assign(ee.y.negate());let te=wt.wrap(qa(ee,s,c,l,f,p,m,h,g,_,v)),re=(0,n.vec4)(1,0,1,1).toVar();(0,n.If)(E.equal((0,n.int)(9)),()=>{let e=pe(o,(0,n.int)(0),(0,n.int)(1),s,a,P);re.assign((0,n.vec4)(e,1,1))}).Else(()=>{re.assign(Ss(te.origin,te.direction,y,b,x,S,C,w,T,E,D,o,a,O,k,A,j,M,N,u,d,P))});let ie=(0,n.uvec2)((0,n.uint)(e),(0,n.uint)(F));(0,n.textureStore)(t,ie,re).toWriteOnly(),(0,n.textureStore)(r,ie,(0,n.vec4)(.5,.5,1,1)).toWriteOnly(),(0,n.textureStore)(i,ie,(0,n.vec4)(re.xyz,1)).toWriteOnly()})})}function ws({sortGlobalHistogram:e,bins:t=256}){return(0,n.Fn)(()=>{(0,n.If)(n.instanceIndex.lessThan((0,n.uint)(t)),()=>{(0,n.atomicStore)(e.element(n.instanceIndex),(0,n.uint)(0))})})}function Ts({hitBufferRO:e,activeIndicesReadRO:t,sortGlobalHistogram:r,counters:i,bins:a=256}){return(0,n.Fn)(()=>{let o=n.instanceIndex,s=n.localId.x,c=T(`uint`,a);(0,n.If)(s.lessThan((0,n.uint)(a)),()=>{(0,n.atomicStore)(c.element(s),(0,n.uint)(0))}),(0,n.workgroupBarrier)();let l=(0,n.atomicLoad)(i.element((0,n.uint)(Ta.ENTERING_COUNT)));(0,n.If)(o.lessThan(l),()=>{let r=(0,n.uint)(ha(e,t.element(o))).clamp((0,n.uint)(0),(0,n.uint)(a-1));(0,n.atomicAdd)(c.element(r),(0,n.uint)(1))}),(0,n.workgroupBarrier)(),(0,n.If)(s.lessThan((0,n.uint)(a)),()=>{let e=(0,n.atomicLoad)(c.element(s));(0,n.If)(e.greaterThan((0,n.uint)(0)),()=>{(0,n.atomicAdd)(r.element(s),e)})})})}function Es({sortGlobalHistogram:e,bins:t=256}){return(0,n.Fn)(()=>{(0,n.If)(n.instanceIndex.equal((0,n.uint)(0)),()=>{let r=(0,n.uint)(0).toVar();(0,n.Loop)({start:(0,n.uint)(0),end:(0,n.uint)(t),type:`uint`},({i:t})=>{let i=e.element(t),a=(0,n.atomicLoad)(i);(0,n.atomicStore)(i,r),r.addAssign(a)})})})}function Ds({hitBufferRO:e,activeIndicesReadRO:t,sortedIndicesRW:r,sortGlobalHistogram:i,counters:a,bins:o=256}){return(0,n.Fn)(()=>{let s=n.instanceIndex,c=(0,n.atomicLoad)(a.element((0,n.uint)(Ta.ENTERING_COUNT)));(0,n.If)(s.lessThan(c),()=>{let a=t.element(s),c=(0,n.uint)(ha(e,a)).clamp((0,n.uint)(0),(0,n.uint)(o-1)),l=(0,n.atomicAdd)(i.element(c),(0,n.uint)(1));r.element(l).assign(a)})})}var Os=class extends Gi{constructor(e,t,r,i={}){super(e,t,r,i),this.name=`PathTracer`,this._packedBuffers=null,this._queueManager=null,this._kernelManager=null,this._gBufferAttr=null,this._wavefrontReady=!1,this._auxGBufferEnabled=!1,this._auxGBufferUniform=(0,n.uniform)(0,`uint`),this._useDynamicDispatch=!0,this._sortMaterials=!1,this._useSubgroupCompact=!1,this._lastBounceCounts=null,this._lastBounceCountsBudget=-1,this._readbackPending=!1,this._readbackEveryNFrames=4,this._readbackFrameCounter=0,this._readbackGeneration=0,this._bounceEarlyExitThreshold=100,this._wfRenderWidth=(0,n.uniform)(1920,`int`),this._wfRenderHeight=(0,n.uniform)(1080,`int`),this._wfMaxRayCount=(0,n.uniform)(0,`uint`),this._wfCurrentBounce=(0,n.uniform)(0,`int`),this.vramTracker=new Na(this.renderer),this._registerVRAMProviders(),console.log(`PathTracer: initialized (wavefront)`)}_registerVRAMProviders(){let e=this.vramTracker;e.register(`rays`,()=>{let e=this._packedBuffers?._attrs;return e?[e.ray,e.rng,e.hit]:null}),e.register(`queues`,()=>{let e=this._queueManager;return e?[e._countersAttr,e._bounceCountsAttr,e._attrA,e._attrB,e._sortAttr]:null}),e.register(`gbuffer`,()=>this._gBufferAttr?[this._gBufferAttr]:null),e.register(`accum`,()=>{let e=this.storageTextures;return e?[e.writeColor,e.writeNormalDepth,e.writeAlbedo,e.readTarget]:null}),e.register(`geometry`,()=>[this.triangleStorageAttr,this.bvhStorageAttr,this.lightStorageAttr]),e.register(`materials`,()=>{let e=this.materialData;return e?[e.materialStorageAttr,e.albedoMaps,e.emissiveMaps,e.normalMaps,e.bumpMaps,e.roughnessMaps,e.metalnessMaps,e.displacementMaps]:null}),e.register(`environment`,()=>{let e=this.environment;return e?[e.environmentTexture,e.envCDFTexture]:null})}setupMaterial(){super.setupMaterial(),this.materialData?.materialCount>0&&(this._kernelManager&&this._kernelManager.dispose(),this._wavefrontReady=!1,this._buildWavefrontKernels())}render(e){if(!this.isReady||!this._wavefrontReady)return;if(this.isComplete||this.frameCount>=this.completionThreshold){this.isComplete||=!0;return}this.performanceMonitor?.start();let t=this.frameCount,n=this.renderMode.value,r=null;n===1&&t===0&&(r=this.maxBounces.value,this.maxBounces.value=1),this._handleResize(),this.manageASVGFForRenderMode(n),e&&e.setState(`tileRenderingComplete`,!0),this.cameraChanged=this._updateCameraUniforms(),this.cameraOptimizer?.updateInteractionMode(this.cameraChanged),this._updateAccumulationUniforms(t,n),this.frame.value=t,this._setWfDispatch();let i=this.storageTextures.getReadTextures();this.shaderBuilder.prevColorTexNode&&(this.shaderBuilder.prevColorTexNode.value=i.color,this.shaderBuilder.prevNormalDepthTexNode.value=i.normalDepth,this.shaderBuilder.prevAlbedoTexNode.value=i.albedo),this._refreshWfTextureNodes();let a=this._kernelManager;if((this.visMode?.value|0)>0&&this.visMode.value!==11){a.dispatch(`debug`),this.storageTextures.copyToReadTargets(this.renderer);let t=this.storageTextures.getReadTextures();e&&this._publishTexturesToContext(e,t),this._emitStateEvents(),this.cameraOptimizer?.isInInteractionMode()||this.frameCount++,r!==null&&(this.maxBounces.value=r),this.performanceMonitor?.end();return}a.dispatch(`resetCounters`),a.dispatch(`generate`),a.dispatch(`initActiveIndices`);let o=this.maxBounces.value,s=o+this.transmissiveBounces.value+this.maxSubsurfaceSteps.value,c=this._wfMaxRayCount.value,l=this._lastBounceCounts,u=l?o<=this._lastBounceCountsBudget?s+1:this._lastBounceCountsBudget:0;for(let e=0;e<=s;e++){this._wfCurrentBounce.value=e;let t=this._useDynamicDispatch;if(t){let t=c;if(e>0){let n=e-1,r;n<u&&l[n]!==void 0?r=l[n]:u>0&&(r=l[u-1]),t=r>0?r:c}let n=Math.min(c,Math.ceil(t*1.5)+1024),r=[Math.ceil(n/256),1,1];a.setDispatchCount(`extend`,r),a.setDispatchCount(`shade`,r),a.setDispatchCount(`globalHist`,r),a.setDispatchCount(`globalScatter`,r),a.setDispatchCount(`compact`,r),a.setDispatchCount(`compactCopyback`,r)}else{a.dispatch(`enterFull`);let e=[Math.ceil(c/256),1,1];a.setDispatchCount(`extend`,e),a.setDispatchCount(`shade`,e),a.setDispatchCount(`compact`,e),a.setDispatchCount(`globalHist`,e),a.setDispatchCount(`globalScatter`,e)}if(a.dispatch(`extend`),this._sortMaterials&&(a.dispatch(`resetGlobalHist`),a.dispatch(`globalHist`),a.dispatch(`globalPrefix`),a.dispatch(`globalScatter`)),a.dispatch(`shade`),a.dispatch(`resetActiveCounter`),a.dispatch(`compact`),t&&a.dispatch(`compactCopyback`),a.dispatch(`snapshotBounceCount`),e<u&&e<s&&l[e]!==void 0&&l[e]<=this._bounceEarlyExitThreshold)break}a.dispatch(`finalWrite`),this._maybeReadbackCounters(),this.storageTextures.copyToReadTargets(this.renderer,this._auxGBufferEnabled);let d=this.storageTextures.getReadTextures();e&&this._publishTexturesToContext(e,d),this._emitStateEvents(),this.cameraOptimizer?.isInInteractionMode()||this.frameCount++,r!==null&&(this.maxBounces.value=r),this.performanceMonitor?.end()}_handleResize(){let e=this.storageTextures.renderWidth,t=this.storageTextures.renderHeight;super._handleResize(),this._rebuildKernelsIfResized(e,t)}setAuxGBufferEnabled(e){e=!!e,this._auxGBufferEnabled!==e&&(this._auxGBufferEnabled=e,this._auxGBufferUniform.value=+!!e,this.reset())}setSize(e,t){let n=this.storageTextures.renderWidth,r=this.storageTextures.renderHeight;super.setSize(e,t),this._rebuildKernelsIfResized(n,r)}_maybeReadbackCounters(){if(this._readbackPending||(this._readbackFrameCounter++,this._readbackFrameCounter<this._readbackEveryNFrames))return;this._readbackFrameCounter=0;let e=this._queueManager?.getBounceCountsAttribute();if(!e)return;this._readbackPending=!0;let t=this._readbackGeneration,n=this.maxBounces.value;this.renderer.getArrayBufferAsync(e).then(e=>{t===this._readbackGeneration&&(this._lastBounceCounts=new Uint32Array(e.slice(0)),this._lastBounceCountsBudget=n),this._readbackPending=!1}).catch(e=>{console.warn(`Wavefront bounceCounts readback failed:`,e),this._readbackPending=!1})}_refreshWfTextureNodes(){let e=this._wfTexNodes;if(!e)return;let t=this.environment?.environmentTexture;t&&e.envTex&&(e.envTex.value=t),this.environment?.envCDFTexture&&e.envCDFTex&&(e.envCDFTex.value=this.environment.envCDFTexture);let n=this.materialData;n&&(n.albedoMaps&&e.albedoMaps&&(e.albedoMaps.value=n.albedoMaps),n.normalMaps&&e.normalMaps&&(e.normalMaps.value=n.normalMaps),n.bumpMaps&&e.bumpMaps&&(e.bumpMaps.value=n.bumpMaps),n.metalnessMaps&&e.metalnessMaps&&(e.metalnessMaps.value=n.metalnessMaps),n.roughnessMaps&&e.roughnessMaps&&(e.roughnessMaps.value=n.roughnessMaps),n.emissiveMaps&&e.emissiveMaps&&(e.emissiveMaps.value=n.emissiveMaps),n.displacementMaps&&e.displacementMaps&&(e.displacementMaps.value=n.displacementMaps))}_rebuildKernelsIfResized(e,t){let n=this.storageTextures.renderWidth,r=this.storageTextures.renderHeight;if(n===e&&r===t||!(this.materialData?.materialCount>0))return;this._lastBounceCounts=null,this._lastBounceCountsBudget=-1,this._readbackFrameCounter=0,this._readbackGeneration++;let i=Yi.requiredCapacity(n*r);!this._packedBuffers||i>this._packedBuffers.capacity?(this._kernelManager&&this._kernelManager.dispose(),this._wavefrontReady=!1,this._buildWavefrontKernels()):this._resizeWavefrontInPlace(n,r)}_resizeWavefrontInPlace(e,t){let n=e*t;this._wfRenderWidth.value=e,this._wfRenderHeight.value=t,this._wfMaxRayCount.value=n,this._kernelManager?.setDispatchCount(`initActiveIndices`,[Math.ceil(n/256),1,1]),this._bounceEarlyExitThreshold!==-1&&(this._bounceEarlyExitThreshold=Math.max(100,Math.floor(n/1e3)))}_buildWavefrontKernels(){let e=this.shaderBuilder.getSceneTextureNodes();if(!e)return;let r=this.storageTextures.renderWidth,i=this.storageTextures.renderHeight,a=r*i;this._bounceEarlyExitThreshold!==-1&&(this._bounceEarlyExitThreshold=Math.max(100,Math.floor(a/1e3))),this._packedBuffers?this._packedBuffers.resize(a):this._packedBuffers=new Yi(a);let o=Yi.requiredCapacity(a)*1;this._gBufferAttr=new t.StorageInstancedBufferAttribute(new Uint32Array(o*4),4);let s=(0,n.storage)(this._gBufferAttr,`uvec4`),c=(0,n.storage)(this._gBufferAttr,`uvec4`).toReadOnly();this._queueManager?this._queueManager.resize(this._packedBuffers.capacity):this._queueManager=new Da(this._packedBuffers.capacity),this._kernelManager||=new Fa(this.renderer);let l=this._packedBuffers,u=this._queueManager;this._wfRenderWidth.value=r,this._wfRenderHeight.value=i,this._wfMaxRayCount.value=a;let d=this.shaderBuilder.prevColorTexNode,f=this.shaderBuilder.prevNormalDepthTexNode,p=this.shaderBuilder.prevAlbedoTexNode,m=this.storageTextures.getWriteTextures(),h=u.getCounters(),g=(0,n.Fn)(()=>{(0,n.atomicStore)(h.element((0,n.uint)(Ta.ACTIVE_RAY_COUNT)),(0,n.uint)(0))});this._kernelManager.register(`resetCounters`,g().compute([1,1,1],[1,1,1]));let _=(0,n.Fn)(()=>{(0,n.atomicStore)(h.element((0,n.uint)(Ta.ACTIVE_RAY_COUNT)),(0,n.uint)(0))});this._kernelManager.register(`resetActiveCounter`,_().compute([1,1,1],[1,1,1]));let v=u.getBounceCounts(),y=this._wfCurrentBounce,b=(0,n.Fn)(()=>{let e=(0,n.atomicLoad)(h.element((0,n.uint)(Ta.ACTIVE_RAY_COUNT))),t=(0,n.uint)(y).clamp((0,n.uint)(0),(0,n.uint)(u.MAX_BOUNCE_SNAPSHOTS-1));v.element(t).assign(e),(0,n.atomicStore)(h.element((0,n.uint)(Ta.ENTERING_COUNT)),e)});this._kernelManager.register(`snapshotBounceCount`,b().compute([1,1,1],[1,1,1]));let x=u.activeIndices.a,S=(0,n.Fn)(()=>{let e=n.instanceIndex;x.element(e).assign(e),(0,n.If)(e.equal((0,n.uint)(0)),()=>{(0,n.atomicStore)(h.element((0,n.uint)(Ta.ACTIVE_RAY_COUNT)),this._wfMaxRayCount),(0,n.atomicStore)(h.element((0,n.uint)(Ta.ENTERING_COUNT)),this._wfMaxRayCount)})});this._kernelManager.register(`initActiveIndices`,S().compute([Math.ceil(a/256),1,1],[256,1,1]));let C=Ja({rayBufferRW:l.rayBuffer.rw,rngBufferRW:l.rngBuffer.rw,gBufferRW:s,resolution:this.resolution,frame:this.frame,cameraWorldMatrix:this.cameraWorldMatrix,cameraProjectionMatrixInverse:this.cameraProjectionMatrixInverse,enableDOF:this.enableDOF,focalLength:this.focalLength,aperture:this.aperture,focusDistance:this.focusDistance,sceneScale:this.sceneScale,apertureScale:this.apertureScale,anamorphicRatio:this.anamorphicRatio,renderWidth:this._wfRenderWidth,renderHeight:this._wfRenderHeight,transmissiveBounces:this.transmissiveBounces,transparentBackground:this.transparentBackground,auxGBufferEnabled:this._auxGBufferUniform});this._kernelManager.register(`generate`,C().compute([Math.ceil(r/16),Math.ceil(i/16),1],[16,16,1]));let w=this.bvhStorageNode,T=this.triangleStorageNode,E=this.materialData.materialStorageNode,D=(0,n.texture)(this.environment.envCDFTexture),O=this.lightStorageNode,k=this.materialData,A=this.environment,j=e.albedoMapsTex,M=k.albedoMaps?(0,n.texture)(k.albedoMaps):j,N=k.normalMaps?(0,n.texture)(k.normalMaps):e.normalMapsTex,P=k.bumpMaps?(0,n.texture)(k.bumpMaps):e.bumpMapsTex,F=k.metalnessMaps?(0,n.texture)(k.metalnessMaps):e.metalnessMapsTex,ee=k.roughnessMaps?(0,n.texture)(k.roughnessMaps):e.roughnessMapsTex,te=k.emissiveMaps?(0,n.texture)(k.emissiveMaps):e.emissiveMapsTex,ne=k.displacementMaps?(0,n.texture)(k.displacementMaps):e.displacementMapsTex,re=A.environmentTexture?(0,n.texture)(A.environmentTexture):e.envTex;this._wfTexNodes={envTex:re,envCDFTex:D,albedoMaps:M,normalMaps:N,bumpMaps:P,metalnessMaps:F,roughnessMaps:ee,emissiveMaps:te,displacementMaps:ne},this._sortMaterials=(B.wavefrontSortMaterials??!1)&&(this.materialData?.materialCount??0)>8;let ie=Ya({bvhBuffer:w,triangleBuffer:T,materialBuffer:E,rayBufferRO:l.rayBuffer.ro,hitBufferRW:l.hitBuffer.rw,activeIndicesRO:u.getActiveReadRO(),counters:h,maxRayCount:this._wfMaxRayCount});if(this._kernelManager.register(`extend`,ie().compute([Math.ceil(a/256),1,1],[256,1,1])),this._sortMaterials){let e=u.getSortGlobalHistogram(),t=Math.min(256,this.materialData?.materialCount??256);this._kernelManager.register(`resetGlobalHist`,ws({sortGlobalHistogram:e,bins:t})().compute([1,1,1],[256,1,1])),this._kernelManager.register(`globalHist`,Ts({hitBufferRO:l.hitBuffer.ro,activeIndicesReadRO:u.getActiveReadRO(),sortGlobalHistogram:e,counters:h,bins:t})().compute([Math.ceil(a/256),1,1],[256,1,1])),this._kernelManager.register(`globalPrefix`,Es({sortGlobalHistogram:e,bins:t})().compute([1,1,1],[1,1,1])),this._kernelManager.register(`globalScatter`,Ds({hitBufferRO:l.hitBuffer.ro,activeIndicesReadRO:u.getActiveReadRO(),sortedIndicesRW:u.getSortedRW(),sortGlobalHistogram:e,counters:h,bins:t})().compute([Math.ceil(a/256),1,1],[256,1,1]))}let I=ms({gBufferRW:s,envCompensationDelta:this.envCompensationDelta,bvhBuffer:w,triangleBuffer:T,materialBuffer:E,envCDFTexture:D,lightBuffer:O,rayBufferRW:l.rayBuffer.rw,rngBufferRW:l.rngBuffer.rw,hitBufferRO:l.hitBuffer.ro,counters:h,activeIndicesRO:this._sortMaterials?u.getSortedRO():u.getActiveReadRO(),albedoMaps:M,normalMaps:N,bumpMaps:P,metalnessMaps:F,roughnessMaps:ee,emissiveMaps:te,displacementMaps:ne,envTexture:re,environmentIntensity:this.environmentIntensity,envMatrix:this.environmentMatrix,enableEnvironmentLight:this.enableEnvironment,useEnvMapIS:this.useEnvMapIS,groundProjectionEnabled:this.groundProjectionEnabled,groundProjectionRadius:this.groundProjectionRadius,groundProjectionHeight:this.groundProjectionHeight,groundProjectionLevel:this.groundProjectionLevel,enableGroundCatcher:this.enableGroundCatcher,groundCatcherHeight:this.groundCatcherHeight,envTotalSum:this.envTotalSum,envResolution:this.envResolution,directionalLightsBuffer:this.directionalLightsBufferNode,numDirectionalLights:this.numDirectionalLights,areaLightsBuffer:this.areaLightsBufferNode,numAreaLights:this.numAreaLights,pointLightsBuffer:this.pointLightsBufferNode,numPointLights:this.numPointLights,spotLightsBuffer:this.spotLightsBufferNode,numSpotLights:this.numSpotLights,maxBounceCount:this.maxBounces,maxSubsurfaceSteps:this.maxSubsurfaceSteps,transparentBackground:this.transparentBackground,backgroundIntensity:this.backgroundIntensity,backgroundColor:this.backgroundColor,backgroundBlurriness:this.backgroundBlurriness,backgroundBlurSamples:this.backgroundBlurSamples,showBackground:this.showBackground,globalIlluminationIntensity:this.globalIlluminationIntensity,cameraProjectionMatrix:this.cameraProjectionMatrix,cameraViewMatrix:this.cameraViewMatrix,fireflyThreshold:this.fireflyThreshold,frame:this.frame,resolution:this.resolution,emissiveTriangleCount:this.emissiveTriangleCount,emissiveVec4Offset:this.emissiveVec4Offset,emissiveTotalPower:this.emissiveTotalPower,emissiveBoost:this.emissiveBoost,totalTriangleCount:this.totalTriangleCount,enableEmissiveTriangleSampling:this.enableEmissiveTriangleSampling,lightBVHNodeCount:this.lightBVHNodeCount,reverseMapVec4Offset:this.reverseMapVec4Offset,currentBounce:this._wfCurrentBounce,maxRayCount:this._wfMaxRayCount,auxGBufferEnabled:this._auxGBufferUniform});this._kernelManager.register(`shade`,I().compute([Math.ceil(a/256),1,1],[256,1,1]));let ae=this._useSubgroupCompact&&(this.renderer.hasFeature?this.renderer.hasFeature(`subgroups`):!1);this._compactIsSubgroup=ae;let oe=(ae?gs:hs)({rayBufferRO:l.rayBuffer.ro,activeIndicesReadRO:u.getActiveReadRO(),activeIndicesWriteRW:u.getActiveWrite(),counters:h,currentActiveCount:this._wfMaxRayCount});this._kernelManager.register(`compact`,oe().compute([Math.ceil(a/256),1,1],[256,1,1]));let se=(0,n.Fn)(()=>{(0,n.atomicStore)(h.element((0,n.uint)(Ta.ENTERING_COUNT)),this._wfMaxRayCount)});this._kernelManager.register(`enterFull`,se().compute([1,1,1],[1,1,1]));let ce=u.activeIndicesRO.b,le=u.activeIndices.a,ue=(0,n.Fn)(()=>{let e=n.instanceIndex;(0,n.If)(e.greaterThanEqual((0,n.atomicLoad)(h.element((0,n.uint)(Ta.ACTIVE_RAY_COUNT)))),()=>{(0,n.Return)()}),le.element(e).assign(ce.element(e))});this._kernelManager.register(`compactCopyback`,ue().compute([Math.ceil(a/256),1,1],[256,1,1]));let de=vs({rayBufferRO:l.rayBuffer.ro,gBufferRO:c,writeColorTex:m.color,writeNDTex:m.normalDepth,writeAlbedoTex:m.albedo,resolution:this.resolution,frame:this.frame,enableAccumulation:this.enableAccumulation,hasPreviousAccumulated:this.hasPreviousAccumulated,accumulationAlpha:this.accumulationAlpha,cameraIsMoving:this.cameraIsMoving,transparentBackground:this.transparentBackground,prevAccumTexture:d,prevNormalDepthTexture:f,prevAlbedoTexture:p,renderWidth:this._wfRenderWidth,renderHeight:this._wfRenderHeight,visMode:this.visMode,auxGBufferEnabled:this._auxGBufferUniform});this._kernelManager.register(`finalWrite`,de().compute([Math.ceil(r/16),Math.ceil(i/16),1],[16,16,1]));let fe=Cs({writeColorTex:m.color,writeNDTex:m.normalDepth,writeAlbedoTex:m.albedo,resolution:this.resolution,renderWidth:this._wfRenderWidth,renderHeight:this._wfRenderHeight,cameraWorldMatrix:this.cameraWorldMatrix,cameraProjectionMatrixInverse:this.cameraProjectionMatrixInverse,cameraProjectionMatrix:this.cameraProjectionMatrix,cameraViewMatrix:this.cameraViewMatrix,enableDOF:this.enableDOF,focalLength:this.focalLength,aperture:this.aperture,focusDistance:this.focusDistance,sceneScale:this.sceneScale,apertureScale:this.apertureScale,anamorphicRatio:this.anamorphicRatio,bvhBuffer:w,triangleBuffer:T,materialBuffer:E,envTexture:re,environmentMatrix:this.environmentMatrix,environmentIntensity:this.environmentIntensity,enableEnvironmentLight:this.enableEnvironment,visMode:this.visMode,debugVisScale:this.debugVisScale,albedoMaps:M,normalMaps:N,bumpMaps:P,metalnessMaps:F,roughnessMaps:ee,emissiveMaps:te,frame:this.frame});this._kernelManager.register(`debug`,fe().compute([Math.ceil(r/16),Math.ceil(i/16),1],[16,16,1])),this._wavefrontReady=!0,console.log(`PathTracer: all wavefront kernels built (${r}×${i}, ${a} rays)`)}_setWfDispatch(){let e=this._wfRenderWidth.value,t=this._wfRenderHeight.value;this._kernelManager.setDispatchCount(`generate`,[Math.ceil(e/16),Math.ceil(t/16),1]),this._kernelManager.setDispatchCount(`finalWrite`,[Math.ceil(e/16),Math.ceil(t/16),1]),this._kernelManager.setDispatchCount(`debug`,[Math.ceil(e/16),Math.ceil(t/16),1])}dispose(){super.dispose(),this._packedBuffers?.dispose(),this._queueManager?.dispose(),this._kernelManager?.dispose(),this._gBufferAttr?.dispose?.(),this._packedBuffers=null,this._queueManager=null,this._kernelManager=null,this._gBufferAttr=null,this._wavefrontReady=!1}},ks=class extends ge{constructor(e,i={}){super(`NormalDepth`,{...i,executionMode:he.ALWAYS}),this.renderer=e,this.pathTracer=i.pathTracer,this._dirty=!0,this.cameraWorldMatrix=(0,n.uniform)(new r.Matrix4,`mat4`),this.cameraProjectionMatrixInverse=(0,n.uniform)(new r.Matrix4,`mat4`),this.resolutionWidth=(0,n.uniform)(i.width||1),this.resolutionHeight=(0,n.uniform)(i.height||1);let a=i.width||1,o=i.height||1;this._outputStorageTex=new t.StorageTexture(V,V),this._outputStorageTex.type=r.HalfFloatType,this._outputStorageTex.format=r.RGBAFormat,this._outputStorageTex.minFilter=r.NearestFilter,this._outputStorageTex.magFilter=r.NearestFilter,this._shadingStorageTex=new t.StorageTexture(V,V),this._shadingStorageTex.type=r.HalfFloatType,this._shadingStorageTex.format=r.RGBAFormat,this._shadingStorageTex.minFilter=r.NearestFilter,this._shadingStorageTex.magFilter=r.NearestFilter,this._shadingRT=new t.RenderTarget(a,o,{type:r.HalfFloatType,format:r.RGBAFormat,minFilter:r.NearestFilter,magFilter:r.NearestFilter,depthBuffer:!1,stencilBuffer:!1}),this._srcRegion=new r.Box2(new r.Vector2(0,0),new r.Vector2(0,0));let s={type:r.HalfFloatType,format:r.RGBAFormat,minFilter:r.NearestFilter,magFilter:r.NearestFilter,depthBuffer:!1,stencilBuffer:!1};this._rtA=new t.RenderTarget(a,o,s),this._rtB=new t.RenderTarget(a,o,s),this._currentIdx=0,this._hasHistory=!1,this._dispatchX=Math.ceil(a/8),this._dispatchY=Math.ceil(o/8),this._triStorageNode=null,this._bvhStorageNode=null,this._matStorageNode=null,this._lastTriAttr=null,this._lastBvhAttr=null,this._lastMatAttr=null,this._computeNode=null,this._computeBuilt=!1,this._normalMapsTex=(0,n.texture)(this._makePlaceholderArray()),this._bumpMapsTex=(0,n.texture)(this._makePlaceholderArray())}_makePlaceholderArray(){let e=new r.DataArrayTexture(new Uint8Array([128,128,255,255]),1,1,1);return e.minFilter=r.LinearFilter,e.magFilter=r.LinearFilter,e.generateMipmaps=!1,e.needsUpdate=!0,e}setupEventListeners(){this.on(`camera:moved`,()=>{this._dirty=!0}),this.on(`pipeline:reset`,()=>{this._dirty=!0,this._hasHistory=!1})}_syncStorageBuffers(){let e=this.pathTracer;if(!e)return!1;let t=e.materialData?.materialStorageAttr,r=e.triangleStorageAttr&&e.triangleStorageAttr!==this._lastTriAttr,i=e.bvhStorageAttr&&e.bvhStorageAttr!==this._lastBvhAttr,a=t&&t!==this._lastMatAttr;(r||i||a)&&(this._computeNode?.dispose?.(),this._computeNode=null,this._computeBuilt=!1,this._triStorageNode=null,this._bvhStorageNode=null,this._matStorageNode=null,this._dirty=!0),e.triangleStorageAttr&&!this._triStorageNode&&(this._triStorageNode=(0,n.storage)(e.triangleStorageAttr,`vec4`,e.triangleStorageAttr.count).toReadOnly()),e.bvhStorageAttr&&!this._bvhStorageNode&&(this._bvhStorageNode=(0,n.storage)(e.bvhStorageAttr,`vec4`,e.bvhStorageAttr.count).toReadOnly()),t&&!this._matStorageNode&&(this._matStorageNode=(0,n.storage)(t,`vec4`,t.count).toReadOnly());let o=e.materialData;return o?.normalMaps&&(this._normalMapsTex.value=o.normalMaps),o?.bumpMaps&&(this._bumpMapsTex.value=o.bumpMaps),this._lastTriAttr=e.triangleStorageAttr||this._lastTriAttr,this._lastBvhAttr=e.bvhStorageAttr||this._lastBvhAttr,this._lastMatAttr=t||this._lastMatAttr,!!(this._triStorageNode&&this._bvhStorageNode&&this._matStorageNode)}_buildCompute(){let e=this._triStorageNode,t=this._bvhStorageNode,r=this._matStorageNode,i=this._normalMapsTex,a=this._bumpMapsTex,o=this.cameraWorldMatrix,s=this.cameraProjectionMatrixInverse,c=this.resolutionWidth,l=this.resolutionHeight,u=this._outputStorageTex,d=this._shadingStorageTex;this._computeNode=(0,n.Fn)(([o,s])=>{let f=(0,n.int)(n.workgroupId.x).mul(8).add((0,n.int)(n.localId.x)),p=(0,n.int)(n.workgroupId.y).mul(8).add((0,n.int)(n.localId.y));(0,n.If)(f.lessThan((0,n.int)(c)).and(p.lessThan((0,n.int)(l))),()=>{let m=(0,n.vec3)((0,n.float)(f).add(.5).div(c).mul(2).sub(1),(0,n.float)(p).add(.5).div(l).mul(2).sub(1).negate(),1),h=s.mul((0,n.vec4)(m,1)),g=(0,n.normalize)((0,n.mat3)(o[0].xyz,o[1].xyz,o[2].xyz).mul(h.xyz.div(h.w))),_=wt({origin:(0,n.vec3)(o[3]),direction:g}),v=Ot.wrap(Ga(_,t,e)),y=v.normal.mul(.5).add(.5),b=v.dst,x=v.didHit.select((0,n.vec4)(y,b),(0,n.vec4)(0,0,0,(0,n.float)(1e6)));(0,n.textureStore)(u,(0,n.uvec2)((0,n.uint)(f),(0,n.uint)(p)),x).toWriteOnly();let S=v.normal.toVar();(0,n.If)(v.didHit,()=>{let e=Tt.wrap(un(v.materialIndex,r)).toVar(),t=It.wrap(ar(v.uv,e)).toVar(),n=cr(i,v.normal,e,t).toVar();S.assign(lr(a,n,e,t))});let C=v.didHit.select((0,n.vec4)(S.mul(.5).add(.5),b),(0,n.vec4)(0,0,0,(0,n.float)(1e6)));(0,n.textureStore)(d,(0,n.uvec2)((0,n.uint)(f),(0,n.uint)(p)),C).toWriteOnly()})})(o,s).compute([this._dispatchX,this._dispatchY,1],[8,8,1]),this._computeBuilt=!0}render(e){if(!this.enabled||!this._syncStorageBuffers())return;this._computeBuilt||this._buildCompute();let t=this.pathTracer;if(t&&(this.cameraWorldMatrix.value.copy(t.uniforms.get(`cameraWorldMatrix`).value),this.cameraProjectionMatrixInverse.value.copy(t.uniforms.get(`cameraProjectionMatrixInverse`).value)),!this._dirty&&this._hasHistory){let t=this._currentIdx===0?this._rtA:this._rtB;e.setTexture(`pathtracer:normalDepth`,t.texture),e.setTexture(`pathtracer:prevNormalDepth`,t.texture),e.setTexture(`pathtracer:shadingNormal`,this._shadingRT.texture);return}let n=e.getTexture(`pathtracer:color`);if(n&&n.image){let e=n.image;e.width>0&&e.height>0&&(e.width!==this._rtA.width||e.height!==this._rtA.height)&&this.setSize(e.width,e.height)}this._hasHistory&&(this._currentIdx=1-this._currentIdx);let r=this._currentIdx===0?this._rtA:this._rtB,i=this._currentIdx===0?this._rtB:this._rtA;this.renderer.compute(this._computeNode),this._srcRegion.max.set(r.width,r.height),this.renderer.copyTextureToTexture(this._outputStorageTex,r.texture,this._srcRegion),this.renderer.copyTextureToTexture(this._shadingStorageTex,this._shadingRT.texture,this._srcRegion),this._hasHistory||=(this.renderer.copyTextureToTexture(this._outputStorageTex,i.texture,this._srcRegion),!0),e.setTexture(`pathtracer:normalDepth`,r.texture),e.setTexture(`pathtracer:prevNormalDepth`,i.texture),e.setTexture(`pathtracer:shadingNormal`,this._shadingRT.texture),this._dirty=!1}releaseGPUMemory(){this._outputStorageTex?.dispose(),this._shadingStorageTex?.dispose(),this.reset()}reset(){this._dirty=!0,this._hasHistory=!1}setSize(e,t){this._rtA.setSize(e,t),this._rtA.texture.needsUpdate=!0,this._rtB.setSize(e,t),this._rtB.texture.needsUpdate=!0,this._shadingRT.setSize(e,t),this._shadingRT.texture.needsUpdate=!0,this._hasHistory=!1,this.resolutionWidth.value=e,this.resolutionHeight.value=t,this._dispatchX=Math.ceil(e/8),this._dispatchY=Math.ceil(t/8),this._computeNode&&(this._computeNode.dispatchSize=[this._dispatchX,this._dispatchY,1]),this._dirty=!0}dispose(){this._computeNode?.dispose(),this._outputStorageTex?.dispose(),this._shadingStorageTex?.dispose(),this._shadingRT?.dispose(),this._rtA?.dispose(),this._rtB?.dispose()}},As=class extends ge{constructor(e,i,a={}){super(`MotionVector`,{...a,executionMode:he.ALWAYS}),this.renderer=e,this.camera=i,this.pathTracer=a.pathTracer||null;let o=a.width||1,s=a.height||1;this.prevViewProjectionMatrix=new r.Matrix4,this.currentViewProjectionMatrix=new r.Matrix4,this.matricesInitialized=!1,this.isFirstFrame=!0,this.frameCount=0,this.cameraWorldMatrix=(0,n.uniform)(new r.Matrix4,`mat4`),this.cameraProjectionMatrixInverse=(0,n.uniform)(new r.Matrix4,`mat4`),this.prevVP=(0,n.uniform)(new r.Matrix4,`mat4`),this.isFirstFrameU=(0,n.uniform)(1),this.deltaTime=(0,n.uniform)(1/60),this.velocityScale=(0,n.uniform)(1),this.resolutionWidth=(0,n.uniform)(o),this.resolutionHeight=(0,n.uniform)(s),this._normalDepthTexNode=new t.TextureNode,this._screenSpaceStorageTex=new t.StorageTexture(V,V),this._screenSpaceStorageTex.type=r.HalfFloatType,this._screenSpaceStorageTex.format=r.RGBAFormat,this._screenSpaceStorageTex.minFilter=r.NearestFilter,this._screenSpaceStorageTex.magFilter=r.NearestFilter,this._worldSpaceStorageTex=new t.StorageTexture(V,V),this._worldSpaceStorageTex.type=r.HalfFloatType,this._worldSpaceStorageTex.format=r.RGBAFormat,this._worldSpaceStorageTex.minFilter=r.NearestFilter,this._worldSpaceStorageTex.magFilter=r.NearestFilter,this._srcRegion=new r.Box2(new r.Vector2(0,0),new r.Vector2(0,0));let c={type:r.HalfFloatType,format:r.RGBAFormat,minFilter:r.NearestFilter,magFilter:r.NearestFilter,depthBuffer:!1,stencilBuffer:!1};this.screenSpaceTarget=new t.RenderTarget(o,s,c),this.worldSpaceTarget=new t.RenderTarget(o,s,c),this._dispatchX=Math.ceil(o/16),this._dispatchY=Math.ceil(s/16),this._buildScreenSpaceCompute(),this._buildWorldSpaceCompute()}_buildScreenSpaceCompute(){let e=this._normalDepthTexNode,t=this.cameraWorldMatrix,r=this.cameraProjectionMatrixInverse,i=this.prevVP,a=this.resolutionWidth,o=this.resolutionHeight,s=this._screenSpaceStorageTex;this._screenSpaceComputeNode=(0,n.Fn)(([t,r])=>{let c=(0,n.int)(n.workgroupId.x).mul(16).add((0,n.int)(n.localId.x)),l=(0,n.int)(n.workgroupId.y).mul(16).add((0,n.int)(n.localId.y));(0,n.If)(c.lessThan((0,n.int)(a)).and(l.lessThan((0,n.int)(o))),()=>{let u=(0,n.textureLoad)(e,(0,n.ivec2)(c,l)).w,d=(0,n.vec2)((0,n.float)(c).add(.5).div(a),(0,n.float)(l).add(.5).div(o)),f=(0,n.vec4)(0,0,u,1).toVar();(0,n.If)(u.lessThan((0,n.float)(1e5)),()=>{let e=(0,n.vec3)((0,n.float)(c).add(.5).div(a).mul(2).sub(1),(0,n.float)(l).add(.5).div(o).mul(2).sub(1).negate(),1),s=r.mul((0,n.vec4)(e,1)),p=(0,n.normalize)((0,n.mat3)(t[0].xyz,t[1].xyz,t[2].xyz).mul(s.xyz.div(s.w))),m=(0,n.vec3)(t[3]).add(p.mul(u)),h=i.mul((0,n.vec4)(m,1)),g=h.xy.div(h.w),_=(0,n.vec2)(g.x.mul(.5).add(.5),g.y.mul(-.5).add(.5)),v=d.sub(_),y=_.x.greaterThanEqual(0).and(_.x.lessThanEqual(1)).and(_.y.greaterThanEqual(0)).and(_.y.lessThanEqual(1));f.assign(y.select((0,n.vec4)(v,u,1),(0,n.vec4)((0,n.float)(1e3),(0,n.float)(1e3),u,0)))}),(0,n.textureStore)(s,(0,n.uvec2)((0,n.uint)(c),(0,n.uint)(l)),f).toWriteOnly()})})(t,r).compute([this._dispatchX,this._dispatchY,1],[16,16,1])}_buildWorldSpaceCompute(){let e=this._normalDepthTexNode,t=this.cameraWorldMatrix,r=this.cameraProjectionMatrixInverse,i=this.prevVP,a=this.isFirstFrameU,o=this.deltaTime,s=this.velocityScale,c=this.resolutionWidth,l=this.resolutionHeight,u=this._worldSpaceStorageTex;this._worldSpaceComputeNode=(0,n.Fn)(([t,r])=>{let d=(0,n.int)(n.workgroupId.x).mul(16).add((0,n.int)(n.localId.x)),f=(0,n.int)(n.workgroupId.y).mul(16).add((0,n.int)(n.localId.y));(0,n.If)(d.lessThan((0,n.int)(c)).and(f.lessThan((0,n.int)(l))),()=>{let p=(0,n.textureLoad)(e,(0,n.ivec2)(d,f)).w,m=(0,n.vec4)(0,0,0,0).toVar();(0,n.If)(a.lessThan(.5).and(p.lessThan((0,n.float)(1e5))),()=>{let e=(0,n.vec3)((0,n.float)(d).add(.5).div(c).mul(2).sub(1),(0,n.float)(f).add(.5).div(l).mul(2).sub(1).negate(),1),a=r.mul((0,n.vec4)(e,1)),u=(0,n.normalize)((0,n.mat3)(t[0].xyz,t[1].xyz,t[2].xyz).mul(a.xyz.div(a.w))),h=(0,n.vec3)(t[3]).add(u.mul(p)),g=(0,n.vec2)((0,n.float)(d).add(.5).div(c),(0,n.float)(f).add(.5).div(l)),_=i.mul((0,n.vec4)(h,1)),v=_.xy.div(_.w),y=(0,n.vec2)(v.x.mul(.5).add(.5),v.y.mul(-.5).add(.5)),b=y.x.greaterThanEqual(0).and(y.x.lessThanEqual(1)).and(y.y.greaterThanEqual(0)).and(y.y.lessThanEqual(1)),x=g.sub(y),S=(0,n.vec3)(x.x.div(o).mul(s),x.y.div(o).mul(s),0);m.assign(b.select((0,n.vec4)(S,1),(0,n.vec4)(0,0,0,.5)))}),(0,n.textureStore)(u,(0,n.uvec2)((0,n.uint)(d),(0,n.uint)(f)),m).toWriteOnly()})})(t,r).compute([this._dispatchX,this._dispatchY,1],[16,16,1])}_updateCameraMatrices(){let e=this.pathTracer,t,n,r,i;if(e&&e.uniforms)t=e.uniforms.get(`cameraWorldMatrix`).value,n=e.uniforms.get(`cameraViewMatrix`).value,r=e.uniforms.get(`cameraProjectionMatrix`).value,i=e.uniforms.get(`cameraProjectionMatrixInverse`).value;else{let e=this.camera;if(!e)return;t=e.matrixWorld,n=e.matrixWorldInverse,r=e.projectionMatrix,i=e.projectionMatrixInverse}this.matricesInitialized?this.prevViewProjectionMatrix.copy(this.currentViewProjectionMatrix):(this.currentViewProjectionMatrix.multiplyMatrices(r,n),this.prevViewProjectionMatrix.copy(this.currentViewProjectionMatrix),this.matricesInitialized=!0),this.currentViewProjectionMatrix.multiplyMatrices(r,n),this.cameraWorldMatrix.value.copy(t),this.cameraProjectionMatrixInverse.value.copy(i),this.prevVP.value.copy(this.prevViewProjectionMatrix)}setupEventListeners(){this.on(`pipeline:reset`,()=>{this.reset()})}render(e){if(!this.enabled)return;let t=e.getTexture(`pathtracer:normalDepth`);if(!t)return;this._updateCameraMatrices(),this.isFirstFrameU.value=+!!this.isFirstFrame,this.frameCount++;let n=t.image;n&&n.width>0&&n.height>0&&(n.width!==this.screenSpaceTarget.width||n.height!==this.screenSpaceTarget.height)&&this.setSize(n.width,n.height),this._normalDepthTexNode.value=t,this.renderer.compute(this._screenSpaceComputeNode),this.renderer.compute(this._worldSpaceComputeNode),this._srcRegion.max.set(this.screenSpaceTarget.width,this.screenSpaceTarget.height),this.renderer.copyTextureToTexture(this._screenSpaceStorageTex,this.screenSpaceTarget.texture,this._srcRegion),this.renderer.copyTextureToTexture(this._worldSpaceStorageTex,this.worldSpaceTarget.texture,this._srcRegion),e.setTexture(`motionVector:screenSpace`,this.screenSpaceTarget.texture),e.setTexture(`motionVector:worldSpace`,this.worldSpaceTarget.texture),e.setTexture(`motionVector:motion`,this.screenSpaceTarget.texture),this.emit(`motionvector:computed`,{frame:this.frameCount,isFirstFrame:this.isFirstFrame}),this.isFirstFrame=!1}releaseGPUMemory(){this._screenSpaceStorageTex?.dispose(),this._worldSpaceStorageTex?.dispose(),this.reset()}reset(){this.matricesInitialized||(this.isFirstFrame=!0),this.frameCount=0}setSize(e,t){this.screenSpaceTarget.setSize(e,t),this.screenSpaceTarget.texture.needsUpdate=!0,this.worldSpaceTarget.setSize(e,t),this.worldSpaceTarget.texture.needsUpdate=!0,this.resolutionWidth.value=e,this.resolutionHeight.value=t,this._dispatchX=Math.ceil(e/16),this._dispatchY=Math.ceil(t/16),this._screenSpaceComputeNode&&(this._screenSpaceComputeNode.dispatchSize=[this._dispatchX,this._dispatchY,1]),this._worldSpaceComputeNode&&(this._worldSpaceComputeNode.dispatchSize=[this._dispatchX,this._dispatchY,1])}setVelocityScale(e){this.velocityScale.value=e}setDeltaTime(e){this.deltaTime.value=e}dispose(){this._screenSpaceComputeNode?.dispose(),this._worldSpaceComputeNode?.dispose(),this._screenSpaceStorageTex?.dispose(),this._worldSpaceStorageTex?.dispose(),this.screenSpaceTarget?.dispose(),this.worldSpaceTarget?.dispose(),this._normalDepthTexNode?.dispose()}},js=e=>(0,n.select)(e.equal(e),e,(0,n.float)(0)).clamp(0,1e7),Ms=e=>(0,n.vec3)(js(e.x),js(e.y),js(e.z)),Ns=class extends ge{constructor(e,i={}){super(`ASVGF`,{...i,executionMode:he.PER_CYCLE}),this.renderer=e,this.temporalAlpha=(0,n.uniform)(i.temporalAlpha??0),this.gradientStrength=(0,n.uniform)(i.gradientStrength??0),this.gradientSigmaScale=(0,n.uniform)(i.gradientSigmaScale??2),this.gradientNoiseFloor=(0,n.uniform)(i.gradientNoiseFloor??0),this.maxAccumFrames=(0,n.uniform)(i.maxAccumFrames??32),this.resW=(0,n.uniform)(i.width||1),this.resH=(0,n.uniform)(i.height||1),this.temporalEnabledU=(0,n.uniform)(1),this.forceResetU=(0,n.uniform)(0),this._colorTexNode=new t.TextureNode,this._albedoTexNode=new t.TextureNode,this._motionTexNode=new t.TextureNode,this._normalDepthTexNode=new t.TextureNode,this._prevNormalDepthTexNode=new t.TextureNode,this._readTemporalTexNode=new t.TextureNode,this._gradientReadTexNode=new t.TextureNode;let a=i.width||1,o=i.height||1;this._temporalTexA=new t.StorageTexture(V,V),this._temporalTexA.type=r.FloatType,this._temporalTexA.format=r.RGBAFormat,this._temporalTexA.minFilter=r.LinearFilter,this._temporalTexA.magFilter=r.LinearFilter,this._temporalTexB=new t.StorageTexture(V,V),this._temporalTexB.type=r.FloatType,this._temporalTexB.format=r.RGBAFormat,this._temporalTexB.minFilter=r.LinearFilter,this._temporalTexB.magFilter=r.LinearFilter,this._outputModulatedTex=new t.StorageTexture(V,V),this._outputModulatedTex.type=r.FloatType,this._outputModulatedTex.format=r.RGBAFormat,this._outputModulatedTex.minFilter=r.LinearFilter,this._outputModulatedTex.magFilter=r.LinearFilter,this._gradientStorageTex=new t.StorageTexture(V,V),this._gradientStorageTex.type=r.HalfFloatType,this._gradientStorageTex.format=r.RGBAFormat,this._gradientStorageTex.minFilter=r.LinearFilter,this._gradientStorageTex.magFilter=r.LinearFilter,this._srcRegion=new r.Box2(new r.Vector2(0,0),new r.Vector2(0,0)),this._demodulatedRT=new t.RenderTarget(a,o,{type:r.FloatType,format:r.RGBAFormat,minFilter:r.LinearFilter,magFilter:r.LinearFilter,depthBuffer:!1,stencilBuffer:!1}),this._outputRT=new t.RenderTarget(a,o,{type:r.FloatType,format:r.RGBAFormat,minFilter:r.LinearFilter,magFilter:r.LinearFilter,depthBuffer:!1,stencilBuffer:!1}),this._gradientRT=new t.RenderTarget(a,o,{type:r.HalfFloatType,format:r.RGBAFormat,minFilter:r.LinearFilter,magFilter:r.LinearFilter,depthBuffer:!1,stencilBuffer:!1}),this.currentMoments=0,this._compiled=!1,this._needsWarmReset=!1,this._dispatchX=Math.ceil(a/8),this._dispatchY=Math.ceil(o/8),this._buildGradientCompute(),this._buildTemporalCompute(),this.showHeatmap=!1,this.debugMode=(0,n.uniform)(0,`int`),this._heatmapStorageTex=new t.StorageTexture(V,V),this._heatmapStorageTex.type=r.FloatType,this._heatmapStorageTex.format=r.RGBAFormat,this._heatmapStorageTex.minFilter=r.NearestFilter,this._heatmapStorageTex.magFilter=r.NearestFilter,this.heatmapTarget=new t.RenderTarget(a,o,{type:r.FloatType,format:r.RGBAFormat,minFilter:r.NearestFilter,magFilter:r.NearestFilter,depthBuffer:!1,stencilBuffer:!1}),this._heatmapRawColorTexNode=new t.TextureNode,this._heatmapColorTexNode=new t.TextureNode,this._heatmapTemporalTexNode=new t.TextureNode,this._heatmapNDTexNode=new t.TextureNode,this._heatmapMotionTexNode=new t.TextureNode,this._heatmapGradientTexNode=new t.TextureNode,this._buildHeatmapCompute()}_buildGradientCompute(){let e=this._colorTexNode,t=this._albedoTexNode,r=this._motionTexNode,i=this._readTemporalTexNode,a=this.gradientNoiseFloor,o=this.gradientSigmaScale,s=this._gradientStorageTex,c=this.resW,l=this.resH,u=(0,n.workgroupArray)(`float`,144),d=(0,n.workgroupArray)(`float`,144);this._gradientNode=(0,n.Fn)(()=>{let f=n.localId.x,p=n.localId.y,m=p.mul(8).add(f),h=(0,n.int)(n.workgroupId.x).mul(8).sub(2).toVar(),g=(0,n.int)(n.workgroupId.y).mul(8).sub(2).toVar(),_=a=>{let o=a.mod((0,n.uint)(12)),s=a.div((0,n.uint)(12)),f=h.add((0,n.int)(o)).clamp((0,n.int)(0),(0,n.int)(c).sub(1)),p=g.add((0,n.int)(s)).clamp((0,n.int)(0),(0,n.int)(l).sub(1)),m=(0,n.textureLoad)(e,(0,n.ivec2)(f,p)).xyz,_=(0,n.textureLoad)(t,(0,n.ivec2)(f,p)).xyz,v=Qt(Ms(m.div((0,n.max)(_,(0,n.vec3)(ke))))).toVar();u.element(a).assign(v);let y=(0,n.textureLoad)(r,(0,n.ivec2)(f,p)),b=(0,n.float)(f).sub(y.x.mul(c)),x=(0,n.float)(p).sub(y.y.mul(l)),S=(0,n.int)(b.add(.5)).clamp((0,n.int)(0),(0,n.int)(c).sub(1)),C=(0,n.int)(x.add(.5)).clamp((0,n.int)(0),(0,n.int)(l).sub(1)),w=y.w.greaterThan(.5),T=(0,n.textureLoad)(i,(0,n.ivec2)(S,C)).xyz,E=w.select(Qt(T),v);d.element(a).assign(E)};_(m);let v=m.add((0,n.uint)(64));(0,n.If)(v.lessThan((0,n.uint)(144)),()=>{_(v)});let y=m.add((0,n.uint)(128));(0,n.If)(y.lessThan((0,n.uint)(144)),()=>{_(y)}),(0,n.workgroupBarrier)();let b=(0,n.int)(n.workgroupId.x).mul(8).add((0,n.int)(f)),x=(0,n.int)(n.workgroupId.y).mul(8).add((0,n.int)(p));(0,n.If)(b.lessThan((0,n.int)(c)).and(x.lessThan((0,n.int)(l))),()=>{let e=(0,n.float)(0).toVar(),t=(0,n.float)(0).toVar();for(let r=-2;r<=2;r++)for(let i=-2;i<=2;i++){let a=p.add((0,n.uint)(2+r)).mul((0,n.uint)(12)).add(f.add((0,n.uint)(2+i))),o=u.element(a);e.addAssign(o),t.addAssign(o.mul(o))}let m=e.div(25),h=(0,n.sqrt)((0,n.max)(t.div(25).sub(m.mul(m)),(0,n.float)(0))).mul(o).toVar(),g=(0,n.float)(0).toVar();for(let e=-2;e<=2;e++)for(let t=-2;t<=2;t++){let r=p.add((0,n.uint)(2+e)).mul((0,n.uint)(12)).add(f.add((0,n.uint)(2+t))),i=u.element(r),o=d.element(r),s=(0,n.max)((0,n.max)((0,n.abs)(i.sub(o)).sub(h),(0,n.float)(0)).div((0,n.max)((0,n.max)(i,o),(0,n.float)(.001))).sub(a),(0,n.float)(0)).div((0,n.max)((0,n.float)(1).sub(a),(0,n.float)(1e-4)));g.addAssign(s.mul(s))}let _=g.div(25).clamp(0,1).toVar(),v=(0,n.textureLoad)(r,(0,n.ivec2)(b,x)),y=(0,n.int)((0,n.float)(b).sub(v.x.mul(c)).add(.5)).clamp((0,n.int)(0),(0,n.int)(c).sub(1)),S=(0,n.int)((0,n.float)(x).sub(v.y.mul(l)).add(.5)).clamp((0,n.int)(0),(0,n.int)(l).sub(1)),C=v.w.greaterThan(.5).select((0,n.textureLoad)(i,(0,n.ivec2)(y,S)).w,(0,n.float)(0)).div(4).clamp(0,1),w=_.mul(C);(0,n.textureStore)(s,(0,n.uvec2)((0,n.uint)(b),(0,n.uint)(x)),(0,n.vec4)(w,_,h,1)).toWriteOnly()})})().compute([this._dispatchX,this._dispatchY,1],[8,8,1])}_buildTemporalCompute(){this._temporalNodeA=this._buildTemporalForDirection(this._temporalTexA),this._temporalNodeB=this._buildTemporalForDirection(this._temporalTexB)}_buildTemporalForDirection(e){let t=this._colorTexNode,r=this._albedoTexNode,i=this._motionTexNode,a=this._normalDepthTexNode,o=this._prevNormalDepthTexNode,s=this._readTemporalTexNode,c=this._gradientReadTexNode,l=this._outputModulatedTex,u=this.maxAccumFrames,d=this.temporalAlpha,f=this.gradientStrength,p=this.temporalEnabledU,m=this.forceResetU,h=this.resW,g=this.resH;return(0,n.Fn)(()=>{let _=(0,n.int)(n.workgroupId.x).mul(8).add((0,n.int)(n.localId.x)),v=(0,n.int)(n.workgroupId.y).mul(8).add((0,n.int)(n.localId.y));(0,n.If)(_.lessThan((0,n.int)(h)).and(v.lessThan((0,n.int)(g))),()=>{let y=(0,n.ivec2)(_,v),b=(0,n.textureLoad)(t,y).xyz,x=(0,n.textureLoad)(r,y).xyz,S=(0,n.max)(x,(0,n.vec3)(ke)),C=Ms(b.div(S)),w=(0,n.vec4)(C,1).toVar(),T=(0,n.vec4)(b,1).toVar();(0,n.If)(p.greaterThan(.5).and(m.lessThan(.5)),()=>{let e=(0,n.textureLoad)(i,y),t=e.w.greaterThan(.5),r=(0,n.float)(_).sub(e.x.mul(h)),l=(0,n.float)(v).sub(e.y.mul(g)),p=r.greaterThanEqual(0).and(r.lessThan((0,n.float)(h))).and(l.greaterThanEqual(0)).and(l.lessThan((0,n.float)(g)));(0,n.If)(t.and(p),()=>{let e=(0,n.textureLoad)(a,y),t=e.xyz.mul(2).sub(1),i=e.w,p=(0,n.int)(r),m=(0,n.int)(l),_=p.add((0,n.int)(1)),v=m.add((0,n.int)(1)),b=r.sub((0,n.float)(p)),x=l.sub((0,n.float)(m)),E=p.clamp((0,n.int)(0),(0,n.int)(h).sub(1)),D=_.clamp((0,n.int)(0),(0,n.int)(h).sub(1)),O=m.clamp((0,n.int)(0),(0,n.int)(g).sub(1)),k=v.clamp((0,n.int)(0),(0,n.int)(g).sub(1)),A=(0,n.float)(1).sub(b).mul((0,n.float)(1).sub(x)),j=b.mul((0,n.float)(1).sub(x)),M=(0,n.float)(1).sub(b).mul(x),N=b.mul(x),P=(e,r,a)=>{let s=(0,n.textureLoad)(o,(0,n.ivec2)(e,r)),c=s.xyz.mul(2).sub(1),l=s.w,u=(0,n.pow)((0,n.dot)(t,c).clamp(0,1),(0,n.float)(16)),d=(0,n.exp)((0,n.abs)(i.sub(l)).div((0,n.max)(i,(0,n.float)(.001))).div((0,n.float)(.05)).negate());return a.mul(u).mul(d)},F=P(E,O,A),ee=P(D,O,j),te=P(E,k,M),ne=P(D,k,N),re=F.add(ee).add(te).add(ne);(0,n.If)(re.greaterThan((0,n.float)(.01)),()=>{let e=(0,n.textureLoad)(s,(0,n.ivec2)(E,O)),t=(0,n.textureLoad)(s,(0,n.ivec2)(D,O)),r=(0,n.textureLoad)(s,(0,n.ivec2)(E,k)),i=(0,n.textureLoad)(s,(0,n.ivec2)(D,k)),a=(0,n.float)(1).div(re),o=e.xyz.mul(F).add(t.xyz.mul(ee)).add(r.xyz.mul(te)).add(i.xyz.mul(ne)).mul(a),l=e.w.mul(F).add(t.w.mul(ee)).add(r.w.mul(te)).add(i.w.mul(ne)).mul(a),p=(0,n.textureLoad)(c,y).x.mul(f).clamp(0,1),m=Ms((0,n.mix)(o,C,(0,n.mix)((0,n.max)((0,n.float)(1).div(l.add(1)),d),(0,n.float)(1),p))),h=(0,n.min)(l.add(1),u);w.assign((0,n.vec4)(m,h)),T.assign((0,n.vec4)(m.mul(S),1))})})}),(0,n.textureStore)(e,(0,n.uvec2)((0,n.uint)(_),(0,n.uint)(v)),w).toWriteOnly(),(0,n.textureStore)(l,(0,n.uvec2)((0,n.uint)(_),(0,n.uint)(v)),T).toWriteOnly()})})().compute([this._dispatchX,this._dispatchY,1],[8,8,1])}_buildHeatmapCompute(){let e=this._heatmapRawColorTexNode,t=this._heatmapColorTexNode,r=this._heatmapTemporalTexNode,i=this._heatmapNDTexNode,a=this._heatmapMotionTexNode,o=this._heatmapGradientTexNode,s=this._heatmapStorageTex,c=this.debugMode,l=this.resW,u=this.resH;this._heatmapComputeNode=(0,n.Fn)(()=>{let d=(0,n.int)(n.workgroupId.x).mul(8).add((0,n.int)(n.localId.x)),f=(0,n.int)(n.workgroupId.y).mul(8).add((0,n.int)(n.localId.y));(0,n.If)(d.lessThan((0,n.int)(l)).and(f.lessThan((0,n.int)(u))),()=>{let p=(0,n.ivec2)(d,f),m=(0,n.vec4)(0,0,0,1).toVar();(0,n.If)(c.equal((0,n.int)(0)),()=>{let e=(0,n.textureLoad)(t,p).xyz;m.assign((0,n.vec4)(e,1))}).ElseIf(c.equal((0,n.int)(1)),()=>{let t=(0,n.float)(0).toVar(),r=(0,n.float)(0).toVar();for(let i=-1;i<=1;i++)for(let a=-1;a<=1;a++){let o=(0,n.textureLoad)(e,(0,n.ivec2)(d.add(a).clamp((0,n.int)(0),(0,n.int)(l).sub(1)),f.add(i).clamp((0,n.int)(0),(0,n.int)(u).sub(1)))).xyz,s=Qt(o);t.addAssign(s),r.addAssign(s.mul(s))}t.divAssign(9),r.divAssign(9);let i=(0,n.max)(r.sub(t.mul(t)),(0,n.float)(0)).div((0,n.max)(t.mul(t),(0,n.float)(1e-4))).mul(10).clamp(0,1),a=i.sub(.5).mul(4).clamp(0,1),o=i.mul(4).clamp(0,1).sub(i.sub(.75).mul(4).clamp(0,1)),s=(0,n.float)(1).sub(i.sub(.25).mul(4).clamp(0,1));m.assign((0,n.vec4)(a,o,s,1))}).ElseIf(c.equal((0,n.int)(2)),()=>{let e=(0,n.textureLoad)(r,p).w.div(32).clamp(0,1);m.assign((0,n.vec4)((0,n.float)(1).sub(e),e,(0,n.float)(.2),1))}).ElseIf(c.equal((0,n.int)(3)),()=>{let e=(0,n.textureLoad)(a,p),t=(0,n.abs)(e.x).mul(100).clamp(0,1),r=(0,n.abs)(e.y).mul(100).clamp(0,1),i=t.add(r).clamp(0,1);m.assign((0,n.vec4)(t,r,i.mul(.3),1))}).ElseIf(c.equal((0,n.int)(4)),()=>{let e=(0,n.textureLoad)(i,p);m.assign((0,n.vec4)(e.xyz,1))}).Else(()=>{let e=(0,n.textureLoad)(o,p).x.mul(5).clamp(0,1);m.assign((0,n.vec4)(e,e.mul(.5),(0,n.float)(1).sub(e),1))}),(0,n.textureStore)(s,(0,n.uvec2)((0,n.uint)(d),(0,n.uint)(f)),m).toWriteOnly()})})().compute([this._dispatchX,this._dispatchY,1],[8,8,1])}setupEventListeners(){this.on(`asvgf:reset`,()=>this.resetTemporalData()),this.on(`asvgf:setTemporal`,e=>{e&&e.enabled!==void 0&&this.setTemporalEnabled(e.enabled)}),this.on(`asvgf:updateParameters`,e=>this.updateParameters(e))}render(e){if(!this.enabled)return;let t=e.getTexture(`pathtracer:color`),n=e.getTexture(`pathtracer:albedo`),r=e.getTexture(`pathtracer:normalDepth`),i=e.getTexture(`pathtracer:prevNormalDepth`)||r,a=e.getTexture(`motionVector:screenSpace`);if(!t)return;let o=t.image;o&&o.width>0&&o.height>0&&(o.width!==this._outputRT.width||o.height!==this._outputRT.height)&&this.setSize(o.width,o.height),this._colorTexNode.value=t,n&&(this._albedoTexNode.value=n),a&&(this._motionTexNode.value=a),r&&(this._normalDepthTexNode.value=r),i&&(this._prevNormalDepthTexNode.value=i);let s=this.currentMoments===0?this._temporalTexB:this._temporalTexA,c=this.currentMoments===0?this._temporalNodeA:this._temporalNodeB,l=this.currentMoments===0?this._temporalTexA:this._temporalTexB;this._compiled||=(this.renderer.compute(this._gradientNode),this.renderer.compute(this._temporalNodeA),this.renderer.compute(this._temporalNodeB),!0),this._readTemporalTexNode.value=s,this._gradientReadTexNode.value=this._gradientStorageTex;let u=this.gradientStrength.value>0||this.showHeatmap;u&&this.renderer.compute(this._gradientNode),this.forceResetU.value=+!!this._needsWarmReset,this.renderer.compute(c),this._needsWarmReset=!1,this._srcRegion.max.set(this.resW.value,this.resH.value),this.renderer.copyTextureToTexture(l,this._demodulatedRT.texture,this._srcRegion),this.renderer.copyTextureToTexture(this._outputModulatedTex,this._outputRT.texture,this._srcRegion),u&&this.renderer.copyTextureToTexture(this._gradientStorageTex,this._gradientRT.texture,this._srcRegion),e.setTexture(`asvgf:demodulated`,this._demodulatedRT.texture),e.setTexture(`asvgf:output`,this._outputRT.texture),e.setTexture(`asvgf:gradient`,this._gradientRT.texture),this.currentMoments=1-this.currentMoments,this.showHeatmap&&(this._heatmapRawColorTexNode.value=t,this._heatmapColorTexNode.value=this._outputModulatedTex,this._heatmapTemporalTexNode.value=l,r&&(this._heatmapNDTexNode.value=r),a&&(this._heatmapMotionTexNode.value=a),this._heatmapGradientTexNode.value=this._gradientStorageTex,this.renderer.compute(this._heatmapComputeNode),this._srcRegion.max.set(this.heatmapTarget.width,this.heatmapTarget.height),this.renderer.copyTextureToTexture(this._heatmapStorageTex,this.heatmapTarget.texture,this._srcRegion))}setHeatmapEnabled(e){this.showHeatmap=e}setTemporalEnabled(e){this.temporalEnabledU.value=+!!e}updateParameters(e){e&&(e.temporalAlpha!==void 0&&(this.temporalAlpha.value=e.temporalAlpha),e.gradientStrength!==void 0&&(this.gradientStrength.value=e.gradientStrength),e.gradientSigmaScale!==void 0&&(this.gradientSigmaScale.value=e.gradientSigmaScale),e.gradientNoiseFloor!==void 0&&(this.gradientNoiseFloor.value=e.gradientNoiseFloor),e.maxAccumFrames!==void 0&&(this.maxAccumFrames.value=e.maxAccumFrames),e.debugMode!==void 0&&(this.debugMode.value=e.debugMode))}resetTemporalData(){this.currentMoments=0,this._needsWarmReset=!0}setSize(e,t){this._demodulatedRT.setSize(e,t),this._demodulatedRT.texture.needsUpdate=!0,this._outputRT.setSize(e,t),this._outputRT.texture.needsUpdate=!0,this._gradientRT.setSize(e,t),this._gradientRT.texture.needsUpdate=!0,this.heatmapTarget.setSize(e,t),this.heatmapTarget.texture.needsUpdate=!0,this.resW.value=e,this.resH.value=t,this._dispatchX=Math.ceil(e/8),this._dispatchY=Math.ceil(t/8),this._gradientNode.dispatchSize=[this._dispatchX,this._dispatchY,1],this._temporalNodeA.dispatchSize=[this._dispatchX,this._dispatchY,1],this._temporalNodeB.dispatchSize=[this._dispatchX,this._dispatchY,1],this._heatmapComputeNode.dispatchSize=[this._dispatchX,this._dispatchY,1]}reset(){}releaseGPUMemory(){this._temporalTexA?.dispose(),this._temporalTexB?.dispose(),this._outputModulatedTex?.dispose(),this._gradientStorageTex?.dispose(),this._heatmapStorageTex?.dispose(),this.context?.removeTexture(`asvgf:demodulated`),this.context?.removeTexture(`asvgf:output`),this.context?.removeTexture(`asvgf:gradient`),this._demodulatedRT?.texture?.dispose(),this._outputRT?.texture?.dispose(),this._gradientRT?.texture?.dispose(),this.heatmapTarget?.texture?.dispose(),this.resetTemporalData()}dispose(){this._gradientNode?.dispose(),this._temporalNodeA?.dispose(),this._temporalNodeB?.dispose(),this._temporalTexA?.dispose(),this._temporalTexB?.dispose(),this._outputModulatedTex?.dispose(),this._gradientStorageTex?.dispose(),this._demodulatedRT?.dispose(),this._outputRT?.dispose(),this._gradientRT?.dispose(),this._heatmapComputeNode?.dispose(),this._heatmapStorageTex?.dispose(),this.heatmapTarget?.dispose(),this._colorTexNode?.dispose(),this._albedoTexNode?.dispose(),this._motionTexNode?.dispose(),this._normalDepthTexNode?.dispose(),this._prevNormalDepthTexNode?.dispose(),this._readTemporalTexNode?.dispose(),this._gradientReadTexNode?.dispose(),this._heatmapRawColorTexNode?.dispose(),this._heatmapColorTexNode?.dispose(),this._heatmapTemporalTexNode?.dispose(),this._heatmapNDTexNode?.dispose(),this._heatmapMotionTexNode?.dispose(),this._heatmapGradientTexNode?.dispose()}},Ps=e=>(0,n.select)(e.equal(e),e,(0,n.float)(0)).clamp(0,1e7),Fs=(0,n.wgslFn)(`
|
|
669
|
+
`),Ss=(0,n.Fn)(([e,t,r,i,a,o,s,c,l,u,d,f,p,m,h,g,_,v,y,b,x,S])=>{let C=(0,n.vec4)(1,0,1,1).toVar(),w=wt({origin:e,direction:t}),T=Ot.wrap(Ga(w,r,i).toVar());return(0,n.If)(u.equal((0,n.int)(7)),()=>{let e=(0,n.float)(T.triTests).div(d);C.assign((0,n.select)(e.lessThan(1),(0,n.vec4)((0,n.vec3)(e),1),(0,n.vec4)(1,0,0,1)))}),(0,n.If)(u.equal((0,n.int)(8)),()=>{let e=(0,n.float)(T.boxTests).div(d);C.assign((0,n.select)(e.lessThan(1),(0,n.vec4)((0,n.vec3)(e),1),(0,n.vec4)(1,0,0,1)))}),(0,n.If)(u.equal((0,n.int)(10)),()=>{(0,n.If)(l,()=>{let e=eo({tex:o,samp:(0,n.sampler)(o),direction:t,environmentMatrix:s,environmentIntensity:c,enableEnvironmentLight:l}).toVar(),r=(0,n.dot)(e.xyz,qt).toVar(),i=r,a=(0,n.max)(d.mul(.1),.001),u=r.div(a),m=(0,n.log)(r.add(1e-6)).add(10).div(10),h=(0,n.select)(d.greaterThan(1),u,m).toVar(),g=(0,n.vec3)(0).toVar();(0,n.If)(h.lessThan(.2),()=>{g.assign((0,n.mix)((0,n.vec3)(0,0,0),(0,n.vec3)(0,0,.5),h.mul(5)))}).ElseIf(h.lessThan(.4),()=>{g.assign((0,n.mix)((0,n.vec3)(0,0,.5),(0,n.vec3)(0,0,1),h.sub(.2).mul(5)))}).ElseIf(h.lessThan(.6),()=>{g.assign((0,n.mix)((0,n.vec3)(0,0,1),(0,n.vec3)(0,1,0),h.sub(.4).mul(5)))}).ElseIf(h.lessThan(.8),()=>{g.assign((0,n.mix)((0,n.vec3)(0,1,0),(0,n.vec3)(1,1,0),h.sub(.6).mul(5)))}).ElseIf(h.lessThan(1),()=>{g.assign((0,n.mix)((0,n.vec3)(1,1,0),(0,n.vec3)(1,0,0),h.sub(.8).mul(5)))}).Else(()=>{g.assign((0,n.mix)((0,n.vec3)(1,0,0),(0,n.vec3)(1,1,1),(0,n.min)(h.sub(1),1)))});let _=f.div(p);(0,n.If)(_.x.lessThan(.1).and(_.y.lessThan(.1)),()=>{let e=i.mul(1);g.assign((0,n.vec3)(e))}).ElseIf(_.x.greaterThan(.9).and(_.y.lessThan(.1)),()=>{g.assign(e.xyz.mul(1))}),C.assign((0,n.vec4)(g,1))}).Else(()=>{C.assign((0,n.vec4)(1,0,1,1))})}),(0,n.If)(u.equal((0,n.int)(4)),()=>{(0,n.If)(T.didHit.not(),()=>{C.assign((0,n.vec4)(0,0,0,1))}).Else(()=>{let t=Tt.wrap(un(T.materialIndex,a)).toVar(),r=Pt.wrap(dr(m,h,g,_,v,y,t,T.uv,T.normal)).toVar().emissive.toVar(),i=(0,n.length)(r).toVar();(0,n.If)(i.greaterThan(0),()=>{let t=r.div((0,n.max)(i.mul(.1),.001)).toVar(),a=(0,n.length)(e.sub(T.hitPoint)),o=(0,n.clamp)((0,n.float)(1).sub(a.div(10)),0,1),s=(0,n.mix)(t,t.mul((0,n.vec3)(1,.8,.6)),o.mul(.3));C.assign((0,n.vec4)(s,1))}).Else(()=>{C.assign((0,n.vec4)(0,0,.1,1))})})}),(0,n.If)(u.equal((0,n.int)(1)),()=>{(0,n.If)(T.didHit.not(),()=>{C.assign((0,n.vec4)(.5,.5,1,1))}).Else(()=>{let e=Tt.wrap(un(T.materialIndex,a)).toVar(),t=(0,n.normalize)(Pt.wrap(dr(m,h,g,_,v,y,e,T.uv,T.normal)).toVar().normal);C.assign((0,n.vec4)(bs({normal:t}),1))})}),(0,n.If)(u.equal((0,n.int)(2)),()=>{(0,n.If)(T.didHit.not(),()=>{C.assign((0,n.vec4)((0,n.vec3)(1),1))}).Else(()=>{let e=xs({worldPos:T.hitPoint,cameraProjectionMatrix:b,cameraViewMatrix:x});C.assign((0,n.vec4)(ys({depth:e}),1))})}),(0,n.If)(u.equal((0,n.int)(3)),()=>{(0,n.If)(T.didHit.not(),()=>{C.assign((0,n.vec4)(0,0,0,1))}).Else(()=>{let e=Tt.wrap(un(T.materialIndex,a)).toVar(),t=Pt.wrap(dr(m,h,g,_,v,y,e,T.uv,T.normal)).toVar().albedo.rgb;C.assign((0,n.vec4)(t,1))})}),(0,n.If)(u.equal((0,n.int)(5)),()=>{(0,n.If)(T.didHit.not(),()=>{C.assign((0,n.vec4)(0,0,0,1))}).Else(()=>{let e=Tt.wrap(un(T.materialIndex,a)).toVar(),t=Pt.wrap(dr(m,h,g,_,v,y,e,T.uv,T.normal)).toVar(),u=t.albedo.rgb,d=(0,n.normalize)(t.normal).toVar(),p=ne({state:re({seed:(0,n.uint)(f.x).mul((0,n.uint)(1973)).add((0,n.uint)(f.y).mul((0,n.uint)(9277))).add(S.mul((0,n.uint)(26699)))})}).toVar(),b=Pn({N:d,xi:(0,n.vec2)(I(p).toVar(),I(p).toVar())}).toVar(),x=wt({origin:T.hitPoint.add(d.mul(.001)),direction:b}),w=Ot.wrap(Ga(x,r,i).toVar()),E=(0,n.vec3)(0).toVar();(0,n.If)(w.didHit.not(),()=>{(0,n.If)(l,()=>{E.assign(eo({tex:o,samp:(0,n.sampler)(o),direction:b,environmentMatrix:s,environmentIntensity:c,enableEnvironmentLight:l}).xyz)})}).Else(()=>{let e=Tt.wrap(un(w.materialIndex,a)).toVar(),t=Pt.wrap(dr(m,h,g,_,v,y,e,w.uv,w.normal)).toVar();E.assign(t.emissive),(0,n.If)(l,()=>{let e=(0,n.normalize)(t.normal).toVar(),r=eo({tex:o,samp:(0,n.sampler)(o),direction:e,environmentMatrix:s,environmentIntensity:c,enableEnvironmentLight:l}).xyz;E.addAssign(t.albedo.rgb.mul(r))})});let D=u.mul(E);C.assign((0,n.vec4)(D,1))})}),(0,n.If)(u.equal((0,n.int)(6)),()=>{(0,n.If)(T.didHit,()=>{let e=T.normal.toVar(),r=(0,n.normalize)(t.sub(e.mul((0,n.dot)(t,e).mul(2)))).toVar(),i=eo({tex:o,samp:(0,n.sampler)(o),direction:r,environmentMatrix:s,environmentIntensity:c,enableEnvironmentLight:l}).xyz,a=i.div(i.add(1));C.assign((0,n.vec4)(a,1))}).Else(()=>{let e=eo({tex:o,samp:(0,n.sampler)(o),direction:t,environmentMatrix:s,environmentIntensity:c,enableEnvironmentLight:l}).xyz,r=e.div(e.add(1));C.assign((0,n.vec4)(r,1))})}),C});function Cs(e){let{writeColorTex:t,writeNDTex:r,writeAlbedoTex:i,resolution:a,renderWidth:o,renderHeight:s,cameraWorldMatrix:c,cameraProjectionMatrixInverse:l,cameraProjectionMatrix:u,cameraViewMatrix:d,enableDOF:f,focalLength:p,aperture:m,focusDistance:h,sceneScale:g,apertureScale:_,anamorphicRatio:v,bvhBuffer:y,triangleBuffer:b,materialBuffer:x,envTexture:S,environmentMatrix:C,environmentIntensity:w,enableEnvironmentLight:T,visMode:E,debugVisScale:D,albedoMaps:O,normalMaps:k,bumpMaps:A,metalnessMaps:j,roughnessMaps:M,emissiveMaps:N,frame:P}=e;return(0,n.Fn)(()=>{let e=(0,n.int)(n.workgroupId.x).mul(16).add((0,n.int)(n.localId.x)),F=(0,n.int)(n.workgroupId.y).mul(16).add((0,n.int)(n.localId.y));(0,n.If)(e.lessThan(o).and(F.lessThan(s)),()=>{let o=(0,n.vec2)((0,n.float)(e).add(.5),(0,n.float)(F).add(.5)),s=ne({state:(0,n.uint)(F.mul((0,n.int)(a.x)).add(e)).add((0,n.uint)(1))}).toVar(),ee=o.div(a).mul(2).sub(1).toVar();ee.y.assign(ee.y.negate());let te=wt.wrap(qa(ee,s,c,l,f,p,m,h,g,_,v)),re=(0,n.vec4)(1,0,1,1).toVar();(0,n.If)(E.equal((0,n.int)(9)),()=>{let e=pe(o,(0,n.int)(0),(0,n.int)(1),s,a,P);re.assign((0,n.vec4)(e,1,1))}).Else(()=>{re.assign(Ss(te.origin,te.direction,y,b,x,S,C,w,T,E,D,o,a,O,k,A,j,M,N,u,d,P))});let ie=(0,n.uvec2)((0,n.uint)(e),(0,n.uint)(F));(0,n.textureStore)(t,ie,re).toWriteOnly(),(0,n.textureStore)(r,ie,(0,n.vec4)(.5,.5,1,1)).toWriteOnly(),(0,n.textureStore)(i,ie,(0,n.vec4)(re.xyz,1)).toWriteOnly()})})}function ws({sortGlobalHistogram:e,bins:t=256}){return(0,n.Fn)(()=>{(0,n.If)(n.instanceIndex.lessThan((0,n.uint)(t)),()=>{(0,n.atomicStore)(e.element(n.instanceIndex),(0,n.uint)(0))})})}function Ts({hitBufferRO:e,activeIndicesReadRO:t,sortGlobalHistogram:r,counters:i,bins:a=256}){return(0,n.Fn)(()=>{let o=n.instanceIndex,s=n.localId.x,c=T(`uint`,a);(0,n.If)(s.lessThan((0,n.uint)(a)),()=>{(0,n.atomicStore)(c.element(s),(0,n.uint)(0))}),(0,n.workgroupBarrier)();let l=(0,n.atomicLoad)(i.element((0,n.uint)(Ta.ENTERING_COUNT)));(0,n.If)(o.lessThan(l),()=>{let r=(0,n.uint)(ha(e,t.element(o))).clamp((0,n.uint)(0),(0,n.uint)(a-1));(0,n.atomicAdd)(c.element(r),(0,n.uint)(1))}),(0,n.workgroupBarrier)(),(0,n.If)(s.lessThan((0,n.uint)(a)),()=>{let e=(0,n.atomicLoad)(c.element(s));(0,n.If)(e.greaterThan((0,n.uint)(0)),()=>{(0,n.atomicAdd)(r.element(s),e)})})})}function Es({sortGlobalHistogram:e,bins:t=256}){return(0,n.Fn)(()=>{(0,n.If)(n.instanceIndex.equal((0,n.uint)(0)),()=>{let r=(0,n.uint)(0).toVar();(0,n.Loop)({start:(0,n.uint)(0),end:(0,n.uint)(t),type:`uint`},({i:t})=>{let i=e.element(t),a=(0,n.atomicLoad)(i);(0,n.atomicStore)(i,r),r.addAssign(a)})})})}function Ds({hitBufferRO:e,activeIndicesReadRO:t,sortedIndicesRW:r,sortGlobalHistogram:i,counters:a,bins:o=256}){return(0,n.Fn)(()=>{let s=n.instanceIndex,c=(0,n.atomicLoad)(a.element((0,n.uint)(Ta.ENTERING_COUNT)));(0,n.If)(s.lessThan(c),()=>{let a=t.element(s),c=(0,n.uint)(ha(e,a)).clamp((0,n.uint)(0),(0,n.uint)(o-1)),l=(0,n.atomicAdd)(i.element(c),(0,n.uint)(1));r.element(l).assign(a)})})}var Os=class extends Gi{constructor(e,t,r,i={}){super(e,t,r,i),this.name=`PathTracer`,this._packedBuffers=null,this._queueManager=null,this._kernelManager=null,this._gBufferAttr=null,this._wavefrontReady=!1,this._auxGBufferEnabled=!1,this._auxGBufferUniform=(0,n.uniform)(0,`uint`),this._useDynamicDispatch=!0,this._sortMaterials=!1,this._useSubgroupCompact=!1,this._lastBounceCounts=null,this._lastBounceCountsBudget=-1,this._readbackPending=!1,this._readbackEveryNFrames=4,this._readbackFrameCounter=0,this._readbackGeneration=0,this._bounceEarlyExitThreshold=100,this._wfRenderWidth=(0,n.uniform)(1920,`int`),this._wfRenderHeight=(0,n.uniform)(1080,`int`),this._wfMaxRayCount=(0,n.uniform)(0,`uint`),this._wfCurrentBounce=(0,n.uniform)(0,`int`),this.vramTracker=new Na(this.renderer),this._registerVRAMProviders(),console.log(`PathTracer: initialized (wavefront)`)}_registerVRAMProviders(){let e=this.vramTracker;e.register(`rays`,()=>{let e=this._packedBuffers?._attrs;return e?[e.ray,e.rng,e.hit]:null}),e.register(`queues`,()=>{let e=this._queueManager;return e?[e._countersAttr,e._bounceCountsAttr,e._attrA,e._attrB,e._sortAttr]:null}),e.register(`gbuffer`,()=>this._gBufferAttr?[this._gBufferAttr]:null),e.register(`accum`,()=>{let e=this.storageTextures;return e?[e.writeColor,e.writeNormalDepth,e.writeAlbedo,e.readTarget]:null}),e.register(`geometry`,()=>[this.triangleStorageAttr,this.bvhStorageAttr,this.lightStorageAttr]),e.register(`materials`,()=>{let e=this.materialData;return e?[e.materialStorageAttr,e.albedoMaps,e.emissiveMaps,e.normalMaps,e.bumpMaps,e.roughnessMaps,e.metalnessMaps,e.displacementMaps]:null}),e.register(`environment`,()=>{let e=this.environment;return e?[e.environmentTexture,e.envCDFTexture]:null})}setupMaterial(){super.setupMaterial(),this.materialData?.materialCount>0&&(this._kernelManager&&this._kernelManager.dispose(),this._wavefrontReady=!1,this._buildWavefrontKernels())}render(e){if(!this.isReady||!this._wavefrontReady)return;if(this.isComplete||this.frameCount>=this.completionThreshold){this.isComplete||=!0;return}this.performanceMonitor?.start();let t=this.frameCount,n=this.renderMode.value,r=null;n===1&&t===0&&(r=this.maxBounces.value,this.maxBounces.value=1),this._handleResize(),this.manageASVGFForRenderMode(n),e&&e.setState(`tileRenderingComplete`,!0),this.cameraChanged=this._updateCameraUniforms(),this.cameraOptimizer?.updateInteractionMode(this.cameraChanged),this._updateAccumulationUniforms(t,n),this.frame.value=t,this._setWfDispatch();let i=this.storageTextures.getReadTextures();this.shaderBuilder.prevColorTexNode&&(this.shaderBuilder.prevColorTexNode.value=i.color,this.shaderBuilder.prevAlbedoTexNode.value=i.albedo),this._refreshWfTextureNodes();let a=this._kernelManager;if((this.visMode?.value|0)>0&&this.visMode.value!==11){a.dispatch(`debug`),this.storageTextures.copyToReadTargets(this.renderer);let t=this.storageTextures.getReadTextures();e&&this._publishTexturesToContext(e,t),this._emitStateEvents(),this.cameraOptimizer?.isInInteractionMode()||this.frameCount++,r!==null&&(this.maxBounces.value=r),this.performanceMonitor?.end();return}a.dispatch(`resetCounters`),a.dispatch(`generate`),a.dispatch(`initActiveIndices`);let o=this.maxBounces.value,s=o+this.transmissiveBounces.value+this.maxSubsurfaceSteps.value,c=this._wfMaxRayCount.value,l=this._lastBounceCounts,u=l?o<=this._lastBounceCountsBudget?s+1:this._lastBounceCountsBudget:0;for(let e=0;e<=s;e++){this._wfCurrentBounce.value=e;let t=this._useDynamicDispatch;if(t){let t=c;if(e>0){let n=e-1,r;n<u&&l[n]!==void 0?r=l[n]:u>0&&(r=l[u-1]),t=r>0?r:c}let n=Math.min(c,Math.ceil(t*1.5)+1024),r=[Math.ceil(n/256),1,1];a.setDispatchCount(`extend`,r),a.setDispatchCount(`shade`,r),a.setDispatchCount(`globalHist`,r),a.setDispatchCount(`globalScatter`,r),a.setDispatchCount(`compact`,r),a.setDispatchCount(`compactCopyback`,r)}else{a.dispatch(`enterFull`);let e=[Math.ceil(c/256),1,1];a.setDispatchCount(`extend`,e),a.setDispatchCount(`shade`,e),a.setDispatchCount(`compact`,e),a.setDispatchCount(`globalHist`,e),a.setDispatchCount(`globalScatter`,e)}if(a.dispatch(`extend`),this._sortMaterials&&(a.dispatch(`resetGlobalHist`),a.dispatch(`globalHist`),a.dispatch(`globalPrefix`),a.dispatch(`globalScatter`)),a.dispatch(`shade`),a.dispatch(`resetActiveCounter`),a.dispatch(`compact`),t&&a.dispatch(`compactCopyback`),a.dispatch(`snapshotBounceCount`),e<u&&e<s&&l[e]!==void 0&&l[e]<=this._bounceEarlyExitThreshold)break}a.dispatch(`finalWrite`),this._maybeReadbackCounters(),this.storageTextures.copyToReadTargets(this.renderer,this._auxGBufferEnabled);let d=this.storageTextures.getReadTextures();e&&this._publishTexturesToContext(e,d),this._emitStateEvents(),this.cameraOptimizer?.isInInteractionMode()||this.frameCount++,r!==null&&(this.maxBounces.value=r),this.performanceMonitor?.end()}_handleResize(){let e=this.storageTextures.renderWidth,t=this.storageTextures.renderHeight;super._handleResize(),this._rebuildKernelsIfResized(e,t)}setAuxGBufferEnabled(e){e=!!e,this._auxGBufferEnabled!==e&&(this._auxGBufferEnabled=e,this._auxGBufferUniform.value=+!!e,this.reset())}setSize(e,t){let n=this.storageTextures.renderWidth,r=this.storageTextures.renderHeight;super.setSize(e,t),this._rebuildKernelsIfResized(n,r)}_maybeReadbackCounters(){if(this._readbackPending||(this._readbackFrameCounter++,this._readbackFrameCounter<this._readbackEveryNFrames))return;this._readbackFrameCounter=0;let e=this._queueManager?.getBounceCountsAttribute();if(!e)return;this._readbackPending=!0;let t=this._readbackGeneration,n=this.maxBounces.value;this.renderer.getArrayBufferAsync(e).then(e=>{t===this._readbackGeneration&&(this._lastBounceCounts=new Uint32Array(e.slice(0)),this._lastBounceCountsBudget=n),this._readbackPending=!1}).catch(e=>{console.warn(`Wavefront bounceCounts readback failed:`,e),this._readbackPending=!1})}_refreshWfTextureNodes(){let e=this._wfTexNodes;if(!e)return;let t=this.environment?.environmentTexture;t&&e.envTex&&(e.envTex.value=t),this.environment?.envCDFTexture&&e.envCDFTex&&(e.envCDFTex.value=this.environment.envCDFTexture);let n=this.materialData;n&&(n.albedoMaps&&e.albedoMaps&&(e.albedoMaps.value=n.albedoMaps),n.normalMaps&&e.normalMaps&&(e.normalMaps.value=n.normalMaps),n.bumpMaps&&e.bumpMaps&&(e.bumpMaps.value=n.bumpMaps),n.metalnessMaps&&e.metalnessMaps&&(e.metalnessMaps.value=n.metalnessMaps),n.roughnessMaps&&e.roughnessMaps&&(e.roughnessMaps.value=n.roughnessMaps),n.emissiveMaps&&e.emissiveMaps&&(e.emissiveMaps.value=n.emissiveMaps),n.displacementMaps&&e.displacementMaps&&(e.displacementMaps.value=n.displacementMaps))}_rebuildKernelsIfResized(e,t){let n=this.storageTextures.renderWidth,r=this.storageTextures.renderHeight;if(n===e&&r===t||!(this.materialData?.materialCount>0))return;this._lastBounceCounts=null,this._lastBounceCountsBudget=-1,this._readbackFrameCounter=0,this._readbackGeneration++;let i=Yi.requiredCapacity(n*r);!this._packedBuffers||i>this._packedBuffers.capacity?(this._kernelManager&&this._kernelManager.dispose(),this._wavefrontReady=!1,this._buildWavefrontKernels()):this._resizeWavefrontInPlace(n,r)}_resizeWavefrontInPlace(e,t){let n=e*t;this._wfRenderWidth.value=e,this._wfRenderHeight.value=t,this._wfMaxRayCount.value=n,this._kernelManager?.setDispatchCount(`initActiveIndices`,[Math.ceil(n/256),1,1]),this._bounceEarlyExitThreshold!==-1&&(this._bounceEarlyExitThreshold=Math.max(100,Math.floor(n/1e3)))}_buildWavefrontKernels(){let e=this.shaderBuilder.getSceneTextureNodes();if(!e)return;let r=this.storageTextures.renderWidth,i=this.storageTextures.renderHeight,a=r*i;this._bounceEarlyExitThreshold!==-1&&(this._bounceEarlyExitThreshold=Math.max(100,Math.floor(a/1e3))),this._packedBuffers?this._packedBuffers.resize(a):this._packedBuffers=new Yi(a);let o=Yi.requiredCapacity(a)*1;this._gBufferAttr=new t.StorageInstancedBufferAttribute(new Uint32Array(o*4),4);let s=(0,n.storage)(this._gBufferAttr,`uvec4`),c=(0,n.storage)(this._gBufferAttr,`uvec4`).toReadOnly();this._queueManager?this._queueManager.resize(this._packedBuffers.capacity):this._queueManager=new Da(this._packedBuffers.capacity),this._kernelManager||=new Fa(this.renderer);let l=this._packedBuffers,u=this._queueManager;this._wfRenderWidth.value=r,this._wfRenderHeight.value=i,this._wfMaxRayCount.value=a;let d=this.shaderBuilder.prevColorTexNode,f=this.shaderBuilder.prevAlbedoTexNode,p=this.storageTextures.getWriteTextures(),m=u.getCounters(),h=(0,n.Fn)(()=>{(0,n.atomicStore)(m.element((0,n.uint)(Ta.ACTIVE_RAY_COUNT)),(0,n.uint)(0))});this._kernelManager.register(`resetCounters`,h().compute([1,1,1],[1,1,1]));let g=(0,n.Fn)(()=>{(0,n.atomicStore)(m.element((0,n.uint)(Ta.ACTIVE_RAY_COUNT)),(0,n.uint)(0))});this._kernelManager.register(`resetActiveCounter`,g().compute([1,1,1],[1,1,1]));let _=u.getBounceCounts(),v=this._wfCurrentBounce,y=(0,n.Fn)(()=>{let e=(0,n.atomicLoad)(m.element((0,n.uint)(Ta.ACTIVE_RAY_COUNT))),t=(0,n.uint)(v).clamp((0,n.uint)(0),(0,n.uint)(u.MAX_BOUNCE_SNAPSHOTS-1));_.element(t).assign(e),(0,n.atomicStore)(m.element((0,n.uint)(Ta.ENTERING_COUNT)),e)});this._kernelManager.register(`snapshotBounceCount`,y().compute([1,1,1],[1,1,1]));let b=u.activeIndices.a,x=(0,n.Fn)(()=>{let e=n.instanceIndex;b.element(e).assign(e),(0,n.If)(e.equal((0,n.uint)(0)),()=>{(0,n.atomicStore)(m.element((0,n.uint)(Ta.ACTIVE_RAY_COUNT)),this._wfMaxRayCount),(0,n.atomicStore)(m.element((0,n.uint)(Ta.ENTERING_COUNT)),this._wfMaxRayCount)})});this._kernelManager.register(`initActiveIndices`,x().compute([Math.ceil(a/256),1,1],[256,1,1]));let S=Ja({rayBufferRW:l.rayBuffer.rw,rngBufferRW:l.rngBuffer.rw,gBufferRW:s,resolution:this.resolution,frame:this.frame,cameraWorldMatrix:this.cameraWorldMatrix,cameraProjectionMatrixInverse:this.cameraProjectionMatrixInverse,enableDOF:this.enableDOF,focalLength:this.focalLength,aperture:this.aperture,focusDistance:this.focusDistance,sceneScale:this.sceneScale,apertureScale:this.apertureScale,anamorphicRatio:this.anamorphicRatio,renderWidth:this._wfRenderWidth,renderHeight:this._wfRenderHeight,transmissiveBounces:this.transmissiveBounces,transparentBackground:this.transparentBackground,auxGBufferEnabled:this._auxGBufferUniform});this._kernelManager.register(`generate`,S().compute([Math.ceil(r/16),Math.ceil(i/16),1],[16,16,1]));let C=this.bvhStorageNode,w=this.triangleStorageNode,T=this.materialData.materialStorageNode,E=(0,n.texture)(this.environment.envCDFTexture),D=this.lightStorageNode,O=this.materialData,k=this.environment,A=e.albedoMapsTex,j=O.albedoMaps?(0,n.texture)(O.albedoMaps):A,M=O.normalMaps?(0,n.texture)(O.normalMaps):e.normalMapsTex,N=O.bumpMaps?(0,n.texture)(O.bumpMaps):e.bumpMapsTex,P=O.metalnessMaps?(0,n.texture)(O.metalnessMaps):e.metalnessMapsTex,F=O.roughnessMaps?(0,n.texture)(O.roughnessMaps):e.roughnessMapsTex,ee=O.emissiveMaps?(0,n.texture)(O.emissiveMaps):e.emissiveMapsTex,te=O.displacementMaps?(0,n.texture)(O.displacementMaps):e.displacementMapsTex,ne=k.environmentTexture?(0,n.texture)(k.environmentTexture):e.envTex;this._wfTexNodes={envTex:ne,envCDFTex:E,albedoMaps:j,normalMaps:M,bumpMaps:N,metalnessMaps:P,roughnessMaps:F,emissiveMaps:ee,displacementMaps:te},this._sortMaterials=(B.wavefrontSortMaterials??!1)&&(this.materialData?.materialCount??0)>8;let re=Ya({bvhBuffer:C,triangleBuffer:w,materialBuffer:T,rayBufferRO:l.rayBuffer.ro,hitBufferRW:l.hitBuffer.rw,activeIndicesRO:u.getActiveReadRO(),counters:m,maxRayCount:this._wfMaxRayCount});if(this._kernelManager.register(`extend`,re().compute([Math.ceil(a/256),1,1],[256,1,1])),this._sortMaterials){let e=u.getSortGlobalHistogram(),t=Math.min(256,this.materialData?.materialCount??256);this._kernelManager.register(`resetGlobalHist`,ws({sortGlobalHistogram:e,bins:t})().compute([1,1,1],[256,1,1])),this._kernelManager.register(`globalHist`,Ts({hitBufferRO:l.hitBuffer.ro,activeIndicesReadRO:u.getActiveReadRO(),sortGlobalHistogram:e,counters:m,bins:t})().compute([Math.ceil(a/256),1,1],[256,1,1])),this._kernelManager.register(`globalPrefix`,Es({sortGlobalHistogram:e,bins:t})().compute([1,1,1],[1,1,1])),this._kernelManager.register(`globalScatter`,Ds({hitBufferRO:l.hitBuffer.ro,activeIndicesReadRO:u.getActiveReadRO(),sortedIndicesRW:u.getSortedRW(),sortGlobalHistogram:e,counters:m,bins:t})().compute([Math.ceil(a/256),1,1],[256,1,1]))}let ie=ms({gBufferRW:s,envCompensationDelta:this.envCompensationDelta,bvhBuffer:C,triangleBuffer:w,materialBuffer:T,envCDFTexture:E,lightBuffer:D,rayBufferRW:l.rayBuffer.rw,rngBufferRW:l.rngBuffer.rw,hitBufferRO:l.hitBuffer.ro,counters:m,activeIndicesRO:this._sortMaterials?u.getSortedRO():u.getActiveReadRO(),albedoMaps:j,normalMaps:M,bumpMaps:N,metalnessMaps:P,roughnessMaps:F,emissiveMaps:ee,displacementMaps:te,envTexture:ne,environmentIntensity:this.environmentIntensity,envMatrix:this.environmentMatrix,enableEnvironmentLight:this.enableEnvironment,useEnvMapIS:this.useEnvMapIS,groundProjectionEnabled:this.groundProjectionEnabled,groundProjectionRadius:this.groundProjectionRadius,groundProjectionHeight:this.groundProjectionHeight,groundProjectionLevel:this.groundProjectionLevel,enableGroundCatcher:this.enableGroundCatcher,groundCatcherHeight:this.groundCatcherHeight,envTotalSum:this.envTotalSum,envResolution:this.envResolution,directionalLightsBuffer:this.directionalLightsBufferNode,numDirectionalLights:this.numDirectionalLights,areaLightsBuffer:this.areaLightsBufferNode,numAreaLights:this.numAreaLights,pointLightsBuffer:this.pointLightsBufferNode,numPointLights:this.numPointLights,spotLightsBuffer:this.spotLightsBufferNode,numSpotLights:this.numSpotLights,maxBounceCount:this.maxBounces,maxSubsurfaceSteps:this.maxSubsurfaceSteps,transparentBackground:this.transparentBackground,backgroundIntensity:this.backgroundIntensity,backgroundColor:this.backgroundColor,backgroundBlurriness:this.backgroundBlurriness,backgroundBlurSamples:this.backgroundBlurSamples,showBackground:this.showBackground,globalIlluminationIntensity:this.globalIlluminationIntensity,cameraProjectionMatrix:this.cameraProjectionMatrix,cameraViewMatrix:this.cameraViewMatrix,fireflyThreshold:this.fireflyThreshold,frame:this.frame,resolution:this.resolution,emissiveTriangleCount:this.emissiveTriangleCount,emissiveVec4Offset:this.emissiveVec4Offset,emissiveTotalPower:this.emissiveTotalPower,emissiveBoost:this.emissiveBoost,totalTriangleCount:this.totalTriangleCount,enableEmissiveTriangleSampling:this.enableEmissiveTriangleSampling,lightBVHNodeCount:this.lightBVHNodeCount,reverseMapVec4Offset:this.reverseMapVec4Offset,currentBounce:this._wfCurrentBounce,maxRayCount:this._wfMaxRayCount,auxGBufferEnabled:this._auxGBufferUniform});this._kernelManager.register(`shade`,ie().compute([Math.ceil(a/256),1,1],[256,1,1]));let I=this._useSubgroupCompact&&(this.renderer.hasFeature?this.renderer.hasFeature(`subgroups`):!1);this._compactIsSubgroup=I;let ae=(I?gs:hs)({rayBufferRO:l.rayBuffer.ro,activeIndicesReadRO:u.getActiveReadRO(),activeIndicesWriteRW:u.getActiveWrite(),counters:m,currentActiveCount:this._wfMaxRayCount});this._kernelManager.register(`compact`,ae().compute([Math.ceil(a/256),1,1],[256,1,1]));let oe=(0,n.Fn)(()=>{(0,n.atomicStore)(m.element((0,n.uint)(Ta.ENTERING_COUNT)),this._wfMaxRayCount)});this._kernelManager.register(`enterFull`,oe().compute([1,1,1],[1,1,1]));let se=u.activeIndicesRO.b,ce=u.activeIndices.a,le=(0,n.Fn)(()=>{let e=n.instanceIndex;(0,n.If)(e.greaterThanEqual((0,n.atomicLoad)(m.element((0,n.uint)(Ta.ACTIVE_RAY_COUNT)))),()=>{(0,n.Return)()}),ce.element(e).assign(se.element(e))});this._kernelManager.register(`compactCopyback`,le().compute([Math.ceil(a/256),1,1],[256,1,1]));let ue=vs({rayBufferRO:l.rayBuffer.ro,gBufferRO:c,writeColorTex:p.color,writeNDTex:p.normalDepth,writeAlbedoTex:p.albedo,resolution:this.resolution,frame:this.frame,enableAccumulation:this.enableAccumulation,hasPreviousAccumulated:this.hasPreviousAccumulated,accumulationAlpha:this.accumulationAlpha,cameraIsMoving:this.cameraIsMoving,transparentBackground:this.transparentBackground,prevAccumTexture:d,prevAlbedoTexture:f,renderWidth:this._wfRenderWidth,renderHeight:this._wfRenderHeight,visMode:this.visMode,auxGBufferEnabled:this._auxGBufferUniform});this._kernelManager.register(`finalWrite`,ue().compute([Math.ceil(r/16),Math.ceil(i/16),1],[16,16,1]));let de=Cs({writeColorTex:p.color,writeNDTex:p.normalDepth,writeAlbedoTex:p.albedo,resolution:this.resolution,renderWidth:this._wfRenderWidth,renderHeight:this._wfRenderHeight,cameraWorldMatrix:this.cameraWorldMatrix,cameraProjectionMatrixInverse:this.cameraProjectionMatrixInverse,cameraProjectionMatrix:this.cameraProjectionMatrix,cameraViewMatrix:this.cameraViewMatrix,enableDOF:this.enableDOF,focalLength:this.focalLength,aperture:this.aperture,focusDistance:this.focusDistance,sceneScale:this.sceneScale,apertureScale:this.apertureScale,anamorphicRatio:this.anamorphicRatio,bvhBuffer:C,triangleBuffer:w,materialBuffer:T,envTexture:ne,environmentMatrix:this.environmentMatrix,environmentIntensity:this.environmentIntensity,enableEnvironmentLight:this.enableEnvironment,visMode:this.visMode,debugVisScale:this.debugVisScale,albedoMaps:j,normalMaps:M,bumpMaps:N,metalnessMaps:P,roughnessMaps:F,emissiveMaps:ee,frame:this.frame});this._kernelManager.register(`debug`,de().compute([Math.ceil(r/16),Math.ceil(i/16),1],[16,16,1])),this._wavefrontReady=!0,console.log(`PathTracer: all wavefront kernels built (${r}×${i}, ${a} rays)`)}_setWfDispatch(){let e=this._wfRenderWidth.value,t=this._wfRenderHeight.value;this._kernelManager.setDispatchCount(`generate`,[Math.ceil(e/16),Math.ceil(t/16),1]),this._kernelManager.setDispatchCount(`finalWrite`,[Math.ceil(e/16),Math.ceil(t/16),1]),this._kernelManager.setDispatchCount(`debug`,[Math.ceil(e/16),Math.ceil(t/16),1])}dispose(){super.dispose(),this._packedBuffers?.dispose(),this._queueManager?.dispose(),this._kernelManager?.dispose(),this._gBufferAttr?.dispose?.(),this._packedBuffers=null,this._queueManager=null,this._kernelManager=null,this._gBufferAttr=null,this._wavefrontReady=!1}},ks=class extends ge{constructor(e,i={}){super(`NormalDepth`,{...i,executionMode:he.ALWAYS}),this.renderer=e,this.pathTracer=i.pathTracer,this._dirty=!0,this.cameraWorldMatrix=(0,n.uniform)(new r.Matrix4,`mat4`),this.cameraProjectionMatrixInverse=(0,n.uniform)(new r.Matrix4,`mat4`),this.resolutionWidth=(0,n.uniform)(i.width||1),this.resolutionHeight=(0,n.uniform)(i.height||1);let a=i.width||1,o=i.height||1;this._outputStorageTex=new t.StorageTexture(V,V),this._outputStorageTex.type=r.HalfFloatType,this._outputStorageTex.format=r.RGBAFormat,this._outputStorageTex.minFilter=r.NearestFilter,this._outputStorageTex.magFilter=r.NearestFilter,this._shadingStorageTex=new t.StorageTexture(V,V),this._shadingStorageTex.type=r.HalfFloatType,this._shadingStorageTex.format=r.RGBAFormat,this._shadingStorageTex.minFilter=r.NearestFilter,this._shadingStorageTex.magFilter=r.NearestFilter,this._shadingRT=new t.RenderTarget(a,o,{type:r.HalfFloatType,format:r.RGBAFormat,minFilter:r.NearestFilter,magFilter:r.NearestFilter,depthBuffer:!1,stencilBuffer:!1}),this._srcRegion=new r.Box2(new r.Vector2(0,0),new r.Vector2(0,0));let s={type:r.HalfFloatType,format:r.RGBAFormat,minFilter:r.NearestFilter,magFilter:r.NearestFilter,depthBuffer:!1,stencilBuffer:!1};this._rtA=new t.RenderTarget(a,o,s),this._rtB=new t.RenderTarget(a,o,s),this._currentIdx=0,this._hasHistory=!1,this._dispatchX=Math.ceil(a/8),this._dispatchY=Math.ceil(o/8),this._triStorageNode=null,this._bvhStorageNode=null,this._matStorageNode=null,this._lastTriAttr=null,this._lastBvhAttr=null,this._lastMatAttr=null,this._computeNode=null,this._computeBuilt=!1,this._normalMapsTex=(0,n.texture)(this._makePlaceholderArray()),this._bumpMapsTex=(0,n.texture)(this._makePlaceholderArray())}_makePlaceholderArray(){let e=new r.DataArrayTexture(new Uint8Array([128,128,255,255]),1,1,1);return e.minFilter=r.LinearFilter,e.magFilter=r.LinearFilter,e.generateMipmaps=!1,e.needsUpdate=!0,e}setupEventListeners(){this.on(`camera:moved`,()=>{this._dirty=!0}),this.on(`pipeline:reset`,()=>{this._dirty=!0,this._hasHistory=!1})}_syncStorageBuffers(){let e=this.pathTracer;if(!e)return!1;let t=e.materialData?.materialStorageAttr,r=e.triangleStorageAttr&&e.triangleStorageAttr!==this._lastTriAttr,i=e.bvhStorageAttr&&e.bvhStorageAttr!==this._lastBvhAttr,a=t&&t!==this._lastMatAttr;(r||i||a)&&(this._computeNode?.dispose?.(),this._computeNode=null,this._computeBuilt=!1,this._triStorageNode=null,this._bvhStorageNode=null,this._matStorageNode=null,this._dirty=!0),e.triangleStorageAttr&&!this._triStorageNode&&(this._triStorageNode=(0,n.storage)(e.triangleStorageAttr,`vec4`,e.triangleStorageAttr.count).toReadOnly()),e.bvhStorageAttr&&!this._bvhStorageNode&&(this._bvhStorageNode=(0,n.storage)(e.bvhStorageAttr,`vec4`,e.bvhStorageAttr.count).toReadOnly()),t&&!this._matStorageNode&&(this._matStorageNode=(0,n.storage)(t,`vec4`,t.count).toReadOnly());let o=e.materialData;return o?.normalMaps&&(this._normalMapsTex.value=o.normalMaps),o?.bumpMaps&&(this._bumpMapsTex.value=o.bumpMaps),this._lastTriAttr=e.triangleStorageAttr||this._lastTriAttr,this._lastBvhAttr=e.bvhStorageAttr||this._lastBvhAttr,this._lastMatAttr=t||this._lastMatAttr,!!(this._triStorageNode&&this._bvhStorageNode&&this._matStorageNode)}_buildCompute(){let e=this._triStorageNode,t=this._bvhStorageNode,r=this._matStorageNode,i=this._normalMapsTex,a=this._bumpMapsTex,o=this.cameraWorldMatrix,s=this.cameraProjectionMatrixInverse,c=this.resolutionWidth,l=this.resolutionHeight,u=this._outputStorageTex,d=this._shadingStorageTex;this._computeNode=(0,n.Fn)(([o,s])=>{let f=(0,n.int)(n.workgroupId.x).mul(8).add((0,n.int)(n.localId.x)),p=(0,n.int)(n.workgroupId.y).mul(8).add((0,n.int)(n.localId.y));(0,n.If)(f.lessThan((0,n.int)(c)).and(p.lessThan((0,n.int)(l))),()=>{let m=(0,n.vec3)((0,n.float)(f).add(.5).div(c).mul(2).sub(1),(0,n.float)(p).add(.5).div(l).mul(2).sub(1).negate(),1),h=s.mul((0,n.vec4)(m,1)),g=(0,n.normalize)((0,n.mat3)(o[0].xyz,o[1].xyz,o[2].xyz).mul(h.xyz.div(h.w))),_=wt({origin:(0,n.vec3)(o[3]),direction:g}),v=Ot.wrap(Ga(_,t,e)),y=v.normal.mul(.5).add(.5),b=v.dst,x=v.didHit.select((0,n.vec4)(y,b),(0,n.vec4)(0,0,0,(0,n.float)(1e6)));(0,n.textureStore)(u,(0,n.uvec2)((0,n.uint)(f),(0,n.uint)(p)),x).toWriteOnly();let S=v.normal.toVar();(0,n.If)(v.didHit,()=>{let e=Tt.wrap(un(v.materialIndex,r)).toVar(),t=It.wrap(ar(v.uv,e)).toVar(),n=cr(i,v.normal,e,t).toVar();S.assign(lr(a,n,e,t))});let C=v.didHit.select((0,n.vec4)(S.mul(.5).add(.5),b),(0,n.vec4)(0,0,0,(0,n.float)(1e6)));(0,n.textureStore)(d,(0,n.uvec2)((0,n.uint)(f),(0,n.uint)(p)),C).toWriteOnly()})})(o,s).compute([this._dispatchX,this._dispatchY,1],[8,8,1]),this._computeBuilt=!0}render(e){if(!this.enabled||!this._syncStorageBuffers())return;this._computeBuilt||this._buildCompute();let t=this.pathTracer;if(t&&(this.cameraWorldMatrix.value.copy(t.uniforms.get(`cameraWorldMatrix`).value),this.cameraProjectionMatrixInverse.value.copy(t.uniforms.get(`cameraProjectionMatrixInverse`).value)),!this._dirty&&this._hasHistory){let t=this._currentIdx===0?this._rtA:this._rtB;e.setTexture(`pathtracer:normalDepth`,t.texture),e.setTexture(`pathtracer:prevNormalDepth`,t.texture),e.setTexture(`pathtracer:shadingNormal`,this._shadingRT.texture);return}let n=e.getTexture(`pathtracer:color`);if(n&&n.image){let e=n.image;e.width>0&&e.height>0&&(e.width!==this._rtA.width||e.height!==this._rtA.height)&&this.setSize(e.width,e.height)}this._hasHistory&&(this._currentIdx=1-this._currentIdx);let r=this._currentIdx===0?this._rtA:this._rtB,i=this._currentIdx===0?this._rtB:this._rtA;this.renderer.compute(this._computeNode),this._srcRegion.max.set(r.width,r.height),this.renderer.copyTextureToTexture(this._outputStorageTex,r.texture,this._srcRegion),this.renderer.copyTextureToTexture(this._shadingStorageTex,this._shadingRT.texture,this._srcRegion),this._hasHistory||=(this.renderer.copyTextureToTexture(this._outputStorageTex,i.texture,this._srcRegion),!0),e.setTexture(`pathtracer:normalDepth`,r.texture),e.setTexture(`pathtracer:prevNormalDepth`,i.texture),e.setTexture(`pathtracer:shadingNormal`,this._shadingRT.texture),this._dirty=!1}releaseGPUMemory(){this._outputStorageTex?.dispose(),this._shadingStorageTex?.dispose(),this.reset()}reset(){this._dirty=!0,this._hasHistory=!1}setSize(e,t){this._rtA.setSize(e,t),this._rtA.texture.needsUpdate=!0,this._rtB.setSize(e,t),this._rtB.texture.needsUpdate=!0,this._shadingRT.setSize(e,t),this._shadingRT.texture.needsUpdate=!0,this._hasHistory=!1,this.resolutionWidth.value=e,this.resolutionHeight.value=t,this._dispatchX=Math.ceil(e/8),this._dispatchY=Math.ceil(t/8),this._computeNode&&(this._computeNode.dispatchSize=[this._dispatchX,this._dispatchY,1]),this._dirty=!0}dispose(){this._computeNode?.dispose(),this._outputStorageTex?.dispose(),this._shadingStorageTex?.dispose(),this._shadingRT?.dispose(),this._rtA?.dispose(),this._rtB?.dispose()}},As=class extends ge{constructor(e,i,a={}){super(`MotionVector`,{...a,executionMode:he.ALWAYS}),this.renderer=e,this.camera=i,this.pathTracer=a.pathTracer||null;let o=a.width||1,s=a.height||1;this.prevViewProjectionMatrix=new r.Matrix4,this.currentViewProjectionMatrix=new r.Matrix4,this.matricesInitialized=!1,this.isFirstFrame=!0,this.frameCount=0,this.cameraWorldMatrix=(0,n.uniform)(new r.Matrix4,`mat4`),this.cameraProjectionMatrixInverse=(0,n.uniform)(new r.Matrix4,`mat4`),this.prevVP=(0,n.uniform)(new r.Matrix4,`mat4`),this.isFirstFrameU=(0,n.uniform)(1),this.deltaTime=(0,n.uniform)(1/60),this.velocityScale=(0,n.uniform)(1),this.resolutionWidth=(0,n.uniform)(o),this.resolutionHeight=(0,n.uniform)(s),this._normalDepthTexNode=new t.TextureNode,this._screenSpaceStorageTex=new t.StorageTexture(V,V),this._screenSpaceStorageTex.type=r.HalfFloatType,this._screenSpaceStorageTex.format=r.RGBAFormat,this._screenSpaceStorageTex.minFilter=r.NearestFilter,this._screenSpaceStorageTex.magFilter=r.NearestFilter,this._worldSpaceStorageTex=new t.StorageTexture(V,V),this._worldSpaceStorageTex.type=r.HalfFloatType,this._worldSpaceStorageTex.format=r.RGBAFormat,this._worldSpaceStorageTex.minFilter=r.NearestFilter,this._worldSpaceStorageTex.magFilter=r.NearestFilter,this._srcRegion=new r.Box2(new r.Vector2(0,0),new r.Vector2(0,0));let c={type:r.HalfFloatType,format:r.RGBAFormat,minFilter:r.NearestFilter,magFilter:r.NearestFilter,depthBuffer:!1,stencilBuffer:!1};this.screenSpaceTarget=new t.RenderTarget(o,s,c),this.worldSpaceTarget=new t.RenderTarget(o,s,c),this._dispatchX=Math.ceil(o/16),this._dispatchY=Math.ceil(s/16),this._buildScreenSpaceCompute(),this._buildWorldSpaceCompute()}_buildScreenSpaceCompute(){let e=this._normalDepthTexNode,t=this.cameraWorldMatrix,r=this.cameraProjectionMatrixInverse,i=this.prevVP,a=this.resolutionWidth,o=this.resolutionHeight,s=this._screenSpaceStorageTex;this._screenSpaceComputeNode=(0,n.Fn)(([t,r])=>{let c=(0,n.int)(n.workgroupId.x).mul(16).add((0,n.int)(n.localId.x)),l=(0,n.int)(n.workgroupId.y).mul(16).add((0,n.int)(n.localId.y));(0,n.If)(c.lessThan((0,n.int)(a)).and(l.lessThan((0,n.int)(o))),()=>{let u=(0,n.textureLoad)(e,(0,n.ivec2)(c,l)).w,d=(0,n.vec2)((0,n.float)(c).add(.5).div(a),(0,n.float)(l).add(.5).div(o)),f=(0,n.vec4)(0,0,u,1).toVar();(0,n.If)(u.lessThan((0,n.float)(1e5)),()=>{let e=(0,n.vec3)((0,n.float)(c).add(.5).div(a).mul(2).sub(1),(0,n.float)(l).add(.5).div(o).mul(2).sub(1).negate(),1),s=r.mul((0,n.vec4)(e,1)),p=(0,n.normalize)((0,n.mat3)(t[0].xyz,t[1].xyz,t[2].xyz).mul(s.xyz.div(s.w))),m=(0,n.vec3)(t[3]).add(p.mul(u)),h=i.mul((0,n.vec4)(m,1)),g=h.xy.div(h.w),_=(0,n.vec2)(g.x.mul(.5).add(.5),g.y.mul(-.5).add(.5)),v=d.sub(_),y=_.x.greaterThanEqual(0).and(_.x.lessThanEqual(1)).and(_.y.greaterThanEqual(0)).and(_.y.lessThanEqual(1));f.assign(y.select((0,n.vec4)(v,u,1),(0,n.vec4)((0,n.float)(1e3),(0,n.float)(1e3),u,0)))}),(0,n.textureStore)(s,(0,n.uvec2)((0,n.uint)(c),(0,n.uint)(l)),f).toWriteOnly()})})(t,r).compute([this._dispatchX,this._dispatchY,1],[16,16,1])}_buildWorldSpaceCompute(){let e=this._normalDepthTexNode,t=this.cameraWorldMatrix,r=this.cameraProjectionMatrixInverse,i=this.prevVP,a=this.isFirstFrameU,o=this.deltaTime,s=this.velocityScale,c=this.resolutionWidth,l=this.resolutionHeight,u=this._worldSpaceStorageTex;this._worldSpaceComputeNode=(0,n.Fn)(([t,r])=>{let d=(0,n.int)(n.workgroupId.x).mul(16).add((0,n.int)(n.localId.x)),f=(0,n.int)(n.workgroupId.y).mul(16).add((0,n.int)(n.localId.y));(0,n.If)(d.lessThan((0,n.int)(c)).and(f.lessThan((0,n.int)(l))),()=>{let p=(0,n.textureLoad)(e,(0,n.ivec2)(d,f)).w,m=(0,n.vec4)(0,0,0,0).toVar();(0,n.If)(a.lessThan(.5).and(p.lessThan((0,n.float)(1e5))),()=>{let e=(0,n.vec3)((0,n.float)(d).add(.5).div(c).mul(2).sub(1),(0,n.float)(f).add(.5).div(l).mul(2).sub(1).negate(),1),a=r.mul((0,n.vec4)(e,1)),u=(0,n.normalize)((0,n.mat3)(t[0].xyz,t[1].xyz,t[2].xyz).mul(a.xyz.div(a.w))),h=(0,n.vec3)(t[3]).add(u.mul(p)),g=(0,n.vec2)((0,n.float)(d).add(.5).div(c),(0,n.float)(f).add(.5).div(l)),_=i.mul((0,n.vec4)(h,1)),v=_.xy.div(_.w),y=(0,n.vec2)(v.x.mul(.5).add(.5),v.y.mul(-.5).add(.5)),b=y.x.greaterThanEqual(0).and(y.x.lessThanEqual(1)).and(y.y.greaterThanEqual(0)).and(y.y.lessThanEqual(1)),x=g.sub(y),S=(0,n.vec3)(x.x.div(o).mul(s),x.y.div(o).mul(s),0);m.assign(b.select((0,n.vec4)(S,1),(0,n.vec4)(0,0,0,.5)))}),(0,n.textureStore)(u,(0,n.uvec2)((0,n.uint)(d),(0,n.uint)(f)),m).toWriteOnly()})})(t,r).compute([this._dispatchX,this._dispatchY,1],[16,16,1])}_updateCameraMatrices(){let e=this.pathTracer,t,n,r,i;if(e&&e.uniforms)t=e.uniforms.get(`cameraWorldMatrix`).value,n=e.uniforms.get(`cameraViewMatrix`).value,r=e.uniforms.get(`cameraProjectionMatrix`).value,i=e.uniforms.get(`cameraProjectionMatrixInverse`).value;else{let e=this.camera;if(!e)return;t=e.matrixWorld,n=e.matrixWorldInverse,r=e.projectionMatrix,i=e.projectionMatrixInverse}this.matricesInitialized?this.prevViewProjectionMatrix.copy(this.currentViewProjectionMatrix):(this.currentViewProjectionMatrix.multiplyMatrices(r,n),this.prevViewProjectionMatrix.copy(this.currentViewProjectionMatrix),this.matricesInitialized=!0),this.currentViewProjectionMatrix.multiplyMatrices(r,n),this.cameraWorldMatrix.value.copy(t),this.cameraProjectionMatrixInverse.value.copy(i),this.prevVP.value.copy(this.prevViewProjectionMatrix)}setupEventListeners(){this.on(`pipeline:reset`,()=>{this.reset()})}render(e){if(!this.enabled)return;let t=e.getTexture(`pathtracer:normalDepth`);if(!t)return;this._updateCameraMatrices(),this.isFirstFrameU.value=+!!this.isFirstFrame,this.frameCount++;let n=t.image;n&&n.width>0&&n.height>0&&(n.width!==this.screenSpaceTarget.width||n.height!==this.screenSpaceTarget.height)&&this.setSize(n.width,n.height),this._normalDepthTexNode.value=t,this.renderer.compute(this._screenSpaceComputeNode),this.renderer.compute(this._worldSpaceComputeNode),this._srcRegion.max.set(this.screenSpaceTarget.width,this.screenSpaceTarget.height),this.renderer.copyTextureToTexture(this._screenSpaceStorageTex,this.screenSpaceTarget.texture,this._srcRegion),this.renderer.copyTextureToTexture(this._worldSpaceStorageTex,this.worldSpaceTarget.texture,this._srcRegion),e.setTexture(`motionVector:screenSpace`,this.screenSpaceTarget.texture),e.setTexture(`motionVector:worldSpace`,this.worldSpaceTarget.texture),e.setTexture(`motionVector:motion`,this.screenSpaceTarget.texture),this.emit(`motionvector:computed`,{frame:this.frameCount,isFirstFrame:this.isFirstFrame}),this.isFirstFrame=!1}releaseGPUMemory(){this._screenSpaceStorageTex?.dispose(),this._worldSpaceStorageTex?.dispose(),this.reset()}reset(){this.matricesInitialized||(this.isFirstFrame=!0),this.frameCount=0}setSize(e,t){this.screenSpaceTarget.setSize(e,t),this.screenSpaceTarget.texture.needsUpdate=!0,this.worldSpaceTarget.setSize(e,t),this.worldSpaceTarget.texture.needsUpdate=!0,this.resolutionWidth.value=e,this.resolutionHeight.value=t,this._dispatchX=Math.ceil(e/16),this._dispatchY=Math.ceil(t/16),this._screenSpaceComputeNode&&(this._screenSpaceComputeNode.dispatchSize=[this._dispatchX,this._dispatchY,1]),this._worldSpaceComputeNode&&(this._worldSpaceComputeNode.dispatchSize=[this._dispatchX,this._dispatchY,1])}setVelocityScale(e){this.velocityScale.value=e}setDeltaTime(e){this.deltaTime.value=e}dispose(){this._screenSpaceComputeNode?.dispose(),this._worldSpaceComputeNode?.dispose(),this._screenSpaceStorageTex?.dispose(),this._worldSpaceStorageTex?.dispose(),this.screenSpaceTarget?.dispose(),this.worldSpaceTarget?.dispose(),this._normalDepthTexNode?.dispose()}},js=e=>(0,n.select)(e.equal(e),e,(0,n.float)(0)).clamp(0,1e7),Ms=e=>(0,n.vec3)(js(e.x),js(e.y),js(e.z)),Ns=class extends ge{constructor(e,i={}){super(`ASVGF`,{...i,executionMode:he.PER_CYCLE}),this.renderer=e,this.temporalAlpha=(0,n.uniform)(i.temporalAlpha??0),this.gradientStrength=(0,n.uniform)(i.gradientStrength??0),this.gradientSigmaScale=(0,n.uniform)(i.gradientSigmaScale??2),this.gradientNoiseFloor=(0,n.uniform)(i.gradientNoiseFloor??0),this.maxAccumFrames=(0,n.uniform)(i.maxAccumFrames??32),this.resW=(0,n.uniform)(i.width||1),this.resH=(0,n.uniform)(i.height||1),this.temporalEnabledU=(0,n.uniform)(1),this.forceResetU=(0,n.uniform)(0),this._colorTexNode=new t.TextureNode,this._albedoTexNode=new t.TextureNode,this._motionTexNode=new t.TextureNode,this._normalDepthTexNode=new t.TextureNode,this._prevNormalDepthTexNode=new t.TextureNode,this._readTemporalTexNode=new t.TextureNode,this._gradientReadTexNode=new t.TextureNode;let a=i.width||1,o=i.height||1;this._temporalTexA=new t.StorageTexture(V,V),this._temporalTexA.type=r.FloatType,this._temporalTexA.format=r.RGBAFormat,this._temporalTexA.minFilter=r.LinearFilter,this._temporalTexA.magFilter=r.LinearFilter,this._temporalTexB=new t.StorageTexture(V,V),this._temporalTexB.type=r.FloatType,this._temporalTexB.format=r.RGBAFormat,this._temporalTexB.minFilter=r.LinearFilter,this._temporalTexB.magFilter=r.LinearFilter,this._outputModulatedTex=new t.StorageTexture(V,V),this._outputModulatedTex.type=r.FloatType,this._outputModulatedTex.format=r.RGBAFormat,this._outputModulatedTex.minFilter=r.LinearFilter,this._outputModulatedTex.magFilter=r.LinearFilter,this._gradientStorageTex=new t.StorageTexture(V,V),this._gradientStorageTex.type=r.HalfFloatType,this._gradientStorageTex.format=r.RGBAFormat,this._gradientStorageTex.minFilter=r.LinearFilter,this._gradientStorageTex.magFilter=r.LinearFilter,this._srcRegion=new r.Box2(new r.Vector2(0,0),new r.Vector2(0,0)),this._demodulatedRT=new t.RenderTarget(a,o,{type:r.FloatType,format:r.RGBAFormat,minFilter:r.LinearFilter,magFilter:r.LinearFilter,depthBuffer:!1,stencilBuffer:!1}),this._outputRT=new t.RenderTarget(a,o,{type:r.FloatType,format:r.RGBAFormat,minFilter:r.LinearFilter,magFilter:r.LinearFilter,depthBuffer:!1,stencilBuffer:!1}),this._gradientRT=new t.RenderTarget(a,o,{type:r.HalfFloatType,format:r.RGBAFormat,minFilter:r.LinearFilter,magFilter:r.LinearFilter,depthBuffer:!1,stencilBuffer:!1}),this.currentMoments=0,this._compiled=!1,this._needsWarmReset=!1,this._dispatchX=Math.ceil(a/8),this._dispatchY=Math.ceil(o/8),this._buildGradientCompute(),this._buildTemporalCompute(),this.showHeatmap=!1,this.debugMode=(0,n.uniform)(0,`int`),this._heatmapStorageTex=new t.StorageTexture(V,V),this._heatmapStorageTex.type=r.FloatType,this._heatmapStorageTex.format=r.RGBAFormat,this._heatmapStorageTex.minFilter=r.NearestFilter,this._heatmapStorageTex.magFilter=r.NearestFilter,this.heatmapTarget=new t.RenderTarget(a,o,{type:r.FloatType,format:r.RGBAFormat,minFilter:r.NearestFilter,magFilter:r.NearestFilter,depthBuffer:!1,stencilBuffer:!1}),this._heatmapRawColorTexNode=new t.TextureNode,this._heatmapColorTexNode=new t.TextureNode,this._heatmapTemporalTexNode=new t.TextureNode,this._heatmapNDTexNode=new t.TextureNode,this._heatmapMotionTexNode=new t.TextureNode,this._heatmapGradientTexNode=new t.TextureNode,this._buildHeatmapCompute()}_buildGradientCompute(){let e=this._colorTexNode,t=this._albedoTexNode,r=this._motionTexNode,i=this._readTemporalTexNode,a=this.gradientNoiseFloor,o=this.gradientSigmaScale,s=this._gradientStorageTex,c=this.resW,l=this.resH,u=(0,n.workgroupArray)(`float`,144),d=(0,n.workgroupArray)(`float`,144);this._gradientNode=(0,n.Fn)(()=>{let f=n.localId.x,p=n.localId.y,m=p.mul(8).add(f),h=(0,n.int)(n.workgroupId.x).mul(8).sub(2).toVar(),g=(0,n.int)(n.workgroupId.y).mul(8).sub(2).toVar(),_=a=>{let o=a.mod((0,n.uint)(12)),s=a.div((0,n.uint)(12)),f=h.add((0,n.int)(o)).clamp((0,n.int)(0),(0,n.int)(c).sub(1)),p=g.add((0,n.int)(s)).clamp((0,n.int)(0),(0,n.int)(l).sub(1)),m=(0,n.textureLoad)(e,(0,n.ivec2)(f,p)).xyz,_=(0,n.textureLoad)(t,(0,n.ivec2)(f,p)).xyz,v=Qt(Ms(m.div((0,n.max)(_,(0,n.vec3)(ke))))).toVar();u.element(a).assign(v);let y=(0,n.textureLoad)(r,(0,n.ivec2)(f,p)),b=(0,n.float)(f).sub(y.x.mul(c)),x=(0,n.float)(p).sub(y.y.mul(l)),S=(0,n.int)(b.add(.5)).clamp((0,n.int)(0),(0,n.int)(c).sub(1)),C=(0,n.int)(x.add(.5)).clamp((0,n.int)(0),(0,n.int)(l).sub(1)),w=y.w.greaterThan(.5),T=(0,n.textureLoad)(i,(0,n.ivec2)(S,C)).xyz,E=w.select(Qt(T),v);d.element(a).assign(E)};_(m);let v=m.add((0,n.uint)(64));(0,n.If)(v.lessThan((0,n.uint)(144)),()=>{_(v)});let y=m.add((0,n.uint)(128));(0,n.If)(y.lessThan((0,n.uint)(144)),()=>{_(y)}),(0,n.workgroupBarrier)();let b=(0,n.int)(n.workgroupId.x).mul(8).add((0,n.int)(f)),x=(0,n.int)(n.workgroupId.y).mul(8).add((0,n.int)(p));(0,n.If)(b.lessThan((0,n.int)(c)).and(x.lessThan((0,n.int)(l))),()=>{let e=(0,n.float)(0).toVar(),t=(0,n.float)(0).toVar();for(let r=-2;r<=2;r++)for(let i=-2;i<=2;i++){let a=p.add((0,n.uint)(2+r)).mul((0,n.uint)(12)).add(f.add((0,n.uint)(2+i))),o=u.element(a);e.addAssign(o),t.addAssign(o.mul(o))}let m=e.div(25),h=(0,n.sqrt)((0,n.max)(t.div(25).sub(m.mul(m)),(0,n.float)(0))).mul(o).toVar(),g=(0,n.float)(0).toVar();for(let e=-2;e<=2;e++)for(let t=-2;t<=2;t++){let r=p.add((0,n.uint)(2+e)).mul((0,n.uint)(12)).add(f.add((0,n.uint)(2+t))),i=u.element(r),o=d.element(r),s=(0,n.max)((0,n.max)((0,n.abs)(i.sub(o)).sub(h),(0,n.float)(0)).div((0,n.max)((0,n.max)(i,o),(0,n.float)(.001))).sub(a),(0,n.float)(0)).div((0,n.max)((0,n.float)(1).sub(a),(0,n.float)(1e-4)));g.addAssign(s.mul(s))}let _=g.div(25).clamp(0,1).toVar(),v=(0,n.textureLoad)(r,(0,n.ivec2)(b,x)),y=(0,n.int)((0,n.float)(b).sub(v.x.mul(c)).add(.5)).clamp((0,n.int)(0),(0,n.int)(c).sub(1)),S=(0,n.int)((0,n.float)(x).sub(v.y.mul(l)).add(.5)).clamp((0,n.int)(0),(0,n.int)(l).sub(1)),C=v.w.greaterThan(.5).select((0,n.textureLoad)(i,(0,n.ivec2)(y,S)).w,(0,n.float)(0)).div(4).clamp(0,1),w=_.mul(C);(0,n.textureStore)(s,(0,n.uvec2)((0,n.uint)(b),(0,n.uint)(x)),(0,n.vec4)(w,_,h,1)).toWriteOnly()})})().compute([this._dispatchX,this._dispatchY,1],[8,8,1])}_buildTemporalCompute(){this._temporalNodeA=this._buildTemporalForDirection(this._temporalTexA),this._temporalNodeB=this._buildTemporalForDirection(this._temporalTexB)}_buildTemporalForDirection(e){let t=this._colorTexNode,r=this._albedoTexNode,i=this._motionTexNode,a=this._normalDepthTexNode,o=this._prevNormalDepthTexNode,s=this._readTemporalTexNode,c=this._gradientReadTexNode,l=this._outputModulatedTex,u=this.maxAccumFrames,d=this.temporalAlpha,f=this.gradientStrength,p=this.temporalEnabledU,m=this.forceResetU,h=this.resW,g=this.resH;return(0,n.Fn)(()=>{let _=(0,n.int)(n.workgroupId.x).mul(8).add((0,n.int)(n.localId.x)),v=(0,n.int)(n.workgroupId.y).mul(8).add((0,n.int)(n.localId.y));(0,n.If)(_.lessThan((0,n.int)(h)).and(v.lessThan((0,n.int)(g))),()=>{let y=(0,n.ivec2)(_,v),b=(0,n.textureLoad)(t,y).xyz,x=(0,n.textureLoad)(r,y).xyz,S=(0,n.max)(x,(0,n.vec3)(ke)),C=Ms(b.div(S)),w=(0,n.vec4)(C,1).toVar(),T=(0,n.vec4)(b,1).toVar();(0,n.If)(p.greaterThan(.5).and(m.lessThan(.5)),()=>{let e=(0,n.textureLoad)(i,y),t=e.w.greaterThan(.5),r=(0,n.float)(_).sub(e.x.mul(h)),l=(0,n.float)(v).sub(e.y.mul(g)),p=r.greaterThanEqual(0).and(r.lessThan((0,n.float)(h))).and(l.greaterThanEqual(0)).and(l.lessThan((0,n.float)(g)));(0,n.If)(t.and(p),()=>{let e=(0,n.textureLoad)(a,y),t=e.xyz.mul(2).sub(1),i=e.w,p=(0,n.int)(r),m=(0,n.int)(l),_=p.add((0,n.int)(1)),v=m.add((0,n.int)(1)),b=r.sub((0,n.float)(p)),x=l.sub((0,n.float)(m)),E=p.clamp((0,n.int)(0),(0,n.int)(h).sub(1)),D=_.clamp((0,n.int)(0),(0,n.int)(h).sub(1)),O=m.clamp((0,n.int)(0),(0,n.int)(g).sub(1)),k=v.clamp((0,n.int)(0),(0,n.int)(g).sub(1)),A=(0,n.float)(1).sub(b).mul((0,n.float)(1).sub(x)),j=b.mul((0,n.float)(1).sub(x)),M=(0,n.float)(1).sub(b).mul(x),N=b.mul(x),P=(e,r,a)=>{let s=(0,n.textureLoad)(o,(0,n.ivec2)(e,r)),c=s.xyz.mul(2).sub(1),l=s.w,u=(0,n.pow)((0,n.dot)(t,c).clamp(0,1),(0,n.float)(16)),d=(0,n.exp)((0,n.abs)(i.sub(l)).div((0,n.max)(i,(0,n.float)(.001))).div((0,n.float)(.05)).negate());return a.mul(u).mul(d)},F=P(E,O,A),ee=P(D,O,j),te=P(E,k,M),ne=P(D,k,N),re=F.add(ee).add(te).add(ne);(0,n.If)(re.greaterThan((0,n.float)(.01)),()=>{let e=(0,n.textureLoad)(s,(0,n.ivec2)(E,O)),t=(0,n.textureLoad)(s,(0,n.ivec2)(D,O)),r=(0,n.textureLoad)(s,(0,n.ivec2)(E,k)),i=(0,n.textureLoad)(s,(0,n.ivec2)(D,k)),a=(0,n.float)(1).div(re),o=e.xyz.mul(F).add(t.xyz.mul(ee)).add(r.xyz.mul(te)).add(i.xyz.mul(ne)).mul(a),l=e.w.mul(F).add(t.w.mul(ee)).add(r.w.mul(te)).add(i.w.mul(ne)).mul(a),p=(0,n.textureLoad)(c,y).x.mul(f).clamp(0,1),m=Ms((0,n.mix)(o,C,(0,n.mix)((0,n.max)((0,n.float)(1).div(l.add(1)),d),(0,n.float)(1),p))),h=(0,n.min)(l.add(1),u);w.assign((0,n.vec4)(m,h)),T.assign((0,n.vec4)(m.mul(S),1))})})}),(0,n.textureStore)(e,(0,n.uvec2)((0,n.uint)(_),(0,n.uint)(v)),w).toWriteOnly(),(0,n.textureStore)(l,(0,n.uvec2)((0,n.uint)(_),(0,n.uint)(v)),T).toWriteOnly()})})().compute([this._dispatchX,this._dispatchY,1],[8,8,1])}_buildHeatmapCompute(){let e=this._heatmapRawColorTexNode,t=this._heatmapColorTexNode,r=this._heatmapTemporalTexNode,i=this._heatmapNDTexNode,a=this._heatmapMotionTexNode,o=this._heatmapGradientTexNode,s=this._heatmapStorageTex,c=this.debugMode,l=this.resW,u=this.resH;this._heatmapComputeNode=(0,n.Fn)(()=>{let d=(0,n.int)(n.workgroupId.x).mul(8).add((0,n.int)(n.localId.x)),f=(0,n.int)(n.workgroupId.y).mul(8).add((0,n.int)(n.localId.y));(0,n.If)(d.lessThan((0,n.int)(l)).and(f.lessThan((0,n.int)(u))),()=>{let p=(0,n.ivec2)(d,f),m=(0,n.vec4)(0,0,0,1).toVar();(0,n.If)(c.equal((0,n.int)(0)),()=>{let e=(0,n.textureLoad)(t,p).xyz;m.assign((0,n.vec4)(e,1))}).ElseIf(c.equal((0,n.int)(1)),()=>{let t=(0,n.float)(0).toVar(),r=(0,n.float)(0).toVar();for(let i=-1;i<=1;i++)for(let a=-1;a<=1;a++){let o=(0,n.textureLoad)(e,(0,n.ivec2)(d.add(a).clamp((0,n.int)(0),(0,n.int)(l).sub(1)),f.add(i).clamp((0,n.int)(0),(0,n.int)(u).sub(1)))).xyz,s=Qt(o);t.addAssign(s),r.addAssign(s.mul(s))}t.divAssign(9),r.divAssign(9);let i=(0,n.max)(r.sub(t.mul(t)),(0,n.float)(0)).div((0,n.max)(t.mul(t),(0,n.float)(1e-4))).mul(10).clamp(0,1),a=i.sub(.5).mul(4).clamp(0,1),o=i.mul(4).clamp(0,1).sub(i.sub(.75).mul(4).clamp(0,1)),s=(0,n.float)(1).sub(i.sub(.25).mul(4).clamp(0,1));m.assign((0,n.vec4)(a,o,s,1))}).ElseIf(c.equal((0,n.int)(2)),()=>{let e=(0,n.textureLoad)(r,p).w.div(32).clamp(0,1);m.assign((0,n.vec4)((0,n.float)(1).sub(e),e,(0,n.float)(.2),1))}).ElseIf(c.equal((0,n.int)(3)),()=>{let e=(0,n.textureLoad)(a,p),t=(0,n.abs)(e.x).mul(100).clamp(0,1),r=(0,n.abs)(e.y).mul(100).clamp(0,1),i=t.add(r).clamp(0,1);m.assign((0,n.vec4)(t,r,i.mul(.3),1))}).ElseIf(c.equal((0,n.int)(4)),()=>{let e=(0,n.textureLoad)(i,p);m.assign((0,n.vec4)(e.xyz,1))}).Else(()=>{let e=(0,n.textureLoad)(o,p).x.mul(5).clamp(0,1);m.assign((0,n.vec4)(e,e.mul(.5),(0,n.float)(1).sub(e),1))}),(0,n.textureStore)(s,(0,n.uvec2)((0,n.uint)(d),(0,n.uint)(f)),m).toWriteOnly()})})().compute([this._dispatchX,this._dispatchY,1],[8,8,1])}setupEventListeners(){this.on(`asvgf:reset`,()=>this.resetTemporalData()),this.on(`asvgf:setTemporal`,e=>{e&&e.enabled!==void 0&&this.setTemporalEnabled(e.enabled)}),this.on(`asvgf:updateParameters`,e=>this.updateParameters(e))}render(e){if(!this.enabled)return;let t=e.getTexture(`pathtracer:color`),n=e.getTexture(`pathtracer:albedo`),r=e.getTexture(`pathtracer:normalDepth`),i=e.getTexture(`pathtracer:prevNormalDepth`)||r,a=e.getTexture(`motionVector:screenSpace`);if(!t)return;let o=t.image;o&&o.width>0&&o.height>0&&(o.width!==this._outputRT.width||o.height!==this._outputRT.height)&&this.setSize(o.width,o.height),this._colorTexNode.value=t,n&&(this._albedoTexNode.value=n),a&&(this._motionTexNode.value=a),r&&(this._normalDepthTexNode.value=r),i&&(this._prevNormalDepthTexNode.value=i);let s=this.currentMoments===0?this._temporalTexB:this._temporalTexA,c=this.currentMoments===0?this._temporalNodeA:this._temporalNodeB,l=this.currentMoments===0?this._temporalTexA:this._temporalTexB;this._compiled||=(this.renderer.compute(this._gradientNode),this.renderer.compute(this._temporalNodeA),this.renderer.compute(this._temporalNodeB),!0),this._readTemporalTexNode.value=s,this._gradientReadTexNode.value=this._gradientStorageTex;let u=this.gradientStrength.value>0||this.showHeatmap;u&&this.renderer.compute(this._gradientNode),this.forceResetU.value=+!!this._needsWarmReset,this.renderer.compute(c),this._needsWarmReset=!1,this._srcRegion.max.set(this.resW.value,this.resH.value),this.renderer.copyTextureToTexture(l,this._demodulatedRT.texture,this._srcRegion),this.renderer.copyTextureToTexture(this._outputModulatedTex,this._outputRT.texture,this._srcRegion),u&&this.renderer.copyTextureToTexture(this._gradientStorageTex,this._gradientRT.texture,this._srcRegion),e.setTexture(`asvgf:demodulated`,this._demodulatedRT.texture),e.setTexture(`asvgf:output`,this._outputRT.texture),e.setTexture(`asvgf:gradient`,this._gradientRT.texture),this.currentMoments=1-this.currentMoments,this.showHeatmap&&(this._heatmapRawColorTexNode.value=t,this._heatmapColorTexNode.value=this._outputModulatedTex,this._heatmapTemporalTexNode.value=l,r&&(this._heatmapNDTexNode.value=r),a&&(this._heatmapMotionTexNode.value=a),this._heatmapGradientTexNode.value=this._gradientStorageTex,this.renderer.compute(this._heatmapComputeNode),this._srcRegion.max.set(this.heatmapTarget.width,this.heatmapTarget.height),this.renderer.copyTextureToTexture(this._heatmapStorageTex,this.heatmapTarget.texture,this._srcRegion))}setHeatmapEnabled(e){this.showHeatmap=e}setTemporalEnabled(e){this.temporalEnabledU.value=+!!e}updateParameters(e){e&&(e.temporalAlpha!==void 0&&(this.temporalAlpha.value=e.temporalAlpha),e.gradientStrength!==void 0&&(this.gradientStrength.value=e.gradientStrength),e.gradientSigmaScale!==void 0&&(this.gradientSigmaScale.value=e.gradientSigmaScale),e.gradientNoiseFloor!==void 0&&(this.gradientNoiseFloor.value=e.gradientNoiseFloor),e.maxAccumFrames!==void 0&&(this.maxAccumFrames.value=e.maxAccumFrames),e.debugMode!==void 0&&(this.debugMode.value=e.debugMode))}resetTemporalData(){this.currentMoments=0,this._needsWarmReset=!0}setSize(e,t){this._demodulatedRT.setSize(e,t),this._demodulatedRT.texture.needsUpdate=!0,this._outputRT.setSize(e,t),this._outputRT.texture.needsUpdate=!0,this._gradientRT.setSize(e,t),this._gradientRT.texture.needsUpdate=!0,this.heatmapTarget.setSize(e,t),this.heatmapTarget.texture.needsUpdate=!0,this.resW.value=e,this.resH.value=t,this._dispatchX=Math.ceil(e/8),this._dispatchY=Math.ceil(t/8),this._gradientNode.dispatchSize=[this._dispatchX,this._dispatchY,1],this._temporalNodeA.dispatchSize=[this._dispatchX,this._dispatchY,1],this._temporalNodeB.dispatchSize=[this._dispatchX,this._dispatchY,1],this._heatmapComputeNode.dispatchSize=[this._dispatchX,this._dispatchY,1]}reset(){}releaseGPUMemory(){this._temporalTexA?.dispose(),this._temporalTexB?.dispose(),this._outputModulatedTex?.dispose(),this._gradientStorageTex?.dispose(),this._heatmapStorageTex?.dispose(),this.context?.removeTexture(`asvgf:demodulated`),this.context?.removeTexture(`asvgf:output`),this.context?.removeTexture(`asvgf:gradient`),this._demodulatedRT?.texture?.dispose(),this._outputRT?.texture?.dispose(),this._gradientRT?.texture?.dispose(),this.heatmapTarget?.texture?.dispose(),this.resetTemporalData()}dispose(){this._gradientNode?.dispose(),this._temporalNodeA?.dispose(),this._temporalNodeB?.dispose(),this._temporalTexA?.dispose(),this._temporalTexB?.dispose(),this._outputModulatedTex?.dispose(),this._gradientStorageTex?.dispose(),this._demodulatedRT?.dispose(),this._outputRT?.dispose(),this._gradientRT?.dispose(),this._heatmapComputeNode?.dispose(),this._heatmapStorageTex?.dispose(),this.heatmapTarget?.dispose(),this._colorTexNode?.dispose(),this._albedoTexNode?.dispose(),this._motionTexNode?.dispose(),this._normalDepthTexNode?.dispose(),this._prevNormalDepthTexNode?.dispose(),this._readTemporalTexNode?.dispose(),this._gradientReadTexNode?.dispose(),this._heatmapRawColorTexNode?.dispose(),this._heatmapColorTexNode?.dispose(),this._heatmapTemporalTexNode?.dispose(),this._heatmapNDTexNode?.dispose(),this._heatmapMotionTexNode?.dispose(),this._heatmapGradientTexNode?.dispose()}},Ps=e=>(0,n.select)(e.equal(e),e,(0,n.float)(0)).clamp(0,1e7),Fs=(0,n.wgslFn)(`
|
|
670
670
|
fn temporalAccumulate(
|
|
671
671
|
lum: f32,
|
|
672
672
|
prevMean: f32,
|