TessellateModifier.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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.isBufferGeometry !== true ) {
  10. console.warn( 'TessellateModifier: geometry is not a BufferGeometry.', geometry );
  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. /*TOFIX?*/
  32. THREE.Color.prototype.lerpColors = function lerpColors( color1, color2, alpha ) {
  33. this.r += ( color1.r - color2.r ) * alpha;
  34. this.g += ( color1.g - color2.g ) * alpha;
  35. this.b += ( color1.b - color2.b ) * alpha;
  36. return this;
  37. };
  38. const attributes = geometry.attributes;
  39. let positions = attributes.position.array;
  40. const hasNormals = attributes.normal !== undefined;
  41. let normals = hasNormals ? attributes.normal.array : null;
  42. const hasColors = attributes.color !== undefined;
  43. let colors = hasColors ? attributes.color.array : null;
  44. let positions2 = positions;
  45. let normals2 = normals;
  46. let colors2 = colors;
  47. let iteration = 0;
  48. let tessellating = true;
  49. function addTriangle( a, b, c ) {
  50. const v1 = vs[ a ];
  51. const v2 = vs[ b ];
  52. const v3 = vs[ c ];
  53. positions2.push( v1.x, v1.y, v1.z );
  54. positions2.push( v2.x, v2.y, v2.z );
  55. positions2.push( v3.x, v3.y, v3.z );
  56. if ( hasNormals ) {
  57. const n1 = ns[ a ];
  58. const n2 = ns[ b ];
  59. const n3 = ns[ c ];
  60. normals2.push( n1.x, n1.y, n1.z );
  61. normals2.push( n2.x, n2.y, n2.z );
  62. normals2.push( n3.x, n3.y, n3.z );
  63. }
  64. if ( hasColors ) {
  65. const c1 = cs[ a ];
  66. const c2 = cs[ b ];
  67. const c3 = cs[ c ];
  68. colors2.push( c1.x, c1.y, c1.z );
  69. colors2.push( c2.x, c2.y, c2.z );
  70. colors2.push( c3.x, c3.y, c3.z );
  71. }
  72. }
  73. while ( tessellating && iteration < maxIterations ) {
  74. iteration ++;
  75. tessellating = false;
  76. positions = positions2;
  77. positions2 = [];
  78. if ( hasNormals ) {
  79. normals = normals2;
  80. normals2 = [];
  81. }
  82. if ( hasColors ) {
  83. colors = colors2;
  84. colors2 = [];
  85. }
  86. for ( var i = 0, il = positions.length; i < il; i += 9 ) {
  87. va.fromArray( positions, i + 0 );
  88. vb.fromArray( positions, i + 3 );
  89. vc.fromArray( positions, i + 6 );
  90. if ( hasNormals ) {
  91. na.fromArray( normals, i + 0 );
  92. nb.fromArray( normals, i + 3 );
  93. nc.fromArray( normals, i + 6 );
  94. }
  95. if ( hasColors ) {
  96. ca.fromArray( colors, i + 0 );
  97. cb.fromArray( colors, i + 3 );
  98. cc.fromArray( colors, i + 6 );
  99. }
  100. const dab = va.distanceToSquared( vb );
  101. const dbc = vb.distanceToSquared( vc );
  102. const dac = va.distanceToSquared( vc );
  103. if ( dab > maxEdgeLengthSquared || dbc > maxEdgeLengthSquared || dac > maxEdgeLengthSquared ) {
  104. tessellating = true;
  105. if ( dab >= dbc && dab >= dac ) {
  106. vm.lerpVectors( va, vb, 0.5 );
  107. if ( hasNormals ) nm.lerpVectors( na, nb, 0.5 );
  108. if ( hasColors ) cm.lerpColors( ca, cb, 0.5 );
  109. addTriangle( 0, 3, 2 );
  110. addTriangle( 3, 1, 2 );
  111. } else if ( dbc >= dab && dbc >= dac ) {
  112. vm.lerpVectors( vb, vc, 0.5 );
  113. if ( hasNormals ) nm.lerpVectors( nb, nc, 0.5 );
  114. if ( hasColors ) cm.lerpColors( cb, cc, 0.5 );
  115. addTriangle( 0, 1, 3 );
  116. addTriangle( 3, 2, 0 );
  117. } else {
  118. vm.lerpVectors( va, vc, 0.5 );
  119. if ( hasNormals ) nm.lerpVectors( na, nc, 0.5 );
  120. if ( hasColors ) cm.lerpColors( ca, cc, 0.5 );
  121. addTriangle( 0, 1, 3 );
  122. addTriangle( 3, 1, 2 );
  123. }
  124. } else {
  125. addTriangle( 0, 1, 2 );
  126. }
  127. }
  128. }
  129. const geometry2 = new THREE.BufferGeometry();
  130. geometry2.setAttribute( 'position', new THREE.Float32BufferAttribute( positions2, 3 ) );
  131. if ( hasNormals ) {
  132. geometry2.setAttribute( 'normal', new THREE.Float32BufferAttribute( normals2, 3 ) );
  133. }
  134. if ( hasColors ) {
  135. geometry2.setAttribute( 'color', new THREE.Float32BufferAttribute( colors2, 3 ) );
  136. }
  137. return geometry2;
  138. };
粤ICP备19079148号