babylon.mesh.vertexData.js 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076
  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. for (i = 0; i < minlg; i++) {
  279. vTotalDistance[i] = 0;
  280. vs[i] = [0];
  281. var path1;
  282. var path2;
  283. for (p = 0; p < pathArray.length - 1; p++) {
  284. path1 = pathArray[p];
  285. path2 = pathArray[p + 1];
  286. vectlg = path2[i].subtract(path1[i]).length();
  287. dist = vectlg + vTotalDistance[i];
  288. vs[i].push(dist);
  289. vTotalDistance[i] = dist;
  290. }
  291. if (closeArray) {
  292. path1 = pathArray[p];
  293. path2 = pathArray[0];
  294. vectlg = path2[i].subtract(path1[i]).length();
  295. dist = vectlg + vTotalDistance[i];
  296. vTotalDistance[i] = dist;
  297. }
  298. }
  299. // uvs
  300. var u;
  301. var v;
  302. for (p = 0; p < pathArray.length; p++) {
  303. for (i = 0; i < minlg; i++) {
  304. u = us[p][i] / uTotalDistance[p];
  305. v = vs[i][p] / vTotalDistance[i];
  306. uvs.push(u, v);
  307. }
  308. }
  309. // indices
  310. p = 0; // path index
  311. var pi = 0; // positions array index
  312. var l1 = lg[p] - 1; // path1 length
  313. var l2 = lg[p + 1] - 1; // path2 length
  314. var min = (l1 < l2) ? l1 : l2; // current path stop index
  315. var shft = idx[1] - idx[0]; // shift
  316. var path1nb = closeArray ? lg.length : lg.length - 1; // number of path1 to iterate
  317. var t1; // two consecutive triangles, so 4 points : point1
  318. var t2; // point2
  319. var t3; // point3
  320. var t4; // point4
  321. while (pi <= min && p < path1nb) {
  322. // 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
  323. t1 = pi;
  324. t2 = pi + shft;
  325. t3 = pi + 1;
  326. t4 = pi + shft + 1;
  327. indices.push(pi, pi + shft, pi + 1);
  328. indices.push(pi + shft + 1, pi + 1, pi + shft);
  329. pi += 1;
  330. if (pi === min) {
  331. if (closePath) {
  332. indices.push(pi, pi + shft, idx[p]);
  333. indices.push(idx[p] + shft, idx[p], pi + shft);
  334. t3 = idx[p];
  335. t4 = idx[p] + shft;
  336. }
  337. p++;
  338. if (p === lg.length - 1) {
  339. shft = idx[0] - idx[p];
  340. l1 = lg[p] - 1;
  341. l2 = lg[0] - 1;
  342. }
  343. else {
  344. shft = idx[p + 1] - idx[p];
  345. l1 = lg[p] - 1;
  346. l2 = lg[p + 1] - 1;
  347. }
  348. pi = idx[p];
  349. min = (l1 < l2) ? l1 + pi : l2 + pi;
  350. }
  351. }
  352. // normals
  353. VertexData.ComputeNormals(positions, indices, normals);
  354. // sides
  355. VertexData._ComputeSides(sideOrientation, positions, indices, normals, uvs);
  356. // Result
  357. var vertexData = new VertexData();
  358. vertexData.indices = indices;
  359. vertexData.positions = positions;
  360. vertexData.normals = normals;
  361. vertexData.uvs = uvs;
  362. return vertexData;
  363. };
  364. VertexData.CreateBox = function (size, sideOrientation) {
  365. if (sideOrientation === void 0) { sideOrientation = BABYLON.Mesh.DEFAULTSIDE; }
  366. var normalsSource = [
  367. new BABYLON.Vector3(0, 0, 1),
  368. new BABYLON.Vector3(0, 0, -1),
  369. new BABYLON.Vector3(1, 0, 0),
  370. new BABYLON.Vector3(-1, 0, 0),
  371. new BABYLON.Vector3(0, 1, 0),
  372. new BABYLON.Vector3(0, -1, 0)
  373. ];
  374. var indices = [];
  375. var positions = [];
  376. var normals = [];
  377. var uvs = [];
  378. size = size || 1;
  379. for (var index = 0; index < normalsSource.length; index++) {
  380. var normal = normalsSource[index];
  381. // Get two vectors perpendicular to the face normal and to each other.
  382. var side1 = new BABYLON.Vector3(normal.y, normal.z, normal.x);
  383. var side2 = BABYLON.Vector3.Cross(normal, side1);
  384. // Six indices (two triangles) per face.
  385. var verticesLength = positions.length / 3;
  386. indices.push(verticesLength);
  387. indices.push(verticesLength + 1);
  388. indices.push(verticesLength + 2);
  389. indices.push(verticesLength);
  390. indices.push(verticesLength + 2);
  391. indices.push(verticesLength + 3);
  392. // Four vertices per face.
  393. var vertex = normal.subtract(side1).subtract(side2).scale(size / 2);
  394. positions.push(vertex.x, vertex.y, vertex.z);
  395. normals.push(normal.x, normal.y, normal.z);
  396. uvs.push(1.0, 1.0);
  397. vertex = normal.subtract(side1).add(side2).scale(size / 2);
  398. positions.push(vertex.x, vertex.y, vertex.z);
  399. normals.push(normal.x, normal.y, normal.z);
  400. uvs.push(0.0, 1.0);
  401. vertex = normal.add(side1).add(side2).scale(size / 2);
  402. positions.push(vertex.x, vertex.y, vertex.z);
  403. normals.push(normal.x, normal.y, normal.z);
  404. uvs.push(0.0, 0.0);
  405. vertex = normal.add(side1).subtract(side2).scale(size / 2);
  406. positions.push(vertex.x, vertex.y, vertex.z);
  407. normals.push(normal.x, normal.y, normal.z);
  408. uvs.push(1.0, 0.0);
  409. }
  410. // sides
  411. VertexData._ComputeSides(sideOrientation, positions, indices, normals, uvs);
  412. // Result
  413. var vertexData = new VertexData();
  414. vertexData.indices = indices;
  415. vertexData.positions = positions;
  416. vertexData.normals = normals;
  417. vertexData.uvs = uvs;
  418. return vertexData;
  419. };
  420. VertexData.CreateSphere = function (segments, diameter, sideOrientation) {
  421. if (sideOrientation === void 0) { sideOrientation = BABYLON.Mesh.DEFAULTSIDE; }
  422. segments = segments || 32;
  423. diameter = diameter || 1;
  424. var radius = diameter / 2;
  425. var totalZRotationSteps = 2 + segments;
  426. var totalYRotationSteps = 2 * totalZRotationSteps;
  427. var indices = [];
  428. var positions = [];
  429. var normals = [];
  430. var uvs = [];
  431. for (var zRotationStep = 0; zRotationStep <= totalZRotationSteps; zRotationStep++) {
  432. var normalizedZ = zRotationStep / totalZRotationSteps;
  433. var angleZ = (normalizedZ * Math.PI);
  434. for (var yRotationStep = 0; yRotationStep <= totalYRotationSteps; yRotationStep++) {
  435. var normalizedY = yRotationStep / totalYRotationSteps;
  436. var angleY = normalizedY * Math.PI * 2;
  437. var rotationZ = BABYLON.Matrix.RotationZ(-angleZ);
  438. var rotationY = BABYLON.Matrix.RotationY(angleY);
  439. var afterRotZ = BABYLON.Vector3.TransformCoordinates(BABYLON.Vector3.Up(), rotationZ);
  440. var complete = BABYLON.Vector3.TransformCoordinates(afterRotZ, rotationY);
  441. var vertex = complete.scale(radius);
  442. var normal = BABYLON.Vector3.Normalize(vertex);
  443. positions.push(vertex.x, vertex.y, vertex.z);
  444. normals.push(normal.x, normal.y, normal.z);
  445. uvs.push(normalizedZ, normalizedY);
  446. }
  447. if (zRotationStep > 0) {
  448. var verticesCount = positions.length / 3;
  449. for (var firstIndex = verticesCount - 2 * (totalYRotationSteps + 1); (firstIndex + totalYRotationSteps + 2) < verticesCount; firstIndex++) {
  450. indices.push((firstIndex));
  451. indices.push((firstIndex + 1));
  452. indices.push(firstIndex + totalYRotationSteps + 1);
  453. indices.push((firstIndex + totalYRotationSteps + 1));
  454. indices.push((firstIndex + 1));
  455. indices.push((firstIndex + totalYRotationSteps + 2));
  456. }
  457. }
  458. }
  459. // Sides
  460. VertexData._ComputeSides(sideOrientation, positions, indices, normals, uvs);
  461. // Result
  462. var vertexData = new VertexData();
  463. vertexData.indices = indices;
  464. vertexData.positions = positions;
  465. vertexData.normals = normals;
  466. vertexData.uvs = uvs;
  467. return vertexData;
  468. };
  469. VertexData.CreateCylinder = function (height, diameterTop, diameterBottom, tessellation, subdivisions, sideOrientation) {
  470. if (subdivisions === void 0) { subdivisions = 1; }
  471. if (sideOrientation === void 0) { sideOrientation = BABYLON.Mesh.DEFAULTSIDE; }
  472. var radiusTop = diameterTop / 2;
  473. var radiusBottom = diameterBottom / 2;
  474. var indices = [];
  475. var positions = [];
  476. var normals = [];
  477. var uvs = [];
  478. height = height || 1;
  479. diameterTop = diameterTop || 0.5;
  480. diameterBottom = diameterBottom || 1;
  481. tessellation = tessellation || 16;
  482. subdivisions = subdivisions || 1;
  483. subdivisions = (subdivisions < 1) ? 1 : subdivisions;
  484. var getCircleVector = function (i) {
  485. var angle = (i * 2.0 * Math.PI / tessellation);
  486. var dx = Math.cos(angle);
  487. var dz = Math.sin(angle);
  488. return new BABYLON.Vector3(dx, 0, dz);
  489. };
  490. var createCylinderCap = function (isTop) {
  491. var radius = isTop ? radiusTop : radiusBottom;
  492. if (radius === 0) {
  493. return;
  494. }
  495. var vbase = positions.length / 3;
  496. var offset = new BABYLON.Vector3(0, height / 2, 0);
  497. var textureScale = new BABYLON.Vector2(0.5, 0.5);
  498. if (!isTop) {
  499. offset.scaleInPlace(-1);
  500. textureScale.x = -textureScale.x;
  501. }
  502. for (var i = 0; i < tessellation; i++) {
  503. var circleVector = getCircleVector(i);
  504. var position = circleVector.scale(radius).add(offset);
  505. var textureCoordinate = new BABYLON.Vector2(circleVector.x * textureScale.x + 0.5, circleVector.z * textureScale.y + 0.5);
  506. positions.push(position.x, position.y, position.z);
  507. uvs.push(textureCoordinate.x, textureCoordinate.y);
  508. }
  509. for (i = 0; i < tessellation - 2; i++) {
  510. if (!isTop) {
  511. indices.push(vbase);
  512. indices.push(vbase + (i + 2) % tessellation);
  513. indices.push(vbase + (i + 1) % tessellation);
  514. }
  515. else {
  516. indices.push(vbase);
  517. indices.push(vbase + (i + 1) % tessellation);
  518. indices.push(vbase + (i + 2) % tessellation);
  519. }
  520. }
  521. };
  522. var base = new BABYLON.Vector3(0, -1, 0).scale(height / 2);
  523. var offset = new BABYLON.Vector3(0, 1, 0).scale(height / subdivisions);
  524. var stride = tessellation + 1;
  525. for (var i = 0; i <= tessellation; i++) {
  526. var circleVector = getCircleVector(i);
  527. var textureCoordinate = new BABYLON.Vector2(i / tessellation, 0);
  528. var position, radius = radiusBottom;
  529. for (var s = 0; s <= subdivisions; s++) {
  530. // Update variables
  531. position = circleVector.scale(radius);
  532. position.addInPlace(base.add(offset.scale(s)));
  533. textureCoordinate.y += 1 / subdivisions;
  534. radius += (radiusTop - radiusBottom) / subdivisions;
  535. // Push in arrays
  536. positions.push(position.x, position.y, position.z);
  537. uvs.push(textureCoordinate.x, textureCoordinate.y);
  538. }
  539. }
  540. subdivisions += 1;
  541. for (s = 0; s < subdivisions - 1; s++) {
  542. for (i = 0; i <= tessellation; i++) {
  543. indices.push(i * subdivisions + s);
  544. indices.push((i * subdivisions + (s + subdivisions)) % (stride * subdivisions));
  545. indices.push(i * subdivisions + (s + 1));
  546. indices.push(i * subdivisions + (s + 1));
  547. indices.push((i * subdivisions + (s + subdivisions)) % (stride * subdivisions));
  548. indices.push((i * subdivisions + (s + subdivisions + 1)) % (stride * subdivisions));
  549. }
  550. }
  551. // Create flat triangle fan caps to seal the top and bottom.
  552. createCylinderCap(true);
  553. createCylinderCap(false);
  554. // Normals
  555. VertexData.ComputeNormals(positions, indices, normals);
  556. // Sides
  557. VertexData._ComputeSides(sideOrientation, positions, indices, normals, uvs);
  558. // Result
  559. var vertexData = new VertexData();
  560. vertexData.indices = indices;
  561. vertexData.positions = positions;
  562. vertexData.normals = normals;
  563. vertexData.uvs = uvs;
  564. return vertexData;
  565. };
  566. VertexData.CreateTorus = function (diameter, thickness, tessellation, sideOrientation) {
  567. if (sideOrientation === void 0) { sideOrientation = BABYLON.Mesh.DEFAULTSIDE; }
  568. var indices = [];
  569. var positions = [];
  570. var normals = [];
  571. var uvs = [];
  572. diameter = diameter || 1;
  573. thickness = thickness || 0.5;
  574. tessellation = tessellation || 16;
  575. var stride = tessellation + 1;
  576. for (var i = 0; i <= tessellation; i++) {
  577. var u = i / tessellation;
  578. var outerAngle = i * Math.PI * 2.0 / tessellation - Math.PI / 2.0;
  579. var transform = BABYLON.Matrix.Translation(diameter / 2.0, 0, 0).multiply(BABYLON.Matrix.RotationY(outerAngle));
  580. for (var j = 0; j <= tessellation; j++) {
  581. var v = 1 - j / tessellation;
  582. var innerAngle = j * Math.PI * 2.0 / tessellation + Math.PI;
  583. var dx = Math.cos(innerAngle);
  584. var dy = Math.sin(innerAngle);
  585. // Create a vertex.
  586. var normal = new BABYLON.Vector3(dx, dy, 0);
  587. var position = normal.scale(thickness / 2);
  588. var textureCoordinate = new BABYLON.Vector2(u, v);
  589. position = BABYLON.Vector3.TransformCoordinates(position, transform);
  590. normal = BABYLON.Vector3.TransformNormal(normal, transform);
  591. positions.push(position.x, position.y, position.z);
  592. normals.push(normal.x, normal.y, normal.z);
  593. uvs.push(textureCoordinate.x, textureCoordinate.y);
  594. // And create indices for two triangles.
  595. var nextI = (i + 1) % stride;
  596. var nextJ = (j + 1) % stride;
  597. indices.push(i * stride + j);
  598. indices.push(i * stride + nextJ);
  599. indices.push(nextI * stride + j);
  600. indices.push(i * stride + nextJ);
  601. indices.push(nextI * stride + nextJ);
  602. indices.push(nextI * stride + j);
  603. }
  604. }
  605. // Sides
  606. VertexData._ComputeSides(sideOrientation, positions, indices, normals, uvs);
  607. // Result
  608. var vertexData = new VertexData();
  609. vertexData.indices = indices;
  610. vertexData.positions = positions;
  611. vertexData.normals = normals;
  612. vertexData.uvs = uvs;
  613. return vertexData;
  614. };
  615. VertexData.CreateLines = function (points) {
  616. var indices = [];
  617. var positions = [];
  618. for (var index = 0; index < points.length; index++) {
  619. positions.push(points[index].x, points[index].y, points[index].z);
  620. if (index > 0) {
  621. indices.push(index - 1);
  622. indices.push(index);
  623. }
  624. }
  625. // Result
  626. var vertexData = new VertexData();
  627. vertexData.indices = indices;
  628. vertexData.positions = positions;
  629. return vertexData;
  630. };
  631. VertexData.CreateDashedLines = function (points, dashSize, gapSize, dashNb) {
  632. dashSize = dashSize || 3;
  633. gapSize = gapSize || 1;
  634. dashNb = dashNb || 200;
  635. var positions = new Array();
  636. var indices = new Array();
  637. var curvect = BABYLON.Vector3.Zero();
  638. var lg = 0;
  639. var nb = 0;
  640. var shft = 0;
  641. var dashshft = 0;
  642. var curshft = 0;
  643. var idx = 0;
  644. var i = 0;
  645. for (i = 0; i < points.length - 1; i++) {
  646. points[i + 1].subtractToRef(points[i], curvect);
  647. lg += curvect.length();
  648. }
  649. shft = lg / dashNb;
  650. dashshft = dashSize * shft / (dashSize + gapSize);
  651. for (i = 0; i < points.length - 1; i++) {
  652. points[i + 1].subtractToRef(points[i], curvect);
  653. nb = Math.floor(curvect.length() / shft);
  654. curvect.normalize();
  655. for (var j = 0; j < nb; j++) {
  656. curshft = shft * j;
  657. positions.push(points[i].x + curshft * curvect.x, points[i].y + curshft * curvect.y, points[i].z + curshft * curvect.z);
  658. positions.push(points[i].x + (curshft + dashshft) * curvect.x, points[i].y + (curshft + dashshft) * curvect.y, points[i].z + (curshft + dashshft) * curvect.z);
  659. indices.push(idx, idx + 1);
  660. idx += 2;
  661. }
  662. }
  663. // Result
  664. var vertexData = new VertexData();
  665. vertexData.positions = positions;
  666. vertexData.indices = indices;
  667. return vertexData;
  668. };
  669. VertexData.CreateGround = function (width, height, subdivisions) {
  670. var indices = [];
  671. var positions = [];
  672. var normals = [];
  673. var uvs = [];
  674. var row, col;
  675. width = width || 1;
  676. height = height || 1;
  677. subdivisions = subdivisions || 1;
  678. for (row = 0; row <= subdivisions; row++) {
  679. for (col = 0; col <= subdivisions; col++) {
  680. var position = new BABYLON.Vector3((col * width) / subdivisions - (width / 2.0), 0, ((subdivisions - row) * height) / subdivisions - (height / 2.0));
  681. var normal = new BABYLON.Vector3(0, 1.0, 0);
  682. positions.push(position.x, position.y, position.z);
  683. normals.push(normal.x, normal.y, normal.z);
  684. uvs.push(col / subdivisions, 1.0 - row / subdivisions);
  685. }
  686. }
  687. for (row = 0; row < subdivisions; row++) {
  688. for (col = 0; col < subdivisions; col++) {
  689. indices.push(col + 1 + (row + 1) * (subdivisions + 1));
  690. indices.push(col + 1 + row * (subdivisions + 1));
  691. indices.push(col + row * (subdivisions + 1));
  692. indices.push(col + (row + 1) * (subdivisions + 1));
  693. indices.push(col + 1 + (row + 1) * (subdivisions + 1));
  694. indices.push(col + row * (subdivisions + 1));
  695. }
  696. }
  697. // Result
  698. var vertexData = new VertexData();
  699. vertexData.indices = indices;
  700. vertexData.positions = positions;
  701. vertexData.normals = normals;
  702. vertexData.uvs = uvs;
  703. return vertexData;
  704. };
  705. VertexData.CreateTiledGround = function (xmin, zmin, xmax, zmax, subdivisions, precision) {
  706. if (subdivisions === void 0) { subdivisions = { w: 1, h: 1 }; }
  707. if (precision === void 0) { precision = { w: 1, h: 1 }; }
  708. var indices = [];
  709. var positions = [];
  710. var normals = [];
  711. var uvs = [];
  712. var row, col, tileRow, tileCol;
  713. subdivisions.h = (subdivisions.w < 1) ? 1 : subdivisions.h;
  714. subdivisions.w = (subdivisions.w < 1) ? 1 : subdivisions.w;
  715. precision.w = (precision.w < 1) ? 1 : precision.w;
  716. precision.h = (precision.h < 1) ? 1 : precision.h;
  717. var tileSize = {
  718. 'w': (xmax - xmin) / subdivisions.w,
  719. 'h': (zmax - zmin) / subdivisions.h
  720. };
  721. function applyTile(xTileMin, zTileMin, xTileMax, zTileMax) {
  722. // Indices
  723. var base = positions.length / 3;
  724. var rowLength = precision.w + 1;
  725. for (row = 0; row < precision.h; row++) {
  726. for (col = 0; col < precision.w; col++) {
  727. var square = [
  728. base + col + row * rowLength,
  729. base + (col + 1) + row * rowLength,
  730. base + (col + 1) + (row + 1) * rowLength,
  731. base + col + (row + 1) * rowLength
  732. ];
  733. indices.push(square[1]);
  734. indices.push(square[2]);
  735. indices.push(square[3]);
  736. indices.push(square[0]);
  737. indices.push(square[1]);
  738. indices.push(square[3]);
  739. }
  740. }
  741. // Position, normals and uvs
  742. var position = BABYLON.Vector3.Zero();
  743. var normal = new BABYLON.Vector3(0, 1.0, 0);
  744. for (row = 0; row <= precision.h; row++) {
  745. position.z = (row * (zTileMax - zTileMin)) / precision.h + zTileMin;
  746. for (col = 0; col <= precision.w; col++) {
  747. position.x = (col * (xTileMax - xTileMin)) / precision.w + xTileMin;
  748. position.y = 0;
  749. positions.push(position.x, position.y, position.z);
  750. normals.push(normal.x, normal.y, normal.z);
  751. uvs.push(col / precision.w, row / precision.h);
  752. }
  753. }
  754. }
  755. for (tileRow = 0; tileRow < subdivisions.h; tileRow++) {
  756. for (tileCol = 0; tileCol < subdivisions.w; tileCol++) {
  757. applyTile(xmin + tileCol * tileSize.w, zmin + tileRow * tileSize.h, xmin + (tileCol + 1) * tileSize.w, zmin + (tileRow + 1) * tileSize.h);
  758. }
  759. }
  760. // Result
  761. var vertexData = new VertexData();
  762. vertexData.indices = indices;
  763. vertexData.positions = positions;
  764. vertexData.normals = normals;
  765. vertexData.uvs = uvs;
  766. return vertexData;
  767. };
  768. VertexData.CreateGroundFromHeightMap = function (width, height, subdivisions, minHeight, maxHeight, buffer, bufferWidth, bufferHeight) {
  769. var indices = [];
  770. var positions = [];
  771. var normals = [];
  772. var uvs = [];
  773. var row, col;
  774. for (row = 0; row <= subdivisions; row++) {
  775. for (col = 0; col <= subdivisions; col++) {
  776. var position = new BABYLON.Vector3((col * width) / subdivisions - (width / 2.0), 0, ((subdivisions - row) * height) / subdivisions - (height / 2.0));
  777. // Compute height
  778. var heightMapX = (((position.x + width / 2) / width) * (bufferWidth - 1)) | 0;
  779. var heightMapY = ((1.0 - (position.z + height / 2) / height) * (bufferHeight - 1)) | 0;
  780. var pos = (heightMapX + heightMapY * bufferWidth) * 4;
  781. var r = buffer[pos] / 255.0;
  782. var g = buffer[pos + 1] / 255.0;
  783. var b = buffer[pos + 2] / 255.0;
  784. var gradient = r * 0.3 + g * 0.59 + b * 0.11;
  785. position.y = minHeight + (maxHeight - minHeight) * gradient;
  786. // Add vertex
  787. positions.push(position.x, position.y, position.z);
  788. normals.push(0, 0, 0);
  789. uvs.push(col / subdivisions, 1.0 - row / subdivisions);
  790. }
  791. }
  792. for (row = 0; row < subdivisions; row++) {
  793. for (col = 0; col < subdivisions; col++) {
  794. indices.push(col + 1 + (row + 1) * (subdivisions + 1));
  795. indices.push(col + 1 + row * (subdivisions + 1));
  796. indices.push(col + row * (subdivisions + 1));
  797. indices.push(col + (row + 1) * (subdivisions + 1));
  798. indices.push(col + 1 + (row + 1) * (subdivisions + 1));
  799. indices.push(col + row * (subdivisions + 1));
  800. }
  801. }
  802. // Normals
  803. VertexData.ComputeNormals(positions, indices, normals);
  804. // Result
  805. var vertexData = new VertexData();
  806. vertexData.indices = indices;
  807. vertexData.positions = positions;
  808. vertexData.normals = normals;
  809. vertexData.uvs = uvs;
  810. return vertexData;
  811. };
  812. VertexData.CreatePlane = function (size, sideOrientation) {
  813. if (sideOrientation === void 0) { sideOrientation = BABYLON.Mesh.DEFAULTSIDE; }
  814. var indices = [];
  815. var positions = [];
  816. var normals = [];
  817. var uvs = [];
  818. size = size || 1;
  819. // Vertices
  820. var halfSize = size / 2.0;
  821. positions.push(-halfSize, -halfSize, 0);
  822. normals.push(0, 0, -1.0);
  823. uvs.push(0.0, 0.0);
  824. positions.push(halfSize, -halfSize, 0);
  825. normals.push(0, 0, -1.0);
  826. uvs.push(1.0, 0.0);
  827. positions.push(halfSize, halfSize, 0);
  828. normals.push(0, 0, -1.0);
  829. uvs.push(1.0, 1.0);
  830. positions.push(-halfSize, halfSize, 0);
  831. normals.push(0, 0, -1.0);
  832. uvs.push(0.0, 1.0);
  833. // Indices
  834. indices.push(0);
  835. indices.push(1);
  836. indices.push(2);
  837. indices.push(0);
  838. indices.push(2);
  839. indices.push(3);
  840. // Sides
  841. VertexData._ComputeSides(sideOrientation, positions, indices, normals, uvs);
  842. // Result
  843. var vertexData = new VertexData();
  844. vertexData.indices = indices;
  845. vertexData.positions = positions;
  846. vertexData.normals = normals;
  847. vertexData.uvs = uvs;
  848. return vertexData;
  849. };
  850. VertexData.CreateDisc = function (radius, tessellation, sideOrientation) {
  851. if (sideOrientation === void 0) { sideOrientation = BABYLON.Mesh.DEFAULTSIDE; }
  852. var positions = [];
  853. var indices = [];
  854. var normals = [];
  855. var uvs = [];
  856. // positions and uvs
  857. positions.push(0, 0, 0); // disc center first
  858. uvs.push(0.5, 0.5);
  859. var step = Math.PI * 2 / tessellation;
  860. for (var a = 0; a < Math.PI * 2; a += step) {
  861. var x = Math.cos(a);
  862. var y = Math.sin(a);
  863. var u = (x + 1) / 2;
  864. var v = (1 - y) / 2;
  865. positions.push(radius * x, radius * y, 0);
  866. uvs.push(u, v);
  867. }
  868. positions.push(positions[3], positions[4], positions[5]); // close the circle
  869. uvs.push(uvs[2], uvs[3]);
  870. //indices
  871. var vertexNb = positions.length / 3;
  872. for (var i = 1; i < vertexNb - 1; i++) {
  873. indices.push(i + 1, 0, i);
  874. }
  875. // result
  876. VertexData.ComputeNormals(positions, indices, normals);
  877. VertexData._ComputeSides(sideOrientation, positions, indices, normals, uvs);
  878. var vertexData = new VertexData();
  879. vertexData.indices = indices;
  880. vertexData.positions = positions;
  881. vertexData.normals = normals;
  882. vertexData.uvs = uvs;
  883. return vertexData;
  884. };
  885. // based on http://code.google.com/p/away3d/source/browse/trunk/fp10/Away3D/src/away3d/primitives/TorusKnot.as?spec=svn2473&r=2473
  886. VertexData.CreateTorusKnot = function (radius, tube, radialSegments, tubularSegments, p, q, sideOrientation) {
  887. if (sideOrientation === void 0) { sideOrientation = BABYLON.Mesh.DEFAULTSIDE; }
  888. var indices = [];
  889. var positions = [];
  890. var normals = [];
  891. var uvs = [];
  892. radius = radius || 2;
  893. tube = tube || 0.5;
  894. radialSegments = radialSegments || 32;
  895. tubularSegments = tubularSegments || 32;
  896. p = p || 2;
  897. q = q || 3;
  898. // Helper
  899. var getPos = function (angle) {
  900. var cu = Math.cos(angle);
  901. var su = Math.sin(angle);
  902. var quOverP = q / p * angle;
  903. var cs = Math.cos(quOverP);
  904. var tx = radius * (2 + cs) * 0.5 * cu;
  905. var ty = radius * (2 + cs) * su * 0.5;
  906. var tz = radius * Math.sin(quOverP) * 0.5;
  907. return new BABYLON.Vector3(tx, ty, tz);
  908. };
  909. for (var i = 0; i <= radialSegments; i++) {
  910. var modI = i % radialSegments;
  911. var u = modI / radialSegments * 2 * p * Math.PI;
  912. var p1 = getPos(u);
  913. var p2 = getPos(u + 0.01);
  914. var tang = p2.subtract(p1);
  915. var n = p2.add(p1);
  916. var bitan = BABYLON.Vector3.Cross(tang, n);
  917. n = BABYLON.Vector3.Cross(bitan, tang);
  918. bitan.normalize();
  919. n.normalize();
  920. for (var j = 0; j < tubularSegments; j++) {
  921. var modJ = j % tubularSegments;
  922. var v = modJ / tubularSegments * 2 * Math.PI;
  923. var cx = -tube * Math.cos(v);
  924. var cy = tube * Math.sin(v);
  925. positions.push(p1.x + cx * n.x + cy * bitan.x);
  926. positions.push(p1.y + cx * n.y + cy * bitan.y);
  927. positions.push(p1.z + cx * n.z + cy * bitan.z);
  928. uvs.push(i / radialSegments);
  929. uvs.push(j / tubularSegments);
  930. }
  931. }
  932. for (i = 0; i < radialSegments; i++) {
  933. for (j = 0; j < tubularSegments; j++) {
  934. var jNext = (j + 1) % tubularSegments;
  935. var a = i * tubularSegments + j;
  936. var b = (i + 1) * tubularSegments + j;
  937. var c = (i + 1) * tubularSegments + jNext;
  938. var d = i * tubularSegments + jNext;
  939. indices.push(d);
  940. indices.push(b);
  941. indices.push(a);
  942. indices.push(d);
  943. indices.push(c);
  944. indices.push(b);
  945. }
  946. }
  947. // Normals
  948. VertexData.ComputeNormals(positions, indices, normals);
  949. // Sides
  950. VertexData._ComputeSides(sideOrientation, positions, indices, normals, uvs);
  951. // Result
  952. var vertexData = new VertexData();
  953. vertexData.indices = indices;
  954. vertexData.positions = positions;
  955. vertexData.normals = normals;
  956. vertexData.uvs = uvs;
  957. return vertexData;
  958. };
  959. // Tools
  960. /**
  961. * @param {any} - positions (number[] or Float32Array)
  962. * @param {any} - indices (number[] or Uint16Array)
  963. * @param {any} - normals (number[] or Float32Array)
  964. */
  965. VertexData.ComputeNormals = function (positions, indices, normals) {
  966. var index = 0;
  967. // temp Vector3
  968. var p1 = BABYLON.Vector3.Zero();
  969. var p2 = BABYLON.Vector3.Zero();
  970. var p3 = BABYLON.Vector3.Zero();
  971. var p1p2 = BABYLON.Vector3.Zero();
  972. var p3p2 = BABYLON.Vector3.Zero();
  973. var faceNormal = BABYLON.Vector3.Zero();
  974. var vertexNormali1 = BABYLON.Vector3.Zero();
  975. var vertexNormali2 = BABYLON.Vector3.Zero();
  976. var vertexNormali3 = BABYLON.Vector3.Zero();
  977. // indice triplet = 1 face
  978. var nbFaces = indices.length / 3;
  979. for (index = 0; index < nbFaces; index++) {
  980. var i1 = indices[index * 3];
  981. var i2 = indices[index * 3 + 1];
  982. var i3 = indices[index * 3 + 2];
  983. // setting the temp V3
  984. BABYLON.Vector3.FromFloatsToRef(positions[i1 * 3], positions[i1 * 3 + 1], positions[i1 * 3 + 2], p1);
  985. BABYLON.Vector3.FromFloatsToRef(positions[i2 * 3], positions[i2 * 3 + 1], positions[i2 * 3 + 2], p2);
  986. BABYLON.Vector3.FromFloatsToRef(positions[i3 * 3], positions[i3 * 3 + 1], positions[i3 * 3 + 2], p3);
  987. p1.subtractToRef(p2, p1p2);
  988. p3.subtractToRef(p2, p3p2);
  989. BABYLON.Vector3.CrossToRef(p1p2, p3p2, faceNormal);
  990. faceNormal.normalize();
  991. // All intermediate results are stored in the normals array :
  992. // get the normals at i1, i2 and i3 indexes
  993. normals[i1 * 3] = normals[i1 * 3] || 0.0;
  994. normals[i1 * 3 + 1] = normals[i1 * 3 + 1] || 0.0;
  995. normals[i1 * 3 + 2] = normals[i1 * 3 + 2] || 0.0;
  996. normals[i2 * 3] = normals[i2 * 3] || 0.0;
  997. normals[i2 * 3 + 1] = normals[i2 * 3 + 1] || 0.0;
  998. normals[i2 * 3 + 2] = normals[i2 * 3 + 2] || 0.0;
  999. normals[i3 * 3] = normals[i3 * 3] || 0.0;
  1000. normals[i3 * 3 + 1] = normals[i3 * 3 + 1] || 0.0;
  1001. normals[i3 * 3 + 2] = normals[i3 * 3 + 2] || 0.0;
  1002. // make intermediate vectors3 from normals values
  1003. BABYLON.Vector3.FromFloatsToRef(normals[i1 * 3], normals[i1 * 3 + 1], normals[i1 * 3 + 2], vertexNormali1);
  1004. BABYLON.Vector3.FromFloatsToRef(normals[i2 * 3], normals[i2 * 3 + 1], normals[i2 * 3 + 2], vertexNormali2);
  1005. BABYLON.Vector3.FromFloatsToRef(normals[i3 * 3], normals[i3 * 3 + 1], normals[i3 * 3 + 2], vertexNormali3);
  1006. // add the current face normals to these intermediate vectors3
  1007. vertexNormali1 = vertexNormali1.addInPlace(faceNormal);
  1008. vertexNormali2 = vertexNormali2.addInPlace(faceNormal);
  1009. vertexNormali3 = vertexNormali3.addInPlace(faceNormal);
  1010. // store back intermediate vectors3 into the normals array
  1011. normals[i1 * 3] = vertexNormali1.x;
  1012. normals[i1 * 3 + 1] = vertexNormali1.y;
  1013. normals[i1 * 3 + 2] = vertexNormali1.z;
  1014. normals[i2 * 3] = vertexNormali2.x;
  1015. normals[i2 * 3 + 1] = vertexNormali2.y;
  1016. normals[i2 * 3 + 2] = vertexNormali2.z;
  1017. normals[i3 * 3] = vertexNormali3.x;
  1018. normals[i3 * 3 + 1] = vertexNormali3.y;
  1019. normals[i3 * 3 + 2] = vertexNormali3.z;
  1020. }
  1021. for (index = 0; index < normals.length / 3; index++) {
  1022. BABYLON.Vector3.FromFloatsToRef(normals[index * 3], normals[index * 3 + 1], normals[index * 3 + 2], vertexNormali1);
  1023. vertexNormali1.normalize();
  1024. normals[index * 3] = vertexNormali1.x;
  1025. normals[index * 3 + 1] = vertexNormali1.y;
  1026. normals[index * 3 + 2] = vertexNormali1.z;
  1027. }
  1028. };
  1029. VertexData._ComputeSides = function (sideOrientation, positions, indices, normals, uvs) {
  1030. var li = indices.length;
  1031. var ln = normals.length;
  1032. var i;
  1033. var n;
  1034. sideOrientation = sideOrientation || BABYLON.Mesh.DEFAULTSIDE;
  1035. switch (sideOrientation) {
  1036. case BABYLON.Mesh.FRONTSIDE:
  1037. break;
  1038. case BABYLON.Mesh.BACKSIDE:
  1039. var tmp;
  1040. for (i = 0; i < li; i += 3) {
  1041. tmp = indices[i];
  1042. indices[i] = indices[i + 2];
  1043. indices[i + 2] = tmp;
  1044. }
  1045. for (n = 0; n < ln; n++) {
  1046. normals[n] = -normals[n];
  1047. }
  1048. break;
  1049. case BABYLON.Mesh.DOUBLESIDE:
  1050. // positions
  1051. var lp = positions.length;
  1052. var l = lp / 3;
  1053. for (var p = 0; p < lp; p++) {
  1054. positions[lp + p] = positions[p];
  1055. }
  1056. for (i = 0; i < li; i += 3) {
  1057. indices[i + li] = indices[i + 2] + l;
  1058. indices[i + 1 + li] = indices[i + 1] + l;
  1059. indices[i + 2 + li] = indices[i] + l;
  1060. }
  1061. for (n = 0; n < ln; n++) {
  1062. normals[ln + n] = -normals[n];
  1063. }
  1064. // uvs
  1065. var lu = uvs.length;
  1066. for (var u = 0; u < lu; u++) {
  1067. uvs[u + lu] = uvs[u];
  1068. }
  1069. break;
  1070. }
  1071. };
  1072. return VertexData;
  1073. })();
  1074. BABYLON.VertexData = VertexData;
  1075. })(BABYLON || (BABYLON = {}));
  1076. //# sourceMappingURL=babylon.mesh.vertexData.js.map