TessellateModifier.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /**
  2. * Break faces with edges longer than maxEdgeLength
  3. * - not recursive
  4. *
  5. * @author alteredq / http://alteredqualia.com/
  6. */
  7. import {
  8. Face3
  9. } from "../../../build/three.module.js";
  10. var TessellateModifier = function ( maxEdgeLength ) {
  11. this.maxEdgeLength = maxEdgeLength;
  12. };
  13. TessellateModifier.prototype.modify = function ( geometry ) {
  14. var edge;
  15. var faces = [];
  16. var faceVertexUvs = [];
  17. var maxEdgeLengthSquared = this.maxEdgeLength * this.maxEdgeLength;
  18. for ( var i = 0, il = geometry.faceVertexUvs.length; i < il; i ++ ) {
  19. faceVertexUvs[ i ] = [];
  20. }
  21. for ( var i = 0, il = geometry.faces.length; i < il; i ++ ) {
  22. var face = geometry.faces[ i ];
  23. if ( face instanceof Face3 ) {
  24. var a = face.a;
  25. var b = face.b;
  26. var c = face.c;
  27. var va = geometry.vertices[ a ];
  28. var vb = geometry.vertices[ b ];
  29. var vc = geometry.vertices[ c ];
  30. var dab = va.distanceToSquared( vb );
  31. var dbc = vb.distanceToSquared( vc );
  32. var dac = va.distanceToSquared( vc );
  33. if ( dab > maxEdgeLengthSquared || dbc > maxEdgeLengthSquared || dac > maxEdgeLengthSquared ) {
  34. var m = geometry.vertices.length;
  35. var triA = face.clone();
  36. var triB = face.clone();
  37. if ( dab >= dbc && dab >= dac ) {
  38. var vm = va.clone();
  39. vm.lerp( vb, 0.5 );
  40. triA.a = a;
  41. triA.b = m;
  42. triA.c = c;
  43. triB.a = m;
  44. triB.b = b;
  45. triB.c = c;
  46. if ( face.vertexNormals.length === 3 ) {
  47. var vnm = face.vertexNormals[ 0 ].clone();
  48. vnm.lerp( face.vertexNormals[ 1 ], 0.5 );
  49. triA.vertexNormals[ 1 ].copy( vnm );
  50. triB.vertexNormals[ 0 ].copy( vnm );
  51. }
  52. if ( face.vertexColors.length === 3 ) {
  53. var vcm = face.vertexColors[ 0 ].clone();
  54. vcm.lerp( face.vertexColors[ 1 ], 0.5 );
  55. triA.vertexColors[ 1 ].copy( vcm );
  56. triB.vertexColors[ 0 ].copy( vcm );
  57. }
  58. edge = 0;
  59. } else if ( dbc >= dab && dbc >= dac ) {
  60. var vm = vb.clone();
  61. vm.lerp( vc, 0.5 );
  62. triA.a = a;
  63. triA.b = b;
  64. triA.c = m;
  65. triB.a = m;
  66. triB.b = c;
  67. triB.c = a;
  68. if ( face.vertexNormals.length === 3 ) {
  69. var vnm = face.vertexNormals[ 1 ].clone();
  70. vnm.lerp( face.vertexNormals[ 2 ], 0.5 );
  71. triA.vertexNormals[ 2 ].copy( vnm );
  72. triB.vertexNormals[ 0 ].copy( vnm );
  73. triB.vertexNormals[ 1 ].copy( face.vertexNormals[ 2 ] );
  74. triB.vertexNormals[ 2 ].copy( face.vertexNormals[ 0 ] );
  75. }
  76. if ( face.vertexColors.length === 3 ) {
  77. var vcm = face.vertexColors[ 1 ].clone();
  78. vcm.lerp( face.vertexColors[ 2 ], 0.5 );
  79. triA.vertexColors[ 2 ].copy( vcm );
  80. triB.vertexColors[ 0 ].copy( vcm );
  81. triB.vertexColors[ 1 ].copy( face.vertexColors[ 2 ] );
  82. triB.vertexColors[ 2 ].copy( face.vertexColors[ 0 ] );
  83. }
  84. edge = 1;
  85. } else {
  86. var vm = va.clone();
  87. vm.lerp( vc, 0.5 );
  88. triA.a = a;
  89. triA.b = b;
  90. triA.c = m;
  91. triB.a = m;
  92. triB.b = b;
  93. triB.c = c;
  94. if ( face.vertexNormals.length === 3 ) {
  95. var vnm = face.vertexNormals[ 0 ].clone();
  96. vnm.lerp( face.vertexNormals[ 2 ], 0.5 );
  97. triA.vertexNormals[ 2 ].copy( vnm );
  98. triB.vertexNormals[ 0 ].copy( vnm );
  99. }
  100. if ( face.vertexColors.length === 3 ) {
  101. var vcm = face.vertexColors[ 0 ].clone();
  102. vcm.lerp( face.vertexColors[ 2 ], 0.5 );
  103. triA.vertexColors[ 2 ].copy( vcm );
  104. triB.vertexColors[ 0 ].copy( vcm );
  105. }
  106. edge = 2;
  107. }
  108. faces.push( triA, triB );
  109. geometry.vertices.push( vm );
  110. for ( var j = 0, jl = geometry.faceVertexUvs.length; j < jl; j ++ ) {
  111. if ( geometry.faceVertexUvs[ j ].length ) {
  112. var uvs = geometry.faceVertexUvs[ j ][ i ];
  113. var uvA = uvs[ 0 ];
  114. var uvB = uvs[ 1 ];
  115. var uvC = uvs[ 2 ];
  116. // AB
  117. if ( edge === 0 ) {
  118. var uvM = uvA.clone();
  119. uvM.lerp( uvB, 0.5 );
  120. var uvsTriA = [ uvA.clone(), uvM.clone(), uvC.clone() ];
  121. var uvsTriB = [ uvM.clone(), uvB.clone(), uvC.clone() ];
  122. // BC
  123. } else if ( edge === 1 ) {
  124. var uvM = uvB.clone();
  125. uvM.lerp( uvC, 0.5 );
  126. var uvsTriA = [ uvA.clone(), uvB.clone(), uvM.clone() ];
  127. var uvsTriB = [ uvM.clone(), uvC.clone(), uvA.clone() ];
  128. // AC
  129. } else {
  130. var uvM = uvA.clone();
  131. uvM.lerp( uvC, 0.5 );
  132. var uvsTriA = [ uvA.clone(), uvB.clone(), uvM.clone() ];
  133. var uvsTriB = [ uvM.clone(), uvB.clone(), uvC.clone() ];
  134. }
  135. faceVertexUvs[ j ].push( uvsTriA, uvsTriB );
  136. }
  137. }
  138. } else {
  139. faces.push( face );
  140. for ( var j = 0, jl = geometry.faceVertexUvs.length; j < jl; j ++ ) {
  141. faceVertexUvs[ j ].push( geometry.faceVertexUvs[ j ][ i ] );
  142. }
  143. }
  144. }
  145. }
  146. geometry.faces = faces;
  147. geometry.faceVertexUvs = faceVertexUvs;
  148. };
  149. export { TessellateModifier };