@react-three/rapier 0.4.2 → 0.5.1
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/CHANGELOG.md +28 -0
- package/dist/declarations/src/Physics.d.ts +23 -1
- package/dist/declarations/src/api.d.ts +18 -1
- package/dist/declarations/src/components.d.ts +6 -1
- package/dist/declarations/src/hooks.d.ts +3 -35
- package/dist/declarations/src/types.d.ts +20 -3
- package/dist/declarations/src/utils.d.ts +5 -4
- package/dist/react-three-rapier.cjs.dev.js +168 -220
- package/dist/react-three-rapier.cjs.prod.js +168 -220
- package/dist/react-three-rapier.esm.js +169 -206
- package/package.json +2 -2
- package/readme.md +25 -19
@@ -113,13 +113,23 @@ const createColliderFromOptions = (options, world, rigidBody, scale = {
|
|
113
113
|
const collider = world.createCollider(colliderDesc, rigidBody);
|
114
114
|
return collider;
|
115
115
|
};
|
116
|
-
|
116
|
+
|
117
|
+
const isChildOfMeshCollider = child => {
|
118
|
+
let flag = false;
|
119
|
+
child.traverseAncestors(a => {
|
120
|
+
if (a.userData.r3RapierType === "MeshCollider") flag = true;
|
121
|
+
});
|
122
|
+
return flag;
|
123
|
+
};
|
124
|
+
|
125
|
+
const createCollidersFromChildren = (object, rigidBody, options, world, ignoreMeshColliders = true) => {
|
117
126
|
const hasCollisionEvents = !!(options.onCollisionEnter || options.onCollisionExit);
|
118
127
|
const colliders = [];
|
119
128
|
let desc;
|
120
129
|
let offset = new three.Vector3();
|
121
130
|
object.traverse(child => {
|
122
131
|
if ("isMesh" in child) {
|
132
|
+
if (ignoreMeshColliders && isChildOfMeshCollider(child)) return;
|
123
133
|
const {
|
124
134
|
geometry
|
125
135
|
} = child;
|
@@ -190,7 +200,8 @@ const createCollidersFromChildren = (object, rigidBody, options, world) => {
|
|
190
200
|
if (hasCollisionEvents) desc.setActiveEvents(rapier3dCompat.ActiveEvents.COLLISION_EVENTS);
|
191
201
|
if (Number.isFinite(options.friction)) desc.setFriction(options.friction);
|
192
202
|
if (Number.isFinite(options.restitution)) desc.setRestitution(options.restitution);
|
193
|
-
const
|
203
|
+
const actualRigidBody = world.getRigidBody(rigidBody === null || rigidBody === void 0 ? void 0 : rigidBody.handle);
|
204
|
+
const collider = world.createCollider(desc, actualRigidBody);
|
194
205
|
colliders.push(collider);
|
195
206
|
}
|
196
207
|
});
|
@@ -207,47 +218,11 @@ const scaleVertices = (vertices, scale) => {
|
|
207
218
|
|
208
219
|
return scaledVerts;
|
209
220
|
};
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
enumerable: true,
|
216
|
-
configurable: true,
|
217
|
-
writable: true
|
218
|
-
});
|
219
|
-
} else {
|
220
|
-
obj[key] = value;
|
221
|
-
}
|
222
|
-
|
223
|
-
return obj;
|
224
|
-
}
|
225
|
-
|
226
|
-
function ownKeys(object, enumerableOnly) {
|
227
|
-
var keys = Object.keys(object);
|
228
|
-
|
229
|
-
if (Object.getOwnPropertySymbols) {
|
230
|
-
var symbols = Object.getOwnPropertySymbols(object);
|
231
|
-
enumerableOnly && (symbols = symbols.filter(function (sym) {
|
232
|
-
return Object.getOwnPropertyDescriptor(object, sym).enumerable;
|
233
|
-
})), keys.push.apply(keys, symbols);
|
234
|
-
}
|
235
|
-
|
236
|
-
return keys;
|
237
|
-
}
|
238
|
-
|
239
|
-
function _objectSpread2(target) {
|
240
|
-
for (var i = 1; i < arguments.length; i++) {
|
241
|
-
var source = null != arguments[i] ? arguments[i] : {};
|
242
|
-
i % 2 ? ownKeys(Object(source), !0).forEach(function (key) {
|
243
|
-
_defineProperty(target, key, source[key]);
|
244
|
-
}) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) {
|
245
|
-
Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
|
246
|
-
});
|
247
|
-
}
|
248
|
-
|
249
|
-
return target;
|
250
|
-
}
|
221
|
+
const quaternion = new three.Quaternion();
|
222
|
+
const euler = new three.Euler();
|
223
|
+
const vector3ToQuaternion = v => {
|
224
|
+
return quaternion.setFromEuler(euler.setFromVector3(v));
|
225
|
+
};
|
251
226
|
|
252
227
|
const createRigidBodyApi = ref => {
|
253
228
|
return {
|
@@ -293,9 +268,19 @@ const createRigidBodyApi = ref => {
|
|
293
268
|
return new three.Quaternion(x, y, z, w);
|
294
269
|
},
|
295
270
|
|
296
|
-
setRotation:
|
297
|
-
|
298
|
-
|
271
|
+
setRotation: ({
|
272
|
+
x,
|
273
|
+
y,
|
274
|
+
z
|
275
|
+
}) => {
|
276
|
+
const q = vector3ToQuaternion(new three.Vector3(x, y, z));
|
277
|
+
ref.current().setRotation({
|
278
|
+
x: q.x,
|
279
|
+
y: q.y,
|
280
|
+
z: q.z,
|
281
|
+
w: q.w
|
282
|
+
}, true);
|
283
|
+
},
|
299
284
|
|
300
285
|
linvel() {
|
301
286
|
const {
|
@@ -318,12 +303,26 @@ const createRigidBodyApi = ref => {
|
|
318
303
|
},
|
319
304
|
|
320
305
|
setAngvel: velocity => ref.current().setAngvel(velocity, true),
|
321
|
-
setNextKinematicRotation:
|
322
|
-
|
323
|
-
|
306
|
+
setNextKinematicRotation: ({
|
307
|
+
x,
|
308
|
+
y,
|
309
|
+
z
|
310
|
+
}) => {
|
311
|
+
const q = vector3ToQuaternion(new three.Vector3(x, y, z));
|
312
|
+
ref.current().setNextKinematicRotation({
|
313
|
+
x: q.x,
|
314
|
+
y: q.y,
|
315
|
+
z: q.z,
|
316
|
+
w: q.w
|
317
|
+
});
|
318
|
+
},
|
324
319
|
setNextKinematicTranslation: translation => ref.current().setNextKinematicTranslation(translation),
|
325
320
|
resetForces: () => ref.current().resetForces(true),
|
326
|
-
resetTorques: () => ref.current().resetTorques(true)
|
321
|
+
resetTorques: () => ref.current().resetTorques(true),
|
322
|
+
lockRotations: locked => ref.current().lockRotations(locked, true),
|
323
|
+
lockTranslations: locked => ref.current().lockTranslations(locked, true),
|
324
|
+
setEnabledRotations: (x, y, z) => ref.current().setEnabledRotations(x, y, z, true),
|
325
|
+
setEnabledTranslations: (x, y, z) => ref.current().setEnabledTranslations(x, y, z, true)
|
327
326
|
};
|
328
327
|
}; // TODO: Flesh this out
|
329
328
|
|
@@ -348,7 +347,16 @@ const createWorldApi = ref => {
|
|
348
347
|
removeCollider: collider => ref.current().removeCollider(collider, true),
|
349
348
|
createImpulseJoint: (params, rigidBodyA, rigidBodyB) => ref.current().createImpulseJoint(params, rigidBodyA, rigidBodyB, true),
|
350
349
|
removeImpulseJoint: joint => ref.current().removeImpulseJoint(joint, true),
|
351
|
-
forEachCollider: callback => ref.current().forEachCollider(callback)
|
350
|
+
forEachCollider: callback => ref.current().forEachCollider(callback),
|
351
|
+
setGravity: ({
|
352
|
+
x,
|
353
|
+
y,
|
354
|
+
z
|
355
|
+
}) => ref.current().gravity = {
|
356
|
+
x,
|
357
|
+
y,
|
358
|
+
z
|
359
|
+
}
|
352
360
|
};
|
353
361
|
}; // TODO: Broken currently, waiting for Rapier3D to fix
|
354
362
|
|
@@ -376,7 +384,8 @@ const importRapier = async () => {
|
|
376
384
|
const Physics = ({
|
377
385
|
colliders: _colliders = 'cuboid',
|
378
386
|
gravity: _gravity = [0, -9.81, 0],
|
379
|
-
children
|
387
|
+
children,
|
388
|
+
timeStep: _timeStep = 'vary'
|
380
389
|
}) => {
|
381
390
|
const rapier = useAsset.useAsset(importRapier);
|
382
391
|
const worldRef = React.useRef();
|
@@ -401,7 +410,15 @@ const Physics = ({
|
|
401
410
|
worldRef.current = undefined;
|
402
411
|
}
|
403
412
|
};
|
404
|
-
}, []);
|
413
|
+
}, []); // Update gravity
|
414
|
+
|
415
|
+
React.useEffect(() => {
|
416
|
+
const world = worldRef.current;
|
417
|
+
|
418
|
+
if (world) {
|
419
|
+
world.gravity = vectorArrayToObject(_gravity);
|
420
|
+
}
|
421
|
+
}, [_gravity]);
|
405
422
|
const time = React.useRef(performance.now());
|
406
423
|
fiber.useFrame(context => {
|
407
424
|
const world = worldRef.current;
|
@@ -410,7 +427,13 @@ const Physics = ({
|
|
410
427
|
|
411
428
|
const now = performance.now();
|
412
429
|
const delta = Math.min(100, now - time.current);
|
413
|
-
|
430
|
+
|
431
|
+
if (_timeStep === 'vary') {
|
432
|
+
world.timestep = delta / 1000;
|
433
|
+
} else {
|
434
|
+
world.timestep = _timeStep;
|
435
|
+
}
|
436
|
+
|
414
437
|
world.step(eventQueue); // Update meshes
|
415
438
|
|
416
439
|
rigidBodyMeshes.forEach((mesh, handle) => {
|
@@ -523,6 +546,47 @@ const Physics = ({
|
|
523
546
|
}, children);
|
524
547
|
};
|
525
548
|
|
549
|
+
function _defineProperty(obj, key, value) {
|
550
|
+
if (key in obj) {
|
551
|
+
Object.defineProperty(obj, key, {
|
552
|
+
value: value,
|
553
|
+
enumerable: true,
|
554
|
+
configurable: true,
|
555
|
+
writable: true
|
556
|
+
});
|
557
|
+
} else {
|
558
|
+
obj[key] = value;
|
559
|
+
}
|
560
|
+
|
561
|
+
return obj;
|
562
|
+
}
|
563
|
+
|
564
|
+
function ownKeys(object, enumerableOnly) {
|
565
|
+
var keys = Object.keys(object);
|
566
|
+
|
567
|
+
if (Object.getOwnPropertySymbols) {
|
568
|
+
var symbols = Object.getOwnPropertySymbols(object);
|
569
|
+
enumerableOnly && (symbols = symbols.filter(function (sym) {
|
570
|
+
return Object.getOwnPropertyDescriptor(object, sym).enumerable;
|
571
|
+
})), keys.push.apply(keys, symbols);
|
572
|
+
}
|
573
|
+
|
574
|
+
return keys;
|
575
|
+
}
|
576
|
+
|
577
|
+
function _objectSpread2(target) {
|
578
|
+
for (var i = 1; i < arguments.length; i++) {
|
579
|
+
var source = null != arguments[i] ? arguments[i] : {};
|
580
|
+
i % 2 ? ownKeys(Object(source), !0).forEach(function (key) {
|
581
|
+
_defineProperty(target, key, source[key]);
|
582
|
+
}) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) {
|
583
|
+
Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
|
584
|
+
});
|
585
|
+
}
|
586
|
+
|
587
|
+
return target;
|
588
|
+
}
|
589
|
+
|
526
590
|
const useRapier = () => {
|
527
591
|
return React.useContext(RapierContext);
|
528
592
|
};
|
@@ -539,7 +603,7 @@ const useRigidBody = (options = {}) => {
|
|
539
603
|
const rigidBodyRef = React.useRef();
|
540
604
|
const getRigidBodyRef = React.useRef(() => {
|
541
605
|
if (!rigidBodyRef.current) {
|
542
|
-
var _options$linearVeloci, _options$angularVeloc, _options$gravityScale, _options$canSleep, _options$ccd;
|
606
|
+
var _options$linearVeloci, _options$angularVeloc, _options$gravityScale, _options$canSleep, _options$ccd, _options$enabledRotat, _options$enabledTrans;
|
543
607
|
|
544
608
|
const type = rigidBodyTypeFromString((options === null || options === void 0 ? void 0 : options.type) || "dynamic");
|
545
609
|
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];
|
@@ -547,11 +611,15 @@ const useRigidBody = (options = {}) => {
|
|
547
611
|
const gravityScale = (_options$gravityScale = options === null || options === void 0 ? void 0 : options.gravityScale) !== null && _options$gravityScale !== void 0 ? _options$gravityScale : 1;
|
548
612
|
const canSleep = (_options$canSleep = options === null || options === void 0 ? void 0 : options.canSleep) !== null && _options$canSleep !== void 0 ? _options$canSleep : true;
|
549
613
|
const ccdEnabled = (_options$ccd = options === null || options === void 0 ? void 0 : options.ccd) !== null && _options$ccd !== void 0 ? _options$ccd : false;
|
614
|
+
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];
|
615
|
+
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];
|
550
616
|
const desc = new rapier.RigidBodyDesc(type).setLinvel(lvx, lvy, lvz).setAngvel({
|
551
617
|
x: avx,
|
552
618
|
y: avy,
|
553
619
|
z: avz
|
554
|
-
}).setGravityScale(gravityScale).setCanSleep(canSleep).setCcdEnabled(ccdEnabled);
|
620
|
+
}).setGravityScale(gravityScale).setCanSleep(canSleep).setCcdEnabled(ccdEnabled).enabledRotations(erx, ery, erz).enabledTranslations(etx, ety, etz);
|
621
|
+
if (options.lockRotations) desc.lockRotations();
|
622
|
+
if (options.lockTranslations) desc.lockTranslations();
|
555
623
|
const rigidBody = world.createRigidBody(desc);
|
556
624
|
rigidBodyRef.current = world.getRigidBody(rigidBody.handle);
|
557
625
|
}
|
@@ -599,7 +667,9 @@ const useRigidBody = (options = {}) => {
|
|
599
667
|
rigidBody.resetForces(false);
|
600
668
|
rigidBody.resetTorques(false);
|
601
669
|
const colliderSetting = (_ref = (_options$colliders = options === null || options === void 0 ? void 0 : options.colliders) !== null && _options$colliders !== void 0 ? _options$colliders : physicsOptions.colliders) !== null && _ref !== void 0 ? _ref : false;
|
602
|
-
const autoColliders = colliderSetting !== false ? createCollidersFromChildren(ref.current, rigidBody,
|
670
|
+
const autoColliders = colliderSetting !== false ? createCollidersFromChildren(ref.current, rigidBody, _objectSpread2(_objectSpread2({}, options), {}, {
|
671
|
+
colliders: colliderSetting
|
672
|
+
}), world) : [];
|
603
673
|
rigidBodyMeshes.set(rigidBody.handle, ref.current);
|
604
674
|
return () => {
|
605
675
|
world.removeRigidBody(rigidBody);
|
@@ -646,142 +716,6 @@ const useCollider = (body, options = {}) => {
|
|
646
716
|
}, []);
|
647
717
|
const api = React.useMemo(() => createColliderApi(getColliderRef), []);
|
648
718
|
return [objectRef, api];
|
649
|
-
};
|
650
|
-
const useRigidBodyWithCollider = (rigidBodyOptions, colliderOptions) => {
|
651
|
-
const {
|
652
|
-
world
|
653
|
-
} = useRapier();
|
654
|
-
const [ref, rigidBody] = useRigidBody(rigidBodyOptions);
|
655
|
-
React.useEffect(() => {
|
656
|
-
if (!colliderOptions) {
|
657
|
-
return;
|
658
|
-
}
|
659
|
-
|
660
|
-
const scale = ref.current.getWorldScale(new three.Vector3());
|
661
|
-
const collider = createColliderFromOptions(colliderOptions, world, world.getRigidBody(rigidBody.handle), scale);
|
662
|
-
return () => {
|
663
|
-
world.removeCollider(collider);
|
664
|
-
};
|
665
|
-
}, []);
|
666
|
-
return [ref, rigidBody];
|
667
|
-
};
|
668
|
-
const useCuboid = (rigidBodyOptions = {}, colliderOptions = {}) => {
|
669
|
-
var _colliderOptions$args;
|
670
|
-
|
671
|
-
return useRigidBodyWithCollider(rigidBodyOptions, _objectSpread2({
|
672
|
-
shape: "cuboid",
|
673
|
-
args: (_colliderOptions$args = colliderOptions.args) !== null && _colliderOptions$args !== void 0 ? _colliderOptions$args : [0.5, 0.5, 0.5]
|
674
|
-
}, colliderOptions));
|
675
|
-
};
|
676
|
-
const useBall = (rigidBodyOptions = {}, colliderOptions = {}) => {
|
677
|
-
var _colliderOptions$args2;
|
678
|
-
|
679
|
-
return useRigidBodyWithCollider(rigidBodyOptions, _objectSpread2({
|
680
|
-
shape: "ball",
|
681
|
-
args: (_colliderOptions$args2 = colliderOptions.args) !== null && _colliderOptions$args2 !== void 0 ? _colliderOptions$args2 : [0.5]
|
682
|
-
}, colliderOptions));
|
683
|
-
};
|
684
|
-
const useCapsule = (rigidBodyOptions = {}, colliderOptions = {}) => {
|
685
|
-
var _colliderOptions$args3;
|
686
|
-
|
687
|
-
return useRigidBodyWithCollider(rigidBodyOptions, _objectSpread2({
|
688
|
-
shape: "capsule",
|
689
|
-
args: (_colliderOptions$args3 = colliderOptions.args) !== null && _colliderOptions$args3 !== void 0 ? _colliderOptions$args3 : [0.5, 0.5]
|
690
|
-
}, colliderOptions));
|
691
|
-
};
|
692
|
-
const useHeightfield = (rigidBodyOptions = {}, colliderOptions = {}) => {
|
693
|
-
return useRigidBodyWithCollider(rigidBodyOptions, _objectSpread2({
|
694
|
-
shape: "heightfield"
|
695
|
-
}, colliderOptions));
|
696
|
-
};
|
697
|
-
/**
|
698
|
-
* Create a trimesh collider and rigid body.
|
699
|
-
* Note that Trimeshes don't have mass unless provided.
|
700
|
-
* See https://rapier.rs/docs/user_guides/javascript/rigid_bodies#mass-properties
|
701
|
-
* for available properties.
|
702
|
-
*/
|
703
|
-
|
704
|
-
const useTrimesh = (rigidBodyOptions = {}, colliderOptions = {}) => {
|
705
|
-
return useRigidBodyWithCollider(rigidBodyOptions, _objectSpread2({
|
706
|
-
shape: "trimesh"
|
707
|
-
}, colliderOptions));
|
708
|
-
};
|
709
|
-
|
710
|
-
useTrimesh.fromMesh = (mesh, rigidBodyOptions = {}, colliderOptions = {}) => {
|
711
|
-
var _mesh$geometry, _mesh$geometry$index;
|
712
|
-
|
713
|
-
return useTrimesh(rigidBodyOptions, _objectSpread2({
|
714
|
-
args: [mesh.geometry.attributes.position.array, ((_mesh$geometry = mesh.geometry) === null || _mesh$geometry === void 0 ? void 0 : (_mesh$geometry$index = _mesh$geometry.index) === null || _mesh$geometry$index === void 0 ? void 0 : _mesh$geometry$index.array) || []]
|
715
|
-
}, colliderOptions));
|
716
|
-
};
|
717
|
-
|
718
|
-
const usePolyline = (rigidBodyOptions = {}, colliderOptions = {}) => {
|
719
|
-
return useRigidBodyWithCollider(rigidBodyOptions, _objectSpread2({
|
720
|
-
shape: "polyline"
|
721
|
-
}, colliderOptions));
|
722
|
-
};
|
723
|
-
const useRoundCuboid = (rigidBodyOptions = {}, colliderOptions = {}) => {
|
724
|
-
return useRigidBodyWithCollider(rigidBodyOptions, _objectSpread2({
|
725
|
-
shape: "roundCuboid"
|
726
|
-
}, colliderOptions));
|
727
|
-
};
|
728
|
-
const useCylinder = (rigidBodyOptions = {}, colliderOptions = {}) => {
|
729
|
-
return useRigidBodyWithCollider(rigidBodyOptions, _objectSpread2({
|
730
|
-
shape: "cylinder"
|
731
|
-
}, colliderOptions));
|
732
|
-
};
|
733
|
-
const useRoundCylinder = (rigidBodyOptions = {}, colliderOptions = {}) => {
|
734
|
-
return useRigidBodyWithCollider(rigidBodyOptions, _objectSpread2({
|
735
|
-
shape: "roundCylinder"
|
736
|
-
}, colliderOptions));
|
737
|
-
};
|
738
|
-
const useCone = (rigidBodyOptions = {}, colliderOptions = {}) => {
|
739
|
-
return useRigidBodyWithCollider(rigidBodyOptions, _objectSpread2({
|
740
|
-
shape: "cone"
|
741
|
-
}, colliderOptions));
|
742
|
-
};
|
743
|
-
const useRoundCone = (rigidBodyOptions = {}, colliderOptions = {}) => {
|
744
|
-
return useRigidBodyWithCollider(rigidBodyOptions, _objectSpread2({
|
745
|
-
shape: "roundCone"
|
746
|
-
}, colliderOptions));
|
747
|
-
};
|
748
|
-
const useConvexHull = (rigidBodyOptions = {}, colliderOptions = {}) => {
|
749
|
-
return useRigidBodyWithCollider(rigidBodyOptions, _objectSpread2({
|
750
|
-
shape: "convexHull"
|
751
|
-
}, colliderOptions));
|
752
|
-
};
|
753
|
-
|
754
|
-
useConvexHull.fromMesh = (mesh, rigidBodyOptions = {}, colliderOptions = {}) => {
|
755
|
-
var _mesh$geometry2, _mesh$geometry2$attri, _mesh$geometry2$attri2;
|
756
|
-
|
757
|
-
return useConvexHull(rigidBodyOptions, _objectSpread2({
|
758
|
-
args: [(mesh === null || mesh === void 0 ? void 0 : (_mesh$geometry2 = mesh.geometry) === null || _mesh$geometry2 === void 0 ? void 0 : (_mesh$geometry2$attri = _mesh$geometry2.attributes) === null || _mesh$geometry2$attri === void 0 ? void 0 : (_mesh$geometry2$attri2 = _mesh$geometry2$attri.position) === null || _mesh$geometry2$attri2 === void 0 ? void 0 : _mesh$geometry2$attri2.array) || []]
|
759
|
-
}, colliderOptions));
|
760
|
-
};
|
761
|
-
|
762
|
-
const useRoundConvexHull = (rigidBodyOptions = {}, colliderOptions = {}) => {
|
763
|
-
return useRigidBodyWithCollider(rigidBodyOptions, _objectSpread2({
|
764
|
-
shape: "roundConvexHull"
|
765
|
-
}, colliderOptions));
|
766
|
-
};
|
767
|
-
const useConvexMesh = (rigidBodyOptions = {}, colliderOptions = {}) => {
|
768
|
-
return useRigidBodyWithCollider(rigidBodyOptions, _objectSpread2({
|
769
|
-
shape: "convexMesh"
|
770
|
-
}, colliderOptions));
|
771
|
-
};
|
772
|
-
|
773
|
-
useConvexMesh.fromMesh = (mesh, rigidBodyOptions = {}, colliderOptions = {}) => {
|
774
|
-
var _mesh$geometry3, _mesh$geometry3$attri, _mesh$geometry3$attri2, _mesh$geometry4, _mesh$geometry4$index;
|
775
|
-
|
776
|
-
return useConvexMesh(rigidBodyOptions, _objectSpread2({
|
777
|
-
args: [mesh === null || mesh === void 0 ? void 0 : (_mesh$geometry3 = mesh.geometry) === null || _mesh$geometry3 === void 0 ? void 0 : (_mesh$geometry3$attri = _mesh$geometry3.attributes) === null || _mesh$geometry3$attri === void 0 ? void 0 : (_mesh$geometry3$attri2 = _mesh$geometry3$attri.position) === null || _mesh$geometry3$attri2 === void 0 ? void 0 : _mesh$geometry3$attri2.array, ((_mesh$geometry4 = mesh.geometry) === null || _mesh$geometry4 === void 0 ? void 0 : (_mesh$geometry4$index = _mesh$geometry4.index) === null || _mesh$geometry4$index === void 0 ? void 0 : _mesh$geometry4$index.array) || []]
|
778
|
-
}, colliderOptions));
|
779
|
-
};
|
780
|
-
|
781
|
-
const useRoundConvexMesh = (rigidBodyOptions = {}, colliderOptions = {}) => {
|
782
|
-
return useRigidBodyWithCollider(rigidBodyOptions, _objectSpread2({
|
783
|
-
shape: "convexMesh"
|
784
|
-
}, colliderOptions));
|
785
719
|
}; // Joints
|
786
720
|
|
787
721
|
const useImpulseJoint = (body1, body2, params) => {
|
@@ -794,12 +728,6 @@ const useImpulseJoint = (body1, body2, params) => {
|
|
794
728
|
let rb1;
|
795
729
|
let rb2;
|
796
730
|
|
797
|
-
if ('handle' in body1 && 'handle' in body2) {
|
798
|
-
rb1 = world.getRigidBody(body1.handle);
|
799
|
-
rb2 = world.getRigidBody(body2.handle);
|
800
|
-
jointRef.current = world.createImpulseJoint(params, rb1, rb2);
|
801
|
-
}
|
802
|
-
|
803
731
|
if ('current' in body1 && body1.current && 'current' in body2 && body2.current) {
|
804
732
|
rb1 = world.getRigidBody(body1.current.handle);
|
805
733
|
rb2 = world.getRigidBody(body2.current.handle);
|
@@ -945,17 +873,52 @@ const RigidBody = /*#__PURE__*/React.forwardRef((_ref, ref) => {
|
|
945
873
|
const [object, rigidBody] = useRigidBody(props);
|
946
874
|
React.useImperativeHandle(ref, () => rigidBody);
|
947
875
|
return /*#__PURE__*/React__default["default"].createElement(RigidBodyContext.Provider, {
|
948
|
-
value: [object, rigidBody, !!(props.onCollisionEnter || props.onCollisionExit)]
|
876
|
+
value: [object, rigidBody, !!(props.onCollisionEnter || props.onCollisionExit), props]
|
949
877
|
}, /*#__PURE__*/React__default["default"].createElement("object3D", {
|
950
878
|
ref: object
|
951
879
|
}, children));
|
952
|
-
});
|
880
|
+
});
|
881
|
+
const MeshCollider = ({
|
882
|
+
children,
|
883
|
+
type
|
884
|
+
}) => {
|
885
|
+
const {
|
886
|
+
physicsOptions,
|
887
|
+
world
|
888
|
+
} = useRapier();
|
889
|
+
const object = React.useRef(null);
|
890
|
+
const [, rigidBody, hasCollisionEvents, rigidBodyOptions] = useRigidBodyContext();
|
891
|
+
React.useEffect(() => {
|
892
|
+
let autoColliders = [];
|
893
|
+
|
894
|
+
if (object.current) {
|
895
|
+
var _ref2;
|
896
|
+
|
897
|
+
const colliderSetting = (_ref2 = type !== null && type !== void 0 ? type : physicsOptions.colliders) !== null && _ref2 !== void 0 ? _ref2 : false;
|
898
|
+
autoColliders = colliderSetting !== false ? createCollidersFromChildren(object.current, rigidBody, _objectSpread2(_objectSpread2({}, rigidBodyOptions), {}, {
|
899
|
+
colliders: colliderSetting
|
900
|
+
}), world, false) : [];
|
901
|
+
}
|
902
|
+
|
903
|
+
return () => {
|
904
|
+
autoColliders.forEach(collider => {
|
905
|
+
world.removeCollider(collider);
|
906
|
+
});
|
907
|
+
};
|
908
|
+
}, []);
|
909
|
+
return /*#__PURE__*/React__default["default"].createElement("object3D", {
|
910
|
+
ref: object,
|
911
|
+
userData: {
|
912
|
+
r3RapierType: 'MeshCollider'
|
913
|
+
}
|
914
|
+
}, children);
|
915
|
+
}; // Colliders
|
953
916
|
|
954
|
-
const AnyCollider =
|
917
|
+
const AnyCollider = _ref3 => {
|
955
918
|
let {
|
956
919
|
children
|
957
|
-
} =
|
958
|
-
props = _objectWithoutProperties(
|
920
|
+
} = _ref3,
|
921
|
+
props = _objectWithoutProperties(_ref3, _excluded2);
|
959
922
|
|
960
923
|
const {
|
961
924
|
world
|
@@ -1155,31 +1118,16 @@ exports.CuboidCollider = CuboidCollider;
|
|
1155
1118
|
exports.CylinderCollider = CylinderCollider;
|
1156
1119
|
exports.Debug = Debug;
|
1157
1120
|
exports.HeightfieldCollider = HeightfieldCollider;
|
1121
|
+
exports.MeshCollider = MeshCollider;
|
1158
1122
|
exports.Physics = Physics;
|
1159
1123
|
exports.RigidBody = RigidBody;
|
1160
1124
|
exports.RoundCuboidCollider = RoundCuboidCollider;
|
1161
1125
|
exports.TrimeshCollider = TrimeshCollider;
|
1162
|
-
exports.useBall = useBall;
|
1163
|
-
exports.useCapsule = useCapsule;
|
1164
1126
|
exports.useCollider = useCollider;
|
1165
|
-
exports.useCone = useCone;
|
1166
|
-
exports.useConvexHull = useConvexHull;
|
1167
|
-
exports.useConvexMesh = useConvexMesh;
|
1168
|
-
exports.useCuboid = useCuboid;
|
1169
|
-
exports.useCylinder = useCylinder;
|
1170
1127
|
exports.useFixedJoint = useFixedJoint;
|
1171
|
-
exports.useHeightfield = useHeightfield;
|
1172
1128
|
exports.useImpulseJoint = useImpulseJoint;
|
1173
|
-
exports.usePolyline = usePolyline;
|
1174
1129
|
exports.usePrismaticJoint = usePrismaticJoint;
|
1175
1130
|
exports.useRapier = useRapier;
|
1176
1131
|
exports.useRevoluteJoint = useRevoluteJoint;
|
1177
1132
|
exports.useRigidBody = useRigidBody;
|
1178
|
-
exports.useRigidBodyWithCollider = useRigidBodyWithCollider;
|
1179
|
-
exports.useRoundCone = useRoundCone;
|
1180
|
-
exports.useRoundConvexHull = useRoundConvexHull;
|
1181
|
-
exports.useRoundConvexMesh = useRoundConvexMesh;
|
1182
|
-
exports.useRoundCuboid = useRoundCuboid;
|
1183
|
-
exports.useRoundCylinder = useRoundCylinder;
|
1184
1133
|
exports.useSphericalJoint = useSphericalJoint;
|
1185
|
-
exports.useTrimesh = useTrimesh;
|