EdgeSplitModifier.js 5.4 KB

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