Skip to content

Commit e2b0cc2

Browse files
committed
improve DebugProvider instance handling
1 parent b9301e1 commit e2b0cc2

File tree

1 file changed

+13
-14
lines changed

1 file changed

+13
-14
lines changed

packages/react-three-cannon/src/debug-provider.tsx

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import type { Body, Quaternion as CQuaternion, Vec3, World } from 'cannon-es'
44
import CannonDebugger from 'cannon-es-debugger'
55
import type { FC } from 'react'
66
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'
99

1010
import type { DebugApi } from './debug-context'
1111
import { debugContext } from './debug-context'
@@ -24,6 +24,14 @@ const s = new Vector3(1, 1, 1)
2424
const v = new Vector3()
2525
const 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+
2735
export 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

Comments
 (0)