@react-three/rapier 0.7.3 → 0.7.5

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 @@ export { CoefficientCombineRule, Collider as RapierCollider, RigidBody as Rapier
3
3
  import React, { useState, useEffect, useRef, useMemo, createContext, useContext, memo, forwardRef, useImperativeHandle, useLayoutEffect } from 'react';
4
4
  import { useAsset } from 'use-asset';
5
5
  import { useFrame } from '@react-three/fiber';
6
- import { Quaternion, Euler, Vector3, Object3D, Matrix4, MathUtils, InstancedMesh, MeshBasicMaterial, Color, PlaneGeometry, ConeBufferGeometry, CapsuleBufferGeometry, CylinderBufferGeometry, BufferGeometry, BufferAttribute, SphereBufferGeometry, BoxBufferGeometry, DynamicDrawUsage } from 'three';
6
+ import { Quaternion, Euler, Vector3, Object3D, Matrix4, MathUtils, InstancedMesh, MeshBasicMaterial, Color, PlaneGeometry, ConeGeometry, CapsuleGeometry, CylinderGeometry, BufferGeometry, BufferAttribute, SphereGeometry, BoxGeometry, DynamicDrawUsage } from 'three';
7
7
  import { mergeVertices, RoundedBoxGeometry } from 'three-stdlib';
8
8
 
9
9
  const _quaternion = new Quaternion();
@@ -59,24 +59,24 @@ const createRigidBodyApi = ref => {
59
59
 
60
60
  mass: () => ref.current().mass(),
61
61
 
62
- applyImpulse(impulseVector) {
63
- ref.current().applyImpulse(impulseVector, true);
62
+ applyImpulse(impulseVector, wakeUp = true) {
63
+ ref.current().applyImpulse(impulseVector, wakeUp);
64
64
  },
65
65
 
66
- applyTorqueImpulse(torqueVector) {
67
- ref.current().applyTorqueImpulse(torqueVector, true);
66
+ applyTorqueImpulse(torqueVector, wakeUp = true) {
67
+ ref.current().applyTorqueImpulse(torqueVector, wakeUp);
68
68
  },
69
69
 
70
- applyImpulseAtPoint: (impulseVector, impulsePoint) => ref.current().applyImpulseAtPoint(impulseVector, impulsePoint, true),
71
- addForce: force => ref.current().addForce(force, true),
72
- addForceAtPoint: (force, point) => ref.current().addForceAtPoint(force, point, true),
73
- addTorque: torque => ref.current().addTorque(torque, true),
70
+ applyImpulseAtPoint: (impulseVector, impulsePoint, wakeUp = true) => ref.current().applyImpulseAtPoint(impulseVector, impulsePoint, wakeUp),
71
+ addForce: (force, wakeUp = true) => ref.current().addForce(force, wakeUp),
72
+ addForceAtPoint: (force, point, wakeUp = true) => ref.current().addForceAtPoint(force, point, wakeUp),
73
+ addTorque: (torque, wakeUp = true) => ref.current().addTorque(torque, wakeUp),
74
74
 
75
75
  translation() {
76
76
  return rapierVector3ToVector3(ref.current().translation());
77
77
  },
78
78
 
79
- setTranslation: translation => ref.current().setTranslation(translation, true),
79
+ setTranslation: (translation, wakeUp = true) => ref.current().setTranslation(translation, wakeUp),
80
80
 
81
81
  rotation() {
82
82
  const {
@@ -88,8 +88,8 @@ const createRigidBodyApi = ref => {
88
88
  return new Quaternion(x, y, z, w);
89
89
  },
90
90
 
91
- setRotation: rotation => {
92
- ref.current().setRotation(rotation, true);
91
+ setRotation: (rotation, wakeUp = true) => {
92
+ ref.current().setRotation(rotation, wakeUp);
93
93
  },
94
94
 
95
95
  linvel() {
@@ -101,7 +101,7 @@ const createRigidBodyApi = ref => {
101
101
  return new Vector3(x, y, z);
102
102
  },
103
103
 
104
- setLinvel: velocity => ref.current().setLinvel(velocity, true),
104
+ setLinvel: (velocity, wakeUp = true) => ref.current().setLinvel(velocity, wakeUp),
105
105
 
106
106
  angvel() {
107
107
  const {
@@ -112,7 +112,7 @@ const createRigidBodyApi = ref => {
112
112
  return new Vector3(x, y, z);
113
113
  },
114
114
 
115
- setAngvel: velocity => ref.current().setAngvel(velocity, true),
115
+ setAngvel: (velocity, wakeUp = true) => ref.current().setAngvel(velocity, wakeUp),
116
116
 
117
117
  linearDamping() {
118
118
  return ref.current().linearDamping();
@@ -129,12 +129,12 @@ const createRigidBodyApi = ref => {
129
129
  ref.current().setNextKinematicRotation(rotation);
130
130
  },
131
131
  setNextKinematicTranslation: translation => ref.current().setNextKinematicTranslation(translation),
132
- resetForces: () => ref.current().resetForces(true),
133
- resetTorques: () => ref.current().resetTorques(true),
134
- lockRotations: locked => ref.current().lockRotations(locked, true),
135
- lockTranslations: locked => ref.current().lockTranslations(locked, true),
136
- setEnabledRotations: (x, y, z) => ref.current().setEnabledRotations(x, y, z, true),
137
- setEnabledTranslations: (x, y, z) => ref.current().setEnabledTranslations(x, y, z, true)
132
+ resetForces: (wakeUp = true) => ref.current().resetForces(wakeUp),
133
+ resetTorques: (wakeUp = true) => ref.current().resetTorques(wakeUp),
134
+ lockRotations: (locked, wakeUp = true) => ref.current().lockRotations(locked, wakeUp),
135
+ lockTranslations: (locked, wakeUp = true) => ref.current().lockTranslations(locked, wakeUp),
136
+ setEnabledRotations: (x, y, z, wakeUp = true) => ref.current().setEnabledRotations(x, y, z, wakeUp),
137
+ setEnabledTranslations: (x, y, z, wakeUp = true) => ref.current().setEnabledTranslations(x, y, z, wakeUp)
138
138
  };
139
139
  };
140
140
  const createInstancedRigidBodiesApi = bodiesGetter => ({
@@ -157,9 +157,9 @@ const createWorldApi = ref => {
157
157
  createRigidBody: desc => ref.current().createRigidBody(desc),
158
158
  createCollider: (desc, rigidBody) => ref.current().createCollider(desc, rigidBody),
159
159
  removeRigidBody: rigidBody => ref.current().removeRigidBody(rigidBody),
160
- removeCollider: collider => ref.current().removeCollider(collider, true),
161
- createImpulseJoint: (params, rigidBodyA, rigidBodyB) => ref.current().createImpulseJoint(params, rigidBodyA, rigidBodyB, true),
162
- removeImpulseJoint: joint => ref.current().removeImpulseJoint(joint, true),
160
+ removeCollider: (collider, wakeUp = true) => ref.current().removeCollider(collider, wakeUp),
161
+ createImpulseJoint: (params, rigidBodyA, rigidBodyB, wakeUp = true) => ref.current().createImpulseJoint(params, rigidBodyA, rigidBodyB, wakeUp),
162
+ removeImpulseJoint: (joint, wakeUp = true) => ref.current().removeImpulseJoint(joint, wakeUp),
163
163
  forEachCollider: callback => ref.current().forEachCollider(callback),
164
164
  setGravity: ({
165
165
  x,
@@ -721,6 +721,32 @@ const createColliderFromOptions = (options, world, scale, rigidBody) => {
721
721
  const desc = ColliderDesc[options.shape](...scaledArgs);
722
722
  return world.createCollider(desc, rigidBody);
723
723
  };
724
+ const massPropertiesConflictError = "Please pick ONLY ONE of the `density`, `mass` and `massProperties` options.";
725
+
726
+ const setColliderMassOptions = (collider, options) => {
727
+ if (options.density !== undefined) {
728
+ if (options.mass !== undefined || options.massProperties !== undefined) {
729
+ throw new Error(massPropertiesConflictError);
730
+ }
731
+
732
+ collider.setDensity(options.density);
733
+ return;
734
+ }
735
+
736
+ if (options.mass !== undefined) {
737
+ if (options.massProperties !== undefined) {
738
+ throw new Error(massPropertiesConflictError);
739
+ }
740
+
741
+ collider.setMass(options.mass);
742
+ return;
743
+ }
744
+
745
+ if (options.massProperties !== undefined) {
746
+ collider.setMassProperties(options.massProperties.mass, options.massProperties.centerOfMass, options.massProperties.principalAngularInertia, options.massProperties.angularInertiaLocalFrame);
747
+ }
748
+ };
749
+
724
750
  const mutableColliderOptions = {
725
751
  sensor: (collider, value) => {
726
752
  collider.setSensor(value);
@@ -734,14 +760,14 @@ const mutableColliderOptions = {
734
760
  friction: (collider, value) => {
735
761
  collider.setFriction(value);
736
762
  },
763
+ frictionCombineRule: (collider, value) => {
764
+ collider.setFrictionCombineRule(value);
765
+ },
737
766
  restitution: (collider, value) => {
738
767
  collider.setRestitution(value);
739
768
  },
740
- density: (collider, value) => {
741
- collider.setDensity(value);
742
- },
743
- mass: (collider, value) => {
744
- collider.setMass(value);
769
+ restitutionCombineRule: (collider, value) => {
770
+ collider.setRestitutionCombineRule(value);
745
771
  }
746
772
  };
747
773
  const mutableColliderOptionKeys = Object.keys(mutableColliderOptions);
@@ -755,17 +781,32 @@ const setColliderOptions = (collider, options, states) => {
755
781
 
756
782
  _matrix4.copy(state.object.matrixWorld).premultiply(state.worldParent.matrixWorld.clone().invert()).decompose(_position, _rotation, _scale);
757
783
 
758
- collider.setTranslationWrtParent({
759
- x: _position.x * parentWorldScale.x,
760
- y: _position.y * parentWorldScale.y,
761
- z: _position.z * parentWorldScale.z
762
- });
763
- collider.setRotationWrtParent(_rotation);
784
+ if (collider.parent()) {
785
+ collider.setTranslationWrtParent({
786
+ x: _position.x * parentWorldScale.x,
787
+ y: _position.y * parentWorldScale.y,
788
+ z: _position.z * parentWorldScale.z
789
+ });
790
+ collider.setRotationWrtParent(_rotation);
791
+ } else {
792
+ collider.setTranslation({
793
+ x: _position.x * parentWorldScale.x,
794
+ y: _position.y * parentWorldScale.y,
795
+ z: _position.z * parentWorldScale.z
796
+ });
797
+ collider.setRotation(_rotation);
798
+ }
799
+
764
800
  mutableColliderOptionKeys.forEach(key => {
765
801
  if (key in options) {
766
- mutableColliderOptions[key](collider, options[key]);
802
+ const option = options[key];
803
+ mutableColliderOptions[key](collider, // @ts-ignore Option does not want to fit into the function, but it will
804
+ option, options);
767
805
  }
768
- });
806
+ }); // handle mass separately, because the assignments
807
+ // are exclusive.
808
+
809
+ setColliderMassOptions(collider, options);
769
810
  }
770
811
  };
771
812
  const useUpdateColliderOptions = (collidersRef, props, states) => {
@@ -1074,7 +1115,7 @@ const usePrismaticJoint = (body1, body2, [body1Anchor, body2Anchor, axis]) => {
1074
1115
  };
1075
1116
 
1076
1117
  // Colliders
1077
- const AnyCollider = /*#__PURE__*/memo(props => {
1118
+ const AnyCollider = /*#__PURE__*/memo( /*#__PURE__*/React.forwardRef((props, forwardedRef) => {
1078
1119
  const {
1079
1120
  children,
1080
1121
  position,
@@ -1089,7 +1130,15 @@ const AnyCollider = /*#__PURE__*/memo(props => {
1089
1130
  } = useRapier();
1090
1131
  const rigidBodyContext = useRigidBodyContext();
1091
1132
  const ref = useRef(null);
1092
- const collidersRef = useRef([]);
1133
+ const collidersRef = useMemo(() => {
1134
+ if (forwardedRef !== null) {
1135
+ return forwardedRef;
1136
+ }
1137
+
1138
+ const result = /*#__PURE__*/React.createRef();
1139
+ result.current = [];
1140
+ return result;
1141
+ }, []);
1093
1142
  useEffect(() => {
1094
1143
  const object = ref.current;
1095
1144
  const worldScale = object.getWorldScale(new Vector3());
@@ -1122,8 +1171,11 @@ const AnyCollider = /*#__PURE__*/memo(props => {
1122
1171
  });
1123
1172
  };
1124
1173
  }, []);
1125
- useUpdateColliderOptions(collidersRef, props, colliderStates);
1126
- useColliderEvents(collidersRef, props, colliderEvents);
1174
+ const mergedProps = useMemo(() => {
1175
+ return _objectSpread2(_objectSpread2({}, rigidBodyContext === null || rigidBodyContext === void 0 ? void 0 : rigidBodyContext.options), props);
1176
+ }, [props, rigidBodyContext === null || rigidBodyContext === void 0 ? void 0 : rigidBodyContext.options]);
1177
+ useUpdateColliderOptions(collidersRef, mergedProps, colliderStates);
1178
+ useColliderEvents(collidersRef, mergedProps, colliderEvents);
1127
1179
  return /*#__PURE__*/React.createElement("object3D", {
1128
1180
  position: position,
1129
1181
  rotation: rotation,
@@ -1131,52 +1183,61 @@ const AnyCollider = /*#__PURE__*/memo(props => {
1131
1183
  scale: scale,
1132
1184
  ref: ref
1133
1185
  }, children);
1134
- });
1135
- const CuboidCollider = props => {
1186
+ }));
1187
+ const CuboidCollider = /*#__PURE__*/React.forwardRef((props, ref) => {
1136
1188
  return /*#__PURE__*/React.createElement(AnyCollider, _extends({}, props, {
1137
- shape: "cuboid"
1189
+ shape: "cuboid",
1190
+ ref: ref
1138
1191
  }));
1139
- };
1140
- const RoundCuboidCollider = props => {
1192
+ });
1193
+ const RoundCuboidCollider = /*#__PURE__*/React.forwardRef((props, ref) => {
1141
1194
  return /*#__PURE__*/React.createElement(AnyCollider, _extends({}, props, {
1142
- shape: "roundCuboid"
1195
+ shape: "roundCuboid",
1196
+ ref: ref
1143
1197
  }));
1144
- };
1145
- const BallCollider = props => {
1198
+ });
1199
+ const BallCollider = /*#__PURE__*/React.forwardRef((props, ref) => {
1146
1200
  return /*#__PURE__*/React.createElement(AnyCollider, _extends({}, props, {
1147
- shape: "ball"
1201
+ shape: "ball",
1202
+ ref: ref
1148
1203
  }));
1149
- };
1150
- const CapsuleCollider = props => {
1204
+ });
1205
+ const CapsuleCollider = /*#__PURE__*/React.forwardRef((props, ref) => {
1151
1206
  return /*#__PURE__*/React.createElement(AnyCollider, _extends({}, props, {
1152
- shape: "capsule"
1207
+ shape: "capsule",
1208
+ ref: ref
1153
1209
  }));
1154
- };
1155
- const HeightfieldCollider = props => {
1210
+ });
1211
+ const HeightfieldCollider = /*#__PURE__*/React.forwardRef((props, ref) => {
1156
1212
  return /*#__PURE__*/React.createElement(AnyCollider, _extends({}, props, {
1157
- shape: "heightfield"
1213
+ shape: "heightfield",
1214
+ ref: ref
1158
1215
  }));
1159
- };
1160
- const TrimeshCollider = props => {
1216
+ });
1217
+ const TrimeshCollider = /*#__PURE__*/React.forwardRef((props, ref) => {
1161
1218
  return /*#__PURE__*/React.createElement(AnyCollider, _extends({}, props, {
1162
- shape: "trimesh"
1219
+ shape: "trimesh",
1220
+ ref: ref
1163
1221
  }));
1164
- };
1165
- const ConeCollider = props => {
1222
+ });
1223
+ const ConeCollider = /*#__PURE__*/React.forwardRef((props, ref) => {
1166
1224
  return /*#__PURE__*/React.createElement(AnyCollider, _extends({}, props, {
1167
- shape: "cone"
1225
+ shape: "cone",
1226
+ ref: ref
1168
1227
  }));
1169
- };
1170
- const CylinderCollider = props => {
1228
+ });
1229
+ const CylinderCollider = /*#__PURE__*/React.forwardRef((props, ref) => {
1171
1230
  return /*#__PURE__*/React.createElement(AnyCollider, _extends({}, props, {
1172
- shape: "cylinder"
1231
+ shape: "cylinder",
1232
+ ref: ref
1173
1233
  }));
1174
- };
1175
- const ConvexHullCollider = props => {
1234
+ });
1235
+ const ConvexHullCollider = /*#__PURE__*/React.forwardRef((props, ref) => {
1176
1236
  return /*#__PURE__*/React.createElement(AnyCollider, _extends({}, props, {
1177
- shape: "convexHull"
1237
+ shape: "convexHull",
1238
+ ref: ref
1178
1239
  }));
1179
- };
1240
+ });
1180
1241
 
1181
1242
  const _excluded$1 = ["children", "type", "position", "rotation", "scale", "quaternion"];
1182
1243
  const RigidBodyContext = /*#__PURE__*/createContext(undefined);
@@ -1252,7 +1313,7 @@ const geometryFromCollider = collider => {
1252
1313
  y,
1253
1314
  z
1254
1315
  } = collider.shape.halfExtents;
1255
- return new BoxBufferGeometry(x * 2 + 0.01, y * 2 + 0.01, z * 2 + 0.01);
1316
+ return new BoxGeometry(x * 2 + 0.01, y * 2 + 0.01, z * 2 + 0.01);
1256
1317
  }
1257
1318
 
1258
1319
  case ShapeType.RoundCuboid:
@@ -1269,7 +1330,7 @@ const geometryFromCollider = collider => {
1269
1330
  case ShapeType.Ball:
1270
1331
  {
1271
1332
  const r = collider.shape.radius;
1272
- return new SphereBufferGeometry(r + +0.01, 8, 8);
1333
+ return new SphereGeometry(r + +0.01, 8, 8);
1273
1334
  }
1274
1335
 
1275
1336
  case ShapeType.TriMesh:
@@ -1301,7 +1362,7 @@ const geometryFromCollider = collider => {
1301
1362
  {
1302
1363
  const r = collider.shape.radius;
1303
1364
  const h = collider.shape.halfHeight;
1304
- const g = new CylinderBufferGeometry(r, r, h * 2);
1365
+ const g = new CylinderGeometry(r, r, h * 2);
1305
1366
  return g;
1306
1367
  }
1307
1368
 
@@ -1309,7 +1370,7 @@ const geometryFromCollider = collider => {
1309
1370
  {
1310
1371
  const r = collider.shape.radius;
1311
1372
  const h = collider.shape.halfHeight;
1312
- const g = new CapsuleBufferGeometry(r, h * 2, 4, 8);
1373
+ const g = new CapsuleGeometry(r, h * 2, 4, 8);
1313
1374
  return g;
1314
1375
  }
1315
1376
 
@@ -1317,7 +1378,7 @@ const geometryFromCollider = collider => {
1317
1378
  {
1318
1379
  const r = collider.shape.radius;
1319
1380
  const h = collider.shape.halfHeight;
1320
- const g = new ConeBufferGeometry(r, h * 2, 16);
1381
+ const g = new ConeGeometry(r, h * 2, 16);
1321
1382
  return g;
1322
1383
  }
1323
1384
 
@@ -1337,7 +1398,7 @@ const geometryFromCollider = collider => {
1337
1398
  }
1338
1399
  }
1339
1400
 
1340
- return new BoxBufferGeometry(1, 1, 1);
1401
+ return new BoxGeometry(1, 1, 1);
1341
1402
  };
1342
1403
 
1343
1404
  const DebugShape = /*#__PURE__*/memo(({
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@react-three/rapier",
3
- "version": "0.7.3",
3
+ "version": "0.7.5",
4
4
  "source": "src/index.ts",
5
5
  "main": "dist/react-three-rapier.cjs.js",
6
6
  "module": "dist/react-three-rapier.esm.js",
@@ -11,11 +11,16 @@
11
11
  "devDependencies": {
12
12
  "@react-three/drei": "^9.6.2",
13
13
  "@react-three/fiber": "^8.0.12",
14
+ "@react-three/test-renderer": "^8.0.17",
14
15
  "@types/react-dom": "^18.0.2",
15
16
  "@types/three": "^0.139.0",
17
+ "@vitejs/plugin-react": "^2.1.0",
18
+ "@vitest/ui": "^0.23.4",
19
+ "happy-dom": "^6.0.4",
16
20
  "react": "^18.1.0",
17
21
  "react-dom": "^18.1.0",
18
- "three": "^0.139.2"
22
+ "three": "^0.139.2",
23
+ "vitest": "^0.23.4"
19
24
  },
20
25
  "peerDependencies": {
21
26
  "@react-three/fiber": "^8.0.12",
@@ -24,8 +29,8 @@
24
29
  },
25
30
  "dependencies": {
26
31
  "@dimforge/rapier3d-compat": "0.9.0",
27
- "use-asset": "^1.0.4",
28
- "three-stdlib": "^2.15.0"
32
+ "three-stdlib": "^2.15.0",
33
+ "use-asset": "^1.0.4"
29
34
  },
30
35
  "repository": "https://github.com/pmndrs/react-three-rapier/tree/master/packages/react-three-rapier"
31
36
  }
package/readme.md CHANGED
@@ -9,6 +9,8 @@
9
9
 
10
10
  <p align="center">⚠️ Under heavy development. All APIs are subject to change. ⚠️</p>
11
11
 
12
+ For contributions, please read the [contributing guide](https://github.com/pmndrs/react-three-rapier/blob/main/packages/react-three-rapier/CONTRIBUTING.md).
13
+
12
14
  ## Usage
13
15
 
14
16
  ```tsx
@@ -171,7 +173,7 @@ const Scene = () => {
171
173
  colliders="ball"
172
174
  >
173
175
  <instancedMesh args={[undefined, undefined, COUNT]}>
174
- <sphereBufferGeometry args={[0.2]} />
176
+ <sphereGeometry args={[0.2]} />
175
177
  <meshPhysicalGeometry color="blue" />
176
178
 
177
179
  <CuboidCollider args={[0.1, 0.2, 0.1]} />