@react-three/rapier 0.4.3 → 0.5.2

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.
@@ -3,7 +3,7 @@ import { useAsset } from 'use-asset';
3
3
  import { useFrame } from '@react-three/fiber';
4
4
  import { ColliderDesc, CoefficientCombineRule, ActiveEvents, EventQueue, ShapeType } from '@dimforge/rapier3d-compat';
5
5
  export { CoefficientCombineRule, Collider as RapierCollider, RigidBody as RapierRigidBody } from '@dimforge/rapier3d-compat';
6
- import { Vector3, Quaternion, Object3D, Euler, CylinderBufferGeometry, BufferGeometry, BufferAttribute, SphereBufferGeometry, BoxBufferGeometry } from 'three';
6
+ import { Quaternion, Euler, Vector3, Object3D, CylinderBufferGeometry, BufferGeometry, BufferAttribute, SphereBufferGeometry, BoxBufferGeometry } from 'three';
7
7
 
8
8
  const vectorArrayToObject = arr => {
9
9
  const [x, y, z] = arr;
@@ -13,6 +13,11 @@ const vectorArrayToObject = arr => {
13
13
  z
14
14
  };
15
15
  };
16
+ const quaternion = new Quaternion();
17
+ const euler = new Euler();
18
+ const vector3ToQuaternion = v => {
19
+ return quaternion.setFromEuler(euler.setFromVector3(v));
20
+ };
16
21
  const rigidBodyTypeMap = {
17
22
  fixed: 1,
18
23
  dynamic: 0,
@@ -38,11 +43,13 @@ const scaleColliderArgs = (shape, args, scale) => {
38
43
  const scaleArray = [scale.x, scale.y, scale.z];
39
44
  return newArgs.map((arg, index) => scaleArray[index] * arg);
40
45
  };
41
- const createColliderFromOptions = (options, world, rigidBody, scale = {
42
- x: 1,
43
- y: 1,
44
- z: 1
45
- }, hasCollisionEvents = false) => {
46
+ const createColliderFromOptions = ({
47
+ options,
48
+ world,
49
+ rigidBody,
50
+ scale,
51
+ hasCollisionEvents
52
+ }) => {
46
53
  var _options$shape, _options$args, _options$restitution, _options$restitutionC, _options$friction, _options$frictionComb;
47
54
 
48
55
  const mass = (options === null || options === void 0 ? void 0 : options.mass) || 1;
@@ -51,15 +58,16 @@ const createColliderFromOptions = (options, world, rigidBody, scale = {
51
58
  const [cmx, cmy, cmz] = (options === null || options === void 0 ? void 0 : options.centerOfMass) || [0, 0, 0];
52
59
  const [pix, piy, piz] = (options === null || options === void 0 ? void 0 : options.principalAngularInertia) || [mass * 0.2, mass * 0.2, mass * 0.2];
53
60
  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]; // @ts-ignore
61
+ const [rx, ry, rz] = (options === null || options === void 0 ? void 0 : options.rotation) || [0, 0, 0];
62
+ const qRotation = vector3ToQuaternion(new Vector3(rx, ry, rz)); // @ts-ignore
55
63
 
56
64
  const scaledArgs = scaleColliderArgs(options.shape, colliderArgs, scale);
57
65
  let colliderDesc = ColliderDesc[colliderShape]( // @ts-ignore
58
66
  ...scaledArgs).setTranslation(x * scale.x, y * scale.y, z * scale.z).setRotation({
59
- x: rx,
60
- y: ry,
61
- z: rz,
62
- w: 1
67
+ x: qRotation.x,
68
+ y: qRotation.y,
69
+ z: qRotation.z,
70
+ w: qRotation.w
63
71
  }).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
72
 
65
73
  if (hasCollisionEvents) {
@@ -67,6 +75,8 @@ const createColliderFromOptions = (options, world, rigidBody, scale = {
67
75
  } // If any of the mass properties are specified, add mass properties
68
76
 
69
77
 
78
+ const qMassRot = vector3ToQuaternion(new Vector3(0, 0, 0));
79
+
70
80
  if (options !== null && options !== void 0 && options.mass || options !== null && options !== void 0 && options.centerOfMass || options !== null && options !== void 0 && options.principalAngularInertia) {
71
81
  colliderDesc.setDensity(0);
72
82
  colliderDesc.setMassProperties(mass, {
@@ -78,23 +88,33 @@ const createColliderFromOptions = (options, world, rigidBody, scale = {
78
88
  y: piy,
79
89
  z: piz
80
90
  }, {
81
- x: 0,
82
- y: 0,
83
- z: 0,
84
- w: 1
91
+ x: qMassRot.x,
92
+ y: qMassRot.y,
93
+ z: qMassRot.z,
94
+ w: qMassRot.w
85
95
  });
86
96
  }
87
97
 
88
98
  const collider = world.createCollider(colliderDesc, rigidBody);
89
99
  return collider;
90
100
  };
91
- const createCollidersFromChildren = (object, rigidBody, options, world) => {
101
+
102
+ const isChildOfMeshCollider = child => {
103
+ let flag = false;
104
+ child.traverseAncestors(a => {
105
+ if (a.userData.r3RapierType === "MeshCollider") flag = true;
106
+ });
107
+ return flag;
108
+ };
109
+
110
+ const createCollidersFromChildren = (object, rigidBody, options, world, ignoreMeshColliders = true) => {
92
111
  const hasCollisionEvents = !!(options.onCollisionEnter || options.onCollisionExit);
93
112
  const colliders = [];
94
113
  let desc;
95
114
  let offset = new Vector3();
96
115
  object.traverse(child => {
97
116
  if ("isMesh" in child) {
117
+ if (ignoreMeshColliders && isChildOfMeshCollider(child)) return;
98
118
  const {
99
119
  geometry
100
120
  } = child;
@@ -165,7 +185,8 @@ const createCollidersFromChildren = (object, rigidBody, options, world) => {
165
185
  if (hasCollisionEvents) desc.setActiveEvents(ActiveEvents.COLLISION_EVENTS);
166
186
  if (Number.isFinite(options.friction)) desc.setFriction(options.friction);
167
187
  if (Number.isFinite(options.restitution)) desc.setRestitution(options.restitution);
168
- const collider = world.createCollider(desc, rigidBody);
188
+ const actualRigidBody = world.getRigidBody(rigidBody === null || rigidBody === void 0 ? void 0 : rigidBody.handle);
189
+ const collider = world.createCollider(desc, actualRigidBody);
169
190
  colliders.push(collider);
170
191
  }
171
192
  });
@@ -183,47 +204,6 @@ const scaleVertices = (vertices, scale) => {
183
204
  return scaledVerts;
184
205
  };
185
206
 
186
- function _defineProperty(obj, key, value) {
187
- if (key in obj) {
188
- Object.defineProperty(obj, key, {
189
- value: value,
190
- enumerable: true,
191
- configurable: true,
192
- writable: true
193
- });
194
- } else {
195
- obj[key] = value;
196
- }
197
-
198
- return obj;
199
- }
200
-
201
- function ownKeys(object, enumerableOnly) {
202
- var keys = Object.keys(object);
203
-
204
- if (Object.getOwnPropertySymbols) {
205
- var symbols = Object.getOwnPropertySymbols(object);
206
- enumerableOnly && (symbols = symbols.filter(function (sym) {
207
- return Object.getOwnPropertyDescriptor(object, sym).enumerable;
208
- })), keys.push.apply(keys, symbols);
209
- }
210
-
211
- return keys;
212
- }
213
-
214
- function _objectSpread2(target) {
215
- for (var i = 1; i < arguments.length; i++) {
216
- var source = null != arguments[i] ? arguments[i] : {};
217
- i % 2 ? ownKeys(Object(source), !0).forEach(function (key) {
218
- _defineProperty(target, key, source[key]);
219
- }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) {
220
- Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
221
- });
222
- }
223
-
224
- return target;
225
- }
226
-
227
207
  const createRigidBodyApi = ref => {
228
208
  return {
229
209
  raw: () => ref.current(),
@@ -268,9 +248,19 @@ const createRigidBodyApi = ref => {
268
248
  return new Quaternion(x, y, z, w);
269
249
  },
270
250
 
271
- setRotation: rotation => ref.current().setRotation(_objectSpread2(_objectSpread2({}, rotation), {}, {
272
- w: 1
273
- }), true),
251
+ setRotation: ({
252
+ x,
253
+ y,
254
+ z
255
+ }) => {
256
+ const q = vector3ToQuaternion(new Vector3(x, y, z));
257
+ ref.current().setRotation({
258
+ x: q.x,
259
+ y: q.y,
260
+ z: q.z,
261
+ w: q.w
262
+ }, true);
263
+ },
274
264
 
275
265
  linvel() {
276
266
  const {
@@ -293,12 +283,26 @@ const createRigidBodyApi = ref => {
293
283
  },
294
284
 
295
285
  setAngvel: velocity => ref.current().setAngvel(velocity, true),
296
- setNextKinematicRotation: rotation => ref.current().setNextKinematicRotation(_objectSpread2(_objectSpread2({}, rotation), {}, {
297
- w: 1
298
- })),
286
+ setNextKinematicRotation: ({
287
+ x,
288
+ y,
289
+ z
290
+ }) => {
291
+ const q = vector3ToQuaternion(new Vector3(x, y, z));
292
+ ref.current().setNextKinematicRotation({
293
+ x: q.x,
294
+ y: q.y,
295
+ z: q.z,
296
+ w: q.w
297
+ });
298
+ },
299
299
  setNextKinematicTranslation: translation => ref.current().setNextKinematicTranslation(translation),
300
300
  resetForces: () => ref.current().resetForces(true),
301
- resetTorques: () => ref.current().resetTorques(true)
301
+ resetTorques: () => ref.current().resetTorques(true),
302
+ lockRotations: locked => ref.current().lockRotations(locked, true),
303
+ lockTranslations: locked => ref.current().lockTranslations(locked, true),
304
+ setEnabledRotations: (x, y, z) => ref.current().setEnabledRotations(x, y, z, true),
305
+ setEnabledTranslations: (x, y, z) => ref.current().setEnabledTranslations(x, y, z, true)
302
306
  };
303
307
  }; // TODO: Flesh this out
304
308
 
@@ -323,7 +327,16 @@ const createWorldApi = ref => {
323
327
  removeCollider: collider => ref.current().removeCollider(collider, true),
324
328
  createImpulseJoint: (params, rigidBodyA, rigidBodyB) => ref.current().createImpulseJoint(params, rigidBodyA, rigidBodyB, true),
325
329
  removeImpulseJoint: joint => ref.current().removeImpulseJoint(joint, true),
326
- forEachCollider: callback => ref.current().forEachCollider(callback)
330
+ forEachCollider: callback => ref.current().forEachCollider(callback),
331
+ setGravity: ({
332
+ x,
333
+ y,
334
+ z
335
+ }) => ref.current().gravity = {
336
+ x,
337
+ y,
338
+ z
339
+ }
327
340
  };
328
341
  }; // TODO: Broken currently, waiting for Rapier3D to fix
329
342
 
@@ -351,7 +364,8 @@ const importRapier = async () => {
351
364
  const Physics = ({
352
365
  colliders: _colliders = 'cuboid',
353
366
  gravity: _gravity = [0, -9.81, 0],
354
- children
367
+ children,
368
+ timeStep: _timeStep = 'vary'
355
369
  }) => {
356
370
  const rapier = useAsset(importRapier);
357
371
  const worldRef = useRef();
@@ -376,7 +390,15 @@ const Physics = ({
376
390
  worldRef.current = undefined;
377
391
  }
378
392
  };
379
- }, []);
393
+ }, []); // Update gravity
394
+
395
+ useEffect(() => {
396
+ const world = worldRef.current;
397
+
398
+ if (world) {
399
+ world.gravity = vectorArrayToObject(_gravity);
400
+ }
401
+ }, [_gravity]);
380
402
  const time = useRef(performance.now());
381
403
  useFrame(context => {
382
404
  const world = worldRef.current;
@@ -385,7 +407,13 @@ const Physics = ({
385
407
 
386
408
  const now = performance.now();
387
409
  const delta = Math.min(100, now - time.current);
388
- world.timestep = delta / 1000;
410
+
411
+ if (_timeStep === 'vary') {
412
+ world.timestep = delta / 1000;
413
+ } else {
414
+ world.timestep = _timeStep;
415
+ }
416
+
389
417
  world.step(eventQueue); // Update meshes
390
418
 
391
419
  rigidBodyMeshes.forEach((mesh, handle) => {
@@ -498,6 +526,47 @@ const Physics = ({
498
526
  }, children);
499
527
  };
500
528
 
529
+ function _defineProperty(obj, key, value) {
530
+ if (key in obj) {
531
+ Object.defineProperty(obj, key, {
532
+ value: value,
533
+ enumerable: true,
534
+ configurable: true,
535
+ writable: true
536
+ });
537
+ } else {
538
+ obj[key] = value;
539
+ }
540
+
541
+ return obj;
542
+ }
543
+
544
+ function ownKeys(object, enumerableOnly) {
545
+ var keys = Object.keys(object);
546
+
547
+ if (Object.getOwnPropertySymbols) {
548
+ var symbols = Object.getOwnPropertySymbols(object);
549
+ enumerableOnly && (symbols = symbols.filter(function (sym) {
550
+ return Object.getOwnPropertyDescriptor(object, sym).enumerable;
551
+ })), keys.push.apply(keys, symbols);
552
+ }
553
+
554
+ return keys;
555
+ }
556
+
557
+ function _objectSpread2(target) {
558
+ for (var i = 1; i < arguments.length; i++) {
559
+ var source = null != arguments[i] ? arguments[i] : {};
560
+ i % 2 ? ownKeys(Object(source), !0).forEach(function (key) {
561
+ _defineProperty(target, key, source[key]);
562
+ }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) {
563
+ Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
564
+ });
565
+ }
566
+
567
+ return target;
568
+ }
569
+
501
570
  const useRapier = () => {
502
571
  return useContext(RapierContext);
503
572
  };
@@ -514,7 +583,7 @@ const useRigidBody = (options = {}) => {
514
583
  const rigidBodyRef = useRef();
515
584
  const getRigidBodyRef = useRef(() => {
516
585
  if (!rigidBodyRef.current) {
517
- var _options$linearVeloci, _options$angularVeloc, _options$gravityScale, _options$canSleep, _options$ccd;
586
+ var _options$linearVeloci, _options$angularVeloc, _options$gravityScale, _options$canSleep, _options$ccd, _options$enabledRotat, _options$enabledTrans;
518
587
 
519
588
  const type = rigidBodyTypeFromString((options === null || options === void 0 ? void 0 : options.type) || "dynamic");
520
589
  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];
@@ -522,11 +591,15 @@ const useRigidBody = (options = {}) => {
522
591
  const gravityScale = (_options$gravityScale = options === null || options === void 0 ? void 0 : options.gravityScale) !== null && _options$gravityScale !== void 0 ? _options$gravityScale : 1;
523
592
  const canSleep = (_options$canSleep = options === null || options === void 0 ? void 0 : options.canSleep) !== null && _options$canSleep !== void 0 ? _options$canSleep : true;
524
593
  const ccdEnabled = (_options$ccd = options === null || options === void 0 ? void 0 : options.ccd) !== null && _options$ccd !== void 0 ? _options$ccd : false;
594
+ 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];
595
+ 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];
525
596
  const desc = new rapier.RigidBodyDesc(type).setLinvel(lvx, lvy, lvz).setAngvel({
526
597
  x: avx,
527
598
  y: avy,
528
599
  z: avz
529
- }).setGravityScale(gravityScale).setCanSleep(canSleep).setCcdEnabled(ccdEnabled);
600
+ }).setGravityScale(gravityScale).setCanSleep(canSleep).setCcdEnabled(ccdEnabled).enabledRotations(erx, ery, erz).enabledTranslations(etx, ety, etz);
601
+ if (options.lockRotations) desc.lockRotations();
602
+ if (options.lockTranslations) desc.lockTranslations();
530
603
  const rigidBody = world.createRigidBody(desc);
531
604
  rigidBodyRef.current = world.getRigidBody(rigidBody.handle);
532
605
  }
@@ -623,144 +696,6 @@ const useCollider = (body, options = {}) => {
623
696
  }, []);
624
697
  const api = useMemo(() => createColliderApi(getColliderRef), []);
625
698
  return [objectRef, api];
626
- };
627
- const useRigidBodyWithCollider = (rigidBodyOptions, colliderOptions) => {
628
- const {
629
- world
630
- } = useRapier();
631
- const [ref, rigidBody] = useRigidBody(_objectSpread2(_objectSpread2({}, rigidBodyOptions), {}, {
632
- colliders: false
633
- }));
634
- useEffect(() => {
635
- if (!colliderOptions) {
636
- return;
637
- }
638
-
639
- const scale = ref.current.getWorldScale(new Vector3());
640
- const collider = createColliderFromOptions(colliderOptions, world, world.getRigidBody(rigidBody.handle), scale);
641
- return () => {
642
- world.removeCollider(collider);
643
- };
644
- }, []);
645
- return [ref, rigidBody];
646
- };
647
- const useCuboid = (rigidBodyOptions = {}, colliderOptions = {}) => {
648
- var _colliderOptions$args;
649
-
650
- return useRigidBodyWithCollider(rigidBodyOptions, _objectSpread2({
651
- shape: "cuboid",
652
- args: (_colliderOptions$args = colliderOptions.args) !== null && _colliderOptions$args !== void 0 ? _colliderOptions$args : [0.5, 0.5, 0.5]
653
- }, colliderOptions));
654
- };
655
- const useBall = (rigidBodyOptions = {}, colliderOptions = {}) => {
656
- var _colliderOptions$args2;
657
-
658
- return useRigidBodyWithCollider(rigidBodyOptions, _objectSpread2({
659
- shape: "ball",
660
- args: (_colliderOptions$args2 = colliderOptions.args) !== null && _colliderOptions$args2 !== void 0 ? _colliderOptions$args2 : [0.5]
661
- }, colliderOptions));
662
- };
663
- const useCapsule = (rigidBodyOptions = {}, colliderOptions = {}) => {
664
- var _colliderOptions$args3;
665
-
666
- return useRigidBodyWithCollider(rigidBodyOptions, _objectSpread2({
667
- shape: "capsule",
668
- args: (_colliderOptions$args3 = colliderOptions.args) !== null && _colliderOptions$args3 !== void 0 ? _colliderOptions$args3 : [0.5, 0.5]
669
- }, colliderOptions));
670
- };
671
- const useHeightfield = (rigidBodyOptions = {}, colliderOptions = {}) => {
672
- return useRigidBodyWithCollider(rigidBodyOptions, _objectSpread2({
673
- shape: "heightfield"
674
- }, colliderOptions));
675
- };
676
- /**
677
- * Create a trimesh collider and rigid body.
678
- * Note that Trimeshes don't have mass unless provided.
679
- * See https://rapier.rs/docs/user_guides/javascript/rigid_bodies#mass-properties
680
- * for available properties.
681
- */
682
-
683
- const useTrimesh = (rigidBodyOptions = {}, colliderOptions = {}) => {
684
- return useRigidBodyWithCollider(rigidBodyOptions, _objectSpread2({
685
- shape: "trimesh"
686
- }, colliderOptions));
687
- };
688
-
689
- useTrimesh.fromMesh = (mesh, rigidBodyOptions = {}, colliderOptions = {}) => {
690
- var _mesh$geometry, _mesh$geometry$index;
691
-
692
- return useTrimesh(rigidBodyOptions, _objectSpread2({
693
- 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) || []]
694
- }, colliderOptions));
695
- };
696
-
697
- const usePolyline = (rigidBodyOptions = {}, colliderOptions = {}) => {
698
- return useRigidBodyWithCollider(rigidBodyOptions, _objectSpread2({
699
- shape: "polyline"
700
- }, colliderOptions));
701
- };
702
- const useRoundCuboid = (rigidBodyOptions = {}, colliderOptions = {}) => {
703
- return useRigidBodyWithCollider(rigidBodyOptions, _objectSpread2({
704
- shape: "roundCuboid"
705
- }, colliderOptions));
706
- };
707
- const useCylinder = (rigidBodyOptions = {}, colliderOptions = {}) => {
708
- return useRigidBodyWithCollider(rigidBodyOptions, _objectSpread2({
709
- shape: "cylinder"
710
- }, colliderOptions));
711
- };
712
- const useRoundCylinder = (rigidBodyOptions = {}, colliderOptions = {}) => {
713
- return useRigidBodyWithCollider(rigidBodyOptions, _objectSpread2({
714
- shape: "roundCylinder"
715
- }, colliderOptions));
716
- };
717
- const useCone = (rigidBodyOptions = {}, colliderOptions = {}) => {
718
- return useRigidBodyWithCollider(rigidBodyOptions, _objectSpread2({
719
- shape: "cone"
720
- }, colliderOptions));
721
- };
722
- const useRoundCone = (rigidBodyOptions = {}, colliderOptions = {}) => {
723
- return useRigidBodyWithCollider(rigidBodyOptions, _objectSpread2({
724
- shape: "roundCone"
725
- }, colliderOptions));
726
- };
727
- const useConvexHull = (rigidBodyOptions = {}, colliderOptions = {}) => {
728
- return useRigidBodyWithCollider(rigidBodyOptions, _objectSpread2({
729
- shape: "convexHull"
730
- }, colliderOptions));
731
- };
732
-
733
- useConvexHull.fromMesh = (mesh, rigidBodyOptions = {}, colliderOptions = {}) => {
734
- var _mesh$geometry2, _mesh$geometry2$attri, _mesh$geometry2$attri2;
735
-
736
- return useConvexHull(rigidBodyOptions, _objectSpread2({
737
- 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) || []]
738
- }, colliderOptions));
739
- };
740
-
741
- const useRoundConvexHull = (rigidBodyOptions = {}, colliderOptions = {}) => {
742
- return useRigidBodyWithCollider(rigidBodyOptions, _objectSpread2({
743
- shape: "roundConvexHull"
744
- }, colliderOptions));
745
- };
746
- const useConvexMesh = (rigidBodyOptions = {}, colliderOptions = {}) => {
747
- return useRigidBodyWithCollider(rigidBodyOptions, _objectSpread2({
748
- shape: "convexMesh"
749
- }, colliderOptions));
750
- };
751
-
752
- useConvexMesh.fromMesh = (mesh, rigidBodyOptions = {}, colliderOptions = {}) => {
753
- var _mesh$geometry3, _mesh$geometry3$attri, _mesh$geometry3$attri2, _mesh$geometry4, _mesh$geometry4$index;
754
-
755
- return useConvexMesh(rigidBodyOptions, _objectSpread2({
756
- 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) || []]
757
- }, colliderOptions));
758
- };
759
-
760
- const useRoundConvexMesh = (rigidBodyOptions = {}, colliderOptions = {}) => {
761
- return useRigidBodyWithCollider(rigidBodyOptions, _objectSpread2({
762
- shape: "convexMesh"
763
- }, colliderOptions));
764
699
  }; // Joints
765
700
 
766
701
  const useImpulseJoint = (body1, body2, params) => {
@@ -773,12 +708,6 @@ const useImpulseJoint = (body1, body2, params) => {
773
708
  let rb1;
774
709
  let rb2;
775
710
 
776
- if ('handle' in body1 && 'handle' in body2) {
777
- rb1 = world.getRigidBody(body1.handle);
778
- rb2 = world.getRigidBody(body2.handle);
779
- jointRef.current = world.createImpulseJoint(params, rb1, rb2);
780
- }
781
-
782
711
  if ('current' in body1 && body1.current && 'current' in body2 && body2.current) {
783
712
  rb1 = world.getRigidBody(body1.current.handle);
784
713
  rb2 = world.getRigidBody(body2.current.handle);
@@ -921,29 +850,80 @@ const RigidBody = /*#__PURE__*/forwardRef((_ref, ref) => {
921
850
  } = _ref,
922
851
  props = _objectWithoutProperties(_ref, _excluded);
923
852
 
924
- const [object, rigidBody] = useRigidBody(props);
925
- useImperativeHandle(ref, () => rigidBody);
853
+ const [object, api] = useRigidBody(props);
854
+ useImperativeHandle(ref, () => api);
926
855
  return /*#__PURE__*/React.createElement(RigidBodyContext.Provider, {
927
- value: [object, rigidBody, !!(props.onCollisionEnter || props.onCollisionExit)]
856
+ value: {
857
+ ref: object,
858
+ api,
859
+ hasCollisionEvents: !!(props.onCollisionEnter || props.onCollisionExit),
860
+ options: props
861
+ }
928
862
  }, /*#__PURE__*/React.createElement("object3D", {
929
863
  ref: object
930
864
  }, children));
931
- }); // Colliders
865
+ });
866
+ const MeshCollider = ({
867
+ children,
868
+ type
869
+ }) => {
870
+ const {
871
+ physicsOptions,
872
+ world
873
+ } = useRapier();
874
+ const object = useRef(null);
875
+ const {
876
+ api,
877
+ options
878
+ } = useRigidBodyContext();
879
+ useEffect(() => {
880
+ let autoColliders = [];
881
+
882
+ if (object.current) {
883
+ var _ref2;
884
+
885
+ const colliderSetting = (_ref2 = type !== null && type !== void 0 ? type : physicsOptions.colliders) !== null && _ref2 !== void 0 ? _ref2 : false;
886
+ autoColliders = colliderSetting !== false ? createCollidersFromChildren(object.current, api, _objectSpread2(_objectSpread2({}, options), {}, {
887
+ colliders: colliderSetting
888
+ }), world, false) : [];
889
+ }
932
890
 
933
- const AnyCollider = _ref2 => {
891
+ return () => {
892
+ autoColliders.forEach(collider => {
893
+ world.removeCollider(collider);
894
+ });
895
+ };
896
+ }, []);
897
+ return /*#__PURE__*/React.createElement("object3D", {
898
+ ref: object,
899
+ userData: {
900
+ r3RapierType: "MeshCollider"
901
+ }
902
+ }, children);
903
+ }; // Colliders
904
+
905
+ const AnyCollider = _ref3 => {
934
906
  let {
935
907
  children
936
- } = _ref2,
937
- props = _objectWithoutProperties(_ref2, _excluded2);
908
+ } = _ref3,
909
+ props = _objectWithoutProperties(_ref3, _excluded2);
938
910
 
939
911
  const {
940
912
  world
941
913
  } = useRapier();
942
- const [, rigidBody, hasCollisionEvents] = useRigidBodyContext();
914
+ const rigidBodyContext = useRigidBodyContext();
943
915
  const ref = useRef(null);
944
916
  useEffect(() => {
917
+ var _rigidBodyContext$api;
918
+
945
919
  const scale = ref.current.getWorldScale(new Vector3());
946
- const collider = createColliderFromOptions(props, world, rigidBody.raw(), scale, hasCollisionEvents);
920
+ const collider = createColliderFromOptions({
921
+ options: props,
922
+ world,
923
+ rigidBody: rigidBodyContext === null || rigidBodyContext === void 0 ? void 0 : (_rigidBodyContext$api = rigidBodyContext.api) === null || _rigidBodyContext$api === void 0 ? void 0 : _rigidBodyContext$api.raw(),
924
+ scale,
925
+ hasCollisionEvents: rigidBodyContext === null || rigidBodyContext === void 0 ? void 0 : rigidBodyContext.hasCollisionEvents
926
+ });
947
927
  return () => {
948
928
  world.removeCollider(collider);
949
929
  };
@@ -1114,4 +1094,4 @@ const Debug = () => {
1114
1094
  })));
1115
1095
  };
1116
1096
 
1117
- export { BallCollider, CapsuleCollider, ConeCollider, ConvexHullCollider, CuboidCollider, CylinderCollider, Debug, HeightfieldCollider, Physics, RigidBody, RoundCuboidCollider, TrimeshCollider, useBall, useCapsule, useCollider, useCone, useConvexHull, useConvexMesh, useCuboid, useCylinder, useFixedJoint, useHeightfield, useImpulseJoint, usePolyline, usePrismaticJoint, useRapier, useRevoluteJoint, useRigidBody, useRigidBodyWithCollider, useRoundCone, useRoundConvexHull, useRoundConvexMesh, useRoundCuboid, useRoundCylinder, useSphericalJoint, useTrimesh };
1097
+ export { BallCollider, CapsuleCollider, ConeCollider, ConvexHullCollider, CuboidCollider, CylinderCollider, Debug, HeightfieldCollider, MeshCollider, Physics, RigidBody, RoundCuboidCollider, TrimeshCollider, useCollider, useFixedJoint, useImpulseJoint, usePrismaticJoint, useRapier, useRevoluteJoint, useRigidBody, useSphericalJoint };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@react-three/rapier",
3
- "version": "0.4.3",
3
+ "version": "0.5.2",
4
4
  "source": "src/index.ts",
5
5
  "main": "dist/react-three-rapier.cjs.js",
6
6
  "module": "dist/react-three-rapier.esm.js",
@@ -23,7 +23,7 @@
23
23
  "three": "^0.139.2"
24
24
  },
25
25
  "dependencies": {
26
- "@dimforge/rapier3d-compat": "0.8.1",
26
+ "@dimforge/rapier3d-compat": "0.9.0",
27
27
  "use-asset": "^1.0.4"
28
28
  },
29
29
  "repository": "https://github.com/pmndrs/react-three-rapier/tree/master/packages/react-three-rapier"