@react-three/rapier 0.14.0-rc.1 → 0.14.0-rc.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,7 +1,7 @@
1
1
  import { ActiveEvents, ColliderDesc, EventQueue, RigidBodyDesc } from '@dimforge/rapier3d-compat';
2
2
  export { CoefficientCombineRule, Collider as RapierCollider, RigidBody as RapierRigidBody } from '@dimforge/rapier3d-compat';
3
- import { useThree, useFrame } from '@react-three/fiber';
4
- import React, { useRef, useEffect, useMemo, useContext, useState, memo, createContext, useCallback, forwardRef, useImperativeHandle, Fragment } from 'react';
3
+ import { useFrame, useThree } from '@react-three/fiber';
4
+ import React$1, { useRef, useMemo, useEffect, useContext, useState, memo, createContext, useCallback, forwardRef, useImperativeHandle, Fragment } from 'react';
5
5
  import { Quaternion, Euler, Vector3, Object3D, Matrix4, MathUtils, BufferAttribute, DynamicDrawUsage } from 'three';
6
6
  import { useAsset } from 'use-asset';
7
7
  import { mergeVertices, VertexNormalsHelper } from 'three-stdlib';
@@ -153,27 +153,6 @@ function useConst(initialValue) {
153
153
  return ref.current.value;
154
154
  }
155
155
 
156
- const useRaf = callback => {
157
- const cb = useRef(callback);
158
- const raf = useRef(0);
159
- const lastFrame = useRef(0);
160
- useEffect(() => {
161
- cb.current = callback;
162
- }, [callback]);
163
- useEffect(() => {
164
- const loop = () => {
165
- const now = performance.now();
166
- const delta = now - lastFrame.current;
167
- raf.current = requestAnimationFrame(loop);
168
- cb.current(delta / 1000);
169
- lastFrame.current = now;
170
- };
171
-
172
- raf.current = requestAnimationFrame(loop);
173
- return () => cancelAnimationFrame(raf.current);
174
- }, []);
175
- };
176
-
177
156
  function _objectWithoutPropertiesLoose(source, excluded) {
178
157
  if (source == null) return {};
179
158
  var target = {};
@@ -681,12 +660,67 @@ const Attractor = /*#__PURE__*/memo(props => {
681
660
  attractorStates.delete(uuid);
682
661
  };
683
662
  }, [props]);
684
- return /*#__PURE__*/React.createElement("object3D", {
663
+ return /*#__PURE__*/React$1.createElement("object3D", {
685
664
  ref: object,
686
665
  position: position
687
666
  });
688
667
  });
689
668
 
669
+ const useRaf = callback => {
670
+ const cb = useRef(callback);
671
+ const raf = useRef(0);
672
+ const lastFrame = useRef(0);
673
+ useEffect(() => {
674
+ cb.current = callback;
675
+ }, [callback]);
676
+ useEffect(() => {
677
+ const loop = () => {
678
+ const now = performance.now();
679
+ const delta = now - lastFrame.current;
680
+ raf.current = requestAnimationFrame(loop);
681
+ cb.current(delta / 1000);
682
+ lastFrame.current = now;
683
+ };
684
+
685
+ raf.current = requestAnimationFrame(loop);
686
+ return () => cancelAnimationFrame(raf.current);
687
+ }, []);
688
+ };
689
+
690
+ const UseFrameStepper = ({
691
+ onStep,
692
+ updatePriority
693
+ }) => {
694
+ useFrame((_, dt) => {
695
+ onStep(dt);
696
+ }, updatePriority);
697
+ return null;
698
+ };
699
+
700
+ const RafStepper = ({
701
+ onStep
702
+ }) => {
703
+ useRaf(dt => {
704
+ onStep(dt);
705
+ });
706
+ return null;
707
+ };
708
+
709
+ const FrameStepper = ({
710
+ onStep,
711
+ type,
712
+ updatePriority
713
+ }) => {
714
+ return type === "independent" ? /*#__PURE__*/React.createElement(RafStepper, {
715
+ onStep: onStep
716
+ }) : /*#__PURE__*/React.createElement(UseFrameStepper, {
717
+ onStep: onStep,
718
+ updatePriority: updatePriority
719
+ });
720
+ };
721
+
722
+ var FrameStepper$1 = /*#__PURE__*/memo(FrameStepper);
723
+
690
724
  const rapierContext = /*#__PURE__*/createContext(undefined);
691
725
 
