ecspresso 0.16.3 → 0.17.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.
@@ -1,4 +1,4 @@
1
- var i=((J)=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(J,{get:(K,Q)=>(typeof require<"u"?require:K)[Q]}):J)(function(J){if(typeof require<"u")return require.apply(this,arguments);throw Error('Dynamic require of "'+J+'" is not supported')});import{definePlugin as n}from"ecspresso";function B(J){let K=new Map,Q=new WeakMap,G=1;function V(O){let $=K.get(O);if($!==void 0)return $;if(G===0)throw Error(`[ecspresso] ${J} layer bitmask overflow: more than 32 distinct layers registered`);let q=G;return K.set(O,q),G<<=1,q}function j(O){let $=Q.get(O);if($!==void 0)return $;let q=0;for(let L=0;L<O.length;L++)q|=V(O[L]);return Q.set(O,q),q}return{getLayerBit:V,getCollidesWithMask:j}}var A={normalX:0,normalY:0,normalZ:0,depth:0},_=0,x=1,D=B("3D collision"),h=D.getLayerBit,u=D.getCollidesWithMask;function f(J,K,Q,G,V,j,O,$,q){if(J.entityId=K,J.layer=j,J.collidesWith=O,J.layerBit=h(j),J.collidesWithMask=u(O),$)return J.x=Q+($.offsetX??0),J.y=G+($.offsetY??0),J.z=V+($.offsetZ??0),J.shape=_,J.halfWidth=$.width/2,J.halfHeight=$.height/2,J.halfDepth=$.depth/2,J.radius=0,!0;if(q)return J.x=Q+(q.offsetX??0),J.y=G+(q.offsetY??0),J.z=V+(q.offsetZ??0),J.shape=x,J.halfWidth=0,J.halfHeight=0,J.halfDepth=0,J.radius=q.radius,!0;return!1}function b(J,K,Q,G,V,j,O,$,q,L,F,H,U){let T=O-J,N=$-K,P=q-Q,k=G+L-Math.abs(T),W=V+F-Math.abs(N),X=j+H-Math.abs(P);if(k<=0||W<=0||X<=0)return!1;if(k<=W&&k<=X)return U.normalX=T>=0?1:-1,U.normalY=0,U.normalZ=0,U.depth=k,!0;if(W<=X)return U.normalX=0,U.normalY=N>=0?1:-1,U.normalZ=0,U.depth=W,!0;return U.normalX=0,U.normalY=0,U.normalZ=P>=0?1:-1,U.depth=X,!0}function d(J,K,Q,G,V,j,O,$,q){let L=V-J,F=j-K,H=O-Q,U=L*L+F*F+H*H,T=G+$;if(U>=T*T)return!1;let N=Math.sqrt(U);if(N===0)return q.normalX=1,q.normalY=0,q.normalZ=0,q.depth=T,!0;return q.normalX=L/N,q.normalY=F/N,q.normalZ=H/N,q.depth=T-N,!0}function g(J,K,Q,G,V,j,O,$,q,L,F){let H=Math.max(J-G,Math.min(O,J+G)),U=Math.max(K-V,Math.min($,K+V)),T=Math.max(Q-j,Math.min(q,Q+j)),N=O-H,P=$-U,k=q-T,W=N*N+P*P+k*k;if(W>=L*L)return!1;if(W===0){let Z=O-(J-G),z=J+G-O,E=$-(K-V),R=K+V-$,Y=q-(Q-j),M=Q+j-q,C=Math.min(Z,z,E,R,Y,M);if(C===z)return F.normalX=1,F.normalY=0,F.normalZ=0,F.depth=z+L,!0;if(C===Z)return F.normalX=-1,F.normalY=0,F.normalZ=0,F.depth=Z+L,!0;if(C===R)return F.normalX=0,F.normalY=1,F.normalZ=0,F.depth=R+L,!0;if(C===E)return F.normalX=0,F.normalY=-1,F.normalZ=0,F.depth=E+L,!0;if(C===M)return F.normalX=0,F.normalY=0,F.normalZ=1,F.depth=M+L,!0;return F.normalX=0,F.normalY=0,F.normalZ=-1,F.depth=Y+L,!0}let X=Math.sqrt(W);return F.normalX=N/X,F.normalY=P/X,F.normalZ=k/X,F.depth=L-X,!0}function y(J,K,Q){if(J.shape===_&&K.shape===_)return b(J.x,J.y,J.z,J.halfWidth,J.halfHeight,J.halfDepth,K.x,K.y,K.z,K.halfWidth,K.halfHeight,K.halfDepth,Q);if(J.shape===x&&K.shape===x)return d(J.x,J.y,J.z,J.radius,K.x,K.y,K.z,K.radius,Q);if(J.shape===_&&K.shape===x)return g(J.x,J.y,J.z,J.halfWidth,J.halfHeight,J.halfDepth,K.x,K.y,K.z,K.radius,Q);if(!g(K.x,K.y,K.z,K.halfWidth,K.halfHeight,K.halfDepth,J.x,J.y,J.z,J.radius,Q))return!1;return Q.normalX=-Q.normalX,Q.normalY=-Q.normalY,Q.normalZ=-Q.normalZ,!0}var m=[],v=!1,r=50;function p(J,K,Q,G,V,j){if(G)c(J,K,Q,G,V,j);else l(J,K,V,j)}function l(J,K,Q,G){if(!v&&K>=r)v=!0,console.warn(`[ecspresso] 3D collision detection is using O(n²) brute force with ${K} colliders. For better performance, install createSpatialIndex3DPlugin() alongside your collision or physics3D plugin.`);for(let V=0;V<K;V++){let j=J[V];if(!j)continue;for(let O=V+1;O<K;O++){let $=J[O];if(!$)continue;if((j.collidesWithMask&$.layerBit|$.collidesWithMask&j.layerBit)===0)continue;if(!y(j,$,A))continue;Q(j,$,A,G)}}}function c(J,K,Q,G,V,j){Q.clear();for(let O=0;O<K;O++){let $=J[O];if(!$)continue;Q.set($.entityId,$)}for(let O=0;O<K;O++){let $=J[O];if(!$)continue;let q=$.shape===_?$.halfWidth:$.radius,L=$.shape===_?$.halfHeight:$.radius,F=$.shape===_?$.halfDepth:$.radius;m.length=0,G.queryBoxInto($.x-q,$.y-L,$.z-F,$.x+q,$.y+L,$.z+F,m,$.entityId);for(let H of m){let U=Q.get(H);if(!U)continue;if(($.collidesWithMask&U.layerBit|U.collidesWithMask&$.layerBit)===0)continue;if(!y($,U,A))continue;V($,U,A,j)}}}function QJ(J,K){return{rigidBody3D:{type:J,mass:J==="static"?1/0:K?.mass??1,drag:K?.drag??0,restitution:K?.restitution??0,friction:K?.friction??0,gravityScale:K?.gravityScale??1},force3D:{x:0,y:0,z:0}}}function $J(J,K,Q){return{force3D:{x:J,y:K,z:Q}}}function jJ(J,K,Q,G,V){let j=J.getComponent(K,"force3D");if(!j)return;j.x+=Q,j.y+=G,j.z+=V}function OJ(J,K,Q,G,V){let j=J.getComponent(K,"velocity3D"),O=J.getComponent(K,"rigidBody3D");if(!j||!O)return;if(O.mass===1/0||O.mass===0)return;j.x+=Q/O.mass,j.y+=G/O.mass,j.z+=V/O.mass}function UJ(J,K,Q,G,V){let j=J.getComponent(K,"velocity3D");if(!j)return;j.x=Q,j.y=G,j.z=V}var S={entityA:0,entityB:0,normalX:0,normalY:0,normalZ:0,depth:0};function s(J,K,Q,G){let V=J.rigidBody.type==="dynamic"&&J.rigidBody.mass>0&&J.rigidBody.mass!==1/0?1/J.rigidBody.mass:0,j=K.rigidBody.type==="dynamic"&&K.rigidBody.mass>0&&K.rigidBody.mass!==1/0?1/K.rigidBody.mass:0,O=V+j;if(O>0){let $=Q.depth/O;if(V>0){let U=G.getComponent(J.entityId,"localTransform3D");if(!U)return;let T=$*V;U.x-=T*Q.normalX,U.y-=T*Q.normalY,U.z-=T*Q.normalZ,J.x=U.x,J.y=U.y,J.z=U.z,G.markChanged(J.entityId,"localTransform3D")}if(j>0){let U=G.getComponent(K.entityId,"localTransform3D");if(!U)return;let T=$*j;U.x+=T*Q.normalX,U.y+=T*Q.normalY,U.z+=T*Q.normalZ,K.x=U.x,K.y=U.y,K.z=U.z,G.markChanged(K.entityId,"localTransform3D")}let q=K.velocity.x-J.velocity.x,L=K.velocity.y-J.velocity.y,F=K.velocity.z-J.velocity.z,H=q*Q.normalX+L*Q.normalY+F*Q.normalZ;if(H<0){let T=-(1+Math.min(J.rigidBody.restitution,K.rigidBody.restitution))*H/O,N=T*V,P=T*j;J.velocity.x-=N*Q.normalX,J.velocity.y-=N*Q.normalY,J.velocity.z-=N*Q.normalZ,K.velocity.x+=P*Q.normalX,K.velocity.y+=P*Q.normalY,K.velocity.z+=P*Q.normalZ;let k=q-H*Q.normalX,W=L-H*Q.normalY,X=F-H*Q.normalZ,Z=Math.sqrt(k*k+W*W+X*X);if(Z>0.000001){let z=k/Z,E=W/Z,R=X/Z,M=Math.sqrt(J.rigidBody.friction*K.rigidBody.friction)*Math.abs(T),C=Math.min(Z/O,M),w=C*V,I=C*j;J.velocity.x+=w*z,J.velocity.y+=w*E,J.velocity.z+=w*R,K.velocity.x-=I*z,K.velocity.y-=I*E,K.velocity.z-=I*R}}G.markChanged(J.entityId,"velocity3D"),G.markChanged(K.entityId,"velocity3D")}S.entityA=J.entityId,S.entityB=K.entityId,S.normalX=Q.normalX,S.normalY=Q.normalY,S.normalZ=Q.normalZ,S.depth=Q.depth,G.eventBus.publish("physics3DCollision",S)}function FJ(J){let{gravity:K={x:0,y:0,z:0},systemGroup:Q="physics3D",collisionSystemGroup:G,integrationPriority:V=1000,collisionPriority:j=900,phase:O="fixedUpdate"}=J??{};return n("physics3D").withComponentTypes().withEventTypes().withResourceTypes().withLabels().withGroups().requires().install(($)=>{$.registerRequired("rigidBody3D","velocity3D",()=>({x:0,y:0,z:0})),$.registerRequired("rigidBody3D","force3D",()=>({x:0,y:0,z:0})),$.addResource("physics3DConfig",{gravity:{x:K.x,y:K.y,z:K.z}}),$.addSystem("physics3D-integration").setPriority(V).inPhase(O).inGroup(Q).addQuery("bodies",{with:["localTransform3D","velocity3D","rigidBody3D","force3D"]}).setProcess(({queries:T,dt:N,ecs:P})=>{let{gravity:k}=P.getResource("physics3DConfig"),W=k.x,X=k.y,Z=k.z;for(let z of T.bodies){let{localTransform3D:E,velocity3D:R,rigidBody3D:Y,force3D:M}=z.components;if(Y.type==="static")continue;if(Y.type==="dynamic"){let C=Y.gravityScale*N;R.x+=W*C,R.y+=X*C,R.z+=Z*C;let w=Y.mass;if(w>0&&w!==1/0){let I=N/w;R.x+=M.x*I,R.y+=M.y*I,R.z+=M.z*I}if(Y.drag>0){let I=Math.max(0,1-Y.drag*N);R.x*=I,R.y*=I,R.z*=I}}E.x+=R.x*N,E.y+=R.y*N,E.z+=R.z*N,M.x=0,M.y=0,M.z=0,P.markChanged(z.id,"localTransform3D")}});let q=$.addSystem("physics3D-collision").setPriority(j).inPhase(O).inGroup(Q);if(G)q.inGroup(G);let L=[],F=new Map,H,U=!1;q.addQuery("collidables",{with:["localTransform3D","rigidBody3D","velocity3D","collisionLayer"]}).setProcess(({queries:T,ecs:N})=>{let P=0;for(let k of T.collidables){let{localTransform3D:W,rigidBody3D:X,velocity3D:Z,collisionLayer:z}=k.components,E=N.getComponent(k.id,"aabb3DCollider"),R=E?void 0:N.getComponent(k.id,"sphereCollider");if(!E&&!R)continue;let Y=L[P];if(!Y)Y={entityId:k.id,x:W.x,y:W.y,z:W.z,layer:z.layer,collidesWith:z.collidesWith,layerBit:0,collidesWithMask:0,shape:_,halfWidth:0,halfHeight:0,halfDepth:0,radius:0,rigidBody:X,velocity:Z},L[P]=Y;else Y.rigidBody=X,Y.velocity=Z;if(!f(Y,k.id,W.x,W.y,W.z,z.layer,z.collidesWith,E,R))continue;P++}if(!U)H=N.tryGetResource("spatialIndex3D"),U=!0;p(L,P,F,H,s,N)})})}export{UJ as setVelocity3D,QJ as createRigidBody3D,FJ as createPhysics3DPlugin,$J as createForce3D,OJ as applyImpulse3D,jJ as applyForce3D};
1
+ var e=((J)=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(J,{get:(K,Q)=>(typeof require<"u"?require:K)[Q]}):J)(function(J){if(typeof require<"u")return require.apply(this,arguments);throw Error('Dynamic require of "'+J+'" is not supported')});import{definePlugin as s}from"ecspresso";function v(J){let K=new Map,Q=new WeakMap,G=1;function V(j){let $=K.get(j);if($!==void 0)return $;if(G===0)throw Error(`[ecspresso] ${J} layer bitmask overflow: more than 32 distinct layers registered`);let N=G;return K.set(j,N),G<<=1,N}function O(j){let $=Q.get(j);if($!==void 0)return $;let N=0;for(let k=0;k<j.length;k++)N|=V(j[k]);return Q.set(j,N),N}return{getLayerBit:V,getCollidesWithMask:O}}var m={normalX:0,normalY:0,normalZ:0,depth:0},w=0,A=1,y=v("3D collision"),b=y.getLayerBit,u=y.getCollidesWithMask;function B(J,K,Q,G,V,O,j,$,N){if(J.entityId=K,J.layer=O,J.collidesWith=j,J.layerBit=b(O),J.collidesWithMask=u(j),$)return J.x=Q+($.offsetX??0),J.y=G+($.offsetY??0),J.z=V+($.offsetZ??0),J.shape=w,J.halfWidth=$.width/2,J.halfHeight=$.height/2,J.halfDepth=$.depth/2,J.radius=0,!0;if(N)return J.x=Q+(N.offsetX??0),J.y=G+(N.offsetY??0),J.z=V+(N.offsetZ??0),J.shape=A,J.halfWidth=0,J.halfHeight=0,J.halfDepth=0,J.radius=N.radius,!0;return!1}function d(J,K,Q,G,V,O,j,$,N,k,F,z,U){let R=j-J,L=$-K,P=N-Q,W=G+k-Math.abs(R),q=V+F-Math.abs(L),T=O+z-Math.abs(P);if(W<=0||q<=0||T<=0)return!1;if(W<=q&&W<=T)return U.normalX=R>=0?1:-1,U.normalY=0,U.normalZ=0,U.depth=W,!0;if(q<=T)return U.normalX=0,U.normalY=L>=0?1:-1,U.normalZ=0,U.depth=q,!0;return U.normalX=0,U.normalY=0,U.normalZ=P>=0?1:-1,U.depth=T,!0}function r(J,K,Q,G,V,O,j,$,N){let k=V-J,F=O-K,z=j-Q,U=k*k+F*F+z*z,R=G+$;if(U>=R*R)return!1;let L=Math.sqrt(U);if(L===0)return N.normalX=1,N.normalY=0,N.normalZ=0,N.depth=R,!0;return N.normalX=k/L,N.normalY=F/L,N.normalZ=z/L,N.depth=R-L,!0}function D(J,K,Q,G,V,O,j,$,N,k,F){let z=Math.max(J-G,Math.min(j,J+G)),U=Math.max(K-V,Math.min($,K+V)),R=Math.max(Q-O,Math.min(N,Q+O)),L=j-z,P=$-U,W=N-R,q=L*L+P*P+W*W;if(q>=k*k)return!1;if(q===0){let Z=j-(J-G),Y=J+G-j,H=$-(K-V),E=K+V-$,X=N-(Q-O),_=Q+O-N,M=Math.min(Z,Y,H,E,X,_);if(M===Y)return F.normalX=1,F.normalY=0,F.normalZ=0,F.depth=Y+k,!0;if(M===Z)return F.normalX=-1,F.normalY=0,F.normalZ=0,F.depth=Z+k,!0;if(M===E)return F.normalX=0,F.normalY=1,F.normalZ=0,F.depth=E+k,!0;if(M===H)return F.normalX=0,F.normalY=-1,F.normalZ=0,F.depth=H+k,!0;if(M===_)return F.normalX=0,F.normalY=0,F.normalZ=1,F.depth=_+k,!0;return F.normalX=0,F.normalY=0,F.normalZ=-1,F.depth=X+k,!0}let T=Math.sqrt(q);return F.normalX=L/T,F.normalY=P/T,F.normalZ=W/T,F.depth=k-T,!0}function p(J,K,Q){if(J.shape===w&&K.shape===w)return d(J.x,J.y,J.z,J.halfWidth,J.halfHeight,J.halfDepth,K.x,K.y,K.z,K.halfWidth,K.halfHeight,K.halfDepth,Q);if(J.shape===A&&K.shape===A)return r(J.x,J.y,J.z,J.radius,K.x,K.y,K.z,K.radius,Q);if(J.shape===w&&K.shape===A)return D(J.x,J.y,J.z,J.halfWidth,J.halfHeight,J.halfDepth,K.x,K.y,K.z,K.radius,Q);if(!D(K.x,K.y,K.z,K.halfWidth,K.halfHeight,K.halfDepth,J.x,J.y,J.z,J.radius,Q))return!1;return Q.normalX=-Q.normalX,Q.normalY=-Q.normalY,Q.normalZ=-Q.normalZ,!0}var g=[],f=!1,c=50;function h(J,K,Q,G,V,O){if(G)n(J,K,Q,G,V,O);else l(J,K,V,O)}function l(J,K,Q,G){if(!f&&K>=c)f=!0,console.warn(`[ecspresso] 3D collision detection is using O(n²) brute force with ${K} colliders. For better performance, install createSpatialIndex3DPlugin() alongside your collision or physics3D plugin.`);for(let V=0;V<K;V++){let O=J[V];if(!O)continue;for(let j=V+1;j<K;j++){let $=J[j];if(!$)continue;if((O.collidesWithMask&$.layerBit|$.collidesWithMask&O.layerBit)===0)continue;if(!p(O,$,m))continue;Q(O,$,m,G)}}}function n(J,K,Q,G,V,O){Q.clear();for(let j=0;j<K;j++){let $=J[j];if(!$)continue;Q.set($.entityId,$)}for(let j=0;j<K;j++){let $=J[j];if(!$)continue;let N=$.shape===w?$.halfWidth:$.radius,k=$.shape===w?$.halfHeight:$.radius,F=$.shape===w?$.halfDepth:$.radius;g.length=0,G.queryBoxInto($.x-N,$.y-k,$.z-F,$.x+N,$.y+k,$.z+F,g,$.entityId);for(let z of g){let U=Q.get(z);if(!U)continue;if(($.collidesWithMask&U.layerBit|U.collidesWithMask&$.layerBit)===0)continue;if(!p($,U,m))continue;V($,U,m,O)}}}function $J(J,K){return{rigidBody3D:{type:J,mass:J==="static"?1/0:K?.mass??1,drag:K?.drag??0,restitution:K?.restitution??0,friction:K?.friction??0,gravityScale:K?.gravityScale??1},force3D:{x:0,y:0,z:0}}}function OJ(J,K,Q){return{force3D:{x:J,y:K,z:Q}}}function jJ(J,K,Q,G,V){let O=J.getComponent(K,"force3D");if(!O)return;O.x+=Q,O.y+=G,O.z+=V}function UJ(J,K,Q,G,V){let O=J.getComponent(K,"velocity3D"),j=J.getComponent(K,"rigidBody3D");if(!O||!j)return;if(j.mass===1/0||j.mass===0)return;O.x+=Q/j.mass,O.y+=G/j.mass,O.z+=V/j.mass}function FJ(J,K,Q,G,V){let O=J.getComponent(K,"velocity3D");if(!O)return;O.x=Q,O.y=G,O.z=V}var x={entityA:0,entityB:0,normalX:0,normalY:0,normalZ:0,depth:0};function i(J,K,Q,G){let V=J.rigidBody.type==="dynamic"&&J.rigidBody.mass>0&&J.rigidBody.mass!==1/0?1/J.rigidBody.mass:0,O=K.rigidBody.type==="dynamic"&&K.rigidBody.mass>0&&K.rigidBody.mass!==1/0?1/K.rigidBody.mass:0,j=V+O;if(j>0){let $=Q.depth/j;if(V>0){let U=G.getComponent(J.entityId,"localTransform3D");if(!U)return;let R=$*V;U.x-=R*Q.normalX,U.y-=R*Q.normalY,U.z-=R*Q.normalZ,J.x=U.x,J.y=U.y,J.z=U.z,G.markChanged(J.entityId,"localTransform3D")}if(O>0){let U=G.getComponent(K.entityId,"localTransform3D");if(!U)return;let R=$*O;U.x+=R*Q.normalX,U.y+=R*Q.normalY,U.z+=R*Q.normalZ,K.x=U.x,K.y=U.y,K.z=U.z,G.markChanged(K.entityId,"localTransform3D")}let N=K.velocity.x-J.velocity.x,k=K.velocity.y-J.velocity.y,F=K.velocity.z-J.velocity.z,z=N*Q.normalX+k*Q.normalY+F*Q.normalZ;if(z<0){let R=-(1+Math.min(J.rigidBody.restitution,K.rigidBody.restitution))*z/j,L=R*V,P=R*O;J.velocity.x-=L*Q.normalX,J.velocity.y-=L*Q.normalY,J.velocity.z-=L*Q.normalZ,K.velocity.x+=P*Q.normalX,K.velocity.y+=P*Q.normalY,K.velocity.z+=P*Q.normalZ;let W=N-z*Q.normalX,q=k-z*Q.normalY,T=F-z*Q.normalZ,Z=Math.sqrt(W*W+q*q+T*T);if(Z>0.000001){let Y=W/Z,H=q/Z,E=T/Z,_=Math.sqrt(J.rigidBody.friction*K.rigidBody.friction)*Math.abs(R),M=Math.min(Z/j,_),I=M*V,C=M*O;J.velocity.x+=I*Y,J.velocity.y+=I*H,J.velocity.z+=I*E,K.velocity.x-=C*Y,K.velocity.y-=C*H,K.velocity.z-=C*E}}G.markChanged(J.entityId,"velocity3D"),G.markChanged(K.entityId,"velocity3D")}x.entityA=J.entityId,x.entityB=K.entityId,x.normalX=Q.normalX,x.normalY=Q.normalY,x.normalZ=Q.normalZ,x.depth=Q.depth,G.eventBus.publish("physics3DCollision",x)}function GJ(J){let{gravity:K={x:0,y:0,z:0},systemGroup:Q="physics3D",collisionSystemGroup:G,integrationPriority:V=1000,collisionPriority:O=900,phase:j="fixedUpdate"}=J??{};return s("physics3D").withComponentTypes().withEventTypes().withResourceTypes().withLabels().withGroups().requires().install(($)=>{$.registerRequired("rigidBody3D","velocity3D",()=>({x:0,y:0,z:0})),$.registerRequired("rigidBody3D","force3D",()=>({x:0,y:0,z:0})),$.addResource("physics3DConfig",{gravity:{x:K.x,y:K.y,z:K.z}}),$.addSystem("physics3D-integration").setPriority(V).inPhase(j).inGroup(Q).addQuery("bodies",{with:["localTransform3D","velocity3D","rigidBody3D","force3D"]}).setProcess(({queries:L,dt:P,ecs:W})=>{let{gravity:q}=W.getResource("physics3DConfig"),T=q.x,Z=q.y,Y=q.z;for(let H of L.bodies){let{localTransform3D:E,velocity3D:X,rigidBody3D:_,force3D:M}=H.components;if(_.type==="static")continue;if(_.type==="dynamic"){let I=_.gravityScale*P;X.x+=T*I,X.y+=Z*I,X.z+=Y*I;let C=_.mass;if(C>0&&C!==1/0){let S=P/C;X.x+=M.x*S,X.y+=M.y*S,X.z+=M.z*S}if(_.drag>0){let S=Math.max(0,1-_.drag*P);X.x*=S,X.y*=S,X.z*=S}}E.x+=X.x*P,E.y+=X.y*P,E.z+=X.z*P,M.x=0,M.y=0,M.z=0,W.markChanged(H.id,"localTransform3D")}});let N=$.addSystem("physics3D-collision").setPriority(O).inPhase(j).inGroup(Q);if(G)N.inGroup(G);let k=[],F=new Map,z,U=!1,R=(L,P,W,q,T,Z)=>{let Y=k[L];if(!Y)Y={entityId:P,x:0,y:0,z:0,layer:W,collidesWith:q,layerBit:0,collidesWithMask:0,shape:w,halfWidth:0,halfHeight:0,halfDepth:0,radius:0,rigidBody:T,velocity:Z},k[L]=Y;else Y.rigidBody=T,Y.velocity=Z;return Y};N.addQuery("aabbOnly",{with:["localTransform3D","rigidBody3D","velocity3D","collisionLayer","aabb3DCollider"],without:["sphereCollider"]}).addQuery("sphereOnly",{with:["localTransform3D","rigidBody3D","velocity3D","collisionLayer","sphereCollider"],without:["aabb3DCollider"]}).addQuery("both",{with:["localTransform3D","rigidBody3D","velocity3D","collisionLayer","aabb3DCollider","sphereCollider"]}).setProcess(({queries:L,ecs:P})=>{let W=0;for(let q of L.aabbOnly){let{localTransform3D:T,rigidBody3D:Z,velocity3D:Y,collisionLayer:H,aabb3DCollider:E}=q.components,X=R(W,q.id,H.layer,H.collidesWith,Z,Y);if(!B(X,q.id,T.x,T.y,T.z,H.layer,H.collidesWith,E,void 0))continue;W++}for(let q of L.sphereOnly){let{localTransform3D:T,rigidBody3D:Z,velocity3D:Y,collisionLayer:H,sphereCollider:E}=q.components,X=R(W,q.id,H.layer,H.collidesWith,Z,Y);if(!B(X,q.id,T.x,T.y,T.z,H.layer,H.collidesWith,void 0,E))continue;W++}for(let q of L.both){let{localTransform3D:T,rigidBody3D:Z,velocity3D:Y,collisionLayer:H,aabb3DCollider:E,sphereCollider:X}=q.components,_=R(W,q.id,H.layer,H.collidesWith,Z,Y);if(!B(_,q.id,T.x,T.y,T.z,H.layer,H.collidesWith,E,X))continue;W++}if(!U)z=P.tryGetResource("spatialIndex3D"),U=!0;h(k,W,F,z,i,P)})})}export{FJ as setVelocity3D,$J as createRigidBody3D,GJ as createPhysics3DPlugin,OJ as createForce3D,UJ as applyImpulse3D,jJ as applyForce3D};
2
2
 
