EdgeSplitModifier.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. ( function () {
  2. const _A = new THREE.Vector3();
  3. const _B = new THREE.Vector3();
  4. const _C = new THREE.Vector3();
  5. class EdgeSplitModifier {
  6. modify( geometry, cutOffAngle, tryKeepNormals = true ) {
  7. function computeNormals() {
  8. normals = new Float32Array( indexes.length * 3 );
  9. for ( let i = 0; i < indexes.length; i += 3 ) {
  10. let index = indexes[ i ];
  11. _A.set( positions[ 3 * index ], positions[ 3 * index + 1 ], positions[ 3 * index + 2 ] );
  12. index = indexes[ i + 1 ];
  13. _B.set( positions[ 3 * index ], positions[ 3 * index + 1 ], positions[ 3 * index + 2 ] );
  14. index = indexes[ i + 2 ];
  15. _C.set( positions[ 3 * index ], positions[ 3 * index + 1 ], positions[ 3 * index + 2 ] );
  16. _C.sub( _B );
  17. _A.sub( _B );
  18. const normal = _C.cross( _A ).normalize();
  19. for ( let j = 0; j < 3; j ++ ) {
  20. normals[ 3 * ( i + j ) ] = normal.x;
  21. normals[ 3 * ( i + j ) + 1 ] = normal.y;
  22. normals[ 3 * ( i + j ) + 2 ] = normal.z;
  23. }
  24. }
  25. }
  26. function mapPositionsToIndexes() {
  27. pointToIndexMap = Array( positions.length / 3 );
  28. for ( let i = 0; i < indexes.length; i ++ ) {
  29. const index = indexes[ i ];
  30. if ( pointToIndexMap[ index ] == null ) {
  31. pointToIndexMap[ index ] = [];
  32. }
  33. pointToIndexMap[ index ].push( i );
  34. }
  35. }
  36. function edgeSplitToGroups( indexes, cutOff, firstIndex ) {
  37. _A.set( normals[ 3 * firstIndex ], normals[ 3 * firstIndex + 1 ], normals[ 3 * firstIndex + 2 ] ).normalize();
  38. const result = {
  39. splitGroup: [],
  40. currentGroup: [ firstIndex ]
  41. };
  42. for ( const j of indexes ) {
  43. if ( j !== firstIndex ) {
  44. _B.set( normals[ 3 * j ], normals[ 3 * j + 1 ], normals[ 3 * j + 2 ] ).normalize();
  45. if ( _B.dot( _A ) < cutOff ) {
  46. result.splitGroup.push( j );
  47. } else {
  48. result.currentGroup.push( j );
  49. }
  50. }
  51. }
  52. return result;
  53. }
  54. function edgeSplit( indexes, cutOff, original = null ) {
  55. if ( indexes.length === 0 ) return;
  56. const groupResults = [];
  57. for ( const index of indexes ) {
  58. groupResults.push( edgeSplitToGroups( indexes, cutOff, index ) );
  59. }
  60. let result = groupResults[ 0 ];
  61. for ( const groupResult of groupResults ) {
  62. if ( groupResult.currentGroup.length > result.currentGroup.length ) {
  63. result = groupResult;
  64. }
  65. }
  66. if ( original != null ) {
  67. splitIndexes.push( {
  68. original: original,
  69. indexes: result.currentGroup
  70. } );
  71. }
  72. if ( result.splitGroup.length ) {
  73. edgeSplit( result.splitGroup, cutOff, original || result.currentGroup[ 0 ] );
  74. }
  75. }
  76. if ( geometry.isGeometry === true ) {
  77. console.error( 'THREE.EdgeSplitModifier no longer supports THREE.Geometry. Use THREE.BufferGeometry instead.' );
  78. return;
  79. }
  80. let hadNormals = false;
  81. let oldNormals = null;
  82. if ( geometry.attributes.normal ) {
  83. hadNormals = true;
  84. geometry = geometry.clone();
  85. if ( tryKeepNormals === true && geometry.index !== null ) {
  86. oldNormals = geometry.attributes.normal.array;
  87. }
  88. geometry.deleteAttribute( 'normal' );
  89. }
  90. if ( geometry.index == null ) {
  91. if ( THREE.BufferGeometryUtils === undefined ) {
  92. throw 'THREE.EdgeSplitModifier relies on THREE.BufferGeometryUtils';
  93. }
  94. geometry = THREE.BufferGeometryUtils.mergeVertices( geometry );
  95. }
  96. const indexes = geometry.index.array;
  97. const positions = geometry.getAttribute( 'position' ).array;
  98. let normals;
  99. let pointToIndexMap;
  100. computeNormals();
  101. mapPositionsToIndexes();
  102. const splitIndexes = [];
  103. for ( const vertexIndexes of pointToIndexMap ) {
  104. edgeSplit( vertexIndexes, Math.cos( cutOffAngle ) - 0.001 );
  105. }
  106. const newAttributes = {};
  107. for ( const name of Object.keys( geometry.attributes ) ) {
  108. const oldAttribute = geometry.attributes[ name ];
  109. const newArray = new oldAttribute.array.constructor( ( indexes.length + splitIndexes.length ) * oldAttribute.itemSize );
  110. newArray.set( oldAttribute.array );
  111. newAttributes[ name ] = new THREE.BufferAttribute( newArray, oldAttribute.itemSize, oldAttribute.normalized );
  112. }
  113. const newIndexes = new Uint32Array( indexes.length );
  114. newIndexes.set( indexes );
  115. for ( let i = 0; i < splitIndexes.length; i ++ ) {
  116. const split = splitIndexes[ i ];
  117. const index = indexes[ split.original ];
  118. for ( const attribute of Object.values( newAttributes ) ) {
  119. for ( let j = 0; j < attribute.itemSize; j ++ ) {
  120. attribute.array[ ( indexes.length + i ) * attribute.itemSize + j ] = attribute.array[ index * attribute.itemSize + j ];
  121. }
  122. }
  123. for ( const j of split.indexes ) {
  124. newIndexes[ j ] = indexes.length + i;
  125. }
  126. }
  127. geometry = new THREE.BufferGeometry();
  128. geometry.setIndex( new THREE.BufferAttribute( newIndexes, 1 ) );
  129. for ( const name of Object.keys( newAttributes ) ) {
  130. geometry.setAttribute( name, newAttributes[ name ] );
  131. }
  132. if ( hadNormals ) {
  133. geometry.computeVertexNormals();
  134. if ( oldNormals !== null ) {
  135. const changedNormals = new Array( oldNormals.length / 3 ).fill( false );
  136. for ( const splitData of splitIndexes ) changedNormals[ splitData.original ] = true;
  137. for ( let i = 0; i < changedNormals.length; i ++ ) {
  138. if ( changedNormals[ i ] === false ) {
  139. for ( let j = 0; j < 3; j ++ ) geometry.attributes.normal.array[ 3 * i + j ] = oldNormals[ 3 * i + j ];
  140. }
  141. }
  142. }
  143. }
  144. return geometry;
  145. }
  146. }
  147. THREE.EdgeSplitModifier = EdgeSplitModifier;
  148. } )();