TessellateModifier.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. ( function () {
  2. /**
  3. * Break faces with edges longer than maxEdgeLength
  4. */
  5. class TessellateModifier {
  6. constructor( maxEdgeLength = 0.1, maxIterations = 6 ) {
  7. this.maxEdgeLength = maxEdgeLength;
  8. this.maxIterations = maxIterations;
  9. }
  10. modify( geometry ) {
  11. if ( geometry.index !== null ) {
  12. geometry = geometry.toNonIndexed();
  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 ua = new THREE.Vector2();
  32. const ub = new THREE.Vector2();
  33. const uc = new THREE.Vector2();
  34. const um = new THREE.Vector2();
  35. const us = [ ua, ub, uc, um ];
  36. const u2a = new THREE.Vector2();
  37. const u2b = new THREE.Vector2();
  38. const u2c = new THREE.Vector2();
  39. const u2m = new THREE.Vector2();
  40. const u2s = [ u2a, u2b, u2c, u2m ];
  41. const attributes = geometry.attributes;
  42. const hasNormals = attributes.normal !== undefined;
  43. const hasColors = attributes.color !== undefined;
  44. const hasUVs = attributes.uv !== undefined;
  45. const hasUV2s = attributes.uv2 !== undefined;
  46. let positions = attributes.position.array;
  47. let normals = hasNormals ? attributes.normal.array : null;
  48. let colors = hasColors ? attributes.color.array : null;
  49. let uvs = hasUVs ? attributes.uv.array : null;
  50. let uv2s = hasUV2s ? attributes.uv2.array : null;
  51. let positions2 = positions;
  52. let normals2 = normals;
  53. let colors2 = colors;
  54. let uvs2 = uvs;
  55. let uv2s2 = uv2s;
  56. let iteration = 0;
  57. let tessellating = true;
  58. function addTriangle( a, b, c ) {
  59. const v1 = vs[ a ];
  60. const v2 = vs[ b ];
  61. const v3 = vs[ c ];
  62. positions2.push( v1.x, v1.y, v1.z );
  63. positions2.push( v2.x, v2.y, v2.z );
  64. positions2.push( v3.x, v3.y, v3.z );
  65. if ( hasNormals ) {
  66. const n1 = ns[ a ];
  67. const n2 = ns[ b ];
  68. const n3 = ns[ c ];
  69. normals2.push( n1.x, n1.y, n1.z );
  70. normals2.push( n2.x, n2.y, n2.z );
  71. normals2.push( n3.x, n3.y, n3.z );
  72. }
  73. if ( hasColors ) {
  74. const c1 = cs[ a ];
  75. const c2 = cs[ b ];
  76. const c3 = cs[ c ];
  77. colors2.push( c1.x, c1.y, c1.z );
  78. colors2.push( c2.x, c2.y, c2.z );
  79. colors2.push( c3.x, c3.y, c3.z );
  80. }
  81. if ( hasUVs ) {
  82. const u1 = us[ a ];
  83. const u2 = us[ b ];
  84. const u3 = us[ c ];
  85. uvs2.push( u1.x, u1.y );
  86. uvs2.push( u2.x, u2.y );
  87. uvs2.push( u3.x, u3.y );
  88. }
  89. if ( hasUV2s ) {
  90. const u21 = u2s[ a ];
  91. const u22 = u2s[ b ];
  92. const u23 = u2s[ c ];
  93. uv2s2.push( u21.x, u21.y );
  94. uv2s2.push( u22.x, u22.y );
  95. uv2s2.push( u23.x, u23.y );
  96. }
  97. }
  98. while ( tessellating && iteration < maxIterations ) {
  99. iteration ++;
  100. tessellating = false;
  101. positions = positions2;
  102. positions2 = [];
  103. if ( hasNormals ) {
  104. normals = normals2;
  105. normals2 = [];
  106. }
  107. if ( hasColors ) {
  108. colors = colors2;
  109. colors2 = [];
  110. }
  111. if ( hasUVs ) {
  112. uvs = uvs2;
  113. uvs2 = [];
  114. }
  115. if ( hasUV2s ) {
  116. uv2s = uv2s2;
  117. uv2s2 = [];
  118. }
  119. for ( let i = 0, i2 = 0, il = positions.length; i < il; i += 9, i2 += 6 ) {
  120. va.fromArray( positions, i + 0 );
  121. vb.fromArray( positions, i + 3 );
  122. vc.fromArray( positions, i + 6 );
  123. if ( hasNormals ) {
  124. na.fromArray( normals, i + 0 );
  125. nb.fromArray( normals, i + 3 );
  126. nc.fromArray( normals, i + 6 );
  127. }
  128. if ( hasColors ) {
  129. ca.fromArray( colors, i + 0 );
  130. cb.fromArray( colors, i + 3 );
  131. cc.fromArray( colors, i + 6 );
  132. }
  133. if ( hasUVs ) {
  134. ua.fromArray( uvs, i2 + 0 );
  135. ub.fromArray( uvs, i2 + 2 );
  136. uc.fromArray( uvs, i2 + 4 );
  137. }
  138. if ( hasUV2s ) {
  139. u2a.fromArray( uv2s, i2 + 0 );
  140. u2b.fromArray( uv2s, i2 + 2 );
  141. u2c.fromArray( uv2s, i2 + 4 );
  142. }
  143. const dab = va.distanceToSquared( vb );
  144. const dbc = vb.distanceToSquared( vc );
  145. const dac = va.distanceToSquared( vc );
  146. if ( dab > maxEdgeLengthSquared || dbc > maxEdgeLengthSquared || dac > maxEdgeLengthSquared ) {
  147. tessellating = true;
  148. if ( dab >= dbc && dab >= dac ) {
  149. vm.lerpVectors( va, vb, 0.5 );
  150. if ( hasNormals ) nm.lerpVectors( na, nb, 0.5 );
  151. if ( hasColors ) cm.lerpColors( ca, cb, 0.5 );
  152. if ( hasUVs ) um.lerpVectors( ua, ub, 0.5 );
  153. if ( hasUV2s ) u2m.lerpVectors( u2a, u2b, 0.5 );
  154. addTriangle( 0, 3, 2 );
  155. addTriangle( 3, 1, 2 );
  156. } else if ( dbc >= dab && dbc >= dac ) {
  157. vm.lerpVectors( vb, vc, 0.5 );
  158. if ( hasNormals ) nm.lerpVectors( nb, nc, 0.5 );
  159. if ( hasColors ) cm.lerpColors( cb, cc, 0.5 );
  160. if ( hasUVs ) um.lerpVectors( ub, uc, 0.5 );
  161. if ( hasUV2s ) u2m.lerpVectors( u2b, u2c, 0.5 );
  162. addTriangle( 0, 1, 3 );
  163. addTriangle( 3, 2, 0 );
  164. } else {
  165. vm.lerpVectors( va, vc, 0.5 );
  166. if ( hasNormals ) nm.lerpVectors( na, nc, 0.5 );
  167. if ( hasColors ) cm.lerpColors( ca, cc, 0.5 );
  168. if ( hasUVs ) um.lerpVectors( ua, uc, 0.5 );
  169. if ( hasUV2s ) u2m.lerpVectors( u2a, u2c, 0.5 );
  170. addTriangle( 0, 1, 3 );
  171. addTriangle( 3, 1, 2 );
  172. }
  173. } else {
  174. addTriangle( 0, 1, 2 );
  175. }
  176. }
  177. }
  178. const geometry2 = new THREE.BufferGeometry();
  179. geometry2.setAttribute( 'position', new THREE.Float32BufferAttribute( positions2, 3 ) );
  180. if ( hasNormals ) {
  181. geometry2.setAttribute( 'normal', new THREE.Float32BufferAttribute( normals2, 3 ) );
  182. }
  183. if ( hasColors ) {
  184. geometry2.setAttribute( 'color', new THREE.Float32BufferAttribute( colors2, 3 ) );
  185. }
  186. if ( hasUVs ) {
  187. geometry2.setAttribute( 'uv', new THREE.Float32BufferAttribute( uvs2, 2 ) );
  188. }
  189. if ( hasUV2s ) {
  190. geometry2.setAttribute( 'uv2', new THREE.Float32BufferAttribute( uv2s2, 2 ) );
  191. }
  192. return geometry2;
  193. }
  194. }
  195. THREE.TessellateModifier = TessellateModifier;
  196. } )();