7 lines
61 KiB
JavaScript
7 lines
61 KiB
JavaScript
|
|
/*!
|
||
|
|
* camera-controls
|
||
|
|
* https://github.com/yomotsu/camera-controls
|
||
|
|
* (c) 2017 @yomotsu
|
||
|
|
* Released under the MIT License.
|
||
|
|
*/
|
||
|
|
const MOUSE_BUTTON={LEFT:1,RIGHT:2,MIDDLE:4};const ACTION=Object.freeze({NONE:0,ROTATE:1,TRUCK:2,SCREEN_PAN:4,OFFSET:8,DOLLY:16,ZOOM:32,TOUCH_ROTATE:64,TOUCH_TRUCK:128,TOUCH_SCREEN_PAN:256,TOUCH_OFFSET:512,TOUCH_DOLLY:1024,TOUCH_ZOOM:2048,TOUCH_DOLLY_TRUCK:4096,TOUCH_DOLLY_SCREEN_PAN:8192,TOUCH_DOLLY_OFFSET:16384,TOUCH_DOLLY_ROTATE:32768,TOUCH_ZOOM_TRUCK:65536,TOUCH_ZOOM_OFFSET:131072,TOUCH_ZOOM_SCREEN_PAN:262144,TOUCH_ZOOM_ROTATE:524288});const DOLLY_DIRECTION={NONE:0,IN:1,OUT:-1};function isPerspectiveCamera(camera){return camera.isPerspectiveCamera}function isOrthographicCamera(camera){return camera.isOrthographicCamera}const PI_2=Math.PI*2;const PI_HALF=Math.PI/2;const EPSILON=1e-5;const DEG2RAD=Math.PI/180;function clamp(value,min,max){return Math.max(min,Math.min(max,value))}function approxZero(number,error=EPSILON){return Math.abs(number)<error}function approxEquals(a,b,error=EPSILON){return approxZero(a-b,error)}function roundToStep(value,step){return Math.round(value/step)*step}function infinityToMaxNumber(value){if(isFinite(value))return value;if(value<0)return-Number.MAX_VALUE;return Number.MAX_VALUE}function maxNumberToInfinity(value){if(Math.abs(value)<Number.MAX_VALUE)return value;return value*Infinity}function smoothDamp(current,target,currentVelocityRef,smoothTime,maxSpeed=Infinity,deltaTime){smoothTime=Math.max(1e-4,smoothTime);const omega=2/smoothTime;const x=omega*deltaTime;const exp=1/(1+x+.48*x*x+.235*x*x*x);let change=current-target;const originalTo=target;const maxChange=maxSpeed*smoothTime;change=clamp(change,-maxChange,maxChange);target=current-change;const temp=(currentVelocityRef.value+omega*change)*deltaTime;currentVelocityRef.value=(currentVelocityRef.value-omega*temp)*exp;let output=target+(change+temp)*exp;if(originalTo-current>0===output>originalTo){output=originalTo;currentVelocityRef.value=(output-originalTo)/deltaTime}return output}function smoothDampVec3(current,target,currentVelocityRef,smoothTime,maxSpeed=Infinity,deltaTime,out){smoothTime=Math.max(1e-4,smoothTime);const omega=2/smoothTime;const x=omega*deltaTime;const exp=1/(1+x+.48*x*x+.235*x*x*x);let targetX=target.x;let targetY=target.y;let targetZ=target.z;let changeX=current.x-targetX;let changeY=current.y-targetY;let changeZ=current.z-targetZ;const originalToX=targetX;const originalToY=targetY;const originalToZ=targetZ;const maxChange=maxSpeed*smoothTime;const maxChangeSq=maxChange*maxChange;const magnitudeSq=changeX*changeX+changeY*changeY+changeZ*changeZ;if(magnitudeSq>maxChangeSq){const magnitude=Math.sqrt(magnitudeSq);changeX=changeX/magnitude*maxChange;changeY=changeY/magnitude*maxChange;changeZ=changeZ/magnitude*maxChange}targetX=current.x-changeX;targetY=current.y-changeY;targetZ=current.z-changeZ;const tempX=(currentVelocityRef.x+omega*changeX)*deltaTime;const tempY=(currentVelocityRef.y+omega*changeY)*deltaTime;const tempZ=(currentVelocityRef.z+omega*changeZ)*deltaTime;currentVelocityRef.x=(currentVelocityRef.x-omega*tempX)*exp;currentVelocityRef.y=(currentVelocityRef.y-omega*tempY)*exp;currentVelocityRef.z=(currentVelocityRef.z-omega*tempZ)*exp;out.x=targetX+(changeX+tempX)*exp;out.y=targetY+(changeY+tempY)*exp;out.z=targetZ+(changeZ+tempZ)*exp;const origMinusCurrentX=originalToX-current.x;const origMinusCurrentY=originalToY-current.y;const origMinusCurrentZ=originalToZ-current.z;const outMinusOrigX=out.x-originalToX;const outMinusOrigY=out.y-originalToY;const outMinusOrigZ=out.z-originalToZ;if(origMinusCurrentX*outMinusOrigX+origMinusCurrentY*outMinusOrigY+origMinusCurrentZ*outMinusOrigZ>0){out.x=originalToX;out.y=originalToY;out.z=originalToZ;currentVelocityRef.x=(out.x-originalToX)/deltaTime;currentVelocityRef.y=(out.y-originalToY)/deltaTime;currentVelocityRef.z=(out.z-originalToZ)/deltaTime}return out}function extractClientCoordFromEvent(pointers,out){out.set(0,0);pointers.forEach((pointer=>{out.x+=pointer.clientX;out.y+=pointer.clientY}));out.x/=pointers.length;out.y/=pointers.length}function notSupportedInOrthographicCamera(camera,message){if(isOrthographicCamera(camera)){console.warn(`${message} is not suppor
|