diff --git a/examples/src/demos/Trimesh.jsx b/examples/src/demos/Trimesh.tsx similarity index 63% rename from examples/src/demos/Trimesh.jsx rename to examples/src/demos/Trimesh.tsx index a81114ae..cb5519bd 100644 --- a/examples/src/demos/Trimesh.jsx +++ b/examples/src/demos/Trimesh.tsx @@ -1,10 +1,20 @@ -import React, { useState, useEffect } from 'react' +import { useState, useEffect } from 'react' import { Canvas, invalidate } from '@react-three/fiber' import { Physics, useSphere, useTrimesh } from '@react-three/cannon' import { OrbitControls, TorusKnot, useGLTF } from '@react-three/drei' import create from 'zustand' -const useStore = create((set) => ({ +import type { SphereProps, TrimeshProps } from '@react-three/cannon' +import type { GLTF } from 'three-stdlib/loaders/GLTFLoader' +import type { BufferGeometry } from 'three' + +type Store = { + isPaused: boolean + pause: () => void + play: () => void +} + +const useStore = create((set) => ({ isPaused: false, pause: () => set({ isPaused: true }), play: () => set({ isPaused: false }), @@ -21,18 +31,28 @@ function Controls() { ) } -const WeirdCheerio = (props) => { - const [ref] = useSphere(() => ({ mass: 1, args: props.radius, ...props })) +const WeirdCheerio = ({ args = 0.1, position }: Pick) => { + const [ref] = useSphere(() => ({ args, mass: 1, position })) return ( - + ) } -const Bowl = (props) => { - const { nodes } = useGLTF('/bowl.glb') +type BowlGLTF = GLTF & { + nodes: { + bowl: { + geometry: BufferGeometry & { + index: ArrayLike + } + } + } +} + +const Bowl = ({ rotation }: Pick) => { + const { nodes } = useGLTF('/bowl.glb') as BowlGLTF const geometry = nodes.bowl.geometry const vertices = geometry.attributes.position.array const indices = geometry.index.array @@ -42,7 +62,7 @@ const Bowl = (props) => { const [ref] = useTrimesh(() => ({ mass: 0, - rotation: props.rotation, + rotation, args: [vertices, indices], })) @@ -55,8 +75,7 @@ const Bowl = (props) => { ref={ref} geometry={nodes.bowl.geometry} onPointerOver={() => setHover(true)} - onPointerOut={() => setHover(false)} - {...props}> + onPointerOut={() => setHover(false)}> ) @@ -71,10 +90,10 @@ export default () => ( - - - - + + + +
, indices: ArrayLike] type HeightfieldArgs = [ data: number[][], options: { elementSize?: number; maxValue?: number; minValue?: number }, diff --git a/src/hooks.ts b/src/hooks.ts index ab46e4b3..6ca5a701 100644 --- a/src/hooks.ts +++ b/src/hooks.ts @@ -68,7 +68,7 @@ export type ShapeType = export type BodyShapeType = ShapeType | 'Compound' export type CylinderArgs = [radiusTop?: number, radiusBottom?: number, height?: number, numSegments?: number] -export type TrimeshArgs = [vertices: number[], indices: number[]] +export type TrimeshArgs = [vertices: ArrayLike, indices: ArrayLike] export type HeightfieldArgs = [ data: number[][], options: { elementSize?: number; maxValue?: number; minValue?: number },