@react-three/rapier 0.7.7 → 0.8.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/declarations/src/Debug.d.ts +0 -8
- package/dist/declarations/src/api.d.ts +2 -1
- package/dist/declarations/src/types.d.ts +4 -0
- package/dist/react-three-rapier.cjs.dev.js +24 -169
- package/dist/react-three-rapier.cjs.prod.js +24 -169
- package/dist/react-three-rapier.esm.js +27 -172
- package/package.json +4 -1
- package/readme.md +12 -7
@@ -1,13 +1,5 @@
|
|
1
1
|
import { FC } from "react";
|
2
2
|
interface DebugProps {
|
3
|
-
/**
|
4
|
-
* The color of the wireframe representing an active collider that is affected by forces and not sleeping.
|
5
|
-
*/
|
6
|
-
color?: string;
|
7
|
-
/**
|
8
|
-
* The color of the wireframe representing a static (fixed or kinematic) or sleeping collider.
|
9
|
-
*/
|
10
|
-
sleepColor?: string;
|
11
3
|
}
|
12
4
|
export declare const Debug: FC<DebugProps>;
|
13
5
|
export {};
|
@@ -1,4 +1,4 @@
|
|
1
|
-
import { Collider, ColliderDesc, ImpulseJoint, JointData, RigidBody, RigidBodyDesc, World } from "@dimforge/rapier3d-compat";
|
1
|
+
import { Collider, ColliderDesc, DebugRenderBuffers, ImpulseJoint, JointData, RigidBody, RigidBodyDesc, World } from "@dimforge/rapier3d-compat";
|
2
2
|
import { Quaternion, Vector3 } from "three";
|
3
3
|
import { RefGetter } from "./types";
|
4
4
|
declare type Vector3Object = {
|
@@ -160,6 +160,7 @@ export interface WorldApi {
|
|
160
160
|
removeImpulseJoint(joint: ImpulseJoint, wakeUp?: boolean): void;
|
161
161
|
forEachCollider(callback: (collider: Collider) => void): void;
|
162
162
|
setGravity(gravity: Vector3): void;
|
163
|
+
debugRender(): DebugRenderBuffers;
|
163
164
|
}
|
164
165
|
export declare const createWorldApi: (ref: RefGetter<World>) => WorldApi;
|
165
166
|
export declare const createJointApi: (ref: RefGetter<ImpulseJoint>) => {
|
@@ -287,6 +287,10 @@ export interface UseRigidBodyOptions extends ColliderProps {
|
|
287
287
|
* Passed down to the object3d representing this collider.
|
288
288
|
*/
|
289
289
|
userData?: Object3DProps["userData"];
|
290
|
+
/**
|
291
|
+
* Include invisible objects on the collider creation estimation.
|
292
|
+
*/
|
293
|
+
includeInvisible?: boolean;
|
290
294
|
}
|
291
295
|
export declare type SphericalJointParams = [
|
292
296
|
body1Anchor: Vector3Array,
|
@@ -220,7 +220,8 @@ const createWorldApi = ref => {
|
|
220
220
|
x,
|
221
221
|
y,
|
222
222
|
z
|
223
|
-
}
|
223
|
+
},
|
224
|
+
debugRender: () => ref.current().debugRender()
|
224
225
|
};
|
225
226
|
}; // TODO: Broken currently, waiting for Rapier3D to fix
|
226
227
|
|
@@ -910,7 +911,8 @@ const createColliderPropsFromChildren = ({
|
|
910
911
|
const colliderProps = [];
|
911
912
|
object.updateWorldMatrix(true, false);
|
912
913
|
const invertedParentMatrixWorld = object.matrixWorld.clone().invert();
|
913
|
-
|
914
|
+
|
915
|
+
const colliderFromChild = child => {
|
914
916
|
if ("isMesh" in child) {
|
915
917
|
if (_ignoreMeshColliders && isChildOfMeshCollider(child)) return;
|
916
918
|
const worldScale = child.getWorldScale(_scale);
|
@@ -935,7 +937,14 @@ const createColliderPropsFromChildren = ({
|
|
935
937
|
scale: [worldScale.x, worldScale.y, worldScale.z]
|
936
938
|
}));
|
937
939
|
}
|
938
|
-
}
|
940
|
+
};
|
941
|
+
|
942
|
+
if (options.includeInvisible) {
|
943
|
+
object.traverse(colliderFromChild);
|
944
|
+
} else {
|
945
|
+
object.traverseVisible(colliderFromChild);
|
946
|
+
}
|
947
|
+
|
939
948
|
return colliderProps;
|
940
949
|
};
|
941
950
|
const getColliderArgsFromGeometry = (geometry, colliders) => {
|
@@ -1369,178 +1378,24 @@ const MeshCollider = props => {
|
|
1369
1378
|
}, colliderProps))));
|
1370
1379
|
};
|
1371
1380
|
|
1372
|
-
const
|
1373
|
-
switch (collider.shape.type) {
|
1374
|
-
case rapier3dCompat.ShapeType.Cuboid:
|
1375
|
-
{
|
1376
|
-
const {
|
1377
|
-
x,
|
1378
|
-
y,
|
1379
|
-
z
|
1380
|
-
} = collider.shape.halfExtents;
|
1381
|
-
return new three.BoxGeometry(x * 2 + 0.01, y * 2 + 0.01, z * 2 + 0.01);
|
1382
|
-
}
|
1383
|
-
|
1384
|
-
case rapier3dCompat.ShapeType.RoundCuboid:
|
1385
|
-
{
|
1386
|
-
const {
|
1387
|
-
x,
|
1388
|
-
y,
|
1389
|
-
z
|
1390
|
-
} = collider.shape.halfExtents;
|
1391
|
-
const radius = collider.shape.borderRadius;
|
1392
|
-
return new threeStdlib.RoundedBoxGeometry(x * 2 + radius * 2, y * 2 + radius * 2, z * 2 + radius * 2, 8, radius);
|
1393
|
-
}
|
1394
|
-
|
1395
|
-
case rapier3dCompat.ShapeType.Ball:
|
1396
|
-
{
|
1397
|
-
const r = collider.shape.radius;
|
1398
|
-
return new three.SphereGeometry(r + +0.01, 8, 8);
|
1399
|
-
}
|
1400
|
-
|
1401
|
-
case rapier3dCompat.ShapeType.TriMesh:
|
1402
|
-
{
|
1403
|
-
var _g$index;
|
1404
|
-
|
1405
|
-
const v = collider.shape.vertices;
|
1406
|
-
const i = collider.shape.indices;
|
1407
|
-
const g = new three.BufferGeometry(); // Vertices are not always a float3darray (???), so we need to convert them
|
1408
|
-
|
1409
|
-
const safeVerts = Float32Array.from(v);
|
1410
|
-
g.setAttribute("position", new three.BufferAttribute(safeVerts, 3));
|
1411
|
-
(_g$index = g.index) === null || _g$index === void 0 ? void 0 : _g$index.set(i);
|
1412
|
-
g.setDrawRange(0, g.attributes.position.array.length / 3 - 1);
|
1413
|
-
return g;
|
1414
|
-
}
|
1415
|
-
|
1416
|
-
case rapier3dCompat.ShapeType.ConvexPolyhedron:
|
1417
|
-
{
|
1418
|
-
const cv = collider.shape.vertices; // Vertices are not always a float3darray (???), so we need to convert them
|
1419
|
-
|
1420
|
-
const safeVerts = Float32Array.from(cv);
|
1421
|
-
const cg = new three.BufferGeometry();
|
1422
|
-
cg.setAttribute("position", new three.BufferAttribute(safeVerts, 3));
|
1423
|
-
return cg;
|
1424
|
-
}
|
1425
|
-
|
1426
|
-
case rapier3dCompat.ShapeType.Cylinder:
|
1427
|
-
{
|
1428
|
-
const r = collider.shape.radius;
|
1429
|
-
const h = collider.shape.halfHeight;
|
1430
|
-
const g = new three.CylinderGeometry(r, r, h * 2);
|
1431
|
-
return g;
|
1432
|
-
}
|
1433
|
-
|
1434
|
-
case rapier3dCompat.ShapeType.Capsule:
|
1435
|
-
{
|
1436
|
-
const r = collider.shape.radius;
|
1437
|
-
const h = collider.shape.halfHeight;
|
1438
|
-
const g = new three.CapsuleGeometry(r, h * 2, 4, 8);
|
1439
|
-
return g;
|
1440
|
-
}
|
1441
|
-
|
1442
|
-
case rapier3dCompat.ShapeType.Cone:
|
1443
|
-
{
|
1444
|
-
const r = collider.shape.radius;
|
1445
|
-
const h = collider.shape.halfHeight;
|
1446
|
-
const g = new three.ConeGeometry(r, h * 2, 16);
|
1447
|
-
return g;
|
1448
|
-
}
|
1449
|
-
|
1450
|
-
case rapier3dCompat.ShapeType.HeightField:
|
1451
|
-
{
|
1452
|
-
const rows = collider.shape.nrows;
|
1453
|
-
const cols = collider.shape.ncols;
|
1454
|
-
const heights = collider.shape.heights;
|
1455
|
-
const scale = collider.shape.scale;
|
1456
|
-
const g = new three.PlaneGeometry(scale.x, scale.z, cols, rows);
|
1457
|
-
const verts = g.attributes.position.array;
|
1458
|
-
verts.forEach((v, index) => verts[index * 3 + 2] = heights[index] * scale.y);
|
1459
|
-
g.scale(1, -1, 1);
|
1460
|
-
g.rotateX(-Math.PI / 2);
|
1461
|
-
g.rotateY(-Math.PI / 2);
|
1462
|
-
return g;
|
1463
|
-
}
|
1464
|
-
}
|
1465
|
-
|
1466
|
-
return new three.BoxGeometry(1, 1, 1);
|
1467
|
-
};
|
1468
|
-
|
1469
|
-
const DebugShape = /*#__PURE__*/React.memo(({
|
1470
|
-
colliderHandle,
|
1471
|
-
color,
|
1472
|
-
sleepColor
|
1473
|
-
}) => {
|
1381
|
+
const Debug = () => {
|
1474
1382
|
const {
|
1475
1383
|
world
|
1476
1384
|
} = useRapier();
|
1477
1385
|
const ref = React.useRef(null);
|
1478
|
-
const [material] = React.useState(new three.MeshBasicMaterial({
|
1479
|
-
color,
|
1480
|
-
wireframe: true
|
1481
|
-
}));
|
1482
|
-
fiber.useFrame(() => {
|
1483
|
-
const collider = world.getCollider(colliderHandle);
|
1484
|
-
|
1485
|
-
if (ref.current && collider) {
|
1486
|
-
const {
|
1487
|
-
x: rx,
|
1488
|
-
y: ry,
|
1489
|
-
z: rz,
|
1490
|
-
w: rw
|
1491
|
-
} = collider.rotation();
|
1492
|
-
const {
|
1493
|
-
x,
|
1494
|
-
y,
|
1495
|
-
z
|
1496
|
-
} = collider.translation();
|
1497
|
-
const parent = collider.parent();
|
1498
|
-
|
1499
|
-
if (parent !== null && parent !== void 0 && parent.isSleeping() || parent !== null && parent !== void 0 && parent.isFixed() || parent !== null && parent !== void 0 && parent.isKinematic()) {
|
1500
|
-
material.color = new three.Color(sleepColor);
|
1501
|
-
} else {
|
1502
|
-
material.color = new three.Color(color);
|
1503
|
-
}
|
1504
|
-
|
1505
|
-
ref.current.position.set(x, y, z);
|
1506
|
-
ref.current.rotation.setFromQuaternion(new three.Quaternion(rx, ry, rz, rw));
|
1507
|
-
}
|
1508
|
-
});
|
1509
|
-
const geometry = React.useMemo(() => {
|
1510
|
-
const collider = world.getCollider(colliderHandle);
|
1511
|
-
return geometryFromCollider(collider);
|
1512
|
-
}, [colliderHandle]);
|
1513
|
-
return /*#__PURE__*/React__default["default"].createElement("mesh", {
|
1514
|
-
ref: ref,
|
1515
|
-
material: material
|
1516
|
-
}, /*#__PURE__*/React__default["default"].createElement("primitive", {
|
1517
|
-
object: geometry,
|
1518
|
-
attach: "geometry"
|
1519
|
-
}));
|
1520
|
-
});
|
1521
|
-
const Debug = ({
|
1522
|
-
color: _color = "red",
|
1523
|
-
sleepColor: _sleepColor = "blue"
|
1524
|
-
}) => {
|
1525
|
-
const {
|
1526
|
-
world
|
1527
|
-
} = useRapier();
|
1528
|
-
const [colliders, setColliders] = React.useState([]);
|
1529
|
-
React.useRef({});
|
1530
|
-
|
1531
1386
|
fiber.useFrame(() => {
|
1532
|
-
const
|
1533
|
-
|
1534
|
-
|
1535
|
-
|
1536
|
-
|
1387
|
+
const mesh = ref.current;
|
1388
|
+
if (!mesh) return;
|
1389
|
+
const buffers = world.debugRender();
|
1390
|
+
mesh.geometry.setAttribute("position", new three.BufferAttribute(buffers.vertices, 3));
|
1391
|
+
mesh.geometry.setAttribute("color", new three.BufferAttribute(buffers.colors, 4));
|
1537
1392
|
});
|
1538
|
-
return /*#__PURE__*/React__default["default"].createElement("
|
1539
|
-
|
1540
|
-
|
1541
|
-
color:
|
1542
|
-
|
1543
|
-
})));
|
1393
|
+
return /*#__PURE__*/React__default["default"].createElement("lineSegments", {
|
1394
|
+
ref: ref
|
1395
|
+
}, /*#__PURE__*/React__default["default"].createElement("lineBasicMaterial", {
|
1396
|
+
color: 0xffffff,
|
1397
|
+
vertexColors: true
|
1398
|
+
}), /*#__PURE__*/React__default["default"].createElement("bufferGeometry", null));
|
1544
1399
|
};
|
1545
1400
|
|
1546
1401
|
const _excluded = ["positions", "rotations", "children"];
|
@@ -220,7 +220,8 @@ const createWorldApi = ref => {
|
|
220
220
|
x,
|
221
221
|
y,
|
222
222
|
z
|
223
|
-
}
|
223
|
+
},
|
224
|
+
debugRender: () => ref.current().debugRender()
|
224
225
|
};
|
225
226
|
}; // TODO: Broken currently, waiting for Rapier3D to fix
|
226
227
|
|
@@ -910,7 +911,8 @@ const createColliderPropsFromChildren = ({
|
|
910
911
|
const colliderProps = [];
|
911
912
|
object.updateWorldMatrix(true, false);
|
912
913
|
const invertedParentMatrixWorld = object.matrixWorld.clone().invert();
|
913
|
-
|
914
|
+
|
915
|
+
const colliderFromChild = child => {
|
914
916
|
if ("isMesh" in child) {
|
915
917
|
if (_ignoreMeshColliders && isChildOfMeshCollider(child)) return;
|
916
918
|
const worldScale = child.getWorldScale(_scale);
|
@@ -935,7 +937,14 @@ const createColliderPropsFromChildren = ({
|
|
935
937
|
scale: [worldScale.x, worldScale.y, worldScale.z]
|
936
938
|
}));
|
937
939
|
}
|
938
|
-
}
|
940
|
+
};
|
941
|
+
|
942
|
+
if (options.includeInvisible) {
|
943
|
+
object.traverse(colliderFromChild);
|
944
|
+
} else {
|
945
|
+
object.traverseVisible(colliderFromChild);
|
946
|
+
}
|
947
|
+
|
939
948
|
return colliderProps;
|
940
949
|
};
|
941
950
|
const getColliderArgsFromGeometry = (geometry, colliders) => {
|
@@ -1369,178 +1378,24 @@ const MeshCollider = props => {
|
|
1369
1378
|
}, colliderProps))));
|
1370
1379
|
};
|
1371
1380
|
|
1372
|
-
const
|
1373
|
-
switch (collider.shape.type) {
|
1374
|
-
case rapier3dCompat.ShapeType.Cuboid:
|
1375
|
-
{
|
1376
|
-
const {
|
1377
|
-
x,
|
1378
|
-
y,
|
1379
|
-
z
|
1380
|
-
} = collider.shape.halfExtents;
|
1381
|
-
return new three.BoxGeometry(x * 2 + 0.01, y * 2 + 0.01, z * 2 + 0.01);
|
1382
|
-
}
|
1383
|
-
|
1384
|
-
case rapier3dCompat.ShapeType.RoundCuboid:
|
1385
|
-
{
|
1386
|
-
const {
|
1387
|
-
x,
|
1388
|
-
y,
|
1389
|
-
z
|
1390
|
-
} = collider.shape.halfExtents;
|
1391
|
-
const radius = collider.shape.borderRadius;
|
1392
|
-
return new threeStdlib.RoundedBoxGeometry(x * 2 + radius * 2, y * 2 + radius * 2, z * 2 + radius * 2, 8, radius);
|
1393
|
-
}
|
1394
|
-
|
1395
|
-
case rapier3dCompat.ShapeType.Ball:
|
1396
|
-
{
|
1397
|
-
const r = collider.shape.radius;
|
1398
|
-
return new three.SphereGeometry(r + +0.01, 8, 8);
|
1399
|
-
}
|
1400
|
-
|
1401
|
-
case rapier3dCompat.ShapeType.TriMesh:
|
1402
|
-
{
|
1403
|
-
var _g$index;
|
1404
|
-
|
1405
|
-
const v = collider.shape.vertices;
|
1406
|
-
const i = collider.shape.indices;
|
1407
|
-
const g = new three.BufferGeometry(); // Vertices are not always a float3darray (???), so we need to convert them
|
1408
|
-
|
1409
|
-
const safeVerts = Float32Array.from(v);
|
1410
|
-
g.setAttribute("position", new three.BufferAttribute(safeVerts, 3));
|
1411
|
-
(_g$index = g.index) === null || _g$index === void 0 ? void 0 : _g$index.set(i);
|
1412
|
-
g.setDrawRange(0, g.attributes.position.array.length / 3 - 1);
|
1413
|
-
return g;
|
1414
|
-
}
|
1415
|
-
|
1416
|
-
case rapier3dCompat.ShapeType.ConvexPolyhedron:
|
1417
|
-
{
|
1418
|
-
const cv = collider.shape.vertices; // Vertices are not always a float3darray (???), so we need to convert them
|
1419
|
-
|
1420
|
-
const safeVerts = Float32Array.from(cv);
|
1421
|
-
const cg = new three.BufferGeometry();
|
1422
|
-
cg.setAttribute("position", new three.BufferAttribute(safeVerts, 3));
|
1423
|
-
return cg;
|
1424
|
-
}
|
1425
|
-
|
1426
|
-
case rapier3dCompat.ShapeType.Cylinder:
|
1427
|
-
{
|
1428
|
-
const r = collider.shape.radius;
|
1429
|
-
const h = collider.shape.halfHeight;
|
1430
|
-
const g = new three.CylinderGeometry(r, r, h * 2);
|
1431
|
-
return g;
|
1432
|
-
}
|
1433
|
-
|
1434
|
-
case rapier3dCompat.ShapeType.Capsule:
|
1435
|
-
{
|
1436
|
-
const r = collider.shape.radius;
|
1437
|
-
const h = collider.shape.halfHeight;
|
1438
|
-
const g = new three.CapsuleGeometry(r, h * 2, 4, 8);
|
1439
|
-
return g;
|
1440
|
-
}
|
1441
|
-
|
1442
|
-
case rapier3dCompat.ShapeType.Cone:
|
1443
|
-
{
|
1444
|
-
const r = collider.shape.radius;
|
1445
|
-
const h = collider.shape.halfHeight;
|
1446
|
-
const g = new three.ConeGeometry(r, h * 2, 16);
|
1447
|
-
return g;
|
1448
|
-
}
|
1449
|
-
|
1450
|
-
case rapier3dCompat.ShapeType.HeightField:
|
1451
|
-
{
|
1452
|
-
const rows = collider.shape.nrows;
|
1453
|
-
const cols = collider.shape.ncols;
|
1454
|
-
const heights = collider.shape.heights;
|
1455
|
-
const scale = collider.shape.scale;
|
1456
|
-
const g = new three.PlaneGeometry(scale.x, scale.z, cols, rows);
|
1457
|
-
const verts = g.attributes.position.array;
|
1458
|
-
verts.forEach((v, index) => verts[index * 3 + 2] = heights[index] * scale.y);
|
1459
|
-
g.scale(1, -1, 1);
|
1460
|
-
g.rotateX(-Math.PI / 2);
|
1461
|
-
g.rotateY(-Math.PI / 2);
|
1462
|
-
return g;
|
1463
|
-
}
|
1464
|
-
}
|
1465
|
-
|
1466
|
-
return new three.BoxGeometry(1, 1, 1);
|
1467
|
-
};
|
1468
|
-
|
1469
|
-
const DebugShape = /*#__PURE__*/React.memo(({
|
1470
|
-
colliderHandle,
|
1471
|
-
color,
|
1472
|
-
sleepColor
|
1473
|
-
}) => {
|
1381
|
+
const Debug = () => {
|
1474
1382
|
const {
|
1475
1383
|
world
|
1476
1384
|
} = useRapier();
|
1477
1385
|
const ref = React.useRef(null);
|
1478
|
-
const [material] = React.useState(new three.MeshBasicMaterial({
|
1479
|
-
color,
|
1480
|
-
wireframe: true
|
1481
|
-
}));
|
1482
|
-
fiber.useFrame(() => {
|
1483
|
-
const collider = world.getCollider(colliderHandle);
|
1484
|
-
|
1485
|
-
if (ref.current && collider) {
|
1486
|
-
const {
|
1487
|
-
x: rx,
|
1488
|
-
y: ry,
|
1489
|
-
z: rz,
|
1490
|
-
w: rw
|
1491
|
-
} = collider.rotation();
|
1492
|
-
const {
|
1493
|
-
x,
|
1494
|
-
y,
|
1495
|
-
z
|
1496
|
-
} = collider.translation();
|
1497
|
-
const parent = collider.parent();
|
1498
|
-
|
1499
|
-
if (parent !== null && parent !== void 0 && parent.isSleeping() || parent !== null && parent !== void 0 && parent.isFixed() || parent !== null && parent !== void 0 && parent.isKinematic()) {
|
1500
|
-
material.color = new three.Color(sleepColor);
|
1501
|
-
} else {
|
1502
|
-
material.color = new three.Color(color);
|
1503
|
-
}
|
1504
|
-
|
1505
|
-
ref.current.position.set(x, y, z);
|
1506
|
-
ref.current.rotation.setFromQuaternion(new three.Quaternion(rx, ry, rz, rw));
|
1507
|
-
}
|
1508
|
-
});
|
1509
|
-
const geometry = React.useMemo(() => {
|
1510
|
-
const collider = world.getCollider(colliderHandle);
|
1511
|
-
return geometryFromCollider(collider);
|
1512
|
-
}, [colliderHandle]);
|
1513
|
-
return /*#__PURE__*/React__default["default"].createElement("mesh", {
|
1514
|
-
ref: ref,
|
1515
|
-
material: material
|
1516
|
-
}, /*#__PURE__*/React__default["default"].createElement("primitive", {
|
1517
|
-
object: geometry,
|
1518
|
-
attach: "geometry"
|
1519
|
-
}));
|
1520
|
-
});
|
1521
|
-
const Debug = ({
|
1522
|
-
color: _color = "red",
|
1523
|
-
sleepColor: _sleepColor = "blue"
|
1524
|
-
}) => {
|
1525
|
-
const {
|
1526
|
-
world
|
1527
|
-
} = useRapier();
|
1528
|
-
const [colliders, setColliders] = React.useState([]);
|
1529
|
-
React.useRef({});
|
1530
|
-
|
1531
1386
|
fiber.useFrame(() => {
|
1532
|
-
const
|
1533
|
-
|
1534
|
-
|
1535
|
-
|
1536
|
-
|
1387
|
+
const mesh = ref.current;
|
1388
|
+
if (!mesh) return;
|
1389
|
+
const buffers = world.debugRender();
|
1390
|
+
mesh.geometry.setAttribute("position", new three.BufferAttribute(buffers.vertices, 3));
|
1391
|
+
mesh.geometry.setAttribute("color", new three.BufferAttribute(buffers.colors, 4));
|
1537
1392
|
});
|
1538
|
-
return /*#__PURE__*/React__default["default"].createElement("
|
1539
|
-
|
1540
|
-
|
1541
|
-
color:
|
1542
|
-
|
1543
|
-
})));
|
1393
|
+
return /*#__PURE__*/React__default["default"].createElement("lineSegments", {
|
1394
|
+
ref: ref
|
1395
|
+
}, /*#__PURE__*/React__default["default"].createElement("lineBasicMaterial", {
|
1396
|
+
color: 0xffffff,
|
1397
|
+
vertexColors: true
|
1398
|
+
}), /*#__PURE__*/React__default["default"].createElement("bufferGeometry", null));
|
1544
1399
|
};
|
1545
1400
|
|
1546
1401
|
const _excluded = ["positions", "rotations", "children"];
|
@@ -1,10 +1,10 @@
|
|
1
|
-
import { EventQueue, RigidBodyDesc, ColliderDesc, ActiveEvents
|
1
|
+
import { EventQueue, RigidBodyDesc, ColliderDesc, ActiveEvents } from '@dimforge/rapier3d-compat';
|
2
2
|
export { CoefficientCombineRule, Collider as RapierCollider, RigidBody as RapierRigidBody } from '@dimforge/rapier3d-compat';
|
3
3
|
import React, { useRef, useState, useEffect, useMemo, createContext, useContext, memo, forwardRef, useImperativeHandle, useLayoutEffect } from 'react';
|
4
4
|
import { useAsset } from 'use-asset';
|
5
5
|
import { useFrame } from '@react-three/fiber';
|
6
|
-
import { Quaternion, Euler, Vector3, Object3D, Matrix4, MathUtils, InstancedMesh,
|
7
|
-
import { mergeVertices
|
6
|
+
import { Quaternion, Euler, Vector3, Object3D, Matrix4, MathUtils, InstancedMesh, BufferAttribute, DynamicDrawUsage } from 'three';
|
7
|
+
import { mergeVertices } from 'three-stdlib';
|
8
8
|
|
9
9
|
const _quaternion = new Quaternion();
|
10
10
|
new Euler();
|
@@ -195,7 +195,8 @@ const createWorldApi = ref => {
|
|
195
195
|
x,
|
196
196
|
y,
|
197
197
|
z
|
198
|
-
}
|
198
|
+
},
|
199
|
+
debugRender: () => ref.current().debugRender()
|
199
200
|
};
|
200
201
|
}; // TODO: Broken currently, waiting for Rapier3D to fix
|
201
202
|
|
@@ -885,7 +886,8 @@ const createColliderPropsFromChildren = ({
|
|
885
886
|
const colliderProps = [];
|
886
887
|
object.updateWorldMatrix(true, false);
|
887
888
|
const invertedParentMatrixWorld = object.matrixWorld.clone().invert();
|
888
|
-
|
889
|
+
|
890
|
+
const colliderFromChild = child => {
|
889
891
|
if ("isMesh" in child) {
|
890
892
|
if (_ignoreMeshColliders && isChildOfMeshCollider(child)) return;
|
891
893
|
const worldScale = child.getWorldScale(_scale);
|
@@ -910,7 +912,14 @@ const createColliderPropsFromChildren = ({
|
|
910
912
|
scale: [worldScale.x, worldScale.y, worldScale.z]
|
911
913
|
}));
|
912
914
|
}
|
913
|
-
}
|
915
|
+
};
|
916
|
+
|
917
|
+
if (options.includeInvisible) {
|
918
|
+
object.traverse(colliderFromChild);
|
919
|
+
} else {
|
920
|
+
object.traverseVisible(colliderFromChild);
|
921
|
+
}
|
922
|
+
|
914
923
|
return colliderProps;
|
915
924
|
};
|
916
925
|
const getColliderArgsFromGeometry = (geometry, colliders) => {
|
@@ -1344,178 +1353,24 @@ const MeshCollider = props => {
|
|
1344
1353
|
}, colliderProps))));
|
1345
1354
|
};
|
1346
1355
|
|
1347
|
-
const
|
1348
|
-
switch (collider.shape.type) {
|
1349
|
-
case ShapeType.Cuboid:
|
1350
|
-
{
|
1351
|
-
const {
|
1352
|
-
x,
|
1353
|
-
y,
|
1354
|
-
z
|
1355
|
-
} = collider.shape.halfExtents;
|
1356
|
-
return new BoxGeometry(x * 2 + 0.01, y * 2 + 0.01, z * 2 + 0.01);
|
1357
|
-
}
|
1358
|
-
|
1359
|
-
case ShapeType.RoundCuboid:
|
1360
|
-
{
|
1361
|
-
const {
|
1362
|
-
x,
|
1363
|
-
y,
|
1364
|
-
z
|
1365
|
-
} = collider.shape.halfExtents;
|
1366
|
-
const radius = collider.shape.borderRadius;
|
1367
|
-
return new RoundedBoxGeometry(x * 2 + radius * 2, y * 2 + radius * 2, z * 2 + radius * 2, 8, radius);
|
1368
|
-
}
|
1369
|
-
|
1370
|
-
case ShapeType.Ball:
|
1371
|
-
{
|
1372
|
-
const r = collider.shape.radius;
|
1373
|
-
return new SphereGeometry(r + +0.01, 8, 8);
|
1374
|
-
}
|
1375
|
-
|
1376
|
-
case ShapeType.TriMesh:
|
1377
|
-
{
|
1378
|
-
var _g$index;
|
1379
|
-
|
1380
|
-
const v = collider.shape.vertices;
|
1381
|
-
const i = collider.shape.indices;
|
1382
|
-
const g = new BufferGeometry(); // Vertices are not always a float3darray (???), so we need to convert them
|
1383
|
-
|
1384
|
-
const safeVerts = Float32Array.from(v);
|
1385
|
-
g.setAttribute("position", new BufferAttribute(safeVerts, 3));
|
1386
|
-
(_g$index = g.index) === null || _g$index === void 0 ? void 0 : _g$index.set(i);
|
1387
|
-
g.setDrawRange(0, g.attributes.position.array.length / 3 - 1);
|
1388
|
-
return g;
|
1389
|
-
}
|
1390
|
-
|
1391
|
-
case ShapeType.ConvexPolyhedron:
|
1392
|
-
{
|
1393
|
-
const cv = collider.shape.vertices; // Vertices are not always a float3darray (???), so we need to convert them
|
1394
|
-
|
1395
|
-
const safeVerts = Float32Array.from(cv);
|
1396
|
-
const cg = new BufferGeometry();
|
1397
|
-
cg.setAttribute("position", new BufferAttribute(safeVerts, 3));
|
1398
|
-
return cg;
|
1399
|
-
}
|
1400
|
-
|
1401
|
-
case ShapeType.Cylinder:
|
1402
|
-
{
|
1403
|
-
const r = collider.shape.radius;
|
1404
|
-
const h = collider.shape.halfHeight;
|
1405
|
-
const g = new CylinderGeometry(r, r, h * 2);
|
1406
|
-
return g;
|
1407
|
-
}
|
1408
|
-
|
1409
|
-
case ShapeType.Capsule:
|
1410
|
-
{
|
1411
|
-
const r = collider.shape.radius;
|
1412
|
-
const h = collider.shape.halfHeight;
|
1413
|
-
const g = new CapsuleGeometry(r, h * 2, 4, 8);
|
1414
|
-
return g;
|
1415
|
-
}
|
1416
|
-
|
1417
|
-
case ShapeType.Cone:
|
1418
|
-
{
|
1419
|
-
const r = collider.shape.radius;
|
1420
|
-
const h = collider.shape.halfHeight;
|
1421
|
-
const g = new ConeGeometry(r, h * 2, 16);
|
1422
|
-
return g;
|
1423
|
-
}
|
1424
|
-
|
1425
|
-
case ShapeType.HeightField:
|
1426
|
-
{
|
1427
|
-
const rows = collider.shape.nrows;
|
1428
|
-
const cols = collider.shape.ncols;
|
1429
|
-
const heights = collider.shape.heights;
|
1430
|
-
const scale = collider.shape.scale;
|
1431
|
-
const g = new PlaneGeometry(scale.x, scale.z, cols, rows);
|
1432
|
-
const verts = g.attributes.position.array;
|
1433
|
-
verts.forEach((v, index) => verts[index * 3 + 2] = heights[index] * scale.y);
|
1434
|
-
g.scale(1, -1, 1);
|
1435
|
-
g.rotateX(-Math.PI / 2);
|
1436
|
-
g.rotateY(-Math.PI / 2);
|
1437
|
-
return g;
|
1438
|
-
}
|
1439
|
-
}
|
1440
|
-
|
1441
|
-
return new BoxGeometry(1, 1, 1);
|
1442
|
-
};
|
1443
|
-
|
1444
|
-
const DebugShape = /*#__PURE__*/memo(({
|
1445
|
-
colliderHandle,
|
1446
|
-
color,
|
1447
|
-
sleepColor
|
1448
|
-
}) => {
|
1356
|
+
const Debug = () => {
|
1449
1357
|
const {
|
1450
1358
|
world
|
1451
1359
|
} = useRapier();
|
1452
1360
|
const ref = useRef(null);
|
1453
|
-
const [material] = useState(new MeshBasicMaterial({
|
1454
|
-
color,
|
1455
|
-
wireframe: true
|
1456
|
-
}));
|
1457
|
-
useFrame(() => {
|
1458
|
-
const collider = world.getCollider(colliderHandle);
|
1459
|
-
|
1460
|
-
if (ref.current && collider) {
|
1461
|
-
const {
|
1462
|
-
x: rx,
|
1463
|
-
y: ry,
|
1464
|
-
z: rz,
|
1465
|
-
w: rw
|
1466
|
-
} = collider.rotation();
|
1467
|
-
const {
|
1468
|
-
x,
|
1469
|
-
y,
|
1470
|
-
z
|
1471
|
-
} = collider.translation();
|
1472
|
-
const parent = collider.parent();
|
1473
|
-
|
1474
|
-
if (parent !== null && parent !== void 0 && parent.isSleeping() || parent !== null && parent !== void 0 && parent.isFixed() || parent !== null && parent !== void 0 && parent.isKinematic()) {
|
1475
|
-
material.color = new Color(sleepColor);
|
1476
|
-
} else {
|
1477
|
-
material.color = new Color(color);
|
1478
|
-
}
|
1479
|
-
|
1480
|
-
ref.current.position.set(x, y, z);
|
1481
|
-
ref.current.rotation.setFromQuaternion(new Quaternion(rx, ry, rz, rw));
|
1482
|
-
}
|
1483
|
-
});
|
1484
|
-
const geometry = useMemo(() => {
|
1485
|
-
const collider = world.getCollider(colliderHandle);
|
1486
|
-
return geometryFromCollider(collider);
|
1487
|
-
}, [colliderHandle]);
|
1488
|
-
return /*#__PURE__*/React.createElement("mesh", {
|
1489
|
-
ref: ref,
|
1490
|
-
material: material
|
1491
|
-
}, /*#__PURE__*/React.createElement("primitive", {
|
1492
|
-
object: geometry,
|
1493
|
-
attach: "geometry"
|
1494
|
-
}));
|
1495
|
-
});
|
1496
|
-
const Debug = ({
|
1497
|
-
color: _color = "red",
|
1498
|
-
sleepColor: _sleepColor = "blue"
|
1499
|
-
}) => {
|
1500
|
-
const {
|
1501
|
-
world
|
1502
|
-
} = useRapier();
|
1503
|
-
const [colliders, setColliders] = useState([]);
|
1504
|
-
useRef({});
|
1505
|
-
|
1506
1361
|
useFrame(() => {
|
1507
|
-
const
|
1508
|
-
|
1509
|
-
|
1510
|
-
|
1511
|
-
|
1362
|
+
const mesh = ref.current;
|
1363
|
+
if (!mesh) return;
|
1364
|
+
const buffers = world.debugRender();
|
1365
|
+
mesh.geometry.setAttribute("position", new BufferAttribute(buffers.vertices, 3));
|
1366
|
+
mesh.geometry.setAttribute("color", new BufferAttribute(buffers.colors, 4));
|
1512
1367
|
});
|
1513
|
-
return /*#__PURE__*/React.createElement("
|
1514
|
-
|
1515
|
-
|
1516
|
-
color:
|
1517
|
-
|
1518
|
-
})));
|
1368
|
+
return /*#__PURE__*/React.createElement("lineSegments", {
|
1369
|
+
ref: ref
|
1370
|
+
}, /*#__PURE__*/React.createElement("lineBasicMaterial", {
|
1371
|
+
color: 0xffffff,
|
1372
|
+
vertexColors: true
|
1373
|
+
}), /*#__PURE__*/React.createElement("bufferGeometry", null));
|
1519
1374
|
};
|
1520
1375
|
|
1521
1376
|
const _excluded = ["positions", "rotations", "children"];
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@react-three/rapier",
|
3
|
-
"version": "0.
|
3
|
+
"version": "0.8.1",
|
4
4
|
"source": "src/index.ts",
|
5
5
|
"main": "dist/react-three-rapier.cjs.js",
|
6
6
|
"module": "dist/react-three-rapier.esm.js",
|
@@ -8,6 +8,9 @@
|
|
8
8
|
"files": [
|
9
9
|
"dist"
|
10
10
|
],
|
11
|
+
"scripts": {
|
12
|
+
"ts": "tsc -w"
|
13
|
+
},
|
11
14
|
"devDependencies": {
|
12
15
|
"@react-three/drei": "^9.34.1",
|
13
16
|
"@react-three/fiber": "^8.8.9",
|
package/readme.md
CHANGED
@@ -122,6 +122,16 @@ const Scene = () => (
|
|
122
122
|
);
|
123
123
|
```
|
124
124
|
|
125
|
+
If part of our meshes are invisible and you want to include them in the collider creation, use the `includeInvisible` flag.
|
126
|
+
|
127
|
+
```tsx
|
128
|
+
<RigidBody colliders="hull" includeInvisible>
|
129
|
+
<object3D>
|
130
|
+
<Suzanne visible={false} />
|
131
|
+
</object3D>
|
132
|
+
</RigidBody>
|
133
|
+
```
|
134
|
+
|
125
135
|
## Instanced Meshes
|
126
136
|
|
127
137
|
Instanced meshes can also be used and have automatic colliders generated from their mesh.
|
@@ -185,12 +195,7 @@ const Scene = () => {
|
|
185
195
|
|
186
196
|
## Debug
|
187
197
|
|
188
|
-
Use the Debug component to see live representations of all colliders in a scene.
|
189
|
-
|
190
|
-
- The `color` prop sets the color of awake colliders that are affected by forces.
|
191
|
-
- The `sleepColor` prop set the color of a static (fixed, or kinematic) or sleeping collider.
|
192
|
-
|
193
|
-
> Note: Experimental. Not all shapes are supported. Unsupported shapes are always represented by cubes.
|
198
|
+
Use the Debug component to see live representations of all colliders in a scene, using the live debug buffer from the physics engine.
|
194
199
|
|
195
200
|
```tsx
|
196
201
|
import { Box, Sphere } from "@react-three/drei";
|
@@ -199,7 +204,7 @@ import { RigidBody, Debug } from "@react-three/rapier";
|
|
199
204
|
const Scene = () => {
|
200
205
|
return (
|
201
206
|
<Physics>
|
202
|
-
<Debug
|
207
|
+
<Debug />
|
203
208
|
|
204
209
|
<RigidBody>
|
205
210
|
<Box />
|