Skip to content

Commit

Permalink
fix: hot reload instancedattribute bug
Browse files Browse the repository at this point in the history
  • Loading branch information
drcmda committed Aug 18, 2024
1 parent 1063a8d commit 25685bd
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/core/Instances.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -280,11 +280,18 @@ export const InstancedAttribute = React.forwardRef(({ name, defaultValue }: Inst
React.useImperativeHandle(fref, () => ref.current, [])
React.useLayoutEffect(() => {
const parent = (ref.current as any).__r3f.parent

parent.geometry.attributes[name] = ref.current

const value = Array.isArray(defaultValue) ? defaultValue : [defaultValue]
const array = Array.from({ length: parent.userData.limit }, () => value).flat()
ref.current.array = new Float32Array(array)
ref.current.itemSize = value.length
ref.current.count = array.length / ref.current.itemSize

return () => {
delete parent.geometry.attributes[name]
}
}, [name])
let iterations = 0
useFrame(() => {
Expand All @@ -304,5 +311,5 @@ export const InstancedAttribute = React.forwardRef(({ name, defaultValue }: Inst
iterations++
}
})
return <instancedBufferAttribute ref={ref} attach={`geometry-attributes-${name}`} usage={THREE.DynamicDrawUsage} />
return <instancedBufferAttribute ref={ref} usage={THREE.DynamicDrawUsage} />
})

0 comments on commit 25685bd

Please sign in to comment.