|
@@ -716,66 +716,62 @@ BufferGeometry.prototype = Object.assign( Object.create( EventDispatcher.prototy
|
|
|
computeVertexNormals: function () {
|
|
|
|
|
|
const index = this.index;
|
|
|
- const attributes = this.attributes;
|
|
|
+ const positionAttribute = this.getAttribute( 'position' );
|
|
|
|
|
|
- if ( attributes.position ) {
|
|
|
+ if ( positionAttribute !== undefined ) {
|
|
|
|
|
|
- const positions = attributes.position.array;
|
|
|
+ let normalAttribute = this.getAttribute( 'normal' );
|
|
|
|
|
|
- if ( attributes.normal === undefined ) {
|
|
|
+ if ( normalAttribute === undefined ) {
|
|
|
|
|
|
- this.setAttribute( 'normal', new BufferAttribute( new Float32Array( positions.length ), 3 ) );
|
|
|
+ normalAttribute = new BufferAttribute( new Float32Array( positionAttribute.count * 3 ), 3 );
|
|
|
+ this.setAttribute( 'normal', normalAttribute );
|
|
|
|
|
|
} else {
|
|
|
|
|
|
// reset existing normals to zero
|
|
|
|
|
|
- const array = attributes.normal.array;
|
|
|
-
|
|
|
- for ( let i = 0, il = array.length; i < il; i ++ ) {
|
|
|
+ for ( let i = 0, il = normalAttribute.count; i < il; i ++ ) {
|
|
|
|
|
|
- array[ i ] = 0;
|
|
|
+ normalAttribute.setXYZ( i, 0, 0, 0 );
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
- const normals = attributes.normal.array;
|
|
|
-
|
|
|
const pA = new Vector3(), pB = new Vector3(), pC = new Vector3();
|
|
|
+ const nA = new Vector3(), nB = new Vector3(), nC = new Vector3();
|
|
|
const cb = new Vector3(), ab = new Vector3();
|
|
|
|
|
|
// indexed elements
|
|
|
|
|
|
if ( index ) {
|
|
|
|
|
|
- const indices = index.array;
|
|
|
-
|
|
|
for ( let i = 0, il = index.count; i < il; i += 3 ) {
|
|
|
|
|
|
- const vA = indices[ i + 0 ] * 3;
|
|
|
- const vB = indices[ i + 1 ] * 3;
|
|
|
- const vC = indices[ i + 2 ] * 3;
|
|
|
+ const vA = index.getX( i + 0 );
|
|
|
+ const vB = index.getX( i + 1 );
|
|
|
+ const vC = index.getX( i + 2 );
|
|
|
|
|
|
- pA.fromArray( positions, vA );
|
|
|
- pB.fromArray( positions, vB );
|
|
|
- pC.fromArray( positions, vC );
|
|
|
+ pA.fromBufferAttribute( positionAttribute, vA );
|
|
|
+ pB.fromBufferAttribute( positionAttribute, vB );
|
|
|
+ pC.fromBufferAttribute( positionAttribute, vC );
|
|
|
|
|
|
cb.subVectors( pC, pB );
|
|
|
ab.subVectors( pA, pB );
|
|
|
cb.cross( ab );
|
|
|
|
|
|
- normals[ vA ] += cb.x;
|
|
|
- normals[ vA + 1 ] += cb.y;
|
|
|
- normals[ vA + 2 ] += cb.z;
|
|
|
+ nA.fromBufferAttribute( normalAttribute, vA );
|
|
|
+ nB.fromBufferAttribute( normalAttribute, vB );
|
|
|
+ nC.fromBufferAttribute( normalAttribute, vC );
|
|
|
|
|
|
- normals[ vB ] += cb.x;
|
|
|
- normals[ vB + 1 ] += cb.y;
|
|
|
- normals[ vB + 2 ] += cb.z;
|
|
|
+ nA.add( cb );
|
|
|
+ nB.add( cb );
|
|
|
+ nC.add( cb );
|
|
|
|
|
|
- normals[ vC ] += cb.x;
|
|
|
- normals[ vC + 1 ] += cb.y;
|
|
|
- normals[ vC + 2 ] += cb.z;
|
|
|
+ normalAttribute.setXYZ( vA, nA.x, nA.y, nA.z );
|
|
|
+ normalAttribute.setXYZ( vB, nB.x, nB.y, nB.z );
|
|
|
+ normalAttribute.setXYZ( vC, nC.x, nC.y, nC.z );
|
|
|
|
|
|
}
|
|
|
|
|
@@ -783,27 +779,19 @@ BufferGeometry.prototype = Object.assign( Object.create( EventDispatcher.prototy
|
|
|
|
|
|
// non-indexed elements (unconnected triangle soup)
|
|
|
|
|
|
- for ( let i = 0, il = positions.length; i < il; i += 9 ) {
|
|
|
+ for ( let i = 0, il = positionAttribute.count; i < il; i += 3 ) {
|
|
|
|
|
|
- pA.fromArray( positions, i );
|
|
|
- pB.fromArray( positions, i + 3 );
|
|
|
- pC.fromArray( positions, i + 6 );
|
|
|
+ pA.fromBufferAttribute( positionAttribute, i + 0 );
|
|
|
+ pB.fromBufferAttribute( positionAttribute, i + 1 );
|
|
|
+ pC.fromBufferAttribute( positionAttribute, i + 2 );
|
|
|
|
|
|
cb.subVectors( pC, pB );
|
|
|
ab.subVectors( pA, pB );
|
|
|
cb.cross( ab );
|
|
|
|
|
|
- normals[ i ] = cb.x;
|
|
|
- normals[ i + 1 ] = cb.y;
|
|
|
- normals[ i + 2 ] = cb.z;
|
|
|
-
|
|
|
- normals[ i + 3 ] = cb.x;
|
|
|
- normals[ i + 4 ] = cb.y;
|
|
|
- normals[ i + 5 ] = cb.z;
|
|
|
-
|
|
|
- normals[ i + 6 ] = cb.x;
|
|
|
- normals[ i + 7 ] = cb.y;
|
|
|
- normals[ i + 8 ] = cb.z;
|
|
|
+ normalAttribute.setXYZ( i + 0, cb.x, cb.y, cb.z );
|
|
|
+ normalAttribute.setXYZ( i + 1, cb.x, cb.y, cb.z );
|
|
|
+ normalAttribute.setXYZ( i + 2, cb.x, cb.y, cb.z );
|
|
|
|
|
|
}
|
|
|
|
|
@@ -811,7 +799,7 @@ BufferGeometry.prototype = Object.assign( Object.create( EventDispatcher.prototy
|
|
|
|
|
|
this.normalizeNormals();
|
|
|
|
|
|
- attributes.normal.needsUpdate = true;
|
|
|
+ normalAttribute.needsUpdate = true;
|
|
|
|
|
|
}
|
|
|
|