EdgeSplitModifier.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. import { BufferAttribute, BufferGeometry, Vector3 } from "../../../build/three.module.js";
  2. import { BufferGeometryUtils } from "../utils/BufferGeometryUtils.js";
  3. export function EdgeSplitModifier() {
  4. const A = new Vector3();
  5. const B = new Vector3();
  6. const C = new Vector3();
  7. let positions, normals;
  8. let indexes;
  9. let pointToIndexMap, splitIndexes;
  10. function computeNormals() {
  11. normals = new Float32Array( indexes.length * 3 );
  12. for ( let i = 0; i < indexes.length; i += 3 ) {
  13. let index = indexes[ i ];
  14. A.set(
  15. positions[ 3 * index ],
  16. positions[ 3 * index + 1 ],
  17. positions[ 3 * index + 2 ] );
  18. index = indexes[ i + 1 ];
  19. B.set(
  20. positions[ 3 * index ],
  21. positions[ 3 * index + 1 ],
  22. positions[ 3 * index + 2 ] );
  23. index = indexes[ i + 2 ];
  24. C.set(
  25. positions[ 3 * index ],
  26. positions[ 3 * index + 1 ],
  27. positions[ 3 * index + 2 ] );
  28. C.sub( B );
  29. A.sub( B );
  30. const normal = C.cross( A ).normalize();
  31. for ( let j = 0; j < 3; j ++ ) {
  32. normals[ 3 * ( i + j ) ] = normal.x;
  33. normals[ 3 * ( i + j ) + 1 ] = normal.y;
  34. normals[ 3 * ( i + j ) + 2 ] = normal.z;
  35. }
  36. }
  37. }
  38. function mapPositionsToIndexes() {
  39. pointToIndexMap = Array( positions.length / 3 );
  40. for ( let i = 0; i < indexes.length; i ++ ) {
  41. const index = indexes[ i ];
  42. if ( pointToIndexMap[ index ] == null )
  43. pointToIndexMap[ index ] = [];
  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 ] )
  49. .normalize();
  50. const result = {
  51. splitGroup: [],
  52. currentGroup: [ firstIndex ]
  53. };
  54. for ( const j of indexes ) {
  55. if ( j !== firstIndex ) {
  56. B.set( normals[ 3 * j ], normals[ 3 * j + 1 ], normals[ 3 * j + 2 ] )
  57. .normalize();
  58. if ( B.dot( A ) < cutOff )
  59. result.splitGroup.push( j );
  60. else
  61. result.currentGroup.push( j );
  62. }
  63. }
  64. return result;
  65. }
  66. function edgeSplit( indexes, cutOff, original = null ) {
  67. if ( indexes.length === 0 )
  68. return;
  69. const groupResults = [];
  70. for ( const index of indexes )
  71. groupResults.push( edgeSplitToGroups( indexes, cutOff, index ) );
  72. let result = groupResults[ 0 ];
  73. for ( const groupResult of groupResults )
  74. if ( groupResult.currentGroup.length > result.currentGroup.length )
  75. result = groupResult;
  76. if ( original != null )
  77. splitIndexes.push( {
  78. original: original,
  79. indexes: result.currentGroup
  80. } );
  81. if ( result.splitGroup.length )
  82. edgeSplit( result.splitGroup, cutOff, original || result.currentGroup[ 0 ] );
  83. }
  84. this.modify = function ( geometry, cutOffAngle ) {
  85. if ( ! geometry.isBufferGeometry ) {
  86. geometry = new BufferGeometry().fromGeometry( geometry );
  87. }
  88. if ( geometry.index == null )
  89. geometry = BufferGeometryUtils.mergeVertices( geometry );
  90. indexes = geometry.index.array;
  91. positions = geometry.getAttribute( "position" ).array;
  92. computeNormals();
  93. mapPositionsToIndexes();
  94. splitIndexes = [];
  95. for ( const vertexIndexes of pointToIndexMap )
  96. edgeSplit( vertexIndexes, Math.cos( cutOffAngle ) - 0.001 );
  97. const newPositions = new Float32Array( positions.length + 3 * splitIndexes.length );
  98. newPositions.set( positions );
  99. const offset = positions.length;
  100. const newIndexes = new Uint32Array( indexes.length );
  101. newIndexes.set( indexes );
  102. for ( let i = 0; i < splitIndexes.length; i ++ ) {
  103. const split = splitIndexes[ i ];
  104. const index = indexes[ split.original ];
  105. newPositions[ offset + 3 * i ] = positions[ 3 * index ];
  106. newPositions[ offset + 3 * i + 1 ] = positions[ 3 * index + 1 ];
  107. newPositions[ offset + 3 * i + 2 ] = positions[ 3 * index + 2 ];
  108. for ( const j of split.indexes )
  109. newIndexes[ j ] = offset / 3 + i;
  110. }
  111. geometry = new BufferGeometry();
  112. geometry.setAttribute( 'position', new BufferAttribute( newPositions, 3, true ) );
  113. geometry.setIndex( new BufferAttribute( newIndexes, 1 ) );
  114. return geometry;
  115. };
  116. }