TessellateModifier.js 4.2 KB

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