TessellateModifier.js 4.7 KB

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