EdgeSplitModifier.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. geometry = THREE.BufferGeometryUtils.mergeVertices( geometry );
  92. }
  93. indexes = geometry.index.array;
  94. positions = geometry.getAttribute( "position" ).array;
  95. computeNormals();
  96. mapPositionsToIndexes();
  97. splitIndexes = [];
  98. for ( var vertexIndexes of pointToIndexMap ) {
  99. edgeSplit( vertexIndexes, Math.cos( cutOffAngle ) - 0.001 );
  100. }
  101. var newPositions = new Float32Array( positions.length + 3 * splitIndexes.length );
  102. newPositions.set( positions );
  103. var offset = positions.length;
  104. var newIndexes = new Uint32Array( indexes.length );
  105. newIndexes.set( indexes );
  106. for ( var i = 0; i < splitIndexes.length; i ++ ) {
  107. var split = splitIndexes[ i ];
  108. var index = indexes[ split.original ];
  109. newPositions[ offset + 3 * i ] = positions[ 3 * index ];
  110. newPositions[ offset + 3 * i + 1 ] = positions[ 3 * index + 1 ];
  111. newPositions[ offset + 3 * i + 2 ] = positions[ 3 * index + 2 ];
  112. for ( var j of split.indexes ) {
  113. newIndexes[ j ] = offset / 3 + i;
  114. }
  115. }
  116. geometry = new THREE.BufferGeometry();
  117. geometry.setAttribute( 'position', new THREE.BufferAttribute( newPositions, 3, true ) );
  118. geometry.setIndex( new THREE.BufferAttribute( newIndexes, 1 ) );
  119. return geometry;
  120. };
  121. };