babylon.mesh.vertexData.js 50 KB

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