@soonspacejs/plugin-first-person-controls 2.4.9 → 2.4.12

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.
package/dist/index.esm.js CHANGED
@@ -1 +1 @@
1
- function e(e,t){for(var s=0;s<t.length;s++){var i=t[s];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(e,i.key,i)}}var t,s=performance.now(),i=function(){function i(e){!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,i),this.ssp=e,this.viewport=e.viewport,this.camera=this.viewport.cameraManager.createCamera("firstPersonCamera"),this.enabled=!1,this.rotationAngle={min:0,max:Math.PI},this.state={moveForward:!1,moveBackward:!1,moveLeft:!1,moveRight:!1,moveUp:!1,moveDown:!1,canJump:!1,canRotate:!1},this.moveSpeed=1,this.eyeHeight=160,this.kneeHeight=50,this.jumpHeight=110,this.enableClash=!0,this.enableGravity=!0,this.onKeyDown=this.onKeyDown.bind(this),this.onKeyUp=this.onKeyUp.bind(this),this.onMouseDown=this.onMouseDown.bind(this),this.onMouseUp=this.onMouseUp.bind(this),this.onMouseMove=this.onMouseMove.bind(this),this.velocity=new this.ssp.THREE.Vector3,this.vector=new this.ssp.THREE.Vector3,this.movement=new this.ssp.THREE.Vector2}var a,o,h;return a=i,(o=[{key:"onKeyDown",value:function(e){if(!1!==this.enabled)switch(e.code){case"ArrowUp":case"KeyW":this.state.moveForward=!0;break;case"ArrowLeft":case"KeyA":this.state.moveLeft=!0;break;case"ArrowDown":case"KeyS":this.state.moveBackward=!0;break;case"ArrowRight":case"KeyD":this.state.moveRight=!0;break;case"KeyI":this.state.canRotate=!0,this.movement.x=0,this.movement.y=-10;break;case"KeyJ":this.state.canRotate=!0,this.movement.x=-10,this.movement.y=0;break;case"KeyK":this.state.canRotate=!0,this.movement.x=0,this.movement.y=10;break;case"KeyL":this.state.canRotate=!0,this.movement.x=10,this.movement.y=0;break;case"KeyQ":this.state.moveUp=!0;break;case"KeyE":this.state.moveDown=!0;break;case"Space":!0===this.state.canJump&&(this.velocity.y+=3*this.jumpHeight),this.state.canJump=!1}}},{key:"onKeyUp",value:function(e){if(!1!==this.enabled)switch(e.code){case"ArrowUp":case"KeyW":this.state.moveForward=!1;break;case"ArrowLeft":case"KeyA":this.state.moveLeft=!1;break;case"ArrowDown":case"KeyS":this.state.moveBackward=!1;break;case"ArrowRight":case"KeyD":this.state.moveRight=!1;break;case"KeyI":case"KeyJ":case"KeyK":case"KeyL":this.state.canRotate=!1;break;case"KeyQ":this.state.moveUp=!1;break;case"KeyE":this.state.moveDown=!1}}},{key:"onMouseDown",value:function(e){this.state.canRotate=!0}},{key:"onMouseUp",value:function(e){this.state.canRotate=!1}},{key:"onMouseMove",value:function(e){if(this.enabled&&this.state.canRotate){var t=e.movementY,s=e.movementX;0===s&&(e.clientX<20?s=-10:e.clientX>this.viewport.interactiveContainer.clientWidth-20&&(s=10)),this.movement.x=s,this.movement.y=t}}},{key:"onRotate",value:function(){var e=new this.ssp.THREE.Euler(0,0,0,"YXZ");e.setFromQuaternion(this.camera.quaternion),e.x-=.003*this.movement.y,e.y-=.003*this.movement.x,e.x=Math.max(Math.PI/2-this.rotationAngle.max,Math.min(Math.PI/2-this.rotationAngle.min,e.x)),this.camera.quaternion.setFromEuler(e)}},{key:"onMoveForward",value:function(e){this.vector.setFromMatrixColumn(this.camera.matrix,0),this.vector.crossVectors(this.camera.up,this.vector),this.camera.position.addScaledVector(this.vector,e)}},{key:"onMoveRight",value:function(e){this.vector.setFromMatrixColumn(this.camera.matrix,0),this.camera.position.addScaledVector(this.vector,e)}},{key:"onMoveUp",value:function(e){this.vector.setFromMatrixColumn(this.camera.matrix,0),this.camera.position.addScaledVector(new this.ssp.THREE.Vector3(0,1,0),e)}},{key:"onMoveDown",value:function(e){this.vector.setFromMatrixColumn(this.camera.matrix,0),this.camera.position.addScaledVector(this.vector,e)}},{key:"onClashCheck",value:function(e){var t=this.camera.getWorldDirection(new this.ssp.THREE.Vector3),s=new this.ssp.THREE.Vector3;"front"===e?s.copy(t.clone()):"back"===e?s.copy(t.clone().applyAxisAngle(new this.ssp.THREE.Vector3(0,1,0),Math.PI)):"left"===e?s.copy(t.clone().applyAxisAngle(new this.ssp.THREE.Vector3(0,1,0),Math.PI/2)):"right"===e?s.copy(t.clone().applyAxisAngle(new this.ssp.THREE.Vector3(0,1,0),-Math.PI/2)):"up"===e?s.copy(new this.ssp.THREE.Vector3(0,1,0)):"down"===e&&s.copy(new this.ssp.THREE.Vector3(0,-1,0));var i=this.camera.position.clone(),a=new this.ssp.THREE.Raycaster(i,s,0,this.eyeHeight/3);a.camera=this.camera;var o=a.intersectObjects(this.viewport.scener.intersectsList.meshOfModelList),h=i.clone().setY(i.y-this.eyeHeight+this.kneeHeight),n=new this.ssp.THREE.Raycaster(h,s,0,this.eyeHeight/3);n.camera=this.camera;var r=n.intersectObjects(this.viewport.scener.intersectsList.meshOfModelList);return 0!==o.length||0!==r.length}},{key:"start",value:function(e){var s=e.position,i=e.rotation,a=void 0===i?{x:0,y:0,z:0}:i,o=e.moveSpeed,h=e.eyeHeight,n=e.kneeHeight,r=e.jumpHeight,c=e.enableClash,l=e.enableGravity;t=this.ssp.getCameraViewpoint(),this.viewport.controls.enabled=!1,this.enabled=!0,h&&(this.eyeHeight=h),n&&(this.kneeHeight=n),r&&(this.jumpHeight=r),this.enableClash=null==c||c,this.enableGravity=null==l||l,this.viewport.cameraManager.setCurrentCamera(this.camera),this.camera.position.set(s.x,s.y+this.eyeHeight,s.z),this.camera.rotation.set(a.x,a.y,a.z),o&&(this.moveSpeed=o),this.ssp.signals.cameraChange.dispatch(this.camera.position.clone()),this.viewport.postRender.set("FirstPersonControls",this.update.bind(this)),this.ssp.signals.mouseDown.add(this.onMouseDown),this.ssp.signals.mouseUp.add(this.onMouseUp),this.ssp.signals.mouseMove.add(this.onMouseMove),this.ssp.signals.keyDown.add(this.onKeyDown),this.ssp.signals.keyUp.add(this.onKeyUp)}},{key:"stop",value:function(){t&&this.ssp.setCameraViewpoint(t),this.viewport.controls.enabled=!0,this.enabled=!1,this.viewport.cameraManager.setCurrentCamera(this.viewport.cameraManager.getMainCamera()),this.viewport.postRender.delete("FirstPersonControls"),this.ssp.signals.mouseDown.remove(this.onMouseDown),this.ssp.signals.mouseUp.remove(this.onMouseUp),this.ssp.signals.mouseMove.remove(this.onMouseMove),this.ssp.signals.keyDown.remove(this.onKeyDown),this.ssp.signals.keyUp.remove(this.onKeyUp),this.ssp.signals.cameraChange.dispatch(this.camera.position.clone())}},{key:"update",value:function(){var e=performance.now();if(!0===this.enabled){var t=new this.ssp.THREE.Raycaster(this.camera.position,new this.ssp.THREE.Vector3(0,-1,0));t.camera=this.camera;var i=t.intersectObjects(this.viewport.scener.intersectsList.getAll()),a=(e-s)/1e3;if(this.velocity.x=0,this.velocity.z=0,this.enableGravity?this.velocity.y-=1e3*a:this.velocity.y=0,this.state.moveForward&&(this.enableClash&&this.onClashCheck("front")?this.velocity.z=0:this.velocity.z-=400*a),this.state.moveBackward&&(this.enableClash&&this.onClashCheck("back")?this.velocity.z=0:this.velocity.z+=400*a),this.state.moveLeft&&(this.enableClash&&this.onClashCheck("left")?this.velocity.x=0:this.velocity.x+=400*a),this.state.moveRight&&(this.enableClash&&this.onClashCheck("right")?this.velocity.x=0:this.velocity.x-=400*a),this.state.moveUp&&(this.enableClash&&this.onClashCheck("up")?this.velocity.y=0:this.velocity.y+=400*a),this.state.moveDown&&(this.enableClash&&this.onClashCheck("down")?this.velocity.y=0:this.velocity.y-=400*a),this.onMoveRight(-this.velocity.x*a*10*this.moveSpeed),this.onMoveForward(-this.velocity.z*a*10*this.moveSpeed),this.enableGravity?this.camera.position.y+=this.velocity.y*a:this.onMoveUp(-this.velocity.y*a*10*this.moveSpeed),i.length>0){var o=i[0].point.y+this.eyeHeight;this.camera.position.y<o&&(this.camera.position.y=o,this.velocity.y=0,this.state.canJump=!0)}else this.enableGravity?(this.velocity.y-=100*a,this.camera.position.y+=this.velocity.y*a,this.state.canJump=!1):this.velocity.y=0;this.state.canRotate&&this.onRotate()}s=e,this.ssp.signals.cameraChange.dispatch(this.camera.position.clone())}}])&&e(a.prototype,o),h&&e(a,h),Object.defineProperty(a,"prototype",{writable:!1}),i}();export{i as default};
1
+ let e=performance.now();let t;class s{constructor(e){this.ssp=e,this.viewport=e.viewport,this.camera=this.viewport.cameraManager.createCamera("firstPersonCamera"),this.enabled=!1,this.rotationAngle={min:0,max:Math.PI},this.state={moveForward:!1,moveBackward:!1,moveLeft:!1,moveRight:!1,moveUp:!1,moveDown:!1,canJump:!1,canRotate:!1},this.moveSpeed=1,this.eyeHeight=160,this.kneeHeight=50,this.jumpHeight=110,this.enableClash=!0,this.enableGravity=!0,this.onKeyDown=this.onKeyDown.bind(this),this.onKeyUp=this.onKeyUp.bind(this),this.onMouseDown=this.onMouseDown.bind(this),this.onMouseUp=this.onMouseUp.bind(this),this.onMouseMove=this.onMouseMove.bind(this),this.velocity=new this.ssp.THREE.Vector3,this.vector=new this.ssp.THREE.Vector3,this.movement=new this.ssp.THREE.Vector2}onKeyDown(e){if(!1!==this.enabled)switch(e.code){case"ArrowUp":case"KeyW":this.state.moveForward=!0;break;case"ArrowLeft":case"KeyA":this.state.moveLeft=!0;break;case"ArrowDown":case"KeyS":this.state.moveBackward=!0;break;case"ArrowRight":case"KeyD":this.state.moveRight=!0;break;case"KeyI":this.state.canRotate=!0,this.movement.x=0,this.movement.y=-10;break;case"KeyJ":this.state.canRotate=!0,this.movement.x=-10,this.movement.y=0;break;case"KeyK":this.state.canRotate=!0,this.movement.x=0,this.movement.y=10;break;case"KeyL":this.state.canRotate=!0,this.movement.x=10,this.movement.y=0;break;case"KeyQ":this.state.moveUp=!0;break;case"KeyE":this.state.moveDown=!0;break;case"Space":!0===this.state.canJump&&(this.velocity.y+=3*this.jumpHeight),this.state.canJump=!1}}onKeyUp(e){if(!1!==this.enabled)switch(e.code){case"ArrowUp":case"KeyW":this.state.moveForward=!1;break;case"ArrowLeft":case"KeyA":this.state.moveLeft=!1;break;case"ArrowDown":case"KeyS":this.state.moveBackward=!1;break;case"ArrowRight":case"KeyD":this.state.moveRight=!1;break;case"KeyI":case"KeyJ":case"KeyK":case"KeyL":this.state.canRotate=!1;break;case"KeyQ":this.state.moveUp=!1;break;case"KeyE":this.state.moveDown=!1}}onMouseDown(e){this.state.canRotate=!0}onMouseUp(e){this.state.canRotate=!1}onMouseMove(e){if(this.enabled&&this.state.canRotate){const t=e.movementY;let s=e.movementX;0===s&&(e.clientX<20?s=-10:e.clientX>this.viewport.interactiveContainer.clientWidth-20&&(s=10)),this.movement.x=s,this.movement.y=t}}onRotate(){const e=new this.ssp.THREE.Euler(0,0,0,"YXZ");e.setFromQuaternion(this.camera.quaternion),e.x-=.003*this.movement.y,e.y-=.003*this.movement.x,e.x=Math.max(Math.PI/2-this.rotationAngle.max,Math.min(Math.PI/2-this.rotationAngle.min,e.x)),this.camera.quaternion.setFromEuler(e)}onMoveForward(e){this.vector.setFromMatrixColumn(this.camera.matrix,0),this.vector.crossVectors(this.camera.up,this.vector),this.camera.position.addScaledVector(this.vector,e)}onMoveRight(e){this.vector.setFromMatrixColumn(this.camera.matrix,0),this.camera.position.addScaledVector(this.vector,e)}onMoveUp(e){this.vector.setFromMatrixColumn(this.camera.matrix,0),this.camera.position.addScaledVector(new this.ssp.THREE.Vector3(0,1,0),e)}onMoveDown(e){this.vector.setFromMatrixColumn(this.camera.matrix,0),this.camera.position.addScaledVector(this.vector,e)}onClashCheck(e){const t=this.camera.getWorldDirection(new this.ssp.THREE.Vector3),s=new this.ssp.THREE.Vector3;"front"===e?s.copy(t.clone()):"back"===e?s.copy(t.clone().applyAxisAngle(new this.ssp.THREE.Vector3(0,1,0),Math.PI)):"left"===e?s.copy(t.clone().applyAxisAngle(new this.ssp.THREE.Vector3(0,1,0),Math.PI/2)):"right"===e?s.copy(t.clone().applyAxisAngle(new this.ssp.THREE.Vector3(0,1,0),-Math.PI/2)):"up"===e?s.copy(new this.ssp.THREE.Vector3(0,1,0)):"down"===e&&s.copy(new this.ssp.THREE.Vector3(0,-1,0));const i=this.camera.position.clone(),o=new this.ssp.THREE.Raycaster(i,s,0,this.eyeHeight/3);o.camera=this.camera;const a=o.intersectObjects(this.viewport.scener.intersectsList.meshOfModelList),h=i.clone().setY(i.y-this.eyeHeight+this.kneeHeight),n=new this.ssp.THREE.Raycaster(h,s,0,this.eyeHeight/3);n.camera=this.camera;const r=n.intersectObjects(this.viewport.scener.intersectsList.meshOfModelList);return 0!==a.length||0!==r.length}start(e){const{position:s,rotation:i={x:0,y:0,z:0},moveSpeed:o,eyeHeight:a,kneeHeight:h,jumpHeight:n,enableClash:r,enableGravity:c}=e;t=this.ssp.getCameraViewpoint(),this.viewport.controls.enabled=!1,this.enabled=!0,a&&(this.eyeHeight=a),h&&(this.kneeHeight=h),n&&(this.jumpHeight=n),this.enableClash=null==r||r,this.enableGravity=null==c||c,this.viewport.cameraManager.setCurrentCamera(this.camera),this.camera.position.set(s.x,s.y+this.eyeHeight,s.z),this.camera.rotation.set(i.x,i.y,i.z),o&&(this.moveSpeed=o),this.ssp.signals.cameraChange.dispatch(this.camera.position.clone()),this.viewport.postRender.set("FirstPersonControls",this.update.bind(this)),this.ssp.signals.mouseDown.add(this.onMouseDown),this.ssp.signals.mouseUp.add(this.onMouseUp),this.ssp.signals.mouseMove.add(this.onMouseMove),this.ssp.signals.keyDown.add(this.onKeyDown),this.ssp.signals.keyUp.add(this.onKeyUp)}stop(){t&&this.ssp.setCameraViewpoint(t),this.viewport.controls.enabled=!0,this.enabled=!1,this.viewport.cameraManager.setCurrentCamera(this.viewport.cameraManager.getMainCamera()),this.viewport.postRender.delete("FirstPersonControls"),this.ssp.signals.mouseDown.remove(this.onMouseDown),this.ssp.signals.mouseUp.remove(this.onMouseUp),this.ssp.signals.mouseMove.remove(this.onMouseMove),this.ssp.signals.keyDown.remove(this.onKeyDown),this.ssp.signals.keyUp.remove(this.onKeyUp),this.ssp.signals.cameraChange.dispatch(this.camera.position.clone())}update(){const t=performance.now();if(!0===this.enabled){const s=new this.ssp.THREE.Raycaster(this.camera.position,new this.ssp.THREE.Vector3(0,-1,0));s.camera=this.camera;const i=s.intersectObjects(this.viewport.scener.intersectsList.getAll()),o=(t-e)/1e3;if(this.velocity.x=0,this.velocity.z=0,this.enableGravity?this.velocity.y-=1e3*o:this.velocity.y=0,this.state.moveForward&&(this.enableClash&&this.onClashCheck("front")?this.velocity.z=0:this.velocity.z-=400*o),this.state.moveBackward&&(this.enableClash&&this.onClashCheck("back")?this.velocity.z=0:this.velocity.z+=400*o),this.state.moveLeft&&(this.enableClash&&this.onClashCheck("left")?this.velocity.x=0:this.velocity.x+=400*o),this.state.moveRight&&(this.enableClash&&this.onClashCheck("right")?this.velocity.x=0:this.velocity.x-=400*o),this.state.moveUp&&(this.enableClash&&this.onClashCheck("up")?this.velocity.y=0:this.velocity.y+=400*o),this.state.moveDown&&(this.enableClash&&this.onClashCheck("down")?this.velocity.y=0:this.velocity.y-=400*o),this.onMoveRight(-this.velocity.x*o*10*this.moveSpeed),this.onMoveForward(-this.velocity.z*o*10*this.moveSpeed),this.enableGravity?this.camera.position.y+=this.velocity.y*o:this.onMoveUp(-this.velocity.y*o*10*this.moveSpeed),i.length>0){const e=i[0].point.y+this.eyeHeight;this.camera.position.y<e&&(this.camera.position.y=e,this.velocity.y=0,this.state.canJump=!0)}else this.enableGravity?(this.velocity.y-=100*o,this.camera.position.y+=this.velocity.y*o,this.state.canJump=!1):this.velocity.y=0;this.state.canRotate&&this.onRotate()}e=t,this.ssp.signals.cameraChange.dispatch(this.camera.position.clone())}}export{s as default};
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@soonspacejs/plugin-first-person-controls",
3
3
  "pluginName": "FirstPersonControlsPlugin",
4
- "version": "2.4.9",
4
+ "version": "2.4.12",
5
5
  "description": "FirstPersonControls plugin for SoonSpace.js",
6
6
  "main": "dist/index.js",
7
7
  "module": "dist/index.esm.js",
@@ -13,5 +13,5 @@
13
13
  ],
14
14
  "author": "xuek",
15
15
  "license": "UNLICENSED",
16
- "gitHead": "bc38cb459b7cd6894e492ffea333465fe423cdc5"
16
+ "gitHead": "1c14eab2badba701ba58d6c8df1a6e1798ca4ec5"
17
17
  }