EdgeSplitModifier.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. console.warn( "THREE.EdgeSplitModifier: As part of the transition to ES6 Modules, the files in 'examples/js' were deprecated in May 2020 (r117) and will be deleted in December 2020 (r124). You can find more information about developing using ES6 Modules in https://threejs.org/docs/#manual/en/introduction/Installation." );
  2. THREE.EdgeSplitModifier = function () {
  3. const A = new THREE.Vector3();
  4. const B = new THREE.Vector3();
  5. const C = new THREE.Vector3();
  6. let positions, normals;
  7. let indexes;
  8. let pointToIndexMap, splitIndexes;
  9. function computeNormals() {
  10. normals = new Float32Array( indexes.length * 3 );
  11. for ( let i = 0; i < indexes.length; i += 3 ) {
  12. let index = indexes[ i ];
  13. A.set(
  14. positions[ 3 * index ],
  15. positions[ 3 * index + 1 ],
  16. positions[ 3 * index + 2 ] );
  17. index = indexes[ i + 1 ];
  18. B.set(
  19. positions[ 3 * index ],
  20. positions[ 3 * index + 1 ],
  21. positions[ 3 * index + 2 ] );
  22. index = indexes[ i + 2 ];
  23. C.set(
  24. positions[ 3 * index ],
  25. positions[ 3 * index + 1 ],
  26. positions[ 3 * index + 2 ] );
  27. C.sub( B );
  28. A.sub( B );
  29. const normal = C.cross( A ).normalize();
  30. for ( let j = 0; j < 3; j ++ ) {
  31. normals[ 3 * ( i + j ) ] = normal.x;
  32. normals[ 3 * ( i + j ) + 1 ] = normal.y;
  33. normals[ 3 * ( i + j ) + 2 ] = normal.z;
  34. }
  35. }
  36. }
  37. function mapPositionsToIndexes() {
  38. pointToIndexMap = Array( positions.length / 3 );
  39. for ( let i = 0; i < indexes.length; i ++ ) {
  40. const index = indexes[ i ];
  41. if ( pointToIndexMap[ index ] == null )
  42. pointToIndexMap[ index ] = [];
  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 ] )
  48. .normalize();
  49. const result = {
  50. splitGroup: [],
  51. currentGroup: [ firstIndex ]
  52. };
  53. for ( const j of indexes ) {
  54. if ( j !== firstIndex ) {
  55. B.set( normals[ 3 * j ], normals[ 3 * j + 1 ], normals[ 3 * j + 2 ] )
  56. .normalize();
  57. if ( B.dot( A ) < cutOff )
  58. result.splitGroup.push( j );
  59. else
  60. result.currentGroup.push( j );
  61. }
  62. }
  63. return result;
  64. }
  65. function edgeSplit( indexes, cutOff, original = null ) {
  66. if ( indexes.length === 0 )
  67. return;
  68. const groupResults = [];
  69. for ( const index of indexes )
  70. groupResults.push( edgeSplitToGroups( indexes, cutOff, index ) );
  71. let result = groupResults[ 0 ];
  72. for ( const groupResult of groupResults )
  73. if ( groupResult.currentGroup.length > result.currentGroup.length )
  74. result = groupResult;
  75. if ( original != null )
  76. splitIndexes.push( {
  77. original: original,
  78. indexes: result.currentGroup
  79. } );
  80. if ( result.splitGroup.length )
  81. edgeSplit( result.splitGroup, cutOff, original || result.currentGroup[ 0 ] );
  82. }
  83. this.modify = function ( geometry, cutOffAngle ) {
  84. if ( ! geometry.isBufferGeometry ) {
  85. geometry = new THREE.BufferGeometry().fromGeometry( geometry );
  86. }
  87. if ( geometry.index == null )
  88. geometry = THREE.BufferGeometryUtils.mergeVertices( geometry );
  89. indexes = geometry.index.array;
  90. positions = geometry.getAttribute( "position" ).array;
  91. computeNormals();
  92. mapPositionsToIndexes();
  93. splitIndexes = [];
  94. for ( const vertexIndexes of pointToIndexMap )
  95. edgeSplit( vertexIndexes, Math.cos( cutOffAngle ) - 0.001 );
  96. const newPositions = new Float32Array( positions.length + 3 * splitIndexes.length );
  97. newPositions.set( positions );
  98. const offset = positions.length;
  99. const newIndexes = new Uint32Array( indexes.length );
  100. newIndexes.set( indexes );
  101. for ( let i = 0; i < splitIndexes.length; i ++ ) {
  102. const split = splitIndexes[ i ];
  103. const index = indexes[ split.original ];
  104. newPositions[ offset + 3 * i ] = positions[ 3 * index ];
  105. newPositions[ offset + 3 * i + 1 ] = positions[ 3 * index + 1 ];
  106. newPositions[ offset + 3 * i + 2 ] = positions[ 3 * index + 2 ];
  107. for ( const j of split.indexes )
  108. newIndexes[ j ] = offset / 3 + i;
  109. }
  110. geometry = new THREE.BufferGeometry();
  111. geometry.setAttribute( 'position', new THREE.BufferAttribute( newPositions, 3, true ) );
  112. geometry.setIndex( new THREE.BufferAttribute( newIndexes, 1 ) );
  113. return geometry;
  114. };
  115. };