@react-three/rapier 0.5.0 → 0.6.0
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/dist/declarations/src/{components.d.ts → AnyCollider.d.ts} +1 -6
- package/dist/declarations/src/InstancedRigidBodies.d.ts +10 -0
- package/dist/declarations/src/MeshCollider.d.ts +8 -0
- package/dist/declarations/src/Physics.d.ts +10 -4
- package/dist/declarations/src/RigidBody.d.ts +14 -0
- package/dist/declarations/src/api.d.ts +10 -1
- package/dist/declarations/src/hooks.d.ts +1 -5
- package/dist/declarations/src/index.d.ts +7 -3
- package/dist/declarations/src/shared-objects.d.ts +6 -0
- package/dist/declarations/src/types.d.ts +4 -4
- package/dist/declarations/src/utils.d.ts +29 -10
- package/dist/react-three-rapier.cjs.dev.js +541 -354
- package/dist/react-three-rapier.cjs.prod.js +541 -354
- package/dist/react-three-rapier.esm.js +517 -331
- package/package.json +1 -1
- package/readme.md +84 -12
- package/CHANGELOG.md +0 -87
@@ -1,9 +1,15 @@
|
|
1
|
-
import
|
1
|
+
import { ColliderDesc, ActiveEvents, RigidBodyDesc, CoefficientCombineRule, EventQueue, ShapeType } from '@dimforge/rapier3d-compat';
|
2
|
+
export { CoefficientCombineRule, Collider as RapierCollider, RigidBody as RapierRigidBody } from '@dimforge/rapier3d-compat';
|
3
|
+
import React$1, { useRef, useState, useEffect, useMemo, createContext, useContext, forwardRef, useImperativeHandle, memo } from 'react';
|
2
4
|
import { useAsset } from 'use-asset';
|
3
5
|
import { useFrame } from '@react-three/fiber';
|
4
|
-
import {
|
5
|
-
|
6
|
-
|
6
|
+
import { Quaternion, Euler, Vector3, Object3D, Matrix4, InstancedMesh, CylinderBufferGeometry, BufferGeometry, BufferAttribute, SphereBufferGeometry, BoxBufferGeometry, DynamicDrawUsage } from 'three';
|
7
|
+
|
8
|
+
const _quaternion = new Quaternion();
|
9
|
+
const _euler = new Euler();
|
10
|
+
const _vector3 = new Vector3();
|
11
|
+
new Object3D();
|
12
|
+
const _matrix4 = new Matrix4();
|
7
13
|
|
8
14
|
const vectorArrayToObject = arr => {
|
9
15
|
const [x, y, z] = arr;
|
@@ -13,6 +19,20 @@ const vectorArrayToObject = arr => {
|
|
13
19
|
z
|
14
20
|
};
|
15
21
|
};
|
22
|
+
const vector3ToQuaternion = v => {
|
23
|
+
return _quaternion.setFromEuler(_euler.setFromVector3(v));
|
24
|
+
};
|
25
|
+
const rapierVector3ToVector3 = ({
|
26
|
+
x,
|
27
|
+
y,
|
28
|
+
z
|
29
|
+
}) => _vector3.set(x, y, z).clone();
|
30
|
+
const rapierQuaternionToQuaternion = ({
|
31
|
+
x,
|
32
|
+
y,
|
33
|
+
z,
|
34
|
+
w
|
35
|
+
}) => _quaternion.set(x, y, z, w);
|
16
36
|
const rigidBodyTypeMap = {
|
17
37
|
fixed: 1,
|
18
38
|
dynamic: 0,
|
@@ -20,6 +40,17 @@ const rigidBodyTypeMap = {
|
|
20
40
|
kinematicVelocity: 3
|
21
41
|
};
|
22
42
|
const rigidBodyTypeFromString = type => rigidBodyTypeMap[type];
|
43
|
+
const decomposeMatrix4 = m => {
|
44
|
+
const position = new Vector3();
|
45
|
+
const rotation = new Quaternion();
|
46
|
+
const scale = new Vector3();
|
47
|
+
m.decompose(position, rotation, scale);
|
48
|
+
return {
|
49
|
+
position,
|
50
|
+
rotation,
|
51
|
+
scale
|
52
|
+
};
|
53
|
+
};
|
23
54
|
const scaleColliderArgs = (shape, args, scale) => {
|
24
55
|
// Heightfield only scales the last arg
|
25
56
|
const newArgs = args.slice();
|
@@ -38,11 +69,13 @@ const scaleColliderArgs = (shape, args, scale) => {
|
|
38
69
|
const scaleArray = [scale.x, scale.y, scale.z];
|
39
70
|
return newArgs.map((arg, index) => scaleArray[index] * arg);
|
40
71
|
};
|
41
|
-
const createColliderFromOptions = (
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
72
|
+
const createColliderFromOptions = ({
|
73
|
+
options,
|
74
|
+
world,
|
75
|
+
rigidBody,
|
76
|
+
scale,
|
77
|
+
hasCollisionEvents
|
78
|
+
}) => {
|
46
79
|
var _options$shape, _options$args, _options$restitution, _options$restitutionC, _options$friction, _options$frictionComb;
|
47
80
|
|
48
81
|
const mass = (options === null || options === void 0 ? void 0 : options.mass) || 1;
|
@@ -51,15 +84,16 @@ const createColliderFromOptions = (options, world, rigidBody, scale = {
|
|
51
84
|
const [cmx, cmy, cmz] = (options === null || options === void 0 ? void 0 : options.centerOfMass) || [0, 0, 0];
|
52
85
|
const [pix, piy, piz] = (options === null || options === void 0 ? void 0 : options.principalAngularInertia) || [mass * 0.2, mass * 0.2, mass * 0.2];
|
53
86
|
const [x, y, z] = (options === null || options === void 0 ? void 0 : options.position) || [0, 0, 0];
|
54
|
-
const [rx, ry, rz] = (options === null || options === void 0 ? void 0 : options.rotation) || [0, 0, 0];
|
87
|
+
const [rx, ry, rz] = (options === null || options === void 0 ? void 0 : options.rotation) || [0, 0, 0];
|
88
|
+
const qRotation = vector3ToQuaternion(new Vector3(rx, ry, rz)); // @ts-ignore
|
55
89
|
|
56
90
|
const scaledArgs = scaleColliderArgs(options.shape, colliderArgs, scale);
|
57
91
|
let colliderDesc = ColliderDesc[colliderShape]( // @ts-ignore
|
58
92
|
...scaledArgs).setTranslation(x * scale.x, y * scale.y, z * scale.z).setRotation({
|
59
|
-
x:
|
60
|
-
y:
|
61
|
-
z:
|
62
|
-
w:
|
93
|
+
x: qRotation.x,
|
94
|
+
y: qRotation.y,
|
95
|
+
z: qRotation.z,
|
96
|
+
w: qRotation.w
|
63
97
|
}).setRestitution((_options$restitution = options === null || options === void 0 ? void 0 : options.restitution) !== null && _options$restitution !== void 0 ? _options$restitution : 0).setRestitutionCombineRule((_options$restitutionC = options === null || options === void 0 ? void 0 : options.restitutionCombineRule) !== null && _options$restitutionC !== void 0 ? _options$restitutionC : CoefficientCombineRule.Average).setFriction((_options$friction = options === null || options === void 0 ? void 0 : options.friction) !== null && _options$friction !== void 0 ? _options$friction : 0.7).setFrictionCombineRule((_options$frictionComb = options === null || options === void 0 ? void 0 : options.frictionCombineRule) !== null && _options$frictionComb !== void 0 ? _options$frictionComb : CoefficientCombineRule.Average);
|
64
98
|
|
65
99
|
if (hasCollisionEvents) {
|
@@ -67,6 +101,8 @@ const createColliderFromOptions = (options, world, rigidBody, scale = {
|
|
67
101
|
} // If any of the mass properties are specified, add mass properties
|
68
102
|
|
69
103
|
|
104
|
+
const qMassRot = vector3ToQuaternion(new Vector3(0, 0, 0));
|
105
|
+
|
70
106
|
if (options !== null && options !== void 0 && options.mass || options !== null && options !== void 0 && options.centerOfMass || options !== null && options !== void 0 && options.principalAngularInertia) {
|
71
107
|
colliderDesc.setDensity(0);
|
72
108
|
colliderDesc.setMassProperties(mass, {
|
@@ -78,23 +114,32 @@ const createColliderFromOptions = (options, world, rigidBody, scale = {
|
|
78
114
|
y: piy,
|
79
115
|
z: piz
|
80
116
|
}, {
|
81
|
-
x:
|
82
|
-
y:
|
83
|
-
z:
|
84
|
-
w:
|
117
|
+
x: qMassRot.x,
|
118
|
+
y: qMassRot.y,
|
119
|
+
z: qMassRot.z,
|
120
|
+
w: qMassRot.w
|
85
121
|
});
|
86
122
|
}
|
87
123
|
|
88
124
|
const collider = world.createCollider(colliderDesc, rigidBody);
|
89
125
|
return collider;
|
90
126
|
};
|
91
|
-
|
127
|
+
|
128
|
+
const isChildOfMeshCollider = child => {
|
129
|
+
let flag = false;
|
130
|
+
child.traverseAncestors(a => {
|
131
|
+
if (a.userData.r3RapierType === "MeshCollider") flag = true;
|
132
|
+
});
|
133
|
+
return flag;
|
134
|
+
};
|
135
|
+
|
136
|
+
const createCollidersFromChildren = (object, rigidBody, options, world, ignoreMeshColliders = true) => {
|
92
137
|
const hasCollisionEvents = !!(options.onCollisionEnter || options.onCollisionExit);
|
93
138
|
const colliders = [];
|
94
|
-
|
95
|
-
let offset = new Vector3();
|
139
|
+
new Vector3();
|
96
140
|
object.traverse(child => {
|
97
141
|
if ("isMesh" in child) {
|
142
|
+
if (ignoreMeshColliders && isChildOfMeshCollider(child)) return;
|
98
143
|
const {
|
99
144
|
geometry
|
100
145
|
} = child;
|
@@ -109,68 +154,86 @@ const createCollidersFromChildren = (object, rigidBody, options, world) => {
|
|
109
154
|
z: rz,
|
110
155
|
w: rw
|
111
156
|
} = new Quaternion().setFromEuler(child.rotation);
|
112
|
-
const scale = child.getWorldScale(new Vector3());
|
113
|
-
|
114
|
-
switch (options.colliders) {
|
115
|
-
case "cuboid":
|
116
|
-
{
|
117
|
-
geometry.computeBoundingBox();
|
118
|
-
const {
|
119
|
-
boundingBox
|
120
|
-
} = geometry;
|
121
|
-
const size = boundingBox.getSize(new Vector3());
|
122
|
-
boundingBox.getCenter(offset);
|
123
|
-
desc = ColliderDesc.cuboid(size.x / 2 * scale.x, size.y / 2 * scale.y, size.z / 2 * scale.z);
|
124
|
-
}
|
125
|
-
break;
|
126
|
-
|
127
|
-
case "ball":
|
128
|
-
{
|
129
|
-
geometry.computeBoundingSphere();
|
130
|
-
const {
|
131
|
-
boundingSphere
|
132
|
-
} = geometry;
|
133
|
-
const radius = boundingSphere.radius * scale.x;
|
134
|
-
offset.copy(boundingSphere.center);
|
135
|
-
desc = ColliderDesc.ball(radius);
|
136
|
-
}
|
137
|
-
break;
|
138
|
-
|
139
|
-
case "trimesh":
|
140
|
-
{
|
141
|
-
var _g$index;
|
157
|
+
const scale = child.getWorldScale(new Vector3()); // We translate the colliders based on the parent's world scale
|
142
158
|
|
143
|
-
|
159
|
+
const parentWorldScale = child.parent.getWorldScale(new Vector3());
|
160
|
+
const desc = colliderDescFromGeometry(geometry, options.colliders, scale, hasCollisionEvents);
|
161
|
+
const offset = new Vector3(0, 0, 0);
|
144
162
|
|
145
|
-
|
146
|
-
|
147
|
-
break;
|
163
|
+
if (options.colliders === "cuboid") {
|
164
|
+
var _geometry$boundingBox;
|
148
165
|
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
desc = ColliderDesc.convexHull(g.attributes.position.array);
|
153
|
-
}
|
154
|
-
break;
|
155
|
-
} // We translate the colliders based on the parent's world scale
|
166
|
+
geometry.computeBoundingBox();
|
167
|
+
(_geometry$boundingBox = geometry.boundingBox) === null || _geometry$boundingBox === void 0 ? void 0 : _geometry$boundingBox.getCenter(offset);
|
168
|
+
}
|
156
169
|
|
170
|
+
if (options.colliders === "ball") {
|
171
|
+
geometry.computeBoundingSphere();
|
172
|
+
offset.copy(geometry.boundingSphere.center);
|
173
|
+
}
|
157
174
|
|
158
|
-
|
175
|
+
if (Number.isFinite(options.friction)) desc.setFriction(options.friction);
|
176
|
+
if (Number.isFinite(options.restitution)) desc.setRestitution(options.restitution);
|
159
177
|
desc.setTranslation((x + offset.x) * parentWorldScale.x, (y + offset.y) * parentWorldScale.y, (z + offset.z) * parentWorldScale.z).setRotation({
|
160
178
|
x: rx,
|
161
179
|
y: ry,
|
162
180
|
z: rz,
|
163
181
|
w: rw
|
164
182
|
});
|
165
|
-
|
166
|
-
|
167
|
-
if (Number.isFinite(options.restitution)) desc.setRestitution(options.restitution);
|
168
|
-
const collider = world.createCollider(desc, rigidBody);
|
183
|
+
const actualRigidBody = world.getRigidBody(rigidBody === null || rigidBody === void 0 ? void 0 : rigidBody.handle);
|
184
|
+
const collider = world.createCollider(desc, actualRigidBody);
|
169
185
|
colliders.push(collider);
|
170
186
|
}
|
171
187
|
});
|
172
188
|
return colliders;
|
173
189
|
};
|
190
|
+
const colliderDescFromGeometry = (geometry, colliders, scale, hasCollisionEvents) => {
|
191
|
+
let desc;
|
192
|
+
|
193
|
+
switch (colliders) {
|
194
|
+
case "cuboid":
|
195
|
+
{
|
196
|
+
geometry.computeBoundingBox();
|
197
|
+
const {
|
198
|
+
boundingBox
|
199
|
+
} = geometry;
|
200
|
+
const size = boundingBox.getSize(new Vector3());
|
201
|
+
desc = ColliderDesc.cuboid(size.x / 2 * scale.x, size.y / 2 * scale.y, size.z / 2 * scale.z);
|
202
|
+
}
|
203
|
+
break;
|
204
|
+
|
205
|
+
case "ball":
|
206
|
+
{
|
207
|
+
geometry.computeBoundingSphere();
|
208
|
+
const {
|
209
|
+
boundingSphere
|
210
|
+
} = geometry;
|
211
|
+
const radius = boundingSphere.radius * scale.x;
|
212
|
+
desc = ColliderDesc.ball(radius);
|
213
|
+
}
|
214
|
+
break;
|
215
|
+
|
216
|
+
case "trimesh":
|
217
|
+
{
|
218
|
+
var _g$index;
|
219
|
+
|
220
|
+
const _g = geometry.clone().scale(scale.x, scale.y, scale.z);
|
221
|
+
|
222
|
+
desc = ColliderDesc.trimesh(_g.attributes.position.array, (_g$index = _g.index) === null || _g$index === void 0 ? void 0 : _g$index.array);
|
223
|
+
}
|
224
|
+
break;
|
225
|
+
|
226
|
+
case "hull":
|
227
|
+
const g = geometry.clone().scale(scale.x, scale.y, scale.z);
|
228
|
+
{
|
229
|
+
desc = ColliderDesc.convexHull(g.attributes.position.array);
|
230
|
+
}
|
231
|
+
break;
|
232
|
+
}
|
233
|
+
|
234
|
+
if (hasCollisionEvents) desc.setActiveEvents(ActiveEvents.COLLISION_EVENTS);
|
235
|
+
return desc;
|
236
|
+
};
|
174
237
|
const scaleVertices = (vertices, scale) => {
|
175
238
|
const scaledVerts = Array.from(vertices);
|
176
239
|
|
@@ -182,53 +245,27 @@ const scaleVertices = (vertices, scale) => {
|
|
182
245
|
|
183
246
|
return scaledVerts;
|
184
247
|
};
|
185
|
-
const
|
186
|
-
|
187
|
-
|
188
|
-
|
248
|
+
const rigidBodyDescFromOptions = options => {
|
249
|
+
var _options$linearVeloci, _options$angularVeloc, _options$gravityScale, _options$canSleep, _options$ccd, _options$enabledRotat, _options$enabledTrans;
|
250
|
+
|
251
|
+
const type = rigidBodyTypeFromString((options === null || options === void 0 ? void 0 : options.type) || "dynamic");
|
252
|
+
const [lvx, lvy, lvz] = (_options$linearVeloci = options === null || options === void 0 ? void 0 : options.linearVelocity) !== null && _options$linearVeloci !== void 0 ? _options$linearVeloci : [0, 0, 0];
|
253
|
+
const [avx, avy, avz] = (_options$angularVeloc = options === null || options === void 0 ? void 0 : options.angularVelocity) !== null && _options$angularVeloc !== void 0 ? _options$angularVeloc : [0, 0, 0];
|
254
|
+
const gravityScale = (_options$gravityScale = options === null || options === void 0 ? void 0 : options.gravityScale) !== null && _options$gravityScale !== void 0 ? _options$gravityScale : 1;
|
255
|
+
const canSleep = (_options$canSleep = options === null || options === void 0 ? void 0 : options.canSleep) !== null && _options$canSleep !== void 0 ? _options$canSleep : true;
|
256
|
+
const ccdEnabled = (_options$ccd = options === null || options === void 0 ? void 0 : options.ccd) !== null && _options$ccd !== void 0 ? _options$ccd : false;
|
257
|
+
const [erx, ery, erz] = (_options$enabledRotat = options === null || options === void 0 ? void 0 : options.enabledRotations) !== null && _options$enabledRotat !== void 0 ? _options$enabledRotat : [true, true, true];
|
258
|
+
const [etx, ety, etz] = (_options$enabledTrans = options === null || options === void 0 ? void 0 : options.enabledTranslations) !== null && _options$enabledTrans !== void 0 ? _options$enabledTrans : [true, true, true];
|
259
|
+
const desc = new RigidBodyDesc(type).setLinvel(lvx, lvy, lvz).setAngvel({
|
260
|
+
x: avx,
|
261
|
+
y: avy,
|
262
|
+
z: avz
|
263
|
+
}).setGravityScale(gravityScale).setCanSleep(canSleep).setCcdEnabled(ccdEnabled).enabledRotations(erx, ery, erz).enabledTranslations(etx, ety, etz);
|
264
|
+
if (options.lockRotations) desc.lockRotations();
|
265
|
+
if (options.lockTranslations) desc.lockTranslations();
|
266
|
+
return desc;
|
189
267
|
};
|
190
268
|
|
191
|
-
function _defineProperty(obj, key, value) {
|
192
|
-
if (key in obj) {
|
193
|
-
Object.defineProperty(obj, key, {
|
194
|
-
value: value,
|
195
|
-
enumerable: true,
|
196
|
-
configurable: true,
|
197
|
-
writable: true
|
198
|
-
});
|
199
|
-
} else {
|
200
|
-
obj[key] = value;
|
201
|
-
}
|
202
|
-
|
203
|
-
return obj;
|
204
|
-
}
|
205
|
-
|
206
|
-
function ownKeys(object, enumerableOnly) {
|
207
|
-
var keys = Object.keys(object);
|
208
|
-
|
209
|
-
if (Object.getOwnPropertySymbols) {
|
210
|
-
var symbols = Object.getOwnPropertySymbols(object);
|
211
|
-
enumerableOnly && (symbols = symbols.filter(function (sym) {
|
212
|
-
return Object.getOwnPropertyDescriptor(object, sym).enumerable;
|
213
|
-
})), keys.push.apply(keys, symbols);
|
214
|
-
}
|
215
|
-
|
216
|
-
return keys;
|
217
|
-
}
|
218
|
-
|
219
|
-
function _objectSpread2(target) {
|
220
|
-
for (var i = 1; i < arguments.length; i++) {
|
221
|
-
var source = null != arguments[i] ? arguments[i] : {};
|
222
|
-
i % 2 ? ownKeys(Object(source), !0).forEach(function (key) {
|
223
|
-
_defineProperty(target, key, source[key]);
|
224
|
-
}) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) {
|
225
|
-
Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
|
226
|
-
});
|
227
|
-
}
|
228
|
-
|
229
|
-
return target;
|
230
|
-
}
|
231
|
-
|
232
269
|
const createRigidBodyApi = ref => {
|
233
270
|
return {
|
234
271
|
raw: () => ref.current(),
|
@@ -273,9 +310,19 @@ const createRigidBodyApi = ref => {
|
|
273
310
|
return new Quaternion(x, y, z, w);
|
274
311
|
},
|
275
312
|
|
276
|
-
setRotation:
|
277
|
-
|
278
|
-
|
313
|
+
setRotation: ({
|
314
|
+
x,
|
315
|
+
y,
|
316
|
+
z
|
317
|
+
}) => {
|
318
|
+
const q = vector3ToQuaternion(new Vector3(x, y, z));
|
319
|
+
ref.current().setRotation({
|
320
|
+
x: q.x,
|
321
|
+
y: q.y,
|
322
|
+
z: q.z,
|
323
|
+
w: q.w
|
324
|
+
}, true);
|
325
|
+
},
|
279
326
|
|
280
327
|
linvel() {
|
281
328
|
const {
|
@@ -319,18 +366,19 @@ const createRigidBodyApi = ref => {
|
|
319
366
|
setEnabledRotations: (x, y, z) => ref.current().setEnabledRotations(x, y, z, true),
|
320
367
|
setEnabledTranslations: (x, y, z) => ref.current().setEnabledTranslations(x, y, z, true)
|
321
368
|
};
|
322
|
-
};
|
369
|
+
};
|
370
|
+
const createInstancedRigidBodiesApi = bodiesGetter => ({
|
371
|
+
at: index => bodiesGetter.current()[index].api,
|
323
372
|
|
324
|
-
|
325
|
-
|
326
|
-
|
373
|
+
forEach(callback) {
|
374
|
+
return bodiesGetter.current().map(b => b.api).forEach(callback);
|
375
|
+
},
|
327
376
|
|
328
|
-
|
329
|
-
|
330
|
-
|
377
|
+
get count() {
|
378
|
+
return bodiesGetter.current().length;
|
379
|
+
}
|
331
380
|
|
332
|
-
|
333
|
-
};
|
381
|
+
}); // TODO: Flesh this out
|
334
382
|
const createWorldApi = ref => {
|
335
383
|
return {
|
336
384
|
raw: () => ref.current(),
|
@@ -377,10 +425,10 @@ const importRapier = async () => {
|
|
377
425
|
};
|
378
426
|
|
379
427
|
const Physics = ({
|
380
|
-
colliders: _colliders =
|
428
|
+
colliders: _colliders = "cuboid",
|
381
429
|
gravity: _gravity = [0, -9.81, 0],
|
382
430
|
children,
|
383
|
-
timeStep: _timeStep =
|
431
|
+
timeStep: _timeStep = "vary"
|
384
432
|
}) => {
|
385
433
|
const rapier = useAsset(importRapier);
|
386
434
|
const worldRef = useRef();
|
@@ -393,7 +441,7 @@ const Physics = ({
|
|
393
441
|
return worldRef.current;
|
394
442
|
});
|
395
443
|
const [colliderMeshes] = useState(() => new Map());
|
396
|
-
const [
|
444
|
+
const [rigidBodyStates] = useState(() => new Map());
|
397
445
|
const [rigidBodyEvents] = useState(() => new Map());
|
398
446
|
const [eventQueue] = useState(() => new EventQueue(false)); // Init world
|
399
447
|
|
@@ -423,7 +471,7 @@ const Physics = ({
|
|
423
471
|
const now = performance.now();
|
424
472
|
const delta = Math.min(100, now - time.current);
|
425
473
|
|
426
|
-
if (_timeStep ===
|
474
|
+
if (_timeStep === "vary") {
|
427
475
|
world.timestep = delta / 1000;
|
428
476
|
} else {
|
429
477
|
world.timestep = _timeStep;
|
@@ -431,52 +479,35 @@ const Physics = ({
|
|
431
479
|
|
432
480
|
world.step(eventQueue); // Update meshes
|
433
481
|
|
434
|
-
|
482
|
+
rigidBodyStates.forEach((state, handle) => {
|
435
483
|
const rigidBody = world.getRigidBody(handle);
|
436
484
|
const events = rigidBodyEvents.get(handle);
|
437
485
|
|
438
486
|
if (events !== null && events !== void 0 && events.onSleep || events !== null && events !== void 0 && events.onWake) {
|
439
|
-
if (rigidBody.isSleeping() && !
|
487
|
+
if (rigidBody.isSleeping() && !state.isSleeping) {
|
440
488
|
var _events$onSleep;
|
441
489
|
|
442
490
|
events === null || events === void 0 ? void 0 : (_events$onSleep = events.onSleep) === null || _events$onSleep === void 0 ? void 0 : _events$onSleep.call(events);
|
443
491
|
}
|
444
492
|
|
445
|
-
if (!rigidBody.isSleeping() &&
|
493
|
+
if (!rigidBody.isSleeping() && state.isSleeping) {
|
446
494
|
var _events$onWake;
|
447
495
|
|
448
496
|
events === null || events === void 0 ? void 0 : (_events$onWake = events.onWake) === null || _events$onWake === void 0 ? void 0 : _events$onWake.call(events);
|
449
497
|
}
|
450
498
|
|
451
|
-
|
499
|
+
state.isSleeping = rigidBody.isSleeping();
|
452
500
|
}
|
453
501
|
|
454
|
-
if (!rigidBody || rigidBody.isSleeping() || rigidBody.isFixed() || !
|
502
|
+
if (!rigidBody || rigidBody.isSleeping() || rigidBody.isFixed() || !state.setMatrix) {
|
455
503
|
return;
|
456
504
|
}
|
457
505
|
|
458
|
-
|
459
|
-
|
460
|
-
|
461
|
-
|
462
|
-
}
|
463
|
-
const {
|
464
|
-
x: rx,
|
465
|
-
y: ry,
|
466
|
-
z: rz,
|
467
|
-
w: rw
|
468
|
-
} = rigidBody.rotation();
|
469
|
-
const scale = mesh.getWorldScale(new Vector3()); // haha matrixes I have no idea what I'm doing :)
|
470
|
-
|
471
|
-
const o = new Object3D();
|
472
|
-
o.position.set(x, y, z);
|
473
|
-
o.rotation.setFromQuaternion(new Quaternion(rx, ry, rz, rw));
|
474
|
-
o.scale.set(scale.x, scale.y, scale.z);
|
475
|
-
o.updateMatrix();
|
476
|
-
o.applyMatrix4(mesh.parent.matrixWorld.clone().invert());
|
477
|
-
o.updateMatrix();
|
478
|
-
mesh.position.setFromMatrixPosition(o.matrix);
|
479
|
-
mesh.rotation.setFromRotationMatrix(o.matrix);
|
506
|
+
state.setMatrix(_matrix4.compose(rapierVector3ToVector3(rigidBody.translation()), rapierQuaternionToQuaternion(rigidBody.rotation()), state.worldScale).premultiply(state.invertedMatrixWorld));
|
507
|
+
|
508
|
+
if (state.mesh instanceof InstancedMesh) {
|
509
|
+
state.mesh.instanceMatrix.needsUpdate = true;
|
510
|
+
}
|
480
511
|
}); // Collision events
|
481
512
|
|
482
513
|
eventQueue.drainCollisionEvents((handle1, handle2, started) => {
|
@@ -533,22 +564,96 @@ const Physics = ({
|
|
533
564
|
gravity: _gravity
|
534
565
|
},
|
535
566
|
colliderMeshes,
|
536
|
-
|
567
|
+
rigidBodyStates,
|
537
568
|
rigidBodyEvents
|
538
569
|
}), []);
|
539
|
-
return /*#__PURE__*/React.createElement(RapierContext.Provider, {
|
570
|
+
return /*#__PURE__*/React$1.createElement(RapierContext.Provider, {
|
540
571
|
value: context
|
541
572
|
}, children);
|
542
573
|
};
|
543
574
|
|
575
|
+
function _objectWithoutPropertiesLoose(source, excluded) {
|
576
|
+
if (source == null) return {};
|
577
|
+
var target = {};
|
578
|
+
var sourceKeys = Object.keys(source);
|
579
|
+
var key, i;
|
580
|
+
|
581
|
+
for (i = 0; i < sourceKeys.length; i++) {
|
582
|
+
key = sourceKeys[i];
|
583
|
+
if (excluded.indexOf(key) >= 0) continue;
|
584
|
+
target[key] = source[key];
|
585
|
+
}
|
586
|
+
|
587
|
+
return target;
|
588
|
+
}
|
589
|
+
|
590
|
+
function _objectWithoutProperties(source, excluded) {
|
591
|
+
if (source == null) return {};
|
592
|
+
var target = _objectWithoutPropertiesLoose(source, excluded);
|
593
|
+
var key, i;
|
594
|
+
|
595
|
+
if (Object.getOwnPropertySymbols) {
|
596
|
+
var sourceSymbolKeys = Object.getOwnPropertySymbols(source);
|
597
|
+
|
598
|
+
for (i = 0; i < sourceSymbolKeys.length; i++) {
|
599
|
+
key = sourceSymbolKeys[i];
|
600
|
+
if (excluded.indexOf(key) >= 0) continue;
|
601
|
+
if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
|
602
|
+
target[key] = source[key];
|
603
|
+
}
|
604
|
+
}
|
605
|
+
|
606
|
+
return target;
|
607
|
+
}
|
608
|
+
|
609
|
+
function _defineProperty(obj, key, value) {
|
610
|
+
if (key in obj) {
|
611
|
+
Object.defineProperty(obj, key, {
|
612
|
+
value: value,
|
613
|
+
enumerable: true,
|
614
|
+
configurable: true,
|
615
|
+
writable: true
|
616
|
+
});
|
617
|
+
} else {
|
618
|
+
obj[key] = value;
|
619
|
+
}
|
620
|
+
|
621
|
+
return obj;
|
622
|
+
}
|
623
|
+
|
624
|
+
function ownKeys(object, enumerableOnly) {
|
625
|
+
var keys = Object.keys(object);
|
626
|
+
|
627
|
+
if (Object.getOwnPropertySymbols) {
|
628
|
+
var symbols = Object.getOwnPropertySymbols(object);
|
629
|
+
enumerableOnly && (symbols = symbols.filter(function (sym) {
|
630
|
+
return Object.getOwnPropertyDescriptor(object, sym).enumerable;
|
631
|
+
})), keys.push.apply(keys, symbols);
|
632
|
+
}
|
633
|
+
|
634
|
+
return keys;
|
635
|
+
}
|
636
|
+
|
637
|
+
function _objectSpread2(target) {
|
638
|
+
for (var i = 1; i < arguments.length; i++) {
|
639
|
+
var source = null != arguments[i] ? arguments[i] : {};
|
640
|
+
i % 2 ? ownKeys(Object(source), !0).forEach(function (key) {
|
641
|
+
_defineProperty(target, key, source[key]);
|
642
|
+
}) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) {
|
643
|
+
Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
|
644
|
+
});
|
645
|
+
}
|
646
|
+
|
647
|
+
return target;
|
648
|
+
}
|
649
|
+
|
544
650
|
const useRapier = () => {
|
545
651
|
return useContext(RapierContext);
|
546
652
|
};
|
547
653
|
const useRigidBody = (options = {}) => {
|
548
654
|
const {
|
549
|
-
rapier,
|
550
655
|
world,
|
551
|
-
|
656
|
+
rigidBodyStates,
|
552
657
|
physicsOptions,
|
553
658
|
rigidBodyEvents
|
554
659
|
} = useRapier();
|
@@ -557,23 +662,7 @@ const useRigidBody = (options = {}) => {
|
|
557
662
|
const rigidBodyRef = useRef();
|
558
663
|
const getRigidBodyRef = useRef(() => {
|
559
664
|
if (!rigidBodyRef.current) {
|
560
|
-
|
561
|
-
|
562
|
-
const type = rigidBodyTypeFromString((options === null || options === void 0 ? void 0 : options.type) || "dynamic");
|
563
|
-
const [lvx, lvy, lvz] = (_options$linearVeloci = options === null || options === void 0 ? void 0 : options.linearVelocity) !== null && _options$linearVeloci !== void 0 ? _options$linearVeloci : [0, 0, 0];
|
564
|
-
const [avx, avy, avz] = (_options$angularVeloc = options === null || options === void 0 ? void 0 : options.angularVelocity) !== null && _options$angularVeloc !== void 0 ? _options$angularVeloc : [0, 0, 0];
|
565
|
-
const gravityScale = (_options$gravityScale = options === null || options === void 0 ? void 0 : options.gravityScale) !== null && _options$gravityScale !== void 0 ? _options$gravityScale : 1;
|
566
|
-
const canSleep = (_options$canSleep = options === null || options === void 0 ? void 0 : options.canSleep) !== null && _options$canSleep !== void 0 ? _options$canSleep : true;
|
567
|
-
const ccdEnabled = (_options$ccd = options === null || options === void 0 ? void 0 : options.ccd) !== null && _options$ccd !== void 0 ? _options$ccd : false;
|
568
|
-
const [erx, ery, erz] = (_options$enabledRotat = options === null || options === void 0 ? void 0 : options.enabledRotations) !== null && _options$enabledRotat !== void 0 ? _options$enabledRotat : [true, true, true];
|
569
|
-
const [etx, ety, etz] = (_options$enabledTrans = options === null || options === void 0 ? void 0 : options.enabledTranslations) !== null && _options$enabledTrans !== void 0 ? _options$enabledTrans : [true, true, true];
|
570
|
-
const desc = new rapier.RigidBodyDesc(type).setLinvel(lvx, lvy, lvz).setAngvel({
|
571
|
-
x: avx,
|
572
|
-
y: avy,
|
573
|
-
z: avz
|
574
|
-
}).setGravityScale(gravityScale).setCanSleep(canSleep).setCcdEnabled(ccdEnabled).enabledRotations(erx, ery, erz).enabledTranslations(etx, ety, etz);
|
575
|
-
if (options.lockRotations) desc.lockRotations();
|
576
|
-
if (options.lockTranslations) desc.lockTranslations();
|
665
|
+
const desc = rigidBodyDescFromOptions(options);
|
577
666
|
const rigidBody = world.createRigidBody(desc);
|
578
667
|
rigidBodyRef.current = world.getRigidBody(rigidBody.handle);
|
579
668
|
}
|
@@ -610,8 +699,7 @@ const useRigidBody = (options = {}) => {
|
|
610
699
|
y: worldPosition.y + y * scale.y,
|
611
700
|
z: worldPosition.z + z * scale.z
|
612
701
|
}, false);
|
613
|
-
const
|
614
|
-
const rotation = new Quaternion().setFromEuler(eulerAngles).multiply(worldRotation);
|
702
|
+
const rotation = vector3ToQuaternion(new Vector3(rx, ry, rz)).multiply(worldRotation);
|
615
703
|
rigidBody.setRotation({
|
616
704
|
x: rotation.x,
|
617
705
|
y: rotation.y,
|
@@ -624,12 +712,19 @@ const useRigidBody = (options = {}) => {
|
|
624
712
|
const autoColliders = colliderSetting !== false ? createCollidersFromChildren(ref.current, rigidBody, _objectSpread2(_objectSpread2({}, options), {}, {
|
625
713
|
colliders: colliderSetting
|
626
714
|
}), world) : [];
|
627
|
-
|
715
|
+
rigidBodyStates.set(rigidBody.handle, {
|
716
|
+
mesh: ref.current,
|
717
|
+
invertedMatrixWorld: ref.current.parent.matrixWorld.clone().invert(),
|
718
|
+
isSleeping: false,
|
719
|
+
worldScale: ref.current.getWorldScale(_vector3).clone(),
|
720
|
+
setMatrix: mat => ref.current.matrix.copy(mat)
|
721
|
+
});
|
722
|
+
ref.current.matrixAutoUpdate = false;
|
628
723
|
return () => {
|
629
724
|
world.removeRigidBody(rigidBody);
|
630
725
|
autoColliders.forEach(collider => world.removeCollider(collider));
|
631
726
|
rigidBodyRef.current = undefined;
|
632
|
-
|
727
|
+
rigidBodyStates.delete(rigidBody.handle);
|
633
728
|
};
|
634
729
|
}, []); // Events
|
635
730
|
|
@@ -647,29 +742,6 @@ const useRigidBody = (options = {}) => {
|
|
647
742
|
}, [options.onCollisionEnter, options.onCollisionExit]);
|
648
743
|
const api = useMemo(() => createRigidBodyApi(getRigidBodyRef), []);
|
649
744
|
return [ref, api];
|
650
|
-
};
|
651
|
-
const useCollider = (body, options = {}) => {
|
652
|
-
const {
|
653
|
-
world
|
654
|
-
} = useRapier();
|
655
|
-
const colliderRef = useRef();
|
656
|
-
const objectRef = useRef();
|
657
|
-
const getColliderRef = useRef(() => {
|
658
|
-
if (!colliderRef.current) {
|
659
|
-
colliderRef.current = createColliderFromOptions(options, world, world.getRigidBody(body.handle));
|
660
|
-
}
|
661
|
-
|
662
|
-
return colliderRef.current;
|
663
|
-
});
|
664
|
-
useEffect(() => {
|
665
|
-
const collider = getColliderRef.current();
|
666
|
-
return () => {
|
667
|
-
if (collider) world.removeCollider(collider);
|
668
|
-
colliderRef.current = undefined;
|
669
|
-
};
|
670
|
-
}, []);
|
671
|
-
const api = useMemo(() => createColliderApi(getColliderRef), []);
|
672
|
-
return [objectRef, api];
|
673
745
|
}; // Joints
|
674
746
|
|
675
747
|
const useImpulseJoint = (body1, body2, params) => {
|
@@ -682,7 +754,7 @@ const useImpulseJoint = (body1, body2, params) => {
|
|
682
754
|
let rb1;
|
683
755
|
let rb2;
|
684
756
|
|
685
|
-
if (
|
757
|
+
if ("current" in body1 && body1.current && "current" in body2 && body2.current) {
|
686
758
|
rb1 = world.getRigidBody(body1.current.handle);
|
687
759
|
rb2 = world.getRigidBody(body2.current.handle);
|
688
760
|
const newJoint = world.createImpulseJoint(params, rb1, rb2);
|
@@ -759,149 +831,69 @@ const usePrismaticJoint = (body1, body2, [body1Anchor, body2Anchor, axis]) => {
|
|
759
831
|
return useImpulseJoint(body1, body2, rapier.JointData.prismatic(vectorArrayToObject(body1Anchor), vectorArrayToObject(body2Anchor), vectorArrayToObject(axis)));
|
760
832
|
};
|
761
833
|
|
762
|
-
|
763
|
-
_extends = Object.assign || function (target) {
|
764
|
-
for (var i = 1; i < arguments.length; i++) {
|
765
|
-
var source = arguments[i];
|
766
|
-
|
767
|
-
for (var key in source) {
|
768
|
-
if (Object.prototype.hasOwnProperty.call(source, key)) {
|
769
|
-
target[key] = source[key];
|
770
|
-
}
|
771
|
-
}
|
772
|
-
}
|
773
|
-
|
774
|
-
return target;
|
775
|
-
};
|
776
|
-
|
777
|
-
return _extends.apply(this, arguments);
|
778
|
-
}
|
779
|
-
|
780
|
-
function _objectWithoutPropertiesLoose(source, excluded) {
|
781
|
-
if (source == null) return {};
|
782
|
-
var target = {};
|
783
|
-
var sourceKeys = Object.keys(source);
|
784
|
-
var key, i;
|
785
|
-
|
786
|
-
for (i = 0; i < sourceKeys.length; i++) {
|
787
|
-
key = sourceKeys[i];
|
788
|
-
if (excluded.indexOf(key) >= 0) continue;
|
789
|
-
target[key] = source[key];
|
790
|
-
}
|
791
|
-
|
792
|
-
return target;
|
793
|
-
}
|
794
|
-
|
795
|
-
function _objectWithoutProperties(source, excluded) {
|
796
|
-
if (source == null) return {};
|
797
|
-
var target = _objectWithoutPropertiesLoose(source, excluded);
|
798
|
-
var key, i;
|
799
|
-
|
800
|
-
if (Object.getOwnPropertySymbols) {
|
801
|
-
var sourceSymbolKeys = Object.getOwnPropertySymbols(source);
|
802
|
-
|
803
|
-
for (i = 0; i < sourceSymbolKeys.length; i++) {
|
804
|
-
key = sourceSymbolKeys[i];
|
805
|
-
if (excluded.indexOf(key) >= 0) continue;
|
806
|
-
if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
|
807
|
-
target[key] = source[key];
|
808
|
-
}
|
809
|
-
}
|
810
|
-
|
811
|
-
return target;
|
812
|
-
}
|
813
|
-
|
814
|
-
const _excluded = ["children"],
|
815
|
-
_excluded2 = ["children"];
|
834
|
+
const _excluded$1 = ["children"];
|
816
835
|
const RigidBodyContext = /*#__PURE__*/createContext(undefined);
|
817
|
-
|
818
836
|
const useRigidBodyContext = () => useContext(RigidBodyContext); // RigidBody
|
819
837
|
|
820
|
-
|
821
838
|
const RigidBody = /*#__PURE__*/forwardRef((_ref, ref) => {
|
822
839
|
let {
|
823
840
|
children
|
824
841
|
} = _ref,
|
825
|
-
props = _objectWithoutProperties(_ref, _excluded);
|
826
|
-
|
827
|
-
const [object,
|
828
|
-
useImperativeHandle(ref, () =>
|
829
|
-
return /*#__PURE__*/React.createElement(RigidBodyContext.Provider, {
|
830
|
-
value:
|
831
|
-
|
842
|
+
props = _objectWithoutProperties(_ref, _excluded$1);
|
843
|
+
|
844
|
+
const [object, api] = useRigidBody(props);
|
845
|
+
useImperativeHandle(ref, () => api);
|
846
|
+
return /*#__PURE__*/React$1.createElement(RigidBodyContext.Provider, {
|
847
|
+
value: {
|
848
|
+
ref: object,
|
849
|
+
api,
|
850
|
+
hasCollisionEvents: !!(props.onCollisionEnter || props.onCollisionExit),
|
851
|
+
options: props
|
852
|
+
}
|
853
|
+
}, /*#__PURE__*/React$1.createElement("object3D", {
|
832
854
|
ref: object
|
833
855
|
}, children));
|
834
|
-
});
|
835
|
-
|
836
|
-
const AnyCollider = _ref2 => {
|
837
|
-
let {
|
838
|
-
children
|
839
|
-
} = _ref2,
|
840
|
-
props = _objectWithoutProperties(_ref2, _excluded2);
|
856
|
+
});
|
841
857
|
|
858
|
+
const MeshCollider = ({
|
859
|
+
children,
|
860
|
+
type
|
861
|
+
}) => {
|
842
862
|
const {
|
863
|
+
physicsOptions,
|
843
864
|
world
|
844
865
|
} = useRapier();
|
845
|
-
const
|
846
|
-
const
|
866
|
+
const object = useRef(null);
|
867
|
+
const {
|
868
|
+
api,
|
869
|
+
options
|
870
|
+
} = useRigidBodyContext();
|
847
871
|
useEffect(() => {
|
848
|
-
|
849
|
-
|
872
|
+
let autoColliders = [];
|
873
|
+
|
874
|
+
if (object.current) {
|
875
|
+
var _ref;
|
876
|
+
|
877
|
+
const colliderSetting = (_ref = type !== null && type !== void 0 ? type : physicsOptions.colliders) !== null && _ref !== void 0 ? _ref : false;
|
878
|
+
autoColliders = colliderSetting !== false ? createCollidersFromChildren(object.current, api, _objectSpread2(_objectSpread2({}, options), {}, {
|
879
|
+
colliders: colliderSetting
|
880
|
+
}), world, false) : [];
|
881
|
+
}
|
882
|
+
|
850
883
|
return () => {
|
851
|
-
|
884
|
+
autoColliders.forEach(collider => {
|
885
|
+
world.removeCollider(collider);
|
886
|
+
});
|
852
887
|
};
|
853
888
|
}, []);
|
854
889
|
return /*#__PURE__*/React.createElement("object3D", {
|
855
|
-
ref:
|
890
|
+
ref: object,
|
891
|
+
userData: {
|
892
|
+
r3RapierType: "MeshCollider"
|
893
|
+
}
|
856
894
|
}, children);
|
857
895
|
};
|
858
896
|
|
859
|
-
const CuboidCollider = props => {
|
860
|
-
return /*#__PURE__*/React.createElement(AnyCollider, _extends({}, props, {
|
861
|
-
shape: "cuboid"
|
862
|
-
}));
|
863
|
-
};
|
864
|
-
const RoundCuboidCollider = props => {
|
865
|
-
return /*#__PURE__*/React.createElement(AnyCollider, _extends({}, props, {
|
866
|
-
shape: "roundCuboid"
|
867
|
-
}));
|
868
|
-
};
|
869
|
-
const BallCollider = props => {
|
870
|
-
return /*#__PURE__*/React.createElement(AnyCollider, _extends({}, props, {
|
871
|
-
shape: "ball"
|
872
|
-
}));
|
873
|
-
};
|
874
|
-
const CapsuleCollider = props => {
|
875
|
-
return /*#__PURE__*/React.createElement(AnyCollider, _extends({}, props, {
|
876
|
-
shape: "capsule"
|
877
|
-
}));
|
878
|
-
};
|
879
|
-
const HeightfieldCollider = props => {
|
880
|
-
return /*#__PURE__*/React.createElement(AnyCollider, _extends({}, props, {
|
881
|
-
shape: "heightfield"
|
882
|
-
}));
|
883
|
-
};
|
884
|
-
const TrimeshCollider = props => {
|
885
|
-
return /*#__PURE__*/React.createElement(AnyCollider, _extends({}, props, {
|
886
|
-
shape: "trimesh"
|
887
|
-
}));
|
888
|
-
};
|
889
|
-
const ConeCollider = props => {
|
890
|
-
return /*#__PURE__*/React.createElement(AnyCollider, _extends({}, props, {
|
891
|
-
shape: "cone"
|
892
|
-
}));
|
893
|
-
};
|
894
|
-
const CylinderCollider = props => {
|
895
|
-
return /*#__PURE__*/React.createElement(AnyCollider, _extends({}, props, {
|
896
|
-
shape: "cylinder"
|
897
|
-
}));
|
898
|
-
};
|
899
|
-
const ConvexHullCollider = props => {
|
900
|
-
return /*#__PURE__*/React.createElement(AnyCollider, _extends({}, props, {
|
901
|
-
shape: "convexHull"
|
902
|
-
}));
|
903
|
-
};
|
904
|
-
|
905
897
|
const geometryFromCollider = collider => {
|
906
898
|
switch (collider.shape.type) {
|
907
899
|
case ShapeType.Cuboid:
|
@@ -987,12 +979,12 @@ const DebugShape = /*#__PURE__*/memo(({
|
|
987
979
|
const collider = world.getCollider(colliderHandle);
|
988
980
|
return geometryFromCollider(collider);
|
989
981
|
}, [colliderHandle]);
|
990
|
-
return /*#__PURE__*/React.createElement("mesh", {
|
982
|
+
return /*#__PURE__*/React$1.createElement("mesh", {
|
991
983
|
ref: ref
|
992
|
-
}, /*#__PURE__*/React.createElement("primitive", {
|
984
|
+
}, /*#__PURE__*/React$1.createElement("primitive", {
|
993
985
|
object: geometry,
|
994
986
|
attach: "geometry"
|
995
|
-
}), /*#__PURE__*/React.createElement("meshBasicMaterial", {
|
987
|
+
}), /*#__PURE__*/React$1.createElement("meshBasicMaterial", {
|
996
988
|
color: "red",
|
997
989
|
wireframe: true
|
998
990
|
}));
|
@@ -1011,10 +1003,204 @@ const Debug = () => {
|
|
1011
1003
|
});
|
1012
1004
|
setColliders(newColliders);
|
1013
1005
|
});
|
1014
|
-
return /*#__PURE__*/React.createElement("group", null, colliders.map(handle => /*#__PURE__*/React.createElement(DebugShape, {
|
1006
|
+
return /*#__PURE__*/React$1.createElement("group", null, colliders.map(handle => /*#__PURE__*/React$1.createElement(DebugShape, {
|
1015
1007
|
key: handle,
|
1016
1008
|
colliderHandle: handle
|
1017
1009
|
})));
|
1018
1010
|
};
|
1019
1011
|
|
1020
|
-
|
1012
|
+
const InstancedRigidBodies = /*#__PURE__*/forwardRef((props, ref) => {
|
1013
|
+
const {
|
1014
|
+
world,
|
1015
|
+
rigidBodyStates,
|
1016
|
+
physicsOptions
|
1017
|
+
} = useRapier();
|
1018
|
+
const object = useRef(null);
|
1019
|
+
const instancesRef = useRef();
|
1020
|
+
const instancesRefGetter = useRef(() => {
|
1021
|
+
if (!instancesRef.current) {
|
1022
|
+
instancesRef.current = [];
|
1023
|
+
}
|
1024
|
+
|
1025
|
+
return instancesRef.current;
|
1026
|
+
});
|
1027
|
+
useEffect(() => {
|
1028
|
+
const colliders = [];
|
1029
|
+
const rigidBodies = instancesRefGetter.current();
|
1030
|
+
|
1031
|
+
if (object.current) {
|
1032
|
+
const scale = object.current.getWorldScale(new Vector3());
|
1033
|
+
let hasOneMesh = false;
|
1034
|
+
object.current.traverse(mesh => {
|
1035
|
+
if (mesh instanceof InstancedMesh) {
|
1036
|
+
if (hasOneMesh) {
|
1037
|
+
console.warn("Can only use a single InstancedMesh inside <InstancedRigidBodies />, more InstancedMeshes will be ignored.");
|
1038
|
+
return;
|
1039
|
+
}
|
1040
|
+
|
1041
|
+
hasOneMesh = true;
|
1042
|
+
mesh.instanceMatrix.setUsage(DynamicDrawUsage);
|
1043
|
+
const rigidBodyDesc = rigidBodyDescFromOptions(props);
|
1044
|
+
const colliderDesc = colliderDescFromGeometry(mesh.geometry, props.colliders || physicsOptions.colliders, scale, false // Collisions currently not enabled for instances
|
1045
|
+
);
|
1046
|
+
|
1047
|
+
for (let index = 0; index < mesh.count; index++) {
|
1048
|
+
const rigidBody = world.createRigidBody(rigidBodyDesc);
|
1049
|
+
const matrix = new Matrix4();
|
1050
|
+
mesh.getMatrixAt(index, matrix);
|
1051
|
+
const {
|
1052
|
+
position,
|
1053
|
+
rotation
|
1054
|
+
} = decomposeMatrix4(matrix); // Set positions
|
1055
|
+
|
1056
|
+
if (props.positions && props.positions[index]) {
|
1057
|
+
rigidBody.setTranslation(vectorArrayToObject(props.positions[index]), true);
|
1058
|
+
} else {
|
1059
|
+
rigidBody.setTranslation(position, true);
|
1060
|
+
} // Set rotations
|
1061
|
+
|
1062
|
+
|
1063
|
+
if (props.rotations && props.rotations[index]) {
|
1064
|
+
const [x, y, z] = props.rotations[index];
|
1065
|
+
rigidBody.setRotation(vector3ToQuaternion(new Vector3(x, y, z)), true);
|
1066
|
+
} else {
|
1067
|
+
rigidBody.setRotation(rotation, true);
|
1068
|
+
}
|
1069
|
+
|
1070
|
+
const collider = world.createCollider(colliderDesc, rigidBody);
|
1071
|
+
rigidBodyStates.set(rigidBody.handle, {
|
1072
|
+
mesh: mesh,
|
1073
|
+
isSleeping: false,
|
1074
|
+
invertedMatrixWorld: object.current.matrixWorld.clone().invert(),
|
1075
|
+
setMatrix: matrix => mesh.setMatrixAt(index, matrix),
|
1076
|
+
worldScale: object.current.getWorldScale(new Vector3())
|
1077
|
+
});
|
1078
|
+
const api = createRigidBodyApi({
|
1079
|
+
current() {
|
1080
|
+
return rigidBody;
|
1081
|
+
}
|
1082
|
+
|
1083
|
+
});
|
1084
|
+
colliders.push(collider);
|
1085
|
+
rigidBodies.push({
|
1086
|
+
rigidBody,
|
1087
|
+
api
|
1088
|
+
});
|
1089
|
+
}
|
1090
|
+
}
|
1091
|
+
|
1092
|
+
if (mesh.type === "Mesh" && !("isInstancedMesh" in mesh)) {
|
1093
|
+
console.warn("Can only use InstancedMesh inside <InstancedRigidBodies />, Mesh will be ignored.");
|
1094
|
+
}
|
1095
|
+
});
|
1096
|
+
return () => {
|
1097
|
+
rigidBodies.forEach(rb => world.removeRigidBody(rb.rigidBody));
|
1098
|
+
colliders.forEach(coll => world.removeCollider(coll));
|
1099
|
+
instancesRef.current = undefined;
|
1100
|
+
};
|
1101
|
+
}
|
1102
|
+
}, []);
|
1103
|
+
useImperativeHandle(ref, () => createInstancedRigidBodiesApi(instancesRefGetter));
|
1104
|
+
return /*#__PURE__*/React.createElement("object3D", {
|
1105
|
+
ref: object
|
1106
|
+
}, props.children);
|
1107
|
+
});
|
1108
|
+
|
1109
|
+
function _extends() {
|
1110
|
+
_extends = Object.assign || function (target) {
|
1111
|
+
for (var i = 1; i < arguments.length; i++) {
|
1112
|
+
var source = arguments[i];
|
1113
|
+
|
1114
|
+
for (var key in source) {
|
1115
|
+
if (Object.prototype.hasOwnProperty.call(source, key)) {
|
1116
|
+
target[key] = source[key];
|
1117
|
+
}
|
1118
|
+
}
|
1119
|
+
}
|
1120
|
+
|
1121
|
+
return target;
|
1122
|
+
};
|
1123
|
+
|
1124
|
+
return _extends.apply(this, arguments);
|
1125
|
+
}
|
1126
|
+
|
1127
|
+
const _excluded = ["children"];
|
1128
|
+
|
1129
|
+
const AnyCollider = _ref => {
|
1130
|
+
let {
|
1131
|
+
children
|
1132
|
+
} = _ref,
|
1133
|
+
props = _objectWithoutProperties(_ref, _excluded);
|
1134
|
+
|
1135
|
+
const {
|
1136
|
+
world
|
1137
|
+
} = useRapier();
|
1138
|
+
const rigidBodyContext = useRigidBodyContext();
|
1139
|
+
const ref = useRef(null);
|
1140
|
+
useEffect(() => {
|
1141
|
+
var _rigidBodyContext$api;
|
1142
|
+
|
1143
|
+
const scale = ref.current.getWorldScale(new Vector3());
|
1144
|
+
const collider = createColliderFromOptions({
|
1145
|
+
options: props,
|
1146
|
+
world,
|
1147
|
+
rigidBody: rigidBodyContext === null || rigidBodyContext === void 0 ? void 0 : (_rigidBodyContext$api = rigidBodyContext.api) === null || _rigidBodyContext$api === void 0 ? void 0 : _rigidBodyContext$api.raw(),
|
1148
|
+
scale,
|
1149
|
+
hasCollisionEvents: rigidBodyContext === null || rigidBodyContext === void 0 ? void 0 : rigidBodyContext.hasCollisionEvents
|
1150
|
+
});
|
1151
|
+
return () => {
|
1152
|
+
world.removeCollider(collider);
|
1153
|
+
};
|
1154
|
+
}, []);
|
1155
|
+
return /*#__PURE__*/React.createElement("object3D", {
|
1156
|
+
ref: ref
|
1157
|
+
}, children);
|
1158
|
+
};
|
1159
|
+
|
1160
|
+
const CuboidCollider = props => {
|
1161
|
+
return /*#__PURE__*/React.createElement(AnyCollider, _extends({}, props, {
|
1162
|
+
shape: "cuboid"
|
1163
|
+
}));
|
1164
|
+
};
|
1165
|
+
const RoundCuboidCollider = props => {
|
1166
|
+
return /*#__PURE__*/React.createElement(AnyCollider, _extends({}, props, {
|
1167
|
+
shape: "roundCuboid"
|
1168
|
+
}));
|
1169
|
+
};
|
1170
|
+
const BallCollider = props => {
|
1171
|
+
return /*#__PURE__*/React.createElement(AnyCollider, _extends({}, props, {
|
1172
|
+
shape: "ball"
|
1173
|
+
}));
|
1174
|
+
};
|
1175
|
+
const CapsuleCollider = props => {
|
1176
|
+
return /*#__PURE__*/React.createElement(AnyCollider, _extends({}, props, {
|
1177
|
+
shape: "capsule"
|
1178
|
+
}));
|
1179
|
+
};
|
1180
|
+
const HeightfieldCollider = props => {
|
1181
|
+
return /*#__PURE__*/React.createElement(AnyCollider, _extends({}, props, {
|
1182
|
+
shape: "heightfield"
|
1183
|
+
}));
|
1184
|
+
};
|
1185
|
+
const TrimeshCollider = props => {
|
1186
|
+
return /*#__PURE__*/React.createElement(AnyCollider, _extends({}, props, {
|
1187
|
+
shape: "trimesh"
|
1188
|
+
}));
|
1189
|
+
};
|
1190
|
+
const ConeCollider = props => {
|
1191
|
+
return /*#__PURE__*/React.createElement(AnyCollider, _extends({}, props, {
|
1192
|
+
shape: "cone"
|
1193
|
+
}));
|
1194
|
+
};
|
1195
|
+
const CylinderCollider = props => {
|
1196
|
+
return /*#__PURE__*/React.createElement(AnyCollider, _extends({}, props, {
|
1197
|
+
shape: "cylinder"
|
1198
|
+
}));
|
1199
|
+
};
|
1200
|
+
const ConvexHullCollider = props => {
|
1201
|
+
return /*#__PURE__*/React.createElement(AnyCollider, _extends({}, props, {
|
1202
|
+
shape: "convexHull"
|
1203
|
+
}));
|
1204
|
+
};
|
1205
|
+
|
1206
|
+
export { BallCollider, CapsuleCollider, ConeCollider, ConvexHullCollider, CuboidCollider, CylinderCollider, Debug, HeightfieldCollider, InstancedRigidBodies, MeshCollider, Physics, RigidBody, RoundCuboidCollider, TrimeshCollider, useFixedJoint, useImpulseJoint, usePrismaticJoint, useRapier, useRevoluteJoint, useRigidBody, useSphericalJoint };
|