TessellateModifier.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /**
  2. * Break faces with edges longer than maxEdgeLength
  3. */
  4. THREE.TessellateModifier = function ( maxEdgeLength = 0.1, maxIterations = 6 ) {
  5. this.maxEdgeLength = maxEdgeLength;
  6. this.maxIterations = maxIterations;
  7. };
  8. THREE.TessellateModifier.prototype.modify = function ( geometry ) {
  9. if ( geometry.isGeometry === true ) {
  10. console.error( 'THREE.TessellateModifier no longer supports THREE.Geometry. Use THREE.BufferGeometry instead.' );
  11. return geometry;
  12. }
  13. //
  14. const maxIterations = this.maxIterations;
  15. const maxEdgeLengthSquared = this.maxEdgeLength * this.maxEdgeLength;
  16. const va = new THREE.Vector3();
  17. const vb = new THREE.Vector3();
  18. const vc = new THREE.Vector3();
  19. const vm = new THREE.Vector3();
  20. const vs = [ va, vb, vc, vm ];
  21. const na = new THREE.Vector3();
  22. const nb = new THREE.Vector3();
  23. const nc = new THREE.Vector3();
  24. const nm = new THREE.Vector3();
  25. const ns = [ na, nb, nc, nm ];
  26. const ca = new THREE.Color();
  27. const cb = new THREE.Color();
  28. const cc = new THREE.Color();
  29. const cm = new THREE.Color();
  30. const cs = [ ca, cb, cc, cm ];
  31. const attributes = geometry.attributes;
  32. const hasNormals = attributes.normal !== undefined;
  33. const hasColors = attributes.color !== undefined;
  34. let positions = attributes.position.array;
  35. let normals = hasNormals ? attributes.normal.array : null;
  36. let colors = hasColors ? attributes.color.array : null;
  37. let positions2 = positions;
  38. let normals2 = normals;
  39. let colors2 = colors;
  40. let iteration = 0;
  41. let tessellating = true;
  42. function addTriangle( a, b, c ) {
  43. const v1 = vs[ a ];
  44. const v2 = vs[ b ];
  45. const v3 = vs[ c ];
  46. positions2.push( v1.x, v1.y, v1.z );
  47. positions2.push( v2.x, v2.y, v2.z );
  48. positions2.push( v3.x, v3.y, v3.z );
  49. if ( hasNormals ) {
  50. const n1 = ns[ a ];
  51. const n2 = ns[ b ];
  52. const n3 = ns[ c ];
  53. normals2.push( n1.x, n1.y, n1.z );
  54. normals2.push( n2.x, n2.y, n2.z );
  55. normals2.push( n3.x, n3.y, n3.z );
  56. }
  57. if ( hasColors ) {
  58. const c1 = cs[ a ];
  59. const c2 = cs[ b ];
  60. const c3 = cs[ c ];
  61. colors2.push( c1.x, c1.y, c1.z );
  62. colors2.push( c2.x, c2.y, c2.z );
  63. colors2.push( c3.x, c3.y, c3.z );
  64. }
  65. }
  66. while ( tessellating && iteration < maxIterations ) {
  67. iteration ++;
  68. tessellating = false;
  69. positions = positions2;
  70. positions2 = [];
  71. if ( hasNormals ) {
  72. normals = normals2;
  73. normals2 = [];
  74. }
  75. if ( hasColors ) {
  76. colors = colors2;
  77. colors2 = [];
  78. }
  79. for ( var i = 0, il = positions.length; i < il; i += 9 ) {
  80. va.fromArray( positions, i + 0 );
  81. vb.fromArray( positions, i + 3 );
  82. vc.fromArray( positions, i + 6 );
  83. if ( hasNormals ) {
  84. na.fromArray( normals, i + 0 );
  85. nb.fromArray( normals, i + 3 );
  86. nc.fromArray( normals, i + 6 );
  87. }
  88. if ( hasColors ) {
  89. ca.fromArray( colors, i + 0 );
  90. cb.fromArray( colors, i + 3 );
  91. cc.fromArray( colors, i + 6 );
  92. }
  93. const dab = va.distanceToSquared( vb );
  94. const dbc = vb.distanceToSquared( vc );
  95. const dac = va.distanceToSquared( vc );
  96. if ( dab > maxEdgeLengthSquared || dbc > maxEdgeLengthSquared || dac > maxEdgeLengthSquared ) {
  97. tessellating = true;
  98. if ( dab >= dbc && dab >= dac ) {
  99. vm.lerpVectors( va, vb, 0.5 );
  100. if ( hasNormals ) nm.lerpVectors( na, nb, 0.5 );
  101. if ( hasColors ) cm.lerpColors( ca, cb, 0.5 );
  102. addTriangle( 0, 3, 2 );
  103. addTriangle( 3, 1, 2 );
  104. } else if ( dbc >= dab && dbc >= dac ) {
  105. vm.lerpVectors( vb, vc, 0.5 );
  106. if ( hasNormals ) nm.lerpVectors( nb, nc, 0.5 );
  107. if ( hasColors ) cm.lerpColors( cb, cc, 0.5 );
  108. addTriangle( 0, 1, 3 );
  109. addTriangle( 3, 2, 0 );
  110. } else {
  111. vm.lerpVectors( va, vc, 0.5 );
  112. if ( hasNormals ) nm.lerpVectors( na, nc, 0.5 );
  113. if ( hasColors ) cm.lerpColors( ca, cc, 0.5 );
  114. addTriangle( 0, 1, 3 );
  115. addTriangle( 3, 1, 2 );
  116. }
  117. } else {
  118. addTriangle( 0, 1, 2 );
  119. }
  120. }
  121. }
  122. const geometry2 = new THREE.BufferGeometry();
  123. geometry2.setAttribute( 'position', new THREE.Float32BufferAttribute( positions2, 3 ) );
  124. if ( hasNormals ) {
  125. geometry2.setAttribute( 'normal', new THREE.Float32BufferAttribute( normals2, 3 ) );
  126. }
  127. if ( hasColors ) {
  128. geometry2.setAttribute( 'color', new THREE.Float32BufferAttribute( colors2, 3 ) );
  129. }
  130. return geometry2;
  131. };