const j=Array,N=1e-6,vt=Math.PI,Mt=Math.tan,$=Math.hypot,_t=Math.acos,L=Math.sin,Y=Math.cos,B=Math.sqrt,k=Math.min,X=Math.max,zt=Math.floor;class V extends WeakMap{set(t,e,i){if("dispose"in t&&typeof t.dispose=="function"){const s=t.dispose.bind(t);t.dispose=()=>{i?.(),s(),this.delete(t)}}return super.set(t,e)}}class P extends j{constructor(){super(16),this.identity()}set(...t){for(let e=0;e<16;e++)this[e]=t[e];return this}copy(t){return this.set(...t)}identity(){return this.set(1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1)}multiply(t){if(typeof t=="number")for(let e=0;e<16;e++)this[e]*=t;else{const[e,i,s,r,a,n,l,h,p,c,o,d,u,g,x,m]=this,[y,b,v,_,M,z,T,A,w,S,E,U,C,I,O,D]=t;this.set(e*y+a*b+p*v+u*_,i*y+n*b+c*v+g*_,s*y+l*b+o*v+x*_,r*y+h*b+d*v+m*_,e*M+a*z+p*T+u*A,i*M+n*z+c*T+g*A,s*M+l*z+o*T+x*A,r*M+h*z+d*T+m*A,e*w+a*S+p*E+u*U,i*w+n*S+c*E+g*U,s*w+l*S+o*E+x*U,r*w+h*S+d*E+m*U,e*C+a*I+p*O+u*D,i*C+n*I+c*O+g*D,s*C+l*I+o*O+x*D,r*C+h*I+d*O+m*D)}return this}determinant(){const[t,e,i,s,r,a,n,l,h,p,c,o,d,u,g,x]=this,m=t*a-e*r,y=t*n-i*r,b=e*n-i*a,v=h*u-p*d,_=h*g-c*d,M=p*g-c*u,z=t*M-e*_+i*v,T=r*M-a*_+n*v,A=h*b-p*y+c*m,w=d*b-u*y+g*m;return l*z-s*T+x*A-o*w}transpose(){const[t,e,i,s,r,a,n,l,h,p,c,o,d,u,g,x]=this;return this.set(t,r,h,d,e,a,p,u,i,n,c,g,s,l,o,x)}invert(){const[t,e,i,s,r,a,n,l,h,p,c,o,d,u,g,x]=this,m=t*a-e*r,y=t*n-i*r,b=t*l-s*r,v=e*n-i*a,_=e*l-s*a,M=i*l-s*n,z=h*u-p*d,T=h*g-c*d,A=h*x-o*d,w=p*g-c*u,S=p*x-o*u,E=c*x-o*g,U=this.determinant();return U?this.set(a*E-n*S+l*w,i*S-e*E-s*w,u*M-g*_+x*v,c*_-p*M-o*v,n*A-r*E-l*T,t*E-i*A+s*T,g*b-d*M-x*y,h*M-c*b+o*y,r*S-a*A+l*z,e*A-t*S-s*z,d*_-u*b+x*m,p*b-h*_-o*m,a*T-r*w-n*z,t*w-e*T+i*z,u*y-d*v-g*m,h*v-p*y+c*m).multiply(1/U):this}perspective(t,e,i,s){const r=t*(vt/180),a=1/Mt(r/2),n=1/(i-s);return this.set(a/e,0,0,0,0,a,0,0,0,0,(s+i)*n,-1,0,0,2*s*i*n,0)}orthogonal(t,e,i,s,r,a){const n=1/(t-e),l=1/(i-s),h=1/(r-a);return this.set(-2*n,0,0,0,0,-2*l,0,0,0,0,2*h,0,(t+e)*n,(s+i)*l,(a+r)*h,1)}compose(t,e,i){const s=2*e.x*e.x,r=2*e.y*e.x,a=2*e.z*e.x,n=2*e.y*e.y,l=2*e.z*e.y,h=2*e.z*e.z,p=2*e.x*e.w,c=2*e.y*e.w,o=2*e.z*e.w;return this.set((1-(n+h))*i.x,(r+o)*i.x,(a-c)*i.x,0,(r-o)*i.y,(1-(s+h))*i.y,(l+p)*i.y,0,(a+c)*i.z,(l-p)*i.z,(1-(s+n))*i.z,0,t.x,t.y,t.z,1)}normal(t){const[e,i,s,r,a,n,l,h,p,c,o,d,u,g,x,m]=t,y=e*n-i*a,b=e*l-s*a,v=e*h-r*a,_=i*l-s*n,M=i*h-r*n,z=s*h-r*l,T=p*g-c*u,A=p*x-o*u,w=p*m-d*u,S=c*x-o*g,E=c*m-d*g,U=o*m-d*x,C=t.determinant();return C?this.set(n*U-l*E+h*S,s*E-i*U-r*S,g*z-x*M+m*_,0,l*w-a*U-h*A,e*U-s*w+r*A,x*v-u*z-m*b,0,a*E-n*w+h*T,i*w-e*E-r*T,u*M-g*v+m*y,0,0,0,0,1).multiply(1/C):this}normalNDC(){return this[8]+=this[12]/=2,this[9]+=this[13]/=2,this[10]+=this[14]/=2,this[11]+=this[15]/=2,this}}class F extends j{constructor(t=0,e=0,i=0){super(3),this.set(t,e,i)}get x(){return this[0]}set x(t){this[0]=t}get y(){return this[1]}set y(t){this[1]=t}get z(){return this[2]}set z(t){this[2]=t}set(...t){for(let e=0;e<3;e++)this[e]=t[e];return this}copy(t){return this.set(...t)}add(t){for(let e=0;e<3;e++)this[e]+=typeof t=="number"?t:t[e];return this}sub(t){for(let e=0;e<3;e++)this[e]-=typeof t=="number"?t:t[e];return this}multiply(t){for(let e=0;e<3;e++)this[e]*=typeof t=="number"?t:t[e];return this}divide(t){for(let e=0;e<3;e++)this[e]/=typeof t=="number"?t:t[e];return this}invert(){return this.multiply(-1)}getLength(){return $(...this)}normalize(){return this.divide(this.getLength()||1)}distanceTo(t){return $(this.x-t.x,this.y-t.y,this.z-t.z)}dot(t){return this.x*t.x+this.y*t.y+this.z*t.z}cross(t){return this.set(this.y*t.z-this.z*t.y,this.z*t.x-this.x*t.z,this.x*t.y-this.y*t.x)}lerp(t,e){return this.set(t.x-this.x,t.y-this.y,t.z-this.z).multiply(e)}applyQuaternion(t){const e=t.w*this.x+t.y*this.z-t.z*this.y,i=t.w*this.y+t.z*this.x-t.x*this.z,s=t.w*this.z+t.x*this.y-t.y*this.x,r=-t.x*this.x-t.y*this.y-t.z*this.z;return this.set(e*t.w+r*-t.x+i*-t.z-s*-t.y,i*t.w+r*-t.y+s*-t.x-e*-t.z,s*t.w+r*-t.z+e*-t.y-i*-t.x)}applyMatrix4(t){const[e,i,s,r,a,n,l,h,p,c,o,d,u,g,x,m]=t;return this.set(e*this.x+a*this.y+p*this.z+u,i*this.x+n*this.y+c*this.z+g,s*this.x+l*this.y+o*this.z+x).divide(r*this.x+h*this.y+d*this.z+m||1)}}class st extends j{_a=new F;_b=new F;_c=new F;constructor(t=0,e=0,i=0,s=1){super(4),this.set(t,e,i,s)}get x(){return this[0]}set x(t){this[0]=t}get y(){return this[1]}set y(t){this[1]=t}get z(){return this[2]}set z(t){this[2]=t}get w(){return this[3]}set w(t){this[3]=t}set(...t){for(let e=0;e<4;e++)this[e]=t[e];return this}copy(t){return this.set(...t)}identity(){return this.set(0,0,0,1)}add(t){for(let e=0;e<4;e++)this[e]+=typeof t=="number"?t:t[e];return this}sub(t){for(let e=0;e<4;e++)this[e]-=typeof t=="number"?t:t[e];return this}multiply(t){if(typeof t=="number")for(let e=0;e<4;e++)this[e]*=t;else{const[e,i,s,r]=this,[a,n,l,h]=t;this.set(e*h+r*a+i*l-s*n,i*h+r*n+s*a-e*l,s*h+r*l+e*n-i*a,r*h-e*a-i*n-s*l)}return this}divide(t){for(let e=0;e<4;e++)this[e]/=typeof t=="number"?t:t[e];return this}invert(){for(let t=0;t<4;t++)this[t]*=-1;return this}getLength(){return $(...this)}normalize(){return this.divide(this.getLength()||1)}dot(t){return this.x*t.x+this.y*t.y+this.z*t.z+this.w*t.w}fromEuler(t,e,i){const s=L(t),r=Y(t),a=L(e),n=Y(e),l=L(i),h=Y(i);return this.set(s*n*h+r*a*l,r*a*h-s*n*l,r*n*l+s*a*h,r*n*h-s*a*l)}slerp(t,e){let i=this.dot(t);i<0&&(i*=-1);let s=1-e,r=e;if(1-i>N){const a=_t(i),n=L(a);s=L((1-e)*a)/n,r=L(e*a)/n}return i<0&&(r*=-1),this.set(s*this.x+r*t.x,s*this.y+r*t.y,s*this.z+r*t.z,s*this.w+r*t.w)}lookAt(t,e,i){const s=this._a.copy(t).sub(e);s.getLength()===0?s.z=1:s.normalize();const r=this._b.copy(i).cross(s);if(r.getLength()===0){const m=this._c.copy(i);m.z?m.x+=N:m.y?m.z+=N:m.y+=N,r.cross(m)}r.normalize();const a=this._c.copy(s).cross(r),[n,l,h]=r,[p,c,o]=a,[d,u,g]=s,x=n+c+g;if(x>0){const m=B(x+1)*2;return this.set((o-u)/m,(d-h)/m,(l-p)/m,m/4)}else if(n>c&&n>g){const m=B(1+n-c-g)*2;return this.set(m/4,(l+p)/m,(d+h)/m,(o-u)/m)}else if(c>g){const m=B(1+c-n-g)*2;return this.set((l+p)/m,m/4,(o+u)/m,(d-h)/m)}else{const m=B(1+g-n-c)*2;return this.set((d+h)/m,(o+u)/m,m/4,(l-p)/m)}}}class H{matrix=new P;quaternion=new st;position=new F;scale=new F(1,1,1);up=new F(0,1,0);children=[];parent=null;matrixAutoUpdate=!0;visible=!0;frustumCulled=!0;lookAt(t){this.quaternion.lookAt(this.position,t,this.up)}updateMatrix(){if(this.matrixAutoUpdate){this.matrix.compose(this.position,this.quaternion,this.scale),this.parent&&this.matrix.multiply(this.parent.matrix);for(const t of this.children)t.updateMatrix()}}add(...t){for(const e of t)this.children.push(e),e.parent=this}remove(...t){for(const e of t){const i=this.children.indexOf(e);i!==-1&&this.children.splice(i,1),e.parent=null}}traverse(t){if(!t(this))for(const e of this.children)e.traverse(t)}}class it extends j{a=new P;constructor(){super(24)}set(...t){for(let e=0;e<24;e++)this[e]=t[e];return this}copy(t){return this.set(...t)}fromMatrix4(t){const[e,i,s,r,a,n,l,h,p,c,o,d,u,g,x,m]=this.a.copy(t);this.set(r-e,h-a,d-p,m-u,r+e,h+a,d+p,m+u,r+i,h+n,d+c,m+g,r-i,h-n,d-c,m-g,r-s,h-l,d-o,m-x,r+s,h+l,d+o,m+x)}contains(t){const{position:e}=t.geometry.attributes,i=e.data.length/e.size;let s=0;for(let r=0;r<i;r+=e.size){let a=0;for(let n=r;n<r+e.size;n++)a+=e.data[n]**2;s=X(s,B(a))}s*=X(...t.scale);for(let r=0;r<6;r++){const a=r*4;if(this[a]*t.matrix[12]+this[a+1]*t.matrix[13]+this[a+2]*t.matrix[14]+this[a+3]<=-s)return!1}return!0}normalNDC(){const t=this.a;return this[20]-=t[3],this[21]-=t[7],this[22]-=t[11],this[23]-=t[15],this}}class J extends H{projectionMatrix=new P;viewMatrix=new P;projectionViewMatrix=new P;frustum=new it;updateMatrix(){super.updateMatrix(),this.matrixAutoUpdate&&this.viewMatrix.copy(this.matrix).invert()}}class rt{constructor(t={}){this.attributes=t;for(const e in t)this.attributes[e]=t[e],this.attributes[e].needsUpdate=!0}drawRange={start:0,count:1/0};dispose(){}}class at{uniforms={};vertex;fragment;compute;side="front";transparent=!1;depthTest=!0;depthWrite=!0;blending;constructor(t){t?.transparent&&(this.blending={color:{operation:"add",srcFactor:"src-alpha",dstFactor:"one-minus-src-alpha"},alpha:{operation:"add",srcFactor:"one",dstFactor:"one-minus-src-alpha"}}),Object.assign(this,t)}dispose(){}}class Q extends H{constructor(t=new rt,e=new at){super(),this.geometry=t,this.material=e}modelViewMatrix=new P;normalMatrix=new P;mode="triangles";instances=1}class Tt extends J{constructor(t=.1,e=1e3,i=-1,s=1,r=-1,a=1){super(),this.near=t,this.far=e,this.left=i,this.right=s,this.bottom=r,this.top=a}updateMatrix(){super.updateMatrix(),this.matrixAutoUpdate&&this.projectionMatrix.orthogonal(this.left,this.right,this.bottom,this.top,this.near,this.far)}}class At extends J{constructor(t=75,e=1,i=.1,s=1e3){super(),this.fov=t,this.aspect=e,this.near=i,this.far=s}updateMatrix(){super.updateMatrix(),this.matrixAutoUpdate&&this.projectionMatrix.perspective(this.fov,this.aspect,this.near,this.far)}}class K{magFilter="nearest";minFilter="nearest";wrapS="clamp";wrapT="clamp";anisotropy=1;generateMipmaps=!0;needsUpdate=!0;constructor(t){t&&Object.assign(this,t)}}class q{constructor(t,e=new K,i,s,r=!0){this.image=t,this.sampler=e,this.format=i,this.type=s,this.needsUpdate=r}dispose(){}}class Et{constructor(t,e,i=1){this.width=t,this.height=e,this.count=i,this.textures=j.from({length:i},()=>new q(void 0,this.sampler))}textures;sampler=new K({generateMipmaps:!1});needsUpdate=!0;setSize(t,e){this.width=t,this.height=e,this.needsUpdate=!0}dispose(){}}const G=36160,Vt=36064,R=3553,St=10240,Ut=10241,Ft=10242,Pt=10243,Ct=6408,Z=2929,nt=2884,ot=3042,Lt=3317,jt=35633,Bt=35632,kt=34963,ht=34962,Rt=35044,It=35048,Ot=16384,Dt=256,Nt=1024,qt=513,Gt=1028,Wt=1029,$t=33984,Yt=0,lt=36386,Xt=35982,Ht=35981,ct=35977,Jt=9728,Qt=9729,dt={nearest:Jt,linear:Qt},Kt=9985,Zt=9986,te={nearest:Kt,linear:Zt},ee=10497,se=33071,ie=33648,ut={repeat:ee,clamp:se,mirror:ie},re=0,ae=1,ne=768,oe=769,he=770,le=771,ce=774,de=772,ue=773,me=775,pe=776,ge=32769,fe=32770,W={zero:re,one:ae,src:ne,"one-minus-src":oe,"src-alpha":he,"one-minus-src-alpha":le,"dst-alpha":de,"one-minus-dst-alpha":ue,dst:ce,"one-minus-dst":me,"src-alpha-saturated":pe,constant:ge,"one-minus-constant":fe},xe=32774,ye=32778,be=32779,we=32775,ve=32776,mt={add:xe,subtract:ye,"reverse-subtract":be,min:we,max:ve},pt=5126,Me=5120,_e=5122,ze=5124,gt=5121,Te=5123,Ae=5125,ft=f=>{switch(f.constructor){case Float32Array:return pt;case Int8Array:return Me;case Int16Array:return _e;case Int32Array:return ze;case Uint8Array:case Uint8ClampedArray:return gt;case Uint16Array:return Te;case Uint32Array:return Ae;default:return null}},Ee=/[^\w](?:varying|out)\s+\w+\s+(\w+)\s*;/g;class Ve{canvas;gl;autoClear=!0;_c=new V;_p=new V;_g=new V;_b=new V;_t=new V;_s=new V;_f=new V;c;a=0;_v=new F;constructor({canvas:t,context:e,...i}={}){this.canvas=t??document.createElement("canvas"),this.gl=e??this.canvas.getContext("webgl2",{alpha:!0,depth:!0,premultipliedAlpha:!0,...i})}setSize(t,e){this.canvas.width=t,this.canvas.height=e,this.gl.viewport(0,0,t,e)}d(t){let e=this._s.get(t);if(e||(e=this.gl.createSampler(),this._s.set(t,e),t.needsUpdate=!0),t.needsUpdate){if(t.anisotropy){const s=this.gl.getExtension("EXT_texture_filter_anisotropic");s&&this.gl.samplerParameteri(e,s.TEXTURE_MAX_ANISOTROPY_EXT,t.anisotropy)}const i=t.generateMipmaps?te:dt;this.gl.samplerParameteri(e,St,dt[t.magFilter]),this.gl.samplerParameteri(e,Ut,i[t.minFilter]),this.gl.samplerParameteri(e,Ft,ut[t.wrapS]),this.gl.samplerParameteri(e,Pt,ut[t.wrapT]),t.needsUpdate=!1}return e}b(t,e=0,i=0){let s=this._t.get(t);if(s||(s=this.gl.createTexture(),this._t.set(t,s,()=>this.gl.deleteTexture(s)),t.needsUpdate=!0),this.gl.bindTexture(R,s),t.needsUpdate){this.gl.pixelStorei(Lt,1);const r=t.format??Ct,a=t.type??gt;t.image?this.gl.texImage2D(R,0,r,r,a,t.image):this.gl.texImage2D(R,0,r,e,i,0,r,a,null),t.image instanceof HTMLVideoElement||(t.needsUpdate=!1)}return(t.needsUpdate||t.sampler.needsUpdate)&&(t.sampler.generateMipmaps&&this.gl.generateMipmap(R),this.d(t.sampler)),s}setRenderTarget(t){if(!t)return this.gl.bindFramebuffer(G,null),this.gl.viewport(0,0,this.canvas.width,this.canvas.height);let e=this._f.get(t);if(!e||t.needsUpdate){e&&this.gl.deleteFramebuffer(e),e=this.gl.createFramebuffer(),this.gl.bindFramebuffer(G,e);const i=[];let s=Vt;for(const r of t.textures){i.push(s);const a=this.b(r,t.width,t.height);this.gl.framebufferTexture2D(G,s,R,a,0),s++}this.gl.drawBuffers(i),t.needsUpdate=!1,this._f.set(t,e,()=>this.gl.deleteFramebuffer(e))}this.gl.bindFramebuffer(G,e),this.gl.viewport(0,0,t.width,t.height)}setDepthTest(t,e=qt){t?(this.gl.enable(Z),this.gl.depthFunc(e)):this.gl.disable(Z)}setDepthMask(t){this.gl.depthMask(t)}setCullSide(t="both"){t==="both"?(this.gl.disable(nt),this.gl.disable(Z)):(this.gl.enable(nt),this.gl.cullFace(t==="front"?Wt:Gt))}setBlending(t){t?(this.gl.enable(ot),this.gl.blendFuncSeparate(W[t.color.srcFactor],W[t.color.dstFactor],W[t.alpha.srcFactor],W[t.alpha.dstFactor]),this.gl.blendEquationSeparate(mt[t.color.operation],mt[t.alpha.operation])):this.gl.disable(ot)}e(t,e,i){const s=this.gl.getUniformLocation(t,e);if(s!==-1){if(i instanceof q){const r=this.a++;this.gl.activeTexture($t+r);const a=this._s.get(i.sampler);return a&&this.gl.bindSampler(r,a),this.b(i),this.gl.uniform1i(s,r)}if(typeof i=="number")return this.gl.uniform1f(s,i);switch(i.length){case 2:return this.gl.uniform2fv(s,i);case 3:return this.gl.uniform3fv(s,i);case 4:return this.gl.uniform4fv(s,i);case 9:return this.gl.uniformMatrix3fv(s,!1,i);case 16:return this.gl.uniformMatrix4fv(s,!1,i)}}}compile(t,e){t.material.uniforms.modelMatrix=t.matrix,e&&(t.material.uniforms.projectionMatrix=e.projectionMatrix,t.material.uniforms.viewMatrix=e.viewMatrix,t.material.uniforms.normalMatrix=t.normalMatrix,t.material.uniforms.modelViewMatrix=t.modelViewMatrix,t.modelViewMatrix.copy(e.viewMatrix).multiply(t.matrix),t.normalMatrix.normal(t.modelViewMatrix));let i=this._c.get(t),s=this._p.get(t.material);if(!s){s=this.gl.createProgram(),this._p.set(t.material,s,()=>this.gl.deleteProgram(s));const a=this.gl.createShader(jt);this.gl.shaderSource(a,t.material.vertex??t.material.compute),this.gl.compileShader(a),this.gl.attachShader(s,a);const n=this.gl.createShader(Bt);this.gl.shaderSource(n,t.material.fragment??`#version 300 es
void main(){}`),this.gl.compileShader(n),this.gl.attachShader(s,n),this.gl.linkProgram(s);for(const l of[a,n]){const h=this.gl.getShaderInfoLog(l);if(h)throw`${h}
${this.gl.getShaderSource(l)}`}if(this.gl.getProgramInfoLog(s))throw`${this.gl.getProgramInfoLog(s)}`;this.gl.deleteShader(a),this.gl.deleteShader(n)}let r=this._g.get(t.geometry);r||(r=this.gl.createVertexArray(),this._g.set(t.geometry,r,()=>this.gl.deleteVertexArray(r))),this.gl.useProgram(s),this.gl.bindVertexArray(r);for(const a in t.geometry.attributes){const n=t.geometry.attributes[a],l=a==="index"?kt:ht;let h=this._b.get(n);if(!h||s!==i?.program||r!==i?.VAO){h?this.gl.bindBuffer(l,h):(h=this.gl.createBuffer(),this._b.set(n,h),this.gl.bindBuffer(l,h),this.gl.bufferData(l,n.data,Rt),n.needsUpdate=!1);const p=this.gl.getAttribLocation(s,a);if(p!==-1){const c=k(4,X(1,zt(n.size/3)));for(let o=0;o<c;o++){this.gl.enableVertexAttribArray(p+o);const d=ft(n.data),u=n.size*n.data.BYTES_PER_ELEMENT,g=n.size*o;d===pt?this.gl.vertexAttribPointer(p,n.size,d,!1,u,g):this.gl.vertexAttribIPointer(p,n.size,d,u,g),n.divisor&&this.gl.vertexAttribDivisor(p+o,n.divisor)}}}n.needsUpdate&&(this.gl.bindBuffer(l,h),this.gl.bufferData(l,n.data,It),n.needsUpdate=!1)}this.a=0;for(const a in t.material.uniforms)this.e(s,a,t.material.uniforms[a]);return i||(i={program:s,VAO:r},this._c.set(t,i)),i}clear(t=Ot|Dt|Nt){this.gl.clear(t)}sort(t,e){const i=[];return e?.matrixAutoUpdate&&(e.projectionViewMatrix.copy(e.projectionMatrix).multiply(e.viewMatrix),e.frustum.fromMatrix4(e.projectionViewMatrix)),t.traverse(s=>{if(!s.visible)return!0;if(s instanceof Q){if(e&&s.frustumCulled&&!e.frustum.contains(s))return!0;i.push(s)}}),i.sort((s,r)=>r.material.depthTest-s.material.depthTest||!!e&&this._v.set(r.matrix[12],r.matrix[13],r.matrix[14]).applyMatrix4(e.projectionViewMatrix).z-this._v.set(s.matrix[12],s.matrix[13],s.matrix[14]).applyMatrix4(e.projectionViewMatrix).z||s.material.transparent-r.material.transparent)}render(t,e){this.autoClear&&this.clear(),t.updateMatrix(),e?.updateMatrix();const i=this.sort(t,e);for(const s of i){this.compile(s,e),this.setDepthTest(s.material.depthTest),this.setDepthMask(s.material.depthWrite),this.setCullSide(s.material.side),this.setBlending(s.material.blending);const r=this.gl[s.mode.toUpperCase()],{index:a,position:n}=s.geometry.attributes,{start:l,count:h}=s.geometry.drawRange;a?this.gl.drawElementsInstanced(r,k(h,a.data.length/a.size),ft(a.data),l,s.instances):n?this.gl.drawArraysInstanced(r,l,k(h,n.data.length/n.size),s.instances):this.gl.drawArraysInstanced(r,0,3,s.instances)}}compute(t){const e=this.compile(t);this.gl.bindVertexArray(null),this.gl.bindBuffer(ht,null);const i=this.c??=this.gl.createTransformFeedback();this.gl.bindTransformFeedback(lt,i);let s=0;const r=[];for(const[,n]of t.material.compute.matchAll(Ee)){const l=t.geometry.attributes[n],h=this._b.get(l);this.gl.bindBufferBase(Xt,r.length,h),s=Math.max(s,l.data.length/l.size),r.push(n)}this.gl.transformFeedbackVaryings(e.program,r,Ht),this.gl.linkProgram(e.program);const a=Yt;this.gl.enable(ct),this.gl.beginTransformFeedback(a),this.gl.bindVertexArray(e.VAO),s?this.gl.drawArraysInstanced(a,0,s,t.instances):this.gl.drawArraysInstanced(a,0,t.instances,1),this.gl.bindVertexArray(null),this.gl.endTransformFeedback(),this.gl.disable(ct),this.gl.bindTransformFeedback(lt,null)}}const Se={front:"back",back:"front",both:"none"},Ue={points:"point-list",lines:"line-list",triangles:"triangle-list"},xt={clamp:"clamp-to-edge",repeat:"repeat",mirror:"mirror-repeat"},Fe=8,Pe=16,Ce=32,Le=64,je=128,Be=1,ke=2,Re=4,tt=16,Ie=15,yt=f=>f+f%2,et=f=>f+(4-f%4)%4;function bt(f,t){const e=f;let i=0;if(!t){for(const s of e)if(typeof s=="number")i++;else{const r=s.length<=2?yt:et;i=r(i),i+=r(s.length)}i=et(i),t=new Float32Array(i)}i=0;for(const s of e)if(typeof s=="number")t[i++]=s;else{const r=s.length<=2?yt:et;t.set(s,i=r(i)),i+=r(s.length)}return t}function wt(...f){if(f=f.filter(Boolean),f.length>1)return f.map(s=>wt(s)).filter(Boolean).sort((s,r)=>r.length-s.length)[0];const t=f[0].replace(/\/\*(?:[^*]|\**[^*/])*\*+\/|\/\/.*/g,""),e=t.match(/var\s*<\s*uniform\s*>[^;]+(?:\s|:)(\w+);/)?.[1],i=t.match(new RegExp(`${e}[^\\{]+\\{([^\\}]+)\\}`))?.[1];if(i)return Array.from(i.match(/\w+(?=[;:])/g))}const Oe=/var\s*<\s*storage[^>]+>\s*(\w+)/g,De=/@workgroup_size\s*\(([^)]+)\)/,Ne=typeof navigator<"u"?await navigator.gpu?.requestAdapter():null,qe=await Ne?.requestDevice();class Ge{canvas;device;format;context;autoClear=!0;samples=4;_b=new V;_g=new V;_u=new V;_p=new V;_t=new V;_s=new V;_f=new V;d;g;e;h;c;a;b=null;_v=new F;constructor({canvas:t,context:e,format:i,device:s}={}){this.canvas=t??document.createElement("canvas"),this.context=e??this.canvas.getContext("webgpu"),this.format=i??navigator.gpu.getPreferredCanvasFormat(),this.device=s??qe,this.f()}f(){this.context.configure({device:this.device,format:this.format,alphaMode:"premultiplied"});const t=[this.canvas.width,this.canvas.height,1],e=tt,i=this.samples;this.d&&this.d.destroy(),this.d=this.device.createTexture({format:this.format,size:t,usage:e,sampleCount:i}),this.g=this.d.createView(),this.e&&this.e.destroy(),this.e=this.device.createTexture({format:"depth24plus-stencil8",size:t,usage:e,sampleCount:i}),this.h=this.e.createView()}setSize(t,e){this.canvas.width=t,this.canvas.height=e,this.f()}setRenderTarget(t){this.b=t}i(t,e){const i=this.device.createBuffer({size:t.byteLength,usage:e|Fe|je,mappedAtCreation:!0});return new t.constructor(i.getMappedRange()).set(t),i.unmap(),i}j(t,e,i=e.byteLength,s=0,r=e.byteOffset){const a=e.BYTES_PER_ELEMENT;this.device.queue.writeBuffer(t,r,e,s/a,i/a)}l(t){let e=this._s.get(t);return(!e||t.needsUpdate)&&(e=this.device.createSampler({addressModeU:xt[t.wrapS],addressModeV:xt[t.wrapT],magFilter:t.magFilter,minFilter:t.minFilter,maxAnisotropy:t.anisotropy}),this._s.set(t,e)),e}k(t,e=t.image?.width??0,i=t.image?.height??0){let s=this._t.get(t);return(!s||t.needsUpdate)&&(t.dispose(),t.image instanceof HTMLVideoElement?s=this.device.importExternalTexture({source:t.image}):(s=this.device.createTexture({format:t.format??this.format,dimension:"2d",size:[e,i,1],usage:ke|Re|tt|Be}),t.image&&this.device.queue.copyExternalImageToTexture({source:t.image},{texture:s},[e,i]),t.needsUpdate=!1),this._t.set(t,s,()=>s.destroy?.())),this.l(t.sampler),s}compile(t,e){t.material.uniforms.modelMatrix=t.matrix,e&&(t.material.uniforms.projectionMatrix=e.projectionMatrix,t.material.uniforms.viewMatrix=e.viewMatrix,t.material.uniforms.normalMatrix=t.normalMatrix,t.material.uniforms.modelViewMatrix=t.modelViewMatrix,t.modelViewMatrix.copy(e.viewMatrix).multiply(t.matrix),t.normalMatrix.normal(t.modelViewMatrix));const i=[];let s=0;for(const c in t.geometry.attributes){const o=t.geometry.attributes[c];if(c==="index")continue;const d=o.data instanceof Float32Array?"float":"uint",u=o.data.BYTES_PER_ELEMENT*8,g=d+u;i.push({arrayStride:o.size*o.data.BYTES_PER_ELEMENT*(o.divisor||1),stepMode:o.divisor?"instance":"vertex",attributes:[{shaderLocation:s++,offset:0,format:`${g}x${Math.min(o.size,4)}`}]})}let r=this._p.get(t);if(t.material.compute)r||(r=this.device.createComputePipeline({compute:{module:this.device.createShaderModule({code:t.material.compute}),entryPoint:"main"},layout:"auto"}));else{const c=t.material.transparent,o=Se[t.material.side],d=Ue[t.mode],u=t.material.depthWrite,g=t.material.depthTest?"less":"always",x=t.material.blending,m=this.b?.count??1,y=this.samples,b=JSON.stringify([c,o,d,u,g,i,x,m,y]);(!r||r.label!==b)&&(r=this.device.createRenderPipeline({label:b,vertex:{module:this.device.createShaderModule({code:t.material.vertex}),entryPoint:"main",buffers:i},fragment:{module:this.device.createShaderModule({code:t.material.fragment}),entryPoint:"main",targets:Array(m).fill({format:this.format,blend:t.material.blending,writeMask:Ie})},primitive:{frontFace:"ccw",cullMode:o,topology:d},depthStencil:{depthWriteEnabled:u,depthCompare:g,format:"depth24plus-stencil8"},multisample:{count:y},layout:"auto"})),this._p.set(t,r)}this.a.setPipeline(r);let a=0;for(const c in t.geometry.attributes){const o=t.geometry.attributes[c],d=c==="index";let u=this._b.get(o);u||(u=this.i(o.data,d?Pe:Ce),this._b.set(o,u)),o.needsUpdate&&this.j(u,o.data),o.needsUpdate=!1,this.a instanceof GPURenderPassEncoder&&(d?this.a.setIndexBuffer(u,`uint${o.data.BYTES_PER_ELEMENT*8}`):this.a.setVertexBuffer(a++,u))}this._g.get(t.geometry)||this._g.set(t.geometry,!0,()=>{for(const c in t.geometry.attributes){const o=t.geometry.attributes[c];this._b.get(o)?.destroy(),this._b.delete(o),o.needsUpdate=!0}});let n=0;const l=[],h=t.material.compute?.matchAll(Oe);if(h)for(const[,c]of h){const o=t.geometry.attributes[c],d=this._b.get(o);l.push({binding:n++,resource:{buffer:d}})}const p=wt(t.material.vertex,t.material.fragment,t.material.compute);if(p){const c=p.map(d=>t.material.uniforms[d]);let o=this._u.get(t);if(o)this.j(o.buffer,bt(c,o.data));else{const d=bt(c),u=this.i(d,Le);o={data:d,buffer:u},this._u.set(t,o),this._u.set(t.material,o,()=>{this._u.delete(t),u.destroy()})}l.push({binding:n++,resource:o})}for(const c in t.material.uniforms){const o=t.material.uniforms[c];if(o instanceof q){this.k(o);const d=this._s.get(o.sampler);d&&l.push({binding:n++,resource:d});const u=this._t.get(o);l.push({binding:n++,resource:u instanceof GPUTexture?u.createView?.():u})}}if(l.length){const c=this.device.createBindGroup({layout:r.getBindGroupLayout(0),entries:l});this.a.setBindGroup(0,c)}}sort(t,e){const i=[];return e?.matrixAutoUpdate&&(e.projectionViewMatrix.copy(e.projectionMatrix).multiply(e.viewMatrix),e.frustum.fromMatrix4(e.projectionViewMatrix),e.frustum.normalNDC()),t.traverse(s=>{if(!s.visible)return!0;if(s instanceof Q){if(e&&s.frustumCulled&&!e.frustum.contains(s))return!0;i.push(s)}}),i.sort((s,r)=>r.material.depthTest-s.material.depthTest||!!e&&this._v.set(r.matrix[12],r.matrix[13],r.matrix[14]).applyMatrix4(e.projectionViewMatrix).z-this._v.set(s.matrix[12],s.matrix[13],s.matrix[14]).applyMatrix4(e.projectionViewMatrix).z||s.material.transparent-r.material.transparent)}render(t,e){let i=this._f.get(this.b);if(this.b&&(!i||this.b.needsUpdate)){i?.depthTexture.destroy();const p=this.b.textures.map(d=>(this.k(d,this.b.width,this.b.height),this._t.get(d).createView())),c=this.device.createTexture({size:[this.b.width,this.b.height,1],format:"depth24plus-stencil8",usage:tt}),o=c.createView();i={views:p,depthTexture:c,depthTextureView:o},this._f.set(this.b,i,()=>c.destroy())}const s=this.samples;i?this.samples=1:this.d.sampleCount!==s&&this.f();const r=i?.views??[this.g],a=i?void 0:this.context.getCurrentTexture().createView(),n=this.autoClear?"clear":"load",l="store";this.c=this.device.createCommandEncoder(),this.a=this.c.beginRenderPass({colorAttachments:r.map(p=>({view:p,resolveTarget:a,loadOp:n,storeOp:l})),depthStencilAttachment:{view:i?.depthTextureView??this.h,depthClearValue:1,depthLoadOp:n,depthStoreOp:l,stencilClearValue:0,stencilLoadOp:n,stencilStoreOp:l}}),this.b?this.a.setViewport(0,0,this.b.width,this.b.height,0,1):this.a.setViewport(0,0,this.canvas.width,this.canvas.height,0,1),t.updateMatrix(),e?.updateMatrix(),e?.matrixAutoUpdate&&e.projectionViewMatrix.normalNDC();const h=this.sort(t,e);for(const p of h){this.compile(p,e);const{index:c,position:o}=p.geometry.attributes,{start:d,count:u}=p.geometry.drawRange;c?this.a.drawIndexed(k(u,c.data.length/c.size),p.instances,d):o?this.a.draw(k(u,o.data.length/o.size),p.instances,d):this.a.draw(3,p.instances)}this.a.end(),this.device.queue.submit([this.c.finish()]),i&&(this.samples=s)}compute(t){this.c=this.device.createCommandEncoder(),this.a=this.c.beginComputePass();const e=JSON.parse(`[${t.material.compute.match(De)[1]}]`);this.compile(t),this.a.dispatchWorkgroups(...e),this.a.end(),this.device.queue.submit([this.c.finish()])}}export{J as Camera,it as Frustum,rt as Geometry,at as Material,P as Matrix4,Q as Mesh,H as Object3D,Tt as OrthographicCamera,At as PerspectiveCamera,st as Quaternion,Et as RenderTarget,K as Sampler,q as Texture,F as Vector3,Ve as WebGLRenderer,Ge as WebGPURenderer};
//# sourceMappingURL=index.js.map
