TessellateModifier.js 6.2 KB

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