babylon.mesh.vertexData.js 28 KB

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