three-base 2.0.121 → 2.0.123
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 +1 -0
- package/FPScontrol.js +32 -11
- package/package.json +1 -1
package/FPScontrol.d.ts
CHANGED
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";
|
|
@@ -33,6 +33,30 @@ export default class FPScontrol extends EventDispatcher {
|
|
|
33
33
|
camera;
|
|
34
34
|
// 八叉树数组
|
|
35
35
|
octreeMap = new Map();
|
|
36
|
+
octree = (() => {
|
|
37
|
+
const octree = new Octree();
|
|
38
|
+
octree.getCapsuleTriangles = (capsule, triangles) => {
|
|
39
|
+
const octrees = Array.from(this.octreeMap.values());
|
|
40
|
+
for (let octree of octrees) {
|
|
41
|
+
for (let i = 0; i < octree.subTrees.length; i++) {
|
|
42
|
+
const subTree = octree.subTrees[i];
|
|
43
|
+
if (!capsule.intersectsBox(subTree.box))
|
|
44
|
+
continue;
|
|
45
|
+
if (subTree.triangles.length > 0) {
|
|
46
|
+
for (let j = 0; j < subTree.triangles.length; j++) {
|
|
47
|
+
if (triangles.indexOf(subTree.triangles[j]) === -1)
|
|
48
|
+
triangles.push(subTree.triangles[j]);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
else {
|
|
52
|
+
subTree.getCapsuleTriangles(capsule, triangles);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return triangles;
|
|
57
|
+
};
|
|
58
|
+
return octree;
|
|
59
|
+
})();
|
|
36
60
|
addOctree(obj) {
|
|
37
61
|
const octree = new Octree();
|
|
38
62
|
octree.fromGraphNode(obj);
|
|
@@ -131,16 +155,13 @@ export default class FPScontrol extends EventDispatcher {
|
|
|
131
155
|
playerCollisions() {
|
|
132
156
|
this.playerOnFloor = false;
|
|
133
157
|
// 遍历 八叉树 做碰撞检测
|
|
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));
|
|
158
|
+
const result = this.octree.capsuleIntersect(this.playerCollider);
|
|
159
|
+
if (result) {
|
|
160
|
+
this.playerOnFloor = result.normal.y > 0;
|
|
161
|
+
if (!this.playerOnFloor) {
|
|
162
|
+
this.playerVelocity.addScaledVector(result.normal, -result.normal.dot(this.playerVelocity));
|
|
143
163
|
}
|
|
164
|
+
this.playerCollider.translate(result.normal.multiplyScalar(result.depth));
|
|
144
165
|
}
|
|
145
166
|
}
|
|
146
167
|
// 更新控制器的位置
|
|
@@ -150,7 +171,7 @@ export default class FPScontrol extends EventDispatcher {
|
|
|
150
171
|
this.playerCollider.end.set(0, 1, 10);
|
|
151
172
|
this.playerCollider.radius = 0.35;
|
|
152
173
|
this.camera.position.copy(this.playerCollider.end);
|
|
153
|
-
//
|
|
174
|
+
// this.camera.position.set(0, 3, 0);
|
|
154
175
|
}
|
|
155
176
|
};
|
|
156
177
|
// 更新
|