@react-three/rapier 0.6.2 → 0.6.3

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.
@@ -2,10 +2,9 @@ import React from "react";
2
2
  import { InstancedRigidBodyApi } from "./api";
3
3
  import { RigidBodyProps } from "./RigidBody";
4
4
  import { Vector3Array } from "./types";
5
- interface InstancedRigidBodiesProps extends Omit<RigidBodyProps, "position" | "rotation" | "onCollisionEnter" | "onCollisionExit"> {
5
+ export interface InstancedRigidBodiesProps extends Omit<RigidBodyProps, "position" | "rotation" | "onCollisionEnter" | "onCollisionExit"> {
6
6
  positions?: Vector3Array[];
7
7
  rotations?: Vector3Array[];
8
8
  scales?: Vector3Array[];
9
9
  }
10
10
  export declare const InstancedRigidBodies: React.ForwardRefExoticComponent<InstancedRigidBodiesProps & React.RefAttributes<InstancedRigidBodyApi>>;
11
- export {};
@@ -59,6 +59,12 @@ interface RapierWorldProps {
59
59
  * @defaultValue "vary"
60
60
  */
61
61
  timeStep?: number | "vary";
62
+ /**
63
+ * Pause the physics simulation
64
+ *
65
+ * @defaultValue false
66
+ */
67
+ paused: boolean;
62
68
  }
63
69
  export declare const Physics: FC<RapierWorldProps>;
64
70
  export {};
@@ -1,12 +1,20 @@
1
- import React, { MutableRefObject } from "react";
1
+ import React, { MutableRefObject, RefObject } from "react";
2
2
  import { ReactNode } from "react";
3
3
  import { Object3D } from "three";
4
+ import { InstancedRigidBodyApi } from "./api";
5
+ import { InstancedRigidBodiesProps } from "./InstancedRigidBodies";
4
6
  import { RigidBodyApi, UseRigidBodyOptions } from "./types";
7
+ export declare const RigidBodyContext: React.Context<{
8
+ ref: RefObject<Object3D> | MutableRefObject<Object3D>;
9
+ api: RigidBodyApi | InstancedRigidBodyApi;
10
+ hasCollisionEvents: boolean;
11
+ options: UseRigidBodyOptions | InstancedRigidBodiesProps;
12
+ }>;
5
13
  export declare const useRigidBodyContext: () => {
6
- ref: MutableRefObject<Object3D>;
7
- api: RigidBodyApi;
14
+ ref: RefObject<Object3D> | MutableRefObject<Object3D>;
15
+ api: RigidBodyApi | InstancedRigidBodyApi;
8
16
  hasCollisionEvents: boolean;
9
- options: UseRigidBodyOptions;
17
+ options: UseRigidBodyOptions | InstancedRigidBodiesProps;
10
18
  };
11
19
  export interface RigidBodyProps extends UseRigidBodyOptions {
12
20
  children?: ReactNode;
@@ -75,6 +75,22 @@ export interface RigidBodyApi {
75
75
  * Sets the angular velocity of this rigid-body.
76
76
  */
77
77
  setAngvel(velocity: Vector3Object): void;
78
+ /**
79
+ * The linear damping of this rigid-body.
80
+ */
81
+ linearDamping(): number;
82
+ /**
83
+ * Sets the linear damping factor applied to this rigid-body.
84
+ */
85
+ setLinearDamping(factor: number): void;
86
+ /**
87
+ * The angular damping of this rigid-body.
88
+ */
89
+ angularDamping(): number;
90
+ /**
91
+ * Sets the anugular damping factor applied to this rigid-body.
92
+ */
93
+ setAngularDamping(factor: number): void;
78
94
  /**
79
95
  * If this rigid body is kinematic, sets its future rotation after the next timestep integration.
80
96
  *
@@ -1,8 +1,8 @@
1
1
  import { MutableRefObject } from "react";
2
- import { CoefficientCombineRule, RigidBody as RapierRigidBody, Collider as RapierCollider, TempContactManifold } from "@dimforge/rapier3d-compat";
2
+ import { CoefficientCombineRule, Collider as RapierCollider, RigidBody as RapierRigidBody, TempContactManifold } from "@dimforge/rapier3d-compat";
3
3
  import { createColliderApi, createJointApi, createRigidBodyApi, createWorldApi } from "./api";
4
- export { RapierRigidBody, RapierCollider };
5
4
  export { CoefficientCombineRule as CoefficientCombineRule } from "@dimforge/rapier3d-compat";
5
+ export { RapierRigidBody, RapierCollider };
6
6
  export declare type RefGetter<T> = MutableRefObject<() => T | undefined>;
7
7
  export declare type RigidBodyAutoCollider = "ball" | "cuboid" | "hull" | "trimesh" | false;
8
8
  export interface UseRigidBodyAPI {
@@ -126,6 +126,10 @@ export interface UseRigidBodyOptions {
126
126
  * default: true
127
127
  */
128
128
  canSleep?: boolean;
129
+ /** The linear damping coefficient of this rigid-body.*/
130
+ linearDamping?: number;
131
+ /** The angular damping coefficient of this rigid-body.*/
132
+ angularDamping?: number;
129
133
  /** The linear velocity of this body.
130
134
  * default: zero velocity
131
135
  */
@@ -1,4 +1,4 @@
1
- import { Collider, ColliderDesc, RigidBody, RigidBodyDesc, Vector3 as RapierVector3, Quaternion as RapierQuaternion } from "@dimforge/rapier3d-compat";
1
+ import { Collider, ColliderDesc, Quaternion as RapierQuaternion, RigidBody, RigidBodyDesc, Vector3 as RapierVector3 } from "@dimforge/rapier3d-compat";
2
2
  import { BufferGeometry, Matrix4, Object3D, Quaternion, Vector3 } from "three";
3
3
  import { RigidBodyApi, RigidBodyAutoCollider, RigidBodyShape, RigidBodyTypeString, UseColliderOptions, UseRigidBodyOptions, Vector3Array, WorldApi } from "./types";
4
4
  export declare const vectorArrayToVector3: (arr: Vector3Array) => Vector3;
@@ -26,7 +26,16 @@ interface CreateColliderFromOptions {
26
26
  }): Collider;
27
27
  }
28
28
  export declare const createColliderFromOptions: CreateColliderFromOptions;
29
- export declare const createCollidersFromChildren: (object: Object3D, rigidBody: RigidBodyApi | RigidBody, options: UseRigidBodyOptions, world: WorldApi, ignoreMeshColliders?: boolean) => Collider[];
29
+ interface CreateCollidersFromChildren {
30
+ (options: {
31
+ object: Object3D;
32
+ rigidBody?: Pick<RigidBodyApi | RigidBody, "handle">;
33
+ options: UseRigidBodyOptions;
34
+ world: WorldApi;
35
+ ignoreMeshColliders: boolean;
36
+ }): Collider[];
37
+ }
38
+ export declare const createCollidersFromChildren: CreateCollidersFromChildren;
30
39
  export declare const colliderDescFromGeometry: (geometry: BufferGeometry, colliders: RigidBodyAutoCollider, scale: Vector3, hasCollisionEvents: boolean) => ColliderDesc;
31
40
  export declare const scaleVertices: (vertices: ArrayLike<number>, scale: Vector3) => number[];
32
41
  export declare const rigidBodyDescFromOptions: (options: UseRigidBodyOptions) => RigidBodyDesc;
@@ -154,13 +154,19 @@ const isChildOfMeshCollider = child => {
154
154
  return flag;
155
155
  };
156
156
 
157
- const createCollidersFromChildren = (object, rigidBody, options, world, ignoreMeshColliders = true) => {
157
+ const createCollidersFromChildren = ({
158
+ object,
159
+ rigidBody,
160
+ options,
161
+ world,
162
+ ignoreMeshColliders: _ignoreMeshColliders = true
163
+ }) => {
158
164
  const hasCollisionEvents = !!(options.onCollisionEnter || options.onCollisionExit);
159
165
  const colliders = [];
160
166
  new three.Vector3();
161
- object.traverse(child => {
167
+ object.traverseVisible(child => {
162
168
  if ("isMesh" in child) {
163
- if (ignoreMeshColliders && isChildOfMeshCollider(child)) return;
169
+ if (_ignoreMeshColliders && isChildOfMeshCollider(child)) return;
164
170
  const {
165
171
  geometry
166
172
  } = child;
@@ -201,7 +207,7 @@ const createCollidersFromChildren = (object, rigidBody, options, world, ignoreMe
201
207
  z: rz,
202
208
  w: rw
203
209
  });
204
- const actualRigidBody = world.getRigidBody(rigidBody === null || rigidBody === void 0 ? void 0 : rigidBody.handle);
210
+ const actualRigidBody = rigidBody ? world.getRigidBody(rigidBody.handle) : undefined;
205
211
  const collider = world.createCollider(desc, actualRigidBody);
206
212
  colliders.push(collider);
207
213
  }
@@ -267,11 +273,13 @@ const scaleVertices = (vertices, scale) => {
267
273
  return scaledVerts;
268
274
  };
269
275
  const rigidBodyDescFromOptions = options => {
270
- var _options$linearVeloci, _options$angularVeloc, _options$gravityScale, _options$canSleep, _options$ccd, _options$enabledRotat, _options$enabledTrans;
276
+ var _options$linearVeloci, _options$angularVeloc, _options$angularDampi, _options$linearDampin, _options$gravityScale, _options$canSleep, _options$ccd, _options$enabledRotat, _options$enabledTrans;
271
277
 
272
278
  const type = rigidBodyTypeFromString((options === null || options === void 0 ? void 0 : options.type) || "dynamic");
273
279
  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];
274
280
  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];
281
+ const angularDamping = (_options$angularDampi = options === null || options === void 0 ? void 0 : options.angularDamping) !== null && _options$angularDampi !== void 0 ? _options$angularDampi : 0;
282
+ const linearDamping = (_options$linearDampin = options === null || options === void 0 ? void 0 : options.linearDamping) !== null && _options$linearDampin !== void 0 ? _options$linearDampin : 0;
275
283
  const gravityScale = (_options$gravityScale = options === null || options === void 0 ? void 0 : options.gravityScale) !== null && _options$gravityScale !== void 0 ? _options$gravityScale : 1;
276
284
  const canSleep = (_options$canSleep = options === null || options === void 0 ? void 0 : options.canSleep) !== null && _options$canSleep !== void 0 ? _options$canSleep : true;
277
285
  const ccdEnabled = (_options$ccd = options === null || options === void 0 ? void 0 : options.ccd) !== null && _options$ccd !== void 0 ? _options$ccd : false;
@@ -281,7 +289,7 @@ const rigidBodyDescFromOptions = options => {
281
289
  x: avx,
282
290
  y: avy,
283
291
  z: avz
284
- }).setGravityScale(gravityScale).setCanSleep(canSleep).setCcdEnabled(ccdEnabled).enabledRotations(erx, ery, erz).enabledTranslations(etx, ety, etz);
292
+ }).setLinearDamping(linearDamping).setAngularDamping(angularDamping).setGravityScale(gravityScale).setCanSleep(canSleep).setCcdEnabled(ccdEnabled).enabledRotations(erx, ery, erz).enabledTranslations(etx, ety, etz);
285
293
  if (options.lockRotations) desc.lockRotations();
286
294
  if (options.lockTranslations) desc.lockTranslations();
287
295
  return desc;
@@ -366,6 +374,18 @@ const createRigidBodyApi = ref => {
366
374
  },
367
375
 
368
376
  setAngvel: velocity => ref.current().setAngvel(velocity, true),
377
+
378
+ linearDamping() {
379
+ return ref.current().linearDamping();
380
+ },
381
+
382
+ setLinearDamping: factor => ref.current().setLinearDamping(factor),
383
+
384
+ angularDamping() {
385
+ return ref.current().angularDamping();
386
+ },
387
+
388
+ setAngularDamping: factor => ref.current().setAngularDamping(factor),
369
389
  setNextKinematicRotation: ({
370
390
  x,
371
391
  y,
@@ -449,7 +469,8 @@ const Physics = ({
449
469
  colliders: _colliders = "cuboid",
450
470
  gravity: _gravity = [0, -9.81, 0],
451
471
  children,
452
- timeStep: _timeStep = "vary"
472
+ timeStep: _timeStep = "vary",
473
+ paused: _paused = false
453
474
  }) => {
454
475
  const rapier = useAsset.useAsset(importRapier);
455
476
  const worldRef = React.useRef();
@@ -498,7 +519,7 @@ const Physics = ({
498
519
  world.timestep = _timeStep;
499
520
  }
500
521
 
501
- world.step(eventQueue); // Update meshes
522
+ if (!_paused) world.step(eventQueue); // Update meshes
502
523
 
503
524
  rigidBodyStates.forEach((state, handle) => {
504
525
  const rigidBody = world.getRigidBody(handle);
@@ -730,9 +751,15 @@ const useRigidBody = (options = {}) => {
730
751
  rigidBody.resetForces(false);
731
752
  rigidBody.resetTorques(false);
732
753
  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;
733
- const autoColliders = colliderSetting !== false ? createCollidersFromChildren(ref.current, rigidBody, _objectSpread2(_objectSpread2({}, options), {}, {
734
- colliders: colliderSetting
735
- }), world) : [];
754
+ const autoColliders = colliderSetting !== false ? createCollidersFromChildren({
755
+ object: ref.current,
756
+ rigidBody,
757
+ options: _objectSpread2(_objectSpread2({}, options), {}, {
758
+ colliders: colliderSetting
759
+ }),
760
+ world,
761
+ ignoreMeshColliders: true
762
+ }) : [];
736
763
  rigidBodyStates.set(rigidBody.handle, {
737
764
  mesh: ref.current,
738
765
  invertedMatrixWorld: ref.current.parent.matrixWorld.clone().invert(),
@@ -897,9 +924,18 @@ const MeshCollider = ({
897
924
  var _ref;
898
925
 
899
926
  const colliderSetting = (_ref = type !== null && type !== void 0 ? type : physicsOptions.colliders) !== null && _ref !== void 0 ? _ref : false;
900
- autoColliders = colliderSetting !== false ? createCollidersFromChildren(object.current, api, _objectSpread2(_objectSpread2({}, options), {}, {
901
- colliders: colliderSetting
902
- }), world, false) : [];
927
+
928
+ if ("raw" in api) {
929
+ autoColliders = createCollidersFromChildren({
930
+ object: object.current,
931
+ rigidBody: api,
932
+ options: _objectSpread2(_objectSpread2({}, options), {}, {
933
+ colliders: colliderSetting
934
+ }),
935
+ world,
936
+ ignoreMeshColliders: false
937
+ });
938
+ }
903
939
  }
904
940
 
905
941
  return () => {
@@ -1046,7 +1082,7 @@ const InstancedRigidBodies = /*#__PURE__*/React.forwardRef((props, ref) => {
1046
1082
 
1047
1083
  return instancesRef.current;
1048
1084
  });
1049
- React.useEffect(() => {
1085
+ React.useLayoutEffect(() => {
1050
1086
  const colliders = [];
1051
1087
  const rigidBodies = instancesRefGetter.current();
1052
1088
 
@@ -1072,15 +1108,21 @@ const InstancedRigidBodies = /*#__PURE__*/React.forwardRef((props, ref) => {
1072
1108
  scale.multiply(s);
1073
1109
  }
1074
1110
 
1075
- const colliderDesc = colliderDescFromGeometry(mesh.geometry, props.colliders || physicsOptions.colliders, scale, false // Collisions currently not enabled for instances
1076
- );
1077
1111
  const rigidBody = world.createRigidBody(rigidBodyDesc);
1078
1112
  const matrix = new three.Matrix4();
1079
1113
  mesh.getMatrixAt(index, matrix);
1080
1114
  const {
1081
1115
  position,
1082
1116
  rotation
1083
- } = decomposeMatrix4(matrix); // Set positions
1117
+ } = decomposeMatrix4(matrix);
1118
+
1119
+ if (props.colliders !== false) {
1120
+ const colliderDesc = colliderDescFromGeometry(mesh.geometry, props.colliders !== undefined ? props.colliders : physicsOptions.colliders, scale, false // Collisions currently not enabled for instances
1121
+ );
1122
+ const collider = world.createCollider(colliderDesc, rigidBody);
1123
+ colliders.push(collider);
1124
+ } // Set positions
1125
+
1084
1126
 
1085
1127
  if (props.positions && props.positions[index]) {
1086
1128
  rigidBody.setTranslation(vectorArrayToVector3(props.positions[index]), true);
@@ -1096,7 +1138,6 @@ const InstancedRigidBodies = /*#__PURE__*/React.forwardRef((props, ref) => {
1096
1138
  rigidBody.setRotation(rotation, true);
1097
1139
  }
1098
1140
 
1099
- const collider = world.createCollider(colliderDesc, rigidBody);
1100
1141
  rigidBodyStates.set(rigidBody.handle, {
1101
1142
  mesh: mesh,
1102
1143
  isSleeping: false,
@@ -1117,7 +1158,6 @@ const InstancedRigidBodies = /*#__PURE__*/React.forwardRef((props, ref) => {
1117
1158
  }
1118
1159
 
1119
1160
  });
1120
- colliders.push(collider);
1121
1161
  rigidBodies.push({
1122
1162
  rigidBody,
1123
1163
  api
@@ -1136,10 +1176,19 @@ const InstancedRigidBodies = /*#__PURE__*/React.forwardRef((props, ref) => {
1136
1176
  };
1137
1177
  }
1138
1178
  }, []);
1139
- React.useImperativeHandle(ref, () => createInstancedRigidBodiesApi(instancesRefGetter));
1140
- return /*#__PURE__*/React__default["default"].createElement("object3D", {
1179
+ const api = React.useMemo(() => createInstancedRigidBodiesApi(instancesRefGetter), []);
1180
+ React.useImperativeHandle(ref, () => api); // console.log(api);
1181
+
1182
+ return /*#__PURE__*/React__default["default"].createElement(RigidBodyContext.Provider, {
1183
+ value: {
1184
+ ref: object,
1185
+ api,
1186
+ hasCollisionEvents: false,
1187
+ options: props
1188
+ }
1189
+ }, /*#__PURE__*/React__default["default"].createElement("object3D", {
1141
1190
  ref: object
1142
- }, props.children);
1191
+ }, props.children));
1143
1192
  });
1144
1193
 
1145
1194
  function _extends() {
@@ -1174,18 +1223,40 @@ const AnyCollider = _ref => {
1174
1223
  const rigidBodyContext = useRigidBodyContext();
1175
1224
  const ref = React.useRef(null);
1176
1225
  React.useEffect(() => {
1177
- var _rigidBodyContext$api;
1178
-
1179
1226
  const scale = ref.current.getWorldScale(new three.Vector3());
1180
- const collider = createColliderFromOptions({
1181
- options: props,
1182
- world,
1183
- rigidBody: rigidBodyContext === null || rigidBodyContext === void 0 ? void 0 : (_rigidBodyContext$api = rigidBodyContext.api) === null || _rigidBodyContext$api === void 0 ? void 0 : _rigidBodyContext$api.raw(),
1184
- scale,
1185
- hasCollisionEvents: rigidBodyContext === null || rigidBodyContext === void 0 ? void 0 : rigidBodyContext.hasCollisionEvents
1186
- });
1227
+ const colliders = []; // If this is an InstancedRigidBody api
1228
+
1229
+ if (rigidBodyContext && "at" in rigidBodyContext.api) {
1230
+ rigidBodyContext.api.forEach((body, index) => {
1231
+ var _rigidBodyContext$opt, _rigidBodyContext$opt2;
1232
+
1233
+ let instanceScale = scale.clone();
1234
+
1235
+ if ("scales" in rigidBodyContext.options && rigidBodyContext !== null && rigidBodyContext !== void 0 && (_rigidBodyContext$opt = rigidBodyContext.options) !== null && _rigidBodyContext$opt !== void 0 && (_rigidBodyContext$opt2 = _rigidBodyContext$opt.scales) !== null && _rigidBodyContext$opt2 !== void 0 && _rigidBodyContext$opt2[index]) {
1236
+ instanceScale.multiply(vectorArrayToVector3(rigidBodyContext.options.scales[index]));
1237
+ }
1238
+
1239
+ colliders.push(createColliderFromOptions({
1240
+ options: props,
1241
+ world,
1242
+ rigidBody: body.raw(),
1243
+ scale: instanceScale,
1244
+ hasCollisionEvents: rigidBodyContext === null || rigidBodyContext === void 0 ? void 0 : rigidBodyContext.hasCollisionEvents
1245
+ }));
1246
+ });
1247
+ } else {
1248
+ colliders.push(createColliderFromOptions({
1249
+ options: props,
1250
+ world,
1251
+ // Initiate with a rigidbody, or undefined, because colliders can exist without a rigid body
1252
+ rigidBody: rigidBodyContext && "raw" in rigidBodyContext.api ? rigidBodyContext.api.raw() : undefined,
1253
+ scale,
1254
+ hasCollisionEvents: rigidBodyContext === null || rigidBodyContext === void 0 ? void 0 : rigidBodyContext.hasCollisionEvents
1255
+ }));
1256
+ }
1257
+
1187
1258
  return () => {
1188
- world.removeCollider(collider);
1259
+ colliders.forEach(collider => world.removeCollider(collider));
1189
1260
  };
1190
1261
  }, []);
1191
1262
  return /*#__PURE__*/React__default["default"].createElement("object3D", {
@@ -154,13 +154,19 @@ const isChildOfMeshCollider = child => {
154
154
  return flag;
155
155
  };
156
156
 
157
- const createCollidersFromChildren = (object, rigidBody, options, world, ignoreMeshColliders = true) => {
157
+ const createCollidersFromChildren = ({
158
+ object,
159
+ rigidBody,
160
+ options,
161
+ world,
162
+ ignoreMeshColliders: _ignoreMeshColliders = true
163
+ }) => {
158
164
  const hasCollisionEvents = !!(options.onCollisionEnter || options.onCollisionExit);
159
165
  const colliders = [];
160
166
  new three.Vector3();
161
- object.traverse(child => {
167
+ object.traverseVisible(child => {
162
168
  if ("isMesh" in child) {
163
- if (ignoreMeshColliders && isChildOfMeshCollider(child)) return;
169
+ if (_ignoreMeshColliders && isChildOfMeshCollider(child)) return;
164
170
  const {
165
171
  geometry
166
172
  } = child;
@@ -201,7 +207,7 @@ const createCollidersFromChildren = (object, rigidBody, options, world, ignoreMe
201
207
  z: rz,
202
208
  w: rw
203
209
  });
204
- const actualRigidBody = world.getRigidBody(rigidBody === null || rigidBody === void 0 ? void 0 : rigidBody.handle);
210
+ const actualRigidBody = rigidBody ? world.getRigidBody(rigidBody.handle) : undefined;
205
211
  const collider = world.createCollider(desc, actualRigidBody);
206
212
  colliders.push(collider);
207
213
  }
@@ -267,11 +273,13 @@ const scaleVertices = (vertices, scale) => {
267
273
  return scaledVerts;
268
274
  };
269
275
  const rigidBodyDescFromOptions = options => {
270
- var _options$linearVeloci, _options$angularVeloc, _options$gravityScale, _options$canSleep, _options$ccd, _options$enabledRotat, _options$enabledTrans;
276
+ var _options$linearVeloci, _options$angularVeloc, _options$angularDampi, _options$linearDampin, _options$gravityScale, _options$canSleep, _options$ccd, _options$enabledRotat, _options$enabledTrans;
271
277
 
272
278
  const type = rigidBodyTypeFromString((options === null || options === void 0 ? void 0 : options.type) || "dynamic");
273
279
  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];
274
280
  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];
281
+ const angularDamping = (_options$angularDampi = options === null || options === void 0 ? void 0 : options.angularDamping) !== null && _options$angularDampi !== void 0 ? _options$angularDampi : 0;
282
+ const linearDamping = (_options$linearDampin = options === null || options === void 0 ? void 0 : options.linearDamping) !== null && _options$linearDampin !== void 0 ? _options$linearDampin : 0;
275
283
  const gravityScale = (_options$gravityScale = options === null || options === void 0 ? void 0 : options.gravityScale) !== null && _options$gravityScale !== void 0 ? _options$gravityScale : 1;
276
284
  const canSleep = (_options$canSleep = options === null || options === void 0 ? void 0 : options.canSleep) !== null && _options$canSleep !== void 0 ? _options$canSleep : true;
277
285
  const ccdEnabled = (_options$ccd = options === null || options === void 0 ? void 0 : options.ccd) !== null && _options$ccd !== void 0 ? _options$ccd : false;
@@ -281,7 +289,7 @@ const rigidBodyDescFromOptions = options => {
281
289
  x: avx,
282
290
  y: avy,
283
291
  z: avz
284
- }).setGravityScale(gravityScale).setCanSleep(canSleep).setCcdEnabled(ccdEnabled).enabledRotations(erx, ery, erz).enabledTranslations(etx, ety, etz);
292
+ }).setLinearDamping(linearDamping).setAngularDamping(angularDamping).setGravityScale(gravityScale).setCanSleep(canSleep).setCcdEnabled(ccdEnabled).enabledRotations(erx, ery, erz).enabledTranslations(etx, ety, etz);
285
293
  if (options.lockRotations) desc.lockRotations();
286
294
  if (options.lockTranslations) desc.lockTranslations();
287
295
  return desc;
@@ -366,6 +374,18 @@ const createRigidBodyApi = ref => {
366
374
  },
367
375
 
368
376
  setAngvel: velocity => ref.current().setAngvel(velocity, true),
377
+
378
+ linearDamping() {
379
+ return ref.current().linearDamping();
380
+ },
381
+
382
+ setLinearDamping: factor => ref.current().setLinearDamping(factor),
383
+
384
+ angularDamping() {
385
+ return ref.current().angularDamping();
386
+ },
387
+
388
+ setAngularDamping: factor => ref.current().setAngularDamping(factor),
369
389
  setNextKinematicRotation: ({
370
390
  x,
371
391
  y,
@@ -449,7 +469,8 @@ const Physics = ({
449
469
  colliders: _colliders = "cuboid",
450
470
  gravity: _gravity = [0, -9.81, 0],
451
471
  children,
452
- timeStep: _timeStep = "vary"
472
+ timeStep: _timeStep = "vary",
473
+ paused: _paused = false
453
474
  }) => {
454
475
  const rapier = useAsset.useAsset(importRapier);
455
476
  const worldRef = React.useRef();
@@ -498,7 +519,7 @@ const Physics = ({
498
519
  world.timestep = _timeStep;
499
520
  }
500
521
 
501
- world.step(eventQueue); // Update meshes
522
+ if (!_paused) world.step(eventQueue); // Update meshes
502
523
 
503
524
  rigidBodyStates.forEach((state, handle) => {
504
525
  const rigidBody = world.getRigidBody(handle);
@@ -730,9 +751,15 @@ const useRigidBody = (options = {}) => {
730
751
  rigidBody.resetForces(false);
731
752
  rigidBody.resetTorques(false);
732
753
  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;
733
- const autoColliders = colliderSetting !== false ? createCollidersFromChildren(ref.current, rigidBody, _objectSpread2(_objectSpread2({}, options), {}, {
734
- colliders: colliderSetting
735
- }), world) : [];
754
+ const autoColliders = colliderSetting !== false ? createCollidersFromChildren({
755
+ object: ref.current,
756
+ rigidBody,
757
+ options: _objectSpread2(_objectSpread2({}, options), {}, {
758
+ colliders: colliderSetting
759
+ }),
760
+ world,
761
+ ignoreMeshColliders: true
762
+ }) : [];
736
763
  rigidBodyStates.set(rigidBody.handle, {
737
764
  mesh: ref.current,
738
765
  invertedMatrixWorld: ref.current.parent.matrixWorld.clone().invert(),
@@ -897,9 +924,18 @@ const MeshCollider = ({
897
924
  var _ref;
898
925
 
899
926
  const colliderSetting = (_ref = type !== null && type !== void 0 ? type : physicsOptions.colliders) !== null && _ref !== void 0 ? _ref : false;
900
- autoColliders = colliderSetting !== false ? createCollidersFromChildren(object.current, api, _objectSpread2(_objectSpread2({}, options), {}, {
901
- colliders: colliderSetting
902
- }), world, false) : [];
927
+
928
+ if ("raw" in api) {
929
+ autoColliders = createCollidersFromChildren({
930
+ object: object.current,
931
+ rigidBody: api,
932
+ options: _objectSpread2(_objectSpread2({}, options), {}, {
933
+ colliders: colliderSetting
934
+ }),
935
+ world,
936
+ ignoreMeshColliders: false
937
+ });
938
+ }
903
939
  }
904
940
 
905
941
  return () => {
@@ -1046,7 +1082,7 @@ const InstancedRigidBodies = /*#__PURE__*/React.forwardRef((props, ref) => {
1046
1082
 
1047
1083
  return instancesRef.current;
1048
1084
  });
1049
- React.useEffect(() => {
1085
+ React.useLayoutEffect(() => {
1050
1086
  const colliders = [];
1051
1087
  const rigidBodies = instancesRefGetter.current();
1052
1088
 
@@ -1072,15 +1108,21 @@ const InstancedRigidBodies = /*#__PURE__*/React.forwardRef((props, ref) => {
1072
1108
  scale.multiply(s);
1073
1109
  }
1074
1110
 
1075
- const colliderDesc = colliderDescFromGeometry(mesh.geometry, props.colliders || physicsOptions.colliders, scale, false // Collisions currently not enabled for instances
1076
- );
1077
1111
  const rigidBody = world.createRigidBody(rigidBodyDesc);
1078
1112
  const matrix = new three.Matrix4();
1079
1113
  mesh.getMatrixAt(index, matrix);
1080
1114
  const {
1081
1115
  position,
1082
1116
  rotation
1083
- } = decomposeMatrix4(matrix); // Set positions
1117
+ } = decomposeMatrix4(matrix);
1118
+
1119
+ if (props.colliders !== false) {
1120
+ const colliderDesc = colliderDescFromGeometry(mesh.geometry, props.colliders !== undefined ? props.colliders : physicsOptions.colliders, scale, false // Collisions currently not enabled for instances
1121
+ );
1122
+ const collider = world.createCollider(colliderDesc, rigidBody);
1123
+ colliders.push(collider);
1124
+ } // Set positions
1125
+
1084
1126
 
1085
1127
  if (props.positions && props.positions[index]) {
1086
1128
  rigidBody.setTranslation(vectorArrayToVector3(props.positions[index]), true);
@@ -1096,7 +1138,6 @@ const InstancedRigidBodies = /*#__PURE__*/React.forwardRef((props, ref) => {
1096
1138
  rigidBody.setRotation(rotation, true);
1097
1139
  }
1098
1140
 
1099
- const collider = world.createCollider(colliderDesc, rigidBody);
1100
1141
  rigidBodyStates.set(rigidBody.handle, {
1101
1142
  mesh: mesh,
1102
1143
  isSleeping: false,
@@ -1117,7 +1158,6 @@ const InstancedRigidBodies = /*#__PURE__*/React.forwardRef((props, ref) => {
1117
1158
  }
1118
1159
 
1119
1160
  });
1120
- colliders.push(collider);
1121
1161
  rigidBodies.push({
1122
1162
  rigidBody,
1123
1163
  api
@@ -1136,10 +1176,19 @@ const InstancedRigidBodies = /*#__PURE__*/React.forwardRef((props, ref) => {
1136
1176
  };
1137
1177
  }
1138
1178
  }, []);
1139
- React.useImperativeHandle(ref, () => createInstancedRigidBodiesApi(instancesRefGetter));
1140
- return /*#__PURE__*/React__default["default"].createElement("object3D", {
1179
+ const api = React.useMemo(() => createInstancedRigidBodiesApi(instancesRefGetter), []);
1180
+ React.useImperativeHandle(ref, () => api); // console.log(api);
1181
+
1182
+ return /*#__PURE__*/React__default["default"].createElement(RigidBodyContext.Provider, {
1183
+ value: {
1184
+ ref: object,
1185
+ api,
1186
+ hasCollisionEvents: false,
1187
+ options: props
1188
+ }
1189
+ }, /*#__PURE__*/React__default["default"].createElement("object3D", {
1141
1190
  ref: object
1142
- }, props.children);
1191
+ }, props.children));
1143
1192
  });
1144
1193
 
1145
1194
  function _extends() {
@@ -1174,18 +1223,40 @@ const AnyCollider = _ref => {
1174
1223
  const rigidBodyContext = useRigidBodyContext();
1175
1224
  const ref = React.useRef(null);
1176
1225
  React.useEffect(() => {
1177
- var _rigidBodyContext$api;
1178
-
1179
1226
  const scale = ref.current.getWorldScale(new three.Vector3());
1180
- const collider = createColliderFromOptions({
1181
- options: props,
1182
- world,
1183
- rigidBody: rigidBodyContext === null || rigidBodyContext === void 0 ? void 0 : (_rigidBodyContext$api = rigidBodyContext.api) === null || _rigidBodyContext$api === void 0 ? void 0 : _rigidBodyContext$api.raw(),
1184
- scale,
1185
- hasCollisionEvents: rigidBodyContext === null || rigidBodyContext === void 0 ? void 0 : rigidBodyContext.hasCollisionEvents
1186
- });
1227
+ const colliders = []; // If this is an InstancedRigidBody api
1228
+
1229
+ if (rigidBodyContext && "at" in rigidBodyContext.api) {
1230
+ rigidBodyContext.api.forEach((body, index) => {
1231
+ var _rigidBodyContext$opt, _rigidBodyContext$opt2;
1232
+
1233
+ let instanceScale = scale.clone();
1234
+
1235
+ if ("scales" in rigidBodyContext.options && rigidBodyContext !== null && rigidBodyContext !== void 0 && (_rigidBodyContext$opt = rigidBodyContext.options) !== null && _rigidBodyContext$opt !== void 0 && (_rigidBodyContext$opt2 = _rigidBodyContext$opt.scales) !== null && _rigidBodyContext$opt2 !== void 0 && _rigidBodyContext$opt2[index]) {
1236
+ instanceScale.multiply(vectorArrayToVector3(rigidBodyContext.options.scales[index]));
1237
+ }
1238
+
1239
+ colliders.push(createColliderFromOptions({
1240
+ options: props,
1241
+ world,
1242
+ rigidBody: body.raw(),
1243
+ scale: instanceScale,
1244
+ hasCollisionEvents: rigidBodyContext === null || rigidBodyContext === void 0 ? void 0 : rigidBodyContext.hasCollisionEvents
1245
+ }));
1246
+ });
1247
+ } else {
1248
+ colliders.push(createColliderFromOptions({
1249
+ options: props,
1250
+ world,
1251
+ // Initiate with a rigidbody, or undefined, because colliders can exist without a rigid body
1252
+ rigidBody: rigidBodyContext && "raw" in rigidBodyContext.api ? rigidBodyContext.api.raw() : undefined,
1253
+ scale,
1254
+ hasCollisionEvents: rigidBodyContext === null || rigidBodyContext === void 0 ? void 0 : rigidBodyContext.hasCollisionEvents
1255
+ }));
1256
+ }
1257
+
1187
1258
  return () => {
1188
- world.removeCollider(collider);
1259
+ colliders.forEach(collider => world.removeCollider(collider));
1189
1260
  };
1190
1261
  }, []);
1191
1262
  return /*#__PURE__*/React__default["default"].createElement("object3D", {
@@ -1,6 +1,6 @@
1
1
  import { ColliderDesc, ActiveEvents, RigidBodyDesc, CoefficientCombineRule, EventQueue, ShapeType } from '@dimforge/rapier3d-compat';
2
2
  export { CoefficientCombineRule, Collider as RapierCollider, RigidBody as RapierRigidBody } from '@dimforge/rapier3d-compat';
3
- import React, { useRef, useState, useEffect, useMemo, createContext, useContext, forwardRef, useImperativeHandle, memo } from 'react';
3
+ import React, { useRef, useState, useEffect, useMemo, createContext, useContext, forwardRef, useImperativeHandle, memo, useLayoutEffect } from 'react';
4
4
  import { useAsset } from 'use-asset';
5
5
  import { useFrame } from '@react-three/fiber';
6
6
  import { Quaternion, Euler, Vector3, Object3D, Matrix4, InstancedMesh, CylinderBufferGeometry, BufferGeometry, BufferAttribute, SphereBufferGeometry, BoxBufferGeometry, DynamicDrawUsage } from 'three';
@@ -129,13 +129,19 @@ const isChildOfMeshCollider = child => {
129
129
  return flag;
130
130
  };
131
131
 
132
- const createCollidersFromChildren = (object, rigidBody, options, world, ignoreMeshColliders = true) => {
132
+ const createCollidersFromChildren = ({
133
+ object,
134
+ rigidBody,
135
+ options,
136
+ world,
137
+ ignoreMeshColliders: _ignoreMeshColliders = true
138
+ }) => {
133
139
  const hasCollisionEvents = !!(options.onCollisionEnter || options.onCollisionExit);
134
140
  const colliders = [];
135
141
  new Vector3();
136
- object.traverse(child => {
142
+ object.traverseVisible(child => {
137
143
  if ("isMesh" in child) {
138
- if (ignoreMeshColliders && isChildOfMeshCollider(child)) return;
144
+ if (_ignoreMeshColliders && isChildOfMeshCollider(child)) return;
139
145
  const {
140
146
  geometry
141
147
  } = child;
@@ -176,7 +182,7 @@ const createCollidersFromChildren = (object, rigidBody, options, world, ignoreMe
176
182
  z: rz,
177
183
  w: rw
178
184
  });
179
- const actualRigidBody = world.getRigidBody(rigidBody === null || rigidBody === void 0 ? void 0 : rigidBody.handle);
185
+ const actualRigidBody = rigidBody ? world.getRigidBody(rigidBody.handle) : undefined;
180
186
  const collider = world.createCollider(desc, actualRigidBody);
181
187
  colliders.push(collider);
182
188
  }
@@ -242,11 +248,13 @@ const scaleVertices = (vertices, scale) => {
242
248
  return scaledVerts;
243
249
  };
244
250
  const rigidBodyDescFromOptions = options => {
245
- var _options$linearVeloci, _options$angularVeloc, _options$gravityScale, _options$canSleep, _options$ccd, _options$enabledRotat, _options$enabledTrans;
251
+ var _options$linearVeloci, _options$angularVeloc, _options$angularDampi, _options$linearDampin, _options$gravityScale, _options$canSleep, _options$ccd, _options$enabledRotat, _options$enabledTrans;
246
252
 
247
253
  const type = rigidBodyTypeFromString((options === null || options === void 0 ? void 0 : options.type) || "dynamic");
248
254
  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];
249
255
  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];
256
+ const angularDamping = (_options$angularDampi = options === null || options === void 0 ? void 0 : options.angularDamping) !== null && _options$angularDampi !== void 0 ? _options$angularDampi : 0;
257
+ const linearDamping = (_options$linearDampin = options === null || options === void 0 ? void 0 : options.linearDamping) !== null && _options$linearDampin !== void 0 ? _options$linearDampin : 0;
250
258
  const gravityScale = (_options$gravityScale = options === null || options === void 0 ? void 0 : options.gravityScale) !== null && _options$gravityScale !== void 0 ? _options$gravityScale : 1;
251
259
  const canSleep = (_options$canSleep = options === null || options === void 0 ? void 0 : options.canSleep) !== null && _options$canSleep !== void 0 ? _options$canSleep : true;
252
260
  const ccdEnabled = (_options$ccd = options === null || options === void 0 ? void 0 : options.ccd) !== null && _options$ccd !== void 0 ? _options$ccd : false;
@@ -256,7 +264,7 @@ const rigidBodyDescFromOptions = options => {
256
264
  x: avx,
257
265
  y: avy,
258
266
  z: avz
259
- }).setGravityScale(gravityScale).setCanSleep(canSleep).setCcdEnabled(ccdEnabled).enabledRotations(erx, ery, erz).enabledTranslations(etx, ety, etz);
267
+ }).setLinearDamping(linearDamping).setAngularDamping(angularDamping).setGravityScale(gravityScale).setCanSleep(canSleep).setCcdEnabled(ccdEnabled).enabledRotations(erx, ery, erz).enabledTranslations(etx, ety, etz);
260
268
  if (options.lockRotations) desc.lockRotations();
261
269
  if (options.lockTranslations) desc.lockTranslations();
262
270
  return desc;
@@ -341,6 +349,18 @@ const createRigidBodyApi = ref => {
341
349
  },
342
350
 
343
351
  setAngvel: velocity => ref.current().setAngvel(velocity, true),
352
+
353
+ linearDamping() {
354
+ return ref.current().linearDamping();
355
+ },
356
+
357
+ setLinearDamping: factor => ref.current().setLinearDamping(factor),
358
+
359
+ angularDamping() {
360
+ return ref.current().angularDamping();
361
+ },
362
+
363
+ setAngularDamping: factor => ref.current().setAngularDamping(factor),
344
364
  setNextKinematicRotation: ({
345
365
  x,
346
366
  y,
@@ -424,7 +444,8 @@ const Physics = ({
424
444
  colliders: _colliders = "cuboid",
425
445
  gravity: _gravity = [0, -9.81, 0],
426
446
  children,
427
- timeStep: _timeStep = "vary"
447
+ timeStep: _timeStep = "vary",
448
+ paused: _paused = false
428
449
  }) => {
429
450
  const rapier = useAsset(importRapier);
430
451
  const worldRef = useRef();
@@ -473,7 +494,7 @@ const Physics = ({
473
494
  world.timestep = _timeStep;
474
495
  }
475
496
 
476
- world.step(eventQueue); // Update meshes
497
+ if (!_paused) world.step(eventQueue); // Update meshes
477
498
 
478
499
  rigidBodyStates.forEach((state, handle) => {
479
500
  const rigidBody = world.getRigidBody(handle);
@@ -705,9 +726,15 @@ const useRigidBody = (options = {}) => {
705
726
  rigidBody.resetForces(false);
706
727
  rigidBody.resetTorques(false);
707
728
  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;
708
- const autoColliders = colliderSetting !== false ? createCollidersFromChildren(ref.current, rigidBody, _objectSpread2(_objectSpread2({}, options), {}, {
709
- colliders: colliderSetting
710
- }), world) : [];
729
+ const autoColliders = colliderSetting !== false ? createCollidersFromChildren({
730
+ object: ref.current,
731
+ rigidBody,
732
+ options: _objectSpread2(_objectSpread2({}, options), {}, {
733
+ colliders: colliderSetting
734
+ }),
735
+ world,
736
+ ignoreMeshColliders: true
737
+ }) : [];
711
738
  rigidBodyStates.set(rigidBody.handle, {
712
739
  mesh: ref.current,
713
740
  invertedMatrixWorld: ref.current.parent.matrixWorld.clone().invert(),
@@ -872,9 +899,18 @@ const MeshCollider = ({
872
899
  var _ref;
873
900
 
874
901
  const colliderSetting = (_ref = type !== null && type !== void 0 ? type : physicsOptions.colliders) !== null && _ref !== void 0 ? _ref : false;
875
- autoColliders = colliderSetting !== false ? createCollidersFromChildren(object.current, api, _objectSpread2(_objectSpread2({}, options), {}, {
876
- colliders: colliderSetting
877
- }), world, false) : [];
902
+
903
+ if ("raw" in api) {
904
+ autoColliders = createCollidersFromChildren({
905
+ object: object.current,
906
+ rigidBody: api,
907
+ options: _objectSpread2(_objectSpread2({}, options), {}, {
908
+ colliders: colliderSetting
909
+ }),
910
+ world,
911
+ ignoreMeshColliders: false
912
+ });
913
+ }
878
914
  }
879
915
 
880
916
  return () => {
@@ -1021,7 +1057,7 @@ const InstancedRigidBodies = /*#__PURE__*/forwardRef((props, ref) => {
1021
1057
 
1022
1058
  return instancesRef.current;
1023
1059
  });
1024
- useEffect(() => {
1060
+ useLayoutEffect(() => {
1025
1061
  const colliders = [];
1026
1062
  const rigidBodies = instancesRefGetter.current();
1027
1063
 
@@ -1047,15 +1083,21 @@ const InstancedRigidBodies = /*#__PURE__*/forwardRef((props, ref) => {
1047
1083
  scale.multiply(s);
1048
1084
  }
1049
1085
 
1050
- const colliderDesc = colliderDescFromGeometry(mesh.geometry, props.colliders || physicsOptions.colliders, scale, false // Collisions currently not enabled for instances
1051
- );
1052
1086
  const rigidBody = world.createRigidBody(rigidBodyDesc);
1053
1087
  const matrix = new Matrix4();
1054
1088
  mesh.getMatrixAt(index, matrix);
1055
1089
  const {
1056
1090
  position,
1057
1091
  rotation
1058
- } = decomposeMatrix4(matrix); // Set positions
1092
+ } = decomposeMatrix4(matrix);
1093
+
1094
+ if (props.colliders !== false) {
1095
+ const colliderDesc = colliderDescFromGeometry(mesh.geometry, props.colliders !== undefined ? props.colliders : physicsOptions.colliders, scale, false // Collisions currently not enabled for instances
1096
+ );
1097
+ const collider = world.createCollider(colliderDesc, rigidBody);
1098
+ colliders.push(collider);
1099
+ } // Set positions
1100
+
1059
1101
 
1060
1102
  if (props.positions && props.positions[index]) {
1061
1103
  rigidBody.setTranslation(vectorArrayToVector3(props.positions[index]), true);
@@ -1071,7 +1113,6 @@ const InstancedRigidBodies = /*#__PURE__*/forwardRef((props, ref) => {
1071
1113
  rigidBody.setRotation(rotation, true);
1072
1114
  }
1073
1115
 
1074
- const collider = world.createCollider(colliderDesc, rigidBody);
1075
1116
  rigidBodyStates.set(rigidBody.handle, {
1076
1117
  mesh: mesh,
1077
1118
  isSleeping: false,
@@ -1092,7 +1133,6 @@ const InstancedRigidBodies = /*#__PURE__*/forwardRef((props, ref) => {
1092
1133
  }
1093
1134
 
1094
1135
  });
1095
- colliders.push(collider);
1096
1136
  rigidBodies.push({
1097
1137
  rigidBody,
1098
1138
  api
@@ -1111,10 +1151,19 @@ const InstancedRigidBodies = /*#__PURE__*/forwardRef((props, ref) => {
1111
1151
  };
1112
1152
  }
1113
1153
  }, []);
1114
- useImperativeHandle(ref, () => createInstancedRigidBodiesApi(instancesRefGetter));
1115
- return /*#__PURE__*/React.createElement("object3D", {
1154
+ const api = useMemo(() => createInstancedRigidBodiesApi(instancesRefGetter), []);
1155
+ useImperativeHandle(ref, () => api); // console.log(api);
1156
+
1157
+ return /*#__PURE__*/React.createElement(RigidBodyContext.Provider, {
1158
+ value: {
1159
+ ref: object,
1160
+ api,
1161
+ hasCollisionEvents: false,
1162
+ options: props
1163
+ }
1164
+ }, /*#__PURE__*/React.createElement("object3D", {
1116
1165
  ref: object
1117
- }, props.children);
1166
+ }, props.children));
1118
1167
  });
1119
1168
 
1120
1169
  function _extends() {
@@ -1149,18 +1198,40 @@ const AnyCollider = _ref => {
1149
1198
  const rigidBodyContext = useRigidBodyContext();
1150
1199
  const ref = useRef(null);
1151
1200
  useEffect(() => {
1152
- var _rigidBodyContext$api;
1153
-
1154
1201
  const scale = ref.current.getWorldScale(new Vector3());
1155
- const collider = createColliderFromOptions({
1156
- options: props,
1157
- world,
1158
- rigidBody: rigidBodyContext === null || rigidBodyContext === void 0 ? void 0 : (_rigidBodyContext$api = rigidBodyContext.api) === null || _rigidBodyContext$api === void 0 ? void 0 : _rigidBodyContext$api.raw(),
1159
- scale,
1160
- hasCollisionEvents: rigidBodyContext === null || rigidBodyContext === void 0 ? void 0 : rigidBodyContext.hasCollisionEvents
1161
- });
1202
+ const colliders = []; // If this is an InstancedRigidBody api
1203
+
1204
+ if (rigidBodyContext && "at" in rigidBodyContext.api) {
1205
+ rigidBodyContext.api.forEach((body, index) => {
1206
+ var _rigidBodyContext$opt, _rigidBodyContext$opt2;
1207
+
1208
+ let instanceScale = scale.clone();
1209
+
1210
+ if ("scales" in rigidBodyContext.options && rigidBodyContext !== null && rigidBodyContext !== void 0 && (_rigidBodyContext$opt = rigidBodyContext.options) !== null && _rigidBodyContext$opt !== void 0 && (_rigidBodyContext$opt2 = _rigidBodyContext$opt.scales) !== null && _rigidBodyContext$opt2 !== void 0 && _rigidBodyContext$opt2[index]) {
1211
+ instanceScale.multiply(vectorArrayToVector3(rigidBodyContext.options.scales[index]));
1212
+ }
1213
+
1214
+ colliders.push(createColliderFromOptions({
1215
+ options: props,
1216
+ world,
1217
+ rigidBody: body.raw(),
1218
+ scale: instanceScale,
1219
+ hasCollisionEvents: rigidBodyContext === null || rigidBodyContext === void 0 ? void 0 : rigidBodyContext.hasCollisionEvents
1220
+ }));
1221
+ });
1222
+ } else {
1223
+ colliders.push(createColliderFromOptions({
1224
+ options: props,
1225
+ world,
1226
+ // Initiate with a rigidbody, or undefined, because colliders can exist without a rigid body
1227
+ rigidBody: rigidBodyContext && "raw" in rigidBodyContext.api ? rigidBodyContext.api.raw() : undefined,
1228
+ scale,
1229
+ hasCollisionEvents: rigidBodyContext === null || rigidBodyContext === void 0 ? void 0 : rigidBodyContext.hasCollisionEvents
1230
+ }));
1231
+ }
1232
+
1162
1233
  return () => {
1163
- world.removeCollider(collider);
1234
+ colliders.forEach(collider => world.removeCollider(collider));
1164
1235
  };
1165
1236
  }, []);
1166
1237
  return /*#__PURE__*/React.createElement("object3D", {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@react-three/rapier",
3
- "version": "0.6.2",
3
+ "version": "0.6.3",
4
4
  "source": "src/index.ts",
5
5
  "main": "dist/react-three-rapier.cjs.js",
6
6
  "module": "dist/react-three-rapier.esm.js",
package/readme.md CHANGED
@@ -2,6 +2,11 @@
2
2
  <img src="https://raw.githubusercontent.com/pmndrs/react-three-rapier/HEAD/packages/react-three-rapier/misc/hero.svg" alt="@react-three/rapier" />
3
3
  </p>
4
4
 
5
+ <p align="center">
6
+ <img src="https://img.shields.io/npm/v/@react-three/rapier?style=for-the-badge&colorA=0099DA&colorB=ffffff" />
7
+ <img src="https://img.shields.io/discord/740090768164651008?style=for-the-badge&colorA=0099DA&colorB=ffffff&label=discord&logo=discord&logoColor=ffffff)](https://discord.gg/ZZjjNvJ" />
8
+ </p>
9
+
5
10
  <p align="center">⚠️ Under heavy development. All APIs are subject to change. ⚠️</p>
6
11
 
7
12
  ## Usage
@@ -121,6 +126,8 @@ Instanced meshes can also be used and have automatic colliders generated from th
121
126
 
122
127
  By wrapping the `InstancedMesh` in `<InstancedRigidBodies />`, each instance will be attached to an individual `RigidBody`.
123
128
 
129
+ > Note: Custom colliders (compound shapes) for InstancedMesh is currently not supported
130
+
124
131
  ```tsx
125
132
  import { InstancedRigidBodies } from "@react-three/rapier";
126
133
 
@@ -166,6 +173,8 @@ const Scene = () => {
166
173
  <instancedMesh args={[undefined, undefined, COUNT]}>
167
174
  <sphereBufferGeometry args={[0.2]} />
168
175
  <meshPhysicalGeometry color="blue" />
176
+
177
+ <CuboidCollider args={[0.1, 0.2, 0.1]} />
169
178
  </instancedMesh>
170
179
  </InstancedRigidBodies>
171
180
  );