react-three-rapier-unified 1.0.0
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/LICENSE +9 -0
- package/README.md +166 -0
- package/dist/index.cjs.js +31 -0
- package/dist/index.d.ts +1191 -0
- package/dist/index.esm.js +1667 -0
- package/package.json +53 -0
- package/src/addons/attractor/Attractor.tsx +178 -0
- package/src/addons/attractor/AttractorDebugHelper.tsx +59 -0
- package/src/components/AnyCollider.tsx +286 -0
- package/src/components/Debug.tsx +33 -0
- package/src/components/FrameStepper.tsx +36 -0
- package/src/components/InstancedRigidBodies.tsx +135 -0
- package/src/components/MeshCollider.tsx +53 -0
- package/src/components/Physics.tsx +894 -0
- package/src/components/RigidBody.tsx +153 -0
- package/src/hooks/hooks.ts +211 -0
- package/src/hooks/joints.ts +221 -0
- package/src/hooks/use-forwarded-ref.ts +19 -0
- package/src/hooks/use-imperative-instance.ts +33 -0
- package/src/index.ts +55 -0
- package/src/types.ts +541 -0
- package/src/utils/interaction-groups.ts +43 -0
- package/src/utils/shared-objects.ts +10 -0
- package/src/utils/singleton-proxy.ts +51 -0
- package/src/utils/three-object-helpers.ts +25 -0
- package/src/utils/utils-collider.ts +491 -0
- package/src/utils/utils-physics.ts +26 -0
- package/src/utils/utils-rigidbody.ts +231 -0
- package/src/utils/utils.ts +105 -0
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import React, {
|
|
2
|
+
forwardRef,
|
|
3
|
+
Fragment,
|
|
4
|
+
memo,
|
|
5
|
+
ReactNode,
|
|
6
|
+
useEffect,
|
|
7
|
+
useRef
|
|
8
|
+
} from "react";
|
|
9
|
+
import { DynamicDrawUsage, InstancedMesh, Object3D } from "three";
|
|
10
|
+
import { useChildColliderProps } from "../hooks/hooks";
|
|
11
|
+
import { useForwardedRef } from "../hooks/use-forwarded-ref";
|
|
12
|
+
import { RapierRigidBody } from "../types";
|
|
13
|
+
import { AnyCollider } from "./AnyCollider";
|
|
14
|
+
import { RigidBodyState } from "./Physics";
|
|
15
|
+
import { RigidBody, RigidBodyProps } from "./RigidBody";
|
|
16
|
+
|
|
17
|
+
export type InstancedRigidBodyProps = RigidBodyProps & {
|
|
18
|
+
key: string | number;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export interface InstancedRigidBodiesProps extends Omit<RigidBodyProps, "ref"> {
|
|
22
|
+
instances: InstancedRigidBodyProps[];
|
|
23
|
+
colliderNodes?: ReactNode[];
|
|
24
|
+
children: ReactNode;
|
|
25
|
+
ref?: React.Ref<(RapierRigidBody | null)[] | null>;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export const InstancedRigidBodies = memo(
|
|
29
|
+
({ ref, ...props }: InstancedRigidBodiesProps) => {
|
|
30
|
+
const rigidBodiesRef = useForwardedRef<(RapierRigidBody | null)[]>(ref, []);
|
|
31
|
+
|
|
32
|
+
const objectRef = useRef<Object3D>(null);
|
|
33
|
+
const instanceWrapperRef = useRef<Object3D>(null);
|
|
34
|
+
const {
|
|
35
|
+
// instanced props
|
|
36
|
+
children,
|
|
37
|
+
instances,
|
|
38
|
+
colliderNodes = [],
|
|
39
|
+
|
|
40
|
+
// wrapper object props
|
|
41
|
+
position,
|
|
42
|
+
rotation,
|
|
43
|
+
quaternion,
|
|
44
|
+
scale,
|
|
45
|
+
|
|
46
|
+
// rigid body specific props, and r3f-object props
|
|
47
|
+
...rigidBodyProps
|
|
48
|
+
} = props;
|
|
49
|
+
|
|
50
|
+
const childColliderProps = useChildColliderProps(objectRef, {
|
|
51
|
+
...props,
|
|
52
|
+
children: undefined
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
const getInstancedMesh = () => {
|
|
56
|
+
const firstChild = instanceWrapperRef.current!.children[0];
|
|
57
|
+
|
|
58
|
+
if (firstChild && "isInstancedMesh" in firstChild) {
|
|
59
|
+
return firstChild as InstancedMesh;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
return undefined;
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
useEffect(() => {
|
|
66
|
+
const instancedMesh = getInstancedMesh();
|
|
67
|
+
|
|
68
|
+
if (instancedMesh) {
|
|
69
|
+
instancedMesh.instanceMatrix.setUsage(DynamicDrawUsage);
|
|
70
|
+
} else {
|
|
71
|
+
console.warn(
|
|
72
|
+
"InstancedRigidBodies expects exactly one child, which must be an InstancedMesh"
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
}, []);
|
|
76
|
+
|
|
77
|
+
// Update the RigidBodyStates whenever the instances change
|
|
78
|
+
const applyInstancedState = (state: RigidBodyState, index: number) => {
|
|
79
|
+
const instancedMesh = getInstancedMesh();
|
|
80
|
+
|
|
81
|
+
if (instancedMesh) {
|
|
82
|
+
return {
|
|
83
|
+
...state,
|
|
84
|
+
getMatrix: (matrix) => {
|
|
85
|
+
instancedMesh.getMatrixAt(index, matrix);
|
|
86
|
+
return matrix;
|
|
87
|
+
},
|
|
88
|
+
setMatrix: (matrix) => {
|
|
89
|
+
instancedMesh.setMatrixAt(index, matrix);
|
|
90
|
+
instancedMesh.instanceMatrix.needsUpdate = true;
|
|
91
|
+
},
|
|
92
|
+
meshType: "instancedMesh"
|
|
93
|
+
} as RigidBodyState;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
return state;
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
return (
|
|
100
|
+
<object3D
|
|
101
|
+
ref={objectRef}
|
|
102
|
+
{...rigidBodyProps}
|
|
103
|
+
position={position}
|
|
104
|
+
rotation={rotation}
|
|
105
|
+
quaternion={quaternion}
|
|
106
|
+
scale={scale}
|
|
107
|
+
>
|
|
108
|
+
<object3D ref={instanceWrapperRef}>{children}</object3D>
|
|
109
|
+
|
|
110
|
+
{instances?.map((instance, index) => (
|
|
111
|
+
<RigidBody
|
|
112
|
+
{...rigidBodyProps}
|
|
113
|
+
{...instance}
|
|
114
|
+
ref={(body) => {
|
|
115
|
+
rigidBodiesRef.current[index] = body;
|
|
116
|
+
}}
|
|
117
|
+
transformState={(state) => applyInstancedState(state, index)}
|
|
118
|
+
>
|
|
119
|
+
<>
|
|
120
|
+
{colliderNodes.map((node, index) => (
|
|
121
|
+
<Fragment key={index}>{node}</Fragment>
|
|
122
|
+
))}
|
|
123
|
+
|
|
124
|
+
{childColliderProps.map((colliderProps, colliderIndex) => (
|
|
125
|
+
<AnyCollider key={colliderIndex} {...colliderProps} />
|
|
126
|
+
))}
|
|
127
|
+
</>
|
|
128
|
+
</RigidBody>
|
|
129
|
+
))}
|
|
130
|
+
</object3D>
|
|
131
|
+
);
|
|
132
|
+
}
|
|
133
|
+
);
|
|
134
|
+
|
|
135
|
+
InstancedRigidBodies.displayName = "InstancedRigidBodies";
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import React, { ReactNode, useRef, useMemo, memo } from "react";
|
|
2
|
+
import { Object3D } from "three";
|
|
3
|
+
import { AnyCollider } from "..";
|
|
4
|
+
import { useChildColliderProps, useRapier } from "../hooks/hooks";
|
|
5
|
+
import { useRigidBodyContext } from "./RigidBody";
|
|
6
|
+
import { RigidBodyAutoCollider } from "../types";
|
|
7
|
+
|
|
8
|
+
export interface MeshColliderProps {
|
|
9
|
+
children: ReactNode;
|
|
10
|
+
type: RigidBodyAutoCollider;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* A mesh collider is a collider that is automatically generated from the geometry of the children.
|
|
15
|
+
* @category Colliders
|
|
16
|
+
*/
|
|
17
|
+
export const MeshCollider = memo((props: MeshColliderProps) => {
|
|
18
|
+
const { children, type } = props;
|
|
19
|
+
const { physicsOptions } = useRapier();
|
|
20
|
+
const object = useRef<Object3D>(null);
|
|
21
|
+
const { options } = useRigidBodyContext();
|
|
22
|
+
|
|
23
|
+
const mergedOptions = useMemo(() => {
|
|
24
|
+
return {
|
|
25
|
+
...physicsOptions,
|
|
26
|
+
...options,
|
|
27
|
+
children: undefined,
|
|
28
|
+
colliders: type
|
|
29
|
+
};
|
|
30
|
+
}, [physicsOptions, options]);
|
|
31
|
+
|
|
32
|
+
const childColliderProps = useChildColliderProps(
|
|
33
|
+
object,
|
|
34
|
+
mergedOptions,
|
|
35
|
+
false
|
|
36
|
+
);
|
|
37
|
+
|
|
38
|
+
return (
|
|
39
|
+
<object3D
|
|
40
|
+
ref={object}
|
|
41
|
+
userData={{
|
|
42
|
+
r3RapierType: "MeshCollider"
|
|
43
|
+
}}
|
|
44
|
+
>
|
|
45
|
+
{children}
|
|
46
|
+
{childColliderProps.map((colliderProps, index) => (
|
|
47
|
+
<AnyCollider key={index} {...colliderProps} />
|
|
48
|
+
))}
|
|
49
|
+
</object3D>
|
|
50
|
+
);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
MeshCollider.displayName = "MeshCollider";
|