Skip to content

Commit b9301e1

Browse files
krispyabjornstar
authored andcommitted
DebugProvider now updates InstancedMeshes
1 parent e8a8d04 commit b9301e1

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

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

Lines changed: 15 additions & 5 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 type { Color } from 'three'
8-
import { Quaternion, Scene, Vector3 } from 'three'
7+
import { Color, InstancedMesh } from 'three'
8+
import { Quaternion, Scene, Vector3, Matrix4 } from 'three'
99

1010
import type { DebugApi } from './debug-context'
1111
import { debugContext } from './debug-context'
@@ -22,6 +22,7 @@ export type DebugProviderProps = {
2222
const q = new Quaternion()
2323
const s = new Vector3(1, 1, 1)
2424
const v = new Vector3()
25+
const m = new Matrix4()
2526

2627
export const DebugProvider: FC<DebugProviderProps> = ({
2728
children,
@@ -36,9 +37,18 @@ export const DebugProvider: FC<DebugProviderProps> = ({
3637

3738
useFrame(() => {
3839
for (const uuid in bodyMap) {
39-
refs[uuid].matrix.decompose(v, q, s)
40-
bodyMap[uuid].position.copy(v as unknown as Vec3)
41-
bodyMap[uuid].quaternion.copy(q as unknown as CQuaternion)
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+
}
4252
}
4353

4454
cannonDebuggerRef.current.update()

0 commit comments

Comments
 (0)