TessellateModifier.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. import {
  2. BufferGeometry,
  3. Color,
  4. Float32BufferAttribute,
  5. Vector2,
  6. Vector3
  7. } from 'three';
  8. /**
  9. * This class can be used to modify a geometry by breaking its edges if they
  10. * are longer than maximum length.
  11. *
  12. * ```js
  13. * const modifier = new TessellateModifier( 8, 6 );
  14. * geometry = modifier.modify( geometry );
  15. * ```
  16. *
  17. * @three_import import { TessellateModifier } from 'three/addons/modifiers/TessellateModifier.js';
  18. */
  19. class TessellateModifier {
  20. /**
  21. * Constructs a new Tessellate modifier.
  22. *
  23. * @param {number} [maxEdgeLength=0.1] - The maximum edge length.
  24. * @param {number} [maxIterations=6] - The number of iterations.
  25. */
  26. constructor( maxEdgeLength = 0.1, maxIterations = 6 ) {
  27. /**
  28. * The maximum edge length.
  29. *
  30. * @type {number}
  31. * @default 0.1
  32. */
  33. this.maxEdgeLength = maxEdgeLength;
  34. /**
  35. * The maximum edge length.
  36. *
  37. * @type {number}
  38. * @default 0.1
  39. */
  40. this.maxIterations = maxIterations;
  41. }
  42. /**
  43. * Returns a new, modified version of the given geometry by applying a tesselation.
  44. * Please note that the resulting geometry is always non-indexed.
  45. *
  46. * @param {BufferGeometry} geometry - The geometry to modify.
  47. * @return {BufferGeometry} A new, modified geometry.
  48. */
  49. modify( geometry ) {
  50. if ( geometry.index !== null ) {
  51. geometry = geometry.toNonIndexed();
  52. }
  53. //
  54. const maxIterations = this.maxIterations;
  55. const maxEdgeLengthSquared = this.maxEdgeLength * this.maxEdgeLength;
  56. const va = new Vector3();
  57. const vb = new Vector3();
  58. const vc = new Vector3();
  59. const vm = new Vector3();
  60. const vs = [ va, vb, vc, vm ];
  61. const na = new Vector3();
  62. const nb = new Vector3();
  63. const nc = new Vector3();
  64. const nm = new Vector3();
  65. const ns = [ na, nb, nc, nm ];
  66. const ca = new Color();
  67. const cb = new Color();
  68. const cc = new Color();
  69. const cm = new Color();
  70. const cs = [ ca, cb, cc, cm ];
  71. const ua = new Vector2();
  72. const ub = new Vector2();
  73. const uc = new Vector2();
  74. const um = new Vector2();
  75. const us = [ ua, ub, uc, um ];
  76. const u2a = new Vector2();
  77. const u2b = new Vector2();
  78. const u2c = new Vector2();
  79. const u2m = new Vector2();
  80. const u2s = [ u2a, u2b, u2c, u2m ];
  81. const attributes = geometry.attributes;
  82. const hasNormals = attributes.normal !== undefined;
  83. const hasColors = attributes.color !== undefined;
  84. const hasUVs = attributes.uv !== undefined;
  85. const hasUV1s = attributes.uv1 !== undefined;
  86. let positions = attributes.position.array;
  87. let normals = hasNormals ? attributes.normal.array : null;
  88. let colors = hasColors ? attributes.color.array : null;
  89. let uvs = hasUVs ? attributes.uv.array : null;
  90. let uv1s = hasUV1s ? attributes.uv1.array : null;
  91. let positions2 = positions;
  92. let normals2 = normals;
  93. let colors2 = colors;
  94. let uvs2 = uvs;
  95. let uv1s2 = uv1s;
  96. let iteration = 0;
  97. let tessellating = true;
  98. function addTriangle( a, b, c ) {
  99. const v1 = vs[ a ];
  100. const v2 = vs[ b ];
  101. const v3 = vs[ c ];
  102. positions2.push( v1.x, v1.y, v1.z );
  103. positions2.push( v2.x, v2.y, v2.z );
  104. positions2.push( v3.x, v3.y, v3.z );
  105. if ( hasNormals ) {
  106. const n1 = ns[ a ];
  107. const n2 = ns[ b ];
  108. const n3 = ns[ c ];
  109. normals2.push( n1.x, n1.y, n1.z );
  110. normals2.push( n2.x, n2.y, n2.z );
  111. normals2.push( n3.x, n3.y, n3.z );
  112. }
  113. if ( hasColors ) {
  114. const c1 = cs[ a ];
  115. const c2 = cs[ b ];
  116. const c3 = cs[ c ];
  117. colors2.push( c1.r, c1.g, c1.b );
  118. colors2.push( c2.r, c2.g, c2.b );
  119. colors2.push( c3.r, c3.g, c3.b );
  120. }
  121. if ( hasUVs ) {
  122. const u1 = us[ a ];
  123. const u2 = us[ b ];
  124. const u3 = us[ c ];
  125. uvs2.push( u1.x, u1.y );
  126. uvs2.push( u2.x, u2.y );
  127. uvs2.push( u3.x, u3.y );
  128. }
  129. if ( hasUV1s ) {
  130. const u21 = u2s[ a ];
  131. const u22 = u2s[ b ];
  132. const u23 = u2s[ c ];
  133. uv1s2.push( u21.x, u21.y );
  134. uv1s2.push( u22.x, u22.y );
  135. uv1s2.push( u23.x, u23.y );
  136. }
  137. }
  138. while ( tessellating && iteration < maxIterations ) {
  139. iteration ++;
  140. tessellating = false;
  141. positions = positions2;
  142. positions2 = [];
  143. if ( hasNormals ) {
  144. normals = normals2;
  145. normals2 = [];
  146. }
  147. if ( hasColors ) {
  148. colors = colors2;
  149. colors2 = [];
  150. }
  151. if ( hasUVs ) {
  152. uvs = uvs2;
  153. uvs2 = [];
  154. }
  155. if ( hasUV1s ) {
  156. uv1s = uv1s2;
  157. uv1s2 = [];
  158. }
  159. for ( let i = 0, i2 = 0, il = positions.length; i < il; i += 9, i2 += 6 ) {
  160. va.fromArray( positions, i + 0 );
  161. vb.fromArray( positions, i + 3 );
  162. vc.fromArray( positions, i + 6 );
  163. if ( hasNormals ) {
  164. na.fromArray( normals, i + 0 );
  165. nb.fromArray( normals, i + 3 );
  166. nc.fromArray( normals, i + 6 );
  167. }
  168. if ( hasColors ) {
  169. ca.fromArray( colors, i + 0 );
  170. cb.fromArray( colors, i + 3 );
  171. cc.fromArray( colors, i + 6 );
  172. }
  173. if ( hasUVs ) {
  174. ua.fromArray( uvs, i2 + 0 );
  175. ub.fromArray( uvs, i2 + 2 );
  176. uc.fromArray( uvs, i2 + 4 );
  177. }
  178. if ( hasUV1s ) {
  179. u2a.fromArray( uv1s, i2 + 0 );
  180. u2b.fromArray( uv1s, i2 + 2 );
  181. u2c.fromArray( uv1s, i2 + 4 );
  182. }
  183. const dab = va.distanceToSquared( vb );
  184. const dbc = vb.distanceToSquared( vc );
  185. const dac = va.distanceToSquared( vc );
  186. if ( dab > maxEdgeLengthSquared || dbc > maxEdgeLengthSquared || dac > maxEdgeLengthSquared ) {
  187. tessellating = true;
  188. if ( dab >= dbc && dab >= dac ) {
  189. vm.lerpVectors( va, vb, 0.5 );
  190. if ( hasNormals ) nm.lerpVectors( na, nb, 0.5 );
  191. if ( hasColors ) cm.lerpColors( ca, cb, 0.5 );
  192. if ( hasUVs ) um.lerpVectors( ua, ub, 0.5 );
  193. if ( hasUV1s ) u2m.lerpVectors( u2a, u2b, 0.5 );
  194. addTriangle( 0, 3, 2 );
  195. addTriangle( 3, 1, 2 );
  196. } else if ( dbc >= dab && dbc >= dac ) {
  197. vm.lerpVectors( vb, vc, 0.5 );
  198. if ( hasNormals ) nm.lerpVectors( nb, nc, 0.5 );
  199. if ( hasColors ) cm.lerpColors( cb, cc, 0.5 );
  200. if ( hasUVs ) um.lerpVectors( ub, uc, 0.5 );
  201. if ( hasUV1s ) u2m.lerpVectors( u2b, u2c, 0.5 );
  202. addTriangle( 0, 1, 3 );
  203. addTriangle( 3, 2, 0 );
  204. } else {
  205. vm.lerpVectors( va, vc, 0.5 );
  206. if ( hasNormals ) nm.lerpVectors( na, nc, 0.5 );
  207. if ( hasColors ) cm.lerpColors( ca, cc, 0.5 );
  208. if ( hasUVs ) um.lerpVectors( ua, uc, 0.5 );
  209. if ( hasUV1s ) u2m.lerpVectors( u2a, u2c, 0.5 );
  210. addTriangle( 0, 1, 3 );
  211. addTriangle( 3, 1, 2 );
  212. }
  213. } else {
  214. addTriangle( 0, 1, 2 );
  215. }
  216. }
  217. }
  218. const geometry2 = new BufferGeometry();
  219. geometry2.setAttribute( 'position', new Float32BufferAttribute( positions2, 3 ) );
  220. if ( hasNormals ) {
  221. geometry2.setAttribute( 'normal', new Float32BufferAttribute( normals2, 3 ) );
  222. }
  223. if ( hasColors ) {
  224. geometry2.setAttribute( 'color', new Float32BufferAttribute( colors2, 3 ) );
  225. }
  226. if ( hasUVs ) {
  227. geometry2.setAttribute( 'uv', new Float32BufferAttribute( uvs2, 2 ) );
  228. }
  229. if ( hasUV1s ) {
  230. geometry2.setAttribute( 'uv1', new Float32BufferAttribute( uv1s2, 2 ) );
  231. }
  232. return geometry2;
  233. }
  234. }
  235. export { TessellateModifier };