@@ -32,6 +32,8 @@ export type InstanceProps = JSX.IntrinsicElements['positionMesh'] & {
32
32
export type InstancedAttributeProps = JSX . IntrinsicElements [ 'instancedBufferAttribute' ] & {
33
33
name : string
34
34
defaultValue : any
35
+ normalized ?: boolean
36
+ usage ?: number
35
37
}
36
38
37
39
type InstancedMesh = Omit < THREE . InstancedMesh , 'instanceMatrix' | 'instanceColor' > & {
@@ -275,41 +277,40 @@ export function createInstances() {
275
277
]
276
278
}
277
279
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
+ }
309
310
}
311
+ iterations ++
310
312
}
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