Skip to content

Commit

Permalink
BatchedMesh: Remove unneeded & redundant matrices array (#27130)
Browse files Browse the repository at this point in the history
* Add support for multi draw

* Fix multidraw

* Add batching material properties

* Adjust batching define

* Move normal shaders

* Move vertex transform shadres

* Move batching pars

* Move batching matrix fetch

* Switch _batch_id_ to batchId

* Remove custom shader callback

* Remove "matrices" array

* Remove unused custom uniforms

* Remove unneeded field
  • Loading branch information
gkjohnson committed Nov 6, 2023
1 parent 67e2670 commit 5f4f0b2
Showing 1 changed file with 17 additions and 41 deletions.
58 changes: 17 additions & 41 deletions examples/jsm/objects/BatchedMesh.js
Expand Up @@ -20,6 +20,10 @@ const _zeroScaleMatrix = new Matrix4().set(

// @TODO: SkinnedMesh support?
// @TODO: Future work if needed. Move into the core. Can be optimized more with WEBGL_multi_draw.
// @TODO: geometry.groups support?
// @TODO: geometry.drawRange support?
// @TODO: geometry.morphAttributes support?
// @TODO: Support uniform parameter per geometry

// copies data from attribute "src" into "target" starting at "targetOffset"
function copyAttributeData( src, target, targetOffset = 0 ) {
Expand Down Expand Up @@ -76,9 +80,6 @@ class BatchedMesh extends Mesh {
this._multiDrawCount = 0;

// Local matrix per geometry by using data texture
// @TODO: Support uniform parameter per geometry

this._matrices = [];
this._matricesTexture = null;

// @TODO: Calculate the entire binding box and make frustumCulled true
Expand Down Expand Up @@ -110,10 +111,6 @@ class BatchedMesh extends Mesh {

_initializeGeometry( reference ) {

// @TODO: geometry.groups support?
// @TODO: geometry.drawRange support?
// @TODO: geometry.morphAttributes support?

const geometry = this.geometry;
const maxVertexCount = this._maxVertexCount;
const maxGeometryCount = this._maxGeometryCount;
Expand Down Expand Up @@ -328,7 +325,6 @@ class BatchedMesh extends Mesh {
const visible = this._visible;
const active = this._active;
const matricesTexture = this._matricesTexture;
const matrices = this._matrices;
const matricesArray = this._matricesTexture.image.data;

// push new visibility states
Expand All @@ -340,7 +336,6 @@ class BatchedMesh extends Mesh {
this._geometryCount ++;

// initialize matrix information
matrices.push( new Matrix4() );
_identityMatrix.toArray( matricesArray, geometryId * 16 );
matricesTexture.needsUpdate = true;

Expand Down Expand Up @@ -490,56 +485,48 @@ class BatchedMesh extends Mesh {
// @TODO: Map geometryId to index of the arrays because
// optimize() can make geometryId mismatch the index

const visible = this._visible;
const active = this._active;
const matricesTexture = this._matricesTexture;
const matrices = this._matrices;
const matricesArray = this._matricesTexture.image.data;
if ( geometryId >= matrices.length || active[ geometryId ] === false ) {
const geometryCount = this._geometryCount;
if ( geometryId >= geometryCount || active[ geometryId ] === false ) {

return this;

}

if ( visible[ geometryId ] === true ) {

matrix.toArray( matricesArray, geometryId * 16 );
matricesTexture.needsUpdate = true;

}

matrices[ geometryId ].copy( matrix );
matrix.toArray( matricesArray, geometryId * 16 );
matricesTexture.needsUpdate = true;

return this;

}

getMatrixAt( geometryId, matrix ) {

const matrices = this._matrices;
const active = this._active;
if ( geometryId >= matrices.length || active[ geometryId ] === false ) {
const matricesArray = this._matricesTexture.image.data;
const geometryCount = this._geometryCount;
if ( geometryId >= geometryCount || active[ geometryId ] === false ) {

return matrix;
return null;

}

return matrix.copy( matrices[ geometryId ] );
return matrix.fromArray( matricesArray, geometryId * 16 );

}

setVisibleAt( geometryId, value ) {

const visible = this._visible;
const active = this._active;
const matricesTexture = this._matricesTexture;
const matrices = this._matrices;
const matricesArray = this._matricesTexture.image.data;
const geometryCount = this._geometryCount;

// if the geometry is out of range, not active, or visibility state
// does not change then return early
if (
geometryId >= visible.length ||
geometryId >= geometryCount ||
active[ geometryId ] === false ||
visible[ geometryId ] === value
) {
Expand All @@ -548,18 +535,6 @@ class BatchedMesh extends Mesh {

}

// scale the matrix to zero if it's hidden
if ( value === true ) {

matrices[ geometryId ].toArray( matricesArray, geometryId * 16 );

} else {

_zeroScaleMatrix.toArray( matricesArray, geometryId * 16 );

}

matricesTexture.needsUpdate = true;
visible[ geometryId ] = value;
return this;

Expand All @@ -569,9 +544,10 @@ class BatchedMesh extends Mesh {

const visible = this._visible;
const active = this._active;
const geometryCount = this._geometryCount;

// return early if the geometry is out of range or not active
if ( geometryId >= visible.length || active[ geometryId ] === false ) {
if ( geometryId >= geometryCount || active[ geometryId ] === false ) {

return false;

Expand Down

0 comments on commit 5f4f0b2

Please sign in to comment.