three-live 1.2.3 → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/index.d.ts +22 -0
- package/dist/three-author-engine.js +31 -29
- package/dist/three-author.cjs +29 -27
- package/dist/three-author.d.ts +22 -0
- package/dist/three-author.js +30 -28
- package/dist/three-convert.cjs +21 -19
- package/dist/three-convert.d.ts +22 -0
- package/dist/three-convert.js +21 -19
- package/dist/three-live-animation.cjs +4 -4
- package/dist/three-live-animation.d.ts +22 -0
- package/dist/three-live-animation.js +4 -4
- package/dist/three-live-animation.min.js +4 -4
- package/dist/three-live-cad.cjs +2 -2
- package/dist/three-live-cad.d.ts +22 -0
- package/dist/three-live-cad.js +2 -2
- package/dist/three-live-cad.min.js +2 -2
- package/dist/three-live-element.cjs +5 -3
- package/dist/three-live-element.d.ts +22 -0
- package/dist/three-live-element.js +5 -3
- package/dist/three-live-full.cjs +44 -42
- package/dist/three-live-full.js +40 -38
- package/dist/three-live-full.min.js +44 -42
- package/dist/three-live-studio.cjs +15 -15
- package/dist/three-live-studio.d.ts +22 -0
- package/dist/three-live-studio.js +13 -13
- package/dist/three-live-studio.min.js +15 -15
- package/dist/three-live-viewer.cjs +5 -3
- package/dist/three-live-viewer.d.ts +22 -0
- package/dist/three-live-viewer.js +5 -3
- package/dist/three-live-viewer.min.js +5 -3
- package/dist/three-live.cjs +22 -20
- package/dist/three-live.js +22 -20
- package/dist/three-live.min.js +22 -20
- package/dist/three-view.cjs +8 -6
- package/dist/three-view.d.ts +22 -0
- package/dist/three-view.js +8 -6
- package/dist/three-xr.cjs +24 -22
- package/dist/three-xr.d.ts +22 -0
- package/dist/three-xr.js +24 -22
- package/package.json +1 -1
package/dist/three-view.js
CHANGED
|
@@ -14,8 +14,9 @@
|
|
|
14
14
|
* See the License for the specific language governing permissions and
|
|
15
15
|
* limitations under the License.
|
|
16
16
|
*/
|
|
17
|
-
var
|
|
18
|
-
layout(location=0) in vec3
|
|
17
|
+
function pe(n,e,t,s=!0){let i=[],r=null;try{for(let[o,c]of[[35633,e],[35632,t]]){let a=n.createShader(o);if(!a){if(s)throw new Error("Shader allocation failed");return null}if(i.push(a),n.shaderSource(a,c),n.compileShader(a),n.getShaderParameter&&!n.getShaderParameter(a,35713)){if(s)throw new Error(`Shader compile error: ${n.getShaderInfoLog(a)}`);return null}}if(r=n.createProgram(),!r){if(s)throw new Error("Program allocation failed");return null}for(let o of i)n.attachShader(r,o);if(n.linkProgram(r),n.getProgramParameter&&!n.getProgramParameter(r,35714)){let o=s?n.getProgramInfoLog(r):null;if(n.deleteProgram?.(r),r=null,s)throw new Error(`Program link error: ${o}`);return null}return r}finally{for(let o of i)r&&n.detachShader?.(r,o),n.deleteShader?.(o)}}var ze={5120:1,5121:1,5122:2,5123:2,5125:4,5126:4};function Me(n,e,t,s){if(!t||t.bufferView===void 0)return null;let i=n.bufferViews?.[t.bufferView];if(!i)return null;let r=Number(t.count??0);if(!Number.isInteger(r)||r<=0)return null;let o=t.componentType??5126,c=ze[o];if(!c)throw new RangeError(`glTF accessor has unsupported componentType ${o}`);let a=c*s,l=i.byteStride&&i.byteStride>0?i.byteStride:a,u=(i.byteOffset||0)+(t.byteOffset||0);if(!Number.isInteger(u)||u<0||l<a||u+(r-1)*l+a>e.byteLength)throw new RangeError(`glTF accessor reads past the end of the binary buffer (${e.byteLength} bytes)`);if(o===5126&&l===a&&u%4===0)return new Float32Array(e,u,r*s);let d=new DataView(e),m=t.normalized===!0,y=new Float32Array(r*s);for(let p=0;p<r;p++){let b=u+p*l;for(let x=0;x<s;x++){let g=b+x*c,w;switch(o){case 5126:w=d.getFloat32(g,!0);break;case 5120:w=d.getInt8(g),m&&(w=Math.max(w/127,-1));break;case 5121:w=d.getUint8(g),m&&(w/=255);break;case 5122:w=d.getInt16(g,!0),m&&(w=Math.max(w/32767,-1));break;case 5123:w=d.getUint16(g,!0),m&&(w/=65535);break;default:w=d.getUint32(g,!0),m&&(w/=4294967295)}y[p*s+x]=w}}return y}function Ae(n,e){let t=Number(e.offset?.[0]??0)||0,s=Number(e.offset?.[1]??0)||0,i=Number(e.scale?.[0]??1),r=Number(e.scale?.[1]??1),o=Number(e.rotation??0)||0,c=Math.cos(o),a=Math.sin(o),l=(Number.isFinite(i)?i:1)*c,u=(Number.isFinite(i)?i:1)*a,d=-(Number.isFinite(r)?r:1)*a,m=(Number.isFinite(r)?r:1)*c;for(let y=0;y+1<n.length;y+=2){let p=n[y],b=n[y+1];n[y]=l*p+u*b+t,n[y+1]=d*p+m*b+s}}function X(){let n=new Float32Array(16);return n[0]=1,n[5]=1,n[10]=1,n[15]=1,n}function be(n,e,t,s,i){let r=1/Math.tan(e/2);n.fill(0),n[0]=r/t,n[5]=r,n[11]=-1;let o=1/(s-i);return n[10]=(i+s)*o,n[14]=2*i*s*o,n}function xe(n,e,t,s){let[i,r,o]=e,[c,a,l]=s,[u,d,m]=t,y=i-u,p=r-d,b=o-m,x=Math.hypot(y,p,b);x<=1e-5?(y=0,p=0,b=1):(y/=x,p/=x,b/=x);let g=a*b-l*p,w=l*y-c*b,M=c*p-a*y;x=Math.hypot(g,w,M),x<=1e-5?(g=0,w=0,M=0):(g/=x,w/=x,M/=x);let T=p*M-b*w,f=b*g-y*M,R=y*w-p*g;return x=Math.hypot(T,f,R),x<=1e-5?(T=0,f=0,R=0):(T/=x,f/=x,R/=x),n[0]=g,n[1]=T,n[2]=y,n[3]=0,n[4]=w,n[5]=f,n[6]=p,n[7]=0,n[8]=M,n[9]=R,n[10]=b,n[11]=0,n[12]=-(g*i+w*r+M*o),n[13]=-(T*i+f*r+R*o),n[14]=-(y*i+p*r+b*o),n[15]=1,n}function J(n,e,t){let s=e[0],i=e[1],r=e[2],o=e[3],c=e[4],a=e[5],l=e[6],u=e[7],d=e[8],m=e[9],y=e[10],p=e[11],b=e[12],x=e[13],g=e[14],w=e[15],M=t[0],T=t[1],f=t[2],R=t[3];return n[0]=M*s+T*c+f*d+R*b,n[1]=M*i+T*a+f*m+R*x,n[2]=M*r+T*l+f*y+R*g,n[3]=M*o+T*u+f*p+R*w,M=t[4],T=t[5],f=t[6],R=t[7],n[4]=M*s+T*c+f*d+R*b,n[5]=M*i+T*a+f*m+R*x,n[6]=M*r+T*l+f*y+R*g,n[7]=M*o+T*u+f*p+R*w,M=t[8],T=t[9],f=t[10],R=t[11],n[8]=M*s+T*c+f*d+R*b,n[9]=M*i+T*a+f*m+R*x,n[10]=M*r+T*l+f*y+R*g,n[11]=M*o+T*u+f*p+R*w,M=t[12],T=t[13],f=t[14],R=t[15],n[12]=M*s+T*c+f*d+R*b,n[13]=M*i+T*a+f*m+R*x,n[14]=M*r+T*l+f*y+R*g,n[15]=M*o+T*u+f*p+R*w,n}function Te(n,e,t,s){let i=e[0],r=e[1],o=e[2],c=e[3],a=i+i,l=r+r,u=o+o,d=i*a,m=i*l,y=i*u,p=r*l,b=r*u,x=o*u,g=c*a,w=c*l,M=c*u,T=s[0],f=s[1],R=s[2];return n[0]=(1-(p+x))*T,n[1]=(m+M)*T,n[2]=(y-w)*T,n[3]=0,n[4]=(m-M)*f,n[5]=(1-(d+x))*f,n[6]=(b+g)*f,n[7]=0,n[8]=(y+w)*R,n[9]=(b-g)*R,n[10]=(1-(d+p))*R,n[11]=0,n[12]=t[0],n[13]=t[1],n[14]=t[2],n[15]=1,n}function ae(n,e,t=0){let s=e[0],i=e[1],r=e[2],o=e[4],c=e[5],a=e[6],l=e[8],u=e[9],d=e[10],m=d*c-a*u,y=-d*o+a*l,p=u*o-c*l,b=s*m+i*y+r*p;return!b||t>0&&(!Number.isFinite(b)||Math.abs(b)<=t)||(b=1/b,n[0]=m*b,n[1]=y*b,n[2]=p*b,n[3]=(-d*i+r*u)*b,n[4]=(d*s-r*l)*b,n[5]=(-u*s+i*l)*b,n[6]=(a*i-r*c)*b,n[7]=(-a*s+r*o)*b,n[8]=(c*s-i*o)*b),n}function De(n,e){let t=e[0],s=e[1],i=e[2],r=e[3],o=e[4],c=e[5],a=e[6],l=e[7],u=e[8],d=e[9],m=e[10],y=e[11],p=e[12],b=e[13],x=e[14],g=e[15],w=t*c-s*o,M=t*a-i*o,T=t*l-r*o,f=s*a-i*c,R=s*l-r*c,S=i*l-r*a,L=u*b-d*p,k=u*x-m*p,U=u*g-y*p,F=d*x-m*b,h=d*g-y*b,P=m*g-y*x,E=w*P-M*h+T*F+f*U-R*k+S*L;return E?(E=1/E,n[0]=(c*P-a*h+l*F)*E,n[1]=(i*h-s*P-r*F)*E,n[2]=(b*S-x*R+g*f)*E,n[3]=(m*R-d*S-y*f)*E,n[4]=(a*U-o*P-l*k)*E,n[5]=(t*P-i*U+r*k)*E,n[6]=(x*T-p*S-g*M)*E,n[7]=(u*S-m*T+y*M)*E,n[8]=(o*h-c*U+l*L)*E,n[9]=(s*U-t*h-r*L)*E,n[10]=(p*R-b*T+g*w)*E,n[11]=(d*T-u*R-y*w)*E,n[12]=(c*k-o*F-a*L)*E,n[13]=(t*F-s*k+i*L)*E,n[14]=(b*M-p*f-x*w)*E,n[15]=(u*f-d*M+m*w)*E,n):null}function Re(n,e,t,s,i){let r=i[0]*e+i[4]*t+i[8]*s+i[12],o=i[1]*e+i[5]*t+i[9]*s+i[13],c=i[2]*e+i[6]*t+i[10]*s+i[14],a=i[3]*e+i[7]*t+i[11]*s+i[15],l=a!==0?1/a:1;n[0]=r*l,n[1]=o*l,n[2]=c*l}function Le(n,e,t,s,i,r,o,c,a,l,u,d,m,y,p,b,x=0){let g=l-o,w=u-c,M=d-a,T=m-o,f=y-c,R=p-a,S=i*R-r*f,L=r*T-s*R,k=s*f-i*T,U=g*S+w*L+M*k;if(U>-1e-7&&U<1e-7)return!1;let F=1/U,h=n-o,P=e-c,E=t-a,v=F*(h*S+P*L+E*k);if(v<-x||v>1+x)return!1;let B=P*M-E*w,D=E*g-h*M,$=h*w-P*g,A=F*(s*B+i*D+r*$);if(A<-x||v+A>1+x)return!1;let W=F*(T*B+f*D+R*$);if(W>1e-4&&W<b.distance){b.distance=W,b.point=[n+s*W,e+i*W,t+r*W];let _=w*R-M*f,K=M*T-g*R,q=g*f-w*T,G=Math.hypot(_,K,q)||1;return b.normal=[_/G,K/G,q/G],!0}return!1}function Ce(n,e){let t=Array.isArray(n?.nodes)?n.nodes.length:0,s=[];for(let u=0;u<t;u++)s.push(X());if(t===0)return s;let i=new Set;for(let u of n.nodes)if(Array.isArray(u.children))for(let d of u.children)i.add(d);let r=new Set,o=128,c=(u,d,m=0)=>{if(m>o||u<0||u>=t||r.has(u))return;r.add(u);let y=n.nodes[u];if(!y)return;let p=X();if(e&&e[u])p.set(e[u]);else if(y.matrix&&y.matrix.length===16)p.set(y.matrix);else{let x=y.translation||[0,0,0],g=y.rotation||[0,0,0,1],w=y.scale||[1,1,1];Te(p,g,x,w)}let b=s[u];if(d?J(b,d,p):b.set(p),Array.isArray(y.children))for(let x of y.children)typeof x=="number"&&c(x,b,m+1)},a=[],l=n.scenes&&n.scenes[n.scene!==void 0?n.scene:0];if(l&&Array.isArray(l.nodes))a.push(...l.nodes);else for(let u=0;u<t;u++)i.has(u)||a.push(u);for(let u of a)c(u,null);for(let u=0;u<t;u++)!i.has(u)&&!a.includes(u)&&c(u,null);return s}var ve="__THREE_LIVE_REGISTRY__";function ue(){let n=typeof globalThis<"u"?globalThis:{};if(!n[ve]){let e={plugins:[],decoders:new Map};try{Object.defineProperty(n,ve,{value:e,writable:!1,configurable:!1,enumerable:!1})}catch{n[ve]=e}}return n[ve]}var Y=ue().plugins;function Ie(n){let e=ue().plugins;e.some(t=>t.name===n.name)||e.push(n),n.onRegister?.()}function Fe(){return ue().plugins}var Ge=["neutral","reinhard","none","aces","agx"];function ye(n){let e=String(n??"").trim().toLowerCase();return Ge.includes(e)?e:"neutral"}function je(n){return Ge.indexOf(ye(n))}function Ye(n){return new TextDecoder().decode(n)}var ke=ue().decoders;function $e(n,e){ke.set(n.toLowerCase(),e)}function le(n){return ke.get(n.toLowerCase())}function _e(n){let e=ArrayBuffer.isView(n)?n.buffer.slice(n.byteOffset,n.byteOffset+n.byteLength):n,t=new DataView(e);if(e.byteLength<12)throw new Error(`Invalid GLB buffer size: ${e.byteLength} bytes`);let s=t.getUint32(0,!0);if(s!==1179937895){let m=t.getUint8(0);throw m===123||m===32||m===10||m===13?new Error("[three-live] Multi-file JSON .gltf is not supported for performance reasons. Please pack your asset into a single binary .glb file."):new Error(`Invalid GLB magic: 0x${s.toString(16)}`)}let i=t.getUint32(4,!0);if(i!==2)throw new Error(`Unsupported GLB version: ${i}`);let r=t.getUint32(8,!0),o=12,c=null,a=null;for(;o+8<=r&&o+8<=e.byteLength;){let m=t.getUint32(o,!0),y=t.getUint32(o+4,!0),p=o+8;if(p+m>e.byteLength)throw new Error(`Truncated GLB chunk: required ${p+m} bytes, buffer has ${e.byteLength}`);if(y===1313821514){let b=new Uint8Array(e,p,m),x=Ye(b);c=JSON.parse(x,(g,w)=>g==="__proto__"||g==="constructor"||g==="prototype"?void 0:w)}else y===5130562&&(a=e.slice(p,p+m));o=p+m}if(!c)throw new Error("Missing JSON chunk in GLB");let l=c.extensionsRequired||[];if(l.includes("KHR_draco_mesh_compression")&&!le("draco"))throw new Error("Model requires KHR_draco_mesh_compression. Register a decoder via ThreeLiveElement.registerDecoder('draco', decoder).");for(let m of["EXT_meshopt_compression","KHR_meshopt_compression"])if(l.includes(m)&&!le("meshopt"))throw new Error(`Model requires ${m}. Register a decoder via ThreeLiveElement.registerDecoder('meshopt', decoder).`);if(l.includes("KHR_texture_basisu")&&!le("ktx2"))throw new Error("Model requires KHR_texture_basisu. Register a decoder via ThreeLiveElement.registerDecoder('ktx2', decoder).");let u=a||new ArrayBuffer(0),d={gltf:c,bin:u,animations:[]};for(let m of Y)m.onParseGLB?.(d);return d}function ht(n,e,t){return e%4===0?new Float32Array(n,e,t):new Float32Array(n.slice(e,e+t*4))}function Ke(n){return typeof n=="object"&&n!==null&&"gltf"in n&&typeof n.gltf=="object"&&n.gltf!==null&&"bin"in n&&(n.bin instanceof ArrayBuffer||ArrayBuffer.isView(n.bin))}function Se(n){if(Ke(n))return n;if(n instanceof ArrayBuffer||ArrayBuffer.isView(n))return _e(n);throw new TypeError("Invalid 3D model input: expected GLBData ({ gltf, bin }) or binary GLB (ArrayBuffer / ArrayBufferView).")}var Ve=`#version 300 es
|
|
18
|
+
layout(location=0) in vec3 aPos;
|
|
19
|
+
layout(location=3) in vec3 aOff;
|
|
19
20
|
layout(location=1) in vec3 aNormal;
|
|
20
21
|
layout(location=2) in vec2 aUV;
|
|
21
22
|
uniform mat4 uMVP,uModel;
|
|
@@ -38,6 +39,7 @@ mat3 xrNormalMatrix(){return uInstanced==1?transpose(inverse(mat3(xrModel()))):u
|
|
|
38
39
|
out vec3 vNormal,vWorldPos;
|
|
39
40
|
out vec2 vUV;
|
|
40
41
|
void main(){
|
|
42
|
+
vec3 aPosition=aPos+aOff;
|
|
41
43
|
vInstTint=uInstanced==1?aInstColor:vec4(1.0);
|
|
42
44
|
vec3 rawN=uNormalMatrix*aNormal;
|
|
43
45
|
vNormal=length(rawN)>1e-5?normalize(rawN):vec3(0.0);
|
|
@@ -47,7 +49,7 @@ vec2 scaleUV=uUvTransform.zw;
|
|
|
47
49
|
if(scaleUV.x==0.0&&scaleUV.y==0.0)scaleUV=vec2(1.0);
|
|
48
50
|
vUV=aUV*scaleUV+uUvTransform.xy;
|
|
49
51
|
gl_Position=uMVP*vec4(aPosition,1.0);
|
|
50
|
-
}`,
|
|
52
|
+
}`,qe=`#version 300 es
|
|
51
53
|
precision highp float;
|
|
52
54
|
in vec3 vNormal,vWorldPos;
|
|
53
55
|
in vec2 vUV;
|
|
@@ -240,11 +242,11 @@ color=applyToneMapping(color,uToneMapping);
|
|
|
240
242
|
float outAlpha=base.a;
|
|
241
243
|
/*XRD_O*/
|
|
242
244
|
fragColor=vec4(color,outAlpha);
|
|
243
|
-
}`;function
|
|
245
|
+
}`;function mt(n,e,t=1){e=ye(e);let s=Number.isFinite(t)?Math.max(0,t):1,i=Math.max(0,(Number(n[0])||0)*s),r=Math.max(0,(Number(n[1])||0)*s),o=Math.max(0,(Number(n[2])||0)*s),c=g=>Math.max(0,Math.min(1,g));if(e==="none")return[c(i),c(r),c(o)];if(e==="reinhard")return[i/(i+1),r/(r+1),o/(o+1)];if(e==="aces"){let g=w=>{let M=w*.6;return c(M*(2.51*M+.03)/(M*(2.43*M+.59)+.14))};return[g(i),g(r),g(o)]}if(e==="agx"){let g=.84247906*i+.0784336*r+.07922375*o,w=.04232824*i+.87846864*r+.07916613*o,M=.04237565*i+.0784336*r+.87914297*o,T=F=>{let h=c((Math.log2(Math.max(F,1e-6))+12.47393)/16.5),P=h*h,E=P*P;return 15.5*E*P-40.14*E*h+31.96*E-6.868*P*h+.4298*P+.1191*h-.00232},f=T(g),R=T(w),S=T(M),L=1.196879*f-.09802088*R-.09902974*S,k=-.05289685*f+1.15190313*R-.09896118*S,U=-.05297164*f-.09804345*R+1.15107367*S;return[c(Math.pow(Math.max(L,0),2.2)),c(Math.pow(Math.max(k,0),2.2)),c(Math.max(0,Math.pow(Math.max(U,0),2.2)))]}let a=.76,l=.15,u=Math.min(i,r,o),d=u<.08?u-6.25*u*u:.04;i-=d,r-=d,o-=d;let m=Math.max(i,r,o);if(m<a)return[c(i),c(r),c(o)];let y=1-a,p=1-y*y/(m+y-a),b=p/m;i*=b,r*=b,o*=b;let x=1-1/(l*(m-p)+1);return[c(i+(p-i)*x),c(r+(p-r)*x),c(o+(p-o)*x)]}var Ze=`#version 300 es
|
|
244
246
|
layout(location=0) in vec3 aPosition;
|
|
245
247
|
uniform mat4 uMVP;
|
|
246
|
-
void main(){gl_Position=uMVP*vec4(aPosition,1.0);}`,
|
|
248
|
+
void main(){gl_Position=uMVP*vec4(aPosition,1.0);}`,Je=`#version 300 es
|
|
247
249
|
precision highp float;
|
|
248
250
|
uniform vec4 uColorId;
|
|
249
251
|
out vec4 fragColor;
|
|
250
|
-
void main(){fragColor=uColorId;}`;function Ne(n,e,t){let s=n.createShader(e);if(n.shaderSource(s,t),n.compileShader(s),!n.getShaderParameter(s,35713)){let i=n.getShaderInfoLog(s);throw n.deleteShader(s),new Error(`Shader compile error: ${i}`)}return s}function Ee(n,e,t){let s=Ne(n,35633,e),i=Ne(n,35632,t),r=n.createProgram();if(n.attachShader(r,s),n.attachShader(r,i),n.linkProgram(r),!n.getProgramParameter(r,35714)){let o=n.getProgramInfoLog(r);throw n.deleteProgram(r),new Error(`Program link error: ${o}`)}return r}function Pe(n,e,t){let s={};for(let i of t)s[i]=n.getUniformLocation(e,i);return s}var Be=["uMVP","uModel","uInstanced","uViewProj","uNormalMatrix","uUvTransform","uBaseColor","uMetallic","uRoughness","uClearcoat","uEmissive","uCameraPos","uBaseColorTexture","uHasTexture","uMetallicRoughnessTexture","uHasMetallicRoughnessTexture","uNormalTexture","uHasNormalTexture","uNormalScale","uOcclusionTexture","uHasOcclusionTexture","uOcclusionStrength","uToneMapping","uEnvironmentMap","uHasEnvironmentMap","uAmbientColor","uNumDirLights","uDirLightDir","uDirLightColor","uNumPointLights","uPointLightPos","uPointLightColor","uEnvIntensity","uExposure","uViewportSize","uSectionPlane","uSectionEnabled"];function We(){let n=ue().xrDepthShader;return{fs:Ze.replace("/*XRD_U*/",n?.uniformsGLSL||"").replace("/*XRD_O*/",n?.occlusionGLSL||""),uniforms:n?[...Be,...n.uniformNames]:Be}}function et(n){if(typeof n!="string")return null;let e=n.trim();if(!e||/[\x00-\x1f\x7f]/.test(e))return null;let t=e.toLowerCase();return t.startsWith("data:")?/^data:image\/(png|jpeg|jpg|webp|ktx2|avif)(;|,)/i.test(e)?e:null:t.startsWith("https://")||t.startsWith("blob:")?e:t.startsWith("//")||/^[a-z][a-z0-9+.-]*:/i.test(e)||/(^|[\\/])\.\.([\\/]|$)/.test(e)?null:e}function Re(n,e,t,s,i,r,o,c){if(s===void 0||!e.accessors?.[s])return null;let a=ye(e,t,e.accessors[s],r);if(!a)return null;c&&(a=a.slice(),Ae(a,c));let l=n.createBuffer();return o.push(l),n.bindBuffer(34962,l),n.bufferData(34962,a,35044),n.enableVertexAttribArray(i),n.vertexAttribPointer(i,r,5126,!1,0,0),a}function we(n){let e=Number(n);return Number.isFinite(e)?Math.max(0,e):1}function He(n,e,t){let s=We(),i=Ue,r=s.fs,o=!1;for(let a of Y)if(a.onResolveShader){let l=a.onResolveShader(e,t,i,r);l&&(i=l.vs,r=l.fs,o=!0)}let c=o?Ee(n,i,r):null;return{program:c,uniforms:c?Pe(n,c,s.uniforms):null}}function tt(n,e,t){let s=e.instanceMatrices;if(!s||s.length<16||typeof n.vertexAttribDivisor!="function"||typeof n.drawElementsInstanced!="function")return!1;let i=s.length>>4,r=e.instanceColors,o=new Float32Array(i*20);for(let a=0;a<i;a++)o.set(s.subarray(a*16,a*16+16),a*20),o[a*20+16]=r?r[a*3]:1,o[a*20+17]=r?r[a*3+1]:1,o[a*20+18]=r?r[a*3+2]:1,o[a*20+19]=1;let c=n.createBuffer();t.push(c),n.bindBuffer(34962,c),n.bufferData(34962,o,35044);for(let a=0;a<5;a++)n.enableVertexAttribArray(8+a),n.vertexAttribPointer(8+a,4,5126,!1,80,a*16),n.vertexAttribDivisor(8+a,1);return!0}function nt(n,e){let t=n.materials?.[e?.material];if(!t)return null;let s=[t.pbrMetallicRoughness?.baseColorTexture,t.pbrMetallicRoughness?.metallicRoughnessTexture,t.normalTexture,t.occlusionTexture,t.emissiveTexture];for(let i of s){let r=i?.extensions?.KHR_texture_transform;if(r&&typeof r=="object")return r}return null}var mt=Object.freeze({maxMeshes:1e3,maxTextures:64,maxVertices:1e6,maxIndices:3e6}),Oe=Object.freeze({maxMeshes:1/0,maxTextures:1/0,maxVertices:1/0,maxIndices:1/0});function ve(n,e){e.indices?n.drawElements(4,e.indexCount,e.indexType,0):n.drawArrays(4,0,e.indexCount)}function Xe(n,e){let t=n?.pbrMetallicRoughness,s=t?.baseColorFactor,i=n?.extensions?.KHR_materials_clearcoat,r=o=>o!==void 0&&e[o.index]?e[o.index]:null;return{baseColor:s?[s[0],s[1],s[2],s[3]??1]:[.8,.8,.8,1],metallic:t?.metallicFactor??.1,roughness:t?.roughnessFactor??.5,clearcoat:i?i.clearcoatFactor??1:0,clearcoatRoughness:Math.max(.04,i?.clearcoatRoughnessFactor??.04),texture:r(t?.baseColorTexture),metallicRoughnessTexture:r(t?.metallicRoughnessTexture),normalTexture:r(n?.normalTexture),normalScale:n?.normalTexture?.scale??1,occlusionTexture:r(n?.occlusionTexture),occlusionStrength:n?.occlusionTexture?.strength??1,isTransmissive:(n?.extensions?.KHR_materials_transmission?.transmissionFactor??0)>0}}function ze(n,e,t){let s=new Set,i=r=>{r&&!s.has(r)&&(s.add(r),n.deleteTexture(r))};for(let r of e)for(let o of r.primitives){if(o.vao&&n.deleteVertexArray(o.vao),o.buffers)for(let a of o.buffers)n.deleteBuffer(a);i(o.texture),i(o.metallicRoughnessTexture),i(o.normalTexture),i(o.occlusionTexture);let c=new Set;o.customProgram&&c.add(o.customProgram);for(let a of o.programCache?.values()??[])a.program&&c.add(a.program);for(let a of c)n.deleteProgram(a)}if(t)for(let r of t)i(r)}function Ge(n,e,t=Oe,s){let i={meshes:[],mesh:null,gpuTextures:[],vao:null,buffers:[]};try{return it(n,e,t,s,i)}catch(r){let o=i.mesh&&!i.meshes.includes(i.mesh)?[...i.meshes,i.mesh]:i.meshes;ze(n,o,i.gpuTextures),i.vao&&n.deleteVertexArray(i.vao);for(let c of i.buffers)n.deleteBuffer(c);throw r}}function it(n,e,t,s,i){if(!e||!e.gltf)throw new Error("Invalid GLBData passed to buildGpuScene: missing gltf object.");let{gltf:r}=e,o=le("meshopt");o?.decodeBufferViews&&o.decodeBufferViews(r,e);let c=e.bin,a=[];i.meshes=a;let l=[],u=r.extensions?.KHR_materials_variants;u&&Array.isArray(u.variants)&&u.variants.forEach((h,P)=>{l.push(typeof h?.name=="string"&&h.name?h.name:`Variant_${P}`)});let p=Ce(r),m=[];if(Array.isArray(r.nodes))for(let h=0;h<r.nodes.length;h++){let P=r.nodes[h];P.mesh!==void 0&&r.meshes&&r.meshes[P.mesh]&&m.push({node:P,nodeIdx:h,meshIdx:P.mesh})}let g=m.length>0?m:(r.meshes||[]).map((h,P)=>({node:h,nodeIdx:-1,meshIdx:P})),d=[];if(Array.isArray(e.lights)&&e.lights.length>0)d.push(...e.lights);else if(r.extensions?.KHR_lights_punctual?.lights){let h=r.extensions.KHR_lights_punctual.lights;for(let P of h){let w=P.type==="directional"?"directional":P.type==="point"?"point":"ambient";d.push({type:w,color:P.color||[1,1,1],intensity:P.intensity!==void 0?P.intensity:1})}}if(d.length===0&&(d.push({type:"ambient",color:[1,1,1],intensity:.35}),d.push({type:"directional",color:[1,1,1],intensity:.85,direction:[.5,1,.8]})),g.length>t.maxMeshes)throw new Error(`SecurityError: GLB scene exceeds host mesh limit (${t.maxMeshes}). Found: ${g.length}`);let E=[];if(i.gpuTextures=E,r.textures&&r.images){if(r.textures.length>t.maxTextures)throw new Error(`SecurityError: GLB scene exceeds host texture limit (${t.maxTextures}). Found: ${r.textures.length}`);for(let h=0;h<r.textures.length;h++){let P=r.textures[h],w=r.images[P.source];if(w){let v=n.createTexture();if(n.bindTexture(3553,v),n.texImage2D(3553,0,6408,1,1,0,6408,5121,new Uint8Array([255,255,255,255])),n.texParameteri(3553,10242,10497),n.texParameteri(3553,10243,10497),n.texParameteri(3553,10241,9729),n.texParameteri(3553,10240,9729),w.bufferView!==void 0){let B=r.bufferViews[w.bufferView],D=c.slice(B.byteOffset||0,(B.byteOffset||0)+B.byteLength),$=new Blob([D],{type:w.mimeType||"image/png"});typeof createImageBitmap=="function"&&createImageBitmap($).then(A=>{n.bindTexture(3553,v),n.texImage2D(3553,0,6408,6408,5121,A),n.generateMipmap(3553),s?.()}).catch(()=>{})}else if(w.uri){let B=et(w.uri);if(B){let D=new Image;D.crossOrigin="anonymous",D.referrerPolicy="no-referrer",D.onload=()=>{n.bindTexture(3553,v),n.texImage2D(3553,0,6408,6408,5121,D),n.generateMipmap(3553),s?.()},D.src=B}else console.warn("[Three-Live Security] Blocked unsafe glTF image URI:",w.uri)}E.push(v)}else E.push(null)}}let b=[1/0,1/0,1/0],x=[-1/0,-1/0,-1/0],R=0,y=0;for(let h=0;h<g.length;h++){let P=g[h],w=r.meshes[P.meshIdx],v=P.nodeIdx>=0?p[P.nodeIdx]:X(),B=new Float32Array(9);ae(B,v);let D={name:P.node.name||w.name||`Mesh_${P.meshIdx}`,nodeIndex:P.nodeIdx,transform:v,normalMatrix:B,primitives:[],extras:P.node.extras||w.extras};i.mesh=D;let $=P.node?.extensions?.EXT_mesh_gpu_instancing;if($&&$.attributes&&typeof $.attributes=="object"){let A=$.attributes,W=A.TRANSLATION!==void 0?r.accessors?.[A.TRANSLATION]:null,_=A.ROTATION!==void 0?r.accessors?.[A.ROTATION]:null,K=A.SCALE!==void 0?r.accessors?.[A.SCALE]:null,q=A._COLOR_0!==void 0?r.accessors?.[A._COLOR_0]:null,G=Number(W?.count??_?.count??K?.count??0);if(!Number.isFinite(G)||G<0||G>1e4)throw new Error(`SecurityError: EXT_mesh_gpu_instancing exceeds maximum instance limit (10000). Found: ${G}`);if(G>0){let z=(N,O)=>{if(!N||N.bufferView===void 0||!r.bufferViews?.[N.bufferView])return null;let te=N.componentType??5126,re=j=>(console.warn(`[three-live] EXT_mesh_gpu_instancing: skipping accessor (${j})`),null);if(te!==5120&&te!==5121&&te!==5122&&te!==5123&&te!==5126)return re(`componentType ${te}`);if(!(Number(N.count)>=G))return re(`count ${N.count} < ${G}`);let C=null;try{C=ye(r,c,{...N,count:G},O)}catch{return re("out of bounds")}return!C||!C.every(Number.isFinite)?re("non-finite value"):C},I=z(W,3),ne=z(_,4),H=z(K,3),Q=q?.type==="VEC4"?4:3,ee=z(q,Q),ie=new Float32Array(G*16),oe=X();for(let N=0;N<G;N++){let O=I?[I[N*3],I[N*3+1],I[N*3+2]]:[0,0,0],te=ne?[ne[N*4],ne[N*4+1],ne[N*4+2],ne[N*4+3]]:[0,0,0,1],re=H?[H[N*3],H[N*3+1],H[N*3+2]]:[1,1,1];Me(oe,te,O,re),ie.set(oe,N*16)}if(D.instanceMatrices=ie,ee){let N=new Float32Array(G*3);for(let O=0;O<G;O++)N[O*3]=ee[O*Q],N[O*3+1]=ee[O*Q+1],N[O*3+2]=ee[O*Q+2];D.instanceColors=N}}}for(let A of w.primitives||[]){if(A.extensions?.KHR_draco_mesh_compression){let C=le("draco");C&&C.decodePrimitive(r,A,c)}let W=[],_=n.createVertexArray();i.vao=_,i.buffers=W,n.bindVertexArray(_);let K=tt(n,D,W),q=r.accessors[A.attributes.POSITION],G=Number(q?.count??0);if(!Number.isFinite(G)||G<0||R+G>t.maxVertices)throw new Error(`SecurityError: GLB scene exceeds host vertex limit (${t.maxVertices}).`);if(R+=G,q?.min&&q?.max){let C=j=>{for(let V=0;V<3;V++){let Z=j[12+V],se=j[12+V];for(let ce=0;ce<3;ce++){let de=j[ce*4+V]*q.min[ce],fe=j[ce*4+V]*q.max[ce];Z+=de<fe?de:fe,se+=de>fe?de:fe}Z<b[V]&&(b[V]=Z),se>x[V]&&(x[V]=se)}};if(D.instanceMatrices&&D.instanceMatrices.length>=16){let j=D.instanceMatrices.length>>4,V=X();for(let Z=0;Z<j;Z++){let se=D.instanceMatrices.subarray(Z*16,Z*16+16);J(V,v,se),C(V)}}else C(v)}let z=Re(n,r,c,A.attributes.POSITION,0,3,W);Re(n,r,c,A.attributes.NORMAL,1,3,W),Re(n,r,c,A.attributes.TEXCOORD_0,2,2,W,nt(r,A));let I=0,ne=5123,H=null;if(A.indices!==void 0){let C=r.accessors[A.indices],V=(r.bufferViews[C.bufferView].byteOffset||0)+(C.byteOffset||0);if(I=Number(C.count??0),!Number.isFinite(I)||I<0||y+I>t.maxIndices)throw new Error(`SecurityError: GLB scene exceeds host index limit (${t.maxIndices}).`);y+=I,ne=C.componentType===5125?5125||5125:5123||5123;let Z=n.createBuffer();W.push(Z),n.bindBuffer(34963,Z),C.componentType===5125?(H=new Uint32Array(c,V,C.count),n.bufferData(34963,H,35044)):C.componentType===5121?(H=Uint16Array.from(new Uint8Array(c,V,C.count)),n.bufferData(34963,H,35044)):(H=V%2===0?new Uint16Array(c,V,C.count):new Uint16Array(c.slice(V,V+C.count*2)),n.bufferData(34963,H,35044))}else I=q.count;let Q={};for(let C of Y)if(C.onBuildPrimitiveVAO){let j=C.onBuildPrimitiveVAO(n,_,A,r,c);j&&(Q[C.name]=j)}n.bindVertexArray(null);let ee=r.materials&&A.material!==void 0?r.materials[A.material]:null,{program:ie,uniforms:oe}=He(n,A,ee),N=Xe(ee,E),O=!!(ee?.extensions?.KHR_materials_transmission||Q.materials?.isTransmissive),te=A.extensions?.KHR_materials_variants?.mappings,re=Array.isArray(te)?te.filter(C=>C&&typeof C.material=="number"&&Array.isArray(C.variants)).map(C=>({material:C.material|0,variants:C.variants.map(j=>Number(j)|0)})):void 0;D.primitives.push({vao:_,instanced:K,indexCount:I,indexType:ne,...N,positions:z,indices:H,buffers:W,materialIndex:A.material,defaultMaterialIndex:A.material,variantMappings:re,primitiveDef:A,programCache:new Map([[A.material??-1,{program:ie,uniforms:oe}]]),pluginData:Q,customProgram:ie,customUniforms:oe,isTransmissive:O})}a.push(D)}isFinite(b[0])||(b=[-1,-1,-1]),isFinite(x[0])||(x=[1,1,1]);let M=[(b[0]+x[0])/2,(b[1]+x[1])/2,(b[2]+x[2])/2],f=x[0]-b[0],T=x[1]-b[1],S=x[2]-b[2],L=Math.max(Math.hypot(f,T,S)/2,.5),k=[b[0],b[1],b[2]],U=[x[0],x[1],x[2]],F=e.animations;if(!F){for(let h of Y)if(h.onParseGLB?.(e),e.animations){F=e.animations;break}}return F||(F=[]),{meshes:a,bounds:{center:M,radius:L,min:k,max:U},lights:d,animations:F,worldMatrices:p,gpuTextures:E,availableVariants:l}}function he(n){let e=n.customTarget||n.bounds.center,t=n.currDistance*Math.cos(n.currPhi);return[e[0]+t*Math.sin(n.currTheta),e[1]+n.currDistance*Math.sin(n.currPhi),e[2]+t*Math.cos(n.currTheta)]}var me=class{constructor(e,t,s){this.meshes=[];this.backdropMeshes=[];this.bounds={center:[0,0,0],radius:1,min:[-1,-1,-1],max:[1,1,1]};this.lights=[];this.modelRootMatrix=X();this.clearColor=[0,0,0,0];this.envIntensity=1;this.exposure=1;this.shadowIntensity=0;this.shadowSoftness=.5;this.toneMapping="neutral";this.environmentTexture=null;this.animations=[];this.currentClip=null;this.animationTime=0;this.isPlayingAnimation=!1;this.autoplay=!1;this.animationName="";this.gpuTextures=[];this.availableVariants=[];this.activeVariantName=null;this.gltf=null;this.worldMatrices=[];this.initialNodeTransforms=[];this.projMatrix=X();this.viewMatrix=X();this.activeViewMatrix=null;this.modelMatrix=X();this.mvpMatrix=X();this.normalMatrix=new Float32Array(9);this.initialTheta=.6;this.initialPhi=.35;this.initialDistance=null;this.customDistance=!1;this.distanceMode="auto";this.initialCustomTarget=null;this.customTarget=null;this.fov=Math.PI/4;this.minTheta=-1/0;this.maxTheta=1/0;this.minPhi=-Math.PI*.48;this.maxPhi=Math.PI*.48;this.minDistanceLimit=null;this.maxDistanceLimit=null;this.activePointers=new Map;this.isPanning=!1;this.targetTheta=.6;this.currTheta=.6;this.targetPhi=.35;this.currPhi=.35;this.targetDistance=3;this.currDistance=3;this.autoRotate=!1;this.autoRotateSpeed=Math.PI/6;this.autoRotateDelay=3e3;this.lastInteractionTime=-1/0;this.turntableRotation=0;this.isPointerDown=!1;this.interactive=!0;this.skyboxTexture=null;this.lastPointerX=0;this.lastPointerY=0;this.totalDragDist=0;this.selectedMeshName="";this.pickFbo=null;this.pickTexture=null;this.pickDepth=null;this.pickFboWidth=0;this.pickFboHeight=0;this.animFrameId=null;this.isVisible=!0;this.xrSessionActive=!1;this.lastTime=0;this.lastReportedTheta=-999;this.lastReportedPhi=-999;this.lastReportedZoom=-999;this.boundPointerDown=null;this.boundPointerMove=null;this.boundPointerUp=null;this.boundWheel=null;this.boundKeyDown=null;this.boundContextMenu=null;this.boundSetCamera=null;this.boundResetCamera=null;this.contextLost=!1;this.lastGlbData=null;this.boundContextLost=null;this.boundContextRestored=null;this.stereoSBS=!1;this.renderErrorReported=!1;this.xrDepthState={enabled:!1,texture:null,uvTransform:X(),rawValueToMeters:.001,format:0};this.container=e;let i,r;if(t&&typeof t.getContext=="function"?(i=t,r=s):(r=t,i=r?.canvas),!i){let a=e.querySelector("canvas");a||(a=document.createElement("canvas"),a.style.width="100%",a.style.height="100%",a.style.display="block",a.style.touchAction="none",e.prepend(a)),i=a}this.canvas=i,this.opts=r||{},this.opts.clearColor&&(this.clearColor=this.opts.clearColor);let o=typeof window<"u"&&typeof window.matchMedia=="function"&&window.matchMedia("(prefers-reduced-motion: reduce)").matches;this.opts.autoRotate!==void 0?this.autoRotate=this.opts.autoRotate&&!o:o&&(this.autoRotate=!1),this.opts.autoRotateSpeed!==void 0&&(this.autoRotateSpeed=this.opts.autoRotateSpeed),this.opts.autoRotateDelay!==void 0&&(this.autoRotateDelay=Math.max(0,Number(this.opts.autoRotateDelay)||0)),this.opts.exposure!==void 0&&(this.exposure=we(this.opts.exposure)),this.opts.camera&&this.setInitialCamera(this.opts.camera.theta,this.opts.camera.phi,this.opts.camera.distance,this.opts.camera.zoom),this.opts.shadowIntensity!==void 0&&(this.shadowIntensity=Math.max(0,Math.min(1,Number(this.opts.shadowIntensity)||0))),this.opts.shadowSoftness!==void 0&&(this.shadowSoftness=Math.max(0,Math.min(1,Number(this.opts.shadowSoftness)||0))),this.opts.toneMapping!==void 0&&(this.toneMapping=ge(this.opts.toneMapping)),this.opts.autoplay!==void 0&&(this.autoplay=this.opts.autoplay),this.opts.animationName!==void 0&&(this.animationName=this.opts.animationName),this.opts.variantName!==void 0&&(this.activeVariantName=this.opts.variantName||null),this.opts.fieldOfView!==void 0&&(this.fov=this.opts.fieldOfView*Math.PI/180),this.opts.distanceMode!==void 0&&(this.distanceMode=this.opts.distanceMode),this.opts.cameraTarget!==void 0&&(this.setTarget(this.opts.cameraTarget[0],this.opts.cameraTarget[1],this.opts.cameraTarget[2]),this.initialCustomTarget=[...this.opts.cameraTarget]),(this.opts.minCameraOrbit||this.opts.maxCameraOrbit)&&this.setOrbitLimits({minTheta:this.opts.minCameraOrbit?.theta,maxTheta:this.opts.maxCameraOrbit?.theta,minPhi:this.opts.minCameraOrbit?.phi,maxPhi:this.opts.maxCameraOrbit?.phi,minDistance:this.opts.minCameraOrbit?.distance,maxDistance:this.opts.maxCameraOrbit?.distance});let c=i.getContext("webgl2",{antialias:!0,alpha:!0,xrCompatible:!0});if(!c)throw new Error("WebGL2 not supported");this.gl=c,this.initGlState(),this.setupEvents(),this.setupContextLossHandling(),this.opts.environmentImage&&this.setEnvironmentImage(this.opts.environmentImage).catch(()=>{}),typeof requestAnimationFrame<"u"&&this.opts.autoStart!==!1&&this.start()}static registerDecoder(e,t){Ke(e,t)}static getDecoder(e){return le(e)}setupContextLossHandling(){let e=this.canvas;typeof e?.addEventListener=="function"&&(this.boundContextLost=t=>{t.preventDefault?.(),this.contextLost=!0,this.stop()},this.boundContextRestored=()=>this.restoreContext(),e.addEventListener("webglcontextlost",this.boundContextLost),e.addEventListener("webglcontextrestored",this.boundContextRestored))}initGlState(){let e=this.gl,t=We();this.renderProg=Ee(e,Ue,t.fs),this.pickProg=Ee(e,Je,Qe),this.uRender=Pe(e,this.renderProg,t.uniforms),this.uPick=Pe(e,this.pickProg,["uMVP","uColorId"]),this.setupPickingFbo()}restoreContext(){let e=this.gl;this.contextLost=!1,this.initGlState();for(let t of Y)t.onContextRestored?.(e,this);this.meshes=[],this.gpuTextures=[],this.environmentTexture=null,this.lastGlbData&&this.loadGLBData(this.lastGlbData,this.lastLimits),this.opts.environmentImage&&this.setEnvironmentImage(this.opts.environmentImage).catch(()=>{}),typeof requestAnimationFrame<"u"&&this.opts.autoStart!==!1&&this.start()}setupPickingFbo(){let e=this.gl;if(typeof e.createFramebuffer!="function")return;this.pickFbo=e.createFramebuffer(),this.pickTexture=e.createTexture(),this.pickDepth=e.createRenderbuffer();let t=Math.max(1,this.canvas.width),s=Math.max(1,this.canvas.height);this.pickFboWidth=t,this.pickFboHeight=s,e.bindTexture(3553,this.pickTexture),e.texImage2D(3553,0,6408,t,s,0,6408,5121,null),e.texParameteri(3553,10241,9728),e.texParameteri(3553,10240,9728),e.bindRenderbuffer(36161,this.pickDepth),e.renderbufferStorage(36161,33189,t,s),e.bindFramebuffer(36160,this.pickFbo),e.framebufferTexture2D(36160,36064,3553,this.pickTexture,0),e.framebufferRenderbuffer(36160,36096,36161,this.pickDepth),e.bindFramebuffer(36160,null)}resizePickingFbo(e,t){if(!this.pickFbo)return;let s=this.gl,i=Math.max(1,e),r=Math.max(1,t);this.pickFboWidth=i,this.pickFboHeight=r,s.bindTexture(3553,this.pickTexture),s.texImage2D(3553,0,6408,i,r,0,6408,5121,null),s.bindRenderbuffer(36161,this.pickDepth),s.renderbufferStorage(36161,33189,i,r)}loadGLBData(e,t){this.lastGlbData=e,this.lastLimits=t;let{meshes:s,bounds:i,lights:r,animations:o,worldMatrices:c,gpuTextures:a,availableVariants:l}=Ge(this.gl,e,t,()=>this.render());if(this.disposeMeshGpuResources(),this.meshes=s,this.bounds=i,this.turntableRotation=0,this.updateTurntableMatrix(),this.lights=r,this.animations=o||[],this.worldMatrices=c||[],this.gpuTextures=a||[],this.availableVariants=l||[],this.gltf=e.gltf,this.activeVariantName&&!this.applyVariantMaterial(this.activeVariantName)&&(this.activeVariantName=null),this.initialNodeTransforms=[],Array.isArray(e.gltf?.nodes))for(let m of e.gltf.nodes)this.initialNodeTransforms.push({t:m.translation?[m.translation[0],m.translation[1],m.translation[2]]:[0,0,0],r:m.rotation?[m.rotation[0],m.rotation[1],m.rotation[2],m.rotation[3]]:[0,0,0,1],s:m.scale?[m.scale[0],m.scale[1],m.scale[2]]:[1,1,1]});this.autoplay&&this.animations.length>0&&this.playAnimation(this.animationName||void 0);for(let m of Y)m.onLoadModel?.(this,this.gltf);this.customDistance||(this.initialDistance=this.distanceMode==="absolute"?3:i.radius*2.5,this.targetDistance=this.initialDistance,this.currDistance=this.initialDistance);let u=e.exposure??e.gltf?.extras?.exposure??e.gltf?.asset?.extras?.exposure;u!==void 0&&(this.exposure=we(u));let p=e.environmentIntensity??e.gltf?.extras?.environmentIntensity??e.gltf?.asset?.extras?.environmentIntensity;p!==void 0&&(this.envIntensity=Math.max(0,Number(p)||0)),this.container&&typeof this.container.hasAttribute=="function"&&this.container.hasAttribute("poster")&&this.container.removeAttribute("poster"),this.resize(),this.update(0),this.render(),this.animFrameId===null&&typeof requestAnimationFrame<"u"&&this.opts.autoStart!==!1&&this.start()}loadGLB(e){this.loadGLBData(Ve(e))}clearScene(){this.disposeMeshGpuResources(),this.lights=[],this.animations=[],this.currentClip=null,this.isPlayingAnimation=!1,this.worldMatrices=[],this.gltf=null,this.initialNodeTransforms=[],this.turntableRotation=0,this.updateTurntableMatrix(),this.gl&&this.render()}loadModel(e){this.loadGLBData(Se(e))}setBackdropGLB(e,t){if(this.disposeMeshListGpuResources(this.backdropMeshes),this.backdropMeshes=[],!e){this.update(0),this.render();return}let s=Se(e),{meshes:i}=Ge(this.gl,s,Oe,()=>this.render());if(t)for(let r of i)r.transform[12]+=t[0],r.transform[13]+=t[1],r.transform[14]+=t[2],ae(r.normalMatrix,r.transform);this.backdropMeshes=i,this.update(0),this.render()}setClearColor(e,t,s,i=1){this.clearColor=[e,t,s,i]}updateMeshTransforms(e){for(let t=0;t<this.meshes.length;t++){let s=this.meshes[t],i;e instanceof Map?i=e.get(s.name)??e.get(t):Array.isArray(e)?i=e[t]:e&&typeof e=="object"&&(Object.prototype.hasOwnProperty.call(e,s.name)?i=e[s.name]:Object.prototype.hasOwnProperty.call(e,t)&&(i=e[t])),i&&i.length===16&&(s.transform.set(i),ae(s.normalMatrix,s.transform))}}bindLights(e=this.uRender){let t=this.gl,s=[.28,.28,.28],i=[],r=[],o=[],c=[];for(let a of this.lights){let l=a.color||[1,1,1],u=a.intensity!==void 0?a.intensity:1;if(a.type==="ambient")s=[l[0]*u,l[1]*u,l[2]*u];else if(a.type==="directional"&&i.length<6){let p=a.direction||[.5,1,.8];i.push(p[0],p[1],p[2]),r.push(l[0]*u,l[1]*u,l[2]*u)}else if(a.type==="point"&&o.length<6){let p=a.position||[0,0,0];o.push(p[0],p[1],p[2]),c.push(l[0]*u,l[1]*u,l[2]*u)}}i.length===0&&(i.push(.5,1,.8),r.push(1,1,1)),t.uniform3fv(e.uAmbientColor,s),t.uniform1i(e.uNumDirLights,i.length/3),i.length>0&&(t.uniform3fv(e.uDirLightDir,i),t.uniform3fv(e.uDirLightColor,r)),t.uniform1i(e.uNumPointLights,o.length/3),o.length>0&&(t.uniform3fv(e.uPointLightPos,o),t.uniform3fv(e.uPointLightColor,c)),t.uniform1f(e.uEnvIntensity,this.envIntensity)}setExposure(e){this.exposure=we(e)}getExposure(){return this.exposure}setShadow(e,t){this.shadowIntensity=Math.max(0,Math.min(1,Number(e)||0)),t!==void 0&&(this.shadowSoftness=Math.max(0,Math.min(1,Number(t)||0)))}getShadowIntensity(){return this.shadowIntensity}getShadowSoftness(){return this.shadowSoftness}setToneMapping(e){this.toneMapping=ge(e)}getToneMapping(){return this.toneMapping}async setEnvironmentImage(e){for(let t of Y)if(t.setEnvironmentImage){await t.setEnvironmentImage(this,e);return}}async setSkyboxImage(e){for(let t of Y)if(t.setSkyboxImage){await t.setSkyboxImage(this,e);return}}setInteractive(e){this.interactive=e}isInteractive(){return this.interactive}getCameraOrbit(){let e=this.currTheta,t=this.currPhi,s=this.currDistance;return{theta:e,phi:t,radius:s,toString(){return`${e.toFixed(4)}rad ${t.toFixed(4)}rad ${s.toFixed(4)}m`}}}getCameraTarget(){let e=this.customTarget||this.bounds.center;return{x:e[0],y:e[1],z:e[2],toString(){return`${e[0].toFixed(4)}m ${e[1].toFixed(4)}m ${e[2].toFixed(4)}m`}}}jumpCameraToGoal(){this.currTheta=this.targetTheta,this.currPhi=this.targetPhi,this.currDistance=this.targetDistance,this.update(0),this.render()}getModelMaterials(){let e=this.gltf?.materials?.length?this.gltf.materials:[{name:"DefaultMaterial",pbrMetallicRoughness:{}}];return e.map((t,s)=>{let i=this.meshes.flatMap(u=>u.primitives.filter(p=>p.materialIndex===s||e.length===1&&p.materialIndex===void 0)),r=i[0],o=r?[...r.baseColor]:t.pbrMetallicRoughness?.baseColorFactor||[.8,.8,.8,1],c=r?r.metallic:t.pbrMetallicRoughness?.metallicFactor??.1,a=r?r.roughness:t.pbrMetallicRoughness?.roughnessFactor??.5,l={baseColorFactor:o,metallicFactor:c,roughnessFactor:a,setBaseColorFactor:u=>{let p=[u[0],u[1],u[2],u[3]??1];l.baseColorFactor=p,t.pbrMetallicRoughness&&(t.pbrMetallicRoughness.baseColorFactor=p);for(let m of i)m.baseColor=p;this.render()},setMetallicFactor:u=>{l.metallicFactor=u,t.pbrMetallicRoughness&&(t.pbrMetallicRoughness.metallicFactor=u);for(let p of i)p.metallic=u;this.render()},setRoughnessFactor:u=>{l.roughnessFactor=u,t.pbrMetallicRoughness&&(t.pbrMetallicRoughness.roughnessFactor=u);for(let p of i)p.roughness=u;this.render()}};return{name:t.name||`Material_${s}`,pbrMetallicRoughness:l}})}playAnimation(e){if(this.animations.length!==0){if(e){let t=this.animations.find(s=>s.name===e);t&&(this.currentClip=t)}else this.currentClip||(this.currentClip=this.animations[0]);this.isPlayingAnimation=!0}}pauseAnimation(){this.isPlayingAnimation=!1}getAvailableAnimations(){return this.animations.map(e=>e.name)}getAvailableVariants(){return[...this.availableVariants]}getVariantName(){return this.activeVariantName}selectVariant(e){let t=this.applyVariantMaterial(e);return t&&this.gl&&this.render(),t}applyVariantMaterial(e){let t=e===null||e==="",s=-1;if(t)this.activeVariantName=null;else{if(s=this.availableVariants.indexOf(e),s<0)return!1;this.activeVariantName=e}let i=this.gltf?.materials;if(!Array.isArray(i))return t;for(let r of this.meshes)for(let o of r.primitives){let c=o.defaultMaterialIndex;if(!t&&Array.isArray(o.variantMappings)){for(let l of o.variantMappings)if(Array.isArray(l.variants)&&l.variants.includes(s)){c=l.material;break}}let a=c!==void 0?i[c]:void 0;o.materialIndex=a?c:void 0,Object.assign(o,Xe(a,this.gpuTextures)),this.applyPrimitiveProgram(o,o.materialIndex,a??null)}return!0}applyPrimitiveProgram(e,t,s){if(!this.gl||!e.primitiveDef)return;e.programCache??(e.programCache=new Map);let i=t??-1,r=e.programCache.get(i);r||(r=He(this.gl,{...e.primitiveDef,material:t},s),e.programCache.set(i,r)),e.customProgram=r.program,e.customUniforms=r.uniforms,e.pluginData&&(e.pluginData.materials=e.isTransmissive?{isTransmissive:!0}:void 0)}setAutoplay(e){this.autoplay=e,e?this.playAnimation(this.animationName||void 0):this.pauseAnimation()}setAnimationName(e){this.animationName=e,(this.isPlayingAnimation||this.autoplay)&&this.playAnimation(e)}evaluateAnimation(e){for(let t of Y)if(t.evaluateAnimation){t.evaluateAnimation(this,e);return}}setTarget(e,t,s){this.customTarget=[e,t,s],this.gl&&(this.meshes.length>0||this.backdropMeshes.length>0)&&(this.update(0),this.render())}getTarget(){return this.customTarget?[...this.customTarget]:null}setFieldOfView(e){let t=Math.max(10,Math.min(90,e));this.fov=t*Math.PI/180}getFieldOfView(){return this.fov*180/Math.PI}setDistanceMode(e){if(this.distanceMode=e,!this.customDistance){let t=this.clampDistance(this.distanceMode==="absolute"?3:this.bounds.radius*2.5);this.initialDistance=t,this.targetDistance=t,this.currDistance=t,this.gl&&(this.meshes.length>0||this.backdropMeshes.length>0)&&(this.update(0),this.render())}}getDistanceMode(){return this.distanceMode}setOrbitLimits(e){e.minTheta!==void 0&&(this.minTheta=e.minTheta*Math.PI/180),e.maxTheta!==void 0&&(this.maxTheta=e.maxTheta*Math.PI/180),e.minPhi!==void 0&&(this.minPhi=Math.max(-Math.PI*.48,e.minPhi*Math.PI/180)),e.maxPhi!==void 0&&(this.maxPhi=Math.min(Math.PI*.48,e.maxPhi*Math.PI/180)),e.minDistance!==void 0&&(this.minDistanceLimit=e.minDistance),e.maxDistance!==void 0&&(this.maxDistanceLimit=e.maxDistance),this.targetTheta=this.clampTheta(this.targetTheta),this.targetPhi=this.clampPhi(this.targetPhi),this.targetDistance=this.clampDistance(this.targetDistance)}getOrbitLimits(){return{minTheta:isFinite(this.minTheta)?this.minTheta*180/Math.PI:void 0,maxTheta:isFinite(this.maxTheta)?this.maxTheta*180/Math.PI:void 0,minPhi:this.minPhi*180/Math.PI,maxPhi:this.maxPhi*180/Math.PI,minDistance:this.minDistanceLimit!==null?this.minDistanceLimit:void 0,maxDistance:this.maxDistanceLimit!==null?this.maxDistanceLimit:void 0}}clampTheta(e){return Math.max(this.minTheta,Math.min(this.maxTheta,e))}clampPhi(e){return Math.max(this.minPhi,Math.min(this.maxPhi,e))}clampDistance(e){let t=this.minDistanceLimit!==null?this.minDistanceLimit:this.bounds?this.bounds.radius*.5:.1,s=this.maxDistanceLimit!==null?this.maxDistanceLimit:this.bounds?this.bounds.radius*8:100;return Math.max(t,Math.min(s,e))}setCamera(e,t,s,i){e!==void 0&&(this.targetTheta=this.clampTheta(e*Math.PI/180)),t!==void 0&&(this.targetPhi=this.clampPhi(t*Math.PI/180)),s!==void 0&&s>0&&(this.targetDistance=this.clampDistance(s)),i!==void 0&&i>0&&(this.targetDistance=this.clampDistance(this.bounds.radius*2.5/i))}setInitialCamera(e,t,s,i){if(e!==void 0&&(this.initialTheta=this.clampTheta(e*Math.PI/180),this.targetTheta=this.initialTheta,this.currTheta=this.initialTheta),t!==void 0&&(this.initialPhi=this.clampPhi(t*Math.PI/180),this.targetPhi=this.initialPhi,this.currPhi=this.initialPhi),s!==void 0&&s>0){let r=this.clampDistance(s);this.initialDistance=r,this.customDistance=!0,this.targetDistance=r,this.currDistance=r}else if(i!==void 0&&i>0){let r=this.clampDistance(this.bounds.radius*2.5/i);this.initialDistance=r,this.customDistance=!0,this.targetDistance=r,this.currDistance=r}else{this.customDistance=!1;let r=this.clampDistance(this.distanceMode==="absolute"?3:this.bounds.radius*2.5);this.initialDistance=r,this.targetDistance=r,this.currDistance=r}this.gl&&(this.meshes.length>0||this.backdropMeshes.length>0)&&(this.update(0),this.render())}resetCamera(){this.targetTheta=this.clampTheta(this.initialTheta),this.targetPhi=this.clampPhi(this.initialPhi),this.initialDistance!==null?this.targetDistance=this.clampDistance(this.initialDistance):this.targetDistance=this.clampDistance(this.bounds.radius*2.5),this.initialCustomTarget?this.customTarget=[...this.initialCustomTarget]:this.customTarget=null,this.turntableRotation=0,this.updateTurntableMatrix()}projectPointToScreen(e,t){let s=X();J(s,this.projMatrix,this.viewMatrix);let[i,r,o]=e,c=t?t[0]:0,a=t?t[1]:0,l=t?t[2]:0,u=this.modelRootMatrix;u&&(i=u[0]*e[0]+u[4]*e[1]+u[8]*e[2]+u[12],r=u[1]*e[0]+u[5]*e[1]+u[9]*e[2]+u[13],o=u[2]*e[0]+u[6]*e[1]+u[10]*e[2]+u[14],t&&(c=u[0]*t[0]+u[4]*t[1]+u[8]*t[2],a=u[1]*t[0]+u[5]*t[1]+u[9]*t[2],l=u[2]*t[0]+u[6]*t[1]+u[10]*t[2]));let p=s[0]*i+s[4]*r+s[8]*o+s[12],m=s[1]*i+s[5]*r+s[9]*o+s[13],g=s[2]*i+s[6]*r+s[10]*o+s[14],d=s[3]*i+s[7]*r+s[11]*o+s[15];if(d<=1e-4)return{x:0,y:0,z:1,visible:!1};let E=p/d,b=m/d,x=g/d,R=this.canvas.getBoundingClientRect(),y=R.width||this.canvas.clientWidth||this.canvas.width||300,M=R.height||this.canvas.clientHeight||this.canvas.height||150,f=(E*.5+.5)*y,T=(-b*.5+.5)*M,S=E>=-1.1&&E<=1.1&&b>=-1.1&&b<=1.1&&x>=-1&&x<=1;if(S&&t){let[L,k,U]=he(this),F=[i-L,r-k,o-U];c*F[0]+a*F[1]+l*F[2]>.05&&(S=!1)}return{x:f,y:T,z:x,visible:S}}setSelectedMesh(e){this.selectedMeshName=e||""}getSelectedMesh(){return this.selectedMeshName}setupEvents(){let e=this.canvas;this.boundContextMenu=t=>{t.preventDefault()},e.addEventListener("contextmenu",this.boundContextMenu),this.boundPointerDown=t=>{this.interactive&&(this.isPointerDown=!0,this.lastInteractionTime=typeof performance<"u"?performance.now():Date.now(),this.lastPointerX=t.clientX,this.lastPointerY=t.clientY,this.totalDragDist=0,this.activePointers.set(t.pointerId,{x:t.clientX,y:t.clientY}),this.isPanning=t.button===2||t.shiftKey||this.activePointers.size===2,e.setPointerCapture(t.pointerId))},e.addEventListener("pointerdown",this.boundPointerDown),this.boundPointerMove=t=>{if(!this.isPointerDown||!this.interactive)return;this.lastInteractionTime=typeof performance<"u"?performance.now():Date.now();let s=this.activePointers.get(t.pointerId)||{x:this.lastPointerX,y:this.lastPointerY},i=t.clientX-s.x,r=t.clientY-s.y;if(this.activePointers.set(t.pointerId,{x:t.clientX,y:t.clientY}),this.lastPointerX=t.clientX,this.lastPointerY=t.clientY,this.totalDragDist+=Math.hypot(i,r),this.isPanning||t.button===2||t.shiftKey||this.activePointers.size===2){let o=[this.viewMatrix[0],this.viewMatrix[4],this.viewMatrix[8]],c=[this.viewMatrix[1],this.viewMatrix[5],this.viewMatrix[9]],a=this.customTarget?[...this.customTarget]:[...this.bounds.center],l=this.currDistance/Math.max(e.clientHeight||300,1)*1.5;a[0]+=(-i*o[0]+r*c[0])*l,a[1]+=(-i*o[1]+r*c[1])*l,a[2]+=(-i*o[2]+r*c[2])*l,this.customTarget=a}else this.targetTheta=this.clampTheta(this.targetTheta-i*.008),this.targetPhi=this.clampPhi(this.targetPhi+r*.008)},e.addEventListener("pointermove",this.boundPointerMove),this.boundPointerUp=t=>{this.lastInteractionTime=typeof performance<"u"?performance.now():Date.now(),this.activePointers.delete(t.pointerId),this.activePointers.size===0?(this.isPointerDown=!1,this.isPanning=!1):this.activePointers.size===1&&(this.isPanning=!1);try{e.releasePointerCapture(t.pointerId)}catch{}if(this.totalDragDist<5&&t.button===0){let s=e.getBoundingClientRect(),i=Math.floor((t.clientX-s.left)*(e.width/Math.max(s.width,1))),r=Math.floor((t.clientY-s.top)*(e.height/Math.max(s.height,1)));this.performMeshPick(i,r,!0)}},e.addEventListener("pointerup",this.boundPointerUp),this.boundWheel=t=>{if(!this.interactive)return;t.preventDefault(),this.lastInteractionTime=typeof performance<"u"?performance.now():Date.now();let s=Math.exp(t.deltaY*.001);this.targetDistance=this.clampDistance(this.targetDistance*s)},e.addEventListener("wheel",this.boundWheel,{passive:!1}),this.boundSetCamera=t=>{let s=t.detail||{};s.reset?this.resetCamera():this.setCamera(s.theta,s.phi,s.distance,s.zoom)},this.container.addEventListener("setCamera",this.boundSetCamera),this.boundResetCamera=()=>{this.resetCamera()},this.container.addEventListener("resetCamera",this.boundResetCamera),e.tabIndex!==void 0&&e.tabIndex<0&&(e.tabIndex=0),e.getAttribute?.("role")||e.setAttribute?.("role","region"),e.getAttribute?.("aria-label")||e.setAttribute?.("aria-label","Interactive 3D model viewport"),this.boundKeyDown=t=>{if(!this.interactive)return;let s=typeof performance<"u"?performance.now():Date.now();switch(t.key){case"ArrowLeft":this.lastInteractionTime=s,this.targetTheta=this.clampTheta(this.targetTheta-.1),t.preventDefault();break;case"ArrowRight":this.lastInteractionTime=s,this.targetTheta=this.clampTheta(this.targetTheta+.1),t.preventDefault();break;case"ArrowUp":this.lastInteractionTime=s,this.targetPhi=this.clampPhi(this.targetPhi+.08),t.preventDefault();break;case"ArrowDown":this.lastInteractionTime=s,this.targetPhi=this.clampPhi(this.targetPhi-.08),t.preventDefault();break;case"+":case"=":{this.lastInteractionTime=s,this.targetDistance=this.clampDistance(this.targetDistance*.9),t.preventDefault();break}case"-":case"_":{this.lastInteractionTime=s,this.targetDistance=this.clampDistance(this.targetDistance*1.1),t.preventDefault();break}case"Home":this.lastInteractionTime=s,this.resetCamera(),t.preventDefault();break}},e.addEventListener("keydown",this.boundKeyDown)}teardownEvents(){let e=this.canvas;this.boundContextLost&&(e.removeEventListener("webglcontextlost",this.boundContextLost),this.boundContextLost=null),this.boundContextRestored&&(e.removeEventListener("webglcontextrestored",this.boundContextRestored),this.boundContextRestored=null),this.boundContextMenu&&(e.removeEventListener("contextmenu",this.boundContextMenu),this.boundContextMenu=null),this.boundPointerDown&&(e.removeEventListener("pointerdown",this.boundPointerDown),this.boundPointerDown=null),this.boundPointerMove&&(e.removeEventListener("pointermove",this.boundPointerMove),this.boundPointerMove=null),this.boundPointerUp&&(e.removeEventListener("pointerup",this.boundPointerUp),this.boundPointerUp=null),this.boundWheel&&(e.removeEventListener("wheel",this.boundWheel),this.boundWheel=null),this.boundKeyDown&&(e.removeEventListener("keydown",this.boundKeyDown),this.boundKeyDown=null),this.boundSetCamera&&(this.container.removeEventListener("setCamera",this.boundSetCamera),this.boundSetCamera=null),this.boundResetCamera&&(this.container.removeEventListener("resetCamera",this.boundResetCamera),this.boundResetCamera=null)}raycastFromScreenPoint(e,t){let s=this.canvas,i=s.getBoundingClientRect(),r=s.width||300,o=s.height||300,c=i.width>0?Math.floor((e-i.left)*(r/i.width)):Math.floor(e),a=i.height>0?Math.floor((t-i.top)*(o/i.height)):Math.floor(t);return this.performMeshPick(c,a,!1)}performMeshPick(e,t,s=!0){if(this.meshes.length===0)return null;let i=this.gl,r=Math.max(1,this.canvas.width||300),o=Math.max(1,this.canvas.height||300);e=this.mapStereoPickX(e,r);let c=X();J(c,this.projMatrix,this.viewMatrix);let a=[],l=Math.max(0,Math.min(r-1,e)),u=Math.max(0,Math.min(o-1,o-1-t));if(this.pickFbo&&i&&!this.xrSessionActive){(this.pickFboWidth!==r||this.pickFboHeight!==o)&&this.resizePickingFbo(r,o),i.bindFramebuffer(36160,this.pickFbo),i.viewport(0,0,r,o),i.enable(2929),i.clearColor(0,0,0,0),i.clear(16384|256),i.useProgram(this.pickProg);for(let S=0;S<this.meshes.length;S++){let L=this.meshes[S];if(L.visible===!1||L.isPluginMesh)continue;let k=S+1,U=(k&255)/255,F=(k>>8&255)/255,h=(k>>16&255)/255;i.uniform4f(this.uPick.uColorId,U,F,h,1),J(this.modelMatrix,this.modelRootMatrix,L.transform);let P=L.instanceMatrices;if(P&&P.length>=16){let w=P.length>>4,v=X();for(let B of L.primitives){i.bindVertexArray(B.vao);for(let D=0;D<w;D++)J(v,this.modelMatrix,P.subarray(D*16,D*16+16)),J(this.mvpMatrix,c,v),i.uniformMatrix4fv(this.uPick.uMVP,!1,this.mvpMatrix),ve(i,B)}}else{J(this.mvpMatrix,c,this.modelMatrix),i.uniformMatrix4fv(this.uPick.uMVP,!1,this.mvpMatrix);for(let w of L.primitives)i.bindVertexArray(w.vao),ve(i,w)}}let f=new Uint8Array(4);i.readPixels(l,u,1,1,6408,5121,f),i.bindFramebuffer(36160,null);let T=f[0]+(f[1]<<8)+(f[2]<<16);T>0&&T<=this.meshes.length&&(a=[this.meshes[T-1]])}a.length===0&&(a=this.meshes.filter(f=>f.visible!==!1));let p=null,m=1/0,g=[0,0,0],d=[0,1,0],[E,b,x]=he(this),R=X();for(let f of a){J(R,this.modelRootMatrix,f.transform);let T=f.instanceMatrices,S=T&&T.length>=16?T.length>>4:1;for(let L=0;L<S;L++){T&&T.length>=16?J(this.modelMatrix,R,T.subarray(L*16,L*16+16)):this.modelMatrix.set(R),J(this.mvpMatrix,c,this.modelMatrix);let k=X();if(!De(k,this.mvpMatrix))continue;let U=(l+.5)/r*2-1,F=(u+.5)/o*2-1,h=[0,0,0],P=[0,0,0];Te(h,U,F,-1,k),Te(P,U,F,1,k);let w=P[0]-h[0],v=P[1]-h[1],B=P[2]-h[2],D=Math.hypot(w,v,B)||1,$=w/D,A=v/D,W=B/D,_={distance:1/0,point:[0,0,0],normal:[0,1,0]};if((q=>{for(let G of f.primitives){if(!G.positions)continue;let z=G.positions,I=G.indices,ne=Math.floor(I?I.length/3:z.length/9);for(let H=0;H<ne;H++){let Q=I?I[H*3]:H*3,ee=I?I[H*3+1]:H*3+1,ie=I?I[H*3+2]:H*3+2;Le(h[0],h[1],h[2],$,A,W,z[Q*3],z[Q*3+1],z[Q*3+2],z[ee*3],z[ee*3+1],z[ee*3+2],z[ie*3],z[ie*3+1],z[ie*3+2],_,q)}}})(1e-4),isFinite(_.distance)){let q=_.point[0],G=_.point[1],z=_.point[2],I=this.modelMatrix,ne=I[0]*q+I[4]*G+I[8]*z+I[12],H=I[1]*q+I[5]*G+I[9]*z+I[13],Q=I[2]*q+I[6]*G+I[10]*z+I[14],ee=ne-E,ie=H-b,oe=Q-x,N=Math.hypot(ee,ie,oe);if(N<m){ae(this.normalMatrix,this.modelMatrix);let O=this.normalMatrix,te=_.normal[0],re=_.normal[1],C=_.normal[2],j=O[0]*te+O[3]*re+O[6]*C,V=O[1]*te+O[4]*re+O[7]*C,Z=O[2]*te+O[5]*re+O[8]*C,se=Math.hypot(j,V,Z)||1;j/=se,V/=se,Z/=se,j*ee+V*ie+Z*oe>0&&(j=-j,V=-V,Z=-Z),p=f,m=Math.round(N*1e3)/1e3,g=[Math.round(ne*1e3)/1e3,Math.round(H*1e3)/1e3,Math.round(Q*1e3)/1e3],d=[Math.round(j*1e3)/1e3,Math.round(V*1e3)/1e3,Math.round(Z*1e3)/1e3]}}}}if(!p)return null;let M={meshIndex:this.meshes.indexOf(p),meshName:p.name,nodeIndex:p.nodeIndex,distance:m,hitPoint:g,normal:d,screenX:e,screenY:t};if(s){for(let f of Y)f.onMeshSelect?.(this,M);this.opts.onMeshClick&&this.opts.onMeshClick(M),typeof CustomEvent<"u"&&typeof this.container.dispatchEvent=="function"&&this.container.dispatchEvent(new CustomEvent("meshClick",{bubbles:!0,detail:M}))}return M}mapStereoPickX(e,t){if(!this.stereoSBS||this.xrSessionActive||t<2)return e;let s=Math.max(1,Math.floor(t*.5)),i=e>=s,r=i?e-s:e,o=i?Math.max(1,t-s):s,c=(r+.5)/o*2-1;return Math.floor((c*.5+1)*.5*t)}setStereoSBS(e){this.stereoSBS=!!e,this.xrSessionActive||(this.syncCameraMatrices(),this.render())}syncCameraMatrices(){let e=Math.max(1,this.canvas.width||1)/Math.max(1,this.canvas.height||1),t=this.backdropMeshes.length>0?Math.max(this.bounds.radius*20,120):Math.max(this.bounds.radius*20,10);pe(this.projMatrix,this.fov,e,.1,t);let s=this.customTarget||this.bounds.center;be(this.viewMatrix,he(this),s,[0,1,0]),J(this.mvpMatrix,this.projMatrix,this.viewMatrix),ae(this.normalMatrix,this.modelMatrix)}resize(){let e=Math.min((typeof window<"u"?window.devicePixelRatio:1)||1,2),t=this.canvas.getBoundingClientRect(),s=Math.max(1,Math.floor(t.width*e)),i=Math.max(1,Math.floor(t.height*e));this.canvas.width!==s||this.canvas.height!==i?(this.canvas.width=s,this.canvas.height=i,this.resizePickingFbo(s,i),this.xrSessionActive||this.syncCameraMatrices()):!this.xrSessionActive&&this.projMatrix[0]===0&&this.syncCameraMatrices()}getGLContext(){return this.gl}setXRSessionActive(e){let t=this.xrSessionActive;if(this.xrSessionActive=e,t&&!e){this.xrDepthState.enabled=!1;for(let s of Y)s.onXRSessionEnd?.(this);if(this.lastTime=typeof performance<"u"?performance.now():Date.now(),this.updateTurntableMatrix(),this.isVisible)try{this.resize(),this.syncCameraMatrices(),this.render()}catch(s){this.reportRenderError(s)}}}reportRenderError(e){this.renderErrorReported||(this.renderErrorReported=!0,typeof console<"u"&&console.warn("[three-live] render frame failed:",e))}stepXRUpdate(e){for(let t of Y)t.onUpdate?.(this,e)}start(){if(this.animFrameId!==null)return;this.lastTime=typeof performance<"u"?performance.now():Date.now();let e=t=>{if(this.animFrameId===null)return;let s=Math.min(Math.max((t-this.lastTime)/1e3,0),.1);if(this.lastTime=t,this.isVisible&&!this.xrSessionActive)try{this.resize(),this.update(s),this.render()}catch(i){this.reportRenderError(i)}this.animFrameId!==null&&(this.animFrameId=requestAnimationFrame(e))};this.animFrameId=requestAnimationFrame(e)}stop(){this.animFrameId!==null&&(cancelAnimationFrame(this.animFrameId),this.animFrameId=null)}setAutoRotate(e){this.autoRotate=e}getAutoRotate(){return this.autoRotate}setAutoRotateSpeed(e){this.autoRotateSpeed=e}getAutoRotateSpeed(){return this.autoRotateSpeed}setAutoRotateDelay(e){this.autoRotateDelay=Math.max(0,Number(e)||0)}getAutoRotateDelay(){return this.autoRotateDelay}resetTurntableRotation(e=0){this.turntableRotation=Number(e)||0,this.updateTurntableMatrix(),this.render()}updateTurntableMatrix(){let e=Math.cos(this.turntableRotation),t=Math.sin(this.turntableRotation),s=this.bounds.center[0],i=this.bounds.center[2],r=this.modelRootMatrix;r[0]=e,r[1]=0,r[2]=-t,r[3]=0,r[4]=0,r[5]=1,r[6]=0,r[7]=0,r[8]=t,r[9]=0,r[10]=e,r[11]=0,r[12]=s-(e*s+t*i),r[13]=0,r[14]=i-(-t*s+e*i),r[15]=1}setVisible(e){this.isVisible=e}update(e){for(let o of Y)o.onUpdate?.(this,e);let t=typeof performance<"u"?performance.now():Date.now();if(!this.isPointerDown&&this.autoRotate&&t-this.lastInteractionTime>=this.autoRotateDelay){this.turntableRotation+=this.autoRotateSpeed*e,this.updateTurntableMatrix();for(let o of Y)o.onCameraChange?.(this,this.container)}this.currTheta+=(this.targetTheta-this.currTheta)*.12,this.currPhi+=(this.targetPhi-this.currPhi)*.12,this.currDistance+=(this.targetDistance-this.currDistance)*.12,this.syncCameraMatrices();let s=Math.round((this.currTheta*180/Math.PI%360+360)%360),i=Math.round(this.currPhi*180/Math.PI),r=Math.round(this.bounds.radius*2.5/this.currDistance*100)/100;if(Math.abs(s-this.lastReportedTheta)>=1||Math.abs(i-this.lastReportedPhi)>=1||Math.abs(r-this.lastReportedZoom)>=.02){this.lastReportedTheta=s,this.lastReportedPhi=i,this.lastReportedZoom=r;let o={theta:s,phi:i,zoom:r,distance:Math.round(this.currDistance*100)/100};this.opts.onCameraChange&&this.opts.onCameraChange(o),typeof CustomEvent<"u"&&typeof this.container.dispatchEvent=="function"&&this.container.dispatchEvent(new CustomEvent("cameraChange",{bubbles:!0,detail:o}))}}renderXRView(e,t,s,i,r=!0,o=0){this.onBeforeRenderXRView&&this.onBeforeRenderXRView(e);let c=e?.transform?.inverse?.matrix||this.viewMatrix,a=e?.projectionMatrix||this.projMatrix,l=e?.transform?.position,u=l?[l.x,l.y,l.z]:[0,1.6,0];t?this.modelRootMatrix.set(t):s&&t&&this.modelRootMatrix.set(t),this.render({viewMatrix:c,projMatrix:a,eyePos:u,viewport:i,eye:e?.eye||"none",clear:o>0?!1:e?.eye!=="right",isAR:r})}render(e){if(this.contextLost)return;if(!e&&this.stereoSBS){let y=Math.max(2,this.canvas.width||2),M=Math.max(1,this.canvas.height||1),f=Math.max(1,Math.floor(y*.5)),T=Math.max(1,y-f),S=this.backdropMeshes.length>0?Math.max(this.bounds.radius*20,120):Math.max(this.bounds.radius*20,10),L=this.customTarget||this.bounds.center,[k,U,F]=he(this),h=Math.cos(this.currTheta),P=-Math.sin(this.currTheta),w=.032,v=X(),B=X(),D=[k-h*w,U,F-P*w],$=[L[0]-h*w*.25,L[1],L[2]-P*w*.25];pe(v,this.fov,f/M,.1,S),be(B,D,$,[0,1,0]);let A=X(),W=X(),_=[k+h*w,U,F+P*w],K=[L[0]+h*w*.25,L[1],L[2]+P*w*.25];pe(A,this.fov,T/M,.1,S),be(W,_,K,[0,1,0]),this.render({viewMatrix:B,projMatrix:v,eyePos:D,viewport:[0,0,f,M],eye:"left",clear:!0,isAR:!1}),this.render({viewMatrix:W,projMatrix:A,eyePos:_,viewport:[f,0,T,M],eye:"right",clear:!1,isAR:!1}),this.gl.viewport(0,0,y,M);return}let t=this.gl,s=0,i=0,r=this.canvas.width,o=this.canvas.height;e?.viewport?(s=e.viewport[0],i=e.viewport[1],r=e.viewport[2],o=e.viewport[3],t.viewport(s,i,r,o)):t.viewport(0,0,this.canvas.width,this.canvas.height),t.enable(2929),(!e||e.clear!==!1)&&(e?.isAR?t.clearColor(0,0,0,0):t.clearColor(this.clearColor[0],this.clearColor[1],this.clearColor[2],this.clearColor[3]),t.clear(16384|256));let c=X(),a=e?.viewMatrix??this.viewMatrix;this.activeViewMatrix=a,J(c,e?.projMatrix??this.projMatrix,a);let l=ue().xrDepthShader,u=Ye(this.toneMapping),[p,m,g]=e?e.eyePos:he(this);for(let y of Y)y.onBeforeDrawScene?.(this,t,c,[p,m,g],u);let d=e?.isAR?0:this.backdropMeshes.length;if(this.meshes.length===0&&d===0)return;let E=e?.eye||"none",b=!1,x=d>0?[...this.backdropMeshes,...this.meshes]:this.meshes,R=y=>{for(let M=0;M<x.length;M++){let f=x[M];if(f.visible===!1)continue;let T=this.selectedMeshName!==""&&f.name===this.selectedMeshName;M<d||f.worldSpace===!0||f.name==="__xr_reticle__"?this.modelMatrix.set(f.transform):J(this.modelMatrix,this.modelRootMatrix,f.transform),J(this.mvpMatrix,c,this.modelMatrix),ae(f.normalMatrix,this.modelMatrix);let S=f.extras?.xr?.stereo||f.userData?.xr?.stereo,L=0,k=0,U=1,F=1;S==="sbs"?(U=.5,L=E==="right"?.5:0):S==="ou"&&(F=.5,k=E==="right"?.5:0);for(let h of f.primitives){let P=!!h.isTransmissive;if(!y&&P){b=!0;continue}if(y&&!P)continue;let w=h.customProgram||this.renderProg,v=h.customUniforms||this.uRender;t.useProgram(w),t.uniform1i(v.uToneMapping,u),t.uniform1f(v.uEnvIntensity,this.envIntensity),v.uExposure&&t.uniform1f(v.uExposure,this.exposure),t.uniform3f(v.uCameraPos,p,m,g),t.uniform1f(v.uEmissive,T?1:0),t.uniformMatrix4fv(v.uMVP,!1,this.mvpMatrix),t.uniformMatrix4fv(v.uModel,!1,this.modelMatrix),t.uniformMatrix3fv(v.uNormalMatrix,!1,f.normalMatrix),v.uUvTransform&&t.uniform4f(v.uUvTransform,L,k,U,F),v.uViewportSize&&t.uniform2f(v.uViewportSize,Math.max(1,r),Math.max(1,o)),l?.bind(t,v,this.xrDepthState,a,[s,i,Math.max(1,r),Math.max(1,o)]),this.environmentTexture?(t.activeTexture(33988),t.bindTexture(3553,this.environmentTexture),t.uniform1i(v.uEnvironmentMap,4),t.uniform1i(v.uHasEnvironmentMap,1)):t.uniform1i(v.uHasEnvironmentMap,0),this.bindLights(v),v.uSectionEnabled&&t.uniform1i(v.uSectionEnabled,0);for(let D of Y)D.onBeforeDrawPrimitive?.(t,w,h,f,this.worldMatrices||[],this.gltf);t.uniform4fv(v.uBaseColor,h.baseColor),t.uniform1f(v.uMetallic,h.metallic),t.uniform1f(v.uRoughness,h.roughness),v.uClearcoat&&t.uniform2f(v.uClearcoat,h.clearcoat??0,h.clearcoatRoughness??.04),h.texture?(t.activeTexture(33984),t.bindTexture(3553,h.texture),t.uniform1i(v.uBaseColorTexture,0),t.uniform1i(v.uHasTexture,1)):t.uniform1i(v.uHasTexture,0),h.metallicRoughnessTexture?(t.activeTexture(33985),t.bindTexture(3553,h.metallicRoughnessTexture),t.uniform1i(v.uMetallicRoughnessTexture,1),t.uniform1i(v.uHasMetallicRoughnessTexture,1)):t.uniform1i(v.uHasMetallicRoughnessTexture,0),h.normalTexture?(t.activeTexture(33986),t.bindTexture(3553,h.normalTexture),t.uniform1i(v.uNormalTexture,2),t.uniform1i(v.uHasNormalTexture,1),t.uniform1f(v.uNormalScale,h.normalScale)):t.uniform1i(v.uHasNormalTexture,0),h.occlusionTexture?(t.activeTexture(33987),t.bindTexture(3553,h.occlusionTexture),t.uniform1i(v.uOcclusionTexture,3),t.uniform1i(v.uHasOcclusionTexture,1),t.uniform1f(v.uOcclusionStrength,h.occlusionStrength)):t.uniform1i(v.uHasOcclusionTexture,0),t.bindVertexArray(h.vao);let B=f.instanceMatrices;if(B&&B.length>=16&&h.instanced&&v.uInstanced&&v.uViewProj){let D=B.length>>4;t.uniform1i(v.uInstanced,1),t.uniformMatrix4fv(v.uViewProj,!1,c),h.indices?t.drawElementsInstanced(4,h.indexCount,h.indexType,0,D):t.drawArraysInstanced(4,0,h.indexCount,D),t.uniform1i(v.uInstanced,0)}else if(B&&B.length>=16){let D=B.length>>4,$=f.instanceColors,A=X(),W=new Float32Array(9),_=[h.baseColor[0],h.baseColor[1],h.baseColor[2],h.baseColor[3]];for(let K=0;K<D;K++)J(A,this.modelMatrix,B.subarray(K*16,K*16+16)),J(this.mvpMatrix,c,A),ae(W,A),t.uniformMatrix4fv(v.uMVP,!1,this.mvpMatrix),t.uniformMatrix4fv(v.uModel,!1,A),t.uniformMatrix3fv(v.uNormalMatrix,!1,W),$&&(_[0]=h.baseColor[0]*$[K*3],_[1]=h.baseColor[1]*$[K*3+1],_[2]=h.baseColor[2]*$[K*3+2],_[3]=h.baseColor[3],t.uniform4fv(v.uBaseColor,_)),ve(t,h)}else ve(t,h)}}};if(R(!1),b){for(let y of Y)y.onBeforeTransmissivePass?.(t,this);R(!0)}for(let y of Y)y.onAfterRender?.(t,this,c)}disposeMeshListGpuResources(e,t){ze(this.gl,e,t)}disposeMeshGpuResources(){this.disposeMeshListGpuResources(this.meshes,this.gpuTextures),this.meshes=[],this.gpuTextures=[],this.availableVariants=[]}disposeGpuResources(){let e=this.gl;this.disposeMeshGpuResources(),this.disposeMeshListGpuResources(this.backdropMeshes),this.backdropMeshes=[];for(let t of Y)t.onDestroy?.(e,this);this.environmentTexture&&(e.deleteTexture(this.environmentTexture),this.environmentTexture=null),this.renderProg&&e.deleteProgram(this.renderProg),this.pickProg&&e.deleteProgram(this.pickProg),this.pickFbo&&e.deleteFramebuffer(this.pickFbo),this.pickTexture&&e.deleteTexture(this.pickTexture),this.pickDepth&&e.deleteRenderbuffer(this.pickDepth),this.pickFbo=null,this.pickTexture=null,this.pickDepth=null}dispose(){this.stop(),this.teardownEvents(),this.disposeGpuResources()}destroy(){this.dispose()}};me.registerPlugin=Ie,me.getRegisteredPlugins=Fe;var dt=me;export{me as MicroViewer,mt as SANDBOX_HOST_SCENE_LIMITS,me as ThreeView,$e as decodeUtf8,dt as default,ht as evaluateToneMapping,le as getDecoder,Fe as getRegisteredPlugins,qe as isGLBData,ae as mat3NormalFromMat4,X as mat4Create,Me as mat4FromRotationTranslationScale,De as mat4Invert,be as mat4LookAt,J as mat4Multiply,pe as mat4Perspective,ge as normalizeToneMapping,Ve as parseGLB,Le as rayIntersectsTriangle,lt as readFloat32Array,Ke as registerDecoder,Ie as registerPlugin,Ce as resolveNodeWorldMatrices,et as sanitizeTextureUri,Se as toGLBData,Ye as toneMappingShaderIndex,Te as unprojectPoint};
|
|
252
|
+
void main(){fragColor=uColorId;}`;function Pe(n,e,t){let s={};for(let i of t)s[i]=n.getUniformLocation(e,i);return s}var Ne=["uMVP","uModel","uInstanced","uViewProj","uNormalMatrix","uUvTransform","uBaseColor","uMetallic","uRoughness","uClearcoat","uEmissive","uCameraPos","uBaseColorTexture","uHasTexture","uMetallicRoughnessTexture","uHasMetallicRoughnessTexture","uNormalTexture","uHasNormalTexture","uNormalScale","uOcclusionTexture","uHasOcclusionTexture","uOcclusionStrength","uToneMapping","uEnvironmentMap","uHasEnvironmentMap","uAmbientColor","uNumDirLights","uDirLightDir","uDirLightColor","uNumPointLights","uPointLightPos","uPointLightColor","uEnvIntensity","uExposure","uViewportSize","uSectionPlane","uSectionEnabled"];function Ue(){let n=ue().xrDepthShader;return{fs:qe.replace("/*XRD_U*/",n?.uniformsGLSL||"").replace("/*XRD_O*/",n?.occlusionGLSL||""),uniforms:n?[...Ne,...n.uniformNames]:Ne}}function Qe(n){if(typeof n!="string")return null;let e=n.trim();if(!e||/[\x00-\x1f\x7f]/.test(e))return null;let t=e.toLowerCase();return t.startsWith("data:")?/^data:image\/(png|jpeg|jpg|webp|ktx2|avif)(;|,)/i.test(e)?e:null:t.startsWith("https://")||t.startsWith("blob:")?e:t.startsWith("//")||/^[a-z][a-z0-9+.-]*:/i.test(e)||/(^|[\\/])\.\.([\\/]|$)/.test(e)?null:e}function we(n,e,t,s,i,r,o,c){if(s===void 0||!e.accessors?.[s])return null;let a=Me(e,t,e.accessors[s],r);if(!a)return null;c&&(a=a.slice(),Ae(a,c));let l=n.createBuffer();return o.push(l),n.bindBuffer(34962,l),n.bufferData(34962,a,35044),n.enableVertexAttribArray(i),n.vertexAttribPointer(i,r,5126,!1,0,0),a}function Ee(n){let e=Number(n);return Number.isFinite(e)?Math.max(0,e):1}function We(n,e,t){let s=Ue(),i=Ve,r=s.fs,o=!1;for(let a of Y)if(a.onResolveShader){let l=a.onResolveShader(e,t,i,r);l&&(i=l.vs,r=l.fs,o=!0)}let c=o?pe(n,i,r):null;return{program:c,uniforms:c?Pe(n,c,s.uniforms):null}}function et(n,e,t){let s=e.instanceMatrices;if(!s||s.length<16||typeof n.vertexAttribDivisor!="function"||typeof n.drawElementsInstanced!="function")return!1;let i=s.length>>4,r=e.instanceColors,o=new Float32Array(i*20);for(let a=0;a<i;a++)o.set(s.subarray(a*16,a*16+16),a*20),o[a*20+16]=r?r[a*3]:1,o[a*20+17]=r?r[a*3+1]:1,o[a*20+18]=r?r[a*3+2]:1,o[a*20+19]=1;let c=n.createBuffer();t.push(c),n.bindBuffer(34962,c),n.bufferData(34962,o,35044);for(let a=0;a<5;a++)n.enableVertexAttribArray(8+a),n.vertexAttribPointer(8+a,4,5126,!1,80,a*16),n.vertexAttribDivisor(8+a,1);return!0}function tt(n,e){let t=n.materials?.[e?.material];if(!t)return null;let s=[t.pbrMetallicRoughness?.baseColorTexture,t.pbrMetallicRoughness?.metallicRoughnessTexture,t.normalTexture,t.occlusionTexture,t.emissiveTexture];for(let i of s){let r=i?.extensions?.KHR_texture_transform;if(r&&typeof r=="object")return r}return null}var ft=Object.freeze({maxMeshes:1e3,maxTextures:64,maxVertices:1e6,maxIndices:3e6}),He=Object.freeze({maxMeshes:1/0,maxTextures:1/0,maxVertices:1/0,maxIndices:1/0});function ge(n,e){e.indices?n.drawElements(4,e.indexCount,e.indexType,0):n.drawArrays(4,0,e.indexCount)}function Oe(n,e){let t=n?.pbrMetallicRoughness,s=t?.baseColorFactor,i=n?.extensions?.KHR_materials_clearcoat,r=o=>o!==void 0&&e[o.index]?e[o.index]:null;return{baseColor:s?[s[0],s[1],s[2],s[3]??1]:[.8,.8,.8,1],metallic:t?.metallicFactor??.1,roughness:t?.roughnessFactor??.5,clearcoat:i?i.clearcoatFactor??1:0,clearcoatRoughness:Math.max(.04,i?.clearcoatRoughnessFactor??.04),texture:r(t?.baseColorTexture),metallicRoughnessTexture:r(t?.metallicRoughnessTexture),normalTexture:r(n?.normalTexture),normalScale:n?.normalTexture?.scale??1,occlusionTexture:r(n?.occlusionTexture),occlusionStrength:n?.occlusionTexture?.strength??1,isTransmissive:(n?.extensions?.KHR_materials_transmission?.transmissionFactor??0)>0}}function Xe(n,e,t){let s=new Set,i=r=>{r&&!s.has(r)&&(s.add(r),n.deleteTexture(r))};for(let r of e)for(let o of r.primitives){if(o.vao&&n.deleteVertexArray(o.vao),o.buffers)for(let a of o.buffers)n.deleteBuffer(a);i(o.texture),i(o.metallicRoughnessTexture),i(o.normalTexture),i(o.occlusionTexture);let c=new Set;o.customProgram&&c.add(o.customProgram);for(let a of o.programCache?.values()??[])a.program&&c.add(a.program);for(let a of c)n.deleteProgram(a)}if(t)for(let r of t)i(r)}function Be(n,e,t=He,s){let i={meshes:[],mesh:null,gpuTextures:[],vao:null,buffers:[]};try{return nt(n,e,t,s,i)}catch(r){let o=i.mesh&&!i.meshes.includes(i.mesh)?[...i.meshes,i.mesh]:i.meshes;Xe(n,o,i.gpuTextures),i.vao&&n.deleteVertexArray(i.vao);for(let c of i.buffers)n.deleteBuffer(c);throw r}}function nt(n,e,t,s,i){if(!e||!e.gltf)throw new Error("Invalid GLBData passed to buildGpuScene: missing gltf object.");let{gltf:r}=e,o=le("meshopt");o?.decodeBufferViews&&o.decodeBufferViews(r,e);let c=e.bin,a=[];i.meshes=a;let l=[],u=r.extensions?.KHR_materials_variants;u&&Array.isArray(u.variants)&&u.variants.forEach((h,P)=>{l.push(typeof h?.name=="string"&&h.name?h.name:`Variant_${P}`)});let d=Ce(r),m=[];if(Array.isArray(r.nodes))for(let h=0;h<r.nodes.length;h++){let P=r.nodes[h];P.mesh!==void 0&&r.meshes&&r.meshes[P.mesh]&&m.push({node:P,nodeIdx:h,meshIdx:P.mesh})}let y=m.length>0?m:(r.meshes||[]).map((h,P)=>({node:h,nodeIdx:-1,meshIdx:P})),p=[];if(Array.isArray(e.lights)&&e.lights.length>0)p.push(...e.lights);else if(r.extensions?.KHR_lights_punctual?.lights){let h=r.extensions.KHR_lights_punctual.lights;for(let P of h){let E=P.type==="directional"?"directional":P.type==="point"?"point":"ambient";p.push({type:E,color:P.color||[1,1,1],intensity:P.intensity!==void 0?P.intensity:1})}}if(p.length===0&&(p.push({type:"ambient",color:[1,1,1],intensity:.35}),p.push({type:"directional",color:[1,1,1],intensity:.85,direction:[.5,1,.8]})),y.length>t.maxMeshes)throw new Error(`SecurityError: GLB scene exceeds host mesh limit (${t.maxMeshes}). Found: ${y.length}`);let b=[];if(i.gpuTextures=b,r.textures&&r.images){if(r.textures.length>t.maxTextures)throw new Error(`SecurityError: GLB scene exceeds host texture limit (${t.maxTextures}). Found: ${r.textures.length}`);for(let h=0;h<r.textures.length;h++){let P=r.textures[h],E=r.images[P.source];if(E){let v=n.createTexture();if(n.bindTexture(3553,v),n.texImage2D(3553,0,6408,1,1,0,6408,5121,new Uint8Array([255,255,255,255])),n.texParameteri(3553,10242,10497),n.texParameteri(3553,10243,10497),n.texParameteri(3553,10241,9729),n.texParameteri(3553,10240,9729),E.bufferView!==void 0){let B=r.bufferViews[E.bufferView],D=c.slice(B.byteOffset||0,(B.byteOffset||0)+B.byteLength),$=new Blob([D],{type:E.mimeType||"image/png"});typeof createImageBitmap=="function"&&createImageBitmap($).then(A=>{n.bindTexture(3553,v),n.texImage2D(3553,0,6408,6408,5121,A),n.generateMipmap(3553),s?.()}).catch(()=>{})}else if(E.uri){let B=Qe(E.uri);if(B){let D=new Image;D.crossOrigin="anonymous",D.referrerPolicy="no-referrer",D.onload=()=>{n.bindTexture(3553,v),n.texImage2D(3553,0,6408,6408,5121,D),n.generateMipmap(3553),s?.()},D.src=B}else console.warn("[Three-Live Security] Blocked unsafe glTF image URI:",E.uri)}b.push(v)}else b.push(null)}}let x=[1/0,1/0,1/0],g=[-1/0,-1/0,-1/0],w=0,M=0;for(let h=0;h<y.length;h++){let P=y[h],E=r.meshes[P.meshIdx],v=P.nodeIdx>=0?d[P.nodeIdx]:X(),B=new Float32Array(9);ae(B,v);let D={name:P.node.name||E.name||`Mesh_${P.meshIdx}`,nodeIndex:P.nodeIdx,transform:v,normalMatrix:B,primitives:[],extras:P.node.extras||E.extras};i.mesh=D;let $=P.node?.extensions?.EXT_mesh_gpu_instancing;if($&&$.attributes&&typeof $.attributes=="object"){let A=$.attributes,W=A.TRANSLATION!==void 0?r.accessors?.[A.TRANSLATION]:null,_=A.ROTATION!==void 0?r.accessors?.[A.ROTATION]:null,K=A.SCALE!==void 0?r.accessors?.[A.SCALE]:null,q=A._COLOR_0!==void 0?r.accessors?.[A._COLOR_0]:null,G=Number(W?.count??_?.count??K?.count??0);if(!Number.isFinite(G)||G<0||G>1e4)throw new Error(`SecurityError: EXT_mesh_gpu_instancing exceeds maximum instance limit (10000). Found: ${G}`);if(G>0){let z=(N,O)=>{if(!N||N.bufferView===void 0||!r.bufferViews?.[N.bufferView])return null;let te=N.componentType??5126,re=j=>(console.warn(`[three-live] EXT_mesh_gpu_instancing: skipping accessor (${j})`),null);if(te!==5120&&te!==5121&&te!==5122&&te!==5123&&te!==5126)return re(`componentType ${te}`);if(!(Number(N.count)>=G))return re(`count ${N.count} < ${G}`);let C=null;try{C=Me(r,c,{...N,count:G},O)}catch{return re("out of bounds")}return!C||!C.every(Number.isFinite)?re("non-finite value"):C},I=z(W,3),ne=z(_,4),H=z(K,3),Q=q?.type==="VEC4"?4:3,ee=z(q,Q),ie=new Float32Array(G*16),oe=X();for(let N=0;N<G;N++){let O=I?[I[N*3],I[N*3+1],I[N*3+2]]:[0,0,0],te=ne?[ne[N*4],ne[N*4+1],ne[N*4+2],ne[N*4+3]]:[0,0,0,1],re=H?[H[N*3],H[N*3+1],H[N*3+2]]:[1,1,1];Te(oe,te,O,re),ie.set(oe,N*16)}if(D.instanceMatrices=ie,ee){let N=new Float32Array(G*3);for(let O=0;O<G;O++)N[O*3]=ee[O*Q],N[O*3+1]=ee[O*Q+1],N[O*3+2]=ee[O*Q+2];D.instanceColors=N}}}for(let A of E.primitives||[]){if(A.extensions?.KHR_draco_mesh_compression){let C=le("draco");C&&C.decodePrimitive(r,A,c)}let W=[],_=n.createVertexArray();i.vao=_,i.buffers=W,n.bindVertexArray(_);let K=et(n,D,W),q=r.accessors[A.attributes.POSITION],G=Number(q?.count??0);if(!Number.isFinite(G)||G<0||w+G>t.maxVertices)throw new Error(`SecurityError: GLB scene exceeds host vertex limit (${t.maxVertices}).`);if(w+=G,q?.min&&q?.max){let C=j=>{for(let V=0;V<3;V++){let Z=j[12+V],se=j[12+V];for(let ce=0;ce<3;ce++){let fe=j[ce*4+V]*q.min[ce],de=j[ce*4+V]*q.max[ce];Z+=fe<de?fe:de,se+=fe>de?fe:de}Z<x[V]&&(x[V]=Z),se>g[V]&&(g[V]=se)}};if(D.instanceMatrices&&D.instanceMatrices.length>=16){let j=D.instanceMatrices.length>>4,V=X();for(let Z=0;Z<j;Z++){let se=D.instanceMatrices.subarray(Z*16,Z*16+16);J(V,v,se),C(V)}}else C(v)}let z=we(n,r,c,A.attributes.POSITION,0,3,W);we(n,r,c,A.attributes.NORMAL,1,3,W),we(n,r,c,A.attributes.TEXCOORD_0,2,2,W,tt(r,A));let I=0,ne=5123,H=null;if(A.indices!==void 0){let C=r.accessors[A.indices],V=(r.bufferViews[C.bufferView].byteOffset||0)+(C.byteOffset||0);if(I=Number(C.count??0),!Number.isFinite(I)||I<0||M+I>t.maxIndices)throw new Error(`SecurityError: GLB scene exceeds host index limit (${t.maxIndices}).`);M+=I,ne=C.componentType===5125?5125||5125:5123||5123;let Z=n.createBuffer();W.push(Z),n.bindBuffer(34963,Z),C.componentType===5125?(H=new Uint32Array(c,V,C.count),n.bufferData(34963,H,35044)):C.componentType===5121?(H=Uint16Array.from(new Uint8Array(c,V,C.count)),n.bufferData(34963,H,35044)):(H=V%2===0?new Uint16Array(c,V,C.count):new Uint16Array(c.slice(V,V+C.count*2)),n.bufferData(34963,H,35044))}else I=q.count;let Q={};for(let C of Y)if(C.onBuildPrimitiveVAO){let j=C.onBuildPrimitiveVAO(n,_,A,r,c);j&&(Q[C.name]=j)}n.bindVertexArray(null);let ee=r.materials&&A.material!==void 0?r.materials[A.material]:null,{program:ie,uniforms:oe}=We(n,A,ee),N=Oe(ee,b),O=!!(ee?.extensions?.KHR_materials_transmission||Q.materials?.isTransmissive),te=A.extensions?.KHR_materials_variants?.mappings,re=Array.isArray(te)?te.filter(C=>C&&typeof C.material=="number"&&Array.isArray(C.variants)).map(C=>({material:C.material|0,variants:C.variants.map(j=>Number(j)|0)})):void 0;D.primitives.push({vao:_,instanced:K,indexCount:I,indexType:ne,...N,positions:z,indices:H,buffers:W,materialIndex:A.material,defaultMaterialIndex:A.material,variantMappings:re,primitiveDef:A,programCache:new Map([[A.material??-1,{program:ie,uniforms:oe}]]),pluginData:Q,customProgram:ie,customUniforms:oe,isTransmissive:O})}a.push(D)}isFinite(x[0])||(x=[-1,-1,-1]),isFinite(g[0])||(g=[1,1,1]);let T=[(x[0]+g[0])/2,(x[1]+g[1])/2,(x[2]+g[2])/2],f=g[0]-x[0],R=g[1]-x[1],S=g[2]-x[2],L=Math.max(Math.hypot(f,R,S)/2,.5),k=[x[0],x[1],x[2]],U=[g[0],g[1],g[2]],F=e.animations;if(!F){for(let h of Y)if(h.onParseGLB?.(e),e.animations){F=e.animations;break}}return F||(F=[]),{meshes:a,bounds:{center:T,radius:L,min:k,max:U},lights:p,animations:F,worldMatrices:d,gpuTextures:b,availableVariants:l}}function he(n){let e=n.customTarget||n.bounds.center,t=n.currDistance*Math.cos(n.currPhi);return[e[0]+t*Math.sin(n.currTheta),e[1]+n.currDistance*Math.sin(n.currPhi),e[2]+t*Math.cos(n.currTheta)]}var me=class{constructor(e,t,s){this.supportsInstanceOffsets=!0;this.meshes=[];this.backdropMeshes=[];this.bounds={center:[0,0,0],radius:1,min:[-1,-1,-1],max:[1,1,1]};this.lights=[];this.modelRootMatrix=X();this.clearColor=[0,0,0,0];this.envIntensity=1;this.exposure=1;this.shadowIntensity=0;this.shadowSoftness=.5;this.toneMapping="neutral";this.environmentTexture=null;this.animations=[];this.currentClip=null;this.animationTime=0;this.isPlayingAnimation=!1;this.autoplay=!1;this.animationName="";this.gpuTextures=[];this.availableVariants=[];this.activeVariantName=null;this.gltf=null;this.worldMatrices=[];this.initialNodeTransforms=[];this.projMatrix=X();this.viewMatrix=X();this.activeViewMatrix=null;this.modelMatrix=X();this.mvpMatrix=X();this.normalMatrix=new Float32Array(9);this.initialTheta=.6;this.initialPhi=.35;this.initialDistance=null;this.customDistance=!1;this.distanceMode="auto";this.initialCustomTarget=null;this.customTarget=null;this.fov=Math.PI/4;this.minTheta=-1/0;this.maxTheta=1/0;this.minPhi=-Math.PI*.48;this.maxPhi=Math.PI*.48;this.minDistanceLimit=null;this.maxDistanceLimit=null;this.activePointers=new Map;this.isPanning=!1;this.targetTheta=.6;this.currTheta=.6;this.targetPhi=.35;this.currPhi=.35;this.targetDistance=3;this.currDistance=3;this.autoRotate=!1;this.autoRotateSpeed=Math.PI/6;this.autoRotateDelay=3e3;this.lastInteractionTime=-1/0;this.turntableRotation=0;this.isPointerDown=!1;this.interactive=!0;this.skyboxTexture=null;this.lastPointerX=0;this.lastPointerY=0;this.totalDragDist=0;this.selectedMeshName="";this.pickFbo=null;this.pickTexture=null;this.pickDepth=null;this.pickFboWidth=0;this.pickFboHeight=0;this.animFrameId=null;this.isVisible=!0;this.xrSessionActive=!1;this.lastTime=0;this.lastReportedTheta=-999;this.lastReportedPhi=-999;this.lastReportedZoom=-999;this.boundPointerDown=null;this.boundPointerMove=null;this.boundPointerUp=null;this.boundWheel=null;this.boundKeyDown=null;this.boundContextMenu=null;this.boundSetCamera=null;this.boundResetCamera=null;this.contextLost=!1;this.lastGlbData=null;this.boundContextLost=null;this.boundContextRestored=null;this.stereoSBS=!1;this.renderErrorReported=!1;this.xrDepthState={enabled:!1,texture:null,uvTransform:X(),rawValueToMeters:.001,format:0};this.container=e;let i,r;if(t&&typeof t.getContext=="function"?(i=t,r=s):(r=t,i=r?.canvas),!i){let a=e.querySelector("canvas");a||(a=document.createElement("canvas"),a.style.width="100%",a.style.height="100%",a.style.display="block",a.style.touchAction="none",e.prepend(a)),i=a}this.canvas=i,this.opts=r||{},this.opts.clearColor&&(this.clearColor=this.opts.clearColor);let o=typeof window<"u"&&typeof window.matchMedia=="function"&&window.matchMedia("(prefers-reduced-motion: reduce)").matches;this.opts.autoRotate!==void 0?this.autoRotate=this.opts.autoRotate&&!o:o&&(this.autoRotate=!1),this.opts.autoRotateSpeed!==void 0&&(this.autoRotateSpeed=this.opts.autoRotateSpeed),this.opts.autoRotateDelay!==void 0&&(this.autoRotateDelay=Math.max(0,Number(this.opts.autoRotateDelay)||0)),this.opts.exposure!==void 0&&(this.exposure=Ee(this.opts.exposure)),this.opts.camera&&this.setInitialCamera(this.opts.camera.theta,this.opts.camera.phi,this.opts.camera.distance,this.opts.camera.zoom),this.opts.shadowIntensity!==void 0&&(this.shadowIntensity=Math.max(0,Math.min(1,Number(this.opts.shadowIntensity)||0))),this.opts.shadowSoftness!==void 0&&(this.shadowSoftness=Math.max(0,Math.min(1,Number(this.opts.shadowSoftness)||0))),this.opts.toneMapping!==void 0&&(this.toneMapping=ye(this.opts.toneMapping)),this.opts.autoplay!==void 0&&(this.autoplay=this.opts.autoplay),this.opts.animationName!==void 0&&(this.animationName=this.opts.animationName),this.opts.variantName!==void 0&&(this.activeVariantName=this.opts.variantName||null),this.opts.fieldOfView!==void 0&&(this.fov=this.opts.fieldOfView*Math.PI/180),this.opts.distanceMode!==void 0&&(this.distanceMode=this.opts.distanceMode),this.opts.cameraTarget!==void 0&&(this.setTarget(this.opts.cameraTarget[0],this.opts.cameraTarget[1],this.opts.cameraTarget[2]),this.initialCustomTarget=[...this.opts.cameraTarget]),(this.opts.minCameraOrbit||this.opts.maxCameraOrbit)&&this.setOrbitLimits({minTheta:this.opts.minCameraOrbit?.theta,maxTheta:this.opts.maxCameraOrbit?.theta,minPhi:this.opts.minCameraOrbit?.phi,maxPhi:this.opts.maxCameraOrbit?.phi,minDistance:this.opts.minCameraOrbit?.distance,maxDistance:this.opts.maxCameraOrbit?.distance});let c=i.getContext("webgl2",{antialias:!0,alpha:!0,xrCompatible:!0});if(!c)throw new Error("WebGL2 not supported");this.gl=c,this.initGlState(),this.setupEvents(),this.setupContextLossHandling(),this.opts.environmentImage&&this.setEnvironmentImage(this.opts.environmentImage).catch(()=>{}),typeof requestAnimationFrame<"u"&&this.opts.autoStart!==!1&&this.start()}static registerDecoder(e,t){$e(e,t)}static getDecoder(e){return le(e)}setupContextLossHandling(){let e=this.canvas;typeof e?.addEventListener=="function"&&(this.boundContextLost=t=>{t.preventDefault?.(),this.contextLost=!0,this.stop()},this.boundContextRestored=()=>this.restoreContext(),e.addEventListener("webglcontextlost",this.boundContextLost),e.addEventListener("webglcontextrestored",this.boundContextRestored))}initGlState(){let e=this.gl,t=Ue();this.renderProg=pe(e,Ve,t.fs),this.pickProg=pe(e,Ze,Je),this.uRender=Pe(e,this.renderProg,t.uniforms),this.uPick=Pe(e,this.pickProg,["uMVP","uColorId"]),this.setupPickingFbo()}restoreContext(){let e=this.gl;this.contextLost=!1,this.initGlState();for(let t of Y)t.onContextRestored?.(e,this);this.meshes=[],this.gpuTextures=[],this.environmentTexture=null,this.lastGlbData&&this.loadGLBData(this.lastGlbData,this.lastLimits),this.opts.environmentImage&&this.setEnvironmentImage(this.opts.environmentImage).catch(()=>{}),typeof requestAnimationFrame<"u"&&this.opts.autoStart!==!1&&this.start()}setupPickingFbo(){let e=this.gl;if(typeof e.createFramebuffer!="function")return;this.pickFbo=e.createFramebuffer(),this.pickTexture=e.createTexture(),this.pickDepth=e.createRenderbuffer();let t=Math.max(1,this.canvas.width),s=Math.max(1,this.canvas.height);this.pickFboWidth=t,this.pickFboHeight=s,e.bindTexture(3553,this.pickTexture),e.texImage2D(3553,0,6408,t,s,0,6408,5121,null),e.texParameteri(3553,10241,9728),e.texParameteri(3553,10240,9728),e.bindRenderbuffer(36161,this.pickDepth),e.renderbufferStorage(36161,33189,t,s),e.bindFramebuffer(36160,this.pickFbo),e.framebufferTexture2D(36160,36064,3553,this.pickTexture,0),e.framebufferRenderbuffer(36160,36096,36161,this.pickDepth),e.bindFramebuffer(36160,null)}resizePickingFbo(e,t){if(!this.pickFbo)return;let s=this.gl,i=Math.max(1,e),r=Math.max(1,t);this.pickFboWidth=i,this.pickFboHeight=r,s.bindTexture(3553,this.pickTexture),s.texImage2D(3553,0,6408,i,r,0,6408,5121,null),s.bindRenderbuffer(36161,this.pickDepth),s.renderbufferStorage(36161,33189,i,r)}loadGLBData(e,t){this.lastGlbData=e,this.lastLimits=t;let{meshes:s,bounds:i,lights:r,animations:o,worldMatrices:c,gpuTextures:a,availableVariants:l}=Be(this.gl,e,t,()=>this.render());if(this.disposeMeshGpuResources(),this.meshes=s,this.bounds=i,this.turntableRotation=0,this.updateTurntableMatrix(),this.lights=r,this.animations=o||[],this.worldMatrices=c||[],this.gpuTextures=a||[],this.availableVariants=l||[],this.gltf=e.gltf,this.activeVariantName&&!this.applyVariantMaterial(this.activeVariantName)&&(this.activeVariantName=null),this.initialNodeTransforms=[],Array.isArray(e.gltf?.nodes))for(let m of e.gltf.nodes)this.initialNodeTransforms.push({t:m.translation?[m.translation[0],m.translation[1],m.translation[2]]:[0,0,0],r:m.rotation?[m.rotation[0],m.rotation[1],m.rotation[2],m.rotation[3]]:[0,0,0,1],s:m.scale?[m.scale[0],m.scale[1],m.scale[2]]:[1,1,1]});this.autoplay&&this.animations.length>0&&this.playAnimation(this.animationName||void 0);for(let m of Y)m.onLoadModel?.(this,this.gltf);this.customDistance||(this.initialDistance=this.distanceMode==="absolute"?3:i.radius*2.5,this.targetDistance=this.initialDistance,this.currDistance=this.initialDistance);let u=e.exposure??e.gltf?.extras?.exposure??e.gltf?.asset?.extras?.exposure;u!==void 0&&(this.exposure=Ee(u));let d=e.environmentIntensity??e.gltf?.extras?.environmentIntensity??e.gltf?.asset?.extras?.environmentIntensity;d!==void 0&&(this.envIntensity=Math.max(0,Number(d)||0)),this.container&&typeof this.container.hasAttribute=="function"&&this.container.hasAttribute("poster")&&this.container.removeAttribute("poster"),this.resize(),this.update(0),this.render(),this.animFrameId===null&&typeof requestAnimationFrame<"u"&&this.opts.autoStart!==!1&&this.start()}loadGLB(e){this.loadGLBData(_e(e))}clearScene(){this.disposeMeshGpuResources(),this.lights=[],this.animations=[],this.currentClip=null,this.isPlayingAnimation=!1,this.worldMatrices=[],this.gltf=null,this.initialNodeTransforms=[],this.turntableRotation=0,this.updateTurntableMatrix(),this.gl&&this.render()}loadModel(e){this.loadGLBData(Se(e))}setBackdropGLB(e,t){if(this.disposeMeshListGpuResources(this.backdropMeshes),this.backdropMeshes=[],!e){this.update(0),this.render();return}let s=Se(e),{meshes:i}=Be(this.gl,s,He,()=>this.render());if(t)for(let r of i)r.transform[12]+=t[0],r.transform[13]+=t[1],r.transform[14]+=t[2],ae(r.normalMatrix,r.transform);this.backdropMeshes=i,this.update(0),this.render()}setClearColor(e,t,s,i=1){this.clearColor=[e,t,s,i]}updateMeshTransforms(e){for(let t=0;t<this.meshes.length;t++){let s=this.meshes[t],i;e instanceof Map?i=e.get(s.name)??e.get(t):Array.isArray(e)?i=e[t]:e&&typeof e=="object"&&(Object.prototype.hasOwnProperty.call(e,s.name)?i=e[s.name]:Object.prototype.hasOwnProperty.call(e,t)&&(i=e[t])),i&&i.length===16&&(s.transform.set(i),ae(s.normalMatrix,s.transform))}}bindLights(e=this.uRender){let t=this.gl,s=[.28,.28,.28],i=[],r=[],o=[],c=[];for(let a of this.lights){let l=a.color||[1,1,1],u=a.intensity!==void 0?a.intensity:1;if(a.type==="ambient")s=[l[0]*u,l[1]*u,l[2]*u];else if(a.type==="directional"&&i.length<6){let d=a.direction||[.5,1,.8];i.push(d[0],d[1],d[2]),r.push(l[0]*u,l[1]*u,l[2]*u)}else if(a.type==="point"&&o.length<6){let d=a.position||[0,0,0];o.push(d[0],d[1],d[2]),c.push(l[0]*u,l[1]*u,l[2]*u)}}i.length===0&&(i.push(.5,1,.8),r.push(1,1,1)),t.uniform3fv(e.uAmbientColor,s),t.uniform1i(e.uNumDirLights,i.length/3),i.length>0&&(t.uniform3fv(e.uDirLightDir,i),t.uniform3fv(e.uDirLightColor,r)),t.uniform1i(e.uNumPointLights,o.length/3),o.length>0&&(t.uniform3fv(e.uPointLightPos,o),t.uniform3fv(e.uPointLightColor,c)),t.uniform1f(e.uEnvIntensity,this.envIntensity)}setExposure(e){this.exposure=Ee(e)}getExposure(){return this.exposure}setShadow(e,t){this.shadowIntensity=Math.max(0,Math.min(1,Number(e)||0)),t!==void 0&&(this.shadowSoftness=Math.max(0,Math.min(1,Number(t)||0)))}getShadowIntensity(){return this.shadowIntensity}getShadowSoftness(){return this.shadowSoftness}setToneMapping(e){this.toneMapping=ye(e)}getToneMapping(){return this.toneMapping}async setEnvironmentImage(e){for(let t of Y)if(t.setEnvironmentImage){await t.setEnvironmentImage(this,e);return}}async setSkyboxImage(e){for(let t of Y)if(t.setSkyboxImage){await t.setSkyboxImage(this,e);return}}setInteractive(e){this.interactive=e}isInteractive(){return this.interactive}getCameraOrbit(){let e=this.currTheta,t=this.currPhi,s=this.currDistance;return{theta:e,phi:t,radius:s,toString(){return`${e.toFixed(4)}rad ${t.toFixed(4)}rad ${s.toFixed(4)}m`}}}getCameraTarget(){let e=this.customTarget||this.bounds.center;return{x:e[0],y:e[1],z:e[2],toString(){return`${e[0].toFixed(4)}m ${e[1].toFixed(4)}m ${e[2].toFixed(4)}m`}}}jumpCameraToGoal(){this.currTheta=this.targetTheta,this.currPhi=this.targetPhi,this.currDistance=this.targetDistance,this.update(0),this.render()}getModelMaterials(){let e=this.gltf?.materials?.length?this.gltf.materials:[{name:"DefaultMaterial",pbrMetallicRoughness:{}}];return e.map((t,s)=>{let i=this.meshes.flatMap(u=>u.primitives.filter(d=>d.materialIndex===s||e.length===1&&d.materialIndex===void 0)),r=i[0],o=r?[...r.baseColor]:t.pbrMetallicRoughness?.baseColorFactor||[.8,.8,.8,1],c=r?r.metallic:t.pbrMetallicRoughness?.metallicFactor??.1,a=r?r.roughness:t.pbrMetallicRoughness?.roughnessFactor??.5,l={baseColorFactor:o,metallicFactor:c,roughnessFactor:a,setBaseColorFactor:u=>{let d=[u[0],u[1],u[2],u[3]??1];l.baseColorFactor=d,t.pbrMetallicRoughness&&(t.pbrMetallicRoughness.baseColorFactor=d);for(let m of i)m.baseColor=d;this.render()},setMetallicFactor:u=>{l.metallicFactor=u,t.pbrMetallicRoughness&&(t.pbrMetallicRoughness.metallicFactor=u);for(let d of i)d.metallic=u;this.render()},setRoughnessFactor:u=>{l.roughnessFactor=u,t.pbrMetallicRoughness&&(t.pbrMetallicRoughness.roughnessFactor=u);for(let d of i)d.roughness=u;this.render()}};return{name:t.name||`Material_${s}`,pbrMetallicRoughness:l}})}playAnimation(e){if(this.animations.length!==0){if(e){let t=this.animations.find(s=>s.name===e);t&&(this.currentClip=t)}else this.currentClip||(this.currentClip=this.animations[0]);this.isPlayingAnimation=!0}}pauseAnimation(){this.isPlayingAnimation=!1}getAvailableAnimations(){return this.animations.map(e=>e.name)}getAvailableVariants(){return[...this.availableVariants]}getVariantName(){return this.activeVariantName}selectVariant(e){let t=this.applyVariantMaterial(e);return t&&this.gl&&this.render(),t}applyVariantMaterial(e){let t=e===null||e==="",s=-1;if(t)this.activeVariantName=null;else{if(s=this.availableVariants.indexOf(e),s<0)return!1;this.activeVariantName=e}let i=this.gltf?.materials;if(!Array.isArray(i))return t;for(let r of this.meshes)for(let o of r.primitives){let c=o.defaultMaterialIndex;if(!t&&Array.isArray(o.variantMappings)){for(let l of o.variantMappings)if(Array.isArray(l.variants)&&l.variants.includes(s)){c=l.material;break}}let a=c!==void 0?i[c]:void 0;o.materialIndex=a?c:void 0,Object.assign(o,Oe(a,this.gpuTextures)),this.applyPrimitiveProgram(o,o.materialIndex,a??null)}return!0}applyPrimitiveProgram(e,t,s){if(!this.gl||!e.primitiveDef)return;e.programCache??(e.programCache=new Map);let i=t??-1,r=e.programCache.get(i);r||(r=We(this.gl,{...e.primitiveDef,material:t},s),e.programCache.set(i,r)),e.customProgram=r.program,e.customUniforms=r.uniforms,e.pluginData&&(e.pluginData.materials=e.isTransmissive?{isTransmissive:!0}:void 0)}setAutoplay(e){this.autoplay=e,e?this.playAnimation(this.animationName||void 0):this.pauseAnimation()}setAnimationName(e){this.animationName=e,(this.isPlayingAnimation||this.autoplay)&&this.playAnimation(e)}evaluateAnimation(e){for(let t of Y)if(t.evaluateAnimation){t.evaluateAnimation(this,e);return}}setTarget(e,t,s){this.customTarget=[e,t,s],this.gl&&(this.meshes.length>0||this.backdropMeshes.length>0)&&(this.update(0),this.render())}getTarget(){return this.customTarget?[...this.customTarget]:null}setFieldOfView(e){let t=Math.max(10,Math.min(90,e));this.fov=t*Math.PI/180}getFieldOfView(){return this.fov*180/Math.PI}setDistanceMode(e){if(this.distanceMode=e,!this.customDistance){let t=this.clampDistance(this.distanceMode==="absolute"?3:this.bounds.radius*2.5);this.initialDistance=t,this.targetDistance=t,this.currDistance=t,this.gl&&(this.meshes.length>0||this.backdropMeshes.length>0)&&(this.update(0),this.render())}}getDistanceMode(){return this.distanceMode}setOrbitLimits(e){e.minTheta!==void 0&&(this.minTheta=e.minTheta*Math.PI/180),e.maxTheta!==void 0&&(this.maxTheta=e.maxTheta*Math.PI/180),e.minPhi!==void 0&&(this.minPhi=Math.max(-Math.PI*.48,e.minPhi*Math.PI/180)),e.maxPhi!==void 0&&(this.maxPhi=Math.min(Math.PI*.48,e.maxPhi*Math.PI/180)),e.minDistance!==void 0&&(this.minDistanceLimit=e.minDistance),e.maxDistance!==void 0&&(this.maxDistanceLimit=e.maxDistance),this.targetTheta=this.clampTheta(this.targetTheta),this.targetPhi=this.clampPhi(this.targetPhi),this.targetDistance=this.clampDistance(this.targetDistance)}getOrbitLimits(){return{minTheta:isFinite(this.minTheta)?this.minTheta*180/Math.PI:void 0,maxTheta:isFinite(this.maxTheta)?this.maxTheta*180/Math.PI:void 0,minPhi:this.minPhi*180/Math.PI,maxPhi:this.maxPhi*180/Math.PI,minDistance:this.minDistanceLimit!==null?this.minDistanceLimit:void 0,maxDistance:this.maxDistanceLimit!==null?this.maxDistanceLimit:void 0}}clampTheta(e){return Math.max(this.minTheta,Math.min(this.maxTheta,e))}clampPhi(e){return Math.max(this.minPhi,Math.min(this.maxPhi,e))}clampDistance(e){let t=this.minDistanceLimit!==null?this.minDistanceLimit:this.bounds?this.bounds.radius*.5:.1,s=this.maxDistanceLimit!==null?this.maxDistanceLimit:this.bounds?this.bounds.radius*8:100;return Math.max(t,Math.min(s,e))}setCamera(e,t,s,i){e!==void 0&&(this.targetTheta=this.clampTheta(e*Math.PI/180)),t!==void 0&&(this.targetPhi=this.clampPhi(t*Math.PI/180)),s!==void 0&&s>0&&(this.targetDistance=this.clampDistance(s)),i!==void 0&&i>0&&(this.targetDistance=this.clampDistance(this.bounds.radius*2.5/i))}setInitialCamera(e,t,s,i){if(e!==void 0&&(this.initialTheta=this.clampTheta(e*Math.PI/180),this.targetTheta=this.initialTheta,this.currTheta=this.initialTheta),t!==void 0&&(this.initialPhi=this.clampPhi(t*Math.PI/180),this.targetPhi=this.initialPhi,this.currPhi=this.initialPhi),s!==void 0&&s>0){let r=this.clampDistance(s);this.initialDistance=r,this.customDistance=!0,this.targetDistance=r,this.currDistance=r}else if(i!==void 0&&i>0){let r=this.clampDistance(this.bounds.radius*2.5/i);this.initialDistance=r,this.customDistance=!0,this.targetDistance=r,this.currDistance=r}else{this.customDistance=!1;let r=this.clampDistance(this.distanceMode==="absolute"?3:this.bounds.radius*2.5);this.initialDistance=r,this.targetDistance=r,this.currDistance=r}this.gl&&(this.meshes.length>0||this.backdropMeshes.length>0)&&(this.update(0),this.render())}resetCamera(){this.targetTheta=this.clampTheta(this.initialTheta),this.targetPhi=this.clampPhi(this.initialPhi),this.initialDistance!==null?this.targetDistance=this.clampDistance(this.initialDistance):this.targetDistance=this.clampDistance(this.bounds.radius*2.5),this.initialCustomTarget?this.customTarget=[...this.initialCustomTarget]:this.customTarget=null,this.turntableRotation=0,this.updateTurntableMatrix()}projectPointToScreen(e,t){let s=X();J(s,this.projMatrix,this.viewMatrix);let[i,r,o]=e,c=t?t[0]:0,a=t?t[1]:0,l=t?t[2]:0,u=this.modelRootMatrix;u&&(i=u[0]*e[0]+u[4]*e[1]+u[8]*e[2]+u[12],r=u[1]*e[0]+u[5]*e[1]+u[9]*e[2]+u[13],o=u[2]*e[0]+u[6]*e[1]+u[10]*e[2]+u[14],t&&(c=u[0]*t[0]+u[4]*t[1]+u[8]*t[2],a=u[1]*t[0]+u[5]*t[1]+u[9]*t[2],l=u[2]*t[0]+u[6]*t[1]+u[10]*t[2]));let d=s[0]*i+s[4]*r+s[8]*o+s[12],m=s[1]*i+s[5]*r+s[9]*o+s[13],y=s[2]*i+s[6]*r+s[10]*o+s[14],p=s[3]*i+s[7]*r+s[11]*o+s[15];if(p<=1e-4)return{x:0,y:0,z:1,visible:!1};let b=d/p,x=m/p,g=y/p,w=this.canvas.getBoundingClientRect(),M=w.width||this.canvas.clientWidth||this.canvas.width||300,T=w.height||this.canvas.clientHeight||this.canvas.height||150,f=(b*.5+.5)*M,R=(-x*.5+.5)*T,S=b>=-1.1&&b<=1.1&&x>=-1.1&&x<=1.1&&g>=-1&&g<=1;if(S&&t){let[L,k,U]=he(this),F=[i-L,r-k,o-U];c*F[0]+a*F[1]+l*F[2]>.05&&(S=!1)}return{x:f,y:R,z:g,visible:S}}setSelectedMesh(e){this.selectedMeshName=e||""}getSelectedMesh(){return this.selectedMeshName}setupEvents(){let e=this.canvas;this.boundContextMenu=t=>{t.preventDefault()},e.addEventListener("contextmenu",this.boundContextMenu),this.boundPointerDown=t=>{this.interactive&&(this.isPointerDown=!0,this.lastInteractionTime=typeof performance<"u"?performance.now():Date.now(),this.lastPointerX=t.clientX,this.lastPointerY=t.clientY,this.totalDragDist=0,this.activePointers.set(t.pointerId,{x:t.clientX,y:t.clientY}),this.isPanning=t.button===2||t.shiftKey||this.activePointers.size===2,e.setPointerCapture(t.pointerId))},e.addEventListener("pointerdown",this.boundPointerDown),this.boundPointerMove=t=>{if(!this.isPointerDown||!this.interactive)return;this.lastInteractionTime=typeof performance<"u"?performance.now():Date.now();let s=this.activePointers.get(t.pointerId)||{x:this.lastPointerX,y:this.lastPointerY},i=t.clientX-s.x,r=t.clientY-s.y;if(this.activePointers.set(t.pointerId,{x:t.clientX,y:t.clientY}),this.lastPointerX=t.clientX,this.lastPointerY=t.clientY,this.totalDragDist+=Math.hypot(i,r),this.isPanning||t.button===2||t.shiftKey||this.activePointers.size===2){let o=[this.viewMatrix[0],this.viewMatrix[4],this.viewMatrix[8]],c=[this.viewMatrix[1],this.viewMatrix[5],this.viewMatrix[9]],a=this.customTarget?[...this.customTarget]:[...this.bounds.center],l=this.currDistance/Math.max(e.clientHeight||300,1)*1.5;a[0]+=(-i*o[0]+r*c[0])*l,a[1]+=(-i*o[1]+r*c[1])*l,a[2]+=(-i*o[2]+r*c[2])*l,this.customTarget=a}else this.targetTheta=this.clampTheta(this.targetTheta-i*.008),this.targetPhi=this.clampPhi(this.targetPhi+r*.008)},e.addEventListener("pointermove",this.boundPointerMove),this.boundPointerUp=t=>{this.lastInteractionTime=typeof performance<"u"?performance.now():Date.now(),this.activePointers.delete(t.pointerId),this.activePointers.size===0?(this.isPointerDown=!1,this.isPanning=!1):this.activePointers.size===1&&(this.isPanning=!1);try{e.releasePointerCapture(t.pointerId)}catch{}if(this.totalDragDist<5&&t.button===0){let s=e.getBoundingClientRect(),i=Math.floor((t.clientX-s.left)*(e.width/Math.max(s.width,1))),r=Math.floor((t.clientY-s.top)*(e.height/Math.max(s.height,1)));this.performMeshPick(i,r,!0)}},e.addEventListener("pointerup",this.boundPointerUp),this.boundWheel=t=>{if(!this.interactive)return;t.preventDefault(),this.lastInteractionTime=typeof performance<"u"?performance.now():Date.now();let s=Math.exp(t.deltaY*.001);this.targetDistance=this.clampDistance(this.targetDistance*s)},e.addEventListener("wheel",this.boundWheel,{passive:!1}),this.boundSetCamera=t=>{let s=t.detail||{};s.reset?this.resetCamera():this.setCamera(s.theta,s.phi,s.distance,s.zoom)},this.container.addEventListener("setCamera",this.boundSetCamera),this.boundResetCamera=()=>{this.resetCamera()},this.container.addEventListener("resetCamera",this.boundResetCamera),e.tabIndex!==void 0&&e.tabIndex<0&&(e.tabIndex=0),e.getAttribute?.("role")||e.setAttribute?.("role","region"),e.getAttribute?.("aria-label")||e.setAttribute?.("aria-label","Interactive 3D model viewport"),this.boundKeyDown=t=>{if(!this.interactive)return;let s=typeof performance<"u"?performance.now():Date.now();switch(t.key){case"ArrowLeft":this.lastInteractionTime=s,this.targetTheta=this.clampTheta(this.targetTheta-.1),t.preventDefault();break;case"ArrowRight":this.lastInteractionTime=s,this.targetTheta=this.clampTheta(this.targetTheta+.1),t.preventDefault();break;case"ArrowUp":this.lastInteractionTime=s,this.targetPhi=this.clampPhi(this.targetPhi+.08),t.preventDefault();break;case"ArrowDown":this.lastInteractionTime=s,this.targetPhi=this.clampPhi(this.targetPhi-.08),t.preventDefault();break;case"+":case"=":{this.lastInteractionTime=s,this.targetDistance=this.clampDistance(this.targetDistance*.9),t.preventDefault();break}case"-":case"_":{this.lastInteractionTime=s,this.targetDistance=this.clampDistance(this.targetDistance*1.1),t.preventDefault();break}case"Home":this.lastInteractionTime=s,this.resetCamera(),t.preventDefault();break}},e.addEventListener("keydown",this.boundKeyDown)}teardownEvents(){let e=this.canvas;this.boundContextLost&&(e.removeEventListener("webglcontextlost",this.boundContextLost),this.boundContextLost=null),this.boundContextRestored&&(e.removeEventListener("webglcontextrestored",this.boundContextRestored),this.boundContextRestored=null),this.boundContextMenu&&(e.removeEventListener("contextmenu",this.boundContextMenu),this.boundContextMenu=null),this.boundPointerDown&&(e.removeEventListener("pointerdown",this.boundPointerDown),this.boundPointerDown=null),this.boundPointerMove&&(e.removeEventListener("pointermove",this.boundPointerMove),this.boundPointerMove=null),this.boundPointerUp&&(e.removeEventListener("pointerup",this.boundPointerUp),this.boundPointerUp=null),this.boundWheel&&(e.removeEventListener("wheel",this.boundWheel),this.boundWheel=null),this.boundKeyDown&&(e.removeEventListener("keydown",this.boundKeyDown),this.boundKeyDown=null),this.boundSetCamera&&(this.container.removeEventListener("setCamera",this.boundSetCamera),this.boundSetCamera=null),this.boundResetCamera&&(this.container.removeEventListener("resetCamera",this.boundResetCamera),this.boundResetCamera=null)}raycastFromScreenPoint(e,t){let s=this.canvas,i=s.getBoundingClientRect(),r=s.width||300,o=s.height||300,c=i.width>0?Math.floor((e-i.left)*(r/i.width)):Math.floor(e),a=i.height>0?Math.floor((t-i.top)*(o/i.height)):Math.floor(t);return this.performMeshPick(c,a,!1)}performMeshPick(e,t,s=!0){if(this.meshes.length===0)return null;let i=this.gl,r=Math.max(1,this.canvas.width||300),o=Math.max(1,this.canvas.height||300);e=this.mapStereoPickX(e,r);let c=X();J(c,this.projMatrix,this.viewMatrix);let a=[],l=Math.max(0,Math.min(r-1,e)),u=Math.max(0,Math.min(o-1,o-1-t));if(this.pickFbo&&i&&!this.xrSessionActive){(this.pickFboWidth!==r||this.pickFboHeight!==o)&&this.resizePickingFbo(r,o),i.bindFramebuffer(36160,this.pickFbo),i.viewport(0,0,r,o),i.enable(2929),i.clearColor(0,0,0,0),i.clear(16384|256),i.useProgram(this.pickProg);for(let S=0;S<this.meshes.length;S++){let L=this.meshes[S];if(L.visible===!1||L.isPluginMesh)continue;let k=S+1,U=(k&255)/255,F=(k>>8&255)/255,h=(k>>16&255)/255;i.uniform4f(this.uPick.uColorId,U,F,h,1),J(this.modelMatrix,this.modelRootMatrix,L.transform);let P=L.instanceMatrices;if(P&&P.length>=16){let E=P.length>>4,v=X();for(let B of L.primitives){i.bindVertexArray(B.vao);for(let D=0;D<E;D++)J(v,this.modelMatrix,P.subarray(D*16,D*16+16)),J(this.mvpMatrix,c,v),i.uniformMatrix4fv(this.uPick.uMVP,!1,this.mvpMatrix),ge(i,B)}}else{J(this.mvpMatrix,c,this.modelMatrix),i.uniformMatrix4fv(this.uPick.uMVP,!1,this.mvpMatrix);for(let E of L.primitives)i.bindVertexArray(E.vao),ge(i,E)}}let f=new Uint8Array(4);i.readPixels(l,u,1,1,6408,5121,f),i.bindFramebuffer(36160,null);let R=f[0]+(f[1]<<8)+(f[2]<<16);R>0&&R<=this.meshes.length&&(a=[this.meshes[R-1]])}a.length===0&&(a=this.meshes.filter(f=>f.visible!==!1));let d=null,m=1/0,y=[0,0,0],p=[0,1,0],[b,x,g]=he(this),w=X();for(let f of a){J(w,this.modelRootMatrix,f.transform);let R=f.instanceMatrices,S=R&&R.length>=16?R.length>>4:1;for(let L=0;L<S;L++){R&&R.length>=16?J(this.modelMatrix,w,R.subarray(L*16,L*16+16)):this.modelMatrix.set(w),J(this.mvpMatrix,c,this.modelMatrix);let k=X();if(!De(k,this.mvpMatrix))continue;let U=(l+.5)/r*2-1,F=(u+.5)/o*2-1,h=[0,0,0],P=[0,0,0];Re(h,U,F,-1,k),Re(P,U,F,1,k);let E=P[0]-h[0],v=P[1]-h[1],B=P[2]-h[2],D=Math.hypot(E,v,B)||1,$=E/D,A=v/D,W=B/D,_={distance:1/0,point:[0,0,0],normal:[0,1,0]};if((q=>{for(let G of f.primitives){if(!G.positions)continue;let z=G.positions,I=G.indices,ne=Math.floor(I?I.length/3:z.length/9);for(let H=0;H<ne;H++){let Q=I?I[H*3]:H*3,ee=I?I[H*3+1]:H*3+1,ie=I?I[H*3+2]:H*3+2;Le(h[0],h[1],h[2],$,A,W,z[Q*3],z[Q*3+1],z[Q*3+2],z[ee*3],z[ee*3+1],z[ee*3+2],z[ie*3],z[ie*3+1],z[ie*3+2],_,q)}}})(1e-4),isFinite(_.distance)){let q=_.point[0],G=_.point[1],z=_.point[2],I=this.modelMatrix,ne=I[0]*q+I[4]*G+I[8]*z+I[12],H=I[1]*q+I[5]*G+I[9]*z+I[13],Q=I[2]*q+I[6]*G+I[10]*z+I[14],ee=ne-b,ie=H-x,oe=Q-g,N=Math.hypot(ee,ie,oe);if(N<m){ae(this.normalMatrix,this.modelMatrix);let O=this.normalMatrix,te=_.normal[0],re=_.normal[1],C=_.normal[2],j=O[0]*te+O[3]*re+O[6]*C,V=O[1]*te+O[4]*re+O[7]*C,Z=O[2]*te+O[5]*re+O[8]*C,se=Math.hypot(j,V,Z)||1;j/=se,V/=se,Z/=se,j*ee+V*ie+Z*oe>0&&(j=-j,V=-V,Z=-Z),d=f,m=Math.round(N*1e3)/1e3,y=[Math.round(ne*1e3)/1e3,Math.round(H*1e3)/1e3,Math.round(Q*1e3)/1e3],p=[Math.round(j*1e3)/1e3,Math.round(V*1e3)/1e3,Math.round(Z*1e3)/1e3]}}}}if(!d)return null;let T={meshIndex:this.meshes.indexOf(d),meshName:d.name,nodeIndex:d.nodeIndex,distance:m,hitPoint:y,normal:p,screenX:e,screenY:t};if(s){for(let f of Y)f.onMeshSelect?.(this,T);this.opts.onMeshClick&&this.opts.onMeshClick(T),typeof CustomEvent<"u"&&typeof this.container.dispatchEvent=="function"&&this.container.dispatchEvent(new CustomEvent("meshClick",{bubbles:!0,detail:T}))}return T}mapStereoPickX(e,t){if(!this.stereoSBS||this.xrSessionActive||t<2)return e;let s=Math.max(1,Math.floor(t*.5)),i=e>=s,r=i?e-s:e,o=i?Math.max(1,t-s):s,c=(r+.5)/o*2-1;return Math.floor((c*.5+1)*.5*t)}setStereoSBS(e){this.stereoSBS=!!e,this.xrSessionActive||(this.syncCameraMatrices(),this.render())}syncCameraMatrices(){let e=Math.max(1,this.canvas.width||1)/Math.max(1,this.canvas.height||1),t=this.backdropMeshes.length>0?Math.max(this.bounds.radius*20,120):Math.max(this.bounds.radius*20,10);be(this.projMatrix,this.fov,e,.1,t);let s=this.customTarget||this.bounds.center;xe(this.viewMatrix,he(this),s,[0,1,0]),J(this.mvpMatrix,this.projMatrix,this.viewMatrix),ae(this.normalMatrix,this.modelMatrix)}resize(){let e=Math.min((typeof window<"u"?window.devicePixelRatio:1)||1,2),t=this.canvas.getBoundingClientRect(),s=Math.max(1,Math.floor(t.width*e)),i=Math.max(1,Math.floor(t.height*e));this.canvas.width!==s||this.canvas.height!==i?(this.canvas.width=s,this.canvas.height=i,this.resizePickingFbo(s,i),this.xrSessionActive||this.syncCameraMatrices()):!this.xrSessionActive&&this.projMatrix[0]===0&&this.syncCameraMatrices()}getGLContext(){return this.gl}setXRSessionActive(e){let t=this.xrSessionActive;if(this.xrSessionActive=e,t&&!e){this.xrDepthState.enabled=!1;for(let s of Y)s.onXRSessionEnd?.(this);if(this.lastTime=typeof performance<"u"?performance.now():Date.now(),this.updateTurntableMatrix(),this.isVisible)try{this.resize(),this.syncCameraMatrices(),this.render()}catch(s){this.reportRenderError(s)}}}reportRenderError(e){this.renderErrorReported||(this.renderErrorReported=!0,typeof console<"u"&&console.warn("[three-live] render frame failed:",e))}stepXRUpdate(e){for(let t of Y)t.onUpdate?.(this,e)}start(){if(this.animFrameId!==null)return;this.lastTime=typeof performance<"u"?performance.now():Date.now();let e=t=>{if(this.animFrameId===null)return;let s=Math.min(Math.max((t-this.lastTime)/1e3,0),.1);if(this.lastTime=t,this.isVisible&&!this.xrSessionActive)try{this.resize(),this.update(s),this.render()}catch(i){this.reportRenderError(i)}this.animFrameId!==null&&(this.animFrameId=requestAnimationFrame(e))};this.animFrameId=requestAnimationFrame(e)}stop(){this.animFrameId!==null&&(cancelAnimationFrame(this.animFrameId),this.animFrameId=null)}setAutoRotate(e){this.autoRotate=e}getAutoRotate(){return this.autoRotate}setAutoRotateSpeed(e){this.autoRotateSpeed=e}getAutoRotateSpeed(){return this.autoRotateSpeed}setAutoRotateDelay(e){this.autoRotateDelay=Math.max(0,Number(e)||0)}getAutoRotateDelay(){return this.autoRotateDelay}resetTurntableRotation(e=0){this.turntableRotation=Number(e)||0,this.updateTurntableMatrix(),this.render()}updateTurntableMatrix(){let e=Math.cos(this.turntableRotation),t=Math.sin(this.turntableRotation),s=this.bounds.center[0],i=this.bounds.center[2],r=this.modelRootMatrix;r[0]=e,r[1]=0,r[2]=-t,r[3]=0,r[4]=0,r[5]=1,r[6]=0,r[7]=0,r[8]=t,r[9]=0,r[10]=e,r[11]=0,r[12]=s-(e*s+t*i),r[13]=0,r[14]=i-(-t*s+e*i),r[15]=1}setVisible(e){this.isVisible=e}update(e){for(let o of Y)o.onUpdate?.(this,e);let t=typeof performance<"u"?performance.now():Date.now();if(!this.isPointerDown&&this.autoRotate&&t-this.lastInteractionTime>=this.autoRotateDelay){this.turntableRotation+=this.autoRotateSpeed*e,this.updateTurntableMatrix();for(let o of Y)o.onCameraChange?.(this,this.container)}this.currTheta+=(this.targetTheta-this.currTheta)*.12,this.currPhi+=(this.targetPhi-this.currPhi)*.12,this.currDistance+=(this.targetDistance-this.currDistance)*.12,this.syncCameraMatrices();let s=Math.round((this.currTheta*180/Math.PI%360+360)%360),i=Math.round(this.currPhi*180/Math.PI),r=Math.round(this.bounds.radius*2.5/this.currDistance*100)/100;if(Math.abs(s-this.lastReportedTheta)>=1||Math.abs(i-this.lastReportedPhi)>=1||Math.abs(r-this.lastReportedZoom)>=.02){this.lastReportedTheta=s,this.lastReportedPhi=i,this.lastReportedZoom=r;let o={theta:s,phi:i,zoom:r,distance:Math.round(this.currDistance*100)/100};this.opts.onCameraChange&&this.opts.onCameraChange(o),typeof CustomEvent<"u"&&typeof this.container.dispatchEvent=="function"&&this.container.dispatchEvent(new CustomEvent("cameraChange",{bubbles:!0,detail:o}))}}renderXRView(e,t,s,i,r=!0,o=0){this.onBeforeRenderXRView&&this.onBeforeRenderXRView(e);let c=e?.transform?.inverse?.matrix||this.viewMatrix,a=e?.projectionMatrix||this.projMatrix,l=e?.transform?.position,u=l?[l.x,l.y,l.z]:[0,1.6,0];t?this.modelRootMatrix.set(t):s&&t&&this.modelRootMatrix.set(t),this.render({viewMatrix:c,projMatrix:a,eyePos:u,viewport:i,eye:e?.eye||"none",clear:o>0?!1:e?.eye!=="right",isAR:r})}render(e){if(this.contextLost)return;if(!e&&this.stereoSBS){let M=Math.max(2,this.canvas.width||2),T=Math.max(1,this.canvas.height||1),f=Math.max(1,Math.floor(M*.5)),R=Math.max(1,M-f),S=this.backdropMeshes.length>0?Math.max(this.bounds.radius*20,120):Math.max(this.bounds.radius*20,10),L=this.customTarget||this.bounds.center,[k,U,F]=he(this),h=Math.cos(this.currTheta),P=-Math.sin(this.currTheta),E=.032,v=X(),B=X(),D=[k-h*E,U,F-P*E],$=[L[0]-h*E*.25,L[1],L[2]-P*E*.25];be(v,this.fov,f/T,.1,S),xe(B,D,$,[0,1,0]);let A=X(),W=X(),_=[k+h*E,U,F+P*E],K=[L[0]+h*E*.25,L[1],L[2]+P*E*.25];be(A,this.fov,R/T,.1,S),xe(W,_,K,[0,1,0]),this.render({viewMatrix:B,projMatrix:v,eyePos:D,viewport:[0,0,f,T],eye:"left",clear:!0,isAR:!1}),this.render({viewMatrix:W,projMatrix:A,eyePos:_,viewport:[f,0,R,T],eye:"right",clear:!1,isAR:!1}),this.gl.viewport(0,0,M,T);return}let t=this.gl,s=0,i=0,r=this.canvas.width,o=this.canvas.height;e?.viewport?(s=e.viewport[0],i=e.viewport[1],r=e.viewport[2],o=e.viewport[3],t.viewport(s,i,r,o)):t.viewport(0,0,this.canvas.width,this.canvas.height),t.enable(2929),(!e||e.clear!==!1)&&(e?.isAR?t.clearColor(0,0,0,0):t.clearColor(this.clearColor[0],this.clearColor[1],this.clearColor[2],this.clearColor[3]),t.clear(16384|256));let c=X(),a=e?.viewMatrix??this.viewMatrix;this.activeViewMatrix=a,J(c,e?.projMatrix??this.projMatrix,a);let l=ue().xrDepthShader,u=je(this.toneMapping),[d,m,y]=e?e.eyePos:he(this);for(let M of Y)M.onBeforeDrawScene?.(this,t,c,[d,m,y],u);let p=e?.isAR?0:this.backdropMeshes.length;if(this.meshes.length===0&&p===0)return;let b=e?.eye||"none",x=!1,g=p>0?[...this.backdropMeshes,...this.meshes]:this.meshes,w=M=>{for(let T=0;T<g.length;T++){let f=g[T];if(f.visible===!1)continue;let R=this.selectedMeshName!==""&&f.name===this.selectedMeshName;T<p||f.worldSpace===!0||f.name==="__xr_reticle__"?this.modelMatrix.set(f.transform):J(this.modelMatrix,this.modelRootMatrix,f.transform),J(this.mvpMatrix,c,this.modelMatrix),ae(f.normalMatrix,this.modelMatrix);let S=f.extras?.xr?.stereo||f.userData?.xr?.stereo,L=0,k=0,U=1,F=1;S==="sbs"?(U=.5,L=b==="right"?.5:0):S==="ou"&&(F=.5,k=b==="right"?.5:0);for(let h of f.primitives){let P=!!h.isTransmissive;if(!M&&P){x=!0;continue}if(M&&!P)continue;let E=h.customProgram||this.renderProg,v=h.customUniforms||this.uRender;t.useProgram(E),t.vertexAttrib3f(3,0,0,0),v.uInstanced&&t.uniform1i(v.uInstanced,0),t.uniform1i(v.uToneMapping,u),t.uniform1f(v.uEnvIntensity,this.envIntensity),v.uExposure&&t.uniform1f(v.uExposure,this.exposure),t.uniform3f(v.uCameraPos,d,m,y),t.uniform1f(v.uEmissive,R?1:0),t.uniformMatrix4fv(v.uMVP,!1,this.mvpMatrix),t.uniformMatrix4fv(v.uModel,!1,this.modelMatrix),t.uniformMatrix3fv(v.uNormalMatrix,!1,f.normalMatrix),v.uUvTransform&&t.uniform4f(v.uUvTransform,L,k,U,F),v.uViewportSize&&t.uniform2f(v.uViewportSize,Math.max(1,r),Math.max(1,o)),l?.bind(t,v,this.xrDepthState,a,[s,i,Math.max(1,r),Math.max(1,o)]),this.environmentTexture?(t.activeTexture(33988),t.bindTexture(3553,this.environmentTexture),t.uniform1i(v.uEnvironmentMap,4),t.uniform1i(v.uHasEnvironmentMap,1)):t.uniform1i(v.uHasEnvironmentMap,0),this.bindLights(v),v.uSectionEnabled&&t.uniform1i(v.uSectionEnabled,0);for(let D of Y)D.onBeforeDrawPrimitive?.(t,E,h,f,this.worldMatrices||[],this.gltf);t.uniform4fv(v.uBaseColor,h.baseColor),t.uniform1f(v.uMetallic,h.metallic),t.uniform1f(v.uRoughness,h.roughness),v.uClearcoat&&t.uniform2f(v.uClearcoat,h.clearcoat??0,h.clearcoatRoughness??.04),h.texture?(t.activeTexture(33984),t.bindTexture(3553,h.texture),t.uniform1i(v.uBaseColorTexture,0),t.uniform1i(v.uHasTexture,1)):t.uniform1i(v.uHasTexture,0),h.metallicRoughnessTexture?(t.activeTexture(33985),t.bindTexture(3553,h.metallicRoughnessTexture),t.uniform1i(v.uMetallicRoughnessTexture,1),t.uniform1i(v.uHasMetallicRoughnessTexture,1)):t.uniform1i(v.uHasMetallicRoughnessTexture,0),h.normalTexture?(t.activeTexture(33986),t.bindTexture(3553,h.normalTexture),t.uniform1i(v.uNormalTexture,2),t.uniform1i(v.uHasNormalTexture,1),t.uniform1f(v.uNormalScale,h.normalScale)):t.uniform1i(v.uHasNormalTexture,0),h.occlusionTexture?(t.activeTexture(33987),t.bindTexture(3553,h.occlusionTexture),t.uniform1i(v.uOcclusionTexture,3),t.uniform1i(v.uHasOcclusionTexture,1),t.uniform1f(v.uOcclusionStrength,h.occlusionStrength)):t.uniform1i(v.uHasOcclusionTexture,0),t.bindVertexArray(h.vao);let B=f.instanceMatrices;if(h.instanceCount!==void 0)t.drawElementsInstanced(4,h.indexCount,h.indexType,0,h.instanceCount);else if(B&&B.length>=16&&h.instanced&&v.uInstanced&&v.uViewProj){let D=B.length>>4;t.uniform1i(v.uInstanced,1),t.uniformMatrix4fv(v.uViewProj,!1,c),h.indices?t.drawElementsInstanced(4,h.indexCount,h.indexType,0,D):t.drawArraysInstanced(4,0,h.indexCount,D),t.uniform1i(v.uInstanced,0)}else if(B&&B.length>=16){let D=B.length>>4,$=f.instanceColors,A=X(),W=new Float32Array(9),_=[h.baseColor[0],h.baseColor[1],h.baseColor[2],h.baseColor[3]];for(let K=0;K<D;K++)J(A,this.modelMatrix,B.subarray(K*16,K*16+16)),J(this.mvpMatrix,c,A),ae(W,A),t.uniformMatrix4fv(v.uMVP,!1,this.mvpMatrix),t.uniformMatrix4fv(v.uModel,!1,A),t.uniformMatrix3fv(v.uNormalMatrix,!1,W),$&&(_[0]=h.baseColor[0]*$[K*3],_[1]=h.baseColor[1]*$[K*3+1],_[2]=h.baseColor[2]*$[K*3+2],_[3]=h.baseColor[3],t.uniform4fv(v.uBaseColor,_)),ge(t,h)}else ge(t,h)}}};if(w(!1),x){for(let M of Y)M.onBeforeTransmissivePass?.(t,this);w(!0)}for(let M of Y)M.onAfterRender?.(t,this,c,e?.eye??"none",[s,i,r,o],e?.projMatrix??this.projMatrix)}disposeMeshListGpuResources(e,t){Xe(this.gl,e,t)}disposeMeshGpuResources(){this.disposeMeshListGpuResources(this.meshes,this.gpuTextures),this.meshes=[],this.gpuTextures=[],this.availableVariants=[]}disposeGpuResources(){let e=this.gl;this.disposeMeshGpuResources(),this.disposeMeshListGpuResources(this.backdropMeshes),this.backdropMeshes=[];for(let t of Y)t.onDestroy?.(e,this);this.environmentTexture&&(e.deleteTexture(this.environmentTexture),this.environmentTexture=null),this.renderProg&&e.deleteProgram(this.renderProg),this.pickProg&&e.deleteProgram(this.pickProg),this.pickFbo&&e.deleteFramebuffer(this.pickFbo),this.pickTexture&&e.deleteTexture(this.pickTexture),this.pickDepth&&e.deleteRenderbuffer(this.pickDepth),this.pickFbo=null,this.pickTexture=null,this.pickDepth=null}dispose(){this.stop(),this.teardownEvents(),this.disposeGpuResources()}destroy(){this.dispose()}};me.registerPlugin=Ie,me.getRegisteredPlugins=Fe;var dt=me;export{me as MicroViewer,ft as SANDBOX_HOST_SCENE_LIMITS,me as ThreeView,Ye as decodeUtf8,dt as default,mt as evaluateToneMapping,le as getDecoder,Fe as getRegisteredPlugins,Ke as isGLBData,ae as mat3NormalFromMat4,X as mat4Create,Te as mat4FromRotationTranslationScale,De as mat4Invert,xe as mat4LookAt,J as mat4Multiply,be as mat4Perspective,ye as normalizeToneMapping,_e as parseGLB,Le as rayIntersectsTriangle,ht as readFloat32Array,$e as registerDecoder,Ie as registerPlugin,Ce as resolveNodeWorldMatrices,Qe as sanitizeTextureUri,Se as toGLBData,je as toneMappingShaderIndex,Re as unprojectPoint};
|