3
- //# debugId=6C1C37A7CF5B331564756E2164756E21
3
+ //# debugId=FABAD286A305821E64756E2164756E21
4
4
  //# sourceMappingURL=physics3D.js.map
@@ -2,11 +2,11 @@
2
2
  "version": 3,
3
3
  "sources": ["../src/plugins/physics/physics3D.ts", "../src/utils/layer-bit-registry.ts", "../src/utils/narrowphase3D.ts"],
4
4
  "sourcesContent": [
5
- "/**\n * Physics 3D Plugin for ECSpresso\n *\n * Provides ECS-native 3D arcade physics: gravity, forces, drag, semi-implicit Euler\n * integration, and impulse-based collision response with friction.\n *\n * Reuses RigidBody and collider types from the 2D physics/collision plugins for\n * shape definitions. Has its own collision detection in fixedUpdate for physics\n * response; the existing collision3D plugin can still run in postUpdate for game\n * logic events.\n */\n\nimport { definePlugin } from 'ecspresso';\nimport type { SystemPhase } from 'ecspresso';\nimport type { Vector3D } from 'ecspresso';\nimport type { Transform3DWorldConfig } from '../spatial/transform3D';\nimport type { Collision3DComponentTypes, LayerFactories } from './collision3D';\nimport type { RigidBody, BodyType, RigidBodyOptions } from './physics2D';\nimport { fillBaseColliderInfo3D, detectCollisions3D, AABB3D_SHAPE, type Contact3D, type BaseColliderInfo3D } from '../../utils/narrowphase3D';\nimport type { SpatialIndex3D } from '../../utils/spatial-hash3D';\n\n// Re-export so consumers can type rigid bodies without importing physics2D\nexport type { RigidBody, BodyType, RigidBodyOptions };\n\n// ==================== Component Types ====================\n\n/**\n * Component types directly provided by the physics3D plugin.\n */\nexport interface Physics3DOwnComponentTypes {\n\trigidBody3D: RigidBody;\n\tvelocity3D: Vector3D;\n\tforce3D: Vector3D;\n}\n\n/**\n * Full component types available when using the physics3D plugin\n * (own components + transform + collision dependencies).\n * Convenience alias for consumer code.\n */\nexport interface Physics3DComponentTypes<L extends string = never> extends Collision3DComponentTypes<L>, Physics3DOwnComponentTypes {}\n\n// ==================== Resource Types ====================\n\n/**\n * Physics 3D configuration resource.\n */\nexport interface Physics3DConfig {\n\tgravity: Vector3D;\n}\n\nexport interface Physics3DResourceTypes {\n\tphysics3DConfig: Physics3DConfig;\n}\n\n// ==================== Event Types ====================\n\n/**\n * Event emitted for each physics 3D collision pair.\n *\n * Normal components are flattened (`normalX`/`normalY`/`normalZ`) rather than\n * nested in a `Vector3D` to avoid a per-event allocation in the physics hot path.\n */\nexport interface Physics3DCollisionEvent {\n\tentityA: number;\n\tentityB: number;\n\t/** Unit normal X, pointing from A toward B */\n\tnormalX: number;\n\t/** Unit normal Y, pointing from A toward B */\n\tnormalY: number;\n\t/** Unit normal Z, pointing from A toward B */\n\tnormalZ: number;\n\t/** Penetration depth (positive) */\n\tdepth: number;\n}\n\nexport interface Physics3DEventTypes {\n\tphysics3DCollision: Physics3DCollisionEvent;\n}\n\n// ==================== Plugin Options ====================\n\nexport interface Physics3DPluginOptions<G extends string = 'physics3D', CG extends string = never> {\n\t/** World gravity vector (default: {x: 0, y: 0, z: 0}) */\n\tgravity?: Vector3D;\n\t/** System group name (default: 'physics3D') */\n\tsystemGroup?: G;\n\t/** Additional group for the collision system only (default: none).\n\t * When set, the collision system belongs to both `systemGroup` and this group,\n\t * allowing independent enable/disable of collision detection. */\n\tcollisionSystemGroup?: CG;\n\t/** Priority for integration system (default: 1000) */\n\tintegrationPriority?: number;\n\t/** Priority for collision system (default: 900) */\n\tcollisionPriority?: number;\n\t/** Execution phase (default: 'fixedUpdate') */\n\tphase?: SystemPhase;\n}\n\n// ==================== Helper Functions ====================\n\n/**\n * Create a rigidBody3D + force3D component pair.\n * Static bodies automatically get mass=Infinity.\n */\nexport function createRigidBody3D(\n\ttype: BodyType,\n\toptions?: RigidBodyOptions,\n): { rigidBody3D: RigidBody; force3D: Vector3D } {\n\treturn {\n\t\trigidBody3D: {\n\t\t\ttype,\n\t\t\tmass: type === 'static' ? Infinity : (options?.mass ?? 1),\n\t\t\tdrag: options?.drag ?? 0,\n\t\t\trestitution: options?.restitution ?? 0,\n\t\t\tfriction: options?.friction ?? 0,\n\t\t\tgravityScale: options?.gravityScale ?? 1,\n\t\t},\n\t\tforce3D: { x: 0, y: 0, z: 0 },\n\t};\n}\n\n/**\n * Create a force3D component with initial values.\n */\nexport function createForce3D(x: number, y: number, z: number): { force3D: Vector3D } {\n\treturn { force3D: { x, y, z } };\n}\n\n/**\n * Accumulate a force onto an entity's force3D component.\n */\nexport function applyForce3D(\n\tecs: { getComponent(id: number, name: 'force3D'): Vector3D | undefined },\n\tentityId: number,\n\tfx: number,\n\tfy: number,\n\tfz: number,\n): void {\n\tconst force = ecs.getComponent(entityId, 'force3D');\n\tif (!force) return;\n\tforce.x += fx;\n\tforce.y += fy;\n\tforce.z += fz;\n}\n\n/**\n * Apply an instantaneous impulse: velocity3D += impulse / mass.\n */\nexport function applyImpulse3D(\n\tecs: {\n\t\tgetComponent(id: number, name: 'velocity3D'): Vector3D | undefined;\n\t\tgetComponent(id: number, name: 'rigidBody3D'): RigidBody | undefined;\n\t},\n\tentityId: number,\n\tix: number,\n\tiy: number,\n\tiz: number,\n): void {\n\tconst velocity = ecs.getComponent(entityId, 'velocity3D');\n\tconst rigidBody = ecs.getComponent(entityId, 'rigidBody3D');\n\tif (!velocity || !rigidBody) return;\n\tif (rigidBody.mass === Infinity || rigidBody.mass === 0) return;\n\tvelocity.x += ix / rigidBody.mass;\n\tvelocity.y += iy / rigidBody.mass;\n\tvelocity.z += iz / rigidBody.mass;\n}\n\n/**\n * Directly set an entity's velocity3D.\n */\nexport function setVelocity3D(\n\tecs: { getComponent(id: number, name: 'velocity3D'): Vector3D | undefined },\n\tentityId: number,\n\tvx: number,\n\tvy: number,\n\tvz: number,\n): void {\n\tconst velocity = ecs.getComponent(entityId, 'velocity3D');\n\tif (!velocity) return;\n\tvelocity.x = vx;\n\tvelocity.y = vy;\n\tvelocity.z = vz;\n}\n\n// ==================== Internal: Collider Info ====================\n\ninterface Physics3DColliderInfo<L extends string = string> extends BaseColliderInfo3D<L> {\n\trigidBody: RigidBody;\n\tvelocity: Vector3D;\n}\n\n// ==================== Collision Response ====================\n\n/**\n * Module-level reusable physics3D collision event. Subscribers must consume\n * synchronously — same contract as the shared narrowphase Contact3D.\n */\nconst _physicsCollisionEvent: Physics3DCollisionEvent = {\n\tentityA: 0, entityB: 0, normalX: 0, normalY: 0, normalZ: 0, depth: 0,\n};\n\ninterface PhysicsEcs3DLike {\n\tgetComponent(id: number, name: 'localTransform3D'): { x: number; y: number; z: number } | undefined;\n\teventBus: { publish(event: 'physics3DCollision', data: Physics3DCollisionEvent): void };\n\tmarkChanged(entityId: number, componentName: 'localTransform3D' | 'velocity3D'): void;\n}\n\n/**\n * Resolve a 3D physics collision pair: position correction, impulse response, event.\n *\n * Friction uses a tangent plane projection: the tangential velocity is the\n * component of relative velocity perpendicular to the contact normal. This\n * generalizes the 2D tangent-line approach to 3D — mathematically the same\n * operation with an added Z component.\n */\nfunction resolvePhysicsContact3D(\n\ta: Physics3DColliderInfo,\n\tb: Physics3DColliderInfo,\n\tcontact: Contact3D,\n\tecs: PhysicsEcs3DLike,\n): void {\n\tconst invMassA = (a.rigidBody.type === 'dynamic' && a.rigidBody.mass > 0 && a.rigidBody.mass !== Infinity)\n\t\t? 1 / a.rigidBody.mass\n\t\t: 0;\n\tconst invMassB = (b.rigidBody.type === 'dynamic' && b.rigidBody.mass > 0 && b.rigidBody.mass !== Infinity)\n\t\t? 1 / b.rigidBody.mass\n\t\t: 0;\n\tconst totalInvMass = invMassA + invMassB;\n\n\t// Position correction\n\tif (totalInvMass > 0) {\n\t\tconst correctionScale = contact.depth / totalInvMass;\n\n\t\tif (invMassA > 0) {\n\t\t\tconst ltA = ecs.getComponent(a.entityId, 'localTransform3D');\n\t\t\tif (!ltA) return;\n\t\t\tconst corrA = correctionScale * invMassA;\n\t\t\tltA.x -= corrA * contact.normalX;\n\t\t\tltA.y -= corrA * contact.normalY;\n\t\t\tltA.z -= corrA * contact.normalZ;\n\t\t\t// Sync cached position so subsequent pairs in this frame use corrected values\n\t\t\ta.x = ltA.x;\n\t\t\ta.y = ltA.y;\n\t\t\ta.z = ltA.z;\n\t\t\tecs.markChanged(a.entityId, 'localTransform3D');\n\t\t}\n\n\t\tif (invMassB > 0) {\n\t\t\tconst ltB = ecs.getComponent(b.entityId, 'localTransform3D');\n\t\t\tif (!ltB) return;\n\t\t\tconst corrB = correctionScale * invMassB;\n\t\t\tltB.x += corrB * contact.normalX;\n\t\t\tltB.y += corrB * contact.normalY;\n\t\t\tltB.z += corrB * contact.normalZ;\n\t\t\tb.x = ltB.x;\n\t\t\tb.y = ltB.y;\n\t\t\tb.z = ltB.z;\n\t\t\tecs.markChanged(b.entityId, 'localTransform3D');\n\t\t}\n\n\t\t// Velocity response (impulse-based)\n\t\tconst relVelX = b.velocity.x - a.velocity.x;\n\t\tconst relVelY = b.velocity.y - a.velocity.y;\n\t\tconst relVelZ = b.velocity.z - a.velocity.z;\n\t\tconst velAlongNormal = relVelX * contact.normalX + relVelY * contact.normalY + relVelZ * contact.normalZ;\n\n\t\tif (velAlongNormal < 0) {\n\t\t\tconst restitution = Math.min(a.rigidBody.restitution, b.rigidBody.restitution);\n\t\t\tconst normalImpulse = -(1 + restitution) * velAlongNormal / totalInvMass;\n\t\t\tconst impA = normalImpulse * invMassA;\n\t\t\tconst impB = normalImpulse * invMassB;\n\n\t\t\ta.velocity.x -= impA * contact.normalX;\n\t\t\ta.velocity.y -= impA * contact.normalY;\n\t\t\ta.velocity.z -= impA * contact.normalZ;\n\t\t\tb.velocity.x += impB * contact.normalX;\n\t\t\tb.velocity.y += impB * contact.normalY;\n\t\t\tb.velocity.z += impB * contact.normalZ;\n\n\t\t\t// Friction (tangential impulse — project relative velocity onto tangent plane)\n\t\t\tconst tangentX = relVelX - velAlongNormal * contact.normalX;\n\t\t\tconst tangentY = relVelY - velAlongNormal * contact.normalY;\n\t\t\tconst tangentZ = relVelZ - velAlongNormal * contact.normalZ;\n\t\t\tconst tangentSpeed = Math.sqrt(tangentX * tangentX + tangentY * tangentY + tangentZ * tangentZ);\n\n\t\t\tif (tangentSpeed > 1e-6) {\n\t\t\t\tconst tangentNX = tangentX / tangentSpeed;\n\t\t\t\tconst tangentNY = tangentY / tangentSpeed;\n\t\t\t\tconst tangentNZ = tangentZ / tangentSpeed;\n\t\t\t\tconst friction = Math.sqrt(a.rigidBody.friction * b.rigidBody.friction);\n\t\t\t\tconst maxFrictionImpulse = friction * Math.abs(normalImpulse);\n\t\t\t\tconst tangentImpulse = Math.min(tangentSpeed / totalInvMass, maxFrictionImpulse);\n\t\t\t\tconst tanA = tangentImpulse * invMassA;\n\t\t\t\tconst tanB = tangentImpulse * invMassB;\n\n\t\t\t\ta.velocity.x += tanA * tangentNX;\n\t\t\t\ta.velocity.y += tanA * tangentNY;\n\t\t\t\ta.velocity.z += tanA * tangentNZ;\n\t\t\t\tb.velocity.x -= tanB * tangentNX;\n\t\t\t\tb.velocity.y -= tanB * tangentNY;\n\t\t\t\tb.velocity.z -= tanB * tangentNZ;\n\t\t\t}\n\t\t}\n\n\t\tecs.markChanged(a.entityId, 'velocity3D');\n\t\tecs.markChanged(b.entityId, 'velocity3D');\n\t}\n\n\t_physicsCollisionEvent.entityA = a.entityId;\n\t_physicsCollisionEvent.entityB = b.entityId;\n\t_physicsCollisionEvent.normalX = contact.normalX;\n\t_physicsCollisionEvent.normalY = contact.normalY;\n\t_physicsCollisionEvent.normalZ = contact.normalZ;\n\t_physicsCollisionEvent.depth = contact.depth;\n\tecs.eventBus.publish('physics3DCollision', _physicsCollisionEvent);\n}\n\n// ==================== Plugin Factory ====================\n\n/**\n * Create a 3D physics plugin for ECSpresso.\n *\n * Provides:\n * - Semi-implicit Euler integration (gravity, forces, drag → velocity3D → position)\n * - Impulse-based collision response with restitution and friction\n * - physics3DCollision events with contact normal and depth\n *\n * @example\n * ```typescript\n * const ecs = ECSpresso.create()\n * .withPlugin(createTransform3DPlugin())\n * .withPlugin(createPhysics3DPlugin({ gravity: { x: 0, y: -9.81, z: 0 } }))\n * .withFixedTimestep(1/60)\n * .build();\n *\n * ecs.spawn({\n * ...createTransform3D(0, 10, 0),\n * ...createRigidBody3D('dynamic', { mass: 1, restitution: 0.5 }),\n * velocity3D: { x: 0, y: 0, z: 0 },\n * ...createAABB3DCollider(1, 1, 1),\n * ...createCollisionLayer('player', ['ground']),\n * });\n * ```\n */\n\ntype Physics3DProvides<L extends string = never> = Physics3DOwnComponentTypes & Collision3DComponentTypes<L>;\n\nexport function createPhysics3DPlugin<L extends string = never, G extends string = 'physics3D', CG extends string = never>(\n\toptions?: Physics3DPluginOptions<G, CG> & { layers?: LayerFactories<Record<L, readonly string[]>> },\n) {\n\tconst {\n\t\tgravity = { x: 0, y: 0, z: 0 },\n\t\tsystemGroup = 'physics3D',\n\t\tcollisionSystemGroup,\n\t\tintegrationPriority = 1000,\n\t\tcollisionPriority = 900,\n\t\tphase = 'fixedUpdate',\n\t} = options ?? {};\n\n\treturn definePlugin('physics3D')\n\t\t.withComponentTypes<Physics3DProvides<L>>()\n\t\t.withEventTypes<Physics3DEventTypes>()\n\t\t.withResourceTypes<Physics3DResourceTypes>()\n\t\t.withLabels<'physics3D-integration' | 'physics3D-collision'>()\n\t\t.withGroups<G | CG>()\n\t\t.requires<Transform3DWorldConfig>()\n\t\t.install((world) => {\n\t\t\t// rigidBody3D requires velocity3D and force3D — auto-add with zero defaults\n\t\t\tworld.registerRequired('rigidBody3D', 'velocity3D', () => ({ x: 0, y: 0, z: 0 }));\n\t\t\tworld.registerRequired('rigidBody3D', 'force3D', () => ({ x: 0, y: 0, z: 0 }));\n\n\t\t\tworld.addResource('physics3DConfig', { gravity: { x: gravity.x, y: gravity.y, z: gravity.z } });\n\n\t\t\t// ==================== Integration System ====================\n\n\t\t\tworld\n\t\t\t\t.addSystem('physics3D-integration')\n\t\t\t\t.setPriority(integrationPriority)\n\t\t\t\t.inPhase(phase)\n\t\t\t\t.inGroup(systemGroup)\n\t\t\t\t.addQuery('bodies', {\n\t\t\t\t\twith: ['localTransform3D', 'velocity3D', 'rigidBody3D', 'force3D'],\n\t\t\t\t})\n\t\t\t\t.setProcess(({ queries, dt, ecs }) => {\n\t\t\t\t\tconst { gravity: g } = ecs.getResource('physics3DConfig');\n\t\t\t\t\tconst gx = g.x;\n\t\t\t\t\tconst gy = g.y;\n\t\t\t\t\tconst gz = g.z;\n\n\t\t\t\t\t// TODO(perf): no early-out for \"sleeping\" dynamic bodies — a packed\n\t\t\t\t\t// pile of resting entities still runs gravity/drag/force-clear/\n\t\t\t\t\t// markChanged every step. A sleep flag on RigidBody that latches\n\t\t\t\t\t// after N frames of near-zero velocity (and clears on impulse or\n\t\t\t\t\t// applied force) would let most of a stabilized scene skip the\n\t\t\t\t\t// full per-entity body of this loop. Needs collision response to\n\t\t\t\t\t// wake sleepers back up; keep in sync with physics2D when landed.\n\t\t\t\t\tfor (const entity of queries.bodies) {\n\t\t\t\t\t\tconst { localTransform3D, velocity3D, rigidBody3D, force3D } = entity.components;\n\n\t\t\t\t\t\t// Static bodies: skip entirely\n\t\t\t\t\t\tif (rigidBody3D.type === 'static') continue;\n\n\t\t\t\t\t\t// Dynamic bodies: apply gravity, forces, drag\n\t\t\t\t\t\tif (rigidBody3D.type === 'dynamic') {\n\t\t\t\t\t\t\t// 1. Gravity\n\t\t\t\t\t\t\tconst gsdt = rigidBody3D.gravityScale * dt;\n\t\t\t\t\t\t\tvelocity3D.x += gx * gsdt;\n\t\t\t\t\t\t\tvelocity3D.y += gy * gsdt;\n\t\t\t\t\t\t\tvelocity3D.z += gz * gsdt;\n\n\t\t\t\t\t\t\t// 2. Forces (F = ma → a = F/m)\n\t\t\t\t\t\t\tconst mass = rigidBody3D.mass;\n\t\t\t\t\t\t\tif (mass > 0 && mass !== Infinity) {\n\t\t\t\t\t\t\t\tconst invMassDt = dt / mass;\n\t\t\t\t\t\t\t\tvelocity3D.x += force3D.x * invMassDt;\n\t\t\t\t\t\t\t\tvelocity3D.y += force3D.y * invMassDt;\n\t\t\t\t\t\t\t\tvelocity3D.z += force3D.z * invMassDt;\n\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\t// 3. Drag\n\t\t\t\t\t\t\tif (rigidBody3D.drag > 0) {\n\t\t\t\t\t\t\t\tconst damping = Math.max(0, 1 - rigidBody3D.drag * dt);\n\t\t\t\t\t\t\t\tvelocity3D.x *= damping;\n\t\t\t\t\t\t\t\tvelocity3D.y *= damping;\n\t\t\t\t\t\t\t\tvelocity3D.z *= damping;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\t// Both dynamic and kinematic: integrate position\n\t\t\t\t\t\tlocalTransform3D.x += velocity3D.x * dt;\n\t\t\t\t\t\tlocalTransform3D.y += velocity3D.y * dt;\n\t\t\t\t\t\tlocalTransform3D.z += velocity3D.z * dt;\n\n\t\t\t\t\t\t// Clear accumulated forces\n\t\t\t\t\t\tforce3D.x = 0;\n\t\t\t\t\t\tforce3D.y = 0;\n\t\t\t\t\t\tforce3D.z = 0;\n\n\t\t\t\t\t\tecs.markChanged(entity.id, 'localTransform3D');\n\t\t\t\t\t}\n\t\t\t\t});\n\n\t\t\t// ==================== Collision System ====================\n\n\t\t\tconst collisionSystem = world\n\t\t\t\t.addSystem('physics3D-collision')\n\t\t\t\t.setPriority(collisionPriority)\n\t\t\t\t.inPhase(phase)\n\t\t\t\t.inGroup(systemGroup);\n\n\t\t\tif (collisionSystemGroup) {\n\t\t\t\tcollisionSystem.inGroup(collisionSystemGroup);\n\t\t\t}\n\n\t\t\t// Grow-only pool of Physics3DColliderInfo slots reused across frames.\n\t\t\t// Steady-state: zero allocations per frame once the pool is warm.\n\t\t\tconst colliderPool: Physics3DColliderInfo<L>[] = [];\n\t\t\t// Reusable entityId → collider lookup for the broadphase path.\n\t\t\tconst broadphaseMap = new Map<number, Physics3DColliderInfo<L>>();\n\t\t\t// Cached spatial index reference (resolved once on first frame).\n\t\t\tlet cachedSI: SpatialIndex3D | undefined;\n\t\t\tlet siResolved = false;\n\n\t\t\tcollisionSystem\n\t\t\t\t.addQuery('collidables', {\n\t\t\t\t\twith: ['localTransform3D', 'rigidBody3D', 'velocity3D', 'collisionLayer'],\n\t\t\t\t})\n\t\t\t\t.setProcess(({ queries, ecs }) => {\n\t\t\t\t\tlet count = 0;\n\n\t\t\t\t\t// TODO(perf): collider shape is discovered via two ecs.getComponent\n\t\t\t\t\t// calls per entity per frame because the query can't express\n\t\t\t\t\t// \"aabb3DCollider OR sphereCollider\". Splitting into two queries\n\t\t\t\t\t// (aabb-bearing, sphere-bearing) would eliminate these lookups at\n\t\t\t\t\t// the cost of two pool-fill passes. Revisit once the query API\n\t\t\t\t\t// gains `anyOf`-style predicates.\n\t\t\t\t\tfor (const entity of queries.collidables) {\n\t\t\t\t\t\tconst { localTransform3D, rigidBody3D, velocity3D, collisionLayer } = entity.components;\n\t\t\t\t\t\tconst aabb = ecs.getComponent(entity.id, 'aabb3DCollider');\n\t\t\t\t\t\tconst sphere = aabb ? undefined : ecs.getComponent(entity.id, 'sphereCollider');\n\t\t\t\t\t\tif (!aabb && !sphere) continue;\n\n\t\t\t\t\t\tlet slot = colliderPool[count];\n\t\t\t\t\t\tif (!slot) {\n\t\t\t\t\t\t\tslot = {\n\t\t\t\t\t\t\t\tentityId: entity.id,\n\t\t\t\t\t\t\t\tx: localTransform3D.x,\n\t\t\t\t\t\t\t\ty: localTransform3D.y,\n\t\t\t\t\t\t\t\tz: localTransform3D.z,\n\t\t\t\t\t\t\t\tlayer: collisionLayer.layer,\n\t\t\t\t\t\t\t\tcollidesWith: collisionLayer.collidesWith,\n\t\t\t\t\t\t\t\tlayerBit: 0,\n\t\t\t\t\t\t\t\tcollidesWithMask: 0,\n\t\t\t\t\t\t\t\tshape: AABB3D_SHAPE,\n\t\t\t\t\t\t\t\thalfWidth: 0,\n\t\t\t\t\t\t\t\thalfHeight: 0,\n\t\t\t\t\t\t\t\thalfDepth: 0,\n\t\t\t\t\t\t\t\tradius: 0,\n\t\t\t\t\t\t\t\trigidBody: rigidBody3D,\n\t\t\t\t\t\t\t\tvelocity: velocity3D,\n\t\t\t\t\t\t\t};\n\t\t\t\t\t\t\tcolliderPool[count] = slot;\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\tslot.rigidBody = rigidBody3D;\n\t\t\t\t\t\t\tslot.velocity = velocity3D;\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tif (!fillBaseColliderInfo3D(\n\t\t\t\t\t\t\tslot,\n\t\t\t\t\t\t\tentity.id, localTransform3D.x, localTransform3D.y, localTransform3D.z,\n\t\t\t\t\t\t\tcollisionLayer.layer, collisionLayer.collidesWith,\n\t\t\t\t\t\t\taabb, sphere,\n\t\t\t\t\t\t)) continue;\n\n\t\t\t\t\t\tcount++;\n\t\t\t\t\t}\n\n\t\t\t\t\tif (!siResolved) {\n\t\t\t\t\t\tcachedSI = ecs.tryGetResource<SpatialIndex3D>('spatialIndex3D');\n\t\t\t\t\t\tsiResolved = true;\n\t\t\t\t\t}\n\t\t\t\t\tdetectCollisions3D(colliderPool, count, broadphaseMap, cachedSI, resolvePhysicsContact3D, ecs);\n\t\t\t\t});\n\t\t});\n}\n",
5
+ "/**\n * Physics 3D Plugin for ECSpresso\n *\n * Provides ECS-native 3D arcade physics: gravity, forces, drag, semi-implicit Euler\n * integration, and impulse-based collision response with friction.\n *\n * Reuses RigidBody and collider types from the 2D physics/collision plugins for\n * shape definitions. Has its own collision detection in fixedUpdate for physics\n * response; the existing collision3D plugin can still run in postUpdate for game\n * logic events.\n */\n\nimport { definePlugin } from 'ecspresso';\nimport type { SystemPhase } from 'ecspresso';\nimport type { Vector3D } from 'ecspresso';\nimport type { Transform3DWorldConfig } from '../spatial/transform3D';\nimport type { Collision3DComponentTypes, LayerFactories } from './collision3D';\nimport type { RigidBody, BodyType, RigidBodyOptions } from './physics2D';\nimport { fillBaseColliderInfo3D, detectCollisions3D, AABB3D_SHAPE, type Contact3D, type BaseColliderInfo3D } from '../../utils/narrowphase3D';\nimport type { SpatialIndex3D } from '../../utils/spatial-hash3D';\n\n// Re-export so consumers can type rigid bodies without importing physics2D\nexport type { RigidBody, BodyType, RigidBodyOptions };\n\n// ==================== Component Types ====================\n\n/**\n * Component types directly provided by the physics3D plugin.\n */\nexport interface Physics3DOwnComponentTypes {\n\trigidBody3D: RigidBody;\n\tvelocity3D: Vector3D;\n\tforce3D: Vector3D;\n}\n\n/**\n * Full component types available when using the physics3D plugin\n * (own components + transform + collision dependencies).\n * Convenience alias for consumer code.\n */\nexport interface Physics3DComponentTypes<L extends string = never> extends Collision3DComponentTypes<L>, Physics3DOwnComponentTypes {}\n\n// ==================== Resource Types ====================\n\n/**\n * Physics 3D configuration resource.\n */\nexport interface Physics3DConfig {\n\tgravity: Vector3D;\n}\n\nexport interface Physics3DResourceTypes {\n\tphysics3DConfig: Physics3DConfig;\n}\n\n// ==================== Event Types ====================\n\n/**\n * Event emitted for each physics 3D collision pair.\n *\n * Normal components are flattened (`normalX`/`normalY`/`normalZ`) rather than\n * nested in a `Vector3D` to avoid a per-event allocation in the physics hot path.\n */\nexport interface Physics3DCollisionEvent {\n\tentityA: number;\n\tentityB: number;\n\t/** Unit normal X, pointing from A toward B */\n\tnormalX: number;\n\t/** Unit normal Y, pointing from A toward B */\n\tnormalY: number;\n\t/** Unit normal Z, pointing from A toward B */\n\tnormalZ: number;\n\t/** Penetration depth (positive) */\n\tdepth: number;\n}\n\nexport interface Physics3DEventTypes {\n\tphysics3DCollision: Physics3DCollisionEvent;\n}\n\n// ==================== Plugin Options ====================\n\nexport interface Physics3DPluginOptions<G extends string = 'physics3D', CG extends string = never> {\n\t/** World gravity vector (default: {x: 0, y: 0, z: 0}) */\n\tgravity?: Vector3D;\n\t/** System group name (default: 'physics3D') */\n\tsystemGroup?: G;\n\t/** Additional group for the collision system only (default: none).\n\t * When set, the collision system belongs to both `systemGroup` and this group,\n\t * allowing independent enable/disable of collision detection. */\n\tcollisionSystemGroup?: CG;\n\t/** Priority for integration system (default: 1000) */\n\tintegrationPriority?: number;\n\t/** Priority for collision system (default: 900) */\n\tcollisionPriority?: number;\n\t/** Execution phase (default: 'fixedUpdate') */\n\tphase?: SystemPhase;\n}\n\n// ==================== Helper Functions ====================\n\n/**\n * Create a rigidBody3D + force3D component pair.\n * Static bodies automatically get mass=Infinity.\n */\nexport function createRigidBody3D(\n\ttype: BodyType,\n\toptions?: RigidBodyOptions,\n): { rigidBody3D: RigidBody; force3D: Vector3D } {\n\treturn {\n\t\trigidBody3D: {\n\t\t\ttype,\n\t\t\tmass: type === 'static' ? Infinity : (options?.mass ?? 1),\n\t\t\tdrag: options?.drag ?? 0,\n\t\t\trestitution: options?.restitution ?? 0,\n\t\t\tfriction: options?.friction ?? 0,\n\t\t\tgravityScale: options?.gravityScale ?? 1,\n\t\t},\n\t\tforce3D: { x: 0, y: 0, z: 0 },\n\t};\n}\n\n/**\n * Create a force3D component with initial values.\n */\nexport function createForce3D(x: number, y: number, z: number): { force3D: Vector3D } {\n\treturn { force3D: { x, y, z } };\n}\n\n/**\n * Accumulate a force onto an entity's force3D component.\n */\nexport function applyForce3D(\n\tecs: { getComponent(id: number, name: 'force3D'): Vector3D | undefined },\n\tentityId: number,\n\tfx: number,\n\tfy: number,\n\tfz: number,\n): void {\n\tconst force = ecs.getComponent(entityId, 'force3D');\n\tif (!force) return;\n\tforce.x += fx;\n\tforce.y += fy;\n\tforce.z += fz;\n}\n\n/**\n * Apply an instantaneous impulse: velocity3D += impulse / mass.\n */\nexport function applyImpulse3D(\n\tecs: {\n\t\tgetComponent(id: number, name: 'velocity3D'): Vector3D | undefined;\n\t\tgetComponent(id: number, name: 'rigidBody3D'): RigidBody | undefined;\n\t},\n\tentityId: number,\n\tix: number,\n\tiy: number,\n\tiz: number,\n): void {\n\tconst velocity = ecs.getComponent(entityId, 'velocity3D');\n\tconst rigidBody = ecs.getComponent(entityId, 'rigidBody3D');\n\tif (!velocity || !rigidBody) return;\n\tif (rigidBody.mass === Infinity || rigidBody.mass === 0) return;\n\tvelocity.x += ix / rigidBody.mass;\n\tvelocity.y += iy / rigidBody.mass;\n\tvelocity.z += iz / rigidBody.mass;\n}\n\n/**\n * Directly set an entity's velocity3D.\n */\nexport function setVelocity3D(\n\tecs: { getComponent(id: number, name: 'velocity3D'): Vector3D | undefined },\n\tentityId: number,\n\tvx: number,\n\tvy: number,\n\tvz: number,\n): void {\n\tconst velocity = ecs.getComponent(entityId, 'velocity3D');\n\tif (!velocity) return;\n\tvelocity.x = vx;\n\tvelocity.y = vy;\n\tvelocity.z = vz;\n}\n\n// ==================== Internal: Collider Info ====================\n\ninterface Physics3DColliderInfo<L extends string = string> extends BaseColliderInfo3D<L> {\n\trigidBody: RigidBody;\n\tvelocity: Vector3D;\n}\n\n// ==================== Collision Response ====================\n\n/**\n * Module-level reusable physics3D collision event. Subscribers must consume\n * synchronously — same contract as the shared narrowphase Contact3D.\n */\nconst _physicsCollisionEvent: Physics3DCollisionEvent = {\n\tentityA: 0, entityB: 0, normalX: 0, normalY: 0, normalZ: 0, depth: 0,\n};\n\ninterface PhysicsEcs3DLike {\n\tgetComponent(id: number, name: 'localTransform3D'): { x: number; y: number; z: number } | undefined;\n\teventBus: { publish(event: 'physics3DCollision', data: Physics3DCollisionEvent): void };\n\tmarkChanged(entityId: number, componentName: 'localTransform3D' | 'velocity3D'): void;\n}\n\n/**\n * Resolve a 3D physics collision pair: position correction, impulse response, event.\n *\n * Friction uses a tangent plane projection: the tangential velocity is the\n * component of relative velocity perpendicular to the contact normal. This\n * generalizes the 2D tangent-line approach to 3D — mathematically the same\n * operation with an added Z component.\n */\nfunction resolvePhysicsContact3D(\n\ta: Physics3DColliderInfo,\n\tb: Physics3DColliderInfo,\n\tcontact: Contact3D,\n\tecs: PhysicsEcs3DLike,\n): void {\n\tconst invMassA = (a.rigidBody.type === 'dynamic' && a.rigidBody.mass > 0 && a.rigidBody.mass !== Infinity)\n\t\t? 1 / a.rigidBody.mass\n\t\t: 0;\n\tconst invMassB = (b.rigidBody.type === 'dynamic' && b.rigidBody.mass > 0 && b.rigidBody.mass !== Infinity)\n\t\t? 1 / b.rigidBody.mass\n\t\t: 0;\n\tconst totalInvMass = invMassA + invMassB;\n\n\t// Position correction\n\tif (totalInvMass > 0) {\n\t\tconst correctionScale = contact.depth / totalInvMass;\n\n\t\tif (invMassA > 0) {\n\t\t\tconst ltA = ecs.getComponent(a.entityId, 'localTransform3D');\n\t\t\tif (!ltA) return;\n\t\t\tconst corrA = correctionScale * invMassA;\n\t\t\tltA.x -= corrA * contact.normalX;\n\t\t\tltA.y -= corrA * contact.normalY;\n\t\t\tltA.z -= corrA * contact.normalZ;\n\t\t\t// Sync cached position so subsequent pairs in this frame use corrected values\n\t\t\ta.x = ltA.x;\n\t\t\ta.y = ltA.y;\n\t\t\ta.z = ltA.z;\n\t\t\tecs.markChanged(a.entityId, 'localTransform3D');\n\t\t}\n\n\t\tif (invMassB > 0) {\n\t\t\tconst ltB = ecs.getComponent(b.entityId, 'localTransform3D');\n\t\t\tif (!ltB) return;\n\t\t\tconst corrB = correctionScale * invMassB;\n\t\t\tltB.x += corrB * contact.normalX;\n\t\t\tltB.y += corrB * contact.normalY;\n\t\t\tltB.z += corrB * contact.normalZ;\n\t\t\tb.x = ltB.x;\n\t\t\tb.y = ltB.y;\n\t\t\tb.z = ltB.z;\n\t\t\tecs.markChanged(b.entityId, 'localTransform3D');\n\t\t}\n\n\t\t// Velocity response (impulse-based)\n\t\tconst relVelX = b.velocity.x - a.velocity.x;\n\t\tconst relVelY = b.velocity.y - a.velocity.y;\n\t\tconst relVelZ = b.velocity.z - a.velocity.z;\n\t\tconst velAlongNormal = relVelX * contact.normalX + relVelY * contact.normalY + relVelZ * contact.normalZ;\n\n\t\tif (velAlongNormal < 0) {\n\t\t\tconst restitution = Math.min(a.rigidBody.restitution, b.rigidBody.restitution);\n\t\t\tconst normalImpulse = -(1 + restitution) * velAlongNormal / totalInvMass;\n\t\t\tconst impA = normalImpulse * invMassA;\n\t\t\tconst impB = normalImpulse * invMassB;\n\n\t\t\ta.velocity.x -= impA * contact.normalX;\n\t\t\ta.velocity.y -= impA * contact.normalY;\n\t\t\ta.velocity.z -= impA * contact.normalZ;\n\t\t\tb.velocity.x += impB * contact.normalX;\n\t\t\tb.velocity.y += impB * contact.normalY;\n\t\t\tb.velocity.z += impB * contact.normalZ;\n\n\t\t\t// Friction (tangential impulse — project relative velocity onto tangent plane)\n\t\t\tconst tangentX = relVelX - velAlongNormal * contact.normalX;\n\t\t\tconst tangentY = relVelY - velAlongNormal * contact.normalY;\n\t\t\tconst tangentZ = relVelZ - velAlongNormal * contact.normalZ;\n\t\t\tconst tangentSpeed = Math.sqrt(tangentX * tangentX + tangentY * tangentY + tangentZ * tangentZ);\n\n\t\t\tif (tangentSpeed > 1e-6) {\n\t\t\t\tconst tangentNX = tangentX / tangentSpeed;\n\t\t\t\tconst tangentNY = tangentY / tangentSpeed;\n\t\t\t\tconst tangentNZ = tangentZ / tangentSpeed;\n\t\t\t\tconst friction = Math.sqrt(a.rigidBody.friction * b.rigidBody.friction);\n\t\t\t\tconst maxFrictionImpulse = friction * Math.abs(normalImpulse);\n\t\t\t\tconst tangentImpulse = Math.min(tangentSpeed / totalInvMass, maxFrictionImpulse);\n\t\t\t\tconst tanA = tangentImpulse * invMassA;\n\t\t\t\tconst tanB = tangentImpulse * invMassB;\n\n\t\t\t\ta.velocity.x += tanA * tangentNX;\n\t\t\t\ta.velocity.y += tanA * tangentNY;\n\t\t\t\ta.velocity.z += tanA * tangentNZ;\n\t\t\t\tb.velocity.x -= tanB * tangentNX;\n\t\t\t\tb.velocity.y -= tanB * tangentNY;\n\t\t\t\tb.velocity.z -= tanB * tangentNZ;\n\t\t\t}\n\t\t}\n\n\t\tecs.markChanged(a.entityId, 'velocity3D');\n\t\tecs.markChanged(b.entityId, 'velocity3D');\n\t}\n\n\t_physicsCollisionEvent.entityA = a.entityId;\n\t_physicsCollisionEvent.entityB = b.entityId;\n\t_physicsCollisionEvent.normalX = contact.normalX;\n\t_physicsCollisionEvent.normalY = contact.normalY;\n\t_physicsCollisionEvent.normalZ = contact.normalZ;\n\t_physicsCollisionEvent.depth = contact.depth;\n\tecs.eventBus.publish('physics3DCollision', _physicsCollisionEvent);\n}\n\n// ==================== Plugin Factory ====================\n\n/**\n * Create a 3D physics plugin for ECSpresso.\n *\n * Provides:\n * - Semi-implicit Euler integration (gravity, forces, drag → velocity3D → position)\n * - Impulse-based collision response with restitution and friction\n * - physics3DCollision events with contact normal and depth\n *\n * @example\n * ```typescript\n * const ecs = ECSpresso.create()\n * .withPlugin(createTransform3DPlugin())\n * .withPlugin(createPhysics3DPlugin({ gravity: { x: 0, y: -9.81, z: 0 } }))\n * .withFixedTimestep(1/60)\n * .build();\n *\n * ecs.spawn({\n * ...createTransform3D(0, 10, 0),\n * ...createRigidBody3D('dynamic', { mass: 1, restitution: 0.5 }),\n * velocity3D: { x: 0, y: 0, z: 0 },\n * ...createAABB3DCollider(1, 1, 1),\n * ...createCollisionLayer('player', ['ground']),\n * });\n * ```\n */\n\ntype Physics3DProvides<L extends string = never> = Physics3DOwnComponentTypes & Collision3DComponentTypes<L>;\n\nexport function createPhysics3DPlugin<L extends string = never, G extends string = 'physics3D', CG extends string = never>(\n\toptions?: Physics3DPluginOptions<G, CG> & { layers?: LayerFactories<Record<L, readonly string[]>> },\n) {\n\tconst {\n\t\tgravity = { x: 0, y: 0, z: 0 },\n\t\tsystemGroup = 'physics3D',\n\t\tcollisionSystemGroup,\n\t\tintegrationPriority = 1000,\n\t\tcollisionPriority = 900,\n\t\tphase = 'fixedUpdate',\n\t} = options ?? {};\n\n\treturn definePlugin('physics3D')\n\t\t.withComponentTypes<Physics3DProvides<L>>()\n\t\t.withEventTypes<Physics3DEventTypes>()\n\t\t.withResourceTypes<Physics3DResourceTypes>()\n\t\t.withLabels<'physics3D-integration' | 'physics3D-collision'>()\n\t\t.withGroups<G | CG>()\n\t\t.requires<Transform3DWorldConfig>()\n\t\t.install((world) => {\n\t\t\t// rigidBody3D requires velocity3D and force3D — auto-add with zero defaults\n\t\t\tworld.registerRequired('rigidBody3D', 'velocity3D', () => ({ x: 0, y: 0, z: 0 }));\n\t\t\tworld.registerRequired('rigidBody3D', 'force3D', () => ({ x: 0, y: 0, z: 0 }));\n\n\t\t\tworld.addResource('physics3DConfig', { gravity: { x: gravity.x, y: gravity.y, z: gravity.z } });\n\n\t\t\t// ==================== Integration System ====================\n\n\t\t\tworld\n\t\t\t\t.addSystem('physics3D-integration')\n\t\t\t\t.setPriority(integrationPriority)\n\t\t\t\t.inPhase(phase)\n\t\t\t\t.inGroup(systemGroup)\n\t\t\t\t.addQuery('bodies', {\n\t\t\t\t\twith: ['localTransform3D', 'velocity3D', 'rigidBody3D', 'force3D'],\n\t\t\t\t})\n\t\t\t\t.setProcess(({ queries, dt, ecs }) => {\n\t\t\t\t\tconst { gravity: g } = ecs.getResource('physics3DConfig');\n\t\t\t\t\tconst gx = g.x;\n\t\t\t\t\tconst gy = g.y;\n\t\t\t\t\tconst gz = g.z;\n\n\t\t\t\t\t// TODO(perf): no early-out for \"sleeping\" dynamic bodies — a packed\n\t\t\t\t\t// pile of resting entities still runs gravity/drag/force-clear/\n\t\t\t\t\t// markChanged every step. A sleep flag on RigidBody that latches\n\t\t\t\t\t// after N frames of near-zero velocity (and clears on impulse or\n\t\t\t\t\t// applied force) would let most of a stabilized scene skip the\n\t\t\t\t\t// full per-entity body of this loop. Needs collision response to\n\t\t\t\t\t// wake sleepers back up; keep in sync with physics2D when landed.\n\t\t\t\t\tfor (const entity of queries.bodies) {\n\t\t\t\t\t\tconst { localTransform3D, velocity3D, rigidBody3D, force3D } = entity.components;\n\n\t\t\t\t\t\t// Static bodies: skip entirely\n\t\t\t\t\t\tif (rigidBody3D.type === 'static') continue;\n\n\t\t\t\t\t\t// Dynamic bodies: apply gravity, forces, drag\n\t\t\t\t\t\tif (rigidBody3D.type === 'dynamic') {\n\t\t\t\t\t\t\t// 1. Gravity\n\t\t\t\t\t\t\tconst gsdt = rigidBody3D.gravityScale * dt;\n\t\t\t\t\t\t\tvelocity3D.x += gx * gsdt;\n\t\t\t\t\t\t\tvelocity3D.y += gy * gsdt;\n\t\t\t\t\t\t\tvelocity3D.z += gz * gsdt;\n\n\t\t\t\t\t\t\t// 2. Forces (F = ma → a = F/m)\n\t\t\t\t\t\t\tconst mass = rigidBody3D.mass;\n\t\t\t\t\t\t\tif (mass > 0 && mass !== Infinity) {\n\t\t\t\t\t\t\t\tconst invMassDt = dt / mass;\n\t\t\t\t\t\t\t\tvelocity3D.x += force3D.x * invMassDt;\n\t\t\t\t\t\t\t\tvelocity3D.y += force3D.y * invMassDt;\n\t\t\t\t\t\t\t\tvelocity3D.z += force3D.z * invMassDt;\n\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\t// 3. Drag\n\t\t\t\t\t\t\tif (rigidBody3D.drag > 0) {\n\t\t\t\t\t\t\t\tconst damping = Math.max(0, 1 - rigidBody3D.drag * dt);\n\t\t\t\t\t\t\t\tvelocity3D.x *= damping;\n\t\t\t\t\t\t\t\tvelocity3D.y *= damping;\n\t\t\t\t\t\t\t\tvelocity3D.z *= damping;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\t// Both dynamic and kinematic: integrate position\n\t\t\t\t\t\tlocalTransform3D.x += velocity3D.x * dt;\n\t\t\t\t\t\tlocalTransform3D.y += velocity3D.y * dt;\n\t\t\t\t\t\tlocalTransform3D.z += velocity3D.z * dt;\n\n\t\t\t\t\t\t// Clear accumulated forces\n\t\t\t\t\t\tforce3D.x = 0;\n\t\t\t\t\t\tforce3D.y = 0;\n\t\t\t\t\t\tforce3D.z = 0;\n\n\t\t\t\t\t\tecs.markChanged(entity.id, 'localTransform3D');\n\t\t\t\t\t}\n\t\t\t\t});\n\n\t\t\t// ==================== Collision System ====================\n\n\t\t\tconst collisionSystem = world\n\t\t\t\t.addSystem('physics3D-collision')\n\t\t\t\t.setPriority(collisionPriority)\n\t\t\t\t.inPhase(phase)\n\t\t\t\t.inGroup(systemGroup);\n\n\t\t\tif (collisionSystemGroup) {\n\t\t\t\tcollisionSystem.inGroup(collisionSystemGroup);\n\t\t\t}\n\n\t\t\t// Grow-only pool of Physics3DColliderInfo slots reused across frames.\n\t\t\t// Steady-state: zero allocations per frame once the pool is warm.\n\t\t\tconst colliderPool: Physics3DColliderInfo<L>[] = [];\n\t\t\t// Reusable entityId → collider lookup for the broadphase path.\n\t\t\tconst broadphaseMap = new Map<number, Physics3DColliderInfo<L>>();\n\t\t\t// Cached spatial index reference (resolved once on first frame).\n\t\t\tlet cachedSI: SpatialIndex3D | undefined;\n\t\t\tlet siResolved = false;\n\n\t\t\tconst ensureSlot = (idx: number, entityId: number, layer: L, collidesWith: readonly L[], rigidBody: RigidBody, velocity: Vector3D): Physics3DColliderInfo<L> => {\n\t\t\t\tlet slot = colliderPool[idx];\n\t\t\t\tif (!slot) {\n\t\t\t\t\tslot = {\n\t\t\t\t\t\tentityId,\n\t\t\t\t\t\tx: 0,\n\t\t\t\t\t\ty: 0,\n\t\t\t\t\t\tz: 0,\n\t\t\t\t\t\tlayer,\n\t\t\t\t\t\tcollidesWith,\n\t\t\t\t\t\tlayerBit: 0,\n\t\t\t\t\t\tcollidesWithMask: 0,\n\t\t\t\t\t\tshape: AABB3D_SHAPE,\n\t\t\t\t\t\thalfWidth: 0,\n\t\t\t\t\t\thalfHeight: 0,\n\t\t\t\t\t\thalfDepth: 0,\n\t\t\t\t\t\tradius: 0,\n\t\t\t\t\t\trigidBody,\n\t\t\t\t\t\tvelocity,\n\t\t\t\t\t};\n\t\t\t\t\tcolliderPool[idx] = slot;\n\t\t\t\t} else {\n\t\t\t\t\tslot.rigidBody = rigidBody;\n\t\t\t\t\tslot.velocity = velocity;\n\t\t\t\t}\n\t\t\t\treturn slot;\n\t\t\t};\n\n\t\t\tcollisionSystem\n\t\t\t\t.addQuery('aabbOnly', {\n\t\t\t\t\twith: ['localTransform3D', 'rigidBody3D', 'velocity3D', 'collisionLayer', 'aabb3DCollider'],\n\t\t\t\t\twithout: ['sphereCollider'],\n\t\t\t\t})\n\t\t\t\t.addQuery('sphereOnly', {\n\t\t\t\t\twith: ['localTransform3D', 'rigidBody3D', 'velocity3D', 'collisionLayer', 'sphereCollider'],\n\t\t\t\t\twithout: ['aabb3DCollider'],\n\t\t\t\t})\n\t\t\t\t.addQuery('both', {\n\t\t\t\t\twith: ['localTransform3D', 'rigidBody3D', 'velocity3D', 'collisionLayer', 'aabb3DCollider', 'sphereCollider'],\n\t\t\t\t})\n\t\t\t\t.setProcess(({ queries, ecs }) => {\n\t\t\t\t\tlet count = 0;\n\n\t\t\t\t\tfor (const entity of queries.aabbOnly) {\n\t\t\t\t\t\tconst { localTransform3D, rigidBody3D, velocity3D, collisionLayer, aabb3DCollider } = entity.components;\n\t\t\t\t\t\tconst slot = ensureSlot(count, entity.id, collisionLayer.layer, collisionLayer.collidesWith, rigidBody3D, velocity3D);\n\t\t\t\t\t\tif (!fillBaseColliderInfo3D(\n\t\t\t\t\t\t\tslot,\n\t\t\t\t\t\t\tentity.id, localTransform3D.x, localTransform3D.y, localTransform3D.z,\n\t\t\t\t\t\t\tcollisionLayer.layer, collisionLayer.collidesWith,\n\t\t\t\t\t\t\taabb3DCollider, undefined,\n\t\t\t\t\t\t)) continue;\n\t\t\t\t\t\tcount++;\n\t\t\t\t\t}\n\n\t\t\t\t\tfor (const entity of queries.sphereOnly) {\n\t\t\t\t\t\tconst { localTransform3D, rigidBody3D, velocity3D, collisionLayer, sphereCollider } = entity.components;\n\t\t\t\t\t\tconst slot = ensureSlot(count, entity.id, collisionLayer.layer, collisionLayer.collidesWith, rigidBody3D, velocity3D);\n\t\t\t\t\t\tif (!fillBaseColliderInfo3D(\n\t\t\t\t\t\t\tslot,\n\t\t\t\t\t\t\tentity.id, localTransform3D.x, localTransform3D.y, localTransform3D.z,\n\t\t\t\t\t\t\tcollisionLayer.layer, collisionLayer.collidesWith,\n\t\t\t\t\t\t\tundefined, sphereCollider,\n\t\t\t\t\t\t)) continue;\n\t\t\t\t\t\tcount++;\n\t\t\t\t\t}\n\n\t\t\t\t\t// fillBaseColliderInfo3D picks aabb when both present — preserves prior precedence.\n\t\t\t\t\tfor (const entity of queries.both) {\n\t\t\t\t\t\tconst { localTransform3D, rigidBody3D, velocity3D, collisionLayer, aabb3DCollider, sphereCollider } = entity.components;\n\t\t\t\t\t\tconst slot = ensureSlot(count, entity.id, collisionLayer.layer, collisionLayer.collidesWith, rigidBody3D, velocity3D);\n\t\t\t\t\t\tif (!fillBaseColliderInfo3D(\n\t\t\t\t\t\t\tslot,\n\t\t\t\t\t\t\tentity.id, localTransform3D.x, localTransform3D.y, localTransform3D.z,\n\t\t\t\t\t\t\tcollisionLayer.layer, collisionLayer.collidesWith,\n\t\t\t\t\t\t\taabb3DCollider, sphereCollider,\n\t\t\t\t\t\t)) continue;\n\t\t\t\t\t\tcount++;\n\t\t\t\t\t}\n\n\t\t\t\t\tif (!siResolved) {\n\t\t\t\t\t\tcachedSI = ecs.tryGetResource<SpatialIndex3D>('spatialIndex3D');\n\t\t\t\t\t\tsiResolved = true;\n\t\t\t\t\t}\n\t\t\t\t\tdetectCollisions3D(colliderPool, count, broadphaseMap, cachedSI, resolvePhysicsContact3D, ecs);\n\t\t\t\t});\n\t\t});\n}\n",
6
6
  "/**\n * Lazy monotonic registry mapping layer name → unique bit. Lets pair\n * filtering use a single `(a.collidesWithMask & b.layerBit)` check\n * instead of `Array.includes` on every collision pair.\n *\n * One registry per dimension (2D and 3D) — user-defined layer namespaces\n * are independent, so bits should not be shared across systems.\n *\n * Maximum 32 layers per registry (one per bit in a 32-bit signed int).\n * Crossing the limit throws on the next `getLayerBit` call.\n */\nexport interface LayerBitRegistry {\n\tgetLayerBit(layer: string): number;\n\t/** OR of `getLayerBit` for every entry. Cached by array reference. */\n\tgetCollidesWithMask(collidesWith: readonly string[]): number;\n}\n\nexport function createLayerBitRegistry(label: string): LayerBitRegistry {\n\tconst layerBits = new Map<string, number>();\n\tconst maskCache = new WeakMap<readonly string[], number>();\n\tlet nextBit = 1;\n\n\tfunction getLayerBit(layer: string): number {\n\t\tconst existing = layerBits.get(layer);\n\t\tif (existing !== undefined) return existing;\n\t\tif (nextBit === 0) {\n\t\t\tthrow new Error(\n\t\t\t\t`[ecspresso] ${label} layer bitmask overflow: more than 32 distinct layers registered`,\n\t\t\t);\n\t\t}\n\t\tconst bit = nextBit;\n\t\tlayerBits.set(layer, bit);\n\t\t// `<<= 1` rolls 1<<31 to 0, which is detected on the next call.\n\t\tnextBit <<= 1;\n\t\treturn bit;\n\t}\n\n\tfunction getCollidesWithMask(collidesWith: readonly string[]): number {\n\t\tconst cached = maskCache.get(collidesWith);\n\t\tif (cached !== undefined) return cached;\n\t\tlet mask = 0;\n\t\tfor (let i = 0; i < collidesWith.length; i++) {\n\t\t\tmask |= getLayerBit(collidesWith[i]!);\n\t\t}\n\t\tmaskCache.set(collidesWith, mask);\n\t\treturn mask;\n\t}\n\n\treturn { getLayerBit, getCollidesWithMask };\n}\n",
7
7
  "/**\n * Shared Narrowphase Module — 3D\n *\n * Provides contact-computing narrowphase tests and a generic collision\n * iteration pipeline for 3D collider pairs (AABB3D + Sphere).\n *\n * Mirrors the 2D narrowphase (`narrowphase.ts`) with an added Z axis.\n */\n\nimport type { SpatialIndex3D } from './spatial-hash3D';\nimport { createLayerBitRegistry } from './layer-bit-registry';\n\n// ==================== Contact3D ====================\n\n/**\n * Contact result from a 3D narrowphase test. Normal points from A toward B.\n *\n * Narrowphase functions use this as an out-parameter: the caller owns the\n * struct, the function writes fields in place and returns `true` on hit.\n * The `onContact` callback in `detectCollisions3D` receives a shared module-\n * level instance — **subscribers must consume it synchronously and must not\n * retain the reference across frames**.\n */\nexport interface Contact3D {\n\tnormalX: number;\n\tnormalY: number;\n\tnormalZ: number;\n\t/** Penetration depth (positive = overlapping) */\n\tdepth: number;\n}\n\n/**\n * Module-level reusable Contact3D passed down from `detectCollisions3D` into\n * narrowphase tests and forwarded to the `onContact` callback. Reused across\n * every pair in every frame — zero allocation in the narrowphase hot path.\n */\nconst _sharedContact: Contact3D = { normalX: 0, normalY: 0, normalZ: 0, depth: 0 };\n\n// ==================== BaseColliderInfo3D ====================\n\n/** Collider shape discriminator for the flattened BaseColliderInfo3D layout. */\nexport const AABB3D_SHAPE = 0;\nexport const SPHERE_SHAPE = 1;\nexport type ColliderShape3D = typeof AABB3D_SHAPE | typeof SPHERE_SHAPE;\n\n/**\n * Minimum collider data shared by 3D collision and physics bundles.\n *\n * Flat layout (no nested sub-objects): the `shape` discriminator tells you\n * whether to read `halfWidth`/`halfHeight`/`halfDepth` (AABB3D) or `radius`\n * (Sphere). Unused fields are set to 0.\n *\n * Pool-friendly — all fields are assigned in place each frame.\n */\nexport interface BaseColliderInfo3D<L extends string = string> {\n\tentityId: number;\n\tx: number;\n\ty: number;\n\tz: number;\n\tlayer: L;\n\tcollidesWith: readonly L[];\n\t/**\n\t * Bit assigned to `layer` from the lazy layer registry. Populated by\n\t * `fillBaseColliderInfo3D`. Used together with `collidesWithMask` to\n\t * replace per-pair `Array.includes` layer checks with a single AND.\n\t */\n\tlayerBit: number;\n\t/** OR of `getLayerBit3D` for every entry in `collidesWith`. */\n\tcollidesWithMask: number;\n\tshape: ColliderShape3D;\n\thalfWidth: number;\n\thalfHeight: number;\n\thalfDepth: number;\n\tradius: number;\n}\n\n// ==================== Layer Bit Registry ====================\n\n// Independent from the 2D registry: 2D and 3D layer namespaces are\n// defined separately by user code, so bits should not collide.\nconst _layerRegistry = createLayerBitRegistry('3D collision');\nexport const getLayerBit3D = _layerRegistry.getLayerBit;\nexport const getCollidesWithMask3D = _layerRegistry.getCollidesWithMask;\n\n// ==================== Collider Construction ====================\n\n/**\n * Populate a `BaseColliderInfo3D` slot in place from raw component data.\n * Returns `true` if the slot was filled, `false` if the entity has no\n * collider (caller should skip it).\n *\n * If an entity has both AABB3D and sphere colliders, AABB3D wins and only\n * the AABB3D offset is applied.\n */\nexport function fillBaseColliderInfo3D<L extends string>(\n\tinfo: BaseColliderInfo3D<L>,\n\tentityId: number,\n\tx: number,\n\ty: number,\n\tz: number,\n\tlayer: L,\n\tcollidesWith: readonly L[],\n\taabb3D: { width: number; height: number; depth: number; offsetX?: number; offsetY?: number; offsetZ?: number } | undefined,\n\tsphere: { radius: number; offsetX?: number; offsetY?: number; offsetZ?: number } | undefined,\n): boolean {\n\tinfo.entityId = entityId;\n\tinfo.layer = layer;\n\tinfo.collidesWith = collidesWith;\n\tinfo.layerBit = getLayerBit3D(layer);\n\tinfo.collidesWithMask = getCollidesWithMask3D(collidesWith);\n\n\tif (aabb3D) {\n\t\tinfo.x = x + (aabb3D.offsetX ?? 0);\n\t\tinfo.y = y + (aabb3D.offsetY ?? 0);\n\t\tinfo.z = z + (aabb3D.offsetZ ?? 0);\n\t\tinfo.shape = AABB3D_SHAPE;\n\t\tinfo.halfWidth = aabb3D.width / 2;\n\t\tinfo.halfHeight = aabb3D.height / 2;\n\t\tinfo.halfDepth = aabb3D.depth / 2;\n\t\tinfo.radius = 0;\n\t\treturn true;\n\t}\n\n\tif (sphere) {\n\t\tinfo.x = x + (sphere.offsetX ?? 0);\n\t\tinfo.y = y + (sphere.offsetY ?? 0);\n\t\tinfo.z = z + (sphere.offsetZ ?? 0);\n\t\tinfo.shape = SPHERE_SHAPE;\n\t\tinfo.halfWidth = 0;\n\t\tinfo.halfHeight = 0;\n\t\tinfo.halfDepth = 0;\n\t\tinfo.radius = sphere.radius;\n\t\treturn true;\n\t}\n\n\treturn false;\n}\n\n// ==================== Spatial Index Lookup ====================\n\n/**\n * Retrieve the optional spatialIndex3D resource, returning undefined when absent.\n * Centralizes the cross-plugin typed lookup so individual plugins don't each\n * need to import SpatialIndex3D or repeat the tryGetResource pattern.\n */\nexport function tryGetSpatialIndex3D(\n\ttryGetResource: <T>(key: string) => T | undefined,\n): SpatialIndex3D | undefined {\n\treturn tryGetResource<SpatialIndex3D>('spatialIndex3D');\n}\n\n// ==================== Narrowphase Tests ====================\n\n/**\n * Write an AABB3D-vs-AABB3D contact into `out`. Returns `true` if the\n * shapes overlap (out was filled), `false` otherwise.\n *\n * Resolves along the axis with minimum penetration depth.\n */\nexport function computeAABB3DvsAABB3D(\n\tax: number, ay: number, az: number, ahw: number, ahh: number, ahd: number,\n\tbx: number, by: number, bz: number, bhw: number, bhh: number, bhd: number,\n\tout: Contact3D,\n): boolean {\n\tconst dx = bx - ax;\n\tconst dy = by - ay;\n\tconst dz = bz - az;\n\tconst overlapX = (ahw + bhw) - Math.abs(dx);\n\tconst overlapY = (ahh + bhh) - Math.abs(dy);\n\tconst overlapZ = (ahd + bhd) - Math.abs(dz);\n\n\tif (overlapX <= 0 || overlapY <= 0 || overlapZ <= 0) return false;\n\n\tif (overlapX <= overlapY && overlapX <= overlapZ) {\n\t\tout.normalX = dx >= 0 ? 1 : -1;\n\t\tout.normalY = 0;\n\t\tout.normalZ = 0;\n\t\tout.depth = overlapX;\n\t\treturn true;\n\t}\n\n\tif (overlapY <= overlapZ) {\n\t\tout.normalX = 0;\n\t\tout.normalY = dy >= 0 ? 1 : -1;\n\t\tout.normalZ = 0;\n\t\tout.depth = overlapY;\n\t\treturn true;\n\t}\n\n\tout.normalX = 0;\n\tout.normalY = 0;\n\tout.normalZ = dz >= 0 ? 1 : -1;\n\tout.depth = overlapZ;\n\treturn true;\n}\n\n/**\n * Write a sphere-vs-sphere contact into `out`. Returns `true` if the\n * spheres overlap.\n */\nexport function computeSphereVsSphere(\n\tax: number, ay: number, az: number, ar: number,\n\tbx: number, by: number, bz: number, br: number,\n\tout: Contact3D,\n): boolean {\n\tconst dx = bx - ax;\n\tconst dy = by - ay;\n\tconst dz = bz - az;\n\tconst distSq = dx * dx + dy * dy + dz * dz;\n\tconst radiusSum = ar + br;\n\n\tif (distSq >= radiusSum * radiusSum) return false;\n\n\tconst dist = Math.sqrt(distSq);\n\tif (dist === 0) {\n\t\tout.normalX = 1;\n\t\tout.normalY = 0;\n\t\tout.normalZ = 0;\n\t\tout.depth = radiusSum;\n\t\treturn true;\n\t}\n\tout.normalX = dx / dist;\n\tout.normalY = dy / dist;\n\tout.normalZ = dz / dist;\n\tout.depth = radiusSum - dist;\n\treturn true;\n}\n\n/**\n * Write an AABB3D-vs-sphere contact into `out`. Returns `true` if the\n * shapes overlap.\n *\n * Uses closest-point-on-AABB to sphere center. When the sphere center\n * is inside the AABB, resolves along the axis with minimum push distance.\n */\nexport function computeAABB3DvsSphere(\n\taabbX: number, aabbY: number, aabbZ: number, ahw: number, ahh: number, ahd: number,\n\tsphereX: number, sphereY: number, sphereZ: number, radius: number,\n\tout: Contact3D,\n): boolean {\n\tconst closestX = Math.max(aabbX - ahw, Math.min(sphereX, aabbX + ahw));\n\tconst closestY = Math.max(aabbY - ahh, Math.min(sphereY, aabbY + ahh));\n\tconst closestZ = Math.max(aabbZ - ahd, Math.min(sphereZ, aabbZ + ahd));\n\n\tconst dx = sphereX - closestX;\n\tconst dy = sphereY - closestY;\n\tconst dz = sphereZ - closestZ;\n\tconst distSq = dx * dx + dy * dy + dz * dz;\n\n\tif (distSq >= radius * radius) return false;\n\n\t// Sphere center inside AABB — resolve along minimum push axis\n\tif (distSq === 0) {\n\t\tconst pushLeft = (sphereX - (aabbX - ahw));\n\t\tconst pushRight = ((aabbX + ahw) - sphereX);\n\t\tconst pushUp = (sphereY - (aabbY - ahh));\n\t\tconst pushDown = ((aabbY + ahh) - sphereY);\n\t\tconst pushFront = (sphereZ - (aabbZ - ahd));\n\t\tconst pushBack = ((aabbZ + ahd) - sphereZ);\n\t\tconst minPush = Math.min(pushLeft, pushRight, pushUp, pushDown, pushFront, pushBack);\n\n\t\tif (minPush === pushRight) {\n\t\t\tout.normalX = 1; out.normalY = 0; out.normalZ = 0; out.depth = pushRight + radius;\n\t\t\treturn true;\n\t\t}\n\t\tif (minPush === pushLeft) {\n\t\t\tout.normalX = -1; out.normalY = 0; out.normalZ = 0; out.depth = pushLeft + radius;\n\t\t\treturn true;\n\t\t}\n\t\tif (minPush === pushDown) {\n\t\t\tout.normalX = 0; out.normalY = 1; out.normalZ = 0; out.depth = pushDown + radius;\n\t\t\treturn true;\n\t\t}\n\t\tif (minPush === pushUp) {\n\t\t\tout.normalX = 0; out.normalY = -1; out.normalZ = 0; out.depth = pushUp + radius;\n\t\t\treturn true;\n\t\t}\n\t\tif (minPush === pushBack) {\n\t\t\tout.normalX = 0; out.normalY = 0; out.normalZ = 1; out.depth = pushBack + radius;\n\t\t\treturn true;\n\t\t}\n\t\tout.normalX = 0; out.normalY = 0; out.normalZ = -1; out.depth = pushFront + radius;\n\t\treturn true;\n\t}\n\n\tconst dist = Math.sqrt(distSq);\n\tout.normalX = dx / dist;\n\tout.normalY = dy / dist;\n\tout.normalZ = dz / dist;\n\tout.depth = radius - dist;\n\treturn true;\n}\n\n// ==================== Contact Dispatcher ====================\n\n/**\n * Dispatch to the correct narrowphase function for the given pair and\n * write the contact into `out`. Returns `true` if the pair overlaps.\n */\nexport function computeContact3D(a: BaseColliderInfo3D, b: BaseColliderInfo3D, out: Contact3D): boolean {\n\tif (a.shape === AABB3D_SHAPE && b.shape === AABB3D_SHAPE) {\n\t\treturn computeAABB3DvsAABB3D(\n\t\t\ta.x, a.y, a.z, a.halfWidth, a.halfHeight, a.halfDepth,\n\t\t\tb.x, b.y, b.z, b.halfWidth, b.halfHeight, b.halfDepth,\n\t\t\tout,\n\t\t);\n\t}\n\n\tif (a.shape === SPHERE_SHAPE && b.shape === SPHERE_SHAPE) {\n\t\treturn computeSphereVsSphere(\n\t\t\ta.x, a.y, a.z, a.radius,\n\t\t\tb.x, b.y, b.z, b.radius,\n\t\t\tout,\n\t\t);\n\t}\n\n\tif (a.shape === AABB3D_SHAPE && b.shape === SPHERE_SHAPE) {\n\t\treturn computeAABB3DvsSphere(\n\t\t\ta.x, a.y, a.z, a.halfWidth, a.halfHeight, a.halfDepth,\n\t\t\tb.x, b.y, b.z, b.radius,\n\t\t\tout,\n\t\t);\n\t}\n\n\t// a is Sphere, b is AABB3D — compute as AABB3D-vs-Sphere, then flip normal\n\tif (!computeAABB3DvsSphere(\n\t\tb.x, b.y, b.z, b.halfWidth, b.halfHeight, b.halfDepth,\n\t\ta.x, a.y, a.z, a.radius,\n\t\tout,\n\t)) return false;\n\tout.normalX = -out.normalX;\n\tout.normalY = -out.normalY;\n\tout.normalZ = -out.normalZ;\n\treturn true;\n}\n\n// ==================== Collision Iteration ====================\n\n/** Module-level reusable array for broadphase candidates. */\nconst _broadphaseCandidates: number[] = [];\n\nlet _bruteForceWarned = false;\nconst BRUTE_FORCE_WARN_THRESHOLD = 50;\n\n/**\n * Generic 3D collision detection pipeline: brute-force or broadphase,\n * with layer filtering and contact computation.\n *\n * `count` is the number of live entries at the front of `colliders`.\n * The array itself may be a grow-only pool — only indices `[0, count)`\n * are iterated, so trailing pool slots are ignored.\n *\n * `workingMap` is a caller-owned `Map<number, I>` used by the broadphase\n * path as an entityId → collider lookup. It is cleared and repopulated on\n * each call; callers should allocate it once and pass the same instance\n * every frame.\n *\n * Uses a context parameter forwarded to the callback to avoid\n * per-frame closure allocation.\n */\nexport function detectCollisions3D<I extends BaseColliderInfo3D, C>(\n\tcolliders: I[],\n\tcount: number,\n\tworkingMap: Map<number, I>,\n\tspatialIndex: SpatialIndex3D | undefined,\n\tonContact: (a: I, b: I, contact: Contact3D, context: C) => void,\n\tcontext: C,\n): void {\n\tif (spatialIndex) {\n\t\tbroadphaseDetect(colliders, count, workingMap, spatialIndex, onContact, context);\n\t} else {\n\t\tbruteForceDetect(colliders, count, onContact, context);\n\t}\n}\n\nfunction bruteForceDetect<I extends BaseColliderInfo3D, C>(\n\tcolliders: I[],\n\tcount: number,\n\tonContact: (a: I, b: I, contact: Contact3D, context: C) => void,\n\tcontext: C,\n): void {\n\tif (!_bruteForceWarned && count >= BRUTE_FORCE_WARN_THRESHOLD) {\n\t\t_bruteForceWarned = true;\n\t\tconsole.warn(\n\t\t\t`[ecspresso] 3D collision detection is using O(n²) brute force with ${count} colliders. ` +\n\t\t\t`For better performance, install createSpatialIndex3DPlugin() alongside your collision or physics3D plugin.`,\n\t\t);\n\t}\n\n\tfor (let i = 0; i < count; i++) {\n\t\tconst a = colliders[i];\n\t\tif (!a) continue;\n\n\t\tfor (let j = i + 1; j < count; j++) {\n\t\t\tconst b = colliders[j];\n\t\t\tif (!b) continue;\n\n\t\t\tif (((a.collidesWithMask & b.layerBit) | (b.collidesWithMask & a.layerBit)) === 0) continue;\n\n\t\t\tif (!computeContact3D(a, b, _sharedContact)) continue;\n\n\t\t\tonContact(a, b, _sharedContact, context);\n\t\t}\n\t}\n}\n\nfunction broadphaseDetect<I extends BaseColliderInfo3D, C>(\n\tcolliders: I[],\n\tcount: number,\n\tcolliderMap: Map<number, I>,\n\tspatialIndex: SpatialIndex3D,\n\tonContact: (a: I, b: I, contact: Contact3D, context: C) => void,\n\tcontext: C,\n): void {\n\tcolliderMap.clear();\n\tfor (let i = 0; i < count; i++) {\n\t\tconst c = colliders[i];\n\t\tif (!c) continue;\n\t\tcolliderMap.set(c.entityId, c);\n\t}\n\n\tfor (let i = 0; i < count; i++) {\n\t\tconst a = colliders[i];\n\t\tif (!a) continue;\n\n\t\tconst aHalfW = a.shape === AABB3D_SHAPE ? a.halfWidth : a.radius;\n\t\tconst aHalfH = a.shape === AABB3D_SHAPE ? a.halfHeight : a.radius;\n\t\tconst aHalfD = a.shape === AABB3D_SHAPE ? a.halfDepth : a.radius;\n\n\t\t_broadphaseCandidates.length = 0;\n\t\tspatialIndex.queryBoxInto(\n\t\t\ta.x - aHalfW, a.y - aHalfH, a.z - aHalfD,\n\t\t\ta.x + aHalfW, a.y + aHalfH, a.z + aHalfD,\n\t\t\t_broadphaseCandidates,\n\t\t\ta.entityId,\n\t\t);\n\n\t\tfor (const bId of _broadphaseCandidates) {\n\t\t\tconst b = colliderMap.get(bId);\n\t\t\tif (!b) continue;\n\n\t\t\tif (((a.collidesWithMask & b.layerBit) | (b.collidesWithMask & a.layerBit)) === 0) continue;\n\n\t\t\tif (!computeContact3D(a, b, _sharedContact)) continue;\n\n\t\t\tonContact(a, b, _sharedContact, context);\n\t\t}\n\t}\n}\n"
8
8
  ],
