EdgeSplitModifier.js 4.4 KB

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