@react-three/rapier 0.7.4 → 0.7.6

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.
@@ -73,6 +73,23 @@ const scaleVertices = (vertices, scale) => {
73
73
 
74
74
  return scaledVerts;
75
75
  };
76
+ const vectorToTuple = v => {
77
+ if (!v) return [0];
78
+
79
+ if (v instanceof three.Quaternion) {
80
+ return [v.x, v.y, v.z, v.w];
81
+ }
82
+
83
+ if (v instanceof three.Vector3 || v instanceof three.Euler) {
84
+ return [v.x, v.y, v.z];
85
+ }
86
+
87
+ if (Array.isArray(v)) {
88
+ return v;
89
+ }
90
+
91
+ return [v];
92
+ };
76
93
 
77
94
  const createRigidBodyApi = ref => {
78
95
  return {
@@ -84,24 +101,24 @@ const createRigidBodyApi = ref => {
84
101
 
85
102
  mass: () => ref.current().mass(),
86
103
 
87
- applyImpulse(impulseVector) {
88
- ref.current().applyImpulse(impulseVector, true);
104
+ applyImpulse(impulseVector, wakeUp = true) {
105
+ ref.current().applyImpulse(impulseVector, wakeUp);
89
106
  },
90
107
 
91
- applyTorqueImpulse(torqueVector) {
92
- ref.current().applyTorqueImpulse(torqueVector, true);
108
+ applyTorqueImpulse(torqueVector, wakeUp = true) {
109
+ ref.current().applyTorqueImpulse(torqueVector, wakeUp);
93
110
  },
94
111
 
95
- applyImpulseAtPoint: (impulseVector, impulsePoint) => ref.current().applyImpulseAtPoint(impulseVector, impulsePoint, true),
96
- addForce: force => ref.current().addForce(force, true),
97
- addForceAtPoint: (force, point) => ref.current().addForceAtPoint(force, point, true),
98
- addTorque: torque => ref.current().addTorque(torque, true),
112
+ applyImpulseAtPoint: (impulseVector, impulsePoint, wakeUp = true) => ref.current().applyImpulseAtPoint(impulseVector, impulsePoint, wakeUp),
113
+ addForce: (force, wakeUp = true) => ref.current().addForce(force, wakeUp),
114
+ addForceAtPoint: (force, point, wakeUp = true) => ref.current().addForceAtPoint(force, point, wakeUp),
115
+ addTorque: (torque, wakeUp = true) => ref.current().addTorque(torque, wakeUp),
99
116
 
100
117
  translation() {
101
118
  return rapierVector3ToVector3(ref.current().translation());
102
119
  },
103
120
 
104
- setTranslation: translation => ref.current().setTranslation(translation, true),
121
+ setTranslation: (translation, wakeUp = true) => ref.current().setTranslation(translation, wakeUp),
105
122
 
106
123
  rotation() {
107
124
  const {
@@ -113,8 +130,8 @@ const createRigidBodyApi = ref => {
113
130
  return new three.Quaternion(x, y, z, w);
114
131
  },
115
132
 
116
- setRotation: rotation => {
117
- ref.current().setRotation(rotation, true);
133
+ setRotation: (rotation, wakeUp = true) => {
134
+ ref.current().setRotation(rotation, wakeUp);
118
135
  },
119
136
 
120
137
  linvel() {
@@ -126,7 +143,7 @@ const createRigidBodyApi = ref => {
126
143
  return new three.Vector3(x, y, z);
127
144
  },
128
145
 
129
- setLinvel: velocity => ref.current().setLinvel(velocity, true),
146
+ setLinvel: (velocity, wakeUp = true) => ref.current().setLinvel(velocity, wakeUp),
130
147
 
131
148
  angvel() {
132
149
  const {
@@ -137,7 +154,7 @@ const createRigidBodyApi = ref => {
137
154
  return new three.Vector3(x, y, z);
138
155
  },
139
156
 
140
- setAngvel: velocity => ref.current().setAngvel(velocity, true),
157
+ setAngvel: (velocity, wakeUp = true) => ref.current().setAngvel(velocity, wakeUp),
141
158
 
142
159
  linearDamping() {
143
160
  return ref.current().linearDamping();
@@ -154,12 +171,12 @@ const createRigidBodyApi = ref => {
154
171
  ref.current().setNextKinematicRotation(rotation);
155
172
  },
156
173
  setNextKinematicTranslation: translation => ref.current().setNextKinematicTranslation(translation),
157
- resetForces: () => ref.current().resetForces(true),
158
- resetTorques: () => ref.current().resetTorques(true),
159
- lockRotations: locked => ref.current().lockRotations(locked, true),
160
- lockTranslations: locked => ref.current().lockTranslations(locked, true),
161
- setEnabledRotations: (x, y, z) => ref.current().setEnabledRotations(x, y, z, true),
162
- setEnabledTranslations: (x, y, z) => ref.current().setEnabledTranslations(x, y, z, true)
174
+ resetForces: (wakeUp = true) => ref.current().resetForces(wakeUp),
175
+ resetTorques: (wakeUp = true) => ref.current().resetTorques(wakeUp),
176
+ lockRotations: (locked, wakeUp = true) => ref.current().lockRotations(locked, wakeUp),
177
+ lockTranslations: (locked, wakeUp = true) => ref.current().lockTranslations(locked, wakeUp),
178
+ setEnabledRotations: (x, y, z, wakeUp = true) => ref.current().setEnabledRotations(x, y, z, wakeUp),
179
+ setEnabledTranslations: (x, y, z, wakeUp = true) => ref.current().setEnabledTranslations(x, y, z, wakeUp)
163
180
  };
164
181
  };
165
182
  const createInstancedRigidBodiesApi = bodiesGetter => ({
@@ -182,9 +199,9 @@ const createWorldApi = ref => {
182
199
  createRigidBody: desc => ref.current().createRigidBody(desc),
183
200
  createCollider: (desc, rigidBody) => ref.current().createCollider(desc, rigidBody),
184
201
  removeRigidBody: rigidBody => ref.current().removeRigidBody(rigidBody),
185
- removeCollider: collider => ref.current().removeCollider(collider, true),
186
- createImpulseJoint: (params, rigidBodyA, rigidBodyB) => ref.current().createImpulseJoint(params, rigidBodyA, rigidBodyB, true),
187
- removeImpulseJoint: joint => ref.current().removeImpulseJoint(joint, true),
202
+ removeCollider: (collider, wakeUp = true) => ref.current().removeCollider(collider, wakeUp),
203
+ createImpulseJoint: (params, rigidBodyA, rigidBodyB, wakeUp = true) => ref.current().createImpulseJoint(params, rigidBodyA, rigidBodyB, wakeUp),
204
+ removeImpulseJoint: (joint, wakeUp = true) => ref.current().removeImpulseJoint(joint, wakeUp),
188
205
  forEachCollider: callback => ref.current().forEachCollider(callback),
189
206
  setGravity: ({
190
207
  x,
@@ -643,7 +660,11 @@ const mutableRigidBodyOptions = {
643
660
  },
644
661
  ccd: (rb, value) => {
645
662
  rb.enableCcd(value);
646
- }
663
+ },
664
+ position: () => {},
665
+ rotation: () => {},
666
+ quaternion: () => {},
667
+ scale: () => {}
647
668
  };
648
669
  const mutableRigidBodyOptionKeys = Object.keys(mutableRigidBodyOptions);
649
670
  const setRigidBodyOptions = (rigidBody, options, states, updateTranslations = true) => {
@@ -671,6 +692,10 @@ const setRigidBodyOptions = (rigidBody, options, states, updateTranslations = tr
671
692
  }
672
693
  };
673
694
  const useUpdateRigidBodyOptions = (rigidBodyRef, props, states, updateTranslations = true) => {
695
+ // TODO: Improve this, split each prop into its own effect
696
+ const mutablePropsAsFlatArray = React.useMemo(() => mutableRigidBodyOptionKeys.flatMap(key => {
697
+ return vectorToTuple(props[key]);
698
+ }), [props]);
674
699
  React.useEffect(() => {
675
700
  if ("length" in rigidBodyRef.current) {
676
701
  rigidBodyRef.current.forEach(rigidBody => {
@@ -679,7 +704,7 @@ const useUpdateRigidBodyOptions = (rigidBodyRef, props, states, updateTranslatio
679
704
  } else {
680
705
  setRigidBodyOptions(rigidBodyRef.current, props, states, updateTranslations);
681
706
  }
682
- }, [props]);
707
+ }, mutablePropsAsFlatArray);
683
708
  };
684
709
  const useRigidBodyEvents = (rigidBodyRef, props, events) => {
685
710
  const {
@@ -716,7 +741,7 @@ const useRigidBodyEvents = (rigidBodyRef, props, events) => {
716
741
  events.delete(rigidBodyRef.current.handle);
717
742
  }
718
743
  };
719
- }, [onWake, onSleep, onCollisionEnter, onCollisionExit]);
744
+ }, [onWake, onSleep, onCollisionEnter, onCollisionExit, onIntersectionEnter, onIntersectionExit]);
720
745
  };
721
746
 
722
747
  const scaleColliderArgs = (shape, args, scale) => {
@@ -746,6 +771,32 @@ const createColliderFromOptions = (options, world, scale, rigidBody) => {
746
771
  const desc = rapier3dCompat.ColliderDesc[options.shape](...scaledArgs);
747
772
  return world.createCollider(desc, rigidBody);
748
773
  };
774
+ const massPropertiesConflictError = "Please pick ONLY ONE of the `density`, `mass` and `massProperties` options.";
775
+
776
+ const setColliderMassOptions = (collider, options) => {
777
+ if (options.density !== undefined) {
778
+ if (options.mass !== undefined || options.massProperties !== undefined) {
779
+ throw new Error(massPropertiesConflictError);
780
+ }
781
+
782
+ collider.setDensity(options.density);
783
+ return;
784
+ }
785
+
786
+ if (options.mass !== undefined) {
787
+ if (options.massProperties !== undefined) {
788
+ throw new Error(massPropertiesConflictError);
789
+ }
790
+
791
+ collider.setMass(options.mass);
792
+ return;
793
+ }
794
+
795
+ if (options.massProperties !== undefined) {
796
+ collider.setMassProperties(options.massProperties.mass, options.massProperties.centerOfMass, options.massProperties.principalAngularInertia, options.massProperties.angularInertiaLocalFrame);
797
+ }
798
+ };
799
+
749
800
  const mutableColliderOptions = {
750
801
  sensor: (collider, value) => {
751
802
  collider.setSensor(value);
@@ -759,15 +810,20 @@ const mutableColliderOptions = {
759
810
  friction: (collider, value) => {
760
811
  collider.setFriction(value);
761
812
  },
813
+ frictionCombineRule: (collider, value) => {
814
+ collider.setFrictionCombineRule(value);
815
+ },
762
816
  restitution: (collider, value) => {
763
817
  collider.setRestitution(value);
764
818
  },
765
- density: (collider, value) => {
766
- collider.setDensity(value);
819
+ restitutionCombineRule: (collider, value) => {
820
+ collider.setRestitutionCombineRule(value);
767
821
  },
768
- mass: (collider, value) => {
769
- collider.setMass(value);
770
- }
822
+ // To make sure the options all mutalbe options are listed
823
+ quaternion: () => {},
824
+ position: () => {},
825
+ rotation: () => {},
826
+ scale: () => {}
771
827
  };
772
828
  const mutableColliderOptionKeys = Object.keys(mutableColliderOptions);
773
829
  const setColliderOptions = (collider, options, states) => {
@@ -798,17 +854,26 @@ const setColliderOptions = (collider, options, states) => {
798
854
 
799
855
  mutableColliderOptionKeys.forEach(key => {
800
856
  if (key in options) {
801
- mutableColliderOptions[key](collider, options[key]);
857
+ const option = options[key];
858
+ mutableColliderOptions[key](collider, // @ts-ignore Option does not want to fit into the function, but it will
859
+ option, options);
802
860
  }
803
- });
861
+ }); // handle mass separately, because the assignments
862
+ // are exclusive.
863
+
864
+ setColliderMassOptions(collider, options);
804
865
  }
805
866
  };
806
867
  const useUpdateColliderOptions = (collidersRef, props, states) => {
868
+ // TODO: Improve this, split each prop into its own effect
869
+ const mutablePropsAsFlatArray = React.useMemo(() => mutableColliderOptionKeys.flatMap(key => {
870
+ return vectorToTuple(props[key]);
871
+ }), [props]);
807
872
  React.useEffect(() => {
808
873
  collidersRef.current.forEach(collider => {
809
874
  setColliderOptions(collider, props, states);
810
875
  });
811
- }, [props]);
876
+ }, mutablePropsAsFlatArray);
812
877
  };
813
878
 
814
879
  const isChildOfMeshCollider = child => {
@@ -949,7 +1014,7 @@ const useColliderEvents = (collidersRef, props, events) => {
949
1014
 
950
1015
  (_collidersRef$current2 = collidersRef.current) === null || _collidersRef$current2 === void 0 ? void 0 : _collidersRef$current2.forEach(collider => events.delete(collider.handle));
951
1016
  };
952
- }, [onCollisionEnter, onCollisionExit]);
1017
+ }, [onCollisionEnter, onCollisionExit, onIntersectionEnter, onIntersectionExit]);
953
1018
  };
954
1019
 
955
1020
  const useRapier = () => {
@@ -1109,7 +1174,7 @@ const usePrismaticJoint = (body1, body2, [body1Anchor, body2Anchor, axis]) => {
1109
1174
  };
1110
1175
 
1111
1176
  // Colliders
1112
- const AnyCollider = /*#__PURE__*/React.memo(props => {
1177
+ const AnyCollider = /*#__PURE__*/React.memo( /*#__PURE__*/React__default["default"].forwardRef((props, forwardedRef) => {
1113
1178
  const {
1114
1179
  children,
1115
1180
  position,
@@ -1124,7 +1189,15 @@ const AnyCollider = /*#__PURE__*/React.memo(props => {
1124
1189
  } = useRapier();
1125
1190
  const rigidBodyContext = useRigidBodyContext();
1126
1191
  const ref = React.useRef(null);
1127
- const collidersRef = React.useRef([]);
1192
+ const collidersRef = React.useMemo(() => {
1193
+ if (forwardedRef !== null) {
1194
+ return forwardedRef;
1195
+ }
1196
+
1197
+ const result = /*#__PURE__*/React__default["default"].createRef();
1198
+ result.current = [];
1199
+ return result;
1200
+ }, []);
1128
1201
  React.useEffect(() => {
1129
1202
  const object = ref.current;
1130
1203
  const worldScale = object.getWorldScale(new three.Vector3());
@@ -1169,57 +1242,66 @@ const AnyCollider = /*#__PURE__*/React.memo(props => {
1169
1242
  scale: scale,
1170
1243
  ref: ref
1171
1244
  }, children);
1172
- });
1173
- const CuboidCollider = props => {
1245
+ }));
1246
+ const CuboidCollider = /*#__PURE__*/React__default["default"].forwardRef((props, ref) => {
1174
1247
  return /*#__PURE__*/React__default["default"].createElement(AnyCollider, _extends({}, props, {
1175
- shape: "cuboid"
1248
+ shape: "cuboid",
1249
+ ref: ref
1176
1250
  }));
1177
- };
1178
- const RoundCuboidCollider = props => {
1251
+ });
1252
+ const RoundCuboidCollider = /*#__PURE__*/React__default["default"].forwardRef((props, ref) => {
1179
1253
  return /*#__PURE__*/React__default["default"].createElement(AnyCollider, _extends({}, props, {
1180
- shape: "roundCuboid"
1254
+ shape: "roundCuboid",
1255
+ ref: ref
1181
1256
  }));
1182
- };
1183
- const BallCollider = props => {
1257
+ });
1258
+ const BallCollider = /*#__PURE__*/React__default["default"].forwardRef((props, ref) => {
1184
1259
  return /*#__PURE__*/React__default["default"].createElement(AnyCollider, _extends({}, props, {
1185
- shape: "ball"
1260
+ shape: "ball",
1261
+ ref: ref
1186
1262
  }));
1187
- };
1188
- const CapsuleCollider = props => {
1263
+ });
1264
+ const CapsuleCollider = /*#__PURE__*/React__default["default"].forwardRef((props, ref) => {
1189
1265
  return /*#__PURE__*/React__default["default"].createElement(AnyCollider, _extends({}, props, {
1190
- shape: "capsule"
1266
+ shape: "capsule",
1267
+ ref: ref
1191
1268
  }));
1192
- };
1193
- const HeightfieldCollider = props => {
1269
+ });
1270
+ const HeightfieldCollider = /*#__PURE__*/React__default["default"].forwardRef((props, ref) => {
1194
1271
  return /*#__PURE__*/React__default["default"].createElement(AnyCollider, _extends({}, props, {
1195
- shape: "heightfield"
1272
+ shape: "heightfield",
1273
+ ref: ref
1196
1274
  }));
1197
- };
1198
- const TrimeshCollider = props => {
1275
+ });
1276
+ const TrimeshCollider = /*#__PURE__*/React__default["default"].forwardRef((props, ref) => {
1199
1277
  return /*#__PURE__*/React__default["default"].createElement(AnyCollider, _extends({}, props, {
1200
- shape: "trimesh"
1278
+ shape: "trimesh",
1279
+ ref: ref
1201
1280
  }));
1202
- };
1203
- const ConeCollider = props => {
1281
+ });
1282
+ const ConeCollider = /*#__PURE__*/React__default["default"].forwardRef((props, ref) => {
1204
1283
  return /*#__PURE__*/React__default["default"].createElement(AnyCollider, _extends({}, props, {
1205
- shape: "cone"
1284
+ shape: "cone",
1285
+ ref: ref
1206
1286
  }));
1207
- };
1208
- const CylinderCollider = props => {
1287
+ });
1288
+ const CylinderCollider = /*#__PURE__*/React__default["default"].forwardRef((props, ref) => {
1209
1289
  return /*#__PURE__*/React__default["default"].createElement(AnyCollider, _extends({}, props, {
1210
- shape: "cylinder"
1290
+ shape: "cylinder",
1291
+ ref: ref
1211
1292
  }));
1212
- };
1213
- const ConvexHullCollider = props => {
1293
+ });
1294
+ const ConvexHullCollider = /*#__PURE__*/React__default["default"].forwardRef((props, ref) => {
1214
1295
  return /*#__PURE__*/React__default["default"].createElement(AnyCollider, _extends({}, props, {
1215
- shape: "convexHull"
1296
+ shape: "convexHull",
1297
+ ref: ref
1216
1298
  }));
1217
- };
1299
+ });
1218
1300
 
1219
1301
  const _excluded$1 = ["children", "type", "position", "rotation", "scale", "quaternion"];
1220
1302
  const RigidBodyContext = /*#__PURE__*/React.createContext(undefined);
1221
1303
  const useRigidBodyContext = () => React.useContext(RigidBodyContext);
1222
- const RigidBody = /*#__PURE__*/React.forwardRef((props, ref) => {
1304
+ const RigidBody = /*#__PURE__*/React.memo( /*#__PURE__*/React.forwardRef((props, ref) => {
1223
1305
  const {
1224
1306
  children,
1225
1307
  type,
@@ -1249,7 +1331,7 @@ const RigidBody = /*#__PURE__*/React.forwardRef((props, ref) => {
1249
1331
  }), children, childColliderProps.map((colliderProps, index) => /*#__PURE__*/React__default["default"].createElement(AnyCollider, _extends({
1250
1332
  key: index
1251
1333
  }, colliderProps)))));
1252
- });
1334
+ }));
1253
1335
 
1254
1336
  const MeshCollider = props => {
1255
1337
  const {
@@ -1290,7 +1372,7 @@ const geometryFromCollider = collider => {
1290
1372
  y,
1291
1373
  z
1292
1374
  } = collider.shape.halfExtents;
1293
- return new three.BoxBufferGeometry(x * 2 + 0.01, y * 2 + 0.01, z * 2 + 0.01);
1375
+ return new three.BoxGeometry(x * 2 + 0.01, y * 2 + 0.01, z * 2 + 0.01);
1294
1376
  }
1295
1377
 
1296
1378
  case rapier3dCompat.ShapeType.RoundCuboid:
@@ -1307,7 +1389,7 @@ const geometryFromCollider = collider => {
1307
1389
  case rapier3dCompat.ShapeType.Ball:
1308
1390
  {
1309
1391
  const r = collider.shape.radius;
1310
- return new three.SphereBufferGeometry(r + +0.01, 8, 8);
1392
+ return new three.SphereGeometry(r + +0.01, 8, 8);
1311
1393
  }
1312
1394
 
1313
1395
  case rapier3dCompat.ShapeType.TriMesh:
@@ -1339,7 +1421,7 @@ const geometryFromCollider = collider => {
1339
1421
  {
1340
1422
  const r = collider.shape.radius;
1341
1423
  const h = collider.shape.halfHeight;
1342
- const g = new three.CylinderBufferGeometry(r, r, h * 2);
1424
+ const g = new three.CylinderGeometry(r, r, h * 2);
1343
1425
  return g;
1344
1426
  }
1345
1427
 
@@ -1347,7 +1429,7 @@ const geometryFromCollider = collider => {
1347
1429
  {
1348
1430
  const r = collider.shape.radius;
1349
1431
  const h = collider.shape.halfHeight;
1350
- const g = new three.CapsuleBufferGeometry(r, h * 2, 4, 8);
1432
+ const g = new three.CapsuleGeometry(r, h * 2, 4, 8);
1351
1433
  return g;
1352
1434
  }
1353
1435
 
@@ -1355,7 +1437,7 @@ const geometryFromCollider = collider => {
1355
1437
  {
1356
1438
  const r = collider.shape.radius;
1357
1439
  const h = collider.shape.halfHeight;
1358
- const g = new three.ConeBufferGeometry(r, h * 2, 16);
1440
+ const g = new three.ConeGeometry(r, h * 2, 16);
1359
1441
  return g;
1360
1442
  }
1361
1443
 
@@ -1375,7 +1457,7 @@ const geometryFromCollider = collider => {
1375
1457
  }
1376
1458
  }
1377
1459
 
1378
- return new three.BoxBufferGeometry(1, 1, 1);
1460
+ return new three.BoxGeometry(1, 1, 1);
1379
1461
  };
1380
1462
 
1381
1463
  const DebugShape = /*#__PURE__*/React.memo(({