Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 13 additions & 20 deletions src/nodes/accessors/Instance.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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 );

Expand All @@ -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 );
Expand All @@ -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() ) {

Expand Down Expand Up @@ -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 );

}
Expand Down Expand Up @@ -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' );


1 change: 0 additions & 1 deletion test/unit/src/objects/InstancedMesh.tests.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { InstancedMesh } from '../../../../src/objects/InstancedMesh.js';

import { Mesh } from '../../../../src/objects/Mesh.js';

export default QUnit.module( 'Objects', () => {
Expand Down
Loading