-
Notifications
You must be signed in to change notification settings - Fork 21
/
index.js
134 lines (119 loc) · 3.05 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
import "three";
import constants from "./constants";
import ammoWorker from "./src/ammo.worker";
export const CONSTANTS = constants;
export const AmmoWorker = ammoWorker;
import { iterateGeometries } from "three-to-ammo";
const MESSAGE_TYPES = CONSTANTS.MESSAGE_TYPES;
export const WorkerHelpers = function(ammoWorker) {
const transform = new THREE.Matrix4();
const inverse = new THREE.Matrix4();
const addBody = function(uuid, mesh, options = {}) {
inverse.copy(mesh.parent.matrixWorld).invert();
transform.multiplyMatrices(inverse, mesh.matrixWorld);
ammoWorker.postMessage({
type: MESSAGE_TYPES.ADD_BODY,
uuid,
matrix: transform.elements,
options
});
};
const removeBody = function(uuid) {
ammoWorker.postMessage({
type: MESSAGE_TYPES.REMOVE_BODY,
uuid
});
};
const addShapes = function(bodyUuid, shapesUuid, mesh, options = {}) {
if (mesh) {
inverse.copy(mesh.parent.matrix).invert();
transform.multiplyMatrices(inverse, mesh.parent.matrix);
const vertices = [];
const matrices = [];
const indexes = [];
mesh.updateMatrixWorld(true);
iterateGeometries(mesh, options, (vertexArray, matrix, index) => {
vertices.push(vertexArray);
matrices.push(matrix);
indexes.push(index);
});
ammoWorker.postMessage({
type: MESSAGE_TYPES.ADD_SHAPES,
bodyUuid,
shapesUuid,
vertices,
matrices,
indexes,
matrixWorld: mesh.matrixWorld.elements,
options
});
} else {
ammoWorker.postMessage({
type: MESSAGE_TYPES.ADD_SHAPES,
bodyUuid,
shapesUuid,
options
});
}
};
const removeShapes = function(bodyUuid, shapesUuid) {
ammoWorker.postMessage({
type: MESSAGE_TYPES.REMOVE_SHAPES,
bodyUuid,
shapesUuid
});
};
const addConstraint = function(constraintId, bodyUuid, targetUuid, options = {}) {
ammoWorker.postMessage({
type: MESSAGE_TYPES.ADD_CONSTRAINT,
constraintId,
bodyUuid,
targetUuid,
options
});
};
const removeConstraint = function(constraintId) {
ammoWorker.postMessage({
type: MESSAGE_TYPES.REMOVE_CONSTRAINT,
constraintId
});
};
const updateBody = function(uuid, options) {
ammoWorker.postMessage({
type: MESSAGE_TYPES.UPDATE_BODY,
uuid,
options
});
};
const enableDebug = function(enable, debugSharedArrayBuffer) {
ammoWorker.postMessage({
type: MESSAGE_TYPES.ENABLE_DEBUG,
enable,
debugSharedArrayBuffer
});
};
const resetDynamicBody = function(uuid) {
ammoWorker.postMessage({
type: MESSAGE_TYPES.RESET_DYNAMIC_BODY,
uuid
});
};
const activateBody = function(uuid) {
ammoWorker.postMessage({
type: MESSAGE_TYPES.ACTIVATE_BODY,
uuid
});
};
return {
addBody,
updateBody,
removeBody,
addShapes,
removeShapes,
addConstraint,
removeConstraint,
enableDebug,
resetDynamicBody,
activateBody
};
};