@soonspacejs/plugin-first-person-controls 2.6.38 → 2.6.39

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.d.ts CHANGED
@@ -181,16 +181,16 @@ export default class FirstPersonControlsPlugin {
181
181
  constructor(ssp: SoonSpace, camera?: PerspectiveCamera | null);
182
182
  onKeyDown(event: KeyboardEvent): void;
183
183
  onKeyUp(event: KeyboardEvent): void;
184
- onMouseDown(): void;
185
- onMouseUp(): void;
186
- onMouseMove(event: MouseEvent): void;
187
184
  private _dblVector;
188
185
  private _dblDeltaVector;
189
186
  onDblClick(event: MouseEvent): void;
190
- protected _touch: Touch | null;
191
- onTouchStart(event: TouchEvent): void;
192
- onTouchEnd(event: TouchEvent): void;
193
- onTouchMove(event: TouchEvent): void;
187
+ private _pointerId;
188
+ onPointerDown(event: PointerEvent): void;
189
+ /**
190
+ * events handler for pointer movement
191
+ */
192
+ onPointerMove(event: PointerEvent): void;
193
+ onPointerUp(event: PointerEvent): void;
194
194
  clearClashCache(): void;
195
195
  lastDirection: Vector3;
196
196
  onClashCheck(origin: Vector3, direction: Vector3): boolean;
package/dist/index.esm.js CHANGED
@@ -1 +1 @@
1
- import{Vector3 as t,Vector2 as e,Sphere as s,Box3 as i,Raycaster as a,Matrix4 as n,Euler as o,Quaternion as h}from"three";import r from"soonspacejs";function c(t,e){const s=r.utils.getBoundingBox(e);return t.intersectsBox(s)}function l(t,e){t.disableAll();for(const s of e)t.enable(s)}let u;class m{get camera(){let t=this._camera;return t||(t=this._camera=this.viewport.cameraManager.createCamera("firstPersonCamera"),this.isExternalCamera=!1),t}set camera(t){this._camera=t,this.isExternalCamera=!0}get rotate(){return this._rotate&&(this.horizontalRotate||this.verticalRotate)}set rotate(t){this._rotate=t}constructor(a,n){this._camera=null,this.isExternalCamera=!1,this.enabled=!1,this.rotationAngle={min:-Math.PI/2,max:Math.PI/2},this.state={moveForward:!1,moveBackward:!1,moveLeft:!1,moveRight:!1,moveUp:!1,moveDown:!1,canJump:!1,canRotate:!1},this.moveSpeed=10,this.gravitySpeed=10,this.jumpOffset=0,this.clashCheckDistance=200,this.clashDistance=50,this.eyeHeight=160,this.kneeHeight=50,this.jumpHeight=110,this.enableClash=!0,this.enableGravity=!0,this.reverseRotate=!1,this._rotate=!0,this.rotateSpeed=1,this.horizontalRotate=!0,this.verticalRotate=!0,this.dblClickForward=!1,this.velocity=new t,this.movement=new e,this._dblVector=new t,this._dblDeltaVector=new t,this._touch=null,this.lastDirection=new t,this.searchRadiusFactor=3,this.checkedSphere=new s,this.gravitySearchFactor=3,this.gravityCheckedObjects=null,this.gravityCheckedBox=new i,this.gravityInterObject=null,this.kneeInterObject=null,this.eyeInterObject=null,this.ssp=a,this._camera=null!=n?n:null,this.viewport=a.viewport,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.onDblClick=this.onDblClick.bind(this),this.onTouchStart=this.onTouchStart.bind(this),this.onTouchMove=this.onTouchMove.bind(this),this.onTouchEnd=this.onTouchEnd.bind(this)}onKeyDown(t){if(!1!==this.enabled){switch(t.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&&!1===t.repeat&&(this.jumpOffset=this.jumpHeight),this.state.canJump=!1}(this.jumpOffset>0||Object.values(this.state).some((t=>t)))&&this.ssp.render()}}onKeyUp(t){if(!1!==this.enabled)switch(t.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(){this.state.canRotate=!0}onMouseUp(){this.state.canRotate=!1}onMouseMove(t){if(this.enabled&&this.state.canRotate){let e=t.movementY,s=t.movementX;this.reverseRotate&&(s=-s,e=-e),this.movement.x=s,this.movement.y=e,this.ssp.render()}}onDblClick(t){if(!0===this.dblClickForward){const[e]=this.ssp.viewport.getIntersects(t);if(e){const{point:t}=e;t.y+=this.eyeHeight,t.sub(this.camera.position),this._dblVector.copy(t),this._dblDeltaVector.set(0,0,0),this.ssp.render()}}}onTouchStart(t){this._touch=t.targetTouches[0],this._touch&&(this.state.canRotate=!0)}onTouchEnd(t){const e=this._touch;if(!e)return;if(t.targetTouches.length>0){if(!Array.from(t.targetTouches).find((t=>t.identifier===e.identifier)))return}this._touch=null,this.state.canRotate=!1}onTouchMove(t){const e=this._touch;if(!e)return;const s=Array.from(t.targetTouches).find((t=>t.identifier===e.identifier));if(!s)return;if(!this.enabled||!this.state.canRotate)return;let i=s.screenX-e.screenX,a=s.screenY-e.screenY;this.reverseRotate&&(i=-i,a=-a),this.movement.x=i,this.movement.y=a,this._touch=s,this.ssp.render()}clearClashCache(){this.kneeInterObject=null,this.eyeInterObject=null}onClashCheck(t,e){const s=e.length(),i=e.clone().divideScalar(s);i.distanceToSquared(this.lastDirection)>1e-9&&this.clearClashCache(),this.lastDirection=i;const{eyeHeight:n,kneeHeight:o,camera:h,clashCheckDistance:r,clashDistance:c,clashLayers:u}=this;let{kneeInterObject:m,eyeInterObject:d}=this;if(!m){const e=t.clone().setY(t.y-n+o),s=new a(e,i,0,r);s.firstHitOnly=!0,null!=u&&l(s.layers,u),s.camera=h;const c=this.getCheckedObjects(e),d=s.intersectObjects(c);this.kneeInterObject=m=d[0]}let p=-s;if(m&&(p+=m.distance,p<c))return!0;if(!d){const e=new a(t,i,0,r);e.firstHitOnly=!0,null!=u&&l(e.layers,u),e.camera=h;const s=this.getCheckedObjects(t),n=e.intersectObjects(s);this.eyeInterObject=d=n[0]}let v=-s;if(d){if(v+=d.distance,v<c)return!0;d.distance=v}return m&&(m.distance=p),!1}getCheckedObjects(t){const{clashDistance:e,clashCheckDistance:s,searchRadiusFactor:i,clashFilter:a}=this,n=s*i,o=this.checkedSphere,h=n+e;o.set(t,h);const r=null!=a?a:function(){return!0};return this.viewport.scener.intersectsList.getAll().filter((t=>!!r(t)&&c(o,t)))}getGravityCheckedObjects(e,s){const i=this.gravityCheckedBox;let a=this.gravityCheckedObjects;if(a){const t=e.clone();t.y=s,i.containsPoint(e)&&i.containsPoint(t)||(a=null)}if(!a){const{eyeHeight:s,clashCheckDistance:n,gravitySearchFactor:o,clashDistance:h,clashFilter:r}=this,l=n+h,u=new t(l,l,0),m=e.clone().add(u),d=e.clone().sub(u);d.y-=s*o,i.set(d,m);const p=this.viewport.scener.intersectsList.getAll(),v=null!=r?r:function(){return!0};a=p.filter((t=>!!v(t)&&c(i,t)))}return a}gravityClashCheck(e,s){var i;let o=this.gravityInterObject;if(o){const{object:s,face:a,instanceId:h}=o,r=null===(i=s.geometry)||void 0===i?void 0:i.getAttribute("position");if(r&&a){let i=o.facePoints;if(!i){let e=s.matrixWorld;if(null!=h){const t=new n;s.getMatrixAt(h,t),e=t.premultiply(e)}const{a:c,b:l,c:u}=a,m=new t(r.getX(c),r.getY(c),r.getZ(c));m.applyMatrix4(e),m.setY(0);const d=new t(r.getX(l),r.getY(l),r.getZ(l));d.applyMatrix4(e),d.setY(0);const p=new t(r.getX(u),r.getY(u),r.getZ(u));p.applyMatrix4(e),p.setY(0),o.facePoints=i=[m,d,p]}let c=i[2],l=null;const u=e.clone();u.y=0;i.every((t=>{const e=t.clone();e.sub(c);const s=u.clone();if(s.sub(c),e.cross(s),l){if(e.dot(l)<=0)return!1}return l=e,c=t,!0}))||(this.gravityInterObject=o=null)}}if(!o){const i=new a(e,new t(0,-1,0));i.firstHitOnly=!0;const n=this.clashLayers;null!=n&&l(i.layers,n),i.camera=this.camera;const h=this.getGravityCheckedObjects(e,s),r=i.intersectObjects(h);this.gravityInterObject=o=r[0]}return o}setOptions(e){const{position:s,rotation:i,moveSpeed:a,eyeHeight:n,kneeHeight:o,jumpHeight:h,enableClash:c,enableGravity:l,searchRadiusFactor:u,clashDistance:m,clashCheckDistance:d,gravitySpeed:p,gravitySearchFactor:v,reverseRotate:y,rotate:b,rotateSpeed:g,horizontalRotate:w,verticalRotate:f,dblClickForward:C,clashFilter:k,clashLayers:D}=e;if(n){const e=this.eyeHeight;this.eyeHeight=n;const s=null==e?n:n-e;this.camera.position.add(new t(0,s,0))}o&&(this.kneeHeight=o),h&&(this.jumpHeight=h),u&&(this.searchRadiusFactor=u),m&&(this.clashDistance=m),d&&(this.clashCheckDistance=d),p&&(this.gravitySpeed=p),v&&(this.gravitySearchFactor=v),null!=y&&(this.reverseRotate=y),null!=b&&(this.rotate=b),null!=w&&(this.horizontalRotate=w),null!=f&&(this.verticalRotate=f),r.utils.isBoolean(C)&&(this.dblClickForward=C),r.utils.isNumber(g)&&(this.rotateSpeed=g),null!=c&&(this.enableClash=c),null!=l&&(this.enableGravity=l),null!=k&&(this.clashFilter=k),null!=D&&(this.clashLayers=D),s&&(this.camera.position.set(s.x,s.y+this.eyeHeight,s.z),this.gravityInterObject=null,this.clearClashCache()),i&&this.camera.rotation.set(i.x,i.y,i.z),a&&(this.moveSpeed=a),this.ssp.signals.cameraChange.dispatch(this.camera.position.clone())}start(t){u=this.ssp.getCameraViewpoint(),this.viewport.controls.currentControls.enabled=!1,this.enabled=!0,this.isExternalCamera||this.viewport.cameraManager.setCurrentCamera(this.camera),this.setOptions(t),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.dblClick.add(this.onDblClick),this.ssp.signals.keyDown.add(this.onKeyDown),this.ssp.signals.keyUp.add(this.onKeyUp);const e=this.viewport.container;e.addEventListener("touchstart",this.onTouchStart,{passive:!0}),e.addEventListener("touchmove",this.onTouchMove,{passive:!0}),e.addEventListener("touchend",this.onTouchEnd)}stop(){u&&this.ssp.setCameraViewpoint(u),this.viewport.controls.currentControls.enabled=!0,this.enabled=!1,this.isExternalCamera||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.dblClick.remove(this.onDblClick),this.ssp.signals.keyDown.remove(this.onKeyDown),this.ssp.signals.keyUp.remove(this.onKeyUp);const t=this.viewport.container;t.removeEventListener("touchstart",this.onTouchStart),t.removeEventListener("touchmove",this.onTouchMove),t.removeEventListener("touchend",this.onTouchEnd),this.ssp.render()}update(){if(!this.enabled)return;const{delta:e}=this.viewport.state,s=new t,{state:i,camera:a,eyeHeight:n,kneeHeight:r}=this,c=20*this.moveSpeed,l=20*this.gravitySpeed;let u=!1;i.moveForward&&(s.z-=c),i.moveBackward&&(s.z+=c),i.moveLeft&&(s.x-=c),i.moveRight&&(s.x+=c),i.moveUp&&(s.y+=2*l),i.moveDown&&(s.y-=2*l),s.multiplyScalar(e);const m=new o(0,0,0,"YXZ");m.setFromQuaternion(a.quaternion);const{x:d,y:p}=this.movement,v=this.horizontalRotate&&0!==d,y=this.verticalRotate&&0!==p;if(this.rotate&&i.canRotate&&(v||y)){y&&(m.x-=.003*p*this.rotateSpeed),v&&(m.y-=.003*d*this.rotateSpeed);const{max:t,min:e}=this.rotationAngle;m.x=Math.min(t,Math.max(m.x,e)),a.quaternion.setFromEuler(m),u=!0}if(0!==m.y&&!s.equals(new t)){const e=new h;e.setFromAxisAngle(new t(0,1,0),m.y),s.applyQuaternion(e)}if(!this._dblVector.equals(new t)){const t=this._dblVector.clone().multiplyScalar(e);this._dblDeltaVector.add(t),this._dblDeltaVector.length()>this._dblVector.length()?(this._dblVector.set(0,0,0),this._dblDeltaVector.set(0,0,0)):s.add(t)}const b=a.position.clone();if(!s.equals(new t)&&(!this.enableClash||!this.onClashCheck(a.position,s))&&(b.add(s),this.gravityInterObject)){const t=this.gravityInterObject.point.clone().sub(b);t.y=0,t.length()>r&&(this.gravityInterObject=null)}let g=b.y;if(this.enableGravity){const t=g;this.jumpOffset>0&&(this.jumpOffset-=l*e,g+=2*l*e),g-=l*e,i.canJump=!1;const s=this.gravityClashCheck(b,g);if(s){const t=s.point.y+n;g<t&&(g=t,this.jumpOffset=0,i.canJump=!0)}t!==g&&this.clearClashCache()}b.y=g;const w=!a.position.equals(b);w&&a.position.copy(b),(w||u)&&this.ssp.signals.cameraChange.dispatch(this.camera.position.clone())}}export{m as default};
1
+ import{Vector3 as t,Vector2 as e,Sphere as s,Box3 as i,Raycaster as a,Matrix4 as n,Euler as o,Quaternion as r}from"three";import h from"soonspacejs";function c(t,e){const s=h.utils.getBoundingBox(e);return t.intersectsBox(s)}function l(t,e){t.disableAll();for(const s of e)t.enable(s)}let p;class m{get camera(){let t=this._camera;return t||(t=this._camera=this.viewport.cameraManager.createCamera("firstPersonCamera"),this.isExternalCamera=!1),t}set camera(t){this._camera=t,this.isExternalCamera=!0}get rotate(){return this._rotate&&(this.horizontalRotate||this.verticalRotate)}set rotate(t){this._rotate=t}constructor(a,n){this._camera=null,this.isExternalCamera=!1,this.enabled=!1,this.rotationAngle={min:-Math.PI/2,max:Math.PI/2},this.state={moveForward:!1,moveBackward:!1,moveLeft:!1,moveRight:!1,moveUp:!1,moveDown:!1,canJump:!1,canRotate:!1},this.moveSpeed=10,this.gravitySpeed=10,this.jumpOffset=0,this.clashCheckDistance=200,this.clashDistance=50,this.eyeHeight=160,this.kneeHeight=50,this.jumpHeight=110,this.enableClash=!0,this.enableGravity=!0,this.reverseRotate=!1,this._rotate=!0,this.rotateSpeed=1,this.horizontalRotate=!0,this.verticalRotate=!0,this.dblClickForward=!1,this.velocity=new t,this.movement=new e,this._dblVector=new t,this._dblDeltaVector=new t,this._pointerId=null,this.lastDirection=new t,this.searchRadiusFactor=3,this.checkedSphere=new s,this.gravitySearchFactor=3,this.gravityCheckedObjects=null,this.gravityCheckedBox=new i,this.gravityInterObject=null,this.kneeInterObject=null,this.eyeInterObject=null,this.ssp=a,this._camera=null!=n?n:null,this.viewport=a.viewport,this.onKeyDown=this.onKeyDown.bind(this),this.onKeyUp=this.onKeyUp.bind(this),this.onDblClick=this.onDblClick.bind(this),this.onPointerDown=this.onPointerDown.bind(this),this.onPointerMove=this.onPointerMove.bind(this),this.onPointerUp=this.onPointerUp.bind(this)}onKeyDown(t){if(!1!==this.enabled){switch(t.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&&!1===t.repeat&&(this.jumpOffset=this.jumpHeight),this.state.canJump=!1}(this.jumpOffset>0||Object.values(this.state).some((t=>t)))&&this.ssp.render()}}onKeyUp(t){if(!1!==this.enabled)switch(t.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}}onDblClick(t){if(!0===this.dblClickForward){const[e]=this.ssp.viewport.getIntersects(t);if(e){const{point:t}=e;t.y+=this.eyeHeight,t.sub(this.camera.position),this._dblVector.copy(t),this._dblDeltaVector.set(0,0,0),this.ssp.render()}}}onPointerDown(t){null===this._pointerId&&0===t.button&&(this._pointerId=t.pointerId,this.state.canRotate=!0)}onPointerMove(t){if(this.enabled&&this.state.canRotate){let e=t.movementY,s=t.movementX;this.reverseRotate&&(s=-s,e=-e),this.movement.x=s,this.movement.y=e,this.ssp.render()}}onPointerUp(t){null!==this._pointerId&&(this._pointerId=null,this.state.canRotate=!1)}clearClashCache(){this.kneeInterObject=null,this.eyeInterObject=null}onClashCheck(t,e){const s=e.length(),i=e.clone().divideScalar(s);i.distanceToSquared(this.lastDirection)>1e-9&&this.clearClashCache(),this.lastDirection=i;const{eyeHeight:n,kneeHeight:o,camera:r,clashCheckDistance:h,clashDistance:c,clashLayers:p}=this;let{kneeInterObject:m,eyeInterObject:d}=this;if(!m){const e=t.clone().setY(t.y-n+o),s=new a(e,i,0,h);s.firstHitOnly=!0,null!=p&&l(s.layers,p),s.camera=r;const c=this.getCheckedObjects(e),d=s.intersectObjects(c);this.kneeInterObject=m=d[0]}let v=-s;if(m&&(v+=m.distance,v<c))return!0;if(!d){const e=new a(t,i,0,h);e.firstHitOnly=!0,null!=p&&l(e.layers,p),e.camera=r;const s=this.getCheckedObjects(t),n=e.intersectObjects(s);this.eyeInterObject=d=n[0]}let y=-s;if(d){if(y+=d.distance,y<c)return!0;d.distance=y}return m&&(m.distance=v),!1}getCheckedObjects(t){const{clashDistance:e,clashCheckDistance:s,searchRadiusFactor:i,clashFilter:a}=this,n=s*i,o=this.checkedSphere,r=n+e;o.set(t,r);const h=null!=a?a:function(){return!0};return this.viewport.scener.intersectsList.getAll().filter((t=>!!h(t)&&c(o,t)))}getGravityCheckedObjects(e,s){const i=this.gravityCheckedBox;let a=this.gravityCheckedObjects;if(a){const t=e.clone();t.y=s,i.containsPoint(e)&&i.containsPoint(t)||(a=null)}if(!a){const{eyeHeight:s,clashCheckDistance:n,gravitySearchFactor:o,clashDistance:r,clashFilter:h}=this,l=n+r,p=new t(l,l,0),m=e.clone().add(p),d=e.clone().sub(p);d.y-=s*o,i.set(d,m);const v=this.viewport.scener.intersectsList.getAll(),y=null!=h?h:function(){return!0};a=v.filter((t=>!!y(t)&&c(i,t)))}return a}gravityClashCheck(e,s){var i;let o=this.gravityInterObject;if(o){const{object:s,face:a,instanceId:r}=o,h=null===(i=s.geometry)||void 0===i?void 0:i.getAttribute("position");if(h&&a){let i=o.facePoints;if(!i){let e=s.matrixWorld;if(null!=r){const t=new n;s.getMatrixAt(r,t),e=t.premultiply(e)}const{a:c,b:l,c:p}=a,m=new t(h.getX(c),h.getY(c),h.getZ(c));m.applyMatrix4(e),m.setY(0);const d=new t(h.getX(l),h.getY(l),h.getZ(l));d.applyMatrix4(e),d.setY(0);const v=new t(h.getX(p),h.getY(p),h.getZ(p));v.applyMatrix4(e),v.setY(0),o.facePoints=i=[m,d,v]}let c=i[2],l=null;const p=e.clone();p.y=0;i.every((t=>{const e=t.clone();e.sub(c);const s=p.clone();if(s.sub(c),e.cross(s),l){if(e.dot(l)<=0)return!1}return l=e,c=t,!0}))||(this.gravityInterObject=o=null)}}if(!o){const i=new a(e,new t(0,-1,0));i.firstHitOnly=!0;const n=this.clashLayers;null!=n&&l(i.layers,n),i.camera=this.camera;const r=this.getGravityCheckedObjects(e,s),h=i.intersectObjects(r);this.gravityInterObject=o=h[0]}return o}setOptions(e){const{position:s,rotation:i,moveSpeed:a,eyeHeight:n,kneeHeight:o,jumpHeight:r,enableClash:c,enableGravity:l,searchRadiusFactor:p,clashDistance:m,clashCheckDistance:d,gravitySpeed:v,gravitySearchFactor:y,reverseRotate:u,rotate:b,rotateSpeed:g,horizontalRotate:w,verticalRotate:C,dblClickForward:k,clashFilter:f,clashLayers:D}=e;if(n){const e=this.eyeHeight;this.eyeHeight=n;const s=null==e?n:n-e;this.camera.position.add(new t(0,s,0))}o&&(this.kneeHeight=o),r&&(this.jumpHeight=r),p&&(this.searchRadiusFactor=p),m&&(this.clashDistance=m),d&&(this.clashCheckDistance=d),v&&(this.gravitySpeed=v),y&&(this.gravitySearchFactor=y),null!=u&&(this.reverseRotate=u),null!=b&&(this.rotate=b),null!=w&&(this.horizontalRotate=w),null!=C&&(this.verticalRotate=C),h.utils.isBoolean(k)&&(this.dblClickForward=k),h.utils.isNumber(g)&&(this.rotateSpeed=g),null!=c&&(this.enableClash=c),null!=l&&(this.enableGravity=l),null!=f&&(this.clashFilter=f),null!=D&&(this.clashLayers=D),s&&(this.camera.position.set(s.x,s.y+this.eyeHeight,s.z),this.gravityInterObject=null,this.clearClashCache()),i&&this.camera.rotation.set(i.x,i.y,i.z),a&&(this.moveSpeed=a),this.ssp.signals.cameraChange.dispatch(this.camera.position.clone())}start(t){p=this.ssp.getCameraViewpoint(),this.viewport.controls.currentControls.enabled=!1,this.enabled=!0,this.isExternalCamera||this.viewport.cameraManager.setCurrentCamera(this.camera),this.setOptions(t),this.viewport.postRender.set("FirstPersonControls",this.update.bind(this)),this.ssp.signals.dblClick.add(this.onDblClick),this.ssp.signals.keyDown.add(this.onKeyDown),this.ssp.signals.keyUp.add(this.onKeyUp);const e=this.viewport.container;e.addEventListener("pointerdown",this.onPointerDown),e.addEventListener("pointermove",this.onPointerMove),e.addEventListener("pointerup",this.onPointerUp)}stop(){p&&this.ssp.setCameraViewpoint(p),this.viewport.controls.currentControls.enabled=!0,this.enabled=!1,this.isExternalCamera||this.viewport.cameraManager.setCurrentCamera(this.viewport.cameraManager.getMainCamera()),this.viewport.postRender.delete("FirstPersonControls"),this.ssp.signals.dblClick.remove(this.onDblClick),this.ssp.signals.keyDown.remove(this.onKeyDown),this.ssp.signals.keyUp.remove(this.onKeyUp);const t=this.viewport.container;t.removeEventListener("pointerdown",this.onPointerDown),t.removeEventListener("pointermove",this.onPointerMove),t.removeEventListener("pointerup",this.onPointerUp),this.ssp.render()}update(){if(!this.enabled)return;const{delta:e}=this.viewport.state,s=new t,{state:i,camera:a,eyeHeight:n,kneeHeight:h}=this,c=20*this.moveSpeed,l=20*this.gravitySpeed;let p=!1;i.moveForward&&(s.z-=c),i.moveBackward&&(s.z+=c),i.moveLeft&&(s.x-=c),i.moveRight&&(s.x+=c),i.moveUp&&(s.y+=2*l),i.moveDown&&(s.y-=2*l),s.multiplyScalar(e);const m=new o(0,0,0,"YXZ");m.setFromQuaternion(a.quaternion);const{x:d,y:v}=this.movement,y=this.horizontalRotate&&0!==d,u=this.verticalRotate&&0!==v;if(this.rotate&&i.canRotate&&(y||u)){u&&(m.x-=.003*v*this.rotateSpeed),y&&(m.y-=.003*d*this.rotateSpeed);const{max:t,min:e}=this.rotationAngle;m.x=Math.min(t,Math.max(m.x,e)),a.quaternion.setFromEuler(m),p=!0}if(0!==m.y&&!s.equals(new t)){const e=new r;e.setFromAxisAngle(new t(0,1,0),m.y),s.applyQuaternion(e)}if(!this._dblVector.equals(new t)){const t=this._dblVector.clone().multiplyScalar(e);this._dblDeltaVector.add(t),this._dblDeltaVector.length()>this._dblVector.length()?(this._dblVector.set(0,0,0),this._dblDeltaVector.set(0,0,0)):s.add(t)}const b=a.position.clone();if(!s.equals(new t)&&(!this.enableClash||!this.onClashCheck(a.position,s))&&(b.add(s),this.gravityInterObject)){const t=this.gravityInterObject.point.clone().sub(b);t.y=0,t.length()>h&&(this.gravityInterObject=null)}let g=b.y;if(this.enableGravity){const t=g;this.jumpOffset>0&&(this.jumpOffset-=l*e,g+=2*l*e),g-=l*e,i.canJump=!1;const s=this.gravityClashCheck(b,g);if(s){const t=s.point.y+n;g<t&&(g=t,this.jumpOffset=0,i.canJump=!0)}t!==g&&this.clearClashCache()}b.y=g;const w=!a.position.equals(b);w&&a.position.copy(b),(w||p)&&this.ssp.signals.cameraChange.dispatch(this.camera.position.clone())}}export{m 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.6.38",
4
+ "version": "2.6.39",
5
5
  "description": "FirstPersonControls plugin for SoonSpace.js",
6
6
  "main": "dist/index.esm.js",
7
7
  "module": "dist/index.esm.js",
@@ -13,8 +13,8 @@
13
13
  ],
14
14
  "author": "xuek",
15
15
  "license": "UNLICENSED",
16
- "gitHead": "94dd9eb1afbabdd3a46e71b66c8cb135286e4dc9",
16
+ "gitHead": "ec83ab42a5db34d708ebfbc88e0b9d6494e61bce",
17
17
  "peerDependencies": {
18
- "soonspacejs": "2.6.38"
18
+ "soonspacejs": "2.6.39"
19
19
  }
20
20
  }