EdgeSplitModifier.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. let hadNormals = false;
  77. let oldNormals = null;
  78. if ( geometry.attributes.normal ) {
  79. hadNormals = true;
  80. geometry = geometry.clone();
  81. if ( tryKeepNormals === true && geometry.index !== null ) {
  82. oldNormals = geometry.attributes.normal.array;
  83. }
  84. geometry.deleteAttribute( 'normal' );
  85. }
  86. if ( geometry.index == null ) {
  87. geometry = THREE.BufferGeometryUtils.mergeVertices( geometry );
  88. }
  89. const indexes = geometry.index.array;
  90. const positions = geometry.getAttribute( 'position' ).array;
  91. let normals;
  92. let pointToIndexMap;
  93. computeNormals();
  94. mapPositionsToIndexes();
  95. const splitIndexes = [];
  96. for ( const vertexIndexes of pointToIndexMap ) {
  97. edgeSplit( vertexIndexes, Math.cos( cutOffAngle ) - 0.001 );
  98. }
  99. const newAttributes = {};
  100. for ( const name of Object.keys( geometry.attributes ) ) {
  101. const oldAttribute = geometry.attributes[ name ];
  102. const newArray = new oldAttribute.array.constructor( ( indexes.length + splitIndexes.length ) * oldAttribute.itemSize );
  103. newArray.set( oldAttribute.array );
  104. newAttributes[ name ] = new THREE.BufferAttribute( newArray, oldAttribute.itemSize, oldAttribute.normalized );
  105. }
  106. const newIndexes = new Uint32Array( indexes.length );
  107. newIndexes.set( indexes );
  108. for ( let i = 0; i < splitIndexes.length; i ++ ) {
  109. const split = splitIndexes[ i ];
  110. const index = indexes[ split.original ];
  111. for ( const attribute of Object.values( newAttributes ) ) {
  112. for ( let j = 0; j < attribute.itemSize; j ++ ) {
  113. attribute.array[ ( indexes.length + i ) * attribute.itemSize + j ] = attribute.array[ index * attribute.itemSize + j ];
  114. }
  115. }
  116. for ( const j of split.indexes ) {
  117. newIndexes[ j ] = indexes.length + i;
  118. }
  119. }
  120. geometry = new THREE.BufferGeometry();
  121. geometry.setIndex( new THREE.BufferAttribute( newIndexes, 1 ) );
  122. for ( const name of Object.keys( newAttributes ) ) {
  123. geometry.setAttribute( name, newAttributes[ name ] );
  124. }
  125. if ( hadNormals ) {
  126. geometry.computeVertexNormals();
  127. if ( oldNormals !== null ) {
  128. const changedNormals = new Array( oldNormals.length / 3 ).fill( false );
  129. for ( const splitData of splitIndexes ) changedNormals[ splitData.original ] = true;
  130. for ( let i = 0; i < changedNormals.length; i ++ ) {
  131. if ( changedNormals[ i ] === false ) {
  132. for ( let j = 0; j < 3; j ++ ) geometry.attributes.normal.array[ 3 * i + j ] = oldNormals[ 3 * i + j ];
  133. }
  134. }
  135. }
  136. }
  137. return geometry;
  138. }
  139. }
  140. THREE.EdgeSplitModifier = EdgeSplitModifier;
  141. } )();