692
726
  const getCollisionPayloadFromSource = (target, other) => {
@@ -728,7 +762,9 @@ const Physics = ({
728
762
  children,
729
763
  timeStep: _timeStep = 1 / 60,
730
764
  paused: _paused = false,
731
- interpolate: _interpolate = true
765
+ interpolate: _interpolate = true,
766
+ updatePriority,
767
+ updateLoop: _updateLoop = "follow"
732
768
  }) => {
733
769
  const rapier = useAsset(importRapier);
734
770
  const {
@@ -1014,13 +1050,10 @@ const Physics = ({
1014
1050
  maxForceMagnitude: event.maxForceMagnitude()
1015
1051
  }));
1016
1052
  });
1017
- world.forEachActiveRigidBody(body => {
1053
+ world.forEachActiveRigidBody(() => {
1018
1054
  invalidate();
1019
1055
  });
1020
1056
  }, [_paused, _timeStep, _interpolate]);
1021
- useRaf(dt => {
1022
- if (!_paused) step(dt);
1023
- });
1024
1057
  const context = useMemo(() => ({
1025
1058
  rapier,
1026
1059
  world: api,
@@ -1038,9 +1071,18 @@ const Physics = ({
1038
1071
  isPaused: _paused,
1039
1072
  step
1040
1073
  }), [_paused, step]);
1041
- return /*#__PURE__*/React.createElement(rapierContext.Provider, {
1074
+ const stepCallback = useCallback(delta => {
1075
+ if (!_paused) {
1076
+ step(delta);
1077
+ }
1078
+ }, [_paused, step]);
1079
+ return /*#__PURE__*/React$1.createElement(rapierContext.Provider, {
1042
1080
  value: context
1043
- }, children);
1081
+ }, /*#__PURE__*/React$1.createElement(FrameStepper$1, {
1082
+ onStep: stepCallback,
1083
+ type: _updateLoop,
1084
+ updatePriority: updatePriority
1085
+ }), children);
1044
1086
  };
1045
1087
 
1046
1088
  function _extends() {
@@ -1176,7 +1218,7 @@ const AnyCollider = /*#__PURE__*/memo( /*#__PURE__*/forwardRef((props, forwarded
1176
1218
  }, [props, rigidBodyContext === null || rigidBodyContext === void 0 ? void 0 : rigidBodyContext.options]);
1177
1219
  useUpdateColliderOptions(getInstance, mergedProps, colliderStates);
1178
1220
  useColliderEvents(getInstance, mergedProps, colliderEvents, getActiveCollisionEventsFromProps(rigidBodyContext === null || rigidBodyContext === void 0 ? void 0 : rigidBodyContext.options));
1179
- return /*#__PURE__*/React.createElement("object3D", {
1221
+ return /*#__PURE__*/React$1.createElement("object3D", {
1180
1222
  position: position,
1181
1223
  rotation: rotation,
1182
1224
  quaternion: quaternion,
@@ -1190,8 +1232,8 @@ const AnyCollider = /*#__PURE__*/memo( /*#__PURE__*/forwardRef((props, forwarded
1190
1232
  * A cuboid collider shape
1191
1233
  * @category Colliders
1192
1234
  */
1193
- const CuboidCollider = /*#__PURE__*/React.forwardRef((props, ref) => {
1194
- return /*#__PURE__*/React.createElement(AnyCollider, _extends({}, props, {
1235
+ const CuboidCollider = /*#__PURE__*/React$1.forwardRef((props, ref) => {
1236
+ return /*#__PURE__*/React$1.createElement(AnyCollider, _extends({}, props, {
1195
1237
  shape: "cuboid",
1196
1238
  ref: ref
1197
1239
  }));
@@ -1201,8 +1243,8 @@ const CuboidCollider = /*#__PURE__*/React.forwardRef((props, ref) => {
1201
1243
  * @category Colliders
1202
1244
  */
1203
1245
 
1204
- const RoundCuboidCollider = /*#__PURE__*/React.forwardRef((props, ref) => {
1205
- return /*#__PURE__*/React.createElement(AnyCollider, _extends({}, props, {
1246
+ const RoundCuboidCollider = /*#__PURE__*/React$1.forwardRef((props, ref) => {
1247
+ return /*#__PURE__*/React$1.createElement(AnyCollider, _extends({}, props, {
1206
1248
  shape: "roundCuboid",
1207
1249
  ref: ref
1208
1250
  }));
@@ -1212,8 +1254,8 @@ const RoundCuboidCollider = /*#__PURE__*/React.forwardRef((props, ref) => {
1212
1254
  * @category Colliders
1213
1255
  */
1214
1256
 
1215
- const BallCollider = /*#__PURE__*/React.forwardRef((props, ref) => {
1216
- return /*#__PURE__*/React.createElement(AnyCollider, _extends({}, props, {
1257
+ const BallCollider = /*#__PURE__*/React$1.forwardRef((props, ref) => {
1258
+ return /*#__PURE__*/React$1.createElement(AnyCollider, _extends({}, props, {
1217
1259
  shape: "ball",
1218
1260
  ref: ref
1219
1261
  }));
@@ -1223,8 +1265,8 @@ const BallCollider = /*#__PURE__*/React.forwardRef((props, ref) => {
1223
1265
  * @category Colliders
1224
1266
  */
1225
1267
 
1226
- const CapsuleCollider = /*#__PURE__*/React.forwardRef((props, ref) => {
1227
- return /*#__PURE__*/React.createElement(AnyCollider, _extends({}, props, {
1268
+ const CapsuleCollider = /*#__PURE__*/React$1.forwardRef((props, ref) => {
1269
+ return /*#__PURE__*/React$1.createElement(AnyCollider, _extends({}, props, {
1228
1270
  shape: "capsule",
1229
1271
  ref: ref
1230
1272
  }));
@@ -1234,8 +1276,8 @@ const CapsuleCollider = /*#__PURE__*/React.forwardRef((props, ref) => {
1234
1276
  * @category Colliders
1235
1277
  */
1236
1278
 
1237
- const HeightfieldCollider = /*#__PURE__*/React.forwardRef((props, ref) => {
1238
- return /*#__PURE__*/React.createElement(AnyCollider, _extends({}, props, {
1279
+ const HeightfieldCollider = /*#__PURE__*/React$1.forwardRef((props, ref) => {
1280
+ return /*#__PURE__*/React$1.createElement(AnyCollider, _extends({}, props, {
1239
1281
  shape: "heightfield",
1240
1282
  ref: ref
1241
1283
  }));
@@ -1245,8 +1287,8 @@ const HeightfieldCollider = /*#__PURE__*/React.forwardRef((props, ref) => {
1245
1287
  * @category Colliders
1246
1288
  */
1247
1289
 
1248
- const TrimeshCollider = /*#__PURE__*/React.forwardRef((props, ref) => {
1249
- return /*#__PURE__*/React.createElement(AnyCollider, _extends({}, props, {
1290
+ const TrimeshCollider = /*#__PURE__*/React$1.forwardRef((props, ref) => {
1291
+ return /*#__PURE__*/React$1.createElement(AnyCollider, _extends({}, props, {
1250
1292
  shape: "trimesh",
1251
1293
  ref: ref
1252
1294
  }));
@@ -1256,8 +1298,8 @@ const TrimeshCollider = /*#__PURE__*/React.forwardRef((props, ref) => {
1256
1298
  * @category Colliders
1257
1299
  */
1258
1300
 
1259
- const ConeCollider = /*#__PURE__*/React.forwardRef((props, ref) => {
1260
- return /*#__PURE__*/React.createElement(AnyCollider, _extends({}, props, {
1301
+ const ConeCollider = /*#__PURE__*/React$1.forwardRef((props, ref) => {
1302
+ return /*#__PURE__*/React$1.createElement(AnyCollider, _extends({}, props, {
1261
1303
  shape: "cone",
1262
1304
  ref: ref
1263
1305
  }));
@@ -1267,8 +1309,8 @@ const ConeCollider = /*#__PURE__*/React.forwardRef((props, ref) => {
1267
1309
  * @category Colliders
1268
1310
  */
1269
1311
 
1270
- const CylinderCollider = /*#__PURE__*/React.forwardRef((props, ref) => {
1271
- return /*#__PURE__*/React.createElement(AnyCollider, _extends({}, props, {
1312
+ const CylinderCollider = /*#__PURE__*/React$1.forwardRef((props, ref) => {
1313
+ return /*#__PURE__*/React$1.createElement(AnyCollider, _extends({}, props, {
1272
1314
  shape: "cylinder",
1273
1315
  ref: ref
1274
1316
  }));
@@ -1278,8 +1320,8 @@ const CylinderCollider = /*#__PURE__*/React.forwardRef((props, ref) => {
1278
1320
  * @category Colliders
1279
1321
  */
1280
1322
 
1281
- const ConvexHullCollider = /*#__PURE__*/React.forwardRef((props, ref) => {
1282
- return /*#__PURE__*/React.createElement(AnyCollider, _extends({}, props, {
1323
+ const ConvexHullCollider = /*#__PURE__*/React$1.forwardRef((props, ref) => {
1324
+ return /*#__PURE__*/React$1.createElement(AnyCollider, _extends({}, props, {
1283
1325
  shape: "convexHull",
1284
1326
  ref: ref
1285
1327
  }));
@@ -1507,16 +1549,16 @@ const RigidBody = /*#__PURE__*/memo( /*#__PURE__*/forwardRef((props, forwardedRe
1507
1549
  options: mergedOptions
1508
1550
  };
1509
1551
  }, [mergedOptions]);
1510
- return /*#__PURE__*/React.createElement(RigidBodyContext.Provider, {
1552
+ return /*#__PURE__*/React$1.createElement(RigidBodyContext.Provider, {
1511
1553
  value: contextValue
1512
- }, /*#__PURE__*/React.createElement("object3D", _extends({
1554
+ }, /*#__PURE__*/React$1.createElement("object3D", _extends({
1513
1555
  ref: ref
1514
1556
  }, objectProps, {
1515
1557
  position: position,
1516
1558
  rotation: rotation,
1517
1559
  quaternion: quaternion,
1518
1560
  scale: scale
1519
- }), children, childColliderProps.map((colliderProps, index) => /*#__PURE__*/React.createElement(AnyCollider, _extends({
1561
+ }), children, childColliderProps.map((colliderProps, index) => /*#__PURE__*/React$1.createElement(AnyCollider, _extends({
1520
1562
  key: index
1521
1563
  }, colliderProps)))));
1522
1564
  }));
@@ -1545,12 +1587,12 @@ const MeshCollider = /*#__PURE__*/memo(props => {
1545
1587
  });
1546
1588
  }, [physicsOptions, options]);
1547
1589
  const childColliderProps = useChildColliderProps(object, mergedOptions, false);
1548
- return /*#__PURE__*/React.createElement("object3D", {
1590
+ return /*#__PURE__*/React$1.createElement("object3D", {
1549
1591
  ref: object,
1550
1592
  userData: {
1551
1593
  r3RapierType: "MeshCollider"
1552
1594
  }
1553
- }, children, childColliderProps.map((colliderProps, index) => /*#__PURE__*/React.createElement(AnyCollider, _extends({
1595
+ }, children, childColliderProps.map((colliderProps, index) => /*#__PURE__*/React$1.createElement(AnyCollider, _extends({
1554
1596
  key: index
1555
1597
  }, colliderProps))));
1556
1598
  });
@@ -1603,13 +1645,13 @@ const AttractorHelper = props => {
1603
1645
  (_normalsHelper$curren = normalsHelper.current) === null || _normalsHelper$curren === void 0 ? void 0 : _normalsHelper$curren.update();
1604
1646
  }
1605
1647
  });
1606
- return /*#__PURE__*/React.createElement("mesh", {
1648
+ return /*#__PURE__*/React$1.createElement("mesh", {
1607
1649
  ref: ref,
1608
1650
  position: props.object.position,
1609
1651
  frustumCulled: false
1610
- }, /*#__PURE__*/React.createElement("sphereGeometry", {
1652
+ }, /*#__PURE__*/React$1.createElement("sphereGeometry", {
1611
1653
  args: [0.2, 6, 6]
1612
- }), /*#__PURE__*/React.createElement("meshBasicMaterial", {
1654
+ }), /*#__PURE__*/React$1.createElement("meshBasicMaterial", {
1613
1655
  color: color,
1614
1656
  wireframe: true
1615
1657
  }));
@@ -1635,13 +1677,13 @@ const Debug = /*#__PURE__*/memo(() => {
1635
1677
  currMap.current = new Map(attractorStates);
1636
1678
  }
1637
1679
  });
1638
- return /*#__PURE__*/React.createElement("group", null, /*#__PURE__*/React.createElement("lineSegments", {
1680
+ return /*#__PURE__*/React$1.createElement("group", null, /*#__PURE__*/React$1.createElement("lineSegments", {
1639
1681
  ref: ref,
1640
1682
  frustumCulled: false
1641
- }, /*#__PURE__*/React.createElement("lineBasicMaterial", {
1683
+ }, /*#__PURE__*/React$1.createElement("lineBasicMaterial", {
1642
1684
  color: 0xffffff,
1643
1685
  vertexColors: true
1644
- }), /*#__PURE__*/React.createElement("bufferGeometry", null)), attractors.map((attractor, i) => /*#__PURE__*/React.createElement(AttractorHelper, _extends({
1686
+ }), /*#__PURE__*/React$1.createElement("bufferGeometry", null)), attractors.map((attractor, i) => /*#__PURE__*/React$1.createElement(AttractorHelper, _extends({
1645
1687
  key: attractor.object.uuid
1646
1688
  }, attractor))));
1647
1689
  });
@@ -1710,21 +1752,21 @@ const InstancedRigidBodies = /*#__PURE__*/memo( /*#__PURE__*/forwardRef((props,
1710
1752
  return state;
1711
1753
  };
1712
1754
 
1713
- return /*#__PURE__*/React.createElement("object3D", _extends({
1755
+ return /*#__PURE__*/React$1.createElement("object3D", _extends({
1714
1756
  ref: object
1715
1757
  }, rigidBodyProps, {
1716
1758
  position: position,
1717
1759
  rotation: rotation,
1718
1760
  quaternion: quaternion,
1719
1761
  scale: scale
1720
- }), /*#__PURE__*/React.createElement("object3D", {
1762
+ }), /*#__PURE__*/React$1.createElement("object3D", {
1721
1763
  ref: instancedWrapper
1722
- }, children), instances === null || instances === void 0 ? void 0 : instances.map((instance, index) => /*#__PURE__*/React.createElement(RigidBody, _extends({}, rigidBodyProps, instance, {
1764
+ }, children), instances === null || instances === void 0 ? void 0 : instances.map((instance, index) => /*#__PURE__*/React$1.createElement(RigidBody, _extends({}, rigidBodyProps, instance, {
1723
1765
  ref: body => rigidBodyApis.current[index] = body,
1724
1766
  transformState: state => applyInstancedState(state, index)
1725
- }), /*#__PURE__*/React.createElement(React.Fragment, null, colliderNodes.map((node, index) => /*#__PURE__*/React.createElement(Fragment, {
1767
+ }), /*#__PURE__*/React$1.createElement(React$1.Fragment, null, colliderNodes.map((node, index) => /*#__PURE__*/React$1.createElement(Fragment, {
1726
1768
  key: index
1727
- }, node)), childColliderProps.map((colliderProps, colliderIndex) => /*#__PURE__*/React.createElement(AnyCollider, _extends({
1769
+ }, node)), childColliderProps.map((colliderProps, colliderIndex) => /*#__PURE__*/React$1.createElement(AnyCollider, _extends({
1728
1770
  key: colliderIndex
1729
1771
  }, colliderProps)))))));
1730
1772
  }));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@react-three/rapier",
3
- "version": "0.14.0-rc.1",
3
+ "version": "0.14.0-rc.2",
4
4
  "source": "src/index.ts",
5
5
  "main": "dist/react-three-rapier.cjs.js",
6
6
  "module": "dist/react-three-rapier.esm.js",
package/readme.md CHANGED
@@ -838,4 +838,14 @@ step(1 / 60);
838
838
  ```
839
839
 
840
840
  ### On-demand rendering
841
- `@react-three/rapier` runs the physics simulation independently from the render loop, and will tell `@react-three/fiber` to render if the scene has active (non-sleeping) RigidBodies. This allows you to use the `<Canvas frameloop="demand" />` (https://docs.pmnd.rs/react-three-fiber/advanced/scaling-performance#on-demand-rendering) strategy to only render the scene when needed.
841
+ By default `@react-three/rapier` will update the physics simulation when a frame renders. This is fine for most cases, but if you want to only render the scene when things have changed, you need to run the physics simulation independently from the render loop.
842
+
843
+ - See https://docs.pmnd.rs/react-three-fiber/advanced/scaling-performance#on-demand-rendering, for info on on-demand rendering in `@react-tree/fiber`.
844
+
845
+ Setting `<Physics updateLoop="independent" />` will make the physics simulation run in it's own `requestAnimationFrame` loop, and call `invalidate` on the canvas only when there are active (moving) bodies.
846
+
847
+ ```tsx
848
+ <Canvas frameloop="demand">
849
+ <Physics updateLoop="independent">...</Physics>
850
+ </Canvas>
851
+ ```