Skip to content

Commit

Permalink
Fix #962
Browse files Browse the repository at this point in the history
  • Loading branch information
gkjohnson committed Feb 12, 2025
1 parent a123fb3 commit bb4d501
Showing 1 changed file with 40 additions and 4 deletions.
44 changes: 40 additions & 4 deletions src/plugins/three/batched/ModelViewBatchedMesh.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,39 @@ export class ModelViewBatchedMesh extends BatchedMesh {
this._lastCameraPos = new Matrix4();
this._forceUpdate = true;

this._matrices = [];

}

setMatrixAt( ...args ) {
setMatrixAt( instanceId, matrix ) {

super.setMatrixAt( ...args );
super.setMatrixAt( instanceId, matrix );
this._forceUpdate = true;

// save the matrices in their original float64 format to avoid
// precision errors when multiplying later
const matrices = this._matrices;
while ( matrices.length <= instanceId ) {

matrices.push( new Matrix4() );

}

matrices[ instanceId ].copy( matrix );

}

setInstanceCount( ...args ) {

super.setInstanceCount( ...args );

const matrices = this._matrices;
while ( matrices.length > this.instanceCount ) {

matrices.pop();

}

}

onBeforeRender( renderer, scene, camera, geometry, material, group ) {
Expand Down Expand Up @@ -61,12 +87,22 @@ export class ModelViewBatchedMesh extends BatchedMesh {
if ( this._forceUpdate || vec1.distanceTo( vec2 ) > this.resetDistance ) {

// transform each objects matrix into local camera frame to avoid precision issues
const matricesArray = matricesTexture.image.data;
const matrices = this._matrices;
const modelViewArray = modelViewMatricesTexture.image.data;
for ( let i = 0; i < this.maxInstanceCount; i ++ ) {

const instanceMatrix = matrices[ i ];
if ( instanceMatrix ) {

matrix.copy( instanceMatrix );

} else {

matrix.identity();

}

matrix
.fromArray( matricesArray, i * 16 )
.premultiply( this.matrixWorld )
.premultiply( camera.matrixWorldInverse )
.toArray( modelViewArray, i * 16 );
Expand Down

0 comments on commit bb4d501

Please sign in to comment.