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: Remove unneeded & redundant matrices array #27130

Merged
merged 17 commits into from Nov 6, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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;
Comment on lines -523 to +512
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there is no data for the matrix then we just return null here to indicate there's no relevant value.


}

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