babylon.mesh.vertexData.js 43 KB

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