@@ -45,6 +45,8 @@ import { DynamicDrawUsage, Euler, InstancedMesh, MathUtils, Object3D, Quaternion
45
45
import { useDebugContext } from './debug-context'
46
46
import type { CannonEvents } from './physics-context'
47
47
import { usePhysicsContext } from './physics-context'
48
+ import type { ThreeToCannonOptions } from './three-to-cannon'
49
+ import { threeToCannon } from './three-to-cannon'
48
50
49
51
export type AtomicApi < K extends AtomicName > = {
50
52
set : ( value : AtomicProps [ K ] ) => void
@@ -153,12 +155,13 @@ function setupCollision(
153
155
type GetByIndex < T extends BodyProps > = ( index : number ) => T
154
156
type ArgFn < T > = ( args : T ) => unknown [ ]
155
157
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 ,
158
160
fn : GetByIndex < B > ,
159
161
argsFn : ArgFn < B [ 'args' ] > ,
160
162
fwdRef : Ref < O > = null ,
161
163
deps : DependencyList = [ ] ,
164
+ threeToCannonOptions ?: ThreeToCannonOptions ,
162
165
) : Api < O > {
163
166
const ref = useForwardedRef ( fwdRef )
164
167
@@ -184,23 +187,35 @@ function useBody<B extends BodyProps<unknown[]>, O extends Object3D>(
184
187
? new Array ( objectCount ) . fill ( 0 ) . map ( ( _ , i ) => `${ object . uuid } /${ i } ` )
185
188
: [ object . uuid ]
186
189
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 =
188
203
object instanceof InstancedMesh
189
204
? uuid . map ( ( id , i ) => {
190
- const props = fn ( i )
205
+ const props = { ... inferredProps , ... fn ( i ) }
191
206
prepare ( temp , props )
192
207
object . setMatrixAt ( i , temp . matrix )
193
208
object . instanceMatrix . needsUpdate = true
194
209
refs [ id ] = object
195
- debugApi ?. add ( id , props , type )
210
+ debugApi ?. add ( id , props , shapeType )
196
211
setupCollision ( events , props , id )
197
212
return { ...props , args : argsFn ( props . args ) }
198
213
} )
199
214
: uuid . map ( ( id , i ) => {
200
- const props = fn ( i )
215
+ const props = { ... inferredProps , ... fn ( i ) }
201
216
prepare ( object , props )
202
217
refs [ id ] = object
203
- debugApi ?. add ( id , props , type )
218
+ debugApi ?. add ( id , props , shapeType )
204
219
setupCollision ( events , props , id )
205
220
return { ...props , args : argsFn ( props . args ) }
206
221
} )
@@ -210,7 +225,7 @@ function useBody<B extends BodyProps<unknown[]>, O extends Object3D>(
210
225
props : props . map ( ( { onCollide, onCollideBegin, onCollideEnd, ...serializableProps } ) => {
211
226
return { onCollide : Boolean ( onCollide ) , ...serializableProps }
212
227
} ) ,
213
- type,
228
+ type : shapeType ,
214
229
uuid,
215
230
} )
216
231
return ( ) => {
@@ -366,44 +381,52 @@ function makeTriplet(v: Vector3 | Triplet): Triplet {
366
381
return v instanceof Vector3 ? [ v . x , v . y , v . z ] : v
367
382
}
368
383
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
+ }
369
392
export function usePlane < O extends Object3D > (
370
393
fn : GetByIndex < PlaneProps > ,
371
394
fwdRef ?: Ref < O > ,
372
395
deps ?: DependencyList ,
373
396
) {
374
- return useBody ( 'Plane' , fn , ( ) => [ ] , fwdRef , deps )
397
+ return useBodyCommon ( 'Plane' , fn , ( ) => [ ] , fwdRef , deps )
375
398
}
376
399
export function useBox < O extends Object3D > ( fn : GetByIndex < BoxProps > , fwdRef ?: Ref < O > , deps ?: DependencyList ) {
377
400
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 )
379
402
}
380
403
export function useCylinder < O extends Object3D > (
381
404
fn : GetByIndex < CylinderProps > ,
382
405
fwdRef ?: Ref < O > ,
383
406
deps ?: DependencyList ,
384
407
) {
385
- return useBody ( 'Cylinder' , fn , ( args = [ ] as [ ] ) => args , fwdRef , deps )
408
+ return useBodyCommon ( 'Cylinder' , fn , ( args = [ ] as [ ] ) => args , fwdRef , deps )
386
409
}
387
410
export function useHeightfield < O extends Object3D > (
388
411
fn : GetByIndex < HeightfieldProps > ,
389
412
fwdRef ?: Ref < O > ,
390
413
deps ?: DependencyList ,
391
414
) {
392
- return useBody ( 'Heightfield' , fn , ( args ) => args , fwdRef , deps )
415
+ return useBodyCommon ( 'Heightfield' , fn , ( args ) => args , fwdRef , deps )
393
416
}
394
417
export function useParticle < O extends Object3D > (
395
418
fn : GetByIndex < ParticleProps > ,
396
419
fwdRef ?: Ref < O > ,
397
420
deps ?: DependencyList ,
398
421
) {
399
- return useBody ( 'Particle' , fn , ( ) => [ ] , fwdRef , deps )
422
+ return useBodyCommon ( 'Particle' , fn , ( ) => [ ] , fwdRef , deps )
400
423
}
401
424
export function useSphere < O extends Object3D > (
402
425
fn : GetByIndex < SphereProps > ,
403
426
fwdRef ?: Ref < O > ,
404
427
deps ?: DependencyList ,
405
428
) {
406
- return useBody (
429
+ return useBodyCommon (
407
430
'Sphere' ,
408
431
fn ,
409
432
( args : SphereArgs = [ 1 ] ) : SphereArgs => {
@@ -419,15 +442,15 @@ export function useTrimesh<O extends Object3D>(
419
442
fwdRef ?: Ref < O > ,
420
443
deps ?: DependencyList ,
421
444
) {
422
- return useBody < TrimeshProps , O > ( 'Trimesh' , fn , ( args ) => args , fwdRef , deps )
445
+ return useBodyCommon < TrimeshProps , O > ( 'Trimesh' , fn , ( args ) => args , fwdRef , deps )
423
446
}
424
447
425
448
export function useConvexPolyhedron < O extends Object3D > (
426
449
fn : GetByIndex < ConvexPolyhedronProps > ,
427
450
fwdRef ?: Ref < O > ,
428
451
deps ?: DependencyList ,
429
452
) {
430
- return useBody < ConvexPolyhedronProps , O > (
453
+ return useBodyCommon < ConvexPolyhedronProps , O > (
431
454
'ConvexPolyhedron' ,
432
455
fn ,
433
456
( [ vertices , faces , normals , axes , boundingSphereRadius ] = [ ] ) : ConvexPolyhedronArgs < Triplet > => [
@@ -446,7 +469,7 @@ export function useCompoundBody<O extends Object3D>(
446
469
fwdRef ?: Ref < O > ,
447
470
deps ?: DependencyList ,
448
471
) {
449
- return useBody ( 'Compound' , fn , ( args ) => args as unknown [ ] , fwdRef , deps )
472
+ return useBodyCommon ( 'Compound' , fn , ( args ) => args as unknown [ ] , fwdRef , deps )
450
473
}
451
474
452
475
type ConstraintApi < A extends Object3D , B extends Object3D > = [
0 commit comments