@@ -9,9 +9,14 @@ import {
99 usePointToPointConstraint ,
1010 useSphere ,
1111} from '@react-three/cannon'
12- import type { BoxBufferGeometryProps , MeshProps , MeshStandardMaterialProps } from '@react-three/fiber'
12+ import type {
13+ BoxBufferGeometryProps ,
14+ MeshProps ,
15+ MeshStandardMaterialProps ,
16+ ThreeEvent ,
17+ } from '@react-three/fiber'
1318import { Canvas , useFrame , useLoader } from '@react-three/fiber'
14- import type { FC , ReactNode , RefObject } from 'react'
19+ import type { ReactNode , RefObject } from 'react'
1520import {
1621 createContext ,
1722 createRef ,
@@ -23,7 +28,7 @@ import {
2328 useMemo ,
2429 useRef ,
2530} from 'react'
26- import type { Material , Mesh , Object3D } from 'three'
31+ import type { Group , Material , Mesh , Object3D , SpotLight } from 'three'
2732import type { GLTF } from 'three-stdlib/loaders/GLTFLoader'
2833import { GLTFLoader } from 'three-stdlib/loaders/GLTFLoader'
2934
@@ -32,16 +37,17 @@ import { createRagdoll } from './createConfig'
3237
3338const { joints, shapes } = createRagdoll ( 4.8 , Math . PI / 16 , Math . PI / 16 , 0 )
3439const context = createContext < RefObject < Object3D > > ( createRef < Object3D > ( ) )
35- const cursor = createRef < Object3D > ( )
40+ const cursor = createRef < Mesh > ( )
3641
3742const double = ( [ x , y , z ] : Readonly < Triplet > ) : Triplet => [ x * 2 , y * 2 , z * 2 ]
3843
3944function useDragConstraint ( child : RefObject < Object3D > ) {
4045 const [ , , api ] = usePointToPointConstraint ( cursor , child , { pivotA : [ 0 , 0 , 0 ] , pivotB : [ 0 , 0 , 0 ] } )
4146 // TODO: make it so we can start the constraint with it disabled
4247 useEffect ( ( ) => void api . disable ( ) , [ ] )
43- const onPointerDown = useCallback ( ( e ) => {
48+ const onPointerDown = useCallback ( ( e : ThreeEvent < PointerEvent > ) => {
4449 e . stopPropagation ( )
50+ //@ts -expect-error Investigate proper types here.
4551 e . target . setPointerCapture ( e . pointerId )
4652 api . enable ( )
4753 } , [ ] )
@@ -53,7 +59,7 @@ type BoxProps = Omit<MeshProps, 'args'> &
5359 Pick < BoxBufferGeometryProps , 'args' > &
5460 Pick < MeshStandardMaterialProps , 'color' | 'opacity' | 'transparent' >
5561
56- const Box = forwardRef < Object3D | undefined , BoxProps > (
62+ const Box = forwardRef < Mesh , BoxProps > (
5763 ( { args = [ 1 , 1 , 1 ] , children, color = 'white' , opacity = 1 , transparent = false , ...props } , ref ) => {
5864 return (
5965 < mesh castShadow receiveShadow ref = { ref } { ...props } >
@@ -71,11 +77,14 @@ type BodyPartProps = BoxProps & {
7177 render ?: ReactNode
7278}
7379
74- const BodyPart : FC < BodyPartProps > = ( { children, config = { } , name, render, ...props } ) => {
80+ function BodyPart ( { children, config = { } , name, render, ...props } : BodyPartProps ) : JSX . Element {
7581 const { color, args, mass, position } = shapes [ name ]
7682 const scale = useMemo < Triplet > ( ( ) => double ( args ) , [ args ] )
7783 const parent = useContext ( context )
78- const [ ref ] = useBox ( ( ) => ( { args : [ ...args ] , linearDamping : 0.99 , mass, position : [ ...position ] } ) )
84+ const [ ref ] = useBox (
85+ ( ) => ( { args : [ ...args ] , linearDamping : 0.99 , mass, position : [ ...position ] } ) ,
86+ useRef < Mesh > ( null ) ,
87+ )
7988 useConeTwistConstraint ( ref , parent , config )
8089 const bind = useDragConstraint ( ref )
8190 return (
@@ -89,8 +98,8 @@ const BodyPart: FC<BodyPartProps> = ({ children, config = {}, name, render, ...p
8998}
9099
91100function Ragdoll ( props : Pick < MeshProps , 'position' > ) {
92- const mouth = useRef < Object3D > ( null )
93- const eyes = useRef < Object3D > ( null )
101+ const mouth = useRef < Mesh > ( null )
102+ const eyes = useRef < Group > ( null )
94103
95104 useFrame ( ( { clock } ) => {
96105 if ( ! eyes . current || ! mouth . current ) return
@@ -151,7 +160,7 @@ function Ragdoll(props: Pick<MeshProps, 'position'>) {
151160}
152161
153162function Plane ( props : PlaneProps ) {
154- const [ ref ] = usePlane ( ( ) => ( { ...props } ) )
163+ const [ ref ] = usePlane ( ( ) => ( { ...props } ) , useRef < Mesh > ( null ) )
155164 return (
156165 < mesh ref = { ref } receiveShadow >
157166 < planeBufferGeometry args = { [ 1000 , 1000 ] } />
@@ -161,19 +170,22 @@ function Plane(props: PlaneProps) {
161170}
162171
163172function Chair ( ) {
164- const [ ref ] = useCompoundBody ( ( ) => ( {
165- mass : 1 ,
166- position : [ - 6 , 0 , 0 ] ,
167- shapes : [
168- { args : [ 1.5 , 1.5 , 0.25 ] , mass : 1 , position : [ 0 , 0 , 0 ] , type : 'Box' } ,
169- { args : [ 1.5 , 0.25 , 1.5 ] , mass : 1 , position : [ 0 , - 1.75 , 1.25 ] , type : 'Box' } ,
170- { args : [ 0.25 , 1.5 , 0.25 ] , mass : 10 , position : [ 5 + - 6.25 , - 3.5 , 0 ] , type : 'Box' } ,
171- { args : [ 0.25 , 1.5 , 0.25 ] , mass : 10 , position : [ 5 + - 3.75 , - 3.5 , 0 ] , type : 'Box' } ,
172- { args : [ 0.25 , 1.5 , 0.25 ] , mass : 10 , position : [ 5 + - 6.25 , - 3.5 , 2.5 ] , type : 'Box' } ,
173- { args : [ 0.25 , 1.5 , 0.25 ] , mass : 10 , position : [ 5 + - 3.75 , - 3.5 , 2.5 ] , type : 'Box' } ,
174- ] ,
175- type : 'Dynamic' ,
176- } ) )
173+ const [ ref ] = useCompoundBody (
174+ ( ) => ( {
175+ mass : 1 ,
176+ position : [ - 6 , 0 , 0 ] ,
177+ shapes : [
178+ { args : [ 1.5 , 1.5 , 0.25 ] , mass : 1 , position : [ 0 , 0 , 0 ] , type : 'Box' } ,
179+ { args : [ 1.5 , 0.25 , 1.5 ] , mass : 1 , position : [ 0 , - 1.75 , 1.25 ] , type : 'Box' } ,
180+ { args : [ 0.25 , 1.5 , 0.25 ] , mass : 10 , position : [ 5 + - 6.25 , - 3.5 , 0 ] , type : 'Box' } ,
181+ { args : [ 0.25 , 1.5 , 0.25 ] , mass : 10 , position : [ 5 + - 3.75 , - 3.5 , 0 ] , type : 'Box' } ,
182+ { args : [ 0.25 , 1.5 , 0.25 ] , mass : 10 , position : [ 5 + - 6.25 , - 3.5 , 2.5 ] , type : 'Box' } ,
183+ { args : [ 0.25 , 1.5 , 0.25 ] , mass : 10 , position : [ 5 + - 3.75 , - 3.5 , 2.5 ] , type : 'Box' } ,
184+ ] ,
185+ type : 'Dynamic' ,
186+ } ) ,
187+ useRef < Group > ( null ) ,
188+ )
177189 const bind = useDragConstraint ( ref )
178190 return (
179191 < group ref = { ref } { ...bind } >
@@ -200,12 +212,15 @@ interface CupGLTF extends GLTF {
200212
201213function Mug ( ) {
202214 const { nodes, materials } = useLoader ( GLTFLoader , '/cup.glb' ) as CupGLTF
203- const [ ref ] = useCylinder ( ( ) => ( {
204- args : [ 0.6 , 0.6 , 1 , 16 ] ,
205- mass : 1 ,
206- position : [ 9 , 0 , 0 ] ,
207- rotation : [ Math . PI / 2 , 0 , 0 ] ,
208- } ) )
215+ const [ ref ] = useCylinder (
216+ ( ) => ( {
217+ args : [ 0.6 , 0.6 , 1 , 16 ] ,
218+ mass : 1 ,
219+ position : [ 9 , 0 , 0 ] ,
220+ rotation : [ Math . PI / 2 , 0 , 0 ] ,
221+ } ) ,
222+ useRef < Group > ( null ) ,
223+ )
209224 const bind = useDragConstraint ( ref )
210225 return (
211226 < group ref = { ref } { ...bind } dispose = { null } >
@@ -228,11 +243,26 @@ function Mug() {
228243}
229244
230245function Table ( ) {
231- const [ seat ] = useBox ( ( ) => ( { args : [ 2.5 , 0.25 , 2.5 ] , position : [ 9 , - 0.8 , 0 ] , type : 'Static' } ) )
232- const [ leg1 ] = useBox ( ( ) => ( { args : [ 0.25 , 2 , 0.25 ] , position : [ 7.2 , - 3 , 1.8 ] , type : 'Static' } ) )
233- const [ leg2 ] = useBox ( ( ) => ( { args : [ 0.25 , 2 , 0.25 ] , position : [ 10.8 , - 3 , 1.8 ] , type : 'Static' } ) )
234- const [ leg3 ] = useBox ( ( ) => ( { args : [ 0.25 , 2 , 0.25 ] , position : [ 7.2 , - 3 , - 1.8 ] , type : 'Static' } ) )
235- const [ leg4 ] = useBox ( ( ) => ( { args : [ 0.25 , 2 , 0.25 ] , position : [ 10.8 , - 3 , - 1.8 ] , type : 'Static' } ) )
246+ const [ seat ] = useBox (
247+ ( ) => ( { args : [ 2.5 , 0.25 , 2.5 ] , position : [ 9 , - 0.8 , 0 ] , type : 'Static' } ) ,
248+ useRef < Mesh > ( null ) ,
249+ )
250+ const [ leg1 ] = useBox (
251+ ( ) => ( { args : [ 0.25 , 2 , 0.25 ] , position : [ 7.2 , - 3 , 1.8 ] , type : 'Static' } ) ,
252+ useRef < Mesh > ( null ) ,
253+ )
254+ const [ leg2 ] = useBox (
255+ ( ) => ( { args : [ 0.25 , 2 , 0.25 ] , position : [ 10.8 , - 3 , 1.8 ] , type : 'Static' } ) ,
256+ useRef < Mesh > ( null ) ,
257+ )
258+ const [ leg3 ] = useBox (
259+ ( ) => ( { args : [ 0.25 , 2 , 0.25 ] , position : [ 7.2 , - 3 , - 1.8 ] , type : 'Static' } ) ,
260+ useRef < Mesh > ( null ) ,
261+ )
262+ const [ leg4 ] = useBox (
263+ ( ) => ( { args : [ 0.25 , 2 , 0.25 ] , position : [ 10.8 , - 3 , - 1.8 ] , type : 'Static' } ) ,
264+ useRef < Mesh > ( null ) ,
265+ )
236266 return (
237267 < >
238268 < Box scale = { [ 5 , 0.5 , 5 ] } ref = { seat } />
@@ -248,15 +278,18 @@ function Table() {
248278}
249279
250280const Lamp = ( ) => {
251- const light = useRef ( )
252- const [ fixed ] = useSphere ( ( ) => ( { args : [ 1 ] , position : [ 0 , 16 , 0 ] , type : 'Static' } ) )
253- const [ lamp ] = useBox ( ( ) => ( {
254- angulardamping : 1.99 ,
255- args : [ 1 , 0 , 5 ] ,
256- linearDamping : 0.9 ,
257- mass : 1 ,
258- position : [ 0 , 16 , 0 ] ,
259- } ) )
281+ const light = useRef < SpotLight > ( null )
282+ const [ fixed ] = useSphere ( ( ) => ( { args : [ 1 ] , position : [ 0 , 16 , 0 ] , type : 'Static' } ) , useRef < Mesh > ( null ) )
283+ const [ lamp ] = useBox (
284+ ( ) => ( {
285+ angulardamping : 1.99 ,
286+ args : [ 1 , 0 , 5 ] ,
287+ linearDamping : 0.9 ,
288+ mass : 1 ,
289+ position : [ 0 , 16 , 0 ] ,
290+ } ) ,
291+ useRef < Mesh > ( null ) ,
292+ )
260293 usePointToPointConstraint ( fixed , lamp , { pivotA : [ 0 , 0 , 0 ] , pivotB : [ 0 , 2 , 0 ] } )
261294 const bind = useDragConstraint ( lamp )
262295 return (
0 commit comments