babylon.mesh.vertexData.ts 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528
  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. vectlg = path2[i].subtract(path1[i]).length();
  414. dist = vectlg + vTotalDistance[i];
  415. vTotalDistance[i] = dist;
  416. }
  417. }
  418. // uvs
  419. var u: number;
  420. var v: number;
  421. for (p = 0; p < pathArray.length; p++) {
  422. for (i = 0; i < minlg + closePathCorr; i++) {
  423. u = us[p][i] / uTotalDistance[p];
  424. v = vs[i][p] / vTotalDistance[i];
  425. uvs.push(u, v);
  426. }
  427. }
  428. // indices
  429. p = 0; // path index
  430. var pi: number = 0; // positions array index
  431. var l1: number = lg[p] - 1; // path1 length
  432. var l2: number = lg[p + 1] - 1; // path2 length
  433. var min: number = (l1 < l2) ? l1 : l2; // current path stop index
  434. var shft: number = idx[1] - idx[0]; // shift
  435. var path1nb: number = closeArray ? lg.length : lg.length - 1; // number of path1 to iterate on
  436. while (pi <= min && p < path1nb) { // stay under min and don't go over next to last path
  437. // 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
  438. indices.push(pi, pi + shft, pi + 1);
  439. indices.push(pi + shft + 1, pi + 1, pi + shft);
  440. pi += 1;
  441. if (pi === min) { // if end of one of two consecutive paths reached, go to next existing path
  442. p++;
  443. if (p === lg.length - 1) { // last path of pathArray reached <=> closeArray == true
  444. shft = idx[0] - idx[p];
  445. l1 = lg[p] - 1;
  446. l2 = lg[0] - 1;
  447. }
  448. else {
  449. shft = idx[p + 1] - idx[p];
  450. l1 = lg[p] - 1;
  451. l2 = lg[p + 1] - 1;
  452. }
  453. pi = idx[p];
  454. min = (l1 < l2) ? l1 + pi : l2 + pi;
  455. }
  456. }
  457. // normals
  458. VertexData.ComputeNormals(positions, indices, normals);
  459. if (closePath) {
  460. var indexFirst: number = 0;
  461. var indexLast: number = 0;
  462. for (p = 0; p < pathArray.length; p++) {
  463. indexFirst = idx[p] * 3;
  464. if (p + 1 < pathArray.length) {
  465. indexLast = (idx[p + 1] - 1) * 3;
  466. }
  467. else {
  468. indexLast = normals.length - 3;
  469. }
  470. normals[indexFirst] = (normals[indexFirst] + normals[indexLast]) * 0.5;
  471. normals[indexFirst + 1] = (normals[indexFirst + 1] + normals[indexLast + 1]) * 0.5;
  472. normals[indexFirst + 2] = (normals[indexFirst + 2] + normals[indexLast + 2]) * 0.5;
  473. normals[indexLast] = normals[indexFirst];
  474. normals[indexLast + 1] = normals[indexFirst + 1];
  475. normals[indexLast + 2] = normals[indexFirst + 2];
  476. }
  477. }
  478. // sides
  479. VertexData._ComputeSides(sideOrientation, positions, indices, normals, uvs);
  480. // Result
  481. var vertexData = new VertexData();
  482. vertexData.indices = indices;
  483. vertexData.positions = positions;
  484. vertexData.normals = normals;
  485. vertexData.uvs = uvs;
  486. if (closePath) {
  487. (<any>vertexData)._idx = idx;
  488. }
  489. return vertexData;
  490. }
  491. public static CreateBox(options: { size?: number, width?: number, height?: number, depth?: number, faceUV?: Vector4[], faceColors?: Color4[], sideOrientation?: number }): VertexData {
  492. var normalsSource = [
  493. new Vector3(0, 0, 1),
  494. new Vector3(0, 0, -1),
  495. new Vector3(1, 0, 0),
  496. new Vector3(-1, 0, 0),
  497. new Vector3(0, 1, 0),
  498. new Vector3(0, -1, 0)
  499. ];
  500. var indices = [];
  501. var positions = [];
  502. var normals = [];
  503. var uvs = [];
  504. var width = options.width || options.size || 1;
  505. var height = options.height || options.size || 1;
  506. var depth = options.depth || options.size || 1;
  507. var faceUV: Vector4[] = options.faceUV || new Array<Vector4>(6);
  508. var faceColors: Color4[];
  509. var colors = [];
  510. if (options.faceColors) {
  511. faceColors = options.faceColors;
  512. }
  513. var sideOrientation = (options.sideOrientation === 0) ? 0 : options.sideOrientation || Mesh.DEFAULTSIDE;
  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, diameterX?: number, diameterY?: number, diameterZ?: number, sideOrientation?: number }): VertexData;
  583. public static CreateSphere(segments: number, diameter?: number, sideOrientation?: number): VertexData;
  584. public static CreateSphere(options: any, diameter?: number, sideOrientation: number = Mesh.DEFAULTSIDE): VertexData {
  585. var segments: number;
  586. var diameterX: number;
  587. var diameterY: number;
  588. var diameterZ: number;
  589. if (options.segments) {
  590. segments = options.segments || 32;
  591. diameterX = options.diameterX || 1;
  592. diameterY = options.diameterY || 1;
  593. diameterZ = options.diameterZ || 1;
  594. } else { // Back-compat
  595. segments = options || 32;
  596. diameterX = diameter || 1;
  597. diameterY = diameterX;
  598. diameterZ = diameterX;
  599. }
  600. sideOrientation = sideOrientation || options.sideOrientation || Mesh.DEFAULTSIDE;
  601. var radius = new Vector3(diameterX / 2, diameterY / 2, diameterZ / 2);
  602. var totalZRotationSteps = 2 + segments;
  603. var totalYRotationSteps = 2 * totalZRotationSteps;
  604. var indices = [];
  605. var positions = [];
  606. var normals = [];
  607. var uvs = [];
  608. for (var zRotationStep = 0; zRotationStep <= totalZRotationSteps; zRotationStep++) {
  609. var normalizedZ = zRotationStep / totalZRotationSteps;
  610. var angleZ = (normalizedZ * Math.PI);
  611. for (var yRotationStep = 0; yRotationStep <= totalYRotationSteps; yRotationStep++) {
  612. var normalizedY = yRotationStep / totalYRotationSteps;
  613. var angleY = normalizedY * Math.PI * 2;
  614. var rotationZ = Matrix.RotationZ(-angleZ);
  615. var rotationY = Matrix.RotationY(angleY);
  616. var afterRotZ = Vector3.TransformCoordinates(Vector3.Up(), rotationZ);
  617. var complete = Vector3.TransformCoordinates(afterRotZ, rotationY);
  618. var vertex = complete.multiply(radius);
  619. var normal = Vector3.Normalize(vertex);
  620. positions.push(vertex.x, vertex.y, vertex.z);
  621. normals.push(normal.x, normal.y, normal.z);
  622. uvs.push(normalizedY, normalizedZ);
  623. }
  624. if (zRotationStep > 0) {
  625. var verticesCount = positions.length / 3;
  626. for (var firstIndex = verticesCount - 2 * (totalYRotationSteps + 1); (firstIndex + totalYRotationSteps + 2) < verticesCount; firstIndex++) {
  627. indices.push((firstIndex));
  628. indices.push((firstIndex + 1));
  629. indices.push(firstIndex + totalYRotationSteps + 1);
  630. indices.push((firstIndex + totalYRotationSteps + 1));
  631. indices.push((firstIndex + 1));
  632. indices.push((firstIndex + totalYRotationSteps + 2));
  633. }
  634. }
  635. }
  636. // Sides
  637. VertexData._ComputeSides(sideOrientation, positions, indices, normals, uvs);
  638. // Result
  639. var vertexData = new VertexData();
  640. vertexData.indices = indices;
  641. vertexData.positions = positions;
  642. vertexData.normals = normals;
  643. vertexData.uvs = uvs;
  644. return vertexData;
  645. }
  646. // Cylinder and cone
  647. public static CreateCylinder(options: { height?: number, diameterTop?: number, diameterBottom?: number, tessellation?: number, subdivisions?: number, sideOrientation?: number }): VertexData {
  648. var height: number = options.height || 3;
  649. var diameterTop: number = (options.diameterTop === 0) ? 0 : options.diameterTop || 1;
  650. var diameterBottom: number = options.diameterBottom || 1;
  651. var tessellation:number = options.tessellation || 24;
  652. var subdivisions:number = options.subdivisions || 1;
  653. var sideOrientation: number = (options.sideOrientation === 0) ? 0 : options.sideOrientation || Mesh.DEFAULTSIDE;
  654. var indices = [];
  655. var positions = [];
  656. var normals = [];
  657. var uvs = [];
  658. var angle_step = Math.PI * 2 / tessellation;
  659. var angle: number;
  660. var h: number;
  661. var radius: number;
  662. var tan = (diameterBottom - diameterTop) / 2 / height;
  663. var ringVertex: Vector3 = Vector3.Zero();
  664. var ringNormal: Vector3 = Vector3.Zero();
  665. // positions, normals, uvs
  666. var i: number;
  667. var j: number;
  668. for (i = 0; i <= subdivisions; i++) {
  669. h = i / subdivisions;
  670. radius = (h * (diameterTop - diameterBottom) + diameterBottom) / 2;
  671. for (j = 0; j <= tessellation; j++) {
  672. angle = j * angle_step;
  673. ringVertex.x = Math.cos(-angle) * radius;
  674. ringVertex.y = -height / 2 + h * height;
  675. ringVertex.z = Math.sin(-angle) * radius;
  676. if (diameterTop === 0 && i === subdivisions) {
  677. // if no top cap, reuse former normals
  678. ringNormal.x = normals[normals.length - (tessellation + 1) * 3];
  679. ringNormal.y = normals[normals.length - (tessellation + 1) * 3 + 1];
  680. ringNormal.z = normals[normals.length - (tessellation + 1) * 3 + 2];
  681. }
  682. else {
  683. ringNormal.x = ringVertex.x;
  684. ringNormal.z = ringVertex.z;
  685. ringNormal.y = Math.sqrt(ringNormal.x * ringNormal.x + ringNormal.z * ringNormal.z) * tan;
  686. ringNormal.normalize();
  687. }
  688. positions.push(ringVertex.x, ringVertex.y, ringVertex.z);
  689. normals.push(ringNormal.x, ringNormal.y, ringNormal.z);
  690. uvs.push(j / tessellation, 1 - h);
  691. }
  692. }
  693. // indices
  694. for (i = 0; i < subdivisions; i++) {
  695. for (j = 0; j < tessellation; j++) {
  696. var i0 = i * (tessellation + 1) + j;
  697. var i1 = (i + 1) * (tessellation + 1) + j;
  698. var i2 = i * (tessellation + 1) + (j + 1);
  699. var i3 = (i + 1) * (tessellation + 1) + (j + 1);
  700. indices.push(i0, i1, i2);
  701. indices.push(i3, i2, i1);
  702. }
  703. }
  704. // Caps
  705. var createCylinderCap = isTop => {
  706. var radius = isTop ? diameterTop / 2 : diameterBottom / 2;
  707. if (radius === 0) {
  708. return;
  709. }
  710. var vbase = positions.length / 3;
  711. var offset = new Vector3(0, isTop ? height / 2 : -height / 2, 0);
  712. var textureScale = new Vector2(0.5, 0.5);
  713. // Cap positions, normals & uvs
  714. var angle;
  715. var circleVector;
  716. var i: number;
  717. for (i = 0; i < tessellation; i++) {
  718. angle = Math.PI * 2 * i / tessellation;
  719. circleVector = new Vector3(Math.cos(-angle), 0, Math.sin(-angle));
  720. var position = circleVector.scale(radius).add(offset);
  721. var textureCoordinate = new Vector2(circleVector.x * textureScale.x + 0.5, circleVector.z * textureScale.y + 0.5);
  722. positions.push(position.x, position.y, position.z);
  723. normals.push(0, isTop ? 1 : -1, 0);
  724. uvs.push(textureCoordinate.x, textureCoordinate.y);
  725. }
  726. // Cap indices
  727. for (i = 0; i < tessellation - 2; i++) {
  728. if (!isTop) {
  729. indices.push(vbase);
  730. indices.push(vbase + (i + 1) % tessellation);
  731. indices.push(vbase + (i + 2) % tessellation);
  732. }
  733. else {
  734. indices.push(vbase);
  735. indices.push(vbase + (i + 2) % tessellation);
  736. indices.push(vbase + (i + 1) % tessellation);
  737. }
  738. }
  739. };
  740. // add caps to geometry
  741. createCylinderCap(true);
  742. createCylinderCap(false);
  743. // Sides
  744. VertexData._ComputeSides(sideOrientation, positions, indices, normals, uvs);
  745. var vertexData = new VertexData();
  746. vertexData.indices = indices;
  747. vertexData.positions = positions;
  748. vertexData.normals = normals;
  749. vertexData.uvs = uvs;
  750. return vertexData;
  751. }
  752. public static CreateTorus(diameter, thickness, tessellation, sideOrientation: number = Mesh.DEFAULTSIDE) {
  753. var indices = [];
  754. var positions = [];
  755. var normals = [];
  756. var uvs = [];
  757. diameter = diameter || 1;
  758. thickness = thickness || 0.5;
  759. tessellation = tessellation || 16;
  760. var stride = tessellation + 1;
  761. for (var i = 0; i <= tessellation; i++) {
  762. var u = i / tessellation;
  763. var outerAngle = i * Math.PI * 2.0 / tessellation - Math.PI / 2.0;
  764. var transform = Matrix.Translation(diameter / 2.0, 0, 0).multiply(Matrix.RotationY(outerAngle));
  765. for (var j = 0; j <= tessellation; j++) {
  766. var v = 1 - j / tessellation;
  767. var innerAngle = j * Math.PI * 2.0 / tessellation + Math.PI;
  768. var dx = Math.cos(innerAngle);
  769. var dy = Math.sin(innerAngle);
  770. // Create a vertex.
  771. var normal = new Vector3(dx, dy, 0);
  772. var position = normal.scale(thickness / 2);
  773. var textureCoordinate = new Vector2(u, v);
  774. position = Vector3.TransformCoordinates(position, transform);
  775. normal = Vector3.TransformNormal(normal, transform);
  776. positions.push(position.x, position.y, position.z);
  777. normals.push(normal.x, normal.y, normal.z);
  778. uvs.push(textureCoordinate.x, textureCoordinate.y);
  779. // And create indices for two triangles.
  780. var nextI = (i + 1) % stride;
  781. var nextJ = (j + 1) % stride;
  782. indices.push(i * stride + j);
  783. indices.push(i * stride + nextJ);
  784. indices.push(nextI * stride + j);
  785. indices.push(i * stride + nextJ);
  786. indices.push(nextI * stride + nextJ);
  787. indices.push(nextI * stride + j);
  788. }
  789. }
  790. // Sides
  791. VertexData._ComputeSides(sideOrientation, positions, indices, normals, uvs);
  792. // Result
  793. var vertexData = new VertexData();
  794. vertexData.indices = indices;
  795. vertexData.positions = positions;
  796. vertexData.normals = normals;
  797. vertexData.uvs = uvs;
  798. return vertexData;
  799. }
  800. public static CreateLines(points: Vector3[]): VertexData {
  801. var indices = [];
  802. var positions = [];
  803. for (var index = 0; index < points.length; index++) {
  804. positions.push(points[index].x, points[index].y, points[index].z);
  805. if (index > 0) {
  806. indices.push(index - 1);
  807. indices.push(index);
  808. }
  809. }
  810. // Result
  811. var vertexData = new VertexData();
  812. vertexData.indices = indices;
  813. vertexData.positions = positions;
  814. return vertexData;
  815. }
  816. public static CreateDashedLines(points: Vector3[], dashSize: number, gapSize: number, dashNb: number): VertexData {
  817. dashSize = dashSize || 3;
  818. gapSize = gapSize || 1;
  819. dashNb = dashNb || 200;
  820. var positions = new Array<number>();
  821. var indices = new Array<number>();
  822. var curvect = Vector3.Zero();
  823. var lg = 0;
  824. var nb = 0;
  825. var shft = 0;
  826. var dashshft = 0;
  827. var curshft = 0;
  828. var idx = 0;
  829. var i = 0;
  830. for (i = 0; i < points.length - 1; i++) {
  831. points[i + 1].subtractToRef(points[i], curvect);
  832. lg += curvect.length();
  833. }
  834. shft = lg / dashNb;
  835. dashshft = dashSize * shft / (dashSize + gapSize);
  836. for (i = 0; i < points.length - 1; i++) {
  837. points[i + 1].subtractToRef(points[i], curvect);
  838. nb = Math.floor(curvect.length() / shft);
  839. curvect.normalize();
  840. for (var j = 0; j < nb; j++) {
  841. curshft = shft * j;
  842. positions.push(points[i].x + curshft * curvect.x, points[i].y + curshft * curvect.y, points[i].z + curshft * curvect.z);
  843. positions.push(points[i].x + (curshft + dashshft) * curvect.x, points[i].y + (curshft + dashshft) * curvect.y, points[i].z + (curshft + dashshft) * curvect.z);
  844. indices.push(idx, idx + 1);
  845. idx += 2;
  846. }
  847. }
  848. // Result
  849. var vertexData = new VertexData();
  850. vertexData.positions = positions;
  851. vertexData.indices = indices;
  852. return vertexData;
  853. }
  854. public static CreateGround(options: { width?: number, height?: number, subdivisions?: number, sideOrientation?: number }): VertexData;
  855. public static CreateGround(width: number, height: number, subdivisions?: number): VertexData;
  856. public static CreateGround(options: any, height?: number, subdivisions?: number): VertexData {
  857. var indices = [];
  858. var positions = [];
  859. var normals = [];
  860. var uvs = [];
  861. var row: number, col: number;
  862. var width: number;
  863. if (options.width) {
  864. width = options.width || 1;
  865. height = options.height || 1;
  866. subdivisions = options.subdivisions || 1;
  867. } else {
  868. width = options || 1;
  869. height = height || 1;
  870. subdivisions = subdivisions || 1;
  871. }
  872. for (row = 0; row <= subdivisions; row++) {
  873. for (col = 0; col <= subdivisions; col++) {
  874. var position = new Vector3((col * width) / subdivisions - (width / 2.0), 0, ((subdivisions - row) * height) / subdivisions - (height / 2.0));
  875. var normal = new Vector3(0, 1.0, 0);
  876. positions.push(position.x, position.y, position.z);
  877. normals.push(normal.x, normal.y, normal.z);
  878. uvs.push(col / subdivisions, 1.0 - row / subdivisions);
  879. }
  880. }
  881. for (row = 0; row < subdivisions; row++) {
  882. for (col = 0; col < subdivisions; col++) {
  883. indices.push(col + 1 + (row + 1) * (subdivisions + 1));
  884. indices.push(col + 1 + row * (subdivisions + 1));
  885. indices.push(col + row * (subdivisions + 1));
  886. indices.push(col + (row + 1) * (subdivisions + 1));
  887. indices.push(col + 1 + (row + 1) * (subdivisions + 1));
  888. indices.push(col + row * (subdivisions + 1));
  889. }
  890. }
  891. // Result
  892. var vertexData = new VertexData();
  893. vertexData.indices = indices;
  894. vertexData.positions = positions;
  895. vertexData.normals = normals;
  896. vertexData.uvs = uvs;
  897. return vertexData;
  898. }
  899. public static CreateTiledGround(xmin: number, zmin: number, xmax: number, zmax: number, subdivisions = { w: 1, h: 1 }, precision = { w: 1, h: 1 }): VertexData {
  900. var indices = [];
  901. var positions = [];
  902. var normals = [];
  903. var uvs = [];
  904. var row: number, col: number, tileRow: number, tileCol: number;
  905. subdivisions.h = (subdivisions.w < 1) ? 1 : subdivisions.h;
  906. subdivisions.w = (subdivisions.w < 1) ? 1 : subdivisions.w;
  907. precision.w = (precision.w < 1) ? 1 : precision.w;
  908. precision.h = (precision.h < 1) ? 1 : precision.h;
  909. var tileSize = {
  910. 'w': (xmax - xmin) / subdivisions.w,
  911. 'h': (zmax - zmin) / subdivisions.h
  912. };
  913. function applyTile(xTileMin: number, zTileMin: number, xTileMax: number, zTileMax: number) {
  914. // Indices
  915. var base = positions.length / 3;
  916. var rowLength = precision.w + 1;
  917. for (row = 0; row < precision.h; row++) {
  918. for (col = 0; col < precision.w; col++) {
  919. var square = [
  920. base + col + row * rowLength,
  921. base + (col + 1) + row * rowLength,
  922. base + (col + 1) + (row + 1) * rowLength,
  923. base + col + (row + 1) * rowLength
  924. ];
  925. indices.push(square[1]);
  926. indices.push(square[2]);
  927. indices.push(square[3]);
  928. indices.push(square[0]);
  929. indices.push(square[1]);
  930. indices.push(square[3]);
  931. }
  932. }
  933. // Position, normals and uvs
  934. var position = Vector3.Zero();
  935. var normal = new Vector3(0, 1.0, 0);
  936. for (row = 0; row <= precision.h; row++) {
  937. position.z = (row * (zTileMax - zTileMin)) / precision.h + zTileMin;
  938. for (col = 0; col <= precision.w; col++) {
  939. position.x = (col * (xTileMax - xTileMin)) / precision.w + xTileMin;
  940. position.y = 0;
  941. positions.push(position.x, position.y, position.z);
  942. normals.push(normal.x, normal.y, normal.z);
  943. uvs.push(col / precision.w, row / precision.h);
  944. }
  945. }
  946. }
  947. for (tileRow = 0; tileRow < subdivisions.h; tileRow++) {
  948. for (tileCol = 0; tileCol < subdivisions.w; tileCol++) {
  949. applyTile(
  950. xmin + tileCol * tileSize.w,
  951. zmin + tileRow * tileSize.h,
  952. xmin + (tileCol + 1) * tileSize.w,
  953. zmin + (tileRow + 1) * tileSize.h
  954. );
  955. }
  956. }
  957. // Result
  958. var vertexData = new VertexData();
  959. vertexData.indices = indices;
  960. vertexData.positions = positions;
  961. vertexData.normals = normals;
  962. vertexData.uvs = uvs;
  963. return vertexData;
  964. }
  965. public static CreateGroundFromHeightMap(width: number, height: number, subdivisions: number, minHeight: number, maxHeight: number, buffer: Uint8Array, bufferWidth: number, bufferHeight: number): VertexData {
  966. var indices = [];
  967. var positions = [];
  968. var normals = [];
  969. var uvs = [];
  970. var row, col;
  971. // Vertices
  972. for (row = 0; row <= subdivisions; row++) {
  973. for (col = 0; col <= subdivisions; col++) {
  974. var position = new Vector3((col * width) / subdivisions - (width / 2.0), 0, ((subdivisions - row) * height) / subdivisions - (height / 2.0));
  975. // Compute height
  976. var heightMapX = (((position.x + width / 2) / width) * (bufferWidth - 1)) | 0;
  977. var heightMapY = ((1.0 - (position.z + height / 2) / height) * (bufferHeight - 1)) | 0;
  978. var pos = (heightMapX + heightMapY * bufferWidth) * 4;
  979. var r = buffer[pos] / 255.0;
  980. var g = buffer[pos + 1] / 255.0;
  981. var b = buffer[pos + 2] / 255.0;
  982. var gradient = r * 0.3 + g * 0.59 + b * 0.11;
  983. position.y = minHeight + (maxHeight - minHeight) * gradient;
  984. // Add vertex
  985. positions.push(position.x, position.y, position.z);
  986. normals.push(0, 0, 0);
  987. uvs.push(col / subdivisions, 1.0 - row / subdivisions);
  988. }
  989. }
  990. // Indices
  991. for (row = 0; row < subdivisions; row++) {
  992. for (col = 0; col < subdivisions; col++) {
  993. indices.push(col + 1 + (row + 1) * (subdivisions + 1));
  994. indices.push(col + 1 + row * (subdivisions + 1));
  995. indices.push(col + row * (subdivisions + 1));
  996. indices.push(col + (row + 1) * (subdivisions + 1));
  997. indices.push(col + 1 + (row + 1) * (subdivisions + 1));
  998. indices.push(col + row * (subdivisions + 1));
  999. }
  1000. }
  1001. // Normals
  1002. VertexData.ComputeNormals(positions, indices, normals);
  1003. // Result
  1004. var vertexData = new VertexData();
  1005. vertexData.indices = indices;
  1006. vertexData.positions = positions;
  1007. vertexData.normals = normals;
  1008. vertexData.uvs = uvs;
  1009. return vertexData;
  1010. }
  1011. public static CreatePlane(options: { width?: number, height?: number, sideOrientation?: number }): VertexData;
  1012. public static CreatePlane(size: number, sideOrientation?: number): VertexData;
  1013. public static CreatePlane(options: any, sideOrientation: number = Mesh.DEFAULTSIDE): VertexData {
  1014. var indices = [];
  1015. var positions = [];
  1016. var normals = [];
  1017. var uvs = [];
  1018. var width: number;
  1019. var height: number;
  1020. if (options.width) {
  1021. width = options.width || 1;
  1022. height = options.height || 1;
  1023. sideOrientation = options.sideOrientation || Mesh.DEFAULTSIDE;
  1024. } else {
  1025. width = options || 1;
  1026. height = options || 1;
  1027. }
  1028. // Vertices
  1029. var halfWidth = width / 2.0;
  1030. var halfHeight = height / 2.0;
  1031. positions.push(-halfWidth, -halfHeight, 0);
  1032. normals.push(0, 0, -1.0);
  1033. uvs.push(0.0, 0.0);
  1034. positions.push(halfWidth, -halfHeight, 0);
  1035. normals.push(0, 0, -1.0);
  1036. uvs.push(1.0, 0.0);
  1037. positions.push(halfWidth, halfHeight, 0);
  1038. normals.push(0, 0, -1.0);
  1039. uvs.push(1.0, 1.0);
  1040. positions.push(-halfWidth, halfHeight, 0);
  1041. normals.push(0, 0, -1.0);
  1042. uvs.push(0.0, 1.0);
  1043. // Indices
  1044. indices.push(0);
  1045. indices.push(1);
  1046. indices.push(2);
  1047. indices.push(0);
  1048. indices.push(2);
  1049. indices.push(3);
  1050. // Sides
  1051. VertexData._ComputeSides(sideOrientation, positions, indices, normals, uvs);
  1052. // Result
  1053. var vertexData = new VertexData();
  1054. vertexData.indices = indices;
  1055. vertexData.positions = positions;
  1056. vertexData.normals = normals;
  1057. vertexData.uvs = uvs;
  1058. return vertexData;
  1059. }
  1060. public static CreateDisc(radius: number, tessellation: number, sideOrientation: number = Mesh.DEFAULTSIDE): VertexData {
  1061. var positions = [];
  1062. var indices = [];
  1063. var normals = [];
  1064. var uvs = [];
  1065. // positions and uvs
  1066. positions.push(0, 0, 0); // disc center first
  1067. uvs.push(0.5, 0.5);
  1068. var step = Math.PI * 2 / tessellation;
  1069. for (var a = 0; a < Math.PI * 2; a += step) {
  1070. var x = Math.cos(a);
  1071. var y = Math.sin(a);
  1072. var u = (x + 1) / 2;
  1073. var v = (1 - y) / 2;
  1074. positions.push(radius * x, radius * y, 0);
  1075. uvs.push(u, v);
  1076. }
  1077. positions.push(positions[3], positions[4], positions[5]); // close the circle
  1078. uvs.push(uvs[2], uvs[3]);
  1079. //indices
  1080. var vertexNb = positions.length / 3;
  1081. for (var i = 1; i < vertexNb - 1; i++) {
  1082. indices.push(i + 1, 0, i);
  1083. }
  1084. // result
  1085. VertexData.ComputeNormals(positions, indices, normals);
  1086. VertexData._ComputeSides(sideOrientation, positions, indices, normals, uvs);
  1087. var vertexData = new VertexData();
  1088. vertexData.indices = indices;
  1089. vertexData.positions = positions;
  1090. vertexData.normals = normals;
  1091. vertexData.uvs = uvs;
  1092. return vertexData;
  1093. }
  1094. // based on http://code.google.com/p/away3d/source/browse/trunk/fp10/Away3D/src/away3d/primitives/TorusKnot.as?spec=svn2473&r=2473
  1095. public static CreateTorusKnot(radius: number, tube: number, radialSegments: number, tubularSegments: number, p: number, q: number, sideOrientation: number = Mesh.DEFAULTSIDE): VertexData {
  1096. var indices = [];
  1097. var positions = [];
  1098. var normals = [];
  1099. var uvs = [];
  1100. radius = radius || 2;
  1101. tube = tube || 0.5;
  1102. radialSegments = radialSegments || 32;
  1103. tubularSegments = tubularSegments || 32;
  1104. p = p || 2;
  1105. q = q || 3;
  1106. // Helper
  1107. var getPos = (angle) => {
  1108. var cu = Math.cos(angle);
  1109. var su = Math.sin(angle);
  1110. var quOverP = q / p * angle;
  1111. var cs = Math.cos(quOverP);
  1112. var tx = radius * (2 + cs) * 0.5 * cu;
  1113. var ty = radius * (2 + cs) * su * 0.5;
  1114. var tz = radius * Math.sin(quOverP) * 0.5;
  1115. return new Vector3(tx, ty, tz);
  1116. };
  1117. // Vertices
  1118. var i: number;
  1119. var j: number;
  1120. for (i = 0; i <= radialSegments; i++) {
  1121. var modI = i % radialSegments;
  1122. var u = modI / radialSegments * 2 * p * Math.PI;
  1123. var p1 = getPos(u);
  1124. var p2 = getPos(u + 0.01);
  1125. var tang = p2.subtract(p1);
  1126. var n = p2.add(p1);
  1127. var bitan = Vector3.Cross(tang, n);
  1128. n = Vector3.Cross(bitan, tang);
  1129. bitan.normalize();
  1130. n.normalize();
  1131. for (j = 0; j < tubularSegments; j++) {
  1132. var modJ = j % tubularSegments;
  1133. var v = modJ / tubularSegments * 2 * Math.PI;
  1134. var cx = -tube * Math.cos(v);
  1135. var cy = tube * Math.sin(v);
  1136. positions.push(p1.x + cx * n.x + cy * bitan.x);
  1137. positions.push(p1.y + cx * n.y + cy * bitan.y);
  1138. positions.push(p1.z + cx * n.z + cy * bitan.z);
  1139. uvs.push(i / radialSegments);
  1140. uvs.push(j / tubularSegments);
  1141. }
  1142. }
  1143. for (i = 0; i < radialSegments; i++) {
  1144. for (j = 0; j < tubularSegments; j++) {
  1145. var jNext = (j + 1) % tubularSegments;
  1146. var a = i * tubularSegments + j;
  1147. var b = (i + 1) * tubularSegments + j;
  1148. var c = (i + 1) * tubularSegments + jNext;
  1149. var d = i * tubularSegments + jNext;
  1150. indices.push(d); indices.push(b); indices.push(a);
  1151. indices.push(d); indices.push(c); indices.push(b);
  1152. }
  1153. }
  1154. // Normals
  1155. VertexData.ComputeNormals(positions, indices, normals);
  1156. // Sides
  1157. VertexData._ComputeSides(sideOrientation, positions, indices, normals, uvs);
  1158. // Result
  1159. var vertexData = new VertexData();
  1160. vertexData.indices = indices;
  1161. vertexData.positions = positions;
  1162. vertexData.normals = normals;
  1163. vertexData.uvs = uvs;
  1164. return vertexData;
  1165. }
  1166. // Tools
  1167. /**
  1168. * @param {any} - positions (number[] or Float32Array)
  1169. * @param {any} - indices (number[] or Uint16Array)
  1170. * @param {any} - normals (number[] or Float32Array)
  1171. */
  1172. public static ComputeNormals(positions: any, indices: any, normals: any) {
  1173. var index = 0;
  1174. // temp Vector3
  1175. var p1p2 = Vector3.Zero();
  1176. var p3p2 = Vector3.Zero();
  1177. var faceNormal = Vector3.Zero();
  1178. var vertexNormali1 = Vector3.Zero();
  1179. for (index = 0; index < positions.length; index++) {
  1180. normals[index] = 0.0;
  1181. }
  1182. // indice triplet = 1 face
  1183. var nbFaces = indices.length / 3;
  1184. for (index = 0; index < nbFaces; index++) {
  1185. var i1 = indices[index * 3];
  1186. var i2 = indices[index * 3 + 1];
  1187. var i3 = indices[index * 3 + 2];
  1188. p1p2.x = positions[i1 * 3] - positions[i2 * 3];
  1189. p1p2.y = positions[i1 * 3 + 1] - positions[i2 * 3 + 1];
  1190. p1p2.z = positions[i1 * 3 + 2] - positions[i2 * 3 + 2];
  1191. p3p2.x = positions[i3 * 3] - positions[i2 * 3];
  1192. p3p2.y = positions[i3 * 3 + 1] - positions[i2 * 3 + 1];
  1193. p3p2.z = positions[i3 * 3 + 2] - positions[i2 * 3 + 2];
  1194. Vector3.CrossToRef(p1p2, p3p2, faceNormal);
  1195. faceNormal.normalize();
  1196. normals[i1 * 3] += faceNormal.x;
  1197. normals[i1 * 3 + 1] += faceNormal.y;
  1198. normals[i1 * 3 + 2] += faceNormal.z;
  1199. normals[i2 * 3] += faceNormal.x;
  1200. normals[i2 * 3 + 1] += faceNormal.y;
  1201. normals[i2 * 3 + 2] += faceNormal.z;
  1202. normals[i3 * 3] += faceNormal.x;
  1203. normals[i3 * 3 + 1] += faceNormal.y;
  1204. normals[i3 * 3 + 2] += faceNormal.z;
  1205. }
  1206. // last normalization
  1207. for (index = 0; index < normals.length / 3; index++) {
  1208. Vector3.FromFloatsToRef(normals[index * 3], normals[index * 3 + 1], normals[index * 3 + 2], vertexNormali1);
  1209. vertexNormali1.normalize();
  1210. normals[index * 3] = vertexNormali1.x;
  1211. normals[index * 3 + 1] = vertexNormali1.y;
  1212. normals[index * 3 + 2] = vertexNormali1.z;
  1213. }
  1214. }
  1215. private static _ComputeSides(sideOrientation: number, positions: number[], indices: number[], normals: number[], uvs: number[]) {
  1216. var li: number = indices.length;
  1217. var ln: number = normals.length;
  1218. var i: number;
  1219. var n: number;
  1220. sideOrientation = sideOrientation || Mesh.DEFAULTSIDE;
  1221. switch (sideOrientation) {
  1222. case Mesh.FRONTSIDE:
  1223. // nothing changed
  1224. break;
  1225. case Mesh.BACKSIDE:
  1226. var tmp: number;
  1227. // indices
  1228. for (i = 0; i < li; i += 3) {
  1229. tmp = indices[i];
  1230. indices[i] = indices[i + 2];
  1231. indices[i + 2] = tmp;
  1232. }
  1233. // normals
  1234. for (n = 0; n < ln; n++) {
  1235. normals[n] = -normals[n];
  1236. }
  1237. break;
  1238. case Mesh.DOUBLESIDE:
  1239. // positions
  1240. var lp: number = positions.length;
  1241. var l: number = lp / 3;
  1242. for (var p = 0; p < lp; p++) {
  1243. positions[lp + p] = positions[p];
  1244. }
  1245. // indices
  1246. for (i = 0; i < li; i += 3) {
  1247. indices[i + li] = indices[i + 2] + l;
  1248. indices[i + 1 + li] = indices[i + 1] + l;
  1249. indices[i + 2 + li] = indices[i] + l;
  1250. }
  1251. // normals
  1252. for (n = 0; n < ln; n++) {
  1253. normals[ln + n] = -normals[n];
  1254. }
  1255. // uvs
  1256. var lu: number = uvs.length;
  1257. for (var u: number = 0; u < lu; u++) {
  1258. uvs[u + lu] = uvs[u];
  1259. }
  1260. break;
  1261. }
  1262. }
  1263. }
  1264. }