EdgeSplitModifier.js 5.5 KB

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