babylon.mesh.vertexData.ts 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404
  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. if (this.positions) {
  151. var position = Vector3.Zero();
  152. for (var index = 0; index < this.positions.length; index += 3) {
  153. Vector3.FromArrayToRef(this.positions, index, position);
  154. Vector3.TransformCoordinatesToRef(position, matrix, transformed);
  155. this.positions[index] = transformed.x;
  156. this.positions[index + 1] = transformed.y;
  157. this.positions[index + 2] = transformed.z;
  158. }
  159. }
  160. if (this.normals) {
  161. var normal = Vector3.Zero();
  162. for (index = 0; index < this.normals.length; index += 3) {
  163. Vector3.FromArrayToRef(this.normals, index, normal);
  164. Vector3.TransformNormalToRef(normal, matrix, transformed);
  165. this.normals[index] = transformed.x;
  166. this.normals[index + 1] = transformed.y;
  167. this.normals[index + 2] = transformed.z;
  168. }
  169. }
  170. }
  171. public merge(other: VertexData): void {
  172. if (other.indices) {
  173. if (!this.indices) {
  174. this.indices = [];
  175. }
  176. var offset = this.positions ? this.positions.length / 3 : 0;
  177. for (var index = 0; index < other.indices.length; index++) {
  178. this.indices.push(other.indices[index] + offset);
  179. }
  180. }
  181. if (other.positions) {
  182. if (!this.positions) {
  183. this.positions = [];
  184. }
  185. for (index = 0; index < other.positions.length; index++) {
  186. this.positions.push(other.positions[index]);
  187. }
  188. }
  189. if (other.normals) {
  190. if (!this.normals) {
  191. this.normals = [];
  192. }
  193. for (index = 0; index < other.normals.length; index++) {
  194. this.normals.push(other.normals[index]);
  195. }
  196. }
  197. if (other.uvs) {
  198. if (!this.uvs) {
  199. this.uvs = [];
  200. }
  201. for (index = 0; index < other.uvs.length; index++) {
  202. this.uvs.push(other.uvs[index]);
  203. }
  204. }
  205. if (other.uvs2) {
  206. if (!this.uvs2) {
  207. this.uvs2 = [];
  208. }
  209. for (index = 0; index < other.uvs2.length; index++) {
  210. this.uvs2.push(other.uvs2[index]);
  211. }
  212. }
  213. if (other.uvs3) {
  214. if (!this.uvs3) {
  215. this.uvs3 = [];
  216. }
  217. for (index = 0; index < other.uvs3.length; index++) {
  218. this.uvs3.push(other.uvs3[index]);
  219. }
  220. }
  221. if (other.uvs4) {
  222. if (!this.uvs4) {
  223. this.uvs4 = [];
  224. }
  225. for (index = 0; index < other.uvs4.length; index++) {
  226. this.uvs4.push(other.uvs4[index]);
  227. }
  228. }
  229. if (other.uvs5) {
  230. if (!this.uvs5) {
  231. this.uvs5 = [];
  232. }
  233. for (index = 0; index < other.uvs5.length; index++) {
  234. this.uvs5.push(other.uvs5[index]);
  235. }
  236. }
  237. if (other.uvs6) {
  238. if (!this.uvs6) {
  239. this.uvs6 = [];
  240. }
  241. for (index = 0; index < other.uvs6.length; index++) {
  242. this.uvs6.push(other.uvs6[index]);
  243. }
  244. }
  245. if (other.matricesIndices) {
  246. if (!this.matricesIndices) {
  247. this.matricesIndices = [];
  248. }
  249. for (index = 0; index < other.matricesIndices.length; index++) {
  250. this.matricesIndices.push(other.matricesIndices[index]);
  251. }
  252. }
  253. if (other.matricesWeights) {
  254. if (!this.matricesWeights) {
  255. this.matricesWeights = [];
  256. }
  257. for (index = 0; index < other.matricesWeights.length; index++) {
  258. this.matricesWeights.push(other.matricesWeights[index]);
  259. }
  260. }
  261. if (other.colors) {
  262. if (!this.colors) {
  263. this.colors = [];
  264. }
  265. for (index = 0; index < other.colors.length; index++) {
  266. this.colors.push(other.colors[index]);
  267. }
  268. }
  269. }
  270. // Statics
  271. public static ExtractFromMesh(mesh: Mesh, copyWhenShared?: boolean): VertexData {
  272. return VertexData._ExtractFrom(mesh, copyWhenShared);
  273. }
  274. public static ExtractFromGeometry(geometry: Geometry, copyWhenShared?: boolean): VertexData {
  275. return VertexData._ExtractFrom(geometry, copyWhenShared);
  276. }
  277. private static _ExtractFrom(meshOrGeometry: IGetSetVerticesData, copyWhenShared?: boolean): VertexData {
  278. var result = new VertexData();
  279. if (meshOrGeometry.isVerticesDataPresent(VertexBuffer.PositionKind)) {
  280. result.positions = meshOrGeometry.getVerticesData(VertexBuffer.PositionKind, copyWhenShared);
  281. }
  282. if (meshOrGeometry.isVerticesDataPresent(VertexBuffer.NormalKind)) {
  283. result.normals = meshOrGeometry.getVerticesData(VertexBuffer.NormalKind, copyWhenShared);
  284. }
  285. if (meshOrGeometry.isVerticesDataPresent(VertexBuffer.UVKind)) {
  286. result.uvs = meshOrGeometry.getVerticesData(VertexBuffer.UVKind, copyWhenShared);
  287. }
  288. if (meshOrGeometry.isVerticesDataPresent(VertexBuffer.UV2Kind)) {
  289. result.uvs2 = meshOrGeometry.getVerticesData(VertexBuffer.UV2Kind, copyWhenShared);
  290. }
  291. if (meshOrGeometry.isVerticesDataPresent(VertexBuffer.UV3Kind)) {
  292. result.uvs3 = meshOrGeometry.getVerticesData(VertexBuffer.UV3Kind, copyWhenShared);
  293. }
  294. if (meshOrGeometry.isVerticesDataPresent(VertexBuffer.UV4Kind)) {
  295. result.uvs4 = meshOrGeometry.getVerticesData(VertexBuffer.UV4Kind, copyWhenShared);
  296. }
  297. if (meshOrGeometry.isVerticesDataPresent(VertexBuffer.UV5Kind)) {
  298. result.uvs5 = meshOrGeometry.getVerticesData(VertexBuffer.UV5Kind, copyWhenShared);
  299. }
  300. if (meshOrGeometry.isVerticesDataPresent(VertexBuffer.UV6Kind)) {
  301. result.uvs6 = meshOrGeometry.getVerticesData(VertexBuffer.UV6Kind, copyWhenShared);
  302. }
  303. if (meshOrGeometry.isVerticesDataPresent(VertexBuffer.ColorKind)) {
  304. result.colors = meshOrGeometry.getVerticesData(VertexBuffer.ColorKind, copyWhenShared);
  305. }
  306. if (meshOrGeometry.isVerticesDataPresent(VertexBuffer.MatricesIndicesKind)) {
  307. result.matricesIndices = meshOrGeometry.getVerticesData(VertexBuffer.MatricesIndicesKind, copyWhenShared);
  308. }
  309. if (meshOrGeometry.isVerticesDataPresent(VertexBuffer.MatricesWeightsKind)) {
  310. result.matricesWeights = meshOrGeometry.getVerticesData(VertexBuffer.MatricesWeightsKind, copyWhenShared);
  311. }
  312. result.indices = meshOrGeometry.getIndices(copyWhenShared);
  313. return result;
  314. }
  315. public static CreateRibbon(pathArray: Vector3[][], closeArray: boolean, closePath: boolean, offset: number, sideOrientation: number = Mesh.DEFAULTSIDE): VertexData {
  316. closeArray = closeArray || false;
  317. closePath = closePath || false;
  318. var defaultOffset = Math.floor(pathArray[0].length / 2);
  319. offset = offset || defaultOffset;
  320. offset = offset > defaultOffset ? defaultOffset : Math.floor(offset); // offset max allowed : defaultOffset
  321. var positions: number[] = [];
  322. var indices: number[] = [];
  323. var normals: number[] = [];
  324. var uvs: number[] = [];
  325. var us: number[][] = []; // us[path_id] = [uDist1, uDist2, uDist3 ... ] distances between points on path path_id
  326. var vs: number[][] = []; // vs[i] = [vDist1, vDist2, vDist3, ... ] distances between points i of consecutives paths from pathArray
  327. var uTotalDistance: number[] = []; // uTotalDistance[p] : total distance of path p
  328. var vTotalDistance: number[] = []; // vTotalDistance[i] : total distance between points i of first and last path from pathArray
  329. var minlg: number; // minimal length among all paths from pathArray
  330. var lg: number[] = []; // array of path lengths : nb of vertex per path
  331. var idx: number[] = []; // array of path indexes : index of each path (first vertex) in the total vertex number
  332. var p: number; // path iterator
  333. var i: number; // point iterator
  334. var j: number; // point iterator
  335. // if single path in pathArray
  336. if (pathArray.length < 2) {
  337. var ar1: Vector3[] = [];
  338. var ar2: Vector3[] = [];
  339. for (i = 0; i < pathArray[0].length - offset; i++) {
  340. ar1.push(pathArray[0][i]);
  341. ar2.push(pathArray[0][i + offset]);
  342. }
  343. pathArray = [ar1, ar2];
  344. }
  345. // positions and horizontal distances (u)
  346. var idc: number = 0;
  347. var closePathCorr: number = (closePath) ? 1 : 0;
  348. var path: Vector3[];
  349. var l: number;
  350. minlg = pathArray[0].length;
  351. for (p = 0; p < pathArray.length; p++) {
  352. uTotalDistance[p] = 0;
  353. us[p] = [0];
  354. path = pathArray[p];
  355. l = path.length;
  356. minlg = (minlg < l) ? minlg : l;
  357. j = 0;
  358. while (j < l) {
  359. positions.push(path[j].x, path[j].y, path[j].z);
  360. if (j > 0) {
  361. var vectlg: number = path[j].subtract(path[j - 1]).length();
  362. var dist: number = vectlg + uTotalDistance[p];
  363. us[p].push(dist);
  364. uTotalDistance[p] = dist;
  365. }
  366. j++;
  367. }
  368. if (closePath) {
  369. j--;
  370. positions.push(path[0].x, path[0].y, path[0].z);
  371. vectlg = path[j].subtract(path[0]).length();
  372. dist = vectlg + uTotalDistance[p];
  373. us[p].push(dist);
  374. uTotalDistance[p] = dist;
  375. }
  376. lg[p] = l + closePathCorr;
  377. idx[p] = idc;
  378. idc += (l + closePathCorr);
  379. }
  380. // vertical distances (v)
  381. var path1: Vector3[];
  382. var path2: Vector3[];
  383. var vertex1: Vector3;
  384. var vertex2: Vector3;
  385. for (i = 0; i < minlg + closePathCorr; i++) {
  386. vTotalDistance[i] = 0;
  387. vs[i] = [0];
  388. for (p = 0; p < pathArray.length - 1; p++) {
  389. path1 = pathArray[p];
  390. path2 = pathArray[p + 1];
  391. if (i === minlg) { // closePath
  392. vertex1 = path1[0];
  393. vertex2 = path2[0];
  394. }
  395. else {
  396. vertex1 = path1[i];
  397. vertex2 = path2[i];
  398. }
  399. vectlg = vertex2.subtract(vertex1).length();
  400. dist = vectlg + vTotalDistance[i];
  401. vs[i].push(dist);
  402. vTotalDistance[i] = dist;
  403. }
  404. if (closeArray) {
  405. path1 = pathArray[p];
  406. path2 = pathArray[0];
  407. vectlg = path2[i].subtract(path1[i]).length();
  408. dist = vectlg + vTotalDistance[i];
  409. vTotalDistance[i] = dist;
  410. }
  411. }
  412. // uvs
  413. var u: number;
  414. var v: number;
  415. for (p = 0; p < pathArray.length; p++) {
  416. for (i = 0; i < minlg + closePathCorr; i++) {
  417. u = us[p][i] / uTotalDistance[p];
  418. v = vs[i][p] / vTotalDistance[i];
  419. uvs.push(u, v);
  420. }
  421. }
  422. // indices
  423. p = 0; // path index
  424. var pi: number = 0; // positions array index
  425. var l1: number = lg[p] - 1; // path1 length
  426. var l2: number = lg[p + 1] - 1; // path2 length
  427. var min: number = (l1 < l2) ? l1 : l2; // current path stop index
  428. var shft: number = idx[1] - idx[0]; // shift
  429. var path1nb: number = closeArray ? lg.length : lg.length - 1; // number of path1 to iterate on
  430. while (pi <= min && p < path1nb) { // stay under min and don't go over next to last path
  431. // 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
  432. indices.push(pi, pi + shft, pi + 1);
  433. indices.push(pi + shft + 1, pi + 1, pi + shft);
  434. pi += 1;
  435. if (pi === min) { // if end of one of two consecutive paths reached, go to next existing path
  436. p++;
  437. if (p === lg.length - 1) { // last path of pathArray reached <=> closeArray == true
  438. shft = idx[0] - idx[p];
  439. l1 = lg[p] - 1;
  440. l2 = lg[0] - 1;
  441. }
  442. else {
  443. shft = idx[p + 1] - idx[p];
  444. l1 = lg[p] - 1;
  445. l2 = lg[p + 1] - 1;
  446. }
  447. pi = idx[p];
  448. min = (l1 < l2) ? l1 + pi : l2 + pi;
  449. }
  450. }
  451. // normals
  452. VertexData.ComputeNormals(positions, indices, normals);
  453. if (closePath) {
  454. var indexFirst: number = 0;
  455. var indexLast: number = 0;
  456. for (p = 0; p < pathArray.length; p++) {
  457. indexFirst = idx[p] * 3;
  458. if (p + 1 < pathArray.length) {
  459. indexLast = (idx[p + 1] - 1) * 3;
  460. }
  461. else {
  462. indexLast = normals.length - 3;
  463. }
  464. normals[indexFirst] = (normals[indexFirst] + normals[indexLast]) * 0.5;
  465. normals[indexFirst + 1] = (normals[indexFirst + 1] + normals[indexLast + 1]) * 0.5;
  466. normals[indexFirst + 2] = (normals[indexFirst + 2] + normals[indexLast + 2]) * 0.5;
  467. normals[indexLast] = normals[indexFirst];
  468. normals[indexLast + 1] = normals[indexFirst + 1];
  469. normals[indexLast + 2] = normals[indexFirst + 2];
  470. }
  471. }
  472. // sides
  473. VertexData._ComputeSides(sideOrientation, positions, indices, normals, uvs);
  474. // Result
  475. var vertexData = new VertexData();
  476. vertexData.indices = indices;
  477. vertexData.positions = positions;
  478. vertexData.normals = normals;
  479. vertexData.uvs = uvs;
  480. if (closePath) {
  481. (<any>vertexData)._idx = idx;
  482. }
  483. return vertexData;
  484. }
  485. public static CreateBox(size: number, sideOrientation: number = Mesh.DEFAULTSIDE): VertexData {
  486. var normalsSource = [
  487. new Vector3(0, 0, 1),
  488. new Vector3(0, 0, -1),
  489. new Vector3(1, 0, 0),
  490. new Vector3(-1, 0, 0),
  491. new Vector3(0, 1, 0),
  492. new Vector3(0, -1, 0)
  493. ];
  494. var indices = [];
  495. var positions = [];
  496. var normals = [];
  497. var uvs = [];
  498. size = size || 1;
  499. // Create each face in turn.
  500. for (var index = 0; index < normalsSource.length; index++) {
  501. var normal = normalsSource[index];
  502. // Get two vectors perpendicular to the face normal and to each other.
  503. var side1 = new Vector3(normal.y, normal.z, normal.x);
  504. var side2 = Vector3.Cross(normal, side1);
  505. // Six indices (two triangles) per face.
  506. var verticesLength = positions.length / 3;
  507. indices.push(verticesLength);
  508. indices.push(verticesLength + 1);
  509. indices.push(verticesLength + 2);
  510. indices.push(verticesLength);
  511. indices.push(verticesLength + 2);
  512. indices.push(verticesLength + 3);
  513. // Four vertices per face.
  514. var vertex = normal.subtract(side1).subtract(side2).scale(size / 2);
  515. positions.push(vertex.x, vertex.y, vertex.z);
  516. normals.push(normal.x, normal.y, normal.z);
  517. uvs.push(1.0, 1.0);
  518. vertex = normal.subtract(side1).add(side2).scale(size / 2);
  519. positions.push(vertex.x, vertex.y, vertex.z);
  520. normals.push(normal.x, normal.y, normal.z);
  521. uvs.push(0.0, 1.0);
  522. vertex = normal.add(side1).add(side2).scale(size / 2);
  523. positions.push(vertex.x, vertex.y, vertex.z);
  524. normals.push(normal.x, normal.y, normal.z);
  525. uvs.push(0.0, 0.0);
  526. vertex = normal.add(side1).subtract(side2).scale(size / 2);
  527. positions.push(vertex.x, vertex.y, vertex.z);
  528. normals.push(normal.x, normal.y, normal.z);
  529. uvs.push(1.0, 0.0);
  530. }
  531. // sides
  532. VertexData._ComputeSides(sideOrientation, positions, indices, normals, uvs);
  533. // Result
  534. var vertexData = new VertexData();
  535. vertexData.indices = indices;
  536. vertexData.positions = positions;
  537. vertexData.normals = normals;
  538. vertexData.uvs = uvs;
  539. return vertexData;
  540. }
  541. public static CreateSphere(segments: number, diameter: number, sideOrientation: number = Mesh.DEFAULTSIDE): VertexData {
  542. segments = segments || 32;
  543. diameter = diameter || 1;
  544. var radius = diameter / 2;
  545. var totalZRotationSteps = 2 + segments;
  546. var totalYRotationSteps = 2 * totalZRotationSteps;
  547. var indices = [];
  548. var positions = [];
  549. var normals = [];
  550. var uvs = [];
  551. for (var zRotationStep = 0; zRotationStep <= totalZRotationSteps; zRotationStep++) {
  552. var normalizedZ = zRotationStep / totalZRotationSteps;
  553. var angleZ = (normalizedZ * Math.PI);
  554. for (var yRotationStep = 0; yRotationStep <= totalYRotationSteps; yRotationStep++) {
  555. var normalizedY = yRotationStep / totalYRotationSteps;
  556. var angleY = normalizedY * Math.PI * 2;
  557. var rotationZ = Matrix.RotationZ(-angleZ);
  558. var rotationY = Matrix.RotationY(angleY);
  559. var afterRotZ = Vector3.TransformCoordinates(Vector3.Up(), rotationZ);
  560. var complete = Vector3.TransformCoordinates(afterRotZ, rotationY);
  561. var vertex = complete.scale(radius);
  562. var normal = Vector3.Normalize(vertex);
  563. positions.push(vertex.x, vertex.y, vertex.z);
  564. normals.push(normal.x, normal.y, normal.z);
  565. uvs.push(normalizedZ, normalizedY);
  566. }
  567. if (zRotationStep > 0) {
  568. var verticesCount = positions.length / 3;
  569. for (var firstIndex = verticesCount - 2 * (totalYRotationSteps + 1); (firstIndex + totalYRotationSteps + 2) < verticesCount; firstIndex++) {
  570. indices.push((firstIndex));
  571. indices.push((firstIndex + 1));
  572. indices.push(firstIndex + totalYRotationSteps + 1);
  573. indices.push((firstIndex + totalYRotationSteps + 1));
  574. indices.push((firstIndex + 1));
  575. indices.push((firstIndex + totalYRotationSteps + 2));
  576. }
  577. }
  578. }
  579. // Sides
  580. VertexData._ComputeSides(sideOrientation, positions, indices, normals, uvs);
  581. // Result
  582. var vertexData = new VertexData();
  583. vertexData.indices = indices;
  584. vertexData.positions = positions;
  585. vertexData.normals = normals;
  586. vertexData.uvs = uvs;
  587. return vertexData;
  588. }
  589. // Cylinder and cone (made using ribbons)
  590. public static CreateCylinder(height: number, diameterTop: number, diameterBottom: number, tessellation: number, subdivisions: number = 1, sideOrientation: number = Mesh.DEFAULTSIDE): VertexData {
  591. // setup tube creation parameters
  592. var path = [];
  593. for (var i = 0; i <= subdivisions; i++) {
  594. path.push(new Vector3(0, height * (- 0.5 + i / subdivisions), 0));
  595. }
  596. // this is what defines the radius along the cylinder
  597. var radiusFunction = function (i, distance) {
  598. return (diameterBottom + (diameterTop - diameterBottom) * distance / height) / 2;
  599. };
  600. // shortcut to 3d path data
  601. var path3D = new Path3D(path);
  602. var tangents = path3D.getTangents();
  603. var normals = path3D.getNormals();
  604. var distances = path3D.getDistances();
  605. // let's build the array of paths (rings)
  606. var pathArray: Vector3[][] = [];
  607. var ringVertex: Vector3;
  608. var angle;
  609. var angle_step = Math.PI * 2 / tessellation;
  610. var distance = 0;
  611. for (var i = 0; i <= subdivisions; i++) {
  612. pathArray[i] = [];
  613. for (var j = 0; j < tessellation; j++) {
  614. angle = j * angle_step;
  615. ringVertex = new Vector3(Math.cos(-angle), 0, Math.sin(-angle));
  616. ringVertex.scaleInPlace(radiusFunction(i, distances[i])).addInPlace(path[i]);
  617. pathArray[i].push(ringVertex);
  618. }
  619. }
  620. // create ribbon based on computed paths (& close seam)
  621. var vertexdata = VertexData.CreateRibbon(pathArray, false, true, 0, sideOrientation);
  622. var createCylinderCap = function (isTop) {
  623. var radius = isTop ? diameterTop / 2 : diameterBottom / 2;
  624. if (radius === 0) {
  625. return;
  626. }
  627. var vbase = vertexdata.positions.length / 3;
  628. var offset = new Vector3(0, isTop ? height / 2 : -height / 2, 0);
  629. var textureScale = new Vector2(0.5, 0.5);
  630. // Positions, normals & uvs
  631. var angle;
  632. var circleVector;
  633. for (var i = 0; i < tessellation; i++) {
  634. angle = Math.PI * 2 * i / tessellation;
  635. circleVector = new Vector3(Math.cos(-angle), 0, Math.sin(-angle));
  636. var position = circleVector.scale(radius).add(offset);
  637. var textureCoordinate = new Vector2(circleVector.x * textureScale.x + 0.5, circleVector.z * textureScale.y + 0.5);
  638. vertexdata.positions.push(position.x, position.y, position.z);
  639. vertexdata.normals.push(0, isTop ? 1 : -1, 0);
  640. vertexdata.uvs.push(textureCoordinate.x, textureCoordinate.y);
  641. }
  642. // Indices
  643. for (i = 0; i < tessellation - 2; i++) {
  644. if (!isTop) {
  645. vertexdata.indices.push(vbase);
  646. vertexdata.indices.push(vbase + (i + 1) % tessellation);
  647. vertexdata.indices.push(vbase + (i + 2) % tessellation);
  648. }
  649. else {
  650. vertexdata.indices.push(vbase);
  651. vertexdata.indices.push(vbase + (i + 2) % tessellation);
  652. vertexdata.indices.push(vbase + (i + 1) % tessellation);
  653. }
  654. }
  655. };
  656. // add caps to geometry
  657. createCylinderCap(true);
  658. createCylinderCap(false);
  659. return vertexdata;
  660. }
  661. public static CreateTorus(diameter, thickness, tessellation, sideOrientation: number = Mesh.DEFAULTSIDE) {
  662. var indices = [];
  663. var positions = [];
  664. var normals = [];
  665. var uvs = [];
  666. diameter = diameter || 1;
  667. thickness = thickness || 0.5;
  668. tessellation = tessellation || 16;
  669. var stride = tessellation + 1;
  670. for (var i = 0; i <= tessellation; i++) {
  671. var u = i / tessellation;
  672. var outerAngle = i * Math.PI * 2.0 / tessellation - Math.PI / 2.0;
  673. var transform = Matrix.Translation(diameter / 2.0, 0, 0).multiply(Matrix.RotationY(outerAngle));
  674. for (var j = 0; j <= tessellation; j++) {
  675. var v = 1 - j / tessellation;
  676. var innerAngle = j * Math.PI * 2.0 / tessellation + Math.PI;
  677. var dx = Math.cos(innerAngle);
  678. var dy = Math.sin(innerAngle);
  679. // Create a vertex.
  680. var normal = new Vector3(dx, dy, 0);
  681. var position = normal.scale(thickness / 2);
  682. var textureCoordinate = new Vector2(u, v);
  683. position = Vector3.TransformCoordinates(position, transform);
  684. normal = Vector3.TransformNormal(normal, transform);
  685. positions.push(position.x, position.y, position.z);
  686. normals.push(normal.x, normal.y, normal.z);
  687. uvs.push(textureCoordinate.x, textureCoordinate.y);
  688. // And create indices for two triangles.
  689. var nextI = (i + 1) % stride;
  690. var nextJ = (j + 1) % stride;
  691. indices.push(i * stride + j);
  692. indices.push(i * stride + nextJ);
  693. indices.push(nextI * stride + j);
  694. indices.push(i * stride + nextJ);
  695. indices.push(nextI * stride + nextJ);
  696. indices.push(nextI * stride + j);
  697. }
  698. }
  699. // Sides
  700. VertexData._ComputeSides(sideOrientation, positions, indices, normals, uvs);
  701. // Result
  702. var vertexData = new VertexData();
  703. vertexData.indices = indices;
  704. vertexData.positions = positions;
  705. vertexData.normals = normals;
  706. vertexData.uvs = uvs;
  707. return vertexData;
  708. }
  709. public static CreateLines(points: Vector3[]): VertexData {
  710. var indices = [];
  711. var positions = [];
  712. for (var index = 0; index < points.length; index++) {
  713. positions.push(points[index].x, points[index].y, points[index].z);
  714. if (index > 0) {
  715. indices.push(index - 1);
  716. indices.push(index);
  717. }
  718. }
  719. // Result
  720. var vertexData = new VertexData();
  721. vertexData.indices = indices;
  722. vertexData.positions = positions;
  723. return vertexData;
  724. }
  725. public static CreateDashedLines(points: Vector3[], dashSize: number, gapSize: number, dashNb: number): VertexData {
  726. dashSize = dashSize || 3;
  727. gapSize = gapSize || 1;
  728. dashNb = dashNb || 200;
  729. var positions = new Array<number>();
  730. var indices = new Array<number>();
  731. var curvect = Vector3.Zero();
  732. var lg = 0;
  733. var nb = 0;
  734. var shft = 0;
  735. var dashshft = 0;
  736. var curshft = 0;
  737. var idx = 0;
  738. var i = 0;
  739. for (i = 0; i < points.length - 1; i++) {
  740. points[i + 1].subtractToRef(points[i], curvect);
  741. lg += curvect.length();
  742. }
  743. shft = lg / dashNb;
  744. dashshft = dashSize * shft / (dashSize + gapSize);
  745. for (i = 0; i < points.length - 1; i++) {
  746. points[i + 1].subtractToRef(points[i], curvect);
  747. nb = Math.floor(curvect.length() / shft);
  748. curvect.normalize();
  749. for (var j = 0; j < nb; j++) {
  750. curshft = shft * j;
  751. positions.push(points[i].x + curshft * curvect.x, points[i].y + curshft * curvect.y, points[i].z + curshft * curvect.z);
  752. positions.push(points[i].x + (curshft + dashshft) * curvect.x, points[i].y + (curshft + dashshft) * curvect.y, points[i].z + (curshft + dashshft) * curvect.z);
  753. indices.push(idx, idx + 1);
  754. idx += 2;
  755. }
  756. }
  757. // Result
  758. var vertexData = new VertexData();
  759. vertexData.positions = positions;
  760. vertexData.indices = indices;
  761. return vertexData;
  762. }
  763. public static CreateGround(width: number, height: number, subdivisions: number): VertexData {
  764. var indices = [];
  765. var positions = [];
  766. var normals = [];
  767. var uvs = [];
  768. var row: number, col: number;
  769. width = width || 1;
  770. height = height || 1;
  771. subdivisions = subdivisions || 1;
  772. for (row = 0; row <= subdivisions; row++) {
  773. for (col = 0; col <= subdivisions; col++) {
  774. var position = new Vector3((col * width) / subdivisions - (width / 2.0), 0, ((subdivisions - row) * height) / subdivisions - (height / 2.0));
  775. var normal = new Vector3(0, 1.0, 0);
  776. positions.push(position.x, position.y, position.z);
  777. normals.push(normal.x, normal.y, normal.z);
  778. uvs.push(col / subdivisions, 1.0 - row / subdivisions);
  779. }
  780. }
  781. for (row = 0; row < subdivisions; row++) {
  782. for (col = 0; col < subdivisions; col++) {
  783. indices.push(col + 1 + (row + 1) * (subdivisions + 1));
  784. indices.push(col + 1 + row * (subdivisions + 1));
  785. indices.push(col + row * (subdivisions + 1));
  786. indices.push(col + (row + 1) * (subdivisions + 1));
  787. indices.push(col + 1 + (row + 1) * (subdivisions + 1));
  788. indices.push(col + row * (subdivisions + 1));
  789. }
  790. }
  791. // Result
  792. var vertexData = new VertexData();
  793. vertexData.indices = indices;
  794. vertexData.positions = positions;
  795. vertexData.normals = normals;
  796. vertexData.uvs = uvs;
  797. return vertexData;
  798. }
  799. public static CreateTiledGround(xmin: number, zmin: number, xmax: number, zmax: number, subdivisions = { w: 1, h: 1 }, precision = { w: 1, h: 1 }): VertexData {
  800. var indices = [];
  801. var positions = [];
  802. var normals = [];
  803. var uvs = [];
  804. var row: number, col: number, tileRow: number, tileCol: number;
  805. subdivisions.h = (subdivisions.w < 1) ? 1 : subdivisions.h;
  806. subdivisions.w = (subdivisions.w < 1) ? 1 : subdivisions.w;
  807. precision.w = (precision.w < 1) ? 1 : precision.w;
  808. precision.h = (precision.h < 1) ? 1 : precision.h;
  809. var tileSize = {
  810. 'w': (xmax - xmin) / subdivisions.w,
  811. 'h': (zmax - zmin) / subdivisions.h
  812. };
  813. function applyTile(xTileMin: number, zTileMin: number, xTileMax: number, zTileMax: number) {
  814. // Indices
  815. var base = positions.length / 3;
  816. var rowLength = precision.w + 1;
  817. for (row = 0; row < precision.h; row++) {
  818. for (col = 0; col < precision.w; col++) {
  819. var square = [
  820. base + col + row * rowLength,
  821. base + (col + 1) + row * rowLength,
  822. base + (col + 1) + (row + 1) * rowLength,
  823. base + col + (row + 1) * rowLength
  824. ];
  825. indices.push(square[1]);
  826. indices.push(square[2]);
  827. indices.push(square[3]);
  828. indices.push(square[0]);
  829. indices.push(square[1]);
  830. indices.push(square[3]);
  831. }
  832. }
  833. // Position, normals and uvs
  834. var position = Vector3.Zero();
  835. var normal = new Vector3(0, 1.0, 0);
  836. for (row = 0; row <= precision.h; row++) {
  837. position.z = (row * (zTileMax - zTileMin)) / precision.h + zTileMin;
  838. for (col = 0; col <= precision.w; col++) {
  839. position.x = (col * (xTileMax - xTileMin)) / precision.w + xTileMin;
  840. position.y = 0;
  841. positions.push(position.x, position.y, position.z);
  842. normals.push(normal.x, normal.y, normal.z);
  843. uvs.push(col / precision.w, row / precision.h);
  844. }
  845. }
  846. }
  847. for (tileRow = 0; tileRow < subdivisions.h; tileRow++) {
  848. for (tileCol = 0; tileCol < subdivisions.w; tileCol++) {
  849. applyTile(
  850. xmin + tileCol * tileSize.w,
  851. zmin + tileRow * tileSize.h,
  852. xmin + (tileCol + 1) * tileSize.w,
  853. zmin + (tileRow + 1) * tileSize.h
  854. );
  855. }
  856. }
  857. // Result
  858. var vertexData = new VertexData();
  859. vertexData.indices = indices;
  860. vertexData.positions = positions;
  861. vertexData.normals = normals;
  862. vertexData.uvs = uvs;
  863. return vertexData;
  864. }
  865. public static CreateGroundFromHeightMap(width: number, height: number, subdivisions: number, minHeight: number, maxHeight: number, buffer: Uint8Array, bufferWidth: number, bufferHeight: number): VertexData {
  866. var indices = [];
  867. var positions = [];
  868. var normals = [];
  869. var uvs = [];
  870. var row, col;
  871. // Vertices
  872. for (row = 0; row <= subdivisions; row++) {
  873. for (col = 0; col <= subdivisions; col++) {
  874. var position = new Vector3((col * width) / subdivisions - (width / 2.0), 0, ((subdivisions - row) * height) / subdivisions - (height / 2.0));
  875. // Compute height
  876. var heightMapX = (((position.x + width / 2) / width) * (bufferWidth - 1)) | 0;
  877. var heightMapY = ((1.0 - (position.z + height / 2) / height) * (bufferHeight - 1)) | 0;
  878. var pos = (heightMapX + heightMapY * bufferWidth) * 4;
  879. var r = buffer[pos] / 255.0;
  880. var g = buffer[pos + 1] / 255.0;
  881. var b = buffer[pos + 2] / 255.0;
  882. var gradient = r * 0.3 + g * 0.59 + b * 0.11;
  883. position.y = minHeight + (maxHeight - minHeight) * gradient;
  884. // Add vertex
  885. positions.push(position.x, position.y, position.z);
  886. normals.push(0, 0, 0);
  887. uvs.push(col / subdivisions, 1.0 - row / subdivisions);
  888. }
  889. }
  890. // Indices
  891. for (row = 0; row < subdivisions; row++) {
  892. for (col = 0; col < subdivisions; col++) {
  893. indices.push(col + 1 + (row + 1) * (subdivisions + 1));
  894. indices.push(col + 1 + row * (subdivisions + 1));
  895. indices.push(col + row * (subdivisions + 1));
  896. indices.push(col + (row + 1) * (subdivisions + 1));
  897. indices.push(col + 1 + (row + 1) * (subdivisions + 1));
  898. indices.push(col + row * (subdivisions + 1));
  899. }
  900. }
  901. // Normals
  902. VertexData.ComputeNormals(positions, indices, normals);
  903. // Result
  904. var vertexData = new VertexData();
  905. vertexData.indices = indices;
  906. vertexData.positions = positions;
  907. vertexData.normals = normals;
  908. vertexData.uvs = uvs;
  909. return vertexData;
  910. }
  911. public static CreatePlane(size: number, sideOrientation: number = Mesh.DEFAULTSIDE): VertexData {
  912. var indices = [];
  913. var positions = [];
  914. var normals = [];
  915. var uvs = [];
  916. size = size || 1;
  917. // Vertices
  918. var halfSize = size / 2.0;
  919. positions.push(-halfSize, -halfSize, 0);
  920. normals.push(0, 0, -1.0);
  921. uvs.push(0.0, 0.0);
  922. positions.push(halfSize, -halfSize, 0);
  923. normals.push(0, 0, -1.0);
  924. uvs.push(1.0, 0.0);
  925. positions.push(halfSize, halfSize, 0);
  926. normals.push(0, 0, -1.0);
  927. uvs.push(1.0, 1.0);
  928. positions.push(-halfSize, halfSize, 0);
  929. normals.push(0, 0, -1.0);
  930. uvs.push(0.0, 1.0);
  931. // Indices
  932. indices.push(0);
  933. indices.push(1);
  934. indices.push(2);
  935. indices.push(0);
  936. indices.push(2);
  937. indices.push(3);
  938. // Sides
  939. VertexData._ComputeSides(sideOrientation, positions, indices, normals, uvs);
  940. // Result
  941. var vertexData = new VertexData();
  942. vertexData.indices = indices;
  943. vertexData.positions = positions;
  944. vertexData.normals = normals;
  945. vertexData.uvs = uvs;
  946. return vertexData;
  947. }
  948. public static CreateDisc(radius: number, tessellation: number, sideOrientation: number = Mesh.DEFAULTSIDE): VertexData {
  949. var positions = [];
  950. var indices = [];
  951. var normals = [];
  952. var uvs = [];
  953. // positions and uvs
  954. positions.push(0, 0, 0); // disc center first
  955. uvs.push(0.5, 0.5);
  956. var step = Math.PI * 2 / tessellation;
  957. for (var a = 0; a < Math.PI * 2; a += step) {
  958. var x = Math.cos(a);
  959. var y = Math.sin(a);
  960. var u = (x + 1) / 2;
  961. var v = (1 - y) / 2;
  962. positions.push(radius * x, radius * y, 0);
  963. uvs.push(u, v);
  964. }
  965. positions.push(positions[3], positions[4], positions[5]); // close the circle
  966. uvs.push(uvs[2], uvs[3]);
  967. //indices
  968. var vertexNb = positions.length / 3;
  969. for (var i = 1; i < vertexNb - 1; i++) {
  970. indices.push(i + 1, 0, i);
  971. }
  972. // result
  973. VertexData.ComputeNormals(positions, indices, normals);
  974. VertexData._ComputeSides(sideOrientation, positions, indices, normals, uvs);
  975. var vertexData = new VertexData();
  976. vertexData.indices = indices;
  977. vertexData.positions = positions;
  978. vertexData.normals = normals;
  979. vertexData.uvs = uvs;
  980. return vertexData;
  981. }
  982. // based on http://code.google.com/p/away3d/source/browse/trunk/fp10/Away3D/src/away3d/primitives/TorusKnot.as?spec=svn2473&r=2473
  983. public static CreateTorusKnot(radius: number, tube: number, radialSegments: number, tubularSegments: number, p: number, q: number, sideOrientation: number = Mesh.DEFAULTSIDE): VertexData {
  984. var indices = [];
  985. var positions = [];
  986. var normals = [];
  987. var uvs = [];
  988. radius = radius || 2;
  989. tube = tube || 0.5;
  990. radialSegments = radialSegments || 32;
  991. tubularSegments = tubularSegments || 32;
  992. p = p || 2;
  993. q = q || 3;
  994. // Helper
  995. var getPos = (angle) => {
  996. var cu = Math.cos(angle);
  997. var su = Math.sin(angle);
  998. var quOverP = q / p * angle;
  999. var cs = Math.cos(quOverP);
  1000. var tx = radius * (2 + cs) * 0.5 * cu;
  1001. var ty = radius * (2 + cs) * su * 0.5;
  1002. var tz = radius * Math.sin(quOverP) * 0.5;
  1003. return new Vector3(tx, ty, tz);
  1004. };
  1005. // Vertices
  1006. for (var i = 0; i <= radialSegments; i++) {
  1007. var modI = i % radialSegments;
  1008. var u = modI / radialSegments * 2 * p * Math.PI;
  1009. var p1 = getPos(u);
  1010. var p2 = getPos(u + 0.01);
  1011. var tang = p2.subtract(p1);
  1012. var n = p2.add(p1);
  1013. var bitan = Vector3.Cross(tang, n);
  1014. n = Vector3.Cross(bitan, tang);
  1015. bitan.normalize();
  1016. n.normalize();
  1017. for (var j = 0; j < tubularSegments; j++) {
  1018. var modJ = j % tubularSegments;
  1019. var v = modJ / tubularSegments * 2 * Math.PI;
  1020. var cx = -tube * Math.cos(v);
  1021. var cy = tube * Math.sin(v);
  1022. positions.push(p1.x + cx * n.x + cy * bitan.x);
  1023. positions.push(p1.y + cx * n.y + cy * bitan.y);
  1024. positions.push(p1.z + cx * n.z + cy * bitan.z);
  1025. uvs.push(i / radialSegments);
  1026. uvs.push(j / tubularSegments);
  1027. }
  1028. }
  1029. for (i = 0; i < radialSegments; i++) {
  1030. for (j = 0; j < tubularSegments; j++) {
  1031. var jNext = (j + 1) % tubularSegments;
  1032. var a = i * tubularSegments + j;
  1033. var b = (i + 1) * tubularSegments + j;
  1034. var c = (i + 1) * tubularSegments + jNext;
  1035. var d = i * tubularSegments + jNext;
  1036. indices.push(d); indices.push(b); indices.push(a);
  1037. indices.push(d); indices.push(c); indices.push(b);
  1038. }
  1039. }
  1040. // Normals
  1041. VertexData.ComputeNormals(positions, indices, normals);
  1042. // Sides
  1043. VertexData._ComputeSides(sideOrientation, positions, indices, normals, uvs);
  1044. // Result
  1045. var vertexData = new VertexData();
  1046. vertexData.indices = indices;
  1047. vertexData.positions = positions;
  1048. vertexData.normals = normals;
  1049. vertexData.uvs = uvs;
  1050. return vertexData;
  1051. }
  1052. // Tools
  1053. /**
  1054. * @param {any} - positions (number[] or Float32Array)
  1055. * @param {any} - indices (number[] or Uint16Array)
  1056. * @param {any} - normals (number[] or Float32Array)
  1057. */
  1058. public static ComputeNormals(positions: any, indices: any, normals: any) {
  1059. var index = 0;
  1060. // temp Vector3
  1061. var p1p2 = Vector3.Zero();
  1062. var p3p2 = Vector3.Zero();
  1063. var faceNormal = Vector3.Zero();
  1064. var vertexNormali1 = Vector3.Zero();
  1065. for (index = 0; index < positions.length; index++) {
  1066. normals[index] = 0.0;
  1067. }
  1068. // indice triplet = 1 face
  1069. var nbFaces = indices.length / 3;
  1070. for (index = 0; index < nbFaces; index++) {
  1071. var i1 = indices[index * 3];
  1072. var i2 = indices[index * 3 + 1];
  1073. var i3 = indices[index * 3 + 2];
  1074. p1p2.x = positions[i1 * 3] - positions[i2 * 3];
  1075. p1p2.y = positions[i1 * 3 + 1] - positions[i2 * 3 + 1];
  1076. p1p2.z = positions[i1 * 3 + 2] - positions[i2 * 3 + 2];
  1077. p3p2.x = positions[i3 * 3] - positions[i2 * 3];
  1078. p3p2.y = positions[i3 * 3 + 1] - positions[i2 * 3 + 1];
  1079. p3p2.z = positions[i3 * 3 + 2] - positions[i2 * 3 + 2];
  1080. Vector3.CrossToRef(p1p2, p3p2, faceNormal);
  1081. faceNormal.normalize();
  1082. normals[i1 * 3] += faceNormal.x;
  1083. normals[i1 * 3 + 1] += faceNormal.y;
  1084. normals[i1 * 3 + 2] += faceNormal.z;
  1085. normals[i2 * 3] += faceNormal.x;
  1086. normals[i2 * 3 + 1] += faceNormal.y;
  1087. normals[i2 * 3 + 2] += faceNormal.z;
  1088. normals[i3 * 3] += faceNormal.x;
  1089. normals[i3 * 3 + 1] += faceNormal.y;
  1090. normals[i3 * 3 + 2] += faceNormal.z;
  1091. }
  1092. // last normalization
  1093. for (index = 0; index < normals.length / 3; index++) {
  1094. Vector3.FromFloatsToRef(normals[index * 3], normals[index * 3 + 1], normals[index * 3 + 2], vertexNormali1);
  1095. vertexNormali1.normalize();
  1096. normals[index * 3] = vertexNormali1.x;
  1097. normals[index * 3 + 1] = vertexNormali1.y;
  1098. normals[index * 3 + 2] = vertexNormali1.z;
  1099. }
  1100. }
  1101. private static _ComputeSides(sideOrientation: number, positions: number[], indices: number[], normals: number[], uvs: number[]) {
  1102. var li: number = indices.length;
  1103. var ln: number = normals.length;
  1104. var i: number;
  1105. var n: number;
  1106. sideOrientation = sideOrientation || Mesh.DEFAULTSIDE;
  1107. switch (sideOrientation) {
  1108. case Mesh.FRONTSIDE:
  1109. // nothing changed
  1110. break;
  1111. case Mesh.BACKSIDE:
  1112. var tmp: number;
  1113. // indices
  1114. for (i = 0; i < li; i += 3) {
  1115. tmp = indices[i];
  1116. indices[i] = indices[i + 2];
  1117. indices[i + 2] = tmp;
  1118. }
  1119. // normals
  1120. for (n = 0; n < ln; n++) {
  1121. normals[n] = -normals[n];
  1122. }
  1123. break;
  1124. case Mesh.DOUBLESIDE:
  1125. // positions
  1126. var lp: number = positions.length;
  1127. var l: number = lp / 3;
  1128. for (var p = 0; p < lp; p++) {
  1129. positions[lp + p] = positions[p];
  1130. }
  1131. // indices
  1132. for (i = 0; i < li; i += 3) {
  1133. indices[i + li] = indices[i + 2] + l;
  1134. indices[i + 1 + li] = indices[i + 1] + l;
  1135. indices[i + 2 + li] = indices[i] + l;
  1136. }
  1137. // normals
  1138. for (n = 0; n < ln; n++) {
  1139. normals[ln + n] = -normals[n];
  1140. }
  1141. // uvs
  1142. var lu: number = uvs.length;
  1143. for (var u: number = 0; u < lu; u++) {
  1144. uvs[u + lu] = uvs[u];
  1145. }
  1146. break;
  1147. }
  1148. }
  1149. }
  1150. }