From a99b79469bcf09e46a1f79499bd86fb874fee989 Mon Sep 17 00:00:00 2001 From: Garrett Johnson Date: Sat, 7 Dec 2024 22:53:05 +0900 Subject: [PATCH] Update ModelViewBatchedMesh.js --- example/src/plugins/batched/ModelViewBatchedMesh.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/example/src/plugins/batched/ModelViewBatchedMesh.js b/example/src/plugins/batched/ModelViewBatchedMesh.js index 0c379575c..3d4293f1a 100644 --- a/example/src/plugins/batched/ModelViewBatchedMesh.js +++ b/example/src/plugins/batched/ModelViewBatchedMesh.js @@ -25,14 +25,16 @@ export class ModelViewBatchedMesh extends BatchedMesh { onBeforeRender( renderer, scene, camera, geometry, material, group ) { + // ensure matrices are complete and up to date super.onBeforeRender( renderer, scene, camera, geometry, material, group ); + // retrieve camera before and after camera positions vec1.setFromMatrixPosition( camera.matrixWorld ); vec2.setFromMatrixPosition( this._lastCameraPos ); + // initialize the model-view matrix texture if needed const matricesTexture = this._matricesTexture; let modelViewMatricesTexture = this._modelViewMatricesTexture; - if ( ! modelViewMatricesTexture || modelViewMatricesTexture.image.width !== matricesTexture.image.width || @@ -55,9 +57,10 @@ export class ModelViewBatchedMesh extends BatchedMesh { } - + // check if we need to update the model view matrices 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 modelViewArray = modelViewMatricesTexture.image.data; for ( let i = 0; i < this.maxInstanceCount; i ++ ) { @@ -76,6 +79,8 @@ export class ModelViewBatchedMesh extends BatchedMesh { } + // save handles, and transform the matrix world into the camera frame used to position the mesh instances + // to offset the position shift. this._matricesTextureHandle = this._matricesTexture; this._matricesTexture = this._modelViewMatricesTexture; this.matrixWorld.copy( this._lastCameraPos );