From 2d550cd453a9a9ec8adbef159f5745a0e7174c05 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=94?= Date: Fri, 23 Aug 2024 17:41:21 +0200 Subject: [PATCH] fix: add usage and normalized to instancedattribute --- src/core/Instances.tsx | 73 +++++++++++++++++++++--------------------- 1 file changed, 37 insertions(+), 36 deletions(-) diff --git a/src/core/Instances.tsx b/src/core/Instances.tsx index e135b8a99..fc27df8fc 100644 --- a/src/core/Instances.tsx +++ b/src/core/Instances.tsx @@ -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 & { @@ -275,41 +277,40 @@ export function createInstances() { ] } -export const InstancedAttribute = React.forwardRef(({ name, defaultValue }: InstancedAttributeProps, fref) => { - const ref = React.useRef(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(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 -}) + }) + return + } +)