UNPKG

ecspresso

Version:

A minimal Entity-Component-System library for typescript and javascript.

5 lines (3 loc) 5.58 kB
var qq=((Q)=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(Q,{get:(j,R)=>(typeof require<"u"?require:j)[R]}):Q)(function(Q){if(typeof require<"u")return require.apply(this,arguments);throw Error('Dynamic require of "'+Q+'" is not supported')});import{definePlugin as e}from"ecspresso";var N={smoothing:5,offsetX:0,offsetY:0,offsetZ:0},H={traumaDecay:1,maxOffsetX:0.3,maxOffsetY:0.3,maxOffsetZ:0.3},E=Math.PI/2,L=0.001,K={x:0,y:0,z:0};function M(Q,j,R){return Math.max(j,Math.min(R,Q))}function a(Q){if(Q===!0)return{...H};return{traumaDecay:Q.traumaDecay??H.traumaDecay,maxOffsetX:Q.maxOffsetX??H.maxOffsetX,maxOffsetY:Q.maxOffsetY??H.maxOffsetY,maxOffsetZ:Q.maxOffsetZ??H.maxOffsetZ}}function f(Q,j,R,W){let z=Math.cos(j);W.x=R*z*Math.sin(Q),W.y=R*Math.sin(j),W.z=R*z*Math.cos(Q)}function Qq(Q){let{systemGroup:j="camera3d",phase:R="postUpdate",azimuth:W=0,elevation:z=0.5,distance:C=10,target:S,minDistance:k=1,maxDistance:x=100,minElevation:U=-E+L,maxElevation:u=E-L,orbitSensitivity:T=0.003,dollySensitivity:p=1.1,enableOrbit:D=!0,follow:G,shake:_,randomFn:w=Math.random}=Q??{},g=Q?.projection??"perspective",h=Q?.projection!=="orthographic"?Q?.fov??75:75,m=Q?.projection==="orthographic"?Q.zoom??1:1,y=_?a(_):H,l=y.traumaDecay,d=y.maxOffsetX,c=y.maxOffsetY,n=y.maxOffsetZ,i={targetX:S?.x??0,targetY:S?.y??0,targetZ:S?.z??0,azimuth:W,elevation:M(z,U,u),distance:M(C,k,x),followTarget:-1,followSmoothing:G?.smoothing??N.smoothing,followOffsetX:G?.offsetX??N.offsetX,followOffsetY:G?.offsetY??N.offsetY,followOffsetZ:G?.offsetZ??N.offsetZ,trauma:0,shakeOffsetX:0,shakeOffsetY:0,shakeOffsetZ:0},r={follow($,B){let I=typeof $==="number"?$:$.id;this.followTarget=I,this.followSmoothing=B?.smoothing??G?.smoothing??N.smoothing,this.followOffsetX=B?.offsetX??G?.offsetX??N.offsetX,this.followOffsetY=B?.offsetY??G?.offsetY??N.offsetY,this.followOffsetZ=B?.offsetZ??G?.offsetZ??N.offsetZ},unfollow(){this.followTarget=-1},setTarget($,B,I){this.targetX=$,this.targetY=B,this.targetZ=I},setOrbit($,B,I){this.azimuth=$,this.elevation=M(B,U,u),this.distance=M(I,k,x)},setDistance($){this.distance=M($,k,x)},addTrauma($){this.trauma=M(this.trauma+$,0,1)}};return e("camera3d").withResourceTypes().withLabels().withGroups().requires().install(($)=>{let B={active:!1,prevX:0,prevY:0,pendingDolly:0,el:null},q={...i,...r,...g==="orthographic"?{projection:"orthographic",zoom:m,setZoom(J){this.zoom=J}}:{projection:"perspective",fov:h,setFov(J){this.fov=J}}};$.addResource("camera3DState",q);function v(J){B.active=!0,B.prevX=J.clientX,B.prevY=J.clientY,B.el?.setPointerCapture(J.pointerId)}function P(J){if(!B.active)return;let Z=J.clientX-B.prevX,b=J.clientY-B.prevY;B.prevX=J.clientX,B.prevY=J.clientY,q.azimuth-=Z*T,q.elevation=M(q.elevation+b*T,U,u)}function F(J){B.active=!1,B.el?.releasePointerCapture(J.pointerId)}function O(J){J.preventDefault(),B.pendingDolly+=Math.sign(J.deltaY)}let V=null,X=null,Y=null;$.addSystem("camera3d-init").inGroup(j).setOnInitialize((J)=>{let Z=J.getResource("threeRenderer");if(V=J.getResource("camera"),V.isPerspectiveCamera)X=V;else if(V.isOrthographicCamera)Y=V;if(q.projection==="perspective"&&!X)throw Error("createCamera3DPlugin: configured as 'perspective' but the renderer's camera is not a PerspectiveCamera.");if(q.projection==="orthographic"&&!Y)throw Error("createCamera3DPlugin: configured as 'orthographic' but the renderer's camera is not an OrthographicCamera.");if(q.projection==="perspective"&&X)q.fov=X.fov;else if(q.projection==="orthographic"&&Y)q.zoom=Y.zoom;if(B.el=Z.domElement,D)B.el.addEventListener("pointerdown",v),B.el.addEventListener("pointermove",P),B.el.addEventListener("pointerup",F);B.el.addEventListener("wheel",O,{passive:!1}),f(q.azimuth,q.elevation,q.distance,K),V.position.set(q.targetX+K.x,q.targetY+K.y,q.targetZ+K.z),V.lookAt(q.targetX,q.targetY,q.targetZ)}).setOnDetach(()=>{if(!B.el)return;if(D)B.el.removeEventListener("pointerdown",v),B.el.removeEventListener("pointermove",P),B.el.removeEventListener("pointerup",F);B.el.removeEventListener("wheel",O),B.el=null,V=null,X=null,Y=null}),$.addSystem("camera3d-follow").setPriority(400).inPhase(R).inGroup(j).setProcess(({ecs:J,dt:Z})=>{if(q.followTarget<0)return;if(!J.getEntity(q.followTarget)){q.followTarget=-1;return}let b=J.getComponent(q.followTarget,"worldTransform3D");if(!b)return;let o=b.x+q.followOffsetX,s=b.y+q.followOffsetY,t=b.z+q.followOffsetZ,A=Math.min(1,q.followSmoothing*Z);q.targetX+=(o-q.targetX)*A,q.targetY+=(s-q.targetY)*A,q.targetZ+=(t-q.targetZ)*A}),$.addSystem("camera3d-shake").setPriority(390).inPhase(R).inGroup(j).setProcess(({dt:J})=>{if(q.trauma<=0){q.shakeOffsetX=0,q.shakeOffsetY=0,q.shakeOffsetZ=0;return}q.trauma=Math.max(0,q.trauma-l*J);let Z=q.trauma*q.trauma;q.shakeOffsetX=d*Z*(w()*2-1),q.shakeOffsetY=c*Z*(w()*2-1),q.shakeOffsetZ=n*Z*(w()*2-1)}),$.addSystem("camera3d-sync").setPriority(380).inPhase(R).inGroup(j).setProcess(()=>{if(!V)return;if(B.pendingDolly!==0)q.distance=M(q.distance*Math.pow(p,B.pendingDolly),k,x),B.pendingDolly=0;if(f(q.azimuth,q.elevation,q.distance,K),V.position.set(q.targetX+K.x+q.shakeOffsetX,q.targetY+K.y+q.shakeOffsetY,q.targetZ+K.z+q.shakeOffsetZ),V.lookAt(q.targetX+q.shakeOffsetX,q.targetY+q.shakeOffsetY,q.targetZ+q.shakeOffsetZ),q.projection==="perspective"&&X&&X.fov!==q.fov)X.fov=q.fov,X.updateProjectionMatrix();else if(q.projection==="orthographic"&&Y&&Y.zoom!==q.zoom)Y.zoom=q.zoom,Y.updateProjectionMatrix()})})}export{f as sphericalToCartesian,Qq as createCamera3DPlugin}; //# debugId=E7D2111F51674CD764756E2164756E21 //# sourceMappingURL=camera3D.js.map