Skip to content

Commit

Permalink
BVH: Optimize .fromMeshGeometry().
Browse files Browse the repository at this point in the history
  • Loading branch information
Mugen87 committed Feb 19, 2020
1 parent 976a1d5 commit 328e3f1
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 70 deletions.
21 changes: 11 additions & 10 deletions build/yuka.js
Original file line number Diff line number Diff line change
Expand Up @@ -8716,19 +8716,19 @@
}

/**
* Returns a new geometry without containing indices.
* Returns a new geometry without containing indices. If the geometry is already
* non-indexed, the method performs no changes.
*
* @return {MeshGeometry} The new geometry.
* @return {MeshGeometry} The new non-indexed geometry.
*/
toTriangleSoup() {

const indices = this.indices;
const vertices = this.vertices;
let newVertices;

if ( indices ) {

newVertices = new Float32Array( indices.length * 3 );
const vertices = this.vertices;
const newVertices = new Float32Array( indices.length * 3 );

for ( let i = 0, l = indices.length; i < l; i ++ ) {

Expand All @@ -8741,14 +8741,14 @@

}

return new MeshGeometry( newVertices );

} else {

newVertices = new Float32Array( vertices );
return this;

}

return new MeshGeometry( newVertices );

}

/**
Expand Down Expand Up @@ -14059,8 +14059,9 @@

// primitives

const nonIndexedGeometry = geometry.toTriangleSoup();
const vertices = nonIndexedGeometry.vertices;
if ( geometry.indices !== null ) geometry = geometry.toTriangleSoup();

const vertices = geometry.vertices;

for ( let i = 0, l = vertices.length; i < l; i ++ ) {

Expand Down
70 changes: 35 additions & 35 deletions build/yuka.min.js

Large diffs are not rendered by default.

21 changes: 11 additions & 10 deletions build/yuka.module.js
Original file line number Diff line number Diff line change
Expand Up @@ -8710,19 +8710,19 @@ class MeshGeometry {
}

/**
* Returns a new geometry without containing indices.
* Returns a new geometry without containing indices. If the geometry is already
* non-indexed, the method performs no changes.
*
* @return {MeshGeometry} The new geometry.
* @return {MeshGeometry} The new non-indexed geometry.
*/
toTriangleSoup() {

const indices = this.indices;
const vertices = this.vertices;
let newVertices;

if ( indices ) {

newVertices = new Float32Array( indices.length * 3 );
const vertices = this.vertices;
const newVertices = new Float32Array( indices.length * 3 );

for ( let i = 0, l = indices.length; i < l; i ++ ) {

Expand All @@ -8735,14 +8735,14 @@ class MeshGeometry {

}

return new MeshGeometry( newVertices );

} else {

newVertices = new Float32Array( vertices );
return this;

}

return new MeshGeometry( newVertices );

}

/**
Expand Down Expand Up @@ -14053,8 +14053,9 @@ class BVH {

// primitives

const nonIndexedGeometry = geometry.toTriangleSoup();
const vertices = nonIndexedGeometry.vertices;
if ( geometry.indices !== null ) geometry = geometry.toTriangleSoup();

const vertices = geometry.vertices;

for ( let i = 0, l = vertices.length; i < l; i ++ ) {

Expand Down
9 changes: 4 additions & 5 deletions examples/math/bvh/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@
dirLight.shadow.camera.far = 20;
scene.add( dirLight );

var sphereGeometry = new THREE.TorusKnotBufferGeometry( 1, 0.2, 64, 16 );
var sphereMaterial = new THREE.MeshPhongMaterial( { color: 0xee0808 } );
var icoSphere = new THREE.Mesh( sphereGeometry, sphereMaterial );
const sphereGeometry = new THREE.TorusKnotBufferGeometry( 1, 0.2, 64, 16 ).toNonIndexed();
const sphereMaterial = new THREE.MeshPhongMaterial( { color: 0xee0808 } );
const icoSphere = new THREE.Mesh( sphereGeometry, sphereMaterial );
icoSphere.position.set( 0, 1.5, 0 );
icoSphere.updateMatrix();
icoSphere.castShadow = true;
Expand Down Expand Up @@ -122,8 +122,7 @@
threeGeometry.applyMatrix( icoSphere.matrix ); // transform vertices to world space

const vertices = threeGeometry.attributes.position.array;
const indices = threeGeometry.index.array;
meshGeometry = new YUKA.MeshGeometry( vertices, indices );
meshGeometry = new YUKA.MeshGeometry( vertices );

createBVH();

Expand Down
16 changes: 8 additions & 8 deletions src/core/MeshGeometry.js
Original file line number Diff line number Diff line change
Expand Up @@ -264,19 +264,19 @@ class MeshGeometry {
}

/**
* Returns a new geometry without containing indices.
* Returns a new geometry without containing indices. If the geometry is already
* non-indexed, the method performs no changes.
*
* @return {MeshGeometry} The new geometry.
* @return {MeshGeometry} The new non-indexed geometry.
*/
toTriangleSoup() {

const indices = this.indices;
const vertices = this.vertices;
let newVertices;

if ( indices ) {

newVertices = new Float32Array( indices.length * 3 );
const vertices = this.vertices;
const newVertices = new Float32Array( indices.length * 3 );

for ( let i = 0, l = indices.length; i < l; i ++ ) {

Expand All @@ -289,14 +289,14 @@ class MeshGeometry {

}

return new MeshGeometry( newVertices );

} else {

newVertices = new Float32Array( vertices );
return this;

}

return new MeshGeometry( newVertices );

}

/**
Expand Down
5 changes: 3 additions & 2 deletions src/math/BVH.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,9 @@ class BVH {

// primitives

const nonIndexedGeometry = geometry.toTriangleSoup();
const vertices = nonIndexedGeometry.vertices;
if ( geometry.indices !== null ) geometry = geometry.toTriangleSoup();

const vertices = geometry.vertices;

for ( let i = 0, l = vertices.length; i < l; i ++ ) {

Expand Down
29 changes: 29 additions & 0 deletions test/unit_test/core/MeshGeometry.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,35 @@ describe( 'MeshGeometry', function () {

} );

describe( '#toTriangleSoup()', function () {

it( 'should convert an indexed to a non-indexed geometry.', function () {

const vertices = new Float32Array( [ 0, 0, 0, 0.5, 0, 1, 1, 0, 0 ] );
const indices = new Uint16Array( [ 0, 1, 2, 2, 1, 0 ] );

const geometry = new MeshGeometry( vertices, indices ).toTriangleSoup();

expect( geometry.vertices ).to.deep.equal( new Float32Array( [
0, 0, 0, 0.5, 0, 1, 1, 0, 0, // 0, 1, 2
1, 0, 0, 0.5, 0, 1, 0, 0, 0, // 2, 1, 0
] ) );
expect( geometry.indices ).to.be.null;

} );

it( 'should perform no changes if the geometry is already non-indexed', function () {

const vertices = new Float32Array( [ 0, 0, 0, 0.5, 0, 1, 1, 0, 0 ] );

const geometry = new MeshGeometry( vertices );

expect( geometry ).to.be.equal( geometry.toTriangleSoup() );

} );

} );

describe( '#toJSON()', function () {

it( 'should serialize this instance to a JSON object', function () {
Expand Down

0 comments on commit 328e3f1

Please sign in to comment.