|
|
@@ -1314,105 +1314,172 @@ function mergeGroups( geometry ) {
|
|
|
*/
|
|
|
function toCreasedNormals( geometry, creaseAngle = Math.PI / 3 /* 60 degrees */ ) {
|
|
|
|
|
|
+ // BufferGeometry.toNonIndexed() warns if the geometry is non-indexed
|
|
|
+ // and returns the original geometry
|
|
|
+ const resultGeometry = geometry.index ? geometry.toNonIndexed() : geometry;
|
|
|
+ const posAttr = resultGeometry.attributes.position;
|
|
|
+ const vertexCount = posAttr.count;
|
|
|
+
|
|
|
+ let positions;
|
|
|
+
|
|
|
+ if ( posAttr.isBufferAttribute === true && posAttr.itemSize === 3 && posAttr.normalized === false ) {
|
|
|
+
|
|
|
+ positions = posAttr.array;
|
|
|
+
|
|
|
+ } else {
|
|
|
+
|
|
|
+ // flatten the position buffer so the math below operates on plain numbers
|
|
|
+ positions = new Float64Array( vertexCount * 3 );
|
|
|
+
|
|
|
+ for ( let i = 0; i < vertexCount; i ++ ) {
|
|
|
+
|
|
|
+ positions[ 3 * i + 0 ] = posAttr.getX( i );
|
|
|
+ positions[ 3 * i + 1 ] = posAttr.getY( i );
|
|
|
+ positions[ 3 * i + 2 ] = posAttr.getZ( i );
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
const creaseDot = Math.cos( creaseAngle );
|
|
|
const hashMultiplier = ( 1 + 1e-10 ) * 1e2;
|
|
|
+ const faceCount = vertexCount / 3;
|
|
|
+
|
|
|
+ // compute the normal of each face
|
|
|
+ const faceNormals = new Float64Array( faceCount * 3 );
|
|
|
+ for ( let f = 0; f < faceCount; f ++ ) {
|
|
|
+
|
|
|
+ const f9 = 9 * f;
|
|
|
+ const ax = positions[ f9 + 0 ], ay = positions[ f9 + 1 ], az = positions[ f9 + 2 ];
|
|
|
+ const bx = positions[ f9 + 3 ], by = positions[ f9 + 4 ], bz = positions[ f9 + 5 ];
|
|
|
+ const cx = positions[ f9 + 6 ], cy = positions[ f9 + 7 ], cz = positions[ f9 + 8 ];
|
|
|
|
|
|
- // reusable vectors
|
|
|
- const verts = [ new Vector3(), new Vector3(), new Vector3() ];
|
|
|
- const tempVec1 = new Vector3();
|
|
|
- const tempVec2 = new Vector3();
|
|
|
- const tempNorm = new Vector3();
|
|
|
- const tempNorm2 = new Vector3();
|
|
|
+ const v1x = cx - bx, v1y = cy - by, v1z = cz - bz;
|
|
|
+ const v2x = ax - bx, v2y = ay - by, v2z = az - bz;
|
|
|
|
|
|
- // hashes a vector
|
|
|
- function hashVertex( v ) {
|
|
|
+ const nx = v1y * v2z - v1z * v2y;
|
|
|
+ const ny = v1z * v2x - v1x * v2z;
|
|
|
+ const nz = v1x * v2y - v1y * v2x;
|
|
|
|
|
|
- const x = ~ ~ ( v.x * hashMultiplier );
|
|
|
- const y = ~ ~ ( v.y * hashMultiplier );
|
|
|
- const z = ~ ~ ( v.z * hashMultiplier );
|
|
|
- return `${x},${y},${z}`;
|
|
|
+ const invLength = 1 / ( Math.sqrt( nx * nx + ny * ny + nz * nz ) || 1 );
|
|
|
+ faceNormals[ 3 * f + 0 ] = nx * invLength;
|
|
|
+ faceNormals[ 3 * f + 1 ] = ny * invLength;
|
|
|
+ faceNormals[ 3 * f + 2 ] = nz * invLength;
|
|
|
|
|
|
}
|
|
|
|
|
|
- // BufferGeometry.toNonIndexed() warns if the geometry is non-indexed
|
|
|
- // and returns the original geometry
|
|
|
- const resultGeometry = geometry.index ? geometry.toNonIndexed() : geometry;
|
|
|
- const posAttr = resultGeometry.attributes.position;
|
|
|
- const vertexMap = {};
|
|
|
+ // assign an id to each vertex, sharing the id between vertices with the same
|
|
|
+ // quantized position via an open-addressed hash table (slots hold id + 1, 0 means empty)
|
|
|
+ const vertexIds = new Int32Array( vertexCount );
|
|
|
+ const quantized = new Int32Array( vertexCount * 3 );
|
|
|
|
|
|
- // find all the normals shared by commonly located vertices
|
|
|
- for ( let i = 0, l = posAttr.count / 3; i < l; i ++ ) {
|
|
|
+ let tableSize = 1;
|
|
|
+ while ( tableSize < vertexCount * 2 ) tableSize <<= 1;
|
|
|
+ const tableMask = tableSize - 1;
|
|
|
+ const table = new Int32Array( tableSize );
|
|
|
+
|
|
|
+ let uniqueCount = 0;
|
|
|
+ for ( let i = 0; i < vertexCount; i ++ ) {
|
|
|
|
|
|
const i3 = 3 * i;
|
|
|
- const a = verts[ 0 ].fromBufferAttribute( posAttr, i3 + 0 );
|
|
|
- const b = verts[ 1 ].fromBufferAttribute( posAttr, i3 + 1 );
|
|
|
- const c = verts[ 2 ].fromBufferAttribute( posAttr, i3 + 2 );
|
|
|
+ const qx = ~ ~ ( positions[ i3 + 0 ] * hashMultiplier );
|
|
|
+ const qy = ~ ~ ( positions[ i3 + 1 ] * hashMultiplier );
|
|
|
+ const qz = ~ ~ ( positions[ i3 + 2 ] * hashMultiplier );
|
|
|
|
|
|
- tempVec1.subVectors( c, b );
|
|
|
- tempVec2.subVectors( a, b );
|
|
|
+ let slot = ( Math.imul( qx, 73856093 ) ^ Math.imul( qy, 19349663 ) ^ Math.imul( qz, 83492791 ) ) & tableMask;
|
|
|
|
|
|
- // add the normal to the map for all vertices
|
|
|
- const normal = new Vector3().crossVectors( tempVec1, tempVec2 ).normalize();
|
|
|
- for ( let n = 0; n < 3; n ++ ) {
|
|
|
+ while ( true ) {
|
|
|
+
|
|
|
+ const id = table[ slot ];
|
|
|
+
|
|
|
+ if ( id === 0 ) {
|
|
|
+
|
|
|
+ const q3 = 3 * uniqueCount;
|
|
|
+ quantized[ q3 + 0 ] = qx;
|
|
|
+ quantized[ q3 + 1 ] = qy;
|
|
|
+ quantized[ q3 + 2 ] = qz;
|
|
|
+
|
|
|
+ table[ slot ] = uniqueCount + 1;
|
|
|
+ vertexIds[ i ] = uniqueCount ++;
|
|
|
+ break;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ const q3 = 3 * ( id - 1 );
|
|
|
|
|
|
- const vert = verts[ n ];
|
|
|
- const hash = hashVertex( vert );
|
|
|
- if ( ! ( hash in vertexMap ) ) {
|
|
|
+ if ( quantized[ q3 + 0 ] === qx && quantized[ q3 + 1 ] === qy && quantized[ q3 + 2 ] === qz ) {
|
|
|
|
|
|
- vertexMap[ hash ] = [];
|
|
|
+ vertexIds[ i ] = id - 1;
|
|
|
+ break;
|
|
|
|
|
|
}
|
|
|
|
|
|
- vertexMap[ hash ].push( normal );
|
|
|
+ slot = ( slot + 1 ) & tableMask;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
- // average normals from all vertices that share a common location if they are within the
|
|
|
- // provided crease threshold
|
|
|
- const normalArray = new Float32Array( posAttr.count * 3 );
|
|
|
- const normAttr = new BufferAttribute( normalArray, 3, false );
|
|
|
- for ( let i = 0, l = posAttr.count / 3; i < l; i ++ ) {
|
|
|
+ // bucket the faces surrounding each unique vertex position
|
|
|
+ const bucketOffsets = new Int32Array( uniqueCount + 1 );
|
|
|
+ for ( let i = 0; i < vertexCount; i ++ ) bucketOffsets[ vertexIds[ i ] + 1 ] ++;
|
|
|
+ for ( let i = 0; i < uniqueCount; i ++ ) bucketOffsets[ i + 1 ] += bucketOffsets[ i ];
|
|
|
|
|
|
- // get the face normal for this vertex
|
|
|
- const i3 = 3 * i;
|
|
|
- const a = verts[ 0 ].fromBufferAttribute( posAttr, i3 + 0 );
|
|
|
- const b = verts[ 1 ].fromBufferAttribute( posAttr, i3 + 1 );
|
|
|
- const c = verts[ 2 ].fromBufferAttribute( posAttr, i3 + 2 );
|
|
|
+ const bucketFaces = new Int32Array( vertexCount );
|
|
|
+ const bucketCursors = bucketOffsets.slice( 0, uniqueCount );
|
|
|
+ for ( let f = 0; f < faceCount; f ++ ) {
|
|
|
+
|
|
|
+ const f3 = 3 * f;
|
|
|
+ bucketFaces[ bucketCursors[ vertexIds[ f3 + 0 ] ] ++ ] = f;
|
|
|
+ bucketFaces[ bucketCursors[ vertexIds[ f3 + 1 ] ] ++ ] = f;
|
|
|
+ bucketFaces[ bucketCursors[ vertexIds[ f3 + 2 ] ] ++ ] = f;
|
|
|
|
|
|
- tempVec1.subVectors( c, b );
|
|
|
- tempVec2.subVectors( a, b );
|
|
|
+ }
|
|
|
+
|
|
|
+ // average the normals of the faces surrounding each vertex if they are within the
|
|
|
+ // provided crease threshold
|
|
|
+ const normalArray = new Float32Array( vertexCount * 3 );
|
|
|
+ for ( let f = 0; f < faceCount; f ++ ) {
|
|
|
|
|
|
- tempNorm.crossVectors( tempVec1, tempVec2 ).normalize();
|
|
|
+ const f3 = 3 * f;
|
|
|
+ const nx = faceNormals[ f3 + 0 ];
|
|
|
+ const ny = faceNormals[ f3 + 1 ];
|
|
|
+ const nz = faceNormals[ f3 + 2 ];
|
|
|
|
|
|
- // average all normals that meet the threshold and set the normal value
|
|
|
for ( let n = 0; n < 3; n ++ ) {
|
|
|
|
|
|
- const vert = verts[ n ];
|
|
|
- const hash = hashVertex( vert );
|
|
|
- const otherNormals = vertexMap[ hash ];
|
|
|
- tempNorm2.set( 0, 0, 0 );
|
|
|
+ const i = f3 + n;
|
|
|
+ const id = vertexIds[ i ];
|
|
|
+
|
|
|
+ let sumX = 0, sumY = 0, sumZ = 0;
|
|
|
+
|
|
|
+ for ( let k = bucketOffsets[ id ], end = bucketOffsets[ id + 1 ]; k < end; k ++ ) {
|
|
|
|
|
|
- for ( let k = 0, lk = otherNormals.length; k < lk; k ++ ) {
|
|
|
+ const o3 = 3 * bucketFaces[ k ];
|
|
|
+ const ox = faceNormals[ o3 + 0 ];
|
|
|
+ const oy = faceNormals[ o3 + 1 ];
|
|
|
+ const oz = faceNormals[ o3 + 2 ];
|
|
|
|
|
|
- const otherNorm = otherNormals[ k ];
|
|
|
- if ( tempNorm.dot( otherNorm ) > creaseDot ) {
|
|
|
+ if ( nx * ox + ny * oy + nz * oz > creaseDot ) {
|
|
|
|
|
|
- tempNorm2.add( otherNorm );
|
|
|
+ sumX += ox;
|
|
|
+ sumY += oy;
|
|
|
+ sumZ += oz;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
- tempNorm2.normalize();
|
|
|
- normAttr.setXYZ( i3 + n, tempNorm2.x, tempNorm2.y, tempNorm2.z );
|
|
|
+ const invLength = 1 / ( Math.sqrt( sumX * sumX + sumY * sumY + sumZ * sumZ ) || 1 );
|
|
|
+ normalArray[ 3 * i + 0 ] = sumX * invLength;
|
|
|
+ normalArray[ 3 * i + 1 ] = sumY * invLength;
|
|
|
+ normalArray[ 3 * i + 2 ] = sumZ * invLength;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
- resultGeometry.setAttribute( 'normal', normAttr );
|
|
|
+ resultGeometry.setAttribute( 'normal', new BufferAttribute( normalArray, 3, false ) );
|
|
|
return resultGeometry;
|
|
|
|
|
|
}
|