forked from InfiniteLee/three-to-ammo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
805 lines (701 loc) · 26.3 KB
/
index.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
"use strict";
/* global Ammo */
import * as THREE from "three";
export const TYPE = {
BOX: "box",
CYLINDER: "cylinder",
SPHERE: "sphere",
CAPSULE: "capsule",
CONE: "cone",
HULL: "hull",
HACD: "hacd", //Hierarchical Approximate Convex Decomposition
VHACD: "vhacd", //Volumetric Hierarchical Approximate Convex Decomposition
MESH: "mesh",
HEIGHTFIELD: "heightfield"
};
export const FIT = {
ALL: "all", //A single shape is automatically sized to bound all meshes within the entity.
MANUAL: "manual" //A single shape is sized manually. Requires halfExtents or sphereRadius.
};
export const HEIGHTFIELD_DATA_TYPE = {
short: "short",
float: "float"
};
export const createCollisionShapes = function(vertices, matrices, indexes, matrixWorld, options = {}) {
switch (options.type) {
case TYPE.BOX:
return [createBoxShape(vertices, matrices, matrixWorld, options)];
case TYPE.CYLINDER:
return [createCylinderShape(vertices, matrices, matrixWorld, options)];
case TYPE.CAPSULE:
return [createCapsuleShape(vertices, matrices, matrixWorld, options)];
case TYPE.CONE:
return [createConeShape(vertices, matrices, matrixWorld, options)];
case TYPE.SPHERE:
return [createSphereShape(vertices, matrices, matrixWorld, options)];
case TYPE.HULL:
return [createHullShape(vertices, matrices, matrixWorld, options)];
case TYPE.HACD:
return createHACDShapes(vertices, matrices, indexes, matrixWorld, options);
case TYPE.VHACD:
return createVHACDShapes(vertices, matrices, indexes, matrixWorld, options);
case TYPE.MESH:
return [createTriMeshShape(vertices, matrices, indexes, matrixWorld, options)];
case TYPE.HEIGHTFIELD:
return [createHeightfieldTerrainShape(options)];
default:
console.warn(options.type + " is not currently supported");
return [];
}
};
//TODO: support gimpact (dynamic trimesh) and heightmap
export const createBoxShape = function(vertices, matrices, matrixWorld, options = {}) {
options.type = TYPE.BOX;
_setOptions(options);
if (options.fit === FIT.ALL) {
options.halfExtents = _computeHalfExtents(
_computeBounds(vertices, matrices),
options.minHalfExtent,
options.maxHalfExtent
);
}
const btHalfExtents = new Ammo.btVector3(options.halfExtents.x, options.halfExtents.y, options.halfExtents.z);
const collisionShape = new Ammo.btBoxShape(btHalfExtents);
Ammo.destroy(btHalfExtents);
_finishCollisionShape(collisionShape, options, _computeScale(matrixWorld, options));
return collisionShape;
};
export const createCylinderShape = function(vertices, matrices, matrixWorld, options = {}) {
options.type = TYPE.CYLINDER;
_setOptions(options);
if (options.fit === FIT.ALL) {
options.halfExtents = _computeHalfExtents(
_computeBounds(vertices, matrices),
options.minHalfExtent,
options.maxHalfExtent
);
}
const btHalfExtents = new Ammo.btVector3(options.halfExtents.x, options.halfExtents.y, options.halfExtents.z);
const collisionShape = (() => {
switch (options.cylinderAxis) {
case "y":
return new Ammo.btCylinderShape(btHalfExtents);
case "x":
return new Ammo.btCylinderShapeX(btHalfExtents);
case "z":
return new Ammo.btCylinderShapeZ(btHalfExtents);
}
return null;
})();
Ammo.destroy(btHalfExtents);
_finishCollisionShape(collisionShape, options, _computeScale(matrixWorld, options));
return collisionShape;
};
export const createCapsuleShape = function(vertices, matrices, matrixWorld, options = {}) {
options.type = TYPE.CAPSULE;
_setOptions(options);
if (options.fit === FIT.ALL) {
options.halfExtents = _computeHalfExtents(
_computeBounds(vertices, matrices),
options.minHalfExtent,
options.maxHalfExtent
);
}
const { x, y, z } = options.halfExtents;
const collisionShape = (() => {
switch (options.cylinderAxis) {
case "y":
return new Ammo.btCapsuleShape(Math.max(x, z), y * 2);
case "x":
return new Ammo.btCapsuleShapeX(Math.max(y, z), x * 2);
case "z":
return new Ammo.btCapsuleShapeZ(Math.max(x, y), z * 2);
}
return null;
})();
_finishCollisionShape(collisionShape, options, _computeScale(matrixWorld, options));
return collisionShape;
};
export const createConeShape = function(vertices, matrices, matrixWorld, options = {}) {
options.type = TYPE.CONE;
_setOptions(options);
if (options.fit === FIT.ALL) {
options.halfExtents = _computeHalfExtents(
_computeBounds(vertices, matrices),
options.minHalfExtent,
options.maxHalfExtent
);
}
const { x, y, z } = options.halfExtents;
const collisionShape = (() => {
switch (options.cylinderAxis) {
case "y":
return new Ammo.btConeShape(Math.max(x, z), y * 2);
case "x":
return new Ammo.btConeShapeX(Math.max(y, z), x * 2);
case "z":
return new Ammo.btConeShapeZ(Math.max(x, y), z * 2);
}
return null;
})();
_finishCollisionShape(collisionShape, options, _computeScale(matrixWorld, options));
return collisionShape;
};
export const createSphereShape = function(vertices, matrices, matrixWorld, options = {}) {
options.type = TYPE.SPHERE;
_setOptions(options);
let radius;
if (options.fit === FIT.MANUAL && !isNaN(options.sphereRadius)) {
radius = options.sphereRadius;
} else {
radius = _computeRadius(vertices, matrices, _computeBounds(vertices, matrices));
}
const collisionShape = new Ammo.btSphereShape(radius);
_finishCollisionShape(collisionShape, options, _computeScale(matrixWorld, options));
return collisionShape;
};
export const createHullShape = (function() {
const vertex = new THREE.Vector3();
const center = new THREE.Vector3();
const matrix = new THREE.Matrix4();
return function(vertices, matrices, matrixWorld, options = {}) {
options.type = TYPE.HULL;
_setOptions(options);
if (options.fit === FIT.MANUAL) {
console.warn("cannot use fit: manual with type: hull");
return null;
}
const bounds = _computeBounds(vertices, matrices);
const btVertex = new Ammo.btVector3();
const originalHull = new Ammo.btConvexHullShape();
originalHull.setMargin(options.margin);
center.addVectors(bounds.max, bounds.min).multiplyScalar(0.5);
let vertexCount = 0;
for (let i = 0; i < vertices.length; i++) {
vertexCount += vertices[i].length / 3;
}
const maxVertices = options.hullMaxVertices || 100000;
// todo: might want to implement this in a deterministic way that doesn't do O(verts) calls to Math.random
if (vertexCount > maxVertices) {
console.warn(`too many vertices for hull shape; sampling ~${maxVertices} from ~${vertexCount} vertices`);
}
const p = Math.min(1, maxVertices / vertexCount);
for (let i = 0; i < vertices.length; i++) {
const components = vertices[i];
matrix.fromArray(matrices[i]);
for (let j = 0; j < components.length; j += 3) {
const isLastVertex = i === vertices.length - 1 && j === components.length - 3;
if (Math.random() <= p || isLastVertex) {
// always include the last vertex
vertex
.set(components[j], components[j + 1], components[j + 2])
.applyMatrix4(matrix)
.sub(center);
btVertex.setValue(vertex.x, vertex.y, vertex.z);
originalHull.addPoint(btVertex, isLastVertex); // recalc AABB only on last geometry
}
}
}
let collisionShape = originalHull;
if (originalHull.getNumVertices() >= 100) {
//Bullet documentation says don't use convexHulls with 100 verts or more
const shapeHull = new Ammo.btShapeHull(originalHull);
shapeHull.buildHull(options.margin);
Ammo.destroy(originalHull);
collisionShape = new Ammo.btConvexHullShape(
Ammo.getPointer(shapeHull.getVertexPointer()),
shapeHull.numVertices()
);
Ammo.destroy(shapeHull); // btConvexHullShape makes a copy
}
Ammo.destroy(btVertex);
_finishCollisionShape(collisionShape, options, _computeScale(matrixWorld, options));
return collisionShape;
};
})();
export const createHACDShapes = (function() {
const vector = new THREE.Vector3();
const center = new THREE.Vector3();
const matrix = new THREE.Matrix4();
return function(vertices, matrices, indexes, matrixWorld, options = {}) {
options.type = TYPE.HACD;
_setOptions(options);
if (options.fit === FIT.MANUAL) {
console.warn("cannot use fit: manual with type: hacd");
return [];
}
if (!Ammo.hasOwnProperty("HACD")) {
console.warn(
"HACD unavailable in included build of Ammo.js. Visit https://github.com/mozillareality/ammo.js for the latest version."
);
return [];
}
const bounds = _computeBounds(vertices, matrices);
const scale = _computeScale(matrixWorld, options);
let vertexCount = 0;
let triCount = 0;
center.addVectors(bounds.max, bounds.min).multiplyScalar(0.5);
for (let i = 0; i < vertices.length; i++) {
vertexCount += vertices[i].length / 3;
if (indexes && indexes[i]) {
triCount += indexes[i].length / 3;
} else {
triCount += vertices[i].length / 9;
}
}
const hacd = new Ammo.HACD();
if (options.hasOwnProperty("compacityWeight")) hacd.SetCompacityWeight(options.compacityWeight);
if (options.hasOwnProperty("volumeWeight")) hacd.SetVolumeWeight(options.volumeWeight);
if (options.hasOwnProperty("nClusters")) hacd.SetNClusters(options.nClusters);
if (options.hasOwnProperty("nVerticesPerCH")) hacd.SetNVerticesPerCH(options.nVerticesPerCH);
if (options.hasOwnProperty("concavity")) hacd.SetConcavity(options.concavity);
const points = Ammo._malloc(vertexCount * 3 * 8);
const triangles = Ammo._malloc(triCount * 3 * 4);
hacd.SetPoints(points);
hacd.SetTriangles(triangles);
hacd.SetNPoints(vertexCount);
hacd.SetNTriangles(triCount);
let pptr = points / 8,
tptr = triangles / 4;
for (let i = 0; i < vertices.length; i++) {
const components = vertices[i];
matrix.fromArray(matrices[i]);
for (let j = 0; j < components.length; j += 3) {
vector
.set(components[j + 0], components[j + 1], components[j + 2])
.applyMatrix4(matrix)
.sub(center);
Ammo.HEAPF64[pptr + 0] = vector.x;
Ammo.HEAPF64[pptr + 1] = vector.y;
Ammo.HEAPF64[pptr + 2] = vector.z;
pptr += 3;
}
if (indexes[i]) {
const indices = indexes[i];
for (let j = 0; j < indices.length; j++) {
Ammo.HEAP32[tptr] = indices[j];
tptr++;
}
} else {
for (let j = 0; j < components.length / 3; j++) {
Ammo.HEAP32[tptr] = j;
tptr++;
}
}
}
hacd.Compute();
Ammo._free(points);
Ammo._free(triangles);
const nClusters = hacd.GetNClusters();
const shapes = [];
for (let i = 0; i < nClusters; i++) {
const hull = new Ammo.btConvexHullShape();
hull.setMargin(options.margin);
const nPoints = hacd.GetNPointsCH(i);
const nTriangles = hacd.GetNTrianglesCH(i);
const hullPoints = Ammo._malloc(nPoints * 3 * 8);
const hullTriangles = Ammo._malloc(nTriangles * 3 * 4);
hacd.GetCH(i, hullPoints, hullTriangles);
const pptr = hullPoints / 8;
for (let pi = 0; pi < nPoints; pi++) {
const btVertex = new Ammo.btVector3();
const px = Ammo.HEAPF64[pptr + pi * 3 + 0];
const py = Ammo.HEAPF64[pptr + pi * 3 + 1];
const pz = Ammo.HEAPF64[pptr + pi * 3 + 2];
btVertex.setValue(px, py, pz);
hull.addPoint(btVertex, pi === nPoints - 1);
Ammo.destroy(btVertex);
}
_finishCollisionShape(hull, options, scale);
shapes.push(hull);
}
return shapes;
};
})();
export const createVHACDShapes = (function() {
const vector = new THREE.Vector3();
const center = new THREE.Vector3();
const matrix = new THREE.Matrix4();
return function(vertices, matrices, indexes, matrixWorld, options = {}) {
options.type = TYPE.VHACD;
_setOptions(options);
if (options.fit === FIT.MANUAL) {
console.warn("cannot use fit: manual with type: vhacd");
return [];
}
if (!Ammo.hasOwnProperty("VHACD")) {
console.warn(
"VHACD unavailable in included build of Ammo.js. Visit https://github.com/mozillareality/ammo.js for the latest version."
);
return [];
}
const bounds = _computeBounds(vertices, matrices);
const scale = _computeScale(matrixWorld, options);
let vertexCount = 0;
let triCount = 0;
center.addVectors(bounds.max, bounds.min).multiplyScalar(0.5);
for (let i = 0; i < vertices.length; i++) {
vertexCount += vertices[i].length / 3;
if (indexes && indexes[i]) {
triCount += indexes[i].length / 3;
} else {
triCount += vertices[i].length / 9;
}
}
const vhacd = new Ammo.VHACD();
const params = new Ammo.Parameters();
//https://kmamou.blogspot.com/2014/12/v-hacd-20-parameters-description.html
if (options.hasOwnProperty("resolution")) params.set_m_resolution(options.resolution);
if (options.hasOwnProperty("depth")) params.set_m_depth(options.depth);
if (options.hasOwnProperty("concavity")) params.set_m_concavity(options.concavity);
if (options.hasOwnProperty("planeDownsampling")) params.set_m_planeDownsampling(options.planeDownsampling);
if (options.hasOwnProperty("convexhullDownsampling"))
params.set_m_convexhullDownsampling(options.convexhullDownsampling);
if (options.hasOwnProperty("alpha")) params.set_m_alpha(options.alpha);
if (options.hasOwnProperty("beta")) params.set_m_beta(options.beta);
if (options.hasOwnProperty("gamma")) params.set_m_gamma(options.gamma);
if (options.hasOwnProperty("pca")) params.set_m_pca(options.pca);
if (options.hasOwnProperty("mode")) params.set_m_mode(options.mode);
if (options.hasOwnProperty("maxNumVerticesPerCH")) params.set_m_maxNumVerticesPerCH(options.maxNumVerticesPerCH);
if (options.hasOwnProperty("minVolumePerCH")) params.set_m_minVolumePerCH(options.minVolumePerCH);
if (options.hasOwnProperty("convexhullApproximation"))
params.set_m_convexhullApproximation(options.convexhullApproximation);
if (options.hasOwnProperty("oclAcceleration")) params.set_m_oclAcceleration(options.oclAcceleration);
const points = Ammo._malloc(vertexCount * 3 * 8 + 3);
const triangles = Ammo._malloc(triCount * 3 * 4);
let pptr = points / 8,
tptr = triangles / 4;
for (let i = 0; i < vertices.length; i++) {
const components = vertices[i];
matrix.fromArray(matrices[i]);
for (let j = 0; j < components.length; j += 3) {
vector
.set(components[j + 0], components[j + 1], components[j + 2])
.applyMatrix4(matrix)
.sub(center);
Ammo.HEAPF64[pptr + 0] = vector.x;
Ammo.HEAPF64[pptr + 1] = vector.y;
Ammo.HEAPF64[pptr + 2] = vector.z;
pptr += 3;
}
if (indexes[i]) {
const indices = indexes[i];
for (let j = 0; j < indices.length; j++) {
Ammo.HEAP32[tptr] = indices[j];
tptr++;
}
} else {
for (let j = 0; j < components.length / 3; j++) {
Ammo.HEAP32[tptr] = j;
tptr++;
}
}
}
vhacd.Compute(points, 3, vertexCount, triangles, 3, triCount, params);
Ammo._free(points);
Ammo._free(triangles);
const nHulls = vhacd.GetNConvexHulls();
const shapes = [];
const ch = new Ammo.ConvexHull();
for (let i = 0; i < nHulls; i++) {
vhacd.GetConvexHull(i, ch);
const nPoints = ch.get_m_nPoints();
const hullPoints = ch.get_m_points();
const hull = new Ammo.btConvexHullShape();
hull.setMargin(options.margin);
for (let pi = 0; pi < nPoints; pi++) {
const btVertex = new Ammo.btVector3();
const px = ch.get_m_points(pi * 3 + 0);
const py = ch.get_m_points(pi * 3 + 1);
const pz = ch.get_m_points(pi * 3 + 2);
btVertex.setValue(px, py, pz);
hull.addPoint(btVertex, pi === nPoints - 1);
Ammo.destroy(btVertex);
}
_finishCollisionShape(hull, options, scale);
shapes.push(hull);
}
Ammo.destroy(ch);
Ammo.destroy(vhacd);
return shapes;
};
})();
export const createTriMeshShape = (function() {
const va = new THREE.Vector3();
const vb = new THREE.Vector3();
const vc = new THREE.Vector3();
const matrix = new THREE.Matrix4();
return function(vertices, matrices, indexes, matrixWorld, options = {}) {
options.type = TYPE.MESH;
_setOptions(options);
if (options.fit === FIT.MANUAL) {
console.warn("cannot use fit: manual with type: mesh");
return null;
}
const scale = _computeScale(matrixWorld, options);
const bta = new Ammo.btVector3();
const btb = new Ammo.btVector3();
const btc = new Ammo.btVector3();
const triMesh = new Ammo.btTriangleMesh(true, false);
for (let i = 0; i < vertices.length; i++) {
const components = vertices[i];
const index = indexes[i] ? indexes[i] : null;
matrix.fromArray(matrices[i]);
if (index) {
for (let j = 0; j < index.length; j += 3) {
const ai = index[j] * 3;
const bi = index[j + 1] * 3;
const ci = index[j + 2] * 3;
va.set(components[ai], components[ai + 1], components[ai + 2]).applyMatrix4(matrix);
vb.set(components[bi], components[bi + 1], components[bi + 2]).applyMatrix4(matrix);
vc.set(components[ci], components[ci + 1], components[ci + 2]).applyMatrix4(matrix);
bta.setValue(va.x, va.y, va.z);
btb.setValue(vb.x, vb.y, vb.z);
btc.setValue(vc.x, vc.y, vc.z);
triMesh.addTriangle(bta, btb, btc, false);
}
} else {
for (let j = 0; j < components.length; j += 9) {
va.set(components[j + 0], components[j + 1], components[j + 2]).applyMatrix4(matrix);
vb.set(components[j + 3], components[j + 4], components[j + 5]).applyMatrix4(matrix);
vc.set(components[j + 6], components[j + 7], components[j + 8]).applyMatrix4(matrix);
bta.setValue(va.x, va.y, va.z);
btb.setValue(vb.x, vb.y, vb.z);
btc.setValue(vc.x, vc.y, vc.z);
triMesh.addTriangle(bta, btb, btc, false);
}
}
}
const localScale = new Ammo.btVector3(scale.x, scale.y, scale.z);
triMesh.setScaling(localScale);
Ammo.destroy(localScale);
const collisionShape = new Ammo.btBvhTriangleMeshShape(triMesh, true, true);
collisionShape.resources = [triMesh];
Ammo.destroy(bta);
Ammo.destroy(btb);
Ammo.destroy(btc);
_finishCollisionShape(collisionShape, options);
return collisionShape;
};
})();
export const createHeightfieldTerrainShape = function(options = {}) {
_setOptions(options);
if (options.fit === FIT.ALL) {
console.warn("cannot use fit: all with type: heightfield");
return null;
}
const heightfieldDistance = options.heightfieldDistance || 1;
const heightfieldData = options.heightfieldData || [];
const heightScale = options.heightScale || 0;
const upAxis = options.hasOwnProperty("upAxis") ? options.upAxis : 1; // x = 0; y = 1; z = 2
const hdt = (() => {
switch (options.heightDataType) {
case "short":
return Ammo.PHY_SHORT;
case "float":
return Ammo.PHY_FLOAT;
default:
return Ammo.PHY_FLOAT;
}
})();
const flipQuadEdges = options.hasOwnProperty("flipQuadEdges") ? options.flipQuadEdges : true;
const heightStickLength = heightfieldData.length;
const heightStickWidth = heightStickLength > 0 ? heightfieldData[0].length : 0;
const data = Ammo._malloc(heightStickLength * heightStickWidth * 4);
const ptr = data / 4;
let minHeight = Number.POSITIVE_INFINITY;
let maxHeight = Number.NEGATIVE_INFINITY;
let index = 0;
for (let l = 0; l < heightStickLength; l++) {
for (let w = 0; w < heightStickWidth; w++) {
const height = heightfieldData[l][w];
Ammo.HEAPF32[ptr + index] = height;
index++;
minHeight = Math.min(minHeight, height);
maxHeight = Math.max(maxHeight, height);
}
}
const collisionShape = new Ammo.btHeightfieldTerrainShape(
heightStickWidth,
heightStickLength,
data,
heightScale,
minHeight,
maxHeight,
upAxis,
hdt,
flipQuadEdges
);
const scale = new Ammo.btVector3(heightfieldDistance, 1, heightfieldDistance);
collisionShape.setLocalScaling(scale);
Ammo.destroy(scale);
collisionShape.heightfieldData = data;
_finishCollisionShape(collisionShape, options);
return collisionShape;
};
function _setOptions(options) {
options.fit = options.hasOwnProperty("fit") ? options.fit : FIT.ALL;
options.type = options.type || TYPE.HULL;
options.minHalfExtent = options.hasOwnProperty("minHalfExtent") ? options.minHalfExtent : 0;
options.maxHalfExtent = options.hasOwnProperty("maxHalfExtent") ? options.maxHalfExtent : Number.POSITIVE_INFINITY;
options.cylinderAxis = options.cylinderAxis || "y";
options.margin = options.hasOwnProperty("margin") ? options.margin : 0.01;
options.includeInvisible = options.hasOwnProperty("includeInvisible") ? options.includeInvisible : false;
if (!options.offset) {
options.offset = new THREE.Vector3();
}
if (!options.orientation) {
options.orientation = new THREE.Quaternion();
}
}
const _finishCollisionShape = function(collisionShape, options, scale) {
collisionShape.type = options.type;
collisionShape.setMargin(options.margin);
collisionShape.destroy = () => {
for (let res of collisionShape.resources || []) {
Ammo.destroy(res);
}
if (collisionShape.heightfieldData) {
Ammo._free(collisionShape.heightfieldData);
}
Ammo.destroy(collisionShape);
};
const localTransform = new Ammo.btTransform();
const rotation = new Ammo.btQuaternion();
localTransform.setIdentity();
localTransform.getOrigin().setValue(options.offset.x, options.offset.y, options.offset.z);
rotation.setValue(options.orientation.x, options.orientation.y, options.orientation.z, options.orientation.w);
localTransform.setRotation(rotation);
Ammo.destroy(rotation);
if (scale) {
const localScale = new Ammo.btVector3(scale.x, scale.y, scale.z);
collisionShape.setLocalScaling(localScale);
Ammo.destroy(localScale);
}
collisionShape.localTransform = localTransform;
};
export const iterateGeometries = (function() {
const inverse = new THREE.Matrix4();
return function(root, options, cb) {
inverse.copy(root.matrixWorld).invert();
root.traverse(mesh => {
const transform = new THREE.Matrix4();
if (
mesh.isMesh &&
mesh.name !== "Sky" &&
(options.includeInvisible || (mesh.el && mesh.el.object3D.visible) || mesh.visible)
) {
if (mesh === root) {
transform.identity();
} else {
mesh.updateWorldMatrix(true);
transform.multiplyMatrices(inverse, mesh.matrixWorld);
}
// todo: might want to return null xform if this is the root so that callers can avoid multiplying
// things by the identity matrix
let vertices;
if (mesh.geometry.isBufferGeometry) {
const verticesAttribute = mesh.geometry.attributes.position;
if (verticesAttribute.isInterleavedBufferAttribute) {
//
// An interleaved buffer attribute shares the underlying
// array with other attributes. We translate it to a
// regular array here to not carry this logic around in
// the shape api.
//
vertices = [];
for (let i = 0; i < verticesAttribute.count; i += 3) {
vertices.push(verticesAttribute.getX(i));
vertices.push(verticesAttribute.getY(i));
vertices.push(verticesAttribute.getZ(i));
}
} else {
vertices = verticesAttribute.array;
}
} else {
vertices = mesh.geometry.vertices;
}
cb(
vertices,
transform.elements,
mesh.geometry.index ? mesh.geometry.index.array : null
);
}
});
};
})();
const _computeScale = (function() {
const matrix = new THREE.Matrix4();
return function(matrixWorld, options = {}) {
const scale = new THREE.Vector3(1, 1, 1);
if (options.fit === FIT.ALL) {
matrix.fromArray(matrixWorld);
scale.setFromMatrixScale(matrix);
}
return scale;
};
})();
const _computeRadius = (function() {
const center = new THREE.Vector3();
return function(vertices, matrices, bounds) {
let maxRadiusSq = 0;
let { x: cx, y: cy, z: cz } = bounds.getCenter(center);
_iterateVertices(vertices, matrices, v => {
const dx = cx - v.x;
const dy = cy - v.y;
const dz = cz - v.z;
maxRadiusSq = Math.max(maxRadiusSq, dx * dx + dy * dy + dz * dz);
});
return Math.sqrt(maxRadiusSq);
};
})();
const _computeHalfExtents = function(bounds, minHalfExtent, maxHalfExtent) {
const halfExtents = new THREE.Vector3();
return halfExtents
.subVectors(bounds.max, bounds.min)
.multiplyScalar(0.5)
.clampScalar(minHalfExtent, maxHalfExtent);
};
const _computeLocalOffset = function(matrix, bounds, target) {
target
.addVectors(bounds.max, bounds.min)
.multiplyScalar(0.5)
.applyMatrix4(matrix);
return target;
};
// returns the bounding box for the geometries underneath `root`.
const _computeBounds = function(vertices, matrices) {
const bounds = new THREE.Box3();
let minX = +Infinity;
let minY = +Infinity;
let minZ = +Infinity;
let maxX = -Infinity;
let maxY = -Infinity;
let maxZ = -Infinity;
bounds.min.set(0, 0, 0);
bounds.max.set(0, 0, 0);
_iterateVertices(vertices, matrices, v => {
if (v.x < minX) minX = v.x;
if (v.y < minY) minY = v.y;
if (v.z < minZ) minZ = v.z;
if (v.x > maxX) maxX = v.x;
if (v.y > maxY) maxY = v.y;
if (v.z > maxZ) maxZ = v.z;
});
bounds.min.set(minX, minY, minZ);
bounds.max.set(maxX, maxY, maxZ);
return bounds;
};
const _iterateVertices = (function() {
const vertex = new THREE.Vector3();
const matrix = new THREE.Matrix4();
return function(vertices, matrices, cb) {
for (let i = 0; i < vertices.length; i++) {
matrix.fromArray(matrices[i]);
for (let j = 0; j < vertices[i].length; j += 3) {
vertex.set(vertices[i][j], vertices[i][j + 1], vertices[i][j + 2]).applyMatrix4(matrix);
cb(vertex);
}
}
};
})();