TessellateModifier.js 6.5 KB

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