babylon.mesh.vertexData.ts 51 KB

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