9
- "mappings": "2PAYA,uBAAS,kBCKF,SAAS,CAAsB,CAAC,EAAiC,CACvE,IAAM,EAAY,IAAI,IAChB,EAAY,IAAI,QAClB,EAAU,EAEd,SAAS,CAAW,CAAC,EAAuB,CAC3C,IAAM,EAAW,EAAU,IAAI,CAAK,EACpC,GAAI,IAAa,OAAW,OAAO,EACnC,GAAI,IAAY,EACf,MAAU,MACT,eAAe,mEAChB,EAED,IAAM,EAAM,EAIZ,OAHA,EAAU,IAAI,EAAO,CAAG,EAExB,IAAY,EACL,EAGR,SAAS,CAAmB,CAAC,EAAyC,CACrE,IAAM,EAAS,EAAU,IAAI,CAAY,EACzC,GAAI,IAAW,OAAW,OAAO,EACjC,IAAI,EAAO,EACX,QAAS,EAAI,EAAG,EAAI,EAAa,OAAQ,IACxC,GAAQ,EAAY,EAAa,EAAG,EAGrC,OADA,EAAU,IAAI,EAAc,CAAI,EACzB,EAGR,MAAO,CAAE,cAAa,qBAAoB,ECZ3C,IAAM,EAA4B,CAAE,QAAS,EAAG,QAAS,EAAG,QAAS,EAAG,MAAO,CAAE,EAKpE,EAAe,EACf,EAAe,EAsCtB,EAAiB,EAAuB,cAAc,EAC/C,EAAgB,EAAe,YAC/B,EAAwB,EAAe,oBAY7C,SAAS,CAAwC,CACvD,EACA,EACA,EACA,EACA,EACA,EACA,EACA,EACA,EACU,CAOV,GANA,EAAK,SAAW,EAChB,EAAK,MAAQ,EACb,EAAK,aAAe,EACpB,EAAK,SAAW,EAAc,CAAK,EACnC,EAAK,iBAAmB,EAAsB,CAA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EAAQ,SAAU,CACtC,IAAQ,mBAAkB,cAAa,aAAY,iBAAgB,kBAAmB,EAAO,WACvF,EAAO,EAAW,EAAO,EAAO,GAAI,EAAe,MAAO,EAAe,aAAc,EAAa,CAAU,EACpH,GAAI,CAAC,EACJ,EACA,EAAO,GAAI,EAAiB,EAAG,EAAiB,EAAG,EAAiB,EACpE,EAAe,MAAO,EAAe,aACrC,EAAgB,MACjB,EAAG,SACH,IAGD,QAAW,KAAU,EAAQ,WAAY,CACxC,IAAQ,mBAAkB,cAAa,aAAY,iBAAgB,kBAAmB,EAAO,WACvF,EAAO,EAAW,EAAO,EAAO,GAAI,EAAe,MAAO,EAAe,aAAc,EAAa,CAAU,EACpH,GAAI,CAAC,EACJ,EACA,EAAO,GAAI,EAAiB,EAAG,EAAiB,EAAG,EAAiB,EACpE,EAAe,MAAO,EAAe,aACrC,OAAW,CACZ,EAAG,SACH,IAID,QAAW,KAAU,EAAQ,KAAM,CAClC,IAAQ,mBAAkB,cAAa,aAAY,iBAAgB,iBAAgB,kBAAmB,EAAO,WACvG,EAAO,EAAW,EAAO,EAAO,GAAI,EAAe,MAAO,EAAe,aAAc,EAAa,CAAU,EACpH,GAAI,CAAC,EACJ,EACA,EAAO,GAAI,EAAiB,EAAG,EAAiB,EAAG,EAAiB,EACpE,EAAe,MAAO,EAAe,aACrC,EAAgB,CACjB,EAAG,SACH,IAGD,GAAI,CAAC,EACJ,EAAW,EAAI,eAA+B,gBAAgB,EAC9D,EAAa,GAEd,EAAmB,EAAc,EAAO,EAAe,EAAU,EAAyB,CAAG,EAC7F,EACF",
10
+ "debugId": "FABAD286A305821E64756E2164756E21",
11
11
  "names": []
