(function(M,Y){typeof exports=="object"&&typeof module<"u"?Y(exports):typeof define=="function"&&define.amd?define(["exports"],Y):(M=typeof globalThis<"u"?globalThis:M||self,Y(M.skewed={}))})(this,function(M){"use strict";const Y={set(t){return this.x=t.x,this.y=t.y,this.z=t.z,this},setX(t){return this.x=t,this},setY(t){return this.y=t,this},setZ(t){return this.z=t,this},add(t){return this.x+=t.x,this.y+=t.y,this.z+=t.z,this},subtract(t){return this.x-=t.x,this.y-=t.y,this.z-=t.z,this},multiply(t){return this.x*=t,this.y*=t,this.z*=t,this},clone(){return K(this.x,this.y,this.z)},normalize(){let t=Math.sqrt(this.x*this.x+this.y*this.y+this.z*this.z);return t===0&&(t=1e-7),this.x/=t,this.y/=t,this.z/=t,this},length(){return Math.sqrt(this.x*this.x+this.y*this.y+this.z*this.z)},lengthSquared(){return this.x*this.x+this.y*this.y+this.z*this.z},dotProduct(t){return this.x*t.x+this.y*t.y+this.z*t.z},crossProduct(t){const e=this.x,n=this.y,i=this.z,o=t.x,s=t.y,a=t.z;return this.x=n*a-i*s,this.y=i*o-e*a,this.z=e*s-n*o,this}};function K(t,e,n){return Object.assign(Object.create(Y),{x:t,y:e,z:n})}function h(t,e,n){if(typeof t=="object")return Array.isArray(t)?K(t[0],t[1],t[2]):K(t.x,t.y,t.z);if(typeof t=="number"&&typeof e=="number"&&typeof n=="number")return K(t,e,n);throw new Error("Invalid arguments to Vector3 factory")}h.isVector3=function(t){return t.constructor===Y.constructor},h.Zero=function(){return h(0,0,0)},h.Up=function(){return h(0,1,0)},h.Down=function(){return h(0,-1,0)},h.Left=function(){return h(-1,0,0)},h.Right=function(){return h(1,0,0)},h.Forward=function(){return h(0,0,-1)},h.Backward=function(){return h(0,0,1)};function D(t,e,n,i,o,s,a,d,u,c,f,g,r,m,p,y){if(arguments.length===0)return D.identity();if(arguments.length===16)return wt(t,e,n,i,o,s,a,d,u,c,f,g,r,m,p,y);throw new Error("Invalid arguments to Matrix4x4 constructor")}function wt(t,e,n,i,o,s,a,d,u,c,f,g,r,m,p,y){return Object.assign(Object.create($t),{elements:[t,e,n,i,o,s,a,d,u,c,f,g,r,m,p,y],toString(){console.log(`Matrix4x4( ${this.elements[0]}, ${this.elements[1]}, ${this.elements[2]}, ${this.elements[3]}, ${this.elements[4]}, ${this.elements[5]}, ${this.elements[6]}, ${this.elements[7]}, ${this.elements[8]}, ${this.elements[9]}, ${this.elements[10]}, ${this.elements[11]}, ${this.elements[12]}, ${this.elements[13]}, ${this.elements[14]}, ${this.elements[15]} )`)}})}const $t={set(t,e,n,i,o,s,a,d,u,c,f,g,r,m,p,y){const l=this.elements;return l[0]=t,l[4]=e,l[8]=n,l[12]=i,l[1]=o,l[5]=s,l[9]=a,l[13]=d,l[2]=u,l[6]=c,l[10]=f,l[14]=g,l[3]=r,l[7]=m,l[11]=p,l[15]=y,this},identity(){return this.set(1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1),this},clone(){return D(1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1).fromArray(this.elements,0)},copy(t){const e=this.elements,n=t.elements;return e[0]=n[0],e[1]=n[1],e[2]=n[2],e[3]=n[3],e[4]=n[4],e[5]=n[5],e[6]=n[6],e[7]=n[7],e[8]=n[8],e[9]=n[9],e[10]=n[10],e[11]=n[11],e[12]=n[12],e[13]=n[13],e[14]=n[14],e[15]=n[15],this},copyTranslationMatrix(t){const e=this.elements,n=t.elements;return e[12]=n[12],e[13]=n[13],e[14]=n[14],this},getTranslation(){const t=this.elements;return h(t[12],t[13],t[14])},getScale(){const t=this.elements;return h(h(t[0],t[4],t[8]).length(),h(t[1],t[5],t[9]).length(),h(t[2],t[6],t[10]).length())},extractBasis(t,e,n){return t!==void 0&&q(t,this.elements,0),e!==void 0&&q(e,this.elements,4),n!==void 0&&q(n,this.elements,8),this},makeBasis(t,e,n){return this.set(t.x,e.x,n.x,0,t.y,e.y,n.y,0,t.z,e.z,n.z,0,0,0,0,1),this},extractRotation(){const t=D(),e=t.elements,n=this.elements,i=1/q(it,n,0).length(),o=1/q(it,n,4).length(),s=1/q(it,n,8).length();return e[0]=n[0]*i,e[1]=n[1]*i,e[2]=n[2]*i,e[3]=0,e[4]=n[4]*o,e[5]=n[5]*o,e[6]=n[6]*o,e[7]=0,e[8]=n[8]*s,e[9]=n[9]*s,e[10]=n[10]*s,e[11]=0,e[12]=0,e[13]=0,e[14]=0,e[15]=1,t},lookAt(t,e,n){const i=this.elements;return R.set(t).subtract(e),R.lengthSquared()===0&&(R.z=1),R.normalize(),B.set(n).crossProduct(R),B.lengthSquared()===0&&(Math.abs(n.z)===1?R.x+=1e-4:R.z+=1e-4,R.normalize(),B.set(n).crossProduct(R)),B.normalize(),tt.set(R).crossProduct(B),i[0]=B.x,i[4]=tt.x,i[8]=R.x,i[1]=B.y,i[5]=tt.y,i[9]=R.y,i[2]=B.z,i[6]=tt.z,i[10]=R.z,this},multiply(t){return this.multiplyMatrices(this,t)},premultiply(t){return this.multiplyMatrices(t,this)},multiplyMatrices(t,e){const n=t.elements,i=e.elements,o=this.elements,s=n[0],a=n[4],d=n[8],u=n[12],c=n[1],f=n[5],g=n[9],r=n[13],m=n[2],p=n[6],y=n[10],l=n[14],S=n[3],b=n[7],P=n[11],C=n[15],w=i[0],x=i[4],L=i[8],T=i[12],U=i[1],E=i[5],I=i[9],z=i[13],$=i[2],k=i[6],A=i[10],W=i[14],V=i[3],v=i[7],N=i[11],Z=i[15];return o[0]=s*w+a*U+d*$+u*V,o[4]=s*x+a*E+d*k+u*v,o[8]=s*L+a*I+d*A+u*N,o[12]=s*T+a*z+d*W+u*Z,o[1]=c*w+f*U+g*$+r*V,o[5]=c*x+f*E+g*k+r*v,o[9]=c*L+f*I+g*A+r*N,o[13]=c*T+f*z+g*W+r*Z,o[2]=m*w+p*U+y*$+l*V,o[6]=m*x+p*E+y*k+l*v,o[10]=m*L+p*I+y*A+l*N,o[14]=m*T+p*z+y*W+l*Z,o[3]=S*w+b*U+P*$+C*V,o[7]=S*x+b*E+P*k+C*v,o[11]=S*L+b*I+P*A+C*N,o[15]=S*T+b*z+P*W+C*Z,this},determinant(){const t=this.elements,e=t[0],n=t[4],i=t[8],o=t[12],s=t[1],a=t[5],d=t[9],u=t[13],c=t[2],f=t[6],g=t[10],r=t[14],m=t[3],p=t[7],y=t[11],l=t[15];return m*(+o*d*f-i*u*f-o*a*g+n*u*g+i*a*r-n*d*r)+p*(+e*d*r-e*u*g+o*s*g-i*s*r+i*u*c-o*d*c)+y*(+e*u*f-e*a*r-o*s*f+n*s*r+o*a*c-n*u*c)+l*(-i*a*c-e*d*f+e*a*g+i*s*f-n*s*g+n*d*c)},transpose(){const t=this.elements;let e;return e=t[1],t[1]=t[4],t[4]=e,e=t[2],t[2]=t[8],t[8]=e,e=t[6],t[6]=t[9],t[9]=e,e=t[3],t[3]=t[12],t[12]=e,e=t[7],t[7]=t[13],t[13]=e,e=t[11],t[11]=t[14],t[14]=e,this},setPosition(t,e,n){const i=this.elements;return i[12]=t,i[13]=e,i[14]=n,this},invert(){const t=this.elements,e=t[0],n=t[1],i=t[2],o=t[3],s=t[4],a=t[5],d=t[6],u=t[7],c=t[8],f=t[9],g=t[10],r=t[11],m=t[12],p=t[13],y=t[14],l=t[15],S=f*y*u-p*g*u+p*d*r-a*y*r-f*d*l+a*g*l,b=m*g*u-c*y*u-m*d*r+s*y*r+c*d*l-s*g*l,P=c*p*u-m*f*u+m*a*r-s*p*r-c*a*l+s*f*l,C=m*f*d-c*p*d-m*a*g+s*p*g+c*a*y-s*f*y,w=e*S+n*b+i*P+o*C;if(w===0)return this.set(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);const x=1/w;return t[0]=S*x,t[1]=(p*g*o-f*y*o-p*i*r+n*y*r+f*i*l-n*g*l)*x,t[2]=(a*y*o-p*d*o+p*i*u-n*y*u-a*i*l+n*d*l)*x,t[3]=(f*d*o-a*g*o-f*i*u+n*g*u+a*i*r-n*d*r)*x,t[4]=b*x,t[5]=(c*y*o-m*g*o+m*i*r-e*y*r-c*i*l+e*g*l)*x,t[6]=(m*d*o-s*y*o-m*i*u+e*y*u+s*i*l-e*d*l)*x,t[7]=(s*g*o-c*d*o+c*i*u-e*g*u-s*i*r+e*d*r)*x,t[8]=P*x,t[9]=(m*f*o-c*p*o-m*n*r+e*p*r+c*n*l-e*f*l)*x,t[10]=(s*p*o-m*a*o+m*n*u-e*p*u-s*n*l+e*a*l)*x,t[11]=(c*a*o-s*f*o-c*n*u+e*f*u+s*n*r-e*a*r)*x,t[12]=C*x,t[13]=(c*p*i-m*f*i+m*n*g-e*p*g-c*n*y+e*f*y)*x,t[14]=(m*a*i-s*p*i-m*n*d+e*p*d+s*n*y-e*a*y)*x,t[15]=(s*f*i-c*a*i+c*n*d-e*f*d-s*n*g+e*a*g)*x,this},scale(t){const e=this.elements,n=t.x,i=t.y,o=t.z;return e[0]*=n,e[4]*=i,e[8]*=o,e[1]*=n,e[5]*=i,e[9]*=o,e[2]*=n,e[6]*=i,e[10]*=o,e[3]*=n,e[7]*=i,e[11]*=o,this},applyToVector3(t){const e=t.x,n=t.y,i=t.z,o=this.elements,s=1/(o[3]*e+o[7]*n+o[11]*i+o[15]);t.x=(o[0]*e+o[4]*n+o[8]*i+o[12])*s,t.y=(o[1]*e+o[5]*n+o[9]*i+o[13])*s,t.z=(o[2]*e+o[6]*n+o[10]*i+o[14])*s},makeTranslation(t,e,n){return this.set(1,0,0,t,0,1,0,e,0,0,1,n,0,0,0,1),this},makeRotationX(t){const e=Math.cos(t),n=Math.sin(t);return this.set(1,0,0,0,0,e,-n,0,0,n,e,0,0,0,0,1),this},makeRotationY(t){const e=Math.cos(t),n=Math.sin(t);return this.set(e,0,n,0,0,1,0,0,-n,0,e,0,0,0,0,1),this},makeRotationZ(t){const e=Math.cos(t),n=Math.sin(t);return this.set(e,-n,0,0,n,e,0,0,0,0,1,0,0,0,0,1),this},makeScale(t,e,n){return this.set(t,0,0,0,0,e,0,0,0,0,n,0,0,0,0,1),this},makeShear(t,e,n,i,o,s){return this.set(1,n,o,0,t,1,s,0,e,i,1,0,0,0,0,1),this},makeOrthographic(t,e,n,i,o,s){const a=this.elements,d=1/(e-t),u=1/(n-i),c=1/(s-o),f=(e+t)*d,g=(n+i)*u;let r,m;return r=(s+o)*c,m=-2*c,a[0]=2*d,a[4]=0,a[8]=0,a[12]=-f,a[1]=0,a[5]=2*u,a[9]=0,a[13]=-g,a[2]=0,a[6]=0,a[10]=m,a[14]=-r,a[3]=0,a[7]=0,a[11]=0,a[15]=1,this},equals(t){const e=this.elements,n=t.elements;for(let i=0;i<16;i++)if(e[i]!==n[i])return!1;return!0},fromArray(t,e=0){for(let n=0;n<16;n++)this.elements[n]=t[n+e];return this}};D.identity=function(){return D(1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1)};function q(t,e,n){return t.x=e[n],t.y=e[n+1],t.z=e[n+2],t}const it=h.Zero(),B=h.Zero(),tt=h.Zero(),R=h.Zero(),kt={};function At(t){return lt(t||D.identity(),D.identity())}function lt(t,e){return Object.assign(Object.create(kt),{matrix:t,projectionMatrix:e})}function zt(t,e,n,i){return{x:(t-n)*Math.cos(Math.PI/6)+i.width/2,y:(t+n)*Math.sin(Math.PI/6)-e+i.height/2}}function Pt(t,e,n,i){let o=Math.PI/4,s=.5;return{x:t+s*n*Math.cos(o)+i.width/2,y:e+s*n*Math.sin(o)+i.height/2}}function H(t,e,n){const i=h(t);return e.applyToVector3(i),i.x=i.x*n.width/2+n.width,i.y=i.y*n.height/2,i}function F(t,e,n,i=1){return{r:t,g:e,b:n,a:i}}const Ct=F(255,0,0),It=F(0,255,0),Dt=F(0,0,255);function O(t){return t.a<1?`rgba(${Math.floor(t.r)},${Math.floor(t.g)},${Math.floor(t.b)},${t.a.toFixed(3)})`:`rgb(${Math.floor(t.r)},${Math.floor(t.g)},${Math.floor(t.b)})`}function j(t,e,n,i){i=Math.min(1,Math.max(0,i));const o=F(t.r/255*i,t.g/255*i,t.b/255*i),s=F(n.r/255,n.g/255,n.b/255),a=F(Math.floor(Math.min(255,e.r*o.r+e.r*s.r)),Math.floor(Math.min(255,e.g*o.g+e.g*s.g)),Math.floor(Math.min(255,e.b*o.b+e.b*s.b)),e.a);return O(a)}const Lt=!1;function et(t){return Math.acos(t)*(180/Math.PI)}function ot(t,e){return(Math.atan2(e,t)*(180/Math.PI)+360)%360}globalThis.calculateRotationAngle=ot;function Tt(t,e){return(ot(t,-e)+90)%360}globalThis.calculateCycleAngle=Tt;function Et(t,e,n,i,o,s,a,d,u){const c=t.directionalLight.direction.clone();d.extractRotation().applyToVector3(c);let f=ot(-c.x,-c.y);const g=c.multiply(-1),r=h(0,0,1).dotProduct(g),m=h(0,0,-1).dotProduct(g);let p;r>0?(p=et(r),Rt(t,e,n,i,o,s,a,u,p,f)):(p=90-et(m),Ft(t,e,n,i,o,s,a,u,p,f)),i.id&&Lt&&console.log(`id: ${i.id}, lightSideDotProduct: ${r}, lightSideDotProductDegrees: ${et(r)}, darkSideLightProduct: ${m}, darkSideLightProductDegrees: ${90-et(m)}, cycleAngle: ${p}, rotationAngle: ${f}, directionalLightInCameraSpace.x = ${c.x.toFixed(1)}, directionalLightInCameraSpace.y = ${c.y.toFixed(1)}, directionalLightInCameraSpace.z = ${c.z.toFixed(1)} scene.directionalLight.direction.x = ${t.directionalLight.direction.x.toFixed(1)}, scene.directionalLight.direction.y = ${t.directionalLight.direction.y.toFixed(1)}, scene.directionalLight.direction.z = ${t.directionalLight.direction.z.toFixed(1)} `)}function Rt(t,e,n,i,o,s,a,d,u,c){const g=s.getScale().x*a,r=i.radius*g,m=r,p=crypto.randomUUID(),y=0,l=[];l.push({offset:0,stopColor:"red"}),l.push({offset:y,stopColor:"red"});for(let A=0;A<=m;A++){const V=A/m*Math.PI/2,v=Math.cos(V),N=Math.sin(V);l.push({offset:N*(1-y)+y,stopColor:j(t.directionalLight.color,i.fill,t.ambientLightColor,v)})}const S=Math.sin(u/180*Math.PI)*r,b=Math.cos(-c/180*Math.PI)*S+r,P=Math.sin(-c/180*Math.PI)*S+r,{x:C,y:w}=H(s.getTranslation(),d,o),x=Math.sin((u-90)/180*Math.PI)*r,L={x:b+r+C-r*2,y:P+r+w-r*2},T=S-x,U=ht(T,-S,r),E={x:T,y:U},I=p+"-fill",z=`url(#${I})`,$=document.createElementNS("http://www.w3.org/2000/svg","circle");$.id="sphere",$.setAttribute("cx",C.toString()),$.setAttribute("cy",w.toString()),$.setAttribute("r",r.toString()),$.setAttribute("fill",z),i.strokeWidth&&i.stroke.a>0&&($.setAttribute("stroke",O(i.stroke)),i.strokeWidth!==1&&$.setAttribute("stroke-width",(i.strokeWidth*g).toString()));const k=document.createElementNS("http://www.w3.org/2000/svg","radialGradient");k.setAttribute("id",I),k.setAttribute("cx","0"),k.setAttribute("cy","0"),k.setAttribute("r","1"),k.setAttribute("gradientUnits","userSpaceOnUse"),k.setAttribute("gradientTransform",`translate(${L.x} ${L.y}) rotate(${-c}) scale(${E.x} ${E.y})`);for(let A of l){const W=document.createElementNS("http://www.w3.org/2000/svg","stop");W.setAttribute("offset",A.offset.toString()),W.setAttribute("stop-color",A.stopColor),k.appendChild(W)}n.appendChild(k),e.appendChild($)}function ht(t,e,n){let i=1-e*e/(t*t);return i===0&&(i=1e-4),Math.sqrt(n*n/i)}function ut(t,e){return{x:Math.cos(e/180*Math.PI)*t,y:-Math.sin(e/180*Math.PI)*t}}window.rotate=ut;function Ft(t,e,n,i,o,s,a,d,u,c){const g=s.getScale().x*a,r=i.radius*g,m=r,p=crypto.randomUUID(),y=0,l=[];l.push({offset:0,stopColor:"red"}),l.push({offset:y,stopColor:"red"});const S=j(t.directionalLight.color,i.fill,t.ambientLightColor,0);l.push({offset:y,stopColor:S}),l.push({offset:.5,stopColor:S});for(let k=0;k<=m;k++){const A=k/m,W=A*Math.PI/2;let V=Math.max(0,Math.sin(W));const v=Math.cos(A*Math.PI/2+Math.PI)+1;l.push({offset:v/2+.5,stopColor:j(t.directionalLight.color,i.fill,t.ambientLightColor,V)})}const{x:b,y:P}=H(s.getTranslation(),d,o),C=Math.cos((u+180)/180*Math.PI)*r,w=ut(C,c),L=Math.sin(u/180*Math.PI)*r-C,T=ht(L,C,r),U={x:L,y:T},E=p+"-fill",I=`url(#${E})`,z=document.createElementNS("http://www.w3.org/2000/svg","circle");z.id="sphere",z.setAttribute("cx",b.toString()),z.setAttribute("cy",P.toString()),z.setAttribute("r",r.toString()),z.setAttribute("fill",I),i.strokeWidth&&i.stroke.a>0&&(z.setAttribute("stroke",O(i.stroke)),i.strokeWidth!==1&&z.setAttribute("stroke-width",(i.strokeWidth*g).toString()));const $=document.createElementNS("http://www.w3.org/2000/svg","radialGradient");$.setAttribute("id",E),$.setAttribute("cx","0"),$.setAttribute("cy","0"),$.setAttribute("r","1"),$.setAttribute("gradientUnits","userSpaceOnUse"),$.setAttribute("gradientTransform",`translate(${w.x+b} ${w.y+P}) rotate(${-c}) scale(${U.x*2} ${U.y*2})`);for(let k of l){const A=document.createElementNS("http://www.w3.org/2000/svg","stop");A.setAttribute("offset",k.offset.toString()),A.setAttribute("stop-color",k.stopColor),$.appendChild(A)}n.appendChild($),e.appendChild(z)}const Wt=.5;function Ut(t,e,n,i,o,s,a,d,u,c){const f=[h(0,i.height/2,0),h(0,-i.height/2,0)].map(Q=>(s.applyToVector3(Q),H(Q,c,o))),g=h(0,0,0);s.extractBasis(void 0,g,void 0);const r=g.clone();u.extractRotation().applyToVector3(r);const m=r.dotProduct(h(0,0,1)),p=Math.abs(m),y=m>0,S=s.getScale().x*a,b=i.radius*S,P=b*p,C=y?g.clone():g.clone().multiply(-1),w=y?r.clone():r.clone().multiply(-1),x=h(w.x,-w.y,0).normalize(),L=y?f[0]:f[1],T=y?f[1]:f[0],U=h(-x.y,x.x,0),E=h(x.y,-x.x,0),I=U.clone().multiply(b).add(L),z=E.clone().multiply(b).add(L),$=U.clone().multiply(b).add(T),k=E.clone().multiply(b).add(T),A=Vt(E.x,E.y),W=0,V=0,v=t.directionalLight.direction.clone().multiply(-1),N=document.createElementNS("http://www.w3.org/2000/svg","path");N.setAttribute("id",y?"cylinder-top":"cylinder-bottom");const Z=j(t.directionalLight.color,i.fill,t.ambientLightColor,v.dotProduct(C.clone()));N.setAttribute("fill",Z),dt(N,i,S,Z),e.appendChild(N),N.setAttribute("d",` M ${I.x} ${I.y} A ${b} ${P} ${A} ${W} ${V} ${z.x} ${z.y} A ${b} ${P} ${A} ${1} ${V} ${I.x} ${I.y}`);const _=document.createElementNS("http://www.w3.org/2000/svg","path");_.setAttribute("id","cylinder-tube"),_.setAttribute("fill","purple"),_.setAttribute("d",` M ${I.x} ${I.y} A ${b} ${P} ${A} 0 1 ${z.x} ${z.y} L ${k.x} ${k.y} A ${b} ${P} ${A} 0 0 ${$.x} ${$.y} Z `),dt(_,i,S),e.appendChild(_);const pt=v.clone();u.extractRotation().applyToVector3(pt);const yt=crypto.randomUUID()+"-fill",ie=`url(#${yt})`;_.setAttribute("fill",ie);const G=document.createElementNS("http://www.w3.org/2000/svg","linearGradient");G.setAttribute("id",yt),G.setAttribute("gradientUnits","userSpaceOnUse");const bt=I.clone().add($).multiply(.5),St=z.clone().add(k).multiply(.5);G.setAttribute("x1",bt.x.toString()),G.setAttribute("y1",bt.y.toString()),G.setAttribute("x2",St.x.toString()),G.setAttribute("y2",St.y.toString());const oe=h(0,0,1).crossProduct(w).normalize(),se=D().lookAt(h(0,0,0),oe,w),xt=Math.max(2,Math.min(255,Math.floor(b)));for(let Q=0;Q0?(t.setAttribute("stroke",O(e.stroke)),e.strokeWidth!==1&&t.setAttribute("stroke-width",(e.strokeWidth*n).toString())):i!==void 0&&(t.setAttribute("stroke",i),t.setAttribute("stroke-width",(Wt*n).toString()))}function Vt(t,e){return Math.atan2(e,t)/Math.PI*180}const Nt=.5;function vt(t,e,n,i){var y;const o=i.matrix.clone().invert(),s=i.projectionMatrix.clone().multiply(o),a=Ot(i.projectionMatrix),d=n.width/a.width,u=gt(e.shapes);t.innerHTML="";var c=document.createElementNS("http://www.w3.org/2000/svg","svg");c.id="scene",c.setAttribute("viewBox",`0 0 ${n.width.toString()} ${n.height.toString()}`);const f=document.createElementNS("http://www.w3.org/2000/svg","defs");c.appendChild(f);const g=i.matrix.extractRotation(),r=h(0,0,0);g.extractBasis(h(0,0,0),h(0,0,0),r);const p=ft(e.shapes).map(l=>{var S;return{shape:l,position:((S=u.get(l.shape))==null?void 0:S.getTranslation())||h(0,0,0)}});p.sort((l,S)=>{const b=l.shape.sortCategory==="background"?-1e3:r.dotProduct(l.position),P=S.shape.sortCategory==="background"?-1e3:r.dotProduct(S.position);return b-P});for(let l of p){const S=l.shape.shape,b=u.get(S);if(b===void 0)throw new Error("World transform is undefined");switch(S.type){case"mesh":Gt(e,c,S,n,b,d,s,r);break;case"sphere":Et(e,c,f,S,n,b,d,o,s);break;case"cylinder":Ut(e,c,f,S,n,b,d,r,o,s);break;default:throw new Error(`Unknown shape type: ${S.type}`)}}(y=c.debugQueue)==null||y.forEach(l=>{c.appendChild(l)}),t.appendChild(c)}function gt(t,e=void 0,n=void 0){n=n||new Map,e=e||D();for(let i of t){const o=Bt(i);if(i.type==="group"||i.type==="grid"){const s=o.clone().premultiply(e);gt(i.children,s,n)}else{const s=o.clone().premultiply(e);n.set(i,s)}}return n}function ft(t,e=[],n="default"){for(let i of t){const o=n==="background"||i.id==="background"||i.type==="grid";i.type==="group"||i.type==="grid"?ft(i.children,e,o?"background":"default"):e.push({shape:i,sortCategory:o?"background":"default"})}return e}function Bt(t){const e=D().makeTranslation(t.position.x,t.position.y,t.position.z),n=D().makeScale(t.scale,t.scale,t.scale),i=D().makeRotationX(t.rotation.x/180*Math.PI),o=D().makeRotationY(t.rotation.y/180*Math.PI),s=D().makeRotationZ(t.rotation.z/180*Math.PI);return e.multiply(n).multiply(s).multiply(o).multiply(i)}function Gt(t,e,n,i,o,s,a,d){const u=o.extractRotation().invert(),c=d.clone();u.applyToVector3(c);const f=t.directionalLight.direction.clone().multiply(-1);u.applyToVector3(f);const g=n.mesh.vertices.map(b=>(b=b.clone(),o.applyToVector3(b),H(b,a,i)));let r=1/0,m=-1/0,p=-1/0,y=-1/0;g.forEach(b=>{r=Math.min(r,b.x),m=Math.max(m,b.x),p=Math.max(p,b.y),y=Math.min(y,b.y)});const l=document.createElementNS("http://www.w3.org/2000/svg","g");l.setAttribute("transform",`translate(${r},${p})`),l.id=n.id;const S=d.clone();o.clone().invert().applyToVector3(S);for(let b of n.mesh.faces){if(c.dotProduct(b.normal)<0)continue;let C="";b.indices.forEach(T=>{C+=`${g[T].x-r},${g[T].y-p} `});const w=document.createElementNS("http://www.w3.org/2000/svg","polygon");w.setAttribute("points",C);const x=f.dotProduct(b.normal),L=j(t.directionalLight.color,n.fill,t.ambientLightColor,x);w.setAttribute("fill",L),w.setAttribute("stroke-linejoin","round"),n.strokeWidth>0&&n.stroke.a>0?(w.setAttribute("stroke",O(n.stroke)),w.setAttribute("stroke-width",(n.strokeWidth*s).toString())):(w.setAttribute("stroke",L),w.setAttribute("stroke-width",(Nt*s).toString())),l.appendChild(w),e.appendChild(l)}}function Ot(t){const e=t.elements,n=e[0],i=e[5],o=e[10],s=2/n,a=2/i,d=-2/o;return{width:s,height:a,depth:d}}function jt(t,e,n,i,o,s,a=F(0,0,0)){const d=e.width/2,u=e.height/2,c=document.createElementNS("http://www.w3.org/2000/svg","line");c.style.zIndex="1000",c.setAttribute("x1",(n+d).toFixed(2)),c.setAttribute("y1",(i+u).toFixed(2)),c.setAttribute("x2",(o+d).toFixed(2)),c.setAttribute("y2",(s+u).toFixed(2)),c.setAttribute("stroke",O(a)),t.debugQueue||(t.debugQueue=[]),t.debugQueue.push(c)}const Xt={direction:h(0,-1,0),color:F(255,255,255)};function Zt(t){return{type:"directional light",...Xt,...t}}const st=()=>({position:h(0,0,0),rotation:h(0,0,0),scale:1}),J=()=>({...st(),fill:F(128,128,128),stroke:F(0,0,0),strokeWidth:1,id:""}),X=100;function Yt(t,e){const n={vertices:[],faces:[]};return n.faces.push({indices:[],normal:h(0,1,0)}),n.faces.push({indices:[],normal:h(0,-1,0)}),n.vertices=n.vertices.concat(t.map(i=>h(i.x,i.y+e/2,i.z))),n.vertices=n.vertices.concat(t.map(i=>h(i.x,i.y-e/2,i.z))),t.forEach((i,o)=>{n.faces[0].indices.push(o),n.faces[1].indices.push(t.length+o);const s=(o+1)%t.length;n.faces.push({indices:[o,o+t.length,s+t.length,s],normal:qt(n.vertices[o],n.vertices[s])})}),n}function qt(t,e){const n=h(e.x-t.x,0,e.z-t.z);return n.normalize(),h(n.z,0,-n.x)}function mt(t,e,n){return Yt([h(-t/2,0,-n/2),h(t/2,0,-n/2),h(t/2,0,n/2),h(-t/2,0,n/2)],e)}const nt={width:X,height:X,depth:X,id:"box"};function rt(t){const e={width:t.width||nt.width,height:t.height||nt.height,depth:t.depth||nt.depth};return{type:"mesh",mesh:mt(e.width,e.height,e.depth),...J(),id:t.id||nt.id,...t}}const _t={cellCount:10,cellSize:100,...J()};function Qt(t){const e={type:"grid",children:[],position:h(0,0,0),scale:1,..._t,...t};for(let n=0;n<=e.cellCount;n++){const i=rt({position:h(0,0,n*e.cellSize-e.cellSize*e.cellCount/2),rotation:h(0,0,0),scale:1,width:e.cellCount*e.cellSize,height:t.strokeWidth,depth:t.strokeWidth,fill:t.stroke,stroke:F(0,0,0),strokeWidth:0});e.children.push(i);const o=rt({position:h(n*e.cellSize-e.cellSize*e.cellCount/2,0,0),rotation:h(0,0,0),scale:1,width:t.strokeWidth,height:t.strokeWidth,depth:e.cellCount*e.cellSize,fill:t.stroke,stroke:F(0,0,0),strokeWidth:0});e.children.push(o)}return e}const Ht={id:"",children:[]};function Jt(t){return{type:"group",...st(),...Ht,...t}}const Kt={radius:X/2,height:X,...J()};function te(t){return{type:"cylinder",...Kt,...t}}const ee={radius:X/2,...J()};function ne(t){return{type:"sphere",...ee,...t}}M.Blue=Dt,M.Box=rt,M.BoxMesh=mt,M.Camera=At,M.Color=F,M.ColorToCSS=O,M.Cylinder=te,M.DebugLine2D=jt,M.DefaultBasicShapeProperties=J,M.DefaultShapeDimension=X,M.DefaultTransformProperties=st,M.DirectionalLight=Zt,M.Green=It,M.Grid=Qt,M.Group=Jt,M.Matrix4x4=D,M.Red=Ct,M.Sphere=ne,M.Vector3=h,M.applyLighting=j,M.point3DToCabinet=Pt,M.point3DToIsometric=zt,M.projectToScreenCoordinate=H,M.render=vt,Object.defineProperty(M,Symbol.toStringTag,{value:"Module"})});