Skip to content

Commit

Permalink
Merge pull request #703 from gkjohnson/batched-bvh-helper
Browse files Browse the repository at this point in the history
MeshBVHHelper: Update BVH Helper for instances and batching
  • Loading branch information
gkjohnson authored Sep 2, 2024
2 parents 2417200 + b9b076c commit be976e6
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,14 @@ displayEdges = true : Boolean

If true displays the bounds as edges other displays the bounds as solid meshes.

### .objectIndex

```js
objectIndex = 0 : Number
```

When using an `InstancedMesh` or a `BatchedMesh` this refers to the item index to use for the BVH and / or matrix transformation to use.

### .edgeMaterial

```js
Expand Down
29 changes: 27 additions & 2 deletions src/objects/MeshBVHHelper.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { LineBasicMaterial, BufferAttribute, Box3, Group, MeshBasicMaterial, Object3D, BufferGeometry, Mesh } from 'three';
import { LineBasicMaterial, BufferAttribute, Box3, Group, MeshBasicMaterial, Object3D, BufferGeometry, Mesh, Matrix4 } from 'three';
import { arrayToBox } from '../utils/ArrayBoxUtilities.js';
import { MeshBVH } from '../core/MeshBVH.js';

const boundingBox = /* @__PURE__ */ new Box3();
const matrix = /* @__PURE__ */ new Matrix4();

class MeshBVHRootHelper extends Object3D {

get isMesh() {
Expand Down Expand Up @@ -254,6 +256,7 @@ class MeshBVHHelper extends Group {
this.bvh = bvh;
this.displayParents = false;
this.displayEdges = true;
this.objectIndex = 0;
this._roots = [];

const edgeMaterial = new LineBasicMaterial( {
Expand Down Expand Up @@ -281,7 +284,21 @@ class MeshBVHHelper extends Group {

update() {

const bvh = this.bvh || this.mesh.geometry.boundsTree;
const mesh = this.mesh;
let bvh = this.bvh || mesh.geometry.boundsTree || null;
if ( mesh.isBatchedMesh && mesh.boundsTrees && ! bvh ) {

// get the bvh from a batchedMesh if not provided
// TODO: we should have an official way to get the geometry index cleanly
const drawInfo = mesh._drawInfo[ this.objectIndex ];
if ( drawInfo ) {

bvh = mesh.boundsTrees[ drawInfo.geometryIndex ] || bvh;

}

}

const totalRoots = bvh ? bvh._roots.length : 0;
while ( this._roots.length > totalRoots ) {

Expand Down Expand Up @@ -338,6 +355,14 @@ class MeshBVHHelper extends Group {

}

// handle batched and instanced mesh bvhs
if ( mesh.isInstancedMesh || mesh.isBatchedMesh ) {

mesh.getMatrixAt( this.objectIndex, matrix );
this.matrix.multiply( matrix );

}

this.matrix.decompose(
this.position,
this.quaternion,
Expand Down

0 comments on commit be976e6

Please sign in to comment.