12
12
  }
@@ -24,7 +24,25 @@ export interface SpatialIndexPluginOptions<G extends string = 'spatialIndex'> {
24
24
  systemGroup?: G;
25
25
  /** Priority for rebuild systems (default: 2000, before collision) */
26
26
  priority?: number;
27
- /** Phases to register rebuild systems in (default: ['fixedUpdate', 'postUpdate']) */
27
+ /**
28
+ * Phases to register rebuild systems in (default: ['fixedUpdate', 'postUpdate']).
29
+ *
30
+ * When both phases are registered, the `postUpdate` rebuild is
31
+ * automatically skipped on cycles where:
32
+ * 1. The `fixedUpdate` rebuild already ran this cycle, AND
33
+ * 2. No entity hierarchy exists.
34
+ *
35
+ * In flat-hierarchy scenes `transform-propagation` copies
36
+ * `localTransform → worldTransform` unchanged, so the postUpdate
37
+ * grid would duplicate the fixedUpdate one. The skip is automatic
38
+ * and re-engages once any parent relationship is set, or whenever
39
+ * `fixedUpdate` doesn't run that cycle (sub-fixed-DT frames).
40
+ *
41
+ * Edge case: if you write to `worldTransform` directly between
42
+ * phases without using `transform-propagation` or entity hierarchy,
43
+ * the auto-skip will leave your writes unindexed. Set
44
+ * `phases: ['postUpdate']` explicitly to bypass the auto-skip.
45
+ */
28
46
  phases?: ReadonlyArray<SpatialIndexPhase>;
29
47
  }
