EdgeSplitModifier.js 4.2 KB

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