babylon.mesh.vertexData.ts 76 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678
  1. module BABYLON {
  2. export interface IGetSetVerticesData {
  3. isVerticesDataPresent(kind: string): boolean;
  4. getVerticesData(kind: string, copyWhenShared?: boolean): number[] | Float32Array;
  5. getIndices(copyWhenShared?: boolean): number[];
  6. setVerticesData(kind: string, data: number[] | Float32Array, updatable?: boolean): void;
  7. updateVerticesData(kind: string, data: number[] | Float32Array, updateExtends?: boolean, makeItUnique?: boolean): void;
  8. setIndices(indices: number[] | Float32Array): void;
  9. }
  10. export class VertexData {
  11. public positions: number[] | Float32Array;
  12. public normals: number[] | Float32Array;
  13. public uvs: number[] | Float32Array;
  14. public uvs2: number[] | Float32Array;
  15. public uvs3: number[] | Float32Array;
  16. public uvs4: number[] | Float32Array;
  17. public uvs5: number[] | Float32Array;
  18. public uvs6: number[] | Float32Array;
  19. public colors: number[] | Float32Array;
  20. public matricesIndices: number[] | Float32Array;
  21. public matricesWeights: number[] | Float32Array;
  22. public indices: number[];
  23. public set(data: number[] | Float32Array, kind: string) {
  24. switch (kind) {
  25. case VertexBuffer.PositionKind:
  26. this.positions = data;
  27. break;
  28. case VertexBuffer.NormalKind:
  29. this.normals = data;
  30. break;
  31. case VertexBuffer.UVKind:
  32. this.uvs = data;
  33. break;
  34. case VertexBuffer.UV2Kind:
  35. this.uvs2 = data;
  36. break;
  37. case VertexBuffer.UV3Kind:
  38. this.uvs3 = data;
  39. break;
  40. case VertexBuffer.UV4Kind:
  41. this.uvs4 = data;
  42. break;
  43. case VertexBuffer.UV5Kind:
  44. this.uvs5 = data;
  45. break;
  46. case VertexBuffer.UV6Kind:
  47. this.uvs6 = data;
  48. break;
  49. case VertexBuffer.ColorKind:
  50. this.colors = data;
  51. break;
  52. case VertexBuffer.MatricesIndicesKind:
  53. this.matricesIndices = data;
  54. break;
  55. case VertexBuffer.MatricesWeightsKind:
  56. this.matricesWeights = data;
  57. break;
  58. }
  59. }
  60. public applyToMesh(mesh: Mesh, updatable?: boolean): void {
  61. this._applyTo(mesh, updatable);
  62. }
  63. public applyToGeometry(geometry: Geometry, updatable?: boolean): void {
  64. this._applyTo(geometry, updatable);
  65. }
  66. public updateMesh(mesh: Mesh, updateExtends?: boolean, makeItUnique?: boolean): void {
  67. this._update(mesh);
  68. }
  69. public updateGeometry(geometry: Geometry, updateExtends?: boolean, makeItUnique?: boolean): void {
  70. this._update(geometry);
  71. }
  72. private _applyTo(meshOrGeometry: IGetSetVerticesData, updatable?: boolean) {
  73. if (this.positions) {
  74. meshOrGeometry.setVerticesData(VertexBuffer.PositionKind, this.positions, updatable);
  75. }
  76. if (this.normals) {
  77. meshOrGeometry.setVerticesData(VertexBuffer.NormalKind, this.normals, updatable);
  78. }
  79. if (this.uvs) {
  80. meshOrGeometry.setVerticesData(VertexBuffer.UVKind, this.uvs, updatable);
  81. }
  82. if (this.uvs2) {
  83. meshOrGeometry.setVerticesData(VertexBuffer.UV2Kind, this.uvs2, updatable);
  84. }
  85. if (this.uvs3) {
  86. meshOrGeometry.setVerticesData(VertexBuffer.UV3Kind, this.uvs3, updatable);
  87. }
  88. if (this.uvs4) {
  89. meshOrGeometry.setVerticesData(VertexBuffer.UV4Kind, this.uvs4, updatable);
  90. }
  91. if (this.uvs5) {
  92. meshOrGeometry.setVerticesData(VertexBuffer.UV5Kind, this.uvs5, updatable);
  93. }
  94. if (this.uvs6) {
  95. meshOrGeometry.setVerticesData(VertexBuffer.UV6Kind, this.uvs6, updatable);
  96. }
  97. if (this.colors) {
  98. meshOrGeometry.setVerticesData(VertexBuffer.ColorKind, this.colors, updatable);
  99. }
  100. if (this.matricesIndices) {
  101. meshOrGeometry.setVerticesData(VertexBuffer.MatricesIndicesKind, this.matricesIndices, updatable);
  102. }
  103. if (this.matricesWeights) {
  104. meshOrGeometry.setVerticesData(VertexBuffer.MatricesWeightsKind, this.matricesWeights, updatable);
  105. }
  106. if (this.indices) {
  107. meshOrGeometry.setIndices(this.indices);
  108. }
  109. }
  110. private _update(meshOrGeometry: IGetSetVerticesData, updateExtends?: boolean, makeItUnique?: boolean) {
  111. if (this.positions) {
  112. meshOrGeometry.updateVerticesData(VertexBuffer.PositionKind, this.positions, updateExtends, makeItUnique);
  113. }
  114. if (this.normals) {
  115. meshOrGeometry.updateVerticesData(VertexBuffer.NormalKind, this.normals, updateExtends, makeItUnique);
  116. }
  117. if (this.uvs) {
  118. meshOrGeometry.updateVerticesData(VertexBuffer.UVKind, this.uvs, updateExtends, makeItUnique);
  119. }
  120. if (this.uvs2) {
  121. meshOrGeometry.updateVerticesData(VertexBuffer.UV2Kind, this.uvs2, updateExtends, makeItUnique);
  122. }
  123. if (this.uvs3) {
  124. meshOrGeometry.updateVerticesData(VertexBuffer.UV3Kind, this.uvs3, updateExtends, makeItUnique);
  125. }
  126. if (this.uvs4) {
  127. meshOrGeometry.updateVerticesData(VertexBuffer.UV4Kind, this.uvs4, updateExtends, makeItUnique);
  128. }
  129. if (this.uvs5) {
  130. meshOrGeometry.updateVerticesData(VertexBuffer.UV5Kind, this.uvs5, updateExtends, makeItUnique);
  131. }
  132. if (this.uvs6) {
  133. meshOrGeometry.updateVerticesData(VertexBuffer.UV6Kind, this.uvs6, updateExtends, makeItUnique);
  134. }
  135. if (this.colors) {
  136. meshOrGeometry.updateVerticesData(VertexBuffer.ColorKind, this.colors, updateExtends, makeItUnique);
  137. }
  138. if (this.matricesIndices) {
  139. meshOrGeometry.updateVerticesData(VertexBuffer.MatricesIndicesKind, this.matricesIndices, updateExtends, makeItUnique);
  140. }
  141. if (this.matricesWeights) {
  142. meshOrGeometry.updateVerticesData(VertexBuffer.MatricesWeightsKind, this.matricesWeights, updateExtends, makeItUnique);
  143. }
  144. if (this.indices) {
  145. meshOrGeometry.setIndices(this.indices);
  146. }
  147. }
  148. public transform(matrix: Matrix): void {
  149. var transformed = Vector3.Zero();
  150. var index: number;
  151. if (this.positions) {
  152. var position = Vector3.Zero();
  153. for (index = 0; index < this.positions.length; index += 3) {
  154. Vector3.FromArrayToRef(this.positions, index, position);
  155. Vector3.TransformCoordinatesToRef(position, matrix, transformed);
  156. this.positions[index] = transformed.x;
  157. this.positions[index + 1] = transformed.y;
  158. this.positions[index + 2] = transformed.z;
  159. }
  160. }
  161. if (this.normals) {
  162. var normal = Vector3.Zero();
  163. for (index = 0; index < this.normals.length; index += 3) {
  164. Vector3.FromArrayToRef(this.normals, index, normal);
  165. Vector3.TransformNormalToRef(normal, matrix, transformed);
  166. this.normals[index] = transformed.x;
  167. this.normals[index + 1] = transformed.y;
  168. this.normals[index + 2] = transformed.z;
  169. }
  170. }
  171. }
  172. public merge(other: VertexData): void {
  173. var index: number;
  174. if (other.indices) {
  175. if (!this.indices) {
  176. this.indices = [];
  177. }
  178. var offset = this.positions ? this.positions.length / 3 : 0;
  179. for (index = 0; index < other.indices.length; index++) {
  180. this.indices.push(other.indices[index] + offset);
  181. }
  182. }
  183. if (other.positions) {
  184. if (!this.positions) {
  185. this.positions = [];
  186. }
  187. for (index = 0; index < other.positions.length; index++) {
  188. (<number[]>this.positions).push(other.positions[index]);
  189. }
  190. }
  191. if (other.normals) {
  192. if (!this.normals) {
  193. this.normals = [];
  194. }
  195. for (index = 0; index < other.normals.length; index++) {
  196. (<number[]>this.normals).push(other.normals[index]);
  197. }
  198. }
  199. if (other.uvs) {
  200. if (!this.uvs) {
  201. this.uvs = [];
  202. }
  203. for (index = 0; index < other.uvs.length; index++) {
  204. (<number[]>this.uvs).push(other.uvs[index]);
  205. }
  206. }
  207. if (other.uvs2) {
  208. if (!this.uvs2) {
  209. this.uvs2 = [];
  210. }
  211. for (index = 0; index < other.uvs2.length; index++) {
  212. (<number[]>this.uvs2).push(other.uvs2[index]);
  213. }
  214. }
  215. if (other.uvs3) {
  216. if (!this.uvs3) {
  217. this.uvs3 = [];
  218. }
  219. for (index = 0; index < other.uvs3.length; index++) {
  220. (<number[]>this.uvs3).push(other.uvs3[index]);
  221. }
  222. }
  223. if (other.uvs4) {
  224. if (!this.uvs4) {
  225. this.uvs4 = [];
  226. }
  227. for (index = 0; index < other.uvs4.length; index++) {
  228. (<number[]>this.uvs4).push(other.uvs4[index]);
  229. }
  230. }
  231. if (other.uvs5) {
  232. if (!this.uvs5) {
  233. this.uvs5 = [];
  234. }
  235. for (index = 0; index < other.uvs5.length; index++) {
  236. (<number[]>this.uvs5).push(other.uvs5[index]);
  237. }
  238. }
  239. if (other.uvs6) {
  240. if (!this.uvs6) {
  241. this.uvs6 = [];
  242. }
  243. for (index = 0; index < other.uvs6.length; index++) {
  244. (<number[]>this.uvs6).push(other.uvs6[index]);
  245. }
  246. }
  247. if (other.matricesIndices) {
  248. if (!this.matricesIndices) {
  249. this.matricesIndices = [];
  250. }
  251. for (index = 0; index < other.matricesIndices.length; index++) {
  252. (<number[]>this.matricesIndices).push(other.matricesIndices[index]);
  253. }
  254. }
  255. if (other.matricesWeights) {
  256. if (!this.matricesWeights) {
  257. this.matricesWeights = [];
  258. }
  259. for (index = 0; index < other.matricesWeights.length; index++) {
  260. (<number[]>this.matricesWeights).push(other.matricesWeights[index]);
  261. }
  262. }
  263. if (other.colors) {
  264. if (!this.colors) {
  265. this.colors = [];
  266. }
  267. for (index = 0; index < other.colors.length; index++) {
  268. (<number[]>this.colors).push(other.colors[index]);
  269. }
  270. }
  271. }
  272. // Statics
  273. public static ExtractFromMesh(mesh: Mesh, copyWhenShared?: boolean): VertexData {
  274. return VertexData._ExtractFrom(mesh, copyWhenShared);
  275. }
  276. public static ExtractFromGeometry(geometry: Geometry, copyWhenShared?: boolean): VertexData {
  277. return VertexData._ExtractFrom(geometry, copyWhenShared);
  278. }
  279. private static _ExtractFrom(meshOrGeometry: IGetSetVerticesData, copyWhenShared?: boolean): VertexData {
  280. var result = new VertexData();
  281. if (meshOrGeometry.isVerticesDataPresent(VertexBuffer.PositionKind)) {
  282. result.positions = meshOrGeometry.getVerticesData(VertexBuffer.PositionKind, copyWhenShared);
  283. }
  284. if (meshOrGeometry.isVerticesDataPresent(VertexBuffer.NormalKind)) {
  285. result.normals = meshOrGeometry.getVerticesData(VertexBuffer.NormalKind, copyWhenShared);
  286. }
  287. if (meshOrGeometry.isVerticesDataPresent(VertexBuffer.UVKind)) {
  288. result.uvs = meshOrGeometry.getVerticesData(VertexBuffer.UVKind, copyWhenShared);
  289. }
  290. if (meshOrGeometry.isVerticesDataPresent(VertexBuffer.UV2Kind)) {
  291. result.uvs2 = meshOrGeometry.getVerticesData(VertexBuffer.UV2Kind, copyWhenShared);
  292. }
  293. if (meshOrGeometry.isVerticesDataPresent(VertexBuffer.UV3Kind)) {
  294. result.uvs3 = meshOrGeometry.getVerticesData(VertexBuffer.UV3Kind, copyWhenShared);
  295. }
  296. if (meshOrGeometry.isVerticesDataPresent(VertexBuffer.UV4Kind)) {
  297. result.uvs4 = meshOrGeometry.getVerticesData(VertexBuffer.UV4Kind, copyWhenShared);
  298. }
  299. if (meshOrGeometry.isVerticesDataPresent(VertexBuffer.UV5Kind)) {
  300. result.uvs5 = meshOrGeometry.getVerticesData(VertexBuffer.UV5Kind, copyWhenShared);
  301. }
  302. if (meshOrGeometry.isVerticesDataPresent(VertexBuffer.UV6Kind)) {
  303. result.uvs6 = meshOrGeometry.getVerticesData(VertexBuffer.UV6Kind, copyWhenShared);
  304. }
  305. if (meshOrGeometry.isVerticesDataPresent(VertexBuffer.ColorKind)) {
  306. result.colors = meshOrGeometry.getVerticesData(VertexBuffer.ColorKind, copyWhenShared);
  307. }
  308. if (meshOrGeometry.isVerticesDataPresent(VertexBuffer.MatricesIndicesKind)) {
  309. result.matricesIndices = meshOrGeometry.getVerticesData(VertexBuffer.MatricesIndicesKind, copyWhenShared);
  310. }
  311. if (meshOrGeometry.isVerticesDataPresent(VertexBuffer.MatricesWeightsKind)) {
  312. result.matricesWeights = meshOrGeometry.getVerticesData(VertexBuffer.MatricesWeightsKind, copyWhenShared);
  313. }
  314. result.indices = meshOrGeometry.getIndices(copyWhenShared);
  315. return result;
  316. }
  317. public static CreateRibbon(options: { pathArray: Vector3[][], closeArray?: boolean, closePath?: boolean, offset?: number, sideOrientation?: number }): VertexData {
  318. var pathArray: Vector3[][] = options.pathArray;
  319. var closeArray: boolean = options.closeArray || false;
  320. var closePath: boolean = options.closePath || false;
  321. var defaultOffset: number = Math.floor(pathArray[0].length / 2);
  322. var offset: number = options.offset || defaultOffset;
  323. offset = offset > defaultOffset ? defaultOffset : Math.floor(offset); // offset max allowed : defaultOffset
  324. var sideOrientation: number = (options.sideOrientation === 0) ? 0 : options.sideOrientation || Mesh.DEFAULTSIDE;
  325. var positions: number[] = [];
  326. var indices: number[] = [];
  327. var normals: number[] = [];
  328. var uvs: number[] = [];
  329. var us: number[][] = []; // us[path_id] = [uDist1, uDist2, uDist3 ... ] distances between points on path path_id
  330. var vs: number[][] = []; // vs[i] = [vDist1, vDist2, vDist3, ... ] distances between points i of consecutives paths from pathArray
  331. var uTotalDistance: number[] = []; // uTotalDistance[p] : total distance of path p
  332. var vTotalDistance: number[] = []; // vTotalDistance[i] : total distance between points i of first and last path from pathArray
  333. var minlg: number; // minimal length among all paths from pathArray
  334. var lg: number[] = []; // array of path lengths : nb of vertex per path
  335. var idx: number[] = []; // array of path indexes : index of each path (first vertex) in the total vertex number
  336. var p: number; // path iterator
  337. var i: number; // point iterator
  338. var j: number; // point iterator
  339. // if single path in pathArray
  340. if (pathArray.length < 2) {
  341. var ar1: Vector3[] = [];
  342. var ar2: Vector3[] = [];
  343. for (i = 0; i < pathArray[0].length - offset; i++) {
  344. ar1.push(pathArray[0][i]);
  345. ar2.push(pathArray[0][i + offset]);
  346. }
  347. pathArray = [ar1, ar2];
  348. }
  349. // positions and horizontal distances (u)
  350. var idc: number = 0;
  351. var closePathCorr: number = (closePath) ? 1 : 0;
  352. var path: Vector3[];
  353. var l: number;
  354. minlg = pathArray[0].length;
  355. var vectlg: number;
  356. var dist: number;
  357. for (p = 0; p < pathArray.length; p++) {
  358. uTotalDistance[p] = 0;
  359. us[p] = [0];
  360. path = pathArray[p];
  361. l = path.length;
  362. minlg = (minlg < l) ? minlg : l;
  363. j = 0;
  364. while (j < l) {
  365. positions.push(path[j].x, path[j].y, path[j].z);
  366. if (j > 0) {
  367. vectlg = path[j].subtract(path[j - 1]).length();
  368. dist = vectlg + uTotalDistance[p];
  369. us[p].push(dist);
  370. uTotalDistance[p] = dist;
  371. }
  372. j++;
  373. }
  374. if (closePath) {
  375. j--;
  376. positions.push(path[0].x, path[0].y, path[0].z);
  377. vectlg = path[j].subtract(path[0]).length();
  378. dist = vectlg + uTotalDistance[p];
  379. us[p].push(dist);
  380. uTotalDistance[p] = dist;
  381. }
  382. lg[p] = l + closePathCorr;
  383. idx[p] = idc;
  384. idc += (l + closePathCorr);
  385. }
  386. // vertical distances (v)
  387. var path1: Vector3[];
  388. var path2: Vector3[];
  389. var vertex1: Vector3;
  390. var vertex2: Vector3;
  391. for (i = 0; i < minlg + closePathCorr; i++) {
  392. vTotalDistance[i] = 0;
  393. vs[i] = [0];
  394. for (p = 0; p < pathArray.length - 1; p++) {
  395. path1 = pathArray[p];
  396. path2 = pathArray[p + 1];
  397. if (i === minlg) { // closePath
  398. vertex1 = path1[0];
  399. vertex2 = path2[0];
  400. }
  401. else {
  402. vertex1 = path1[i];
  403. vertex2 = path2[i];
  404. }
  405. vectlg = vertex2.subtract(vertex1).length();
  406. dist = vectlg + vTotalDistance[i];
  407. vs[i].push(dist);
  408. vTotalDistance[i] = dist;
  409. }
  410. if (closeArray) {
  411. path1 = pathArray[p];
  412. path2 = pathArray[0];
  413. if (i === minlg) { // closePath
  414. vertex2 = path2[0];
  415. }
  416. vectlg = vertex2.subtract(vertex1).length();
  417. dist = vectlg + vTotalDistance[i];
  418. vTotalDistance[i] = dist;
  419. }
  420. }
  421. // uvs
  422. var u: number;
  423. var v: number;
  424. for (p = 0; p < pathArray.length; p++) {
  425. for (i = 0; i < minlg + closePathCorr; i++) {
  426. u = us[p][i] / uTotalDistance[p];
  427. v = vs[i][p] / vTotalDistance[i];
  428. uvs.push(u, v);
  429. }
  430. }
  431. // indices
  432. p = 0; // path index
  433. var pi: number = 0; // positions array index
  434. var l1: number = lg[p] - 1; // path1 length
  435. var l2: number = lg[p + 1] - 1; // path2 length
  436. var min: number = (l1 < l2) ? l1 : l2; // current path stop index
  437. var shft: number = idx[1] - idx[0]; // shift
  438. var path1nb: number = closeArray ? lg.length : lg.length - 1; // number of path1 to iterate on
  439. while (pi <= min && p < path1nb) { // stay under min and don't go over next to last path
  440. // draw two triangles between path1 (p1) and path2 (p2) : (p1.pi, p2.pi, p1.pi+1) and (p2.pi+1, p1.pi+1, p2.pi) clockwise
  441. indices.push(pi, pi + shft, pi + 1);
  442. indices.push(pi + shft + 1, pi + 1, pi + shft);
  443. pi += 1;
  444. if (pi === min) { // if end of one of two consecutive paths reached, go to next existing path
  445. p++;
  446. if (p === lg.length - 1) { // last path of pathArray reached <=> closeArray == true
  447. shft = idx[0] - idx[p];
  448. l1 = lg[p] - 1;
  449. l2 = lg[0] - 1;
  450. }
  451. else {
  452. shft = idx[p + 1] - idx[p];
  453. l1 = lg[p] - 1;
  454. l2 = lg[p + 1] - 1;
  455. }
  456. pi = idx[p];
  457. min = (l1 < l2) ? l1 + pi : l2 + pi;
  458. }
  459. }
  460. // normals
  461. VertexData.ComputeNormals(positions, indices, normals);
  462. if (closePath) {
  463. var indexFirst: number = 0;
  464. var indexLast: number = 0;
  465. for (p = 0; p < pathArray.length; p++) {
  466. indexFirst = idx[p] * 3;
  467. if (p + 1 < pathArray.length) {
  468. indexLast = (idx[p + 1] - 1) * 3;
  469. }
  470. else {
  471. indexLast = normals.length - 3;
  472. }
  473. normals[indexFirst] = (normals[indexFirst] + normals[indexLast]) * 0.5;
  474. normals[indexFirst + 1] = (normals[indexFirst + 1] + normals[indexLast + 1]) * 0.5;
  475. normals[indexFirst + 2] = (normals[indexFirst + 2] + normals[indexLast + 2]) * 0.5;
  476. normals[indexLast] = normals[indexFirst];
  477. normals[indexLast + 1] = normals[indexFirst + 1];
  478. normals[indexLast + 2] = normals[indexFirst + 2];
  479. }
  480. }
  481. // sides
  482. VertexData._ComputeSides(sideOrientation, positions, indices, normals, uvs);
  483. // Result
  484. var vertexData = new VertexData();
  485. vertexData.indices = indices;
  486. vertexData.positions = positions;
  487. vertexData.normals = normals;
  488. vertexData.uvs = uvs;
  489. if (closePath) {
  490. (<any>vertexData)._idx = idx;
  491. }
  492. return vertexData;
  493. }
  494. public static CreateBox(options: { size?: number, width?: number, height?: number, depth?: number, faceUV?: Vector4[], faceColors?: Color4[], sideOrientation?: number }): VertexData {
  495. var normalsSource = [
  496. new Vector3(0, 0, 1),
  497. new Vector3(0, 0, -1),
  498. new Vector3(1, 0, 0),
  499. new Vector3(-1, 0, 0),
  500. new Vector3(0, 1, 0),
  501. new Vector3(0, -1, 0)
  502. ];
  503. var indices = [];
  504. var positions = [];
  505. var normals = [];
  506. var uvs = [];
  507. var width = options.width || options.size || 1;
  508. var height = options.height || options.size || 1;
  509. var depth = options.depth || options.size || 1;
  510. var sideOrientation = (options.sideOrientation === 0) ? 0 : options.sideOrientation || Mesh.DEFAULTSIDE;
  511. var faceUV: Vector4[] = options.faceUV || new Array<Vector4>(6);
  512. var faceColors: Color4[] = options.faceColors;
  513. var colors = [];
  514. // default face colors and UV if undefined
  515. for (var f = 0; f < 6; f++) {
  516. if (faceUV[f] === undefined) {
  517. faceUV[f] = new Vector4(0, 0, 1, 1);
  518. }
  519. if (faceColors && faceColors[f] === undefined) {
  520. faceColors[f] = new Color4(1, 1, 1, 1);
  521. }
  522. }
  523. var scaleVector = new Vector3(width / 2, height / 2, depth / 2);
  524. // Create each face in turn.
  525. for (var index = 0; index < normalsSource.length; index++) {
  526. var normal = normalsSource[index];
  527. // Get two vectors perpendicular to the face normal and to each other.
  528. var side1 = new Vector3(normal.y, normal.z, normal.x);
  529. var side2 = Vector3.Cross(normal, side1);
  530. // Six indices (two triangles) per face.
  531. var verticesLength = positions.length / 3;
  532. indices.push(verticesLength);
  533. indices.push(verticesLength + 1);
  534. indices.push(verticesLength + 2);
  535. indices.push(verticesLength);
  536. indices.push(verticesLength + 2);
  537. indices.push(verticesLength + 3);
  538. // Four vertices per face.
  539. var vertex = normal.subtract(side1).subtract(side2).multiply(scaleVector);
  540. positions.push(vertex.x, vertex.y, vertex.z);
  541. normals.push(normal.x, normal.y, normal.z);
  542. uvs.push(faceUV[index].z, faceUV[index].w);
  543. if (faceColors) {
  544. colors.push(faceColors[index].r, faceColors[index].g, faceColors[index].b, faceColors[index].a);
  545. }
  546. vertex = normal.subtract(side1).add(side2).multiply(scaleVector);
  547. positions.push(vertex.x, vertex.y, vertex.z);
  548. normals.push(normal.x, normal.y, normal.z);
  549. uvs.push(faceUV[index].x, faceUV[index].w);
  550. if (faceColors) {
  551. colors.push(faceColors[index].r, faceColors[index].g, faceColors[index].b, faceColors[index].a);
  552. }
  553. vertex = normal.add(side1).add(side2).multiply(scaleVector);
  554. positions.push(vertex.x, vertex.y, vertex.z);
  555. normals.push(normal.x, normal.y, normal.z);
  556. uvs.push(faceUV[index].x, faceUV[index].y);
  557. if (faceColors) {
  558. colors.push(faceColors[index].r, faceColors[index].g, faceColors[index].b, faceColors[index].a);
  559. }
  560. vertex = normal.add(side1).subtract(side2).multiply(scaleVector);
  561. positions.push(vertex.x, vertex.y, vertex.z);
  562. normals.push(normal.x, normal.y, normal.z);
  563. uvs.push(faceUV[index].z, faceUV[index].y);
  564. if (faceColors) {
  565. colors.push(faceColors[index].r, faceColors[index].g, faceColors[index].b, faceColors[index].a);
  566. }
  567. }
  568. // sides
  569. VertexData._ComputeSides(sideOrientation, positions, indices, normals, uvs);
  570. // Result
  571. var vertexData = new VertexData();
  572. vertexData.indices = indices;
  573. vertexData.positions = positions;
  574. vertexData.normals = normals;
  575. vertexData.uvs = uvs;
  576. if (faceColors) {
  577. var totalColors = (sideOrientation === Mesh.DOUBLESIDE) ? colors.concat(colors) : colors;
  578. vertexData.colors = totalColors;
  579. }
  580. return vertexData;
  581. }
  582. public static CreateSphere(options: { segments?: number, diameter?: number, diameterX?: number, diameterY?: number, diameterZ?: number, arc?: number, slice?: number, sideOrientation?: number }): VertexData {
  583. var segments: number = options.segments || 32;
  584. var diameterX: number = options.diameterX || options.diameter || 1;
  585. var diameterY: number = options.diameterY || options.diameter || 1;
  586. var diameterZ: number = options.diameterZ || options.diameter || 1;
  587. var arc: number = (options.arc <= 0 || options.arc > 1) ? 1.0 : options.arc || 1.0;
  588. var slice: number = (options.slice <= 0) ? 1.0 : options.slice || 1.0;
  589. var sideOrientation = (options.sideOrientation === 0) ? 0 : options.sideOrientation || Mesh.DEFAULTSIDE;
  590. var radius = new Vector3(diameterX / 2, diameterY / 2, diameterZ / 2);
  591. var totalZRotationSteps = 2 + segments;
  592. var totalYRotationSteps = 2 * totalZRotationSteps;
  593. var indices = [];
  594. var positions = [];
  595. var normals = [];
  596. var uvs = [];
  597. for (var zRotationStep = 0; zRotationStep <= totalZRotationSteps; zRotationStep++) {
  598. var normalizedZ = zRotationStep / totalZRotationSteps;
  599. var angleZ = normalizedZ * Math.PI * slice;
  600. for (var yRotationStep = 0; yRotationStep <= totalYRotationSteps; yRotationStep++) {
  601. var normalizedY = yRotationStep / totalYRotationSteps;
  602. var angleY = normalizedY * Math.PI * 2 * arc;
  603. var rotationZ = Matrix.RotationZ(-angleZ);
  604. var rotationY = Matrix.RotationY(angleY);
  605. var afterRotZ = Vector3.TransformCoordinates(Vector3.Up(), rotationZ);
  606. var complete = Vector3.TransformCoordinates(afterRotZ, rotationY);
  607. var vertex = complete.multiply(radius);
  608. var normal = Vector3.Normalize(vertex);
  609. positions.push(vertex.x, vertex.y, vertex.z);
  610. normals.push(normal.x, normal.y, normal.z);
  611. uvs.push(normalizedY, normalizedZ);
  612. }
  613. if (zRotationStep > 0) {
  614. var verticesCount = positions.length / 3;
  615. for (var firstIndex = verticesCount - 2 * (totalYRotationSteps + 1); (firstIndex + totalYRotationSteps + 2) < verticesCount; firstIndex++) {
  616. indices.push((firstIndex));
  617. indices.push((firstIndex + 1));
  618. indices.push(firstIndex + totalYRotationSteps + 1);
  619. indices.push((firstIndex + totalYRotationSteps + 1));
  620. indices.push((firstIndex + 1));
  621. indices.push((firstIndex + totalYRotationSteps + 2));
  622. }
  623. }
  624. }
  625. // Sides
  626. VertexData._ComputeSides(sideOrientation, positions, indices, normals, uvs);
  627. // Result
  628. var vertexData = new VertexData();
  629. vertexData.indices = indices;
  630. vertexData.positions = positions;
  631. vertexData.normals = normals;
  632. vertexData.uvs = uvs;
  633. return vertexData;
  634. }
  635. // Cylinder and cone
  636. public static CreateCylinder(options: { height?: number, diameterTop?: number, diameterBottom?: number, diameter?: number, tessellation?: number, subdivisions?: number, arc?: number, faceColors?: Color4[], faceUV?: Vector4[], sideOrientation?: number }): VertexData {
  637. var height: number = options.height || 2;
  638. var diameterTop: number = (options.diameterTop === 0) ? 0 : options.diameterTop || options.diameter || 1;
  639. var diameterBottom: number = options.diameterBottom || options.diameter || 1;
  640. var tessellation: number = options.tessellation || 24;
  641. var subdivisions: number = options.subdivisions || 1;
  642. var arc: number = (options.arc <= 0 || options.arc > 1) ? 1.0 : options.arc || 1.0;
  643. var sideOrientation: number = (options.sideOrientation === 0) ? 0 : options.sideOrientation || Mesh.DEFAULTSIDE;
  644. var faceUV: Vector4[] = options.faceUV || new Array<Vector4>(3);
  645. var faceColors: Color4[] = options.faceColors;
  646. // default face colors and UV if undefined
  647. for (var f = 0; f < 3; f++) {
  648. if (faceColors && faceColors[f] === undefined) {
  649. faceColors[f] = new Color4(1, 1, 1, 1);
  650. }
  651. if (faceUV && faceUV[f] === undefined) {
  652. faceUV[f] = new Vector4(0, 0, 1, 1);
  653. }
  654. }
  655. var indices = [];
  656. var positions = [];
  657. var normals = [];
  658. var uvs = [];
  659. var colors = [];
  660. var angle_step = Math.PI * 2 * arc / tessellation;
  661. var angle: number;
  662. var h: number;
  663. var radius: number;
  664. var tan = (diameterBottom - diameterTop) / 2 / height;
  665. var ringVertex: Vector3 = Vector3.Zero();
  666. var ringNormal: Vector3 = Vector3.Zero();
  667. // positions, normals, uvs
  668. var i: number;
  669. var j: number;
  670. for (i = 0; i <= subdivisions; i++) {
  671. h = i / subdivisions;
  672. radius = (h * (diameterTop - diameterBottom) + diameterBottom) / 2;
  673. for (j = 0; j <= tessellation; j++) {
  674. angle = j * angle_step;
  675. ringVertex.x = Math.cos(-angle) * radius;
  676. ringVertex.y = -height / 2 + h * height;
  677. ringVertex.z = Math.sin(-angle) * radius;
  678. if (diameterTop === 0 && i === subdivisions) {
  679. // if no top cap, reuse former normals
  680. ringNormal.x = normals[normals.length - (tessellation + 1) * 3];
  681. ringNormal.y = normals[normals.length - (tessellation + 1) * 3 + 1];
  682. ringNormal.z = normals[normals.length - (tessellation + 1) * 3 + 2];
  683. }
  684. else {
  685. ringNormal.x = ringVertex.x;
  686. ringNormal.z = ringVertex.z;
  687. ringNormal.y = Math.sqrt(ringNormal.x * ringNormal.x + ringNormal.z * ringNormal.z) * tan;
  688. ringNormal.normalize();
  689. }
  690. positions.push(ringVertex.x, ringVertex.y, ringVertex.z);
  691. normals.push(ringNormal.x, ringNormal.y, ringNormal.z);
  692. uvs.push(faceUV[1].x + (faceUV[1].z - faceUV[1].x) * j / tessellation, faceUV[1].y + (faceUV[1].w - faceUV[1].y) * h);
  693. if (faceColors) {
  694. colors.push(faceColors[1].r, faceColors[1].g, faceColors[1].b, faceColors[1].a);
  695. }
  696. }
  697. }
  698. // indices
  699. for (i = 0; i < subdivisions; i++) {
  700. for (j = 0; j < tessellation; j++) {
  701. var i0 = i * (tessellation + 1) + j;
  702. var i1 = (i + 1) * (tessellation + 1) + j;
  703. var i2 = i * (tessellation + 1) + (j + 1);
  704. var i3 = (i + 1) * (tessellation + 1) + (j + 1);
  705. indices.push(i0, i1, i2);
  706. indices.push(i3, i2, i1);
  707. }
  708. }
  709. // Caps
  710. var createCylinderCap = isTop => {
  711. var radius = isTop ? diameterTop / 2 : diameterBottom / 2;
  712. if (radius === 0) {
  713. return;
  714. }
  715. // Cap positions, normals & uvs
  716. var angle;
  717. var circleVector;
  718. var i: number;
  719. var u: Vector4 = (isTop) ? faceUV[2] : faceUV[0];
  720. var c: Color4;
  721. if (faceColors) {
  722. c = (isTop) ? faceColors[2] : faceColors[0];
  723. }
  724. // cap center
  725. var vbase = positions.length / 3;
  726. var offset = isTop ? height / 2 : -height / 2;
  727. var center = new Vector3(0, offset, 0);
  728. positions.push(center.x, center.y, center.z);
  729. normals.push(0, isTop ? 1 : -1, 0);
  730. uvs.push(u.x + (u.z - u.x) * 0.5, u.y + (u.w - u.y) * 0.5);
  731. if (faceColors) {
  732. colors.push(c.r, c.g, c.b, c.a);
  733. }
  734. var textureScale = new Vector2(0.5, 0.5);
  735. for (i = 0; i <= tessellation; i++) {
  736. angle = Math.PI * 2 * i * arc / tessellation;
  737. var cos = Math.cos(-angle);
  738. var sin = Math.sin(-angle);
  739. circleVector = new Vector3(cos * radius, offset, sin * radius);
  740. var textureCoordinate = new Vector2(cos * textureScale.x + 0.5, sin * textureScale.y + 0.5);
  741. positions.push(circleVector.x, circleVector.y, circleVector.z);
  742. normals.push(0, isTop ? 1 : -1, 0);
  743. uvs.push(u.x + (u.z - u.x) * textureCoordinate.x, u.y + (u.w - u.y) * textureCoordinate.y);
  744. if (faceColors) {
  745. colors.push(c.r, c.g, c.b, c.a);
  746. }
  747. }
  748. // Cap indices
  749. for (i = 0; i < tessellation; i++) {
  750. if (!isTop) {
  751. indices.push(vbase);
  752. indices.push(vbase + (i + 1));
  753. indices.push(vbase + (i + 2));
  754. }
  755. else {
  756. indices.push(vbase);
  757. indices.push(vbase + (i + 2));
  758. indices.push(vbase + (i + 1));
  759. }
  760. }
  761. };
  762. // add caps to geometry
  763. createCylinderCap(false);
  764. createCylinderCap(true);
  765. // Sides
  766. VertexData._ComputeSides(sideOrientation, positions, indices, normals, uvs);
  767. var vertexData = new VertexData();
  768. vertexData.indices = indices;
  769. vertexData.positions = positions;
  770. vertexData.normals = normals;
  771. vertexData.uvs = uvs;
  772. if (faceColors) {
  773. vertexData.colors = colors;
  774. }
  775. return vertexData;
  776. }
  777. public static CreateTorus(options: { diameter?: number, thickness?: number, tessellation?: number, sideOrientation?: number }) {
  778. var indices = [];
  779. var positions = [];
  780. var normals = [];
  781. var uvs = [];
  782. var diameter = options.diameter || 1;
  783. var thickness = options.thickness || 0.5;
  784. var tessellation = options.tessellation || 16;
  785. var sideOrientation = (options.sideOrientation === 0) ? 0 : options.sideOrientation || Mesh.DEFAULTSIDE;
  786. var stride = tessellation + 1;
  787. for (var i = 0; i <= tessellation; i++) {
  788. var u = i / tessellation;
  789. var outerAngle = i * Math.PI * 2.0 / tessellation - Math.PI / 2.0;
  790. var transform = Matrix.Translation(diameter / 2.0, 0, 0).multiply(Matrix.RotationY(outerAngle));
  791. for (var j = 0; j <= tessellation; j++) {
  792. var v = 1 - j / tessellation;
  793. var innerAngle = j * Math.PI * 2.0 / tessellation + Math.PI;
  794. var dx = Math.cos(innerAngle);
  795. var dy = Math.sin(innerAngle);
  796. // Create a vertex.
  797. var normal = new Vector3(dx, dy, 0);
  798. var position = normal.scale(thickness / 2);
  799. var textureCoordinate = new Vector2(u, v);
  800. position = Vector3.TransformCoordinates(position, transform);
  801. normal = Vector3.TransformNormal(normal, transform);
  802. positions.push(position.x, position.y, position.z);
  803. normals.push(normal.x, normal.y, normal.z);
  804. uvs.push(textureCoordinate.x, textureCoordinate.y);
  805. // And create indices for two triangles.
  806. var nextI = (i + 1) % stride;
  807. var nextJ = (j + 1) % stride;
  808. indices.push(i * stride + j);
  809. indices.push(i * stride + nextJ);
  810. indices.push(nextI * stride + j);
  811. indices.push(i * stride + nextJ);
  812. indices.push(nextI * stride + nextJ);
  813. indices.push(nextI * stride + j);
  814. }
  815. }
  816. // Sides
  817. VertexData._ComputeSides(sideOrientation, positions, indices, normals, uvs);
  818. // Result
  819. var vertexData = new VertexData();
  820. vertexData.indices = indices;
  821. vertexData.positions = positions;
  822. vertexData.normals = normals;
  823. vertexData.uvs = uvs;
  824. return vertexData;
  825. }
  826. public static CreateLines(options: { points: Vector3[] }): VertexData {
  827. var indices = [];
  828. var positions = [];
  829. var points = options.points;
  830. for (var index = 0; index < points.length; index++) {
  831. positions.push(points[index].x, points[index].y, points[index].z);
  832. if (index > 0) {
  833. indices.push(index - 1);
  834. indices.push(index);
  835. }
  836. }
  837. // Result
  838. var vertexData = new VertexData();
  839. vertexData.indices = indices;
  840. vertexData.positions = positions;
  841. return vertexData;
  842. }
  843. public static CreateDashedLines(options: { points: Vector3[], dashSize?: number, gapSize?: number, dashNb?: number }): VertexData {
  844. var dashSize = options.dashSize || 3;
  845. var gapSize = options.gapSize || 1;
  846. var dashNb = options.dashNb || 200;
  847. var points = options.points;
  848. var positions = new Array<number>();
  849. var indices = new Array<number>();
  850. var curvect = Vector3.Zero();
  851. var lg = 0;
  852. var nb = 0;
  853. var shft = 0;
  854. var dashshft = 0;
  855. var curshft = 0;
  856. var idx = 0;
  857. var i = 0;
  858. for (i = 0; i < points.length - 1; i++) {
  859. points[i + 1].subtractToRef(points[i], curvect);
  860. lg += curvect.length();
  861. }
  862. shft = lg / dashNb;
  863. dashshft = dashSize * shft / (dashSize + gapSize);
  864. for (i = 0; i < points.length - 1; i++) {
  865. points[i + 1].subtractToRef(points[i], curvect);
  866. nb = Math.floor(curvect.length() / shft);
  867. curvect.normalize();
  868. for (var j = 0; j < nb; j++) {
  869. curshft = shft * j;
  870. positions.push(points[i].x + curshft * curvect.x, points[i].y + curshft * curvect.y, points[i].z + curshft * curvect.z);
  871. positions.push(points[i].x + (curshft + dashshft) * curvect.x, points[i].y + (curshft + dashshft) * curvect.y, points[i].z + (curshft + dashshft) * curvect.z);
  872. indices.push(idx, idx + 1);
  873. idx += 2;
  874. }
  875. }
  876. // Result
  877. var vertexData = new VertexData();
  878. vertexData.positions = positions;
  879. vertexData.indices = indices;
  880. return vertexData;
  881. }
  882. public static CreateGround(options: { width?: number, height?: number, subdivisions?: number }): VertexData {
  883. var indices = [];
  884. var positions = [];
  885. var normals = [];
  886. var uvs = [];
  887. var row: number, col: number;
  888. var width: number = options.width || 1;
  889. var height: number = options.height || 1;
  890. var subdivisions: number = options.subdivisions || 1;
  891. for (row = 0; row <= subdivisions; row++) {
  892. for (col = 0; col <= subdivisions; col++) {
  893. var position = new Vector3((col * width) / subdivisions - (width / 2.0), 0, ((subdivisions - row) * height) / subdivisions - (height / 2.0));
  894. var normal = new Vector3(0, 1.0, 0);
  895. positions.push(position.x, position.y, position.z);
  896. normals.push(normal.x, normal.y, normal.z);
  897. uvs.push(col / subdivisions, 1.0 - row / subdivisions);
  898. }
  899. }
  900. for (row = 0; row < subdivisions; row++) {
  901. for (col = 0; col < subdivisions; col++) {
  902. indices.push(col + 1 + (row + 1) * (subdivisions + 1));
  903. indices.push(col + 1 + row * (subdivisions + 1));
  904. indices.push(col + row * (subdivisions + 1));
  905. indices.push(col + (row + 1) * (subdivisions + 1));
  906. indices.push(col + 1 + (row + 1) * (subdivisions + 1));
  907. indices.push(col + row * (subdivisions + 1));
  908. }
  909. }
  910. // Result
  911. var vertexData = new VertexData();
  912. vertexData.indices = indices;
  913. vertexData.positions = positions;
  914. vertexData.normals = normals;
  915. vertexData.uvs = uvs;
  916. return vertexData;
  917. }
  918. public static CreateTiledGround(options: { xmin: number, zmin: number, xmax: number, zmax: number, subdivisions?: { w: number; h: number; }, precision?: { w: number; h: number; } }): VertexData {
  919. var xmin = options.xmin;
  920. var zmin = options.zmin;
  921. var xmax = options.xmax;
  922. var zmax = options.zmax;
  923. var subdivisions = options.subdivisions || { w: 1, h: 1 };
  924. var precision = options.precision || { w: 1, h: 1 };
  925. var indices = [];
  926. var positions = [];
  927. var normals = [];
  928. var uvs = [];
  929. var row: number, col: number, tileRow: number, tileCol: number;
  930. subdivisions.h = (subdivisions.w < 1) ? 1 : subdivisions.h;
  931. subdivisions.w = (subdivisions.w < 1) ? 1 : subdivisions.w;
  932. precision.w = (precision.w < 1) ? 1 : precision.w;
  933. precision.h = (precision.h < 1) ? 1 : precision.h;
  934. var tileSize = {
  935. 'w': (xmax - xmin) / subdivisions.w,
  936. 'h': (zmax - zmin) / subdivisions.h
  937. };
  938. function applyTile(xTileMin: number, zTileMin: number, xTileMax: number, zTileMax: number) {
  939. // Indices
  940. var base = positions.length / 3;
  941. var rowLength = precision.w + 1;
  942. for (row = 0; row < precision.h; row++) {
  943. for (col = 0; col < precision.w; col++) {
  944. var square = [
  945. base + col + row * rowLength,
  946. base + (col + 1) + row * rowLength,
  947. base + (col + 1) + (row + 1) * rowLength,
  948. base + col + (row + 1) * rowLength
  949. ];
  950. indices.push(square[1]);
  951. indices.push(square[2]);
  952. indices.push(square[3]);
  953. indices.push(square[0]);
  954. indices.push(square[1]);
  955. indices.push(square[3]);
  956. }
  957. }
  958. // Position, normals and uvs
  959. var position = Vector3.Zero();
  960. var normal = new Vector3(0, 1.0, 0);
  961. for (row = 0; row <= precision.h; row++) {
  962. position.z = (row * (zTileMax - zTileMin)) / precision.h + zTileMin;
  963. for (col = 0; col <= precision.w; col++) {
  964. position.x = (col * (xTileMax - xTileMin)) / precision.w + xTileMin;
  965. position.y = 0;
  966. positions.push(position.x, position.y, position.z);
  967. normals.push(normal.x, normal.y, normal.z);
  968. uvs.push(col / precision.w, row / precision.h);
  969. }
  970. }
  971. }
  972. for (tileRow = 0; tileRow < subdivisions.h; tileRow++) {
  973. for (tileCol = 0; tileCol < subdivisions.w; tileCol++) {
  974. applyTile(
  975. xmin + tileCol * tileSize.w,
  976. zmin + tileRow * tileSize.h,
  977. xmin + (tileCol + 1) * tileSize.w,
  978. zmin + (tileRow + 1) * tileSize.h
  979. );
  980. }
  981. }
  982. // Result
  983. var vertexData = new VertexData();
  984. vertexData.indices = indices;
  985. vertexData.positions = positions;
  986. vertexData.normals = normals;
  987. vertexData.uvs = uvs;
  988. return vertexData;
  989. }
  990. public static CreateGroundFromHeightMap(options: { width: number, height: number, subdivisions: number, minHeight: number, maxHeight: number, buffer: Uint8Array, bufferWidth: number, bufferHeight: number }): VertexData {
  991. var indices = [];
  992. var positions = [];
  993. var normals = [];
  994. var uvs = [];
  995. var row, col;
  996. // Vertices
  997. for (row = 0; row <= options.subdivisions; row++) {
  998. for (col = 0; col <= options.subdivisions; col++) {
  999. var position = new Vector3((col * options.width) / options.subdivisions - (options.width / 2.0), 0, ((options.subdivisions - row) * options.height) / options.subdivisions - (options.height / 2.0));
  1000. // Compute height
  1001. var heightMapX = (((position.x + options.width / 2) / options.width) * (options.bufferWidth - 1)) | 0;
  1002. var heightMapY = ((1.0 - (position.z + options.height / 2) / options.height) * (options.bufferHeight - 1)) | 0;
  1003. var pos = (heightMapX + heightMapY * options.bufferWidth) * 4;
  1004. var r = options.buffer[pos] / 255.0;
  1005. var g = options.buffer[pos + 1] / 255.0;
  1006. var b = options.buffer[pos + 2] / 255.0;
  1007. var gradient = r * 0.3 + g * 0.59 + b * 0.11;
  1008. position.y = options.minHeight + (options.maxHeight - options.minHeight) * gradient;
  1009. // Add vertex
  1010. positions.push(position.x, position.y, position.z);
  1011. normals.push(0, 0, 0);
  1012. uvs.push(col / options.subdivisions, 1.0 - row / options.subdivisions);
  1013. }
  1014. }
  1015. // Indices
  1016. for (row = 0; row < options.subdivisions; row++) {
  1017. for (col = 0; col < options.subdivisions; col++) {
  1018. indices.push(col + 1 + (row + 1) * (options.subdivisions + 1));
  1019. indices.push(col + 1 + row * (options.subdivisions + 1));
  1020. indices.push(col + row * (options.subdivisions + 1));
  1021. indices.push(col + (row + 1) * (options.subdivisions + 1));
  1022. indices.push(col + 1 + (row + 1) * (options.subdivisions + 1));
  1023. indices.push(col + row * (options.subdivisions + 1));
  1024. }
  1025. }
  1026. // Normals
  1027. VertexData.ComputeNormals(positions, indices, normals);
  1028. // Result
  1029. var vertexData = new VertexData();
  1030. vertexData.indices = indices;
  1031. vertexData.positions = positions;
  1032. vertexData.normals = normals;
  1033. vertexData.uvs = uvs;
  1034. return vertexData;
  1035. }
  1036. public static CreatePlane(options: { size?: number, width?: number, height?: number, sideOrientation?: number }): VertexData {
  1037. var indices = [];
  1038. var positions = [];
  1039. var normals = [];
  1040. var uvs = [];
  1041. var width: number = options.width || options.size || 1;
  1042. var height: number = options.height || options.size || 1;
  1043. var sideOrientation = (options.sideOrientation === 0) ? 0 : options.sideOrientation || Mesh.DEFAULTSIDE;
  1044. // Vertices
  1045. var halfWidth = width / 2.0;
  1046. var halfHeight = height / 2.0;
  1047. positions.push(-halfWidth, -halfHeight, 0);
  1048. normals.push(0, 0, -1.0);
  1049. uvs.push(0.0, 0.0);
  1050. positions.push(halfWidth, -halfHeight, 0);
  1051. normals.push(0, 0, -1.0);
  1052. uvs.push(1.0, 0.0);
  1053. positions.push(halfWidth, halfHeight, 0);
  1054. normals.push(0, 0, -1.0);
  1055. uvs.push(1.0, 1.0);
  1056. positions.push(-halfWidth, halfHeight, 0);
  1057. normals.push(0, 0, -1.0);
  1058. uvs.push(0.0, 1.0);
  1059. // Indices
  1060. indices.push(0);
  1061. indices.push(1);
  1062. indices.push(2);
  1063. indices.push(0);
  1064. indices.push(2);
  1065. indices.push(3);
  1066. // Sides
  1067. VertexData._ComputeSides(sideOrientation, positions, indices, normals, uvs);
  1068. // Result
  1069. var vertexData = new VertexData();
  1070. vertexData.indices = indices;
  1071. vertexData.positions = positions;
  1072. vertexData.normals = normals;
  1073. vertexData.uvs = uvs;
  1074. return vertexData;
  1075. }
  1076. public static CreateDisc(options: { radius?: number, tessellation?: number, arc?: number, sideOrientation?: number }): VertexData {
  1077. var positions = [];
  1078. var indices = [];
  1079. var normals = [];
  1080. var uvs = [];
  1081. var radius = options.radius || 0.5;
  1082. var tessellation = options.tessellation || 64;
  1083. var arc: number = (options.arc <= 0 || options.arc > 1) ? 1.0 : options.arc || 1.0;
  1084. var sideOrientation = (options.sideOrientation === 0) ? 0 : options.sideOrientation || Mesh.DEFAULTSIDE;
  1085. // positions and uvs
  1086. positions.push(0, 0, 0); // disc center first
  1087. uvs.push(0.5, 0.5);
  1088. var theta = Math.PI * 2 * arc;
  1089. var step = theta / tessellation;
  1090. for (var a = 0; a < theta; a += step) {
  1091. var x = Math.cos(a);
  1092. var y = Math.sin(a);
  1093. var u = (x + 1) / 2;
  1094. var v = (1 - y) / 2;
  1095. positions.push(radius * x, radius * y, 0);
  1096. uvs.push(u, v);
  1097. }
  1098. if (arc === 1) {
  1099. positions.push(positions[3], positions[4], positions[5]); // close the circle
  1100. uvs.push(uvs[2], uvs[3]);
  1101. }
  1102. //indices
  1103. var vertexNb = positions.length / 3;
  1104. for (var i = 1; i < vertexNb - 1; i++) {
  1105. indices.push(i + 1, 0, i);
  1106. }
  1107. // result
  1108. VertexData.ComputeNormals(positions, indices, normals);
  1109. VertexData._ComputeSides(sideOrientation, positions, indices, normals, uvs);
  1110. var vertexData = new VertexData();
  1111. vertexData.indices = indices;
  1112. vertexData.positions = positions;
  1113. vertexData.normals = normals;
  1114. vertexData.uvs = uvs;
  1115. return vertexData;
  1116. }
  1117. // inspired from // http://stemkoski.github.io/Three.js/Polyhedra.html
  1118. public static CreatePolyhedron(options: { type?: number, size?: number, sizeX?: number, sizeY?: number, sizeZ?: number, custom?: any, faceUV?: Vector4[], faceColors?: Color4[], singleFace?: boolean, sideOrientation?: number }): VertexData {
  1119. // provided polyhedron types :
  1120. // 0 : Tetrahedron, 1 : Octahedron, 2 : Dodecahedron, 3 : Icosahedron, 4 : Rhombicuboctahedron, 5 : Triangular Prism, 6 : Pentagonal Prism, 7 : Hexagonal Prism, 8 : Square Pyramid (J1)
  1121. // 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)
  1122. var polyhedra: { vertex: number[][], face: number[][] }[] = [];
  1123. polyhedra[0] = { vertex: [[0, 0, 1.732051], [1.632993, 0, -0.5773503], [-0.8164966, 1.414214, -0.5773503], [-0.8164966, -1.414214, -0.5773503]], face: [[0, 1, 2], [0, 2, 3], [0, 3, 1], [1, 3, 2]] };
  1124. polyhedra[1] = { vertex: [[0, 0, 1.414214], [1.414214, 0, 0], [0, 1.414214, 0], [-1.414214, 0, 0], [0, -1.414214, 0], [0, 0, -1.414214]], face: [[0, 1, 2], [0, 2, 3], [0, 3, 4], [0, 4, 1], [1, 4, 5], [1, 5, 2], [2, 5, 3], [3, 5, 4]] };
  1125. polyhedra[2] = {
  1126. vertex: [[0, 0, 1.070466], [0.7136442, 0, 0.7978784], [-0.3568221, 0.618034, 0.7978784], [-0.3568221, -0.618034, 0.7978784], [0.7978784, 0.618034, 0.3568221], [0.7978784, -0.618034, 0.3568221], [-0.9341724, 0.381966, 0.3568221], [0.1362939, 1, 0.3568221], [0.1362939, -1, 0.3568221], [-0.9341724, -0.381966, 0.3568221], [0.9341724, 0.381966, -0.3568221], [0.9341724, -0.381966, -0.3568221], [-0.7978784, 0.618034, -0.3568221], [-0.1362939, 1, -0.3568221], [-0.1362939, -1, -0.3568221], [-0.7978784, -0.618034, -0.3568221], [0.3568221, 0.618034, -0.7978784], [0.3568221, -0.618034, -0.7978784], [-0.7136442, 0, -0.7978784], [0, 0, -1.070466]],
  1127. face: [[0, 1, 4, 7, 2], [0, 2, 6, 9, 3], [0, 3, 8, 5, 1], [1, 5, 11, 10, 4], [2, 7, 13, 12, 6], [3, 9, 15, 14, 8], [4, 10, 16, 13, 7], [5, 8, 14, 17, 11], [6, 12, 18, 15, 9], [10, 11, 17, 19, 16], [12, 13, 16, 19, 18], [14, 15, 18, 19, 17]]
  1128. };
  1129. polyhedra[3] = {
  1130. vertex: [[0, 0, 1.175571], [1.051462, 0, 0.5257311], [0.3249197, 1, 0.5257311], [-0.8506508, 0.618034, 0.5257311], [-0.8506508, -0.618034, 0.5257311], [0.3249197, -1, 0.5257311], [0.8506508, 0.618034, -0.5257311], [0.8506508, -0.618034, -0.5257311], [-0.3249197, 1, -0.5257311], [-1.051462, 0, -0.5257311], [-0.3249197, -1, -0.5257311], [0, 0, -1.175571]],
  1131. face: [[0, 1, 2], [0, 2, 3], [0, 3, 4], [0, 4, 5], [0, 5, 1], [1, 5, 7], [1, 7, 6], [1, 6, 2], [2, 6, 8], [2, 8, 3], [3, 8, 9], [3, 9, 4], [4, 9, 10], [4, 10, 5], [5, 10, 7], [6, 7, 11], [6, 11, 8], [7, 10, 11], [8, 11, 9], [9, 11, 10]]
  1132. };
  1133. polyhedra[4] = {
  1134. vertex: [[0, 0, 1.070722], [0.7148135, 0, 0.7971752], [-0.104682, 0.7071068, 0.7971752], [-0.6841528, 0.2071068, 0.7971752], [-0.104682, -0.7071068, 0.7971752], [0.6101315, 0.7071068, 0.5236279], [1.04156, 0.2071068, 0.1367736], [0.6101315, -0.7071068, 0.5236279], [-0.3574067, 1, 0.1367736], [-0.7888348, -0.5, 0.5236279], [-0.9368776, 0.5, 0.1367736], [-0.3574067, -1, 0.1367736], [0.3574067, 1, -0.1367736], [0.9368776, -0.5, -0.1367736], [0.7888348, 0.5, -0.5236279], [0.3574067, -1, -0.1367736], [-0.6101315, 0.7071068, -0.5236279], [-1.04156, -0.2071068, -0.1367736], [-0.6101315, -0.7071068, -0.5236279], [0.104682, 0.7071068, -0.7971752], [0.6841528, -0.2071068, -0.7971752], [0.104682, -0.7071068, -0.7971752], [-0.7148135, 0, -0.7971752], [0, 0, -1.070722]],
  1135. face: [[0, 2, 3], [1, 6, 5], [4, 9, 11], [7, 15, 13], [8, 16, 10], [12, 14, 19], [17, 22, 18], [20, 21, 23], [0, 1, 5, 2], [0, 3, 9, 4], [0, 4, 7, 1], [1, 7, 13, 6], [2, 5, 12, 8], [2, 8, 10, 3], [3, 10, 17, 9], [4, 11, 15, 7], [5, 6, 14, 12], [6, 13, 20, 14], [8, 12, 19, 16], [9, 17, 18, 11], [10, 16, 22, 17], [11, 18, 21, 15], [13, 15, 21, 20], [14, 20, 23, 19], [16, 19, 23, 22], [18, 22, 23, 21]]
  1136. };
  1137. polyhedra[5] = { vertex: [[0, 0, 1.322876], [1.309307, 0, 0.1889822], [-0.9819805, 0.8660254, 0.1889822], [0.1636634, -1.299038, 0.1889822], [0.3273268, 0.8660254, -0.9449112], [-0.8183171, -0.4330127, -0.9449112]], face: [[0, 3, 1], [2, 4, 5], [0, 1, 4, 2], [0, 2, 5, 3], [1, 3, 5, 4]] };
  1138. polyhedra[6] = { vertex: [[0, 0, 1.159953], [1.013464, 0, 0.5642542], [-0.3501431, 0.9510565, 0.5642542], [-0.7715208, -0.6571639, 0.5642542], [0.6633206, 0.9510565, -0.03144481], [0.8682979, -0.6571639, -0.3996071], [-1.121664, 0.2938926, -0.03144481], [-0.2348831, -1.063314, -0.3996071], [0.5181548, 0.2938926, -0.9953061], [-0.5850262, -0.112257, -0.9953061]], face: [[0, 1, 4, 2], [0, 2, 6, 3], [1, 5, 8, 4], [3, 6, 9, 7], [5, 7, 9, 8], [0, 3, 7, 5, 1], [2, 4, 8, 9, 6]] };
  1139. polyhedra[7] = { vertex: [[0, 0, 1.118034], [0.8944272, 0, 0.6708204], [-0.2236068, 0.8660254, 0.6708204], [-0.7826238, -0.4330127, 0.6708204], [0.6708204, 0.8660254, 0.2236068], [1.006231, -0.4330127, -0.2236068], [-1.006231, 0.4330127, 0.2236068], [-0.6708204, -0.8660254, -0.2236068], [0.7826238, 0.4330127, -0.6708204], [0.2236068, -0.8660254, -0.6708204], [-0.8944272, 0, -0.6708204], [0, 0, -1.118034]], face: [[0, 1, 4, 2], [0, 2, 6, 3], [1, 5, 8, 4], [3, 6, 10, 7], [5, 9, 11, 8], [7, 10, 11, 9], [0, 3, 7, 9, 5, 1], [2, 4, 8, 11, 10, 6]] };
  1140. polyhedra[8] = { vertex: [[-0.729665, 0.670121, 0.319155], [-0.655235, -0.29213, -0.754096], [-0.093922, -0.607123, 0.537818], [0.702196, 0.595691, 0.485187], [0.776626, -0.36656, -0.588064]], face: [[1, 4, 2], [0, 1, 2], [3, 0, 2], [4, 3, 2], [4, 1, 0, 3]] };
  1141. polyhedra[9] = { vertex: [[-0.868849, -0.100041, 0.61257], [-0.329458, 0.976099, 0.28078], [-0.26629, -0.013796, -0.477654], [-0.13392, -1.034115, 0.229829], [0.738834, 0.707117, -0.307018], [0.859683, -0.535264, -0.338508]], face: [[3, 0, 2], [5, 3, 2], [4, 5, 2], [1, 4, 2], [0, 1, 2], [0, 3, 5, 4, 1]] };
  1142. polyhedra[10] = { vertex: [[-0.610389, 0.243975, 0.531213], [-0.187812, -0.48795, -0.664016], [-0.187812, 0.9759, -0.664016], [0.187812, -0.9759, 0.664016], [0.798201, 0.243975, 0.132803]], face: [[1, 3, 0], [3, 4, 0], [3, 1, 4], [0, 2, 1], [0, 4, 2], [2, 4, 1]] };
  1143. polyhedra[11] = { vertex: [[-1.028778, 0.392027, -0.048786], [-0.640503, -0.646161, 0.621837], [-0.125162, -0.395663, -0.540059], [0.004683, 0.888447, -0.651988], [0.125161, 0.395663, 0.540059], [0.632925, -0.791376, 0.433102], [1.031672, 0.157063, -0.354165]], face: [[3, 2, 0], [2, 1, 0], [2, 5, 1], [0, 4, 3], [0, 1, 4], [4, 1, 5], [2, 3, 6], [3, 4, 6], [5, 2, 6], [4, 5, 6]] };
  1144. polyhedra[12] = { vertex: [[-0.669867, 0.334933, -0.529576], [-0.669867, 0.334933, 0.529577], [-0.4043, 1.212901, 0], [-0.334933, -0.669867, -0.529576], [-0.334933, -0.669867, 0.529577], [0.334933, 0.669867, -0.529576], [0.334933, 0.669867, 0.529577], [0.4043, -1.212901, 0], [0.669867, -0.334933, -0.529576], [0.669867, -0.334933, 0.529577]], face: [[8, 9, 7], [6, 5, 2], [3, 8, 7], [5, 0, 2], [4, 3, 7], [0, 1, 2], [9, 4, 7], [1, 6, 2], [9, 8, 5, 6], [8, 3, 0, 5], [3, 4, 1, 0], [4, 9, 6, 1]] };
  1145. polyhedra[13] = { vertex: [[-0.931836, 0.219976, -0.264632], [-0.636706, 0.318353, 0.692816], [-0.613483, -0.735083, -0.264632], [-0.326545, 0.979634, 0], [-0.318353, -0.636706, 0.692816], [-0.159176, 0.477529, -0.856368], [0.159176, -0.477529, -0.856368], [0.318353, 0.636706, 0.692816], [0.326545, -0.979634, 0], [0.613482, 0.735082, -0.264632], [0.636706, -0.318353, 0.692816], [0.931835, -0.219977, -0.264632]], face: [[11, 10, 8], [7, 9, 3], [6, 11, 8], [9, 5, 3], [2, 6, 8], [5, 0, 3], [4, 2, 8], [0, 1, 3], [10, 4, 8], [1, 7, 3], [10, 11, 9, 7], [11, 6, 5, 9], [6, 2, 0, 5], [2, 4, 1, 0], [4, 10, 7, 1]] };
  1146. polyhedra[14] = {
  1147. vertex: [[-0.93465, 0.300459, -0.271185], [-0.838689, -0.260219, -0.516017], [-0.711319, 0.717591, 0.128359], [-0.710334, -0.156922, 0.080946], [-0.599799, 0.556003, -0.725148], [-0.503838, -0.004675, -0.969981], [-0.487004, 0.26021, 0.48049], [-0.460089, -0.750282, -0.512622], [-0.376468, 0.973135, -0.325605], [-0.331735, -0.646985, 0.084342], [-0.254001, 0.831847, 0.530001], [-0.125239, -0.494738, -0.966586], [0.029622, 0.027949, 0.730817], [0.056536, -0.982543, -0.262295], [0.08085, 1.087391, 0.076037], [0.125583, -0.532729, 0.485984], [0.262625, 0.599586, 0.780328], [0.391387, -0.726999, -0.716259], [0.513854, -0.868287, 0.139347], [0.597475, 0.85513, 0.326364], [0.641224, 0.109523, 0.783723], [0.737185, -0.451155, 0.538891], [0.848705, -0.612742, -0.314616], [0.976075, 0.365067, 0.32976], [1.072036, -0.19561, 0.084927]],
  1148. face: [[15, 18, 21], [12, 20, 16], [6, 10, 2], [3, 0, 1], [9, 7, 13], [2, 8, 4, 0], [0, 4, 5, 1], [1, 5, 11, 7], [7, 11, 17, 13], [13, 17, 22, 18], [18, 22, 24, 21], [21, 24, 23, 20], [20, 23, 19, 16], [16, 19, 14, 10], [10, 14, 8, 2], [15, 9, 13, 18], [12, 15, 21, 20], [6, 12, 16, 10], [3, 6, 2, 0], [9, 3, 1, 7], [9, 15, 12, 6, 3], [22, 17, 11, 5, 4, 8, 14, 19, 23, 24]]
  1149. };
  1150. var type: number = (options.type < 0 || options.type >= polyhedra.length) ? 0 : options.type || 0;
  1151. var size: number = options.size;
  1152. var sizeX: number = options.sizeX || size || 1;
  1153. var sizeY: number = options.sizeY || size || 1;
  1154. var sizeZ: number = options.sizeZ || size || 1;
  1155. var data: { vertex: number[][], face: number[][], name?: string, category?: string } = options.custom || polyhedra[type];
  1156. var nbfaces = data.face.length;
  1157. var faceUV = options.faceUV || new Array(nbfaces);
  1158. var faceColors = options.faceColors;
  1159. var singleFace = options.singleFace;
  1160. var sideOrientation = (options.sideOrientation === 0) ? 0 : options.sideOrientation || Mesh.DEFAULTSIDE;
  1161. var positions = [];
  1162. var indices = [];
  1163. var normals = [];
  1164. var uvs = [];
  1165. var colors = [];
  1166. var index = 0;
  1167. var faceIdx = 0; // face cursor in the array "indexes"
  1168. var indexes = [];
  1169. var i = 0;
  1170. var f = 0;
  1171. var u: number, v: number, ang: number, x: number, y: number, tmp: number;
  1172. // default face colors and UV if undefined
  1173. if (!singleFace) {
  1174. for (f = 0; f < nbfaces; f++) {
  1175. if (faceColors && faceColors[f] === undefined) {
  1176. faceColors[f] = new Color4(1, 1, 1, 1);
  1177. }
  1178. if (faceUV && faceUV[f] === undefined) {
  1179. faceUV[f] = new Vector4(0, 0, 1, 1);
  1180. }
  1181. }
  1182. }
  1183. if (singleFace) {
  1184. for (i = 0; i < data.vertex.length; i++) {
  1185. positions.push(data.vertex[i][0] * sizeX, data.vertex[i][1] * sizeY, data.vertex[i][2] * sizeZ);
  1186. uvs.push(0, 0);
  1187. }
  1188. for (f = 0; f < nbfaces; f++) {
  1189. for (i = 0; i < data.face[f].length - 2; i++) {
  1190. indices.push(data.face[f][0], data.face[f][i + 2], data.face[f][i + 1]);
  1191. }
  1192. }
  1193. } else {
  1194. for (f = 0; f < nbfaces; f++) {
  1195. var fl = data.face[f].length; // number of vertices of the current face
  1196. ang = 2 * Math.PI / fl;
  1197. x = 0.5 * Math.tan(ang / 2);
  1198. y = 0.5;
  1199. // positions, uvs, colors
  1200. for (i = 0; i < fl; i++) {
  1201. // positions
  1202. 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);
  1203. indexes.push(index);
  1204. index++;
  1205. // uvs
  1206. u = faceUV[f].x + (faceUV[f].z - faceUV[f].x) * (0.5 + x);
  1207. v = faceUV[f].y + (faceUV[f].w - faceUV[f].y) * (y - 0.5);
  1208. uvs.push(u, v);
  1209. tmp = x * Math.cos(ang) - y * Math.sin(ang);
  1210. y = x * Math.sin(ang) + y * Math.cos(ang);
  1211. x = tmp;
  1212. // colors
  1213. if (faceColors) {
  1214. colors.push(faceColors[f].r, faceColors[f].g, faceColors[f].b, faceColors[f].a);
  1215. }
  1216. }
  1217. // indices from indexes
  1218. for (i = 0; i < fl - 2; i++) {
  1219. indices.push(indexes[0 + faceIdx], indexes[i + 2 + faceIdx], indexes[i + 1 + faceIdx]);
  1220. }
  1221. faceIdx += fl;
  1222. }
  1223. }
  1224. VertexData.ComputeNormals(positions, indices, normals);
  1225. VertexData._ComputeSides(sideOrientation, positions, indices, normals, uvs);
  1226. var vertexData = new VertexData();
  1227. vertexData.positions = positions;
  1228. vertexData.indices = indices;
  1229. vertexData.normals = normals;
  1230. vertexData.uvs = uvs;
  1231. if (faceColors && !singleFace) {
  1232. vertexData.colors = colors;
  1233. }
  1234. return vertexData;
  1235. }
  1236. // based on http://code.google.com/p/away3d/source/browse/trunk/fp10/Away3D/src/away3d/primitives/TorusKnot.as?spec=svn2473&r=2473
  1237. public static CreateTorusKnot(options: { radius?: number, tube?: number, radialSegments?: number, tubularSegments?: number, p?: number, q?: number, sideOrientation?: number }): VertexData {
  1238. var indices = [];
  1239. var positions = [];
  1240. var normals = [];
  1241. var uvs = [];
  1242. var radius = options.radius || 2;
  1243. var tube = options.tube || 0.5;
  1244. var radialSegments = options.radialSegments || 32;
  1245. var tubularSegments = options.tubularSegments || 32;
  1246. var p = options.p || 2;
  1247. var q = options.q || 3;
  1248. var sideOrientation = (options.sideOrientation === 0) ? 0 : options.sideOrientation || Mesh.DEFAULTSIDE;
  1249. // Helper
  1250. var getPos = (angle) => {
  1251. var cu = Math.cos(angle);
  1252. var su = Math.sin(angle);
  1253. var quOverP = q / p * angle;
  1254. var cs = Math.cos(quOverP);
  1255. var tx = radius * (2 + cs) * 0.5 * cu;
  1256. var ty = radius * (2 + cs) * su * 0.5;
  1257. var tz = radius * Math.sin(quOverP) * 0.5;
  1258. return new Vector3(tx, ty, tz);
  1259. };
  1260. // Vertices
  1261. var i: number;
  1262. var j: number;
  1263. for (i = 0; i <= radialSegments; i++) {
  1264. var modI = i % radialSegments;
  1265. var u = modI / radialSegments * 2 * p * Math.PI;
  1266. var p1 = getPos(u);
  1267. var p2 = getPos(u + 0.01);
  1268. var tang = p2.subtract(p1);
  1269. var n = p2.add(p1);
  1270. var bitan = Vector3.Cross(tang, n);
  1271. n = Vector3.Cross(bitan, tang);
  1272. bitan.normalize();
  1273. n.normalize();
  1274. for (j = 0; j < tubularSegments; j++) {
  1275. var modJ = j % tubularSegments;
  1276. var v = modJ / tubularSegments * 2 * Math.PI;
  1277. var cx = -tube * Math.cos(v);
  1278. var cy = tube * Math.sin(v);
  1279. positions.push(p1.x + cx * n.x + cy * bitan.x);
  1280. positions.push(p1.y + cx * n.y + cy * bitan.y);
  1281. positions.push(p1.z + cx * n.z + cy * bitan.z);
  1282. uvs.push(i / radialSegments);
  1283. uvs.push(j / tubularSegments);
  1284. }
  1285. }
  1286. for (i = 0; i < radialSegments; i++) {
  1287. for (j = 0; j < tubularSegments; j++) {
  1288. var jNext = (j + 1) % tubularSegments;
  1289. var a = i * tubularSegments + j;
  1290. var b = (i + 1) * tubularSegments + j;
  1291. var c = (i + 1) * tubularSegments + jNext;
  1292. var d = i * tubularSegments + jNext;
  1293. indices.push(d); indices.push(b); indices.push(a);
  1294. indices.push(d); indices.push(c); indices.push(b);
  1295. }
  1296. }
  1297. // Normals
  1298. VertexData.ComputeNormals(positions, indices, normals);
  1299. // Sides
  1300. VertexData._ComputeSides(sideOrientation, positions, indices, normals, uvs);
  1301. // Result
  1302. var vertexData = new VertexData();
  1303. vertexData.indices = indices;
  1304. vertexData.positions = positions;
  1305. vertexData.normals = normals;
  1306. vertexData.uvs = uvs;
  1307. return vertexData;
  1308. }
  1309. // Tools
  1310. /**
  1311. * @param {any} - positions (number[] or Float32Array)
  1312. * @param {any} - indices (number[] or Uint16Array)
  1313. * @param {any} - normals (number[] or Float32Array)
  1314. */
  1315. public static ComputeNormals(positions: any, indices: any, normals: any) {
  1316. var index = 0;
  1317. // temp Vector3
  1318. var p1p2 = Vector3.Zero();
  1319. var p3p2 = Vector3.Zero();
  1320. var faceNormal = Vector3.Zero();
  1321. var vertexNormali1 = Vector3.Zero();
  1322. for (index = 0; index < positions.length; index++) {
  1323. normals[index] = 0.0;
  1324. }
  1325. // indice triplet = 1 face
  1326. var nbFaces = indices.length / 3;
  1327. for (index = 0; index < nbFaces; index++) {
  1328. var i1 = indices[index * 3];
  1329. var i2 = indices[index * 3 + 1];
  1330. var i3 = indices[index * 3 + 2];
  1331. p1p2.x = positions[i1 * 3] - positions[i2 * 3];
  1332. p1p2.y = positions[i1 * 3 + 1] - positions[i2 * 3 + 1];
  1333. p1p2.z = positions[i1 * 3 + 2] - positions[i2 * 3 + 2];
  1334. p3p2.x = positions[i3 * 3] - positions[i2 * 3];
  1335. p3p2.y = positions[i3 * 3 + 1] - positions[i2 * 3 + 1];
  1336. p3p2.z = positions[i3 * 3 + 2] - positions[i2 * 3 + 2];
  1337. Vector3.CrossToRef(p1p2, p3p2, faceNormal);
  1338. faceNormal.normalize();
  1339. normals[i1 * 3] += faceNormal.x;
  1340. normals[i1 * 3 + 1] += faceNormal.y;
  1341. normals[i1 * 3 + 2] += faceNormal.z;
  1342. normals[i2 * 3] += faceNormal.x;
  1343. normals[i2 * 3 + 1] += faceNormal.y;
  1344. normals[i2 * 3 + 2] += faceNormal.z;
  1345. normals[i3 * 3] += faceNormal.x;
  1346. normals[i3 * 3 + 1] += faceNormal.y;
  1347. normals[i3 * 3 + 2] += faceNormal.z;
  1348. }
  1349. // last normalization
  1350. for (index = 0; index < normals.length / 3; index++) {
  1351. Vector3.FromFloatsToRef(normals[index * 3], normals[index * 3 + 1], normals[index * 3 + 2], vertexNormali1);
  1352. vertexNormali1.normalize();
  1353. normals[index * 3] = vertexNormali1.x;
  1354. normals[index * 3 + 1] = vertexNormali1.y;
  1355. normals[index * 3 + 2] = vertexNormali1.z;
  1356. }
  1357. }
  1358. private static _ComputeSides(sideOrientation: number, positions: number[] | Float32Array, indices: number[] | Float32Array, normals: number[] | Float32Array, uvs: number[] | Float32Array) {
  1359. var li: number = indices.length;
  1360. var ln: number = normals.length;
  1361. var i: number;
  1362. var n: number;
  1363. sideOrientation = sideOrientation || Mesh.DEFAULTSIDE;
  1364. switch (sideOrientation) {
  1365. case Mesh.FRONTSIDE:
  1366. // nothing changed
  1367. break;
  1368. case Mesh.BACKSIDE:
  1369. var tmp: number;
  1370. // indices
  1371. for (i = 0; i < li; i += 3) {
  1372. tmp = indices[i];
  1373. indices[i] = indices[i + 2];
  1374. indices[i + 2] = tmp;
  1375. }
  1376. // normals
  1377. for (n = 0; n < ln; n++) {
  1378. normals[n] = -normals[n];
  1379. }
  1380. break;
  1381. case Mesh.DOUBLESIDE:
  1382. // positions
  1383. var lp: number = positions.length;
  1384. var l: number = lp / 3;
  1385. for (var p = 0; p < lp; p++) {
  1386. positions[lp + p] = positions[p];
  1387. }
  1388. // indices
  1389. for (i = 0; i < li; i += 3) {
  1390. indices[i + li] = indices[i + 2] + l;
  1391. indices[i + 1 + li] = indices[i + 1] + l;
  1392. indices[i + 2 + li] = indices[i] + l;
  1393. }
  1394. // normals
  1395. for (n = 0; n < ln; n++) {
  1396. normals[ln + n] = -normals[n];
  1397. }
  1398. // uvs
  1399. var lu: number = uvs.length;
  1400. for (var u: number = 0; u < lu; u++) {
  1401. uvs[u + lu] = uvs[u];
  1402. }
  1403. break;
  1404. }
  1405. }
  1406. }
  1407. }