30
48
  /**
@@ -1,4 +1,4 @@
1
- var v=((j)=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(j,{get:(A,B)=>(typeof require<"u"?require:A)[B]}):j)(function(j){if(typeof require<"u")return require.apply(this,arguments);throw Error('Dynamic require of "'+j+'" is not supported')});import{definePlugin as k}from"ecspresso";function w(j,A){return j*73856093^A*19349663}function R(j){return{cellSize:j,invCellSize:1/j,cells:new Map,entries:[],_aliveGen:0,_queryGen:0}}function S(j){j._aliveGen++}function H(j,A,B,D,J,O){let U=j._aliveGen,T=j.entries[A],V;if(T)T.x=B,T.y=D,T.halfW=J,T.halfH=O,T._aliveGen=U,V=T;else V={entityId:A,x:B,y:D,halfW:J,halfH:O,_lastSeenGen:0,_aliveGen:U},j.entries[A]=V;let Z=j.invCellSize,E=Math.floor((B-J)*Z),L=Math.floor((B+J)*Z),_=Math.floor((D-O)*Z),$=Math.floor((D+O)*Z);for(let K=E;K<=L;K++)for(let M=_;M<=$;M++){let F=w(K,M),N=j.cells.get(F);if(N&&N._gen===U)N.push(V);else if(N)N.length=0,N._gen=U,N.push(V);else{let P=[V];P._gen=U,j.cells.set(F,P)}}}function Q(j,A,B,D,J,O,U=-1){let T=j.invCellSize,V=Math.floor(A*T),Z=Math.floor(D*T),E=Math.floor(B*T),L=Math.floor(J*T),_=++j._queryGen,$=j._aliveGen;for(let K=V;K<=Z;K++)for(let M=E;M<=L;M++){let F=j.cells.get(w(K,M));if(!F||F._gen!==$)continue;for(let N of F){if(N.entityId<=U||N._lastSeenGen===_)continue;N._lastSeenGen=_,O.push(N.entityId)}}}function z(j,A,B,D,J){let O=D*D,U=j.invCellSize,T=Math.floor((A-D)*U),V=Math.floor((A+D)*U),Z=Math.floor((B-D)*U),E=Math.floor((B+D)*U),L=++j._queryGen,_=j._aliveGen;for(let $=T;$<=V;$++)for(let K=Z;K<=E;K++){let M=j.cells.get(w($,K));if(!M||M._gen!==_)continue;for(let F of M){if(F._lastSeenGen===L)continue;F._lastSeenGen=L;let N=Math.max(F.x-F.halfW,Math.min(A,F.x+F.halfW)),P=Math.max(F.y-F.halfH,Math.min(B,F.y+F.halfH)),q=A-N,G=B-P;if(q*q+G*G<=O)J.push(F.entityId)}}}function W(j,A){let B=j.entries[A];if(!B||B._aliveGen!==j._aliveGen)return;return B}function p(j){return{grid:j,queryRect(A,B,D,J){let O=[];return Q(j,A,B,D,J,O),O},queryRectInto(A,B,D,J,O,U){Q(j,A,B,D,J,O,U)},queryRadius(A,B,D){let J=[];return z(j,A,B,D,J),J},queryRadiusInto(A,B,D,J){z(j,A,B,D,J)},getEntry(A){return W(j,A)}}}function b(j){let{cellSize:A=64,systemGroup:B="spatialIndex",priority:D=2000,phases:J=["fixedUpdate","postUpdate"]}=j??{},O=R(A),U=p(O);return k("spatialIndex").withComponentTypes().withResourceTypes().withLabels().withGroups().install((T)=>{T.addResource("spatialIndex",U);for(let V of J){let Z=V==="fixedUpdate"?"localTransform":"worldTransform";T.addSystem(`spatial-index-rebuild-${V}`).setPriority(D).inPhase(V).inGroup(B).addQuery("transforms",{with:[Z]}).setProcess(({queries:E,ecs:L})=>{S(O);for(let _ of E.transforms){let $=_.components[Z],K=L.getComponent(_.id,"aabbCollider"),M=L.getComponent(_.id,"circleCollider");if(!K&&!M)continue;let{x:F,y:N}=$,P=0,q=0;if(K)F+=K.offsetX??0,N+=K.offsetY??0,P=K.width/2,q=K.height/2;if(M)F+=M.offsetX??0,N+=M.offsetY??0,P=Math.max(P,M.radius),q=Math.max(q,M.radius);H(O,_.id,F,N,P,q)}})}})}export{b as createSpatialIndexPlugin};
1
+ var X=((j)=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(j,{get:(B,D)=>(typeof require<"u"?require:B)[D]}):j)(function(j){if(typeof require<"u")return require.apply(this,arguments);throw Error('Dynamic require of "'+j+'" is not supported')});import{definePlugin as I}from"ecspresso";function G(j,B){return j*73856093^B*19349663}function H(j){return{cellSize:j,invCellSize:1/j,cells:new Map,entries:[],_aliveGen:0,_queryGen:0}}function W(j){j._aliveGen++}function z(j,B,D,K,O,V){let _=j._aliveGen,Z=j.entries[B],$;if(Z)Z.x=D,Z.y=K,Z.halfW=O,Z.halfH=V,Z._aliveGen=_,$=Z;else $={entityId:B,x:D,y:K,halfW:O,halfH:V,_lastSeenGen:0,_aliveGen:_},j.entries[B]=$;let F=j.invCellSize,L=Math.floor((D-O)*F),T=Math.floor((D+O)*F),U=Math.floor((K-V)*F),w=Math.floor((K+V)*F);for(let J=L;J<=T;J++)for(let N=U;N<=w;N++){let A=G(J,N),M=j.cells.get(A);if(M&&M._gen===_)M.push($);else if(M)M.length=0,M._gen=_,M.push($);else{let P=[$];P._gen=_,j.cells.set(A,P)}}}function q(j,B,D,K,O,V,_=-1){let Z=j.invCellSize,$=Math.floor(B*Z),F=Math.floor(K*Z),L=Math.floor(D*Z),T=Math.floor(O*Z),U=++j._queryGen,w=j._aliveGen;for(let J=$;J<=F;J++)for(let N=L;N<=T;N++){let A=j.cells.get(G(J,N));if(!A||A._gen!==w)continue;for(let M of A){if(M.entityId<=_||M._lastSeenGen===U)continue;M._lastSeenGen=U,V.push(M.entityId)}}}function S(j,B,D,K,O){let V=K*K,_=j.invCellSize,Z=Math.floor((B-K)*_),$=Math.floor((B+K)*_),F=Math.floor((D-K)*_),L=Math.floor((D+K)*_),T=++j._queryGen,U=j._aliveGen;for(let w=Z;w<=$;w++)for(let J=F;J<=L;J++){let N=j.cells.get(G(w,J));if(!N||N._gen!==U)continue;for(let A of N){if(A._lastSeenGen===T)continue;A._lastSeenGen=T;let M=Math.max(A.x-A.halfW,Math.min(B,A.x+A.halfW)),P=Math.max(A.y-A.halfH,Math.min(D,A.y+A.halfH)),E=B-M,Q=D-P;if(E*E+Q*Q<=V)O.push(A.entityId)}}}function R(j,B){let D=j.entries[B];if(!D||D._aliveGen!==j._aliveGen)return;return D}function v(j){return{grid:j,queryRect(B,D,K,O){let V=[];return q(j,B,D,K,O,V),V},queryRectInto(B,D,K,O,V,_){q(j,B,D,K,O,V,_)},queryRadius(B,D,K){let O=[];return S(j,B,D,K,O),O},queryRadiusInto(B,D,K,O){S(j,B,D,K,O)},getEntry(B){return R(j,B)}}}function h(j){let{cellSize:B=64,systemGroup:D="spatialIndex",priority:K=2000,phases:O=["fixedUpdate","postUpdate"]}=j??{},V=H(B),_=v(V);return I("spatialIndex").withComponentTypes().withResourceTypes().withLabels().withGroups().install((Z)=>{Z.addResource("spatialIndex",_);let $=!1;for(let F of O){let L=F==="fixedUpdate"?"localTransform":"worldTransform",T=F==="postUpdate";Z.addSystem(`spatial-index-rebuild-${F}`).setPriority(K).inPhase(F).inGroup(D).addQuery("aabbOnly",{with:[L,"aabbCollider"],without:["circleCollider"]}).addQuery("circleOnly",{with:[L,"circleCollider"],without:["aabbCollider"]}).addQuery("both",{with:[L,"aabbCollider","circleCollider"]}).setProcess(({queries:U,ecs:w})=>{if(T){let J=$;if($=!1,J&&!w.entityManager.hasHierarchy)return}W(V);for(let J of U.aabbOnly){let N=J.components[L],{aabbCollider:A}=J.components,M=N.x+(A.offsetX??0),P=N.y+(A.offsetY??0);z(V,J.id,M,P,A.width/2,A.height/2)}for(let J of U.circleOnly){let N=J.components[L],{circleCollider:A}=J.components,M=N.x+(A.offsetX??0),P=N.y+(A.offsetY??0);z(V,J.id,M,P,A.radius,A.radius)}for(let J of U.both){let N=J.components[L],{aabbCollider:A,circleCollider:M}=J.components,P=N.x+(A.offsetX??0)+(M.offsetX??0),E=N.y+(A.offsetY??0)+(M.offsetY??0),Q=Math.max(A.width/2,M.radius),k=Math.max(A.height/2,M.radius);z(V,J.id,P,E,Q,k)}if(!T)$=!0})}})}export{h as createSpatialIndexPlugin};
2
2
 
3
- //# debugId=9F6AD37F3DB1C43164756E2164756E21
3
+ //# debugId=31D57D221FCA9D0D64756E2164756E21
4
4
  //# sourceMappingURL=spatial-index.js.map
@@ -2,10 +2,10 @@
2
2
  "version": 3,
3
3
  "sources": ["../src/plugins/spatial/spatial-index.ts", "../src/utils/spatial-hash.ts"],
4
4
  "sourcesContent": [
5
- "/**\n * Spatial Index Plugin for ECSpresso\n *\n * Provides a uniform-grid spatial hash for broadphase collision detection\n * and proximity queries. Replaces O(n²) brute-force with O(n·d) where\n * d = local density.\n *\n * Standalone usage: queryRect / queryRadius for proximity queries.\n * Automatic acceleration: collision and physics2D plugins detect the\n * spatialIndex resource at runtime and use it for broadphase when present.\n */\n\nimport { definePlugin } from 'ecspresso';\nimport type { SystemPhase } from 'ecspresso';\nimport type { TransformComponentTypes } from './transform';\nimport type { CollisionComponentTypes } from '../physics/collision';\nimport {\n\ttype SpatialEntry,\n\ttype SpatialHashGrid,\n\ttype SpatialIndex,\n\tcreateGrid,\n\tclearGrid,\n\tinsertEntity,\n\tgridQueryRect,\n\tgridQueryRadius,\n\tgetLiveEntry,\n} from '../../utils/spatial-hash';\n\n// ==================== Resource API ====================\n\nexport interface SpatialIndexResourceTypes {\n\tspatialIndex: SpatialIndex;\n}\n\nfunction createSpatialIndexResource(grid: SpatialHashGrid): SpatialIndex {\n\treturn {\n\t\tgrid,\n\t\tqueryRect(minX: number, minY: number, maxX: number, maxY: number): number[] {\n\t\t\tconst out: number[] = [];\n\t\t\tgridQueryRect(grid, minX, minY, maxX, maxY, out);\n\t\t\treturn out;\n\t\t},\n\t\tqueryRectInto(minX: number, minY: number, maxX: number, maxY: number, result: number[], minId?: number): void {\n\t\t\tgridQueryRect(grid, minX, minY, maxX, maxY, result, minId);\n\t\t},\n\t\tqueryRadius(cx: number, cy: number, radius: number): number[] {\n\t\t\tconst out: number[] = [];\n\t\t\tgridQueryRadius(grid, cx, cy, radius, out);\n\t\t\treturn out;\n\t\t},\n\t\tqueryRadiusInto(cx: number, cy: number, radius: number, result: number[]): void {\n\t\t\tgridQueryRadius(grid, cx, cy, radius, result);\n\t\t},\n\t\tgetEntry(entityId: number): SpatialEntry | undefined {\n\t\t\treturn getLiveEntry(grid, entityId);\n\t\t},\n\t};\n}\n\n// ==================== Component Types ====================\n\ntype SpatialIndexComponentTypes =\n\tTransformComponentTypes & Pick<CollisionComponentTypes<string>, 'aabbCollider' | 'circleCollider'>;\n\n// ==================== Plugin Options ====================\n\nexport type SpatialIndexPhase = 'fixedUpdate' | 'postUpdate';\ntype SpatialIndexLabel = `spatial-index-rebuild-${SpatialIndexPhase}`;\n\nexport interface SpatialIndexPluginOptions<G extends string = 'spatialIndex'> {\n\t/** Cell size for the spatial hash grid (default: 64) */\n\tcellSize?: number;\n\t/** System group name (default: 'spatialIndex') */\n\tsystemGroup?: G;\n\t/** Priority for rebuild systems (default: 2000, before collision) */\n\tpriority?: number;\n\t/** Phases to register rebuild systems in (default: ['fixedUpdate', 'postUpdate']) */\n\tphases?: ReadonlyArray<SpatialIndexPhase>;\n}\n\n// ==================== Plugin Factory ====================\n\n/**\n * Create a spatial index plugin for ECSpresso.\n *\n * Provides a uniform-grid spatial hash that accelerates collision detection.\n * When installed alongside the collision or physics2D plugins, they\n * automatically use the spatial index for broadphase instead of O(n²)\n * brute-force.\n *\n * Also provides proximity query methods for game logic (e.g. \"find all\n * enemies within 200 units\").\n *\n * @example\n * ```typescript\n * const ecs = ECSpresso.create()\n * .withPlugin(createTransformPlugin())\n * .withPlugin(createCollisionPlugin({ layers }))\n * .withPlugin(createSpatialIndexPlugin({ cellSize: 128 }))\n * .build();\n *\n * // Proximity query in a system:\n * const si = ecs.getResource('spatialIndex');\n * const nearby = si.queryRadius(playerX, playerY, 200);\n * ```\n */\nexport function createSpatialIndexPlugin<G extends string = 'spatialIndex'>(\n\toptions?: SpatialIndexPluginOptions<G>,\n) {\n\tconst {\n\t\tcellSize = 64,\n\t\tsystemGroup = 'spatialIndex',\n\t\tpriority = 2000,\n\t\tphases = ['fixedUpdate', 'postUpdate'] as const,\n\t} = options ?? {};\n\n\tconst grid = createGrid(cellSize);\n\tconst resource = createSpatialIndexResource(grid);\n\n\treturn definePlugin('spatialIndex')\n\t\t.withComponentTypes<SpatialIndexComponentTypes>()\n\t\t.withResourceTypes<SpatialIndexResourceTypes>()\n\t\t.withLabels<SpatialIndexLabel>()\n\t\t.withGroups<G>()\n\t\t.install((world) => {\n\t\t\tworld.addResource('spatialIndex', resource);\n\n\t\t\t// Register a rebuild system for each requested phase\n\t\t\tfor (const phase of phases) {\n\t\t\t\tconst transformComponent = phase === 'fixedUpdate' ? 'localTransform' : 'worldTransform';\n\n\t\t\t\tworld\n\t\t\t\t\t.addSystem(`spatial-index-rebuild-${phase}`)\n\t\t\t\t\t.setPriority(priority)\n\t\t\t\t\t.inPhase(phase as SystemPhase)\n\t\t\t\t\t.inGroup(systemGroup)\n\t\t\t\t\t.addQuery('transforms', {\n\t\t\t\t\t\twith: [transformComponent],\n\t\t\t\t\t})\n\t\t\t\t\t.setProcess(({ queries, ecs }) => {\n\t\t\t\t\t\tclearGrid(grid);\n\n\t\t\t\t\t\tfor (const entity of queries.transforms) {\n\t\t\t\t\t\t\tconst transform = entity.components[transformComponent];\n\t\t\t\t\t\t\tconst aabb = ecs.getComponent(entity.id, 'aabbCollider');\n\t\t\t\t\t\t\tconst circle = ecs.getComponent(entity.id, 'circleCollider');\n\n\t\t\t\t\t\t\t// Only insert entities that have a collider\n\t\t\t\t\t\t\tif (!aabb && !circle) continue;\n\n\t\t\t\t\t\t\tlet x = transform.x;\n\t\t\t\t\t\t\tlet y = transform.y;\n\t\t\t\t\t\t\tlet halfW = 0;\n\t\t\t\t\t\t\tlet halfH = 0;\n\n\t\t\t\t\t\t\tif (aabb) {\n\t\t\t\t\t\t\t\tx += aabb.offsetX ?? 0;\n\t\t\t\t\t\t\t\ty += aabb.offsetY ?? 0;\n\t\t\t\t\t\t\t\thalfW = aabb.width / 2;\n\t\t\t\t\t\t\t\thalfH = aabb.height / 2;\n\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\tif (circle) {\n\t\t\t\t\t\t\t\tx += circle.offsetX ?? 0;\n\t\t\t\t\t\t\t\ty += circle.offsetY ?? 0;\n\t\t\t\t\t\t\t\t// Circle: use radius as half-extent in both dimensions\n\t\t\t\t\t\t\t\thalfW = Math.max(halfW, circle.radius);\n\t\t\t\t\t\t\t\thalfH = Math.max(halfH, circle.radius);\n\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\tinsertEntity(grid, entity.id, x, y, halfW, halfH);\n\t\t\t\t\t\t}\n\t\t\t\t\t});\n\t\t\t}\n\t\t});\n}\n",
5
+ "/**\n * Spatial Index Plugin for ECSpresso\n *\n * Provides a uniform-grid spatial hash for broadphase collision detection\n * and proximity queries. Replaces O(n²) brute-force with O(n·d) where\n * d = local density.\n *\n * Standalone usage: queryRect / queryRadius for proximity queries.\n * Automatic acceleration: collision and physics2D plugins detect the\n * spatialIndex resource at runtime and use it for broadphase when present.\n */\n\nimport { definePlugin } from 'ecspresso';\nimport type { SystemPhase } from 'ecspresso';\nimport type { TransformComponentTypes } from './transform';\nimport type { CollisionComponentTypes } from '../physics/collision';\nimport {\n\ttype SpatialEntry,\n\ttype SpatialHashGrid,\n\ttype SpatialIndex,\n\tcreateGrid,\n\tclearGrid,\n\tinsertEntity,\n\tgridQueryRect,\n\tgridQueryRadius,\n\tgetLiveEntry,\n} from '../../utils/spatial-hash';\n\n// ==================== Resource API ====================\n\nexport interface SpatialIndexResourceTypes {\n\tspatialIndex: SpatialIndex;\n}\n\nfunction createSpatialIndexResource(grid: SpatialHashGrid): SpatialIndex {\n\treturn {\n\t\tgrid,\n\t\tqueryRect(minX: number, minY: number, maxX: number, maxY: number): number[] {\n\t\t\tconst out: number[] = [];\n\t\t\tgridQueryRect(grid, minX, minY, maxX, maxY, out);\n\t\t\treturn out;\n\t\t},\n\t\tqueryRectInto(minX: number, minY: number, maxX: number, maxY: number, result: number[], minId?: number): void {\n\t\t\tgridQueryRect(grid, minX, minY, maxX, maxY, result, minId);\n\t\t},\n\t\tqueryRadius(cx: number, cy: number, radius: number): number[] {\n\t\t\tconst out: number[] = [];\n\t\t\tgridQueryRadius(grid, cx, cy, radius, out);\n\t\t\treturn out;\n\t\t},\n\t\tqueryRadiusInto(cx: number, cy: number, radius: number, result: number[]): void {\n\t\t\tgridQueryRadius(grid, cx, cy, radius, result);\n\t\t},\n\t\tgetEntry(entityId: number): SpatialEntry | undefined {\n\t\t\treturn getLiveEntry(grid, entityId);\n\t\t},\n\t};\n}\n\n// ==================== Component Types ====================\n\ntype SpatialIndexComponentTypes =\n\tTransformComponentTypes & Pick<CollisionComponentTypes<string>, 'aabbCollider' | 'circleCollider'>;\n\n// ==================== Plugin Options ====================\n\nexport type SpatialIndexPhase = 'fixedUpdate' | 'postUpdate';\ntype SpatialIndexLabel = `spatial-index-rebuild-${SpatialIndexPhase}`;\n\nexport interface SpatialIndexPluginOptions<G extends string = 'spatialIndex'> {\n\t/** Cell size for the spatial hash grid (default: 64) */\n\tcellSize?: number;\n\t/** System group name (default: 'spatialIndex') */\n\tsystemGroup?: G;\n\t/** Priority for rebuild systems (default: 2000, before collision) */\n\tpriority?: number;\n\t/**\n\t * Phases to register rebuild systems in (default: ['fixedUpdate', 'postUpdate']).\n\t *\n\t * When both phases are registered, the `postUpdate` rebuild is\n\t * automatically skipped on cycles where:\n\t * 1. The `fixedUpdate` rebuild already ran this cycle, AND\n\t * 2. No entity hierarchy exists.\n\t *\n\t * In flat-hierarchy scenes `transform-propagation` copies\n\t * `localTransform → worldTransform` unchanged, so the postUpdate\n\t * grid would duplicate the fixedUpdate one. The skip is automatic\n\t * and re-engages once any parent relationship is set, or whenever\n\t * `fixedUpdate` doesn't run that cycle (sub-fixed-DT frames).\n\t *\n\t * Edge case: if you write to `worldTransform` directly between\n\t * phases without using `transform-propagation` or entity hierarchy,\n\t * the auto-skip will leave your writes unindexed. Set\n\t * `phases: ['postUpdate']` explicitly to bypass the auto-skip.\n\t */\n\tphases?: ReadonlyArray<SpatialIndexPhase>;\n}\n\n// ==================== Plugin Factory ====================\n\n/**\n * Create a spatial index plugin for ECSpresso.\n *\n * Provides a uniform-grid spatial hash that accelerates collision detection.\n * When installed alongside the collision or physics2D plugins, they\n * automatically use the spatial index for broadphase instead of O(n²)\n * brute-force.\n *\n * Also provides proximity query methods for game logic (e.g. \"find all\n * enemies within 200 units\").\n *\n * @example\n * ```typescript\n * const ecs = ECSpresso.create()\n * .withPlugin(createTransformPlugin())\n * .withPlugin(createCollisionPlugin({ layers }))\n * .withPlugin(createSpatialIndexPlugin({ cellSize: 128 }))\n * .build();\n *\n * // Proximity query in a system:\n * const si = ecs.getResource('spatialIndex');\n * const nearby = si.queryRadius(playerX, playerY, 200);\n * ```\n */\nexport function createSpatialIndexPlugin<G extends string = 'spatialIndex'>(\n\toptions?: SpatialIndexPluginOptions<G>,\n) {\n\tconst {\n\t\tcellSize = 64,\n\t\tsystemGroup = 'spatialIndex',\n\t\tpriority = 2000,\n\t\tphases = ['fixedUpdate', 'postUpdate'] as const,\n\t} = options ?? {};\n\n\tconst grid = createGrid(cellSize);\n\tconst resource = createSpatialIndexResource(grid);\n\n\treturn definePlugin('spatialIndex')\n\t\t.withComponentTypes<SpatialIndexComponentTypes>()\n\t\t.withResourceTypes<SpatialIndexResourceTypes>()\n\t\t.withLabels<SpatialIndexLabel>()\n\t\t.withGroups<G>()\n\t\t.install((world) => {\n\t\t\tworld.addResource('spatialIndex', resource);\n\n\t\t\t// Flag flipped true by the fixedUpdate rebuild; checked + reset by the\n\t\t\t// postUpdate rebuild to detect whether fixedUpdate ran this cycle.\n\t\t\t// Lets postUpdate auto-skip when its grid would duplicate fixedUpdate's\n\t\t\t// in flat-hierarchy scenes — without breaking sub-fixed-DT frames\n\t\t\t// where only postUpdate runs.\n\t\t\tlet fixedRebuildRanThisCycle = false;\n\n\t\t\t// Register a rebuild system for each requested phase\n\t\t\tfor (const phase of phases) {\n\t\t\t\tconst transformComponent = phase === 'fixedUpdate' ? 'localTransform' : 'worldTransform';\n\t\t\t\tconst isPostUpdate = phase === 'postUpdate';\n\n\t\t\t\tworld\n\t\t\t\t\t.addSystem(`spatial-index-rebuild-${phase}`)\n\t\t\t\t\t.setPriority(priority)\n\t\t\t\t\t.inPhase(phase as SystemPhase)\n\t\t\t\t\t.inGroup(systemGroup)\n\t\t\t\t\t.addQuery('aabbOnly', {\n\t\t\t\t\t\twith: [transformComponent, 'aabbCollider'],\n\t\t\t\t\t\twithout: ['circleCollider'],\n\t\t\t\t\t})\n\t\t\t\t\t.addQuery('circleOnly', {\n\t\t\t\t\t\twith: [transformComponent, 'circleCollider'],\n\t\t\t\t\t\twithout: ['aabbCollider'],\n\t\t\t\t\t})\n\t\t\t\t\t.addQuery('both', {\n\t\t\t\t\t\twith: [transformComponent, 'aabbCollider', 'circleCollider'],\n\t\t\t\t\t})\n\t\t\t\t\t.setProcess(({ queries, ecs }) => {\n\t\t\t\t\t\tif (isPostUpdate) {\n\t\t\t\t\t\t\tconst ranFixed = fixedRebuildRanThisCycle;\n\t\t\t\t\t\t\tfixedRebuildRanThisCycle = false;\n\t\t\t\t\t\t\t// Skip only when fixedUpdate populated the grid this\n\t\t\t\t\t\t\t// cycle AND no hierarchy exists (so worldTransform\n\t\t\t\t\t\t\t// equals localTransform — identical grid data).\n\t\t\t\t\t\t\tif (ranFixed && !ecs.entityManager.hasHierarchy) return;\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tclearGrid(grid);\n\n\t\t\t\t\t\tfor (const entity of queries.aabbOnly) {\n\t\t\t\t\t\t\tconst transform = entity.components[transformComponent];\n\t\t\t\t\t\t\tconst { aabbCollider } = entity.components;\n\t\t\t\t\t\t\tconst x = transform.x + (aabbCollider.offsetX ?? 0);\n\t\t\t\t\t\t\tconst y = transform.y + (aabbCollider.offsetY ?? 0);\n\t\t\t\t\t\t\tinsertEntity(grid, entity.id, x, y, aabbCollider.width / 2, aabbCollider.height / 2);\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tfor (const entity of queries.circleOnly) {\n\t\t\t\t\t\t\tconst transform = entity.components[transformComponent];\n\t\t\t\t\t\t\tconst { circleCollider } = entity.components;\n\t\t\t\t\t\t\tconst x = transform.x + (circleCollider.offsetX ?? 0);\n\t\t\t\t\t\t\tconst y = transform.y + (circleCollider.offsetY ?? 0);\n\t\t\t\t\t\t\tinsertEntity(grid, entity.id, x, y, circleCollider.radius, circleCollider.radius);\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\t// Conservative broadphase: stack both offsets and take the\n\t\t\t\t\t\t// larger half-extent in each axis. Preserves the previous\n\t\t\t\t\t\t// \"both colliders\" behavior exactly.\n\t\t\t\t\t\tfor (const entity of queries.both) {\n\t\t\t\t\t\t\tconst transform = entity.components[transformComponent];\n\t\t\t\t\t\t\tconst { aabbCollider, circleCollider } = entity.components;\n\t\t\t\t\t\t\tconst x = transform.x + (aabbCollider.offsetX ?? 0) + (circleCollider.offsetX ?? 0);\n\t\t\t\t\t\t\tconst y = transform.y + (aabbCollider.offsetY ?? 0) + (circleCollider.offsetY ?? 0);\n\t\t\t\t\t\t\tconst halfW = Math.max(aabbCollider.width / 2, circleCollider.radius);\n\t\t\t\t\t\t\tconst halfH = Math.max(aabbCollider.height / 2, circleCollider.radius);\n\t\t\t\t\t\t\tinsertEntity(grid, entity.id, x, y, halfW, halfH);\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tif (!isPostUpdate) fixedRebuildRanThisCycle = true;\n\t\t\t\t\t});\n\t\t\t}\n\t\t});\n}\n",
6
6
  "/**\n * Spatial Hash Grid\n *\n * Uniform-grid spatial hash for broadphase collision detection and\n * proximity queries. Pure data structure, no ECS dependencies.\n */\n\n// ==================== Data Structure ====================\n\nexport interface SpatialEntry {\n\tentityId: number;\n\tx: number;\n\ty: number;\n\thalfW: number;\n\thalfH: number;\n\t/** Generation stamp used by query functions to dedup multi-cell hits without a Set. Internal. */\n\t_lastSeenGen: number;\n\t/** Rebuild generation when this entry was last inserted. Internal. */\n\t_aliveGen: number;\n}\n\n/**\n * A cell bucket — entries plus the alive-gen at which the bucket was last\n * filled. Buckets are reset lazily on the next insert in a new generation\n * (see `insertEntity`); queries skip buckets whose `_gen` is stale.\n *\n * Internal — exposed only through `SpatialHashGrid.cells`.\n */\ninterface CellBucket extends Array<SpatialEntry> {\n\t_gen: number;\n}\n\nexport interface SpatialHashGrid {\n\tcellSize: number;\n\tinvCellSize: number;\n\tcells: Map<number, CellBucket>;\n\t/**\n\t * Dense, indexed by entityId. Holes are `undefined`. Entries from previous\n\t * rebuilds remain in place for in-place reuse (zero allocation in steady\n\t * state); liveness is determined by `entry._aliveGen === grid._aliveGen`.\n\t * Internal — read live entries via `getEntry` / `liveEntryCount` helpers.\n\t *\n\t * High-water-mark grows with max entityId ever inserted; despawned ids\n\t * leave their slot occupied by a stale entry. Acceptable when the entity\n\t * manager recycles ids or peak count is bounded.\n\t */\n\tentries: (SpatialEntry | undefined)[];\n\t/** Monotonic counter bumped by each `clearGrid` call. Internal. */\n\t_aliveGen: number;\n\t/** Monotonic counter bumped on each query; entries record their last-seen gen for O(1) dedup. Internal. */\n\t_queryGen: number;\n}\n\n// ==================== Pure Functions ====================\n\n/**\n * Hash a cell coordinate pair to a single integer key.\n * Uses large-prime XOR to distribute values.\n */\nexport function hashCell(cx: number, cy: number): number {\n\t// Large primes for spatial hashing distribution\n\treturn (cx * 73856093) ^ (cy * 19349663);\n}\n\n/**\n * Create a new empty spatial hash grid.\n */\nexport function createGrid(cellSize: number): SpatialHashGrid {\n\treturn {\n\t\tcellSize,\n\t\tinvCellSize: 1 / cellSize,\n\t\tcells: new Map(),\n\t\tentries: [],\n\t\t_aliveGen: 0,\n\t\t_queryGen: 0,\n\t};\n}\n\n/**\n * Prepare the grid for a rebuild.\n *\n * O(1): bumps the alive-generation counter so entries inserted prior to this\n * call are implicitly stale. `getLiveEntry` / `liveEntryCount` filter\n * entries by the current gen; queries skip buckets whose own `_gen` lags\n * behind the alive gen; `insertEntity` resets a bucket's `length` lazily\n * the first time it is touched in a new generation.\n *\n * Existing `SpatialEntry` objects and `CellBucket` arrays remain in place\n * for reuse, so steady-state rebuilds allocate zero entries and zero\n * buckets, regardless of how many cells have ever been touched.\n */\nexport function clearGrid(grid: SpatialHashGrid): void {\n\tgrid._aliveGen++;\n}\n\n/**\n * Insert an entity into all overlapping cells of the grid.\n */\nexport function insertEntity(\n\tgrid: SpatialHashGrid,\n\tentityId: number,\n\tx: number,\n\ty: number,\n\thalfW: number,\n\thalfH: number,\n): void {\n\tconst gen = grid._aliveGen;\n\tconst existing = grid.entries[entityId];\n\tlet entry: SpatialEntry;\n\tif (existing) {\n\t\texisting.x = x;\n\t\texisting.y = y;\n\t\texisting.halfW = halfW;\n\t\texisting.halfH = halfH;\n\t\texisting._aliveGen = gen;\n\t\tentry = existing;\n\t} else {\n\t\tentry = { entityId, x, y, halfW, halfH, _lastSeenGen: 0, _aliveGen: gen };\n\t\tgrid.entries[entityId] = entry;\n\t}\n\n\tconst inv = grid.invCellSize;\n\tconst minCX = Math.floor((x - halfW) * inv);\n\tconst maxCX = Math.floor((x + halfW) * inv);\n\tconst minCY = Math.floor((y - halfH) * inv);\n\tconst maxCY = Math.floor((y + halfH) * inv);\n\n\tfor (let cx = minCX; cx <= maxCX; cx++) {\n\t\tfor (let cy = minCY; cy <= maxCY; cy++) {\n\t\t\tconst key = hashCell(cx, cy);\n\t\t\tconst bucket = grid.cells.get(key);\n\t\t\tif (bucket && bucket._gen === gen) {\n\t\t\t\t// Hot path: bucket already populated this generation.\n\t\t\t\tbucket.push(entry);\n\t\t\t} else if (bucket) {\n\t\t\t\t// First touch in this generation — drop stale entries from prior rebuilds.\n\t\t\t\tbucket.length = 0;\n\t\t\t\tbucket._gen = gen;\n\t\t\t\tbucket.push(entry);\n\t\t\t} else {\n\t\t\t\tconst fresh = [entry] as CellBucket;\n\t\t\t\tfresh._gen = gen;\n\t\t\t\tgrid.cells.set(key, fresh);\n\t\t\t}\n\t\t}\n\t}\n}\n\n/**\n * Collect entity IDs from all cells overlapping the given rectangle.\n *\n * Appends to `result` (caller clears/truncates first if reusing). Multi-cell\n * entries are deduplicated via a per-grid generation stamp on each\n * `SpatialEntry`.\n *\n * When `minId` is provided, only entries with `entityId > minId` are added —\n * used for symmetric broadphase pair generation.\n */\nexport function gridQueryRect(\n\tgrid: SpatialHashGrid,\n\tminX: number,\n\tminY: number,\n\tmaxX: number,\n\tmaxY: number,\n\tresult: number[],\n\tminId: number = -1,\n): void {\n\tconst inv = grid.invCellSize;\n\tconst minCX = Math.floor(minX * inv);\n\tconst maxCX = Math.floor(maxX * inv);\n\tconst minCY = Math.floor(minY * inv);\n\tconst maxCY = Math.floor(maxY * inv);\n\n\tconst gen = ++grid._queryGen;\n\tconst aliveGen = grid._aliveGen;\n\n\tfor (let cx = minCX; cx <= maxCX; cx++) {\n\t\tfor (let cy = minCY; cy <= maxCY; cy++) {\n\t\t\tconst bucket = grid.cells.get(hashCell(cx, cy));\n\t\t\tif (!bucket || bucket._gen !== aliveGen) continue;\n\t\t\tfor (const entry of bucket) {\n\t\t\t\tif (entry.entityId <= minId || entry._lastSeenGen === gen) continue;\n\t\t\t\tentry._lastSeenGen = gen;\n\t\t\t\tresult.push(entry.entityId);\n\t\t\t}\n\t\t}\n\t}\n}\n\n/**\n * Collect entity IDs within a circle. AABB-to-point distance filter against\n * the cells overlapping the circle's bounding rect. Appends to `result`.\n */\nexport function gridQueryRadius(\n\tgrid: SpatialHashGrid,\n\tcx: number,\n\tcy: number,\n\tradius: number,\n\tresult: number[],\n): void {\n\tconst rSq = radius * radius;\n\tconst inv = grid.invCellSize;\n\tconst minCX = Math.floor((cx - radius) * inv);\n\tconst maxCX = Math.floor((cx + radius) * inv);\n\tconst minCY = Math.floor((cy - radius) * inv);\n\tconst maxCY = Math.floor((cy + radius) * inv);\n\n\tconst gen = ++grid._queryGen;\n\tconst aliveGen = grid._aliveGen;\n\n\tfor (let icx = minCX; icx <= maxCX; icx++) {\n\t\tfor (let icy = minCY; icy <= maxCY; icy++) {\n\t\t\tconst bucket = grid.cells.get(hashCell(icx, icy));\n\t\t\tif (!bucket || bucket._gen !== aliveGen) continue;\n\t\t\tfor (const entry of bucket) {\n\t\t\t\tif (entry._lastSeenGen === gen) continue;\n\t\t\t\tentry._lastSeenGen = gen;\n\n\t\t\t\tconst closestX = Math.max(entry.x - entry.halfW, Math.min(cx, entry.x + entry.halfW));\n\t\t\t\tconst closestY = Math.max(entry.y - entry.halfH, Math.min(cy, entry.y + entry.halfH));\n\t\t\t\tconst dx = cx - closestX;\n\t\t\t\tconst dy = cy - closestY;\n\n\t\t\t\tif (dx * dx + dy * dy <= rSq) {\n\t\t\t\t\tresult.push(entry.entityId);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n}\n\n/**\n * Get the current-generation entry for an entityId, or `undefined` if the\n * entity isn't in the index for this rebuild. Stale entries from previous\n * rebuilds remain in `entries` for in-place reuse but are filtered here.\n */\nexport function getLiveEntry(grid: SpatialHashGrid, entityId: number): SpatialEntry | undefined {\n\tconst entry = grid.entries[entityId];\n\tif (!entry || entry._aliveGen !== grid._aliveGen) return undefined;\n\treturn entry;\n}\n\n/**\n * Count entries inserted in the current rebuild generation. Linear scan —\n * intended for tests and diagnostics, not hot paths.\n */\nexport function liveEntryCount(grid: SpatialHashGrid): number {\n\tconst gen = grid._aliveGen;\n\tlet n = 0;\n\tfor (const entry of grid.entries) {\n\t\tif (entry && entry._aliveGen === gen) n++;\n\t}\n\treturn n;\n}\n\n// ==================== Resource API ====================\n\n// TODO: Move SpatialIndex interface to src/plugins/spatial/spatial-index.ts.\n// It's a resource API concern, not a data structure concern. This file should\n// only contain the grid primitives (SpatialEntry, SpatialHashGrid, and the\n// pure functions that operate on them).\nexport interface SpatialIndex {\n\treadonly grid: SpatialHashGrid;\n\tqueryRect(minX: number, minY: number, maxX: number, maxY: number): number[];\n\tqueryRectInto(minX: number, minY: number, maxX: number, maxY: number, result: number[], minId?: number): void;\n\tqueryRadius(cx: number, cy: number, radius: number): number[];\n\tqueryRadiusInto(cx: number, cy: number, radius: number, result: number[]): void;\n\tgetEntry(entityId: number): SpatialEntry | undefined;\n}\n"
