EdgeSplitModifier.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. import {
  2. BufferAttribute,
  3. BufferGeometry,
  4. Vector3
  5. } from 'three';
  6. import * as BufferGeometryUtils from '../utils/BufferGeometryUtils.js';
  7. const _A = new Vector3();
  8. const _B = new Vector3();
  9. const _C = new Vector3();
  10. /**
  11. * The modifier can be used to split faces at sharp edges. This allows to compute
  12. * normals without smoothing the edges which can lead to an improved visual result.
  13. *
  14. * ```js
  15. * const modifier = new EdgeSplitModifier();
  16. * geometry = modifier.modify( geometry, Math.PI * 0.4 );
  17. * ```
  18. */
  19. class EdgeSplitModifier {
  20. /**
  21. * Returns a new, modified version of the given geometry by applying an edge-split operation.
  22. * Please note that the resulting geometry is always indexed.
  23. *
  24. * @param {BufferGeometry} geometry - The geometry to modify.
  25. * @param {number} cutOffAngle - The cut off angle in radians.
  26. * @param {boolean} [tryKeepNormals=true] - Whether to try to keep normals or not.
  27. * @return {BufferGeometry} A new, modified geometry.
  28. */
  29. modify( geometry, cutOffAngle, tryKeepNormals = true ) {
  30. function computeNormals() {
  31. normals = new Float32Array( indexes.length * 3 );
  32. for ( let i = 0; i < indexes.length; i += 3 ) {
  33. let index = indexes[ i ];
  34. _A.set(
  35. positions[ 3 * index ],
  36. positions[ 3 * index + 1 ],
  37. positions[ 3 * index + 2 ] );
  38. index = indexes[ i + 1 ];
  39. _B.set(
  40. positions[ 3 * index ],
  41. positions[ 3 * index + 1 ],
  42. positions[ 3 * index + 2 ] );
  43. index = indexes[ i + 2 ];
  44. _C.set(
  45. positions[ 3 * index ],
  46. positions[ 3 * index + 1 ],
  47. positions[ 3 * index + 2 ] );
  48. _C.sub( _B );
  49. _A.sub( _B );
  50. const normal = _C.cross( _A ).normalize();
  51. for ( let j = 0; j < 3; j ++ ) {
  52. normals[ 3 * ( i + j ) ] = normal.x;
  53. normals[ 3 * ( i + j ) + 1 ] = normal.y;
  54. normals[ 3 * ( i + j ) + 2 ] = normal.z;
  55. }
  56. }
  57. }
  58. function mapPositionsToIndexes() {
  59. pointToIndexMap = Array( positions.length / 3 );
  60. for ( let i = 0; i < indexes.length; i ++ ) {
  61. const index = indexes[ i ];
  62. if ( pointToIndexMap[ index ] == null ) {
  63. pointToIndexMap[ index ] = [];
  64. }
  65. pointToIndexMap[ index ].push( i );
  66. }
  67. }
  68. function edgeSplitToGroups( indexes, cutOff, firstIndex ) {
  69. _A.set( normals[ 3 * firstIndex ], normals[ 3 * firstIndex + 1 ], normals[ 3 * firstIndex + 2 ] ).normalize();
  70. const result = {
  71. splitGroup: [],
  72. currentGroup: [ firstIndex ]
  73. };
  74. for ( const j of indexes ) {
  75. if ( j !== firstIndex ) {
  76. _B.set( normals[ 3 * j ], normals[ 3 * j + 1 ], normals[ 3 * j + 2 ] ).normalize();
  77. if ( _B.dot( _A ) < cutOff ) {
  78. result.splitGroup.push( j );
  79. } else {
  80. result.currentGroup.push( j );
  81. }
  82. }
  83. }
  84. return result;
  85. }
  86. function edgeSplit( indexes, cutOff, original = null ) {
  87. if ( indexes.length === 0 ) return;
  88. const groupResults = [];
  89. for ( const index of indexes ) {
  90. groupResults.push( edgeSplitToGroups( indexes, cutOff, index ) );
  91. }
  92. let result = groupResults[ 0 ];
  93. for ( const groupResult of groupResults ) {
  94. if ( groupResult.currentGroup.length > result.currentGroup.length ) {
  95. result = groupResult;
  96. }
  97. }
  98. if ( original != null ) {
  99. splitIndexes.push( {
  100. original: original,
  101. indexes: result.currentGroup
  102. } );
  103. }
  104. if ( result.splitGroup.length ) {
  105. edgeSplit( result.splitGroup, cutOff, original || result.currentGroup[ 0 ] );
  106. }
  107. }
  108. let hadNormals = false;
  109. let oldNormals = null;
  110. if ( geometry.attributes.normal ) {
  111. hadNormals = true;
  112. geometry = geometry.clone();
  113. if ( tryKeepNormals === true && geometry.index !== null ) {
  114. oldNormals = geometry.attributes.normal.array;
  115. }
  116. geometry.deleteAttribute( 'normal' );
  117. }
  118. if ( geometry.index == null ) {
  119. geometry = BufferGeometryUtils.mergeVertices( geometry );
  120. }
  121. const indexes = geometry.index.array;
  122. const positions = geometry.getAttribute( 'position' ).array;
  123. let normals;
  124. let pointToIndexMap;
  125. computeNormals();
  126. mapPositionsToIndexes();
  127. const splitIndexes = [];
  128. for ( const vertexIndexes of pointToIndexMap ) {
  129. edgeSplit( vertexIndexes, Math.cos( cutOffAngle ) - 0.001 );
  130. }
  131. const newAttributes = {};
  132. for ( const name of Object.keys( geometry.attributes ) ) {
  133. const oldAttribute = geometry.attributes[ name ];
  134. const newArray = new oldAttribute.array.constructor( ( indexes.length + splitIndexes.length ) * oldAttribute.itemSize );
  135. newArray.set( oldAttribute.array );
  136. newAttributes[ name ] = new BufferAttribute( newArray, oldAttribute.itemSize, oldAttribute.normalized );
  137. }
  138. const newIndexes = new Uint32Array( indexes.length );
  139. newIndexes.set( indexes );
  140. for ( let i = 0; i < splitIndexes.length; i ++ ) {
  141. const split = splitIndexes[ i ];
  142. const index = indexes[ split.original ];
  143. for ( const attribute of Object.values( newAttributes ) ) {
  144. for ( let j = 0; j < attribute.itemSize; j ++ ) {
  145. attribute.array[ ( indexes.length + i ) * attribute.itemSize + j ] =
  146. attribute.array[ index * attribute.itemSize + j ];
  147. }
  148. }
  149. for ( const j of split.indexes ) {
  150. newIndexes[ j ] = indexes.length + i;
  151. }
  152. }
  153. geometry = new BufferGeometry();
  154. geometry.setIndex( new BufferAttribute( newIndexes, 1 ) );
  155. for ( const name of Object.keys( newAttributes ) ) {
  156. geometry.setAttribute( name, newAttributes[ name ] );
  157. }
  158. if ( hadNormals ) {
  159. geometry.computeVertexNormals();
  160. if ( oldNormals !== null ) {
  161. const changedNormals = new Array( oldNormals.length / 3 ).fill( false );
  162. for ( const splitData of splitIndexes )
  163. changedNormals[ splitData.original ] = true;
  164. for ( let i = 0; i < changedNormals.length; i ++ ) {
  165. if ( changedNormals[ i ] === false ) {
  166. for ( let j = 0; j < 3; j ++ )
  167. geometry.attributes.normal.array[ 3 * i + j ] = oldNormals[ 3 * i + j ];
  168. }
  169. }
  170. }
  171. }
  172. return geometry;
  173. }
  174. }
  175. export { EdgeSplitModifier };
粤ICP备19079148号