@@ -4,8 +4,8 @@ import type { Body, Quaternion as CQuaternion, Vec3, World } from 'cannon-es'
4
4
import CannonDebugger from 'cannon-es-debugger'
5
5
import type { FC } from 'react'
6
6
import { 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'
9
9
10
10
import type { DebugApi } from './debug-context'
11
11
import { debugContext } from './debug-context'
@@ -24,6 +24,14 @@ const s = new Vector3(1, 1, 1)
24
24
const v = new Vector3 ( )
25
25
const m = new Matrix4 ( )
26
26
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
+
27
35
export const DebugProvider : FC < DebugProviderProps > = ( {
28
36
children,
29
37
color = 'black' ,
@@ -37,18 +45,9 @@ export const DebugProvider: FC<DebugProviderProps> = ({
37
45
38
46
useFrame ( ( ) => {
39
47
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 )
52
51
}
53
52
54
53
cannonDebuggerRef . current . update ( )
0 commit comments