From 2b80e6a57c052d21715b1f29868855667a27b550 Mon Sep 17 00:00:00 2001 From: Jure Triglav Date: Sat, 27 Jun 2026 19:34:30 +0200 Subject: [PATCH] TSL: Restore instanced matrix buffer capacity (#33889) --- src/nodes/accessors/Instance.js | 33 ++++++++------------ test/unit/src/objects/InstancedMesh.tests.js | 1 - 2 files changed, 13 insertions(+), 21 deletions(-) diff --git a/src/nodes/accessors/Instance.js b/src/nodes/accessors/Instance.js index b9dbe74da72e07..b89834b9166c75 100644 --- a/src/nodes/accessors/Instance.js +++ b/src/nodes/accessors/Instance.js @@ -23,26 +23,26 @@ const _previousInstanceMatrices = /*@__PURE__*/ new WeakMap(); * * @param {NodeBuilder} builder - The current node builder. * @param {InstancedBufferAttribute|StorageInstancedBufferAttribute} instanceMatrix - The matrix buffer attribute. - * @param {number} count - The instance count. * @returns {Node} The matrix node. */ -function createInstanceMatrixNode( builder, instanceMatrix, count ) { +function createInstanceMatrixNode( builder, instanceMatrix ) { let instanceMatrixNode; + const matrixCount = Math.max( instanceMatrix.count, 1 ); const isStorageMatrix = instanceMatrix.isStorageInstancedBufferAttribute === true; if ( isStorageMatrix ) { - instanceMatrixNode = storage( instanceMatrix, 'mat4', Math.max( count, 1 ) ).element( instanceIndex ); + instanceMatrixNode = storage( instanceMatrix, 'mat4', matrixCount ).element( instanceIndex ); } else { - const uniformBufferSize = count * 16 * 4; + const uniformBufferSize = matrixCount * 16 * 4; if ( uniformBufferSize <= builder.getUniformBufferLimit() ) { - instanceMatrixNode = buffer( instanceMatrix.array, 'mat4', Math.max( count, 1 ) ).element( instanceIndex ); + instanceMatrixNode = buffer( instanceMatrix.array, 'mat4', matrixCount ).element( instanceIndex ); } else { @@ -81,10 +81,9 @@ function createInstanceMatrixNode( builder, instanceMatrix, count ) { * @param {InstancedMesh} instancedMesh - The instanced mesh object. * @param {InstancedBufferAttribute|StorageInstancedBufferAttribute} instanceMatrix - The current matrix buffer attribute. * @param {NodeBuilder} builder - The current node builder. - * @param {number} count - The instance count. * @returns {Node} The previous frame instance matrix node. */ -function getPreviousInstance( instancedMesh, instanceMatrix, builder, count ) { +function getPreviousInstance( instancedMesh, instanceMatrix, builder ) { let data = _previousInstanceMatrices.get( instancedMesh ); @@ -94,7 +93,7 @@ function getPreviousInstance( instancedMesh, instanceMatrix, builder, count ) { data = { previousInstanceMatrix, - node: createInstanceMatrixNode( builder, previousInstanceMatrix, count ) + node: createInstanceMatrixNode( builder, previousInstanceMatrix ) }; _previousInstanceMatrices.set( instancedMesh, data ); @@ -118,26 +117,22 @@ export const instanceColor = /*@__PURE__*/ varyingProperty( 'vec3', 'vInstanceCo * * @tsl * @function - * @param {number} count - The instance count. * @param {InstancedBufferAttribute|StorageInstancedBufferAttribute} matrices - The instanced transformation matrices. * @param {?InstancedBufferAttribute|StorageInstancedBufferAttribute} [colors=null] - The optional instanced colors. */ -export const instance = /*@__PURE__*/ Fn( ( [ count, matrices, colors = null ], builder ) => { - - // get numeric value (non-node) - count = count.value; +export const instance = /*@__PURE__*/ Fn( ( [ matrices, colors = null ], builder ) => { const isStorageMatrix = matrices.isStorageInstancedBufferAttribute === true; const isStorageColor = colors && colors.isStorageInstancedBufferAttribute === true; - const instanceMatrixNode = createInstanceMatrixNode( builder, matrices, count ); + const instanceMatrixNode = createInstanceMatrixNode( builder, matrices ); // interleaved buffer tracking for matrix let interleavedMatrix = null; if ( ! isStorageMatrix ) { - const uniformBufferSize = count * 16 * 4; + const uniformBufferSize = Math.max( matrices.count, 1 ) * 16 * 4; if ( uniformBufferSize > builder.getUniformBufferLimit() ) { @@ -229,7 +224,7 @@ export const instance = /*@__PURE__*/ Fn( ( [ count, matrices, colors = null ], } ); - const previousInstanceMatrixNode = getPreviousInstance( instancedMesh, matrices, builder, count ); + const previousInstanceMatrixNode = getPreviousInstance( instancedMesh, matrices, builder ); positionPrevious.assign( previousInstanceMatrixNode.mul( positionPrevious ).xyz ); } @@ -262,10 +257,8 @@ export const instance = /*@__PURE__*/ Fn( ( [ count, matrices, colors = null ], */ export const instancedMesh = /*@__PURE__*/ Fn( ( [ instancedMesh ] ) => { - const { count, instanceMatrix, instanceColor } = instancedMesh; + const { instanceMatrix, instanceColor } = instancedMesh; - instance( count, instanceMatrix, instanceColor ); + instance( instanceMatrix, instanceColor ); }, 'void' ); - - diff --git a/test/unit/src/objects/InstancedMesh.tests.js b/test/unit/src/objects/InstancedMesh.tests.js index 192870c200820e..12661730212f3f 100644 --- a/test/unit/src/objects/InstancedMesh.tests.js +++ b/test/unit/src/objects/InstancedMesh.tests.js @@ -1,5 +1,4 @@ import { InstancedMesh } from '../../../../src/objects/InstancedMesh.js'; - import { Mesh } from '../../../../src/objects/Mesh.js'; export default QUnit.module( 'Objects', () => {