Skip to content

Commit 2d550cd

Browse files
committed
fix: add usage and normalized to instancedattribute
1 parent d681f3e commit 2d550cd

File tree

1 file changed

+37
-36
lines changed

1 file changed

+37
-36
lines changed

src/core/Instances.tsx

Lines changed: 37 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ export type InstanceProps = JSX.IntrinsicElements['positionMesh'] & {
3232
export type InstancedAttributeProps = JSX.IntrinsicElements['instancedBufferAttribute'] & {
3333
name: string
3434
defaultValue: any
35+
normalized?: boolean
36+
usage?: number
3537
}
3638

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

278-
export const InstancedAttribute = React.forwardRef(({ name, defaultValue }: InstancedAttributeProps, fref) => {
279-
const ref = React.useRef<THREE.InstancedBufferAttribute>(null!)
280-
React.useImperativeHandle(fref, () => ref.current, [])
281-
React.useLayoutEffect(() => {
282-
const parent = (ref.current as any).__r3f.parent
283-
284-
parent.geometry.attributes[name] = ref.current
285-
286-
const value = Array.isArray(defaultValue) ? defaultValue : [defaultValue]
287-
const array = Array.from({ length: parent.userData.limit }, () => value).flat()
288-
ref.current.array = new Float32Array(array)
289-
ref.current.itemSize = value.length
290-
ref.current.count = array.length / ref.current.itemSize
291-
292-
return () => {
293-
delete parent.geometry.attributes[name]
294-
}
295-
}, [name])
296-
let iterations = 0
297-
useFrame(() => {
298-
const parent = (ref.current as any).__r3f.parent
299-
if (parent.userData.frames === Infinity || iterations < parent.userData.frames) {
300-
for (let i = 0; i < parent.userData.instances.length; i++) {
301-
const instance = parent.userData.instances[i].current
302-
const value = instance[name]
303-
if (value !== undefined) {
304-
ref.current.set(
305-
Array.isArray(value) ? value : typeof value.toArray === 'function' ? value.toArray() : [value],
306-
i * ref.current.itemSize
307-
)
308-
ref.current.needsUpdate = true
280+
export const InstancedAttribute = React.forwardRef(
281+
({ name, defaultValue, normalized, usage = THREE.DynamicDrawUsage }: InstancedAttributeProps, fref) => {
282+
const ref = React.useRef<THREE.InstancedBufferAttribute>(null!)
283+
React.useImperativeHandle(fref, () => ref.current, [])
284+
React.useLayoutEffect(() => {
285+
const parent = (ref.current as any).__r3f.parent
286+
parent.geometry.attributes[name] = ref.current
287+
const value = Array.isArray(defaultValue) ? defaultValue : [defaultValue]
288+
const array = Array.from({ length: parent.userData.limit }, () => value).flat()
289+
ref.current.array = new Float32Array(array)
290+
ref.current.itemSize = value.length
291+
ref.current.count = array.length / ref.current.itemSize
292+
return () => {
293+
delete parent.geometry.attributes[name]
294+
}
295+
}, [name])
296+
let iterations = 0
297+
useFrame(() => {
298+
const parent = (ref.current as any).__r3f.parent
299+
if (parent.userData.frames === Infinity || iterations < parent.userData.frames) {
300+
for (let i = 0; i < parent.userData.instances.length; i++) {
301+
const instance = parent.userData.instances[i].current
302+
const value = instance[name]
303+
if (value !== undefined) {
304+
ref.current.set(
305+
Array.isArray(value) ? value : typeof value.toArray === 'function' ? value.toArray() : [value],
306+
i * ref.current.itemSize
307+
)
308+
ref.current.needsUpdate = true
309+
}
309310
}
311+
iterations++
310312
}
311-
iterations++
312-
}
313-
})
314-
return <instancedBufferAttribute ref={ref} usage={THREE.DynamicDrawUsage} />
315-
})
313+
})
314+
return <instancedBufferAttribute ref={ref} usage={usage} normalized={normalized} />
315+
}
316+
)

0 commit comments

Comments
 (0)