EdgeSplitModifier.js 4.9 KB

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