2
0

EdgeSplitModifier.js 5.3 KB

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