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

InstancedMesh: Add bounding volumes. #21507

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20,072 changes: 11,224 additions & 8,848 deletions build/three.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion build/three.min.js

Large diffs are not rendered by default.

882 changes: 512 additions & 370 deletions build/three.module.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions examples/jsm/modifiers/CurveModifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ export class InstancedFlow extends Flow {
);
this.object3D.setMatrixAt( index, matrix );
this.object3D.instanceMatrix.needsUpdate = true;
this.object3D.computeBoundingSphere();

}

Expand Down
1 change: 1 addition & 0 deletions examples/jsm/physics/AmmoPhysics.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ async function AmmoPhysics() {
}

mesh.instanceMatrix.needsUpdate = true;
mesh.computeBoundingSphere();

} else if ( mesh.isMesh ) {

Expand Down
1 change: 1 addition & 0 deletions examples/webgl_buffergeometry_instancing_interleaved.html
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@
}

mesh.instanceMatrix.needsUpdate = true;
mesh.computeBoundingSphere();

lastTime = time;

Expand Down
8 changes: 7 additions & 1 deletion examples/webgl_instancing_dynamic.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

import * as THREE from '../build/three.module.js';

import { OrbitControls } from './jsm/controls/OrbitControls.js';

import Stats from './jsm/libs/stats.module.js';
import { GUI } from './jsm/libs/dat.gui.module.js';

Expand All @@ -29,7 +31,6 @@

camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 0.1, 100 );
camera.position.set( amount * 0.9, amount * 0.9, amount * 0.9 );
camera.lookAt( 0, 0, 0 );

scene = new THREE.Scene();

Expand Down Expand Up @@ -70,6 +71,10 @@

window.addEventListener( 'resize', onWindowResize );

const controls = new OrbitControls( camera, renderer.domElement );
controls.minDistance = 10;
controls.maxDistance = 50;

}

function onWindowResize() {
Expand Down Expand Up @@ -126,6 +131,7 @@
}

mesh.instanceMatrix.needsUpdate = true;
mesh.computeBoundingSphere();

}

Expand Down
1 change: 1 addition & 0 deletions examples/webgl_instancing_modified.html
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@
}

mesh.instanceMatrix.needsUpdate = true;
mesh.computeBoundingSphere();

}

Expand Down
3 changes: 3 additions & 0 deletions examples/webgl_instancing_scatter.html
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,9 @@
stemMesh.instanceMatrix.needsUpdate = true;
blossomMesh.instanceMatrix.needsUpdate = true;

stemMesh.computeBoundingSphere();
blossomMesh.computeBoundingSphere();

}

renderer.render( scene, camera );
Expand Down
29 changes: 23 additions & 6 deletions src/math/Box3.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,21 +209,38 @@ class Box3 {

object.updateWorldMatrix( false, false );

const geometry = object.geometry;
if ( object.boundingBox !== undefined ) {

if ( geometry !== undefined ) {
if ( object.boundingBox === null ) {

if ( geometry.boundingBox === null ) {

geometry.computeBoundingBox();
object.computeBoundingBox();

}

_box.copy( geometry.boundingBox );
_box.copy( object.boundingBox );
_box.applyMatrix4( object.matrixWorld );

this.union( _box );

} else {

const geometry = object.geometry;

if ( geometry !== undefined ) {

if ( geometry.boundingBox === null ) {

geometry.computeBoundingBox();

}

_box.copy( geometry.boundingBox );
Mugen87 marked this conversation as resolved.
Show resolved Hide resolved
_box.applyMatrix4( object.matrixWorld );

this.union( _box );

}

}

const children = object.children;
Expand Down
16 changes: 13 additions & 3 deletions src/math/Frustum.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,21 @@ class Frustum {

intersectsObject( object ) {

const geometry = object.geometry;
if ( object.boundingSphere !== undefined ) {

if ( geometry.boundingSphere === null ) geometry.computeBoundingSphere();
if ( object.boundingSphere === null ) object.computeBoundingSphere();

_sphere.copy( geometry.boundingSphere ).applyMatrix4( object.matrixWorld );
_sphere.copy( object.boundingSphere ).applyMatrix4( object.matrixWorld );

} else {

const geometry = object.geometry;

if ( geometry.boundingSphere === null ) geometry.computeBoundingSphere();

_sphere.copy( geometry.boundingSphere ).applyMatrix4( object.matrixWorld );

}

return this.intersectsSphere( _sphere );

Expand Down
73 changes: 72 additions & 1 deletion src/objects/InstancedMesh.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import { BufferAttribute } from '../core/BufferAttribute.js';
import { Mesh } from './Mesh.js';
import { Box3 } from '../math/Box3.js';
import { Matrix4 } from '../math/Matrix4.js';
import { Sphere } from '../math/Sphere.js';

const _instanceLocalMatrix = new Matrix4();
const _instanceWorldMatrix = new Matrix4();

const _instanceIntersects = [];

const _box3 = new Box3();
const _mesh = new Mesh();
const _sphere = new Sphere();

function InstancedMesh( geometry, material, count ) {

Expand All @@ -18,7 +22,8 @@ function InstancedMesh( geometry, material, count ) {

this.count = count;

this.frustumCulled = false;
this.boundingBox = null;
this.boundingSphere = null;

}

Expand All @@ -28,6 +33,72 @@ InstancedMesh.prototype = Object.assign( Object.create( Mesh.prototype ), {

isInstancedMesh: true,

computeBoundingBox: function () {

const geometry = this.geometry;
const count = this.count;

if ( this.boundingBox === null ) {

this.boundingBox = new Box3();

}

if ( geometry.boundingBox === null ) {

geometry.computeBoundingBox();

}

this.boundingBox.makeEmpty();

for ( let i = 0; i < count; i ++ ) {

this.getMatrixAt( i, _instanceLocalMatrix );

_box3.copy( geometry.boundingBox ).applyMatrix4( _instanceLocalMatrix );

this.boundingBox.union( _box3 );

}

return this;

},

computeBoundingSphere: function () {

const geometry = this.geometry;
const count = this.count;

if ( this.boundingSphere === null ) {

this.boundingSphere = new Sphere();

}

if ( geometry.boundingSphere === null ) {

geometry.computeBoundingSphere();

}

this.boundingSphere.makeEmpty();

for ( let i = 0; i < count; i ++ ) {

this.getMatrixAt( i, _instanceLocalMatrix );

_sphere.copy( geometry.boundingSphere ).applyMatrix4( _instanceLocalMatrix );

this.boundingSphere.union( _sphere );

}

return this;

},

copy: function ( source ) {

Mesh.prototype.copy.call( this, source );
Expand Down