Skip to content

Commit db7851f

Browse files
authored
Octree: Fix and optimize fromGraphNode(). (#21834)
1 parent 54f1b36 commit db7851f

File tree

2 files changed

+25
-29
lines changed

2 files changed

+25
-29
lines changed

examples/js/math/Octree.js

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -411,37 +411,35 @@
411411

412412
fromGraphNode( group ) {
413413

414+
group.updateWorldMatrix( true, true );
414415
group.traverse( obj => {
415416

416-
if ( obj.type === 'Mesh' ) {
417+
if ( obj.isMesh === true ) {
417418

418-
obj.updateMatrix();
419-
obj.updateWorldMatrix();
420419
let geometry,
421420
isTemp = false;
422421

423-
if ( obj.geometry.index ) {
422+
if ( obj.geometry.index !== null ) {
424423

425424
isTemp = true;
426-
geometry = obj.geometry.clone().toNonIndexed();
425+
geometry = obj.geometry.toNonIndexed();
427426

428427
} else {
429428

430429
geometry = obj.geometry;
431430

432431
}
433432

434-
const positions = geometry.attributes.position.array;
435-
const transform = obj.matrixWorld;
433+
const positionAttribute = geometry.getAttribute( 'position' );
436434

437-
for ( let i = 0; i < positions.length; i += 9 ) {
435+
for ( let i = 0; i < positionAttribute.count; i += 3 ) {
438436

439-
const v1 = new THREE.Vector3( positions[ i ], positions[ i + 1 ], positions[ i + 2 ] );
440-
const v2 = new THREE.Vector3( positions[ i + 3 ], positions[ i + 4 ], positions[ i + 5 ] );
441-
const v3 = new THREE.Vector3( positions[ i + 6 ], positions[ i + 7 ], positions[ i + 8 ] );
442-
v1.applyMatrix4( transform );
443-
v2.applyMatrix4( transform );
444-
v3.applyMatrix4( transform );
437+
const v1 = new THREE.Vector3().fromBufferAttribute( positionAttribute, i );
438+
const v2 = new THREE.Vector3().fromBufferAttribute( positionAttribute, i + 1 );
439+
const v3 = new THREE.Vector3().fromBufferAttribute( positionAttribute, i + 2 );
440+
v1.applyMatrix4( obj.matrixWorld );
441+
v2.applyMatrix4( obj.matrixWorld );
442+
v3.applyMatrix4( obj.matrixWorld );
445443
this.addTriangle( new THREE.Triangle( v1, v2, v3 ) );
446444

447445
}

examples/jsm/math/Octree.js

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -406,38 +406,36 @@ class Octree {
406406

407407
fromGraphNode( group ) {
408408

409-
group.traverse( ( obj ) => {
409+
group.updateWorldMatrix( true, true );
410410

411-
if ( obj.type === 'Mesh' ) {
411+
group.traverse( ( obj ) => {
412412

413-
obj.updateMatrix();
414-
obj.updateWorldMatrix();
413+
if ( obj.isMesh === true ) {
415414

416415
let geometry, isTemp = false;
417416

418-
if ( obj.geometry.index ) {
417+
if ( obj.geometry.index !== null ) {
419418

420419
isTemp = true;
421-
geometry = obj.geometry.clone().toNonIndexed();
420+
geometry = obj.geometry.toNonIndexed();
422421

423422
} else {
424423

425424
geometry = obj.geometry;
426425

427426
}
428427

429-
const positions = geometry.attributes.position.array;
430-
const transform = obj.matrixWorld;
428+
const positionAttribute = geometry.getAttribute( 'position' );
431429

432-
for ( let i = 0; i < positions.length; i += 9 ) {
430+
for ( let i = 0; i < positionAttribute.count; i += 3 ) {
433431

434-
const v1 = new Vector3( positions[ i ], positions[ i + 1 ], positions[ i + 2 ] );
435-
const v2 = new Vector3( positions[ i + 3 ], positions[ i + 4 ], positions[ i + 5 ] );
436-
const v3 = new Vector3( positions[ i + 6 ], positions[ i + 7 ], positions[ i + 8 ] );
432+
const v1 = new Vector3().fromBufferAttribute( positionAttribute, i );
433+
const v2 = new Vector3().fromBufferAttribute( positionAttribute, i + 1 );
434+
const v3 = new Vector3().fromBufferAttribute( positionAttribute, i + 2 );
437435

438-
v1.applyMatrix4( transform );
439-
v2.applyMatrix4( transform );
440-
v3.applyMatrix4( transform );
436+
v1.applyMatrix4( obj.matrixWorld );
437+
v2.applyMatrix4( obj.matrixWorld );
438+
v3.applyMatrix4( obj.matrixWorld );
441439

442440
this.addTriangle( new Triangle( v1, v2, v3 ) );
443441

0 commit comments

Comments
 (0)