babylon.mesh.vertexData.js 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091
  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.CreateDashedLines = function (points, dashSize, gapSize, dashNb) {
  638. dashSize = dashSize || 3;
  639. gapSize = gapSize || 1;
  640. dashNb = dashNb || 200;
  641. var positions = new Array();
  642. var indices = new Array();
  643. var curvect = BABYLON.Vector3.Zero();
  644. var lg = 0;
  645. var nb = 0;
  646. var shft = 0;
  647. var dashshft = 0;
  648. var curshft = 0;
  649. var idx = 0;
  650. var i = 0;
  651. for (i = 0; i < points.length - 1; i++) {
  652. points[i + 1].subtractToRef(points[i], curvect);
  653. lg += curvect.length();
  654. }
  655. shft = lg / dashNb;
  656. dashshft = dashSize * shft / (dashSize + gapSize);
  657. for (i = 0; i < points.length - 1; i++) {
  658. points[i + 1].subtractToRef(points[i], curvect);
  659. nb = Math.floor(curvect.length() / shft);
  660. curvect.normalize();
  661. for (var j = 0; j < nb; j++) {
  662. curshft = shft * j;
  663. positions.push(points[i].x + curshft * curvect.x, points[i].y + curshft * curvect.y, points[i].z + curshft * curvect.z);
  664. positions.push(points[i].x + (curshft + dashshft) * curvect.x, points[i].y + (curshft + dashshft) * curvect.y, points[i].z + (curshft + dashshft) * curvect.z);
  665. indices.push(idx, idx + 1);
  666. idx += 2;
  667. }
  668. }
  669. // Result
  670. var vertexData = new VertexData();
  671. vertexData.positions = positions;
  672. vertexData.indices = indices;
  673. return vertexData;
  674. };
  675. VertexData.CreateGround = function (width, height, subdivisions) {
  676. var indices = [];
  677. var positions = [];
  678. var normals = [];
  679. var uvs = [];
  680. var row, col;
  681. width = width || 1;
  682. height = height || 1;
  683. subdivisions = subdivisions || 1;
  684. for (row = 0; row <= subdivisions; row++) {
  685. for (col = 0; col <= subdivisions; col++) {
  686. var position = new BABYLON.Vector3((col * width) / subdivisions - (width / 2.0), 0, ((subdivisions - row) * height) / subdivisions - (height / 2.0));
  687. var normal = new BABYLON.Vector3(0, 1.0, 0);
  688. positions.push(position.x, position.y, position.z);
  689. normals.push(normal.x, normal.y, normal.z);
  690. uvs.push(col / subdivisions, 1.0 - row / subdivisions);
  691. }
  692. }
  693. for (row = 0; row < subdivisions; row++) {
  694. for (col = 0; col < subdivisions; col++) {
  695. indices.push(col + 1 + (row + 1) * (subdivisions + 1));
  696. indices.push(col + 1 + row * (subdivisions + 1));
  697. indices.push(col + row * (subdivisions + 1));
  698. indices.push(col + (row + 1) * (subdivisions + 1));
  699. indices.push(col + 1 + (row + 1) * (subdivisions + 1));
  700. indices.push(col + row * (subdivisions + 1));
  701. }
  702. }
  703. // Result
  704. var vertexData = new VertexData();
  705. vertexData.indices = indices;
  706. vertexData.positions = positions;
  707. vertexData.normals = normals;
  708. vertexData.uvs = uvs;
  709. return vertexData;
  710. };
  711. VertexData.CreateTiledGround = function (xmin, zmin, xmax, zmax, subdivisions, precision) {
  712. if (subdivisions === void 0) { subdivisions = { w: 1, h: 1 }; }
  713. if (precision === void 0) { precision = { w: 1, h: 1 }; }
  714. var indices = [];
  715. var positions = [];
  716. var normals = [];
  717. var uvs = [];
  718. var row, col, tileRow, tileCol;
  719. subdivisions.h = (subdivisions.w < 1) ? 1 : subdivisions.h;
  720. subdivisions.w = (subdivisions.w < 1) ? 1 : subdivisions.w;
  721. precision.w = (precision.w < 1) ? 1 : precision.w;
  722. precision.h = (precision.h < 1) ? 1 : precision.h;
  723. var tileSize = {
  724. 'w': (xmax - xmin) / subdivisions.w,
  725. 'h': (zmax - zmin) / subdivisions.h
  726. };
  727. function applyTile(xTileMin, zTileMin, xTileMax, zTileMax) {
  728. // Indices
  729. var base = positions.length / 3;
  730. var rowLength = precision.w + 1;
  731. for (row = 0; row < precision.h; row++) {
  732. for (col = 0; col < precision.w; col++) {
  733. var square = [
  734. base + col + row * rowLength,
  735. base + (col + 1) + row * rowLength,
  736. base + (col + 1) + (row + 1) * rowLength,
  737. base + col + (row + 1) * rowLength
  738. ];
  739. indices.push(square[1]);
  740. indices.push(square[2]);
  741. indices.push(square[3]);
  742. indices.push(square[0]);
  743. indices.push(square[1]);
  744. indices.push(square[3]);
  745. }
  746. }
  747. // Position, normals and uvs
  748. var position = BABYLON.Vector3.Zero();
  749. var normal = new BABYLON.Vector3(0, 1.0, 0);
  750. for (row = 0; row <= precision.h; row++) {
  751. position.z = (row * (zTileMax - zTileMin)) / precision.h + zTileMin;
  752. for (col = 0; col <= precision.w; col++) {
  753. position.x = (col * (xTileMax - xTileMin)) / precision.w + xTileMin;
  754. position.y = 0;
  755. positions.push(position.x, position.y, position.z);
  756. normals.push(normal.x, normal.y, normal.z);
  757. uvs.push(col / precision.w, row / precision.h);
  758. }
  759. }
  760. }
  761. for (tileRow = 0; tileRow < subdivisions.h; tileRow++) {
  762. for (tileCol = 0; tileCol < subdivisions.w; tileCol++) {
  763. applyTile(xmin + tileCol * tileSize.w, zmin + tileRow * tileSize.h, xmin + (tileCol + 1) * tileSize.w, zmin + (tileRow + 1) * tileSize.h);
  764. }
  765. }
  766. // Result
  767. var vertexData = new VertexData();
  768. vertexData.indices = indices;
  769. vertexData.positions = positions;
  770. vertexData.normals = normals;
  771. vertexData.uvs = uvs;
  772. return vertexData;
  773. };
  774. VertexData.CreateGroundFromHeightMap = function (width, height, subdivisions, minHeight, maxHeight, buffer, bufferWidth, bufferHeight) {
  775. var indices = [];
  776. var positions = [];
  777. var normals = [];
  778. var uvs = [];
  779. var row, col;
  780. // Vertices
  781. for (row = 0; row <= subdivisions; row++) {
  782. for (col = 0; col <= subdivisions; col++) {
  783. var position = new BABYLON.Vector3((col * width) / subdivisions - (width / 2.0), 0, ((subdivisions - row) * height) / subdivisions - (height / 2.0));
  784. // Compute height
  785. var heightMapX = (((position.x + width / 2) / width) * (bufferWidth - 1)) | 0;
  786. var heightMapY = ((1.0 - (position.z + height / 2) / height) * (bufferHeight - 1)) | 0;
  787. var pos = (heightMapX + heightMapY * bufferWidth) * 4;
  788. var r = buffer[pos] / 255.0;
  789. var g = buffer[pos + 1] / 255.0;
  790. var b = buffer[pos + 2] / 255.0;
  791. var gradient = r * 0.3 + g * 0.59 + b * 0.11;
  792. position.y = minHeight + (maxHeight - minHeight) * gradient;
  793. // Add vertex
  794. positions.push(position.x, position.y, position.z);
  795. normals.push(0, 0, 0);
  796. uvs.push(col / subdivisions, 1.0 - row / subdivisions);
  797. }
  798. }
  799. // Indices
  800. for (row = 0; row < subdivisions; row++) {
  801. for (col = 0; col < subdivisions; col++) {
  802. indices.push(col + 1 + (row + 1) * (subdivisions + 1));
  803. indices.push(col + 1 + row * (subdivisions + 1));
  804. indices.push(col + row * (subdivisions + 1));
  805. indices.push(col + (row + 1) * (subdivisions + 1));
  806. indices.push(col + 1 + (row + 1) * (subdivisions + 1));
  807. indices.push(col + row * (subdivisions + 1));
  808. }
  809. }
  810. // Normals
  811. VertexData.ComputeNormals(positions, indices, normals);
  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.CreatePlane = function (size, sideOrientation) {
  821. if (sideOrientation === void 0) { sideOrientation = BABYLON.Mesh.DEFAULTSIDE; }
  822. var indices = [];
  823. var positions = [];
  824. var normals = [];
  825. var uvs = [];
  826. size = size || 1;
  827. // Vertices
  828. var halfSize = size / 2.0;
  829. positions.push(-halfSize, -halfSize, 0);
  830. normals.push(0, 0, -1.0);
  831. uvs.push(0.0, 0.0);
  832. positions.push(halfSize, -halfSize, 0);
  833. normals.push(0, 0, -1.0);
  834. uvs.push(1.0, 0.0);
  835. positions.push(halfSize, halfSize, 0);
  836. normals.push(0, 0, -1.0);
  837. uvs.push(1.0, 1.0);
  838. positions.push(-halfSize, halfSize, 0);
  839. normals.push(0, 0, -1.0);
  840. uvs.push(0.0, 1.0);
  841. // Indices
  842. indices.push(0);
  843. indices.push(1);
  844. indices.push(2);
  845. indices.push(0);
  846. indices.push(2);
  847. indices.push(3);
  848. // Sides
  849. VertexData._ComputeSides(sideOrientation, positions, indices, normals, uvs);
  850. // Result
  851. var vertexData = new VertexData();
  852. vertexData.indices = indices;
  853. vertexData.positions = positions;
  854. vertexData.normals = normals;
  855. vertexData.uvs = uvs;
  856. return vertexData;
  857. };
  858. VertexData.CreateDisc = function (radius, tessellation, sideOrientation) {
  859. if (sideOrientation === void 0) { sideOrientation = BABYLON.Mesh.DEFAULTSIDE; }
  860. var positions = [];
  861. var indices = [];
  862. var normals = [];
  863. var uvs = [];
  864. // positions and uvs
  865. positions.push(0, 0, 0); // disc center first
  866. uvs.push(0.5, 0.5);
  867. var step = Math.PI * 2 / tessellation;
  868. for (var a = 0; a < Math.PI * 2; a += step) {
  869. var x = Math.cos(a);
  870. var y = Math.sin(a);
  871. var u = (x + 1) / 2;
  872. var v = (1 - y) / 2;
  873. positions.push(radius * x, radius * y, 0);
  874. uvs.push(u, v);
  875. }
  876. positions.push(positions[3], positions[4], positions[5]); // close the circle
  877. uvs.push(uvs[2], uvs[3]);
  878. //indices
  879. var vertexNb = positions.length / 3;
  880. for (var i = 1; i < vertexNb - 1; i++) {
  881. indices.push(i + 1, 0, i);
  882. }
  883. // result
  884. VertexData.ComputeNormals(positions, indices, normals);
  885. VertexData._ComputeSides(sideOrientation, positions, indices, normals, uvs);
  886. var vertexData = new VertexData();
  887. vertexData.indices = indices;
  888. vertexData.positions = positions;
  889. vertexData.normals = normals;
  890. vertexData.uvs = uvs;
  891. return vertexData;
  892. };
  893. // based on http://code.google.com/p/away3d/source/browse/trunk/fp10/Away3D/src/away3d/primitives/TorusKnot.as?spec=svn2473&r=2473
  894. VertexData.CreateTorusKnot = function (radius, tube, radialSegments, tubularSegments, p, q, sideOrientation) {
  895. if (sideOrientation === void 0) { sideOrientation = BABYLON.Mesh.DEFAULTSIDE; }
  896. var indices = [];
  897. var positions = [];
  898. var normals = [];
  899. var uvs = [];
  900. radius = radius || 2;
  901. tube = tube || 0.5;
  902. radialSegments = radialSegments || 32;
  903. tubularSegments = tubularSegments || 32;
  904. p = p || 2;
  905. q = q || 3;
  906. // Helper
  907. var getPos = function (angle) {
  908. var cu = Math.cos(angle);
  909. var su = Math.sin(angle);
  910. var quOverP = q / p * angle;
  911. var cs = Math.cos(quOverP);
  912. var tx = radius * (2 + cs) * 0.5 * cu;
  913. var ty = radius * (2 + cs) * su * 0.5;
  914. var tz = radius * Math.sin(quOverP) * 0.5;
  915. return new BABYLON.Vector3(tx, ty, tz);
  916. };
  917. // Vertices
  918. for (var i = 0; i <= radialSegments; i++) {
  919. var modI = i % radialSegments;
  920. var u = modI / radialSegments * 2 * p * Math.PI;
  921. var p1 = getPos(u);
  922. var p2 = getPos(u + 0.01);
  923. var tang = p2.subtract(p1);
  924. var n = p2.add(p1);
  925. var bitan = BABYLON.Vector3.Cross(tang, n);
  926. n = BABYLON.Vector3.Cross(bitan, tang);
  927. bitan.normalize();
  928. n.normalize();
  929. for (var j = 0; j < tubularSegments; j++) {
  930. var modJ = j % tubularSegments;
  931. var v = modJ / tubularSegments * 2 * Math.PI;
  932. var cx = -tube * Math.cos(v);
  933. var cy = tube * Math.sin(v);
  934. positions.push(p1.x + cx * n.x + cy * bitan.x);
  935. positions.push(p1.y + cx * n.y + cy * bitan.y);
  936. positions.push(p1.z + cx * n.z + cy * bitan.z);
  937. uvs.push(i / radialSegments);
  938. uvs.push(j / tubularSegments);
  939. }
  940. }
  941. for (i = 0; i < radialSegments; i++) {
  942. for (j = 0; j < tubularSegments; j++) {
  943. var jNext = (j + 1) % tubularSegments;
  944. var a = i * tubularSegments + j;
  945. var b = (i + 1) * tubularSegments + j;
  946. var c = (i + 1) * tubularSegments + jNext;
  947. var d = i * tubularSegments + jNext;
  948. indices.push(d);
  949. indices.push(b);
  950. indices.push(a);
  951. indices.push(d);
  952. indices.push(c);
  953. indices.push(b);
  954. }
  955. }
  956. // Normals
  957. VertexData.ComputeNormals(positions, indices, normals);
  958. // Sides
  959. VertexData._ComputeSides(sideOrientation, positions, indices, normals, uvs);
  960. // Result
  961. var vertexData = new VertexData();
  962. vertexData.indices = indices;
  963. vertexData.positions = positions;
  964. vertexData.normals = normals;
  965. vertexData.uvs = uvs;
  966. return vertexData;
  967. };
  968. // Tools
  969. /**
  970. * @param {any} - positions (number[] or Float32Array)
  971. * @param {any} - indices (number[] or Uint16Array)
  972. * @param {any} - normals (number[] or Float32Array)
  973. */
  974. VertexData.ComputeNormals = function (positions, indices, normals) {
  975. var index = 0;
  976. // temp Vector3
  977. var p1 = BABYLON.Vector3.Zero();
  978. var p2 = BABYLON.Vector3.Zero();
  979. var p3 = BABYLON.Vector3.Zero();
  980. var p1p2 = BABYLON.Vector3.Zero();
  981. var p3p2 = BABYLON.Vector3.Zero();
  982. var faceNormal = BABYLON.Vector3.Zero();
  983. var vertexNormali1 = BABYLON.Vector3.Zero();
  984. var vertexNormali2 = BABYLON.Vector3.Zero();
  985. var vertexNormali3 = BABYLON.Vector3.Zero();
  986. // indice triplet = 1 face
  987. var nbFaces = indices.length / 3;
  988. for (index = 0; index < nbFaces; index++) {
  989. var i1 = indices[index * 3];
  990. var i2 = indices[index * 3 + 1];
  991. var i3 = indices[index * 3 + 2];
  992. // setting the temp V3
  993. BABYLON.Vector3.FromFloatsToRef(positions[i1 * 3], positions[i1 * 3 + 1], positions[i1 * 3 + 2], p1);
  994. BABYLON.Vector3.FromFloatsToRef(positions[i2 * 3], positions[i2 * 3 + 1], positions[i2 * 3 + 2], p2);
  995. BABYLON.Vector3.FromFloatsToRef(positions[i3 * 3], positions[i3 * 3 + 1], positions[i3 * 3 + 2], p3);
  996. p1.subtractToRef(p2, p1p2);
  997. p3.subtractToRef(p2, p3p2);
  998. BABYLON.Vector3.CrossToRef(p1p2, p3p2, faceNormal);
  999. faceNormal.normalize();
  1000. // All intermediate results are stored in the normals array :
  1001. // get the normals at i1, i2 and i3 indexes
  1002. normals[i1 * 3] = normals[i1 * 3] || 0.0;
  1003. normals[i1 * 3 + 1] = normals[i1 * 3 + 1] || 0.0;
  1004. normals[i1 * 3 + 2] = normals[i1 * 3 + 2] || 0.0;
  1005. normals[i2 * 3] = normals[i2 * 3] || 0.0;
  1006. normals[i2 * 3 + 1] = normals[i2 * 3 + 1] || 0.0;
  1007. normals[i2 * 3 + 2] = normals[i2 * 3 + 2] || 0.0;
  1008. normals[i3 * 3] = normals[i3 * 3] || 0.0;
  1009. normals[i3 * 3 + 1] = normals[i3 * 3 + 1] || 0.0;
  1010. normals[i3 * 3 + 2] = normals[i3 * 3 + 2] || 0.0;
  1011. // make intermediate vectors3 from normals values
  1012. BABYLON.Vector3.FromFloatsToRef(normals[i1 * 3], normals[i1 * 3 + 1], normals[i1 * 3 + 2], vertexNormali1);
  1013. BABYLON.Vector3.FromFloatsToRef(normals[i2 * 3], normals[i2 * 3 + 1], normals[i2 * 3 + 2], vertexNormali2);
  1014. BABYLON.Vector3.FromFloatsToRef(normals[i3 * 3], normals[i3 * 3 + 1], normals[i3 * 3 + 2], vertexNormali3);
  1015. // add the current face normals to these intermediate vectors3
  1016. vertexNormali1 = vertexNormali1.addInPlace(faceNormal);
  1017. vertexNormali2 = vertexNormali2.addInPlace(faceNormal);
  1018. vertexNormali3 = vertexNormali3.addInPlace(faceNormal);
  1019. // store back intermediate vectors3 into the normals array
  1020. normals[i1 * 3] = vertexNormali1.x;
  1021. normals[i1 * 3 + 1] = vertexNormali1.y;
  1022. normals[i1 * 3 + 2] = vertexNormali1.z;
  1023. normals[i2 * 3] = vertexNormali2.x;
  1024. normals[i2 * 3 + 1] = vertexNormali2.y;
  1025. normals[i2 * 3 + 2] = vertexNormali2.z;
  1026. normals[i3 * 3] = vertexNormali3.x;
  1027. normals[i3 * 3 + 1] = vertexNormali3.y;
  1028. normals[i3 * 3 + 2] = vertexNormali3.z;
  1029. }
  1030. // last normalization
  1031. for (index = 0; index < normals.length / 3; index++) {
  1032. BABYLON.Vector3.FromFloatsToRef(normals[index * 3], normals[index * 3 + 1], normals[index * 3 + 2], vertexNormali1);
  1033. vertexNormali1.normalize();
  1034. normals[index * 3] = vertexNormali1.x;
  1035. normals[index * 3 + 1] = vertexNormali1.y;
  1036. normals[index * 3 + 2] = vertexNormali1.z;
  1037. }
  1038. };
  1039. VertexData._ComputeSides = function (sideOrientation, positions, indices, normals, uvs) {
  1040. var li = indices.length;
  1041. var ln = normals.length;
  1042. var i;
  1043. var n;
  1044. sideOrientation = sideOrientation || BABYLON.Mesh.DEFAULTSIDE;
  1045. switch (sideOrientation) {
  1046. case BABYLON.Mesh.FRONTSIDE:
  1047. // nothing changed
  1048. break;
  1049. case BABYLON.Mesh.BACKSIDE:
  1050. var tmp;
  1051. // indices
  1052. for (i = 0; i < li; i += 3) {
  1053. tmp = indices[i];
  1054. indices[i] = indices[i + 2];
  1055. indices[i + 2] = tmp;
  1056. }
  1057. // normals
  1058. for (n = 0; n < ln; n++) {
  1059. normals[n] = -normals[n];
  1060. }
  1061. break;
  1062. case BABYLON.Mesh.DOUBLESIDE:
  1063. // positions
  1064. var lp = positions.length;
  1065. var l = lp / 3;
  1066. for (var p = 0; p < lp; p++) {
  1067. positions[lp + p] = positions[p];
  1068. }
  1069. // indices
  1070. for (i = 0; i < li; i += 3) {
  1071. indices[i + li] = indices[i + 2] + l;
  1072. indices[i + 1 + li] = indices[i + 1] + l;
  1073. indices[i + 2 + li] = indices[i] + l;
  1074. }
  1075. // normals
  1076. for (n = 0; n < ln; n++) {
  1077. normals[ln + n] = -normals[n];
  1078. }
  1079. // uvs
  1080. var lu = uvs.length;
  1081. for (var u = 0; u < lu; u++) {
  1082. uvs[u + lu] = uvs[u];
  1083. }
  1084. break;
  1085. }
  1086. };
  1087. return VertexData;
  1088. })();
  1089. BABYLON.VertexData = VertexData;
  1090. })(BABYLON || (BABYLON = {}));
  1091. //# sourceMappingURL=babylon.mesh.vertexData.js.map