forked from sillysecurityawards/awards-site
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cannon-es-debugger.js
300 lines (243 loc) · 7.86 KB
/
cannon-es-debugger.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
import { Vec3, Quaternion, Shape } from 'cannon-es';
import { MeshBasicMaterial, SphereGeometry, BoxGeometry, PlaneGeometry, Mesh, CylinderGeometry, BufferGeometry, Float32BufferAttribute } from 'three';
function CannonDebugger(scene, world, _temp) {
let {
color = 0x00ff00,
scale = 1,
onInit,
onUpdate
} = _temp === void 0 ? {} : _temp;
const _meshes = [];
const _material = new MeshBasicMaterial({
color: color != null ? color : 0x00ff00,
wireframe: true
});
const _tempVec0 = new Vec3();
const _tempVec1 = new Vec3();
const _tempVec2 = new Vec3();
const _tempQuat0 = new Quaternion();
const _sphereGeometry = new SphereGeometry(1);
const _boxGeometry = new BoxGeometry(1, 1, 1);
const _planeGeometry = new PlaneGeometry(10, 10, 10, 10); // Move the planeGeometry forward a little bit to prevent z-fighting
_planeGeometry.translate(0, 0, 0.0001);
function createConvexPolyhedronGeometry(shape) {
const geometry = new BufferGeometry(); // Add vertices
const positions = [];
for (let i = 0; i < shape.vertices.length; i++) {
const vertex = shape.vertices[i];
positions.push(vertex.x, vertex.y, vertex.z);
}
geometry.setAttribute('position', new Float32BufferAttribute(positions, 3)); // Add faces
const indices = [];
for (let i = 0; i < shape.faces.length; i++) {
const face = shape.faces[i];
const a = face[0];
for (let j = 1; j < face.length - 1; j++) {
const b = face[j];
const c = face[j + 1];
indices.push(a, b, c);
}
}
geometry.setIndex(indices);
geometry.computeBoundingSphere();
geometry.computeVertexNormals();
return geometry;
}
function createTrimeshGeometry(shape) {
const geometry = new BufferGeometry();
const positions = [];
const v0 = _tempVec0;
const v1 = _tempVec1;
const v2 = _tempVec2;
for (let i = 0; i < shape.indices.length / 3; i++) {
shape.getTriangleVertices(i, v0, v1, v2);
positions.push(v0.x, v0.y, v0.z);
positions.push(v1.x, v1.y, v1.z);
positions.push(v2.x, v2.y, v2.z);
}
geometry.setAttribute('position', new Float32BufferAttribute(positions, 3));
geometry.computeBoundingSphere();
geometry.computeVertexNormals();
return geometry;
}
function createHeightfieldGeometry(shape) {
const geometry = new BufferGeometry();
const s = shape.elementSize || 1; // assumes square heightfield, else i*x, j*y
const positions = shape.data.flatMap((row, i) => row.flatMap((z, j) => [i * s, j * s, z]));
const indices = [];
for (let xi = 0; xi < shape.data.length - 1; xi++) {
for (let yi = 0; yi < shape.data[xi].length - 1; yi++) {
const stride = shape.data[xi].length;
const index = xi * stride + yi;
indices.push(index + 1, index + stride, index + stride + 1);
indices.push(index + stride, index + 1, index);
}
}
geometry.setIndex(indices);
geometry.setAttribute('position', new Float32BufferAttribute(positions, 3));
geometry.computeBoundingSphere();
geometry.computeVertexNormals();
return geometry;
}
function createMesh(shape) {
let mesh = new Mesh();
const {
SPHERE,
BOX,
PLANE,
CYLINDER,
CONVEXPOLYHEDRON,
TRIMESH,
HEIGHTFIELD
} = Shape.types;
switch (shape.type) {
case SPHERE:
{
mesh = new Mesh(_sphereGeometry, _material);
break;
}
case BOX:
{
mesh = new Mesh(_boxGeometry, _material);
break;
}
case PLANE:
{
mesh = new Mesh(_planeGeometry, _material);
break;
}
case CYLINDER:
{
const geometry = new CylinderGeometry(shape.radiusTop, shape.radiusBottom, shape.height, shape.numSegments);
mesh = new Mesh(geometry, _material);
shape.geometryId = geometry.id;
break;
}
case CONVEXPOLYHEDRON:
{
const geometry = createConvexPolyhedronGeometry(shape);
mesh = new Mesh(geometry, _material);
shape.geometryId = geometry.id;
break;
}
case TRIMESH:
{
const geometry = createTrimeshGeometry(shape);
mesh = new Mesh(geometry, _material);
shape.geometryId = geometry.id;
break;
}
case HEIGHTFIELD:
{
const geometry = createHeightfieldGeometry(shape);
mesh = new Mesh(geometry, _material);
shape.geometryId = geometry.id;
break;
}
}
scene.add(mesh);
return mesh;
}
function scaleMesh(mesh, shape) {
const {
SPHERE,
BOX,
PLANE,
CYLINDER,
CONVEXPOLYHEDRON,
TRIMESH,
HEIGHTFIELD
} = Shape.types;
switch (shape.type) {
case SPHERE:
{
const {
radius
} = shape;
mesh.scale.set(radius * scale, radius * scale, radius * scale);
break;
}
case BOX:
{
mesh.scale.copy(shape.halfExtents);
mesh.scale.multiplyScalar(2 * scale);
break;
}
case PLANE:
{
break;
}
case CYLINDER:
{
mesh.scale.set(1 * scale, 1 * scale, 1 * scale);
break;
}
case CONVEXPOLYHEDRON:
{
mesh.scale.set(1 * scale, 1 * scale, 1 * scale);
break;
}
case TRIMESH:
{
mesh.scale.copy(shape.scale).multiplyScalar(scale);
break;
}
case HEIGHTFIELD:
{
mesh.scale.set(1 * scale, 1 * scale, 1 * scale);
break;
}
}
}
function typeMatch(mesh, shape) {
if (!mesh) return false;
const {
geometry
} = mesh;
return geometry instanceof SphereGeometry && shape.type === Shape.types.SPHERE || geometry instanceof BoxGeometry && shape.type === Shape.types.BOX || geometry instanceof PlaneGeometry && shape.type === Shape.types.PLANE || geometry.id === shape.geometryId && shape.type === Shape.types.CYLINDER || geometry.id === shape.geometryId && shape.type === Shape.types.CONVEXPOLYHEDRON || geometry.id === shape.geometryId && shape.type === Shape.types.TRIMESH || geometry.id === shape.geometryId && shape.type === Shape.types.HEIGHTFIELD;
}
function updateMesh(index, shape) {
let mesh = _meshes[index];
let didCreateNewMesh = false;
if (!typeMatch(mesh, shape)) {
if (mesh) scene.remove(mesh);
_meshes[index] = mesh = createMesh(shape);
didCreateNewMesh = true;
}
scaleMesh(mesh, shape);
return didCreateNewMesh;
}
function update() {
const meshes = _meshes;
const shapeWorldPosition = _tempVec0;
const shapeWorldQuaternion = _tempQuat0;
let meshIndex = 0;
for (const body of world.bodies) {
for (let i = 0; i !== body.shapes.length; i++) {
const shape = body.shapes[i];
const didCreateNewMesh = updateMesh(meshIndex, shape);
const mesh = meshes[meshIndex];
if (mesh) {
// Get world position
body.quaternion.vmult(body.shapeOffsets[i], shapeWorldPosition);
body.position.vadd(shapeWorldPosition, shapeWorldPosition); // Get world quaternion
body.quaternion.mult(body.shapeOrientations[i], shapeWorldQuaternion); // Copy to meshes
mesh.position.copy(shapeWorldPosition);
mesh.quaternion.copy(shapeWorldQuaternion);
if (didCreateNewMesh && onInit instanceof Function) onInit(body, mesh, shape);
if (!didCreateNewMesh && onUpdate instanceof Function) onUpdate(body, mesh, shape);
}
meshIndex++;
}
}
for (let i = meshIndex; i < meshes.length; i++) {
const mesh = meshes[i];
if (mesh) scene.remove(mesh);
}
meshes.length = meshIndex;
}
return {
update
};
}
export { CannonDebugger as default };