@react-three/rapier 0.5.0 → 0.6.0

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,10 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- var React = require('react');
5
+ var rapier3dCompat = require('@dimforge/rapier3d-compat');
6
+ var React$1 = require('react');
6
7
  var useAsset = require('use-asset');
7
8
  var fiber = require('@react-three/fiber');
8
- var rapier3dCompat = require('@dimforge/rapier3d-compat');
9
9
  var three = require('three');
10
10
 
11
11
  function _interopDefault (e) { return e && e.__esModule ? e : { 'default': e }; }
@@ -28,7 +28,13 @@ function _interopNamespace(e) {
28
28
  return Object.freeze(n);
29
29
  }
30
30
 
31
- var React__default = /*#__PURE__*/_interopDefault(React);
31
+ var React__default = /*#__PURE__*/_interopDefault(React$1);
32
+
33
+ const _quaternion = new three.Quaternion();
34
+ const _euler = new three.Euler();
35
+ const _vector3 = new three.Vector3();
36
+ new three.Object3D();
37
+ const _matrix4 = new three.Matrix4();
32
38
 
33
39
  const vectorArrayToObject = arr => {
34
40
  const [x, y, z] = arr;
@@ -38,6 +44,20 @@ const vectorArrayToObject = arr => {
38
44
  z
39
45
  };
40
46
  };
