babylon.mesh.vertexData.js 47 KB

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