Skip to content

Commit 25ce027

Browse files
authored
BatchedMesh: Declare local variables to avoid using "this" in functions and improve readability (mrdoob#27045)
1 parent 8fea5d2 commit 25ce027

File tree

1 file changed

+39
-21
lines changed

1 file changed

+39
-21
lines changed

examples/jsm/objects/BatchedMesh.js

Lines changed: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ class BatchedMesh extends Mesh {
199199

200200
// @TODO: geometry.groups support?
201201
// @TODO: geometry.drawRange support?
202-
// @TODO: geometry.mortphAttributes support?
202+
// @TODO: geometry.morphAttributes support?
203203

204204
if ( this._geometryCount >= this._maxGeometryCount ) {
205205

@@ -345,76 +345,94 @@ class BatchedMesh extends Mesh {
345345
// @TODO: Map geometryId to index of the arrays because
346346
// optimize() can make geometryId mismatch the index
347347

348-
if ( geometryId >= this._matrices.length || this._alives[ geometryId ] === false ) {
348+
const visibles = this._visibles;
349+
const alives = this._alives;
350+
const matricesTexture = this._matricesTexture;
351+
const matrices = this._matrices;
352+
const matricesArray = this._matricesArray;
353+
if ( geometryId >= matrices.length || alives[ geometryId ] === false ) {
349354

350355
return this;
351356

352357
}
353358

354-
this._matrices[ geometryId ].copy( matrix );
359+
if ( visibles[ geometryId ] === true ) {
355360

356-
if ( this._visibles[ geometryId ] === true ) {
357-
358-
matrix.toArray( this._matricesArray, geometryId * 16 );
359-
this._matricesTexture.needsUpdate = true;
361+
matrix.toArray( matricesArray, geometryId * 16 );
362+
matricesTexture.needsUpdate = true;
360363

361364
}
362365

366+
matrices[ geometryId ].copy( matrix );
367+
363368
return this;
364369

365370
}
366371

367372
getMatrixAt( geometryId, matrix ) {
368373

369-
if ( geometryId >= this._matrices.length || this._alives[ geometryId ] === false ) {
374+
const matrices = this._matrices;
375+
const alives = this._alives;
376+
if ( geometryId >= matrices.length || alives[ geometryId ] === false ) {
370377

371378
return matrix;
372379

373380
}
374381

375-
return matrix.copy( this._matrices[ geometryId ] );
382+
return matrix.copy( matrices[ geometryId ] );
376383

377384
}
378385

379386
setVisibleAt( geometryId, visible ) {
380387

381-
if ( geometryId >= this._visibles.length || this._alives[ geometryId ] === false ) {
382-
383-
return this;
384-
385-
}
388+
const visibles = this._visibles;
389+
const alives = this._alives;
390+
const matricesTexture = this._matricesTexture;
391+
const matrices = this._matrices;
392+
const matricesArray = this._matricesArray;
386393

387-
if ( this._visibles[ geometryId ] === visible ) {
394+
// if the geometry is out of range, not active, or visibility state
395+
// does not change then return early
396+
if (
397+
geometryId >= visibles.length ||
398+
alives[ geometryId ] === false ||
399+
visibles[ geometryId ] === visible
400+
) {
388401

389402
return this;
390403

391404
}
392405

406+
// scale the matrix to zero if it's hidden
393407
if ( visible === true ) {
394408

395-
this._matrices[ geometryId ].toArray( this._matricesArray, geometryId * 16 );
409+
matrices[ geometryId ].toArray( matricesArray, geometryId * 16 );
396410

397411
} else {
398412

399-
_zeroScaleMatrix.toArray( this._matricesArray, geometryId * 16 );
413+
_zeroScaleMatrix.toArray( matricesArray, geometryId * 16 );
400414

401415
}
402416

403-
this._matricesTexture.needsUpdate = true;
404-
this._visibles[ geometryId ] = visible;
417+
matricesTexture.needsUpdate = true;
418+
visibles[ geometryId ] = visible;
405419
return this;
406420

407421
}
408422

409423
getVisibleAt( geometryId ) {
410424

411-
if ( geometryId >= this._visibles.length || this._alives[ geometryId ] === false ) {
425+
const visibles = this._visibles;
426+
const alives = this._alives;
427+
428+
// return early if the geometry is out of range or not active
429+
if ( geometryId >= visibles.length || alives[ geometryId ] === false ) {
412430

413431
return false;
414432

415433
}
416434

417-
return this._visibles[ geometryId ];
435+
return visibles[ geometryId ];
418436

419437
}
420438

0 commit comments

Comments
 (0)