babylon.mesh.vertexData.js 42 KB

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