three-base 2.0.122 → 2.0.124
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/FPScontrol.d.ts +2 -0
- package/FPScontrol.js +45 -11
- package/package.json +1 -1
package/FPScontrol.d.ts
CHANGED
|
@@ -16,7 +16,9 @@ export default class FPScontrol extends EventDispatcher {
|
|
|
16
16
|
STEPS_PER_FRAME: number;
|
|
17
17
|
speed: number;
|
|
18
18
|
camera: Camera;
|
|
19
|
+
inertia: boolean;
|
|
19
20
|
octreeMap: Map<number, Octree>;
|
|
21
|
+
octree: Octree;
|
|
20
22
|
addOctree(obj: Object3D): Octree;
|
|
21
23
|
playerCollider: Capsule;
|
|
22
24
|
clock: Clock;
|
package/FPScontrol.js
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* @Last Modified by: xiaosihan
|
|
5
5
|
* @Last Modified time: 2024-11-07 21:57:39
|
|
6
6
|
*/
|
|
7
|
-
import { Clock, EventDispatcher, Vector3 } from "three";
|
|
7
|
+
import { Clock, EventDispatcher, Vector3, } from "three";
|
|
8
8
|
import { Capsule } from "three/examples/jsm/math/Capsule";
|
|
9
9
|
import { Octree } from "three/examples/jsm/math/Octree";
|
|
10
10
|
import { clamp, degToRad } from "three/src/math/MathUtils";
|
|
@@ -31,8 +31,34 @@ export default class FPScontrol extends EventDispatcher {
|
|
|
31
31
|
STEPS_PER_FRAME = 5; // 每帧碰撞计算的次数
|
|
32
32
|
speed = 1; // 速度
|
|
33
33
|
camera;
|
|
34
|
+
// 移动时是否保持惯性 由外部来配置
|
|
35
|
+
inertia = true;
|
|
34
36
|
// 八叉树数组
|
|
35
37
|
octreeMap = new Map();
|
|
38
|
+
octree = (() => {
|
|
39
|
+
const octree = new Octree();
|
|
40
|
+
octree.getCapsuleTriangles = (capsule, triangles) => {
|
|
41
|
+
const octrees = Array.from(this.octreeMap.values());
|
|
42
|
+
for (let octree of octrees) {
|
|
43
|
+
for (let i = 0; i < octree.subTrees.length; i++) {
|
|
44
|
+
const subTree = octree.subTrees[i];
|
|
45
|
+
if (!capsule.intersectsBox(subTree.box))
|
|
46
|
+
continue;
|
|
47
|
+
if (subTree.triangles.length > 0) {
|
|
48
|
+
for (let j = 0; j < subTree.triangles.length; j++) {
|
|
49
|
+
if (triangles.indexOf(subTree.triangles[j]) === -1)
|
|
50
|
+
triangles.push(subTree.triangles[j]);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
else {
|
|
54
|
+
subTree.getCapsuleTriangles(capsule, triangles);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
return triangles;
|
|
59
|
+
};
|
|
60
|
+
return octree;
|
|
61
|
+
})();
|
|
36
62
|
addOctree(obj) {
|
|
37
63
|
const octree = new Octree();
|
|
38
64
|
octree.fromGraphNode(obj);
|
|
@@ -95,6 +121,7 @@ export default class FPScontrol extends EventDispatcher {
|
|
|
95
121
|
// 玩家移动控制器
|
|
96
122
|
controls = (deltaTime) => {
|
|
97
123
|
const speedDelta = deltaTime * (this.playerOnFloor ? 25 : 8) * this.speed;
|
|
124
|
+
// const speedDelta = this.speed;
|
|
98
125
|
if (this.keyStates["KeyW"]) {
|
|
99
126
|
this.playerVelocity.add(this.getForwardVector().multiplyScalar(speedDelta));
|
|
100
127
|
}
|
|
@@ -107,6 +134,16 @@ export default class FPScontrol extends EventDispatcher {
|
|
|
107
134
|
if (this.keyStates["KeyD"]) {
|
|
108
135
|
this.playerVelocity.add(this.getSideVector().multiplyScalar(speedDelta));
|
|
109
136
|
}
|
|
137
|
+
if (this.playerOnFloor &&
|
|
138
|
+
!this.keyStates["KeyW"] &&
|
|
139
|
+
!this.keyStates["KeyS"] &&
|
|
140
|
+
!this.keyStates["KeyA"] &&
|
|
141
|
+
!this.keyStates["KeyD"]) {
|
|
142
|
+
if (!this.inertia) { // 关闭惯性时就大幅增加动能的衰减
|
|
143
|
+
this.playerVelocity.x *= 0.85;
|
|
144
|
+
this.playerVelocity.z *= 0.85;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
110
147
|
if (this.playerOnFloor) {
|
|
111
148
|
if (this.keyStates["Space"]) {
|
|
112
149
|
this.playerVelocity.y = 15;
|
|
@@ -131,16 +168,13 @@ export default class FPScontrol extends EventDispatcher {
|
|
|
131
168
|
playerCollisions() {
|
|
132
169
|
this.playerOnFloor = false;
|
|
133
170
|
// 遍历 八叉树 做碰撞检测
|
|
134
|
-
const
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
if (
|
|
138
|
-
this.
|
|
139
|
-
if (!this.playerOnFloor) {
|
|
140
|
-
this.playerVelocity.addScaledVector(result.normal, -result.normal.dot(this.playerVelocity));
|
|
141
|
-
}
|
|
142
|
-
this.playerCollider.translate(result.normal.multiplyScalar(result.depth));
|
|
171
|
+
const result = this.octree.capsuleIntersect(this.playerCollider);
|
|
172
|
+
if (result) {
|
|
173
|
+
this.playerOnFloor = result.normal.y > 0;
|
|
174
|
+
if (!this.playerOnFloor) {
|
|
175
|
+
this.playerVelocity.addScaledVector(result.normal, -result.normal.dot(this.playerVelocity));
|
|
143
176
|
}
|
|
177
|
+
this.playerCollider.translate(result.normal.multiplyScalar(result.depth));
|
|
144
178
|
}
|
|
145
179
|
}
|
|
146
180
|
// 更新控制器的位置
|
|
@@ -150,7 +184,7 @@ export default class FPScontrol extends EventDispatcher {
|
|
|
150
184
|
this.playerCollider.end.set(0, 1, 10);
|
|
151
185
|
this.playerCollider.radius = 0.35;
|
|
152
186
|
this.camera.position.copy(this.playerCollider.end);
|
|
153
|
-
//
|
|
187
|
+
// this.camera.position.set(0, 3, 0);
|
|
154
188
|
}
|
|
155
189
|
};
|
|
156
190
|
// 更新
|