babylon.mesh.vertexData.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  1. "use strict";
  2. var BABYLON = BABYLON || {};
  3. (function () {
  4. BABYLON.VertexData = function () {
  5. };
  6. // Methods
  7. BABYLON.VertexData.prototype.applyToMesh = function(mesh, updatable) {
  8. if (this.positions) {
  9. mesh.setVerticesData(this.positions, BABYLON.VertexBuffer.PositionKind, updatable);
  10. }
  11. if (this.normals) {
  12. mesh.setVerticesData(this.normals, BABYLON.VertexBuffer.NormalKind, updatable);
  13. }
  14. if (this.uvs) {
  15. mesh.setVerticesData(this.uvs, BABYLON.VertexBuffer.UVKind, updatable);
  16. }
  17. if (this.uv2s) {
  18. mesh.setVerticesData(this.uv2s, BABYLON.VertexBuffer.UV2Kind, updatable);
  19. }
  20. if (this.colors) {
  21. mesh.setVerticesData(this.colors, BABYLON.VertexBuffer.ColorKind, updatable);
  22. }
  23. if (this.matricesIndices) {
  24. mesh.setVerticesData(this.matricesIndices, BABYLON.VertexBuffer.MatricesIndicesKind, updatable);
  25. }
  26. if (this.matricesWeights) {
  27. mesh.setVerticesData(this.matricesWeights, BABYLON.VertexBuffer.MatricesWeightsKind, updatable);
  28. }
  29. if (this.indices) {
  30. mesh.setIndices(this.indices);
  31. }
  32. };
  33. BABYLON.VertexData.prototype.transform = function (matrix) {
  34. var transformed = BABYLON.Vector3.Zero();
  35. if (this.positions) {
  36. var position = BABYLON.Vector3.Zero();
  37. for (var index = 0; index < this.positions.length; index += 3) {
  38. BABYLON.Vector3.FromArrayToRef(this.positions, index, position);
  39. BABYLON.Vector3.TransformCoordinatesToRef(position, matrix, transformed);
  40. this.positions[index] = transformed.x;
  41. this.positions[index + 1] = transformed.y;
  42. this.positions[index + 2] = transformed.z;
  43. }
  44. }
  45. if (this.normals) {
  46. var normal = BABYLON.Vector3.Zero();
  47. for (index = 0; index < this.normals.length; index += 3) {
  48. BABYLON.Vector3.FromArrayToRef(this.normals, index, normal);
  49. BABYLON.Vector3.TransformNormalToRef(normal, matrix, transformed);
  50. this.normals[index] = transformed.x;
  51. this.normals[index + 1] = transformed.y;
  52. this.normals[index + 2] = transformed.z;
  53. }
  54. }
  55. };
  56. BABYLON.VertexData.prototype.merge = function (other) {
  57. if (other.indices) {
  58. if (!this.indices) {
  59. this.indices = [];
  60. }
  61. var offset = this.positions ? this.positions.length / 3 : 0;
  62. for (var index = 0; index < other.indices.length; index++) {
  63. this.indices.push(other.indices[index] + offset);
  64. }
  65. }
  66. if (other.positions) {
  67. if (!this.positions) {
  68. this.positions = [];
  69. }
  70. for (index = 0; index < other.positions.length; index++) {
  71. this.positions.push(other.positions[index]);
  72. }
  73. }
  74. if (other.normals) {
  75. if (!this.normals) {
  76. this.normals = [];
  77. }
  78. for (index = 0; index < other.normals.length; index++) {
  79. this.normals.push(other.normals[index]);
  80. }
  81. }
  82. if (other.uvs) {
  83. if (!this.uvs) {
  84. this.uvs = [];
  85. }
  86. for (index = 0; index < other.uvs.length; index++) {
  87. this.uvs.push(other.uvs[index]);
  88. }
  89. }
  90. if (other.uv2s) {
  91. if (!this.uv2s) {
  92. this.uv2s = [];
  93. }
  94. for (index = 0; index < other.uv2s.length; index++) {
  95. this.uv2s.push(other.uv2s[index]);
  96. }
  97. }
  98. if (other.matricesIndices) {
  99. if (!this.matricesIndices) {
  100. this.matricesIndices = [];
  101. }
  102. for (index = 0; index < other.matricesIndices.length; index++) {
  103. this.matricesIndices.push(other.matricesIndices[index]);
  104. }
  105. }
  106. if (other.matricesWeights) {
  107. if (!this.matricesWeights) {
  108. this.matricesWeights = [];
  109. }
  110. for (index = 0; index < other.matricesWeights.length; index++) {
  111. this.matricesWeights.push(other.matricesWeights[index]);
  112. }
  113. }
  114. if (other.colors) {
  115. if (!this.colors) {
  116. this.colors = [];
  117. }
  118. for (index = 0; index < other.colors.length; index++) {
  119. this.colors.push(other.colors[index]);
  120. }
  121. }
  122. };
  123. // Statics
  124. BABYLON.VertexData.ExtractFromMesh = function (mesh) {
  125. var result = new BABYLON.VertexData();
  126. if (mesh.isVerticesDataPresent(BABYLON.VertexBuffer.PositionKind)) {
  127. result.positions = mesh.getVerticesData(BABYLON.VertexBuffer.PositionKind);
  128. }
  129. if (mesh.isVerticesDataPresent(BABYLON.VertexBuffer.NormalKind)) {
  130. result.normals = mesh.getVerticesData(BABYLON.VertexBuffer.NormalKind);
  131. }
  132. if (mesh.isVerticesDataPresent(BABYLON.VertexBuffer.UVKind)) {
  133. result.uvs = mesh.getVerticesData(BABYLON.VertexBuffer.UVKind);
  134. }
  135. if (mesh.isVerticesDataPresent(BABYLON.VertexBuffer.UV2Kind)) {
  136. result.uv2s = mesh.getVerticesData(BABYLON.VertexBuffer.UV2Kind);
  137. }
  138. if (mesh.isVerticesDataPresent(BABYLON.VertexBuffer.ColorKind)) {
  139. result.colors = mesh.getVerticesData(BABYLON.VertexBuffer.ColorKind);
  140. }
  141. if (mesh.isVerticesDataPresent(BABYLON.VertexBuffer.MatricesIndicesKind)) {
  142. result.matricesIndices = mesh.getVerticesData(BABYLON.VertexBuffer.MatricesIndicesKind);
  143. }
  144. if (mesh.isVerticesDataPresent(BABYLON.VertexBuffer.MatricesWeightsKind)) {
  145. result.matricesWeights = mesh.getVerticesData(BABYLON.VertexBuffer.MatricesWeightsKind);
  146. }
  147. result.indices = mesh.getIndices();
  148. return result;
  149. };
  150. BABYLON.VertexData.CreateBox = function(size) {
  151. var normalsSource = [
  152. new BABYLON.Vector3(0, 0, 1),
  153. new BABYLON.Vector3(0, 0, -1),
  154. new BABYLON.Vector3(1, 0, 0),
  155. new BABYLON.Vector3(-1, 0, 0),
  156. new BABYLON.Vector3(0, 1, 0),
  157. new BABYLON.Vector3(0, -1, 0)
  158. ];
  159. var indices = [];
  160. var positions = [];
  161. var normals = [];
  162. var uvs = [];
  163. size = size || 1;
  164. // Create each face in turn.
  165. for (var index = 0; index < normalsSource.length; index++) {
  166. var normal = normalsSource[index];
  167. // Get two vectors perpendicular to the face normal and to each other.
  168. var side1 = new BABYLON.Vector3(normal.y, normal.z, normal.x);
  169. var side2 = BABYLON.Vector3.Cross(normal, side1);
  170. // Six indices (two triangles) per face.
  171. var verticesLength = positions.length / 3;
  172. indices.push(verticesLength);
  173. indices.push(verticesLength + 1);
  174. indices.push(verticesLength + 2);
  175. indices.push(verticesLength);
  176. indices.push(verticesLength + 2);
  177. indices.push(verticesLength + 3);
  178. // Four vertices per face.
  179. var vertex = normal.subtract(side1).subtract(side2).scale(size / 2);
  180. positions.push(vertex.x, vertex.y, vertex.z);
  181. normals.push(normal.x, normal.y, normal.z);
  182. uvs.push(1.0, 1.0);
  183. vertex = normal.subtract(side1).add(side2).scale(size / 2);
  184. positions.push(vertex.x, vertex.y, vertex.z);
  185. normals.push(normal.x, normal.y, normal.z);
  186. uvs.push(0.0, 1.0);
  187. vertex = normal.add(side1).add(side2).scale(size / 2);
  188. positions.push(vertex.x, vertex.y, vertex.z);
  189. normals.push(normal.x, normal.y, normal.z);
  190. uvs.push(0.0, 0.0);
  191. vertex = normal.add(side1).subtract(side2).scale(size / 2);
  192. positions.push(vertex.x, vertex.y, vertex.z);
  193. normals.push(normal.x, normal.y, normal.z);
  194. uvs.push(1.0, 0.0);
  195. }
  196. // Result
  197. var vertexData = new BABYLON.VertexData();
  198. vertexData.indices = indices;
  199. vertexData.positions = positions;
  200. vertexData.normals = normals;
  201. vertexData.uvs = uvs;
  202. return vertexData;
  203. };
  204. BABYLON.VertexData.CreateSphere = function (segments, diameter) {
  205. segments = segments || 32;
  206. diameter = diameter || 1;
  207. var radius = diameter / 2;
  208. var totalZRotationSteps = 2 + segments;
  209. var totalYRotationSteps = 2 * totalZRotationSteps;
  210. var indices = [];
  211. var positions = [];
  212. var normals = [];
  213. var uvs = [];
  214. for (var zRotationStep = 0; zRotationStep <= totalZRotationSteps; zRotationStep++) {
  215. var normalizedZ = zRotationStep / totalZRotationSteps;
  216. var angleZ = (normalizedZ * Math.PI);
  217. for (var yRotationStep = 0; yRotationStep <= totalYRotationSteps; yRotationStep++) {
  218. var normalizedY = yRotationStep / totalYRotationSteps;
  219. var angleY = normalizedY * Math.PI * 2;
  220. var rotationZ = BABYLON.Matrix.RotationZ(-angleZ);
  221. var rotationY = BABYLON.Matrix.RotationY(angleY);
  222. var afterRotZ = BABYLON.Vector3.TransformCoordinates(BABYLON.Vector3.Up(), rotationZ);
  223. var complete = BABYLON.Vector3.TransformCoordinates(afterRotZ, rotationY);
  224. var vertex = complete.scale(radius);
  225. var normal = BABYLON.Vector3.Normalize(vertex);
  226. positions.push(vertex.x, vertex.y, vertex.z);
  227. normals.push(normal.x, normal.y, normal.z);
  228. uvs.push(normalizedZ, normalizedY);
  229. }
  230. if (zRotationStep > 0) {
  231. var verticesCount = positions.length / 3;
  232. for (var firstIndex = verticesCount - 2 * (totalYRotationSteps + 1) ; (firstIndex + totalYRotationSteps + 2) < verticesCount; firstIndex++) {
  233. indices.push((firstIndex));
  234. indices.push((firstIndex + 1));
  235. indices.push(firstIndex + totalYRotationSteps + 1);
  236. indices.push((firstIndex + totalYRotationSteps + 1));
  237. indices.push((firstIndex + 1));
  238. indices.push((firstIndex + totalYRotationSteps + 2));
  239. }
  240. }
  241. }
  242. // Result
  243. var vertexData = new BABYLON.VertexData();
  244. vertexData.indices = indices;
  245. vertexData.positions = positions;
  246. vertexData.normals = normals;
  247. vertexData.uvs = uvs;
  248. return vertexData;
  249. };
  250. BABYLON.VertexData.CreateCylinder = function (height, diameterTop, diameterBottom, tessellation) {
  251. var radiusTop = diameterTop / 2;
  252. var radiusBottom = diameterBottom / 2;
  253. var indices = [];
  254. var positions = [];
  255. var normals = [];
  256. var uvs = [];
  257. height = height || 1;
  258. diameterTop = diameterTop || 0.5;
  259. diameterBottom = diameterBottom || 1;
  260. tessellation = tessellation || 16;
  261. var getCircleVector = function (i) {
  262. var angle = (i * 2.0 * Math.PI / tessellation);
  263. var dx = Math.sin(angle);
  264. var dz = Math.cos(angle);
  265. return new BABYLON.Vector3(dx, 0, dz);
  266. };
  267. var createCylinderCap = function (isTop) {
  268. var radius = isTop ? radiusTop : radiusBottom;
  269. if (radius == 0) {
  270. return;
  271. }
  272. // Create cap indices.
  273. for (var i = 0; i < tessellation - 2; i++) {
  274. var i1 = (i + 1) % tessellation;
  275. var i2 = (i + 2) % tessellation;
  276. if (!isTop) {
  277. var tmp = i1;
  278. var i1 = i2;
  279. i2 = tmp;
  280. }
  281. var vbase = positions.length / 3;
  282. indices.push(vbase);
  283. indices.push(vbase + i1);
  284. indices.push(vbase + i2);
  285. }
  286. // Which end of the cylinder is this?
  287. var normal = new BABYLON.Vector3(0, -1, 0);
  288. var textureScale = new BABYLON.Vector2(-0.5, -0.5);
  289. if (!isTop) {
  290. normal = normal.scale(-1);
  291. textureScale.x = -textureScale.x;
  292. }
  293. // Create cap vertices.
  294. for (var i = 0; i < tessellation; i++) {
  295. var circleVector = getCircleVector(i);
  296. var position = circleVector.scale(radius).add(normal.scale(height));
  297. var textureCoordinate = new BABYLON.Vector2(circleVector.x * textureScale.x + 0.5, circleVector.z * textureScale.y + 0.5);
  298. positions.push(position.x, position.y, position.z);
  299. normals.push(normal.x, normal.y, normal.z);
  300. uvs.push(textureCoordinate.x, textureCoordinate.y);
  301. }
  302. };
  303. height /= 2;
  304. var topOffset = new BABYLON.Vector3(0, 1, 0).scale(height);
  305. var stride = tessellation + 1;
  306. // Create a ring of triangles around the outside of the cylinder.
  307. for (var i = 0; i <= tessellation; i++) {
  308. var normal = getCircleVector(i);
  309. var sideOffsetBottom = normal.scale(radiusBottom);
  310. var sideOffsetTop = normal.scale(radiusTop);
  311. var textureCoordinate = new BABYLON.Vector2(i / tessellation, 0);
  312. var position = sideOffsetBottom.add(topOffset);
  313. positions.push(position.x, position.y, position.z);
  314. normals.push(normal.x, normal.y, normal.z);
  315. uvs.push(textureCoordinate.x, textureCoordinate.y);
  316. position = sideOffsetTop.subtract(topOffset);
  317. textureCoordinate.y += 1;
  318. positions.push(position.x, position.y, position.z);
  319. normals.push(normal.x, normal.y, normal.z);
  320. uvs.push(textureCoordinate.x, textureCoordinate.y);
  321. indices.push(i * 2);
  322. indices.push((i * 2 + 2) % (stride * 2));
  323. indices.push(i * 2 + 1);
  324. indices.push(i * 2 + 1);
  325. indices.push((i * 2 + 2) % (stride * 2));
  326. indices.push((i * 2 + 3) % (stride * 2));
  327. }
  328. // Create flat triangle fan caps to seal the top and bottom.
  329. createCylinderCap(true);
  330. createCylinderCap(false);
  331. // Result
  332. var vertexData = new BABYLON.VertexData();
  333. vertexData.indices = indices;
  334. vertexData.positions = positions;
  335. vertexData.normals = normals;
  336. vertexData.uvs = uvs;
  337. return vertexData;
  338. };
  339. BABYLON.VertexData.CreateTorus = function (diameter, thickness, tessellation) {
  340. var indices = [];
  341. var positions = [];
  342. var normals = [];
  343. var uvs = [];
  344. diameter = diameter || 1;
  345. thickness = thickness || 0.5;
  346. tessellation = tessellation || 16;
  347. var stride = tessellation + 1;
  348. for (var i = 0; i <= tessellation; i++) {
  349. var u = i / tessellation;
  350. var outerAngle = i * Math.PI * 2.0 / tessellation - Math.PI / 2.0;
  351. var transform = BABYLON.Matrix.Translation(diameter / 2.0, 0, 0).multiply(BABYLON.Matrix.RotationY(outerAngle));
  352. for (var j = 0; j <= tessellation; j++) {
  353. var v = 1 - j / tessellation;
  354. var innerAngle = j * Math.PI * 2.0 / tessellation + Math.PI;
  355. var dx = Math.cos(innerAngle);
  356. var dy = Math.sin(innerAngle);
  357. // Create a vertex.
  358. var normal = new BABYLON.Vector3(dx, dy, 0);
  359. var position = normal.scale(thickness / 2);
  360. var textureCoordinate = new BABYLON.Vector2(u, v);
  361. position = BABYLON.Vector3.TransformCoordinates(position, transform);
  362. normal = BABYLON.Vector3.TransformNormal(normal, transform);
  363. positions.push(position.x, position.y, position.z);
  364. normals.push(normal.x, normal.y, normal.z);
  365. uvs.push(textureCoordinate.x, textureCoordinate.y);
  366. // And create indices for two triangles.
  367. var nextI = (i + 1) % stride;
  368. var nextJ = (j + 1) % stride;
  369. indices.push(i * stride + j);
  370. indices.push(i * stride + nextJ);
  371. indices.push(nextI * stride + j);
  372. indices.push(i * stride + nextJ);
  373. indices.push(nextI * stride + nextJ);
  374. indices.push(nextI * stride + j);
  375. }
  376. }
  377. // Result
  378. var vertexData = new BABYLON.VertexData();
  379. vertexData.indices = indices;
  380. vertexData.positions = positions;
  381. vertexData.normals = normals;
  382. vertexData.uvs = uvs;
  383. return vertexData;
  384. };
  385. BABYLON.VertexData.CreateGround = function (width, height, subdivisions) {
  386. var indices = [];
  387. var positions = [];
  388. var normals = [];
  389. var uvs = [];
  390. var row, col;
  391. width = width || 1;
  392. height = height || 1;
  393. subdivisions = subdivisions || 1;
  394. for (row = 0; row <= subdivisions; row++) {
  395. for (col = 0; col <= subdivisions; col++) {
  396. var position = new BABYLON.Vector3((col * width) / subdivisions - (width / 2.0), 0, ((subdivisions - row) * height) / subdivisions - (height / 2.0));
  397. var normal = new BABYLON.Vector3(0, 1.0, 0);
  398. positions.push(position.x, position.y, position.z);
  399. normals.push(normal.x, normal.y, normal.z);
  400. uvs.push(col / subdivisions, 1.0 - row / subdivisions);
  401. }
  402. }
  403. for (row = 0; row < subdivisions; row++) {
  404. for (col = 0; col < subdivisions; col++) {
  405. indices.push(col + 1 + (row + 1) * (subdivisions + 1));
  406. indices.push(col + 1 + row * (subdivisions + 1));
  407. indices.push(col + row * (subdivisions + 1));
  408. indices.push(col + (row + 1) * (subdivisions + 1));
  409. indices.push(col + 1 + (row + 1) * (subdivisions + 1));
  410. indices.push(col + row * (subdivisions + 1));
  411. }
  412. }
  413. // Result
  414. var vertexData = new BABYLON.VertexData();
  415. vertexData.indices = indices;
  416. vertexData.positions = positions;
  417. vertexData.normals = normals;
  418. vertexData.uvs = uvs;
  419. return vertexData;
  420. };
  421. BABYLON.VertexData.CreatePlane = function (size) {
  422. var indices = [];
  423. var positions = [];
  424. var normals = [];
  425. var uvs = [];
  426. size = size || 1;
  427. // Vertices
  428. var halfSize = size / 2.0;
  429. positions.push(-halfSize, -halfSize, 0);
  430. normals.push(0, 0, -1.0);
  431. uvs.push(0.0, 0.0);
  432. positions.push(halfSize, -halfSize, 0);
  433. normals.push(0, 0, -1.0);
  434. uvs.push(1.0, 0.0);
  435. positions.push(halfSize, halfSize, 0);
  436. normals.push(0, 0, -1.0);
  437. uvs.push(1.0, 1.0);
  438. positions.push(-halfSize, halfSize, 0);
  439. normals.push(0, 0, -1.0);
  440. uvs.push(0.0, 1.0);
  441. // Indices
  442. indices.push(0);
  443. indices.push(1);
  444. indices.push(2);
  445. indices.push(0);
  446. indices.push(2);
  447. indices.push(3);
  448. // Result
  449. var vertexData = new BABYLON.VertexData();
  450. vertexData.indices = indices;
  451. vertexData.positions = positions;
  452. vertexData.normals = normals;
  453. vertexData.uvs = uvs;
  454. return vertexData;
  455. };
  456. // based on http://code.google.com/p/away3d/source/browse/trunk/fp10/Away3D/src/away3d/primitives/TorusKnot.as?spec=svn2473&r=2473
  457. BABYLON.VertexData.CreateTorusKnot = function (radius, tube, radialSegments, tubularSegments, p, q) {
  458. var indices = [];
  459. var positions = [];
  460. var normals = [];
  461. var uvs = [];
  462. radius = radius || 2;
  463. tube = tube || 0.5;
  464. radialSegments = radialSegments || 32;
  465. tubularSegments = tubularSegments || 32;
  466. p = p || 2;
  467. q = q || 3;
  468. // Helper
  469. function getPos(u, q, p, radius) {
  470. var cu = Math.cos(u);
  471. var su = Math.sin(u);
  472. var quOverP = q / p * u;
  473. var cs = Math.cos(quOverP);
  474. var tx = radius * (2 + cs) * 0.5 * cu;
  475. var ty = radius * (2 + cs) * su * 0.5;
  476. var tz = radius * Math.sin(quOverP) * 0.5;
  477. return new BABYLON.Vector3(tx, ty, tz);
  478. };
  479. // Vertices
  480. for (var i = 0; i <= radialSegments; i++) {
  481. var modI = i % radialSegments;
  482. var u = modI / radialSegments * 2 * p * Math.PI;
  483. var p1 = getPos(u, q, p, radius);
  484. var p2 = getPos(u + 0.01, q, p, radius);
  485. var tang = p2.subtract(p1);
  486. var n = p2.add(p1);
  487. var bitan = BABYLON.Vector3.Cross(tang, n);
  488. n = BABYLON.Vector3.Cross(bitan, tang);
  489. bitan.normalize();
  490. n.normalize();
  491. for (var j = 0; j <= tubularSegments; j++) {
  492. var modJ = j % tubularSegments;
  493. var v = modJ / tubularSegments * 2 * Math.PI;
  494. var cx = -tube * Math.cos(v);
  495. var cy = tube * Math.sin(v);
  496. var pos = new BABYLON.Vector3();
  497. pos.x = p1.x + cx * n.x + cy * bitan.x;
  498. pos.y = p1.y + cx * n.y + cy * bitan.y;
  499. pos.z = p1.z + cx * n.z + cy * bitan.z;
  500. positions.push(pos.x);
  501. positions.push(pos.y);
  502. positions.push(pos.z);
  503. uvs.push(i / radialSegments);
  504. uvs.push(j / tubularSegments);
  505. }
  506. }
  507. for (var i = 0; i <= radialSegments; i++) {
  508. for (var j = 0; j < tubularSegments; j++) {
  509. var a = i * tubularSegments + j;
  510. var b = (i + 1) * tubularSegments + j;
  511. var c = (i + 1) * tubularSegments + j + 1;
  512. var d = i * tubularSegments + j + 1;
  513. indices.push(a); indices.push(b); indices.push(d);
  514. indices.push(b); indices.push(c); indices.push(d);
  515. }
  516. }
  517. // Normals
  518. BABYLON.VertexData.ComputeNormals(positions, indices, normals);
  519. // Result
  520. var vertexData = new BABYLON.VertexData();
  521. vertexData.indices = indices;
  522. vertexData.positions = positions;
  523. vertexData.normals = normals;
  524. vertexData.uvs = uvs;
  525. return vertexData;
  526. };
  527. // Tools
  528. BABYLON.VertexData.ComputeNormals = function (positions, indices, normals) {
  529. var positionVectors = [];
  530. var facesOfVertices = [];
  531. var index;
  532. for (index = 0; index < positions.length; index += 3) {
  533. var vector3 = new BABYLON.Vector3(positions[index], positions[index + 1], positions[index + 2]);
  534. positionVectors.push(vector3);
  535. facesOfVertices.push([]);
  536. }
  537. // Compute normals
  538. var facesNormals = [];
  539. for (index = 0; index < indices.length / 3; index++) {
  540. var i1 = indices[index * 3];
  541. var i2 = indices[index * 3 + 1];
  542. var i3 = indices[index * 3 + 2];
  543. var p1 = positionVectors[i1];
  544. var p2 = positionVectors[i2];
  545. var p3 = positionVectors[i3];
  546. var p1p2 = p1.subtract(p2);
  547. var p3p2 = p3.subtract(p2);
  548. facesNormals[index] = BABYLON.Vector3.Normalize(BABYLON.Vector3.Cross(p1p2, p3p2));
  549. facesOfVertices[i1].push(index);
  550. facesOfVertices[i2].push(index);
  551. facesOfVertices[i3].push(index);
  552. }
  553. for (index = 0; index < positionVectors.length; index++) {
  554. var faces = facesOfVertices[index];
  555. var normal = BABYLON.Vector3.Zero();
  556. for (var faceIndex = 0; faceIndex < faces.length; faceIndex++) {
  557. normal.addInPlace(facesNormals[faces[faceIndex]]);
  558. }
  559. normal = BABYLON.Vector3.Normalize(normal.scale(1.0 / faces.length));
  560. normals[index * 3] = normal.x;
  561. normals[index * 3 + 1] = normal.y;
  562. normals[index * 3 + 2] = normal.z;
  563. }
  564. };
  565. })();