UNPKG

ecspresso

Version:

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

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