EdgeSplitModifier.js 5.4 KB

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