7
7
  ],
8
- "mappings": "2PAYA,uBAAS,kBC+CF,SAAS,CAAQ,CAAC,EAAY,EAAoB,CAExD,OAAQ,EAAK,SAAa,EAAK,SAMzB,SAAS,CAAU,CAAC,EAAmC,CAC7D,MAAO,CACN,WACA,YAAa,EAAI,EACjB,MAAO,IAAI,IACX,QAAS,CAAC,EACV,UAAW,EACX,UAAW,CACZ,EAgBM,SAAS,CAAS,CAAC,EAA6B,CACtD,EAAK,YAMC,SAAS,CAAY,CAC3B,EACA,EACA,EACA,EACA,EACA,EACO,CACP,IAAM,EAAM,EAAK,UACX,EAAW,EAAK,QAAQ,GAC1B,EACJ,GAAI,EACH,EAAS,EAAI,EACb,EAAS,EAAI,EACb,EAAS,MAAQ,EACjB,EAAS,MAAQ,EACjB,EAAS,UAAY,EACrB,EAAQ,EAER,OAAQ,CAAE,WAAU,IAAG,IAAG,QAAO,QAAO,aAAc,EAAG,UAAW,CAAI,EACxE,EAAK,QAAQ,GAAY,EAG1B,IAAM,EAAM,EAAK,YACX,EAAQ,KAAK,OAAO,EAAI,GAAS,CAAG,EACpC,EAAQ,KAAK,OAAO,EAAI,GAAS,CAAG,EACpC,EAAQ,KAAK,OAA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AAI,EAAU,GAAK,EAAa,SAAW,IAAM,EAAe,SAAW,GAC3E,EAAQ,KAAK,IAAI,EAAa,MAAQ,EAAG,EAAe,MAAM,EAC9D,EAAQ,KAAK,IAAI,EAAa,OAAS,EAAG,EAAe,MAAM,EACrE,EAAa,EAAM,EAAO,GAAI,EAAG,EAAG,EAAO,CAAK,EAGjD,GAAI,CAAC,EAAc,EAA2B,GAC9C,GAEH",
9
+ "debugId": "31D57D221FCA9D0D64756E2164756E21",
10
10
  "names": []
