@thinkersteps_labs/3d-viewer-core 1.1.0 → 1.2.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/dist/index.cjs CHANGED
@@ -1,4 +1,4 @@
1
- "use strict";var Yi=Object.create;var Ht=Object.defineProperty;var Xi=Object.getOwnPropertyDescriptor;var ji=Object.getOwnPropertyNames;var Ki=Object.getPrototypeOf,Ji=Object.prototype.hasOwnProperty;var Zi=(n,t,e)=>t in n?Ht(n,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):n[t]=e;var Qi=(n,t,e,i)=>{if(t&&typeof t=="object"||typeof t=="function")for(let o of ji(t))!Ji.call(n,o)&&o!==e&&Ht(n,o,{get:()=>t[o],enumerable:!(i=Xi(t,o))||i.enumerable});return n};var lt=(n,t,e)=>(e=n!=null?Yi(Ki(n)):{},Qi(t||!n||!n.__esModule?Ht(e,"default",{value:n,enumerable:!0}):e,n));var re=(n,t,e)=>Zi(n,typeof t!="symbol"?t+"":t,e);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const C=require("three"),eo=require("three/examples/jsm/loaders/GLTFLoader.js"),to=require("three/examples/jsm/loaders/DRACOLoader.js"),no=require("three/examples/jsm/libs/meshopt_decoder.module.js"),Fn=require("three/examples/jsm/loaders/RGBELoader.js"),_n=require("three/examples/jsm/loaders/EXRLoader.js"),io=require("three/examples/jsm/controls/OrbitControls.js");function oo(n){const t=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(n){for(const e in n)if(e!=="default"){const i=Object.getOwnPropertyDescriptor(n,e);Object.defineProperty(t,e,i.get?i:{enumerable:!0,get:()=>n[e]})}}return t.default=n,Object.freeze(t)}const v=oo(C);class Ft extends Error{constructor(e){super(`Unsupported format: "${e}"`);re(this,"format");this.name="UnsupportedFormatError",this.format=e,Object.setPrototypeOf(this,Ft.prototype)}}class Ae extends Error{constructor(e,i){super(e);re(this,"cause");this.name="CorruptFileError",this.cause=i,Object.setPrototypeOf(this,Ae.prototype)}}class ot extends Error{constructor(e,i,o){super(e);re(this,"status");re(this,"url");this.name="NetworkError",this.status=i,this.url=o,Object.setPrototypeOf(this,ot.prototype)}}function _t(n){n.position.set(0,0,0),n.scale.set(1,1,1),n.updateMatrixWorld(!0);const t=new v.Box3().setFromObject(n),e=t.getCenter(new v.Vector3),i=t.getSize(new v.Vector3),o=Math.max(i.x,i.y,i.z),r=o>0?1/o:1;n.scale.setScalar(r),n.position.sub(e.multiplyScalar(r)),n.updateMatrixWorld(!0)}const gn="https://www.gstatic.com/draco/versioned/decoders/1.5.6/";if(typeof globalThis.FileReader>"u"){class n{constructor(){re(this,"result",null);re(this,"onloadend",null);re(this,"onload",null);re(this,"onerror",null)}readAsArrayBuffer(e){e.arrayBuffer().then(i=>{var o,r;this.result=i,(o=this.onload)==null||o.call(this),(r=this.onloadend)==null||r.call(this)},i=>{var o,r;(o=this.onerror)==null||o.call(this,i),(r=this.onloadend)==null||r.call(this)})}readAsDataURL(e){e.arrayBuffer().then(i=>{var s,a;const o=globalThis.Buffer,r=o?o.from(i).toString("base64"):"";this.result=`data:${e.type};base64,${r}`,(s=this.onload)==null||s.call(this),(a=this.onloadend)==null||a.call(this)},i=>{var o,r;(o=this.onerror)==null||o.call(this,i),(r=this.onloadend)==null||r.call(this)})}}globalThis.FileReader=n}const ro=50*1024*1024;async function so(n,t){const e=[];switch(n.data.byteLength>ro&&e.push("File exceeds 50 MB; performance may be affected"),n.format){case"glb":case"gltf":return ao(n,e,t);case"stl":return co(n,e);case"obj":return lo(n,e)}}function di(n){const t=new eo.GLTFLoader,e=new to.DRACOLoader;return e.setDecoderPath((n==null?void 0:n.dracoDecoderPath)??gn),t.setDRACOLoader(e),t.setMeshoptDecoder(no.MeshoptDecoder),{loader:t,dispose:()=>{e.dispose()}}}async function ao(n,t,e){const{loader:i,dispose:o}=di({dracoDecoderPath:e});try{const r=await new Promise((s,a)=>{i.parse(n.data,"",s,a)});return _t(r.scene),{scene:r.scene,animations:r.animations,warnings:t}}catch(r){throw new Ae("Failed to parse GLTF/GLB",r)}finally{o()}}async function co(n,t){let e;try{e=(await import("three/examples/jsm/loaders/STLLoader.js")).STLLoader}catch(c){throw new Ae("Failed to load STL parser module. Please check your network connection.",c)}const i=new e;let o;try{o=i.parse(n.data)}catch(c){throw new Ae("Failed to parse STL",c)}const r=new v.MeshPhysicalMaterial({name:"metal",color:12632256,metalness:1,roughness:.1,envMapIntensity:1}),s=new v.Mesh(o,r);s.name="metal";const a=new v.Group;return a.add(s),_t(a),{scene:a,warnings:t}}async function lo(n,t){let e,i;try{const[a,c]=await Promise.all([import("three/examples/jsm/loaders/OBJLoader.js"),import("three/examples/jsm/loaders/MTLLoader.js")]);e=a.OBJLoader,i=c.MTLLoader}catch(a){throw new Ae("Failed to load OBJ parser module. Please check your network connection.",a)}n.mtlData||t.push("No material file provided; default material applied");let o;if(n.mtlData){const a=new i,c=new TextDecoder().decode(n.mtlData);try{o=a.parse(c,""),o.preload()}catch{t.push("Failed to parse MTL file; default material applied"),o=void 0}}const r=new e;o&&r.setMaterials(o);let s;try{const a=new TextDecoder().decode(n.data);s=r.parse(a)}catch(a){throw new Ae("Failed to parse OBJ",a)}return _t(s),{scene:s,warnings:t}}async function uo(n){let t;try{t=(await import("three/examples/jsm/exporters/GLTFExporter.js")).GLTFExporter}catch(i){throw new Ae("Failed to load GLTFExporter module. Please check your network connection.",i)}const e=new t;return new Promise((i,o)=>{e.parse(n,r=>{if(r instanceof ArrayBuffer)i(r);else{const s=JSON.stringify(r),a=new TextEncoder().encode(s);i(a.buffer)}},o,{binary:!0})})}class fo{constructor(){re(this,"_listeners",new Map)}on(t,e){return this._listeners.has(t)||this._listeners.set(t,new Set),this._listeners.get(t).add(e),()=>this.off(t,e)}off(t,e){var i;(i=this._listeners.get(t))==null||i.delete(e)}emit(t,e){var i;(i=this._listeners.get(t))==null||i.forEach(o=>{try{o(e)}catch(r){console.error(`[3d-viewer] uncaught listener error on "${String(t)}":`,r)}})}clear(){this._listeners.clear()}}const mo={".stl":"stl",".obj":"obj",".glb":"glb",".gltf":"gltf"},po={"model/gltf-binary":"glb","model/gltf+json":"gltf"};function ho(n){try{const t=new URL(n,"http://dummy").pathname,e=t.lastIndexOf(".");if(e===-1)return null;const i=t.slice(e).toLowerCase();return mo[i]??null}catch{return null}}function yo(n){const t=n.split(";")[0].trim().toLowerCase();return po[t]??null}function Dn(n){if(n.byteLength<4)return null;const t=new Uint8Array(n,0,Math.min(n.byteLength,80));return t[0]===103&&t[1]===108&&t[2]===84&&t[3]===70?"glb":t[0]===123?"gltf":String.fromCharCode(...t.slice(0,5)).toLowerCase()==="solid"?"stl":null}async function Nn(n){const t=new Uint8Array(n);if(t.length>=2&&t[0]===31&&t[1]===139&&typeof DecompressionStream<"u")try{const e=new DecompressionStream("gzip"),i=new Blob([n]).stream().pipeThrough(e),o=await new Response(i).arrayBuffer();if(o.byteLength>0)return o}catch(e){console.warn("[fetchSource] Decompression failed, proceeding with original buffer:",e)}return n}async function go(n,t){if("data"in n){let s=n.data instanceof Blob?await n.data.arrayBuffer():n.data;s=await Nn(s);const a=n.mtlData instanceof Blob?await n.mtlData.arrayBuffer():n.mtlData,c=n.format||Dn(s)||"glb";return{data:s,mtlData:a,format:c}}let e=n.format??ho(n.url);const i=await fetch(n.url,{headers:n.headers,signal:t}).catch(s=>{throw s.name==="AbortError"?s:new ot(`Fetch failed: ${s.message}`,void 0,n.url)});if(!i.ok)throw new ot(`HTTP ${i.status}`,i.status,n.url);if(!e){const s=i.headers.get("content-type")??"";e=yo(s)}let o=await i.arrayBuffer();if(o=await Nn(o),e||(e=Dn(o)),!e)throw new Ft("unknown");let r;if(n.mtlUrl){const s=await fetch(n.mtlUrl,{headers:n.headers,signal:t});s.ok&&(r=await s.arrayBuffer())}return{data:o,mtlData:r,format:e}}const vo=["glb","gltf","obj","stl"];function mi(n){var i;const e=(i=n.split("?")[0].split("#")[0].split(".").pop())==null?void 0:i.toLowerCase();return e&&vo.includes(e)?e:"glb"}function bo(n){if(!n)return null;if(typeof n=="string"){const t=n.trim();return t?{url:t,format:mi(t)}:null}return n}async function pi(n,t,e,i){e==null||e(0);const o=await go(n,t);e==null||e(.3);const{scene:r,animations:s,warnings:a}=await so(o,i==null?void 0:i.dracoDecoderPath);e==null||e(.7);const c=o.format==="glb"?o.data:await uo(r);e==null||e(1);let u=0;return r.traverse(f=>{f.isMesh&&u++}),{glb:c,scene:r,animations:s,sourceFormat:o.format,meshCount:u,warnings:a}}const xo=["stone","diamond","gem","jewel","crystal"],wo=["metal","gold","silver","platinum","band","ring","prong","shank"];function Ln(n,t){const e=n.toLowerCase();return t.some(i=>e.includes(i))}function Vn(n){return n.trim().replace(/[^a-zA-Z0-9_-]/g,"_")}function Mo(n,t,e){var a,c;if(typeof((a=n.userData)==null?void 0:a.partId)=="string"&&n.userData.partId.trim()){const u=n.userData.partId.trim();return e.add(u),u}if(typeof((c=n.userData)==null?void 0:c.id)=="string"&&n.userData.id.trim()){const u=n.userData.id.trim();return e.add(u),u}if(n.name&&n.name.trim()){const u=n.name.trim();if(!e.has(u))return e.add(u),u}let i=n.name?n.name.trim():"mesh",o=n.parent;for(;o&&o!==t;)o.name&&o.name.trim()&&(i=`${o.name.trim()}/${i}`),o=o.parent;let r=i,s=1;for(;e.has(r);)r=`${i}_${s++}`;return e.add(r),r}function Ao(n){var o,r,s;if(Array.isArray((o=n.userData)==null?void 0:o.originalSlots)&&n.userData.originalSlots.length>0)return n.userData.originalSlots;if(Array.isArray(n.material))return n.material.map((a,c)=>{var d,l;const u=((d=a==null?void 0:a.name)==null?void 0:d.trim())||`Material_${c}`;return{slotId:typeof((l=a==null?void 0:a.userData)==null?void 0:l.slotId)=="string"&&a.userData.slotId||`slot_${c}_${Vn(u)}`,materialName:u,index:c}});const t=n.material,e=((r=t==null?void 0:t.name)==null?void 0:r.trim())||(n.name?`${n.name}_mat`:"default");return[{slotId:typeof((s=t==null?void 0:t.userData)==null?void 0:s.slotId)=="string"&&t.userData.slotId||`slot_0_${Vn(e)}`,materialName:e,index:0}]}function So(n,t){const e=`${n} ${t}`,i=Ln(e,xo),o=Ln(e,wo);return i?"stone":"metal"}function zn(n,t){const e=[],i=new Set;return n.traverse(o=>{var l;if(!(o instanceof v.Mesh))return;const r=o,s=Mo(r,n,i);r.userData.partId=s;const a=Ao(r);r.userData.originalSlots=a;const c=((l=a[0])==null?void 0:l.materialName)??"";r.userData.originalMaterialName=c;const u=a.map(m=>m.materialName).join(" "),f=r.uuid in t||s in t,d=t[s]??t[r.uuid];f&&d?e.push({meshId:r.uuid,partId:s,meshName:r.name,materialName:c,slots:a,type:d,overridden:!0}):e.push({meshId:r.uuid,partId:s,meshName:r.name,materialName:c,slots:a,type:So(r.name,u),overridden:!1})}),e}function kn(n){const t=new Map;for(const e of n)for(const i of e.slots){const o=t.get(i.slotId);o?(o.partIds.includes(e.partId)||o.partIds.push(e.partId),o.meshIds.includes(e.meshId)||o.meshIds.push(e.meshId),o.type!=="stone"&&e.type==="stone"&&(o.type="stone")):t.set(i.slotId,{slotId:i.slotId,name:i.materialName,type:e.type,partIds:[e.partId],meshIds:[e.meshId]})}return Array.from(t.values())}const Se=[{id:"yellow-gold-18k",label:"18K Yellow Gold",hex:"#ffd566",metalness:1,roughness:.04,category:"gold",specularColor:"#fffdbf",density:15500,thumbnail:"/thumbnails/yellow-gold-18k.svg"},{id:"yellow-gold-14k",label:"14K Yellow Gold",hex:"#fed882",metalness:1,roughness:.05,category:"gold",specularColor:"#fffec9",density:13500,thumbnail:"/thumbnails/yellow-gold-14k.svg"},{id:"pure-gold-24k",label:"24K Pure Gold",hex:"#ffe496",metalness:1,roughness:.03,category:"gold",specularColor:"#fffdbf",density:19320,source:"physicallybased.info/gold",thumbnail:"/thumbnails/pure-gold-24k.svg"},{id:"rose-gold-18k",label:"18K Rose Gold",hex:"#e8927c",metalness:1,roughness:.04,category:"gold",specularColor:"#ffd7cb",density:15400,thumbnail:"/thumbnails/rose-gold-18k.svg"},{id:"rose-gold-14k",label:"14K Rose Gold",hex:"#f1a998",metalness:1,roughness:.05,category:"gold",specularColor:"#ffe4db",density:13300,thumbnail:"/thumbnails/rose-gold-14k.svg"},{id:"white-gold-18k",label:"18K White Gold",hex:"#f5f6f8",metalness:1,roughness:.03,category:"gold",specularColor:"#ffffff",density:15600,thumbnail:"/thumbnails/white-gold-18k.svg"},{id:"platinum-950",label:"Platinum 950",hex:"#e3ded7",metalness:1,roughness:.04,category:"platinum-silver",specularColor:"#edebe6",density:21450,source:"physicallybased.info/platinum",thumbnail:"/thumbnails/platinum-950.svg"},{id:"silver-925",label:"Sterling Silver 925",hex:"#fefdfc",metalness:1,roughness:.05,category:"platinum-silver",specularColor:"#ffffff",density:10360,source:"physicallybased.info/silver",thumbnail:"/thumbnails/silver-925.svg"},{id:"titanium",label:"Titanium",hex:"#b1aaa2",metalness:1,roughness:.1,category:"alternative",specularColor:"#c8c3bd",density:4506,source:"physicallybased.info/titanium",thumbnail:"/thumbnails/titanium.svg"},{id:"black-ruthenium",label:"Black Ruthenium",hex:"#28282c",metalness:.95,roughness:.06,category:"alternative",specularColor:"#444448",density:12450,thumbnail:"/thumbnails/black-ruthenium.svg"},{id:"rmj-custom-yellow-gold",label:"RMJ Custom Yellow Gold",hex:"#ffc766",metalness:1,roughness:0,category:"gold",specularColor:"#ffffff",ior:1.5,source:"app.rendermyjewelry.com",thumbnail:"/thumbnails/rmj-custom-yellow-gold.svg"},{id:"rmj-custom-rose-gold",label:"RMJ Custom Rose Gold",hex:"#ffb6a3",metalness:1,roughness:0,category:"gold",specularColor:"#ffffff",ior:1.5,source:"app.rendermyjewelry.com",thumbnail:"/thumbnails/rmj-custom-rose-gold.svg"},{id:"rmj-white-gold-polished",label:"RMJ White Gold Polished",hex:"#c2c2c3",metalness:1,roughness:0,category:"gold",specularColor:"#ffffff",ior:1.5,source:"app.rendermyjewelry.com",thumbnail:"/thumbnails/rmj-white-gold-polished.svg"},{id:"rmj-gold-polished",label:"RMJ Gold Polished (18K)",hex:"#e5b377",metalness:1,roughness:0,category:"gold",specularColor:"#ffffff",ior:1.5,source:"app.rendermyjewelry.com",thumbnail:"/thumbnails/rmj-gold-polished.svg"},{id:"rmj-rose-gold-polished",label:"RMJ Rose Gold Polished",hex:"#f2af83",metalness:1,roughness:0,category:"gold",specularColor:"#ffffff",ior:1.5,source:"app.rendermyjewelry.com",thumbnail:"/thumbnails/rmj-rose-gold-polished.svg"},{id:"rmj-gold-22k",label:"RMJ Gold 22K",hex:"#eaa958",metalness:1,roughness:0,category:"gold",specularColor:"#ffffff",ior:1.5,source:"app.rendermyjewelry.com",thumbnail:"/thumbnails/rmj-gold-22k.svg"},{id:"rmj-gold-14k",label:"RMJ Gold 14K",hex:"#d6af80",metalness:1,roughness:0,category:"gold",specularColor:"#ffffff",ior:1.5,source:"app.rendermyjewelry.com",thumbnail:"/thumbnails/rmj-gold-14k.svg"},{id:"rmj-gold-09k",label:"RMJ Gold 9K",hex:"#d4b38c",metalness:1,roughness:0,category:"gold",specularColor:"#ffffff",ior:1.5,source:"app.rendermyjewelry.com",thumbnail:"/thumbnails/rmj-gold-09k.svg"},{id:"rmj-silver-polished",label:"RMJ Silver Polished",hex:"#dedede",metalness:1,roughness:.1,category:"platinum-silver",specularColor:"#ffffff",ior:1.5,source:"app.rendermyjewelry.com",thumbnail:"/thumbnails/rmj-silver-polished.svg"},{id:"rmj-platinum-polished",label:"RMJ Platinum Polished",hex:"#b9bbbc",metalness:1,roughness:0,category:"platinum-silver",specularColor:"#ffffff",ior:1.5,source:"app.rendermyjewelry.com",thumbnail:"/thumbnails/rmj-platinum-polished.svg"},{id:"yellow-gold",label:"Yellow Gold",hex:"#ffc766",metalness:1,roughness:.06,thumbnail:"/thumbnails/yellow-gold.svg"},{id:"rose-gold",label:"Rose Gold",hex:"#ffb6a3",metalness:1,roughness:.06,thumbnail:"/thumbnails/rose-gold.svg"},{id:"white-gold",label:"White Gold",hex:"#f0f0f0",metalness:1,roughness:.05,thumbnail:"/thumbnails/white-gold.svg"},{id:"platinum",label:"Platinum",hex:"#e5e5e5",metalness:1,roughness:.05,thumbnail:"/thumbnails/platinum.svg"},{id:"silver",label:"Silver",hex:"#dcdcdc",metalness:1,roughness:.08,thumbnail:"/thumbnails/silver.svg"}],de=[{id:"diamond-d",label:"White Diamond",hex:"#ffffff",ior:2.417,transmission:.98,roughness:0,category:"diamond",grade:"D-F Colorless",dispersion:.024,absorptionFactor:0,ambientFloor:.05,source:"physicallybased.info/diamond",thumbnail:"/thumbnails/diamond-d.svg"},{id:"diamond-gh",label:"Near Colorless Diamond",hex:"#fbf9f5",ior:2.417,transmission:.97,roughness:0,category:"diamond",grade:"G-H Near Colorless",dispersion:.012,absorptionFactor:0,ambientFloor:.05,thumbnail:"/thumbnails/diamond-gh.svg"},{id:"diamond-fancy-yellow",label:"Yellow Diamond",hex:"#fdd835",ior:2.417,transmission:.95,roughness:0,category:"diamond",grade:"Fancy Yellow",dispersion:.01,absorptionFactor:.11,thumbnail:"/thumbnails/diamond-fancy-yellow.svg"},{id:"diamond-fancy-pink",label:"Pink Diamond",hex:"#f472b6",ior:2.417,transmission:.94,roughness:0,category:"diamond",grade:"Fancy Pink",dispersion:.01,absorptionFactor:.14,thumbnail:"/thumbnails/diamond-fancy-pink.svg"},{id:"diamond-fancy-blue",label:"Blue Diamond",hex:"#38bdf8",ior:2.417,transmission:.94,roughness:0,category:"diamond",grade:"Fancy Blue",dispersion:.01,absorptionFactor:.14,thumbnail:"/thumbnails/diamond-fancy-blue.svg"},{id:"diamond-fancy-green",label:"Green Diamond",hex:"#34d399",ior:2.417,transmission:.94,roughness:0,category:"diamond",grade:"Fancy Green",dispersion:.01,absorptionFactor:.14,thumbnail:"/thumbnails/diamond-fancy-green.svg"},{id:"diamond-fancy-champagne",label:"Champagne Diamond",hex:"#c28b51",ior:2.417,transmission:.93,roughness:0,category:"diamond",grade:"Fancy Champagne",dispersion:.01,absorptionFactor:.18,thumbnail:"/thumbnails/diamond-fancy-champagne.svg"},{id:"diamond-fancy-black",label:"Black Diamond",hex:"#151515",ior:2.417,transmission:0,roughness:.05,category:"diamond",grade:"Fancy Black",dispersion:0,absorptionFactor:0,thumbnail:"/thumbnails/diamond-fancy-black.svg"},{id:"ruby",label:"Ruby",hex:"#e14276",ior:1.77,transmission:.88,roughness:.03,category:"gemstone",dispersion:0,absorptionFactor:.5,thumbnail:"/thumbnails/ruby.svg"},{id:"sapphire",label:"Sapphire",hex:"#1a4fb4",ior:1.77,transmission:.88,roughness:.03,category:"gemstone",dispersion:0,absorptionFactor:.45,source:"physicallybased.info/sapphire",thumbnail:"/thumbnails/sapphire.svg"},{id:"emerald",label:"Emerald",hex:"#20cc95",ior:1.58,transmission:.9,roughness:.04,category:"gemstone",dispersion:0,absorptionFactor:.4,thumbnail:"/thumbnails/emerald.svg"},{id:"aquamarine",label:"Aquamarine",hex:"#5eead4",ior:1.577,transmission:.94,roughness:0,category:"gemstone",dispersion:0,absorptionFactor:.1,thumbnail:"/thumbnails/aquamarine.svg"},{id:"amethyst",label:"Amethyst",hex:"#9333ea",ior:1.544,transmission:.91,roughness:0,category:"gemstone",dispersion:0,absorptionFactor:.35,source:"physicallybased.info/quartz",thumbnail:"/thumbnails/amethyst.svg"},{id:"tanzanite",label:"Tanzanite",hex:"#6366f1",ior:1.691,transmission:.9,roughness:0,category:"gemstone",dispersion:0,absorptionFactor:.42,thumbnail:"/thumbnails/tanzanite.svg"},{id:"rmj-diamond-d-colorless",label:"RMJ Diamond D Colorless",hex:"#ececec",ior:2.4,transmission:.98,roughness:0,category:"diamond",dispersion:.004,absorptionFactor:1,boostFactors:[2,2,2],source:"app.rendermyjewelry.com",thumbnail:"/thumbnails/rmj-diamond-d-colorless.svg"},{id:"rmj-ruby-1",label:"RMJ Ruby 1",hex:"#e14276",ior:1.77,transmission:.88,roughness:0,category:"gemstone",dispersion:0,absorptionFactor:1.6,boostFactors:[1.3,.16,.9],source:"app.rendermyjewelry.com",thumbnail:"/thumbnails/rmj-ruby-1.svg"},{id:"rmj-emerald-1",label:"RMJ Emerald 1",hex:"#20cc95",ior:1.577,transmission:.9,roughness:0,category:"gemstone",dispersion:.012,absorptionFactor:1,boostFactors:[.892,.892,.986],source:"app.rendermyjewelry.com",thumbnail:"/thumbnails/rmj-emerald-1.svg"},{id:"rmj-sapphire-cabachon-2",label:"RMJ Sapphire Cabochon 2",hex:"#45b1e1",ior:1.66,transmission:.88,roughness:0,category:"gemstone",dispersion:0,absorptionFactor:2.51,boostFactors:[1,1,1],source:"app.rendermyjewelry.com",thumbnail:"/thumbnails/rmj-sapphire-cabachon-2.svg"},{id:"rmj-diamond-pink-2",label:"RMJ Diamond Pink 2",hex:"#f1bcd5",ior:2.6,transmission:.94,roughness:0,category:"diamond",dispersion:.012,absorptionFactor:1,boostFactors:[1.3,1,1],source:"app.rendermyjewelry.com",thumbnail:"/thumbnails/rmj-diamond-pink-2.svg"},{id:"rmj-diamond-yellow-2",label:"RMJ Diamond Yellow 2",hex:"#fce88c",ior:2.6,transmission:.94,roughness:0,category:"diamond",dispersion:.012,absorptionFactor:1.3,boostFactors:[1,1,1],source:"app.rendermyjewelry.com",thumbnail:"/thumbnails/rmj-diamond-yellow-2.svg"},{id:"rmj-diamond-blue-2",label:"RMJ Diamond Blue 2",hex:"#80c9fc",ior:2.6,transmission:.94,roughness:0,category:"diamond",dispersion:.013,absorptionFactor:1,boostFactors:[.9,1,1],source:"app.rendermyjewelry.com",thumbnail:"/thumbnails/rmj-diamond-blue-2.svg"},{id:"diamond",label:"White Diamond",hex:"#ffffff",ior:2.417,transmission:.95,roughness:.02,dispersion:.012,absorptionFactor:0,ambientFloor:.05,thumbnail:"/thumbnails/diamond-d.svg"},{id:"yellow-diamond",label:"Yellow Diamond",hex:"#fcd34d",ior:2.42,transmission:.94,roughness:.02,dispersion:.01,absorptionFactor:.11,thumbnail:"/thumbnails/diamond-fancy-yellow.svg"},{id:"pink-diamond",label:"Pink Diamond",hex:"#f472b6",ior:2.42,transmission:.94,roughness:.02,dispersion:.01,absorptionFactor:.14,thumbnail:"/thumbnails/diamond-fancy-pink.svg"},{id:"blue-diamond",label:"Blue Diamond",hex:"#60a5fa",ior:2.42,transmission:.94,roughness:.02,dispersion:.01,absorptionFactor:.14,thumbnail:"/thumbnails/diamond-fancy-blue.svg"},{id:"black-diamond",label:"Black Diamond",hex:"#1a1a1a",ior:2.42,transmission:0,roughness:.05,dispersion:0,absorptionFactor:0,thumbnail:"/thumbnails/diamond-fancy-black.svg"}],vn=Se,bn=de,To=Se.filter(n=>n.category!==void 0&&!n.id.startsWith("rmj-"));Se.filter(n=>n.id.startsWith("rmj-"));const Co=de.filter(n=>n.category==="diamond"&&!n.id.startsWith("rmj-")&&n.id.startsWith("diamond-")),Bo=de.filter(n=>n.category==="gemstone"&&!n.id.startsWith("rmj-"));de.filter(n=>n.id.startsWith("rmj-"));const Po=new Set(["yellow-gold","rose-gold","white-gold","platinum","silver"]),Io=new Set(["diamond","yellow-diamond","pink-diamond","blue-diamond","black-diamond"]),Ro=Se.filter(n=>!Po.has(n.id)),Eo=de.filter(n=>!Io.has(n.id));function Fo(n){if(!n)return;const t=n.toLowerCase().trim();return Se.find(e=>e.id.toLowerCase()===t||e.hex.toLowerCase()===t)}function _o(n){if(!n)return;const t=n.toLowerCase().trim();return de.find(e=>e.id.toLowerCase()===t||e.hex.toLowerCase()===t)}const Un=new WeakMap;function Do(n){var g,x;n.boundingSphere||n.computeBoundingSphere();const t=(g=n.boundingSphere)!=null&&g.center?n.boundingSphere.center.clone():new v.Vector3,e=((x=n.boundingSphere)==null?void 0:x.radius)??1,i=Math.max(e,.01),o=n.getAttribute("position");if(!o||o.count===0)return{centroid:t,radius:i,triangleCount:0,starConvexFailureArea:1,isBakeable:!1,flippedWinding:!1};const r=n.getIndex(),s=r?r.count/3:o.count/3;let a=0,c=0,u=0;const f=new v.Vector3,d=new v.Vector3,l=new v.Vector3,m=new v.Vector3,p=new v.Vector3,w=new v.Vector3,y=new v.Vector3;for(let b=0;b<s;b++){const A=r?r.getX(b*3):b*3,S=r?r.getX(b*3+1):b*3+1,P=r?r.getX(b*3+2):b*3+2;f.fromBufferAttribute(o,A).sub(t).divideScalar(i),d.fromBufferAttribute(o,S).sub(t).divideScalar(i),l.fromBufferAttribute(o,P).sub(t).divideScalar(i),m.subVectors(d,f),p.subVectors(l,f),w.crossVectors(m,p);const E=w.length();if(E<1e-12)continue;const T=E*.5;a+=T,w.divideScalar(E);const R=f.dot(w);y.crossVectors(d,l),u+=f.dot(y)/6,R<=1e-4&&(c+=T)}const M=a>0?c/a:1,h=u<0;return{centroid:t,radius:i,triangleCount:s,starConvexFailureArea:M,isBakeable:M<=.005&&!isNaN(a)&&a>0,flippedWinding:h}}function No(n,t){const e=n.getIndex()!==null,i=e?n.toNonIndexed():n;try{const o=i.getAttribute("position");if(!o)return new Float32Array(0);const r=o.count,s=new Float32Array(r*4),a=new v.Vector3,c=new v.Vector3,u=new v.Vector3,f=new v.Vector3,d=new v.Vector3,l=new v.Vector3,m=t.flippedWinding?-1:1;for(let p=0;p<r;p+=3){a.fromBufferAttribute(o,p).sub(t.centroid).divideScalar(t.radius),c.fromBufferAttribute(o,p+1).sub(t.centroid).divideScalar(t.radius),u.fromBufferAttribute(o,p+2).sub(t.centroid).divideScalar(t.radius),f.subVectors(c,a),d.subVectors(u,a),l.crossVectors(f,d).multiplyScalar(m);const w=l.length();w>1e-12?l.divideScalar(w):l.set(0,1,0);const y=a.dot(l);for(let M=0;M<3;M++){const h=(p+M)*4;s[h]=l.x,s[h+1]=l.y,s[h+2]=l.z,s[h+3]=y}}return s}finally{e&&i.dispose()}}function Lo(n,t){const e=Un.get(n);if(e)return e;const i=n.getAttribute("position"),o=n.getIndex();let r=2166136261;const s=u=>{r^=u&255,r=Math.imul(r,16777619),r^=u>>8&255,r=Math.imul(r,16777619),r^=u>>16&255,r=Math.imul(r,16777619),r^=u>>24&255,r=Math.imul(r,16777619)};if(s(t.triangleCount),o)for(let u=0;u<o.count;u++)s(o.getX(u));const a=new v.Vector3;for(let u=0;u<i.count;u++)a.fromBufferAttribute(i,u).sub(t.centroid).divideScalar(t.radius),s(Math.round(a.x*1e4)),s(Math.round(a.y*1e4)),s(Math.round(a.z*1e4));const c=(r>>>0).toString(16);return Un.set(n,c),c}const hi=0,Vo=1,yi=2,Gn=2,qt=1.25,On=1,te=6*4+4+4,J=te/4,xn=65535,zo=xn<<16,Ct=Math.pow(2,-24),wn=Symbol("SKIP_GENERATION"),gi={strategy:hi,maxDepth:40,targetLeafSize:10,useSharedArrayBuffer:!1,setBoundingBox:!0,onProgress:null,indirect:!1,verbose:!0,range:null,[wn]:!1};function W(n,t,e){return e.min.x=t[n],e.min.y=t[n+1],e.min.z=t[n+2],e.max.x=t[n+3],e.max.y=t[n+4],e.max.z=t[n+5],e}function nn(n){let t=-1,e=-1/0;for(let i=0;i<3;i++){const o=n[i+3]-n[i];o>e&&(e=o,t=i)}return t}function Hn(n,t){t.set(n)}function qn(n,t,e){let i,o;for(let r=0;r<3;r++){const s=r+3;i=n[r],o=t[r],e[r]=i<o?i:o,i=n[s],o=t[s],e[s]=i>o?i:o}}function ut(n,t,e){for(let i=0;i<3;i++){const o=t[n+2*i],r=t[n+2*i+1],s=o-r,a=o+r;s<e[i]&&(e[i]=s),a>e[i+3]&&(e[i+3]=a)}}function je(n){const t=n[3]-n[0],e=n[4]-n[1],i=n[5]-n[2];return 2*(t*e+e*i+i*t)}function Y(n,t){return t[n+15]===xn}function Z(n,t){return t[n+6]}function ie(n,t){return t[n+14]}function Q(n){return n+J}function ee(n,t){const e=t[n+6];return n+e*J}function Dt(n,t){return t[n+7]}function $t(n,t,e,i,o){let r=1/0,s=1/0,a=1/0,c=-1/0,u=-1/0,f=-1/0,d=1/0,l=1/0,m=1/0,p=-1/0,w=-1/0,y=-1/0;const M=n.offset||0;for(let h=(t-M)*6,g=(t+e-M)*6;h<g;h+=6){const x=n[h+0],b=n[h+1],A=x-b,S=x+b;A<r&&(r=A),S>c&&(c=S),x<d&&(d=x),x>p&&(p=x);const P=n[h+2],E=n[h+3],T=P-E,R=P+E;T<s&&(s=T),R>u&&(u=R),P<l&&(l=P),P>w&&(w=P);const B=n[h+4],F=n[h+5],_=B-F,V=B+F;_<a&&(a=_),V>f&&(f=V),B<m&&(m=B),B>y&&(y=B)}i[0]=r,i[1]=s,i[2]=a,i[3]=c,i[4]=u,i[5]=f,o[0]=d,o[1]=l,o[2]=m,o[3]=p,o[4]=w,o[5]=y}const xe=32,ko=(n,t)=>n.candidate-t.candidate,Be=new Array(xe).fill().map(()=>({count:0,bounds:new Float32Array(6),rightCacheBounds:new Float32Array(6),leftCacheBounds:new Float32Array(6),candidate:0})),ft=new Float32Array(6);function Uo(n,t,e,i,o,r){let s=-1,a=0;if(r===hi)s=nn(t),s!==-1&&(a=(t[s]+t[s+3])/2);else if(r===Vo)s=nn(n),s!==-1&&(a=Go(e,i,o,s));else if(r===yi){const c=je(n);let u=qt*o;const f=e.offset||0,d=(i-f)*6,l=(i+o-f)*6;for(let m=0;m<3;m++){const p=t[m],M=(t[m+3]-p)/xe;if(o<xe/4){const h=[...Be];h.length=o;let g=0;for(let b=d;b<l;b+=6,g++){const A=h[g];A.candidate=e[b+2*m],A.count=0;const{bounds:S,leftCacheBounds:P,rightCacheBounds:E}=A;for(let T=0;T<3;T++)E[T]=1/0,E[T+3]=-1/0,P[T]=1/0,P[T+3]=-1/0,S[T]=1/0,S[T+3]=-1/0;ut(b,e,S)}h.sort(ko);let x=o;for(let b=0;b<x;b++){const A=h[b];for(;b+1<x&&h[b+1].candidate===A.candidate;)h.splice(b+1,1),x--}for(let b=d;b<l;b+=6){const A=e[b+2*m];for(let S=0;S<x;S++){const P=h[S];A>=P.candidate?ut(b,e,P.rightCacheBounds):(ut(b,e,P.leftCacheBounds),P.count++)}}for(let b=0;b<x;b++){const A=h[b],S=A.count,P=o-A.count,E=A.leftCacheBounds,T=A.rightCacheBounds;let R=0;S!==0&&(R=je(E)/c);let B=0;P!==0&&(B=je(T)/c);const F=On+qt*(R*S+B*P);F<u&&(s=m,u=F,a=A.candidate)}}else{for(let x=0;x<xe;x++){const b=Be[x];b.count=0,b.candidate=p+M+x*M;const A=b.bounds;for(let S=0;S<3;S++)A[S]=1/0,A[S+3]=-1/0}for(let x=d;x<l;x+=6){let S=~~((e[x+2*m]-p)/M);S>=xe&&(S=xe-1);const P=Be[S];P.count++,ut(x,e,P.bounds)}const h=Be[xe-1];Hn(h.bounds,h.rightCacheBounds);for(let x=xe-2;x>=0;x--){const b=Be[x],A=Be[x+1];qn(b.bounds,A.rightCacheBounds,b.rightCacheBounds)}let g=0;for(let x=0;x<xe-1;x++){const b=Be[x],A=b.count,S=b.bounds,E=Be[x+1].rightCacheBounds;A!==0&&(g===0?Hn(S,ft):qn(S,ft,ft)),g+=A;let T=0,R=0;g!==0&&(T=je(ft)/c);const B=o-g;B!==0&&(R=je(E)/c);const F=On+qt*(T*g+R*B);F<u&&(s=m,u=F,a=b.candidate)}}}}else console.warn(`BVH: Invalid build strategy value ${r} used.`);return{axis:s,pos:a}}function Go(n,t,e,i){let o=0;const r=n.offset;for(let s=t,a=t+e;s<a;s++)o+=n[(s-r)*6+i*2];return o/e}class Wt{constructor(){this.boundingData=new Float32Array(6)}}function Oo(n,t,e,i,o,r){let s=i,a=i+o-1;const c=r.pos,u=r.axis*2,f=e.offset||0;for(;;){for(;s<=a&&e[(s-f)*6+u]<c;)s++;for(;s<=a&&e[(a-f)*6+u]>=c;)a--;if(s<a){for(let d=0;d<t;d++){let l=n[s*t+d];n[s*t+d]=n[a*t+d],n[a*t+d]=l}for(let d=0;d<6;d++){const l=s-f,m=a-f,p=e[l*6+d];e[l*6+d]=e[m*6+d],e[m*6+d]=p}s++,a--}else return s}}let vi,Bt,on,bi;const Ho=Math.pow(2,32);function rn(n){return"count"in n?1:1+rn(n.left)+rn(n.right)}function qo(n,t,e){return vi=new Float32Array(e),Bt=new Uint32Array(e),on=new Uint16Array(e),bi=new Uint8Array(e),sn(n,t)}function sn(n,t){const e=n/4,i=n/2,o="count"in t,r=t.boundingData;for(let s=0;s<6;s++)vi[e+s]=r[s];if(o)return t.buffer?(bi.set(new Uint8Array(t.buffer),n),n+t.buffer.byteLength):(Bt[e+6]=t.offset,on[i+14]=t.count,on[i+15]=xn,n+te);{const{left:s,right:a,splitAxis:c}=t,u=n+te;let f=sn(u,s);const d=n/te,m=f/te-d;if(m>Ho)throw new Error("MeshBVH: Cannot store relative child node offset greater than 32 bits.");return Bt[e+6]=m,Bt[e+7]=c,sn(f,a)}}function $o(n,t,e,i,o,r){const{maxDepth:s,verbose:a,targetLeafSize:c,_strictLeafSize:u=1/0,strategy:f,onProgress:d}=o,l=n.primitiveBuffer,m=n.primitiveBufferStride,p=new Float32Array(6);let w=!1;const y=new Wt;return $t(t,e,i,y.boundingData,p),h(y,e,i,p),y;function M(g){d&&d((g-r.offset)/r.count)}function h(g,x,b,A=null,S=0){!w&&S>=s&&(w=!0,a&&console.warn(`BVH: Max depth of ${s} reached when generating BVH. Consider increasing maxDepth.`));const P=b>u;if(b<=c&&!P||S>=s)return M(x+b),g.offset=x,g.count=b,g;const E=Uo(g.boundingData,A,t,x,b,f);let T=E.axis===-1?-1:Oo(l,m,t,x,b,E);if(E.axis===-1||T===x||T===x+b){if(!P)return M(x+b),g.offset=x,g.count=b,g;E.axis=Math.max(0,nn(g.boundingData)),T=x+Math.max(1,Math.floor(b/2))}g.splitAxis=E.axis;const R=new Wt,B=x,F=T-x;g.left=R,$t(t,B,F,R.boundingData,p),h(R,B,F,p,S+1);const _=new Wt,V=T,U=b-F;return g.right=_,$t(t,V,U,_.boundingData,p),h(_,V,U,p,S+1),g}}function Wo(n,t){const e=t.useSharedArrayBuffer?SharedArrayBuffer:ArrayBuffer,i=n.getRootRanges(t.range),o=i[0],r=i[i.length-1],s={offset:o.offset,count:r.offset+r.count-o.offset},a=new Float32Array(6*s.count);a.offset=s.offset,n.computePrimitiveBounds(s.offset,s.count,a),n._roots=i.map(c=>{const u=$o(n,a,c.offset,c.count,t,s),f=rn(u),d=new e(te*f);return qo(0,u,d),d})}class Mn{constructor(t){this._getNewPrimitive=t,this._primitives=[]}getPrimitive(){const t=this._primitives;return t.length===0?this._getNewPrimitive():t.pop()}releasePrimitive(t){this._primitives.push(t)}}class Yo{constructor(){this.float32Array=null,this.uint16Array=null,this.uint32Array=null;const t=[];let e=null;this.setBuffer=i=>{e&&t.push(e),e=i,this.float32Array=new Float32Array(i),this.uint16Array=new Uint16Array(i),this.uint32Array=new Uint32Array(i)},this.clearBuffer=()=>{e=null,this.float32Array=null,this.uint16Array=null,this.uint32Array=null,t.length!==0&&this.setBuffer(t.pop())}}}const H=new Yo;let Ie,Ye;const Ve=[],dt=new Mn(()=>new C.Box3);function Xo(n,t,e,i,o,r){Ie=dt.getPrimitive(),Ye=dt.getPrimitive(),Ve.push(Ie,Ye),H.setBuffer(n._roots[t]);const s=an(0,n.geometry,e,i,o,r);H.clearBuffer(),dt.releasePrimitive(Ie),dt.releasePrimitive(Ye),Ve.pop(),Ve.pop();const a=Ve.length;return a>0&&(Ye=Ve[a-1],Ie=Ve[a-2]),s}function an(n,t,e,i,o=null,r=0,s=0){const{float32Array:a,uint16Array:c,uint32Array:u}=H;let f=n*2;if(Y(f,c)){const l=Z(n,u),m=ie(f,c);return W(n,a,Ie),i(l,m,!1,s,r+n/J,Ie)}else{let T=function(B){const{uint16Array:F,uint32Array:_}=H;let V=B*2;for(;!Y(V,F);)B=Q(B),V=B*2;return Z(B,_)},R=function(B){const{uint16Array:F,uint32Array:_}=H;let V=B*2;for(;!Y(V,F);)B=ee(B,_),V=B*2;return Z(B,_)+ie(V,F)};const l=Q(n),m=ee(n,u);let p=l,w=m,y,M,h,g;if(o&&(h=Ie,g=Ye,W(p,a,h),W(w,a,g),y=o(h),M=o(g),M<y)){p=m,w=l;const B=y;y=M,M=B,h=g}h||(h=Ie,W(p,a,h));const x=Y(p*2,c),b=e(h,x,y,s+1,r+p/J);let A;if(b===Gn){const B=T(p),_=R(p)-B;A=i(B,_,!0,s+1,r+p/J,h)}else A=b&&an(p,t,e,i,o,r,s+1);if(A)return!0;g=Ye,W(w,a,g);const S=Y(w*2,c),P=e(g,S,M,s+1,r+w/J);let E;if(P===Gn){const B=T(w),_=R(w)-B;E=i(B,_,!0,s+1,r+w/J,g)}else E=P&&an(w,t,e,i,o,r,s+1);return!!E}}const it=new H.constructor,Pt=new H.constructor,Pe=new Mn(()=>new C.Box3),ze=new C.Box3,ke=new C.Box3,Yt=new C.Box3,Xt=new C.Box3;let jt=!1;function jo(n,t,e,i){if(jt)throw new Error("MeshBVH: Recursive calls to bvhcast not supported.");jt=!0;const o=n._roots,r=t._roots;let s,a=0,c=0;const u=new C.Matrix4().copy(e).invert();for(let f=0,d=o.length;f<d;f++){it.setBuffer(o[f]),c=0;const l=Pe.getPrimitive();W(0,it.float32Array,l),l.applyMatrix4(u);for(let m=0,p=r.length;m<p&&(Pt.setBuffer(r[m]),s=pe(0,0,e,u,i,a,c,0,0,l),Pt.clearBuffer(),c+=r[m].byteLength/te,!s);m++);if(Pe.releasePrimitive(l),it.clearBuffer(),a+=o[f].byteLength/te,s)break}return jt=!1,s}function pe(n,t,e,i,o,r=0,s=0,a=0,c=0,u=null,f=!1){let d,l;f?(d=Pt,l=it):(d=it,l=Pt);const m=d.float32Array,p=d.uint32Array,w=d.uint16Array,y=l.float32Array,M=l.uint32Array,h=l.uint16Array,g=n*2,x=t*2,b=Y(g,w),A=Y(x,h);let S=!1;if(A&&b)f?S=o(Z(t,M),ie(t*2,h),Z(n,p),ie(n*2,w),c,s+t/J,a,r+n/J):S=o(Z(n,p),ie(n*2,w),Z(t,M),ie(t*2,h),a,r+n/J,c,s+t/J);else if(A){const P=Pe.getPrimitive();W(t,y,P),P.applyMatrix4(e);const E=Q(n),T=ee(n,p);W(E,m,ze),W(T,m,ke);const R=P.intersectsBox(ze),B=P.intersectsBox(ke);S=R&&pe(t,E,i,e,o,s,r,c,a+1,P,!f)||B&&pe(t,T,i,e,o,s,r,c,a+1,P,!f),Pe.releasePrimitive(P)}else{const P=Q(t),E=ee(t,M);W(P,y,Yt),W(E,y,Xt);const T=u.intersectsBox(Yt),R=u.intersectsBox(Xt);if(T&&R)S=pe(n,P,e,i,o,r,s,a,c+1,u,f)||pe(n,E,e,i,o,r,s,a,c+1,u,f);else if(T)if(b)S=pe(n,P,e,i,o,r,s,a,c+1,u,f);else{const B=Pe.getPrimitive();B.copy(Yt).applyMatrix4(e);const F=Q(n),_=ee(n,p);W(F,m,ze),W(_,m,ke);const V=B.intersectsBox(ze),U=B.intersectsBox(ke);S=V&&pe(P,F,i,e,o,s,r,c,a+1,B,!f)||U&&pe(P,_,i,e,o,s,r,c,a+1,B,!f),Pe.releasePrimitive(B)}else if(R)if(b)S=pe(n,E,e,i,o,r,s,a,c+1,u,f);else{const B=Pe.getPrimitive();B.copy(Xt).applyMatrix4(e);const F=Q(n),_=ee(n,p);W(F,m,ze),W(_,m,ke);const V=B.intersectsBox(ze),U=B.intersectsBox(ke);S=V&&pe(E,F,i,e,o,s,r,c,a+1,B,!f)||U&&pe(E,_,i,e,o,s,r,c,a+1,B,!f),Pe.releasePrimitive(B)}}return S}const Kt=new class{constructor(){let n=null,t=null,e=null,i=!1;this.root=null,this.buffer=null,this.uint32Array=null,this.uint16Array=null,this.setBVH=(r,s)=>{if(i)throw new Error("BVHTraversalHelper: cannot call setBVH during an active traversal.");this.root=s,this.buffer=n=r._roots[s],this.uint16Array=e=new Uint16Array(n),this.uint32Array=t=new Uint32Array(n)},this.reset=()=>{this.root=null,this.buffer=n=null,this.uint16Array=e=null,this.uint32Array=t=null},this.getRangeStart=r=>{let s=r*2;for(;!Y(s,e);)r=Q(r),s=r*2;return Z(r,t)},this.getRangeEnd=r=>{let s=r*2;for(;!Y(s,e);)r=ee(r,t),s=r*2;return Z(r,t)+ie(s,e)};const o=(r,s,a)=>{const c=s*2,u=Y(c,e);if(!r(a,u,s)&&!u){const d=Q(s),l=ee(s,t);o(r,d,a+1),o(r,l,a+1)}};this.traverseBuffer=r=>{if(i)throw new Error("BVHTraversalHelper: cannot start a traversal during an active traversal.");i=!0;try{o(r,0,0)}finally{i=!1}},this.traverse=r=>{this.traverseBuffer((s,a,c)=>{if(a){const u=c*2,f=t[c+6],d=e[u+14];return r(s,a,new Float32Array(n,c*4,6),f,d)}else{const u=Dt(c,t);return r(s,a,new Float32Array(n,c*4,6),u)}})}}},$n=new C.Box3,Ue=new Float32Array(6);class Ko{constructor(){this._roots=null,this.primitiveBuffer=null,this.primitiveBufferStride=null}init(t){t={...gi,...t},"maxLeafSize"in t&&(console.warn('BVH: "maxLeafSize" option has been deprecated. Use "targetLeafSize", instead.'),t={...t,targetLeafSize:t.maxLeafSize}),Wo(this,t)}getRootRanges(){throw new Error("BVH: getRootRanges() not implemented")}writePrimitiveBounds(){throw new Error("BVH: writePrimitiveBounds() not implemented")}writePrimitiveRangeBounds(t,e,i,o){let r=1/0,s=1/0,a=1/0,c=-1/0,u=-1/0,f=-1/0;for(let d=t,l=t+e;d<l;d++){this.writePrimitiveBounds(d,Ue,0);const[m,p,w,y,M,h]=Ue;m<r&&(r=m),y>c&&(c=y),p<s&&(s=p),M>u&&(u=M),w<a&&(a=w),h>f&&(f=h)}return i[o+0]=r,i[o+1]=s,i[o+2]=a,i[o+3]=c,i[o+4]=u,i[o+5]=f,i}computePrimitiveBounds(t,e,i){const o=i.offset||0;for(let r=t,s=t+e;r<s;r++){this.writePrimitiveBounds(r,Ue,0);const[a,c,u,f,d,l]=Ue,m=(a+f)/2,p=(c+d)/2,w=(u+l)/2,y=(f-a)/2,M=(d-c)/2,h=(l-u)/2,g=(r-o)*6;i[g+0]=m,i[g+1]=y+(Math.abs(m)+y)*Ct,i[g+2]=p,i[g+3]=M+(Math.abs(p)+M)*Ct,i[g+4]=w,i[g+5]=h+(Math.abs(w)+h)*Ct}return i}shiftPrimitiveOffsets(t){const e=this._indirectBuffer;if(e)for(let i=0,o=e.length;i<o;i++)e[i]+=t;else{const i=this._roots;for(let o=0;o<i.length;o++){const r=i[o],s=new Uint32Array(r),a=new Uint16Array(r),c=r.byteLength/te;for(let u=0;u<c;u++){const f=J*u,d=2*f;Y(d,a)&&(s[f+6]+=t)}}}}traverse(t,e=0){Kt.setBVH(this,e),Kt.traverse(t),Kt.reset()}refit(){const t=this._roots;for(let e=0,i=t.length;e<i;e++){const o=t[e],r=new Uint32Array(o),s=new Uint16Array(o),a=new Float32Array(o),c=o.byteLength/te;for(let u=c-1;u>=0;u--){const f=u*J,d=f*2;if(Y(d,s)){const m=Z(f,r),p=ie(d,s);this.writePrimitiveRangeBounds(m,p,Ue,0),a.set(Ue,f)}else{const m=Q(f),p=ee(f,r);for(let w=0;w<3;w++){const y=a[m+w],M=a[m+w+3],h=a[p+w],g=a[p+w+3];a[f+w]=y<h?y:h,a[f+w+3]=M>g?M:g}}}}}getBoundingBox(t){return t.makeEmpty(),this._roots.forEach(i=>{W(0,new Float32Array(i),$n),t.union($n)}),t}shapecast(t){let{boundsTraverseOrder:e,intersectsBounds:i,intersectsRange:o,intersectsPrimitive:r,scratchPrimitive:s,iterate:a}=t;if(o&&r){const d=o;o=(l,m,p,w,y)=>d(l,m,p,w,y)?!0:a(l,m,this,r,p,w,s)}else o||(r?o=(d,l,m,p)=>a(d,l,this,r,m,p,s):o=(d,l,m)=>m);let c=!1,u=0;const f=this._roots;for(let d=0,l=f.length;d<l;d++){const m=f[d];if(c=Xo(this,d,i,o,e,u),c)break;u+=m.byteLength/te}return c}bvhcast(t,e,i){let{intersectsRanges:o}=i;return jo(this,t,e,o)}}function Jo(){return typeof SharedArrayBuffer<"u"}function Nt(n){return n.index?n.index.count:n.attributes.position.count}function Lt(n){return Nt(n)/3}function xi(n,t=ArrayBuffer){return n>65535?new Uint32Array(new t(4*n)):new Uint16Array(new t(2*n))}function Zo(n,t){if(!n.index){const e=n.attributes.position.count,i=t.useSharedArrayBuffer?SharedArrayBuffer:ArrayBuffer,o=xi(e,i);n.setIndex(new C.BufferAttribute(o,1));for(let r=0;r<e;r++)o[r]=r}}function Qo(n,t,e){const i=Nt(n)/e,o=t||n.drawRange,r=o.start/e,s=(o.start+o.count)/e,a=Math.max(0,r),c=Math.min(i,s)-a;return{offset:Math.floor(a),count:Math.floor(c)}}function er(n,t){return n.groups.map(e=>({offset:e.start/t,count:e.count/t}))}function Wn(n,t,e){const i=Qo(n,t,e),o=er(n,e);if(!o.length)return[i];const r=[],s=i.offset,a=i.offset+i.count,c=Nt(n)/e,u=[];for(const l of o){const{offset:m,count:p}=l,w=m,y=isFinite(p)?p:c-m,M=m+y;w<a&&M>s&&(u.push({pos:Math.max(s,w),isStart:!0}),u.push({pos:Math.min(a,M),isStart:!1}))}u.sort((l,m)=>l.pos!==m.pos?l.pos-m.pos:l.type==="end"?-1:1);let f=0,d=null;for(const l of u){const m=l.pos;f!==0&&m!==d&&r.push({offset:d,count:m-d}),f+=l.isStart?1:-1,d=m}return r}function tr(n,t){const e=n[n.length-1],i=e.offset+e.count>2**16,o=n.reduce((u,f)=>u+f.count,0),r=i?4:2,s=t?new SharedArrayBuffer(o*r):new ArrayBuffer(o*r),a=i?new Uint32Array(s):new Uint16Array(s);let c=0;for(let u=0;u<n.length;u++){const{offset:f,count:d}=n[u];for(let l=0;l<d;l++)a[c+l]=f+l;c+=d}return a}class nr extends Ko{get indirect(){return!!this._indirectBuffer}get primitiveStride(){return null}get primitiveBufferStride(){return this.indirect?1:this.primitiveStride}set primitiveBufferStride(t){}get primitiveBuffer(){return this.indirect?this._indirectBuffer:this.geometry.index.array}set primitiveBuffer(t){}constructor(t,e={}){if(t.isBufferGeometry){if(t.index&&t.index.isInterleavedBufferAttribute)throw new Error("BVH: InterleavedBufferAttribute is not supported for the index attribute.")}else throw new Error("BVH: Only BufferGeometries are supported.");if(e.useSharedArrayBuffer&&!Jo())throw new Error("BVH: SharedArrayBuffer is not available.");super(),this.geometry=t,this.resolvePrimitiveIndex=e.indirect?i=>this._indirectBuffer[i]:i=>i,this.primitiveBuffer=null,this.primitiveBufferStride=null,this._indirectBuffer=null,e={...gi,...e},e[wn]||this.init(e)}init(t){const{geometry:e,primitiveStride:i}=this;if(t.indirect){const o=Wn(e,t.range,i),r=tr(o,t.useSharedArrayBuffer);this._indirectBuffer=r}else Zo(e,t);super.init(t),!e.boundingBox&&t.setBoundingBox&&(e.boundingBox=this.getBoundingBox(new C.Box3))}getRootRanges(t){return this.indirect?[{offset:0,count:this._indirectBuffer.length}]:Wn(this.geometry,t,this.primitiveStride)}raycastObject3D(){throw new Error("BVH: raycastObject3D() not implemented")}}class Te{constructor(){this.min=1/0,this.max=-1/0}setFromPointsField(t,e){let i=1/0,o=-1/0;for(let r=0,s=t.length;r<s;r++){const c=t[r][e];i=c<i?c:i,o=c>o?c:o}this.min=i,this.max=o}setFromPoints(t,e){let i=1/0,o=-1/0;for(let r=0,s=e.length;r<s;r++){const a=e[r],c=t.dot(a);i=c<i?c:i,o=c>o?c:o}this.min=i,this.max=o}isSeparated(t){return this.min>t.max||t.min>this.max}}Te.prototype.setFromBox=function(){const n=new C.Vector3;return function(e,i){const o=i.min,r=i.max;let s=1/0,a=-1/0;for(let c=0;c<=1;c++)for(let u=0;u<=1;u++)for(let f=0;f<=1;f++){n.x=o.x*c+r.x*(1-c),n.y=o.y*u+r.y*(1-u),n.z=o.z*f+r.z*(1-f);const d=e.dot(n);s=Math.min(d,s),a=Math.max(d,a)}this.min=s,this.max=a}}();const ir=function(){const n=new C.Vector3,t=new C.Vector3,e=new C.Vector3;return function(o,r,s){const a=o.start,c=n,u=r.start,f=t;e.subVectors(a,u),n.subVectors(o.end,o.start),t.subVectors(r.end,r.start);const d=e.dot(f),l=f.dot(c),m=f.dot(f),p=e.dot(c),y=c.dot(c)*m-l*l;let M,h;y!==0?M=(d*l-p*m)/y:M=0,h=(d+M*l)/m,s.x=M,s.y=h}}(),An=function(){const n=new C.Vector2,t=new C.Vector3,e=new C.Vector3;return function(o,r,s,a){ir(o,r,n);let c=n.x,u=n.y;if(c>=0&&c<=1&&u>=0&&u<=1){o.at(c,s),r.at(u,a);return}else if(c>=0&&c<=1){u<0?r.at(0,a):r.at(1,a),o.closestPointToPoint(a,!0,s);return}else if(u>=0&&u<=1){c<0?o.at(0,s):o.at(1,s),r.closestPointToPoint(s,!0,a);return}else{let f;c<0?f=o.start:f=o.end;let d;u<0?d=r.start:d=r.end;const l=t,m=e;if(o.closestPointToPoint(d,!0,t),r.closestPointToPoint(f,!0,e),l.distanceToSquared(d)<=m.distanceToSquared(f)){s.copy(l),a.copy(d);return}else{s.copy(f),a.copy(m);return}}}}(),or=function(){const n=new C.Vector3,t=new C.Vector3,e=new C.Plane,i=new C.Line3;return function(r,s){const{radius:a,center:c}=r,{a:u,b:f,c:d}=s;if(i.start=u,i.end=f,i.closestPointToPoint(c,!0,n).distanceTo(c)<=a||(i.start=u,i.end=d,i.closestPointToPoint(c,!0,n).distanceTo(c)<=a)||(i.start=f,i.end=d,i.closestPointToPoint(c,!0,n).distanceTo(c)<=a))return!0;const w=s.getPlane(e);if(Math.abs(w.distanceToPoint(c))<=a){const M=w.projectPoint(c,t);if(s.containsPoint(M))return!0}return!1}}(),rr=["x","y","z"],we=1e-15,Yn=we*we;function ue(n){return Math.abs(n)<we}class he extends C.Triangle{constructor(...t){super(...t),this.isExtendedTriangle=!0,this.satAxes=new Array(4).fill().map(()=>new C.Vector3),this.satBounds=new Array(4).fill().map(()=>new Te),this.points=[this.a,this.b,this.c],this.plane=new C.Plane,this.isDegenerateIntoSegment=!1,this.isDegenerateIntoPoint=!1,this.degenerateSegment=new C.Line3,this.needsUpdate=!0}intersectsSphere(t){return or(t,this)}update(){const t=this.a,e=this.b,i=this.c,o=this.points,r=this.satAxes,s=this.satBounds,a=r[0],c=s[0];this.getNormal(a),c.setFromPoints(a,o);const u=r[1],f=s[1];u.subVectors(t,e),f.setFromPoints(u,o);const d=r[2],l=s[2];d.subVectors(e,i),l.setFromPoints(d,o);const m=r[3],p=s[3];m.subVectors(i,t),p.setFromPoints(m,o);const w=u.length(),y=d.length(),M=m.length();this.isDegenerateIntoPoint=!1,this.isDegenerateIntoSegment=!1,w<we?y<we||M<we?this.isDegenerateIntoPoint=!0:(this.isDegenerateIntoSegment=!0,this.degenerateSegment.start.copy(t),this.degenerateSegment.end.copy(i)):y<we?M<we?this.isDegenerateIntoPoint=!0:(this.isDegenerateIntoSegment=!0,this.degenerateSegment.start.copy(e),this.degenerateSegment.end.copy(t)):M<we&&(this.isDegenerateIntoSegment=!0,this.degenerateSegment.start.copy(i),this.degenerateSegment.end.copy(e)),this.plane.setFromNormalAndCoplanarPoint(a,t),this.needsUpdate=!1}}he.prototype.closestPointToSegment=function(){const n=new C.Vector3,t=new C.Vector3,e=new C.Line3;return function(o,r=null,s=null){const{start:a,end:c}=o,u=this.points;let f,d=1/0;for(let l=0;l<3;l++){const m=(l+1)%3;e.start.copy(u[l]),e.end.copy(u[m]),An(e,o,n,t),f=n.distanceToSquared(t),f<d&&(d=f,r&&r.copy(n),s&&s.copy(t))}return this.closestPointToPoint(a,n),f=a.distanceToSquared(n),f<d&&(d=f,r&&r.copy(n),s&&s.copy(a)),this.closestPointToPoint(c,n),f=c.distanceToSquared(n),f<d&&(d=f,r&&r.copy(n),s&&s.copy(c)),Math.sqrt(d)}}();he.prototype.intersectsTriangle=function(){const n=new he,t=new Te,e=new Te,i=new C.Vector3,o=new C.Vector3,r=new C.Vector3,s=new C.Vector3,a=new C.Line3,c=new C.Line3,u=new C.Vector3,f=new C.Vector2,d=new C.Vector2;function l(g,x,b,A){const S=i;!g.isDegenerateIntoPoint&&!g.isDegenerateIntoSegment?S.copy(g.plane.normal):S.copy(x.plane.normal);const P=g.satBounds,E=g.satAxes;for(let B=1;B<4;B++){const F=P[B],_=E[B];if(t.setFromPoints(_,x.points),F.isSeparated(t)||(s.copy(S).cross(_),t.setFromPoints(s,g.points),e.setFromPoints(s,x.points),t.isSeparated(e)))return!1}const T=x.satBounds,R=x.satAxes;for(let B=1;B<4;B++){const F=T[B],_=R[B];if(t.setFromPoints(_,g.points),F.isSeparated(t)||(s.crossVectors(S,_),t.setFromPoints(s,g.points),e.setFromPoints(s,x.points),t.isSeparated(e)))return!1}return b&&(A||console.warn("ExtendedTriangle.intersectsTriangle: Triangles are coplanar which does not support an output edge. Setting edge to 0, 0, 0."),b.start.set(0,0,0),b.end.set(0,0,0)),!0}function m(g,x,b,A,S,P,E,T,R,B,F){let _=E/(E-T);B.x=A+(S-A)*_,F.start.subVectors(x,g).multiplyScalar(_).add(g),_=E/(E-R),B.y=A+(P-A)*_,F.end.subVectors(b,g).multiplyScalar(_).add(g)}function p(g,x,b,A,S,P,E,T,R,B,F){if(S>0)m(g.c,g.a,g.b,A,x,b,R,E,T,B,F);else if(P>0)m(g.b,g.a,g.c,b,x,A,T,E,R,B,F);else if(T*R>0||E!=0)m(g.a,g.b,g.c,x,b,A,E,T,R,B,F);else if(T!=0)m(g.b,g.a,g.c,b,x,A,T,E,R,B,F);else if(R!=0)m(g.c,g.a,g.b,A,x,b,R,E,T,B,F);else return!0;return!1}function w(g,x,b,A){const S=x.degenerateSegment,P=g.plane.distanceToPoint(S.start),E=g.plane.distanceToPoint(S.end);return ue(P)?ue(E)?l(g,x,b,A):(b&&(b.start.copy(S.start),b.end.copy(S.start)),g.containsPoint(S.start)):ue(E)?(b&&(b.start.copy(S.end),b.end.copy(S.end)),g.containsPoint(S.end)):g.plane.intersectLine(S,i)!=null?(b&&(b.start.copy(i),b.end.copy(i)),g.containsPoint(i)):!1}function y(g,x,b){const A=x.a;return ue(g.plane.distanceToPoint(A))&&g.containsPoint(A)?(b&&(b.start.copy(A),b.end.copy(A)),!0):!1}function M(g,x,b){const A=g.degenerateSegment,S=x.a;return A.closestPointToPoint(S,!0,i),S.distanceToSquared(i)<Yn?(b&&(b.start.copy(S),b.end.copy(S)),!0):!1}function h(g,x,b,A){if(g.isDegenerateIntoSegment)if(x.isDegenerateIntoSegment){const S=g.degenerateSegment,P=x.degenerateSegment,E=o,T=r;S.delta(E),P.delta(T);const R=i.subVectors(P.start,S.start),B=E.x*T.y-E.y*T.x;if(ue(B))return!1;const F=(R.x*T.y-R.y*T.x)/B,_=-(E.x*R.y-E.y*R.x)/B;if(F<0||F>1||_<0||_>1)return!1;const V=S.start.z+E.z*F,U=P.start.z+T.z*_;return ue(V-U)?(b&&(b.start.copy(S.start).addScaledVector(E,F),b.end.copy(S.start).addScaledVector(E,F)),!0):!1}else return x.isDegenerateIntoPoint?M(g,x,b):w(x,g,b,A);else{if(g.isDegenerateIntoPoint)return x.isDegenerateIntoPoint?x.a.distanceToSquared(g.a)<Yn?(b&&(b.start.copy(g.a),b.end.copy(g.a)),!0):!1:x.isDegenerateIntoSegment?M(x,g,b):y(x,g,b);if(x.isDegenerateIntoPoint)return y(g,x,b);if(x.isDegenerateIntoSegment)return w(g,x,b,A)}}return function(x,b=null,A=!1){this.needsUpdate&&this.update(),x.isExtendedTriangle?x.needsUpdate&&x.update():(n.copy(x),n.update(),x=n);const S=h(this,x,b,A);if(S!==void 0)return S;const P=this.plane,E=x.plane;let T=E.distanceToPoint(this.a),R=E.distanceToPoint(this.b),B=E.distanceToPoint(this.c);ue(T)&&(T=0),ue(R)&&(R=0),ue(B)&&(B=0);const F=T*R,_=T*B;if(F>0&&_>0)return!1;let V=P.distanceToPoint(x.a),U=P.distanceToPoint(x.b),ne=P.distanceToPoint(x.c);ue(V)&&(V=0),ue(U)&&(U=0),ue(ne)&&(ne=0);const q=V*U,$=V*ne;if(q>0&&$>0)return!1;o.copy(P.normal),r.copy(E.normal);const G=o.cross(r);let X=0,se=Math.abs(G.x);const ce=Math.abs(G.y);ce>se&&(se=ce,X=1),Math.abs(G.z)>se&&(X=2);const oe=rr[X],ye=this.a[oe],zt=this.b[oe],ve=this.c[oe],kt=x.a[oe],rt=x.b[oe],st=x.c[oe];if(p(this,ye,zt,ve,F,_,T,R,B,f,a))return l(this,x,b,A);if(p(x,kt,rt,st,q,$,V,U,ne,d,c))return l(this,x,b,A);if(f.y<f.x){const Le=f.y;f.y=f.x,f.x=Le,u.copy(a.start),a.start.copy(a.end),a.end.copy(u)}if(d.y<d.x){const Le=d.y;d.y=d.x,d.x=Le,u.copy(c.start),c.start.copy(c.end),c.end.copy(u)}return f.y<d.x||d.y<f.x?!1:(b&&(d.x>f.x?b.start.copy(c.start):b.start.copy(a.start),d.y<f.y?b.end.copy(c.end):b.end.copy(a.end)),!0)}}();he.prototype.distanceToPoint=function(){const n=new C.Vector3;return function(e){return this.closestPointToPoint(e,n),e.distanceTo(n)}}();he.prototype.distanceToTriangle=function(){const n=new C.Vector3,t=new C.Vector3,e=["a","b","c"],i=new C.Line3,o=new C.Line3;return function(s,a=null,c=null){const u=a||c?i:null;if(this.intersectsTriangle(s,u,!0))return(a||c)&&(a&&u.getCenter(a),c&&u.getCenter(c)),0;let f=1/0;for(let d=0;d<3;d++){let l;const m=e[d],p=s[m];this.closestPointToPoint(p,n),l=p.distanceToSquared(n),l<f&&(f=l,a&&a.copy(n),c&&c.copy(p));const w=this[m];s.closestPointToPoint(w,n),l=w.distanceToSquared(n),l<f&&(f=l,a&&a.copy(w),c&&c.copy(n))}for(let d=0;d<3;d++){const l=e[d],m=e[(d+1)%3];i.set(this[l],this[m]);for(let p=0;p<3;p++){const w=e[p],y=e[(p+1)%3];o.set(s[w],s[y]),An(i,o,n,t);const M=n.distanceToSquared(t);M<f&&(f=M,a&&a.copy(n),c&&c.copy(t))}}return Math.sqrt(f)}}();class ae{constructor(t,e,i){this.isOrientedBox=!0,this.min=new C.Vector3,this.max=new C.Vector3,this.matrix=new C.Matrix4,this.invMatrix=new C.Matrix4,this.points=new Array(8).fill().map(()=>new C.Vector3),this.satAxes=new Array(3).fill().map(()=>new C.Vector3),this.satBounds=new Array(3).fill().map(()=>new Te),this.alignedSatBounds=new Array(3).fill().map(()=>new Te),this.needsUpdate=!1,t&&this.min.copy(t),e&&this.max.copy(e),i&&this.matrix.copy(i)}set(t,e,i){this.min.copy(t),this.max.copy(e),this.matrix.copy(i),this.needsUpdate=!0}copy(t){this.min.copy(t.min),this.max.copy(t.max),this.matrix.copy(t.matrix),this.needsUpdate=!0}}ae.prototype.update=function(){return function(){const t=this.matrix,e=this.min,i=this.max,o=this.points;for(let u=0;u<=1;u++)for(let f=0;f<=1;f++)for(let d=0;d<=1;d++){const l=1*u|2*f|4*d,m=o[l];m.x=u?i.x:e.x,m.y=f?i.y:e.y,m.z=d?i.z:e.z,m.applyMatrix4(t)}const r=this.satBounds,s=this.satAxes,a=o[0];for(let u=0;u<3;u++){const f=s[u],d=r[u],l=1<<u,m=o[l];f.subVectors(a,m),d.setFromPoints(f,o)}const c=this.alignedSatBounds;c[0].setFromPointsField(o,"x"),c[1].setFromPointsField(o,"y"),c[2].setFromPointsField(o,"z"),this.invMatrix.copy(this.matrix).invert(),this.needsUpdate=!1}}();ae.prototype.intersectsBox=function(){const n=new Te;return function(e){this.needsUpdate&&this.update();const i=e.min,o=e.max,r=this.satBounds,s=this.satAxes,a=this.alignedSatBounds;if(n.min=i.x,n.max=o.x,a[0].isSeparated(n)||(n.min=i.y,n.max=o.y,a[1].isSeparated(n))||(n.min=i.z,n.max=o.z,a[2].isSeparated(n)))return!1;for(let c=0;c<3;c++){const u=s[c],f=r[c];if(n.setFromBox(u,e),f.isSeparated(n))return!1}return!0}}();ae.prototype.intersectsTriangle=function(){const n=new he,t=new Array(3),e=new Te,i=new Te,o=new C.Vector3;return function(s){this.needsUpdate&&this.update(),s.isExtendedTriangle?s.needsUpdate&&s.update():(n.copy(s),n.update(),s=n);const a=this.satBounds,c=this.satAxes;t[0]=s.a,t[1]=s.b,t[2]=s.c;for(let l=0;l<3;l++){const m=a[l],p=c[l];if(e.setFromPoints(p,t),m.isSeparated(e))return!1}const u=s.satBounds,f=s.satAxes,d=this.points;for(let l=0;l<3;l++){const m=u[l],p=f[l];if(e.setFromPoints(p,d),m.isSeparated(e))return!1}for(let l=0;l<3;l++){const m=c[l];for(let p=0;p<4;p++){const w=f[p];if(o.crossVectors(m,w),e.setFromPoints(o,t),i.setFromPoints(o,d),e.isSeparated(i))return!1}}return!0}}();ae.prototype.closestPointToPoint=function(){return function(t,e){return this.needsUpdate&&this.update(),e.copy(t).applyMatrix4(this.invMatrix).clamp(this.min,this.max).applyMatrix4(this.matrix),e}}();ae.prototype.distanceToPoint=function(){const n=new C.Vector3;return function(e){return this.closestPointToPoint(e,n),e.distanceTo(n)}}();ae.prototype.distanceToBox=function(){const n=["x","y","z"],t=new Array(12).fill().map(()=>new C.Line3),e=new Array(12).fill().map(()=>new C.Line3),i=new C.Vector3,o=new C.Vector3;return function(s,a=0,c=null,u=null){if(this.needsUpdate&&this.update(),this.intersectsBox(s))return(c||u)&&(s.getCenter(o),this.closestPointToPoint(o,i),s.closestPointToPoint(i,o),c&&c.copy(i),u&&u.copy(o)),0;const f=a*a,d=s.min,l=s.max,m=this.points;let p=1/0;for(let y=0;y<8;y++){const M=m[y];o.copy(M).clamp(d,l);const h=M.distanceToSquared(o);if(h<p&&(p=h,c&&c.copy(M),u&&u.copy(o),h<f))return Math.sqrt(h)}let w=0;for(let y=0;y<3;y++)for(let M=0;M<=1;M++)for(let h=0;h<=1;h++){const g=(y+1)%3,x=(y+2)%3,b=M<<g|h<<x,A=1<<y|M<<g|h<<x,S=m[b],P=m[A];t[w].set(S,P);const T=n[y],R=n[g],B=n[x],F=e[w],_=F.start,V=F.end;_[T]=d[T],_[R]=M?d[R]:l[R],_[B]=h?d[B]:l[R],V[T]=l[T],V[R]=M?d[R]:l[R],V[B]=h?d[B]:l[R],w++}for(let y=0;y<=1;y++)for(let M=0;M<=1;M++)for(let h=0;h<=1;h++){o.x=y?l.x:d.x,o.y=M?l.y:d.y,o.z=h?l.z:d.z,this.closestPointToPoint(o,i);const g=o.distanceToSquared(i);if(g<p&&(p=g,c&&c.copy(i),u&&u.copy(o),g<f))return Math.sqrt(g)}for(let y=0;y<12;y++){const M=t[y];for(let h=0;h<12;h++){const g=e[h];An(M,g,i,o);const x=i.distanceToSquared(o);if(x<p&&(p=x,c&&c.copy(i),u&&u.copy(o),x<f))return Math.sqrt(x)}}return Math.sqrt(p)}}();class sr extends Mn{constructor(){super(()=>new he)}}const fe=new sr,Ke=new C.Vector3,Jt=new C.Vector3;function ar(n,t,e={},i=0,o=1/0){const r=i*i,s=o*o;let a=1/0,c=null;if(n.shapecast({boundsTraverseOrder:f=>(Ke.copy(t).clamp(f.min,f.max),Ke.distanceToSquared(t)),intersectsBounds:(f,d,l)=>l<a&&l<s,intersectsTriangle:(f,d)=>{f.closestPointToPoint(t,Ke);const l=t.distanceToSquared(Ke);return l<a&&(Jt.copy(Ke),a=l,c=d),l<r}}),a===1/0)return null;const u=Math.sqrt(a);return e.point?e.point.copy(Jt):e.point=Jt.clone(),e.distance=u,e.faceIndex=c,e}const mt=parseInt(C.REVISION)>=169,cr=parseInt(C.REVISION)<=161,_e=new C.Vector3,De=new C.Vector3,Ne=new C.Vector3,pt=new C.Vector2,ht=new C.Vector2,yt=new C.Vector2,Xn=new C.Vector3,jn=new C.Vector3,Kn=new C.Vector3,Je=new C.Vector3;function lr(n,t,e,i,o,r,s,a){let c;if(r===C.BackSide?c=n.intersectTriangle(i,e,t,!0,o):c=n.intersectTriangle(t,e,i,r!==C.DoubleSide,o),c===null)return null;const u=n.origin.distanceTo(o);return u<s||u>a?null:{distance:u,point:o.clone()}}function Jn(n,t,e,i,o,r,s,a,c,u,f){_e.fromBufferAttribute(t,r),De.fromBufferAttribute(t,s),Ne.fromBufferAttribute(t,a);const d=lr(n,_e,De,Ne,Je,c,u,f);if(d){if(i){pt.fromBufferAttribute(i,r),ht.fromBufferAttribute(i,s),yt.fromBufferAttribute(i,a),d.uv=new C.Vector2;const m=C.Triangle.getInterpolation(Je,_e,De,Ne,pt,ht,yt,d.uv);mt||(d.uv=m)}if(o){pt.fromBufferAttribute(o,r),ht.fromBufferAttribute(o,s),yt.fromBufferAttribute(o,a),d.uv1=new C.Vector2;const m=C.Triangle.getInterpolation(Je,_e,De,Ne,pt,ht,yt,d.uv1);mt||(d.uv1=m),cr&&(d.uv2=d.uv1)}if(e){Xn.fromBufferAttribute(e,r),jn.fromBufferAttribute(e,s),Kn.fromBufferAttribute(e,a),d.normal=new C.Vector3;const m=C.Triangle.getInterpolation(Je,_e,De,Ne,Xn,jn,Kn,d.normal);d.normal.dot(n.direction)>0&&d.normal.multiplyScalar(-1),mt||(d.normal=m)}const l={a:r,b:s,c:a,normal:new C.Vector3,materialIndex:0};if(C.Triangle.getNormal(_e,De,Ne,l.normal),d.face=l,d.faceIndex=r,mt){const m=new C.Vector3;C.Triangle.getBarycoord(Je,_e,De,Ne,m),d.barycoord=m}}return d}function Zn(n){return n&&n.isMaterial?n.side:n}function Vt(n,t,e,i,o,r,s){const a=i*3;let c=a+0,u=a+1,f=a+2;const{index:d,groups:l}=n;n.index&&(c=d.getX(c),u=d.getX(u),f=d.getX(f));const{position:m,normal:p,uv:w,uv1:y}=n.attributes;if(Array.isArray(t)){const M=i*3;for(let h=0,g=l.length;h<g;h++){const{start:x,count:b,materialIndex:A}=l[h];if(M>=x&&M<x+b){const S=Zn(t[A]),P=Jn(e,m,p,w,y,c,u,f,S,r,s);if(P)if(P.faceIndex=i,P.face.materialIndex=A,o)o.push(P);else return P}}}else{const M=Zn(t),h=Jn(e,m,p,w,y,c,u,f,M,r,s);if(h)if(h.faceIndex=i,h.face.materialIndex=0,o)o.push(h);else return h}return null}function K(n,t,e,i){const o=n.a,r=n.b,s=n.c;let a=t,c=t+1,u=t+2;e&&(a=e.getX(a),c=e.getX(c),u=e.getX(u)),o.x=i.getX(a),o.y=i.getY(a),o.z=i.getZ(a),r.x=i.getX(c),r.y=i.getY(c),r.z=i.getZ(c),s.x=i.getX(u),s.y=i.getY(u),s.z=i.getZ(u)}function ur(n,t,e,i,o,r,s,a){const{geometry:c,_indirectBuffer:u}=n;for(let f=i,d=i+o;f<d;f++)Vt(c,t,e,f,r,s,a)}function fr(n,t,e,i,o,r,s){const{geometry:a,_indirectBuffer:c}=n;let u=1/0,f=null;for(let d=i,l=i+o;d<l;d++){let m;m=Vt(a,t,e,d,null,r,s),m&&m.distance<u&&(f=m,u=m.distance)}return f}function dr(n,t,e,i,o,r,s){const{geometry:a}=e,{index:c}=a,u=a.attributes.position;for(let f=n,d=t+n;f<d;f++){let l;if(l=f,K(s,l*3,c,u),s.needsUpdate=!0,i(s,l,o,r))return!0}return!1}function mr(n,t=null){t&&Array.isArray(t)&&(t=new Set(t));const e=n.geometry,i=e.index?e.index.array:null,o=e.attributes.position;let r,s,a,c,u=0;const f=n._roots;for(let l=0,m=f.length;l<m;l++)r=f[l],s=new Uint32Array(r),a=new Uint16Array(r),c=new Float32Array(r),d(0,u),u+=r.byteLength;function d(l,m,p=!1){const w=l*2;if(Y(w,a)){const y=Z(l,s),M=ie(w,a);let h=1/0,g=1/0,x=1/0,b=-1/0,A=-1/0,S=-1/0;for(let P=3*y,E=3*(y+M);P<E;P++){let T=i[P];const R=o.getX(T),B=o.getY(T),F=o.getZ(T);R<h&&(h=R),R>b&&(b=R),B<g&&(g=B),B>A&&(A=B),F<x&&(x=F),F>S&&(S=F)}return c[l+0]!==h||c[l+1]!==g||c[l+2]!==x||c[l+3]!==b||c[l+4]!==A||c[l+5]!==S?(c[l+0]=h,c[l+1]=g,c[l+2]=x,c[l+3]=b,c[l+4]=A,c[l+5]=S,!0):!1}else{const y=Q(l),M=ee(l,s);let h=p,g=!1,x=!1;if(t){if(!h){const T=y/J+m/te,R=M/J+m/te;g=t.has(T),x=t.has(R),h=!g&&!x}}else g=!0,x=!0;const b=h||g,A=h||x;let S=!1;b&&(S=d(y,m,h));let P=!1;A&&(P=d(M,m,h));const E=S||P;if(E)for(let T=0;T<3;T++){const R=y+T,B=M+T,F=c[R],_=c[R+3],V=c[B],U=c[B+3];c[l+T]=F<V?F:V,c[l+T+3]=_>U?_:U}return E}}}function Re(n,t,e,i,o){let r,s,a,c,u,f;const d=1/e.direction.x,l=1/e.direction.y,m=1/e.direction.z,p=e.origin.x,w=e.origin.y,y=e.origin.z;let M=t[n],h=t[n+3],g=t[n+1],x=t[n+3+1],b=t[n+2],A=t[n+3+2];return d>=0?(r=(M-p)*d,s=(h-p)*d):(r=(h-p)*d,s=(M-p)*d),l>=0?(a=(g-w)*l,c=(x-w)*l):(a=(x-w)*l,c=(g-w)*l),r>c||a>s||((a>r||isNaN(r))&&(r=a),(c<s||isNaN(s))&&(s=c),m>=0?(u=(b-y)*m,f=(A-y)*m):(u=(A-y)*m,f=(b-y)*m),r>f||u>s)?!1:((u>r||r!==r)&&(r=u),(f<s||s!==s)&&(s=f),r<=o&&s>=i)}function pr(n,t,e,i,o,r,s,a){const{geometry:c,_indirectBuffer:u}=n;for(let f=i,d=i+o;f<d;f++){let l=u?u[f]:f;Vt(c,t,e,l,r,s,a)}}function hr(n,t,e,i,o,r,s){const{geometry:a,_indirectBuffer:c}=n;let u=1/0,f=null;for(let d=i,l=i+o;d<l;d++){let m;m=Vt(a,t,e,c?c[d]:d,null,r,s),m&&m.distance<u&&(f=m,u=m.distance)}return f}function yr(n,t,e,i,o,r,s){const{geometry:a}=e,{index:c}=a,u=a.attributes.position;for(let f=n,d=t+n;f<d;f++){let l;if(l=e.resolveTriangleIndex(f),K(s,l*3,c,u),s.needsUpdate=!0,i(s,l,o,r))return!0}return!1}function gr(n,t,e,i,o,r,s){H.setBuffer(n._roots[t]),cn(0,n,e,i,o,r,s),H.clearBuffer()}function cn(n,t,e,i,o,r,s){const{float32Array:a,uint16Array:c,uint32Array:u}=H,f=n*2;if(Y(f,c)){const l=Z(n,u),m=ie(f,c);ur(t,e,i,l,m,o,r,s)}else{const l=Q(n);Re(l,a,i,r,s)&&cn(l,t,e,i,o,r,s);const m=ee(n,u);Re(m,a,i,r,s)&&cn(m,t,e,i,o,r,s)}}const vr=["x","y","z"];function br(n,t,e,i,o,r){H.setBuffer(n._roots[t]);const s=ln(0,n,e,i,o,r);return H.clearBuffer(),s}function ln(n,t,e,i,o,r){const{float32Array:s,uint16Array:a,uint32Array:c}=H;let u=n*2;if(Y(u,a)){const d=Z(n,c),l=ie(u,a);return fr(t,e,i,d,l,o,r)}else{const d=Dt(n,c),l=vr[d],p=i.direction[l]>=0;let w,y;p?(w=Q(n),y=ee(n,c)):(w=ee(n,c),y=Q(n));const h=Re(w,s,i,o,r)?ln(w,t,e,i,o,r):null;if(h){const b=h.point[l];if(p?b<=s[y+d]:b>=s[y+d+3])return h}const x=Re(y,s,i,o,r)?ln(y,t,e,i,o,r):null;return h&&x?h.distance<=x.distance?h:x:h||x||null}}const gt=new C.Box3,Ge=new he,Oe=new he,Ze=new C.Matrix4,Qn=new ae,vt=new ae;function xr(n,t,e,i){H.setBuffer(n._roots[t]);const o=un(0,n,e,i);return H.clearBuffer(),o}function un(n,t,e,i,o=null){const{float32Array:r,uint16Array:s,uint32Array:a}=H;let c=n*2;if(o===null&&(e.boundingBox||e.computeBoundingBox(),Qn.set(e.boundingBox.min,e.boundingBox.max,i),o=Qn),Y(c,s)){const f=t.geometry,d=f.index,l=f.attributes.position,m=e.index,p=e.attributes.position,w=Z(n,a),y=ie(c,s);if(Ze.copy(i).invert(),e.boundsTree)return W(n,r,vt),vt.matrix.copy(Ze),vt.needsUpdate=!0,e.boundsTree.shapecast({intersectsBounds:h=>vt.intersectsBox(h),intersectsTriangle:h=>{h.a.applyMatrix4(i),h.b.applyMatrix4(i),h.c.applyMatrix4(i),h.needsUpdate=!0;for(let g=w*3,x=(y+w)*3;g<x;g+=3)if(K(Oe,g,d,l),Oe.needsUpdate=!0,h.intersectsTriangle(Oe))return!0;return!1}});{const M=Lt(e);for(let h=w*3,g=(y+w)*3;h<g;h+=3){K(Ge,h,d,l),Ge.a.applyMatrix4(Ze),Ge.b.applyMatrix4(Ze),Ge.c.applyMatrix4(Ze),Ge.needsUpdate=!0;for(let x=0,b=M*3;x<b;x+=3)if(K(Oe,x,m,p),Oe.needsUpdate=!0,Ge.intersectsTriangle(Oe))return!0}}}else{const f=Q(n),d=ee(n,a);return W(f,r,gt),!!(o.intersectsBox(gt)&&un(f,t,e,i,o)||(W(d,r,gt),o.intersectsBox(gt)&&un(d,t,e,i,o)))}}const bt=new C.Matrix4,Zt=new ae,Qe=new ae,wr=new C.Vector3,Mr=new C.Vector3,Ar=new C.Vector3,Sr=new C.Vector3;function Tr(n,t,e,i={},o={},r=0,s=1/0){t.boundingBox||t.computeBoundingBox(),Zt.set(t.boundingBox.min,t.boundingBox.max,e),Zt.needsUpdate=!0;const a=n.geometry,c=a.attributes.position,u=a.index,f=t.attributes.position,d=t.index,l=fe.getPrimitive(),m=fe.getPrimitive();let p=wr,w=Mr,y=null,M=null;o&&(y=Ar,M=Sr);let h=1/0,g=null,x=null;return bt.copy(e).invert(),Qe.matrix.copy(bt),n.shapecast({boundsTraverseOrder:b=>Zt.distanceToBox(b),intersectsBounds:(b,A,S)=>S<h&&S<s?(A&&(Qe.min.copy(b.min),Qe.max.copy(b.max),Qe.needsUpdate=!0),!0):!1,intersectsRange:(b,A)=>{if(t.boundsTree)return t.boundsTree.shapecast({boundsTraverseOrder:P=>Qe.distanceToBox(P),intersectsBounds:(P,E,T)=>T<h&&T<s,intersectsRange:(P,E)=>{for(let T=P,R=P+E;T<R;T++){K(m,3*T,d,f),m.a.applyMatrix4(e),m.b.applyMatrix4(e),m.c.applyMatrix4(e),m.needsUpdate=!0;for(let B=b,F=b+A;B<F;B++){K(l,3*B,u,c),l.needsUpdate=!0;const _=l.distanceToTriangle(m,p,y);if(_<h&&(w.copy(p),M&&M.copy(y),h=_,g=B,x=T),_<r)return!0}}}});{const S=Lt(t);for(let P=0,E=S;P<E;P++){K(m,3*P,d,f),m.a.applyMatrix4(e),m.b.applyMatrix4(e),m.c.applyMatrix4(e),m.needsUpdate=!0;for(let T=b,R=b+A;T<R;T++){K(l,3*T,u,c),l.needsUpdate=!0;const B=l.distanceToTriangle(m,p,y);if(B<h&&(w.copy(p),M&&M.copy(y),h=B,g=T,x=P),B<r)return!0}}}}}),fe.releasePrimitive(l),fe.releasePrimitive(m),h===1/0?null:(i.point?i.point.copy(w):i.point=w.clone(),i.distance=h,i.faceIndex=g,o&&(o.point?o.point.copy(M):o.point=M.clone(),o.point.applyMatrix4(bt),w.applyMatrix4(bt),o.distance=w.sub(o.point).length(),o.faceIndex=x),i)}function Cr(n,t=null){t&&Array.isArray(t)&&(t=new Set(t));const e=n.geometry,i=e.index?e.index.array:null,o=e.attributes.position;let r,s,a,c,u=0;const f=n._roots;for(let l=0,m=f.length;l<m;l++)r=f[l],s=new Uint32Array(r),a=new Uint16Array(r),c=new Float32Array(r),d(0,u),u+=r.byteLength;function d(l,m,p=!1){const w=l*2;if(Y(w,a)){const y=Z(l,s),M=ie(w,a);let h=1/0,g=1/0,x=1/0,b=-1/0,A=-1/0,S=-1/0;for(let P=y,E=y+M;P<E;P++){const T=3*n.resolveTriangleIndex(P);for(let R=0;R<3;R++){let B=T+R;B=i?i[B]:B;const F=o.getX(B),_=o.getY(B),V=o.getZ(B);F<h&&(h=F),F>b&&(b=F),_<g&&(g=_),_>A&&(A=_),V<x&&(x=V),V>S&&(S=V)}}return c[l+0]!==h||c[l+1]!==g||c[l+2]!==x||c[l+3]!==b||c[l+4]!==A||c[l+5]!==S?(c[l+0]=h,c[l+1]=g,c[l+2]=x,c[l+3]=b,c[l+4]=A,c[l+5]=S,!0):!1}else{const y=Q(l),M=ee(l,s);let h=p,g=!1,x=!1;if(t){if(!h){const T=y/J+m/te,R=M/J+m/te;g=t.has(T),x=t.has(R),h=!g&&!x}}else g=!0,x=!0;const b=h||g,A=h||x;let S=!1;b&&(S=d(y,m,h));let P=!1;A&&(P=d(M,m,h));const E=S||P;if(E)for(let T=0;T<3;T++){const R=y+T,B=M+T,F=c[R],_=c[R+3],V=c[B],U=c[B+3];c[l+T]=F<V?F:V,c[l+T+3]=_>U?_:U}return E}}}function Br(n,t,e,i,o,r,s){H.setBuffer(n._roots[t]),fn(0,n,e,i,o,r,s),H.clearBuffer()}function fn(n,t,e,i,o,r,s){const{float32Array:a,uint16Array:c,uint32Array:u}=H,f=n*2;if(Y(f,c)){const l=Z(n,u),m=ie(f,c);pr(t,e,i,l,m,o,r,s)}else{const l=Q(n);Re(l,a,i,r,s)&&fn(l,t,e,i,o,r,s);const m=ee(n,u);Re(m,a,i,r,s)&&fn(m,t,e,i,o,r,s)}}const Pr=["x","y","z"];function Ir(n,t,e,i,o,r){H.setBuffer(n._roots[t]);const s=dn(0,n,e,i,o,r);return H.clearBuffer(),s}function dn(n,t,e,i,o,r){const{float32Array:s,uint16Array:a,uint32Array:c}=H;let u=n*2;if(Y(u,a)){const d=Z(n,c),l=ie(u,a);return hr(t,e,i,d,l,o,r)}else{const d=Dt(n,c),l=Pr[d],p=i.direction[l]>=0;let w,y;p?(w=Q(n),y=ee(n,c)):(w=ee(n,c),y=Q(n));const h=Re(w,s,i,o,r)?dn(w,t,e,i,o,r):null;if(h){const b=h.point[l];if(p?b<=s[y+d]:b>=s[y+d+3])return h}const x=Re(y,s,i,o,r)?dn(y,t,e,i,o,r):null;return h&&x?h.distance<=x.distance?h:x:h||x||null}}const xt=new C.Box3,He=new he,qe=new he,et=new C.Matrix4,ei=new ae,wt=new ae;function Rr(n,t,e,i){H.setBuffer(n._roots[t]);const o=mn(0,n,e,i);return H.clearBuffer(),o}function mn(n,t,e,i,o=null){const{float32Array:r,uint16Array:s,uint32Array:a}=H;let c=n*2;if(o===null&&(e.boundingBox||e.computeBoundingBox(),ei.set(e.boundingBox.min,e.boundingBox.max,i),o=ei),Y(c,s)){const f=t.geometry,d=f.index,l=f.attributes.position,m=e.index,p=e.attributes.position,w=Z(n,a),y=ie(c,s);if(et.copy(i).invert(),e.boundsTree)return W(n,r,wt),wt.matrix.copy(et),wt.needsUpdate=!0,e.boundsTree.shapecast({intersectsBounds:h=>wt.intersectsBox(h),intersectsTriangle:h=>{h.a.applyMatrix4(i),h.b.applyMatrix4(i),h.c.applyMatrix4(i),h.needsUpdate=!0;for(let g=w,x=y+w;g<x;g++)if(K(qe,3*t.resolveTriangleIndex(g),d,l),qe.needsUpdate=!0,h.intersectsTriangle(qe))return!0;return!1}});{const M=Lt(e);for(let h=w,g=y+w;h<g;h++){const x=t.resolveTriangleIndex(h);K(He,3*x,d,l),He.a.applyMatrix4(et),He.b.applyMatrix4(et),He.c.applyMatrix4(et),He.needsUpdate=!0;for(let b=0,A=M*3;b<A;b+=3)if(K(qe,b,m,p),qe.needsUpdate=!0,He.intersectsTriangle(qe))return!0}}}else{const f=Q(n),d=ee(n,a);return W(f,r,xt),!!(o.intersectsBox(xt)&&mn(f,t,e,i,o)||(W(d,r,xt),o.intersectsBox(xt)&&mn(d,t,e,i,o)))}}const Mt=new C.Matrix4,Qt=new ae,tt=new ae,Er=new C.Vector3,Fr=new C.Vector3,_r=new C.Vector3,Dr=new C.Vector3;function Nr(n,t,e,i={},o={},r=0,s=1/0){t.boundingBox||t.computeBoundingBox(),Qt.set(t.boundingBox.min,t.boundingBox.max,e),Qt.needsUpdate=!0;const a=n.geometry,c=a.attributes.position,u=a.index,f=t.attributes.position,d=t.index,l=fe.getPrimitive(),m=fe.getPrimitive();let p=Er,w=Fr,y=null,M=null;o&&(y=_r,M=Dr);let h=1/0,g=null,x=null;return Mt.copy(e).invert(),tt.matrix.copy(Mt),n.shapecast({boundsTraverseOrder:b=>Qt.distanceToBox(b),intersectsBounds:(b,A,S)=>S<h&&S<s?(A&&(tt.min.copy(b.min),tt.max.copy(b.max),tt.needsUpdate=!0),!0):!1,intersectsRange:(b,A)=>{if(t.boundsTree){const S=t.boundsTree;return S.shapecast({boundsTraverseOrder:P=>tt.distanceToBox(P),intersectsBounds:(P,E,T)=>T<h&&T<s,intersectsRange:(P,E)=>{for(let T=P,R=P+E;T<R;T++){const B=S.resolveTriangleIndex(T);K(m,3*B,d,f),m.a.applyMatrix4(e),m.b.applyMatrix4(e),m.c.applyMatrix4(e),m.needsUpdate=!0;for(let F=b,_=b+A;F<_;F++){const V=n.resolveTriangleIndex(F);K(l,3*V,u,c),l.needsUpdate=!0;const U=l.distanceToTriangle(m,p,y);if(U<h&&(w.copy(p),M&&M.copy(y),h=U,g=F,x=T),U<r)return!0}}}})}else{const S=Lt(t);for(let P=0,E=S;P<E;P++){K(m,3*P,d,f),m.a.applyMatrix4(e),m.b.applyMatrix4(e),m.c.applyMatrix4(e),m.needsUpdate=!0;for(let T=b,R=b+A;T<R;T++){const B=n.resolveTriangleIndex(T);K(l,3*B,u,c),l.needsUpdate=!0;const F=l.distanceToTriangle(m,p,y);if(F<h&&(w.copy(p),M&&M.copy(y),h=F,g=T,x=P),F<r)return!0}}}}}),fe.releasePrimitive(l),fe.releasePrimitive(m),h===1/0?null:(i.point?i.point.copy(w):i.point=w.clone(),i.distance=h,i.faceIndex=g,o&&(o.point?o.point.copy(M):o.point=M.clone(),o.point.applyMatrix4(Mt),w.applyMatrix4(Mt),o.distance=w.sub(o.point).length(),o.faceIndex=x),i)}function ti(n,t,e){return n===null?null:(n.point.applyMatrix4(t.matrixWorld),n.distance=n.point.distanceTo(e.ray.origin),n.object=t,n)}const At=new ae,St=new C.Ray,ni=new C.Vector3,ii=new C.Matrix4,oi=new C.Vector3,en=["getX","getY","getZ"];class It extends nr{static serialize(t,e={}){e={cloneBuffers:!0,...e};const i=t.geometry,o=t._roots,r=t._indirectBuffer,s=i.getIndex(),a={version:1,roots:null,index:null,indirectBuffer:null};return e.cloneBuffers?(a.roots=o.map(c=>c.slice()),a.index=s?s.array.slice():null,a.indirectBuffer=r?r.slice():null):(a.roots=o,a.index=s?s.array:null,a.indirectBuffer=r),a}static deserialize(t,e,i={}){i={setIndex:!0,indirect:!!t.indirectBuffer,...i};const{index:o,roots:r,indirectBuffer:s}=t;t.version||(console.warn("MeshBVH.deserialize: Serialization format has been changed and will be fixed up. It is recommended to regenerate any stored serialized data."),c(r));const a=new It(e,{...i,[wn]:!0});if(a._roots=r,a._indirectBuffer=s||null,i.setIndex){const u=e.getIndex();if(u===null){const f=new C.BufferAttribute(t.index,1,!1);e.setIndex(f)}else u.array!==o&&(u.array.set(o),u.needsUpdate=!0)}return a;function c(u){for(let f=0;f<u.length;f++){const d=u[f],l=new Uint32Array(d),m=new Uint16Array(d);for(let p=0,w=d.byteLength/te;p<w;p++){const y=J*p,M=2*y;Y(M,m)||(l[y+6]=l[y+6]/J-p)}}}}get primitiveStride(){return 3}get resolveTriangleIndex(){return this.resolvePrimitiveIndex}constructor(t,e={}){e.maxLeafTris&&(console.warn('MeshBVH: "maxLeafTris" option has been deprecated. Use "targetLeafSize", instead.'),e={...e,targetLeafSize:e.maxLeafTris}),super(t,e)}shiftTriangleOffsets(t){return super.shiftPrimitiveOffsets(t)}writePrimitiveBounds(t,e,i){const o=this.geometry,r=this._indirectBuffer,s=o.attributes.position,a=o.index?o.index.array:null,u=(r?r[t]:t)*3;let f=u+0,d=u+1,l=u+2;a&&(f=a[f],d=a[d],l=a[l]);for(let m=0;m<3;m++){const p=s[en[m]](f),w=s[en[m]](d),y=s[en[m]](l);let M=p;w<M&&(M=w),y<M&&(M=y);let h=p;w>h&&(h=w),y>h&&(h=y),e[i+m]=M,e[i+m+3]=h}return e}computePrimitiveBounds(t,e,i){const o=this.geometry,r=this._indirectBuffer,s=o.attributes.position,a=o.index?o.index.array:null,c=s.normalized;if(t<0||e+t-i.offset>i.length/6)throw new Error("MeshBVH: compute triangle bounds range is invalid.");const u=s.array,f=s.offset||0;let d=3;s.isInterleavedBufferAttribute&&(d=s.data.stride);const l=["getX","getY","getZ"],m=i.offset;for(let p=t,w=t+e;p<w;p++){const M=(r?r[p]:p)*3,h=(p-m)*6;let g=M+0,x=M+1,b=M+2;a&&(g=a[g],x=a[x],b=a[b]),c||(g=g*d+f,x=x*d+f,b=b*d+f);for(let A=0;A<3;A++){let S,P,E;c?(S=s[l[A]](g),P=s[l[A]](x),E=s[l[A]](b)):(S=u[g+A],P=u[x+A],E=u[b+A]);let T=S;P<T&&(T=P),E<T&&(T=E);let R=S;P>R&&(R=P),E>R&&(R=E);const B=(R-T)/2,F=A*2;i[h+F+0]=T+B,i[h+F+1]=B+(Math.abs(T)+B)*Ct}}return i}raycastObject3D(t,e,i=[]){const{material:o}=t;if(o===void 0)return;ii.copy(t.matrixWorld).invert(),St.copy(e.ray).applyMatrix4(ii),oi.setFromMatrixScale(t.matrixWorld),ni.copy(St.direction).multiply(oi);const r=ni.length(),s=e.near/r,a=e.far/r;if(e.firstHitOnly===!0){let c=this.raycastFirst(St,o,s,a);c=ti(c,t,e),c&&i.push(c)}else{const c=this.raycast(St,o,s,a);for(let u=0,f=c.length;u<f;u++){const d=ti(c[u],t,e);d&&i.push(d)}}return i}refit(t=null){return(this.indirect?Cr:mr)(this,t)}raycast(t,e=C.FrontSide,i=0,o=1/0){const r=this._roots,s=[],a=this.indirect?Br:gr;for(let c=0,u=r.length;c<u;c++)a(this,c,e,t,s,i,o);return s}raycastFirst(t,e=C.FrontSide,i=0,o=1/0){const r=this._roots;let s=null;const a=this.indirect?Ir:br;for(let c=0,u=r.length;c<u;c++){const f=a(this,c,e,t,i,o);f!=null&&(s==null||f.distance<s.distance)&&(s=f)}return s}intersectsGeometry(t,e){let i=!1;const o=this._roots,r=this.indirect?Rr:xr;for(let s=0,a=o.length;s<a&&(i=r(this,s,t,e),!i);s++);return i}shapecast(t){const e=fe.getPrimitive(),i=super.shapecast({...t,intersectsPrimitive:t.intersectsTriangle,scratchPrimitive:e,iterate:this.indirect?yr:dr});return fe.releasePrimitive(e),i}bvhcast(t,e,i){let{intersectsRanges:o,intersectsTriangles:r}=i;const s=fe.getPrimitive(),a=this.geometry.index,c=this.geometry.attributes.position,u=this.indirect?p=>{const w=this.resolveTriangleIndex(p);K(s,w*3,a,c)}:p=>{K(s,p*3,a,c)},f=fe.getPrimitive(),d=t.geometry.index,l=t.geometry.attributes.position,m=t.indirect?p=>{const w=t.resolveTriangleIndex(p);K(f,w*3,d,l)}:p=>{K(f,p*3,d,l)};if(r){if(!(t instanceof It))throw new Error('MeshBVH: "intersectsTriangles" callback can only be used with another MeshBVH.');const p=(w,y,M,h,g,x,b,A)=>{for(let S=M,P=M+h;S<P;S++){m(S),f.a.applyMatrix4(e),f.b.applyMatrix4(e),f.c.applyMatrix4(e),f.needsUpdate=!0;for(let E=w,T=w+y;E<T;E++)if(u(E),s.needsUpdate=!0,r(s,f,E,S,g,x,b,A))return!0}return!1};if(o){const w=o;o=function(y,M,h,g,x,b,A,S){return w(y,M,h,g,x,b,A,S)?!0:p(y,M,h,g,x,b,A,S)}}else o=p}return super.bvhcast(t,e,{intersectsRanges:o})}intersectsBox(t,e){return At.set(t.min,t.max,e),At.needsUpdate=!0,this.shapecast({intersectsBounds:i=>At.intersectsBox(i),intersectsTriangle:i=>At.intersectsTriangle(i)})}intersectsSphere(t){return this.shapecast({intersectsBounds:e=>t.intersectsBox(e),intersectsTriangle:e=>e.intersectsSphere(t)})}closestPointToGeometry(t,e,i={},o={},r=0,s=1/0){return(this.indirect?Nr:Tr)(this,t,e,i,o,r,s)}closestPointToPoint(t,e={},i=0,o=1/0){return ar(this,t,e,i,o)}}function Lr(n){switch(n){case 1:return"R";case 2:return"RG";case 3:return"RGBA";case 4:return"RGBA"}throw new Error}function Vr(n){switch(n){case 1:return C.RedFormat;case 2:return C.RGFormat;case 3:return C.RGBAFormat;case 4:return C.RGBAFormat}}function ri(n){switch(n){case 1:return C.RedIntegerFormat;case 2:return C.RGIntegerFormat;case 3:return C.RGBAIntegerFormat;case 4:return C.RGBAIntegerFormat}}class wi extends C.DataTexture{constructor(){super(),this.minFilter=C.NearestFilter,this.magFilter=C.NearestFilter,this.generateMipmaps=!1,this.overrideItemSize=null,this._forcedType=null}updateFrom(t){const e=this.overrideItemSize,i=t.itemSize,o=t.count;if(e!==null){if(i*o%e!==0)throw new Error("VertexAttributeTexture: overrideItemSize must divide evenly into buffer length.");t.itemSize=e,t.count=o*i/e}const r=t.itemSize,s=t.count,a=t.normalized,c=t.array.constructor,u=c.BYTES_PER_ELEMENT;let f=this._forcedType,d=r;if(f===null)switch(c){case Float32Array:f=C.FloatType;break;case Uint8Array:case Uint16Array:case Uint32Array:f=C.UnsignedIntType;break;case Int8Array:case Int16Array:case Int32Array:f=C.IntType;break}let l,m,p,w,y=Lr(r);switch(f){case C.FloatType:p=1,m=Vr(r),a&&u===1?(w=c,y+="8",c===Uint8Array?l=C.UnsignedByteType:(l=C.ByteType,y+="_SNORM")):(w=Float32Array,y+="32F",l=C.FloatType);break;case C.IntType:y+=u*8+"I",p=a?Math.pow(2,c.BYTES_PER_ELEMENT*8-1):1,m=ri(r),u===1?(w=Int8Array,l=C.ByteType):u===2?(w=Int16Array,l=C.ShortType):(w=Int32Array,l=C.IntType);break;case C.UnsignedIntType:y+=u*8+"UI",p=a?Math.pow(2,c.BYTES_PER_ELEMENT*8-1):1,m=ri(r),u===1?(w=Uint8Array,l=C.UnsignedByteType):u===2?(w=Uint16Array,l=C.UnsignedShortType):(w=Uint32Array,l=C.UnsignedIntType);break}d===3&&(m===C.RGBAFormat||m===C.RGBAIntegerFormat)&&(d=4);const M=Math.ceil(Math.sqrt(s))||1,h=d*M*M,g=new w(h),x=t.normalized;t.normalized=!1;for(let b=0;b<s;b++){const A=d*b;g[A]=t.getX(b)/p,r>=2&&(g[A+1]=t.getY(b)/p),r>=3&&(g[A+2]=t.getZ(b)/p,d===4&&(g[A+3]=1)),r>=4&&(g[A+3]=t.getW(b)/p)}t.normalized=x,this.internalFormat=y,this.format=m,this.type=l,this.image.width=M,this.image.height=M,this.image.data=g,this.needsUpdate=!0,this.dispose(),t.itemSize=i,t.count=o}}class zr extends wi{constructor(){super(),this._forcedType=C.UnsignedIntType}}class kr extends wi{constructor(){super(),this._forcedType=C.FloatType}}class Mi{constructor(){this.index=new zr,this.position=new kr,this.bvhBounds=new C.DataTexture,this.bvhContents=new C.DataTexture,this._cachedIndexAttr=null,this.index.overrideItemSize=3}updateFrom(t){const{geometry:e}=t;if(Gr(t,this.bvhBounds,this.bvhContents),this.position.updateFrom(e.attributes.position),t.indirect){const i=t._indirectBuffer;if(this._cachedIndexAttr===null||this._cachedIndexAttr.count!==i.length)if(e.index)this._cachedIndexAttr=e.index.clone();else{const o=xi(Nt(e));this._cachedIndexAttr=new C.BufferAttribute(o,1,!1)}Ur(e,i,this._cachedIndexAttr),this.index.updateFrom(this._cachedIndexAttr)}else this.index.updateFrom(e.index)}dispose(){const{index:t,position:e,bvhBounds:i,bvhContents:o}=this;t&&t.dispose(),e&&e.dispose(),i&&i.dispose(),o&&o.dispose()}}function Ur(n,t,e){const i=e.array,o=n.index?n.index.array:null;for(let r=0,s=t.length;r<s;r++){const a=3*r,c=3*t[r];for(let u=0;u<3;u++)i[a+u]=o?o[c+u]:c+u}}function Gr(n,t,e){const i=n._roots;if(i.length!==1)throw new Error("MeshBVHUniformStruct: Multi-root BVHs not supported.");const o=i[0],r=new Uint16Array(o),s=new Uint32Array(o),a=new Float32Array(o),c=o.byteLength/te,u=2*Math.ceil(Math.sqrt(c/2)),f=new Float32Array(4*u*u),d=Math.ceil(Math.sqrt(c)),l=new Uint32Array(2*d*d);for(let m=0;m<c;m++){const p=m*te/4,w=p*2,y=p;for(let M=0;M<3;M++)f[8*m+0+M]=a[y+0+M],f[8*m+4+M]=a[y+3+M];if(Y(w,r)){const M=ie(w,r),h=Z(p,s),g=zo|M;l[m*2+0]=g,l[m*2+1]=h}else{const M=s[p+6],h=Dt(p,s);l[m*2+0]=h,l[m*2+1]=M}}t.image.data=f,t.image.width=u,t.image.height=u,t.format=C.RGBAFormat,t.type=C.FloatType,t.internalFormat="RGBA32F",t.minFilter=C.NearestFilter,t.magFilter=C.NearestFilter,t.generateMipmaps=!1,t.needsUpdate=!0,t.dispose(),e.image.data=l,e.image.width=d,e.image.height=d,e.format=C.RGIntegerFormat,e.type=C.UnsignedIntType,e.internalFormat="RG32UI",e.minFilter=C.NearestFilter,e.magFilter=C.NearestFilter,e.generateMipmaps=!1,e.needsUpdate=!0,e.dispose()}const Or=`
1
+ "use strict";var Yi=Object.create;var Ht=Object.defineProperty;var Xi=Object.getOwnPropertyDescriptor;var ji=Object.getOwnPropertyNames;var Ki=Object.getPrototypeOf,Ji=Object.prototype.hasOwnProperty;var Zi=(n,t,e)=>t in n?Ht(n,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):n[t]=e;var Qi=(n,t,e,i)=>{if(t&&typeof t=="object"||typeof t=="function")for(let o of ji(t))!Ji.call(n,o)&&o!==e&&Ht(n,o,{get:()=>t[o],enumerable:!(i=Xi(t,o))||i.enumerable});return n};var lt=(n,t,e)=>(e=n!=null?Yi(Ki(n)):{},Qi(t||!n||!n.__esModule?Ht(e,"default",{value:n,enumerable:!0}):e,n));var re=(n,t,e)=>Zi(n,typeof t!="symbol"?t+"":t,e);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const C=require("three"),eo=require("three/examples/jsm/loaders/GLTFLoader.js"),to=require("three/examples/jsm/loaders/DRACOLoader.js"),no=require("three/examples/jsm/libs/meshopt_decoder.module.js"),Fn=require("three/examples/jsm/loaders/RGBELoader.js"),_n=require("three/examples/jsm/loaders/EXRLoader.js"),io=require("three/examples/jsm/controls/OrbitControls.js");function oo(n){const t=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(n){for(const e in n)if(e!=="default"){const i=Object.getOwnPropertyDescriptor(n,e);Object.defineProperty(t,e,i.get?i:{enumerable:!0,get:()=>n[e]})}}return t.default=n,Object.freeze(t)}const v=oo(C);class Ft extends Error{constructor(e){super(`Unsupported format: "${e}"`);re(this,"format");this.name="UnsupportedFormatError",this.format=e,Object.setPrototypeOf(this,Ft.prototype)}}class Ae extends Error{constructor(e,i){super(e);re(this,"cause");this.name="CorruptFileError",this.cause=i,Object.setPrototypeOf(this,Ae.prototype)}}class ot extends Error{constructor(e,i,o){super(e);re(this,"status");re(this,"url");this.name="NetworkError",this.status=i,this.url=o,Object.setPrototypeOf(this,ot.prototype)}}function _t(n){n.position.set(0,0,0),n.scale.set(1,1,1),n.updateMatrixWorld(!0);const t=new v.Box3().setFromObject(n),e=t.getCenter(new v.Vector3),i=t.getSize(new v.Vector3),o=Math.max(i.x,i.y,i.z),r=o>0?1/o:1;n.scale.setScalar(r),n.position.sub(e.multiplyScalar(r)),n.updateMatrixWorld(!0)}const gn="https://www.gstatic.com/draco/versioned/decoders/1.5.6/";if(typeof globalThis.FileReader>"u"){class n{constructor(){re(this,"result",null);re(this,"onloadend",null);re(this,"onload",null);re(this,"onerror",null)}readAsArrayBuffer(e){e.arrayBuffer().then(i=>{var o,r;this.result=i,(o=this.onload)==null||o.call(this),(r=this.onloadend)==null||r.call(this)},i=>{var o,r;(o=this.onerror)==null||o.call(this,i),(r=this.onloadend)==null||r.call(this)})}readAsDataURL(e){e.arrayBuffer().then(i=>{var s,a;const o=globalThis.Buffer,r=o?o.from(i).toString("base64"):"";this.result=`data:${e.type};base64,${r}`,(s=this.onload)==null||s.call(this),(a=this.onloadend)==null||a.call(this)},i=>{var o,r;(o=this.onerror)==null||o.call(this,i),(r=this.onloadend)==null||r.call(this)})}}globalThis.FileReader=n}const ro=50*1024*1024;async function so(n,t){const e=[];switch(n.data.byteLength>ro&&e.push("File exceeds 50 MB; performance may be affected"),n.format){case"glb":case"gltf":return ao(n,e,t);case"stl":return co(n,e);case"obj":return lo(n,e)}}function di(n){const t=new eo.GLTFLoader,e=new to.DRACOLoader;return e.setDecoderPath((n==null?void 0:n.dracoDecoderPath)??gn),t.setDRACOLoader(e),t.setMeshoptDecoder(no.MeshoptDecoder),{loader:t,dispose:()=>{e.dispose()}}}async function ao(n,t,e){const{loader:i,dispose:o}=di({dracoDecoderPath:e});try{const r=await new Promise((s,a)=>{i.parse(n.data,"",s,a)});return _t(r.scene),{scene:r.scene,warnings:t}}catch(r){throw new Ae("Failed to parse GLTF/GLB",r)}finally{o()}}async function co(n,t){let e;try{e=(await import("three/examples/jsm/loaders/STLLoader.js")).STLLoader}catch(c){throw new Ae("Failed to load STL parser module. Please check your network connection.",c)}const i=new e;let o;try{o=i.parse(n.data)}catch(c){throw new Ae("Failed to parse STL",c)}const r=new v.MeshPhysicalMaterial({name:"metal",color:12632256,metalness:1,roughness:.1,envMapIntensity:1}),s=new v.Mesh(o,r);s.name="metal";const a=new v.Group;return a.add(s),_t(a),{scene:a,warnings:t}}async function lo(n,t){let e,i;try{const[a,c]=await Promise.all([import("three/examples/jsm/loaders/OBJLoader.js"),import("three/examples/jsm/loaders/MTLLoader.js")]);e=a.OBJLoader,i=c.MTLLoader}catch(a){throw new Ae("Failed to load OBJ parser module. Please check your network connection.",a)}n.mtlData||t.push("No material file provided; default material applied");let o;if(n.mtlData){const a=new i,c=new TextDecoder().decode(n.mtlData);try{o=a.parse(c,""),o.preload()}catch{t.push("Failed to parse MTL file; default material applied"),o=void 0}}const r=new e;o&&r.setMaterials(o);let s;try{const a=new TextDecoder().decode(n.data);s=r.parse(a)}catch(a){throw new Ae("Failed to parse OBJ",a)}return _t(s),{scene:s,warnings:t}}async function uo(n){let t;try{t=(await import("three/examples/jsm/exporters/GLTFExporter.js")).GLTFExporter}catch(i){throw new Ae("Failed to load GLTFExporter module. Please check your network connection.",i)}const e=new t;return new Promise((i,o)=>{e.parse(n,r=>{if(r instanceof ArrayBuffer)i(r);else{const s=JSON.stringify(r),a=new TextEncoder().encode(s);i(a.buffer)}},o,{binary:!0})})}class fo{constructor(){re(this,"_listeners",new Map)}on(t,e){return this._listeners.has(t)||this._listeners.set(t,new Set),this._listeners.get(t).add(e),()=>this.off(t,e)}off(t,e){var i;(i=this._listeners.get(t))==null||i.delete(e)}emit(t,e){var i;(i=this._listeners.get(t))==null||i.forEach(o=>{try{o(e)}catch(r){console.error(`[3d-viewer] uncaught listener error on "${String(t)}":`,r)}})}clear(){this._listeners.clear()}}const mo={".stl":"stl",".obj":"obj",".glb":"glb",".gltf":"gltf"},po={"model/gltf-binary":"glb","model/gltf+json":"gltf"};function ho(n){try{const t=new URL(n,"http://dummy").pathname,e=t.lastIndexOf(".");if(e===-1)return null;const i=t.slice(e).toLowerCase();return mo[i]??null}catch{return null}}function yo(n){const t=n.split(";")[0].trim().toLowerCase();return po[t]??null}function Dn(n){if(n.byteLength<4)return null;const t=new Uint8Array(n,0,Math.min(n.byteLength,80));return t[0]===103&&t[1]===108&&t[2]===84&&t[3]===70?"glb":t[0]===123?"gltf":String.fromCharCode(...t.slice(0,5)).toLowerCase()==="solid"?"stl":null}async function Nn(n){const t=new Uint8Array(n);if(t.length>=2&&t[0]===31&&t[1]===139&&typeof DecompressionStream<"u")try{const e=new DecompressionStream("gzip"),i=new Blob([n]).stream().pipeThrough(e),o=await new Response(i).arrayBuffer();if(o.byteLength>0)return o}catch(e){console.warn("[fetchSource] Decompression failed, proceeding with original buffer:",e)}return n}async function go(n,t){if("data"in n){let s=n.data instanceof Blob?await n.data.arrayBuffer():n.data;s=await Nn(s);const a=n.mtlData instanceof Blob?await n.mtlData.arrayBuffer():n.mtlData,c=n.format||Dn(s)||"glb";return{data:s,mtlData:a,format:c}}let e=n.format??ho(n.url);const i=await fetch(n.url,{headers:n.headers,signal:t}).catch(s=>{throw s.name==="AbortError"?s:new ot(`Fetch failed: ${s.message}`,void 0,n.url)});if(!i.ok)throw new ot(`HTTP ${i.status}`,i.status,n.url);if(!e){const s=i.headers.get("content-type")??"";e=yo(s)}let o=await i.arrayBuffer();if(o=await Nn(o),e||(e=Dn(o)),!e)throw new Ft("unknown");let r;if(n.mtlUrl){const s=await fetch(n.mtlUrl,{headers:n.headers,signal:t});s.ok&&(r=await s.arrayBuffer())}return{data:o,mtlData:r,format:e}}const vo=["glb","gltf","obj","stl"];function mi(n){var i;const e=(i=n.split("?")[0].split("#")[0].split(".").pop())==null?void 0:i.toLowerCase();return e&&vo.includes(e)?e:"glb"}function bo(n){if(!n)return null;if(typeof n=="string"){const t=n.trim();return t?{url:t,format:mi(t)}:null}return n}async function pi(n,t,e,i){e==null||e(0);const o=await go(n,t);e==null||e(.3);const{scene:r,warnings:s}=await so(o,i==null?void 0:i.dracoDecoderPath);e==null||e(.7);const a=o.format==="glb"?o.data:await uo(r);e==null||e(1);let c=0;return r.traverse(u=>{u.isMesh&&c++}),{glb:a,scene:r,sourceFormat:o.format,meshCount:c,warnings:s}}const xo=["stone","diamond","gem","jewel","crystal"],wo=["metal","gold","silver","platinum","band","ring","prong","shank"];function Ln(n,t){const e=n.toLowerCase();return t.some(i=>e.includes(i))}function Vn(n){return n.trim().replace(/[^a-zA-Z0-9_-]/g,"_")}function Mo(n,t,e){var a,c;if(typeof((a=n.userData)==null?void 0:a.partId)=="string"&&n.userData.partId.trim()){const u=n.userData.partId.trim();return e.add(u),u}if(typeof((c=n.userData)==null?void 0:c.id)=="string"&&n.userData.id.trim()){const u=n.userData.id.trim();return e.add(u),u}if(n.name&&n.name.trim()){const u=n.name.trim();if(!e.has(u))return e.add(u),u}let i=n.name?n.name.trim():"mesh",o=n.parent;for(;o&&o!==t;)o.name&&o.name.trim()&&(i=`${o.name.trim()}/${i}`),o=o.parent;let r=i,s=1;for(;e.has(r);)r=`${i}_${s++}`;return e.add(r),r}function Ao(n){var o,r,s;if(Array.isArray((o=n.userData)==null?void 0:o.originalSlots)&&n.userData.originalSlots.length>0)return n.userData.originalSlots;if(Array.isArray(n.material))return n.material.map((a,c)=>{var d,l;const u=((d=a==null?void 0:a.name)==null?void 0:d.trim())||`Material_${c}`;return{slotId:typeof((l=a==null?void 0:a.userData)==null?void 0:l.slotId)=="string"&&a.userData.slotId||`slot_${c}_${Vn(u)}`,materialName:u,index:c}});const t=n.material,e=((r=t==null?void 0:t.name)==null?void 0:r.trim())||(n.name?`${n.name}_mat`:"default");return[{slotId:typeof((s=t==null?void 0:t.userData)==null?void 0:s.slotId)=="string"&&t.userData.slotId||`slot_0_${Vn(e)}`,materialName:e,index:0}]}function So(n,t){const e=`${n} ${t}`,i=Ln(e,xo),o=Ln(e,wo);return i?"stone":"metal"}function zn(n,t){const e=[],i=new Set;return n.traverse(o=>{var l;if(!(o instanceof v.Mesh))return;const r=o,s=Mo(r,n,i);r.userData.partId=s;const a=Ao(r);r.userData.originalSlots=a;const c=((l=a[0])==null?void 0:l.materialName)??"";r.userData.originalMaterialName=c;const u=a.map(m=>m.materialName).join(" "),f=r.uuid in t||s in t,d=t[s]??t[r.uuid];f&&d?e.push({meshId:r.uuid,partId:s,meshName:r.name,materialName:c,slots:a,type:d,overridden:!0}):e.push({meshId:r.uuid,partId:s,meshName:r.name,materialName:c,slots:a,type:So(r.name,u),overridden:!1})}),e}function kn(n){const t=new Map;for(const e of n)for(const i of e.slots){const o=t.get(i.slotId);o?(o.partIds.includes(e.partId)||o.partIds.push(e.partId),o.meshIds.includes(e.meshId)||o.meshIds.push(e.meshId),o.type!=="stone"&&e.type==="stone"&&(o.type="stone")):t.set(i.slotId,{slotId:i.slotId,name:i.materialName,type:e.type,partIds:[e.partId],meshIds:[e.meshId]})}return Array.from(t.values())}const Se=[{id:"yellow-gold-18k",label:"18K Yellow Gold",hex:"#ffd566",metalness:1,roughness:.04,category:"gold",specularColor:"#fffdbf",density:15500,thumbnail:"/thumbnails/yellow-gold-18k.svg"},{id:"yellow-gold-14k",label:"14K Yellow Gold",hex:"#fed882",metalness:1,roughness:.05,category:"gold",specularColor:"#fffec9",density:13500,thumbnail:"/thumbnails/yellow-gold-14k.svg"},{id:"pure-gold-24k",label:"24K Pure Gold",hex:"#ffe496",metalness:1,roughness:.03,category:"gold",specularColor:"#fffdbf",density:19320,source:"physicallybased.info/gold",thumbnail:"/thumbnails/pure-gold-24k.svg"},{id:"rose-gold-18k",label:"18K Rose Gold",hex:"#e8927c",metalness:1,roughness:.04,category:"gold",specularColor:"#ffd7cb",density:15400,thumbnail:"/thumbnails/rose-gold-18k.svg"},{id:"rose-gold-14k",label:"14K Rose Gold",hex:"#f1a998",metalness:1,roughness:.05,category:"gold",specularColor:"#ffe4db",density:13300,thumbnail:"/thumbnails/rose-gold-14k.svg"},{id:"white-gold-18k",label:"18K White Gold",hex:"#f5f6f8",metalness:1,roughness:.03,category:"gold",specularColor:"#ffffff",density:15600,thumbnail:"/thumbnails/white-gold-18k.svg"},{id:"platinum-950",label:"Platinum 950",hex:"#e3ded7",metalness:1,roughness:.04,category:"platinum-silver",specularColor:"#edebe6",density:21450,source:"physicallybased.info/platinum",thumbnail:"/thumbnails/platinum-950.svg"},{id:"silver-925",label:"Sterling Silver 925",hex:"#fefdfc",metalness:1,roughness:.05,category:"platinum-silver",specularColor:"#ffffff",density:10360,source:"physicallybased.info/silver",thumbnail:"/thumbnails/silver-925.svg"},{id:"titanium",label:"Titanium",hex:"#b1aaa2",metalness:1,roughness:.1,category:"alternative",specularColor:"#c8c3bd",density:4506,source:"physicallybased.info/titanium",thumbnail:"/thumbnails/titanium.svg"},{id:"black-ruthenium",label:"Black Ruthenium",hex:"#28282c",metalness:.95,roughness:.06,category:"alternative",specularColor:"#444448",density:12450,thumbnail:"/thumbnails/black-ruthenium.svg"},{id:"rmj-custom-yellow-gold",label:"RMJ Custom Yellow Gold",hex:"#ffc766",metalness:1,roughness:0,category:"gold",specularColor:"#ffffff",ior:1.5,source:"app.rendermyjewelry.com",thumbnail:"/thumbnails/rmj-custom-yellow-gold.svg"},{id:"rmj-custom-rose-gold",label:"RMJ Custom Rose Gold",hex:"#ffb6a3",metalness:1,roughness:0,category:"gold",specularColor:"#ffffff",ior:1.5,source:"app.rendermyjewelry.com",thumbnail:"/thumbnails/rmj-custom-rose-gold.svg"},{id:"rmj-white-gold-polished",label:"RMJ White Gold Polished",hex:"#c2c2c3",metalness:1,roughness:0,category:"gold",specularColor:"#ffffff",ior:1.5,source:"app.rendermyjewelry.com",thumbnail:"/thumbnails/rmj-white-gold-polished.svg"},{id:"rmj-gold-polished",label:"RMJ Gold Polished (18K)",hex:"#e5b377",metalness:1,roughness:0,category:"gold",specularColor:"#ffffff",ior:1.5,source:"app.rendermyjewelry.com",thumbnail:"/thumbnails/rmj-gold-polished.svg"},{id:"rmj-rose-gold-polished",label:"RMJ Rose Gold Polished",hex:"#f2af83",metalness:1,roughness:0,category:"gold",specularColor:"#ffffff",ior:1.5,source:"app.rendermyjewelry.com",thumbnail:"/thumbnails/rmj-rose-gold-polished.svg"},{id:"rmj-gold-22k",label:"RMJ Gold 22K",hex:"#eaa958",metalness:1,roughness:0,category:"gold",specularColor:"#ffffff",ior:1.5,source:"app.rendermyjewelry.com",thumbnail:"/thumbnails/rmj-gold-22k.svg"},{id:"rmj-gold-14k",label:"RMJ Gold 14K",hex:"#d6af80",metalness:1,roughness:0,category:"gold",specularColor:"#ffffff",ior:1.5,source:"app.rendermyjewelry.com",thumbnail:"/thumbnails/rmj-gold-14k.svg"},{id:"rmj-gold-09k",label:"RMJ Gold 9K",hex:"#d4b38c",metalness:1,roughness:0,category:"gold",specularColor:"#ffffff",ior:1.5,source:"app.rendermyjewelry.com",thumbnail:"/thumbnails/rmj-gold-09k.svg"},{id:"rmj-silver-polished",label:"RMJ Silver Polished",hex:"#dedede",metalness:1,roughness:.1,category:"platinum-silver",specularColor:"#ffffff",ior:1.5,source:"app.rendermyjewelry.com",thumbnail:"/thumbnails/rmj-silver-polished.svg"},{id:"rmj-platinum-polished",label:"RMJ Platinum Polished",hex:"#b9bbbc",metalness:1,roughness:0,category:"platinum-silver",specularColor:"#ffffff",ior:1.5,source:"app.rendermyjewelry.com",thumbnail:"/thumbnails/rmj-platinum-polished.svg"},{id:"yellow-gold",label:"Yellow Gold",hex:"#ffc766",metalness:1,roughness:.06,thumbnail:"/thumbnails/yellow-gold.svg"},{id:"rose-gold",label:"Rose Gold",hex:"#ffb6a3",metalness:1,roughness:.06,thumbnail:"/thumbnails/rose-gold.svg"},{id:"white-gold",label:"White Gold",hex:"#f0f0f0",metalness:1,roughness:.05,thumbnail:"/thumbnails/white-gold.svg"},{id:"platinum",label:"Platinum",hex:"#e5e5e5",metalness:1,roughness:.05,thumbnail:"/thumbnails/platinum.svg"},{id:"silver",label:"Silver",hex:"#dcdcdc",metalness:1,roughness:.08,thumbnail:"/thumbnails/silver.svg"}],de=[{id:"diamond-d",label:"White Diamond",hex:"#ffffff",ior:2.417,transmission:.98,roughness:0,category:"diamond",grade:"D-F Colorless",dispersion:.024,absorptionFactor:0,ambientFloor:.05,source:"physicallybased.info/diamond",thumbnail:"/thumbnails/diamond-d.svg"},{id:"diamond-gh",label:"Near Colorless Diamond",hex:"#fbf9f5",ior:2.417,transmission:.97,roughness:0,category:"diamond",grade:"G-H Near Colorless",dispersion:.012,absorptionFactor:0,ambientFloor:.05,thumbnail:"/thumbnails/diamond-gh.svg"},{id:"diamond-fancy-yellow",label:"Yellow Diamond",hex:"#fdd835",ior:2.417,transmission:.95,roughness:0,category:"diamond",grade:"Fancy Yellow",dispersion:.01,absorptionFactor:.11,thumbnail:"/thumbnails/diamond-fancy-yellow.svg"},{id:"diamond-fancy-pink",label:"Pink Diamond",hex:"#f472b6",ior:2.417,transmission:.94,roughness:0,category:"diamond",grade:"Fancy Pink",dispersion:.01,absorptionFactor:.14,thumbnail:"/thumbnails/diamond-fancy-pink.svg"},{id:"diamond-fancy-blue",label:"Blue Diamond",hex:"#38bdf8",ior:2.417,transmission:.94,roughness:0,category:"diamond",grade:"Fancy Blue",dispersion:.01,absorptionFactor:.14,thumbnail:"/thumbnails/diamond-fancy-blue.svg"},{id:"diamond-fancy-green",label:"Green Diamond",hex:"#34d399",ior:2.417,transmission:.94,roughness:0,category:"diamond",grade:"Fancy Green",dispersion:.01,absorptionFactor:.14,thumbnail:"/thumbnails/diamond-fancy-green.svg"},{id:"diamond-fancy-champagne",label:"Champagne Diamond",hex:"#c28b51",ior:2.417,transmission:.93,roughness:0,category:"diamond",grade:"Fancy Champagne",dispersion:.01,absorptionFactor:.18,thumbnail:"/thumbnails/diamond-fancy-champagne.svg"},{id:"diamond-fancy-black",label:"Black Diamond",hex:"#151515",ior:2.417,transmission:0,roughness:.05,category:"diamond",grade:"Fancy Black",dispersion:0,absorptionFactor:0,thumbnail:"/thumbnails/diamond-fancy-black.svg"},{id:"ruby",label:"Ruby",hex:"#e14276",ior:1.77,transmission:.88,roughness:.03,category:"gemstone",dispersion:0,absorptionFactor:.5,thumbnail:"/thumbnails/ruby.svg"},{id:"sapphire",label:"Sapphire",hex:"#1a4fb4",ior:1.77,transmission:.88,roughness:.03,category:"gemstone",dispersion:0,absorptionFactor:.45,source:"physicallybased.info/sapphire",thumbnail:"/thumbnails/sapphire.svg"},{id:"emerald",label:"Emerald",hex:"#20cc95",ior:1.58,transmission:.9,roughness:.04,category:"gemstone",dispersion:0,absorptionFactor:.4,thumbnail:"/thumbnails/emerald.svg"},{id:"aquamarine",label:"Aquamarine",hex:"#5eead4",ior:1.577,transmission:.94,roughness:0,category:"gemstone",dispersion:0,absorptionFactor:.1,thumbnail:"/thumbnails/aquamarine.svg"},{id:"amethyst",label:"Amethyst",hex:"#9333ea",ior:1.544,transmission:.91,roughness:0,category:"gemstone",dispersion:0,absorptionFactor:.35,source:"physicallybased.info/quartz",thumbnail:"/thumbnails/amethyst.svg"},{id:"tanzanite",label:"Tanzanite",hex:"#6366f1",ior:1.691,transmission:.9,roughness:0,category:"gemstone",dispersion:0,absorptionFactor:.42,thumbnail:"/thumbnails/tanzanite.svg"},{id:"rmj-diamond-d-colorless",label:"RMJ Diamond D Colorless",hex:"#ececec",ior:2.4,transmission:.98,roughness:0,category:"diamond",dispersion:.004,absorptionFactor:1,boostFactors:[2,2,2],source:"app.rendermyjewelry.com",thumbnail:"/thumbnails/rmj-diamond-d-colorless.svg"},{id:"rmj-ruby-1",label:"RMJ Ruby 1",hex:"#e14276",ior:1.77,transmission:.88,roughness:0,category:"gemstone",dispersion:0,absorptionFactor:1.6,boostFactors:[1.3,.16,.9],source:"app.rendermyjewelry.com",thumbnail:"/thumbnails/rmj-ruby-1.svg"},{id:"rmj-emerald-1",label:"RMJ Emerald 1",hex:"#20cc95",ior:1.577,transmission:.9,roughness:0,category:"gemstone",dispersion:.012,absorptionFactor:1,boostFactors:[.892,.892,.986],source:"app.rendermyjewelry.com",thumbnail:"/thumbnails/rmj-emerald-1.svg"},{id:"rmj-sapphire-cabachon-2",label:"RMJ Sapphire Cabochon 2",hex:"#45b1e1",ior:1.66,transmission:.88,roughness:0,category:"gemstone",dispersion:0,absorptionFactor:2.51,boostFactors:[1,1,1],source:"app.rendermyjewelry.com",thumbnail:"/thumbnails/rmj-sapphire-cabachon-2.svg"},{id:"rmj-diamond-pink-2",label:"RMJ Diamond Pink 2",hex:"#f1bcd5",ior:2.6,transmission:.94,roughness:0,category:"diamond",dispersion:.012,absorptionFactor:1,boostFactors:[1.3,1,1],source:"app.rendermyjewelry.com",thumbnail:"/thumbnails/rmj-diamond-pink-2.svg"},{id:"rmj-diamond-yellow-2",label:"RMJ Diamond Yellow 2",hex:"#fce88c",ior:2.6,transmission:.94,roughness:0,category:"diamond",dispersion:.012,absorptionFactor:1.3,boostFactors:[1,1,1],source:"app.rendermyjewelry.com",thumbnail:"/thumbnails/rmj-diamond-yellow-2.svg"},{id:"rmj-diamond-blue-2",label:"RMJ Diamond Blue 2",hex:"#80c9fc",ior:2.6,transmission:.94,roughness:0,category:"diamond",dispersion:.013,absorptionFactor:1,boostFactors:[.9,1,1],source:"app.rendermyjewelry.com",thumbnail:"/thumbnails/rmj-diamond-blue-2.svg"},{id:"diamond",label:"White Diamond",hex:"#ffffff",ior:2.417,transmission:.95,roughness:.02,dispersion:.012,absorptionFactor:0,ambientFloor:.05,thumbnail:"/thumbnails/diamond-d.svg"},{id:"yellow-diamond",label:"Yellow Diamond",hex:"#fcd34d",ior:2.42,transmission:.94,roughness:.02,dispersion:.01,absorptionFactor:.11,thumbnail:"/thumbnails/diamond-fancy-yellow.svg"},{id:"pink-diamond",label:"Pink Diamond",hex:"#f472b6",ior:2.42,transmission:.94,roughness:.02,dispersion:.01,absorptionFactor:.14,thumbnail:"/thumbnails/diamond-fancy-pink.svg"},{id:"blue-diamond",label:"Blue Diamond",hex:"#60a5fa",ior:2.42,transmission:.94,roughness:.02,dispersion:.01,absorptionFactor:.14,thumbnail:"/thumbnails/diamond-fancy-blue.svg"},{id:"black-diamond",label:"Black Diamond",hex:"#1a1a1a",ior:2.42,transmission:0,roughness:.05,dispersion:0,absorptionFactor:0,thumbnail:"/thumbnails/diamond-fancy-black.svg"}],vn=Se,bn=de,To=Se.filter(n=>n.category!==void 0&&!n.id.startsWith("rmj-"));Se.filter(n=>n.id.startsWith("rmj-"));const Co=de.filter(n=>n.category==="diamond"&&!n.id.startsWith("rmj-")&&n.id.startsWith("diamond-")),Bo=de.filter(n=>n.category==="gemstone"&&!n.id.startsWith("rmj-"));de.filter(n=>n.id.startsWith("rmj-"));const Po=new Set(["yellow-gold","rose-gold","white-gold","platinum","silver"]),Io=new Set(["diamond","yellow-diamond","pink-diamond","blue-diamond","black-diamond"]),Ro=Se.filter(n=>!Po.has(n.id)),Eo=de.filter(n=>!Io.has(n.id));function Fo(n){if(!n)return;const t=n.toLowerCase().trim();return Se.find(e=>e.id.toLowerCase()===t||e.hex.toLowerCase()===t)}function _o(n){if(!n)return;const t=n.toLowerCase().trim();return de.find(e=>e.id.toLowerCase()===t||e.hex.toLowerCase()===t)}const Un=new WeakMap;function Do(n){var g,x;n.boundingSphere||n.computeBoundingSphere();const t=(g=n.boundingSphere)!=null&&g.center?n.boundingSphere.center.clone():new v.Vector3,e=((x=n.boundingSphere)==null?void 0:x.radius)??1,i=Math.max(e,.01),o=n.getAttribute("position");if(!o||o.count===0)return{centroid:t,radius:i,triangleCount:0,starConvexFailureArea:1,isBakeable:!1,flippedWinding:!1};const r=n.getIndex(),s=r?r.count/3:o.count/3;let a=0,c=0,u=0;const f=new v.Vector3,d=new v.Vector3,l=new v.Vector3,m=new v.Vector3,p=new v.Vector3,w=new v.Vector3,y=new v.Vector3;for(let b=0;b<s;b++){const A=r?r.getX(b*3):b*3,S=r?r.getX(b*3+1):b*3+1,P=r?r.getX(b*3+2):b*3+2;f.fromBufferAttribute(o,A).sub(t).divideScalar(i),d.fromBufferAttribute(o,S).sub(t).divideScalar(i),l.fromBufferAttribute(o,P).sub(t).divideScalar(i),m.subVectors(d,f),p.subVectors(l,f),w.crossVectors(m,p);const E=w.length();if(E<1e-12)continue;const T=E*.5;a+=T,w.divideScalar(E);const R=f.dot(w);y.crossVectors(d,l),u+=f.dot(y)/6,R<=1e-4&&(c+=T)}const M=a>0?c/a:1,h=u<0;return{centroid:t,radius:i,triangleCount:s,starConvexFailureArea:M,isBakeable:M<=.005&&!isNaN(a)&&a>0,flippedWinding:h}}function No(n,t){const e=n.getIndex()!==null,i=e?n.toNonIndexed():n;try{const o=i.getAttribute("position");if(!o)return new Float32Array(0);const r=o.count,s=new Float32Array(r*4),a=new v.Vector3,c=new v.Vector3,u=new v.Vector3,f=new v.Vector3,d=new v.Vector3,l=new v.Vector3,m=t.flippedWinding?-1:1;for(let p=0;p<r;p+=3){a.fromBufferAttribute(o,p).sub(t.centroid).divideScalar(t.radius),c.fromBufferAttribute(o,p+1).sub(t.centroid).divideScalar(t.radius),u.fromBufferAttribute(o,p+2).sub(t.centroid).divideScalar(t.radius),f.subVectors(c,a),d.subVectors(u,a),l.crossVectors(f,d).multiplyScalar(m);const w=l.length();w>1e-12?l.divideScalar(w):l.set(0,1,0);const y=a.dot(l);for(let M=0;M<3;M++){const h=(p+M)*4;s[h]=l.x,s[h+1]=l.y,s[h+2]=l.z,s[h+3]=y}}return s}finally{e&&i.dispose()}}function Lo(n,t){const e=Un.get(n);if(e)return e;const i=n.getAttribute("position"),o=n.getIndex();let r=2166136261;const s=u=>{r^=u&255,r=Math.imul(r,16777619),r^=u>>8&255,r=Math.imul(r,16777619),r^=u>>16&255,r=Math.imul(r,16777619),r^=u>>24&255,r=Math.imul(r,16777619)};if(s(t.triangleCount),o)for(let u=0;u<o.count;u++)s(o.getX(u));const a=new v.Vector3;for(let u=0;u<i.count;u++)a.fromBufferAttribute(i,u).sub(t.centroid).divideScalar(t.radius),s(Math.round(a.x*1e4)),s(Math.round(a.y*1e4)),s(Math.round(a.z*1e4));const c=(r>>>0).toString(16);return Un.set(n,c),c}const hi=0,Vo=1,yi=2,Gn=2,qt=1.25,On=1,te=6*4+4+4,J=te/4,xn=65535,zo=xn<<16,Ct=Math.pow(2,-24),wn=Symbol("SKIP_GENERATION"),gi={strategy:hi,maxDepth:40,targetLeafSize:10,useSharedArrayBuffer:!1,setBoundingBox:!0,onProgress:null,indirect:!1,verbose:!0,range:null,[wn]:!1};function W(n,t,e){return e.min.x=t[n],e.min.y=t[n+1],e.min.z=t[n+2],e.max.x=t[n+3],e.max.y=t[n+4],e.max.z=t[n+5],e}function nn(n){let t=-1,e=-1/0;for(let i=0;i<3;i++){const o=n[i+3]-n[i];o>e&&(e=o,t=i)}return t}function Hn(n,t){t.set(n)}function qn(n,t,e){let i,o;for(let r=0;r<3;r++){const s=r+3;i=n[r],o=t[r],e[r]=i<o?i:o,i=n[s],o=t[s],e[s]=i>o?i:o}}function ut(n,t,e){for(let i=0;i<3;i++){const o=t[n+2*i],r=t[n+2*i+1],s=o-r,a=o+r;s<e[i]&&(e[i]=s),a>e[i+3]&&(e[i+3]=a)}}function je(n){const t=n[3]-n[0],e=n[4]-n[1],i=n[5]-n[2];return 2*(t*e+e*i+i*t)}function Y(n,t){return t[n+15]===xn}function Z(n,t){return t[n+6]}function ie(n,t){return t[n+14]}function Q(n){return n+J}function ee(n,t){const e=t[n+6];return n+e*J}function Dt(n,t){return t[n+7]}function $t(n,t,e,i,o){let r=1/0,s=1/0,a=1/0,c=-1/0,u=-1/0,f=-1/0,d=1/0,l=1/0,m=1/0,p=-1/0,w=-1/0,y=-1/0;const M=n.offset||0;for(let h=(t-M)*6,g=(t+e-M)*6;h<g;h+=6){const x=n[h+0],b=n[h+1],A=x-b,S=x+b;A<r&&(r=A),S>c&&(c=S),x<d&&(d=x),x>p&&(p=x);const P=n[h+2],E=n[h+3],T=P-E,R=P+E;T<s&&(s=T),R>u&&(u=R),P<l&&(l=P),P>w&&(w=P);const B=n[h+4],F=n[h+5],_=B-F,V=B+F;_<a&&(a=_),V>f&&(f=V),B<m&&(m=B),B>y&&(y=B)}i[0]=r,i[1]=s,i[2]=a,i[3]=c,i[4]=u,i[5]=f,o[0]=d,o[1]=l,o[2]=m,o[3]=p,o[4]=w,o[5]=y}const xe=32,ko=(n,t)=>n.candidate-t.candidate,Be=new Array(xe).fill().map(()=>({count:0,bounds:new Float32Array(6),rightCacheBounds:new Float32Array(6),leftCacheBounds:new Float32Array(6),candidate:0})),ft=new Float32Array(6);function Uo(n,t,e,i,o,r){let s=-1,a=0;if(r===hi)s=nn(t),s!==-1&&(a=(t[s]+t[s+3])/2);else if(r===Vo)s=nn(n),s!==-1&&(a=Go(e,i,o,s));else if(r===yi){const c=je(n);let u=qt*o;const f=e.offset||0,d=(i-f)*6,l=(i+o-f)*6;for(let m=0;m<3;m++){const p=t[m],M=(t[m+3]-p)/xe;if(o<xe/4){const h=[...Be];h.length=o;let g=0;for(let b=d;b<l;b+=6,g++){const A=h[g];A.candidate=e[b+2*m],A.count=0;const{bounds:S,leftCacheBounds:P,rightCacheBounds:E}=A;for(let T=0;T<3;T++)E[T]=1/0,E[T+3]=-1/0,P[T]=1/0,P[T+3]=-1/0,S[T]=1/0,S[T+3]=-1/0;ut(b,e,S)}h.sort(ko);let x=o;for(let b=0;b<x;b++){const A=h[b];for(;b+1<x&&h[b+1].candidate===A.candidate;)h.splice(b+1,1),x--}for(let b=d;b<l;b+=6){const A=e[b+2*m];for(let S=0;S<x;S++){const P=h[S];A>=P.candidate?ut(b,e,P.rightCacheBounds):(ut(b,e,P.leftCacheBounds),P.count++)}}for(let b=0;b<x;b++){const A=h[b],S=A.count,P=o-A.count,E=A.leftCacheBounds,T=A.rightCacheBounds;let R=0;S!==0&&(R=je(E)/c);let B=0;P!==0&&(B=je(T)/c);const F=On+qt*(R*S+B*P);F<u&&(s=m,u=F,a=A.candidate)}}else{for(let x=0;x<xe;x++){const b=Be[x];b.count=0,b.candidate=p+M+x*M;const A=b.bounds;for(let S=0;S<3;S++)A[S]=1/0,A[S+3]=-1/0}for(let x=d;x<l;x+=6){let S=~~((e[x+2*m]-p)/M);S>=xe&&(S=xe-1);const P=Be[S];P.count++,ut(x,e,P.bounds)}const h=Be[xe-1];Hn(h.bounds,h.rightCacheBounds);for(let x=xe-2;x>=0;x--){const b=Be[x],A=Be[x+1];qn(b.bounds,A.rightCacheBounds,b.rightCacheBounds)}let g=0;for(let x=0;x<xe-1;x++){const b=Be[x],A=b.count,S=b.bounds,E=Be[x+1].rightCacheBounds;A!==0&&(g===0?Hn(S,ft):qn(S,ft,ft)),g+=A;let T=0,R=0;g!==0&&(T=je(ft)/c);const B=o-g;B!==0&&(R=je(E)/c);const F=On+qt*(T*g+R*B);F<u&&(s=m,u=F,a=b.candidate)}}}}else console.warn(`BVH: Invalid build strategy value ${r} used.`);return{axis:s,pos:a}}function Go(n,t,e,i){let o=0;const r=n.offset;for(let s=t,a=t+e;s<a;s++)o+=n[(s-r)*6+i*2];return o/e}class Wt{constructor(){this.boundingData=new Float32Array(6)}}function Oo(n,t,e,i,o,r){let s=i,a=i+o-1;const c=r.pos,u=r.axis*2,f=e.offset||0;for(;;){for(;s<=a&&e[(s-f)*6+u]<c;)s++;for(;s<=a&&e[(a-f)*6+u]>=c;)a--;if(s<a){for(let d=0;d<t;d++){let l=n[s*t+d];n[s*t+d]=n[a*t+d],n[a*t+d]=l}for(let d=0;d<6;d++){const l=s-f,m=a-f,p=e[l*6+d];e[l*6+d]=e[m*6+d],e[m*6+d]=p}s++,a--}else return s}}let vi,Bt,on,bi;const Ho=Math.pow(2,32);function rn(n){return"count"in n?1:1+rn(n.left)+rn(n.right)}function qo(n,t,e){return vi=new Float32Array(e),Bt=new Uint32Array(e),on=new Uint16Array(e),bi=new Uint8Array(e),sn(n,t)}function sn(n,t){const e=n/4,i=n/2,o="count"in t,r=t.boundingData;for(let s=0;s<6;s++)vi[e+s]=r[s];if(o)return t.buffer?(bi.set(new Uint8Array(t.buffer),n),n+t.buffer.byteLength):(Bt[e+6]=t.offset,on[i+14]=t.count,on[i+15]=xn,n+te);{const{left:s,right:a,splitAxis:c}=t,u=n+te;let f=sn(u,s);const d=n/te,m=f/te-d;if(m>Ho)throw new Error("MeshBVH: Cannot store relative child node offset greater than 32 bits.");return Bt[e+6]=m,Bt[e+7]=c,sn(f,a)}}function $o(n,t,e,i,o,r){const{maxDepth:s,verbose:a,targetLeafSize:c,_strictLeafSize:u=1/0,strategy:f,onProgress:d}=o,l=n.primitiveBuffer,m=n.primitiveBufferStride,p=new Float32Array(6);let w=!1;const y=new Wt;return $t(t,e,i,y.boundingData,p),h(y,e,i,p),y;function M(g){d&&d((g-r.offset)/r.count)}function h(g,x,b,A=null,S=0){!w&&S>=s&&(w=!0,a&&console.warn(`BVH: Max depth of ${s} reached when generating BVH. Consider increasing maxDepth.`));const P=b>u;if(b<=c&&!P||S>=s)return M(x+b),g.offset=x,g.count=b,g;const E=Uo(g.boundingData,A,t,x,b,f);let T=E.axis===-1?-1:Oo(l,m,t,x,b,E);if(E.axis===-1||T===x||T===x+b){if(!P)return M(x+b),g.offset=x,g.count=b,g;E.axis=Math.max(0,nn(g.boundingData)),T=x+Math.max(1,Math.floor(b/2))}g.splitAxis=E.axis;const R=new Wt,B=x,F=T-x;g.left=R,$t(t,B,F,R.boundingData,p),h(R,B,F,p,S+1);const _=new Wt,V=T,U=b-F;return g.right=_,$t(t,V,U,_.boundingData,p),h(_,V,U,p,S+1),g}}function Wo(n,t){const e=t.useSharedArrayBuffer?SharedArrayBuffer:ArrayBuffer,i=n.getRootRanges(t.range),o=i[0],r=i[i.length-1],s={offset:o.offset,count:r.offset+r.count-o.offset},a=new Float32Array(6*s.count);a.offset=s.offset,n.computePrimitiveBounds(s.offset,s.count,a),n._roots=i.map(c=>{const u=$o(n,a,c.offset,c.count,t,s),f=rn(u),d=new e(te*f);return qo(0,u,d),d})}class Mn{constructor(t){this._getNewPrimitive=t,this._primitives=[]}getPrimitive(){const t=this._primitives;return t.length===0?this._getNewPrimitive():t.pop()}releasePrimitive(t){this._primitives.push(t)}}class Yo{constructor(){this.float32Array=null,this.uint16Array=null,this.uint32Array=null;const t=[];let e=null;this.setBuffer=i=>{e&&t.push(e),e=i,this.float32Array=new Float32Array(i),this.uint16Array=new Uint16Array(i),this.uint32Array=new Uint32Array(i)},this.clearBuffer=()=>{e=null,this.float32Array=null,this.uint16Array=null,this.uint32Array=null,t.length!==0&&this.setBuffer(t.pop())}}}const H=new Yo;let Ie,Ye;const Ve=[],dt=new Mn(()=>new C.Box3);function Xo(n,t,e,i,o,r){Ie=dt.getPrimitive(),Ye=dt.getPrimitive(),Ve.push(Ie,Ye),H.setBuffer(n._roots[t]);const s=an(0,n.geometry,e,i,o,r);H.clearBuffer(),dt.releasePrimitive(Ie),dt.releasePrimitive(Ye),Ve.pop(),Ve.pop();const a=Ve.length;return a>0&&(Ye=Ve[a-1],Ie=Ve[a-2]),s}function an(n,t,e,i,o=null,r=0,s=0){const{float32Array:a,uint16Array:c,uint32Array:u}=H;let f=n*2;if(Y(f,c)){const l=Z(n,u),m=ie(f,c);return W(n,a,Ie),i(l,m,!1,s,r+n/J,Ie)}else{let T=function(B){const{uint16Array:F,uint32Array:_}=H;let V=B*2;for(;!Y(V,F);)B=Q(B),V=B*2;return Z(B,_)},R=function(B){const{uint16Array:F,uint32Array:_}=H;let V=B*2;for(;!Y(V,F);)B=ee(B,_),V=B*2;return Z(B,_)+ie(V,F)};const l=Q(n),m=ee(n,u);let p=l,w=m,y,M,h,g;if(o&&(h=Ie,g=Ye,W(p,a,h),W(w,a,g),y=o(h),M=o(g),M<y)){p=m,w=l;const B=y;y=M,M=B,h=g}h||(h=Ie,W(p,a,h));const x=Y(p*2,c),b=e(h,x,y,s+1,r+p/J);let A;if(b===Gn){const B=T(p),_=R(p)-B;A=i(B,_,!0,s+1,r+p/J,h)}else A=b&&an(p,t,e,i,o,r,s+1);if(A)return!0;g=Ye,W(w,a,g);const S=Y(w*2,c),P=e(g,S,M,s+1,r+w/J);let E;if(P===Gn){const B=T(w),_=R(w)-B;E=i(B,_,!0,s+1,r+w/J,g)}else E=P&&an(w,t,e,i,o,r,s+1);return!!E}}const it=new H.constructor,Pt=new H.constructor,Pe=new Mn(()=>new C.Box3),ze=new C.Box3,ke=new C.Box3,Yt=new C.Box3,Xt=new C.Box3;let jt=!1;function jo(n,t,e,i){if(jt)throw new Error("MeshBVH: Recursive calls to bvhcast not supported.");jt=!0;const o=n._roots,r=t._roots;let s,a=0,c=0;const u=new C.Matrix4().copy(e).invert();for(let f=0,d=o.length;f<d;f++){it.setBuffer(o[f]),c=0;const l=Pe.getPrimitive();W(0,it.float32Array,l),l.applyMatrix4(u);for(let m=0,p=r.length;m<p&&(Pt.setBuffer(r[m]),s=pe(0,0,e,u,i,a,c,0,0,l),Pt.clearBuffer(),c+=r[m].byteLength/te,!s);m++);if(Pe.releasePrimitive(l),it.clearBuffer(),a+=o[f].byteLength/te,s)break}return jt=!1,s}function pe(n,t,e,i,o,r=0,s=0,a=0,c=0,u=null,f=!1){let d,l;f?(d=Pt,l=it):(d=it,l=Pt);const m=d.float32Array,p=d.uint32Array,w=d.uint16Array,y=l.float32Array,M=l.uint32Array,h=l.uint16Array,g=n*2,x=t*2,b=Y(g,w),A=Y(x,h);let S=!1;if(A&&b)f?S=o(Z(t,M),ie(t*2,h),Z(n,p),ie(n*2,w),c,s+t/J,a,r+n/J):S=o(Z(n,p),ie(n*2,w),Z(t,M),ie(t*2,h),a,r+n/J,c,s+t/J);else if(A){const P=Pe.getPrimitive();W(t,y,P),P.applyMatrix4(e);const E=Q(n),T=ee(n,p);W(E,m,ze),W(T,m,ke);const R=P.intersectsBox(ze),B=P.intersectsBox(ke);S=R&&pe(t,E,i,e,o,s,r,c,a+1,P,!f)||B&&pe(t,T,i,e,o,s,r,c,a+1,P,!f),Pe.releasePrimitive(P)}else{const P=Q(t),E=ee(t,M);W(P,y,Yt),W(E,y,Xt);const T=u.intersectsBox(Yt),R=u.intersectsBox(Xt);if(T&&R)S=pe(n,P,e,i,o,r,s,a,c+1,u,f)||pe(n,E,e,i,o,r,s,a,c+1,u,f);else if(T)if(b)S=pe(n,P,e,i,o,r,s,a,c+1,u,f);else{const B=Pe.getPrimitive();B.copy(Yt).applyMatrix4(e);const F=Q(n),_=ee(n,p);W(F,m,ze),W(_,m,ke);const V=B.intersectsBox(ze),U=B.intersectsBox(ke);S=V&&pe(P,F,i,e,o,s,r,c,a+1,B,!f)||U&&pe(P,_,i,e,o,s,r,c,a+1,B,!f),Pe.releasePrimitive(B)}else if(R)if(b)S=pe(n,E,e,i,o,r,s,a,c+1,u,f);else{const B=Pe.getPrimitive();B.copy(Xt).applyMatrix4(e);const F=Q(n),_=ee(n,p);W(F,m,ze),W(_,m,ke);const V=B.intersectsBox(ze),U=B.intersectsBox(ke);S=V&&pe(E,F,i,e,o,s,r,c,a+1,B,!f)||U&&pe(E,_,i,e,o,s,r,c,a+1,B,!f),Pe.releasePrimitive(B)}}return S}const Kt=new class{constructor(){let n=null,t=null,e=null,i=!1;this.root=null,this.buffer=null,this.uint32Array=null,this.uint16Array=null,this.setBVH=(r,s)=>{if(i)throw new Error("BVHTraversalHelper: cannot call setBVH during an active traversal.");this.root=s,this.buffer=n=r._roots[s],this.uint16Array=e=new Uint16Array(n),this.uint32Array=t=new Uint32Array(n)},this.reset=()=>{this.root=null,this.buffer=n=null,this.uint16Array=e=null,this.uint32Array=t=null},this.getRangeStart=r=>{let s=r*2;for(;!Y(s,e);)r=Q(r),s=r*2;return Z(r,t)},this.getRangeEnd=r=>{let s=r*2;for(;!Y(s,e);)r=ee(r,t),s=r*2;return Z(r,t)+ie(s,e)};const o=(r,s,a)=>{const c=s*2,u=Y(c,e);if(!r(a,u,s)&&!u){const d=Q(s),l=ee(s,t);o(r,d,a+1),o(r,l,a+1)}};this.traverseBuffer=r=>{if(i)throw new Error("BVHTraversalHelper: cannot start a traversal during an active traversal.");i=!0;try{o(r,0,0)}finally{i=!1}},this.traverse=r=>{this.traverseBuffer((s,a,c)=>{if(a){const u=c*2,f=t[c+6],d=e[u+14];return r(s,a,new Float32Array(n,c*4,6),f,d)}else{const u=Dt(c,t);return r(s,a,new Float32Array(n,c*4,6),u)}})}}},$n=new C.Box3,Ue=new Float32Array(6);class Ko{constructor(){this._roots=null,this.primitiveBuffer=null,this.primitiveBufferStride=null}init(t){t={...gi,...t},"maxLeafSize"in t&&(console.warn('BVH: "maxLeafSize" option has been deprecated. Use "targetLeafSize", instead.'),t={...t,targetLeafSize:t.maxLeafSize}),Wo(this,t)}getRootRanges(){throw new Error("BVH: getRootRanges() not implemented")}writePrimitiveBounds(){throw new Error("BVH: writePrimitiveBounds() not implemented")}writePrimitiveRangeBounds(t,e,i,o){let r=1/0,s=1/0,a=1/0,c=-1/0,u=-1/0,f=-1/0;for(let d=t,l=t+e;d<l;d++){this.writePrimitiveBounds(d,Ue,0);const[m,p,w,y,M,h]=Ue;m<r&&(r=m),y>c&&(c=y),p<s&&(s=p),M>u&&(u=M),w<a&&(a=w),h>f&&(f=h)}return i[o+0]=r,i[o+1]=s,i[o+2]=a,i[o+3]=c,i[o+4]=u,i[o+5]=f,i}computePrimitiveBounds(t,e,i){const o=i.offset||0;for(let r=t,s=t+e;r<s;r++){this.writePrimitiveBounds(r,Ue,0);const[a,c,u,f,d,l]=Ue,m=(a+f)/2,p=(c+d)/2,w=(u+l)/2,y=(f-a)/2,M=(d-c)/2,h=(l-u)/2,g=(r-o)*6;i[g+0]=m,i[g+1]=y+(Math.abs(m)+y)*Ct,i[g+2]=p,i[g+3]=M+(Math.abs(p)+M)*Ct,i[g+4]=w,i[g+5]=h+(Math.abs(w)+h)*Ct}return i}shiftPrimitiveOffsets(t){const e=this._indirectBuffer;if(e)for(let i=0,o=e.length;i<o;i++)e[i]+=t;else{const i=this._roots;for(let o=0;o<i.length;o++){const r=i[o],s=new Uint32Array(r),a=new Uint16Array(r),c=r.byteLength/te;for(let u=0;u<c;u++){const f=J*u,d=2*f;Y(d,a)&&(s[f+6]+=t)}}}}traverse(t,e=0){Kt.setBVH(this,e),Kt.traverse(t),Kt.reset()}refit(){const t=this._roots;for(let e=0,i=t.length;e<i;e++){const o=t[e],r=new Uint32Array(o),s=new Uint16Array(o),a=new Float32Array(o),c=o.byteLength/te;for(let u=c-1;u>=0;u--){const f=u*J,d=f*2;if(Y(d,s)){const m=Z(f,r),p=ie(d,s);this.writePrimitiveRangeBounds(m,p,Ue,0),a.set(Ue,f)}else{const m=Q(f),p=ee(f,r);for(let w=0;w<3;w++){const y=a[m+w],M=a[m+w+3],h=a[p+w],g=a[p+w+3];a[f+w]=y<h?y:h,a[f+w+3]=M>g?M:g}}}}}getBoundingBox(t){return t.makeEmpty(),this._roots.forEach(i=>{W(0,new Float32Array(i),$n),t.union($n)}),t}shapecast(t){let{boundsTraverseOrder:e,intersectsBounds:i,intersectsRange:o,intersectsPrimitive:r,scratchPrimitive:s,iterate:a}=t;if(o&&r){const d=o;o=(l,m,p,w,y)=>d(l,m,p,w,y)?!0:a(l,m,this,r,p,w,s)}else o||(r?o=(d,l,m,p)=>a(d,l,this,r,m,p,s):o=(d,l,m)=>m);let c=!1,u=0;const f=this._roots;for(let d=0,l=f.length;d<l;d++){const m=f[d];if(c=Xo(this,d,i,o,e,u),c)break;u+=m.byteLength/te}return c}bvhcast(t,e,i){let{intersectsRanges:o}=i;return jo(this,t,e,o)}}function Jo(){return typeof SharedArrayBuffer<"u"}function Nt(n){return n.index?n.index.count:n.attributes.position.count}function Lt(n){return Nt(n)/3}function xi(n,t=ArrayBuffer){return n>65535?new Uint32Array(new t(4*n)):new Uint16Array(new t(2*n))}function Zo(n,t){if(!n.index){const e=n.attributes.position.count,i=t.useSharedArrayBuffer?SharedArrayBuffer:ArrayBuffer,o=xi(e,i);n.setIndex(new C.BufferAttribute(o,1));for(let r=0;r<e;r++)o[r]=r}}function Qo(n,t,e){const i=Nt(n)/e,o=t||n.drawRange,r=o.start/e,s=(o.start+o.count)/e,a=Math.max(0,r),c=Math.min(i,s)-a;return{offset:Math.floor(a),count:Math.floor(c)}}function er(n,t){return n.groups.map(e=>({offset:e.start/t,count:e.count/t}))}function Wn(n,t,e){const i=Qo(n,t,e),o=er(n,e);if(!o.length)return[i];const r=[],s=i.offset,a=i.offset+i.count,c=Nt(n)/e,u=[];for(const l of o){const{offset:m,count:p}=l,w=m,y=isFinite(p)?p:c-m,M=m+y;w<a&&M>s&&(u.push({pos:Math.max(s,w),isStart:!0}),u.push({pos:Math.min(a,M),isStart:!1}))}u.sort((l,m)=>l.pos!==m.pos?l.pos-m.pos:l.type==="end"?-1:1);let f=0,d=null;for(const l of u){const m=l.pos;f!==0&&m!==d&&r.push({offset:d,count:m-d}),f+=l.isStart?1:-1,d=m}return r}function tr(n,t){const e=n[n.length-1],i=e.offset+e.count>2**16,o=n.reduce((u,f)=>u+f.count,0),r=i?4:2,s=t?new SharedArrayBuffer(o*r):new ArrayBuffer(o*r),a=i?new Uint32Array(s):new Uint16Array(s);let c=0;for(let u=0;u<n.length;u++){const{offset:f,count:d}=n[u];for(let l=0;l<d;l++)a[c+l]=f+l;c+=d}return a}class nr extends Ko{get indirect(){return!!this._indirectBuffer}get primitiveStride(){return null}get primitiveBufferStride(){return this.indirect?1:this.primitiveStride}set primitiveBufferStride(t){}get primitiveBuffer(){return this.indirect?this._indirectBuffer:this.geometry.index.array}set primitiveBuffer(t){}constructor(t,e={}){if(t.isBufferGeometry){if(t.index&&t.index.isInterleavedBufferAttribute)throw new Error("BVH: InterleavedBufferAttribute is not supported for the index attribute.")}else throw new Error("BVH: Only BufferGeometries are supported.");if(e.useSharedArrayBuffer&&!Jo())throw new Error("BVH: SharedArrayBuffer is not available.");super(),this.geometry=t,this.resolvePrimitiveIndex=e.indirect?i=>this._indirectBuffer[i]:i=>i,this.primitiveBuffer=null,this.primitiveBufferStride=null,this._indirectBuffer=null,e={...gi,...e},e[wn]||this.init(e)}init(t){const{geometry:e,primitiveStride:i}=this;if(t.indirect){const o=Wn(e,t.range,i),r=tr(o,t.useSharedArrayBuffer);this._indirectBuffer=r}else Zo(e,t);super.init(t),!e.boundingBox&&t.setBoundingBox&&(e.boundingBox=this.getBoundingBox(new C.Box3))}getRootRanges(t){return this.indirect?[{offset:0,count:this._indirectBuffer.length}]:Wn(this.geometry,t,this.primitiveStride)}raycastObject3D(){throw new Error("BVH: raycastObject3D() not implemented")}}class Te{constructor(){this.min=1/0,this.max=-1/0}setFromPointsField(t,e){let i=1/0,o=-1/0;for(let r=0,s=t.length;r<s;r++){const c=t[r][e];i=c<i?c:i,o=c>o?c:o}this.min=i,this.max=o}setFromPoints(t,e){let i=1/0,o=-1/0;for(let r=0,s=e.length;r<s;r++){const a=e[r],c=t.dot(a);i=c<i?c:i,o=c>o?c:o}this.min=i,this.max=o}isSeparated(t){return this.min>t.max||t.min>this.max}}Te.prototype.setFromBox=function(){const n=new C.Vector3;return function(e,i){const o=i.min,r=i.max;let s=1/0,a=-1/0;for(let c=0;c<=1;c++)for(let u=0;u<=1;u++)for(let f=0;f<=1;f++){n.x=o.x*c+r.x*(1-c),n.y=o.y*u+r.y*(1-u),n.z=o.z*f+r.z*(1-f);const d=e.dot(n);s=Math.min(d,s),a=Math.max(d,a)}this.min=s,this.max=a}}();const ir=function(){const n=new C.Vector3,t=new C.Vector3,e=new C.Vector3;return function(o,r,s){const a=o.start,c=n,u=r.start,f=t;e.subVectors(a,u),n.subVectors(o.end,o.start),t.subVectors(r.end,r.start);const d=e.dot(f),l=f.dot(c),m=f.dot(f),p=e.dot(c),y=c.dot(c)*m-l*l;let M,h;y!==0?M=(d*l-p*m)/y:M=0,h=(d+M*l)/m,s.x=M,s.y=h}}(),An=function(){const n=new C.Vector2,t=new C.Vector3,e=new C.Vector3;return function(o,r,s,a){ir(o,r,n);let c=n.x,u=n.y;if(c>=0&&c<=1&&u>=0&&u<=1){o.at(c,s),r.at(u,a);return}else if(c>=0&&c<=1){u<0?r.at(0,a):r.at(1,a),o.closestPointToPoint(a,!0,s);return}else if(u>=0&&u<=1){c<0?o.at(0,s):o.at(1,s),r.closestPointToPoint(s,!0,a);return}else{let f;c<0?f=o.start:f=o.end;let d;u<0?d=r.start:d=r.end;const l=t,m=e;if(o.closestPointToPoint(d,!0,t),r.closestPointToPoint(f,!0,e),l.distanceToSquared(d)<=m.distanceToSquared(f)){s.copy(l),a.copy(d);return}else{s.copy(f),a.copy(m);return}}}}(),or=function(){const n=new C.Vector3,t=new C.Vector3,e=new C.Plane,i=new C.Line3;return function(r,s){const{radius:a,center:c}=r,{a:u,b:f,c:d}=s;if(i.start=u,i.end=f,i.closestPointToPoint(c,!0,n).distanceTo(c)<=a||(i.start=u,i.end=d,i.closestPointToPoint(c,!0,n).distanceTo(c)<=a)||(i.start=f,i.end=d,i.closestPointToPoint(c,!0,n).distanceTo(c)<=a))return!0;const w=s.getPlane(e);if(Math.abs(w.distanceToPoint(c))<=a){const M=w.projectPoint(c,t);if(s.containsPoint(M))return!0}return!1}}(),rr=["x","y","z"],we=1e-15,Yn=we*we;function ue(n){return Math.abs(n)<we}class he extends C.Triangle{constructor(...t){super(...t),this.isExtendedTriangle=!0,this.satAxes=new Array(4).fill().map(()=>new C.Vector3),this.satBounds=new Array(4).fill().map(()=>new Te),this.points=[this.a,this.b,this.c],this.plane=new C.Plane,this.isDegenerateIntoSegment=!1,this.isDegenerateIntoPoint=!1,this.degenerateSegment=new C.Line3,this.needsUpdate=!0}intersectsSphere(t){return or(t,this)}update(){const t=this.a,e=this.b,i=this.c,o=this.points,r=this.satAxes,s=this.satBounds,a=r[0],c=s[0];this.getNormal(a),c.setFromPoints(a,o);const u=r[1],f=s[1];u.subVectors(t,e),f.setFromPoints(u,o);const d=r[2],l=s[2];d.subVectors(e,i),l.setFromPoints(d,o);const m=r[3],p=s[3];m.subVectors(i,t),p.setFromPoints(m,o);const w=u.length(),y=d.length(),M=m.length();this.isDegenerateIntoPoint=!1,this.isDegenerateIntoSegment=!1,w<we?y<we||M<we?this.isDegenerateIntoPoint=!0:(this.isDegenerateIntoSegment=!0,this.degenerateSegment.start.copy(t),this.degenerateSegment.end.copy(i)):y<we?M<we?this.isDegenerateIntoPoint=!0:(this.isDegenerateIntoSegment=!0,this.degenerateSegment.start.copy(e),this.degenerateSegment.end.copy(t)):M<we&&(this.isDegenerateIntoSegment=!0,this.degenerateSegment.start.copy(i),this.degenerateSegment.end.copy(e)),this.plane.setFromNormalAndCoplanarPoint(a,t),this.needsUpdate=!1}}he.prototype.closestPointToSegment=function(){const n=new C.Vector3,t=new C.Vector3,e=new C.Line3;return function(o,r=null,s=null){const{start:a,end:c}=o,u=this.points;let f,d=1/0;for(let l=0;l<3;l++){const m=(l+1)%3;e.start.copy(u[l]),e.end.copy(u[m]),An(e,o,n,t),f=n.distanceToSquared(t),f<d&&(d=f,r&&r.copy(n),s&&s.copy(t))}return this.closestPointToPoint(a,n),f=a.distanceToSquared(n),f<d&&(d=f,r&&r.copy(n),s&&s.copy(a)),this.closestPointToPoint(c,n),f=c.distanceToSquared(n),f<d&&(d=f,r&&r.copy(n),s&&s.copy(c)),Math.sqrt(d)}}();he.prototype.intersectsTriangle=function(){const n=new he,t=new Te,e=new Te,i=new C.Vector3,o=new C.Vector3,r=new C.Vector3,s=new C.Vector3,a=new C.Line3,c=new C.Line3,u=new C.Vector3,f=new C.Vector2,d=new C.Vector2;function l(g,x,b,A){const S=i;!g.isDegenerateIntoPoint&&!g.isDegenerateIntoSegment?S.copy(g.plane.normal):S.copy(x.plane.normal);const P=g.satBounds,E=g.satAxes;for(let B=1;B<4;B++){const F=P[B],_=E[B];if(t.setFromPoints(_,x.points),F.isSeparated(t)||(s.copy(S).cross(_),t.setFromPoints(s,g.points),e.setFromPoints(s,x.points),t.isSeparated(e)))return!1}const T=x.satBounds,R=x.satAxes;for(let B=1;B<4;B++){const F=T[B],_=R[B];if(t.setFromPoints(_,g.points),F.isSeparated(t)||(s.crossVectors(S,_),t.setFromPoints(s,g.points),e.setFromPoints(s,x.points),t.isSeparated(e)))return!1}return b&&(A||console.warn("ExtendedTriangle.intersectsTriangle: Triangles are coplanar which does not support an output edge. Setting edge to 0, 0, 0."),b.start.set(0,0,0),b.end.set(0,0,0)),!0}function m(g,x,b,A,S,P,E,T,R,B,F){let _=E/(E-T);B.x=A+(S-A)*_,F.start.subVectors(x,g).multiplyScalar(_).add(g),_=E/(E-R),B.y=A+(P-A)*_,F.end.subVectors(b,g).multiplyScalar(_).add(g)}function p(g,x,b,A,S,P,E,T,R,B,F){if(S>0)m(g.c,g.a,g.b,A,x,b,R,E,T,B,F);else if(P>0)m(g.b,g.a,g.c,b,x,A,T,E,R,B,F);else if(T*R>0||E!=0)m(g.a,g.b,g.c,x,b,A,E,T,R,B,F);else if(T!=0)m(g.b,g.a,g.c,b,x,A,T,E,R,B,F);else if(R!=0)m(g.c,g.a,g.b,A,x,b,R,E,T,B,F);else return!0;return!1}function w(g,x,b,A){const S=x.degenerateSegment,P=g.plane.distanceToPoint(S.start),E=g.plane.distanceToPoint(S.end);return ue(P)?ue(E)?l(g,x,b,A):(b&&(b.start.copy(S.start),b.end.copy(S.start)),g.containsPoint(S.start)):ue(E)?(b&&(b.start.copy(S.end),b.end.copy(S.end)),g.containsPoint(S.end)):g.plane.intersectLine(S,i)!=null?(b&&(b.start.copy(i),b.end.copy(i)),g.containsPoint(i)):!1}function y(g,x,b){const A=x.a;return ue(g.plane.distanceToPoint(A))&&g.containsPoint(A)?(b&&(b.start.copy(A),b.end.copy(A)),!0):!1}function M(g,x,b){const A=g.degenerateSegment,S=x.a;return A.closestPointToPoint(S,!0,i),S.distanceToSquared(i)<Yn?(b&&(b.start.copy(S),b.end.copy(S)),!0):!1}function h(g,x,b,A){if(g.isDegenerateIntoSegment)if(x.isDegenerateIntoSegment){const S=g.degenerateSegment,P=x.degenerateSegment,E=o,T=r;S.delta(E),P.delta(T);const R=i.subVectors(P.start,S.start),B=E.x*T.y-E.y*T.x;if(ue(B))return!1;const F=(R.x*T.y-R.y*T.x)/B,_=-(E.x*R.y-E.y*R.x)/B;if(F<0||F>1||_<0||_>1)return!1;const V=S.start.z+E.z*F,U=P.start.z+T.z*_;return ue(V-U)?(b&&(b.start.copy(S.start).addScaledVector(E,F),b.end.copy(S.start).addScaledVector(E,F)),!0):!1}else return x.isDegenerateIntoPoint?M(g,x,b):w(x,g,b,A);else{if(g.isDegenerateIntoPoint)return x.isDegenerateIntoPoint?x.a.distanceToSquared(g.a)<Yn?(b&&(b.start.copy(g.a),b.end.copy(g.a)),!0):!1:x.isDegenerateIntoSegment?M(x,g,b):y(x,g,b);if(x.isDegenerateIntoPoint)return y(g,x,b);if(x.isDegenerateIntoSegment)return w(g,x,b,A)}}return function(x,b=null,A=!1){this.needsUpdate&&this.update(),x.isExtendedTriangle?x.needsUpdate&&x.update():(n.copy(x),n.update(),x=n);const S=h(this,x,b,A);if(S!==void 0)return S;const P=this.plane,E=x.plane;let T=E.distanceToPoint(this.a),R=E.distanceToPoint(this.b),B=E.distanceToPoint(this.c);ue(T)&&(T=0),ue(R)&&(R=0),ue(B)&&(B=0);const F=T*R,_=T*B;if(F>0&&_>0)return!1;let V=P.distanceToPoint(x.a),U=P.distanceToPoint(x.b),ne=P.distanceToPoint(x.c);ue(V)&&(V=0),ue(U)&&(U=0),ue(ne)&&(ne=0);const q=V*U,$=V*ne;if(q>0&&$>0)return!1;o.copy(P.normal),r.copy(E.normal);const G=o.cross(r);let X=0,se=Math.abs(G.x);const ce=Math.abs(G.y);ce>se&&(se=ce,X=1),Math.abs(G.z)>se&&(X=2);const oe=rr[X],ye=this.a[oe],zt=this.b[oe],ve=this.c[oe],kt=x.a[oe],rt=x.b[oe],st=x.c[oe];if(p(this,ye,zt,ve,F,_,T,R,B,f,a))return l(this,x,b,A);if(p(x,kt,rt,st,q,$,V,U,ne,d,c))return l(this,x,b,A);if(f.y<f.x){const Le=f.y;f.y=f.x,f.x=Le,u.copy(a.start),a.start.copy(a.end),a.end.copy(u)}if(d.y<d.x){const Le=d.y;d.y=d.x,d.x=Le,u.copy(c.start),c.start.copy(c.end),c.end.copy(u)}return f.y<d.x||d.y<f.x?!1:(b&&(d.x>f.x?b.start.copy(c.start):b.start.copy(a.start),d.y<f.y?b.end.copy(c.end):b.end.copy(a.end)),!0)}}();he.prototype.distanceToPoint=function(){const n=new C.Vector3;return function(e){return this.closestPointToPoint(e,n),e.distanceTo(n)}}();he.prototype.distanceToTriangle=function(){const n=new C.Vector3,t=new C.Vector3,e=["a","b","c"],i=new C.Line3,o=new C.Line3;return function(s,a=null,c=null){const u=a||c?i:null;if(this.intersectsTriangle(s,u,!0))return(a||c)&&(a&&u.getCenter(a),c&&u.getCenter(c)),0;let f=1/0;for(let d=0;d<3;d++){let l;const m=e[d],p=s[m];this.closestPointToPoint(p,n),l=p.distanceToSquared(n),l<f&&(f=l,a&&a.copy(n),c&&c.copy(p));const w=this[m];s.closestPointToPoint(w,n),l=w.distanceToSquared(n),l<f&&(f=l,a&&a.copy(w),c&&c.copy(n))}for(let d=0;d<3;d++){const l=e[d],m=e[(d+1)%3];i.set(this[l],this[m]);for(let p=0;p<3;p++){const w=e[p],y=e[(p+1)%3];o.set(s[w],s[y]),An(i,o,n,t);const M=n.distanceToSquared(t);M<f&&(f=M,a&&a.copy(n),c&&c.copy(t))}}return Math.sqrt(f)}}();class ae{constructor(t,e,i){this.isOrientedBox=!0,this.min=new C.Vector3,this.max=new C.Vector3,this.matrix=new C.Matrix4,this.invMatrix=new C.Matrix4,this.points=new Array(8).fill().map(()=>new C.Vector3),this.satAxes=new Array(3).fill().map(()=>new C.Vector3),this.satBounds=new Array(3).fill().map(()=>new Te),this.alignedSatBounds=new Array(3).fill().map(()=>new Te),this.needsUpdate=!1,t&&this.min.copy(t),e&&this.max.copy(e),i&&this.matrix.copy(i)}set(t,e,i){this.min.copy(t),this.max.copy(e),this.matrix.copy(i),this.needsUpdate=!0}copy(t){this.min.copy(t.min),this.max.copy(t.max),this.matrix.copy(t.matrix),this.needsUpdate=!0}}ae.prototype.update=function(){return function(){const t=this.matrix,e=this.min,i=this.max,o=this.points;for(let u=0;u<=1;u++)for(let f=0;f<=1;f++)for(let d=0;d<=1;d++){const l=1*u|2*f|4*d,m=o[l];m.x=u?i.x:e.x,m.y=f?i.y:e.y,m.z=d?i.z:e.z,m.applyMatrix4(t)}const r=this.satBounds,s=this.satAxes,a=o[0];for(let u=0;u<3;u++){const f=s[u],d=r[u],l=1<<u,m=o[l];f.subVectors(a,m),d.setFromPoints(f,o)}const c=this.alignedSatBounds;c[0].setFromPointsField(o,"x"),c[1].setFromPointsField(o,"y"),c[2].setFromPointsField(o,"z"),this.invMatrix.copy(this.matrix).invert(),this.needsUpdate=!1}}();ae.prototype.intersectsBox=function(){const n=new Te;return function(e){this.needsUpdate&&this.update();const i=e.min,o=e.max,r=this.satBounds,s=this.satAxes,a=this.alignedSatBounds;if(n.min=i.x,n.max=o.x,a[0].isSeparated(n)||(n.min=i.y,n.max=o.y,a[1].isSeparated(n))||(n.min=i.z,n.max=o.z,a[2].isSeparated(n)))return!1;for(let c=0;c<3;c++){const u=s[c],f=r[c];if(n.setFromBox(u,e),f.isSeparated(n))return!1}return!0}}();ae.prototype.intersectsTriangle=function(){const n=new he,t=new Array(3),e=new Te,i=new Te,o=new C.Vector3;return function(s){this.needsUpdate&&this.update(),s.isExtendedTriangle?s.needsUpdate&&s.update():(n.copy(s),n.update(),s=n);const a=this.satBounds,c=this.satAxes;t[0]=s.a,t[1]=s.b,t[2]=s.c;for(let l=0;l<3;l++){const m=a[l],p=c[l];if(e.setFromPoints(p,t),m.isSeparated(e))return!1}const u=s.satBounds,f=s.satAxes,d=this.points;for(let l=0;l<3;l++){const m=u[l],p=f[l];if(e.setFromPoints(p,d),m.isSeparated(e))return!1}for(let l=0;l<3;l++){const m=c[l];for(let p=0;p<4;p++){const w=f[p];if(o.crossVectors(m,w),e.setFromPoints(o,t),i.setFromPoints(o,d),e.isSeparated(i))return!1}}return!0}}();ae.prototype.closestPointToPoint=function(){return function(t,e){return this.needsUpdate&&this.update(),e.copy(t).applyMatrix4(this.invMatrix).clamp(this.min,this.max).applyMatrix4(this.matrix),e}}();ae.prototype.distanceToPoint=function(){const n=new C.Vector3;return function(e){return this.closestPointToPoint(e,n),e.distanceTo(n)}}();ae.prototype.distanceToBox=function(){const n=["x","y","z"],t=new Array(12).fill().map(()=>new C.Line3),e=new Array(12).fill().map(()=>new C.Line3),i=new C.Vector3,o=new C.Vector3;return function(s,a=0,c=null,u=null){if(this.needsUpdate&&this.update(),this.intersectsBox(s))return(c||u)&&(s.getCenter(o),this.closestPointToPoint(o,i),s.closestPointToPoint(i,o),c&&c.copy(i),u&&u.copy(o)),0;const f=a*a,d=s.min,l=s.max,m=this.points;let p=1/0;for(let y=0;y<8;y++){const M=m[y];o.copy(M).clamp(d,l);const h=M.distanceToSquared(o);if(h<p&&(p=h,c&&c.copy(M),u&&u.copy(o),h<f))return Math.sqrt(h)}let w=0;for(let y=0;y<3;y++)for(let M=0;M<=1;M++)for(let h=0;h<=1;h++){const g=(y+1)%3,x=(y+2)%3,b=M<<g|h<<x,A=1<<y|M<<g|h<<x,S=m[b],P=m[A];t[w].set(S,P);const T=n[y],R=n[g],B=n[x],F=e[w],_=F.start,V=F.end;_[T]=d[T],_[R]=M?d[R]:l[R],_[B]=h?d[B]:l[R],V[T]=l[T],V[R]=M?d[R]:l[R],V[B]=h?d[B]:l[R],w++}for(let y=0;y<=1;y++)for(let M=0;M<=1;M++)for(let h=0;h<=1;h++){o.x=y?l.x:d.x,o.y=M?l.y:d.y,o.z=h?l.z:d.z,this.closestPointToPoint(o,i);const g=o.distanceToSquared(i);if(g<p&&(p=g,c&&c.copy(i),u&&u.copy(o),g<f))return Math.sqrt(g)}for(let y=0;y<12;y++){const M=t[y];for(let h=0;h<12;h++){const g=e[h];An(M,g,i,o);const x=i.distanceToSquared(o);if(x<p&&(p=x,c&&c.copy(i),u&&u.copy(o),x<f))return Math.sqrt(x)}}return Math.sqrt(p)}}();class sr extends Mn{constructor(){super(()=>new he)}}const fe=new sr,Ke=new C.Vector3,Jt=new C.Vector3;function ar(n,t,e={},i=0,o=1/0){const r=i*i,s=o*o;let a=1/0,c=null;if(n.shapecast({boundsTraverseOrder:f=>(Ke.copy(t).clamp(f.min,f.max),Ke.distanceToSquared(t)),intersectsBounds:(f,d,l)=>l<a&&l<s,intersectsTriangle:(f,d)=>{f.closestPointToPoint(t,Ke);const l=t.distanceToSquared(Ke);return l<a&&(Jt.copy(Ke),a=l,c=d),l<r}}),a===1/0)return null;const u=Math.sqrt(a);return e.point?e.point.copy(Jt):e.point=Jt.clone(),e.distance=u,e.faceIndex=c,e}const mt=parseInt(C.REVISION)>=169,cr=parseInt(C.REVISION)<=161,_e=new C.Vector3,De=new C.Vector3,Ne=new C.Vector3,pt=new C.Vector2,ht=new C.Vector2,yt=new C.Vector2,Xn=new C.Vector3,jn=new C.Vector3,Kn=new C.Vector3,Je=new C.Vector3;function lr(n,t,e,i,o,r,s,a){let c;if(r===C.BackSide?c=n.intersectTriangle(i,e,t,!0,o):c=n.intersectTriangle(t,e,i,r!==C.DoubleSide,o),c===null)return null;const u=n.origin.distanceTo(o);return u<s||u>a?null:{distance:u,point:o.clone()}}function Jn(n,t,e,i,o,r,s,a,c,u,f){_e.fromBufferAttribute(t,r),De.fromBufferAttribute(t,s),Ne.fromBufferAttribute(t,a);const d=lr(n,_e,De,Ne,Je,c,u,f);if(d){if(i){pt.fromBufferAttribute(i,r),ht.fromBufferAttribute(i,s),yt.fromBufferAttribute(i,a),d.uv=new C.Vector2;const m=C.Triangle.getInterpolation(Je,_e,De,Ne,pt,ht,yt,d.uv);mt||(d.uv=m)}if(o){pt.fromBufferAttribute(o,r),ht.fromBufferAttribute(o,s),yt.fromBufferAttribute(o,a),d.uv1=new C.Vector2;const m=C.Triangle.getInterpolation(Je,_e,De,Ne,pt,ht,yt,d.uv1);mt||(d.uv1=m),cr&&(d.uv2=d.uv1)}if(e){Xn.fromBufferAttribute(e,r),jn.fromBufferAttribute(e,s),Kn.fromBufferAttribute(e,a),d.normal=new C.Vector3;const m=C.Triangle.getInterpolation(Je,_e,De,Ne,Xn,jn,Kn,d.normal);d.normal.dot(n.direction)>0&&d.normal.multiplyScalar(-1),mt||(d.normal=m)}const l={a:r,b:s,c:a,normal:new C.Vector3,materialIndex:0};if(C.Triangle.getNormal(_e,De,Ne,l.normal),d.face=l,d.faceIndex=r,mt){const m=new C.Vector3;C.Triangle.getBarycoord(Je,_e,De,Ne,m),d.barycoord=m}}return d}function Zn(n){return n&&n.isMaterial?n.side:n}function Vt(n,t,e,i,o,r,s){const a=i*3;let c=a+0,u=a+1,f=a+2;const{index:d,groups:l}=n;n.index&&(c=d.getX(c),u=d.getX(u),f=d.getX(f));const{position:m,normal:p,uv:w,uv1:y}=n.attributes;if(Array.isArray(t)){const M=i*3;for(let h=0,g=l.length;h<g;h++){const{start:x,count:b,materialIndex:A}=l[h];if(M>=x&&M<x+b){const S=Zn(t[A]),P=Jn(e,m,p,w,y,c,u,f,S,r,s);if(P)if(P.faceIndex=i,P.face.materialIndex=A,o)o.push(P);else return P}}}else{const M=Zn(t),h=Jn(e,m,p,w,y,c,u,f,M,r,s);if(h)if(h.faceIndex=i,h.face.materialIndex=0,o)o.push(h);else return h}return null}function K(n,t,e,i){const o=n.a,r=n.b,s=n.c;let a=t,c=t+1,u=t+2;e&&(a=e.getX(a),c=e.getX(c),u=e.getX(u)),o.x=i.getX(a),o.y=i.getY(a),o.z=i.getZ(a),r.x=i.getX(c),r.y=i.getY(c),r.z=i.getZ(c),s.x=i.getX(u),s.y=i.getY(u),s.z=i.getZ(u)}function ur(n,t,e,i,o,r,s,a){const{geometry:c,_indirectBuffer:u}=n;for(let f=i,d=i+o;f<d;f++)Vt(c,t,e,f,r,s,a)}function fr(n,t,e,i,o,r,s){const{geometry:a,_indirectBuffer:c}=n;let u=1/0,f=null;for(let d=i,l=i+o;d<l;d++){let m;m=Vt(a,t,e,d,null,r,s),m&&m.distance<u&&(f=m,u=m.distance)}return f}function dr(n,t,e,i,o,r,s){const{geometry:a}=e,{index:c}=a,u=a.attributes.position;for(let f=n,d=t+n;f<d;f++){let l;if(l=f,K(s,l*3,c,u),s.needsUpdate=!0,i(s,l,o,r))return!0}return!1}function mr(n,t=null){t&&Array.isArray(t)&&(t=new Set(t));const e=n.geometry,i=e.index?e.index.array:null,o=e.attributes.position;let r,s,a,c,u=0;const f=n._roots;for(let l=0,m=f.length;l<m;l++)r=f[l],s=new Uint32Array(r),a=new Uint16Array(r),c=new Float32Array(r),d(0,u),u+=r.byteLength;function d(l,m,p=!1){const w=l*2;if(Y(w,a)){const y=Z(l,s),M=ie(w,a);let h=1/0,g=1/0,x=1/0,b=-1/0,A=-1/0,S=-1/0;for(let P=3*y,E=3*(y+M);P<E;P++){let T=i[P];const R=o.getX(T),B=o.getY(T),F=o.getZ(T);R<h&&(h=R),R>b&&(b=R),B<g&&(g=B),B>A&&(A=B),F<x&&(x=F),F>S&&(S=F)}return c[l+0]!==h||c[l+1]!==g||c[l+2]!==x||c[l+3]!==b||c[l+4]!==A||c[l+5]!==S?(c[l+0]=h,c[l+1]=g,c[l+2]=x,c[l+3]=b,c[l+4]=A,c[l+5]=S,!0):!1}else{const y=Q(l),M=ee(l,s);let h=p,g=!1,x=!1;if(t){if(!h){const T=y/J+m/te,R=M/J+m/te;g=t.has(T),x=t.has(R),h=!g&&!x}}else g=!0,x=!0;const b=h||g,A=h||x;let S=!1;b&&(S=d(y,m,h));let P=!1;A&&(P=d(M,m,h));const E=S||P;if(E)for(let T=0;T<3;T++){const R=y+T,B=M+T,F=c[R],_=c[R+3],V=c[B],U=c[B+3];c[l+T]=F<V?F:V,c[l+T+3]=_>U?_:U}return E}}}function Re(n,t,e,i,o){let r,s,a,c,u,f;const d=1/e.direction.x,l=1/e.direction.y,m=1/e.direction.z,p=e.origin.x,w=e.origin.y,y=e.origin.z;let M=t[n],h=t[n+3],g=t[n+1],x=t[n+3+1],b=t[n+2],A=t[n+3+2];return d>=0?(r=(M-p)*d,s=(h-p)*d):(r=(h-p)*d,s=(M-p)*d),l>=0?(a=(g-w)*l,c=(x-w)*l):(a=(x-w)*l,c=(g-w)*l),r>c||a>s||((a>r||isNaN(r))&&(r=a),(c<s||isNaN(s))&&(s=c),m>=0?(u=(b-y)*m,f=(A-y)*m):(u=(A-y)*m,f=(b-y)*m),r>f||u>s)?!1:((u>r||r!==r)&&(r=u),(f<s||s!==s)&&(s=f),r<=o&&s>=i)}function pr(n,t,e,i,o,r,s,a){const{geometry:c,_indirectBuffer:u}=n;for(let f=i,d=i+o;f<d;f++){let l=u?u[f]:f;Vt(c,t,e,l,r,s,a)}}function hr(n,t,e,i,o,r,s){const{geometry:a,_indirectBuffer:c}=n;let u=1/0,f=null;for(let d=i,l=i+o;d<l;d++){let m;m=Vt(a,t,e,c?c[d]:d,null,r,s),m&&m.distance<u&&(f=m,u=m.distance)}return f}function yr(n,t,e,i,o,r,s){const{geometry:a}=e,{index:c}=a,u=a.attributes.position;for(let f=n,d=t+n;f<d;f++){let l;if(l=e.resolveTriangleIndex(f),K(s,l*3,c,u),s.needsUpdate=!0,i(s,l,o,r))return!0}return!1}function gr(n,t,e,i,o,r,s){H.setBuffer(n._roots[t]),cn(0,n,e,i,o,r,s),H.clearBuffer()}function cn(n,t,e,i,o,r,s){const{float32Array:a,uint16Array:c,uint32Array:u}=H,f=n*2;if(Y(f,c)){const l=Z(n,u),m=ie(f,c);ur(t,e,i,l,m,o,r,s)}else{const l=Q(n);Re(l,a,i,r,s)&&cn(l,t,e,i,o,r,s);const m=ee(n,u);Re(m,a,i,r,s)&&cn(m,t,e,i,o,r,s)}}const vr=["x","y","z"];function br(n,t,e,i,o,r){H.setBuffer(n._roots[t]);const s=ln(0,n,e,i,o,r);return H.clearBuffer(),s}function ln(n,t,e,i,o,r){const{float32Array:s,uint16Array:a,uint32Array:c}=H;let u=n*2;if(Y(u,a)){const d=Z(n,c),l=ie(u,a);return fr(t,e,i,d,l,o,r)}else{const d=Dt(n,c),l=vr[d],p=i.direction[l]>=0;let w,y;p?(w=Q(n),y=ee(n,c)):(w=ee(n,c),y=Q(n));const h=Re(w,s,i,o,r)?ln(w,t,e,i,o,r):null;if(h){const b=h.point[l];if(p?b<=s[y+d]:b>=s[y+d+3])return h}const x=Re(y,s,i,o,r)?ln(y,t,e,i,o,r):null;return h&&x?h.distance<=x.distance?h:x:h||x||null}}const gt=new C.Box3,Ge=new he,Oe=new he,Ze=new C.Matrix4,Qn=new ae,vt=new ae;function xr(n,t,e,i){H.setBuffer(n._roots[t]);const o=un(0,n,e,i);return H.clearBuffer(),o}function un(n,t,e,i,o=null){const{float32Array:r,uint16Array:s,uint32Array:a}=H;let c=n*2;if(o===null&&(e.boundingBox||e.computeBoundingBox(),Qn.set(e.boundingBox.min,e.boundingBox.max,i),o=Qn),Y(c,s)){const f=t.geometry,d=f.index,l=f.attributes.position,m=e.index,p=e.attributes.position,w=Z(n,a),y=ie(c,s);if(Ze.copy(i).invert(),e.boundsTree)return W(n,r,vt),vt.matrix.copy(Ze),vt.needsUpdate=!0,e.boundsTree.shapecast({intersectsBounds:h=>vt.intersectsBox(h),intersectsTriangle:h=>{h.a.applyMatrix4(i),h.b.applyMatrix4(i),h.c.applyMatrix4(i),h.needsUpdate=!0;for(let g=w*3,x=(y+w)*3;g<x;g+=3)if(K(Oe,g,d,l),Oe.needsUpdate=!0,h.intersectsTriangle(Oe))return!0;return!1}});{const M=Lt(e);for(let h=w*3,g=(y+w)*3;h<g;h+=3){K(Ge,h,d,l),Ge.a.applyMatrix4(Ze),Ge.b.applyMatrix4(Ze),Ge.c.applyMatrix4(Ze),Ge.needsUpdate=!0;for(let x=0,b=M*3;x<b;x+=3)if(K(Oe,x,m,p),Oe.needsUpdate=!0,Ge.intersectsTriangle(Oe))return!0}}}else{const f=Q(n),d=ee(n,a);return W(f,r,gt),!!(o.intersectsBox(gt)&&un(f,t,e,i,o)||(W(d,r,gt),o.intersectsBox(gt)&&un(d,t,e,i,o)))}}const bt=new C.Matrix4,Zt=new ae,Qe=new ae,wr=new C.Vector3,Mr=new C.Vector3,Ar=new C.Vector3,Sr=new C.Vector3;function Tr(n,t,e,i={},o={},r=0,s=1/0){t.boundingBox||t.computeBoundingBox(),Zt.set(t.boundingBox.min,t.boundingBox.max,e),Zt.needsUpdate=!0;const a=n.geometry,c=a.attributes.position,u=a.index,f=t.attributes.position,d=t.index,l=fe.getPrimitive(),m=fe.getPrimitive();let p=wr,w=Mr,y=null,M=null;o&&(y=Ar,M=Sr);let h=1/0,g=null,x=null;return bt.copy(e).invert(),Qe.matrix.copy(bt),n.shapecast({boundsTraverseOrder:b=>Zt.distanceToBox(b),intersectsBounds:(b,A,S)=>S<h&&S<s?(A&&(Qe.min.copy(b.min),Qe.max.copy(b.max),Qe.needsUpdate=!0),!0):!1,intersectsRange:(b,A)=>{if(t.boundsTree)return t.boundsTree.shapecast({boundsTraverseOrder:P=>Qe.distanceToBox(P),intersectsBounds:(P,E,T)=>T<h&&T<s,intersectsRange:(P,E)=>{for(let T=P,R=P+E;T<R;T++){K(m,3*T,d,f),m.a.applyMatrix4(e),m.b.applyMatrix4(e),m.c.applyMatrix4(e),m.needsUpdate=!0;for(let B=b,F=b+A;B<F;B++){K(l,3*B,u,c),l.needsUpdate=!0;const _=l.distanceToTriangle(m,p,y);if(_<h&&(w.copy(p),M&&M.copy(y),h=_,g=B,x=T),_<r)return!0}}}});{const S=Lt(t);for(let P=0,E=S;P<E;P++){K(m,3*P,d,f),m.a.applyMatrix4(e),m.b.applyMatrix4(e),m.c.applyMatrix4(e),m.needsUpdate=!0;for(let T=b,R=b+A;T<R;T++){K(l,3*T,u,c),l.needsUpdate=!0;const B=l.distanceToTriangle(m,p,y);if(B<h&&(w.copy(p),M&&M.copy(y),h=B,g=T,x=P),B<r)return!0}}}}}),fe.releasePrimitive(l),fe.releasePrimitive(m),h===1/0?null:(i.point?i.point.copy(w):i.point=w.clone(),i.distance=h,i.faceIndex=g,o&&(o.point?o.point.copy(M):o.point=M.clone(),o.point.applyMatrix4(bt),w.applyMatrix4(bt),o.distance=w.sub(o.point).length(),o.faceIndex=x),i)}function Cr(n,t=null){t&&Array.isArray(t)&&(t=new Set(t));const e=n.geometry,i=e.index?e.index.array:null,o=e.attributes.position;let r,s,a,c,u=0;const f=n._roots;for(let l=0,m=f.length;l<m;l++)r=f[l],s=new Uint32Array(r),a=new Uint16Array(r),c=new Float32Array(r),d(0,u),u+=r.byteLength;function d(l,m,p=!1){const w=l*2;if(Y(w,a)){const y=Z(l,s),M=ie(w,a);let h=1/0,g=1/0,x=1/0,b=-1/0,A=-1/0,S=-1/0;for(let P=y,E=y+M;P<E;P++){const T=3*n.resolveTriangleIndex(P);for(let R=0;R<3;R++){let B=T+R;B=i?i[B]:B;const F=o.getX(B),_=o.getY(B),V=o.getZ(B);F<h&&(h=F),F>b&&(b=F),_<g&&(g=_),_>A&&(A=_),V<x&&(x=V),V>S&&(S=V)}}return c[l+0]!==h||c[l+1]!==g||c[l+2]!==x||c[l+3]!==b||c[l+4]!==A||c[l+5]!==S?(c[l+0]=h,c[l+1]=g,c[l+2]=x,c[l+3]=b,c[l+4]=A,c[l+5]=S,!0):!1}else{const y=Q(l),M=ee(l,s);let h=p,g=!1,x=!1;if(t){if(!h){const T=y/J+m/te,R=M/J+m/te;g=t.has(T),x=t.has(R),h=!g&&!x}}else g=!0,x=!0;const b=h||g,A=h||x;let S=!1;b&&(S=d(y,m,h));let P=!1;A&&(P=d(M,m,h));const E=S||P;if(E)for(let T=0;T<3;T++){const R=y+T,B=M+T,F=c[R],_=c[R+3],V=c[B],U=c[B+3];c[l+T]=F<V?F:V,c[l+T+3]=_>U?_:U}return E}}}function Br(n,t,e,i,o,r,s){H.setBuffer(n._roots[t]),fn(0,n,e,i,o,r,s),H.clearBuffer()}function fn(n,t,e,i,o,r,s){const{float32Array:a,uint16Array:c,uint32Array:u}=H,f=n*2;if(Y(f,c)){const l=Z(n,u),m=ie(f,c);pr(t,e,i,l,m,o,r,s)}else{const l=Q(n);Re(l,a,i,r,s)&&fn(l,t,e,i,o,r,s);const m=ee(n,u);Re(m,a,i,r,s)&&fn(m,t,e,i,o,r,s)}}const Pr=["x","y","z"];function Ir(n,t,e,i,o,r){H.setBuffer(n._roots[t]);const s=dn(0,n,e,i,o,r);return H.clearBuffer(),s}function dn(n,t,e,i,o,r){const{float32Array:s,uint16Array:a,uint32Array:c}=H;let u=n*2;if(Y(u,a)){const d=Z(n,c),l=ie(u,a);return hr(t,e,i,d,l,o,r)}else{const d=Dt(n,c),l=Pr[d],p=i.direction[l]>=0;let w,y;p?(w=Q(n),y=ee(n,c)):(w=ee(n,c),y=Q(n));const h=Re(w,s,i,o,r)?dn(w,t,e,i,o,r):null;if(h){const b=h.point[l];if(p?b<=s[y+d]:b>=s[y+d+3])return h}const x=Re(y,s,i,o,r)?dn(y,t,e,i,o,r):null;return h&&x?h.distance<=x.distance?h:x:h||x||null}}const xt=new C.Box3,He=new he,qe=new he,et=new C.Matrix4,ei=new ae,wt=new ae;function Rr(n,t,e,i){H.setBuffer(n._roots[t]);const o=mn(0,n,e,i);return H.clearBuffer(),o}function mn(n,t,e,i,o=null){const{float32Array:r,uint16Array:s,uint32Array:a}=H;let c=n*2;if(o===null&&(e.boundingBox||e.computeBoundingBox(),ei.set(e.boundingBox.min,e.boundingBox.max,i),o=ei),Y(c,s)){const f=t.geometry,d=f.index,l=f.attributes.position,m=e.index,p=e.attributes.position,w=Z(n,a),y=ie(c,s);if(et.copy(i).invert(),e.boundsTree)return W(n,r,wt),wt.matrix.copy(et),wt.needsUpdate=!0,e.boundsTree.shapecast({intersectsBounds:h=>wt.intersectsBox(h),intersectsTriangle:h=>{h.a.applyMatrix4(i),h.b.applyMatrix4(i),h.c.applyMatrix4(i),h.needsUpdate=!0;for(let g=w,x=y+w;g<x;g++)if(K(qe,3*t.resolveTriangleIndex(g),d,l),qe.needsUpdate=!0,h.intersectsTriangle(qe))return!0;return!1}});{const M=Lt(e);for(let h=w,g=y+w;h<g;h++){const x=t.resolveTriangleIndex(h);K(He,3*x,d,l),He.a.applyMatrix4(et),He.b.applyMatrix4(et),He.c.applyMatrix4(et),He.needsUpdate=!0;for(let b=0,A=M*3;b<A;b+=3)if(K(qe,b,m,p),qe.needsUpdate=!0,He.intersectsTriangle(qe))return!0}}}else{const f=Q(n),d=ee(n,a);return W(f,r,xt),!!(o.intersectsBox(xt)&&mn(f,t,e,i,o)||(W(d,r,xt),o.intersectsBox(xt)&&mn(d,t,e,i,o)))}}const Mt=new C.Matrix4,Qt=new ae,tt=new ae,Er=new C.Vector3,Fr=new C.Vector3,_r=new C.Vector3,Dr=new C.Vector3;function Nr(n,t,e,i={},o={},r=0,s=1/0){t.boundingBox||t.computeBoundingBox(),Qt.set(t.boundingBox.min,t.boundingBox.max,e),Qt.needsUpdate=!0;const a=n.geometry,c=a.attributes.position,u=a.index,f=t.attributes.position,d=t.index,l=fe.getPrimitive(),m=fe.getPrimitive();let p=Er,w=Fr,y=null,M=null;o&&(y=_r,M=Dr);let h=1/0,g=null,x=null;return Mt.copy(e).invert(),tt.matrix.copy(Mt),n.shapecast({boundsTraverseOrder:b=>Qt.distanceToBox(b),intersectsBounds:(b,A,S)=>S<h&&S<s?(A&&(tt.min.copy(b.min),tt.max.copy(b.max),tt.needsUpdate=!0),!0):!1,intersectsRange:(b,A)=>{if(t.boundsTree){const S=t.boundsTree;return S.shapecast({boundsTraverseOrder:P=>tt.distanceToBox(P),intersectsBounds:(P,E,T)=>T<h&&T<s,intersectsRange:(P,E)=>{for(let T=P,R=P+E;T<R;T++){const B=S.resolveTriangleIndex(T);K(m,3*B,d,f),m.a.applyMatrix4(e),m.b.applyMatrix4(e),m.c.applyMatrix4(e),m.needsUpdate=!0;for(let F=b,_=b+A;F<_;F++){const V=n.resolveTriangleIndex(F);K(l,3*V,u,c),l.needsUpdate=!0;const U=l.distanceToTriangle(m,p,y);if(U<h&&(w.copy(p),M&&M.copy(y),h=U,g=F,x=T),U<r)return!0}}}})}else{const S=Lt(t);for(let P=0,E=S;P<E;P++){K(m,3*P,d,f),m.a.applyMatrix4(e),m.b.applyMatrix4(e),m.c.applyMatrix4(e),m.needsUpdate=!0;for(let T=b,R=b+A;T<R;T++){const B=n.resolveTriangleIndex(T);K(l,3*B,u,c),l.needsUpdate=!0;const F=l.distanceToTriangle(m,p,y);if(F<h&&(w.copy(p),M&&M.copy(y),h=F,g=T,x=P),F<r)return!0}}}}}),fe.releasePrimitive(l),fe.releasePrimitive(m),h===1/0?null:(i.point?i.point.copy(w):i.point=w.clone(),i.distance=h,i.faceIndex=g,o&&(o.point?o.point.copy(M):o.point=M.clone(),o.point.applyMatrix4(Mt),w.applyMatrix4(Mt),o.distance=w.sub(o.point).length(),o.faceIndex=x),i)}function ti(n,t,e){return n===null?null:(n.point.applyMatrix4(t.matrixWorld),n.distance=n.point.distanceTo(e.ray.origin),n.object=t,n)}const At=new ae,St=new C.Ray,ni=new C.Vector3,ii=new C.Matrix4,oi=new C.Vector3,en=["getX","getY","getZ"];class It extends nr{static serialize(t,e={}){e={cloneBuffers:!0,...e};const i=t.geometry,o=t._roots,r=t._indirectBuffer,s=i.getIndex(),a={version:1,roots:null,index:null,indirectBuffer:null};return e.cloneBuffers?(a.roots=o.map(c=>c.slice()),a.index=s?s.array.slice():null,a.indirectBuffer=r?r.slice():null):(a.roots=o,a.index=s?s.array:null,a.indirectBuffer=r),a}static deserialize(t,e,i={}){i={setIndex:!0,indirect:!!t.indirectBuffer,...i};const{index:o,roots:r,indirectBuffer:s}=t;t.version||(console.warn("MeshBVH.deserialize: Serialization format has been changed and will be fixed up. It is recommended to regenerate any stored serialized data."),c(r));const a=new It(e,{...i,[wn]:!0});if(a._roots=r,a._indirectBuffer=s||null,i.setIndex){const u=e.getIndex();if(u===null){const f=new C.BufferAttribute(t.index,1,!1);e.setIndex(f)}else u.array!==o&&(u.array.set(o),u.needsUpdate=!0)}return a;function c(u){for(let f=0;f<u.length;f++){const d=u[f],l=new Uint32Array(d),m=new Uint16Array(d);for(let p=0,w=d.byteLength/te;p<w;p++){const y=J*p,M=2*y;Y(M,m)||(l[y+6]=l[y+6]/J-p)}}}}get primitiveStride(){return 3}get resolveTriangleIndex(){return this.resolvePrimitiveIndex}constructor(t,e={}){e.maxLeafTris&&(console.warn('MeshBVH: "maxLeafTris" option has been deprecated. Use "targetLeafSize", instead.'),e={...e,targetLeafSize:e.maxLeafTris}),super(t,e)}shiftTriangleOffsets(t){return super.shiftPrimitiveOffsets(t)}writePrimitiveBounds(t,e,i){const o=this.geometry,r=this._indirectBuffer,s=o.attributes.position,a=o.index?o.index.array:null,u=(r?r[t]:t)*3;let f=u+0,d=u+1,l=u+2;a&&(f=a[f],d=a[d],l=a[l]);for(let m=0;m<3;m++){const p=s[en[m]](f),w=s[en[m]](d),y=s[en[m]](l);let M=p;w<M&&(M=w),y<M&&(M=y);let h=p;w>h&&(h=w),y>h&&(h=y),e[i+m]=M,e[i+m+3]=h}return e}computePrimitiveBounds(t,e,i){const o=this.geometry,r=this._indirectBuffer,s=o.attributes.position,a=o.index?o.index.array:null,c=s.normalized;if(t<0||e+t-i.offset>i.length/6)throw new Error("MeshBVH: compute triangle bounds range is invalid.");const u=s.array,f=s.offset||0;let d=3;s.isInterleavedBufferAttribute&&(d=s.data.stride);const l=["getX","getY","getZ"],m=i.offset;for(let p=t,w=t+e;p<w;p++){const M=(r?r[p]:p)*3,h=(p-m)*6;let g=M+0,x=M+1,b=M+2;a&&(g=a[g],x=a[x],b=a[b]),c||(g=g*d+f,x=x*d+f,b=b*d+f);for(let A=0;A<3;A++){let S,P,E;c?(S=s[l[A]](g),P=s[l[A]](x),E=s[l[A]](b)):(S=u[g+A],P=u[x+A],E=u[b+A]);let T=S;P<T&&(T=P),E<T&&(T=E);let R=S;P>R&&(R=P),E>R&&(R=E);const B=(R-T)/2,F=A*2;i[h+F+0]=T+B,i[h+F+1]=B+(Math.abs(T)+B)*Ct}}return i}raycastObject3D(t,e,i=[]){const{material:o}=t;if(o===void 0)return;ii.copy(t.matrixWorld).invert(),St.copy(e.ray).applyMatrix4(ii),oi.setFromMatrixScale(t.matrixWorld),ni.copy(St.direction).multiply(oi);const r=ni.length(),s=e.near/r,a=e.far/r;if(e.firstHitOnly===!0){let c=this.raycastFirst(St,o,s,a);c=ti(c,t,e),c&&i.push(c)}else{const c=this.raycast(St,o,s,a);for(let u=0,f=c.length;u<f;u++){const d=ti(c[u],t,e);d&&i.push(d)}}return i}refit(t=null){return(this.indirect?Cr:mr)(this,t)}raycast(t,e=C.FrontSide,i=0,o=1/0){const r=this._roots,s=[],a=this.indirect?Br:gr;for(let c=0,u=r.length;c<u;c++)a(this,c,e,t,s,i,o);return s}raycastFirst(t,e=C.FrontSide,i=0,o=1/0){const r=this._roots;let s=null;const a=this.indirect?Ir:br;for(let c=0,u=r.length;c<u;c++){const f=a(this,c,e,t,i,o);f!=null&&(s==null||f.distance<s.distance)&&(s=f)}return s}intersectsGeometry(t,e){let i=!1;const o=this._roots,r=this.indirect?Rr:xr;for(let s=0,a=o.length;s<a&&(i=r(this,s,t,e),!i);s++);return i}shapecast(t){const e=fe.getPrimitive(),i=super.shapecast({...t,intersectsPrimitive:t.intersectsTriangle,scratchPrimitive:e,iterate:this.indirect?yr:dr});return fe.releasePrimitive(e),i}bvhcast(t,e,i){let{intersectsRanges:o,intersectsTriangles:r}=i;const s=fe.getPrimitive(),a=this.geometry.index,c=this.geometry.attributes.position,u=this.indirect?p=>{const w=this.resolveTriangleIndex(p);K(s,w*3,a,c)}:p=>{K(s,p*3,a,c)},f=fe.getPrimitive(),d=t.geometry.index,l=t.geometry.attributes.position,m=t.indirect?p=>{const w=t.resolveTriangleIndex(p);K(f,w*3,d,l)}:p=>{K(f,p*3,d,l)};if(r){if(!(t instanceof It))throw new Error('MeshBVH: "intersectsTriangles" callback can only be used with another MeshBVH.');const p=(w,y,M,h,g,x,b,A)=>{for(let S=M,P=M+h;S<P;S++){m(S),f.a.applyMatrix4(e),f.b.applyMatrix4(e),f.c.applyMatrix4(e),f.needsUpdate=!0;for(let E=w,T=w+y;E<T;E++)if(u(E),s.needsUpdate=!0,r(s,f,E,S,g,x,b,A))return!0}return!1};if(o){const w=o;o=function(y,M,h,g,x,b,A,S){return w(y,M,h,g,x,b,A,S)?!0:p(y,M,h,g,x,b,A,S)}}else o=p}return super.bvhcast(t,e,{intersectsRanges:o})}intersectsBox(t,e){return At.set(t.min,t.max,e),At.needsUpdate=!0,this.shapecast({intersectsBounds:i=>At.intersectsBox(i),intersectsTriangle:i=>At.intersectsTriangle(i)})}intersectsSphere(t){return this.shapecast({intersectsBounds:e=>t.intersectsBox(e),intersectsTriangle:e=>e.intersectsSphere(t)})}closestPointToGeometry(t,e,i={},o={},r=0,s=1/0){return(this.indirect?Nr:Tr)(this,t,e,i,o,r,s)}closestPointToPoint(t,e={},i=0,o=1/0){return ar(this,t,e,i,o)}}function Lr(n){switch(n){case 1:return"R";case 2:return"RG";case 3:return"RGBA";case 4:return"RGBA"}throw new Error}function Vr(n){switch(n){case 1:return C.RedFormat;case 2:return C.RGFormat;case 3:return C.RGBAFormat;case 4:return C.RGBAFormat}}function ri(n){switch(n){case 1:return C.RedIntegerFormat;case 2:return C.RGIntegerFormat;case 3:return C.RGBAIntegerFormat;case 4:return C.RGBAIntegerFormat}}class wi extends C.DataTexture{constructor(){super(),this.minFilter=C.NearestFilter,this.magFilter=C.NearestFilter,this.generateMipmaps=!1,this.overrideItemSize=null,this._forcedType=null}updateFrom(t){const e=this.overrideItemSize,i=t.itemSize,o=t.count;if(e!==null){if(i*o%e!==0)throw new Error("VertexAttributeTexture: overrideItemSize must divide evenly into buffer length.");t.itemSize=e,t.count=o*i/e}const r=t.itemSize,s=t.count,a=t.normalized,c=t.array.constructor,u=c.BYTES_PER_ELEMENT;let f=this._forcedType,d=r;if(f===null)switch(c){case Float32Array:f=C.FloatType;break;case Uint8Array:case Uint16Array:case Uint32Array:f=C.UnsignedIntType;break;case Int8Array:case Int16Array:case Int32Array:f=C.IntType;break}let l,m,p,w,y=Lr(r);switch(f){case C.FloatType:p=1,m=Vr(r),a&&u===1?(w=c,y+="8",c===Uint8Array?l=C.UnsignedByteType:(l=C.ByteType,y+="_SNORM")):(w=Float32Array,y+="32F",l=C.FloatType);break;case C.IntType:y+=u*8+"I",p=a?Math.pow(2,c.BYTES_PER_ELEMENT*8-1):1,m=ri(r),u===1?(w=Int8Array,l=C.ByteType):u===2?(w=Int16Array,l=C.ShortType):(w=Int32Array,l=C.IntType);break;case C.UnsignedIntType:y+=u*8+"UI",p=a?Math.pow(2,c.BYTES_PER_ELEMENT*8-1):1,m=ri(r),u===1?(w=Uint8Array,l=C.UnsignedByteType):u===2?(w=Uint16Array,l=C.UnsignedShortType):(w=Uint32Array,l=C.UnsignedIntType);break}d===3&&(m===C.RGBAFormat||m===C.RGBAIntegerFormat)&&(d=4);const M=Math.ceil(Math.sqrt(s))||1,h=d*M*M,g=new w(h),x=t.normalized;t.normalized=!1;for(let b=0;b<s;b++){const A=d*b;g[A]=t.getX(b)/p,r>=2&&(g[A+1]=t.getY(b)/p),r>=3&&(g[A+2]=t.getZ(b)/p,d===4&&(g[A+3]=1)),r>=4&&(g[A+3]=t.getW(b)/p)}t.normalized=x,this.internalFormat=y,this.format=m,this.type=l,this.image.width=M,this.image.height=M,this.image.data=g,this.needsUpdate=!0,this.dispose(),t.itemSize=i,t.count=o}}class zr extends wi{constructor(){super(),this._forcedType=C.UnsignedIntType}}class kr extends wi{constructor(){super(),this._forcedType=C.FloatType}}class Mi{constructor(){this.index=new zr,this.position=new kr,this.bvhBounds=new C.DataTexture,this.bvhContents=new C.DataTexture,this._cachedIndexAttr=null,this.index.overrideItemSize=3}updateFrom(t){const{geometry:e}=t;if(Gr(t,this.bvhBounds,this.bvhContents),this.position.updateFrom(e.attributes.position),t.indirect){const i=t._indirectBuffer;if(this._cachedIndexAttr===null||this._cachedIndexAttr.count!==i.length)if(e.index)this._cachedIndexAttr=e.index.clone();else{const o=xi(Nt(e));this._cachedIndexAttr=new C.BufferAttribute(o,1,!1)}Ur(e,i,this._cachedIndexAttr),this.index.updateFrom(this._cachedIndexAttr)}else this.index.updateFrom(e.index)}dispose(){const{index:t,position:e,bvhBounds:i,bvhContents:o}=this;t&&t.dispose(),e&&e.dispose(),i&&i.dispose(),o&&o.dispose()}}function Ur(n,t,e){const i=e.array,o=n.index?n.index.array:null;for(let r=0,s=t.length;r<s;r++){const a=3*r,c=3*t[r];for(let u=0;u<3;u++)i[a+u]=o?o[c+u]:c+u}}function Gr(n,t,e){const i=n._roots;if(i.length!==1)throw new Error("MeshBVHUniformStruct: Multi-root BVHs not supported.");const o=i[0],r=new Uint16Array(o),s=new Uint32Array(o),a=new Float32Array(o),c=o.byteLength/te,u=2*Math.ceil(Math.sqrt(c/2)),f=new Float32Array(4*u*u),d=Math.ceil(Math.sqrt(c)),l=new Uint32Array(2*d*d);for(let m=0;m<c;m++){const p=m*te/4,w=p*2,y=p;for(let M=0;M<3;M++)f[8*m+0+M]=a[y+0+M],f[8*m+4+M]=a[y+3+M];if(Y(w,r)){const M=ie(w,r),h=Z(p,s),g=zo|M;l[m*2+0]=g,l[m*2+1]=h}else{const M=s[p+6],h=Dt(p,s);l[m*2+0]=h,l[m*2+1]=M}}t.image.data=f,t.image.width=u,t.image.height=u,t.format=C.RGBAFormat,t.type=C.FloatType,t.internalFormat="RGBA32F",t.minFilter=C.NearestFilter,t.magFilter=C.NearestFilter,t.generateMipmaps=!1,t.needsUpdate=!0,t.dispose(),e.image.data=l,e.image.width=d,e.image.height=d,e.format=C.RGIntegerFormat,e.type=C.UnsignedIntType,e.internalFormat="RG32UI",e.minFilter=C.NearestFilter,e.magFilter=C.NearestFilter,e.generateMipmaps=!1,e.needsUpdate=!0,e.dispose()}const Or=`
2
2
 
3
3
  // A stack of uint32 indices can can store the indices for
4
4
  // a perfectly balanced tree with a depth up to 31. Lower stack