babylon.mesh.vertexData.ts 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540
  1. module BABYLON {
  2. export interface IGetSetVerticesData {
  3. isVerticesDataPresent(kind: string): boolean;
  4. getVerticesData(kind: string, copyWhenShared?: boolean): number[];
  5. getIndices(copyWhenShared?: boolean): number[];
  6. setVerticesData(kind: string, data: number[], updatable?: boolean): void;
  7. updateVerticesData(kind: string, data: number[], updateExtends?: boolean, makeItUnique?: boolean): void;
  8. setIndices(indices: number[]): void;
  9. }
  10. export class VertexData {
  11. public positions: number[];
  12. public normals: number[];
  13. public uvs: number[];
  14. public uvs2: number[];
  15. public uvs3: number[];
  16. public uvs4: number[];
  17. public uvs5: number[];
  18. public uvs6: number[];
  19. public colors: number[];
  20. public matricesIndices: number[];
  21. public matricesWeights: number[];
  22. public indices: number[];
  23. public set(data: number[], 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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) ? 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 = (options.arc <= 0) ? 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: any, precision: any }): 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, 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 sideOrientation = (options.sideOrientation === 0) ? 0 : options.sideOrientation || Mesh.DEFAULTSIDE;
  1084. // positions and uvs
  1085. positions.push(0, 0, 0); // disc center first
  1086. uvs.push(0.5, 0.5);
  1087. var step = Math.PI * 2 / tessellation;
  1088. for (var a = 0; a < Math.PI * 2; a += step) {
  1089. var x = Math.cos(a);
  1090. var y = Math.sin(a);
  1091. var u = (x + 1) / 2;
  1092. var v = (1 - y) / 2;
  1093. positions.push(radius * x, radius * y, 0);
  1094. uvs.push(u, v);
  1095. }
  1096. positions.push(positions[3], positions[4], positions[5]); // close the circle
  1097. uvs.push(uvs[2], uvs[3]);
  1098. //indices
  1099. var vertexNb = positions.length / 3;
  1100. for (var i = 1; i < vertexNb - 1; i++) {
  1101. indices.push(i + 1, 0, i);
  1102. }
  1103. // result
  1104. VertexData.ComputeNormals(positions, indices, normals);
  1105. VertexData._ComputeSides(sideOrientation, positions, indices, normals, uvs);
  1106. var vertexData = new VertexData();
  1107. vertexData.indices = indices;
  1108. vertexData.positions = positions;
  1109. vertexData.normals = normals;
  1110. vertexData.uvs = uvs;
  1111. return vertexData;
  1112. }
  1113. // based on http://code.google.com/p/away3d/source/browse/trunk/fp10/Away3D/src/away3d/primitives/TorusKnot.as?spec=svn2473&r=2473
  1114. public static CreateTorusKnot(options: { radius?: number, tube?: number, radialSegments?: number, tubularSegments?: number, p?: number, q?: number, sideOrientation?: number }): VertexData {
  1115. var indices = [];
  1116. var positions = [];
  1117. var normals = [];
  1118. var uvs = [];
  1119. var radius = options.radius || 2;
  1120. var tube = options.tube || 0.5;
  1121. var radialSegments = options.radialSegments || 32;
  1122. var tubularSegments = options.tubularSegments || 32;
  1123. var p = options.p || 2;
  1124. var q = options.q || 3;
  1125. var sideOrientation = (options.sideOrientation === 0) ? 0 : options.sideOrientation || Mesh.DEFAULTSIDE;
  1126. // Helper
  1127. var getPos = (angle) => {
  1128. var cu = Math.cos(angle);
  1129. var su = Math.sin(angle);
  1130. var quOverP = q / p * angle;
  1131. var cs = Math.cos(quOverP);
  1132. var tx = radius * (2 + cs) * 0.5 * cu;
  1133. var ty = radius * (2 + cs) * su * 0.5;
  1134. var tz = radius * Math.sin(quOverP) * 0.5;
  1135. return new Vector3(tx, ty, tz);
  1136. };
  1137. // Vertices
  1138. var i: number;
  1139. var j: number;
  1140. for (i = 0; i <= radialSegments; i++) {
  1141. var modI = i % radialSegments;
  1142. var u = modI / radialSegments * 2 * p * Math.PI;
  1143. var p1 = getPos(u);
  1144. var p2 = getPos(u + 0.01);
  1145. var tang = p2.subtract(p1);
  1146. var n = p2.add(p1);
  1147. var bitan = Vector3.Cross(tang, n);
  1148. n = Vector3.Cross(bitan, tang);
  1149. bitan.normalize();
  1150. n.normalize();
  1151. for (j = 0; j < tubularSegments; j++) {
  1152. var modJ = j % tubularSegments;
  1153. var v = modJ / tubularSegments * 2 * Math.PI;
  1154. var cx = -tube * Math.cos(v);
  1155. var cy = tube * Math.sin(v);
  1156. positions.push(p1.x + cx * n.x + cy * bitan.x);
  1157. positions.push(p1.y + cx * n.y + cy * bitan.y);
  1158. positions.push(p1.z + cx * n.z + cy * bitan.z);
  1159. uvs.push(i / radialSegments);
  1160. uvs.push(j / tubularSegments);
  1161. }
  1162. }
  1163. for (i = 0; i < radialSegments; i++) {
  1164. for (j = 0; j < tubularSegments; j++) {
  1165. var jNext = (j + 1) % tubularSegments;
  1166. var a = i * tubularSegments + j;
  1167. var b = (i + 1) * tubularSegments + j;
  1168. var c = (i + 1) * tubularSegments + jNext;
  1169. var d = i * tubularSegments + jNext;
  1170. indices.push(d); indices.push(b); indices.push(a);
  1171. indices.push(d); indices.push(c); indices.push(b);
  1172. }
  1173. }
  1174. // Normals
  1175. VertexData.ComputeNormals(positions, indices, normals);
  1176. // Sides
  1177. VertexData._ComputeSides(sideOrientation, positions, indices, normals, uvs);
  1178. // Result
  1179. var vertexData = new VertexData();
  1180. vertexData.indices = indices;
  1181. vertexData.positions = positions;
  1182. vertexData.normals = normals;
  1183. vertexData.uvs = uvs;
  1184. return vertexData;
  1185. }
  1186. // Tools
  1187. /**
  1188. * @param {any} - positions (number[] or Float32Array)
  1189. * @param {any} - indices (number[] or Uint16Array)
  1190. * @param {any} - normals (number[] or Float32Array)
  1191. */
  1192. public static ComputeNormals(positions: any, indices: any, normals: any) {
  1193. var index = 0;
  1194. // temp Vector3
  1195. var p1p2 = Vector3.Zero();
  1196. var p3p2 = Vector3.Zero();
  1197. var faceNormal = Vector3.Zero();
  1198. var vertexNormali1 = Vector3.Zero();
  1199. for (index = 0; index < positions.length; index++) {
  1200. normals[index] = 0.0;
  1201. }
  1202. // indice triplet = 1 face
  1203. var nbFaces = indices.length / 3;
  1204. for (index = 0; index < nbFaces; index++) {
  1205. var i1 = indices[index * 3];
  1206. var i2 = indices[index * 3 + 1];
  1207. var i3 = indices[index * 3 + 2];
  1208. p1p2.x = positions[i1 * 3] - positions[i2 * 3];
  1209. p1p2.y = positions[i1 * 3 + 1] - positions[i2 * 3 + 1];
  1210. p1p2.z = positions[i1 * 3 + 2] - positions[i2 * 3 + 2];
  1211. p3p2.x = positions[i3 * 3] - positions[i2 * 3];
  1212. p3p2.y = positions[i3 * 3 + 1] - positions[i2 * 3 + 1];
  1213. p3p2.z = positions[i3 * 3 + 2] - positions[i2 * 3 + 2];
  1214. Vector3.CrossToRef(p1p2, p3p2, faceNormal);
  1215. faceNormal.normalize();
  1216. normals[i1 * 3] += faceNormal.x;
  1217. normals[i1 * 3 + 1] += faceNormal.y;
  1218. normals[i1 * 3 + 2] += faceNormal.z;
  1219. normals[i2 * 3] += faceNormal.x;
  1220. normals[i2 * 3 + 1] += faceNormal.y;
  1221. normals[i2 * 3 + 2] += faceNormal.z;
  1222. normals[i3 * 3] += faceNormal.x;
  1223. normals[i3 * 3 + 1] += faceNormal.y;
  1224. normals[i3 * 3 + 2] += faceNormal.z;
  1225. }
  1226. // last normalization
  1227. for (index = 0; index < normals.length / 3; index++) {
  1228. Vector3.FromFloatsToRef(normals[index * 3], normals[index * 3 + 1], normals[index * 3 + 2], vertexNormali1);
  1229. vertexNormali1.normalize();
  1230. normals[index * 3] = vertexNormali1.x;
  1231. normals[index * 3 + 1] = vertexNormali1.y;
  1232. normals[index * 3 + 2] = vertexNormali1.z;
  1233. }
  1234. }
  1235. private static _ComputeSides(sideOrientation: number, positions: number[], indices: number[], normals: number[], uvs: number[]) {
  1236. var li: number = indices.length;
  1237. var ln: number = normals.length;
  1238. var i: number;
  1239. var n: number;
  1240. sideOrientation = sideOrientation || Mesh.DEFAULTSIDE;
  1241. switch (sideOrientation) {
  1242. case Mesh.FRONTSIDE:
  1243. // nothing changed
  1244. break;
  1245. case Mesh.BACKSIDE:
  1246. var tmp: number;
  1247. // indices
  1248. for (i = 0; i < li; i += 3) {
  1249. tmp = indices[i];
  1250. indices[i] = indices[i + 2];
  1251. indices[i + 2] = tmp;
  1252. }
  1253. // normals
  1254. for (n = 0; n < ln; n++) {
  1255. normals[n] = -normals[n];
  1256. }
  1257. break;
  1258. case Mesh.DOUBLESIDE:
  1259. // positions
  1260. var lp: number = positions.length;
  1261. var l: number = lp / 3;
  1262. for (var p = 0; p < lp; p++) {
  1263. positions[lp + p] = positions[p];
  1264. }
  1265. // indices
  1266. for (i = 0; i < li; i += 3) {
  1267. indices[i + li] = indices[i + 2] + l;
  1268. indices[i + 1 + li] = indices[i + 1] + l;
  1269. indices[i + 2 + li] = indices[i] + l;
  1270. }
  1271. // normals
  1272. for (n = 0; n < ln; n++) {
  1273. normals[ln + n] = -normals[n];
  1274. }
  1275. // uvs
  1276. var lu: number = uvs.length;
  1277. for (var u: number = 0; u < lu; u++) {
  1278. uvs[u + lu] = uvs[u];
  1279. }
  1280. break;
  1281. }
  1282. }
  1283. }
  1284. }