11
11
  }
@@ -49,7 +49,25 @@ export interface SpatialIndex3DPluginOptions<G extends string = 'spatialIndex3D'
49
49
  systemGroup?: G;
50
50
  /** Priority for rebuild systems (default: 2000, before collision) */
51
51
  priority?: number;
52
- /** Phases to register rebuild systems in (default: ['fixedUpdate', 'postUpdate']) */
52
+ /**
53
+ * Phases to register rebuild systems in (default: ['fixedUpdate', 'postUpdate']).
54
+ *
55
+ * When both phases are registered, the `postUpdate` rebuild is
56
+ * automatically skipped on cycles where:
57
+ * 1. The `fixedUpdate` rebuild already ran this cycle, AND
58
+ * 2. No entity hierarchy exists.
59
+ *
60
+ * In flat-hierarchy scenes `transform3d-propagation` copies
61
+ * `localTransform3D → worldTransform3D` unchanged, so the postUpdate
62
+ * grid would duplicate the fixedUpdate one. The skip is automatic
63
+ * and re-engages once any parent relationship is set, or whenever
64
+ * `fixedUpdate` doesn't run that cycle (sub-fixed-DT frames).
65
+ *
66
+ * Edge case: if you write to `worldTransform3D` directly between
67
+ * phases without using `transform3d-propagation` or entity hierarchy,
68
+ * the auto-skip will leave your writes unindexed. Set
69
+ * `phases: ['postUpdate']` explicitly to bypass the auto-skip.
70
+ */
53
71
  phases?: ReadonlyArray<SpatialIndex3DPhase>;
