@react-three/rapier 0.12.0 → 0.12.2
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.
@@ -1,4 +1,4 @@
|
|
1
|
-
|
1
|
+
import React from "react";
|
2
2
|
import { InstancedRigidBodyApi } from "./api";
|
3
3
|
import { RigidBodyProps } from "./RigidBody";
|
4
4
|
import { Vector3Array } from "./types";
|
@@ -7,4 +7,4 @@ export interface InstancedRigidBodiesProps extends Omit<RigidBodyProps, "positio
|
|
7
7
|
rotations?: Vector3Array[];
|
8
8
|
scales?: Vector3Array[];
|
9
9
|
}
|
10
|
-
export declare const InstancedRigidBodies:
|
10
|
+
export declare const InstancedRigidBodies: React.ForwardRefExoticComponent<InstancedRigidBodiesProps & React.RefAttributes<InstancedRigidBodyApi>>;
|
@@ -4,7 +4,7 @@ Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
5
5
|
var rapier3dCompat = require('@dimforge/rapier3d-compat');
|
6
6
|
var fiber = require('@react-three/fiber');
|
7
|
-
var React
|
7
|
+
var React = require('react');
|
8
8
|
var three = require('three');
|
9
9
|
var useAsset = require('use-asset');
|
10
10
|
var threeStdlib = require('three-stdlib');
|
@@ -29,7 +29,7 @@ function _interopNamespace(e) {
|
|
29
29
|
return Object.freeze(n);
|
30
30
|
}
|
31
31
|
|
32
|
-
var React__default = /*#__PURE__*/_interopDefault(React
|
32
|
+
var React__default = /*#__PURE__*/_interopDefault(React);
|
33
33
|
|
34
34
|
function _defineProperty(obj, key, value) {
|
35
35
|
if (key in obj) {
|
@@ -138,7 +138,7 @@ const vectorToTuple = v => {
|
|
138
138
|
return [v];
|
139
139
|
};
|
140
140
|
function useConst(initialValue) {
|
141
|
-
const ref = React
|
141
|
+
const ref = React.useRef();
|
142
142
|
|
143
143
|
if (ref.current === undefined) {
|
144
144
|
ref.current = {
|
@@ -426,10 +426,10 @@ const setColliderOptions = (collider, options, states) => {
|
|
426
426
|
};
|
427
427
|
const useUpdateColliderOptions = (collidersRef, props, states) => {
|
428
428
|
// TODO: Improve this, split each prop into its own effect
|
429
|
-
const mutablePropsAsFlatArray = React
|
429
|
+
const mutablePropsAsFlatArray = React.useMemo(() => mutableColliderOptionKeys.flatMap(key => {
|
430
430
|
return vectorToTuple(props[key]);
|
431
431
|
}), [props]);
|
432
|
-
React
|
432
|
+
React.useEffect(() => {
|
433
433
|
collidersRef.current.forEach(collider => {
|
434
434
|
setColliderOptions(collider, props, states);
|
435
435
|
});
|
@@ -563,7 +563,7 @@ const useColliderEvents = (collidersRef, props, events) => {
|
|
563
563
|
onIntersectionExit,
|
564
564
|
onContactForce
|
565
565
|
} = props;
|
566
|
-
React
|
566
|
+
React.useEffect(() => {
|
567
567
|
var _collidersRef$current;
|
568
568
|
|
569
569
|
(_collidersRef$current = collidersRef.current) === null || _collidersRef$current === void 0 ? void 0 : _collidersRef$current.forEach(collider => {
|
@@ -703,10 +703,10 @@ const setRigidBodyOptions = (rigidBody, options, states, updateTranslations = tr
|
|
703
703
|
};
|
704
704
|
const useUpdateRigidBodyOptions = (rigidBodyRef, props, states, updateTranslations = true) => {
|
705
705
|
// TODO: Improve this, split each prop into its own effect
|
706
|
-
const mutablePropsAsFlatArray = React
|
706
|
+
const mutablePropsAsFlatArray = React.useMemo(() => mutableRigidBodyOptionKeys.flatMap(key => {
|
707
707
|
return vectorToTuple(props[key]);
|
708
708
|
}), [props]);
|
709
|
-
React
|
709
|
+
React.useEffect(() => {
|
710
710
|
if (Array.isArray(rigidBodyRef.current)) {
|
711
711
|
for (const rigidBody of rigidBodyRef.current) {
|
712
712
|
setRigidBodyOptions(rigidBody, props, states, updateTranslations);
|
@@ -733,7 +733,7 @@ const useRigidBodyEvents = (rigidBodyRef, props, events) => {
|
|
733
733
|
onIntersectionEnter,
|
734
734
|
onIntersectionExit
|
735
735
|
};
|
736
|
-
React
|
736
|
+
React.useEffect(() => {
|
737
737
|
if (Array.isArray(rigidBodyRef.current)) {
|
738
738
|
for (const rigidBody of rigidBodyRef.current) {
|
739
739
|
events.set(rigidBody.handle, eventHandlers);
|
@@ -755,13 +755,13 @@ const useRigidBodyEvents = (rigidBodyRef, props, events) => {
|
|
755
755
|
};
|
756
756
|
|
757
757
|
const useRapier = () => {
|
758
|
-
return React
|
758
|
+
return React.useContext(rapierContext);
|
759
759
|
};
|
760
760
|
const useBeforePhysicsStep = callback => {
|
761
761
|
const {
|
762
762
|
beforeStepCallbacks
|
763
763
|
} = useRapier();
|
764
|
-
React
|
764
|
+
React.useEffect(() => {
|
765
765
|
beforeStepCallbacks.add(callback);
|
766
766
|
return () => {
|
767
767
|
beforeStepCallbacks.delete(callback);
|
@@ -772,7 +772,7 @@ const useAfterPhysicsStep = callback => {
|
|
772
772
|
const {
|
773
773
|
afterStepCallbacks
|
774
774
|
} = useRapier();
|
775
|
-
React
|
775
|
+
React.useEffect(() => {
|
776
776
|
afterStepCallbacks.add(callback);
|
777
777
|
return () => {
|
778
778
|
afterStepCallbacks.delete(callback);
|
@@ -781,8 +781,8 @@ const useAfterPhysicsStep = callback => {
|
|
781
781
|
}; // Internal hooks
|
782
782
|
|
783
783
|
const useChildColliderProps = (ref, options, ignoreMeshColliders = true) => {
|
784
|
-
const [colliderProps, setColliderProps] = React
|
785
|
-
React
|
784
|
+
const [colliderProps, setColliderProps] = React.useState([]);
|
785
|
+
React.useEffect(() => {
|
786
786
|
const object = ref.current;
|
787
787
|
|
788
788
|
if (object && options.colliders !== false) {
|
@@ -802,16 +802,16 @@ const useRigidBody = (options = {}) => {
|
|
802
802
|
physicsOptions,
|
803
803
|
rigidBodyEvents
|
804
804
|
} = useRapier();
|
805
|
-
const ref = React
|
806
|
-
const mergedOptions = React
|
805
|
+
const ref = React.useRef();
|
806
|
+
const mergedOptions = React.useMemo(() => {
|
807
807
|
return _objectSpread2(_objectSpread2(_objectSpread2({}, physicsOptions), options), {}, {
|
808
808
|
children: undefined
|
809
809
|
});
|
810
810
|
}, [physicsOptions, options]);
|
811
811
|
const childColliderProps = useChildColliderProps(ref, mergedOptions); // Create rigidbody
|
812
812
|
|
813
|
-
const rigidBodyRef = React
|
814
|
-
const getRigidBodyRef = React
|
813
|
+
const rigidBodyRef = React.useRef();
|
814
|
+
const getRigidBodyRef = React.useRef(() => {
|
815
815
|
if (!rigidBodyRef.current) {
|
816
816
|
const desc = rigidBodyDescFromOptions(options);
|
817
817
|
const rigidBody = world.createRigidBody(desc);
|
@@ -821,7 +821,7 @@ const useRigidBody = (options = {}) => {
|
|
821
821
|
return rigidBodyRef.current;
|
822
822
|
}); // Setup
|
823
823
|
|
824
|
-
React
|
824
|
+
React.useEffect(() => {
|
825
825
|
const rigidBody = getRigidBodyRef.current();
|
826
826
|
rigidBodyRef.current = rigidBody;
|
827
827
|
|
@@ -841,7 +841,7 @@ const useRigidBody = (options = {}) => {
|
|
841
841
|
}, []);
|
842
842
|
useUpdateRigidBodyOptions(rigidBodyRef, mergedOptions, rigidBodyStates);
|
843
843
|
useRigidBodyEvents(rigidBodyRef, mergedOptions, rigidBodyEvents);
|
844
|
-
const api = React
|
844
|
+
const api = React.useMemo(() => createRigidBodyApi(getRigidBodyRef), []);
|
845
845
|
return [ref, api, childColliderProps];
|
846
846
|
};
|
847
847
|
|
@@ -891,7 +891,7 @@ const applyAttractorForceOnRigidBody = (rigidBody, {
|
|
891
891
|
}
|
892
892
|
}
|
893
893
|
};
|
894
|
-
const Attractor = /*#__PURE__*/React
|
894
|
+
const Attractor = /*#__PURE__*/React.memo(props => {
|
895
895
|
const {
|
896
896
|
position = [0, 0, 0],
|
897
897
|
strength = 1,
|
@@ -903,8 +903,8 @@ const Attractor = /*#__PURE__*/React$1.memo(props => {
|
|
903
903
|
const {
|
904
904
|
attractorStates
|
905
905
|
} = useRapier();
|
906
|
-
const object = React
|
907
|
-
React
|
906
|
+
const object = React.useRef(null);
|
907
|
+
React.useEffect(() => {
|
908
908
|
var _object$current;
|
909
909
|
|
910
910
|
let uuid = ((_object$current = object.current) === null || _object$current === void 0 ? void 0 : _object$current.uuid) || "_";
|
@@ -930,7 +930,7 @@ const Attractor = /*#__PURE__*/React$1.memo(props => {
|
|
930
930
|
});
|
931
931
|
});
|
932
932
|
|
933
|
-
const rapierContext = /*#__PURE__*/React
|
933
|
+
const rapierContext = /*#__PURE__*/React.createContext(undefined);
|
934
934
|
|
935
935
|
const getCollisionPayloadFromSource = (target, other) => {
|
936
936
|
var _target$collider$stat, _target$rigidBody$sta, _other$collider$state, _other$rigidBody$stat, _other$collider$state2, _other$rigidBody$stat2;
|
@@ -971,8 +971,8 @@ const Physics = ({
|
|
971
971
|
interpolate: _interpolate = true
|
972
972
|
}) => {
|
973
973
|
const rapier = useAsset.useAsset(importRapier);
|
974
|
-
const worldRef = React
|
975
|
-
const getWorldRef = React
|
974
|
+
const worldRef = React.useRef();
|
975
|
+
const getWorldRef = React.useRef(() => {
|
976
976
|
if (!worldRef.current) {
|
977
977
|
const world = new rapier.World(vectorArrayToVector3(_gravity));
|
978
978
|
worldRef.current = world;
|
@@ -989,7 +989,7 @@ const Physics = ({
|
|
989
989
|
const beforeStepCallbacks = useConst(() => new Set());
|
990
990
|
const afterStepCallbacks = useConst(() => new Set()); // Init world
|
991
991
|
|
992
|
-
React
|
992
|
+
React.useEffect(() => {
|
993
993
|
const world = getWorldRef.current();
|
994
994
|
return () => {
|
995
995
|
if (world) {
|
@@ -999,15 +999,15 @@ const Physics = ({
|
|
999
999
|
};
|
1000
1000
|
}, []); // Update gravity
|
1001
1001
|
|
1002
|
-
React
|
1002
|
+
React.useEffect(() => {
|
1003
1003
|
const world = worldRef.current;
|
1004
1004
|
|
1005
1005
|
if (world) {
|
1006
1006
|
world.gravity = vectorArrayToVector3(_gravity);
|
1007
1007
|
}
|
1008
1008
|
}, [_gravity]);
|
1009
|
-
const api = React
|
1010
|
-
const getSourceFromColliderHandle = React
|
1009
|
+
const api = React.useMemo(() => createWorldApi(getWorldRef), []);
|
1010
|
+
const getSourceFromColliderHandle = React.useCallback(handle => {
|
1011
1011
|
const world = worldRef.current;
|
1012
1012
|
|
1013
1013
|
if (world) {
|
@@ -1035,11 +1035,11 @@ const Physics = ({
|
|
1035
1035
|
return source;
|
1036
1036
|
}
|
1037
1037
|
}, []);
|
1038
|
-
const [steppingState] = React
|
1038
|
+
const [steppingState] = React.useState({
|
1039
1039
|
previousState: {},
|
1040
1040
|
accumulator: 0
|
1041
1041
|
});
|
1042
|
-
const step = React
|
1042
|
+
const step = React.useCallback(dt => {
|
1043
1043
|
const world = worldRef.current;
|
1044
1044
|
if (!world) return;
|
1045
1045
|
/* Check if the timestep is supposed to be variable. We'll do this here
|
@@ -1054,7 +1054,13 @@ const Physics = ({
|
|
1054
1054
|
const clampedDelta = three.MathUtils.clamp(dt, 0, 0.2);
|
1055
1055
|
|
1056
1056
|
const stepWorld = () => {
|
1057
|
-
//
|
1057
|
+
// Apply attractors
|
1058
|
+
world.forEachRigidBody(body => {
|
1059
|
+
attractorStates.forEach(attractorState => {
|
1060
|
+
applyAttractorForceOnRigidBody(body, attractorState);
|
1061
|
+
});
|
1062
|
+
}); // Trigger beforeStep callbacks
|
1063
|
+
|
1058
1064
|
beforeStepCallbacks.forEach(callback => {
|
1059
1065
|
callback(api);
|
1060
1066
|
});
|
@@ -1075,22 +1081,18 @@ const Physics = ({
|
|
1075
1081
|
steppingState.accumulator += clampedDelta;
|
1076
1082
|
|
1077
1083
|
while (steppingState.accumulator >= _timeStep) {
|
1078
|
-
|
1079
|
-
|
1080
|
-
|
1081
|
-
|
1082
|
-
|
1084
|
+
// Set up previous state
|
1085
|
+
// needed for accurate interpolations if the world steps more than once
|
1086
|
+
if (_interpolate) {
|
1087
|
+
steppingState.previousState = {};
|
1088
|
+
world.forEachRigidBody(body => {
|
1083
1089
|
steppingState.previousState[body.handle] = {
|
1084
1090
|
position: body.translation(),
|
1085
1091
|
rotation: body.rotation()
|
1086
1092
|
};
|
1087
|
-
} // Apply attractors
|
1088
|
-
|
1089
|
-
|
1090
|
-
attractorStates.forEach(attractorState => {
|
1091
|
-
applyAttractorForceOnRigidBody(body, attractorState);
|
1092
1093
|
});
|
1093
|
-
}
|
1094
|
+
}
|
1095
|
+
|
1094
1096
|
stepWorld();
|
1095
1097
|
steppingState.accumulator -= _timeStep;
|
1096
1098
|
}
|
@@ -1254,7 +1256,7 @@ const Physics = ({
|
|
1254
1256
|
fiber.useFrame((_, dt) => {
|
1255
1257
|
if (!_paused) step(dt);
|
1256
1258
|
}, updatePriority);
|
1257
|
-
const context = React
|
1259
|
+
const context = React.useMemo(() => ({
|
1258
1260
|
rapier,
|
1259
1261
|
world: api,
|
1260
1262
|
physicsOptions: {
|
@@ -1328,7 +1330,7 @@ function _objectWithoutProperties(source, excluded) {
|
|
1328
1330
|
}
|
1329
1331
|
|
1330
1332
|
// Colliders
|
1331
|
-
const AnyCollider = /*#__PURE__*/React
|
1333
|
+
const AnyCollider = /*#__PURE__*/React.memo( /*#__PURE__*/React__default["default"].forwardRef((props, forwardedRef) => {
|
1332
1334
|
const {
|
1333
1335
|
children,
|
1334
1336
|
position,
|
@@ -1343,8 +1345,8 @@ const AnyCollider = /*#__PURE__*/React$1.memo( /*#__PURE__*/React__default["defa
|
|
1343
1345
|
colliderStates
|
1344
1346
|
} = useRapier();
|
1345
1347
|
const rigidBodyContext = useRigidBodyContext();
|
1346
|
-
const ref = React
|
1347
|
-
const collidersRef = React
|
1348
|
+
const ref = React.useRef(null);
|
1349
|
+
const collidersRef = React.useMemo(() => {
|
1348
1350
|
if (forwardedRef !== null) {
|
1349
1351
|
return forwardedRef;
|
1350
1352
|
}
|
@@ -1353,7 +1355,7 @@ const AnyCollider = /*#__PURE__*/React$1.memo( /*#__PURE__*/React__default["defa
|
|
1353
1355
|
result.current = [];
|
1354
1356
|
return result;
|
1355
1357
|
}, []);
|
1356
|
-
React
|
1358
|
+
React.useEffect(() => {
|
1357
1359
|
const object = ref.current;
|
1358
1360
|
const worldScale = object.getWorldScale(new three.Vector3());
|
1359
1361
|
const colliders = []; // If this is an InstancedRigidBody api
|
@@ -1385,7 +1387,7 @@ const AnyCollider = /*#__PURE__*/React$1.memo( /*#__PURE__*/React__default["defa
|
|
1385
1387
|
});
|
1386
1388
|
};
|
1387
1389
|
}, []);
|
1388
|
-
const mergedProps = React
|
1390
|
+
const mergedProps = React.useMemo(() => {
|
1389
1391
|
return _objectSpread2(_objectSpread2({}, rigidBodyContext === null || rigidBodyContext === void 0 ? void 0 : rigidBodyContext.options), props);
|
1390
1392
|
}, [props, rigidBodyContext === null || rigidBodyContext === void 0 ? void 0 : rigidBodyContext.options]);
|
1391
1393
|
useUpdateColliderOptions(collidersRef, mergedProps, colliderStates);
|
@@ -1464,9 +1466,9 @@ CylinderCollider.displayName = "CylinderCollider";
|
|
1464
1466
|
ConvexHullCollider.displayName = "ConvexHullCollider";
|
1465
1467
|
|
1466
1468
|
const _excluded$1 = ["children", "type", "position", "rotation", "scale", "quaternion"];
|
1467
|
-
const RigidBodyContext = /*#__PURE__*/React
|
1468
|
-
const useRigidBodyContext = () => React
|
1469
|
-
const RigidBody = /*#__PURE__*/React
|
1469
|
+
const RigidBodyContext = /*#__PURE__*/React.createContext(undefined);
|
1470
|
+
const useRigidBodyContext = () => React.useContext(RigidBodyContext);
|
1471
|
+
const RigidBody = /*#__PURE__*/React.memo( /*#__PURE__*/React.forwardRef((props, ref) => {
|
1470
1472
|
const {
|
1471
1473
|
children,
|
1472
1474
|
type,
|
@@ -1478,8 +1480,8 @@ const RigidBody = /*#__PURE__*/React$1.memo( /*#__PURE__*/React$1.forwardRef((pr
|
|
1478
1480
|
objectProps = _objectWithoutProperties(props, _excluded$1);
|
1479
1481
|
|
1480
1482
|
const [object, api, childColliderProps] = useRigidBody(props);
|
1481
|
-
React
|
1482
|
-
const contextValue = React
|
1483
|
+
React.useImperativeHandle(ref, () => api);
|
1484
|
+
const contextValue = React.useMemo(() => ({
|
1483
1485
|
ref: object,
|
1484
1486
|
api,
|
1485
1487
|
options: props
|
@@ -1499,7 +1501,7 @@ const RigidBody = /*#__PURE__*/React$1.memo( /*#__PURE__*/React$1.forwardRef((pr
|
|
1499
1501
|
}));
|
1500
1502
|
RigidBody.displayName = "RigidBody";
|
1501
1503
|
|
1502
|
-
const MeshCollider = /*#__PURE__*/React
|
1504
|
+
const MeshCollider = /*#__PURE__*/React.memo(props => {
|
1503
1505
|
const {
|
1504
1506
|
children,
|
1505
1507
|
type
|
@@ -1508,11 +1510,11 @@ const MeshCollider = /*#__PURE__*/React$1.memo(props => {
|
|
1508
1510
|
physicsOptions,
|
1509
1511
|
world
|
1510
1512
|
} = useRapier();
|
1511
|
-
const object = React
|
1513
|
+
const object = React.useRef(null);
|
1512
1514
|
const {
|
1513
1515
|
options
|
1514
1516
|
} = useRigidBodyContext();
|
1515
|
-
const mergedOptions = React
|
1517
|
+
const mergedOptions = React.useMemo(() => {
|
1516
1518
|
return _objectSpread2(_objectSpread2(_objectSpread2({}, physicsOptions), options), {}, {
|
1517
1519
|
children: undefined,
|
1518
1520
|
colliders: type
|
@@ -1552,10 +1554,10 @@ const AttractorHelper = props => {
|
|
1552
1554
|
const {
|
1553
1555
|
scene
|
1554
1556
|
} = fiber.useThree();
|
1555
|
-
const ref = React
|
1556
|
-
const normalsHelper = React
|
1557
|
+
const ref = React.useRef(null);
|
1558
|
+
const normalsHelper = React.useRef();
|
1557
1559
|
const color = props.strength > 0 ? 0x0000ff : 0xff0000;
|
1558
|
-
React
|
1560
|
+
React.useEffect(() => {
|
1559
1561
|
if (ref.current) {
|
1560
1562
|
normalsHelper.current = new threeStdlib.VertexNormalsHelper(ref.current, props.range, color);
|
1561
1563
|
normalsHelper.current.frustumCulled = false;
|
@@ -1589,14 +1591,14 @@ const AttractorHelper = props => {
|
|
1589
1591
|
}));
|
1590
1592
|
};
|
1591
1593
|
|
1592
|
-
const Debug = /*#__PURE__*/React
|
1594
|
+
const Debug = /*#__PURE__*/React.memo(() => {
|
1593
1595
|
const {
|
1594
1596
|
world,
|
1595
1597
|
attractorStates
|
1596
1598
|
} = useRapier();
|
1597
|
-
const ref = React
|
1598
|
-
const [attractors, setAttractors] = React
|
1599
|
-
const currMap = React
|
1599
|
+
const ref = React.useRef(null);
|
1600
|
+
const [attractors, setAttractors] = React.useState([]);
|
1601
|
+
const currMap = React.useRef(new Map());
|
1600
1602
|
fiber.useFrame(() => {
|
1601
1603
|
const mesh = ref.current;
|
1602
1604
|
if (!mesh) return;
|
@@ -1621,14 +1623,14 @@ const Debug = /*#__PURE__*/React$1.memo(() => {
|
|
1621
1623
|
});
|
1622
1624
|
|
1623
1625
|
const _excluded = ["positions", "rotations", "children"];
|
1624
|
-
const InstancedRigidBodies = /*#__PURE__*/React
|
1626
|
+
const InstancedRigidBodies = /*#__PURE__*/React.forwardRef((props, ref) => {
|
1625
1627
|
const {
|
1626
1628
|
world,
|
1627
1629
|
rigidBodyStates,
|
1628
1630
|
physicsOptions,
|
1629
1631
|
rigidBodyEvents
|
1630
1632
|
} = useRapier();
|
1631
|
-
const object = React
|
1633
|
+
const object = React.useRef(null);
|
1632
1634
|
|
1633
1635
|
const {
|
1634
1636
|
positions,
|
@@ -1637,20 +1639,20 @@ const InstancedRigidBodies = /*#__PURE__*/React$1.forwardRef((props, ref) => {
|
|
1637
1639
|
} = props,
|
1638
1640
|
options = _objectWithoutProperties(props, _excluded);
|
1639
1641
|
|
1640
|
-
const instancesRef = React
|
1641
|
-
const rigidBodyRefs = React
|
1642
|
-
const instancesRefGetter = React
|
1642
|
+
const instancesRef = React.useRef([]);
|
1643
|
+
const rigidBodyRefs = React.useRef([]);
|
1644
|
+
const instancesRefGetter = React.useRef(() => {
|
1643
1645
|
if (!instancesRef.current) {
|
1644
1646
|
instancesRef.current = [];
|
1645
1647
|
}
|
1646
1648
|
|
1647
1649
|
return instancesRef.current;
|
1648
1650
|
});
|
1649
|
-
const mergedOptions = React
|
1651
|
+
const mergedOptions = React.useMemo(() => {
|
1650
1652
|
return _objectSpread2(_objectSpread2({}, physicsOptions), options);
|
1651
1653
|
}, [physicsOptions, options]);
|
1652
1654
|
const childColliderProps = useChildColliderProps(object, mergedOptions);
|
1653
|
-
React
|
1655
|
+
React.useLayoutEffect(() => {
|
1654
1656
|
object.current.updateWorldMatrix(true, false);
|
1655
1657
|
const instances = instancesRefGetter.current();
|
1656
1658
|
const invertedWorld = object.current.matrixWorld.clone().invert();
|
@@ -1711,22 +1713,22 @@ const InstancedRigidBodies = /*#__PURE__*/React$1.forwardRef((props, ref) => {
|
|
1711
1713
|
instancesRef.current = [];
|
1712
1714
|
};
|
1713
1715
|
}, []);
|
1714
|
-
const api = React
|
1715
|
-
React
|
1716
|
+
const api = React.useMemo(() => createInstancedRigidBodiesApi(instancesRefGetter), []);
|
1717
|
+
React.useImperativeHandle(ref, () => api);
|
1716
1718
|
useUpdateRigidBodyOptions(rigidBodyRefs, mergedOptions, rigidBodyStates, false);
|
1717
1719
|
useRigidBodyEvents(rigidBodyRefs, mergedOptions, rigidBodyEvents);
|
1718
|
-
const contextValue = React
|
1720
|
+
const contextValue = React.useMemo(() => {
|
1719
1721
|
return {
|
1720
1722
|
ref: object,
|
1721
1723
|
api,
|
1722
1724
|
options: mergedOptions
|
1723
1725
|
};
|
1724
1726
|
}, [api, mergedOptions]);
|
1725
|
-
return /*#__PURE__*/
|
1727
|
+
return /*#__PURE__*/React__default["default"].createElement(RigidBodyContext.Provider, {
|
1726
1728
|
value: contextValue
|
1727
|
-
}, /*#__PURE__*/
|
1729
|
+
}, /*#__PURE__*/React__default["default"].createElement("object3D", {
|
1728
1730
|
ref: object
|
1729
|
-
}, props.children, childColliderProps.map((colliderProps, index) => /*#__PURE__*/
|
1731
|
+
}, props.children, childColliderProps.map((colliderProps, index) => /*#__PURE__*/React__default["default"].createElement(AnyCollider, _extends({
|
1730
1732
|
key: index
|
1731
1733
|
}, colliderProps)))));
|
1732
1734
|
});
|
@@ -1736,8 +1738,8 @@ const useImpulseJoint = (body1, body2, params) => {
|
|
1736
1738
|
const {
|
1737
1739
|
world
|
1738
1740
|
} = useRapier();
|
1739
|
-
const jointRef = React
|
1740
|
-
const getJointRef = React
|
1741
|
+
const jointRef = React.useRef();
|
1742
|
+
const getJointRef = React.useRef(() => {
|
1741
1743
|
if (!jointRef.current) {
|
1742
1744
|
let rb1;
|
1743
1745
|
let rb2;
|
@@ -1752,7 +1754,7 @@ const useImpulseJoint = (body1, body2, params) => {
|
|
1752
1754
|
|
1753
1755
|
return jointRef.current;
|
1754
1756
|
});
|
1755
|
-
React
|
1757
|
+
React.useEffect(() => {
|
1756
1758
|
const joint = getJointRef.current();
|
1757
1759
|
return () => {
|
1758
1760
|
if (joint) {
|
@@ -1761,7 +1763,7 @@ const useImpulseJoint = (body1, body2, params) => {
|
|
1761
1763
|
}
|
1762
1764
|
};
|
1763
1765
|
}, []);
|
1764
|
-
const api = React
|
1766
|
+
const api = React.useMemo(() => createJointApi(getJointRef), []);
|
1765
1767
|
return api;
|
1766
1768
|
};
|
1767
1769
|
/**
|
@@ -4,7 +4,7 @@ Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
5
5
|
var rapier3dCompat = require('@dimforge/rapier3d-compat');
|
6
6
|
var fiber = require('@react-three/fiber');
|
7
|
-
var React
|
7
|
+
var React = require('react');
|
8
8
|
var three = require('three');
|
9
9
|
var useAsset = require('use-asset');
|
10
10
|
var threeStdlib = require('three-stdlib');
|
@@ -29,7 +29,7 @@ function _interopNamespace(e) {
|
|
29
29
|
return Object.freeze(n);
|
30
30
|
}
|
31
31
|
|
32
|
-
var React__default = /*#__PURE__*/_interopDefault(React
|
32
|
+
var React__default = /*#__PURE__*/_interopDefault(React);
|
33
33
|
|
34
34
|
function _defineProperty(obj, key, value) {
|
35
35
|
if (key in obj) {
|
@@ -138,7 +138,7 @@ const vectorToTuple = v => {
|
|
138
138
|
return [v];
|
139
139
|
};
|
140
140
|
function useConst(initialValue) {
|
141
|
-
const ref = React
|
141
|
+
const ref = React.useRef();
|
142
142
|
|
143
143
|
if (ref.current === undefined) {
|
144
144
|
ref.current = {
|
@@ -426,10 +426,10 @@ const setColliderOptions = (collider, options, states) => {
|
|
426
426
|
};
|
427
427
|
const useUpdateColliderOptions = (collidersRef, props, states) => {
|
428
428
|
// TODO: Improve this, split each prop into its own effect
|
429
|
-
const mutablePropsAsFlatArray = React
|
429
|
+
const mutablePropsAsFlatArray = React.useMemo(() => mutableColliderOptionKeys.flatMap(key => {
|
430
430
|
return vectorToTuple(props[key]);
|
431
431
|
}), [props]);
|
432
|
-
React
|
432
|
+
React.useEffect(() => {
|
433
433
|
collidersRef.current.forEach(collider => {
|
434
434
|
setColliderOptions(collider, props, states);
|
435
435
|
});
|
@@ -563,7 +563,7 @@ const useColliderEvents = (collidersRef, props, events) => {
|
|
563
563
|
onIntersectionExit,
|
564
564
|
onContactForce
|
565
565
|
} = props;
|
566
|
-
React
|
566
|
+
React.useEffect(() => {
|
567
567
|
var _collidersRef$current;
|
568
568
|
|
569
569
|
(_collidersRef$current = collidersRef.current) === null || _collidersRef$current === void 0 ? void 0 : _collidersRef$current.forEach(collider => {
|
@@ -703,10 +703,10 @@ const setRigidBodyOptions = (rigidBody, options, states, updateTranslations = tr
|
|
703
703
|
};
|
704
704
|
const useUpdateRigidBodyOptions = (rigidBodyRef, props, states, updateTranslations = true) => {
|
705
705
|
// TODO: Improve this, split each prop into its own effect
|
706
|
-
const mutablePropsAsFlatArray = React
|
706
|
+
const mutablePropsAsFlatArray = React.useMemo(() => mutableRigidBodyOptionKeys.flatMap(key => {
|
707
707
|
return vectorToTuple(props[key]);
|
708
708
|
}), [props]);
|
709
|
-
React
|
709
|
+
React.useEffect(() => {
|
710
710
|
if (Array.isArray(rigidBodyRef.current)) {
|
711
711
|
for (const rigidBody of rigidBodyRef.current) {
|
712
712
|
setRigidBodyOptions(rigidBody, props, states, updateTranslations);
|
@@ -733,7 +733,7 @@ const useRigidBodyEvents = (rigidBodyRef, props, events) => {
|
|
733
733
|
onIntersectionEnter,
|
734
734
|
onIntersectionExit
|
735
735
|
};
|
736
|
-
React
|
736
|
+
React.useEffect(() => {
|
737
737
|
if (Array.isArray(rigidBodyRef.current)) {
|
738
738
|
for (const rigidBody of rigidBodyRef.current) {
|
739
739
|
events.set(rigidBody.handle, eventHandlers);
|
@@ -755,13 +755,13 @@ const useRigidBodyEvents = (rigidBodyRef, props, events) => {
|
|
755
755
|
};
|
756
756
|
|
757
757
|
const useRapier = () => {
|
758
|
-
return React
|
758
|
+
return React.useContext(rapierContext);
|
759
759
|
};
|
760
760
|
const useBeforePhysicsStep = callback => {
|
761
761
|
const {
|
762
762
|
beforeStepCallbacks
|
763
763
|
} = useRapier();
|
764
|
-
React
|
764
|
+
React.useEffect(() => {
|
765
765
|
beforeStepCallbacks.add(callback);
|
766
766
|
return () => {
|
767
767
|
beforeStepCallbacks.delete(callback);
|
@@ -772,7 +772,7 @@ const useAfterPhysicsStep = callback => {
|
|
772
772
|
const {
|
773
773
|
afterStepCallbacks
|
774
774
|
} = useRapier();
|
775
|
-
React
|
775
|
+
React.useEffect(() => {
|
776
776
|
afterStepCallbacks.add(callback);
|
777
777
|
return () => {
|
778
778
|
afterStepCallbacks.delete(callback);
|
@@ -781,8 +781,8 @@ const useAfterPhysicsStep = callback => {
|
|
781
781
|
}; // Internal hooks
|
782
782
|
|
783
783
|
const useChildColliderProps = (ref, options, ignoreMeshColliders = true) => {
|
784
|
-
const [colliderProps, setColliderProps] = React
|
785
|
-
React
|
784
|
+
const [colliderProps, setColliderProps] = React.useState([]);
|
785
|
+
React.useEffect(() => {
|
786
786
|
const object = ref.current;
|
787
787
|
|
788
788
|
if (object && options.colliders !== false) {
|
@@ -802,16 +802,16 @@ const useRigidBody = (options = {}) => {
|
|
802
802
|
physicsOptions,
|
803
803
|
rigidBodyEvents
|
804
804
|
} = useRapier();
|
805
|
-
const ref = React
|
806
|
-
const mergedOptions = React
|
805
|
+
const ref = React.useRef();
|
806
|
+
const mergedOptions = React.useMemo(() => {
|
807
807
|
return _objectSpread2(_objectSpread2(_objectSpread2({}, physicsOptions), options), {}, {
|
808
808
|
children: undefined
|
809
809
|
});
|
810
810
|
}, [physicsOptions, options]);
|
811
811
|
const childColliderProps = useChildColliderProps(ref, mergedOptions); // Create rigidbody
|
812
812
|
|
813
|
-
const rigidBodyRef = React
|
814
|
-
const getRigidBodyRef = React
|
813
|
+
const rigidBodyRef = React.useRef();
|
814
|
+
const getRigidBodyRef = React.useRef(() => {
|
815
815
|
if (!rigidBodyRef.current) {
|
816
816
|
const desc = rigidBodyDescFromOptions(options);
|
817
817
|
const rigidBody = world.createRigidBody(desc);
|
@@ -821,7 +821,7 @@ const useRigidBody = (options = {}) => {
|
|
821
821
|
return rigidBodyRef.current;
|
822
822
|
}); // Setup
|
823
823
|
|
824
|
-
React
|
824
|
+
React.useEffect(() => {
|
825
825
|
const rigidBody = getRigidBodyRef.current();
|
826
826
|
rigidBodyRef.current = rigidBody;
|
827
827
|
|
@@ -841,7 +841,7 @@ const useRigidBody = (options = {}) => {
|
|
841
841
|
}, []);
|
842
842
|
useUpdateRigidBodyOptions(rigidBodyRef, mergedOptions, rigidBodyStates);
|
843
843
|
useRigidBodyEvents(rigidBodyRef, mergedOptions, rigidBodyEvents);
|
844
|
-
const api = React
|
844
|
+
const api = React.useMemo(() => createRigidBodyApi(getRigidBodyRef), []);
|
845
845
|
return [ref, api, childColliderProps];
|
846
846
|
};
|
847
847
|
|
@@ -891,7 +891,7 @@ const applyAttractorForceOnRigidBody = (rigidBody, {
|
|
891
891
|
}
|
892
892
|
}
|
893
893
|
};
|
894
|
-
const Attractor = /*#__PURE__*/React
|
894
|
+
const Attractor = /*#__PURE__*/React.memo(props => {
|
895
895
|
const {
|
896
896
|
position = [0, 0, 0],
|
897
897
|
strength = 1,
|
@@ -903,8 +903,8 @@ const Attractor = /*#__PURE__*/React$1.memo(props => {
|
|
903
903
|
const {
|
904
904
|
attractorStates
|
905
905
|
} = useRapier();
|
906
|
-
const object = React
|
907
|
-
React
|
906
|
+
const object = React.useRef(null);
|
907
|
+
React.useEffect(() => {
|
908
908
|
var _object$current;
|
909
909
|
|
910
910
|
let uuid = ((_object$current = object.current) === null || _object$current === void 0 ? void 0 : _object$current.uuid) || "_";
|
@@ -930,7 +930,7 @@ const Attractor = /*#__PURE__*/React$1.memo(props => {
|
|
930
930
|
});
|
931
931
|
});
|
932
932
|
|
933
|
-
const rapierContext = /*#__PURE__*/React
|
933
|
+
const rapierContext = /*#__PURE__*/React.createContext(undefined);
|
934
934
|
|
935
935
|
const getCollisionPayloadFromSource = (target, other) => {
|
936
936
|
var _target$collider$stat, _target$rigidBody$sta, _other$collider$state, _other$rigidBody$stat, _other$collider$state2, _other$rigidBody$stat2;
|
@@ -971,8 +971,8 @@ const Physics = ({
|
|
971
971
|
interpolate: _interpolate = true
|
972
972
|
}) => {
|
973
973
|
const rapier = useAsset.useAsset(importRapier);
|
974
|
-
const worldRef = React
|
975
|
-
const getWorldRef = React
|
974
|
+
const worldRef = React.useRef();
|
975
|
+
const getWorldRef = React.useRef(() => {
|
976
976
|
if (!worldRef.current) {
|
977
977
|
const world = new rapier.World(vectorArrayToVector3(_gravity));
|
978
978
|
worldRef.current = world;
|
@@ -989,7 +989,7 @@ const Physics = ({
|
|
989
989
|
const beforeStepCallbacks = useConst(() => new Set());
|
990
990
|
const afterStepCallbacks = useConst(() => new Set()); // Init world
|
991
991
|
|
992
|
-
React
|
992
|
+
React.useEffect(() => {
|
993
993
|
const world = getWorldRef.current();
|
994
994
|
return () => {
|
995
995
|
if (world) {
|
@@ -999,15 +999,15 @@ const Physics = ({
|
|
999
999
|
};
|
1000
1000
|
}, []); // Update gravity
|
1001
1001
|
|
1002
|
-
React
|
1002
|
+
React.useEffect(() => {
|
1003
1003
|
const world = worldRef.current;
|
1004
1004
|
|
1005
1005
|
if (world) {
|
1006
1006
|
world.gravity = vectorArrayToVector3(_gravity);
|
1007
1007
|
}
|
1008
1008
|
}, [_gravity]);
|
1009
|
-
const api = React
|
1010
|
-
const getSourceFromColliderHandle = React
|
1009
|
+
const api = React.useMemo(() => createWorldApi(getWorldRef), []);
|
1010
|
+
const getSourceFromColliderHandle = React.useCallback(handle => {
|
1011
1011
|
const world = worldRef.current;
|
1012
1012
|
|
1013
1013
|
if (world) {
|
@@ -1035,11 +1035,11 @@ const Physics = ({
|
|
1035
1035
|
return source;
|
1036
1036
|
}
|
1037
1037
|
}, []);
|
1038
|
-
const [steppingState] = React
|
1038
|
+
const [steppingState] = React.useState({
|
1039
1039
|
previousState: {},
|
1040
1040
|
accumulator: 0
|
1041
1041
|
});
|
1042
|
-
const step = React
|
1042
|
+
const step = React.useCallback(dt => {
|
1043
1043
|
const world = worldRef.current;
|
1044
1044
|
if (!world) return;
|
1045
1045
|
/* Check if the timestep is supposed to be variable. We'll do this here
|
@@ -1054,7 +1054,13 @@ const Physics = ({
|
|
1054
1054
|
const clampedDelta = three.MathUtils.clamp(dt, 0, 0.2);
|
1055
1055
|
|
1056
1056
|
const stepWorld = () => {
|
1057
|
-
//
|
1057
|
+
// Apply attractors
|
1058
|
+
world.forEachRigidBody(body => {
|
1059
|
+
attractorStates.forEach(attractorState => {
|
1060
|
+
applyAttractorForceOnRigidBody(body, attractorState);
|
1061
|
+
});
|
1062
|
+
}); // Trigger beforeStep callbacks
|
1063
|
+
|
1058
1064
|
beforeStepCallbacks.forEach(callback => {
|
1059
1065
|
callback(api);
|
1060
1066
|
});
|
@@ -1075,22 +1081,18 @@ const Physics = ({
|
|
1075
1081
|
steppingState.accumulator += clampedDelta;
|
1076
1082
|
|
1077
1083
|
while (steppingState.accumulator >= _timeStep) {
|
1078
|
-
|
1079
|
-
|
1080
|
-
|
1081
|
-
|
1082
|
-
|
1084
|
+
// Set up previous state
|
1085
|
+
// needed for accurate interpolations if the world steps more than once
|
1086
|
+
if (_interpolate) {
|
1087
|
+
steppingState.previousState = {};
|
1088
|
+
world.forEachRigidBody(body => {
|
1083
1089
|
steppingState.previousState[body.handle] = {
|
1084
1090
|
position: body.translation(),
|
1085
1091
|
rotation: body.rotation()
|
1086
1092
|
};
|
1087
|
-
} // Apply attractors
|
1088
|
-
|
1089
|
-
|
1090
|
-
attractorStates.forEach(attractorState => {
|
1091
|
-
applyAttractorForceOnRigidBody(body, attractorState);
|
1092
1093
|
});
|
1093
|
-
}
|
1094
|
+
}
|
1095
|
+
|
1094
1096
|
stepWorld();
|
1095
1097
|
steppingState.accumulator -= _timeStep;
|
1096
1098
|
}
|
@@ -1254,7 +1256,7 @@ const Physics = ({
|
|
1254
1256
|
fiber.useFrame((_, dt) => {
|
1255
1257
|
if (!_paused) step(dt);
|
1256
1258
|
}, updatePriority);
|
1257
|
-
const context = React
|
1259
|
+
const context = React.useMemo(() => ({
|
1258
1260
|
rapier,
|
1259
1261
|
world: api,
|
1260
1262
|
physicsOptions: {
|
@@ -1328,7 +1330,7 @@ function _objectWithoutProperties(source, excluded) {
|
|
1328
1330
|
}
|
1329
1331
|
|
1330
1332
|
// Colliders
|
1331
|
-
const AnyCollider = /*#__PURE__*/React
|
1333
|
+
const AnyCollider = /*#__PURE__*/React.memo( /*#__PURE__*/React__default["default"].forwardRef((props, forwardedRef) => {
|
1332
1334
|
const {
|
1333
1335
|
children,
|
1334
1336
|
position,
|
@@ -1343,8 +1345,8 @@ const AnyCollider = /*#__PURE__*/React$1.memo( /*#__PURE__*/React__default["defa
|
|
1343
1345
|
colliderStates
|
1344
1346
|
} = useRapier();
|
1345
1347
|
const rigidBodyContext = useRigidBodyContext();
|
1346
|
-
const ref = React
|
1347
|
-
const collidersRef = React
|
1348
|
+
const ref = React.useRef(null);
|
1349
|
+
const collidersRef = React.useMemo(() => {
|
1348
1350
|
if (forwardedRef !== null) {
|
1349
1351
|
return forwardedRef;
|
1350
1352
|
}
|
@@ -1353,7 +1355,7 @@ const AnyCollider = /*#__PURE__*/React$1.memo( /*#__PURE__*/React__default["defa
|
|
1353
1355
|
result.current = [];
|
1354
1356
|
return result;
|
1355
1357
|
}, []);
|
1356
|
-
React
|
1358
|
+
React.useEffect(() => {
|
1357
1359
|
const object = ref.current;
|
1358
1360
|
const worldScale = object.getWorldScale(new three.Vector3());
|
1359
1361
|
const colliders = []; // If this is an InstancedRigidBody api
|
@@ -1385,7 +1387,7 @@ const AnyCollider = /*#__PURE__*/React$1.memo( /*#__PURE__*/React__default["defa
|
|
1385
1387
|
});
|
1386
1388
|
};
|
1387
1389
|
}, []);
|
1388
|
-
const mergedProps = React
|
1390
|
+
const mergedProps = React.useMemo(() => {
|
1389
1391
|
return _objectSpread2(_objectSpread2({}, rigidBodyContext === null || rigidBodyContext === void 0 ? void 0 : rigidBodyContext.options), props);
|
1390
1392
|
}, [props, rigidBodyContext === null || rigidBodyContext === void 0 ? void 0 : rigidBodyContext.options]);
|
1391
1393
|
useUpdateColliderOptions(collidersRef, mergedProps, colliderStates);
|
@@ -1464,9 +1466,9 @@ CylinderCollider.displayName = "CylinderCollider";
|
|
1464
1466
|
ConvexHullCollider.displayName = "ConvexHullCollider";
|
1465
1467
|
|
1466
1468
|
const _excluded$1 = ["children", "type", "position", "rotation", "scale", "quaternion"];
|
1467
|
-
const RigidBodyContext = /*#__PURE__*/React
|
1468
|
-
const useRigidBodyContext = () => React
|
1469
|
-
const RigidBody = /*#__PURE__*/React
|
1469
|
+
const RigidBodyContext = /*#__PURE__*/React.createContext(undefined);
|
1470
|
+
const useRigidBodyContext = () => React.useContext(RigidBodyContext);
|
1471
|
+
const RigidBody = /*#__PURE__*/React.memo( /*#__PURE__*/React.forwardRef((props, ref) => {
|
1470
1472
|
const {
|
1471
1473
|
children,
|
1472
1474
|
type,
|
@@ -1478,8 +1480,8 @@ const RigidBody = /*#__PURE__*/React$1.memo( /*#__PURE__*/React$1.forwardRef((pr
|
|
1478
1480
|
objectProps = _objectWithoutProperties(props, _excluded$1);
|
1479
1481
|
|
1480
1482
|
const [object, api, childColliderProps] = useRigidBody(props);
|
1481
|
-
React
|
1482
|
-
const contextValue = React
|
1483
|
+
React.useImperativeHandle(ref, () => api);
|
1484
|
+
const contextValue = React.useMemo(() => ({
|
1483
1485
|
ref: object,
|
1484
1486
|
api,
|
1485
1487
|
options: props
|
@@ -1499,7 +1501,7 @@ const RigidBody = /*#__PURE__*/React$1.memo( /*#__PURE__*/React$1.forwardRef((pr
|
|
1499
1501
|
}));
|
1500
1502
|
RigidBody.displayName = "RigidBody";
|
1501
1503
|
|
1502
|
-
const MeshCollider = /*#__PURE__*/React
|
1504
|
+
const MeshCollider = /*#__PURE__*/React.memo(props => {
|
1503
1505
|
const {
|
1504
1506
|
children,
|
1505
1507
|
type
|
@@ -1508,11 +1510,11 @@ const MeshCollider = /*#__PURE__*/React$1.memo(props => {
|
|
1508
1510
|
physicsOptions,
|
1509
1511
|
world
|
1510
1512
|
} = useRapier();
|
1511
|
-
const object = React
|
1513
|
+
const object = React.useRef(null);
|
1512
1514
|
const {
|
1513
1515
|
options
|
1514
1516
|
} = useRigidBodyContext();
|
1515
|
-
const mergedOptions = React
|
1517
|
+
const mergedOptions = React.useMemo(() => {
|
1516
1518
|
return _objectSpread2(_objectSpread2(_objectSpread2({}, physicsOptions), options), {}, {
|
1517
1519
|
children: undefined,
|
1518
1520
|
colliders: type
|
@@ -1552,10 +1554,10 @@ const AttractorHelper = props => {
|
|
1552
1554
|
const {
|
1553
1555
|
scene
|
1554
1556
|
} = fiber.useThree();
|
1555
|
-
const ref = React
|
1556
|
-
const normalsHelper = React
|
1557
|
+
const ref = React.useRef(null);
|
1558
|
+
const normalsHelper = React.useRef();
|
1557
1559
|
const color = props.strength > 0 ? 0x0000ff : 0xff0000;
|
1558
|
-
React
|
1560
|
+
React.useEffect(() => {
|
1559
1561
|
if (ref.current) {
|
1560
1562
|
normalsHelper.current = new threeStdlib.VertexNormalsHelper(ref.current, props.range, color);
|
1561
1563
|
normalsHelper.current.frustumCulled = false;
|
@@ -1589,14 +1591,14 @@ const AttractorHelper = props => {
|
|
1589
1591
|
}));
|
1590
1592
|
};
|
1591
1593
|
|
1592
|
-
const Debug = /*#__PURE__*/React
|
1594
|
+
const Debug = /*#__PURE__*/React.memo(() => {
|
1593
1595
|
const {
|
1594
1596
|
world,
|
1595
1597
|
attractorStates
|
1596
1598
|
} = useRapier();
|
1597
|
-
const ref = React
|
1598
|
-
const [attractors, setAttractors] = React
|
1599
|
-
const currMap = React
|
1599
|
+
const ref = React.useRef(null);
|
1600
|
+
const [attractors, setAttractors] = React.useState([]);
|
1601
|
+
const currMap = React.useRef(new Map());
|
1600
1602
|
fiber.useFrame(() => {
|
1601
1603
|
const mesh = ref.current;
|
1602
1604
|
if (!mesh) return;
|
@@ -1621,14 +1623,14 @@ const Debug = /*#__PURE__*/React$1.memo(() => {
|
|
1621
1623
|
});
|
1622
1624
|
|
1623
1625
|
const _excluded = ["positions", "rotations", "children"];
|
1624
|
-
const InstancedRigidBodies = /*#__PURE__*/React
|
1626
|
+
const InstancedRigidBodies = /*#__PURE__*/React.forwardRef((props, ref) => {
|
1625
1627
|
const {
|
1626
1628
|
world,
|
1627
1629
|
rigidBodyStates,
|
1628
1630
|
physicsOptions,
|
1629
1631
|
rigidBodyEvents
|
1630
1632
|
} = useRapier();
|
1631
|
-
const object = React
|
1633
|
+
const object = React.useRef(null);
|
1632
1634
|
|
1633
1635
|
const {
|
1634
1636
|
positions,
|
@@ -1637,20 +1639,20 @@ const InstancedRigidBodies = /*#__PURE__*/React$1.forwardRef((props, ref) => {
|
|
1637
1639
|
} = props,
|
1638
1640
|
options = _objectWithoutProperties(props, _excluded);
|
1639
1641
|
|
1640
|
-
const instancesRef = React
|
1641
|
-
const rigidBodyRefs = React
|
1642
|
-
const instancesRefGetter = React
|
1642
|
+
const instancesRef = React.useRef([]);
|
1643
|
+
const rigidBodyRefs = React.useRef([]);
|
1644
|
+
const instancesRefGetter = React.useRef(() => {
|
1643
1645
|
if (!instancesRef.current) {
|
1644
1646
|
instancesRef.current = [];
|
1645
1647
|
}
|
1646
1648
|
|
1647
1649
|
return instancesRef.current;
|
1648
1650
|
});
|
1649
|
-
const mergedOptions = React
|
1651
|
+
const mergedOptions = React.useMemo(() => {
|
1650
1652
|
return _objectSpread2(_objectSpread2({}, physicsOptions), options);
|
1651
1653
|
}, [physicsOptions, options]);
|
1652
1654
|
const childColliderProps = useChildColliderProps(object, mergedOptions);
|
1653
|
-
React
|
1655
|
+
React.useLayoutEffect(() => {
|
1654
1656
|
object.current.updateWorldMatrix(true, false);
|
1655
1657
|
const instances = instancesRefGetter.current();
|
1656
1658
|
const invertedWorld = object.current.matrixWorld.clone().invert();
|
@@ -1711,22 +1713,22 @@ const InstancedRigidBodies = /*#__PURE__*/React$1.forwardRef((props, ref) => {
|
|
1711
1713
|
instancesRef.current = [];
|
1712
1714
|
};
|
1713
1715
|
}, []);
|
1714
|
-
const api = React
|
1715
|
-
React
|
1716
|
+
const api = React.useMemo(() => createInstancedRigidBodiesApi(instancesRefGetter), []);
|
1717
|
+
React.useImperativeHandle(ref, () => api);
|
1716
1718
|
useUpdateRigidBodyOptions(rigidBodyRefs, mergedOptions, rigidBodyStates, false);
|
1717
1719
|
useRigidBodyEvents(rigidBodyRefs, mergedOptions, rigidBodyEvents);
|
1718
|
-
const contextValue = React
|
1720
|
+
const contextValue = React.useMemo(() => {
|
1719
1721
|
return {
|
1720
1722
|
ref: object,
|
1721
1723
|
api,
|
1722
1724
|
options: mergedOptions
|
1723
1725
|
};
|
1724
1726
|
}, [api, mergedOptions]);
|
1725
|
-
return /*#__PURE__*/
|
1727
|
+
return /*#__PURE__*/React__default["default"].createElement(RigidBodyContext.Provider, {
|
1726
1728
|
value: contextValue
|
1727
|
-
}, /*#__PURE__*/
|
1729
|
+
}, /*#__PURE__*/React__default["default"].createElement("object3D", {
|
1728
1730
|
ref: object
|
1729
|
-
}, props.children, childColliderProps.map((colliderProps, index) => /*#__PURE__*/
|
1731
|
+
}, props.children, childColliderProps.map((colliderProps, index) => /*#__PURE__*/React__default["default"].createElement(AnyCollider, _extends({
|
1730
1732
|
key: index
|
1731
1733
|
}, colliderProps)))));
|
1732
1734
|
});
|
@@ -1736,8 +1738,8 @@ const useImpulseJoint = (body1, body2, params) => {
|
|
1736
1738
|
const {
|
1737
1739
|
world
|
1738
1740
|
} = useRapier();
|
1739
|
-
const jointRef = React
|
1740
|
-
const getJointRef = React
|
1741
|
+
const jointRef = React.useRef();
|
1742
|
+
const getJointRef = React.useRef(() => {
|
1741
1743
|
if (!jointRef.current) {
|
1742
1744
|
let rb1;
|
1743
1745
|
let rb2;
|
@@ -1752,7 +1754,7 @@ const useImpulseJoint = (body1, body2, params) => {
|
|
1752
1754
|
|
1753
1755
|
return jointRef.current;
|
1754
1756
|
});
|
1755
|
-
React
|
1757
|
+
React.useEffect(() => {
|
1756
1758
|
const joint = getJointRef.current();
|
1757
1759
|
return () => {
|
1758
1760
|
if (joint) {
|
@@ -1761,7 +1763,7 @@ const useImpulseJoint = (body1, body2, params) => {
|
|
1761
1763
|
}
|
1762
1764
|
};
|
1763
1765
|
}, []);
|
1764
|
-
const api = React
|
1766
|
+
const api = React.useMemo(() => createJointApi(getJointRef), []);
|
1765
1767
|
return api;
|
1766
1768
|
};
|
1767
1769
|
/**
|
@@ -1,7 +1,7 @@
|
|
1
1
|
import { ColliderDesc, ActiveEvents, RigidBodyDesc, EventQueue } from '@dimforge/rapier3d-compat';
|
2
2
|
export { CoefficientCombineRule, Collider as RapierCollider, RigidBody as RapierRigidBody } from '@dimforge/rapier3d-compat';
|
3
3
|
import { useFrame, useThree } from '@react-three/fiber';
|
4
|
-
import React
|
4
|
+
import React, { useRef, useMemo, useEffect, useContext, useState, memo, createContext, useCallback, forwardRef, useImperativeHandle, useLayoutEffect } from 'react';
|
5
5
|
import { Quaternion, Euler, Vector3, Object3D, Matrix4, MathUtils, InstancedMesh, BufferAttribute, DynamicDrawUsage } from 'three';
|
6
6
|
import { useAsset } from 'use-asset';
|
7
7
|
import { mergeVertices, VertexNormalsHelper } from 'three-stdlib';
|
@@ -899,7 +899,7 @@ const Attractor = /*#__PURE__*/memo(props => {
|
|
899
899
|
attractorStates.delete(uuid);
|
900
900
|
};
|
901
901
|
}, [props]);
|
902
|
-
return /*#__PURE__*/React
|
902
|
+
return /*#__PURE__*/React.createElement("object3D", {
|
903
903
|
ref: object,
|
904
904
|
position: position
|
905
905
|
});
|
@@ -1029,7 +1029,13 @@ const Physics = ({
|
|
1029
1029
|
const clampedDelta = MathUtils.clamp(dt, 0, 0.2);
|
1030
1030
|
|
1031
1031
|
const stepWorld = () => {
|
1032
|
-
//
|
1032
|
+
// Apply attractors
|
1033
|
+
world.forEachRigidBody(body => {
|
1034
|
+
attractorStates.forEach(attractorState => {
|
1035
|
+
applyAttractorForceOnRigidBody(body, attractorState);
|
1036
|
+
});
|
1037
|
+
}); // Trigger beforeStep callbacks
|
1038
|
+
|
1033
1039
|
beforeStepCallbacks.forEach(callback => {
|
1034
1040
|
callback(api);
|
1035
1041
|
});
|
@@ -1050,22 +1056,18 @@ const Physics = ({
|
|
1050
1056
|
steppingState.accumulator += clampedDelta;
|
1051
1057
|
|
1052
1058
|
while (steppingState.accumulator >= _timeStep) {
|
1053
|
-
|
1054
|
-
|
1055
|
-
|
1056
|
-
|
1057
|
-
|
1059
|
+
// Set up previous state
|
1060
|
+
// needed for accurate interpolations if the world steps more than once
|
1061
|
+
if (_interpolate) {
|
1062
|
+
steppingState.previousState = {};
|
1063
|
+
world.forEachRigidBody(body => {
|
1058
1064
|
steppingState.previousState[body.handle] = {
|
1059
1065
|
position: body.translation(),
|
1060
1066
|
rotation: body.rotation()
|
1061
1067
|
};
|
1062
|
-
} // Apply attractors
|
1063
|
-
|
1064
|
-
|
1065
|
-
attractorStates.forEach(attractorState => {
|
1066
|
-
applyAttractorForceOnRigidBody(body, attractorState);
|
1067
1068
|
});
|
1068
|
-
}
|
1069
|
+
}
|
1070
|
+
|
1069
1071
|
stepWorld();
|
1070
1072
|
steppingState.accumulator -= _timeStep;
|
1071
1073
|
}
|
@@ -1246,7 +1248,7 @@ const Physics = ({
|
|
1246
1248
|
isPaused: _paused,
|
1247
1249
|
step
|
1248
1250
|
}), [_paused, step]);
|
1249
|
-
return /*#__PURE__*/React
|
1251
|
+
return /*#__PURE__*/React.createElement(rapierContext.Provider, {
|
1250
1252
|
value: context
|
1251
1253
|
}, children);
|
1252
1254
|
};
|
@@ -1303,7 +1305,7 @@ function _objectWithoutProperties(source, excluded) {
|
|
1303
1305
|
}
|
1304
1306
|
|
1305
1307
|
// Colliders
|
1306
|
-
const AnyCollider = /*#__PURE__*/memo( /*#__PURE__*/React
|
1308
|
+
const AnyCollider = /*#__PURE__*/memo( /*#__PURE__*/React.forwardRef((props, forwardedRef) => {
|
1307
1309
|
const {
|
1308
1310
|
children,
|
1309
1311
|
position,
|
@@ -1324,7 +1326,7 @@ const AnyCollider = /*#__PURE__*/memo( /*#__PURE__*/React$1.forwardRef((props, f
|
|
1324
1326
|
return forwardedRef;
|
1325
1327
|
}
|
1326
1328
|
|
1327
|
-
const result = /*#__PURE__*/React
|
1329
|
+
const result = /*#__PURE__*/React.createRef();
|
1328
1330
|
result.current = [];
|
1329
1331
|
return result;
|
1330
1332
|
}, []);
|
@@ -1365,7 +1367,7 @@ const AnyCollider = /*#__PURE__*/memo( /*#__PURE__*/React$1.forwardRef((props, f
|
|
1365
1367
|
}, [props, rigidBodyContext === null || rigidBodyContext === void 0 ? void 0 : rigidBodyContext.options]);
|
1366
1368
|
useUpdateColliderOptions(collidersRef, mergedProps, colliderStates);
|
1367
1369
|
useColliderEvents(collidersRef, mergedProps, colliderEvents);
|
1368
|
-
return /*#__PURE__*/React
|
1370
|
+
return /*#__PURE__*/React.createElement("object3D", {
|
1369
1371
|
position: position,
|
1370
1372
|
rotation: rotation,
|
1371
1373
|
quaternion: quaternion,
|
@@ -1374,56 +1376,56 @@ const AnyCollider = /*#__PURE__*/memo( /*#__PURE__*/React$1.forwardRef((props, f
|
|
1374
1376
|
name: name
|
1375
1377
|
}, children);
|
1376
1378
|
}));
|
1377
|
-
const CuboidCollider = /*#__PURE__*/React
|
1378
|
-
return /*#__PURE__*/React
|
1379
|
+
const CuboidCollider = /*#__PURE__*/React.forwardRef((props, ref) => {
|
1380
|
+
return /*#__PURE__*/React.createElement(AnyCollider, _extends({}, props, {
|
1379
1381
|
shape: "cuboid",
|
1380
1382
|
ref: ref
|
1381
1383
|
}));
|
1382
1384
|
});
|
1383
|
-
const RoundCuboidCollider = /*#__PURE__*/React
|
1384
|
-
return /*#__PURE__*/React
|
1385
|
+
const RoundCuboidCollider = /*#__PURE__*/React.forwardRef((props, ref) => {
|
1386
|
+
return /*#__PURE__*/React.createElement(AnyCollider, _extends({}, props, {
|
1385
1387
|
shape: "roundCuboid",
|
1386
1388
|
ref: ref
|
1387
1389
|
}));
|
1388
1390
|
});
|
1389
|
-
const BallCollider = /*#__PURE__*/React
|
1390
|
-
return /*#__PURE__*/React
|
1391
|
+
const BallCollider = /*#__PURE__*/React.forwardRef((props, ref) => {
|
1392
|
+
return /*#__PURE__*/React.createElement(AnyCollider, _extends({}, props, {
|
1391
1393
|
shape: "ball",
|
1392
1394
|
ref: ref
|
1393
1395
|
}));
|
1394
1396
|
});
|
1395
|
-
const CapsuleCollider = /*#__PURE__*/React
|
1396
|
-
return /*#__PURE__*/React
|
1397
|
+
const CapsuleCollider = /*#__PURE__*/React.forwardRef((props, ref) => {
|
1398
|
+
return /*#__PURE__*/React.createElement(AnyCollider, _extends({}, props, {
|
1397
1399
|
shape: "capsule",
|
1398
1400
|
ref: ref
|
1399
1401
|
}));
|
1400
1402
|
});
|
1401
|
-
const HeightfieldCollider = /*#__PURE__*/React
|
1402
|
-
return /*#__PURE__*/React
|
1403
|
+
const HeightfieldCollider = /*#__PURE__*/React.forwardRef((props, ref) => {
|
1404
|
+
return /*#__PURE__*/React.createElement(AnyCollider, _extends({}, props, {
|
1403
1405
|
shape: "heightfield",
|
1404
1406
|
ref: ref
|
1405
1407
|
}));
|
1406
1408
|
});
|
1407
|
-
const TrimeshCollider = /*#__PURE__*/React
|
1408
|
-
return /*#__PURE__*/React
|
1409
|
+
const TrimeshCollider = /*#__PURE__*/React.forwardRef((props, ref) => {
|
1410
|
+
return /*#__PURE__*/React.createElement(AnyCollider, _extends({}, props, {
|
1409
1411
|
shape: "trimesh",
|
1410
1412
|
ref: ref
|
1411
1413
|
}));
|
1412
1414
|
});
|
1413
|
-
const ConeCollider = /*#__PURE__*/React
|
1414
|
-
return /*#__PURE__*/React
|
1415
|
+
const ConeCollider = /*#__PURE__*/React.forwardRef((props, ref) => {
|
1416
|
+
return /*#__PURE__*/React.createElement(AnyCollider, _extends({}, props, {
|
1415
1417
|
shape: "cone",
|
1416
1418
|
ref: ref
|
1417
1419
|
}));
|
1418
1420
|
});
|
1419
|
-
const CylinderCollider = /*#__PURE__*/React
|
1420
|
-
return /*#__PURE__*/React
|
1421
|
+
const CylinderCollider = /*#__PURE__*/React.forwardRef((props, ref) => {
|
1422
|
+
return /*#__PURE__*/React.createElement(AnyCollider, _extends({}, props, {
|
1421
1423
|
shape: "cylinder",
|
1422
1424
|
ref: ref
|
1423
1425
|
}));
|
1424
1426
|
});
|
1425
|
-
const ConvexHullCollider = /*#__PURE__*/React
|
1426
|
-
return /*#__PURE__*/React
|
1427
|
+
const ConvexHullCollider = /*#__PURE__*/React.forwardRef((props, ref) => {
|
1428
|
+
return /*#__PURE__*/React.createElement(AnyCollider, _extends({}, props, {
|
1427
1429
|
shape: "convexHull",
|
1428
1430
|
ref: ref
|
1429
1431
|
}));
|
@@ -1459,16 +1461,16 @@ const RigidBody = /*#__PURE__*/memo( /*#__PURE__*/forwardRef((props, ref) => {
|
|
1459
1461
|
api,
|
1460
1462
|
options: props
|
1461
1463
|
}), [object, api, props]);
|
1462
|
-
return /*#__PURE__*/React
|
1464
|
+
return /*#__PURE__*/React.createElement(RigidBodyContext.Provider, {
|
1463
1465
|
value: contextValue
|
1464
|
-
}, /*#__PURE__*/React
|
1466
|
+
}, /*#__PURE__*/React.createElement("object3D", _extends({
|
1465
1467
|
ref: object
|
1466
1468
|
}, objectProps, {
|
1467
1469
|
position: position,
|
1468
1470
|
rotation: rotation,
|
1469
1471
|
quaternion: quaternion,
|
1470
1472
|
scale: scale
|
1471
|
-
}), children, childColliderProps.map((colliderProps, index) => /*#__PURE__*/React
|
1473
|
+
}), children, childColliderProps.map((colliderProps, index) => /*#__PURE__*/React.createElement(AnyCollider, _extends({
|
1472
1474
|
key: index
|
1473
1475
|
}, colliderProps)))));
|
1474
1476
|
}));
|
@@ -1494,12 +1496,12 @@ const MeshCollider = /*#__PURE__*/memo(props => {
|
|
1494
1496
|
});
|
1495
1497
|
}, [physicsOptions, options]);
|
1496
1498
|
const childColliderProps = useChildColliderProps(object, mergedOptions, false);
|
1497
|
-
return /*#__PURE__*/React
|
1499
|
+
return /*#__PURE__*/React.createElement("object3D", {
|
1498
1500
|
ref: object,
|
1499
1501
|
userData: {
|
1500
1502
|
r3RapierType: "MeshCollider"
|
1501
1503
|
}
|
1502
|
-
}, children, childColliderProps.map((colliderProps, index) => /*#__PURE__*/React
|
1504
|
+
}, children, childColliderProps.map((colliderProps, index) => /*#__PURE__*/React.createElement(AnyCollider, _extends({
|
1503
1505
|
key: index
|
1504
1506
|
}, colliderProps))));
|
1505
1507
|
});
|
@@ -1552,13 +1554,13 @@ const AttractorHelper = props => {
|
|
1552
1554
|
(_normalsHelper$curren = normalsHelper.current) === null || _normalsHelper$curren === void 0 ? void 0 : _normalsHelper$curren.update();
|
1553
1555
|
}
|
1554
1556
|
});
|
1555
|
-
return /*#__PURE__*/React
|
1557
|
+
return /*#__PURE__*/React.createElement("mesh", {
|
1556
1558
|
ref: ref,
|
1557
1559
|
position: props.object.position,
|
1558
1560
|
frustumCulled: false
|
1559
|
-
}, /*#__PURE__*/React
|
1561
|
+
}, /*#__PURE__*/React.createElement("sphereGeometry", {
|
1560
1562
|
args: [0.2, 6, 6]
|
1561
|
-
}), /*#__PURE__*/React
|
1563
|
+
}), /*#__PURE__*/React.createElement("meshBasicMaterial", {
|
1562
1564
|
color: color,
|
1563
1565
|
wireframe: true
|
1564
1566
|
}));
|
@@ -1584,13 +1586,13 @@ const Debug = /*#__PURE__*/memo(() => {
|
|
1584
1586
|
currMap.current = new Map(attractorStates);
|
1585
1587
|
}
|
1586
1588
|
});
|
1587
|
-
return /*#__PURE__*/React
|
1589
|
+
return /*#__PURE__*/React.createElement("group", null, /*#__PURE__*/React.createElement("lineSegments", {
|
1588
1590
|
ref: ref,
|
1589
1591
|
frustumCulled: false
|
1590
|
-
}, /*#__PURE__*/React
|
1592
|
+
}, /*#__PURE__*/React.createElement("lineBasicMaterial", {
|
1591
1593
|
color: 0xffffff,
|
1592
1594
|
vertexColors: true
|
1593
|
-
}), /*#__PURE__*/React
|
1595
|
+
}), /*#__PURE__*/React.createElement("bufferGeometry", null)), attractors.map((attractor, i) => /*#__PURE__*/React.createElement(AttractorHelper, _extends({
|
1594
1596
|
key: attractor.object.uuid
|
1595
1597
|
}, attractor))));
|
1596
1598
|
});
|