@soonspacejs/plugin-first-person-controls 2.13.9 → 2.13.11

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,214 +1,214 @@
1
- import { Vector3, Vector2, Intersection, Sphere, Object3D, Box3, Camera } from 'three';
2
- import SoonSpace, { Position, Rotation } from 'soonspacejs';
3
- export type ClashCheckAxis = 'front' | 'back' | 'left' | 'right' | 'up' | 'down';
4
- /**
5
- * 碰撞对象过滤器
6
- * @remarks
7
- * 会在遍历对象时逐个调用该方法
8
- *
9
- * @param obj - Object3D 对象
10
- * @returns true:表示检测该对象的碰撞;false | null | undefined | void:表示不检测该对象的碰撞;
11
- */
12
- export type ClashFilter = (obj: Object3D) => boolean | null | undefined | void;
13
- export interface StartOptions {
14
- position?: Position | null;
15
- rotation?: Rotation | null;
16
- moveSpeed?: number;
17
- eyeHeight?: number;
18
- kneeHeight?: number;
19
- jumpHeight?: number;
20
- enableClash?: boolean;
21
- enableGravity?: boolean;
22
- /**
23
- * 模型对象的搜索半径的系数
24
- * @remarks
25
- * 搜索半径是 clashCheckDistance 的多少倍
26
- */
27
- searchRadiusFactor?: number;
28
- /**
29
- * 碰撞距离
30
- */
31
- clashDistance?: number;
32
- /**
33
- * 碰撞检测距离
34
- */
35
- clashCheckDistance?: number;
36
- /**
37
- * 重力速度
38
- */
39
- gravitySpeed?: number;
40
- /**
41
- * 重力搜索系数
42
- * @remarks
43
- * 重力搜索系数 表示 向下搜索多少个 eyeHeight 的深度
44
- */
45
- gravitySearchFactor?: number;
46
- /**
47
- * 碰撞对象过滤器
48
- * @remarks
49
- * 会在遍历对象时逐个调用该方法
50
- *
51
- * @param obj - Object3D 对象
52
- * @returns true:表示检测该对象的碰撞;false | null | undefined | void:表示不检测该对象的碰撞;
53
- */
54
- clashFilter?: ClashFilter;
55
- /**
56
- * 碰撞检测的层级
57
- */
58
- clashLayers?: number[];
59
- /**
60
- * 反向旋转
61
- */
62
- reverseRotate?: boolean | null;
63
- /**
64
- * 是否开启旋转
65
- */
66
- rotate?: boolean | null;
67
- /**
68
- * 旋转速度
69
- */
70
- rotateSpeed?: number;
71
- /**
72
- /**
73
- * 是否开启水平方向的旋转
74
- */
75
- horizontalRotate?: boolean | null;
76
- /**
77
- * 是否开启垂直方向旋转
78
- */
79
- verticalRotate?: boolean | null;
80
- /**
81
- * 开启双击前进
82
- */
83
- dblClickForward?: boolean;
84
- }
85
- export default class FirstPersonControlsPlugin {
86
- ssp: SoonSpace;
87
- viewport: SoonSpace['viewport'];
88
- controls: SoonSpace['controls'];
89
- protected camera: Camera;
90
- protected container: HTMLElement;
91
- enabled: boolean;
92
- keyCodeMap: {
93
- moveForward: string[];
94
- moveBackward: string[];
95
- moveLeft: string[];
96
- moveRight: string[];
97
- moveUp: string[];
98
- moveDown: string[];
99
- rotateUp: string[];
100
- rotateDown: string[];
101
- rotateLeft: string[];
102
- rotateRight: string[];
103
- jump: string[];
104
- };
105
- private state;
106
- /**
107
- * 移动速度
108
- */
109
- moveSpeed: number;
110
- /**
111
- * 重力速度
112
- */
113
- gravitySpeed: number;
114
- jumpOffset: number;
115
- /**
116
- * 碰撞检测距离
117
- */
118
- clashCheckDistance: number;
119
- /**
120
- * 碰撞距离
121
- */
122
- clashDistance: number;
123
- eyeHeight: number;
124
- kneeHeight: number;
125
- jumpHeight: number;
126
- enableClash: boolean;
127
- enableGravity: boolean;
128
- /**
129
- * 反向旋转
130
- */
131
- reverseRotate: boolean;
132
- _rotate: boolean;
133
- /**
134
- * 是否能旋转
135
- */
136
- get rotate(): boolean;
137
- set rotate(value: boolean);
138
- /**
139
- * 旋转速度
140
- */
141
- rotateSpeed: number;
142
- /**
143
- * 是否开启水平方向的旋转
144
- */
145
- horizontalRotate: boolean;
146
- /**
147
- * 是否开启垂直方向旋转
148
- */
149
- verticalRotate: boolean;
150
- /**
151
- * 开启双击前进
152
- */
153
- dblClickForward: boolean;
154
- readonly movement: Vector2;
155
- /**
156
- * 碰撞对象过滤器
157
- * @remarks
158
- * 会在遍历对象时逐个调用该方法
159
- *
160
- * @param obj - Object3D 对象
161
- * @returns true:表示检测该对象的碰撞;false | null | undefined | void:表示不检测该对象的碰撞;
162
- */
163
- clashFilter?: ClashFilter;
164
- /**
165
- * 碰撞检测的层级
166
- */
167
- clashLayers?: number[];
168
- /**
169
- *
170
- * @param ssp
171
- * @param camera - 自定义的摄像机,默认会自动生成一个新的摄像机
172
- */
173
- constructor(ssp: SoonSpace);
174
- onKeyDown(event: KeyboardEvent): void;
175
- onKeyUp(event: KeyboardEvent): void;
176
- private _dblVector;
177
- private _dblDeltaVector;
178
- onDblClick(event: MouseEvent): void;
179
- private _pointerId;
180
- onPointerDown(event: PointerEvent): void;
181
- /**
182
- * events handler for pointer movement
183
- */
184
- onPointerMove(event: PointerEvent): void;
185
- onPointerUp(event: PointerEvent): void;
186
- clearClashCache(): void;
187
- lastDirection: Vector3;
188
- onClashCheck(origin: Vector3, direction: Vector3): boolean;
189
- /**
190
- * 模型对象的搜索半径的系数
191
- * @remarks
192
- * 搜索半径是 clashCheckDistance 的多少倍
193
- */
194
- searchRadiusFactor: number;
195
- checkedSphere: Sphere;
196
- getCheckedObjects(origin: Vector3): Object3D<import("three").Object3DEventMap>[];
197
- /**
198
- * 重力搜索系数
199
- * @remarks
200
- * 重力搜索系数 表示 向下搜索多少个 eyeHeight 的深度
201
- */
202
- gravitySearchFactor: number;
203
- gravityCheckedObjects: Object3D[] | null;
204
- gravityCheckedBox: Box3;
205
- getGravityCheckedObjects(origin: Vector3, dropY: number): Object3D<import("three").Object3DEventMap>[];
206
- gravityClashCheck(origin: Vector3, dropY: number): Intersection<Object3D<import("three").Object3DEventMap>>;
207
- setOptions(options: StartOptions): void;
208
- start(options: StartOptions): void;
209
- stop(): void;
210
- gravityInterObject?: Intersection | null;
211
- kneeInterObject?: Intersection | null;
212
- eyeInterObject?: Intersection | null;
213
- update(): void;
214
- }
1
+ import { Vector3, Vector2, Intersection, Sphere, Object3D, Box3, Camera } from 'three';
2
+ import SoonSpace, { Position, Rotation } from 'soonspacejs';
3
+ export type ClashCheckAxis = 'front' | 'back' | 'left' | 'right' | 'up' | 'down';
4
+ /**
5
+ * 碰撞对象过滤器
6
+ * @remarks
7
+ * 会在遍历对象时逐个调用该方法
8
+ *
9
+ * @param obj - Object3D 对象
10
+ * @returns true:表示检测该对象的碰撞;false | null | undefined | void:表示不检测该对象的碰撞;
11
+ */
12
+ export type ClashFilter = (obj: Object3D) => boolean | null | undefined | void;
13
+ export interface StartOptions {
14
+ position?: Position | null;
15
+ rotation?: Rotation | null;
16
+ moveSpeed?: number;
17
+ eyeHeight?: number;
18
+ kneeHeight?: number;
19
+ jumpHeight?: number;
20
+ enableClash?: boolean;
21
+ enableGravity?: boolean;
22
+ /**
23
+ * 模型对象的搜索半径的系数
24
+ * @remarks
25
+ * 搜索半径是 clashCheckDistance 的多少倍
26
+ */
27
+ searchRadiusFactor?: number;
28
+ /**
29
+ * 碰撞距离
30
+ */
31
+ clashDistance?: number;
32
+ /**
33
+ * 碰撞检测距离
34
+ */
35
+ clashCheckDistance?: number;
36
+ /**
37
+ * 重力速度
38
+ */
39
+ gravitySpeed?: number;
40
+ /**
41
+ * 重力搜索系数
42
+ * @remarks
43
+ * 重力搜索系数 表示 向下搜索多少个 eyeHeight 的深度
44
+ */
45
+ gravitySearchFactor?: number;
46
+ /**
47
+ * 碰撞对象过滤器
48
+ * @remarks
49
+ * 会在遍历对象时逐个调用该方法
50
+ *
51
+ * @param obj - Object3D 对象
52
+ * @returns true:表示检测该对象的碰撞;false | null | undefined | void:表示不检测该对象的碰撞;
53
+ */
54
+ clashFilter?: ClashFilter;
55
+ /**
56
+ * 碰撞检测的层级
57
+ */
58
+ clashLayers?: number[];
59
+ /**
60
+ * 反向旋转
61
+ */
62
+ reverseRotate?: boolean | null;
63
+ /**
64
+ * 是否开启旋转
65
+ */
66
+ rotate?: boolean | null;
67
+ /**
68
+ * 旋转速度
69
+ */
70
+ rotateSpeed?: number;
71
+ /**
72
+ /**
73
+ * 是否开启水平方向的旋转
74
+ */
75
+ horizontalRotate?: boolean | null;
76
+ /**
77
+ * 是否开启垂直方向旋转
78
+ */
79
+ verticalRotate?: boolean | null;
80
+ /**
81
+ * 开启双击前进
82
+ */
83
+ dblClickForward?: boolean;
84
+ }
85
+ export default class FirstPersonControlsPlugin {
86
+ ssp: SoonSpace;
87
+ viewport: SoonSpace['viewport'];
88
+ controls: SoonSpace['controls'];
89
+ protected camera: Camera;
90
+ protected container: HTMLElement;
91
+ enabled: boolean;
92
+ keyCodeMap: {
93
+ moveForward: string[];
94
+ moveBackward: string[];
95
+ moveLeft: string[];
96
+ moveRight: string[];
97
+ moveUp: string[];
98
+ moveDown: string[];
99
+ rotateUp: string[];
100
+ rotateDown: string[];
101
+ rotateLeft: string[];
102
+ rotateRight: string[];
103
+ jump: string[];
104
+ };
105
+ private state;
106
+ /**
107
+ * 移动速度
108
+ */
109
+ moveSpeed: number;
110
+ /**
111
+ * 重力速度
112
+ */
113
+ gravitySpeed: number;
114
+ jumpOffset: number;
115
+ /**
116
+ * 碰撞检测距离
117
+ */
118
+ clashCheckDistance: number;
119
+ /**
120
+ * 碰撞距离
121
+ */
122
+ clashDistance: number;
123
+ eyeHeight: number;
124
+ kneeHeight: number;
125
+ jumpHeight: number;
126
+ enableClash: boolean;
127
+ enableGravity: boolean;
128
+ /**
129
+ * 反向旋转
130
+ */
131
+ reverseRotate: boolean;
132
+ _rotate: boolean;
133
+ /**
134
+ * 是否能旋转
135
+ */
136
+ get rotate(): boolean;
137
+ set rotate(value: boolean);
138
+ /**
139
+ * 旋转速度
140
+ */
141
+ rotateSpeed: number;
142
+ /**
143
+ * 是否开启水平方向的旋转
144
+ */
145
+ horizontalRotate: boolean;
146
+ /**
147
+ * 是否开启垂直方向旋转
148
+ */
149
+ verticalRotate: boolean;
150
+ /**
151
+ * 开启双击前进
152
+ */
153
+ dblClickForward: boolean;
154
+ readonly movement: Vector2;
155
+ /**
156
+ * 碰撞对象过滤器
157
+ * @remarks
158
+ * 会在遍历对象时逐个调用该方法
159
+ *
160
+ * @param obj - Object3D 对象
161
+ * @returns true:表示检测该对象的碰撞;false | null | undefined | void:表示不检测该对象的碰撞;
162
+ */
163
+ clashFilter?: ClashFilter;
164
+ /**
165
+ * 碰撞检测的层级
166
+ */
167
+ clashLayers?: number[];
168
+ /**
169
+ *
170
+ * @param ssp
171
+ * @param camera - 自定义的摄像机,默认会自动生成一个新的摄像机
172
+ */
173
+ constructor(ssp: SoonSpace);
174
+ onKeyDown(event: KeyboardEvent): void;
175
+ onKeyUp(event: KeyboardEvent): void;
176
+ private _dblVector;
177
+ private _dblDeltaVector;
178
+ onDblClick(event: MouseEvent): void;
179
+ private _pointerId;
180
+ onPointerDown(event: PointerEvent): void;
181
+ /**
182
+ * events handler for pointer movement
183
+ */
184
+ onPointerMove(event: PointerEvent): void;
185
+ onPointerUp(event: PointerEvent): void;
186
+ clearClashCache(): void;
187
+ lastDirection: Vector3;
188
+ onClashCheck(origin: Vector3, direction: Vector3): boolean;
189
+ /**
190
+ * 模型对象的搜索半径的系数
191
+ * @remarks
192
+ * 搜索半径是 clashCheckDistance 的多少倍
193
+ */
194
+ searchRadiusFactor: number;
195
+ checkedSphere: Sphere;
196
+ getCheckedObjects(origin: Vector3): Object3D<import("three").Object3DEventMap>[];
197
+ /**
198
+ * 重力搜索系数
199
+ * @remarks
200
+ * 重力搜索系数 表示 向下搜索多少个 eyeHeight 的深度
201
+ */
202
+ gravitySearchFactor: number;
203
+ gravityCheckedObjects: Object3D[] | null;
204
+ gravityCheckedBox: Box3;
205
+ getGravityCheckedObjects(origin: Vector3, dropY: number): Object3D<import("three").Object3DEventMap>[];
206
+ gravityClashCheck(origin: Vector3, dropY: number): Intersection<Object3D<import("three").Object3DEventMap>>;
207
+ setOptions(options: StartOptions): void;
208
+ start(options: StartOptions): void;
209
+ stop(): void;
210
+ gravityInterObject?: Intersection | null;
211
+ kneeInterObject?: Intersection | null;
212
+ eyeInterObject?: Intersection | null;
213
+ update(): void;
214
+ }
package/dist/index.esm.js CHANGED
@@ -1 +1 @@
1
- import{Vector2 as t,Vector3 as e,Sphere as s,Box3 as i,Raycaster as o,Matrix4 as n,Euler as a,Quaternion as r}from"three";import h from"soonspacejs";const{utils:{isObjectVisible:c}}=h;function l(t,e){const s=h.utils.getBoundingBox(e);return t.intersectsBox(s)}function d(t,e){t.disableAll();for(const s of e)t.enable(s)}function p(t,e){return t.intersectObjects(e).filter((({object:t})=>c(t)))}const{EPSILON:v,approxEquals:m}=h.utils,{utils:y}=h;class u{get rotate(){return this._rotate&&(this.horizontalRotate||this.verticalRotate)}set rotate(t){this._rotate=t}constructor(o){this.enabled=!1,this.keyCodeMap={moveForward:["ArrowUp","KeyW"],moveBackward:["ArrowDown","KeyS"],moveLeft:["ArrowLeft","KeyA"],moveRight:["ArrowRight","KeyD"],moveUp:["KeyQ"],moveDown:["KeyE"],rotateUp:["KeyI"],rotateDown:["KeyK"],rotateLeft:["KeyJ"],rotateRight:["KeyL"],jump:["Space"]},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.movement=new t,this._dblVector=new e,this._dblDeltaVector=new e,this._pointerId=null,this.lastDirection=new e,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=o,this.viewport=o.viewport,this.controls=o.controls,this.camera=o.controls.camera,this.container=o.viewport.container,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){t:for(const[e,s]of Object.entries(this.keyCodeMap))if(s.includes(t.code))switch(e){case"moveForward":this.state.moveForward=!0;break t;case"moveBackward":this.state.moveBackward=!0;break t;case"moveLeft":this.state.moveLeft=!0;break t;case"moveRight":this.state.moveRight=!0;break t;case"moveUp":this.state.moveUp=!0;break t;case"moveDown":this.state.moveDown=!0;break t;case"rotateUp":this.state.canRotate=!0,this.movement.x=0,this.movement.y=-10;break t;case"rotateDown":this.state.canRotate=!0,this.movement.x=0,this.movement.y=10;break t;case"rotateLeft":this.state.canRotate=!0,this.movement.x=-10,this.movement.y=0;break t;case"rotateRight":this.state.canRotate=!0,this.movement.x=10,this.movement.y=0;break t;case"jump":!0===this.state.canJump&&!1===t.repeat&&(this.jumpOffset=this.jumpHeight),this.state.canJump=!1;break t}(this.jumpOffset>0||Object.values(this.state).some((t=>t)))&&this.ssp.render()}}onKeyUp(t){t:for(const[e,s]of Object.entries(this.keyCodeMap))if(s.includes(t.code))switch(e){case"moveForward":this.state.moveForward=!1;break t;case"moveBackward":this.state.moveBackward=!1;break t;case"moveLeft":this.state.moveLeft=!1;break t;case"moveRight":this.state.moveRight=!1;break t;case"moveUp":this.state.moveUp=!1;break t;case"moveDown":this.state.moveDown=!1;break t;case"rotateUp":case"rotateDown":case"rotateLeft":case"rotateRight":this.state.canRotate=!1;break t}}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.container.setPointerCapture(t.pointerId),this._pointerId=t.pointerId,this.state.canRotate=!0)}onPointerMove(t){if(t.preventDefault(),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.container.releasePointerCapture(t.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:a,camera:r,clashCheckDistance:h,clashDistance:c,clashLayers:l}=this;let{kneeInterObject:v,eyeInterObject:m}=this;if(!v){const e=t.clone().setY(t.y-n+a),s=new o(e,i,0,h);s.firstHitOnly=!0,null!=l&&d(s.layers,l),s.camera=r;const c=p(s,this.getCheckedObjects(e));this.kneeInterObject=v=c[0]}let y=-s;if(v&&(y+=v.distance,y<c))return!0;if(!m){const e=new o(t,i,0,h);e.firstHitOnly=!0,null!=l&&d(e.layers,l),e.camera=r;const s=p(e,this.getCheckedObjects(t));this.eyeInterObject=m=s[0]}let u=-s;if(m){if(u+=m.distance,u<c)return!0;m.distance=u}return v&&(v.distance=y),!1}getCheckedObjects(t){const{clashDistance:e,clashCheckDistance:s,searchRadiusFactor:i,clashFilter:o}=this,n=s*i,a=this.checkedSphere,r=n+e;a.set(t,r);const h=null!=o?o:function(){return!0};return this.viewport.scener.intersectsList.getAll().filter((t=>!!h(t)&&l(a,t)))}getGravityCheckedObjects(t,s){const i=this.gravityCheckedBox;let o=this.gravityCheckedObjects;if(o){const e=t.clone();e.y=s,i.containsPoint(t)&&i.containsPoint(e)||(o=null)}if(!o){const{eyeHeight:s,clashCheckDistance:n,gravitySearchFactor:a,clashDistance:r,clashFilter:h}=this,c=n+r,d=new e(c,c,0),p=t.clone().add(d),v=t.clone().sub(d);v.y-=s*a,i.set(v,p);const m=this.viewport.scener.intersectsList.getAll(),y=null!=h?h:function(){return!0};o=m.filter((t=>!!y(t)&&l(i,t)))}return o}gravityClashCheck(t,s){var i;let a=this.gravityInterObject;if(a){const{object:s,face:o,instanceId:r}=a,h=null===(i=s.geometry)||void 0===i?void 0:i.getAttribute("position");if(h&&o){let i=a.facePoints;if(!i){let t=s.matrixWorld;if(null!=r){const e=new n;s.getMatrixAt(r,e),t=e.premultiply(t)}const{a:c,b:l,c:d}=o,p=new e(h.getX(c),h.getY(c),h.getZ(c));p.applyMatrix4(t),p.setY(0);const v=new e(h.getX(l),h.getY(l),h.getZ(l));v.applyMatrix4(t),v.setY(0);const m=new e(h.getX(d),h.getY(d),h.getZ(d));m.applyMatrix4(t),m.setY(0),a.facePoints=i=[p,v,m]}let c=i[2],l=null;const d=t.clone();d.y=0;i.every((t=>{const e=t.clone();e.sub(c);const s=d.clone();if(s.sub(c),e.cross(s),l){if(e.dot(l)<=0)return!1}return l=e,c=t,!0}))||(this.gravityInterObject=a=null)}}if(!a){const i=new o(t,new e(0,-1,0));i.firstHitOnly=!0;const n=this.clashLayers;null!=n&&d(i.layers,n),i.camera=this.camera;const r=p(i,this.getGravityCheckedObjects(t,s));this.gravityInterObject=a=r[0]}return a}setOptions(t){const{position:s=new e,rotation:i=new a,moveSpeed:o,eyeHeight:n,kneeHeight:r,jumpHeight:h,enableClash:c,enableGravity:l,searchRadiusFactor:d,clashDistance:p,clashCheckDistance:m,gravitySpeed:u,gravitySearchFactor:b,reverseRotate:g,rotate:w,rotateSpeed:f,horizontalRotate:k,verticalRotate:C,dblClickForward:D,clashFilter:j,clashLayers:R}=t;if(n&&(this.eyeHeight=n),r&&(this.kneeHeight=r),h&&(this.jumpHeight=h),d&&(this.searchRadiusFactor=d),p&&(this.clashDistance=p),m&&(this.clashCheckDistance=m),u&&(this.gravitySpeed=u),b&&(this.gravitySearchFactor=b),null!=g&&(this.reverseRotate=g),null!=w&&(this.rotate=w),null!=k&&(this.horizontalRotate=k),null!=C&&(this.verticalRotate=C),y.isBoolean(D)&&(this.dblClickForward=D),y.isNumber(f)&&(this.rotateSpeed=f),null!=c&&(this.enableClash=c),null!=l&&(this.enableGravity=l),null!=j&&(this.clashFilter=j),null!=R&&(this.clashLayers=R),s&&(this.controls.setPosition(s.x,s.y+this.eyeHeight,s.z),this.gravityInterObject=null,this.clearClashCache()),i){const t=y.computeTargetByRotation(this.controls.getPosition(new e),i,v);this.controls.setTarget(t.x,t.y,t.z)}o&&(this.moveSpeed=o)}start(t){this.viewport.controls.saveState(),this.viewport.controls.enabled=!1,this.enabled=!0,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.container;e.addEventListener("pointerdown",this.onPointerDown),e.addEventListener("pointermove",this.onPointerMove),e.addEventListener("pointerup",this.onPointerUp),e.style.touchAction="none",e.style.userSelect="none"}stop(){this.viewport.controls.reset(),this.viewport.controls.enabled=!0,this.enabled=!1,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.container;t.removeEventListener("pointerdown",this.onPointerDown),t.removeEventListener("pointermove",this.onPointerMove),t.removeEventListener("pointerup",this.onPointerUp),t.style.touchAction="auto",t.style.userSelect="auto",this.ssp.render()}update(){if(!this.enabled)return;const{delta:t}=this.viewport.state,s=new e,{controls:i,state:o,camera:n,eyeHeight:h,kneeHeight:c}=this,l=i.getPosition(new e),d=20*this.moveSpeed,p=20*this.gravitySpeed;o.moveForward&&(s.z-=d),o.moveBackward&&(s.z+=d),o.moveLeft&&(s.x-=d),o.moveRight&&(s.x+=d),o.moveUp&&(s.y+=2*p),o.moveDown&&(s.y-=2*p),s.multiplyScalar(t);const{x:y,y:u}=this.movement,b=this.horizontalRotate&&0!==y,g=this.verticalRotate&&0!==u;if(this.rotate&&o.canRotate&&(b||g)){let{azimuthAngle:t,polarAngle:e}=i;g&&(e-=.003*u*this.rotateSpeed),b&&(t-=.003*y*this.rotateSpeed),i.rotateTo(t,e)}const w=new a(0,0,0,"YXZ");if(w.setFromQuaternion(n.quaternion),0!==w.y&&!s.equals(new e)){const t=new r;t.setFromAxisAngle(new e(0,1,0),w.y),s.applyQuaternion(t)}if(!this._dblVector.equals(new e)){const e=this._dblVector.clone().multiplyScalar(t);this._dblDeltaVector.add(e),this._dblDeltaVector.length()>this._dblVector.length()?(this._dblVector.set(0,0,0),this._dblDeltaVector.set(0,0,0)):s.add(e)}const f=l.clone();if((!this.enableClash||!this.onClashCheck(l,s))&&(f.add(s),this.gravityInterObject)){const t=this.gravityInterObject.point.clone().sub(f);t.y=0,t.length()>c&&(this.gravityInterObject=null)}let k=f.y;if(this.enableGravity){const e=k;this.jumpOffset>0&&(this.jumpOffset-=p*t,k+=2*p*t),k-=p*t,o.canJump=!1;const s=this.gravityClashCheck(f,k);if(s){const t=s.point.y+h;k<t&&(k=t,this.jumpOffset=0,o.canJump=!0)}e!==k&&this.clearClashCache()}f.y=k,m(l.x,f.x)&&m(l.y,f.y)&&m(l.z,f.z)||(i.moveTo(f.x,f.y,f.z),i.dollyTo(v))}}export{u as default};
1
+ import{Vector2 as t,Vector3 as e,Sphere as s,Box3 as i,Raycaster as o,Matrix4 as n,Euler as a,Quaternion as r}from"three";import h from"soonspacejs";const{utils:{isObjectVisible:c}}=h;function l(t,e){const s=h.utils.getBoundingBox(e);return t.intersectsBox(s)}function d(t,e){t.disableAll();for(const s of e)t.enable(s)}function p(t,e){return t.intersectObjects(e).filter(({object:t})=>c(t))}const{EPSILON:v,approxEquals:m}=h.utils,{utils:y}=h;class u{get rotate(){return this._rotate&&(this.horizontalRotate||this.verticalRotate)}set rotate(t){this._rotate=t}constructor(o){this.enabled=!1,this.keyCodeMap={moveForward:["ArrowUp","KeyW"],moveBackward:["ArrowDown","KeyS"],moveLeft:["ArrowLeft","KeyA"],moveRight:["ArrowRight","KeyD"],moveUp:["KeyQ"],moveDown:["KeyE"],rotateUp:["KeyI"],rotateDown:["KeyK"],rotateLeft:["KeyJ"],rotateRight:["KeyL"],jump:["Space"]},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.movement=new t,this._dblVector=new e,this._dblDeltaVector=new e,this._pointerId=null,this.lastDirection=new e,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=o,this.viewport=o.viewport,this.controls=o.controls,this.camera=o.controls.camera,this.container=o.viewport.container,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){t:for(const[e,s]of Object.entries(this.keyCodeMap))if(s.includes(t.code))switch(e){case"moveForward":this.state.moveForward=!0;break t;case"moveBackward":this.state.moveBackward=!0;break t;case"moveLeft":this.state.moveLeft=!0;break t;case"moveRight":this.state.moveRight=!0;break t;case"moveUp":this.state.moveUp=!0;break t;case"moveDown":this.state.moveDown=!0;break t;case"rotateUp":this.state.canRotate=!0,this.movement.x=0,this.movement.y=-10;break t;case"rotateDown":this.state.canRotate=!0,this.movement.x=0,this.movement.y=10;break t;case"rotateLeft":this.state.canRotate=!0,this.movement.x=-10,this.movement.y=0;break t;case"rotateRight":this.state.canRotate=!0,this.movement.x=10,this.movement.y=0;break t;case"jump":!0===this.state.canJump&&!1===t.repeat&&(this.jumpOffset=this.jumpHeight),this.state.canJump=!1;break t}(this.jumpOffset>0||Object.values(this.state).some(t=>t))&&this.ssp.render()}}onKeyUp(t){t:for(const[e,s]of Object.entries(this.keyCodeMap))if(s.includes(t.code))switch(e){case"moveForward":this.state.moveForward=!1;break t;case"moveBackward":this.state.moveBackward=!1;break t;case"moveLeft":this.state.moveLeft=!1;break t;case"moveRight":this.state.moveRight=!1;break t;case"moveUp":this.state.moveUp=!1;break t;case"moveDown":this.state.moveDown=!1;break t;case"rotateUp":case"rotateDown":case"rotateLeft":case"rotateRight":this.state.canRotate=!1;break t}}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.container.setPointerCapture(t.pointerId),this._pointerId=t.pointerId,this.state.canRotate=!0)}onPointerMove(t){if(t.preventDefault(),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.container.releasePointerCapture(t.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:a,camera:r,clashCheckDistance:h,clashDistance:c,clashLayers:l}=this;let{kneeInterObject:v,eyeInterObject:m}=this;if(!v){const e=t.clone().setY(t.y-n+a),s=new o(e,i,0,h);s.firstHitOnly=!0,null!=l&&d(s.layers,l),s.camera=r;const c=p(s,this.getCheckedObjects(e));this.kneeInterObject=v=c[0]}let y=-s;if(v&&(y+=v.distance,y<c))return!0;if(!m){const e=new o(t,i,0,h);e.firstHitOnly=!0,null!=l&&d(e.layers,l),e.camera=r;const s=p(e,this.getCheckedObjects(t));this.eyeInterObject=m=s[0]}let u=-s;if(m){if(u+=m.distance,u<c)return!0;m.distance=u}return v&&(v.distance=y),!1}getCheckedObjects(t){const{clashDistance:e,clashCheckDistance:s,searchRadiusFactor:i,clashFilter:o}=this,n=s*i,a=this.checkedSphere,r=n+e;a.set(t,r);const h=null!=o?o:function(){return!0};return this.viewport.scener.intersectsList.getAll().filter(t=>!!h(t)&&l(a,t))}getGravityCheckedObjects(t,s){const i=this.gravityCheckedBox;let o=this.gravityCheckedObjects;if(o){const e=t.clone();e.y=s,i.containsPoint(t)&&i.containsPoint(e)||(o=null)}if(!o){const{eyeHeight:s,clashCheckDistance:n,gravitySearchFactor:a,clashDistance:r,clashFilter:h}=this,c=n+r,d=new e(c,c,0),p=t.clone().add(d),v=t.clone().sub(d);v.y-=s*a,i.set(v,p);const m=this.viewport.scener.intersectsList.getAll(),y=null!=h?h:function(){return!0};o=m.filter(t=>!!y(t)&&l(i,t))}return o}gravityClashCheck(t,s){var i;let a=this.gravityInterObject;if(a){const{object:s,face:o,instanceId:r}=a,h=null===(i=s.geometry)||void 0===i?void 0:i.getAttribute("position");if(h&&o){let i=a.facePoints;if(!i){let t=s.matrixWorld;if(null!=r){const e=new n;s.getMatrixAt(r,e),t=e.premultiply(t)}const{a:c,b:l,c:d}=o,p=new e(h.getX(c),h.getY(c),h.getZ(c));p.applyMatrix4(t),p.setY(0);const v=new e(h.getX(l),h.getY(l),h.getZ(l));v.applyMatrix4(t),v.setY(0);const m=new e(h.getX(d),h.getY(d),h.getZ(d));m.applyMatrix4(t),m.setY(0),a.facePoints=i=[p,v,m]}let c=i[2],l=null;const d=t.clone();d.y=0;i.every(t=>{const e=t.clone();e.sub(c);const s=d.clone();if(s.sub(c),e.cross(s),l){if(e.dot(l)<=0)return!1}return l=e,c=t,!0})||(this.gravityInterObject=a=null)}}if(!a){const i=new o(t,new e(0,-1,0));i.firstHitOnly=!0;const n=this.clashLayers;null!=n&&d(i.layers,n),i.camera=this.camera;const r=p(i,this.getGravityCheckedObjects(t,s));this.gravityInterObject=a=r[0]}return a}setOptions(t){const{position:s=new e,rotation:i=new a,moveSpeed:o,eyeHeight:n,kneeHeight:r,jumpHeight:h,enableClash:c,enableGravity:l,searchRadiusFactor:d,clashDistance:p,clashCheckDistance:m,gravitySpeed:u,gravitySearchFactor:b,reverseRotate:g,rotate:w,rotateSpeed:f,horizontalRotate:k,verticalRotate:C,dblClickForward:D,clashFilter:j,clashLayers:R}=t;if(n&&(this.eyeHeight=n),r&&(this.kneeHeight=r),h&&(this.jumpHeight=h),d&&(this.searchRadiusFactor=d),p&&(this.clashDistance=p),m&&(this.clashCheckDistance=m),u&&(this.gravitySpeed=u),b&&(this.gravitySearchFactor=b),null!=g&&(this.reverseRotate=g),null!=w&&(this.rotate=w),null!=k&&(this.horizontalRotate=k),null!=C&&(this.verticalRotate=C),y.isBoolean(D)&&(this.dblClickForward=D),y.isNumber(f)&&(this.rotateSpeed=f),null!=c&&(this.enableClash=c),null!=l&&(this.enableGravity=l),null!=j&&(this.clashFilter=j),null!=R&&(this.clashLayers=R),s&&(this.controls.setPosition(s.x,s.y+this.eyeHeight,s.z),this.gravityInterObject=null,this.clearClashCache()),i){const t=y.computeTargetByRotation(this.controls.getPosition(new e),i,v);this.controls.setTarget(t.x,t.y,t.z)}o&&(this.moveSpeed=o)}start(t){this.viewport.controls.saveState(),this.viewport.controls.enabled=!1,this.enabled=!0,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.container;e.addEventListener("pointerdown",this.onPointerDown),e.addEventListener("pointermove",this.onPointerMove),e.addEventListener("pointerup",this.onPointerUp),e.style.touchAction="none",e.style.userSelect="none"}stop(){this.viewport.controls.reset(),this.viewport.controls.enabled=!0,this.enabled=!1,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.container;t.removeEventListener("pointerdown",this.onPointerDown),t.removeEventListener("pointermove",this.onPointerMove),t.removeEventListener("pointerup",this.onPointerUp),t.style.touchAction="auto",t.style.userSelect="auto",this.ssp.render()}update(){if(!this.enabled)return;const{delta:t}=this.viewport.state,s=new e,{controls:i,state:o,camera:n,eyeHeight:h,kneeHeight:c}=this,l=i.getPosition(new e),d=20*this.moveSpeed,p=20*this.gravitySpeed;o.moveForward&&(s.z-=d),o.moveBackward&&(s.z+=d),o.moveLeft&&(s.x-=d),o.moveRight&&(s.x+=d),o.moveUp&&(s.y+=2*p),o.moveDown&&(s.y-=2*p),s.multiplyScalar(t);const{x:y,y:u}=this.movement,b=this.horizontalRotate&&0!==y,g=this.verticalRotate&&0!==u;if(this.rotate&&o.canRotate&&(b||g)){let{azimuthAngle:t,polarAngle:e}=i;g&&(e-=.003*u*this.rotateSpeed),b&&(t-=.003*y*this.rotateSpeed),i.rotateTo(t,e)}const w=new a(0,0,0,"YXZ");if(w.setFromQuaternion(n.quaternion),0!==w.y&&!s.equals(new e)){const t=new r;t.setFromAxisAngle(new e(0,1,0),w.y),s.applyQuaternion(t)}if(!this._dblVector.equals(new e)){const e=this._dblVector.clone().multiplyScalar(t);this._dblDeltaVector.add(e),this._dblDeltaVector.length()>this._dblVector.length()?(this._dblVector.set(0,0,0),this._dblDeltaVector.set(0,0,0)):s.add(e)}const f=l.clone();if((!this.enableClash||!this.onClashCheck(l,s))&&(f.add(s),this.gravityInterObject)){const t=this.gravityInterObject.point.clone().sub(f);t.y=0,t.length()>c&&(this.gravityInterObject=null)}let k=f.y;if(this.enableGravity){const e=k;this.jumpOffset>0&&(this.jumpOffset-=p*t,k+=2*p*t),k-=p*t,o.canJump=!1;const s=this.gravityClashCheck(f,k);if(s){const t=s.point.y+h;k<t&&(k=t,this.jumpOffset=0,o.canJump=!0)}e!==k&&this.clearClashCache()}f.y=k,m(l.x,f.x)&&m(l.y,f.y)&&m(l.z,f.z)||(i.moveTo(f.x,f.y,f.z),i.dollyTo(v))}}export{u as default};
package/dist/tools.d.ts CHANGED
@@ -1,7 +1,7 @@
1
- import { Box3, Object3D, Sphere, Layers, Raycaster } from 'three';
2
- /**
3
- * 判断包围合 与 对象的包围盒是否相交
4
- */
5
- export declare function boundingIsIntersected(target: Box3 | Sphere, obj: Object3D): boolean;
6
- export declare function setLayers(layer: Layers, layerNums: number[]): void;
7
- export declare function raycasterFilter(raycaster: Raycaster, objs: Object3D[]): import("three").Intersection<Object3D<import("three").Object3DEventMap>>[];
1
+ import { Box3, Object3D, Sphere, Layers, Raycaster } from 'three';
2
+ /**
3
+ * 判断包围合 与 对象的包围盒是否相交
4
+ */
5
+ export declare function boundingIsIntersected(target: Box3 | Sphere, obj: Object3D): boolean;
6
+ export declare function setLayers(layer: Layers, layerNums: number[]): void;
7
+ export declare function raycasterFilter(raycaster: Raycaster, objs: Object3D[]): import("three").Intersection<Object3D<import("three").Object3DEventMap>>[];
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.13.9",
4
+ "version": "2.13.11",
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": "xunwei",
15
15
  "license": "UNLICENSED",
16
- "gitHead": "a5596909f48bd136905b85f239e13690f9f2c66e",
16
+ "gitHead": "08bf9efb38c291f22ca044b936fd49269c716bbb",
17
17
  "peerDependencies": {
18
- "soonspacejs": "2.13.9"
18
+ "soonspacejs": "2.13.11"
19
19
  }
20
20
  }