@soonspacejs/plugin-first-person-controls 2.6.36 → 2.6.37

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
@@ -1,4 +1,5 @@
1
- import SoonSpace, { Position, Rotation, PerspectiveCamera, Vector3, Vector2, Intersection, Sphere, Object3D, Box3 } from 'soonspacejs';
1
+ import { PerspectiveCamera, Vector3, Vector2, Intersection, Sphere, Object3D, Box3 } from 'three';
2
+ import SoonSpace, { Position, Rotation } from 'soonspacejs';
2
3
  export type ClashCheckAxis = 'front' | 'back' | 'left' | 'right' | 'up' | 'down';
3
4
  /**
4
5
  * 碰撞对象过滤器
@@ -8,7 +9,7 @@ export type ClashCheckAxis = 'front' | 'back' | 'left' | 'right' | 'up' | 'down'
8
9
  * @param obj - Object3D 对象
9
10
  * @returns true:表示检测该对象的碰撞;false | null | undefined | void:表示不检测该对象的碰撞;
10
11
  */
11
- export type ClashFilter = (obj: Object3D) => (boolean | null | undefined | void);
12
+ export type ClashFilter = (obj: Object3D) => boolean | null | undefined | void;
12
13
  export interface StartOptions {
13
14
  position?: Position | null;
14
15
  rotation?: Rotation | null;
@@ -64,14 +65,22 @@ export interface StartOptions {
64
65
  */
65
66
  rotate?: boolean | null;
66
67
  /**
67
- /**
68
- * 是否开启水平方向的旋转
69
- */
68
+ * 旋转速度
69
+ */
70
+ rotateSpeed?: number;
71
+ /**
72
+ /**
73
+ * 是否开启水平方向的旋转
74
+ */
70
75
  horizontalRotate?: boolean | null;
71
76
  /**
72
77
  * 是否开启垂直方向旋转
73
78
  */
74
79
  verticalRotate?: boolean | null;
80
+ /**
81
+ * 开启双击前进
82
+ */
83
+ dblClickForward?: boolean;
75
84
  }
76
85
  export default class FirstPersonControlsPlugin {
77
86
  ssp: SoonSpace;
@@ -96,10 +105,10 @@ export default class FirstPersonControlsPlugin {
96
105
  moveBackward: boolean;
97
106
  moveLeft: boolean;
98
107
  moveRight: boolean;
99
- canJump: boolean;
100
- canRotate: boolean;
101
108
  moveUp: boolean;
102
109
  moveDown: boolean;
110
+ canJump: boolean;
111
+ canRotate: boolean;
103
112
  };
104
113
  /**
105
114
  * 移动速度
@@ -133,6 +142,10 @@ export default class FirstPersonControlsPlugin {
133
142
  */
134
143
  get rotate(): boolean;
135
144
  set rotate(value: boolean);
145
+ /**
146
+ * 旋转速度
147
+ */
148
+ rotateSpeed: number;
136
149
  /**
137
150
  * 是否开启水平方向的旋转
138
151
  */
@@ -141,21 +154,24 @@ export default class FirstPersonControlsPlugin {
141
154
  * 是否开启垂直方向旋转
142
155
  */
143
156
  verticalRotate: boolean;
157
+ /**
158
+ * 开启双击前进
159
+ */
160
+ dblClickForward: boolean;
144
161
  readonly velocity: Vector3;
145
- readonly vector: Vector3;
146
162
  readonly movement: Vector2;
147
163
  /**
148
- * 碰撞对象过滤器
149
- * @remarks
150
- * 会在遍历对象时逐个调用该方法
151
- *
152
- * @param obj - Object3D 对象
153
- * @returns true:表示检测该对象的碰撞;false | null | undefined | void:表示不检测该对象的碰撞;
154
- */
164
+ * 碰撞对象过滤器
165
+ * @remarks
166
+ * 会在遍历对象时逐个调用该方法
167
+ *
168
+ * @param obj - Object3D 对象
169
+ * @returns true:表示检测该对象的碰撞;false | null | undefined | void:表示不检测该对象的碰撞;
170
+ */
155
171
  clashFilter?: ClashFilter;
156
172
  /**
157
- * 碰撞检测的层级
158
- */
173
+ * 碰撞检测的层级
174
+ */
159
175
  clashLayers?: number[];
160
176
  /**
161
177
  *
@@ -165,9 +181,12 @@ export default class FirstPersonControlsPlugin {
165
181
  constructor(ssp: SoonSpace, camera?: PerspectiveCamera | null);
166
182
  onKeyDown(event: KeyboardEvent): void;
167
183
  onKeyUp(event: KeyboardEvent): void;
168
- onMouseDown(event: MouseEvent): void;
169
- onMouseUp(event: MouseEvent): void;
184
+ onMouseDown(): void;
185
+ onMouseUp(): void;
170
186
  onMouseMove(event: MouseEvent): void;
187
+ private _dblVector;
188
+ private _dblDeltaVector;
189
+ onDblClick(event: MouseEvent): void;
171
190
  protected _touch: Touch | null;
172
191
  onTouchStart(event: TouchEvent): void;
173
192
  onTouchEnd(event: TouchEvent): void;
@@ -181,10 +200,8 @@ export default class FirstPersonControlsPlugin {
181
200
  * 搜索半径是 clashCheckDistance 的多少倍
182
201
  */
183
202
  searchRadiusFactor: number;
184
- checkedObjects: Object3D[] | null;
185
203
  checkedSphere: Sphere;
186
- sceneObjectsChanged: () => void;
187
- getCheckedObjects(origin: Vector3): Object3D<import("soonspacejs").Event>[];
204
+ getCheckedObjects(origin: Vector3): Object3D<import("three").Event>[];
188
205
  /**
189
206
  * 重力搜索系数
190
207
  * @remarks
@@ -193,15 +210,13 @@ export default class FirstPersonControlsPlugin {
193
210
  gravitySearchFactor: number;
194
211
  gravityCheckedObjects: Object3D[] | null;
195
212
  gravityCheckedBox: Box3;
196
- getGravityCheckedObjects(origin: Vector3, dropY: number): Object3D<import("soonspacejs").Event>[];
197
- gravityClashCheck(origin: Vector3, dropY: number): Intersection<Object3D<import("soonspacejs").Event>>;
213
+ getGravityCheckedObjects(origin: Vector3, dropY: number): Object3D<import("three").Event>[];
214
+ gravityClashCheck(origin: Vector3, dropY: number): Intersection<Object3D<import("three").Event>>;
198
215
  setOptions(options: StartOptions): void;
199
216
  start(options: StartOptions): void;
200
217
  stop(): void;
201
- needsUpdate(): void;
202
218
  gravityInterObject?: Intersection | null;
203
219
  kneeInterObject?: Intersection | null;
204
220
  eyeInterObject?: Intersection | null;
205
- hasUpdated: boolean;
206
221
  update(): void;
207
222
  }
package/dist/index.esm.js CHANGED
@@ -1 +1 @@
1
- import t,{Raycaster as e,Vector3 as s}from"soonspacejs";function i(e,s){const i=t.utils.getBoundingBox(s);return e.intersectsBox(i)}function a(t,e){t.disableAll();for(const s of e)t.enable(s)}let n=performance.now();let o;class h{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(t,e){this._camera=null,this.isExternalCamera=!1,this.gravitySpeed=10,this.jumpOffset=0,this.clashCheckDistance=200,this.clashDistance=50,this.reverseRotate=!1,this._rotate=!0,this.horizontalRotate=!0,this.verticalRotate=!0,this._touch=null,this.searchRadiusFactor=3,this.checkedObjects=null,this.sceneObjectsChanged=()=>{this.checkedObjects=null},this.gravitySearchFactor=3,this.gravityCheckedObjects=null,this.gravityInterObject=null,this.kneeInterObject=null,this.eyeInterObject=null,this.hasUpdated=!1,this.ssp=t,this._camera=null!=e?e:null,this.viewport=t.viewport,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.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.onTouchStart=this.onTouchStart.bind(this),this.onTouchMove=this.onTouchMove.bind(this),this.onTouchEnd=this.onTouchEnd.bind(this);const s=this.ssp.THREE,{Vector3:i,Sphere:a,Vector2:n,Box3:o}=s;this.velocity=new i,this.vector=new i,this.movement=new n,this.lastDirection=new i,this.checkedSphere=new a,this.gravityCheckedBox=new o}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&&(this.jumpOffset+=3*this.jumpHeight),this.state.canJump=!1}(this.jumpOffset||Object.values(this.state).some((t=>t)))&&this.needsUpdate()}}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(t){this.state.canRotate=!0}onMouseUp(t){this.state.canRotate=!1}onMouseMove(t){if(this.enabled&&this.state.canRotate){let e=t.movementY,s=t.movementX;0===s&&(t.clientX<20?s=-10:t.clientX>this.viewport.interactiveContainer.clientWidth-20&&(s=10)),this.reverseRotate&&(s=-s,e=-e),this.movement.x=s,this.movement.y=e,this.needsUpdate()}}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;0===i&&(s.clientX<20?i=-10:s.clientX>this.viewport.interactiveContainer.clientWidth-20&&(i=10)),this.reverseRotate&&(i=-i,a=-a),this.movement.x=i,this.movement.y=a,this._touch=s,this.needsUpdate()}clearClashCache(){this.kneeInterObject=null,this.eyeInterObject=null}onClashCheck(t,s){const i=s.length(),n=s.clone().divideScalar(i);n.distanceToSquared(this.lastDirection)>1e-9&&this.clearClashCache(),this.lastDirection=n;const o=this.ssp.THREE,{eyeHeight:h,kneeHeight:r,camera:c,clashCheckDistance:l,clashDistance:u,clashLayers:m}=this;let{kneeInterObject:d,eyeInterObject:v}=this;if(!d){const s=t.clone().setY(t.y-h+r),i=new e(s,n,0,l);i.firstHitOnly=!0,null!=m&&a(i.layers,m),i.camera=c;const o=this.getCheckedObjects(s),u=i.intersectObjects(o);this.kneeInterObject=d=u[0]}let p=-i;if(d&&(p+=d.distance,p<u))return!0;if(!v){const e=new o.Raycaster(t,n,0,l);e.firstHitOnly=!0,null!=m&&a(e.layers,m),e.camera=c;const s=this.getCheckedObjects(t),i=e.intersectObjects(s);this.eyeInterObject=v=i[0]}let y=-i;if(v){if(y+=v.distance,y<u)return!0;v.distance=y}return d&&(d.distance=p),!1}getCheckedObjects(t){const{clashDistance:e,clashCheckDistance:s,searchRadiusFactor:a,clashFilter:n}=this,o=s*a,h=this.checkedSphere,r=o+e;h.set(t,r);const c=null!=n?n:function(){return!0};return this.viewport.scener.intersectsList.getAll().filter((t=>!!c(t)&&i(h,t)))}getGravityCheckedObjects(t,e){const a=this.gravityCheckedBox;let n=this.gravityCheckedObjects;if(n){const s=t.clone();s.y=e,a.containsPoint(t)&&a.containsPoint(s)||(n=null)}if(!n){const{eyeHeight:e,clashCheckDistance:o,gravitySearchFactor:h,clashDistance:r,clashFilter:c}=this,l=o+r,u=new s(l,l,0),m=t.clone().add(u),d=t.clone().sub(u);d.y-=e*h,a.set(d,m);const v=this.viewport.scener.intersectsList.getAll(),p=null!=c?c:function(){return!0};this.checkedObjects=n=v.filter((t=>!!p(t)&&i(a,t)))}return n}gravityClashCheck(t,i){var n;let o=this.gravityInterObject;if(o){const{Vector3:e,Matrix4:s}=this.ssp.THREE,{object:i,face:a,instanceId:h}=o,r=null===(n=i.geometry)||void 0===n?void 0:n.getAttribute("position");if(r&&a){let n=o.facePoints;if(!n){let t=i.matrixWorld;if(null!=h){const e=new s;i.getMatrixAt(h,e),t=e.premultiply(t)}const{a:c,b:l,c:u}=a,m=new e(r.getX(c),r.getY(c),r.getZ(c));m.applyMatrix4(t),m.setY(0);const d=new e(r.getX(l),r.getY(l),r.getZ(l));d.applyMatrix4(t),d.setY(0);const v=new e(r.getX(u),r.getY(u),r.getZ(u));v.applyMatrix4(t),v.setY(0),o.facePoints=n=[m,d,v]}let c=n[2],l=null;const u=t.clone();u.y=0;n.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 n=new e(t,new s(0,-1,0));n.firstHitOnly=!0;const h=this.clashLayers;null!=h&&a(n.layers,h),n.camera=this.camera;const r=this.getGravityCheckedObjects(t,i),c=n.intersectObjects(r);this.gravityInterObject=o=c[0]}return o}setOptions(t){const{position:e,rotation:i,moveSpeed:a,eyeHeight:n,kneeHeight:o,jumpHeight:h,enableClash:r,enableGravity:c,searchRadiusFactor:l,clashDistance:u,clashCheckDistance:m,gravitySpeed:d,gravitySearchFactor:v,reverseRotate:p,rotate:y,horizontalRotate:g,verticalRotate:b,clashFilter:w,clashLayers:f}=t;if(n){const t=this.eyeHeight;this.eyeHeight=n;const e=null==t?n:n-t;this.camera.position.add(new s(0,e,0))}o&&(this.kneeHeight=o),h&&(this.jumpHeight=h),l&&(this.searchRadiusFactor=l),u&&(this.clashDistance=u),m&&(this.clashCheckDistance=m),d&&(this.gravitySpeed=d),v&&(this.gravitySearchFactor=v),null!=p&&(this.reverseRotate=p),null!=y&&(this.rotate=y),null!=g&&(this.horizontalRotate=g),null!=b&&(this.verticalRotate=b),null!=r&&(this.enableClash=r),null!=c&&(this.enableGravity=c),null!=w&&(this.clashFilter=w),null!=f&&(this.clashLayers=f),e&&(this.camera.position.set(e.x,e.y+this.eyeHeight,e.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){o=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.keyDown.add(this.onKeyDown),this.ssp.signals.keyUp.add(this.onKeyUp);const e=this.ssp.viewport.container;e.addEventListener("touchstart",this.onTouchStart),e.addEventListener("touchmove",this.onTouchMove),e.addEventListener("touchend",this.onTouchEnd),this.ssp.signals.objectAdded.add(this.sceneObjectsChanged),this.ssp.signals.objectRemoved.add(this.sceneObjectsChanged)}stop(){o&&this.ssp.setCameraViewpoint(o),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.keyDown.remove(this.onKeyDown),this.ssp.signals.keyUp.remove(this.onKeyUp);const t=this.ssp.viewport.container;t.removeEventListener("touchstart",this.onTouchStart),t.removeEventListener("touchmove",this.onTouchMove),t.removeEventListener("touchend",this.onTouchEnd),this.ssp.signals.objectAdded.remove(this.sceneObjectsChanged),this.ssp.signals.objectRemoved.remove(this.sceneObjectsChanged),this.needsUpdate()}needsUpdate(){this.hasUpdated&&(this.hasUpdated=!1,n=performance.now(),this.ssp.signals.cameraChange.dispatch(this.camera.position.clone()))}update(){if(!this.enabled)return;const t=performance.now(),e=(t-n)/1e3;n=t;const s=this.ssp.THREE,{Vector3:i}=s,a=new s.Vector3,{state:o,camera:h,eyeHeight:r,kneeHeight:c}=this,l=20*this.moveSpeed,u=20*this.gravitySpeed,{position:m,quaternion:d}=h;let v=0,p=!1;o.moveForward&&(a.z-=l),o.moveBackward&&(a.z+=l),o.moveLeft&&(a.x-=l),o.moveRight&&(a.x+=l),o.moveUp&&(v+=l),o.moveDown&&(v-=l),a.multiplyScalar(e);const y=new s.Euler(0,0,0,"YXZ");y.setFromQuaternion(d);const{x:g,y:b}=this.movement,w=this.horizontalRotate&&0!==g,f=this.verticalRotate&&0!==b;if(this.rotate&&o.canRotate&&(w||f)){f&&(y.x-=.003*b),w&&(y.y-=.003*g);const{max:t,min:e}=this.rotationAngle;y.x=Math.min(t,Math.max(y.x,e)),h.quaternion.setFromEuler(y),p=!0}const C=this.jumpOffset+v*e;if(0!==y.y&&!a.equals(new i)){const t=new s.Quaternion;t.setFromAxisAngle(new i(0,1,0),y.y),a.applyQuaternion(t)}const k=m.clone();if((C||!a.equals(new i))&&(a.y+=C,!this.onClashCheck(m,a)&&(k.add(a),this.gravityInterObject))){const t=this.gravityInterObject.point.clone().sub(k);t.y=0,t.length()>c&&(this.gravityInterObject=null)}let j=k.y;if(this.enableGravity){const t=j;j-=u*e,o.canJump=!1;const s=this.gravityClashCheck(k,j);if(s){const t=s.point.y+r;j<t&&(j=t,o.canJump=!0)}t!==j&&this.clearClashCache()}k.y=j;const R=!m.equals(k);R&&h.position.copy(k),this.jumpOffset=0,(R||p)&&this.ssp.signals.cameraChange.dispatch(this.camera.position.clone()),this.hasUpdated=!0}}export{h 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 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};
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.36",
4
+ "version": "2.6.37",
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": "1534cbda6bf463ee55aaa9749504f4551c188cac",
16
+ "gitHead": "2ac01051fdd71a9831f104aacedf2325d8b94a57",
17
17
  "peerDependencies": {
18
- "soonspacejs": "2.6.36"
18
+ "soonspacejs": "2.6.37"
19
19
  }
20
20
  }