babylon.mesh.vertexData.ts 61 KB

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