EdgeSplitModifier.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. const wasNotBufferGeometry = geometry.isBufferGeometry === undefined;
  88. if ( ! geometry.isBufferGeometry ) {
  89. geometry = new THREE.BufferGeometry().fromGeometry( geometry );
  90. }
  91. let hadNormals = false;
  92. if ( geometry.attributes.normal ) {
  93. hadNormals = true;
  94. if ( wasNotBufferGeometry === false )
  95. geometry = geometry.clone();
  96. geometry.deleteAttribute( 'normal' );
  97. }
  98. if ( geometry.index == null ) {
  99. if ( THREE.BufferGeometryUtils === undefined ) {
  100. throw 'THREE.EdgeSplitModifier relies on THREE.BufferGeometryUtils';
  101. }
  102. geometry = THREE.BufferGeometryUtils.mergeVertices( geometry );
  103. }
  104. indexes = geometry.index.array;
  105. positions = geometry.getAttribute( 'position' ).array;
  106. computeNormals();
  107. mapPositionsToIndexes();
  108. splitIndexes = [];
  109. for ( var vertexIndexes of pointToIndexMap ) {
  110. edgeSplit( vertexIndexes, Math.cos( cutOffAngle ) - 0.001 );
  111. }
  112. const newAttributes = {};
  113. for ( const name of Object.keys( geometry.attributes ) ) {
  114. const oldAttribute = geometry.attributes[ name ];
  115. const newArray = new oldAttribute.array.constructor( ( indexes.length + splitIndexes.length ) * oldAttribute.itemSize );
  116. newArray.set( oldAttribute.array );
  117. newAttributes[ name ] = new THREE.BufferAttribute( newArray, oldAttribute.itemSize, oldAttribute.normalized );
  118. }
  119. var newIndexes = new Uint32Array( indexes.length );
  120. newIndexes.set( indexes );
  121. for ( var i = 0; i < splitIndexes.length; i ++ ) {
  122. var split = splitIndexes[ i ];
  123. var index = indexes[ split.original ];
  124. for ( const attribute of Object.values( newAttributes ) ) {
  125. for ( let j = 0; j < attribute.itemSize; j ++ ) {
  126. attribute.array[ ( indexes.length + i ) * attribute.itemSize + j ] =
  127. attribute.array[ index * attribute.itemSize + j ];
  128. }
  129. }
  130. for ( var j of split.indexes ) {
  131. newIndexes[ j ] = indexes.length + i;
  132. }
  133. }
  134. geometry = new THREE.BufferGeometry();
  135. geometry.setIndex( new THREE.BufferAttribute( newIndexes, 1 ) );
  136. for ( const name of Object.keys( newAttributes ) ) {
  137. geometry.setAttribute( name, newAttributes[ name ] );
  138. }
  139. if ( hadNormals ) {
  140. geometry.computeVertexNormals();
  141. }
  142. return geometry;
  143. };
  144. };