polyhedronBuilder.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  1. import { Vector4 } from "../../Maths/math.vector.js";
  2. import { Color4 } from "../../Maths/math.color.js";
  3. import { Mesh } from "../mesh.js";
  4. import { VertexData } from "../mesh.vertexData.js";
  5. import { CompatibilityOptions } from "../../Compat/compatibilityOptions.js";
  6. // inspired from // http://stemkoski.github.io/Three.js/Polyhedra.html
  7. /**
  8. * Creates the VertexData for a Polyhedron
  9. * @param options an object used to set the following optional parameters for the polyhedron, required but can be empty
  10. * * type provided types are:
  11. * * 0 : Tetrahedron, 1 : Octahedron, 2 : Dodecahedron, 3 : Icosahedron, 4 : Rhombicuboctahedron, 5 : Triangular Prism, 6 : Pentagonal Prism, 7 : Hexagonal Prism, 8 : Square Pyramid (J1)
  12. * * 9 : Pentagonal Pyramid (J2), 10 : Triangular Dipyramid (J12), 11 : Pentagonal Dipyramid (J13), 12 : Elongated Square Dipyramid (J15), 13 : Elongated Pentagonal Dipyramid (J16), 14 : Elongated Pentagonal Cupola (J20)
  13. * * size the size of the IcoSphere, optional default 1
  14. * * sizeX allows stretching in the x direction, optional, default size
  15. * * sizeY allows stretching in the y direction, optional, default size
  16. * * sizeZ allows stretching in the z direction, optional, default size
  17. * * custom a number that overwrites the type to create from an extended set of polyhedron from https://www.babylonjs-playground.com/#21QRSK#15 with minimised editor
  18. * * faceUV an array of Vector4 elements used to set different images to the top, rings and bottom respectively
  19. * * faceColors an array of Color3 elements used to set different colors to the top, rings and bottom respectively
  20. * * flat when true creates a flat shaded mesh, optional, default true
  21. * * subdivisions increasing the subdivisions increases the number of faces, optional, default 4
  22. * * sideOrientation optional and takes the values : Mesh.FRONTSIDE (default), Mesh.BACKSIDE or Mesh.DOUBLESIDE
  23. * * 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)
  24. * * 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)
  25. * @returns the VertexData of the Polyhedron
  26. */
  27. export function CreatePolyhedronVertexData(options) {
  28. // provided polyhedron types :
  29. // 0 : Tetrahedron, 1 : Octahedron, 2 : Dodecahedron, 3 : Icosahedron, 4 : Rhombicuboctahedron, 5 : Triangular Prism, 6 : Pentagonal Prism, 7 : Hexagonal Prism, 8 : Square Pyramid (J1)
  30. // 9 : Pentagonal Pyramid (J2), 10 : Triangular Dipyramid (J12), 11 : Pentagonal Dipyramid (J13), 12 : Elongated Square Dipyramid (J15), 13 : Elongated Pentagonal Dipyramid (J16), 14 : Elongated Pentagonal Cupola (J20)
  31. const polyhedra = [];
  32. polyhedra[0] = {
  33. vertex: [
  34. [0, 0, 1.732051],
  35. [1.632993, 0, -0.5773503],
  36. [-0.8164966, 1.414214, -0.5773503],
  37. [-0.8164966, -1.414214, -0.5773503],
  38. ],
  39. face: [
  40. [0, 1, 2],
  41. [0, 2, 3],
  42. [0, 3, 1],
  43. [1, 3, 2],
  44. ],
  45. };
  46. polyhedra[1] = {
  47. vertex: [
  48. [0, 0, 1.414214],
  49. [1.414214, 0, 0],
  50. [0, 1.414214, 0],
  51. [-1.414214, 0, 0],
  52. [0, -1.414214, 0],
  53. [0, 0, -1.414214],
  54. ],
  55. face: [
  56. [0, 1, 2],
  57. [0, 2, 3],
  58. [0, 3, 4],
  59. [0, 4, 1],
  60. [1, 4, 5],
  61. [1, 5, 2],
  62. [2, 5, 3],
  63. [3, 5, 4],
  64. ],
  65. };
  66. polyhedra[2] = {
  67. vertex: [
  68. [0, 0, 1.070466],
  69. [0.7136442, 0, 0.7978784],
  70. [-0.3568221, 0.618034, 0.7978784],
  71. [-0.3568221, -0.618034, 0.7978784],
  72. [0.7978784, 0.618034, 0.3568221],
  73. [0.7978784, -0.618034, 0.3568221],
  74. [-0.9341724, 0.381966, 0.3568221],
  75. [0.1362939, 1, 0.3568221],
  76. [0.1362939, -1, 0.3568221],
  77. [-0.9341724, -0.381966, 0.3568221],
  78. [0.9341724, 0.381966, -0.3568221],
  79. [0.9341724, -0.381966, -0.3568221],
  80. [-0.7978784, 0.618034, -0.3568221],
  81. [-0.1362939, 1, -0.3568221],
  82. [-0.1362939, -1, -0.3568221],
  83. [-0.7978784, -0.618034, -0.3568221],
  84. [0.3568221, 0.618034, -0.7978784],
  85. [0.3568221, -0.618034, -0.7978784],
  86. [-0.7136442, 0, -0.7978784],
  87. [0, 0, -1.070466],
  88. ],
  89. face: [
  90. [0, 1, 4, 7, 2],
  91. [0, 2, 6, 9, 3],
  92. [0, 3, 8, 5, 1],
  93. [1, 5, 11, 10, 4],
  94. [2, 7, 13, 12, 6],
  95. [3, 9, 15, 14, 8],
  96. [4, 10, 16, 13, 7],
  97. [5, 8, 14, 17, 11],
  98. [6, 12, 18, 15, 9],
  99. [10, 11, 17, 19, 16],
  100. [12, 13, 16, 19, 18],
  101. [14, 15, 18, 19, 17],
  102. ],
  103. };
  104. polyhedra[3] = {
  105. vertex: [
  106. [0, 0, 1.175571],
  107. [1.051462, 0, 0.5257311],
  108. [0.3249197, 1, 0.5257311],
  109. [-0.8506508, 0.618034, 0.5257311],
  110. [-0.8506508, -0.618034, 0.5257311],
  111. [0.3249197, -1, 0.5257311],
  112. [0.8506508, 0.618034, -0.5257311],
  113. [0.8506508, -0.618034, -0.5257311],
  114. [-0.3249197, 1, -0.5257311],
  115. [-1.051462, 0, -0.5257311],
  116. [-0.3249197, -1, -0.5257311],
  117. [0, 0, -1.175571],
  118. ],
  119. face: [
  120. [0, 1, 2],
  121. [0, 2, 3],
  122. [0, 3, 4],
  123. [0, 4, 5],
  124. [0, 5, 1],
  125. [1, 5, 7],
  126. [1, 7, 6],
  127. [1, 6, 2],
  128. [2, 6, 8],
  129. [2, 8, 3],
  130. [3, 8, 9],
  131. [3, 9, 4],
  132. [4, 9, 10],
  133. [4, 10, 5],
  134. [5, 10, 7],
  135. [6, 7, 11],
  136. [6, 11, 8],
  137. [7, 10, 11],
  138. [8, 11, 9],
  139. [9, 11, 10],
  140. ],
  141. };
  142. polyhedra[4] = {
  143. vertex: [
  144. [0, 0, 1.070722],
  145. [0.7148135, 0, 0.7971752],
  146. [-0.104682, 0.7071068, 0.7971752],
  147. [-0.6841528, 0.2071068, 0.7971752],
  148. [-0.104682, -0.7071068, 0.7971752],
  149. [0.6101315, 0.7071068, 0.5236279],
  150. [1.04156, 0.2071068, 0.1367736],
  151. [0.6101315, -0.7071068, 0.5236279],
  152. [-0.3574067, 1, 0.1367736],
  153. [-0.7888348, -0.5, 0.5236279],
  154. [-0.9368776, 0.5, 0.1367736],
  155. [-0.3574067, -1, 0.1367736],
  156. [0.3574067, 1, -0.1367736],
  157. [0.9368776, -0.5, -0.1367736],
  158. [0.7888348, 0.5, -0.5236279],
  159. [0.3574067, -1, -0.1367736],
  160. [-0.6101315, 0.7071068, -0.5236279],
  161. [-1.04156, -0.2071068, -0.1367736],
  162. [-0.6101315, -0.7071068, -0.5236279],
  163. [0.104682, 0.7071068, -0.7971752],
  164. [0.6841528, -0.2071068, -0.7971752],
  165. [0.104682, -0.7071068, -0.7971752],
  166. [-0.7148135, 0, -0.7971752],
  167. [0, 0, -1.070722],
  168. ],
  169. face: [
  170. [0, 2, 3],
  171. [1, 6, 5],
  172. [4, 9, 11],
  173. [7, 15, 13],
  174. [8, 16, 10],
  175. [12, 14, 19],
  176. [17, 22, 18],
  177. [20, 21, 23],
  178. [0, 1, 5, 2],
  179. [0, 3, 9, 4],
  180. [0, 4, 7, 1],
  181. [1, 7, 13, 6],
  182. [2, 5, 12, 8],
  183. [2, 8, 10, 3],
  184. [3, 10, 17, 9],
  185. [4, 11, 15, 7],
  186. [5, 6, 14, 12],
  187. [6, 13, 20, 14],
  188. [8, 12, 19, 16],
  189. [9, 17, 18, 11],
  190. [10, 16, 22, 17],
  191. [11, 18, 21, 15],
  192. [13, 15, 21, 20],
  193. [14, 20, 23, 19],
  194. [16, 19, 23, 22],
  195. [18, 22, 23, 21],
  196. ],
  197. };
  198. polyhedra[5] = {
  199. vertex: [
  200. [0, 0, 1.322876],
  201. [1.309307, 0, 0.1889822],
  202. [-0.9819805, 0.8660254, 0.1889822],
  203. [0.1636634, -1.299038, 0.1889822],
  204. [0.3273268, 0.8660254, -0.9449112],
  205. [-0.8183171, -0.4330127, -0.9449112],
  206. ],
  207. face: [
  208. [0, 3, 1],
  209. [2, 4, 5],
  210. [0, 1, 4, 2],
  211. [0, 2, 5, 3],
  212. [1, 3, 5, 4],
  213. ],
  214. };
  215. polyhedra[6] = {
  216. vertex: [
  217. [0, 0, 1.159953],
  218. [1.013464, 0, 0.5642542],
  219. [-0.3501431, 0.9510565, 0.5642542],
  220. [-0.7715208, -0.6571639, 0.5642542],
  221. [0.6633206, 0.9510565, -0.03144481],
  222. [0.8682979, -0.6571639, -0.3996071],
  223. [-1.121664, 0.2938926, -0.03144481],
  224. [-0.2348831, -1.063314, -0.3996071],
  225. [0.5181548, 0.2938926, -0.9953061],
  226. [-0.5850262, -0.112257, -0.9953061],
  227. ],
  228. face: [
  229. [0, 1, 4, 2],
  230. [0, 2, 6, 3],
  231. [1, 5, 8, 4],
  232. [3, 6, 9, 7],
  233. [5, 7, 9, 8],
  234. [0, 3, 7, 5, 1],
  235. [2, 4, 8, 9, 6],
  236. ],
  237. };
  238. polyhedra[7] = {
  239. vertex: [
  240. [0, 0, 1.118034],
  241. [0.8944272, 0, 0.6708204],
  242. [-0.2236068, 0.8660254, 0.6708204],
  243. [-0.7826238, -0.4330127, 0.6708204],
  244. [0.6708204, 0.8660254, 0.2236068],
  245. [1.006231, -0.4330127, -0.2236068],
  246. [-1.006231, 0.4330127, 0.2236068],
  247. [-0.6708204, -0.8660254, -0.2236068],
  248. [0.7826238, 0.4330127, -0.6708204],
  249. [0.2236068, -0.8660254, -0.6708204],
  250. [-0.8944272, 0, -0.6708204],
  251. [0, 0, -1.118034],
  252. ],
  253. face: [
  254. [0, 1, 4, 2],
  255. [0, 2, 6, 3],
  256. [1, 5, 8, 4],
  257. [3, 6, 10, 7],
  258. [5, 9, 11, 8],
  259. [7, 10, 11, 9],
  260. [0, 3, 7, 9, 5, 1],
  261. [2, 4, 8, 11, 10, 6],
  262. ],
  263. };
  264. polyhedra[8] = {
  265. vertex: [
  266. [-0.729665, 0.670121, 0.319155],
  267. [-0.655235, -0.29213, -0.754096],
  268. [-0.093922, -0.607123, 0.537818],
  269. [0.702196, 0.595691, 0.485187],
  270. [0.776626, -0.36656, -0.588064],
  271. ],
  272. face: [
  273. [1, 4, 2],
  274. [0, 1, 2],
  275. [3, 0, 2],
  276. [4, 3, 2],
  277. [4, 1, 0, 3],
  278. ],
  279. };
  280. polyhedra[9] = {
  281. vertex: [
  282. [-0.868849, -0.100041, 0.61257],
  283. [-0.329458, 0.976099, 0.28078],
  284. [-0.26629, -0.013796, -0.477654],
  285. [-0.13392, -1.034115, 0.229829],
  286. [0.738834, 0.707117, -0.307018],
  287. [0.859683, -0.535264, -0.338508],
  288. ],
  289. face: [
  290. [3, 0, 2],
  291. [5, 3, 2],
  292. [4, 5, 2],
  293. [1, 4, 2],
  294. [0, 1, 2],
  295. [0, 3, 5, 4, 1],
  296. ],
  297. };
  298. polyhedra[10] = {
  299. vertex: [
  300. [-0.610389, 0.243975, 0.531213],
  301. [-0.187812, -0.48795, -0.664016],
  302. [-0.187812, 0.9759, -0.664016],
  303. [0.187812, -0.9759, 0.664016],
  304. [0.798201, 0.243975, 0.132803],
  305. ],
  306. face: [
  307. [1, 3, 0],
  308. [3, 4, 0],
  309. [3, 1, 4],
  310. [0, 2, 1],
  311. [0, 4, 2],
  312. [2, 4, 1],
  313. ],
  314. };
  315. polyhedra[11] = {
  316. vertex: [
  317. [-1.028778, 0.392027, -0.048786],
  318. [-0.640503, -0.646161, 0.621837],
  319. [-0.125162, -0.395663, -0.540059],
  320. [0.004683, 0.888447, -0.651988],
  321. [0.125161, 0.395663, 0.540059],
  322. [0.632925, -0.791376, 0.433102],
  323. [1.031672, 0.157063, -0.354165],
  324. ],
  325. face: [
  326. [3, 2, 0],
  327. [2, 1, 0],
  328. [2, 5, 1],
  329. [0, 4, 3],
  330. [0, 1, 4],
  331. [4, 1, 5],
  332. [2, 3, 6],
  333. [3, 4, 6],
  334. [5, 2, 6],
  335. [4, 5, 6],
  336. ],
  337. };
  338. polyhedra[12] = {
  339. vertex: [
  340. [-0.669867, 0.334933, -0.529576],
  341. [-0.669867, 0.334933, 0.529577],
  342. [-0.4043, 1.212901, 0],
  343. [-0.334933, -0.669867, -0.529576],
  344. [-0.334933, -0.669867, 0.529577],
  345. [0.334933, 0.669867, -0.529576],
  346. [0.334933, 0.669867, 0.529577],
  347. [0.4043, -1.212901, 0],
  348. [0.669867, -0.334933, -0.529576],
  349. [0.669867, -0.334933, 0.529577],
  350. ],
  351. face: [
  352. [8, 9, 7],
  353. [6, 5, 2],
  354. [3, 8, 7],
  355. [5, 0, 2],
  356. [4, 3, 7],
  357. [0, 1, 2],
  358. [9, 4, 7],
  359. [1, 6, 2],
  360. [9, 8, 5, 6],
  361. [8, 3, 0, 5],
  362. [3, 4, 1, 0],
  363. [4, 9, 6, 1],
  364. ],
  365. };
  366. polyhedra[13] = {
  367. vertex: [
  368. [-0.931836, 0.219976, -0.264632],
  369. [-0.636706, 0.318353, 0.692816],
  370. [-0.613483, -0.735083, -0.264632],
  371. [-0.326545, 0.979634, 0],
  372. [-0.318353, -0.636706, 0.692816],
  373. [-0.159176, 0.477529, -0.856368],
  374. [0.159176, -0.477529, -0.856368],
  375. [0.318353, 0.636706, 0.692816],
  376. [0.326545, -0.979634, 0],
  377. [0.613482, 0.735082, -0.264632],
  378. [0.636706, -0.318353, 0.692816],
  379. [0.931835, -0.219977, -0.264632],
  380. ],
  381. face: [
  382. [11, 10, 8],
  383. [7, 9, 3],
  384. [6, 11, 8],
  385. [9, 5, 3],
  386. [2, 6, 8],
  387. [5, 0, 3],
  388. [4, 2, 8],
  389. [0, 1, 3],
  390. [10, 4, 8],
  391. [1, 7, 3],
  392. [10, 11, 9, 7],
  393. [11, 6, 5, 9],
  394. [6, 2, 0, 5],
  395. [2, 4, 1, 0],
  396. [4, 10, 7, 1],
  397. ],
  398. };
  399. polyhedra[14] = {
  400. vertex: [
  401. [-0.93465, 0.300459, -0.271185],
  402. [-0.838689, -0.260219, -0.516017],
  403. [-0.711319, 0.717591, 0.128359],
  404. [-0.710334, -0.156922, 0.080946],
  405. [-0.599799, 0.556003, -0.725148],
  406. [-0.503838, -0.004675, -0.969981],
  407. [-0.487004, 0.26021, 0.48049],
  408. [-0.460089, -0.750282, -0.512622],
  409. [-0.376468, 0.973135, -0.325605],
  410. [-0.331735, -0.646985, 0.084342],
  411. [-0.254001, 0.831847, 0.530001],
  412. [-0.125239, -0.494738, -0.966586],
  413. [0.029622, 0.027949, 0.730817],
  414. [0.056536, -0.982543, -0.262295],
  415. [0.08085, 1.087391, 0.076037],
  416. [0.125583, -0.532729, 0.485984],
  417. [0.262625, 0.599586, 0.780328],
  418. [0.391387, -0.726999, -0.716259],
  419. [0.513854, -0.868287, 0.139347],
  420. [0.597475, 0.85513, 0.326364],
  421. [0.641224, 0.109523, 0.783723],
  422. [0.737185, -0.451155, 0.538891],
  423. [0.848705, -0.612742, -0.314616],
  424. [0.976075, 0.365067, 0.32976],
  425. [1.072036, -0.19561, 0.084927],
  426. ],
  427. face: [
  428. [15, 18, 21],
  429. [12, 20, 16],
  430. [6, 10, 2],
  431. [3, 0, 1],
  432. [9, 7, 13],
  433. [2, 8, 4, 0],
  434. [0, 4, 5, 1],
  435. [1, 5, 11, 7],
  436. [7, 11, 17, 13],
  437. [13, 17, 22, 18],
  438. [18, 22, 24, 21],
  439. [21, 24, 23, 20],
  440. [20, 23, 19, 16],
  441. [16, 19, 14, 10],
  442. [10, 14, 8, 2],
  443. [15, 9, 13, 18],
  444. [12, 15, 21, 20],
  445. [6, 12, 16, 10],
  446. [3, 6, 2, 0],
  447. [9, 3, 1, 7],
  448. [9, 15, 12, 6, 3],
  449. [22, 17, 11, 5, 4, 8, 14, 19, 23, 24],
  450. ],
  451. };
  452. const type = options.type && (options.type < 0 || options.type >= polyhedra.length) ? 0 : options.type || 0;
  453. const size = options.size;
  454. const sizeX = options.sizeX || size || 1;
  455. const sizeY = options.sizeY || size || 1;
  456. const sizeZ = options.sizeZ || size || 1;
  457. const data = options.custom || polyhedra[type];
  458. const nbfaces = data.face.length;
  459. const faceUV = options.faceUV || new Array(nbfaces);
  460. const faceColors = options.faceColors;
  461. const flat = options.flat === undefined ? true : options.flat;
  462. const sideOrientation = options.sideOrientation === 0 ? 0 : options.sideOrientation || VertexData.DEFAULTSIDE;
  463. const positions = [];
  464. const indices = [];
  465. const normals = [];
  466. const uvs = [];
  467. const colors = [];
  468. let index = 0;
  469. let faceIdx = 0; // face cursor in the array "indexes"
  470. const indexes = [];
  471. let i = 0;
  472. let f = 0;
  473. let u, v, ang, x, y, tmp;
  474. // default face colors and UV if undefined
  475. if (flat) {
  476. for (f = 0; f < nbfaces; f++) {
  477. if (faceColors && faceColors[f] === undefined) {
  478. faceColors[f] = new Color4(1, 1, 1, 1);
  479. }
  480. if (faceUV && faceUV[f] === undefined) {
  481. faceUV[f] = new Vector4(0, 0, 1, 1);
  482. }
  483. }
  484. }
  485. if (!flat) {
  486. for (i = 0; i < data.vertex.length; i++) {
  487. positions.push(data.vertex[i][0] * sizeX, data.vertex[i][1] * sizeY, data.vertex[i][2] * sizeZ);
  488. uvs.push(0, CompatibilityOptions.UseOpenGLOrientationForUV ? 1.0 : 0);
  489. }
  490. for (f = 0; f < nbfaces; f++) {
  491. for (i = 0; i < data.face[f].length - 2; i++) {
  492. indices.push(data.face[f][0], data.face[f][i + 2], data.face[f][i + 1]);
  493. }
  494. }
  495. }
  496. else {
  497. for (f = 0; f < nbfaces; f++) {
  498. const fl = data.face[f].length; // number of vertices of the current face
  499. ang = (2 * Math.PI) / fl;
  500. x = 0.5 * Math.tan(ang / 2);
  501. y = 0.5;
  502. // positions, uvs, colors
  503. for (i = 0; i < fl; i++) {
  504. // positions
  505. positions.push(data.vertex[data.face[f][i]][0] * sizeX, data.vertex[data.face[f][i]][1] * sizeY, data.vertex[data.face[f][i]][2] * sizeZ);
  506. indexes.push(index);
  507. index++;
  508. // uvs
  509. u = faceUV[f].x + (faceUV[f].z - faceUV[f].x) * (0.5 + x);
  510. v = faceUV[f].y + (faceUV[f].w - faceUV[f].y) * (y - 0.5);
  511. uvs.push(u, CompatibilityOptions.UseOpenGLOrientationForUV ? 1.0 - v : v);
  512. tmp = x * Math.cos(ang) - y * Math.sin(ang);
  513. y = x * Math.sin(ang) + y * Math.cos(ang);
  514. x = tmp;
  515. // colors
  516. if (faceColors) {
  517. colors.push(faceColors[f].r, faceColors[f].g, faceColors[f].b, faceColors[f].a);
  518. }
  519. }
  520. // indices from indexes
  521. for (i = 0; i < fl - 2; i++) {
  522. indices.push(indexes[0 + faceIdx], indexes[i + 2 + faceIdx], indexes[i + 1 + faceIdx]);
  523. }
  524. faceIdx += fl;
  525. }
  526. }
  527. VertexData.ComputeNormals(positions, indices, normals);
  528. VertexData._ComputeSides(sideOrientation, positions, indices, normals, uvs, options.frontUVs, options.backUVs);
  529. const vertexData = new VertexData();
  530. vertexData.positions = positions;
  531. vertexData.indices = indices;
  532. vertexData.normals = normals;
  533. vertexData.uvs = uvs;
  534. if (faceColors && flat) {
  535. vertexData.colors = colors;
  536. }
  537. return vertexData;
  538. }
  539. /**
  540. * Creates a polyhedron mesh
  541. * * The parameter `type` (positive integer, max 14, default 0) sets the polyhedron type to build among the 15 embbeded types. Please refer to the type sheet in the tutorial to choose the wanted type
  542. * * The parameter `size` (positive float, default 1) sets the polygon size
  543. * * You can overwrite the `size` on each dimension bu using the parameters `sizeX`, `sizeY` or `sizeZ` (positive floats, default to `size` value)
  544. * * You can build other polyhedron types than the 15 embbeded ones by setting the parameter `custom` (`polyhedronObject`, default null). If you set the parameter `custom`, this overrides the parameter `type`
  545. * * A `polyhedronObject` is a formatted javascript object. You'll find a full file with pre-set polyhedra here : https://github.com/BabylonJS/Extensions/tree/master/Polyhedron
  546. * * You can set the color and the UV of each side of the polyhedron with the parameters `faceColors` (Color4, default `(1, 1, 1, 1)`) and faceUV (Vector4, default `(0, 0, 1, 1)`)
  547. * * To understand how to set `faceUV` or `faceColors`, please read this by considering the right number of faces of your polyhedron, instead of only 6 for the box : https://doc.babylonjs.com/features/featuresDeepDive/materials/using/texturePerBoxFace
  548. * * The parameter `flat` (boolean, default true). If set to false, it gives the polyhedron a single global face, so less vertices and shared normals. In this case, `faceColors` and `faceUV` are ignored
  549. * * You can also set the mesh side orientation with the values : BABYLON.Mesh.FRONTSIDE (default), BABYLON.Mesh.BACKSIDE or BABYLON.Mesh.DOUBLESIDE
  550. * * If you create a double-sided mesh, you can choose what parts of the texture image to crop and stick respectively on the front and the back sides with the parameters `frontUVs` and `backUVs` (Vector4). Detail here : https://doc.babylonjs.com/features/featuresDeepDive/mesh/creation/set#side-orientation
  551. * * The mesh can be set to updatable with the boolean parameter `updatable` (default false) if its internal geometry is supposed to change once created
  552. * @param name defines the name of the mesh
  553. * @param options defines the options used to create the mesh
  554. * @param scene defines the hosting scene
  555. * @returns the polyhedron mesh
  556. * @see https://doc.babylonjs.com/features/featuresDeepDive/mesh/creation/polyhedra
  557. */
  558. export function CreatePolyhedron(name, options = {}, scene = null) {
  559. const polyhedron = new Mesh(name, scene);
  560. options.sideOrientation = Mesh._GetDefaultSideOrientation(options.sideOrientation);
  561. polyhedron._originalBuilderSideOrientation = options.sideOrientation;
  562. const vertexData = CreatePolyhedronVertexData(options);
  563. vertexData.applyToMesh(polyhedron, options.updatable);
  564. return polyhedron;
  565. }
  566. /**
  567. * Class containing static functions to help procedurally build meshes
  568. * @deprecated use the function directly from the module
  569. */
  570. export const PolyhedronBuilder = {
  571. // eslint-disable-next-line @typescript-eslint/naming-convention
  572. CreatePolyhedron,
  573. };
  574. VertexData.CreatePolyhedron = CreatePolyhedronVertexData;
  575. Mesh.CreatePolyhedron = (name, options, scene) => {
  576. return CreatePolyhedron(name, options, scene);
  577. };
  578. //# sourceMappingURL=polyhedronBuilder.js.map