@@ -4,8 +4,8 @@ import type { Body, Quaternion as CQuaternion, Vec3, World } from 'cannon-es'
44import CannonDebugger from 'cannon-es-debugger'
55import type { FC } from 'react'
66import { useMemo , useRef , useState } from 'react'
7- import { Color , InstancedMesh } from 'three'
8- import { Quaternion , Scene , Vector3 , Matrix4 } from 'three'
7+ import type { Color , Object3D } from 'three'
8+ import { InstancedMesh , Matrix4 , Quaternion , Scene , Vector3 } from 'three'
99
1010import type { DebugApi } from './debug-context'
1111import { debugContext } from './debug-context'
@@ -24,6 +24,14 @@ const s = new Vector3(1, 1, 1)
2424const v = new Vector3 ( )
2525const m = new Matrix4 ( )
2626
27+ const getMatrix = ( o : Object3D ) : Matrix4 => {
28+ if ( o instanceof InstancedMesh ) {
29+ o . getMatrixAt ( parseInt ( o . uuid . split ( '/' ) [ 1 ] ) , m )
30+ return m
31+ }
32+ return o . matrix
33+ }
34+
2735export const DebugProvider : FC < DebugProviderProps > = ( {
2836 children,
2937 color = 'black' ,
@@ -37,18 +45,9 @@ export const DebugProvider: FC<DebugProviderProps> = ({
3745
3846 useFrame ( ( ) => {
3947 for ( const uuid in bodyMap ) {
40- let ref = refs [ uuid ]
41- if ( ref instanceof InstancedMesh ) {
42- const index = parseInt ( uuid . split ( '/' ) [ 1 ] )
43- ref . getMatrixAt ( index , m )
44- m . decompose ( v , q , s )
45- bodyMap [ uuid ] . position . copy ( v as unknown as Vec3 )
46- bodyMap [ uuid ] . quaternion . copy ( q as unknown as CQuaternion )
47- } else {
48- ref . matrix . decompose ( v , q , s )
49- bodyMap [ uuid ] . position . copy ( v as unknown as Vec3 )
50- bodyMap [ uuid ] . quaternion . copy ( q as unknown as CQuaternion )
51- }
48+ getMatrix ( refs [ uuid ] ) . decompose ( v , q , s )
49+ bodyMap [ uuid ] . position . copy ( v as unknown as Vec3 )
50+ bodyMap [ uuid ] . quaternion . copy ( q as unknown as CQuaternion )
5251 }
5352
5453 cannonDebuggerRef . current . update ( )
0 commit comments