Skip to content

Commit d24a6b4

Browse files
WebGPURenderer: Respect TypedArray in StorageInstancedBufferAttribute (mrdoob#30218)
* WebGPURenderer: Respect TypedArray in StorageInstancedBufferAttribute * Update NodeUtils.js --------- Co-authored-by: Michael Herzog <[email protected]>
1 parent b35ad2a commit d24a6b4

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

src/nodes/accessors/Arrays.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import StorageInstancedBufferAttribute from '../../renderers/common/StorageInstancedBufferAttribute.js';
22
import StorageBufferAttribute from '../../renderers/common/StorageBufferAttribute.js';
33
import { storage } from './StorageBufferNode.js';
4-
import { getLengthFromType } from '../core/NodeUtils.js';
4+
import { getLengthFromType, getTypedArrayFromType } from '../core/NodeUtils.js';
55

66
/** @module Arrays **/
77

@@ -16,8 +16,9 @@ import { getLengthFromType } from '../core/NodeUtils.js';
1616
export const attributeArray = ( count, type = 'float' ) => {
1717

1818
const itemSize = getLengthFromType( type );
19+
const typedArray = getTypedArrayFromType( type );
1920

20-
const buffer = new StorageBufferAttribute( count, itemSize );
21+
const buffer = new StorageBufferAttribute( count, itemSize, typedArray );
2122
const node = storage( buffer, type, count );
2223

2324
return node;
@@ -35,8 +36,9 @@ export const attributeArray = ( count, type = 'float' ) => {
3536
export const instancedArray = ( count, type = 'float' ) => {
3637

3738
const itemSize = getLengthFromType( type );
39+
const typedArray = getTypedArrayFromType( type );
3840

39-
const buffer = new StorageInstancedBufferAttribute( count, itemSize );
41+
const buffer = new StorageInstancedBufferAttribute( count, itemSize, typedArray );
4042
const node = storage( buffer, type, count );
4143

4244
return node;

src/nodes/core/NodeUtils.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,39 @@ export function getTypeFromLength( length ) {
183183

184184
}
185185

186+
/**
187+
* Returns the typed array for the given data type.
188+
*
189+
* @method
190+
* @param {String} type - The data type.
191+
* @return {TypedArray} The typed array.
192+
*/
193+
export function getTypedArrayFromType( type ) {
194+
195+
// Handle component type for vectors and matrices
196+
if ( /[iu]?vec\d/.test( type ) ) {
197+
198+
// Handle int vectors
199+
if ( type.startsWith( 'ivec' ) ) return Int32Array;
200+
// Handle uint vectors
201+
if ( type.startsWith( 'uvec' ) ) return Uint32Array;
202+
// Default to float vectors
203+
return Float32Array;
204+
205+
}
206+
207+
// Handle matrices (always float)
208+
if ( /mat\d/.test( type ) ) return Float32Array;
209+
210+
// Basic types
211+
if ( /float/.test( type ) ) return Float32Array;
212+
if ( /uint/.test( type ) ) return Uint32Array;
213+
if ( /int/.test( type ) ) return Int32Array;
214+
215+
throw new Error( `THREE.NodeUtils: Unsupported type: ${type}` );
216+
217+
}
218+
186219
/**
187220
* Returns the length for the given data type.
188221
*

0 commit comments

Comments
 (0)