Skip to content

Commit

Permalink
fix: add usage and normalized to instancedattribute
Browse files Browse the repository at this point in the history
  • Loading branch information
drcmda committed Aug 23, 2024
1 parent d681f3e commit 2d550cd
Showing 1 changed file with 37 additions and 36 deletions.
73 changes: 37 additions & 36 deletions src/core/Instances.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ export type InstanceProps = JSX.IntrinsicElements['positionMesh'] & {
export type InstancedAttributeProps = JSX.IntrinsicElements['instancedBufferAttribute'] & {
name: string
defaultValue: any
normalized?: boolean
usage?: number
}

type InstancedMesh = Omit<THREE.InstancedMesh, 'instanceMatrix' | 'instanceColor'> & {
Expand Down Expand Up @@ -275,41 +277,40 @@ export function createInstances() {
]
}

export const InstancedAttribute = React.forwardRef(({ name, defaultValue }: InstancedAttributeProps, fref) => {
const ref = React.useRef<THREE.InstancedBufferAttribute>(null!)
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(() => {
const parent = (ref.current as any).__r3f.parent
if (parent.userData.frames === Infinity || iterations < parent.userData.frames) {
for (let i = 0; i < parent.userData.instances.length; i++) {
const instance = parent.userData.instances[i].current
const value = instance[name]
if (value !== undefined) {
ref.current.set(
Array.isArray(value) ? value : typeof value.toArray === 'function' ? value.toArray() : [value],
i * ref.current.itemSize
)
ref.current.needsUpdate = true
export const InstancedAttribute = React.forwardRef(
({ name, defaultValue, normalized, usage = THREE.DynamicDrawUsage }: InstancedAttributeProps, fref) => {
const ref = React.useRef<THREE.InstancedBufferAttribute>(null!)
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(() => {
const parent = (ref.current as any).__r3f.parent
if (parent.userData.frames === Infinity || iterations < parent.userData.frames) {
for (let i = 0; i < parent.userData.instances.length; i++) {
const instance = parent.userData.instances[i].current
const value = instance[name]
if (value !== undefined) {
ref.current.set(
Array.isArray(value) ? value : typeof value.toArray === 'function' ? value.toArray() : [value],
i * ref.current.itemSize
)
ref.current.needsUpdate = true
}
}
iterations++
}
iterations++
}
})
return <instancedBufferAttribute ref={ref} usage={THREE.DynamicDrawUsage} />
})
})
return <instancedBufferAttribute ref={ref} usage={usage} normalized={normalized} />
}
)

0 comments on commit 2d550cd

Please sign in to comment.