@react-three/rapier 0.4.2 → 0.5.1
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.
- package/CHANGELOG.md +28 -0
- package/dist/declarations/src/Physics.d.ts +23 -1
- package/dist/declarations/src/api.d.ts +18 -1
- package/dist/declarations/src/components.d.ts +6 -1
- package/dist/declarations/src/hooks.d.ts +3 -35
- package/dist/declarations/src/types.d.ts +20 -3
- package/dist/declarations/src/utils.d.ts +5 -4
- package/dist/react-three-rapier.cjs.dev.js +168 -220
- package/dist/react-three-rapier.cjs.prod.js +168 -220
- package/dist/react-three-rapier.esm.js +169 -206
- package/package.json +2 -2
- package/readme.md +25 -19
@@ -3,7 +3,7 @@ import { useAsset } from 'use-asset';
|
|
3
3
|
import { useFrame } from '@react-three/fiber';
|
4
4
|
import { ColliderDesc, CoefficientCombineRule, ActiveEvents, EventQueue, ShapeType } from '@dimforge/rapier3d-compat';
|
5
5
|
export { CoefficientCombineRule, Collider as RapierCollider, RigidBody as RapierRigidBody } from '@dimforge/rapier3d-compat';
|
6
|
-
import {
|
6
|
+
import { Quaternion, Euler, Vector3, Object3D, CylinderBufferGeometry, BufferGeometry, BufferAttribute, SphereBufferGeometry, BoxBufferGeometry } from 'three';
|
7
7
|
|
8
8
|
const vectorArrayToObject = arr => {
|
9
9
|
const [x, y, z] = arr;
|
@@ -88,13 +88,23 @@ const createColliderFromOptions = (options, world, rigidBody, scale = {
|
|
88
88
|
const collider = world.createCollider(colliderDesc, rigidBody);
|
89
89
|
return collider;
|
90
90
|
};
|
91
|
-
|
91
|
+
|
92
|
+
const isChildOfMeshCollider = child => {
|
93
|
+
let flag = false;
|
94
|
+
child.traverseAncestors(a => {
|
95
|
+
if (a.userData.r3RapierType === "MeshCollider") flag = true;
|
96
|
+
});
|
97
|
+
return flag;
|
98
|
+
};
|
99
|
+
|
100
|
+
const createCollidersFromChildren = (object, rigidBody, options, world, ignoreMeshColliders = true) => {
|
92
101
|
const hasCollisionEvents = !!(options.onCollisionEnter || options.onCollisionExit);
|
93
102
|
const colliders = [];
|
94
103
|
let desc;
|
95
104
|
let offset = new Vector3();
|
96
105
|
object.traverse(child => {
|
97
106
|
if ("isMesh" in child) {
|
107
|
+
if (ignoreMeshColliders && isChildOfMeshCollider(child)) return;
|
98
108
|
const {
|
99
109
|
geometry
|
100
110
|
} = child;
|
@@ -165,7 +175,8 @@ const createCollidersFromChildren = (object, rigidBody, options, world) => {
|
|
165
175
|
if (hasCollisionEvents) desc.setActiveEvents(ActiveEvents.COLLISION_EVENTS);
|
166
176
|
if (Number.isFinite(options.friction)) desc.setFriction(options.friction);
|
167
177
|
if (Number.isFinite(options.restitution)) desc.setRestitution(options.restitution);
|
168
|
-
const
|
178
|
+
const actualRigidBody = world.getRigidBody(rigidBody === null || rigidBody === void 0 ? void 0 : rigidBody.handle);
|
179
|
+
const collider = world.createCollider(desc, actualRigidBody);
|
169
180
|
colliders.push(collider);
|
170
181
|
}
|
171
182
|
});
|
@@ -182,47 +193,11 @@ const scaleVertices = (vertices, scale) => {
|
|
182
193
|
|
183
194
|
return scaledVerts;
|
184
195
|
};
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
enumerable: true,
|
191
|
-
configurable: true,
|
192
|
-
writable: true
|
193
|
-
});
|
194
|
-
} else {
|
195
|
-
obj[key] = value;
|
196
|
-
}
|
197
|
-
|
198
|
-
return obj;
|
199
|
-
}
|
200
|
-
|
201
|
-
function ownKeys(object, enumerableOnly) {
|
202
|
-
var keys = Object.keys(object);
|
203
|
-
|
204
|
-
if (Object.getOwnPropertySymbols) {
|
205
|
-
var symbols = Object.getOwnPropertySymbols(object);
|
206
|
-
enumerableOnly && (symbols = symbols.filter(function (sym) {
|
207
|
-
return Object.getOwnPropertyDescriptor(object, sym).enumerable;
|
208
|
-
})), keys.push.apply(keys, symbols);
|
209
|
-
}
|
210
|
-
|
211
|
-
return keys;
|
212
|
-
}
|
213
|
-
|
214
|
-
function _objectSpread2(target) {
|
215
|
-
for (var i = 1; i < arguments.length; i++) {
|
216
|
-
var source = null != arguments[i] ? arguments[i] : {};
|
217
|
-
i % 2 ? ownKeys(Object(source), !0).forEach(function (key) {
|
218
|
-
_defineProperty(target, key, source[key]);
|
219
|
-
}) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) {
|
220
|
-
Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
|
221
|
-
});
|
222
|
-
}
|
223
|
-
|
224
|
-
return target;
|
225
|
-
}
|
196
|
+
const quaternion = new Quaternion();
|
197
|
+
const euler = new Euler();
|
198
|
+
const vector3ToQuaternion = v => {
|
199
|
+
return quaternion.setFromEuler(euler.setFromVector3(v));
|
200
|
+
};
|
226
201
|
|
227
202
|
const createRigidBodyApi = ref => {
|
228
203
|
return {
|
@@ -268,9 +243,19 @@ const createRigidBodyApi = ref => {
|
|
268
243
|
return new Quaternion(x, y, z, w);
|
269
244
|
},
|
270
245
|
|
271
|
-
setRotation:
|
272
|
-
|
273
|
-
|
246
|
+
setRotation: ({
|
247
|
+
x,
|
248
|
+
y,
|
249
|
+
z
|
250
|
+
}) => {
|
251
|
+
const q = vector3ToQuaternion(new Vector3(x, y, z));
|
252
|
+
ref.current().setRotation({
|
253
|
+
x: q.x,
|
254
|
+
y: q.y,
|
255
|
+
z: q.z,
|
256
|
+
w: q.w
|
257
|
+
}, true);
|
258
|
+
},
|
274
259
|
|
275
260
|
linvel() {
|
276
261
|
const {
|
@@ -293,12 +278,26 @@ const createRigidBodyApi = ref => {
|
|
293
278
|
},
|
294
279
|
|
295
280
|
setAngvel: velocity => ref.current().setAngvel(velocity, true),
|
296
|
-
setNextKinematicRotation:
|
297
|
-
|
298
|
-
|
281
|
+
setNextKinematicRotation: ({
|
282
|
+
x,
|
283
|
+
y,
|
284
|
+
z
|
285
|
+
}) => {
|
286
|
+
const q = vector3ToQuaternion(new Vector3(x, y, z));
|
287
|
+
ref.current().setNextKinematicRotation({
|
288
|
+
x: q.x,
|
289
|
+
y: q.y,
|
290
|
+
z: q.z,
|
291
|
+
w: q.w
|
292
|
+
});
|
293
|
+
},
|
299
294
|
setNextKinematicTranslation: translation => ref.current().setNextKinematicTranslation(translation),
|
300
295
|
resetForces: () => ref.current().resetForces(true),
|
301
|
-
resetTorques: () => ref.current().resetTorques(true)
|
296
|
+
resetTorques: () => ref.current().resetTorques(true),
|
297
|
+
lockRotations: locked => ref.current().lockRotations(locked, true),
|
298
|
+
lockTranslations: locked => ref.current().lockTranslations(locked, true),
|
299
|
+
setEnabledRotations: (x, y, z) => ref.current().setEnabledRotations(x, y, z, true),
|
300
|
+
setEnabledTranslations: (x, y, z) => ref.current().setEnabledTranslations(x, y, z, true)
|
302
301
|
};
|
303
302
|
}; // TODO: Flesh this out
|
304
303
|
|
@@ -323,7 +322,16 @@ const createWorldApi = ref => {
|
|
323
322
|
removeCollider: collider => ref.current().removeCollider(collider, true),
|
324
323
|
createImpulseJoint: (params, rigidBodyA, rigidBodyB) => ref.current().createImpulseJoint(params, rigidBodyA, rigidBodyB, true),
|
325
324
|
removeImpulseJoint: joint => ref.current().removeImpulseJoint(joint, true),
|
326
|
-
forEachCollider: callback => ref.current().forEachCollider(callback)
|
325
|
+
forEachCollider: callback => ref.current().forEachCollider(callback),
|
326
|
+
setGravity: ({
|
327
|
+
x,
|
328
|
+
y,
|
329
|
+
z
|
330
|
+
}) => ref.current().gravity = {
|
331
|
+
x,
|
332
|
+
y,
|
333
|
+
z
|
334
|
+
}
|
327
335
|
};
|
328
336
|
}; // TODO: Broken currently, waiting for Rapier3D to fix
|
329
337
|
|
@@ -351,7 +359,8 @@ const importRapier = async () => {
|
|
351
359
|
const Physics = ({
|
352
360
|
colliders: _colliders = 'cuboid',
|
353
361
|
gravity: _gravity = [0, -9.81, 0],
|
354
|
-
children
|
362
|
+
children,
|
363
|
+
timeStep: _timeStep = 'vary'
|
355
364
|
}) => {
|
356
365
|
const rapier = useAsset(importRapier);
|
357
366
|
const worldRef = useRef();
|
@@ -376,7 +385,15 @@ const Physics = ({
|
|
376
385
|
worldRef.current = undefined;
|
377
386
|
}
|
378
387
|
};
|
379
|
-
}, []);
|
388
|
+
}, []); // Update gravity
|
389
|
+
|
390
|
+
useEffect(() => {
|
391
|
+
const world = worldRef.current;
|
392
|
+
|
393
|
+
if (world) {
|
394
|
+
world.gravity = vectorArrayToObject(_gravity);
|
395
|
+
}
|
396
|
+
}, [_gravity]);
|
380
397
|
const time = useRef(performance.now());
|
381
398
|
useFrame(context => {
|
382
399
|
const world = worldRef.current;
|
@@ -385,7 +402,13 @@ const Physics = ({
|
|
385
402
|
|
386
403
|
const now = performance.now();
|
387
404
|
const delta = Math.min(100, now - time.current);
|
388
|
-
|
405
|
+
|
406
|
+
if (_timeStep === 'vary') {
|
407
|
+
world.timestep = delta / 1000;
|
408
|
+
} else {
|
409
|
+
world.timestep = _timeStep;
|
410
|
+
}
|
411
|
+
|
389
412
|
world.step(eventQueue); // Update meshes
|
390
413
|
|
391
414
|
rigidBodyMeshes.forEach((mesh, handle) => {
|
@@ -498,6 +521,47 @@ const Physics = ({
|
|
498
521
|
}, children);
|
499
522
|
};
|
500
523
|
|
524
|
+
function _defineProperty(obj, key, value) {
|
525
|
+
if (key in obj) {
|
526
|
+
Object.defineProperty(obj, key, {
|
527
|
+
value: value,
|
528
|
+
enumerable: true,
|
529
|
+
configurable: true,
|
530
|
+
writable: true
|
531
|
+
});
|
532
|
+
} else {
|
533
|
+
obj[key] = value;
|
534
|
+
}
|
535
|
+
|
536
|
+
return obj;
|
537
|
+
}
|
538
|
+
|
539
|
+
function ownKeys(object, enumerableOnly) {
|
540
|
+
var keys = Object.keys(object);
|
541
|
+
|
542
|
+
if (Object.getOwnPropertySymbols) {
|
543
|
+
var symbols = Object.getOwnPropertySymbols(object);
|
544
|
+
enumerableOnly && (symbols = symbols.filter(function (sym) {
|
545
|
+
return Object.getOwnPropertyDescriptor(object, sym).enumerable;
|
546
|
+
})), keys.push.apply(keys, symbols);
|
547
|
+
}
|
548
|
+
|
549
|
+
return keys;
|
550
|
+
}
|
551
|
+
|
552
|
+
function _objectSpread2(target) {
|
553
|
+
for (var i = 1; i < arguments.length; i++) {
|
554
|
+
var source = null != arguments[i] ? arguments[i] : {};
|
555
|
+
i % 2 ? ownKeys(Object(source), !0).forEach(function (key) {
|
556
|
+
_defineProperty(target, key, source[key]);
|
557
|
+
}) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) {
|
558
|
+
Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
|
559
|
+
});
|
560
|
+
}
|
561
|
+
|
562
|
+
return target;
|
563
|
+
}
|
564
|
+
|
501
565
|
const useRapier = () => {
|
502
566
|
return useContext(RapierContext);
|
503
567
|
};
|
@@ -514,7 +578,7 @@ const useRigidBody = (options = {}) => {
|
|
514
578
|
const rigidBodyRef = useRef();
|
515
579
|
const getRigidBodyRef = useRef(() => {
|
516
580
|
if (!rigidBodyRef.current) {
|
517
|
-
var _options$linearVeloci, _options$angularVeloc, _options$gravityScale, _options$canSleep, _options$ccd;
|
581
|
+
var _options$linearVeloci, _options$angularVeloc, _options$gravityScale, _options$canSleep, _options$ccd, _options$enabledRotat, _options$enabledTrans;
|
518
582
|
|
519
583
|
const type = rigidBodyTypeFromString((options === null || options === void 0 ? void 0 : options.type) || "dynamic");
|
520
584
|
const [lvx, lvy, lvz] = (_options$linearVeloci = options === null || options === void 0 ? void 0 : options.linearVelocity) !== null && _options$linearVeloci !== void 0 ? _options$linearVeloci : [0, 0, 0];
|
@@ -522,11 +586,15 @@ const useRigidBody = (options = {}) => {
|
|
522
586
|
const gravityScale = (_options$gravityScale = options === null || options === void 0 ? void 0 : options.gravityScale) !== null && _options$gravityScale !== void 0 ? _options$gravityScale : 1;
|
523
587
|
const canSleep = (_options$canSleep = options === null || options === void 0 ? void 0 : options.canSleep) !== null && _options$canSleep !== void 0 ? _options$canSleep : true;
|
524
588
|
const ccdEnabled = (_options$ccd = options === null || options === void 0 ? void 0 : options.ccd) !== null && _options$ccd !== void 0 ? _options$ccd : false;
|
589
|
+
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];
|
590
|
+
const [etx, ety, etz] = (_options$enabledTrans = options === null || options === void 0 ? void 0 : options.enabledTranslations) !== null && _options$enabledTrans !== void 0 ? _options$enabledTrans : [true, true, true];
|
525
591
|
const desc = new rapier.RigidBodyDesc(type).setLinvel(lvx, lvy, lvz).setAngvel({
|
526
592
|
x: avx,
|
527
593
|
y: avy,
|
528
594
|
z: avz
|
529
|
-
}).setGravityScale(gravityScale).setCanSleep(canSleep).setCcdEnabled(ccdEnabled);
|
595
|
+
}).setGravityScale(gravityScale).setCanSleep(canSleep).setCcdEnabled(ccdEnabled).enabledRotations(erx, ery, erz).enabledTranslations(etx, ety, etz);
|
596
|
+
if (options.lockRotations) desc.lockRotations();
|
597
|
+
if (options.lockTranslations) desc.lockTranslations();
|
530
598
|
const rigidBody = world.createRigidBody(desc);
|
531
599
|
rigidBodyRef.current = world.getRigidBody(rigidBody.handle);
|
532
600
|
}
|
@@ -574,7 +642,9 @@ const useRigidBody = (options = {}) => {
|
|
574
642
|
rigidBody.resetForces(false);
|
575
643
|
rigidBody.resetTorques(false);
|
576
644
|
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;
|
577
|
-
const autoColliders = colliderSetting !== false ? createCollidersFromChildren(ref.current, rigidBody,
|
645
|
+
const autoColliders = colliderSetting !== false ? createCollidersFromChildren(ref.current, rigidBody, _objectSpread2(_objectSpread2({}, options), {}, {
|
646
|
+
colliders: colliderSetting
|
647
|
+
}), world) : [];
|
578
648
|
rigidBodyMeshes.set(rigidBody.handle, ref.current);
|
579
649
|
return () => {
|
580
650
|
world.removeRigidBody(rigidBody);
|
@@ -621,142 +691,6 @@ const useCollider = (body, options = {}) => {
|
|
621
691
|
}, []);
|
622
692
|
const api = useMemo(() => createColliderApi(getColliderRef), []);
|
623
693
|
return [objectRef, api];
|
624
|
-
};
|
625
|
-
const useRigidBodyWithCollider = (rigidBodyOptions, colliderOptions) => {
|
626
|
-
const {
|
627
|
-
world
|
628
|
-
} = useRapier();
|
629
|
-
const [ref, rigidBody] = useRigidBody(rigidBodyOptions);
|
630
|
-
useEffect(() => {
|
631
|
-
if (!colliderOptions) {
|
632
|
-
return;
|
633
|
-
}
|
634
|
-
|
635
|
-
const scale = ref.current.getWorldScale(new Vector3());
|
636
|
-
const collider = createColliderFromOptions(colliderOptions, world, world.getRigidBody(rigidBody.handle), scale);
|
637
|
-
return () => {
|
638
|
-
world.removeCollider(collider);
|
639
|
-
};
|
640
|
-
}, []);
|
641
|
-
return [ref, rigidBody];
|
642
|
-
};
|
643
|
-
const useCuboid = (rigidBodyOptions = {}, colliderOptions = {}) => {
|
644
|
-
var _colliderOptions$args;
|
645
|
-
|
646
|
-
return useRigidBodyWithCollider(rigidBodyOptions, _objectSpread2({
|
647
|
-
shape: "cuboid",
|
648
|
-
args: (_colliderOptions$args = colliderOptions.args) !== null && _colliderOptions$args !== void 0 ? _colliderOptions$args : [0.5, 0.5, 0.5]
|
649
|
-
}, colliderOptions));
|
650
|
-
};
|
651
|
-
const useBall = (rigidBodyOptions = {}, colliderOptions = {}) => {
|
652
|
-
var _colliderOptions$args2;
|
653
|
-
|
654
|
-
return useRigidBodyWithCollider(rigidBodyOptions, _objectSpread2({
|
655
|
-
shape: "ball",
|
656
|
-
args: (_colliderOptions$args2 = colliderOptions.args) !== null && _colliderOptions$args2 !== void 0 ? _colliderOptions$args2 : [0.5]
|
657
|
-
}, colliderOptions));
|
658
|
-
};
|
659
|
-
const useCapsule = (rigidBodyOptions = {}, colliderOptions = {}) => {
|
660
|
-
var _colliderOptions$args3;
|
661
|
-
|
662
|
-
return useRigidBodyWithCollider(rigidBodyOptions, _objectSpread2({
|
663
|
-
shape: "capsule",
|
664
|
-
args: (_colliderOptions$args3 = colliderOptions.args) !== null && _colliderOptions$args3 !== void 0 ? _colliderOptions$args3 : [0.5, 0.5]
|
665
|
-
}, colliderOptions));
|
666
|
-
};
|
667
|
-
const useHeightfield = (rigidBodyOptions = {}, colliderOptions = {}) => {
|
668
|
-
return useRigidBodyWithCollider(rigidBodyOptions, _objectSpread2({
|
669
|
-
shape: "heightfield"
|
670
|
-
}, colliderOptions));
|
671
|
-
};
|
672
|
-
/**
|
673
|
-
* Create a trimesh collider and rigid body.
|
674
|
-
* Note that Trimeshes don't have mass unless provided.
|
675
|
-
* See https://rapier.rs/docs/user_guides/javascript/rigid_bodies#mass-properties
|
676
|
-
* for available properties.
|
677
|
-
*/
|
678
|
-
|
679
|
-
const useTrimesh = (rigidBodyOptions = {}, colliderOptions = {}) => {
|
680
|
-
return useRigidBodyWithCollider(rigidBodyOptions, _objectSpread2({
|
681
|
-
shape: "trimesh"
|
682
|
-
}, colliderOptions));
|
683
|
-
};
|
684
|
-
|
685
|
-
useTrimesh.fromMesh = (mesh, rigidBodyOptions = {}, colliderOptions = {}) => {
|
686
|
-
var _mesh$geometry, _mesh$geometry$index;
|
687
|
-
|
688
|
-
return useTrimesh(rigidBodyOptions, _objectSpread2({
|
689
|
-
args: [mesh.geometry.attributes.position.array, ((_mesh$geometry = mesh.geometry) === null || _mesh$geometry === void 0 ? void 0 : (_mesh$geometry$index = _mesh$geometry.index) === null || _mesh$geometry$index === void 0 ? void 0 : _mesh$geometry$index.array) || []]
|
690
|
-
}, colliderOptions));
|
691
|
-
};
|
692
|
-
|
693
|
-
const usePolyline = (rigidBodyOptions = {}, colliderOptions = {}) => {
|
694
|
-
return useRigidBodyWithCollider(rigidBodyOptions, _objectSpread2({
|
695
|
-
shape: "polyline"
|
696
|
-
}, colliderOptions));
|
697
|
-
};
|
698
|
-
const useRoundCuboid = (rigidBodyOptions = {}, colliderOptions = {}) => {
|
699
|
-
return useRigidBodyWithCollider(rigidBodyOptions, _objectSpread2({
|
700
|
-
shape: "roundCuboid"
|
701
|
-
}, colliderOptions));
|
702
|
-
};
|
703
|
-
const useCylinder = (rigidBodyOptions = {}, colliderOptions = {}) => {
|
704
|
-
return useRigidBodyWithCollider(rigidBodyOptions, _objectSpread2({
|
705
|
-
shape: "cylinder"
|
706
|
-
}, colliderOptions));
|
707
|
-
};
|
708
|
-
const useRoundCylinder = (rigidBodyOptions = {}, colliderOptions = {}) => {
|
709
|
-
return useRigidBodyWithCollider(rigidBodyOptions, _objectSpread2({
|
710
|
-
shape: "roundCylinder"
|
711
|
-
}, colliderOptions));
|
712
|
-
};
|
713
|
-
const useCone = (rigidBodyOptions = {}, colliderOptions = {}) => {
|
714
|
-
return useRigidBodyWithCollider(rigidBodyOptions, _objectSpread2({
|
715
|
-
shape: "cone"
|
716
|
-
}, colliderOptions));
|
717
|
-
};
|
718
|
-
const useRoundCone = (rigidBodyOptions = {}, colliderOptions = {}) => {
|
719
|
-
return useRigidBodyWithCollider(rigidBodyOptions, _objectSpread2({
|
720
|
-
shape: "roundCone"
|
721
|
-
}, colliderOptions));
|
722
|
-
};
|
723
|
-
const useConvexHull = (rigidBodyOptions = {}, colliderOptions = {}) => {
|
724
|
-
return useRigidBodyWithCollider(rigidBodyOptions, _objectSpread2({
|
725
|
-
shape: "convexHull"
|
726
|
-
}, colliderOptions));
|
727
|
-
};
|
728
|
-
|
729
|
-
useConvexHull.fromMesh = (mesh, rigidBodyOptions = {}, colliderOptions = {}) => {
|
730
|
-
var _mesh$geometry2, _mesh$geometry2$attri, _mesh$geometry2$attri2;
|
731
|
-
|
732
|
-
return useConvexHull(rigidBodyOptions, _objectSpread2({
|
733
|
-
args: [(mesh === null || mesh === void 0 ? void 0 : (_mesh$geometry2 = mesh.geometry) === null || _mesh$geometry2 === void 0 ? void 0 : (_mesh$geometry2$attri = _mesh$geometry2.attributes) === null || _mesh$geometry2$attri === void 0 ? void 0 : (_mesh$geometry2$attri2 = _mesh$geometry2$attri.position) === null || _mesh$geometry2$attri2 === void 0 ? void 0 : _mesh$geometry2$attri2.array) || []]
|
734
|
-
}, colliderOptions));
|
735
|
-
};
|
736
|
-
|
737
|
-
const useRoundConvexHull = (rigidBodyOptions = {}, colliderOptions = {}) => {
|
738
|
-
return useRigidBodyWithCollider(rigidBodyOptions, _objectSpread2({
|
739
|
-
shape: "roundConvexHull"
|
740
|
-
}, colliderOptions));
|
741
|
-
};
|
742
|
-
const useConvexMesh = (rigidBodyOptions = {}, colliderOptions = {}) => {
|
743
|
-
return useRigidBodyWithCollider(rigidBodyOptions, _objectSpread2({
|
744
|
-
shape: "convexMesh"
|
745
|
-
}, colliderOptions));
|
746
|
-
};
|
747
|
-
|
748
|
-
useConvexMesh.fromMesh = (mesh, rigidBodyOptions = {}, colliderOptions = {}) => {
|
749
|
-
var _mesh$geometry3, _mesh$geometry3$attri, _mesh$geometry3$attri2, _mesh$geometry4, _mesh$geometry4$index;
|
750
|
-
|
751
|
-
return useConvexMesh(rigidBodyOptions, _objectSpread2({
|
752
|
-
args: [mesh === null || mesh === void 0 ? void 0 : (_mesh$geometry3 = mesh.geometry) === null || _mesh$geometry3 === void 0 ? void 0 : (_mesh$geometry3$attri = _mesh$geometry3.attributes) === null || _mesh$geometry3$attri === void 0 ? void 0 : (_mesh$geometry3$attri2 = _mesh$geometry3$attri.position) === null || _mesh$geometry3$attri2 === void 0 ? void 0 : _mesh$geometry3$attri2.array, ((_mesh$geometry4 = mesh.geometry) === null || _mesh$geometry4 === void 0 ? void 0 : (_mesh$geometry4$index = _mesh$geometry4.index) === null || _mesh$geometry4$index === void 0 ? void 0 : _mesh$geometry4$index.array) || []]
|
753
|
-
}, colliderOptions));
|
754
|
-
};
|
755
|
-
|
756
|
-
const useRoundConvexMesh = (rigidBodyOptions = {}, colliderOptions = {}) => {
|
757
|
-
return useRigidBodyWithCollider(rigidBodyOptions, _objectSpread2({
|
758
|
-
shape: "convexMesh"
|
759
|
-
}, colliderOptions));
|
760
694
|
}; // Joints
|
761
695
|
|
762
696
|
const useImpulseJoint = (body1, body2, params) => {
|
@@ -769,12 +703,6 @@ const useImpulseJoint = (body1, body2, params) => {
|
|
769
703
|
let rb1;
|
770
704
|
let rb2;
|
771
705
|
|
772
|
-
if ('handle' in body1 && 'handle' in body2) {
|
773
|
-
rb1 = world.getRigidBody(body1.handle);
|
774
|
-
rb2 = world.getRigidBody(body2.handle);
|
775
|
-
jointRef.current = world.createImpulseJoint(params, rb1, rb2);
|
776
|
-
}
|
777
|
-
|
778
706
|
if ('current' in body1 && body1.current && 'current' in body2 && body2.current) {
|
779
707
|
rb1 = world.getRigidBody(body1.current.handle);
|
780
708
|
rb2 = world.getRigidBody(body2.current.handle);
|
@@ -920,17 +848,52 @@ const RigidBody = /*#__PURE__*/forwardRef((_ref, ref) => {
|
|
920
848
|
const [object, rigidBody] = useRigidBody(props);
|
921
849
|
useImperativeHandle(ref, () => rigidBody);
|
922
850
|
return /*#__PURE__*/React.createElement(RigidBodyContext.Provider, {
|
923
|
-
value: [object, rigidBody, !!(props.onCollisionEnter || props.onCollisionExit)]
|
851
|
+
value: [object, rigidBody, !!(props.onCollisionEnter || props.onCollisionExit), props]
|
924
852
|
}, /*#__PURE__*/React.createElement("object3D", {
|
925
853
|
ref: object
|
926
854
|
}, children));
|
927
|
-
});
|
855
|
+
});
|
856
|
+
const MeshCollider = ({
|
857
|
+
children,
|
858
|
+
type
|
859
|
+
}) => {
|
860
|
+
const {
|
861
|
+
physicsOptions,
|
862
|
+
world
|
863
|
+
} = useRapier();
|
864
|
+
const object = useRef(null);
|
865
|
+
const [, rigidBody, hasCollisionEvents, rigidBodyOptions] = useRigidBodyContext();
|
866
|
+
useEffect(() => {
|
867
|
+
let autoColliders = [];
|
868
|
+
|
869
|
+
if (object.current) {
|
870
|
+
var _ref2;
|
871
|
+
|
872
|
+
const colliderSetting = (_ref2 = type !== null && type !== void 0 ? type : physicsOptions.colliders) !== null && _ref2 !== void 0 ? _ref2 : false;
|
873
|
+
autoColliders = colliderSetting !== false ? createCollidersFromChildren(object.current, rigidBody, _objectSpread2(_objectSpread2({}, rigidBodyOptions), {}, {
|
874
|
+
colliders: colliderSetting
|
875
|
+
}), world, false) : [];
|
876
|
+
}
|
877
|
+
|
878
|
+
return () => {
|
879
|
+
autoColliders.forEach(collider => {
|
880
|
+
world.removeCollider(collider);
|
881
|
+
});
|
882
|
+
};
|
883
|
+
}, []);
|
884
|
+
return /*#__PURE__*/React.createElement("object3D", {
|
885
|
+
ref: object,
|
886
|
+
userData: {
|
887
|
+
r3RapierType: 'MeshCollider'
|
888
|
+
}
|
889
|
+
}, children);
|
890
|
+
}; // Colliders
|
928
891
|
|
929
|
-
const AnyCollider =
|
892
|
+
const AnyCollider = _ref3 => {
|
930
893
|
let {
|
931
894
|
children
|
932
|
-
} =
|
933
|
-
props = _objectWithoutProperties(
|
895
|
+
} = _ref3,
|
896
|
+
props = _objectWithoutProperties(_ref3, _excluded2);
|
934
897
|
|
935
898
|
const {
|
936
899
|
world
|
@@ -1110,4 +1073,4 @@ const Debug = () => {
|
|
1110
1073
|
})));
|
1111
1074
|
};
|
1112
1075
|
|
1113
|
-
export { BallCollider, CapsuleCollider, ConeCollider, ConvexHullCollider, CuboidCollider, CylinderCollider, Debug, HeightfieldCollider, Physics, RigidBody, RoundCuboidCollider, TrimeshCollider,
|
1076
|
+
export { BallCollider, CapsuleCollider, ConeCollider, ConvexHullCollider, CuboidCollider, CylinderCollider, Debug, HeightfieldCollider, MeshCollider, Physics, RigidBody, RoundCuboidCollider, TrimeshCollider, useCollider, useFixedJoint, useImpulseJoint, usePrismaticJoint, useRapier, useRevoluteJoint, useRigidBody, useSphericalJoint };
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@react-three/rapier",
|
3
|
-
"version": "0.
|
3
|
+
"version": "0.5.1",
|
4
4
|
"source": "src/index.ts",
|
5
5
|
"main": "dist/react-three-rapier.cjs.js",
|
6
6
|
"module": "dist/react-three-rapier.esm.js",
|
@@ -23,7 +23,7 @@
|
|
23
23
|
"three": "^0.139.2"
|
24
24
|
},
|
25
25
|
"dependencies": {
|
26
|
-
"@dimforge/rapier3d-compat": "0.
|
26
|
+
"@dimforge/rapier3d-compat": "0.9.0",
|
27
27
|
"use-asset": "^1.0.4"
|
28
28
|
},
|
29
29
|
"repository": "https://github.com/pmndrs/react-three-rapier/tree/master/packages/react-three-rapier"
|
package/readme.md
CHANGED
@@ -60,19 +60,39 @@ Turn off automatic collider generation globally, but apply auto generation local
|
|
60
60
|
```tsx
|
61
61
|
const Scene = () => (
|
62
62
|
<Physics colliders={false}>
|
63
|
+
{/* Use an automatic CuboidCollider for all meshes inside this RigidBody */}
|
63
64
|
<RigidBody colliders="cuboid">
|
64
65
|
<Box />
|
65
66
|
</RigidBody>
|
66
67
|
|
68
|
+
{/* Use an automatic BallCollider for all meshes inside this RigidBody */}
|
67
69
|
<RigidBody position={[0, 10, 0]} colliders="ball">
|
68
70
|
<Sphere />
|
69
71
|
</RigidBody>
|
70
72
|
|
73
|
+
{/* Make a compound shape with two custom BallColliders */}
|
71
74
|
<RigidBody position={[0, 10, 0]}>
|
72
75
|
<Sphere />
|
73
76
|
<BallCollider args={0.5} />
|
74
77
|
<BallCollider args={0.5} position={[1, 0, 0]} />
|
75
78
|
</RigidBody>
|
79
|
+
|
80
|
+
{/* Make a compound shape with two custom BallColliders, an automatic BallCollider,
|
81
|
+
Two automatic MeshColliders, based on two different shape strategies */}
|
82
|
+
<RigidBody position={[0, 10, 0]} colliders='ball'>
|
83
|
+
<MeshCollider type="trimesh">
|
84
|
+
<mesh ... />
|
85
|
+
</MeshCollider>
|
86
|
+
|
87
|
+
<MeshCollider type="hull">
|
88
|
+
<mesh ... />
|
89
|
+
</MeshCollider>
|
90
|
+
|
91
|
+
<Sphere />
|
92
|
+
|
93
|
+
<BallCollider args={0.5} />
|
94
|
+
<BallCollider args={0.5} position={[1, 0, 0]} />
|
95
|
+
</RigidBody>
|
76
96
|
</Physics>
|
77
97
|
);
|
78
98
|
```
|
@@ -134,7 +154,7 @@ return (
|
|
134
154
|
colliders="hull"
|
135
155
|
onSleep={() => setIsAsleep(true)}
|
136
156
|
onWake={() => setIsAsleep(false)}
|
137
|
-
|
157
|
+
onCollisionEnter={({manifold}) => {
|
138
158
|
console.log('Collision at world position ', manifold.solverContactPoint(0))
|
139
159
|
}}
|
140
160
|
>
|
@@ -146,24 +166,9 @@ return (
|
|
146
166
|
}
|
147
167
|
```
|
148
168
|
|
149
|
-
##
|
169
|
+
## Joints
|
150
170
|
|
151
|
-
|
152
|
-
|
153
|
-
```tsx
|
154
|
-
import { Box } from "@react-three/drei";
|
155
|
-
import { useCuboid } from "@react-three/rapier";
|
156
|
-
|
157
|
-
const RigidBox = () => {
|
158
|
-
// Generates a RigidBody and attaches a BoxCollider to it, returns a ref
|
159
|
-
const [box, rigidBody, collider] = useCuboid(
|
160
|
-
{ position: [1, 1, 1] },
|
161
|
-
{ args: [0.5, 0.5, 0.5] }
|
162
|
-
);
|
163
|
-
|
164
|
-
return <Box ref={box} />;
|
165
|
-
};
|
166
|
-
```
|
171
|
+
WIP
|
167
172
|
|
168
173
|
## Roadmap?
|
169
174
|
|
@@ -174,8 +179,9 @@ In order, but also not necessarily:
|
|
174
179
|
- [x] Nested objects retain world transforms
|
175
180
|
- [x] Nested objects retain correct collider scale
|
176
181
|
- [x] Automatic colliders based on rigidbody children
|
177
|
-
- [
|
182
|
+
- [x] Translation and rotational constraints
|
178
183
|
- [x] Collision events
|
184
|
+
- [ ] Colliders outside RigidBodies
|
179
185
|
- [ ] InstancedMesh support
|
180
186
|
- [ ] Docs
|
181
187
|
- [ ] CodeSandbox examples
|