EdgeSplitModifier.js 6.0 KB

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