Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BatchedMesh: Update Example, some optimization #27202

Merged
merged 8 commits into from
Nov 19, 2023
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
5 changes: 4 additions & 1 deletion examples/webgl_mesh_batch.html
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@

//

const MAX_GEOMETRY_COUNT = 8192;
const MAX_GEOMETRY_COUNT = 20000;

const Method = {
BATCHED: 'BATCHED',
Expand All @@ -64,6 +64,7 @@
dynamic: 16,

sortObjects: true,
perObjectFrustumCulled: true,
opacity: 1,
};

Expand Down Expand Up @@ -266,6 +267,7 @@

} );
gui.add( api, 'sortObjects' );
gui.add( api, 'perObjectFrustumCulled' );

guiStatsEl = document.createElement( 'li' );
guiStatsEl.classList.add( 'gui-stats' );
Expand Down Expand Up @@ -344,6 +346,7 @@
if ( mesh.isBatchedMesh ) {

mesh.sortObjects = api.sortObjects;
mesh.perObjectFrustumCulled = api.perObjectFrustumCulled;

}

Expand Down
19 changes: 15 additions & 4 deletions src/objects/BatchedMesh.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { BufferAttribute } from '../core/BufferAttribute.js';
import { BufferGeometry } from '../core/BufferGeometry.js';
import { DataTexture } from '../textures/DataTexture.js';
import { FloatType } from '../constants.js';
import { MathUtils } from '../math/MathUtils.js';
import { Matrix4 } from '../math/Matrix4.js';
import { Mesh } from './Mesh.js';
import { RGBAFormat } from '../constants.js';
Expand Down Expand Up @@ -147,6 +146,7 @@ class BatchedMesh extends Mesh {
this._multiDrawCounts = new Int32Array( maxGeometryCount );
this._multiDrawStarts = new Int32Array( maxGeometryCount );
this._multiDrawCount = 0;
this._visibilityChanged = true;

// Local matrix per geometry by using data texture
this._matricesTexture = null;
Expand All @@ -165,7 +165,7 @@ class BatchedMesh extends Mesh {
// 64x64 pixel texture max 1024 matrices * 4 pixels = (64 * 64)

let size = Math.sqrt( this._maxGeometryCount * 4 ); // 4 pixels needed for 1 matrix
size = MathUtils.ceilPowerOfTwo( size );
size = Math.ceil( size / 4 ) * 4;
size = Math.max( size, 4 );

const matricesArray = new Float32Array( size * size * 4 ); // 4 floats per RGBA pixel
Expand Down Expand Up @@ -594,6 +594,7 @@ class BatchedMesh extends Mesh {
const drawRange = this._drawRanges[ id ];
const posAttr = geometry.getAttribute( 'position' );
drawRange.count = hasIndex ? srcIndex.count : posAttr.count;
this._visibilityChanged = true;

return id;

Expand All @@ -611,6 +612,7 @@ class BatchedMesh extends Mesh {
}

active[ geometryId ] = false;
this._visibilityChanged = true;

return this;

Expand Down Expand Up @@ -765,6 +767,8 @@ class BatchedMesh extends Mesh {
}

visibility[ geometryId ] = value;
this._visibilityChanged = true;

return this;

}
Expand Down Expand Up @@ -901,6 +905,14 @@ class BatchedMesh extends Mesh {

onBeforeRender( _renderer, _scene, camera, geometry, material/*, _group*/ ) {

// if visibility has not changed and frustum culling and object sorting is not required
// then skip iterating over all items
if ( ! this._visibilityChanged && ! this.perObjectFrustumCulled && ! this.sortObjects ) {

return;

}

// the indexed version of the multi draw function requires specifying the start
// offset in bytes.
const index = geometry.getIndex();
Expand Down Expand Up @@ -1015,8 +1027,7 @@ class BatchedMesh extends Mesh {
}

this._multiDrawCount = count;

// @TODO: Implement geometry sorting for transparent and opaque materials
this._visibilityChanged = false;

}

Expand Down