babylon.mesh.vertexData.ts 61 KB

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