babylon.mesh.vertexData.ts 26 KB

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