babylon.mesh.vertexData.js 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907
  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) {
  187. return VertexData._ExtractFrom(mesh);
  188. };
  189. VertexData.ExtractFromGeometry = function (geometry) {
  190. return VertexData._ExtractFrom(geometry);
  191. };
  192. VertexData._ExtractFrom = function (meshOrGeometry) {
  193. var result = new VertexData();
  194. if (meshOrGeometry.isVerticesDataPresent(BABYLON.VertexBuffer.PositionKind)) {
  195. result.positions = meshOrGeometry.getVerticesData(BABYLON.VertexBuffer.PositionKind);
  196. }
  197. if (meshOrGeometry.isVerticesDataPresent(BABYLON.VertexBuffer.NormalKind)) {
  198. result.normals = meshOrGeometry.getVerticesData(BABYLON.VertexBuffer.NormalKind);
  199. }
  200. if (meshOrGeometry.isVerticesDataPresent(BABYLON.VertexBuffer.UVKind)) {
  201. result.uvs = meshOrGeometry.getVerticesData(BABYLON.VertexBuffer.UVKind);
  202. }
  203. if (meshOrGeometry.isVerticesDataPresent(BABYLON.VertexBuffer.UV2Kind)) {
  204. result.uv2s = meshOrGeometry.getVerticesData(BABYLON.VertexBuffer.UV2Kind);
  205. }
  206. if (meshOrGeometry.isVerticesDataPresent(BABYLON.VertexBuffer.ColorKind)) {
  207. result.colors = meshOrGeometry.getVerticesData(BABYLON.VertexBuffer.ColorKind);
  208. }
  209. if (meshOrGeometry.isVerticesDataPresent(BABYLON.VertexBuffer.MatricesIndicesKind)) {
  210. result.matricesIndices = meshOrGeometry.getVerticesData(BABYLON.VertexBuffer.MatricesIndicesKind);
  211. }
  212. if (meshOrGeometry.isVerticesDataPresent(BABYLON.VertexBuffer.MatricesWeightsKind)) {
  213. result.matricesWeights = meshOrGeometry.getVerticesData(BABYLON.VertexBuffer.MatricesWeightsKind);
  214. }
  215. result.indices = meshOrGeometry.getIndices();
  216. return result;
  217. };
  218. VertexData.CreateRibbon = function (pathArray, closeArray, closePath, offset) {
  219. closeArray = closeArray || false;
  220. closePath = closePath || false;
  221. var defaultOffset = Math.floor(pathArray[0].length / 2);
  222. offset = offset || defaultOffset;
  223. offset = offset > defaultOffset ? defaultOffset : Math.floor(offset); // offset max allowed : defaultOffset
  224. var positions = [];
  225. var indices = [];
  226. var normals = [];
  227. var uvs = [];
  228. var us = []; // us[path_id] = [uDist1, uDist2, uDist3 ... ] distances between points on path path_id
  229. var vs = []; // vs[i] = [vDist1, vDist2, vDist3, ... ] distances between points i of consecutives paths from pathArray
  230. var uTotalDistance = []; // uTotalDistance[p] : total distance of path p
  231. var vTotalDistance = []; // vTotalDistance[i] : total distance between points i of first and last path from pathArray
  232. var minlg; // minimal length among all paths from pathArray
  233. var lg = []; // array of path lengths : nb of vertex per path
  234. var idx = []; // array of path indexes : index of each path (first vertex) in positions array
  235. var p; // path iterator
  236. var i; // point iterator
  237. var j; // point iterator
  238. // if single path in pathArray
  239. if (pathArray.length < 2) {
  240. var ar1 = [];
  241. var ar2 = [];
  242. for (i = 0; i < pathArray[0].length - offset; i++) {
  243. ar1.push(pathArray[0][i]);
  244. ar2.push(pathArray[0][i + offset]);
  245. }
  246. pathArray = [ar1, ar2];
  247. }
  248. // positions and horizontal distances (u)
  249. var idc = 0;
  250. minlg = pathArray[0].length;
  251. for (p = 0; p < pathArray.length; p++) {
  252. uTotalDistance[p] = 0;
  253. us[p] = [0];
  254. var path = pathArray[p];
  255. var l = path.length;
  256. minlg = (minlg < l) ? minlg : l;
  257. lg[p] = l;
  258. idx[p] = idc;
  259. j = 0;
  260. while (j < l) {
  261. positions.push(path[j].x, path[j].y, path[j].z);
  262. if (j > 0) {
  263. var vectlg = path[j].subtract(path[j - 1]).length();
  264. var dist = vectlg + uTotalDistance[p];
  265. us[p].push(dist);
  266. uTotalDistance[p] = dist;
  267. }
  268. j++;
  269. }
  270. if (closePath) {
  271. vectlg = path[0].subtract(path[j - 1]).length();
  272. dist = vectlg + uTotalDistance[p];
  273. uTotalDistance[p] = dist;
  274. }
  275. idc += l;
  276. }
  277. for (i = 0; i < minlg; i++) {
  278. vTotalDistance[i] = 0;
  279. vs[i] = [0];
  280. var path1;
  281. var path2;
  282. for (p = 0; p < pathArray.length - 1; p++) {
  283. path1 = pathArray[p];
  284. path2 = pathArray[p + 1];
  285. vectlg = path2[i].subtract(path1[i]).length();
  286. dist = vectlg + vTotalDistance[i];
  287. vs[i].push(dist);
  288. vTotalDistance[i] = dist;
  289. }
  290. if (closeArray) {
  291. path1 = pathArray[p];
  292. path2 = pathArray[0];
  293. vectlg = path2[i].subtract(path1[i]).length();
  294. dist = vectlg + vTotalDistance[i];
  295. vTotalDistance[i] = dist;
  296. }
  297. }
  298. // uvs
  299. var u;
  300. var v;
  301. for (p = 0; p < pathArray.length; p++) {
  302. for (i = 0; i < minlg; i++) {
  303. u = us[p][i] / uTotalDistance[p];
  304. v = vs[i][p] / vTotalDistance[i];
  305. uvs.push(u, v);
  306. }
  307. }
  308. // indices
  309. p = 0; // path index
  310. var pi = 0; // positions array index
  311. var l1 = lg[p] - 1; // path1 length
  312. var l2 = lg[p + 1] - 1; // path2 length
  313. var min = (l1 < l2) ? l1 : l2; // current path stop index
  314. var shft = idx[1] - idx[0]; // shift
  315. var path1nb = closeArray ? lg.length : lg.length - 1; // number of path1 to iterate
  316. var t1; // two consecutive triangles, so 4 points : point1
  317. var t2; // point2
  318. var t3; // point3
  319. var t4; // point4
  320. while (pi <= min && p < path1nb) {
  321. // 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
  322. t1 = pi;
  323. t2 = pi + shft;
  324. t3 = pi + 1;
  325. t4 = pi + shft + 1;
  326. indices.push(pi, pi + shft, pi + 1);
  327. indices.push(pi + shft + 1, pi + 1, pi + shft);
  328. pi += 1;
  329. if (pi === min) {
  330. if (closePath) {
  331. indices.push(pi, pi + shft, idx[p]);
  332. indices.push(idx[p] + shft, idx[p], pi + shft);
  333. t3 = idx[p];
  334. t4 = idx[p] + shft;
  335. }
  336. p++;
  337. if (p === lg.length - 1) {
  338. shft = idx[0] - idx[p];
  339. l1 = lg[p] - 1;
  340. l2 = lg[0] - 1;
  341. }
  342. else {
  343. shft = idx[p + 1] - idx[p];
  344. l1 = lg[p] - 1;
  345. l2 = lg[p + 1] - 1;
  346. }
  347. pi = idx[p];
  348. min = (l1 < l2) ? l1 + pi : l2 + pi;
  349. }
  350. }
  351. // normals
  352. VertexData.ComputeNormals(positions, indices, normals);
  353. // Result
  354. var vertexData = new VertexData();
  355. vertexData.indices = indices;
  356. vertexData.positions = positions;
  357. vertexData.normals = normals;
  358. vertexData.uvs = uvs;
  359. return vertexData;
  360. };
  361. VertexData.CreateBox = function (size) {
  362. var normalsSource = [
  363. new BABYLON.Vector3(0, 0, 1),
  364. new BABYLON.Vector3(0, 0, -1),
  365. new BABYLON.Vector3(1, 0, 0),
  366. new BABYLON.Vector3(-1, 0, 0),
  367. new BABYLON.Vector3(0, 1, 0),
  368. new BABYLON.Vector3(0, -1, 0)
  369. ];
  370. var indices = [];
  371. var positions = [];
  372. var normals = [];
  373. var uvs = [];
  374. size = size || 1;
  375. for (var index = 0; index < normalsSource.length; index++) {
  376. var normal = normalsSource[index];
  377. // Get two vectors perpendicular to the face normal and to each other.
  378. var side1 = new BABYLON.Vector3(normal.y, normal.z, normal.x);
  379. var side2 = BABYLON.Vector3.Cross(normal, side1);
  380. // Six indices (two triangles) per face.
  381. var verticesLength = positions.length / 3;
  382. indices.push(verticesLength);
  383. indices.push(verticesLength + 1);
  384. indices.push(verticesLength + 2);
  385. indices.push(verticesLength);
  386. indices.push(verticesLength + 2);
  387. indices.push(verticesLength + 3);
  388. // Four vertices per face.
  389. var vertex = normal.subtract(side1).subtract(side2).scale(size / 2);
  390. positions.push(vertex.x, vertex.y, vertex.z);
  391. normals.push(normal.x, normal.y, normal.z);
  392. uvs.push(1.0, 1.0);
  393. vertex = normal.subtract(side1).add(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(0.0, 1.0);
  397. vertex = normal.add(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, 0.0);
  401. vertex = normal.add(side1).subtract(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(1.0, 0.0);
  405. }
  406. // Result
  407. var vertexData = new VertexData();
  408. vertexData.indices = indices;
  409. vertexData.positions = positions;
  410. vertexData.normals = normals;
  411. vertexData.uvs = uvs;
  412. return vertexData;
  413. };
  414. VertexData.CreateSphere = function (segments, diameter) {
  415. segments = segments || 32;
  416. diameter = diameter || 1;
  417. var radius = diameter / 2;
  418. var totalZRotationSteps = 2 + segments;
  419. var totalYRotationSteps = 2 * totalZRotationSteps;
  420. var indices = [];
  421. var positions = [];
  422. var normals = [];
  423. var uvs = [];
  424. for (var zRotationStep = 0; zRotationStep <= totalZRotationSteps; zRotationStep++) {
  425. var normalizedZ = zRotationStep / totalZRotationSteps;
  426. var angleZ = (normalizedZ * Math.PI);
  427. for (var yRotationStep = 0; yRotationStep <= totalYRotationSteps; yRotationStep++) {
  428. var normalizedY = yRotationStep / totalYRotationSteps;
  429. var angleY = normalizedY * Math.PI * 2;
  430. var rotationZ = BABYLON.Matrix.RotationZ(-angleZ);
  431. var rotationY = BABYLON.Matrix.RotationY(angleY);
  432. var afterRotZ = BABYLON.Vector3.TransformCoordinates(BABYLON.Vector3.Up(), rotationZ);
  433. var complete = BABYLON.Vector3.TransformCoordinates(afterRotZ, rotationY);
  434. var vertex = complete.scale(radius);
  435. var normal = BABYLON.Vector3.Normalize(vertex);
  436. positions.push(vertex.x, vertex.y, vertex.z);
  437. normals.push(normal.x, normal.y, normal.z);
  438. uvs.push(normalizedZ, normalizedY);
  439. }
  440. if (zRotationStep > 0) {
  441. var verticesCount = positions.length / 3;
  442. for (var firstIndex = verticesCount - 2 * (totalYRotationSteps + 1); (firstIndex + totalYRotationSteps + 2) < verticesCount; firstIndex++) {
  443. indices.push((firstIndex));
  444. indices.push((firstIndex + 1));
  445. indices.push(firstIndex + totalYRotationSteps + 1);
  446. indices.push((firstIndex + totalYRotationSteps + 1));
  447. indices.push((firstIndex + 1));
  448. indices.push((firstIndex + totalYRotationSteps + 2));
  449. }
  450. }
  451. }
  452. // Result
  453. var vertexData = new VertexData();
  454. vertexData.indices = indices;
  455. vertexData.positions = positions;
  456. vertexData.normals = normals;
  457. vertexData.uvs = uvs;
  458. return vertexData;
  459. };
  460. VertexData.CreateCylinder = function (height, diameterTop, diameterBottom, tessellation, subdivisions) {
  461. if (subdivisions === void 0) { subdivisions = 1; }
  462. var radiusTop = diameterTop / 2;
  463. var radiusBottom = diameterBottom / 2;
  464. var indices = [];
  465. var positions = [];
  466. var normals = [];
  467. var uvs = [];
  468. height = height || 1;
  469. diameterTop = diameterTop || 0.5;
  470. diameterBottom = diameterBottom || 1;
  471. tessellation = tessellation || 16;
  472. subdivisions = subdivisions || 1;
  473. subdivisions = (subdivisions < 1) ? 1 : subdivisions;
  474. var getCircleVector = function (i) {
  475. var angle = (i * 2.0 * Math.PI / tessellation);
  476. var dx = Math.cos(angle);
  477. var dz = Math.sin(angle);
  478. return new BABYLON.Vector3(dx, 0, dz);
  479. };
  480. var createCylinderCap = function (isTop) {
  481. var radius = isTop ? radiusTop : radiusBottom;
  482. if (radius === 0) {
  483. return;
  484. }
  485. var vbase = positions.length / 3;
  486. var offset = new BABYLON.Vector3(0, height / 2, 0);
  487. var textureScale = new BABYLON.Vector2(0.5, 0.5);
  488. if (!isTop) {
  489. offset.scaleInPlace(-1);
  490. textureScale.x = -textureScale.x;
  491. }
  492. for (var i = 0; i < tessellation; i++) {
  493. var circleVector = getCircleVector(i);
  494. var position = circleVector.scale(radius).add(offset);
  495. var textureCoordinate = new BABYLON.Vector2(circleVector.x * textureScale.x + 0.5, circleVector.z * textureScale.y + 0.5);
  496. positions.push(position.x, position.y, position.z);
  497. uvs.push(textureCoordinate.x, textureCoordinate.y);
  498. }
  499. for (i = 0; i < tessellation - 2; i++) {
  500. if (!isTop) {
  501. indices.push(vbase);
  502. indices.push(vbase + (i + 2) % tessellation);
  503. indices.push(vbase + (i + 1) % tessellation);
  504. }
  505. else {
  506. indices.push(vbase);
  507. indices.push(vbase + (i + 1) % tessellation);
  508. indices.push(vbase + (i + 2) % tessellation);
  509. }
  510. }
  511. };
  512. var base = new BABYLON.Vector3(0, -1, 0).scale(height / 2);
  513. var offset = new BABYLON.Vector3(0, 1, 0).scale(height / subdivisions);
  514. var stride = tessellation + 1;
  515. for (var i = 0; i <= tessellation; i++) {
  516. var circleVector = getCircleVector(i);
  517. var textureCoordinate = new BABYLON.Vector2(i / tessellation, 0);
  518. var position, radius = radiusBottom;
  519. for (var s = 0; s <= subdivisions; s++) {
  520. // Update variables
  521. position = circleVector.scale(radius);
  522. position.addInPlace(base.add(offset.scale(s)));
  523. textureCoordinate.y += 1 / subdivisions;
  524. radius += (radiusTop - radiusBottom) / subdivisions;
  525. // Push in arrays
  526. positions.push(position.x, position.y, position.z);
  527. uvs.push(textureCoordinate.x, textureCoordinate.y);
  528. }
  529. }
  530. subdivisions += 1;
  531. for (s = 0; s < subdivisions - 1; s++) {
  532. for (i = 0; i <= tessellation; i++) {
  533. indices.push(i * subdivisions + s);
  534. indices.push((i * subdivisions + (s + subdivisions)) % (stride * subdivisions));
  535. indices.push(i * subdivisions + (s + 1));
  536. indices.push(i * subdivisions + (s + 1));
  537. indices.push((i * subdivisions + (s + subdivisions)) % (stride * subdivisions));
  538. indices.push((i * subdivisions + (s + subdivisions + 1)) % (stride * subdivisions));
  539. }
  540. }
  541. // Create flat triangle fan caps to seal the top and bottom.
  542. createCylinderCap(true);
  543. createCylinderCap(false);
  544. // Normals
  545. VertexData.ComputeNormals(positions, indices, normals);
  546. // Result
  547. var vertexData = new VertexData();
  548. vertexData.indices = indices;
  549. vertexData.positions = positions;
  550. vertexData.normals = normals;
  551. vertexData.uvs = uvs;
  552. return vertexData;
  553. };
  554. VertexData.CreateTorus = function (diameter, thickness, tessellation) {
  555. var indices = [];
  556. var positions = [];
  557. var normals = [];
  558. var uvs = [];
  559. diameter = diameter || 1;
  560. thickness = thickness || 0.5;
  561. tessellation = tessellation || 16;
  562. var stride = tessellation + 1;
  563. for (var i = 0; i <= tessellation; i++) {
  564. var u = i / tessellation;
  565. var outerAngle = i * Math.PI * 2.0 / tessellation - Math.PI / 2.0;
  566. var transform = BABYLON.Matrix.Translation(diameter / 2.0, 0, 0).multiply(BABYLON.Matrix.RotationY(outerAngle));
  567. for (var j = 0; j <= tessellation; j++) {
  568. var v = 1 - j / tessellation;
  569. var innerAngle = j * Math.PI * 2.0 / tessellation + Math.PI;
  570. var dx = Math.cos(innerAngle);
  571. var dy = Math.sin(innerAngle);
  572. // Create a vertex.
  573. var normal = new BABYLON.Vector3(dx, dy, 0);
  574. var position = normal.scale(thickness / 2);
  575. var textureCoordinate = new BABYLON.Vector2(u, v);
  576. position = BABYLON.Vector3.TransformCoordinates(position, transform);
  577. normal = BABYLON.Vector3.TransformNormal(normal, transform);
  578. positions.push(position.x, position.y, position.z);
  579. normals.push(normal.x, normal.y, normal.z);
  580. uvs.push(textureCoordinate.x, textureCoordinate.y);
  581. // And create indices for two triangles.
  582. var nextI = (i + 1) % stride;
  583. var nextJ = (j + 1) % stride;
  584. indices.push(i * stride + j);
  585. indices.push(i * stride + nextJ);
  586. indices.push(nextI * stride + j);
  587. indices.push(i * stride + nextJ);
  588. indices.push(nextI * stride + nextJ);
  589. indices.push(nextI * stride + j);
  590. }
  591. }
  592. // Result
  593. var vertexData = new VertexData();
  594. vertexData.indices = indices;
  595. vertexData.positions = positions;
  596. vertexData.normals = normals;
  597. vertexData.uvs = uvs;
  598. return vertexData;
  599. };
  600. VertexData.CreateLines = function (points) {
  601. var indices = [];
  602. var positions = [];
  603. for (var index = 0; index < points.length; index++) {
  604. positions.push(points[index].x, points[index].y, points[index].z);
  605. if (index > 0) {
  606. indices.push(index - 1);
  607. indices.push(index);
  608. }
  609. }
  610. // Result
  611. var vertexData = new VertexData();
  612. vertexData.indices = indices;
  613. vertexData.positions = positions;
  614. return vertexData;
  615. };
  616. VertexData.CreateGround = function (width, height, subdivisions) {
  617. var indices = [];
  618. var positions = [];
  619. var normals = [];
  620. var uvs = [];
  621. var row, col;
  622. width = width || 1;
  623. height = height || 1;
  624. subdivisions = subdivisions || 1;
  625. for (row = 0; row <= subdivisions; row++) {
  626. for (col = 0; col <= subdivisions; col++) {
  627. var position = new BABYLON.Vector3((col * width) / subdivisions - (width / 2.0), 0, ((subdivisions - row) * height) / subdivisions - (height / 2.0));
  628. var normal = new BABYLON.Vector3(0, 1.0, 0);
  629. positions.push(position.x, position.y, position.z);
  630. normals.push(normal.x, normal.y, normal.z);
  631. uvs.push(col / subdivisions, 1.0 - row / subdivisions);
  632. }
  633. }
  634. for (row = 0; row < subdivisions; row++) {
  635. for (col = 0; col < subdivisions; col++) {
  636. indices.push(col + 1 + (row + 1) * (subdivisions + 1));
  637. indices.push(col + 1 + row * (subdivisions + 1));
  638. indices.push(col + row * (subdivisions + 1));
  639. indices.push(col + (row + 1) * (subdivisions + 1));
  640. indices.push(col + 1 + (row + 1) * (subdivisions + 1));
  641. indices.push(col + row * (subdivisions + 1));
  642. }
  643. }
  644. // Result
  645. var vertexData = new VertexData();
  646. vertexData.indices = indices;
  647. vertexData.positions = positions;
  648. vertexData.normals = normals;
  649. vertexData.uvs = uvs;
  650. return vertexData;
  651. };
  652. VertexData.CreateTiledGround = function (xmin, zmin, xmax, zmax, subdivisions, precision) {
  653. if (subdivisions === void 0) { subdivisions = { w: 1, h: 1 }; }
  654. if (precision === void 0) { precision = { w: 1, h: 1 }; }
  655. var indices = [];
  656. var positions = [];
  657. var normals = [];
  658. var uvs = [];
  659. var row, col, tileRow, tileCol;
  660. subdivisions.h = (subdivisions.w < 1) ? 1 : subdivisions.h;
  661. subdivisions.w = (subdivisions.w < 1) ? 1 : subdivisions.w;
  662. precision.w = (precision.w < 1) ? 1 : precision.w;
  663. precision.h = (precision.h < 1) ? 1 : precision.h;
  664. var tileSize = {
  665. 'w': (xmax - xmin) / subdivisions.w,
  666. 'h': (zmax - zmin) / subdivisions.h
  667. };
  668. function applyTile(xTileMin, zTileMin, xTileMax, zTileMax) {
  669. // Indices
  670. var base = positions.length / 3;
  671. var rowLength = precision.w + 1;
  672. for (row = 0; row < precision.h; row++) {
  673. for (col = 0; col < precision.w; col++) {
  674. var square = [
  675. base + col + row * rowLength,
  676. base + (col + 1) + row * rowLength,
  677. base + (col + 1) + (row + 1) * rowLength,
  678. base + col + (row + 1) * rowLength
  679. ];
  680. indices.push(square[1]);
  681. indices.push(square[2]);
  682. indices.push(square[3]);
  683. indices.push(square[0]);
  684. indices.push(square[1]);
  685. indices.push(square[3]);
  686. }
  687. }
  688. // Position, normals and uvs
  689. var position = BABYLON.Vector3.Zero();
  690. var normal = new BABYLON.Vector3(0, 1.0, 0);
  691. for (row = 0; row <= precision.h; row++) {
  692. position.z = (row * (zTileMax - zTileMin)) / precision.h + zTileMin;
  693. for (col = 0; col <= precision.w; col++) {
  694. position.x = (col * (xTileMax - xTileMin)) / precision.w + xTileMin;
  695. position.y = 0;
  696. positions.push(position.x, position.y, position.z);
  697. normals.push(normal.x, normal.y, normal.z);
  698. uvs.push(col / precision.w, row / precision.h);
  699. }
  700. }
  701. }
  702. for (tileRow = 0; tileRow < subdivisions.h; tileRow++) {
  703. for (tileCol = 0; tileCol < subdivisions.w; tileCol++) {
  704. applyTile(xmin + tileCol * tileSize.w, zmin + tileRow * tileSize.h, xmin + (tileCol + 1) * tileSize.w, zmin + (tileRow + 1) * tileSize.h);
  705. }
  706. }
  707. // Result
  708. var vertexData = new VertexData();
  709. vertexData.indices = indices;
  710. vertexData.positions = positions;
  711. vertexData.normals = normals;
  712. vertexData.uvs = uvs;
  713. return vertexData;
  714. };
  715. VertexData.CreateGroundFromHeightMap = function (width, height, subdivisions, minHeight, maxHeight, buffer, bufferWidth, bufferHeight) {
  716. var indices = [];
  717. var positions = [];
  718. var normals = [];
  719. var uvs = [];
  720. var row, col;
  721. for (row = 0; row <= subdivisions; row++) {
  722. for (col = 0; col <= subdivisions; col++) {
  723. var position = new BABYLON.Vector3((col * width) / subdivisions - (width / 2.0), 0, ((subdivisions - row) * height) / subdivisions - (height / 2.0));
  724. // Compute height
  725. var heightMapX = (((position.x + width / 2) / width) * (bufferWidth - 1)) | 0;
  726. var heightMapY = ((1.0 - (position.z + height / 2) / height) * (bufferHeight - 1)) | 0;
  727. var pos = (heightMapX + heightMapY * bufferWidth) * 4;
  728. var r = buffer[pos] / 255.0;
  729. var g = buffer[pos + 1] / 255.0;
  730. var b = buffer[pos + 2] / 255.0;
  731. var gradient = r * 0.3 + g * 0.59 + b * 0.11;
  732. position.y = minHeight + (maxHeight - minHeight) * gradient;
  733. // Add vertex
  734. positions.push(position.x, position.y, position.z);
  735. normals.push(0, 0, 0);
  736. uvs.push(col / subdivisions, 1.0 - row / subdivisions);
  737. }
  738. }
  739. for (row = 0; row < subdivisions; row++) {
  740. for (col = 0; col < subdivisions; col++) {
  741. indices.push(col + 1 + (row + 1) * (subdivisions + 1));
  742. indices.push(col + 1 + row * (subdivisions + 1));
  743. indices.push(col + row * (subdivisions + 1));
  744. indices.push(col + (row + 1) * (subdivisions + 1));
  745. indices.push(col + 1 + (row + 1) * (subdivisions + 1));
  746. indices.push(col + row * (subdivisions + 1));
  747. }
  748. }
  749. // Normals
  750. VertexData.ComputeNormals(positions, indices, normals);
  751. // Result
  752. var vertexData = new VertexData();
  753. vertexData.indices = indices;
  754. vertexData.positions = positions;
  755. vertexData.normals = normals;
  756. vertexData.uvs = uvs;
  757. return vertexData;
  758. };
  759. VertexData.CreatePlane = function (size) {
  760. var indices = [];
  761. var positions = [];
  762. var normals = [];
  763. var uvs = [];
  764. size = size || 1;
  765. // Vertices
  766. var halfSize = size / 2.0;
  767. positions.push(-halfSize, -halfSize, 0);
  768. normals.push(0, 0, -1.0);
  769. uvs.push(0.0, 0.0);
  770. positions.push(halfSize, -halfSize, 0);
  771. normals.push(0, 0, -1.0);
  772. uvs.push(1.0, 0.0);
  773. positions.push(halfSize, halfSize, 0);
  774. normals.push(0, 0, -1.0);
  775. uvs.push(1.0, 1.0);
  776. positions.push(-halfSize, halfSize, 0);
  777. normals.push(0, 0, -1.0);
  778. uvs.push(0.0, 1.0);
  779. // Indices
  780. indices.push(0);
  781. indices.push(1);
  782. indices.push(2);
  783. indices.push(0);
  784. indices.push(2);
  785. indices.push(3);
  786. // Result
  787. var vertexData = new VertexData();
  788. vertexData.indices = indices;
  789. vertexData.positions = positions;
  790. vertexData.normals = normals;
  791. vertexData.uvs = uvs;
  792. return vertexData;
  793. };
  794. // based on http://code.google.com/p/away3d/source/browse/trunk/fp10/Away3D/src/away3d/primitives/TorusKnot.as?spec=svn2473&r=2473
  795. VertexData.CreateTorusKnot = function (radius, tube, radialSegments, tubularSegments, p, q) {
  796. var indices = [];
  797. var positions = [];
  798. var normals = [];
  799. var uvs = [];
  800. radius = radius || 2;
  801. tube = tube || 0.5;
  802. radialSegments = radialSegments || 32;
  803. tubularSegments = tubularSegments || 32;
  804. p = p || 2;
  805. q = q || 3;
  806. // Helper
  807. var getPos = function (angle) {
  808. var cu = Math.cos(angle);
  809. var su = Math.sin(angle);
  810. var quOverP = q / p * angle;
  811. var cs = Math.cos(quOverP);
  812. var tx = radius * (2 + cs) * 0.5 * cu;
  813. var ty = radius * (2 + cs) * su * 0.5;
  814. var tz = radius * Math.sin(quOverP) * 0.5;
  815. return new BABYLON.Vector3(tx, ty, tz);
  816. };
  817. for (var i = 0; i <= radialSegments; i++) {
  818. var modI = i % radialSegments;
  819. var u = modI / radialSegments * 2 * p * Math.PI;
  820. var p1 = getPos(u);
  821. var p2 = getPos(u + 0.01);
  822. var tang = p2.subtract(p1);
  823. var n = p2.add(p1);
  824. var bitan = BABYLON.Vector3.Cross(tang, n);
  825. n = BABYLON.Vector3.Cross(bitan, tang);
  826. bitan.normalize();
  827. n.normalize();
  828. for (var j = 0; j < tubularSegments; j++) {
  829. var modJ = j % tubularSegments;
  830. var v = modJ / tubularSegments * 2 * Math.PI;
  831. var cx = -tube * Math.cos(v);
  832. var cy = tube * Math.sin(v);
  833. positions.push(p1.x + cx * n.x + cy * bitan.x);
  834. positions.push(p1.y + cx * n.y + cy * bitan.y);
  835. positions.push(p1.z + cx * n.z + cy * bitan.z);
  836. uvs.push(i / radialSegments);
  837. uvs.push(j / tubularSegments);
  838. }
  839. }
  840. for (i = 0; i < radialSegments; i++) {
  841. for (j = 0; j < tubularSegments; j++) {
  842. var jNext = (j + 1) % tubularSegments;
  843. var a = i * tubularSegments + j;
  844. var b = (i + 1) * tubularSegments + j;
  845. var c = (i + 1) * tubularSegments + jNext;
  846. var d = i * tubularSegments + jNext;
  847. indices.push(d);
  848. indices.push(b);
  849. indices.push(a);
  850. indices.push(d);
  851. indices.push(c);
  852. indices.push(b);
  853. }
  854. }
  855. // Normals
  856. VertexData.ComputeNormals(positions, indices, normals);
  857. // Result
  858. var vertexData = new VertexData();
  859. vertexData.indices = indices;
  860. vertexData.positions = positions;
  861. vertexData.normals = normals;
  862. vertexData.uvs = uvs;
  863. return vertexData;
  864. };
  865. // Tools
  866. VertexData.ComputeNormals = function (positions, indices, normals) {
  867. var positionVectors = [];
  868. var facesOfVertices = [];
  869. var index;
  870. for (index = 0; index < positions.length; index += 3) {
  871. var vector3 = new BABYLON.Vector3(positions[index], positions[index + 1], positions[index + 2]);
  872. positionVectors.push(vector3);
  873. facesOfVertices.push([]);
  874. }
  875. // Compute normals
  876. var facesNormals = [];
  877. for (index = 0; index < indices.length / 3; index++) {
  878. var i1 = indices[index * 3];
  879. var i2 = indices[index * 3 + 1];
  880. var i3 = indices[index * 3 + 2];
  881. var p1 = positionVectors[i1];
  882. var p2 = positionVectors[i2];
  883. var p3 = positionVectors[i3];
  884. var p1p2 = p1.subtract(p2);
  885. var p3p2 = p3.subtract(p2);
  886. facesNormals[index] = BABYLON.Vector3.Normalize(BABYLON.Vector3.Cross(p1p2, p3p2));
  887. facesOfVertices[i1].push(index);
  888. facesOfVertices[i2].push(index);
  889. facesOfVertices[i3].push(index);
  890. }
  891. for (index = 0; index < positionVectors.length; index++) {
  892. var faces = facesOfVertices[index];
  893. var normal = BABYLON.Vector3.Zero();
  894. for (var faceIndex = 0; faceIndex < faces.length; faceIndex++) {
  895. normal.addInPlace(facesNormals[faces[faceIndex]]);
  896. }
  897. normal = BABYLON.Vector3.Normalize(normal.scale(1.0 / faces.length));
  898. normals[index * 3] = normal.x;
  899. normals[index * 3 + 1] = normal.y;
  900. normals[index * 3 + 2] = normal.z;
  901. }
  902. };
  903. return VertexData;
  904. })();
  905. BABYLON.VertexData = VertexData;
  906. })(BABYLON || (BABYLON = {}));
  907. //# sourceMappingURL=babylon.mesh.vertexData.js.map