@@ -45,6 +45,8 @@ import { DynamicDrawUsage, Euler, InstancedMesh, MathUtils, Object3D, Quaternion
4545import { useDebugContext } from './debug-context'
4646import type { CannonEvents } from './physics-context'
4747import { usePhysicsContext } from './physics-context'
48+ import type { ThreeToCannonOptions } from './three-to-cannon'
49+ import { threeToCannon } from './three-to-cannon'
4850
4951export type AtomicApi < K extends AtomicName > = {
5052 set : ( value : AtomicProps [ K ] ) => void
@@ -153,12 +155,13 @@ function setupCollision(
153155type GetByIndex < T extends BodyProps > = ( index : number ) => T
154156type ArgFn < T > = ( args : T ) => unknown [ ]
155157
156- function useBody < B extends BodyProps < unknown [ ] > , O extends Object3D > (
157- type : BodyShapeType ,
158+ function useBodyCommon < B extends BodyProps < unknown [ ] > , O extends Object3D > (
159+ type : BodyShapeType | null ,
158160 fn : GetByIndex < B > ,
159161 argsFn : ArgFn < B [ 'args' ] > ,
160162 fwdRef : Ref < O > = null ,
161163 deps : DependencyList = [ ] ,
164+ threeToCannonOptions ?: ThreeToCannonOptions ,
162165) : Api < O > {
163166 const ref = useForwardedRef ( fwdRef )
164167
@@ -184,23 +187,35 @@ function useBody<B extends BodyProps<unknown[]>, O extends Object3D>(
184187 ? new Array ( objectCount ) . fill ( 0 ) . map ( ( _ , i ) => `${ object . uuid } /${ i } ` )
185188 : [ object . uuid ]
186189
187- const props : ( B & { args : unknown } ) [ ] =
190+ let shapeType : BodyShapeType = type || 'Particle'
191+ let inferredProps : BodyProps < unknown [ ] > = { }
192+
193+ if ( ! type ) {
194+ const result = threeToCannon ( object , threeToCannonOptions )
195+
196+ if ( result ) {
197+ shapeType = result . shape
198+ inferredProps = result . props
199+ }
200+ }
201+
202+ const props =
188203 object instanceof InstancedMesh
189204 ? uuid . map ( ( id , i ) => {
190- const props = fn ( i )
205+ const props = { ... inferredProps , ... fn ( i ) }
191206 prepare ( temp , props )
192207 object . setMatrixAt ( i , temp . matrix )
193208 object . instanceMatrix . needsUpdate = true
194209 refs [ id ] = object
195- debugApi ?. add ( id , props , type )
210+ debugApi ?. add ( id , props , shapeType )
196211 setupCollision ( events , props , id )
197212 return { ...props , args : argsFn ( props . args ) }
198213 } )
199214 : uuid . map ( ( id , i ) => {
200- const props = fn ( i )
215+ const props = { ... inferredProps , ... fn ( i ) }
201216 prepare ( object , props )
202217 refs [ id ] = object
203- debugApi ?. add ( id , props , type )
218+ debugApi ?. add ( id , props , shapeType )
204219 setupCollision ( events , props , id )
205220 return { ...props , args : argsFn ( props . args ) }
206221 } )
@@ -210,7 +225,7 @@ function useBody<B extends BodyProps<unknown[]>, O extends Object3D>(
210225 props : props . map ( ( { onCollide, onCollideBegin, onCollideEnd, ...serializableProps } ) => {
211226 return { onCollide : Boolean ( onCollide ) , ...serializableProps }
212227 } ) ,
213- type,
228+ type : shapeType ,
214229 uuid,
215230 } )
216231 return ( ) => {
@@ -366,44 +381,52 @@ function makeTriplet(v: Vector3 | Triplet): Triplet {
366381 return v instanceof Vector3 ? [ v . x , v . y , v . z ] : v
367382}
368383
384+ export function useBody < O extends Object3D > (
385+ fn : GetByIndex < PlaneProps > ,
386+ fwdRef : Ref < O > = null ,
387+ threeToCannonOptions ?: ThreeToCannonOptions ,
388+ deps ?: DependencyList ,
389+ ) {
390+ return useBodyCommon ( null , fn , ( args ) => args || [ ] , fwdRef , deps , threeToCannonOptions )
391+ }
369392export function usePlane < O extends Object3D > (
370393 fn : GetByIndex < PlaneProps > ,
371394 fwdRef ?: Ref < O > ,
372395 deps ?: DependencyList ,
373396) {
374- return useBody ( 'Plane' , fn , ( ) => [ ] , fwdRef , deps )
397+ return useBodyCommon ( 'Plane' , fn , ( ) => [ ] , fwdRef , deps )
375398}
376399export function useBox < O extends Object3D > ( fn : GetByIndex < BoxProps > , fwdRef ?: Ref < O > , deps ?: DependencyList ) {
377400 const defaultBoxArgs : Triplet = [ 1 , 1 , 1 ]
378- return useBody ( 'Box' , fn , ( args = defaultBoxArgs ) : Triplet => args , fwdRef , deps )
401+ return useBodyCommon ( 'Box' , fn , ( args = defaultBoxArgs ) : Triplet => args , fwdRef , deps )
379402}
380403export function useCylinder < O extends Object3D > (
381404 fn : GetByIndex < CylinderProps > ,
382405 fwdRef ?: Ref < O > ,
383406 deps ?: DependencyList ,
384407) {
385- return useBody ( 'Cylinder' , fn , ( args = [ ] as [ ] ) => args , fwdRef , deps )
408+ return useBodyCommon ( 'Cylinder' , fn , ( args = [ ] as [ ] ) => args , fwdRef , deps )
386409}
387410export function useHeightfield < O extends Object3D > (
388411 fn : GetByIndex < HeightfieldProps > ,
389412 fwdRef ?: Ref < O > ,
390413 deps ?: DependencyList ,
391414) {
392- return useBody ( 'Heightfield' , fn , ( args ) => args , fwdRef , deps )
415+ return useBodyCommon ( 'Heightfield' , fn , ( args ) => args , fwdRef , deps )
393416}
394417export function useParticle < O extends Object3D > (
395418 fn : GetByIndex < ParticleProps > ,
396419 fwdRef ?: Ref < O > ,
397420 deps ?: DependencyList ,
398421) {
399- return useBody ( 'Particle' , fn , ( ) => [ ] , fwdRef , deps )
422+ return useBodyCommon ( 'Particle' , fn , ( ) => [ ] , fwdRef , deps )
400423}
401424export function useSphere < O extends Object3D > (
402425 fn : GetByIndex < SphereProps > ,
403426 fwdRef ?: Ref < O > ,
404427 deps ?: DependencyList ,
405428) {
406- return useBody (
429+ return useBodyCommon (
407430 'Sphere' ,
408431 fn ,
409432 ( args : SphereArgs = [ 1 ] ) : SphereArgs => {
@@ -419,15 +442,15 @@ export function useTrimesh<O extends Object3D>(
419442 fwdRef ?: Ref < O > ,
420443 deps ?: DependencyList ,
421444) {
422- return useBody < TrimeshProps , O > ( 'Trimesh' , fn , ( args ) => args , fwdRef , deps )
445+ return useBodyCommon < TrimeshProps , O > ( 'Trimesh' , fn , ( args ) => args , fwdRef , deps )
423446}
424447
425448export function useConvexPolyhedron < O extends Object3D > (
426449 fn : GetByIndex < ConvexPolyhedronProps > ,
427450 fwdRef ?: Ref < O > ,
428451 deps ?: DependencyList ,
429452) {
430- return useBody < ConvexPolyhedronProps , O > (
453+ return useBodyCommon < ConvexPolyhedronProps , O > (
431454 'ConvexPolyhedron' ,
432455 fn ,
433456 ( [ vertices , faces , normals , axes , boundingSphereRadius ] = [ ] ) : ConvexPolyhedronArgs < Triplet > => [
@@ -446,7 +469,7 @@ export function useCompoundBody<O extends Object3D>(
446469 fwdRef ?: Ref < O > ,
447470 deps ?: DependencyList ,
448471) {
449- return useBody ( 'Compound' , fn , ( args ) => args as unknown [ ] , fwdRef , deps )
472+ return useBodyCommon ( 'Compound' , fn , ( args ) => args as unknown [ ] , fwdRef , deps )
450473}
451474
452475type ConstraintApi < A extends Object3D , B extends Object3D > = [
0 commit comments