54
72
  }
55
73
  /**
@@ -1,4 +1,4 @@
1
- var z=((F)=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(F,{get:(K,J)=>(typeof require<"u"?require:K)[J]}):F)(function(F){if(typeof require<"u")return require.apply(this,arguments);throw Error('Dynamic require of "'+F+'" is not supported')});import{definePlugin as b}from"ecspresso";function H(F,K,J){return F*73856093^K*19349663^J*83492791}function C(F){return{cellSize:F,invCellSize:1/F,cells:new Map,entries:[],_aliveGen:0,_queryGen:0}}function D(F){F._aliveGen++}function k(F,K,J,U,M,O,L,V){let $=F._aliveGen,_=F.entries[K],A;if(_)_.x=J,_.y=U,_.z=M,_.halfW=O,_.halfH=L,_.halfD=V,_._aliveGen=$,A=_;else A={entityId:K,x:J,y:U,z:M,halfW:O,halfH:L,halfD:V,_lastSeenGen:0,_aliveGen:$},F.entries[K]=A;let P=F.invCellSize,B=Math.floor((J-O)*P),R=Math.floor((J+O)*P),j=Math.floor((U-L)*P),E=Math.floor((U+L)*P),q=Math.floor((M-V)*P),S=Math.floor((M+V)*P);for(let Q=B;Q<=R;Q++)for(let G=j;G<=E;G++)for(let N=q;N<=S;N++){let w=H(Q,G,N),T=F.cells.get(w);if(T&&T._gen===$)T.push(A);else if(T)T.length=0,T._gen=$,T.push(A);else{let W=[A];W._gen=$,F.cells.set(w,W)}}}function I(F,K,J,U,M,O,L,V,$=-1){let _=F.invCellSize,A=Math.floor(K*_),P=Math.floor(M*_),B=Math.floor(J*_),R=Math.floor(O*_),j=Math.floor(U*_),E=Math.floor(L*_),q=++F._queryGen,S=F._aliveGen;for(let Q=A;Q<=P;Q++)for(let G=B;G<=R;G++)for(let N=j;N<=E;N++){let w=F.cells.get(H(Q,G,N));if(!w||w._gen!==S)continue;for(let T of w){if(T.entityId<=$||T._lastSeenGen===q)continue;T._lastSeenGen=q,V.push(T.entityId)}}}function X(F,K,J,U,M,O){let L=M*M,V=F.invCellSize,$=Math.floor((K-M)*V),_=Math.floor((K+M)*V),A=Math.floor((J-M)*V),P=Math.floor((J+M)*V),B=Math.floor((U-M)*V),R=Math.floor((U+M)*V),j=++F._queryGen,E=F._aliveGen;for(let q=$;q<=_;q++)for(let S=A;S<=P;S++)for(let Q=B;Q<=R;Q++){let G=F.cells.get(H(q,S,Q));if(!G||G._gen!==E)continue;for(let N of G){if(N._lastSeenGen===j)continue;N._lastSeenGen=j;let w=Math.max(N.x-N.halfW,Math.min(K,N.x+N.halfW)),T=Math.max(N.y-N.halfH,Math.min(J,N.y+N.halfH)),W=Math.max(N.z-N.halfD,Math.min(U,N.z+N.halfD)),Y=K-w,Z=J-T,v=U-W;if(Y*Y+Z*Z+v*v<=L)O.push(N.entityId)}}}function p(F,K){let J=F.entries[K];if(!J||J._aliveGen!==F._aliveGen)return;return J}function f(F){return{grid:F,queryBox(K,J,U,M,O,L){let V=[];return I(F,K,J,U,M,O,L,V),V},queryBoxInto(K,J,U,M,O,L,V,$){I(F,K,J,U,M,O,L,V,$)},queryRadius(K,J,U,M){let O=[];return X(F,K,J,U,M,O),O},queryRadiusInto(K,J,U,M,O){X(F,K,J,U,M,O)},getEntry(K){return p(F,K)}}}function m(F){let{cellSize:K=64,systemGroup:J="spatialIndex3D",priority:U=2000,phases:M=["fixedUpdate","postUpdate"]}=F??{},O=C(K),L=f(O);return b("spatialIndex3D").withComponentTypes().withResourceTypes().withLabels().withGroups().install((V)=>{V.addResource("spatialIndex3D",L);for(let $ of M){let _=$==="fixedUpdate"?"localTransform3D":"worldTransform3D";V.addSystem(`spatial-index3D-rebuild-${$}`).setPriority(U).inPhase($).inGroup(J).addQuery("transforms",{with:[_]}).runWhenEmpty().setProcess(({queries:A,ecs:P})=>{D(O);for(let B of A.transforms){let R=B.components[_],j=P.getComponent(B.id,"aabb3DCollider"),E=j?void 0:P.getComponent(B.id,"sphereCollider");if(j){k(O,B.id,R.x+(j.offsetX??0),R.y+(j.offsetY??0),R.z+(j.offsetZ??0),j.width/2,j.height/2,j.depth/2);continue}if(E){let q=E.radius;k(O,B.id,R.x+(E.offsetX??0),R.y+(E.offsetY??0),R.z+(E.offsetZ??0),q,q,q)}}})}})}export{m as createSpatialIndex3DPlugin};
1
+ var f=((J)=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(J,{get:(N,K)=>(typeof require<"u"?require:N)[K]}):J)(function(J){if(typeof require<"u")return require.apply(this,arguments);throw Error('Dynamic require of "'+J+'" is not supported')});import{definePlugin as z}from"ecspresso";function R(J,N,K){return J*73856093^N*19349663^K*83492791}function b(J){return{cellSize:J,invCellSize:1/J,cells:new Map,entries:[],_aliveGen:0,_queryGen:0}}function p(J){J._aliveGen++}function k(J,N,K,j,O,$,U,A){let M=J._aliveGen,V=J.entries[N],B;if(V)V.x=K,V.y=j,V.z=O,V.halfW=$,V.halfH=U,V.halfD=A,V._aliveGen=M,B=V;else B={entityId:N,x:K,y:j,z:O,halfW:$,halfH:U,halfD:A,_lastSeenGen:0,_aliveGen:M},J.entries[N]=B;let P=J.invCellSize,q=Math.floor((K-$)*P),W=Math.floor((K+$)*P),L=Math.floor((j-U)*P),Q=Math.floor((j+U)*P),F=Math.floor((O-A)*P),T=Math.floor((O+A)*P);for(let G=q;G<=W;G++)for(let w=L;w<=Q;w++)for(let _=F;_<=T;_++){let S=R(G,w,_),E=J.cells.get(S);if(E&&E._gen===M)E.push(B);else if(E)E.length=0,E._gen=M,E.push(B);else{let H=[B];H._gen=M,J.cells.set(S,H)}}}function I(J,N,K,j,O,$,U,A,M=-1){let V=J.invCellSize,B=Math.floor(N*V),P=Math.floor(O*V),q=Math.floor(K*V),W=Math.floor($*V),L=Math.floor(j*V),Q=Math.floor(U*V),F=++J._queryGen,T=J._aliveGen;for(let G=B;G<=P;G++)for(let w=q;w<=W;w++)for(let _=L;_<=Q;_++){let S=J.cells.get(R(G,w,_));if(!S||S._gen!==T)continue;for(let E of S){if(E.entityId<=M||E._lastSeenGen===F)continue;E._lastSeenGen=F,A.push(E.entityId)}}}function X(J,N,K,j,O,$){let U=O*O,A=J.invCellSize,M=Math.floor((N-O)*A),V=Math.floor((N+O)*A),B=Math.floor((K-O)*A),P=Math.floor((K+O)*A),q=Math.floor((j-O)*A),W=Math.floor((j+O)*A),L=++J._queryGen,Q=J._aliveGen;for(let F=M;F<=V;F++)for(let T=B;T<=P;T++)for(let G=q;G<=W;G++){let w=J.cells.get(R(F,T,G));if(!w||w._gen!==Q)continue;for(let _ of w){if(_._lastSeenGen===L)continue;_._lastSeenGen=L;let S=Math.max(_.x-_.halfW,Math.min(N,_.x+_.halfW)),E=Math.max(_.y-_.halfH,Math.min(K,_.y+_.halfH)),H=Math.max(_.z-_.halfD,Math.min(j,_.z+_.halfD)),Y=N-S,Z=K-E,v=j-H;if(Y*Y+Z*Z+v*v<=U)$.push(_.entityId)}}}function D(J,N){let K=J.entries[N];if(!K||K._aliveGen!==J._aliveGen)return;return K}function C(J){return{grid:J,queryBox(N,K,j,O,$,U){let A=[];return I(J,N,K,j,O,$,U,A),A},queryBoxInto(N,K,j,O,$,U,A,M){I(J,N,K,j,O,$,U,A,M)},queryRadius(N,K,j,O){let $=[];return X(J,N,K,j,O,$),$},queryRadiusInto(N,K,j,O,$){X(J,N,K,j,O,$)},getEntry(N){return D(J,N)}}}function x(J){let{cellSize:N=64,systemGroup:K="spatialIndex3D",priority:j=2000,phases:O=["fixedUpdate","postUpdate"]}=J??{},$=b(N),U=C($);return z("spatialIndex3D").withComponentTypes().withResourceTypes().withLabels().withGroups().install((A)=>{A.addResource("spatialIndex3D",U);let M=!1;for(let V of O){let B=V==="fixedUpdate"?"localTransform3D":"worldTransform3D",P=V==="postUpdate";A.addSystem(`spatial-index3D-rebuild-${V}`).setPriority(j).inPhase(V).inGroup(K).addQuery("aabbWith",{with:[B,"aabb3DCollider"]}).addQuery("sphereOnly",{with:[B,"sphereCollider"],without:["aabb3DCollider"]}).runWhenEmpty().setProcess(({queries:q,ecs:W})=>{if(P){let L=M;if(M=!1,L&&!W.entityManager.hasHierarchy)return}p($);for(let L of q.aabbWith){let Q=L.components[B],{aabb3DCollider:F}=L.components;k($,L.id,Q.x+(F.offsetX??0),Q.y+(F.offsetY??0),Q.z+(F.offsetZ??0),F.width/2,F.height/2,F.depth/2)}for(let L of q.sphereOnly){let Q=L.components[B],{sphereCollider:F}=L.components,T=F.radius;k($,L.id,Q.x+(F.offsetX??0),Q.y+(F.offsetY??0),Q.z+(F.offsetZ??0),T,T,T)}if(!P)M=!0})}})}export{x as createSpatialIndex3DPlugin};
2
2
 
3
- //# debugId=FC8F8AAE90676C3164756E2164756E21
3
+ //# debugId=C91D70E729A54E7764756E2164756E21
4
4
  //# sourceMappingURL=spatial-index3D.js.map