@react-three/rapier 0.6.0 → 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.
- package/dist/declarations/src/InstancedRigidBodies.d.ts +4 -4
- package/dist/declarations/src/Physics.d.ts +8 -1
- package/dist/declarations/src/RigidBody.d.ts +12 -4
- package/dist/declarations/src/api.d.ts +16 -0
- package/dist/declarations/src/types.d.ts +6 -2
- package/dist/declarations/src/utils.d.ts +12 -7
- package/dist/react-three-rapier.cjs.dev.js +186 -104
- package/dist/react-three-rapier.cjs.prod.js +186 -104
- package/dist/react-three-rapier.esm.js +141 -59
- package/package.json +1 -1
- package/readme.md +19 -2
@@ -3,7 +3,7 @@
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
4
4
|
|
5
5
|
var rapier3dCompat = require('@dimforge/rapier3d-compat');
|
6
|
-
var React
|
6
|
+
var React = require('react');
|
7
7
|
var useAsset = require('use-asset');
|
8
8
|
var fiber = require('@react-three/fiber');
|
9
9
|
var three = require('three');
|
@@ -28,7 +28,7 @@ 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);
|
32
32
|
|
33
33
|
const _quaternion = new three.Quaternion();
|
34
34
|
const _euler = new three.Euler();
|
@@ -36,13 +36,9 @@ const _vector3 = new three.Vector3();
|
|
36
36
|
new three.Object3D();
|
37
37
|
const _matrix4 = new three.Matrix4();
|
38
38
|
|
39
|
-
const
|
39
|
+
const vectorArrayToVector3 = arr => {
|
40
40
|
const [x, y, z] = arr;
|
41
|
-
return
|
42
|
-
x,
|
43
|
-
y,
|
44
|
-
z
|
45
|
-
};
|
41
|
+
return new three.Vector3(x, y, z);
|
46
42
|
};
|
47
43
|
const vector3ToQuaternion = v => {
|
48
44
|
return _quaternion.setFromEuler(_euler.setFromVector3(v));
|
@@ -158,13 +154,19 @@ const isChildOfMeshCollider = child => {
|
|
158
154
|
return flag;
|
159
155
|
};
|
160
156
|
|
161
|
-
const createCollidersFromChildren = (
|
157
|
+
const createCollidersFromChildren = ({
|
158
|
+
object,
|
159
|
+
rigidBody,
|
160
|
+
options,
|
161
|
+
world,
|
162
|
+
ignoreMeshColliders: _ignoreMeshColliders = true
|
163
|
+
}) => {
|
162
164
|
const hasCollisionEvents = !!(options.onCollisionEnter || options.onCollisionExit);
|
163
165
|
const colliders = [];
|
164
166
|
new three.Vector3();
|
165
|
-
object.
|
167
|
+
object.traverseVisible(child => {
|
166
168
|
if ("isMesh" in child) {
|
167
|
-
if (
|
169
|
+
if (_ignoreMeshColliders && isChildOfMeshCollider(child)) return;
|
168
170
|
const {
|
169
171
|
geometry
|
170
172
|
} = child;
|
@@ -205,7 +207,7 @@ const createCollidersFromChildren = (object, rigidBody, options, world, ignoreMe
|
|
205
207
|
z: rz,
|
206
208
|
w: rw
|
207
209
|
});
|
208
|
-
const actualRigidBody = world.getRigidBody(rigidBody
|
210
|
+
const actualRigidBody = rigidBody ? world.getRigidBody(rigidBody.handle) : undefined;
|
209
211
|
const collider = world.createCollider(desc, actualRigidBody);
|
210
212
|
colliders.push(collider);
|
211
213
|
}
|
@@ -271,11 +273,13 @@ const scaleVertices = (vertices, scale) => {
|
|
271
273
|
return scaledVerts;
|
272
274
|
};
|
273
275
|
const rigidBodyDescFromOptions = options => {
|
274
|
-
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;
|
275
277
|
|
276
278
|
const type = rigidBodyTypeFromString((options === null || options === void 0 ? void 0 : options.type) || "dynamic");
|
277
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];
|
278
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;
|
279
283
|
const gravityScale = (_options$gravityScale = options === null || options === void 0 ? void 0 : options.gravityScale) !== null && _options$gravityScale !== void 0 ? _options$gravityScale : 1;
|
280
284
|
const canSleep = (_options$canSleep = options === null || options === void 0 ? void 0 : options.canSleep) !== null && _options$canSleep !== void 0 ? _options$canSleep : true;
|
281
285
|
const ccdEnabled = (_options$ccd = options === null || options === void 0 ? void 0 : options.ccd) !== null && _options$ccd !== void 0 ? _options$ccd : false;
|
@@ -285,7 +289,7 @@ const rigidBodyDescFromOptions = options => {
|
|
285
289
|
x: avx,
|
286
290
|
y: avy,
|
287
291
|
z: avz
|
288
|
-
}).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);
|
289
293
|
if (options.lockRotations) desc.lockRotations();
|
290
294
|
if (options.lockTranslations) desc.lockTranslations();
|
291
295
|
return desc;
|
@@ -370,6 +374,18 @@ const createRigidBodyApi = ref => {
|
|
370
374
|
},
|
371
375
|
|
372
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),
|
373
389
|
setNextKinematicRotation: ({
|
374
390
|
x,
|
375
391
|
y,
|
@@ -441,7 +457,7 @@ const createJointApi = ref => {
|
|
441
457
|
};
|
442
458
|
};
|
443
459
|
|
444
|
-
const RapierContext = /*#__PURE__*/React
|
460
|
+
const RapierContext = /*#__PURE__*/React.createContext(undefined);
|
445
461
|
|
446
462
|
const importRapier = async () => {
|
447
463
|
let r = await Promise.resolve().then(function () { return /*#__PURE__*/_interopNamespace(require('@dimforge/rapier3d-compat')); });
|
@@ -453,24 +469,25 @@ const Physics = ({
|
|
453
469
|
colliders: _colliders = "cuboid",
|
454
470
|
gravity: _gravity = [0, -9.81, 0],
|
455
471
|
children,
|
456
|
-
timeStep: _timeStep = "vary"
|
472
|
+
timeStep: _timeStep = "vary",
|
473
|
+
paused: _paused = false
|
457
474
|
}) => {
|
458
475
|
const rapier = useAsset.useAsset(importRapier);
|
459
|
-
const worldRef = React
|
460
|
-
const getWorldRef = React
|
476
|
+
const worldRef = React.useRef();
|
477
|
+
const getWorldRef = React.useRef(() => {
|
461
478
|
if (!worldRef.current) {
|
462
|
-
const world = new rapier.World(
|
479
|
+
const world = new rapier.World(vectorArrayToVector3(_gravity));
|
463
480
|
worldRef.current = world;
|
464
481
|
}
|
465
482
|
|
466
483
|
return worldRef.current;
|
467
484
|
});
|
468
|
-
const [colliderMeshes] = React
|
469
|
-
const [rigidBodyStates] = React
|
470
|
-
const [rigidBodyEvents] = React
|
471
|
-
const [eventQueue] = React
|
485
|
+
const [colliderMeshes] = React.useState(() => new Map());
|
486
|
+
const [rigidBodyStates] = React.useState(() => new Map());
|
487
|
+
const [rigidBodyEvents] = React.useState(() => new Map());
|
488
|
+
const [eventQueue] = React.useState(() => new rapier3dCompat.EventQueue(false)); // Init world
|
472
489
|
|
473
|
-
React
|
490
|
+
React.useEffect(() => {
|
474
491
|
const world = getWorldRef.current();
|
475
492
|
return () => {
|
476
493
|
if (world) {
|
@@ -480,14 +497,14 @@ const Physics = ({
|
|
480
497
|
};
|
481
498
|
}, []); // Update gravity
|
482
499
|
|
483
|
-
React
|
500
|
+
React.useEffect(() => {
|
484
501
|
const world = worldRef.current;
|
485
502
|
|
486
503
|
if (world) {
|
487
|
-
world.gravity =
|
504
|
+
world.gravity = vectorArrayToVector3(_gravity);
|
488
505
|
}
|
489
506
|
}, [_gravity]);
|
490
|
-
const time = React
|
507
|
+
const time = React.useRef(performance.now());
|
491
508
|
fiber.useFrame(context => {
|
492
509
|
const world = worldRef.current;
|
493
510
|
if (!world) return; // Set timestep to current delta, to allow for variable frame rates
|
@@ -502,7 +519,7 @@ const Physics = ({
|
|
502
519
|
world.timestep = _timeStep;
|
503
520
|
}
|
504
521
|
|
505
|
-
world.step(eventQueue); // Update meshes
|
522
|
+
if (!_paused) world.step(eventQueue); // Update meshes
|
506
523
|
|
507
524
|
rigidBodyStates.forEach((state, handle) => {
|
508
525
|
const rigidBody = world.getRigidBody(handle);
|
@@ -580,8 +597,8 @@ const Physics = ({
|
|
580
597
|
});
|
581
598
|
time.current = now;
|
582
599
|
});
|
583
|
-
const api = React
|
584
|
-
const context = React
|
600
|
+
const api = React.useMemo(() => createWorldApi(getWorldRef), []);
|
601
|
+
const context = React.useMemo(() => ({
|
585
602
|
rapier,
|
586
603
|
world: api,
|
587
604
|
physicsOptions: {
|
@@ -673,7 +690,7 @@ function _objectSpread2(target) {
|
|
673
690
|
}
|
674
691
|
|
675
692
|
const useRapier = () => {
|
676
|
-
return React
|
693
|
+
return React.useContext(RapierContext);
|
677
694
|
};
|
678
695
|
const useRigidBody = (options = {}) => {
|
679
696
|
const {
|
@@ -682,10 +699,10 @@ const useRigidBody = (options = {}) => {
|
|
682
699
|
physicsOptions,
|
683
700
|
rigidBodyEvents
|
684
701
|
} = useRapier();
|
685
|
-
const ref = React
|
702
|
+
const ref = React.useRef(); // Create rigidbody
|
686
703
|
|
687
|
-
const rigidBodyRef = React
|
688
|
-
const getRigidBodyRef = React
|
704
|
+
const rigidBodyRef = React.useRef();
|
705
|
+
const getRigidBodyRef = React.useRef(() => {
|
689
706
|
if (!rigidBodyRef.current) {
|
690
707
|
const desc = rigidBodyDescFromOptions(options);
|
691
708
|
const rigidBody = world.createRigidBody(desc);
|
@@ -695,7 +712,7 @@ const useRigidBody = (options = {}) => {
|
|
695
712
|
return rigidBodyRef.current;
|
696
713
|
}); // Setup
|
697
714
|
|
698
|
-
React
|
715
|
+
React.useEffect(() => {
|
699
716
|
var _ref$current$parent, _ref, _options$colliders;
|
700
717
|
|
701
718
|
const rigidBody = getRigidBodyRef.current();
|
@@ -734,15 +751,22 @@ const useRigidBody = (options = {}) => {
|
|
734
751
|
rigidBody.resetForces(false);
|
735
752
|
rigidBody.resetTorques(false);
|
736
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;
|
737
|
-
const autoColliders = colliderSetting !== false ? createCollidersFromChildren(
|
738
|
-
|
739
|
-
|
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
|
+
}) : [];
|
740
763
|
rigidBodyStates.set(rigidBody.handle, {
|
741
764
|
mesh: ref.current,
|
742
765
|
invertedMatrixWorld: ref.current.parent.matrixWorld.clone().invert(),
|
743
766
|
isSleeping: false,
|
744
767
|
worldScale: ref.current.getWorldScale(_vector3).clone(),
|
745
|
-
setMatrix: mat => ref.current.matrix.copy(mat)
|
768
|
+
setMatrix: mat => ref.current.matrix.copy(mat),
|
769
|
+
getMatrix: () => ref.current.matrix
|
746
770
|
});
|
747
771
|
ref.current.matrixAutoUpdate = false;
|
748
772
|
return () => {
|
@@ -753,7 +777,7 @@ const useRigidBody = (options = {}) => {
|
|
753
777
|
};
|
754
778
|
}, []); // Events
|
755
779
|
|
756
|
-
React
|
780
|
+
React.useEffect(() => {
|
757
781
|
const rigidBody = getRigidBodyRef.current();
|
758
782
|
rigidBodyEvents.set(rigidBody.handle, {
|
759
783
|
onCollisionEnter: options === null || options === void 0 ? void 0 : options.onCollisionEnter,
|
@@ -765,7 +789,7 @@ const useRigidBody = (options = {}) => {
|
|
765
789
|
rigidBodyEvents.delete(rigidBody.handle);
|
766
790
|
};
|
767
791
|
}, [options.onCollisionEnter, options.onCollisionExit]);
|
768
|
-
const api = React
|
792
|
+
const api = React.useMemo(() => createRigidBodyApi(getRigidBodyRef), []);
|
769
793
|
return [ref, api];
|
770
794
|
}; // Joints
|
771
795
|
|
@@ -773,8 +797,8 @@ const useImpulseJoint = (body1, body2, params) => {
|
|
773
797
|
const {
|
774
798
|
world
|
775
799
|
} = useRapier();
|
776
|
-
const jointRef = React
|
777
|
-
const getJointRef = React
|
800
|
+
const jointRef = React.useRef();
|
801
|
+
const getJointRef = React.useRef(() => {
|
778
802
|
if (!jointRef.current) {
|
779
803
|
let rb1;
|
780
804
|
let rb2;
|
@@ -789,7 +813,7 @@ const useImpulseJoint = (body1, body2, params) => {
|
|
789
813
|
|
790
814
|
return jointRef.current;
|
791
815
|
});
|
792
|
-
React
|
816
|
+
React.useEffect(() => {
|
793
817
|
const joint = getJointRef.current();
|
794
818
|
return () => {
|
795
819
|
if (joint) {
|
@@ -798,7 +822,7 @@ const useImpulseJoint = (body1, body2, params) => {
|
|
798
822
|
}
|
799
823
|
};
|
800
824
|
}, []);
|
801
|
-
const api = React
|
825
|
+
const api = React.useMemo(() => createJointApi(getJointRef), []);
|
802
826
|
return api;
|
803
827
|
};
|
804
828
|
/**
|
@@ -812,9 +836,9 @@ const useFixedJoint = (body1, body2, [body1Anchor, body1LocalFrame, body2Anchor,
|
|
812
836
|
const {
|
813
837
|
rapier
|
814
838
|
} = useRapier();
|
815
|
-
return useImpulseJoint(body1, body2, rapier.JointData.fixed(
|
839
|
+
return useImpulseJoint(body1, body2, rapier.JointData.fixed(vectorArrayToVector3(body1Anchor), _objectSpread2(_objectSpread2({}, vectorArrayToVector3(body1LocalFrame)), {}, {
|
816
840
|
w: 1
|
817
|
-
}),
|
841
|
+
}), vectorArrayToVector3(body2Anchor), _objectSpread2(_objectSpread2({}, vectorArrayToVector3(body2LocalFrame)), {}, {
|
818
842
|
w: 1
|
819
843
|
})));
|
820
844
|
};
|
@@ -829,7 +853,7 @@ const useSphericalJoint = (body1, body2, [body1Anchor, body2Anchor]) => {
|
|
829
853
|
const {
|
830
854
|
rapier
|
831
855
|
} = useRapier();
|
832
|
-
return useImpulseJoint(body1, body2, rapier.JointData.spherical(
|
856
|
+
return useImpulseJoint(body1, body2, rapier.JointData.spherical(vectorArrayToVector3(body1Anchor), vectorArrayToVector3(body2Anchor)));
|
833
857
|
};
|
834
858
|
/**
|
835
859
|
* The revolute joint prevents any relative movement between two rigid-bodies, except for relative
|
@@ -841,7 +865,7 @@ const useRevoluteJoint = (body1, body2, [body1Anchor, body2Anchor, axis]) => {
|
|
841
865
|
const {
|
842
866
|
rapier
|
843
867
|
} = useRapier();
|
844
|
-
return useImpulseJoint(body1, body2, rapier.JointData.revolute(
|
868
|
+
return useImpulseJoint(body1, body2, rapier.JointData.revolute(vectorArrayToVector3(body1Anchor), vectorArrayToVector3(body2Anchor), vectorArrayToVector3(axis)));
|
845
869
|
};
|
846
870
|
/**
|
847
871
|
* The prismatic joint prevents any relative movement between two rigid-bodies, except for relative translations along one axis.
|
@@ -853,21 +877,21 @@ const usePrismaticJoint = (body1, body2, [body1Anchor, body2Anchor, axis]) => {
|
|
853
877
|
const {
|
854
878
|
rapier
|
855
879
|
} = useRapier();
|
856
|
-
return useImpulseJoint(body1, body2, rapier.JointData.prismatic(
|
880
|
+
return useImpulseJoint(body1, body2, rapier.JointData.prismatic(vectorArrayToVector3(body1Anchor), vectorArrayToVector3(body2Anchor), vectorArrayToVector3(axis)));
|
857
881
|
};
|
858
882
|
|
859
883
|
const _excluded$1 = ["children"];
|
860
|
-
const RigidBodyContext = /*#__PURE__*/React
|
861
|
-
const useRigidBodyContext = () => React
|
884
|
+
const RigidBodyContext = /*#__PURE__*/React.createContext(undefined);
|
885
|
+
const useRigidBodyContext = () => React.useContext(RigidBodyContext); // RigidBody
|
862
886
|
|
863
|
-
const RigidBody = /*#__PURE__*/React
|
887
|
+
const RigidBody = /*#__PURE__*/React.forwardRef((_ref, ref) => {
|
864
888
|
let {
|
865
889
|
children
|
866
890
|
} = _ref,
|
867
891
|
props = _objectWithoutProperties(_ref, _excluded$1);
|
868
892
|
|
869
893
|
const [object, api] = useRigidBody(props);
|
870
|
-
React
|
894
|
+
React.useImperativeHandle(ref, () => api);
|
871
895
|
return /*#__PURE__*/React__default["default"].createElement(RigidBodyContext.Provider, {
|
872
896
|
value: {
|
873
897
|
ref: object,
|
@@ -888,21 +912,30 @@ const MeshCollider = ({
|
|
888
912
|
physicsOptions,
|
889
913
|
world
|
890
914
|
} = useRapier();
|
891
|
-
const object = React
|
915
|
+
const object = React.useRef(null);
|
892
916
|
const {
|
893
917
|
api,
|
894
918
|
options
|
895
919
|
} = useRigidBodyContext();
|
896
|
-
React
|
920
|
+
React.useEffect(() => {
|
897
921
|
let autoColliders = [];
|
898
922
|
|
899
923
|
if (object.current) {
|
900
924
|
var _ref;
|
901
925
|
|
902
926
|
const colliderSetting = (_ref = type !== null && type !== void 0 ? type : physicsOptions.colliders) !== null && _ref !== void 0 ? _ref : false;
|
903
|
-
|
904
|
-
|
905
|
-
|
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
|
+
}
|
906
939
|
}
|
907
940
|
|
908
941
|
return () => {
|
@@ -911,7 +944,7 @@ const MeshCollider = ({
|
|
911
944
|
});
|
912
945
|
};
|
913
946
|
}, []);
|
914
|
-
return /*#__PURE__*/
|
947
|
+
return /*#__PURE__*/React__default["default"].createElement("object3D", {
|
915
948
|
ref: object,
|
916
949
|
userData: {
|
917
950
|
r3RapierType: "MeshCollider"
|
@@ -974,13 +1007,13 @@ const geometryFromCollider = collider => {
|
|
974
1007
|
return new three.BoxBufferGeometry(1, 1, 1);
|
975
1008
|
};
|
976
1009
|
|
977
|
-
const DebugShape = /*#__PURE__*/React
|
1010
|
+
const DebugShape = /*#__PURE__*/React.memo(({
|
978
1011
|
colliderHandle
|
979
1012
|
}) => {
|
980
1013
|
const {
|
981
1014
|
world
|
982
1015
|
} = useRapier();
|
983
|
-
const ref = React
|
1016
|
+
const ref = React.useRef(null);
|
984
1017
|
fiber.useFrame(() => {
|
985
1018
|
const collider = world.getCollider(colliderHandle);
|
986
1019
|
|
@@ -1000,7 +1033,7 @@ const DebugShape = /*#__PURE__*/React$1.memo(({
|
|
1000
1033
|
ref.current.rotation.setFromQuaternion(new three.Quaternion(rx, ry, rz, rw));
|
1001
1034
|
}
|
1002
1035
|
});
|
1003
|
-
const geometry = React
|
1036
|
+
const geometry = React.useMemo(() => {
|
1004
1037
|
const collider = world.getCollider(colliderHandle);
|
1005
1038
|
return geometryFromCollider(collider);
|
1006
1039
|
}, [colliderHandle]);
|
@@ -1018,8 +1051,8 @@ const Debug = () => {
|
|
1018
1051
|
const {
|
1019
1052
|
world
|
1020
1053
|
} = useRapier();
|
1021
|
-
const [colliders, setColliders] = React
|
1022
|
-
React
|
1054
|
+
const [colliders, setColliders] = React.useState([]);
|
1055
|
+
React.useRef({});
|
1023
1056
|
|
1024
1057
|
fiber.useFrame(() => {
|
1025
1058
|
const newColliders = [];
|
@@ -1034,27 +1067,27 @@ const Debug = () => {
|
|
1034
1067
|
})));
|
1035
1068
|
};
|
1036
1069
|
|
1037
|
-
const InstancedRigidBodies = /*#__PURE__*/React
|
1070
|
+
const InstancedRigidBodies = /*#__PURE__*/React.forwardRef((props, ref) => {
|
1038
1071
|
const {
|
1039
1072
|
world,
|
1040
1073
|
rigidBodyStates,
|
1041
1074
|
physicsOptions
|
1042
1075
|
} = useRapier();
|
1043
|
-
const object = React
|
1044
|
-
const instancesRef = React
|
1045
|
-
const instancesRefGetter = React
|
1076
|
+
const object = React.useRef(null);
|
1077
|
+
const instancesRef = React.useRef();
|
1078
|
+
const instancesRefGetter = React.useRef(() => {
|
1046
1079
|
if (!instancesRef.current) {
|
1047
1080
|
instancesRef.current = [];
|
1048
1081
|
}
|
1049
1082
|
|
1050
1083
|
return instancesRef.current;
|
1051
1084
|
});
|
1052
|
-
React
|
1085
|
+
React.useLayoutEffect(() => {
|
1053
1086
|
const colliders = [];
|
1054
1087
|
const rigidBodies = instancesRefGetter.current();
|
1055
1088
|
|
1056
1089
|
if (object.current) {
|
1057
|
-
const
|
1090
|
+
const worldScale = object.current.getWorldScale(new three.Vector3());
|
1058
1091
|
let hasOneMesh = false;
|
1059
1092
|
object.current.traverse(mesh => {
|
1060
1093
|
if (mesh instanceof three.InstancedMesh) {
|
@@ -1065,21 +1098,34 @@ const InstancedRigidBodies = /*#__PURE__*/React$1.forwardRef((props, ref) => {
|
|
1065
1098
|
|
1066
1099
|
hasOneMesh = true;
|
1067
1100
|
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
1101
|
|
1072
1102
|
for (let index = 0; index < mesh.count; index++) {
|
1103
|
+
const scale = worldScale.clone();
|
1104
|
+
const rigidBodyDesc = rigidBodyDescFromOptions(props);
|
1105
|
+
|
1106
|
+
if (props.scales && props.scales[index]) {
|
1107
|
+
const s = vectorArrayToVector3(props.scales[index]);
|
1108
|
+
scale.multiply(s);
|
1109
|
+
}
|
1110
|
+
|
1073
1111
|
const rigidBody = world.createRigidBody(rigidBodyDesc);
|
1074
1112
|
const matrix = new three.Matrix4();
|
1075
1113
|
mesh.getMatrixAt(index, matrix);
|
1076
1114
|
const {
|
1077
1115
|
position,
|
1078
1116
|
rotation
|
1079
|
-
} = decomposeMatrix4(matrix);
|
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
|
+
|
1080
1126
|
|
1081
1127
|
if (props.positions && props.positions[index]) {
|
1082
|
-
rigidBody.setTranslation(
|
1128
|
+
rigidBody.setTranslation(vectorArrayToVector3(props.positions[index]), true);
|
1083
1129
|
} else {
|
1084
1130
|
rigidBody.setTranslation(position, true);
|
1085
1131
|
} // Set rotations
|
@@ -1092,13 +1138,19 @@ const InstancedRigidBodies = /*#__PURE__*/React$1.forwardRef((props, ref) => {
|
|
1092
1138
|
rigidBody.setRotation(rotation, true);
|
1093
1139
|
}
|
1094
1140
|
|
1095
|
-
const collider = world.createCollider(colliderDesc, rigidBody);
|
1096
1141
|
rigidBodyStates.set(rigidBody.handle, {
|
1097
1142
|
mesh: mesh,
|
1098
1143
|
isSleeping: false,
|
1099
1144
|
invertedMatrixWorld: object.current.matrixWorld.clone().invert(),
|
1100
1145
|
setMatrix: matrix => mesh.setMatrixAt(index, matrix),
|
1101
|
-
|
1146
|
+
getMatrix: () => {
|
1147
|
+
const m = new three.Matrix4();
|
1148
|
+
mesh.getMatrixAt(index, m);
|
1149
|
+
return m;
|
1150
|
+
},
|
1151
|
+
// Setting the world scale to the scale here, because
|
1152
|
+
// we want the scales to be reflected by instance
|
1153
|
+
worldScale: scale
|
1102
1154
|
});
|
1103
1155
|
const api = createRigidBodyApi({
|
1104
1156
|
current() {
|
@@ -1106,7 +1158,6 @@ const InstancedRigidBodies = /*#__PURE__*/React$1.forwardRef((props, ref) => {
|
|
1106
1158
|
}
|
1107
1159
|
|
1108
1160
|
});
|
1109
|
-
colliders.push(collider);
|
1110
1161
|
rigidBodies.push({
|
1111
1162
|
rigidBody,
|
1112
1163
|
api
|
@@ -1125,10 +1176,19 @@ const InstancedRigidBodies = /*#__PURE__*/React$1.forwardRef((props, ref) => {
|
|
1125
1176
|
};
|
1126
1177
|
}
|
1127
1178
|
}, []);
|
1128
|
-
React
|
1129
|
-
|
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", {
|
1130
1190
|
ref: object
|
1131
|
-
}, props.children);
|
1191
|
+
}, props.children));
|
1132
1192
|
});
|
1133
1193
|
|
1134
1194
|
function _extends() {
|
@@ -1161,69 +1221,91 @@ const AnyCollider = _ref => {
|
|
1161
1221
|
world
|
1162
1222
|
} = useRapier();
|
1163
1223
|
const rigidBodyContext = useRigidBodyContext();
|
1164
|
-
const ref = React
|
1165
|
-
React
|
1166
|
-
var _rigidBodyContext$api;
|
1167
|
-
|
1224
|
+
const ref = React.useRef(null);
|
1225
|
+
React.useEffect(() => {
|
1168
1226
|
const scale = ref.current.getWorldScale(new three.Vector3());
|
1169
|
-
const
|
1170
|
-
|
1171
|
-
|
1172
|
-
|
1173
|
-
|
1174
|
-
|
1175
|
-
|
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
|
+
|
1176
1258
|
return () => {
|
1177
|
-
world.removeCollider(collider);
|
1259
|
+
colliders.forEach(collider => world.removeCollider(collider));
|
1178
1260
|
};
|
1179
1261
|
}, []);
|
1180
|
-
return /*#__PURE__*/
|
1262
|
+
return /*#__PURE__*/React__default["default"].createElement("object3D", {
|
1181
1263
|
ref: ref
|
1182
1264
|
}, children);
|
1183
1265
|
};
|
1184
1266
|
|
1185
1267
|
const CuboidCollider = props => {
|
1186
|
-
return /*#__PURE__*/
|
1268
|
+
return /*#__PURE__*/React__default["default"].createElement(AnyCollider, _extends({}, props, {
|
1187
1269
|
shape: "cuboid"
|
1188
1270
|
}));
|
1189
1271
|
};
|
1190
1272
|
const RoundCuboidCollider = props => {
|
1191
|
-
return /*#__PURE__*/
|
1273
|
+
return /*#__PURE__*/React__default["default"].createElement(AnyCollider, _extends({}, props, {
|
1192
1274
|
shape: "roundCuboid"
|
1193
1275
|
}));
|
1194
1276
|
};
|
1195
1277
|
const BallCollider = props => {
|
1196
|
-
return /*#__PURE__*/
|
1278
|
+
return /*#__PURE__*/React__default["default"].createElement(AnyCollider, _extends({}, props, {
|
1197
1279
|
shape: "ball"
|
1198
1280
|
}));
|
1199
1281
|
};
|
1200
1282
|
const CapsuleCollider = props => {
|
1201
|
-
return /*#__PURE__*/
|
1283
|
+
return /*#__PURE__*/React__default["default"].createElement(AnyCollider, _extends({}, props, {
|
1202
1284
|
shape: "capsule"
|
1203
1285
|
}));
|
1204
1286
|
};
|
1205
1287
|
const HeightfieldCollider = props => {
|
1206
|
-
return /*#__PURE__*/
|
1288
|
+
return /*#__PURE__*/React__default["default"].createElement(AnyCollider, _extends({}, props, {
|
1207
1289
|
shape: "heightfield"
|
1208
1290
|
}));
|
1209
1291
|
};
|
1210
1292
|
const TrimeshCollider = props => {
|
1211
|
-
return /*#__PURE__*/
|
1293
|
+
return /*#__PURE__*/React__default["default"].createElement(AnyCollider, _extends({}, props, {
|
1212
1294
|
shape: "trimesh"
|
1213
1295
|
}));
|
1214
1296
|
};
|
1215
1297
|
const ConeCollider = props => {
|
1216
|
-
return /*#__PURE__*/
|
1298
|
+
return /*#__PURE__*/React__default["default"].createElement(AnyCollider, _extends({}, props, {
|
1217
1299
|
shape: "cone"
|
1218
1300
|
}));
|
1219
1301
|
};
|
1220
1302
|
const CylinderCollider = props => {
|
1221
|
-
return /*#__PURE__*/
|
1303
|
+
return /*#__PURE__*/React__default["default"].createElement(AnyCollider, _extends({}, props, {
|
1222
1304
|
shape: "cylinder"
|
1223
1305
|
}));
|
1224
1306
|
};
|
1225
1307
|
const ConvexHullCollider = props => {
|
1226
|
-
return /*#__PURE__*/
|
1308
|
+
return /*#__PURE__*/React__default["default"].createElement(AnyCollider, _extends({}, props, {
|
1227
1309
|
shape: "convexHull"
|
1228
1310
|
}));
|
1229
1311
|
};
|