tiledPlaneBuilder.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. import { Mesh } from "../mesh.js";
  2. import { VertexData } from "../mesh.vertexData.js";
  3. /**
  4. * Creates the VertexData for a tiled plane
  5. * @see https://doc.babylonjs.com/features/featuresDeepDive/mesh/creation/set/tiled_plane
  6. * @param options an object used to set the following optional parameters for the tiled plane, required but can be empty
  7. * * pattern a limited pattern arrangement depending on the number
  8. * * size of the box
  9. * * width of the box, overwrites size
  10. * * height of the box, overwrites size
  11. * * tileSize sets the width, height and depth of the tile to the value of size, optional default 1
  12. * * tileWidth sets the width (x direction) of the tile, overwrites the width set by size, optional, default size
  13. * * tileHeight sets the height (y direction) of the tile, overwrites the height set by size, optional, default size
  14. * * sideOrientation optional and takes the values : Mesh.FRONTSIDE (default), Mesh.BACKSIDE or Mesh.DOUBLESIDE
  15. * * alignHorizontal places whole tiles aligned to the center, left or right of a row
  16. * * alignVertical places whole tiles aligned to the center, left or right of a column
  17. * * frontUvs only usable when you create a double-sided mesh, used to choose what parts of the texture image to crop and apply on the front side, optional, default vector4 (0, 0, 1, 1)
  18. * @param options.pattern
  19. * @param options.tileSize
  20. * @param options.tileWidth
  21. * @param options.tileHeight
  22. * @param options.size
  23. * @param options.width
  24. * @param options.height
  25. * @param options.alignHorizontal
  26. * @param options.alignVertical
  27. * @param options.sideOrientation
  28. * @param options.frontUVs
  29. * @param options.backUVs
  30. * * backUVs only usable when you create a double-sided mesh, used to choose what parts of the texture image to crop and apply on the back side, optional, default vector4 (0, 0, 1, 1)
  31. * @returns the VertexData of the tiled plane
  32. */
  33. export function CreateTiledPlaneVertexData(options) {
  34. const flipTile = options.pattern || Mesh.NO_FLIP;
  35. const tileWidth = options.tileWidth || options.tileSize || 1;
  36. const tileHeight = options.tileHeight || options.tileSize || 1;
  37. const alignH = options.alignHorizontal || 0;
  38. const alignV = options.alignVertical || 0;
  39. const width = options.width || options.size || 1;
  40. const tilesX = Math.floor(width / tileWidth);
  41. let offsetX = width - tilesX * tileWidth;
  42. const height = options.height || options.size || 1;
  43. const tilesY = Math.floor(height / tileHeight);
  44. let offsetY = height - tilesY * tileHeight;
  45. const halfWidth = (tileWidth * tilesX) / 2;
  46. const halfHeight = (tileHeight * tilesY) / 2;
  47. let adjustX = 0;
  48. let adjustY = 0;
  49. let startX = 0;
  50. let startY = 0;
  51. let endX = 0;
  52. let endY = 0;
  53. //Part Tiles
  54. if (offsetX > 0 || offsetY > 0) {
  55. startX = -halfWidth;
  56. startY = -halfHeight;
  57. endX = halfWidth;
  58. endY = halfHeight;
  59. switch (alignH) {
  60. case Mesh.CENTER:
  61. offsetX /= 2;
  62. startX -= offsetX;
  63. endX += offsetX;
  64. break;
  65. case Mesh.LEFT:
  66. endX += offsetX;
  67. adjustX = -offsetX / 2;
  68. break;
  69. case Mesh.RIGHT:
  70. startX -= offsetX;
  71. adjustX = offsetX / 2;
  72. break;
  73. }
  74. switch (alignV) {
  75. case Mesh.CENTER:
  76. offsetY /= 2;
  77. startY -= offsetY;
  78. endY += offsetY;
  79. break;
  80. case Mesh.BOTTOM:
  81. endY += offsetY;
  82. adjustY = -offsetY / 2;
  83. break;
  84. case Mesh.TOP:
  85. startY -= offsetY;
  86. adjustY = offsetY / 2;
  87. break;
  88. }
  89. }
  90. const positions = [];
  91. const normals = [];
  92. const uvBase = [];
  93. uvBase[0] = [0, 0, 1, 0, 1, 1, 0, 1];
  94. uvBase[1] = [0, 0, 1, 0, 1, 1, 0, 1];
  95. if (flipTile === Mesh.ROTATE_TILE || flipTile === Mesh.ROTATE_ROW) {
  96. uvBase[1] = [1, 1, 0, 1, 0, 0, 1, 0];
  97. }
  98. if (flipTile === Mesh.FLIP_TILE || flipTile === Mesh.FLIP_ROW) {
  99. uvBase[1] = [1, 0, 0, 0, 0, 1, 1, 1];
  100. }
  101. if (flipTile === Mesh.FLIP_N_ROTATE_TILE || flipTile === Mesh.FLIP_N_ROTATE_ROW) {
  102. uvBase[1] = [0, 1, 1, 1, 1, 0, 0, 0];
  103. }
  104. let uvs = [];
  105. const colors = [];
  106. const indices = [];
  107. let index = 0;
  108. for (let y = 0; y < tilesY; y++) {
  109. for (let x = 0; x < tilesX; x++) {
  110. positions.push(-halfWidth + x * tileWidth + adjustX, -halfHeight + y * tileHeight + adjustY, 0);
  111. positions.push(-halfWidth + (x + 1) * tileWidth + adjustX, -halfHeight + y * tileHeight + adjustY, 0);
  112. positions.push(-halfWidth + (x + 1) * tileWidth + adjustX, -halfHeight + (y + 1) * tileHeight + adjustY, 0);
  113. positions.push(-halfWidth + x * tileWidth + adjustX, -halfHeight + (y + 1) * tileHeight + adjustY, 0);
  114. indices.push(index, index + 1, index + 3, index + 1, index + 2, index + 3);
  115. if (flipTile === Mesh.FLIP_TILE || flipTile === Mesh.ROTATE_TILE || flipTile === Mesh.FLIP_N_ROTATE_TILE) {
  116. uvs = uvs.concat(uvBase[((x % 2) + (y % 2)) % 2]);
  117. }
  118. else if (flipTile === Mesh.FLIP_ROW || flipTile === Mesh.ROTATE_ROW || flipTile === Mesh.FLIP_N_ROTATE_ROW) {
  119. uvs = uvs.concat(uvBase[y % 2]);
  120. }
  121. else {
  122. uvs = uvs.concat(uvBase[0]);
  123. }
  124. colors.push(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1);
  125. normals.push(0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1);
  126. index += 4;
  127. }
  128. }
  129. //Part Tiles
  130. if (offsetX > 0 || offsetY > 0) {
  131. const partialBottomRow = offsetY > 0 && (alignV === Mesh.CENTER || alignV === Mesh.TOP);
  132. const partialTopRow = offsetY > 0 && (alignV === Mesh.CENTER || alignV === Mesh.BOTTOM);
  133. const partialLeftCol = offsetX > 0 && (alignH === Mesh.CENTER || alignH === Mesh.RIGHT);
  134. const partialRightCol = offsetX > 0 && (alignH === Mesh.CENTER || alignH === Mesh.LEFT);
  135. let uvPart = [];
  136. let a, b, c, d;
  137. //corners
  138. if (partialBottomRow && partialLeftCol) {
  139. //bottom left corner
  140. positions.push(startX + adjustX, startY + adjustY, 0);
  141. positions.push(-halfWidth + adjustX, startY + adjustY, 0);
  142. positions.push(-halfWidth + adjustX, startY + offsetY + adjustY, 0);
  143. positions.push(startX + adjustX, startY + offsetY + adjustY, 0);
  144. indices.push(index, index + 1, index + 3, index + 1, index + 2, index + 3);
  145. index += 4;
  146. a = 1 - offsetX / tileWidth;
  147. b = 1 - offsetY / tileHeight;
  148. c = 1;
  149. d = 1;
  150. uvPart = [a, b, c, b, c, d, a, d];
  151. if (flipTile === Mesh.ROTATE_ROW) {
  152. uvPart = [1 - a, 1 - b, 1 - c, 1 - b, 1 - c, 1 - d, 1 - a, 1 - d];
  153. }
  154. if (flipTile === Mesh.FLIP_ROW) {
  155. uvPart = [1 - a, b, 1 - c, b, 1 - c, d, 1 - a, d];
  156. }
  157. if (flipTile === Mesh.FLIP_N_ROTATE_ROW) {
  158. uvPart = [a, 1 - b, c, 1 - b, c, 1 - d, a, 1 - d];
  159. }
  160. uvs = uvs.concat(uvPart);
  161. colors.push(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1);
  162. normals.push(0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1);
  163. }
  164. if (partialBottomRow && partialRightCol) {
  165. //bottom right corner
  166. positions.push(halfWidth + adjustX, startY + adjustY, 0);
  167. positions.push(endX + adjustX, startY + adjustY, 0);
  168. positions.push(endX + adjustX, startY + offsetY + adjustY, 0);
  169. positions.push(halfWidth + adjustX, startY + offsetY + adjustY, 0);
  170. indices.push(index, index + 1, index + 3, index + 1, index + 2, index + 3);
  171. index += 4;
  172. a = 0;
  173. b = 1 - offsetY / tileHeight;
  174. c = offsetX / tileWidth;
  175. d = 1;
  176. uvPart = [a, b, c, b, c, d, a, d];
  177. if (flipTile === Mesh.ROTATE_ROW || (flipTile === Mesh.ROTATE_TILE && tilesX % 2 === 0)) {
  178. uvPart = [1 - a, 1 - b, 1 - c, 1 - b, 1 - c, 1 - d, 1 - a, 1 - d];
  179. }
  180. if (flipTile === Mesh.FLIP_ROW || (flipTile === Mesh.FLIP_TILE && tilesX % 2 === 0)) {
  181. uvPart = [1 - a, b, 1 - c, b, 1 - c, d, 1 - a, d];
  182. }
  183. if (flipTile === Mesh.FLIP_N_ROTATE_ROW || (flipTile === Mesh.FLIP_N_ROTATE_TILE && tilesX % 2 === 0)) {
  184. uvPart = [a, 1 - b, c, 1 - b, c, 1 - d, a, 1 - d];
  185. }
  186. uvs = uvs.concat(uvPart);
  187. colors.push(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1);
  188. normals.push(0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1);
  189. }
  190. if (partialTopRow && partialLeftCol) {
  191. //top left corner
  192. positions.push(startX + adjustX, halfHeight + adjustY, 0);
  193. positions.push(-halfWidth + adjustX, halfHeight + adjustY, 0);
  194. positions.push(-halfWidth + adjustX, endY + adjustY, 0);
  195. positions.push(startX + adjustX, endY + adjustY, 0);
  196. indices.push(index, index + 1, index + 3, index + 1, index + 2, index + 3);
  197. index += 4;
  198. a = 1 - offsetX / tileWidth;
  199. b = 0;
  200. c = 1;
  201. d = offsetY / tileHeight;
  202. uvPart = [a, b, c, b, c, d, a, d];
  203. if ((flipTile === Mesh.ROTATE_ROW && tilesY % 2 === 1) || (flipTile === Mesh.ROTATE_TILE && tilesY % 1 === 0)) {
  204. uvPart = [1 - a, 1 - b, 1 - c, 1 - b, 1 - c, 1 - d, 1 - a, 1 - d];
  205. }
  206. if ((flipTile === Mesh.FLIP_ROW && tilesY % 2 === 1) || (flipTile === Mesh.FLIP_TILE && tilesY % 2 === 0)) {
  207. uvPart = [1 - a, b, 1 - c, b, 1 - c, d, 1 - a, d];
  208. }
  209. if ((flipTile === Mesh.FLIP_N_ROTATE_ROW && tilesY % 2 === 1) || (flipTile === Mesh.FLIP_N_ROTATE_TILE && tilesY % 2 === 0)) {
  210. uvPart = [a, 1 - b, c, 1 - b, c, 1 - d, a, 1 - d];
  211. }
  212. uvs = uvs.concat(uvPart);
  213. colors.push(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1);
  214. normals.push(0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1);
  215. }
  216. if (partialTopRow && partialRightCol) {
  217. //top right corner
  218. positions.push(halfWidth + adjustX, halfHeight + adjustY, 0);
  219. positions.push(endX + adjustX, halfHeight + adjustY, 0);
  220. positions.push(endX + adjustX, endY + adjustY, 0);
  221. positions.push(halfWidth + adjustX, endY + adjustY, 0);
  222. indices.push(index, index + 1, index + 3, index + 1, index + 2, index + 3);
  223. index += 4;
  224. a = 0;
  225. b = 0;
  226. c = offsetX / tileWidth;
  227. d = offsetY / tileHeight;
  228. uvPart = [a, b, c, b, c, d, a, d];
  229. if ((flipTile === Mesh.ROTATE_ROW && tilesY % 2 === 1) || (flipTile === Mesh.ROTATE_TILE && (tilesY + tilesX) % 2 === 1)) {
  230. uvPart = [1 - a, 1 - b, 1 - c, 1 - b, 1 - c, 1 - d, 1 - a, 1 - d];
  231. }
  232. if ((flipTile === Mesh.FLIP_ROW && tilesY % 2 === 1) || (flipTile === Mesh.FLIP_TILE && (tilesY + tilesX) % 2 === 1)) {
  233. uvPart = [1 - a, b, 1 - c, b, 1 - c, d, 1 - a, d];
  234. }
  235. if ((flipTile === Mesh.FLIP_N_ROTATE_ROW && tilesY % 2 === 1) || (flipTile === Mesh.FLIP_N_ROTATE_TILE && (tilesY + tilesX) % 2 === 1)) {
  236. uvPart = [a, 1 - b, c, 1 - b, c, 1 - d, a, 1 - d];
  237. }
  238. uvs = uvs.concat(uvPart);
  239. colors.push(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1);
  240. normals.push(0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1);
  241. }
  242. //part rows
  243. if (partialBottomRow) {
  244. const uvBaseBR = [];
  245. a = 0;
  246. b = 1 - offsetY / tileHeight;
  247. c = 1;
  248. d = 1;
  249. uvBaseBR[0] = [a, b, c, b, c, d, a, d];
  250. uvBaseBR[1] = [a, b, c, b, c, d, a, d];
  251. if (flipTile === Mesh.ROTATE_TILE || flipTile === Mesh.ROTATE_ROW) {
  252. uvBaseBR[1] = [1 - a, 1 - b, 1 - c, 1 - b, 1 - c, 1 - d, 1 - a, 1 - d];
  253. }
  254. if (flipTile === Mesh.FLIP_TILE || flipTile === Mesh.FLIP_ROW) {
  255. uvBaseBR[1] = [1 - a, b, 1 - c, b, 1 - c, d, 1 - a, d];
  256. }
  257. if (flipTile === Mesh.FLIP_N_ROTATE_TILE || flipTile === Mesh.FLIP_N_ROTATE_ROW) {
  258. uvBaseBR[1] = [a, 1 - b, c, 1 - b, c, 1 - d, a, 1 - d];
  259. }
  260. for (let x = 0; x < tilesX; x++) {
  261. positions.push(-halfWidth + x * tileWidth + adjustX, startY + adjustY, 0);
  262. positions.push(-halfWidth + (x + 1) * tileWidth + adjustX, startY + adjustY, 0);
  263. positions.push(-halfWidth + (x + 1) * tileWidth + adjustX, startY + offsetY + adjustY, 0);
  264. positions.push(-halfWidth + x * tileWidth + adjustX, startY + offsetY + adjustY, 0);
  265. indices.push(index, index + 1, index + 3, index + 1, index + 2, index + 3);
  266. index += 4;
  267. if (flipTile === Mesh.FLIP_TILE || flipTile === Mesh.ROTATE_TILE || flipTile === Mesh.FLIP_N_ROTATE_TILE) {
  268. uvs = uvs.concat(uvBaseBR[(x + 1) % 2]);
  269. }
  270. else if (flipTile === Mesh.FLIP_ROW || flipTile === Mesh.ROTATE_ROW || flipTile === Mesh.FLIP_N_ROTATE_ROW) {
  271. uvs = uvs.concat(uvBaseBR[1]);
  272. }
  273. else {
  274. uvs = uvs.concat(uvBaseBR[0]);
  275. }
  276. colors.push(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1);
  277. normals.push(0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1);
  278. }
  279. }
  280. if (partialTopRow) {
  281. const uvBaseTR = [];
  282. a = 0;
  283. b = 0;
  284. c = 1;
  285. d = offsetY / tileHeight;
  286. uvBaseTR[0] = [a, b, c, b, c, d, a, d];
  287. uvBaseTR[1] = [a, b, c, b, c, d, a, d];
  288. if (flipTile === Mesh.ROTATE_TILE || flipTile === Mesh.ROTATE_ROW) {
  289. uvBaseTR[1] = [1 - a, 1 - b, 1 - c, 1 - b, 1 - c, 1 - d, 1 - a, 1 - d];
  290. }
  291. if (flipTile === Mesh.FLIP_TILE || flipTile === Mesh.FLIP_ROW) {
  292. uvBaseTR[1] = [1 - a, b, 1 - c, b, 1 - c, d, 1 - a, d];
  293. }
  294. if (flipTile === Mesh.FLIP_N_ROTATE_TILE || flipTile === Mesh.FLIP_N_ROTATE_ROW) {
  295. uvBaseTR[1] = [a, 1 - b, c, 1 - b, c, 1 - d, a, 1 - d];
  296. }
  297. for (let x = 0; x < tilesX; x++) {
  298. positions.push(-halfWidth + x * tileWidth + adjustX, endY - offsetY + adjustY, 0);
  299. positions.push(-halfWidth + (x + 1) * tileWidth + adjustX, endY - offsetY + adjustY, 0);
  300. positions.push(-halfWidth + (x + 1) * tileWidth + adjustX, endY + adjustY, 0);
  301. positions.push(-halfWidth + x * tileWidth + adjustX, endY + adjustY, 0);
  302. indices.push(index, index + 1, index + 3, index + 1, index + 2, index + 3);
  303. index += 4;
  304. if (flipTile === Mesh.FLIP_TILE || flipTile === Mesh.ROTATE_TILE || flipTile === Mesh.FLIP_N_ROTATE_TILE) {
  305. uvs = uvs.concat(uvBaseTR[(x + tilesY) % 2]);
  306. }
  307. else if (flipTile === Mesh.FLIP_ROW || flipTile === Mesh.ROTATE_ROW || flipTile === Mesh.FLIP_N_ROTATE_ROW) {
  308. uvs = uvs.concat(uvBaseTR[tilesY % 2]);
  309. }
  310. else {
  311. uvs = uvs.concat(uvBaseTR[0]);
  312. }
  313. colors.push(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1);
  314. normals.push(0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1);
  315. }
  316. }
  317. if (partialLeftCol) {
  318. const uvBaseLC = [];
  319. a = 1 - offsetX / tileWidth;
  320. b = 0;
  321. c = 1;
  322. d = 1;
  323. uvBaseLC[0] = [a, b, c, b, c, d, a, d];
  324. uvBaseLC[1] = [a, b, c, b, c, d, a, d];
  325. if (flipTile === Mesh.ROTATE_TILE || flipTile === Mesh.ROTATE_ROW) {
  326. uvBaseLC[1] = [1 - a, 1 - b, 1 - c, 1 - b, 1 - c, 1 - d, 1 - a, 1 - d];
  327. }
  328. if (flipTile === Mesh.FLIP_TILE || flipTile === Mesh.FLIP_ROW) {
  329. uvBaseLC[1] = [1 - a, b, 1 - c, b, 1 - c, d, 1 - a, d];
  330. }
  331. if (flipTile === Mesh.FLIP_N_ROTATE_TILE || flipTile === Mesh.FLIP_N_ROTATE_ROW) {
  332. uvBaseLC[1] = [a, 1 - b, c, 1 - b, c, 1 - d, a, 1 - d];
  333. }
  334. for (let y = 0; y < tilesY; y++) {
  335. positions.push(startX + adjustX, -halfHeight + y * tileHeight + adjustY, 0);
  336. positions.push(startX + offsetX + adjustX, -halfHeight + y * tileHeight + adjustY, 0);
  337. positions.push(startX + offsetX + adjustX, -halfHeight + (y + 1) * tileHeight + adjustY, 0);
  338. positions.push(startX + adjustX, -halfHeight + (y + 1) * tileHeight + adjustY, 0);
  339. indices.push(index, index + 1, index + 3, index + 1, index + 2, index + 3);
  340. index += 4;
  341. if (flipTile === Mesh.FLIP_TILE || flipTile === Mesh.ROTATE_TILE || flipTile === Mesh.FLIP_N_ROTATE_TILE) {
  342. uvs = uvs.concat(uvBaseLC[(y + 1) % 2]);
  343. }
  344. else if (flipTile === Mesh.FLIP_ROW || flipTile === Mesh.ROTATE_ROW || flipTile === Mesh.FLIP_N_ROTATE_ROW) {
  345. uvs = uvs.concat(uvBaseLC[y % 2]);
  346. }
  347. else {
  348. uvs = uvs.concat(uvBaseLC[0]);
  349. }
  350. colors.push(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1);
  351. normals.push(0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1);
  352. }
  353. }
  354. if (partialRightCol) {
  355. const uvBaseRC = [];
  356. a = 0;
  357. b = 0;
  358. c = offsetX / tileHeight;
  359. d = 1;
  360. uvBaseRC[0] = [a, b, c, b, c, d, a, d];
  361. uvBaseRC[1] = [a, b, c, b, c, d, a, d];
  362. if (flipTile === Mesh.ROTATE_TILE || flipTile === Mesh.ROTATE_ROW) {
  363. uvBaseRC[1] = [1 - a, 1 - b, 1 - c, 1 - b, 1 - c, 1 - d, 1 - a, 1 - d];
  364. }
  365. if (flipTile === Mesh.FLIP_TILE || flipTile === Mesh.FLIP_ROW) {
  366. uvBaseRC[1] = [1 - a, b, 1 - c, b, 1 - c, d, 1 - a, d];
  367. }
  368. if (flipTile === Mesh.FLIP_N_ROTATE_TILE || flipTile === Mesh.FLIP_N_ROTATE_ROW) {
  369. uvBaseRC[1] = [a, 1 - b, c, 1 - b, c, 1 - d, a, 1 - d];
  370. }
  371. for (let y = 0; y < tilesY; y++) {
  372. positions.push(endX - offsetX + adjustX, -halfHeight + y * tileHeight + adjustY, 0);
  373. positions.push(endX + adjustX, -halfHeight + y * tileHeight + adjustY, 0);
  374. positions.push(endX + adjustX, -halfHeight + (y + 1) * tileHeight + adjustY, 0);
  375. positions.push(endX - offsetX + adjustX, -halfHeight + (y + 1) * tileHeight + adjustY, 0);
  376. indices.push(index, index + 1, index + 3, index + 1, index + 2, index + 3);
  377. index += 4;
  378. if (flipTile === Mesh.FLIP_TILE || flipTile === Mesh.ROTATE_TILE || flipTile === Mesh.FLIP_N_ROTATE_TILE) {
  379. uvs = uvs.concat(uvBaseRC[(y + tilesX) % 2]);
  380. }
  381. else if (flipTile === Mesh.FLIP_ROW || flipTile === Mesh.ROTATE_ROW || flipTile === Mesh.FLIP_N_ROTATE_ROW) {
  382. uvs = uvs.concat(uvBaseRC[y % 2]);
  383. }
  384. else {
  385. uvs = uvs.concat(uvBaseRC[0]);
  386. }
  387. colors.push(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1);
  388. normals.push(0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1);
  389. }
  390. }
  391. }
  392. const sideOrientation = options.sideOrientation === 0 ? 0 : options.sideOrientation || VertexData.DEFAULTSIDE;
  393. // sides
  394. VertexData._ComputeSides(sideOrientation, positions, indices, normals, uvs, options.frontUVs, options.backUVs);
  395. // Result
  396. const vertexData = new VertexData();
  397. vertexData.indices = indices;
  398. vertexData.positions = positions;
  399. vertexData.normals = normals;
  400. vertexData.uvs = uvs;
  401. const totalColors = sideOrientation === VertexData.DOUBLESIDE ? colors.concat(colors) : colors;
  402. vertexData.colors = totalColors;
  403. return vertexData;
  404. }
  405. /**
  406. * Creates a tiled plane mesh
  407. * @see https://doc.babylonjs.com/features/featuresDeepDive/mesh/creation/set/tiled_plane
  408. * @param name defines the name of the mesh
  409. * @param options an object used to set the following optional parameters for the tiled plane, required but can be empty
  410. * * pattern a limited pattern arrangement depending on the number
  411. * * size of the box
  412. * * width of the box, overwrites size
  413. * * height of the box, overwrites size
  414. * * tileSize sets the width, height and depth of the tile to the value of size, optional default 1
  415. * * tileWidth sets the width (x direction) of the tile, overwrites the width set by size, optional, default size
  416. * * tileHeight sets the height (y direction) of the tile, overwrites the height set by size, optional, default size
  417. * * sideOrientation optional and takes the values : Mesh.FRONTSIDE (default), Mesh.BACKSIDE or Mesh.DOUBLESIDE
  418. * * alignHorizontal places whole tiles aligned to the center, left or right of a row
  419. * * alignVertical places whole tiles aligned to the center, left or right of a column
  420. * * frontUvs only usable when you create a double-sided mesh, used to choose what parts of the texture image to crop and apply on the front side, optional, default vector4 (0, 0, 1, 1)
  421. * * backUVs only usable when you create a double-sided mesh, used to choose what parts of the texture image to crop and apply on the back side, optional, default vector4 (0, 0, 1, 1)
  422. * @param options.pattern
  423. * @param options.tileSize
  424. * @param options.tileWidth
  425. * @param options.tileHeight
  426. * @param options.size
  427. * @param options.width
  428. * @param options.height
  429. * @param options.alignHorizontal
  430. * @param options.alignVertical
  431. * @param options.sideOrientation
  432. * @param options.frontUVs
  433. * @param options.backUVs
  434. * @param options.updatable
  435. * @param scene defines the hosting scene
  436. * @returns the box mesh
  437. */
  438. export function CreateTiledPlane(name, options, scene = null) {
  439. const plane = new Mesh(name, scene);
  440. options.sideOrientation = Mesh._GetDefaultSideOrientation(options.sideOrientation);
  441. plane._originalBuilderSideOrientation = options.sideOrientation;
  442. const vertexData = CreateTiledPlaneVertexData(options);
  443. vertexData.applyToMesh(plane, options.updatable);
  444. return plane;
  445. }
  446. /**
  447. * Class containing static functions to help procedurally build meshes
  448. * @deprecated use CreateTiledPlane instead
  449. */
  450. export const TiledPlaneBuilder = {
  451. // eslint-disable-next-line @typescript-eslint/naming-convention
  452. CreateTiledPlane,
  453. };
  454. VertexData.CreateTiledPlane = CreateTiledPlaneVertexData;
  455. //# sourceMappingURL=tiledPlaneBuilder.js.map