@react-three/rapier 0.5.2 → 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 -11
- 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 +9 -0
- 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/utils.d.ts +13 -4
- package/dist/react-three-rapier.cjs.dev.js +421 -312
- package/dist/react-three-rapier.cjs.prod.js +421 -312
- package/dist/react-three-rapier.esm.js +395 -286
- package/package.json +1 -1
- package/readme.md +64 -12
@@ -2,10 +2,10 @@
|
|
2
2
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
4
4
|
|
5
|
-
var
|
5
|
+
var rapier3dCompat = require('@dimforge/rapier3d-compat');
|
6
|
+
var React$1 = require('react');
|
6
7
|
var useAsset = require('use-asset');
|
7
8
|
var fiber = require('@react-three/fiber');
|
8
|
-
var rapier3dCompat = require('@dimforge/rapier3d-compat');
|
9
9
|
var three = require('three');
|
10
10
|
|
11
11
|
function _interopDefault (e) { return e && e.__esModule ? e : { 'default': e }; }
|
@@ -28,7 +28,13 @@ function _interopNamespace(e) {
|
|
28
28
|
return Object.freeze(n);
|
29
29
|
}
|
30
30
|
|
31
|
-
var React__default = /*#__PURE__*/_interopDefault(React);
|
31
|
+
var React__default = /*#__PURE__*/_interopDefault(React$1);
|
32
|
+
|
33
|
+
const _quaternion = new three.Quaternion();
|
34
|
+
const _euler = new three.Euler();
|
35
|
+
const _vector3 = new three.Vector3();
|
36
|
+
new three.Object3D();
|
37
|
+
const _matrix4 = new three.Matrix4();
|
32
38
|
|
33
39
|
const vectorArrayToObject = arr => {
|
34
40
|
const [x, y, z] = arr;
|
@@ -38,11 +44,20 @@ const vectorArrayToObject = arr => {
|
|
38
44
|
z
|
39
45
|
};
|
40
46
|
};
|
41
|
-
const quaternion = new three.Quaternion();
|
42
|
-
const euler = new three.Euler();
|
43
47
|
const vector3ToQuaternion = v => {
|
44
|
-
return
|
48
|
+
return _quaternion.setFromEuler(_euler.setFromVector3(v));
|
45
49
|
};
|
50
|
+
const rapierVector3ToVector3 = ({
|
51
|
+
x,
|
52
|
+
y,
|
53
|
+
z
|
54
|
+
}) => _vector3.set(x, y, z).clone();
|
55
|
+
const rapierQuaternionToQuaternion = ({
|
56
|
+
x,
|
57
|
+
y,
|
58
|
+
z,
|
59
|
+
w
|
60
|
+
}) => _quaternion.set(x, y, z, w);
|
46
61
|
const rigidBodyTypeMap = {
|
47
62
|
fixed: 1,
|
48
63
|
dynamic: 0,
|
@@ -50,6 +65,17 @@ const rigidBodyTypeMap = {
|
|
50
65
|
kinematicVelocity: 3
|
51
66
|
};
|
52
67
|
const rigidBodyTypeFromString = type => rigidBodyTypeMap[type];
|
68
|
+
const decomposeMatrix4 = m => {
|
69
|
+
const position = new three.Vector3();
|
70
|
+
const rotation = new three.Quaternion();
|
71
|
+
const scale = new three.Vector3();
|
72
|
+
m.decompose(position, rotation, scale);
|
73
|
+
return {
|
74
|
+
position,
|
75
|
+
rotation,
|
76
|
+
scale
|
77
|
+
};
|
78
|
+
};
|
53
79
|
const scaleColliderArgs = (shape, args, scale) => {
|
54
80
|
// Heightfield only scales the last arg
|
55
81
|
const newArgs = args.slice();
|
@@ -135,8 +161,7 @@ const isChildOfMeshCollider = child => {
|
|
135
161
|
const createCollidersFromChildren = (object, rigidBody, options, world, ignoreMeshColliders = true) => {
|
136
162
|
const hasCollisionEvents = !!(options.onCollisionEnter || options.onCollisionExit);
|
137
163
|
const colliders = [];
|
138
|
-
|
139
|
-
let offset = new three.Vector3();
|
164
|
+
new three.Vector3();
|
140
165
|
object.traverse(child => {
|
141
166
|
if ("isMesh" in child) {
|
142
167
|
if (ignoreMeshColliders && isChildOfMeshCollider(child)) return;
|
@@ -154,62 +179,32 @@ const createCollidersFromChildren = (object, rigidBody, options, world, ignoreMe
|
|
154
179
|
z: rz,
|
155
180
|
w: rw
|
156
181
|
} = new three.Quaternion().setFromEuler(child.rotation);
|
157
|
-
const scale = child.getWorldScale(new three.Vector3());
|
182
|
+
const scale = child.getWorldScale(new three.Vector3()); // We translate the colliders based on the parent's world scale
|
158
183
|
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
geometry.computeBoundingBox();
|
163
|
-
const {
|
164
|
-
boundingBox
|
165
|
-
} = geometry;
|
166
|
-
const size = boundingBox.getSize(new three.Vector3());
|
167
|
-
boundingBox.getCenter(offset);
|
168
|
-
desc = rapier3dCompat.ColliderDesc.cuboid(size.x / 2 * scale.x, size.y / 2 * scale.y, size.z / 2 * scale.z);
|
169
|
-
}
|
170
|
-
break;
|
171
|
-
|
172
|
-
case "ball":
|
173
|
-
{
|
174
|
-
geometry.computeBoundingSphere();
|
175
|
-
const {
|
176
|
-
boundingSphere
|
177
|
-
} = geometry;
|
178
|
-
const radius = boundingSphere.radius * scale.x;
|
179
|
-
offset.copy(boundingSphere.center);
|
180
|
-
desc = rapier3dCompat.ColliderDesc.ball(radius);
|
181
|
-
}
|
182
|
-
break;
|
183
|
-
|
184
|
-
case "trimesh":
|
185
|
-
{
|
186
|
-
var _g$index;
|
187
|
-
|
188
|
-
const _g = geometry.clone().scale(scale.x, scale.y, scale.z);
|
184
|
+
const parentWorldScale = child.parent.getWorldScale(new three.Vector3());
|
185
|
+
const desc = colliderDescFromGeometry(geometry, options.colliders, scale, hasCollisionEvents);
|
186
|
+
const offset = new three.Vector3(0, 0, 0);
|
189
187
|
|
190
|
-
|
191
|
-
|
192
|
-
break;
|
188
|
+
if (options.colliders === "cuboid") {
|
189
|
+
var _geometry$boundingBox;
|
193
190
|
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
desc = rapier3dCompat.ColliderDesc.convexHull(g.attributes.position.array);
|
198
|
-
}
|
199
|
-
break;
|
200
|
-
} // We translate the colliders based on the parent's world scale
|
191
|
+
geometry.computeBoundingBox();
|
192
|
+
(_geometry$boundingBox = geometry.boundingBox) === null || _geometry$boundingBox === void 0 ? void 0 : _geometry$boundingBox.getCenter(offset);
|
193
|
+
}
|
201
194
|
|
195
|
+
if (options.colliders === "ball") {
|
196
|
+
geometry.computeBoundingSphere();
|
197
|
+
offset.copy(geometry.boundingSphere.center);
|
198
|
+
}
|
202
199
|
|
203
|
-
|
200
|
+
if (Number.isFinite(options.friction)) desc.setFriction(options.friction);
|
201
|
+
if (Number.isFinite(options.restitution)) desc.setRestitution(options.restitution);
|
204
202
|
desc.setTranslation((x + offset.x) * parentWorldScale.x, (y + offset.y) * parentWorldScale.y, (z + offset.z) * parentWorldScale.z).setRotation({
|
205
203
|
x: rx,
|
206
204
|
y: ry,
|
207
205
|
z: rz,
|
208
206
|
w: rw
|
209
207
|
});
|
210
|
-
if (hasCollisionEvents) desc.setActiveEvents(rapier3dCompat.ActiveEvents.COLLISION_EVENTS);
|
211
|
-
if (Number.isFinite(options.friction)) desc.setFriction(options.friction);
|
212
|
-
if (Number.isFinite(options.restitution)) desc.setRestitution(options.restitution);
|
213
208
|
const actualRigidBody = world.getRigidBody(rigidBody === null || rigidBody === void 0 ? void 0 : rigidBody.handle);
|
214
209
|
const collider = world.createCollider(desc, actualRigidBody);
|
215
210
|
colliders.push(collider);
|
@@ -217,6 +212,53 @@ const createCollidersFromChildren = (object, rigidBody, options, world, ignoreMe
|
|
217
212
|
});
|
218
213
|
return colliders;
|
219
214
|
};
|
215
|
+
const colliderDescFromGeometry = (geometry, colliders, scale, hasCollisionEvents) => {
|
216
|
+
let desc;
|
217
|
+
|
218
|
+
switch (colliders) {
|
219
|
+
case "cuboid":
|
220
|
+
{
|
221
|
+
geometry.computeBoundingBox();
|
222
|
+
const {
|
223
|
+
boundingBox
|
224
|
+
} = geometry;
|
225
|
+
const size = boundingBox.getSize(new three.Vector3());
|
226
|
+
desc = rapier3dCompat.ColliderDesc.cuboid(size.x / 2 * scale.x, size.y / 2 * scale.y, size.z / 2 * scale.z);
|
227
|
+
}
|
228
|
+
break;
|
229
|
+
|
230
|
+
case "ball":
|
231
|
+
{
|
232
|
+
geometry.computeBoundingSphere();
|
233
|
+
const {
|
234
|
+
boundingSphere
|
235
|
+
} = geometry;
|
236
|
+
const radius = boundingSphere.radius * scale.x;
|
237
|
+
desc = rapier3dCompat.ColliderDesc.ball(radius);
|
238
|
+
}
|
239
|
+
break;
|
240
|
+
|
241
|
+
case "trimesh":
|
242
|
+
{
|
243
|
+
var _g$index;
|
244
|
+
|
245
|
+
const _g = geometry.clone().scale(scale.x, scale.y, scale.z);
|
246
|
+
|
247
|
+
desc = rapier3dCompat.ColliderDesc.trimesh(_g.attributes.position.array, (_g$index = _g.index) === null || _g$index === void 0 ? void 0 : _g$index.array);
|
248
|
+
}
|
249
|
+
break;
|
250
|
+
|
251
|
+
case "hull":
|
252
|
+
const g = geometry.clone().scale(scale.x, scale.y, scale.z);
|
253
|
+
{
|
254
|
+
desc = rapier3dCompat.ColliderDesc.convexHull(g.attributes.position.array);
|
255
|
+
}
|
256
|
+
break;
|
257
|
+
}
|
258
|
+
|
259
|
+
if (hasCollisionEvents) desc.setActiveEvents(rapier3dCompat.ActiveEvents.COLLISION_EVENTS);
|
260
|
+
return desc;
|
261
|
+
};
|
220
262
|
const scaleVertices = (vertices, scale) => {
|
221
263
|
const scaledVerts = Array.from(vertices);
|
222
264
|
|
@@ -228,6 +270,26 @@ const scaleVertices = (vertices, scale) => {
|
|
228
270
|
|
229
271
|
return scaledVerts;
|
230
272
|
};
|
273
|
+
const rigidBodyDescFromOptions = options => {
|
274
|
+
var _options$linearVeloci, _options$angularVeloc, _options$gravityScale, _options$canSleep, _options$ccd, _options$enabledRotat, _options$enabledTrans;
|
275
|
+
|
276
|
+
const type = rigidBodyTypeFromString((options === null || options === void 0 ? void 0 : options.type) || "dynamic");
|
277
|
+
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];
|
278
|
+
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];
|
279
|
+
const gravityScale = (_options$gravityScale = options === null || options === void 0 ? void 0 : options.gravityScale) !== null && _options$gravityScale !== void 0 ? _options$gravityScale : 1;
|
280
|
+
const canSleep = (_options$canSleep = options === null || options === void 0 ? void 0 : options.canSleep) !== null && _options$canSleep !== void 0 ? _options$canSleep : true;
|
281
|
+
const ccdEnabled = (_options$ccd = options === null || options === void 0 ? void 0 : options.ccd) !== null && _options$ccd !== void 0 ? _options$ccd : false;
|
282
|
+
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];
|
283
|
+
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];
|
284
|
+
const desc = new rapier3dCompat.RigidBodyDesc(type).setLinvel(lvx, lvy, lvz).setAngvel({
|
285
|
+
x: avx,
|
286
|
+
y: avy,
|
287
|
+
z: avz
|
288
|
+
}).setGravityScale(gravityScale).setCanSleep(canSleep).setCcdEnabled(ccdEnabled).enabledRotations(erx, ery, erz).enabledTranslations(etx, ety, etz);
|
289
|
+
if (options.lockRotations) desc.lockRotations();
|
290
|
+
if (options.lockTranslations) desc.lockTranslations();
|
291
|
+
return desc;
|
292
|
+
};
|
231
293
|
|
232
294
|
const createRigidBodyApi = ref => {
|
233
295
|
return {
|
@@ -329,18 +391,19 @@ const createRigidBodyApi = ref => {
|
|
329
391
|
setEnabledRotations: (x, y, z) => ref.current().setEnabledRotations(x, y, z, true),
|
330
392
|
setEnabledTranslations: (x, y, z) => ref.current().setEnabledTranslations(x, y, z, true)
|
331
393
|
};
|
332
|
-
};
|
394
|
+
};
|
395
|
+
const createInstancedRigidBodiesApi = bodiesGetter => ({
|
396
|
+
at: index => bodiesGetter.current()[index].api,
|
333
397
|
|
334
|
-
|
335
|
-
|
336
|
-
|
398
|
+
forEach(callback) {
|
399
|
+
return bodiesGetter.current().map(b => b.api).forEach(callback);
|
400
|
+
},
|
337
401
|
|
338
|
-
|
339
|
-
|
340
|
-
|
402
|
+
get count() {
|
403
|
+
return bodiesGetter.current().length;
|
404
|
+
}
|
341
405
|
|
342
|
-
|
343
|
-
};
|
406
|
+
}); // TODO: Flesh this out
|
344
407
|
const createWorldApi = ref => {
|
345
408
|
return {
|
346
409
|
raw: () => ref.current(),
|
@@ -378,7 +441,7 @@ const createJointApi = ref => {
|
|
378
441
|
};
|
379
442
|
};
|
380
443
|
|
381
|
-
const RapierContext = /*#__PURE__*/React.createContext(undefined);
|
444
|
+
const RapierContext = /*#__PURE__*/React$1.createContext(undefined);
|
382
445
|
|
383
446
|
const importRapier = async () => {
|
384
447
|
let r = await Promise.resolve().then(function () { return /*#__PURE__*/_interopNamespace(require('@dimforge/rapier3d-compat')); });
|
@@ -387,14 +450,14 @@ const importRapier = async () => {
|
|
387
450
|
};
|
388
451
|
|
389
452
|
const Physics = ({
|
390
|
-
colliders: _colliders =
|
453
|
+
colliders: _colliders = "cuboid",
|
391
454
|
gravity: _gravity = [0, -9.81, 0],
|
392
455
|
children,
|
393
|
-
timeStep: _timeStep =
|
456
|
+
timeStep: _timeStep = "vary"
|
394
457
|
}) => {
|
395
458
|
const rapier = useAsset.useAsset(importRapier);
|
396
|
-
const worldRef = React.useRef();
|
397
|
-
const getWorldRef = React.useRef(() => {
|
459
|
+
const worldRef = React$1.useRef();
|
460
|
+
const getWorldRef = React$1.useRef(() => {
|
398
461
|
if (!worldRef.current) {
|
399
462
|
const world = new rapier.World(vectorArrayToObject(_gravity));
|
400
463
|
worldRef.current = world;
|
@@ -402,12 +465,12 @@ const Physics = ({
|
|
402
465
|
|
403
466
|
return worldRef.current;
|
404
467
|
});
|
405
|
-
const [colliderMeshes] = React.useState(() => new Map());
|
406
|
-
const [
|
407
|
-
const [rigidBodyEvents] = React.useState(() => new Map());
|
408
|
-
const [eventQueue] = React.useState(() => new rapier3dCompat.EventQueue(false)); // Init world
|
468
|
+
const [colliderMeshes] = React$1.useState(() => new Map());
|
469
|
+
const [rigidBodyStates] = React$1.useState(() => new Map());
|
470
|
+
const [rigidBodyEvents] = React$1.useState(() => new Map());
|
471
|
+
const [eventQueue] = React$1.useState(() => new rapier3dCompat.EventQueue(false)); // Init world
|
409
472
|
|
410
|
-
React.useEffect(() => {
|
473
|
+
React$1.useEffect(() => {
|
411
474
|
const world = getWorldRef.current();
|
412
475
|
return () => {
|
413
476
|
if (world) {
|
@@ -417,14 +480,14 @@ const Physics = ({
|
|
417
480
|
};
|
418
481
|
}, []); // Update gravity
|
419
482
|
|
420
|
-
React.useEffect(() => {
|
483
|
+
React$1.useEffect(() => {
|
421
484
|
const world = worldRef.current;
|
422
485
|
|
423
486
|
if (world) {
|
424
487
|
world.gravity = vectorArrayToObject(_gravity);
|
425
488
|
}
|
426
489
|
}, [_gravity]);
|
427
|
-
const time = React.useRef(performance.now());
|
490
|
+
const time = React$1.useRef(performance.now());
|
428
491
|
fiber.useFrame(context => {
|
429
492
|
const world = worldRef.current;
|
430
493
|
if (!world) return; // Set timestep to current delta, to allow for variable frame rates
|
@@ -433,7 +496,7 @@ const Physics = ({
|
|
433
496
|
const now = performance.now();
|
434
497
|
const delta = Math.min(100, now - time.current);
|
435
498
|
|
436
|
-
if (_timeStep ===
|
499
|
+
if (_timeStep === "vary") {
|
437
500
|
world.timestep = delta / 1000;
|
438
501
|
} else {
|
439
502
|
world.timestep = _timeStep;
|
@@ -441,52 +504,35 @@ const Physics = ({
|
|
441
504
|
|
442
505
|
world.step(eventQueue); // Update meshes
|
443
506
|
|
444
|
-
|
507
|
+
rigidBodyStates.forEach((state, handle) => {
|
445
508
|
const rigidBody = world.getRigidBody(handle);
|
446
509
|
const events = rigidBodyEvents.get(handle);
|
447
510
|
|
448
511
|
if (events !== null && events !== void 0 && events.onSleep || events !== null && events !== void 0 && events.onWake) {
|
449
|
-
if (rigidBody.isSleeping() && !
|
512
|
+
if (rigidBody.isSleeping() && !state.isSleeping) {
|
450
513
|
var _events$onSleep;
|
451
514
|
|
452
515
|
events === null || events === void 0 ? void 0 : (_events$onSleep = events.onSleep) === null || _events$onSleep === void 0 ? void 0 : _events$onSleep.call(events);
|
453
516
|
}
|
454
517
|
|
455
|
-
if (!rigidBody.isSleeping() &&
|
518
|
+
if (!rigidBody.isSleeping() && state.isSleeping) {
|
456
519
|
var _events$onWake;
|
457
520
|
|
458
521
|
events === null || events === void 0 ? void 0 : (_events$onWake = events.onWake) === null || _events$onWake === void 0 ? void 0 : _events$onWake.call(events);
|
459
522
|
}
|
460
523
|
|
461
|
-
|
524
|
+
state.isSleeping = rigidBody.isSleeping();
|
462
525
|
}
|
463
526
|
|
464
|
-
if (!rigidBody || rigidBody.isSleeping() || rigidBody.isFixed() || !
|
527
|
+
if (!rigidBody || rigidBody.isSleeping() || rigidBody.isFixed() || !state.setMatrix) {
|
465
528
|
return;
|
466
529
|
}
|
467
530
|
|
468
|
-
|
469
|
-
|
470
|
-
|
471
|
-
|
472
|
-
}
|
473
|
-
const {
|
474
|
-
x: rx,
|
475
|
-
y: ry,
|
476
|
-
z: rz,
|
477
|
-
w: rw
|
478
|
-
} = rigidBody.rotation();
|
479
|
-
const scale = mesh.getWorldScale(new three.Vector3()); // haha matrixes I have no idea what I'm doing :)
|
480
|
-
|
481
|
-
const o = new three.Object3D();
|
482
|
-
o.position.set(x, y, z);
|
483
|
-
o.rotation.setFromQuaternion(new three.Quaternion(rx, ry, rz, rw));
|
484
|
-
o.scale.set(scale.x, scale.y, scale.z);
|
485
|
-
o.updateMatrix();
|
486
|
-
o.applyMatrix4(mesh.parent.matrixWorld.clone().invert());
|
487
|
-
o.updateMatrix();
|
488
|
-
mesh.position.setFromMatrixPosition(o.matrix);
|
489
|
-
mesh.rotation.setFromRotationMatrix(o.matrix);
|
531
|
+
state.setMatrix(_matrix4.compose(rapierVector3ToVector3(rigidBody.translation()), rapierQuaternionToQuaternion(rigidBody.rotation()), state.worldScale).premultiply(state.invertedMatrixWorld));
|
532
|
+
|
533
|
+
if (state.mesh instanceof three.InstancedMesh) {
|
534
|
+
state.mesh.instanceMatrix.needsUpdate = true;
|
535
|
+
}
|
490
536
|
}); // Collision events
|
491
537
|
|
492
538
|
eventQueue.drainCollisionEvents((handle1, handle2, started) => {
|
@@ -534,8 +580,8 @@ const Physics = ({
|
|
534
580
|
});
|
535
581
|
time.current = now;
|
536
582
|
});
|
537
|
-
const api = React.useMemo(() => createWorldApi(getWorldRef), []);
|
538
|
-
const context = React.useMemo(() => ({
|
583
|
+
const api = React$1.useMemo(() => createWorldApi(getWorldRef), []);
|
584
|
+
const context = React$1.useMemo(() => ({
|
539
585
|
rapier,
|
540
586
|
world: api,
|
541
587
|
physicsOptions: {
|
@@ -543,7 +589,7 @@ const Physics = ({
|
|
543
589
|
gravity: _gravity
|
544
590
|
},
|
545
591
|
colliderMeshes,
|
546
|
-
|
592
|
+
rigidBodyStates,
|
547
593
|
rigidBodyEvents
|
548
594
|
}), []);
|
549
595
|
return /*#__PURE__*/React__default["default"].createElement(RapierContext.Provider, {
|
@@ -551,6 +597,40 @@ const Physics = ({
|
|
551
597
|
}, children);
|
552
598
|
};
|
553
599
|
|
600
|
+
function _objectWithoutPropertiesLoose(source, excluded) {
|
601
|
+
if (source == null) return {};
|
602
|
+
var target = {};
|
603
|
+
var sourceKeys = Object.keys(source);
|
604
|
+
var key, i;
|
605
|
+
|
606
|
+
for (i = 0; i < sourceKeys.length; i++) {
|
607
|
+
key = sourceKeys[i];
|
608
|
+
if (excluded.indexOf(key) >= 0) continue;
|
609
|
+
target[key] = source[key];
|
610
|
+
}
|
611
|
+
|
612
|
+
return target;
|
613
|
+
}
|
614
|
+
|
615
|
+
function _objectWithoutProperties(source, excluded) {
|
616
|
+
if (source == null) return {};
|
617
|
+
var target = _objectWithoutPropertiesLoose(source, excluded);
|
618
|
+
var key, i;
|
619
|
+
|
620
|
+
if (Object.getOwnPropertySymbols) {
|
621
|
+
var sourceSymbolKeys = Object.getOwnPropertySymbols(source);
|
622
|
+
|
623
|
+
for (i = 0; i < sourceSymbolKeys.length; i++) {
|
624
|
+
key = sourceSymbolKeys[i];
|
625
|
+
if (excluded.indexOf(key) >= 0) continue;
|
626
|
+
if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
|
627
|
+
target[key] = source[key];
|
628
|
+
}
|
629
|
+
}
|
630
|
+
|
631
|
+
return target;
|
632
|
+
}
|
633
|
+
|
554
634
|
function _defineProperty(obj, key, value) {
|
555
635
|
if (key in obj) {
|
556
636
|
Object.defineProperty(obj, key, {
|
@@ -593,38 +673,21 @@ function _objectSpread2(target) {
|
|
593
673
|
}
|
594
674
|
|
595
675
|
const useRapier = () => {
|
596
|
-
return React.useContext(RapierContext);
|
676
|
+
return React$1.useContext(RapierContext);
|
597
677
|
};
|
598
678
|
const useRigidBody = (options = {}) => {
|
599
679
|
const {
|
600
|
-
rapier,
|
601
680
|
world,
|
602
|
-
|
681
|
+
rigidBodyStates,
|
603
682
|
physicsOptions,
|
604
683
|
rigidBodyEvents
|
605
684
|
} = useRapier();
|
606
|
-
const ref = React.useRef(); // Create rigidbody
|
685
|
+
const ref = React$1.useRef(); // Create rigidbody
|
607
686
|
|
608
|
-
const rigidBodyRef = React.useRef();
|
609
|
-
const getRigidBodyRef = React.useRef(() => {
|
687
|
+
const rigidBodyRef = React$1.useRef();
|
688
|
+
const getRigidBodyRef = React$1.useRef(() => {
|
610
689
|
if (!rigidBodyRef.current) {
|
611
|
-
|
612
|
-
|
613
|
-
const type = rigidBodyTypeFromString((options === null || options === void 0 ? void 0 : options.type) || "dynamic");
|
614
|
-
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];
|
615
|
-
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];
|
616
|
-
const gravityScale = (_options$gravityScale = options === null || options === void 0 ? void 0 : options.gravityScale) !== null && _options$gravityScale !== void 0 ? _options$gravityScale : 1;
|
617
|
-
const canSleep = (_options$canSleep = options === null || options === void 0 ? void 0 : options.canSleep) !== null && _options$canSleep !== void 0 ? _options$canSleep : true;
|
618
|
-
const ccdEnabled = (_options$ccd = options === null || options === void 0 ? void 0 : options.ccd) !== null && _options$ccd !== void 0 ? _options$ccd : false;
|
619
|
-
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];
|
620
|
-
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];
|
621
|
-
const desc = new rapier.RigidBodyDesc(type).setLinvel(lvx, lvy, lvz).setAngvel({
|
622
|
-
x: avx,
|
623
|
-
y: avy,
|
624
|
-
z: avz
|
625
|
-
}).setGravityScale(gravityScale).setCanSleep(canSleep).setCcdEnabled(ccdEnabled).enabledRotations(erx, ery, erz).enabledTranslations(etx, ety, etz);
|
626
|
-
if (options.lockRotations) desc.lockRotations();
|
627
|
-
if (options.lockTranslations) desc.lockTranslations();
|
690
|
+
const desc = rigidBodyDescFromOptions(options);
|
628
691
|
const rigidBody = world.createRigidBody(desc);
|
629
692
|
rigidBodyRef.current = world.getRigidBody(rigidBody.handle);
|
630
693
|
}
|
@@ -632,7 +695,7 @@ const useRigidBody = (options = {}) => {
|
|
632
695
|
return rigidBodyRef.current;
|
633
696
|
}); // Setup
|
634
697
|
|
635
|
-
React.useEffect(() => {
|
698
|
+
React$1.useEffect(() => {
|
636
699
|
var _ref$current$parent, _ref, _options$colliders;
|
637
700
|
|
638
701
|
const rigidBody = getRigidBodyRef.current();
|
@@ -661,8 +724,7 @@ const useRigidBody = (options = {}) => {
|
|
661
724
|
y: worldPosition.y + y * scale.y,
|
662
725
|
z: worldPosition.z + z * scale.z
|
663
726
|
}, false);
|
664
|
-
const
|
665
|
-
const rotation = new three.Quaternion().setFromEuler(eulerAngles).multiply(worldRotation);
|
727
|
+
const rotation = vector3ToQuaternion(new three.Vector3(rx, ry, rz)).multiply(worldRotation);
|
666
728
|
rigidBody.setRotation({
|
667
729
|
x: rotation.x,
|
668
730
|
y: rotation.y,
|
@@ -675,16 +737,23 @@ const useRigidBody = (options = {}) => {
|
|
675
737
|
const autoColliders = colliderSetting !== false ? createCollidersFromChildren(ref.current, rigidBody, _objectSpread2(_objectSpread2({}, options), {}, {
|
676
738
|
colliders: colliderSetting
|
677
739
|
}), world) : [];
|
678
|
-
|
740
|
+
rigidBodyStates.set(rigidBody.handle, {
|
741
|
+
mesh: ref.current,
|
742
|
+
invertedMatrixWorld: ref.current.parent.matrixWorld.clone().invert(),
|
743
|
+
isSleeping: false,
|
744
|
+
worldScale: ref.current.getWorldScale(_vector3).clone(),
|
745
|
+
setMatrix: mat => ref.current.matrix.copy(mat)
|
746
|
+
});
|
747
|
+
ref.current.matrixAutoUpdate = false;
|
679
748
|
return () => {
|
680
749
|
world.removeRigidBody(rigidBody);
|
681
750
|
autoColliders.forEach(collider => world.removeCollider(collider));
|
682
751
|
rigidBodyRef.current = undefined;
|
683
|
-
|
752
|
+
rigidBodyStates.delete(rigidBody.handle);
|
684
753
|
};
|
685
754
|
}, []); // Events
|
686
755
|
|
687
|
-
React.useEffect(() => {
|
756
|
+
React$1.useEffect(() => {
|
688
757
|
const rigidBody = getRigidBodyRef.current();
|
689
758
|
rigidBodyEvents.set(rigidBody.handle, {
|
690
759
|
onCollisionEnter: options === null || options === void 0 ? void 0 : options.onCollisionEnter,
|
@@ -696,44 +765,21 @@ const useRigidBody = (options = {}) => {
|
|
696
765
|
rigidBodyEvents.delete(rigidBody.handle);
|
697
766
|
};
|
698
767
|
}, [options.onCollisionEnter, options.onCollisionExit]);
|
699
|
-
const api = React.useMemo(() => createRigidBodyApi(getRigidBodyRef), []);
|
768
|
+
const api = React$1.useMemo(() => createRigidBodyApi(getRigidBodyRef), []);
|
700
769
|
return [ref, api];
|
701
|
-
};
|
702
|
-
const useCollider = (body, options = {}) => {
|
703
|
-
const {
|
704
|
-
world
|
705
|
-
} = useRapier();
|
706
|
-
const colliderRef = React.useRef();
|
707
|
-
const objectRef = React.useRef();
|
708
|
-
const getColliderRef = React.useRef(() => {
|
709
|
-
if (!colliderRef.current) {
|
710
|
-
colliderRef.current = createColliderFromOptions(options, world, world.getRigidBody(body.handle));
|
711
|
-
}
|
712
|
-
|
713
|
-
return colliderRef.current;
|
714
|
-
});
|
715
|
-
React.useEffect(() => {
|
716
|
-
const collider = getColliderRef.current();
|
717
|
-
return () => {
|
718
|
-
if (collider) world.removeCollider(collider);
|
719
|
-
colliderRef.current = undefined;
|
720
|
-
};
|
721
|
-
}, []);
|
722
|
-
const api = React.useMemo(() => createColliderApi(getColliderRef), []);
|
723
|
-
return [objectRef, api];
|
724
770
|
}; // Joints
|
725
771
|
|
726
772
|
const useImpulseJoint = (body1, body2, params) => {
|
727
773
|
const {
|
728
774
|
world
|
729
775
|
} = useRapier();
|
730
|
-
const jointRef = React.useRef();
|
731
|
-
const getJointRef = React.useRef(() => {
|
776
|
+
const jointRef = React$1.useRef();
|
777
|
+
const getJointRef = React$1.useRef(() => {
|
732
778
|
if (!jointRef.current) {
|
733
779
|
let rb1;
|
734
780
|
let rb2;
|
735
781
|
|
736
|
-
if (
|
782
|
+
if ("current" in body1 && body1.current && "current" in body2 && body2.current) {
|
737
783
|
rb1 = world.getRigidBody(body1.current.handle);
|
738
784
|
rb2 = world.getRigidBody(body2.current.handle);
|
739
785
|
const newJoint = world.createImpulseJoint(params, rb1, rb2);
|
@@ -743,7 +789,7 @@ const useImpulseJoint = (body1, body2, params) => {
|
|
743
789
|
|
744
790
|
return jointRef.current;
|
745
791
|
});
|
746
|
-
React.useEffect(() => {
|
792
|
+
React$1.useEffect(() => {
|
747
793
|
const joint = getJointRef.current();
|
748
794
|
return () => {
|
749
795
|
if (joint) {
|
@@ -752,7 +798,7 @@ const useImpulseJoint = (body1, body2, params) => {
|
|
752
798
|
}
|
753
799
|
};
|
754
800
|
}, []);
|
755
|
-
const api = React.useMemo(() => createJointApi(getJointRef), []);
|
801
|
+
const api = React$1.useMemo(() => createJointApi(getJointRef), []);
|
756
802
|
return api;
|
757
803
|
};
|
758
804
|
/**
|
@@ -810,73 +856,18 @@ const usePrismaticJoint = (body1, body2, [body1Anchor, body2Anchor, axis]) => {
|
|
810
856
|
return useImpulseJoint(body1, body2, rapier.JointData.prismatic(vectorArrayToObject(body1Anchor), vectorArrayToObject(body2Anchor), vectorArrayToObject(axis)));
|
811
857
|
};
|
812
858
|
|
813
|
-
|
814
|
-
|
815
|
-
|
816
|
-
var source = arguments[i];
|
817
|
-
|
818
|
-
for (var key in source) {
|
819
|
-
if (Object.prototype.hasOwnProperty.call(source, key)) {
|
820
|
-
target[key] = source[key];
|
821
|
-
}
|
822
|
-
}
|
823
|
-
}
|
824
|
-
|
825
|
-
return target;
|
826
|
-
};
|
859
|
+
const _excluded$1 = ["children"];
|
860
|
+
const RigidBodyContext = /*#__PURE__*/React$1.createContext(undefined);
|
861
|
+
const useRigidBodyContext = () => React$1.useContext(RigidBodyContext); // RigidBody
|
827
862
|
|
828
|
-
|
829
|
-
}
|
830
|
-
|
831
|
-
function _objectWithoutPropertiesLoose(source, excluded) {
|
832
|
-
if (source == null) return {};
|
833
|
-
var target = {};
|
834
|
-
var sourceKeys = Object.keys(source);
|
835
|
-
var key, i;
|
836
|
-
|
837
|
-
for (i = 0; i < sourceKeys.length; i++) {
|
838
|
-
key = sourceKeys[i];
|
839
|
-
if (excluded.indexOf(key) >= 0) continue;
|
840
|
-
target[key] = source[key];
|
841
|
-
}
|
842
|
-
|
843
|
-
return target;
|
844
|
-
}
|
845
|
-
|
846
|
-
function _objectWithoutProperties(source, excluded) {
|
847
|
-
if (source == null) return {};
|
848
|
-
var target = _objectWithoutPropertiesLoose(source, excluded);
|
849
|
-
var key, i;
|
850
|
-
|
851
|
-
if (Object.getOwnPropertySymbols) {
|
852
|
-
var sourceSymbolKeys = Object.getOwnPropertySymbols(source);
|
853
|
-
|
854
|
-
for (i = 0; i < sourceSymbolKeys.length; i++) {
|
855
|
-
key = sourceSymbolKeys[i];
|
856
|
-
if (excluded.indexOf(key) >= 0) continue;
|
857
|
-
if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
|
858
|
-
target[key] = source[key];
|
859
|
-
}
|
860
|
-
}
|
861
|
-
|
862
|
-
return target;
|
863
|
-
}
|
864
|
-
|
865
|
-
const _excluded = ["children"],
|
866
|
-
_excluded2 = ["children"];
|
867
|
-
const RigidBodyContext = /*#__PURE__*/React.createContext(undefined);
|
868
|
-
|
869
|
-
const useRigidBodyContext = () => React.useContext(RigidBodyContext); // RigidBody
|
870
|
-
|
871
|
-
|
872
|
-
const RigidBody = /*#__PURE__*/React.forwardRef((_ref, ref) => {
|
863
|
+
const RigidBody = /*#__PURE__*/React$1.forwardRef((_ref, ref) => {
|
873
864
|
let {
|
874
865
|
children
|
875
866
|
} = _ref,
|
876
|
-
props = _objectWithoutProperties(_ref, _excluded);
|
867
|
+
props = _objectWithoutProperties(_ref, _excluded$1);
|
877
868
|
|
878
869
|
const [object, api] = useRigidBody(props);
|
879
|
-
React.useImperativeHandle(ref, () => api);
|
870
|
+
React$1.useImperativeHandle(ref, () => api);
|
880
871
|
return /*#__PURE__*/React__default["default"].createElement(RigidBodyContext.Provider, {
|
881
872
|
value: {
|
882
873
|
ref: object,
|
@@ -888,6 +879,7 @@ const RigidBody = /*#__PURE__*/React.forwardRef((_ref, ref) => {
|
|
888
879
|
ref: object
|
889
880
|
}, children));
|
890
881
|
});
|
882
|
+
|
891
883
|
const MeshCollider = ({
|
892
884
|
children,
|
893
885
|
type
|
@@ -896,18 +888,18 @@ const MeshCollider = ({
|
|
896
888
|
physicsOptions,
|
897
889
|
world
|
898
890
|
} = useRapier();
|
899
|
-
const object = React.useRef(null);
|
891
|
+
const object = React$1.useRef(null);
|
900
892
|
const {
|
901
893
|
api,
|
902
894
|
options
|
903
895
|
} = useRigidBodyContext();
|
904
|
-
React.useEffect(() => {
|
896
|
+
React$1.useEffect(() => {
|
905
897
|
let autoColliders = [];
|
906
898
|
|
907
899
|
if (object.current) {
|
908
|
-
var
|
900
|
+
var _ref;
|
909
901
|
|
910
|
-
const colliderSetting = (
|
902
|
+
const colliderSetting = (_ref = type !== null && type !== void 0 ? type : physicsOptions.colliders) !== null && _ref !== void 0 ? _ref : false;
|
911
903
|
autoColliders = colliderSetting !== false ? createCollidersFromChildren(object.current, api, _objectSpread2(_objectSpread2({}, options), {}, {
|
912
904
|
colliders: colliderSetting
|
913
905
|
}), world, false) : [];
|
@@ -919,89 +911,12 @@ const MeshCollider = ({
|
|
919
911
|
});
|
920
912
|
};
|
921
913
|
}, []);
|
922
|
-
return /*#__PURE__*/
|
914
|
+
return /*#__PURE__*/React.createElement("object3D", {
|
923
915
|
ref: object,
|
924
916
|
userData: {
|
925
917
|
r3RapierType: "MeshCollider"
|
926
918
|
}
|
927
919
|
}, children);
|
928
|
-
}; // Colliders
|
929
|
-
|
930
|
-
const AnyCollider = _ref3 => {
|
931
|
-
let {
|
932
|
-
children
|
933
|
-
} = _ref3,
|
934
|
-
props = _objectWithoutProperties(_ref3, _excluded2);
|
935
|
-
|
936
|
-
const {
|
937
|
-
world
|
938
|
-
} = useRapier();
|
939
|
-
const rigidBodyContext = useRigidBodyContext();
|
940
|
-
const ref = React.useRef(null);
|
941
|
-
React.useEffect(() => {
|
942
|
-
var _rigidBodyContext$api;
|
943
|
-
|
944
|
-
const scale = ref.current.getWorldScale(new three.Vector3());
|
945
|
-
const collider = createColliderFromOptions({
|
946
|
-
options: props,
|
947
|
-
world,
|
948
|
-
rigidBody: rigidBodyContext === null || rigidBodyContext === void 0 ? void 0 : (_rigidBodyContext$api = rigidBodyContext.api) === null || _rigidBodyContext$api === void 0 ? void 0 : _rigidBodyContext$api.raw(),
|
949
|
-
scale,
|
950
|
-
hasCollisionEvents: rigidBodyContext === null || rigidBodyContext === void 0 ? void 0 : rigidBodyContext.hasCollisionEvents
|
951
|
-
});
|
952
|
-
return () => {
|
953
|
-
world.removeCollider(collider);
|
954
|
-
};
|
955
|
-
}, []);
|
956
|
-
return /*#__PURE__*/React__default["default"].createElement("object3D", {
|
957
|
-
ref: ref
|
958
|
-
}, children);
|
959
|
-
};
|
960
|
-
|
961
|
-
const CuboidCollider = props => {
|
962
|
-
return /*#__PURE__*/React__default["default"].createElement(AnyCollider, _extends({}, props, {
|
963
|
-
shape: "cuboid"
|
964
|
-
}));
|
965
|
-
};
|
966
|
-
const RoundCuboidCollider = props => {
|
967
|
-
return /*#__PURE__*/React__default["default"].createElement(AnyCollider, _extends({}, props, {
|
968
|
-
shape: "roundCuboid"
|
969
|
-
}));
|
970
|
-
};
|
971
|
-
const BallCollider = props => {
|
972
|
-
return /*#__PURE__*/React__default["default"].createElement(AnyCollider, _extends({}, props, {
|
973
|
-
shape: "ball"
|
974
|
-
}));
|
975
|
-
};
|
976
|
-
const CapsuleCollider = props => {
|
977
|
-
return /*#__PURE__*/React__default["default"].createElement(AnyCollider, _extends({}, props, {
|
978
|
-
shape: "capsule"
|
979
|
-
}));
|
980
|
-
};
|
981
|
-
const HeightfieldCollider = props => {
|
982
|
-
return /*#__PURE__*/React__default["default"].createElement(AnyCollider, _extends({}, props, {
|
983
|
-
shape: "heightfield"
|
984
|
-
}));
|
985
|
-
};
|
986
|
-
const TrimeshCollider = props => {
|
987
|
-
return /*#__PURE__*/React__default["default"].createElement(AnyCollider, _extends({}, props, {
|
988
|
-
shape: "trimesh"
|
989
|
-
}));
|
990
|
-
};
|
991
|
-
const ConeCollider = props => {
|
992
|
-
return /*#__PURE__*/React__default["default"].createElement(AnyCollider, _extends({}, props, {
|
993
|
-
shape: "cone"
|
994
|
-
}));
|
995
|
-
};
|
996
|
-
const CylinderCollider = props => {
|
997
|
-
return /*#__PURE__*/React__default["default"].createElement(AnyCollider, _extends({}, props, {
|
998
|
-
shape: "cylinder"
|
999
|
-
}));
|
1000
|
-
};
|
1001
|
-
const ConvexHullCollider = props => {
|
1002
|
-
return /*#__PURE__*/React__default["default"].createElement(AnyCollider, _extends({}, props, {
|
1003
|
-
shape: "convexHull"
|
1004
|
-
}));
|
1005
920
|
};
|
1006
921
|
|
1007
922
|
const geometryFromCollider = collider => {
|
@@ -1059,13 +974,13 @@ const geometryFromCollider = collider => {
|
|
1059
974
|
return new three.BoxBufferGeometry(1, 1, 1);
|
1060
975
|
};
|
1061
976
|
|
1062
|
-
const DebugShape = /*#__PURE__*/React.memo(({
|
977
|
+
const DebugShape = /*#__PURE__*/React$1.memo(({
|
1063
978
|
colliderHandle
|
1064
979
|
}) => {
|
1065
980
|
const {
|
1066
981
|
world
|
1067
982
|
} = useRapier();
|
1068
|
-
const ref = React.useRef(null);
|
983
|
+
const ref = React$1.useRef(null);
|
1069
984
|
fiber.useFrame(() => {
|
1070
985
|
const collider = world.getCollider(colliderHandle);
|
1071
986
|
|
@@ -1085,7 +1000,7 @@ const DebugShape = /*#__PURE__*/React.memo(({
|
|
1085
1000
|
ref.current.rotation.setFromQuaternion(new three.Quaternion(rx, ry, rz, rw));
|
1086
1001
|
}
|
1087
1002
|
});
|
1088
|
-
const geometry = React.useMemo(() => {
|
1003
|
+
const geometry = React$1.useMemo(() => {
|
1089
1004
|
const collider = world.getCollider(colliderHandle);
|
1090
1005
|
return geometryFromCollider(collider);
|
1091
1006
|
}, [colliderHandle]);
|
@@ -1103,8 +1018,8 @@ const Debug = () => {
|
|
1103
1018
|
const {
|
1104
1019
|
world
|
1105
1020
|
} = useRapier();
|
1106
|
-
const [colliders, setColliders] = React.useState([]);
|
1107
|
-
React.useRef({});
|
1021
|
+
const [colliders, setColliders] = React$1.useState([]);
|
1022
|
+
React$1.useRef({});
|
1108
1023
|
|
1109
1024
|
fiber.useFrame(() => {
|
1110
1025
|
const newColliders = [];
|
@@ -1119,6 +1034,200 @@ const Debug = () => {
|
|
1119
1034
|
})));
|
1120
1035
|
};
|
1121
1036
|
|
1037
|
+
const InstancedRigidBodies = /*#__PURE__*/React$1.forwardRef((props, ref) => {
|
1038
|
+
const {
|
1039
|
+
world,
|
1040
|
+
rigidBodyStates,
|
1041
|
+
physicsOptions
|
1042
|
+
} = useRapier();
|
1043
|
+
const object = React$1.useRef(null);
|
1044
|
+
const instancesRef = React$1.useRef();
|
1045
|
+
const instancesRefGetter = React$1.useRef(() => {
|
1046
|
+
if (!instancesRef.current) {
|
1047
|
+
instancesRef.current = [];
|
1048
|
+
}
|
1049
|
+
|
1050
|
+
return instancesRef.current;
|
1051
|
+
});
|
1052
|
+
React$1.useEffect(() => {
|
1053
|
+
const colliders = [];
|
1054
|
+
const rigidBodies = instancesRefGetter.current();
|
1055
|
+
|
1056
|
+
if (object.current) {
|
1057
|
+
const scale = object.current.getWorldScale(new three.Vector3());
|
1058
|
+
let hasOneMesh = false;
|
1059
|
+
object.current.traverse(mesh => {
|
1060
|
+
if (mesh instanceof three.InstancedMesh) {
|
1061
|
+
if (hasOneMesh) {
|
1062
|
+
console.warn("Can only use a single InstancedMesh inside <InstancedRigidBodies />, more InstancedMeshes will be ignored.");
|
1063
|
+
return;
|
1064
|
+
}
|
1065
|
+
|
1066
|
+
hasOneMesh = true;
|
1067
|
+
mesh.instanceMatrix.setUsage(three.DynamicDrawUsage);
|
1068
|
+
const rigidBodyDesc = rigidBodyDescFromOptions(props);
|
1069
|
+
const colliderDesc = colliderDescFromGeometry(mesh.geometry, props.colliders || physicsOptions.colliders, scale, false // Collisions currently not enabled for instances
|
1070
|
+
);
|
1071
|
+
|
1072
|
+
for (let index = 0; index < mesh.count; index++) {
|
1073
|
+
const rigidBody = world.createRigidBody(rigidBodyDesc);
|
1074
|
+
const matrix = new three.Matrix4();
|
1075
|
+
mesh.getMatrixAt(index, matrix);
|
1076
|
+
const {
|
1077
|
+
position,
|
1078
|
+
rotation
|
1079
|
+
} = decomposeMatrix4(matrix); // Set positions
|
1080
|
+
|
1081
|
+
if (props.positions && props.positions[index]) {
|
1082
|
+
rigidBody.setTranslation(vectorArrayToObject(props.positions[index]), true);
|
1083
|
+
} else {
|
1084
|
+
rigidBody.setTranslation(position, true);
|
1085
|
+
} // Set rotations
|
1086
|
+
|
1087
|
+
|
1088
|
+
if (props.rotations && props.rotations[index]) {
|
1089
|
+
const [x, y, z] = props.rotations[index];
|
1090
|
+
rigidBody.setRotation(vector3ToQuaternion(new three.Vector3(x, y, z)), true);
|
1091
|
+
} else {
|
1092
|
+
rigidBody.setRotation(rotation, true);
|
1093
|
+
}
|
1094
|
+
|
1095
|
+
const collider = world.createCollider(colliderDesc, rigidBody);
|
1096
|
+
rigidBodyStates.set(rigidBody.handle, {
|
1097
|
+
mesh: mesh,
|
1098
|
+
isSleeping: false,
|
1099
|
+
invertedMatrixWorld: object.current.matrixWorld.clone().invert(),
|
1100
|
+
setMatrix: matrix => mesh.setMatrixAt(index, matrix),
|
1101
|
+
worldScale: object.current.getWorldScale(new three.Vector3())
|
1102
|
+
});
|
1103
|
+
const api = createRigidBodyApi({
|
1104
|
+
current() {
|
1105
|
+
return rigidBody;
|
1106
|
+
}
|
1107
|
+
|
1108
|
+
});
|
1109
|
+
colliders.push(collider);
|
1110
|
+
rigidBodies.push({
|
1111
|
+
rigidBody,
|
1112
|
+
api
|
1113
|
+
});
|
1114
|
+
}
|
1115
|
+
}
|
1116
|
+
|
1117
|
+
if (mesh.type === "Mesh" && !("isInstancedMesh" in mesh)) {
|
1118
|
+
console.warn("Can only use InstancedMesh inside <InstancedRigidBodies />, Mesh will be ignored.");
|
1119
|
+
}
|
1120
|
+
});
|
1121
|
+
return () => {
|
1122
|
+
rigidBodies.forEach(rb => world.removeRigidBody(rb.rigidBody));
|
1123
|
+
colliders.forEach(coll => world.removeCollider(coll));
|
1124
|
+
instancesRef.current = undefined;
|
1125
|
+
};
|
1126
|
+
}
|
1127
|
+
}, []);
|
1128
|
+
React$1.useImperativeHandle(ref, () => createInstancedRigidBodiesApi(instancesRefGetter));
|
1129
|
+
return /*#__PURE__*/React.createElement("object3D", {
|
1130
|
+
ref: object
|
1131
|
+
}, props.children);
|
1132
|
+
});
|
1133
|
+
|
1134
|
+
function _extends() {
|
1135
|
+
_extends = Object.assign || function (target) {
|
1136
|
+
for (var i = 1; i < arguments.length; i++) {
|
1137
|
+
var source = arguments[i];
|
1138
|
+
|
1139
|
+
for (var key in source) {
|
1140
|
+
if (Object.prototype.hasOwnProperty.call(source, key)) {
|
1141
|
+
target[key] = source[key];
|
1142
|
+
}
|
1143
|
+
}
|
1144
|
+
}
|
1145
|
+
|
1146
|
+
return target;
|
1147
|
+
};
|
1148
|
+
|
1149
|
+
return _extends.apply(this, arguments);
|
1150
|
+
}
|
1151
|
+
|
1152
|
+
const _excluded = ["children"];
|
1153
|
+
|
1154
|
+
const AnyCollider = _ref => {
|
1155
|
+
let {
|
1156
|
+
children
|
1157
|
+
} = _ref,
|
1158
|
+
props = _objectWithoutProperties(_ref, _excluded);
|
1159
|
+
|
1160
|
+
const {
|
1161
|
+
world
|
1162
|
+
} = useRapier();
|
1163
|
+
const rigidBodyContext = useRigidBodyContext();
|
1164
|
+
const ref = React$1.useRef(null);
|
1165
|
+
React$1.useEffect(() => {
|
1166
|
+
var _rigidBodyContext$api;
|
1167
|
+
|
1168
|
+
const scale = ref.current.getWorldScale(new three.Vector3());
|
1169
|
+
const collider = createColliderFromOptions({
|
1170
|
+
options: props,
|
1171
|
+
world,
|
1172
|
+
rigidBody: rigidBodyContext === null || rigidBodyContext === void 0 ? void 0 : (_rigidBodyContext$api = rigidBodyContext.api) === null || _rigidBodyContext$api === void 0 ? void 0 : _rigidBodyContext$api.raw(),
|
1173
|
+
scale,
|
1174
|
+
hasCollisionEvents: rigidBodyContext === null || rigidBodyContext === void 0 ? void 0 : rigidBodyContext.hasCollisionEvents
|
1175
|
+
});
|
1176
|
+
return () => {
|
1177
|
+
world.removeCollider(collider);
|
1178
|
+
};
|
1179
|
+
}, []);
|
1180
|
+
return /*#__PURE__*/React.createElement("object3D", {
|
1181
|
+
ref: ref
|
1182
|
+
}, children);
|
1183
|
+
};
|
1184
|
+
|
1185
|
+
const CuboidCollider = props => {
|
1186
|
+
return /*#__PURE__*/React.createElement(AnyCollider, _extends({}, props, {
|
1187
|
+
shape: "cuboid"
|
1188
|
+
}));
|
1189
|
+
};
|
1190
|
+
const RoundCuboidCollider = props => {
|
1191
|
+
return /*#__PURE__*/React.createElement(AnyCollider, _extends({}, props, {
|
1192
|
+
shape: "roundCuboid"
|
1193
|
+
}));
|
1194
|
+
};
|
1195
|
+
const BallCollider = props => {
|
1196
|
+
return /*#__PURE__*/React.createElement(AnyCollider, _extends({}, props, {
|
1197
|
+
shape: "ball"
|
1198
|
+
}));
|
1199
|
+
};
|
1200
|
+
const CapsuleCollider = props => {
|
1201
|
+
return /*#__PURE__*/React.createElement(AnyCollider, _extends({}, props, {
|
1202
|
+
shape: "capsule"
|
1203
|
+
}));
|
1204
|
+
};
|
1205
|
+
const HeightfieldCollider = props => {
|
1206
|
+
return /*#__PURE__*/React.createElement(AnyCollider, _extends({}, props, {
|
1207
|
+
shape: "heightfield"
|
1208
|
+
}));
|
1209
|
+
};
|
1210
|
+
const TrimeshCollider = props => {
|
1211
|
+
return /*#__PURE__*/React.createElement(AnyCollider, _extends({}, props, {
|
1212
|
+
shape: "trimesh"
|
1213
|
+
}));
|
1214
|
+
};
|
1215
|
+
const ConeCollider = props => {
|
1216
|
+
return /*#__PURE__*/React.createElement(AnyCollider, _extends({}, props, {
|
1217
|
+
shape: "cone"
|
1218
|
+
}));
|
1219
|
+
};
|
1220
|
+
const CylinderCollider = props => {
|
1221
|
+
return /*#__PURE__*/React.createElement(AnyCollider, _extends({}, props, {
|
1222
|
+
shape: "cylinder"
|
1223
|
+
}));
|
1224
|
+
};
|
1225
|
+
const ConvexHullCollider = props => {
|
1226
|
+
return /*#__PURE__*/React.createElement(AnyCollider, _extends({}, props, {
|
1227
|
+
shape: "convexHull"
|
1228
|
+
}));
|
1229
|
+
};
|
1230
|
+
|
1122
1231
|
Object.defineProperty(exports, 'CoefficientCombineRule', {
|
1123
1232
|
enumerable: true,
|
1124
1233
|
get: function () { return rapier3dCompat.CoefficientCombineRule; }
|
@@ -1139,12 +1248,12 @@ exports.CuboidCollider = CuboidCollider;
|
|
1139
1248
|
exports.CylinderCollider = CylinderCollider;
|
1140
1249
|
exports.Debug = Debug;
|
1141
1250
|
exports.HeightfieldCollider = HeightfieldCollider;
|
1251
|
+
exports.InstancedRigidBodies = InstancedRigidBodies;
|
1142
1252
|
exports.MeshCollider = MeshCollider;
|
1143
1253
|
exports.Physics = Physics;
|
1144
1254
|
exports.RigidBody = RigidBody;
|
1145
1255
|
exports.RoundCuboidCollider = RoundCuboidCollider;
|
1146
1256
|
exports.TrimeshCollider = TrimeshCollider;
|
1147
|
-
exports.useCollider = useCollider;
|
1148
1257
|
exports.useFixedJoint = useFixedJoint;
|
1149
1258
|
exports.useImpulseJoint = useImpulseJoint;
|
1150
1259
|
exports.usePrismaticJoint = usePrismaticJoint;
|