Skip to content

Commit

Permalink
fix batching before instancing and add multidraw instance support (#2…
Browse files Browse the repository at this point in the history
  • Loading branch information
RenaudRohlinger authored Mar 20, 2024
1 parent 10d1622 commit 70aed3d
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 5 deletions.
8 changes: 4 additions & 4 deletions examples/jsm/nodes/materials/NodeMaterial.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,15 +205,15 @@ class NodeMaterial extends ShaderMaterial {

}

if ( ( object.instanceMatrix && object.instanceMatrix.isInstancedBufferAttribute === true ) && builder.isAvailable( 'instance' ) === true ) {
if ( object.isBatchedMesh ) {

instance( object ).append();
batch( object ).append();

}

if ( object.isBatchedMesh ) {
if ( ( object.instanceMatrix && object.instanceMatrix.isInstancedBufferAttribute === true ) && builder.isAvailable( 'instance' ) === true ) {

batch( object ).append();
instance( object ).append();

}

Expand Down
19 changes: 18 additions & 1 deletion examples/jsm/renderers/webgl/WebGLBackend.js
Original file line number Diff line number Diff line change
Expand Up @@ -699,7 +699,24 @@ class WebGLBackend extends Backend {

if ( object.isBatchedMesh ) {

renderer.renderMultiDraw( object._multiDrawStarts, object._multiDrawCounts, object._multiDrawCount );
if ( instanceCount > 1 ) {

// TODO: Better support with InstancedBatchedMesh
if ( object._multiDrawInstances === undefined ) {

object._multiDrawInstances = new Int32Array( object._maxGeometryCount );

}

object._multiDrawInstances.fill( instanceCount );

renderer.renderMultiDrawInstances( object._multiDrawStarts, object._multiDrawCounts, object._multiDrawCount, object._multiDrawInstances );

} else {

renderer.renderMultiDraw( object._multiDrawStarts, object._multiDrawCounts, object._multiDrawCount );

}

} else if ( instanceCount > 1 ) {

Expand Down
42 changes: 42 additions & 0 deletions examples/jsm/renderers/webgl/WebGLBufferRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,48 @@ class WebGLBufferRenderer {

}

renderMultiDrawInstances( starts, counts, drawCount, primcount ) {

const { extensions, mode, object, info } = this;

if ( drawCount === 0 ) return;

const extension = extensions.get( 'WEBGL_multi_draw' );

if ( extension === null ) {

for ( let i = 0; i < drawCount; i ++ ) {

this.renderInstances( starts[ i ], counts[ i ], primcount[ i ] );

}

} else {

if ( this.index !== 0 ) {

extension.multiDrawElementsInstancedWEBGL( mode, counts, 0, this.type, starts, 0, primcount, 0, drawCount );

} else {

extension.multiDrawArraysInstancedWEBGL( mode, starts, 0, counts, 0, primcount, 0, drawCount );

}

let elementCount = 0;

for ( let i = 0; i < drawCount; i ++ ) {

elementCount += counts[ i ];

}

info.update( object, elementCount, mode, primcount );

}

}

//

}
Expand Down

0 comments on commit 70aed3d

Please sign in to comment.