47
+ const vector3ToQuaternion = v => {
48
+ return _quaternion.setFromEuler(_euler.setFromVector3(v));
49
+ };
50
+ const rapierVector3ToVector3 = ({
51
+ x,
52
+ y,
53
+ z
54
+ }) => _vector3.set(x, y, z).clone();
55
+ const rapierQuaternionToQuaternion = ({
56
+ x,
57
+ y,
58
+ z,
59
+ w
60
+ }) => _quaternion.set(x, y, z, w);
41
61
  const rigidBodyTypeMap = {
42
62
  fixed: 1,
43
63
  dynamic: 0,
@@ -45,6 +65,17 @@ const rigidBodyTypeMap = {
45
65
  kinematicVelocity: 3
46
66
  };
47
67
  const rigidBodyTypeFromString = type => rigidBodyTypeMap[type];
68
+ const decomposeMatrix4 = m => {
69
+ const position = new three.Vector3();
70
+ const rotation = new three.Quaternion();
71
+ const scale = new three.Vector3();
72
+ m.decompose(position, rotation, scale);
73
+ return {
74
+ position,
75
+ rotation,
76
+ scale
77
+ };
78
+ };
48
79
  const scaleColliderArgs = (shape, args, scale) => {
49
80
  // Heightfield only scales the last arg
50
81
  const newArgs = args.slice();
@@ -63,11 +94,13 @@ const scaleColliderArgs = (shape, args, scale) => {
63
94
  const scaleArray = [scale.x, scale.y, scale.z];
64
95
  return newArgs.map((arg, index) => scaleArray[index] * arg);
65
96
  };
66
- const createColliderFromOptions = (options, world, rigidBody, scale = {
67
- x: 1,
68
- y: 1,
69
- z: 1
70
- }, hasCollisionEvents = false) => {
97
+ const createColliderFromOptions = ({
98
+ options,
99
+ world,
100
+ rigidBody,
101
+ scale,
102
+ hasCollisionEvents
103
+ }) => {
71
104
  var _options$shape, _options$args, _options$restitution, _options$restitutionC, _options$friction, _options$frictionComb;
72
105
 
73
106
  const mass = (options === null || options === void 0 ? void 0 : options.mass) || 1;
@@ -76,15 +109,16 @@ const createColliderFromOptions = (options, world, rigidBody, scale = {
76
109
  const [cmx, cmy, cmz] = (options === null || options === void 0 ? void 0 : options.centerOfMass) || [0, 0, 0];
77
110
  const [pix, piy, piz] = (options === null || options === void 0 ? void 0 : options.principalAngularInertia) || [mass * 0.2, mass * 0.2, mass * 0.2];
78
111
  const [x, y, z] = (options === null || options === void 0 ? void 0 : options.position) || [0, 0, 0];
79
- const [rx, ry, rz] = (options === null || options === void 0 ? void 0 : options.rotation) || [0, 0, 0]; // @ts-ignore
112
+ const [rx, ry, rz] = (options === null || options === void 0 ? void 0 : options.rotation) || [0, 0, 0];
113
+ const qRotation = vector3ToQuaternion(new three.Vector3(rx, ry, rz)); // @ts-ignore
80
114
 
81
115
  const scaledArgs = scaleColliderArgs(options.shape, colliderArgs, scale);
82
116
  let colliderDesc = rapier3dCompat.ColliderDesc[colliderShape]( // @ts-ignore
83
117
  ...scaledArgs).setTranslation(x * scale.x, y * scale.y, z * scale.z).setRotation({
84
- x: rx,
85
- y: ry,
86
- z: rz,
87
- w: 1
118
+ x: qRotation.x,
119
+ y: qRotation.y,
120
+ z: qRotation.z,
121
+ w: qRotation.w
88
122
  }).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 : rapier3dCompat.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 : rapier3dCompat.CoefficientCombineRule.Average);
89
123
 
90
124
  if (hasCollisionEvents) {
@@ -92,6 +126,8 @@ const createColliderFromOptions = (options, world, rigidBody, scale = {
92
126
  } // If any of the mass properties are specified, add mass properties
93
127
 
94
128
 
129
+ const qMassRot = vector3ToQuaternion(new three.Vector3(0, 0, 0));
130
+
95
131
  if (options !== null && options !== void 0 && options.mass || options !== null && options !== void 0 && options.centerOfMass || options !== null && options !== void 0 && options.principalAngularInertia) {
96
132
  colliderDesc.setDensity(0);
97
133
  colliderDesc.setMassProperties(mass, {
@@ -103,23 +139,32 @@ const createColliderFromOptions = (options, world, rigidBody, scale = {
103
139
  y: piy,
104
140
  z: piz
105
141
  }, {
106
- x: 0,
107
- y: 0,
108
- z: 0,
109
- w: 1
142
+ x: qMassRot.x,
143
+ y: qMassRot.y,
144
+ z: qMassRot.z,
145
+ w: qMassRot.w
110
146
  });
111
147
  }
112
148
 
113
149
  const collider = world.createCollider(colliderDesc, rigidBody);
114
150
  return collider;
115
151
  };
116
- const createCollidersFromChildren = (object, rigidBody, options, world) => {
152
+
153
+ const isChildOfMeshCollider = child => {
154
+ let flag = false;
155
+ child.traverseAncestors(a => {
156
+ if (a.userData.r3RapierType === "MeshCollider") flag = true;
157
+ });
158
+ return flag;
159
+ };
160
+
161
+ const createCollidersFromChildren = (object, rigidBody, options, world, ignoreMeshColliders = true) => {
117
162
  const hasCollisionEvents = !!(options.onCollisionEnter || options.onCollisionExit);
118
163
  const colliders = [];
119
- let desc;
120
- let offset = new three.Vector3();
164
+ new three.Vector3();
121
165
  object.traverse(child => {
122
166
  if ("isMesh" in child) {
167
+ if (ignoreMeshColliders && isChildOfMeshCollider(child)) return;
123
168
  const {
124
169
  geometry
125
170
  } = child;
@@ -134,68 +179,86 @@ const createCollidersFromChildren = (object, rigidBody, options, world) => {
134
179
  z: rz,
135
180
  w: rw
136
181
  } = new three.Quaternion().setFromEuler(child.rotation);
137
- const scale = child.getWorldScale(new three.Vector3());
182
+ const scale = child.getWorldScale(new three.Vector3()); // We translate the colliders based on the parent's world scale
138
183
 
139
- switch (options.colliders) {
140
- case "cuboid":
141
- {
142
- geometry.computeBoundingBox();
143
- const {
144
- boundingBox
145
- } = geometry;
146
- const size = boundingBox.getSize(new three.Vector3());
147
- boundingBox.getCenter(offset);
148
- desc = rapier3dCompat.ColliderDesc.cuboid(size.x / 2 * scale.x, size.y / 2 * scale.y, size.z / 2 * scale.z);
149
- }
150
- break;
151
-
152
- case "ball":
153
- {
154
- geometry.computeBoundingSphere();
155
- const {
156
- boundingSphere
157
- } = geometry;
158
- const radius = boundingSphere.radius * scale.x;
159
- offset.copy(boundingSphere.center);
160
- desc = rapier3dCompat.ColliderDesc.ball(radius);
161
- }
162
- break;
163
-
164
- case "trimesh":
165
- {
166
- var _g$index;
167
-
168
- const _g = geometry.clone().scale(scale.x, scale.y, scale.z);
184
+ const parentWorldScale = child.parent.getWorldScale(new three.Vector3());
185
+ const desc = colliderDescFromGeometry(geometry, options.colliders, scale, hasCollisionEvents);
186
+ const offset = new three.Vector3(0, 0, 0);
169
187
 
170
- desc = rapier3dCompat.ColliderDesc.trimesh(_g.attributes.position.array, (_g$index = _g.index) === null || _g$index === void 0 ? void 0 : _g$index.array);
171
- }
172
- break;
188
+ if (options.colliders === "cuboid") {
189
+ var _geometry$boundingBox;
173
190
 
174
- case "hull":
175
- const g = geometry.clone().scale(scale.x, scale.y, scale.z);
176
- {
177
- desc = rapier3dCompat.ColliderDesc.convexHull(g.attributes.position.array);
178
- }
179
- break;
180
- } // We translate the colliders based on the parent's world scale
191
+ geometry.computeBoundingBox();
192
+ (_geometry$boundingBox = geometry.boundingBox) === null || _geometry$boundingBox === void 0 ? void 0 : _geometry$boundingBox.getCenter(offset);
193
+ }
181
194
 
195
+ if (options.colliders === "ball") {
196
+ geometry.computeBoundingSphere();
197
+ offset.copy(geometry.boundingSphere.center);
198
+ }
182
199
 
183
- const parentWorldScale = child.parent.getWorldScale(new three.Vector3());
200
+ if (Number.isFinite(options.friction)) desc.setFriction(options.friction);
201
+ if (Number.isFinite(options.restitution)) desc.setRestitution(options.restitution);
184
202
  desc.setTranslation((x + offset.x) * parentWorldScale.x, (y + offset.y) * parentWorldScale.y, (z + offset.z) * parentWorldScale.z).setRotation({
185
203
  x: rx,
186
204
  y: ry,
187
205
  z: rz,
188
206
  w: rw
189
207
  });
190
- if (hasCollisionEvents) desc.setActiveEvents(rapier3dCompat.ActiveEvents.COLLISION_EVENTS);
191
- if (Number.isFinite(options.friction)) desc.setFriction(options.friction);
192
- if (Number.isFinite(options.restitution)) desc.setRestitution(options.restitution);
193
- const collider = world.createCollider(desc, rigidBody);
208
+ const actualRigidBody = world.getRigidBody(rigidBody === null || rigidBody === void 0 ? void 0 : rigidBody.handle);
209
+ const collider = world.createCollider(desc, actualRigidBody);
194
210
  colliders.push(collider);
195
211
  }
196
212
  });
197
213
  return colliders;
198
214
  };
215
+ const colliderDescFromGeometry = (geometry, colliders, scale, hasCollisionEvents) => {
216
+ let desc;
217
+
218
+ switch (colliders) {
219
+ case "cuboid":
220
+ {
221
+ geometry.computeBoundingBox();
222
+ const {
223
+ boundingBox
224
+ } = geometry;
225
+ const size = boundingBox.getSize(new three.Vector3());
226
+ desc = rapier3dCompat.ColliderDesc.cuboid(size.x / 2 * scale.x, size.y / 2 * scale.y, size.z / 2 * scale.z);
227
+ }
228
+ break;
229
+
230
+ case "ball":
231
+ {
232
+ geometry.computeBoundingSphere();
233
+ const {
234
+ boundingSphere
235
+ } = geometry;
236
+ const radius = boundingSphere.radius * scale.x;
237
+ desc = rapier3dCompat.ColliderDesc.ball(radius);
238
+ }
239
+ break;
240
+
241
+ case "trimesh":
242
+ {
243
+ var _g$index;
244
+
245
+ const _g = geometry.clone().scale(scale.x, scale.y, scale.z);
246
+
247
+ desc = rapier3dCompat.ColliderDesc.trimesh(_g.attributes.position.array, (_g$index = _g.index) === null || _g$index === void 0 ? void 0 : _g$index.array);
248
+ }
249
+ break;
250
+
251
+ case "hull":
252
+ const g = geometry.clone().scale(scale.x, scale.y, scale.z);
253
+ {
254
+ desc = rapier3dCompat.ColliderDesc.convexHull(g.attributes.position.array);
255
+ }
256
+ break;
257
+ }
258
+
259
+ if (hasCollisionEvents) desc.setActiveEvents(rapier3dCompat.ActiveEvents.COLLISION_EVENTS);
260
+ return desc;
261
+ };
199
262
  const scaleVertices = (vertices, scale) => {
200
263
  const scaledVerts = Array.from(vertices);
201
264
 
@@ -207,53 +270,27 @@ const scaleVertices = (vertices, scale) => {
207
270
 
208
271
  return scaledVerts;
209
272
  };
210
- const quaternion = new three.Quaternion();
211
- const euler = new three.Euler();
212
- const vector3ToQuaternion = v => {
213
- return quaternion.setFromEuler(euler.setFromVector3(v));
273
+ const rigidBodyDescFromOptions = options => {
274
+ var _options$linearVeloci, _options$angularVeloc, _options$gravityScale, _options$canSleep, _options$ccd, _options$enabledRotat, _options$enabledTrans;
275
+
276
+ const type = rigidBodyTypeFromString((options === null || options === void 0 ? void 0 : options.type) || "dynamic");
277
+ 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];
278
+ 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];
279
+ const gravityScale = (_options$gravityScale = options === null || options === void 0 ? void 0 : options.gravityScale) !== null && _options$gravityScale !== void 0 ? _options$gravityScale : 1;
280
+ const canSleep = (_options$canSleep = options === null || options === void 0 ? void 0 : options.canSleep) !== null && _options$canSleep !== void 0 ? _options$canSleep : true;
281
+ const ccdEnabled = (_options$ccd = options === null || options === void 0 ? void 0 : options.ccd) !== null && _options$ccd !== void 0 ? _options$ccd : false;
282
+ 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];
283
+ 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];
284
+ const desc = new rapier3dCompat.RigidBodyDesc(type).setLinvel(lvx, lvy, lvz).setAngvel({
285
+ x: avx,
286
+ y: avy,
287
+ z: avz
288
+ }).setGravityScale(gravityScale).setCanSleep(canSleep).setCcdEnabled(ccdEnabled).enabledRotations(erx, ery, erz).enabledTranslations(etx, ety, etz);
289
+ if (options.lockRotations) desc.lockRotations();
290
+ if (options.lockTranslations) desc.lockTranslations();
291
+ return desc;
214
292
  };
215
293
 
216
- function _defineProperty(obj, key, value) {
217
- if (key in obj) {
218
- Object.defineProperty(obj, key, {
219
- value: value,
220
- enumerable: true,
221
- configurable: true,
222
- writable: true
223
- });
224
- } else {
225
- obj[key] = value;
226
- }
227
-
228
- return obj;
229
- }
230
-
231
- function ownKeys(object, enumerableOnly) {
232
- var keys = Object.keys(object);
233
-
234
- if (Object.getOwnPropertySymbols) {
235
- var symbols = Object.getOwnPropertySymbols(object);
236
- enumerableOnly && (symbols = symbols.filter(function (sym) {
237
- return Object.getOwnPropertyDescriptor(object, sym).enumerable;
238
- })), keys.push.apply(keys, symbols);
239
- }
240
-
241
- return keys;
242
- }
243
-
244
- function _objectSpread2(target) {
245
- for (var i = 1; i < arguments.length; i++) {
246
- var source = null != arguments[i] ? arguments[i] : {};
247
- i % 2 ? ownKeys(Object(source), !0).forEach(function (key) {
248
- _defineProperty(target, key, source[key]);
249
- }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) {
250
- Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
251
- });
252
- }
253
-
254
- return target;
255
- }
256
-
257
294
  const createRigidBodyApi = ref => {
258
295
  return {
259
296
  raw: () => ref.current(),
@@ -298,9 +335,19 @@ const createRigidBodyApi = ref => {
298
335
  return new three.Quaternion(x, y, z, w);
299
336
  },
300
337
 
301
- setRotation: rotation => ref.current().setRotation(_objectSpread2(_objectSpread2({}, rotation), {}, {
302
- w: 1
303
- }), true),
338
+ setRotation: ({
339
+ x,
340
+ y,
341
+ z
342
+ }) => {
343
+ const q = vector3ToQuaternion(new three.Vector3(x, y, z));
344
+ ref.current().setRotation({
345
+ x: q.x,
346
+ y: q.y,
347
+ z: q.z,
348
+ w: q.w
349
+ }, true);
350
+ },
304
351
 
305
352
  linvel() {
306
353
  const {
@@ -344,18 +391,19 @@ const createRigidBodyApi = ref => {
344
391
  setEnabledRotations: (x, y, z) => ref.current().setEnabledRotations(x, y, z, true),
345
392
  setEnabledTranslations: (x, y, z) => ref.current().setEnabledTranslations(x, y, z, true)
346
393
  };
347
- }; // TODO: Flesh this out
394
+ };
395
+ const createInstancedRigidBodiesApi = bodiesGetter => ({
396
+ at: index => bodiesGetter.current()[index].api,
348
397
 
349
- const createColliderApi = ref => {
350
- return {
351
- raw: () => ref.current(),
398
+ forEach(callback) {
399
+ return bodiesGetter.current().map(b => b.api).forEach(callback);
400
+ },
352
401
 
353
- get handle() {
354
- return ref.current().handle;
355
- }
402
+ get count() {
403
+ return bodiesGetter.current().length;
404
+ }
356
405
 
357
- };
358
- };
406
+ }); // TODO: Flesh this out
359
407
  const createWorldApi = ref => {
360
408
  return {
361
409
  raw: () => ref.current(),
@@ -393,7 +441,7 @@ const createJointApi = ref => {
393
441
  };
394
442
  };
395
443
 
396
- const RapierContext = /*#__PURE__*/React.createContext(undefined);
444
+ const RapierContext = /*#__PURE__*/React$1.createContext(undefined);
397
445
 
398
446
  const importRapier = async () => {
399
447
  let r = await Promise.resolve().then(function () { return /*#__PURE__*/_interopNamespace(require('@dimforge/rapier3d-compat')); });
@@ -402,14 +450,14 @@ const importRapier = async () => {
402
450
  };
403
451
 
404
452
  const Physics = ({
405
- colliders: _colliders = 'cuboid',
453
+ colliders: _colliders = "cuboid",
406
454
  gravity: _gravity = [0, -9.81, 0],
407
455
  children,
408
- timeStep: _timeStep = 'vary'
456
+ timeStep: _timeStep = "vary"
409
457
  }) => {
410
458
  const rapier = useAsset.useAsset(importRapier);
411
- const worldRef = React.useRef();
412
- const getWorldRef = React.useRef(() => {
459
+ const worldRef = React$1.useRef();
460
+ const getWorldRef = React$1.useRef(() => {
413
461
  if (!worldRef.current) {
414
462
  const world = new rapier.World(vectorArrayToObject(_gravity));
415
463
  worldRef.current = world;
@@ -417,12 +465,12 @@ const Physics = ({
417
465
 
418
466
  return worldRef.current;
419
467
  });
420
- const [colliderMeshes] = React.useState(() => new Map());
421
- const [rigidBodyMeshes] = React.useState(() => new Map());
422
- const [rigidBodyEvents] = React.useState(() => new Map());
423
- const [eventQueue] = React.useState(() => new rapier3dCompat.EventQueue(false)); // Init world
468
+ const [colliderMeshes] = React$1.useState(() => new Map());
469
+ const [rigidBodyStates] = React$1.useState(() => new Map());
470
+ const [rigidBodyEvents] = React$1.useState(() => new Map());
471
+ const [eventQueue] = React$1.useState(() => new rapier3dCompat.EventQueue(false)); // Init world
424
472
 
425
- React.useEffect(() => {
473
+ React$1.useEffect(() => {
426
474
  const world = getWorldRef.current();
427
475
  return () => {
428
476
  if (world) {
@@ -432,14 +480,14 @@ const Physics = ({
432
480
  };
433
481
  }, []); // Update gravity
434
482
 
435
- React.useEffect(() => {
483
+ React$1.useEffect(() => {
436
484
  const world = worldRef.current;
437
485
 
438
486
  if (world) {
439
487
  world.gravity = vectorArrayToObject(_gravity);
440
488
  }
441
489
  }, [_gravity]);
442
- const time = React.useRef(performance.now());
490
+ const time = React$1.useRef(performance.now());
443
491
  fiber.useFrame(context => {
444
492
  const world = worldRef.current;
445
493
  if (!world) return; // Set timestep to current delta, to allow for variable frame rates
@@ -448,7 +496,7 @@ const Physics = ({
448
496
  const now = performance.now();
449
497
  const delta = Math.min(100, now - time.current);
450
498
 
451
- if (_timeStep === 'vary') {
499
+ if (_timeStep === "vary") {
452
500
  world.timestep = delta / 1000;
453
501
  } else {
454
502
  world.timestep = _timeStep;
@@ -456,52 +504,35 @@ const Physics = ({
456
504
 
457
505
  world.step(eventQueue); // Update meshes
458
506
 
459
- rigidBodyMeshes.forEach((mesh, handle) => {
507
+ rigidBodyStates.forEach((state, handle) => {
460
508
  const rigidBody = world.getRigidBody(handle);
461
509
  const events = rigidBodyEvents.get(handle);
462
510
 
463
511
  if (events !== null && events !== void 0 && events.onSleep || events !== null && events !== void 0 && events.onWake) {
464
- if (rigidBody.isSleeping() && !mesh.userData.isSleeping) {
512
+ if (rigidBody.isSleeping() && !state.isSleeping) {
465
513
  var _events$onSleep;
466
514
 
467
515
  events === null || events === void 0 ? void 0 : (_events$onSleep = events.onSleep) === null || _events$onSleep === void 0 ? void 0 : _events$onSleep.call(events);
468
516
  }
469
517
 
470
- if (!rigidBody.isSleeping() && mesh.userData.isSleeping) {
518
+ if (!rigidBody.isSleeping() && state.isSleeping) {
471
519
  var _events$onWake;
472
520
 
473
521
  events === null || events === void 0 ? void 0 : (_events$onWake = events.onWake) === null || _events$onWake === void 0 ? void 0 : _events$onWake.call(events);
474
522
  }
475
523
 
476
- mesh.userData.isSleeping = rigidBody.isSleeping();
524
+ state.isSleeping = rigidBody.isSleeping();
477
525
  }
478
526
 
479
- if (!rigidBody || rigidBody.isSleeping() || rigidBody.isFixed() || !mesh.parent) {
527
+ if (!rigidBody || rigidBody.isSleeping() || rigidBody.isFixed() || !state.setMatrix) {
480
528
  return;
481
529
  }
482
530
 
483
- const {
484
- x,
485
- y,
486
- z
487
- } = rigidBody.translation();
488
- const {
489
- x: rx,
490
- y: ry,
491
- z: rz,
492
- w: rw
493
- } = rigidBody.rotation();
494
- const scale = mesh.getWorldScale(new three.Vector3()); // haha matrixes I have no idea what I'm doing :)
495
-
496
- const o = new three.Object3D();
497
- o.position.set(x, y, z);
498
- o.rotation.setFromQuaternion(new three.Quaternion(rx, ry, rz, rw));
499
- o.scale.set(scale.x, scale.y, scale.z);
500
- o.updateMatrix();
501
- o.applyMatrix4(mesh.parent.matrixWorld.clone().invert());
502
- o.updateMatrix();
503
- mesh.position.setFromMatrixPosition(o.matrix);
504
- mesh.rotation.setFromRotationMatrix(o.matrix);
531
+ state.setMatrix(_matrix4.compose(rapierVector3ToVector3(rigidBody.translation()), rapierQuaternionToQuaternion(rigidBody.rotation()), state.worldScale).premultiply(state.invertedMatrixWorld));
532
+
533
+ if (state.mesh instanceof three.InstancedMesh) {
534
+ state.mesh.instanceMatrix.needsUpdate = true;
535
+ }
505
536
  }); // Collision events
506
537
 
507
538
  eventQueue.drainCollisionEvents((handle1, handle2, started) => {
@@ -549,8 +580,8 @@ const Physics = ({
549
580
  });
550
581
  time.current = now;
551
582
  });
552
- const api = React.useMemo(() => createWorldApi(getWorldRef), []);
553
- const context = React.useMemo(() => ({
583
+ const api = React$1.useMemo(() => createWorldApi(getWorldRef), []);
584
+ const context = React$1.useMemo(() => ({
554
585
  rapier,
555
586
  world: api,
556
587
  physicsOptions: {
@@ -558,7 +589,7 @@ const Physics = ({
558
589
  gravity: _gravity
559
590
  },
560
591
  colliderMeshes,
561
- rigidBodyMeshes,
592
+ rigidBodyStates,
562
593
  rigidBodyEvents
563
594
  }), []);
564
595
  return /*#__PURE__*/React__default["default"].createElement(RapierContext.Provider, {
@@ -566,39 +597,97 @@ const Physics = ({
566
597
  }, children);
567
598
  };
568
599
 
600
+ function _objectWithoutPropertiesLoose(source, excluded) {
601
+ if (source == null) return {};
602
+ var target = {};
603
+ var sourceKeys = Object.keys(source);
604
+ var key, i;
605
+
606
+ for (i = 0; i < sourceKeys.length; i++) {
607
+ key = sourceKeys[i];
608
+ if (excluded.indexOf(key) >= 0) continue;
609
+ target[key] = source[key];
610
+ }
611
+
612
+ return target;
613
+ }
614
+
615
+ function _objectWithoutProperties(source, excluded) {
616
+ if (source == null) return {};
617
+ var target = _objectWithoutPropertiesLoose(source, excluded);
618
+ var key, i;
619
+
620
+ if (Object.getOwnPropertySymbols) {
621
+ var sourceSymbolKeys = Object.getOwnPropertySymbols(source);
622
+
623
+ for (i = 0; i < sourceSymbolKeys.length; i++) {
624
+ key = sourceSymbolKeys[i];
625
+ if (excluded.indexOf(key) >= 0) continue;
626
+ if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
627
+ target[key] = source[key];
628
+ }
629
+ }
630
+
631
+ return target;
632
+ }
633
+
634
+ function _defineProperty(obj, key, value) {
635
+ if (key in obj) {
636
+ Object.defineProperty(obj, key, {
637
+ value: value,
638
+ enumerable: true,
639
+ configurable: true,
640
+ writable: true
641
+ });
642
+ } else {
643
+ obj[key] = value;
644
+ }
645
+
646
+ return obj;
647
+ }
648
+
649
+ function ownKeys(object, enumerableOnly) {
650
+ var keys = Object.keys(object);
651
+
652
+ if (Object.getOwnPropertySymbols) {
653
+ var symbols = Object.getOwnPropertySymbols(object);
654
+ enumerableOnly && (symbols = symbols.filter(function (sym) {
655
+ return Object.getOwnPropertyDescriptor(object, sym).enumerable;
656
+ })), keys.push.apply(keys, symbols);
657
+ }
658
+
659
+ return keys;
660
+ }
661
+
662
+ function _objectSpread2(target) {
663
+ for (var i = 1; i < arguments.length; i++) {
664
+ var source = null != arguments[i] ? arguments[i] : {};
665
+ i % 2 ? ownKeys(Object(source), !0).forEach(function (key) {
666
+ _defineProperty(target, key, source[key]);
667
+ }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) {
668
+ Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
669
+ });
670
+ }
671
+
672
+ return target;
673
+ }
674
+
569
675
  const useRapier = () => {
570
- return React.useContext(RapierContext);
676
+ return React$1.useContext(RapierContext);
571
677
  };
572
678
  const useRigidBody = (options = {}) => {
573
679
  const {
574
- rapier,
575
680
  world,
576
- rigidBodyMeshes,
681
+ rigidBodyStates,
577
682
  physicsOptions,
578
683
  rigidBodyEvents
579
684
  } = useRapier();
580
- const ref = React.useRef(); // Create rigidbody
685
+ const ref = React$1.useRef(); // Create rigidbody
581
686
 
582
- const rigidBodyRef = React.useRef();
583
- const getRigidBodyRef = React.useRef(() => {
687
+ const rigidBodyRef = React$1.useRef();
688
+ const getRigidBodyRef = React$1.useRef(() => {
584
689
  if (!rigidBodyRef.current) {
585
- var _options$linearVeloci, _options$angularVeloc, _options$gravityScale, _options$canSleep, _options$ccd, _options$enabledRotat, _options$enabledTrans;
586
-
587
- const type = rigidBodyTypeFromString((options === null || options === void 0 ? void 0 : options.type) || "dynamic");
588
- 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];
589
- 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];
590
- const gravityScale = (_options$gravityScale = options === null || options === void 0 ? void 0 : options.gravityScale) !== null && _options$gravityScale !== void 0 ? _options$gravityScale : 1;
591
- const canSleep = (_options$canSleep = options === null || options === void 0 ? void 0 : options.canSleep) !== null && _options$canSleep !== void 0 ? _options$canSleep : true;
592
- const ccdEnabled = (_options$ccd = options === null || options === void 0 ? void 0 : options.ccd) !== null && _options$ccd !== void 0 ? _options$ccd : false;
593
- 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];
594
- 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];
595
- const desc = new rapier.RigidBodyDesc(type).setLinvel(lvx, lvy, lvz).setAngvel({
596
- x: avx,
597
- y: avy,
598
- z: avz
599
- }).setGravityScale(gravityScale).setCanSleep(canSleep).setCcdEnabled(ccdEnabled).enabledRotations(erx, ery, erz).enabledTranslations(etx, ety, etz);
600
- if (options.lockRotations) desc.lockRotations();
601
- if (options.lockTranslations) desc.lockTranslations();
690
+ const desc = rigidBodyDescFromOptions(options);
602
691
  const rigidBody = world.createRigidBody(desc);
603
692
  rigidBodyRef.current = world.getRigidBody(rigidBody.handle);
604
693
  }
@@ -606,7 +695,7 @@ const useRigidBody = (options = {}) => {
606
695
  return rigidBodyRef.current;
607
696
  }); // Setup
608
697
 
609
- React.useEffect(() => {
698
+ React$1.useEffect(() => {
610
699
  var _ref$current$parent, _ref, _options$colliders;
611
700
 
612
701
  const rigidBody = getRigidBodyRef.current();
@@ -635,8 +724,7 @@ const useRigidBody = (options = {}) => {
635
724
  y: worldPosition.y + y * scale.y,
636
725
  z: worldPosition.z + z * scale.z
637
726
  }, false);
638
- const eulerAngles = new three.Euler(rx, ry, rz, 'XYZ');
639
- const rotation = new three.Quaternion().setFromEuler(eulerAngles).multiply(worldRotation);
727
+ const rotation = vector3ToQuaternion(new three.Vector3(rx, ry, rz)).multiply(worldRotation);
640
728
  rigidBody.setRotation({
641
729
  x: rotation.x,
642
730
  y: rotation.y,
@@ -649,16 +737,23 @@ const useRigidBody = (options = {}) => {
649
737
  const autoColliders = colliderSetting !== false ? createCollidersFromChildren(ref.current, rigidBody, _objectSpread2(_objectSpread2({}, options), {}, {
650
738
  colliders: colliderSetting
651
739
  }), world) : [];
652
- rigidBodyMeshes.set(rigidBody.handle, ref.current);
740
+ rigidBodyStates.set(rigidBody.handle, {
741
+ mesh: ref.current,
742
+ invertedMatrixWorld: ref.current.parent.matrixWorld.clone().invert(),
743
+ isSleeping: false,
744
+ worldScale: ref.current.getWorldScale(_vector3).clone(),
745
+ setMatrix: mat => ref.current.matrix.copy(mat)
746
+ });
747
+ ref.current.matrixAutoUpdate = false;
653
748
  return () => {
654
749
  world.removeRigidBody(rigidBody);
655
750
  autoColliders.forEach(collider => world.removeCollider(collider));
656
751
  rigidBodyRef.current = undefined;
657
- rigidBodyMeshes.delete(rigidBody.handle);
752
+ rigidBodyStates.delete(rigidBody.handle);
658
753
  };
659
754
  }, []); // Events
660
755
 
661
- React.useEffect(() => {
756
+ React$1.useEffect(() => {
662
757
  const rigidBody = getRigidBodyRef.current();
663
758
  rigidBodyEvents.set(rigidBody.handle, {
664
759
  onCollisionEnter: options === null || options === void 0 ? void 0 : options.onCollisionEnter,
@@ -670,44 +765,21 @@ const useRigidBody = (options = {}) => {
670
765
  rigidBodyEvents.delete(rigidBody.handle);
671
766
  };
672
767
  }, [options.onCollisionEnter, options.onCollisionExit]);
673
- const api = React.useMemo(() => createRigidBodyApi(getRigidBodyRef), []);
768
+ const api = React$1.useMemo(() => createRigidBodyApi(getRigidBodyRef), []);
674
769
  return [ref, api];
675
- };
676
- const useCollider = (body, options = {}) => {
677
- const {
678
- world
679
- } = useRapier();
680
- const colliderRef = React.useRef();
681
- const objectRef = React.useRef();
682
- const getColliderRef = React.useRef(() => {
683
- if (!colliderRef.current) {
684
- colliderRef.current = createColliderFromOptions(options, world, world.getRigidBody(body.handle));
685
- }
686
-
687
- return colliderRef.current;
688
- });
689
- React.useEffect(() => {
690
- const collider = getColliderRef.current();
691
- return () => {
692
- if (collider) world.removeCollider(collider);
693
- colliderRef.current = undefined;
694
- };
695
- }, []);
696
- const api = React.useMemo(() => createColliderApi(getColliderRef), []);
697
- return [objectRef, api];
698
770
  }; // Joints
699
771
 
700
772
  const useImpulseJoint = (body1, body2, params) => {
701
773
  const {
702
774
  world
703
775
  } = useRapier();
704
- const jointRef = React.useRef();
705
- const getJointRef = React.useRef(() => {
776
+ const jointRef = React$1.useRef();
777
+ const getJointRef = React$1.useRef(() => {
706
778
  if (!jointRef.current) {
707
779
  let rb1;
708
780
  let rb2;
709
781
 
710
- if ('current' in body1 && body1.current && 'current' in body2 && body2.current) {
782
+ if ("current" in body1 && body1.current && "current" in body2 && body2.current) {
711
783
  rb1 = world.getRigidBody(body1.current.handle);
712
784
  rb2 = world.getRigidBody(body2.current.handle);
713
785
  const newJoint = world.createImpulseJoint(params, rb1, rb2);
@@ -717,7 +789,7 @@ const useImpulseJoint = (body1, body2, params) => {
717
789
 
718
790
  return jointRef.current;
719
791
  });
720
- React.useEffect(() => {
792
+ React$1.useEffect(() => {
721
793
  const joint = getJointRef.current();
722
794
  return () => {
723
795
  if (joint) {
@@ -726,7 +798,7 @@ const useImpulseJoint = (body1, body2, params) => {
726
798
  }
727
799
  };
728
800
  }, []);
729
- const api = React.useMemo(() => createJointApi(getJointRef), []);
801
+ const api = React$1.useMemo(() => createJointApi(getJointRef), []);
730
802
  return api;
731
803
  };
732
804
  /**
@@ -784,149 +856,69 @@ const usePrismaticJoint = (body1, body2, [body1Anchor, body2Anchor, axis]) => {
784
856
  return useImpulseJoint(body1, body2, rapier.JointData.prismatic(vectorArrayToObject(body1Anchor), vectorArrayToObject(body2Anchor), vectorArrayToObject(axis)));
785
857
  };
786
858
 
787
- function _extends() {
788
- _extends = Object.assign || function (target) {
789
- for (var i = 1; i < arguments.length; i++) {
790
- var source = arguments[i];
791
-
792
- for (var key in source) {
793
- if (Object.prototype.hasOwnProperty.call(source, key)) {
794
- target[key] = source[key];
795
- }
796
- }
797
- }
798
-
799
- return target;
800
- };
801
-
802
- return _extends.apply(this, arguments);
803
- }
804
-
805
- function _objectWithoutPropertiesLoose(source, excluded) {
806
- if (source == null) return {};
807
- var target = {};
808
- var sourceKeys = Object.keys(source);
809
- var key, i;
810
-
811
- for (i = 0; i < sourceKeys.length; i++) {
812
- key = sourceKeys[i];
813
- if (excluded.indexOf(key) >= 0) continue;
814
- target[key] = source[key];
815
- }
816
-
817
- return target;
818
- }
819
-
820
- function _objectWithoutProperties(source, excluded) {
821
- if (source == null) return {};
822
- var target = _objectWithoutPropertiesLoose(source, excluded);
823
- var key, i;
824
-
825
- if (Object.getOwnPropertySymbols) {
826
- var sourceSymbolKeys = Object.getOwnPropertySymbols(source);
827
-
828
- for (i = 0; i < sourceSymbolKeys.length; i++) {
829
- key = sourceSymbolKeys[i];
830
- if (excluded.indexOf(key) >= 0) continue;
831
- if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
832
- target[key] = source[key];
833
- }
834
- }
835
-
836
- return target;
837
- }
838
-
839
- const _excluded = ["children"],
840
- _excluded2 = ["children"];
841
- const RigidBodyContext = /*#__PURE__*/React.createContext(undefined);
842
-
843
- const useRigidBodyContext = () => React.useContext(RigidBodyContext); // RigidBody
844
-
859
+ const _excluded$1 = ["children"];
860
+ const RigidBodyContext = /*#__PURE__*/React$1.createContext(undefined);
861
+ const useRigidBodyContext = () => React$1.useContext(RigidBodyContext); // RigidBody
845
862
 
846
- const RigidBody = /*#__PURE__*/React.forwardRef((_ref, ref) => {
863
+ const RigidBody = /*#__PURE__*/React$1.forwardRef((_ref, ref) => {
847
864
  let {
848
865
  children
849
866
  } = _ref,
850
- props = _objectWithoutProperties(_ref, _excluded);
867
+ props = _objectWithoutProperties(_ref, _excluded$1);
851
868
 
852
- const [object, rigidBody] = useRigidBody(props);
853
- React.useImperativeHandle(ref, () => rigidBody);
869
+ const [object, api] = useRigidBody(props);
870
+ React$1.useImperativeHandle(ref, () => api);
854
871
  return /*#__PURE__*/React__default["default"].createElement(RigidBodyContext.Provider, {
855
- value: [object, rigidBody, !!(props.onCollisionEnter || props.onCollisionExit)]
872
+ value: {
873
+ ref: object,
874
+ api,
875
+ hasCollisionEvents: !!(props.onCollisionEnter || props.onCollisionExit),
876
+ options: props
877
+ }
856
878
  }, /*#__PURE__*/React__default["default"].createElement("object3D", {
857
879
  ref: object
858
880
  }, children));
859
- }); // Colliders
860
-
861
- const AnyCollider = _ref2 => {
862
- let {
863
- children
864
- } = _ref2,
865
- props = _objectWithoutProperties(_ref2, _excluded2);
881
+ });
866
882
 
883
+ const MeshCollider = ({
884
+ children,
885
+ type
886
+ }) => {
867
887
  const {
888
+ physicsOptions,
868
889
  world
869
890
  } = useRapier();
870
- const [, rigidBody, hasCollisionEvents] = useRigidBodyContext();
871
- const ref = React.useRef(null);
872
- React.useEffect(() => {
873
- const scale = ref.current.getWorldScale(new three.Vector3());
874
- const collider = createColliderFromOptions(props, world, rigidBody.raw(), scale, hasCollisionEvents);
891
+ const object = React$1.useRef(null);
892
+ const {
893
+ api,
894
+ options
895
+ } = useRigidBodyContext();
896
+ React$1.useEffect(() => {
897
+ let autoColliders = [];
898
+
899
+ if (object.current) {
900
+ var _ref;
901
+
902
+ const colliderSetting = (_ref = type !== null && type !== void 0 ? type : physicsOptions.colliders) !== null && _ref !== void 0 ? _ref : false;
903
+ autoColliders = colliderSetting !== false ? createCollidersFromChildren(object.current, api, _objectSpread2(_objectSpread2({}, options), {}, {
904
+ colliders: colliderSetting
905
+ }), world, false) : [];
906
+ }
907
+
875
908
  return () => {
876
- world.removeCollider(collider);
909
+ autoColliders.forEach(collider => {
910
+ world.removeCollider(collider);
911
+ });
877
912
  };
878
913
  }, []);
879
- return /*#__PURE__*/React__default["default"].createElement("object3D", {
880
- ref: ref
914
+ return /*#__PURE__*/React.createElement("object3D", {
915
+ ref: object,
916
+ userData: {
917
+ r3RapierType: "MeshCollider"
918
+ }
881
919
  }, children);
882
920
  };
883
921
 
884
- const CuboidCollider = props => {
885
- return /*#__PURE__*/React__default["default"].createElement(AnyCollider, _extends({}, props, {
886
- shape: "cuboid"
887
- }));
888
- };
889
- const RoundCuboidCollider = props => {
890
- return /*#__PURE__*/React__default["default"].createElement(AnyCollider, _extends({}, props, {
891
- shape: "roundCuboid"
892
- }));
893
- };
894
- const BallCollider = props => {
895
- return /*#__PURE__*/React__default["default"].createElement(AnyCollider, _extends({}, props, {
896
- shape: "ball"
897
- }));
898
- };
899
- const CapsuleCollider = props => {
900
- return /*#__PURE__*/React__default["default"].createElement(AnyCollider, _extends({}, props, {
901
- shape: "capsule"
902
- }));
903
- };
904
- const HeightfieldCollider = props => {
905
- return /*#__PURE__*/React__default["default"].createElement(AnyCollider, _extends({}, props, {
906
- shape: "heightfield"
907
- }));
908
- };
909
- const TrimeshCollider = props => {
910
- return /*#__PURE__*/React__default["default"].createElement(AnyCollider, _extends({}, props, {
911
- shape: "trimesh"
912
- }));
913
- };
914
- const ConeCollider = props => {
915
- return /*#__PURE__*/React__default["default"].createElement(AnyCollider, _extends({}, props, {
916
- shape: "cone"
917
- }));
918
- };
919
- const CylinderCollider = props => {
920
- return /*#__PURE__*/React__default["default"].createElement(AnyCollider, _extends({}, props, {
921
- shape: "cylinder"
922
- }));
923
- };
924
- const ConvexHullCollider = props => {
925
- return /*#__PURE__*/React__default["default"].createElement(AnyCollider, _extends({}, props, {
926
- shape: "convexHull"
927
- }));
928
- };
929
-
930
922
  const geometryFromCollider = collider => {
931
923
  switch (collider.shape.type) {
932
924
  case rapier3dCompat.ShapeType.Cuboid:
@@ -982,13 +974,13 @@ const geometryFromCollider = collider => {
982
974
  return new three.BoxBufferGeometry(1, 1, 1);
983
975
  };
984
976
 
985
- const DebugShape = /*#__PURE__*/React.memo(({
977
+ const DebugShape = /*#__PURE__*/React$1.memo(({
986
978
  colliderHandle
987
979
  }) => {
988
980
  const {
989
981
  world
990
982
  } = useRapier();
991
- const ref = React.useRef(null);
983
+ const ref = React$1.useRef(null);
992
984
  fiber.useFrame(() => {
993
985
  const collider = world.getCollider(colliderHandle);
994
986
 
@@ -1008,7 +1000,7 @@ const DebugShape = /*#__PURE__*/React.memo(({
1008
1000
  ref.current.rotation.setFromQuaternion(new three.Quaternion(rx, ry, rz, rw));
1009
1001
  }
1010
1002
  });
1011
- const geometry = React.useMemo(() => {
1003
+ const geometry = React$1.useMemo(() => {
1012
1004
  const collider = world.getCollider(colliderHandle);
1013
1005
  return geometryFromCollider(collider);
1014
1006
  }, [colliderHandle]);
@@ -1026,8 +1018,8 @@ const Debug = () => {
1026
1018
  const {
1027
1019
  world
1028
1020
  } = useRapier();
1029
- const [colliders, setColliders] = React.useState([]);
1030
- React.useRef({});
1021
+ const [colliders, setColliders] = React$1.useState([]);
1022
+ React$1.useRef({});
1031
1023
 
1032
1024
  fiber.useFrame(() => {
1033
1025
  const newColliders = [];
@@ -1042,6 +1034,200 @@ const Debug = () => {
1042
1034
  })));
1043
1035
  };
1044
1036
 
1037
+ const InstancedRigidBodies = /*#__PURE__*/React$1.forwardRef((props, ref) => {
1038
+ const {
1039
+ world,
1040
+ rigidBodyStates,
1041
+ physicsOptions
1042
+ } = useRapier();
1043
+ const object = React$1.useRef(null);
1044
+ const instancesRef = React$1.useRef();
1045
+ const instancesRefGetter = React$1.useRef(() => {
1046
+ if (!instancesRef.current) {
1047
+ instancesRef.current = [];
1048
+ }
1049
+
1050
+ return instancesRef.current;
1051
+ });
1052
+ React$1.useEffect(() => {
1053
+ const colliders = [];
1054
+ const rigidBodies = instancesRefGetter.current();
1055
+
1056
+ if (object.current) {
1057
+ const scale = object.current.getWorldScale(new three.Vector3());
1058
+ let hasOneMesh = false;
1059
+ object.current.traverse(mesh => {
1060
+ if (mesh instanceof three.InstancedMesh) {
1061
+ if (hasOneMesh) {
1062
+ console.warn("Can only use a single InstancedMesh inside <InstancedRigidBodies />, more InstancedMeshes will be ignored.");
1063
+ return;
1064
+ }
1065
+
1066
+ hasOneMesh = true;
1067
+ mesh.instanceMatrix.setUsage(three.DynamicDrawUsage);
1068
+ const rigidBodyDesc = rigidBodyDescFromOptions(props);
1069
+ const colliderDesc = colliderDescFromGeometry(mesh.geometry, props.colliders || physicsOptions.colliders, scale, false // Collisions currently not enabled for instances
1070
+ );
1071
+
1072
+ for (let index = 0; index < mesh.count; index++) {
1073
+ const rigidBody = world.createRigidBody(rigidBodyDesc);
1074
+ const matrix = new three.Matrix4();
1075
+ mesh.getMatrixAt(index, matrix);
1076
+ const {
1077
+ position,
1078
+ rotation
1079
+ } = decomposeMatrix4(matrix); // Set positions
1080
+
1081
+ if (props.positions && props.positions[index]) {
1082
+ rigidBody.setTranslation(vectorArrayToObject(props.positions[index]), true);
1083
+ } else {
1084
+ rigidBody.setTranslation(position, true);
1085
+ } // Set rotations
1086
+
1087
+
1088
+ if (props.rotations && props.rotations[index]) {
1089
+ const [x, y, z] = props.rotations[index];
1090
+ rigidBody.setRotation(vector3ToQuaternion(new three.Vector3(x, y, z)), true);
1091
+ } else {
1092
+ rigidBody.setRotation(rotation, true);
1093
+ }
1094
+
1095
+ const collider = world.createCollider(colliderDesc, rigidBody);
1096
+ rigidBodyStates.set(rigidBody.handle, {
1097
+ mesh: mesh,
1098
+ isSleeping: false,
1099
+ invertedMatrixWorld: object.current.matrixWorld.clone().invert(),
1100
+ setMatrix: matrix => mesh.setMatrixAt(index, matrix),
1101
+ worldScale: object.current.getWorldScale(new three.Vector3())
1102
+ });
1103
+ const api = createRigidBodyApi({
1104
+ current() {
1105
+ return rigidBody;
1106
+ }
1107
+
1108
+ });
1109
+ colliders.push(collider);
1110
+ rigidBodies.push({
1111
+ rigidBody,
1112
+ api
1113
+ });
1114
+ }
1115
+ }
1116
+
1117
+ if (mesh.type === "Mesh" && !("isInstancedMesh" in mesh)) {
1118
+ console.warn("Can only use InstancedMesh inside <InstancedRigidBodies />, Mesh will be ignored.");
1119
+ }
1120
+ });
1121
+ return () => {
1122
+ rigidBodies.forEach(rb => world.removeRigidBody(rb.rigidBody));
1123
+ colliders.forEach(coll => world.removeCollider(coll));
1124
+ instancesRef.current = undefined;
1125
+ };
1126
+ }
1127
+ }, []);
1128
+ React$1.useImperativeHandle(ref, () => createInstancedRigidBodiesApi(instancesRefGetter));
1129
+ return /*#__PURE__*/React.createElement("object3D", {
1130
+ ref: object
1131
+ }, props.children);
1132
+ });
1133
+
1134
+ function _extends() {
1135
+ _extends = Object.assign || function (target) {
1136
+ for (var i = 1; i < arguments.length; i++) {
1137
+ var source = arguments[i];
1138
+
1139
+ for (var key in source) {
1140
+ if (Object.prototype.hasOwnProperty.call(source, key)) {
1141
+ target[key] = source[key];
1142
+ }
1143
+ }
1144
+ }
1145
+
1146
+ return target;
1147
+ };
1148
+
1149
+ return _extends.apply(this, arguments);
1150
+ }
1151
+
1152
+ const _excluded = ["children"];
1153
+
1154
+ const AnyCollider = _ref => {
1155
+ let {
1156
+ children
1157
+ } = _ref,
1158
+ props = _objectWithoutProperties(_ref, _excluded);
1159
+
1160
+ const {
1161
+ world
1162
+ } = useRapier();
1163
+ const rigidBodyContext = useRigidBodyContext();
1164
+ const ref = React$1.useRef(null);
1165
+ React$1.useEffect(() => {
1166
+ var _rigidBodyContext$api;
1167
+
1168
+ const scale = ref.current.getWorldScale(new three.Vector3());
1169
+ const collider = createColliderFromOptions({
1170
+ options: props,
1171
+ world,
1172
+ rigidBody: rigidBodyContext === null || rigidBodyContext === void 0 ? void 0 : (_rigidBodyContext$api = rigidBodyContext.api) === null || _rigidBodyContext$api === void 0 ? void 0 : _rigidBodyContext$api.raw(),
1173
+ scale,
1174
+ hasCollisionEvents: rigidBodyContext === null || rigidBodyContext === void 0 ? void 0 : rigidBodyContext.hasCollisionEvents
1175
+ });
1176
+ return () => {
1177
+ world.removeCollider(collider);
1178
+ };
1179
+ }, []);
1180
+ return /*#__PURE__*/React.createElement("object3D", {
1181
+ ref: ref
1182
+ }, children);
1183
+ };
1184
+
1185
+ const CuboidCollider = props => {
1186
+ return /*#__PURE__*/React.createElement(AnyCollider, _extends({}, props, {
1187
+ shape: "cuboid"
1188
+ }));
1189
+ };
1190
+ const RoundCuboidCollider = props => {
1191
+ return /*#__PURE__*/React.createElement(AnyCollider, _extends({}, props, {
1192
+ shape: "roundCuboid"
1193
+ }));
1194
+ };
1195
+ const BallCollider = props => {
1196
+ return /*#__PURE__*/React.createElement(AnyCollider, _extends({}, props, {
1197
+ shape: "ball"
1198
+ }));
1199
+ };
1200
+ const CapsuleCollider = props => {
1201
+ return /*#__PURE__*/React.createElement(AnyCollider, _extends({}, props, {
1202
+ shape: "capsule"
1203
+ }));
1204
+ };
1205
+ const HeightfieldCollider = props => {
1206
+ return /*#__PURE__*/React.createElement(AnyCollider, _extends({}, props, {
1207
+ shape: "heightfield"
1208
+ }));
1209
+ };
1210
+ const TrimeshCollider = props => {
1211
+ return /*#__PURE__*/React.createElement(AnyCollider, _extends({}, props, {
1212
+ shape: "trimesh"
1213
+ }));
1214
+ };
1215
+ const ConeCollider = props => {
1216
+ return /*#__PURE__*/React.createElement(AnyCollider, _extends({}, props, {
1217
+ shape: "cone"
1218
+ }));
1219
+ };
1220
+ const CylinderCollider = props => {
1221
+ return /*#__PURE__*/React.createElement(AnyCollider, _extends({}, props, {
1222
+ shape: "cylinder"
1223
+ }));
1224
+ };
1225
+ const ConvexHullCollider = props => {
1226
+ return /*#__PURE__*/React.createElement(AnyCollider, _extends({}, props, {
1227
+ shape: "convexHull"
1228
+ }));
1229
+ };
1230
+
1045
1231
  Object.defineProperty(exports, 'CoefficientCombineRule', {
1046
1232
  enumerable: true,
1047
1233
  get: function () { return rapier3dCompat.CoefficientCombineRule; }
@@ -1062,11 +1248,12 @@ exports.CuboidCollider = CuboidCollider;
1062
1248
  exports.CylinderCollider = CylinderCollider;
1063
1249
  exports.Debug = Debug;
1064
1250
  exports.HeightfieldCollider = HeightfieldCollider;
1251
+ exports.InstancedRigidBodies = InstancedRigidBodies;
1252
+ exports.MeshCollider = MeshCollider;
1065
1253
  exports.Physics = Physics;
1066
1254
  exports.RigidBody = RigidBody;
1067
1255
  exports.RoundCuboidCollider = RoundCuboidCollider;
1068
1256
  exports.TrimeshCollider = TrimeshCollider;
1069
- exports.useCollider = useCollider;
1070
1257
  exports.useFixedJoint = useFixedJoint;
1071
1258
  exports.useImpulseJoint = useImpulseJoint;
1072
1259
  exports.usePrismaticJoint = usePrismaticJoint;