babylon.mesh.vertexData.js 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226
  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.uvs2 = data;
  19. break;
  20. case BABYLON.VertexBuffer.UV3Kind:
  21. this.uvs3 = data;
  22. break;
  23. case BABYLON.VertexBuffer.UV4Kind:
  24. this.uvs4 = data;
  25. break;
  26. case BABYLON.VertexBuffer.UV5Kind:
  27. this.uvs5 = data;
  28. break;
  29. case BABYLON.VertexBuffer.UV6Kind:
  30. this.uvs6 = data;
  31. break;
  32. case BABYLON.VertexBuffer.ColorKind:
  33. this.colors = data;
  34. break;
  35. case BABYLON.VertexBuffer.MatricesIndicesKind:
  36. this.matricesIndices = data;
  37. break;
  38. case BABYLON.VertexBuffer.MatricesWeightsKind:
  39. this.matricesWeights = data;
  40. break;
  41. }
  42. };
  43. VertexData.prototype.applyToMesh = function (mesh, updatable) {
  44. this._applyTo(mesh, updatable);
  45. };
  46. VertexData.prototype.applyToGeometry = function (geometry, updatable) {
  47. this._applyTo(geometry, updatable);
  48. };
  49. VertexData.prototype.updateMesh = function (mesh, updateExtends, makeItUnique) {
  50. this._update(mesh);
  51. };
  52. VertexData.prototype.updateGeometry = function (geometry, updateExtends, makeItUnique) {
  53. this._update(geometry);
  54. };
  55. VertexData.prototype._applyTo = function (meshOrGeometry, updatable) {
  56. if (this.positions) {
  57. meshOrGeometry.setVerticesData(BABYLON.VertexBuffer.PositionKind, this.positions, updatable);
  58. }
  59. if (this.normals) {
  60. meshOrGeometry.setVerticesData(BABYLON.VertexBuffer.NormalKind, this.normals, updatable);
  61. }
  62. if (this.uvs) {
  63. meshOrGeometry.setVerticesData(BABYLON.VertexBuffer.UVKind, this.uvs, updatable);
  64. }
  65. if (this.uvs2) {
  66. meshOrGeometry.setVerticesData(BABYLON.VertexBuffer.UV2Kind, this.uvs2, updatable);
  67. }
  68. if (this.uvs3) {
  69. meshOrGeometry.setVerticesData(BABYLON.VertexBuffer.UV3Kind, this.uvs3, updatable);
  70. }
  71. if (this.uvs4) {
  72. meshOrGeometry.setVerticesData(BABYLON.VertexBuffer.UV4Kind, this.uvs4, updatable);
  73. }
  74. if (this.uvs5) {
  75. meshOrGeometry.setVerticesData(BABYLON.VertexBuffer.UV5Kind, this.uvs5, updatable);
  76. }
  77. if (this.uvs6) {
  78. meshOrGeometry.setVerticesData(BABYLON.VertexBuffer.UV6Kind, this.uvs6, updatable);
  79. }
  80. if (this.colors) {
  81. meshOrGeometry.setVerticesData(BABYLON.VertexBuffer.ColorKind, this.colors, updatable);
  82. }
  83. if (this.matricesIndices) {
  84. meshOrGeometry.setVerticesData(BABYLON.VertexBuffer.MatricesIndicesKind, this.matricesIndices, updatable);
  85. }
  86. if (this.matricesWeights) {
  87. meshOrGeometry.setVerticesData(BABYLON.VertexBuffer.MatricesWeightsKind, this.matricesWeights, updatable);
  88. }
  89. if (this.indices) {
  90. meshOrGeometry.setIndices(this.indices);
  91. }
  92. };
  93. VertexData.prototype._update = function (meshOrGeometry, updateExtends, makeItUnique) {
  94. if (this.positions) {
  95. meshOrGeometry.updateVerticesData(BABYLON.VertexBuffer.PositionKind, this.positions, updateExtends, makeItUnique);
  96. }
  97. if (this.normals) {
  98. meshOrGeometry.updateVerticesData(BABYLON.VertexBuffer.NormalKind, this.normals, updateExtends, makeItUnique);
  99. }
  100. if (this.uvs) {
  101. meshOrGeometry.updateVerticesData(BABYLON.VertexBuffer.UVKind, this.uvs, updateExtends, makeItUnique);
  102. }
  103. if (this.uvs2) {
  104. meshOrGeometry.updateVerticesData(BABYLON.VertexBuffer.UV2Kind, this.uvs2, updateExtends, makeItUnique);
  105. }
  106. if (this.uvs3) {
  107. meshOrGeometry.updateVerticesData(BABYLON.VertexBuffer.UV3Kind, this.uvs3, updateExtends, makeItUnique);
  108. }
  109. if (this.uvs4) {
  110. meshOrGeometry.updateVerticesData(BABYLON.VertexBuffer.UV4Kind, this.uvs4, updateExtends, makeItUnique);
  111. }
  112. if (this.uvs5) {
  113. meshOrGeometry.updateVerticesData(BABYLON.VertexBuffer.UV5Kind, this.uvs5, updateExtends, makeItUnique);
  114. }
  115. if (this.uvs6) {
  116. meshOrGeometry.updateVerticesData(BABYLON.VertexBuffer.UV6Kind, this.uvs6, updateExtends, makeItUnique);
  117. }
  118. if (this.colors) {
  119. meshOrGeometry.updateVerticesData(BABYLON.VertexBuffer.ColorKind, this.colors, updateExtends, makeItUnique);
  120. }
  121. if (this.matricesIndices) {
  122. meshOrGeometry.updateVerticesData(BABYLON.VertexBuffer.MatricesIndicesKind, this.matricesIndices, updateExtends, makeItUnique);
  123. }
  124. if (this.matricesWeights) {
  125. meshOrGeometry.updateVerticesData(BABYLON.VertexBuffer.MatricesWeightsKind, this.matricesWeights, updateExtends, makeItUnique);
  126. }
  127. if (this.indices) {
  128. meshOrGeometry.setIndices(this.indices);
  129. }
  130. };
  131. VertexData.prototype.transform = function (matrix) {
  132. var transformed = BABYLON.Vector3.Zero();
  133. var index;
  134. if (this.positions) {
  135. var position = BABYLON.Vector3.Zero();
  136. for (index = 0; index < this.positions.length; index += 3) {
  137. BABYLON.Vector3.FromArrayToRef(this.positions, index, position);
  138. BABYLON.Vector3.TransformCoordinatesToRef(position, matrix, transformed);
  139. this.positions[index] = transformed.x;
  140. this.positions[index + 1] = transformed.y;
  141. this.positions[index + 2] = transformed.z;
  142. }
  143. }
  144. if (this.normals) {
  145. var normal = BABYLON.Vector3.Zero();
  146. for (index = 0; index < this.normals.length; index += 3) {
  147. BABYLON.Vector3.FromArrayToRef(this.normals, index, normal);
  148. BABYLON.Vector3.TransformNormalToRef(normal, matrix, transformed);
  149. this.normals[index] = transformed.x;
  150. this.normals[index + 1] = transformed.y;
  151. this.normals[index + 2] = transformed.z;
  152. }
  153. }
  154. };
  155. VertexData.prototype.merge = function (other) {
  156. var index;
  157. if (other.indices) {
  158. if (!this.indices) {
  159. this.indices = [];
  160. }
  161. var offset = this.positions ? this.positions.length / 3 : 0;
  162. for (index = 0; index < other.indices.length; index++) {
  163. this.indices.push(other.indices[index] + offset);
  164. }
  165. }
  166. if (other.positions) {
  167. if (!this.positions) {
  168. this.positions = [];
  169. }
  170. for (index = 0; index < other.positions.length; index++) {
  171. this.positions.push(other.positions[index]);
  172. }
  173. }
  174. if (other.normals) {
  175. if (!this.normals) {
  176. this.normals = [];
  177. }
  178. for (index = 0; index < other.normals.length; index++) {
  179. this.normals.push(other.normals[index]);
  180. }
  181. }
  182. if (other.uvs) {
  183. if (!this.uvs) {
  184. this.uvs = [];
  185. }
  186. for (index = 0; index < other.uvs.length; index++) {
  187. this.uvs.push(other.uvs[index]);
  188. }
  189. }
  190. if (other.uvs2) {
  191. if (!this.uvs2) {
  192. this.uvs2 = [];
  193. }
  194. for (index = 0; index < other.uvs2.length; index++) {
  195. this.uvs2.push(other.uvs2[index]);
  196. }
  197. }
  198. if (other.uvs3) {
  199. if (!this.uvs3) {
  200. this.uvs3 = [];
  201. }
  202. for (index = 0; index < other.uvs3.length; index++) {
  203. this.uvs3.push(other.uvs3[index]);
  204. }
  205. }
  206. if (other.uvs4) {
  207. if (!this.uvs4) {
  208. this.uvs4 = [];
  209. }
  210. for (index = 0; index < other.uvs4.length; index++) {
  211. this.uvs4.push(other.uvs4[index]);
  212. }
  213. }
  214. if (other.uvs5) {
  215. if (!this.uvs5) {
  216. this.uvs5 = [];
  217. }
  218. for (index = 0; index < other.uvs5.length; index++) {
  219. this.uvs5.push(other.uvs5[index]);
  220. }
  221. }
  222. if (other.uvs6) {
  223. if (!this.uvs6) {
  224. this.uvs6 = [];
  225. }
  226. for (index = 0; index < other.uvs6.length; index++) {
  227. this.uvs6.push(other.uvs6[index]);
  228. }
  229. }
  230. if (other.matricesIndices) {
  231. if (!this.matricesIndices) {
  232. this.matricesIndices = [];
  233. }
  234. for (index = 0; index < other.matricesIndices.length; index++) {
  235. this.matricesIndices.push(other.matricesIndices[index]);
  236. }
  237. }
  238. if (other.matricesWeights) {
  239. if (!this.matricesWeights) {
  240. this.matricesWeights = [];
  241. }
  242. for (index = 0; index < other.matricesWeights.length; index++) {
  243. this.matricesWeights.push(other.matricesWeights[index]);
  244. }
  245. }
  246. if (other.colors) {
  247. if (!this.colors) {
  248. this.colors = [];
  249. }
  250. for (index = 0; index < other.colors.length; index++) {
  251. this.colors.push(other.colors[index]);
  252. }
  253. }
  254. };
  255. // Statics
  256. VertexData.ExtractFromMesh = function (mesh, copyWhenShared) {
  257. return VertexData._ExtractFrom(mesh, copyWhenShared);
  258. };
  259. VertexData.ExtractFromGeometry = function (geometry, copyWhenShared) {
  260. return VertexData._ExtractFrom(geometry, copyWhenShared);
  261. };
  262. VertexData._ExtractFrom = function (meshOrGeometry, copyWhenShared) {
  263. var result = new VertexData();
  264. if (meshOrGeometry.isVerticesDataPresent(BABYLON.VertexBuffer.PositionKind)) {
  265. result.positions = meshOrGeometry.getVerticesData(BABYLON.VertexBuffer.PositionKind, copyWhenShared);
  266. }
  267. if (meshOrGeometry.isVerticesDataPresent(BABYLON.VertexBuffer.NormalKind)) {
  268. result.normals = meshOrGeometry.getVerticesData(BABYLON.VertexBuffer.NormalKind, copyWhenShared);
  269. }
  270. if (meshOrGeometry.isVerticesDataPresent(BABYLON.VertexBuffer.UVKind)) {
  271. result.uvs = meshOrGeometry.getVerticesData(BABYLON.VertexBuffer.UVKind, copyWhenShared);
  272. }
  273. if (meshOrGeometry.isVerticesDataPresent(BABYLON.VertexBuffer.UV2Kind)) {
  274. result.uvs2 = meshOrGeometry.getVerticesData(BABYLON.VertexBuffer.UV2Kind, copyWhenShared);
  275. }
  276. if (meshOrGeometry.isVerticesDataPresent(BABYLON.VertexBuffer.UV3Kind)) {
  277. result.uvs3 = meshOrGeometry.getVerticesData(BABYLON.VertexBuffer.UV3Kind, copyWhenShared);
  278. }
  279. if (meshOrGeometry.isVerticesDataPresent(BABYLON.VertexBuffer.UV4Kind)) {
  280. result.uvs4 = meshOrGeometry.getVerticesData(BABYLON.VertexBuffer.UV4Kind, copyWhenShared);
  281. }
  282. if (meshOrGeometry.isVerticesDataPresent(BABYLON.VertexBuffer.UV5Kind)) {
  283. result.uvs5 = meshOrGeometry.getVerticesData(BABYLON.VertexBuffer.UV5Kind, copyWhenShared);
  284. }
  285. if (meshOrGeometry.isVerticesDataPresent(BABYLON.VertexBuffer.UV6Kind)) {
  286. result.uvs6 = meshOrGeometry.getVerticesData(BABYLON.VertexBuffer.UV6Kind, copyWhenShared);
  287. }
  288. if (meshOrGeometry.isVerticesDataPresent(BABYLON.VertexBuffer.ColorKind)) {
  289. result.colors = meshOrGeometry.getVerticesData(BABYLON.VertexBuffer.ColorKind, copyWhenShared);
  290. }
  291. if (meshOrGeometry.isVerticesDataPresent(BABYLON.VertexBuffer.MatricesIndicesKind)) {
  292. result.matricesIndices = meshOrGeometry.getVerticesData(BABYLON.VertexBuffer.MatricesIndicesKind, copyWhenShared);
  293. }
  294. if (meshOrGeometry.isVerticesDataPresent(BABYLON.VertexBuffer.MatricesWeightsKind)) {
  295. result.matricesWeights = meshOrGeometry.getVerticesData(BABYLON.VertexBuffer.MatricesWeightsKind, copyWhenShared);
  296. }
  297. result.indices = meshOrGeometry.getIndices(copyWhenShared);
  298. return result;
  299. };
  300. VertexData.CreateRibbon = function (options) {
  301. var pathArray = options.pathArray;
  302. var closeArray = options.closeArray || false;
  303. var closePath = options.closePath || false;
  304. var defaultOffset = Math.floor(pathArray[0].length / 2);
  305. var offset = options.offset || defaultOffset;
  306. offset = offset > defaultOffset ? defaultOffset : Math.floor(offset); // offset max allowed : defaultOffset
  307. var sideOrientation = (options.sideOrientation === 0) ? 0 : options.sideOrientation || BABYLON.Mesh.DEFAULTSIDE;
  308. var positions = [];
  309. var indices = [];
  310. var normals = [];
  311. var uvs = [];
  312. var us = []; // us[path_id] = [uDist1, uDist2, uDist3 ... ] distances between points on path path_id
  313. var vs = []; // vs[i] = [vDist1, vDist2, vDist3, ... ] distances between points i of consecutives paths from pathArray
  314. var uTotalDistance = []; // uTotalDistance[p] : total distance of path p
  315. var vTotalDistance = []; // vTotalDistance[i] : total distance between points i of first and last path from pathArray
  316. var minlg; // minimal length among all paths from pathArray
  317. var lg = []; // array of path lengths : nb of vertex per path
  318. var idx = []; // array of path indexes : index of each path (first vertex) in the total vertex number
  319. var p; // path iterator
  320. var i; // point iterator
  321. var j; // point iterator
  322. // if single path in pathArray
  323. if (pathArray.length < 2) {
  324. var ar1 = [];
  325. var ar2 = [];
  326. for (i = 0; i < pathArray[0].length - offset; i++) {
  327. ar1.push(pathArray[0][i]);
  328. ar2.push(pathArray[0][i + offset]);
  329. }
  330. pathArray = [ar1, ar2];
  331. }
  332. // positions and horizontal distances (u)
  333. var idc = 0;
  334. var closePathCorr = (closePath) ? 1 : 0;
  335. var path;
  336. var l;
  337. minlg = pathArray[0].length;
  338. var vectlg;
  339. var dist;
  340. for (p = 0; p < pathArray.length; p++) {
  341. uTotalDistance[p] = 0;
  342. us[p] = [0];
  343. path = pathArray[p];
  344. l = path.length;
  345. minlg = (minlg < l) ? minlg : l;
  346. j = 0;
  347. while (j < l) {
  348. positions.push(path[j].x, path[j].y, path[j].z);
  349. if (j > 0) {
  350. vectlg = path[j].subtract(path[j - 1]).length();
  351. dist = vectlg + uTotalDistance[p];
  352. us[p].push(dist);
  353. uTotalDistance[p] = dist;
  354. }
  355. j++;
  356. }
  357. if (closePath) {
  358. j--;
  359. positions.push(path[0].x, path[0].y, path[0].z);
  360. vectlg = path[j].subtract(path[0]).length();
  361. dist = vectlg + uTotalDistance[p];
  362. us[p].push(dist);
  363. uTotalDistance[p] = dist;
  364. }
  365. lg[p] = l + closePathCorr;
  366. idx[p] = idc;
  367. idc += (l + closePathCorr);
  368. }
  369. // vertical distances (v)
  370. var path1;
  371. var path2;
  372. var vertex1;
  373. var vertex2;
  374. for (i = 0; i < minlg + closePathCorr; i++) {
  375. vTotalDistance[i] = 0;
  376. vs[i] = [0];
  377. for (p = 0; p < pathArray.length - 1; p++) {
  378. path1 = pathArray[p];
  379. path2 = pathArray[p + 1];
  380. if (i === minlg) {
  381. vertex1 = path1[0];
  382. vertex2 = path2[0];
  383. }
  384. else {
  385. vertex1 = path1[i];
  386. vertex2 = path2[i];
  387. }
  388. vectlg = vertex2.subtract(vertex1).length();
  389. dist = vectlg + vTotalDistance[i];
  390. vs[i].push(dist);
  391. vTotalDistance[i] = dist;
  392. }
  393. if (closeArray) {
  394. path1 = pathArray[p];
  395. path2 = pathArray[0];
  396. vectlg = path2[i].subtract(path1[i]).length();
  397. dist = vectlg + vTotalDistance[i];
  398. vTotalDistance[i] = dist;
  399. }
  400. }
  401. // uvs
  402. var u;
  403. var v;
  404. for (p = 0; p < pathArray.length; p++) {
  405. for (i = 0; i < minlg + closePathCorr; i++) {
  406. u = us[p][i] / uTotalDistance[p];
  407. v = vs[i][p] / vTotalDistance[i];
  408. uvs.push(u, v);
  409. }
  410. }
  411. // indices
  412. p = 0; // path index
  413. var pi = 0; // positions array index
  414. var l1 = lg[p] - 1; // path1 length
  415. var l2 = lg[p + 1] - 1; // path2 length
  416. var min = (l1 < l2) ? l1 : l2; // current path stop index
  417. var shft = idx[1] - idx[0]; // shift
  418. var path1nb = closeArray ? lg.length : lg.length - 1; // number of path1 to iterate on
  419. while (pi <= min && p < path1nb) {
  420. // 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
  421. indices.push(pi, pi + shft, pi + 1);
  422. indices.push(pi + shft + 1, pi + 1, pi + shft);
  423. pi += 1;
  424. if (pi === min) {
  425. p++;
  426. if (p === lg.length - 1) {
  427. shft = idx[0] - idx[p];
  428. l1 = lg[p] - 1;
  429. l2 = lg[0] - 1;
  430. }
  431. else {
  432. shft = idx[p + 1] - idx[p];
  433. l1 = lg[p] - 1;
  434. l2 = lg[p + 1] - 1;
  435. }
  436. pi = idx[p];
  437. min = (l1 < l2) ? l1 + pi : l2 + pi;
  438. }
  439. }
  440. // normals
  441. VertexData.ComputeNormals(positions, indices, normals);
  442. if (closePath) {
  443. var indexFirst = 0;
  444. var indexLast = 0;
  445. for (p = 0; p < pathArray.length; p++) {
  446. indexFirst = idx[p] * 3;
  447. if (p + 1 < pathArray.length) {
  448. indexLast = (idx[p + 1] - 1) * 3;
  449. }
  450. else {
  451. indexLast = normals.length - 3;
  452. }
  453. normals[indexFirst] = (normals[indexFirst] + normals[indexLast]) * 0.5;
  454. normals[indexFirst + 1] = (normals[indexFirst + 1] + normals[indexLast + 1]) * 0.5;
  455. normals[indexFirst + 2] = (normals[indexFirst + 2] + normals[indexLast + 2]) * 0.5;
  456. normals[indexLast] = normals[indexFirst];
  457. normals[indexLast + 1] = normals[indexFirst + 1];
  458. normals[indexLast + 2] = normals[indexFirst + 2];
  459. }
  460. }
  461. // sides
  462. VertexData._ComputeSides(sideOrientation, positions, indices, normals, uvs);
  463. // Result
  464. var vertexData = new VertexData();
  465. vertexData.indices = indices;
  466. vertexData.positions = positions;
  467. vertexData.normals = normals;
  468. vertexData.uvs = uvs;
  469. if (closePath) {
  470. vertexData._idx = idx;
  471. }
  472. return vertexData;
  473. };
  474. VertexData.CreateBox = function (options) {
  475. var normalsSource = [
  476. new BABYLON.Vector3(0, 0, 1),
  477. new BABYLON.Vector3(0, 0, -1),
  478. new BABYLON.Vector3(1, 0, 0),
  479. new BABYLON.Vector3(-1, 0, 0),
  480. new BABYLON.Vector3(0, 1, 0),
  481. new BABYLON.Vector3(0, -1, 0)
  482. ];
  483. var indices = [];
  484. var positions = [];
  485. var normals = [];
  486. var uvs = [];
  487. var width = options.width || options.size || 1;
  488. var height = options.height || options.size || 1;
  489. var depth = options.depth || options.size || 1;
  490. var sideOrientation = (options.sideOrientation === 0) ? 0 : options.sideOrientation || BABYLON.Mesh.DEFAULTSIDE;
  491. var faceUV = options.faceUV || new Array(6);
  492. var faceColors;
  493. var colors = [];
  494. if (options.faceColors) {
  495. faceColors = options.faceColors;
  496. }
  497. // default face colors and UV if undefined
  498. for (var f = 0; f < 6; f++) {
  499. if (faceUV[f] === undefined) {
  500. faceUV[f] = new BABYLON.Vector4(0, 0, 1, 1);
  501. }
  502. if (faceColors && faceColors[f] === undefined) {
  503. faceColors[f] = new BABYLON.Color4(1, 1, 1, 1);
  504. }
  505. }
  506. var scaleVector = new BABYLON.Vector3(width / 2, height / 2, depth / 2);
  507. // Create each face in turn.
  508. for (var index = 0; index < normalsSource.length; index++) {
  509. var normal = normalsSource[index];
  510. // Get two vectors perpendicular to the face normal and to each other.
  511. var side1 = new BABYLON.Vector3(normal.y, normal.z, normal.x);
  512. var side2 = BABYLON.Vector3.Cross(normal, side1);
  513. // Six indices (two triangles) per face.
  514. var verticesLength = positions.length / 3;
  515. indices.push(verticesLength);
  516. indices.push(verticesLength + 1);
  517. indices.push(verticesLength + 2);
  518. indices.push(verticesLength);
  519. indices.push(verticesLength + 2);
  520. indices.push(verticesLength + 3);
  521. // Four vertices per face.
  522. var vertex = normal.subtract(side1).subtract(side2).multiply(scaleVector);
  523. positions.push(vertex.x, vertex.y, vertex.z);
  524. normals.push(normal.x, normal.y, normal.z);
  525. uvs.push(faceUV[index].z, faceUV[index].w);
  526. if (faceColors) {
  527. colors.push(faceColors[index].r, faceColors[index].g, faceColors[index].b, faceColors[index].a);
  528. }
  529. vertex = normal.subtract(side1).add(side2).multiply(scaleVector);
  530. positions.push(vertex.x, vertex.y, vertex.z);
  531. normals.push(normal.x, normal.y, normal.z);
  532. uvs.push(faceUV[index].x, faceUV[index].w);
  533. if (faceColors) {
  534. colors.push(faceColors[index].r, faceColors[index].g, faceColors[index].b, faceColors[index].a);
  535. }
  536. vertex = normal.add(side1).add(side2).multiply(scaleVector);
  537. positions.push(vertex.x, vertex.y, vertex.z);
  538. normals.push(normal.x, normal.y, normal.z);
  539. uvs.push(faceUV[index].x, faceUV[index].y);
  540. if (faceColors) {
  541. colors.push(faceColors[index].r, faceColors[index].g, faceColors[index].b, faceColors[index].a);
  542. }
  543. vertex = normal.add(side1).subtract(side2).multiply(scaleVector);
  544. positions.push(vertex.x, vertex.y, vertex.z);
  545. normals.push(normal.x, normal.y, normal.z);
  546. uvs.push(faceUV[index].z, faceUV[index].y);
  547. if (faceColors) {
  548. colors.push(faceColors[index].r, faceColors[index].g, faceColors[index].b, faceColors[index].a);
  549. }
  550. }
  551. // sides
  552. VertexData._ComputeSides(sideOrientation, positions, indices, normals, uvs);
  553. // Result
  554. var vertexData = new VertexData();
  555. vertexData.indices = indices;
  556. vertexData.positions = positions;
  557. vertexData.normals = normals;
  558. vertexData.uvs = uvs;
  559. if (faceColors) {
  560. var totalColors = (sideOrientation === BABYLON.Mesh.DOUBLESIDE) ? colors.concat(colors) : colors;
  561. vertexData.colors = totalColors;
  562. }
  563. return vertexData;
  564. };
  565. VertexData.CreateSphere = function (options) {
  566. var segments = options.segments || 32;
  567. var diameterX = options.diameterX || options.diameter || 1;
  568. var diameterY = options.diameterY || options.diameter || 1;
  569. var diameterZ = options.diameterZ || options.diameter || 1;
  570. var sideOrientation = (options.sideOrientation === 0) ? 0 : options.sideOrientation || BABYLON.Mesh.DEFAULTSIDE;
  571. var radius = new BABYLON.Vector3(diameterX / 2, diameterY / 2, diameterZ / 2);
  572. var totalZRotationSteps = 2 + segments;
  573. var totalYRotationSteps = 2 * totalZRotationSteps;
  574. var indices = [];
  575. var positions = [];
  576. var normals = [];
  577. var uvs = [];
  578. for (var zRotationStep = 0; zRotationStep <= totalZRotationSteps; zRotationStep++) {
  579. var normalizedZ = zRotationStep / totalZRotationSteps;
  580. var angleZ = (normalizedZ * Math.PI);
  581. for (var yRotationStep = 0; yRotationStep <= totalYRotationSteps; yRotationStep++) {
  582. var normalizedY = yRotationStep / totalYRotationSteps;
  583. var angleY = normalizedY * Math.PI * 2;
  584. var rotationZ = BABYLON.Matrix.RotationZ(-angleZ);
  585. var rotationY = BABYLON.Matrix.RotationY(angleY);
  586. var afterRotZ = BABYLON.Vector3.TransformCoordinates(BABYLON.Vector3.Up(), rotationZ);
  587. var complete = BABYLON.Vector3.TransformCoordinates(afterRotZ, rotationY);
  588. var vertex = complete.multiply(radius);
  589. var normal = BABYLON.Vector3.Normalize(vertex);
  590. positions.push(vertex.x, vertex.y, vertex.z);
  591. normals.push(normal.x, normal.y, normal.z);
  592. uvs.push(normalizedY, normalizedZ);
  593. }
  594. if (zRotationStep > 0) {
  595. var verticesCount = positions.length / 3;
  596. for (var firstIndex = verticesCount - 2 * (totalYRotationSteps + 1); (firstIndex + totalYRotationSteps + 2) < verticesCount; firstIndex++) {
  597. indices.push((firstIndex));
  598. indices.push((firstIndex + 1));
  599. indices.push(firstIndex + totalYRotationSteps + 1);
  600. indices.push((firstIndex + totalYRotationSteps + 1));
  601. indices.push((firstIndex + 1));
  602. indices.push((firstIndex + totalYRotationSteps + 2));
  603. }
  604. }
  605. }
  606. // Sides
  607. VertexData._ComputeSides(sideOrientation, positions, indices, normals, uvs);
  608. // Result
  609. var vertexData = new VertexData();
  610. vertexData.indices = indices;
  611. vertexData.positions = positions;
  612. vertexData.normals = normals;
  613. vertexData.uvs = uvs;
  614. return vertexData;
  615. };
  616. // Cylinder and cone
  617. VertexData.CreateCylinder = function (options) {
  618. var height = options.height || 2;
  619. var diameterTop = (options.diameterTop === 0) ? 0 : options.diameterTop || 1;
  620. var diameterBottom = options.diameterBottom || 1;
  621. var tessellation = options.tessellation || 24;
  622. var subdivisions = options.subdivisions || 1;
  623. var sideOrientation = (options.sideOrientation === 0) ? 0 : options.sideOrientation || BABYLON.Mesh.DEFAULTSIDE;
  624. var indices = [];
  625. var positions = [];
  626. var normals = [];
  627. var uvs = [];
  628. var angle_step = Math.PI * 2 / tessellation;
  629. var angle;
  630. var h;
  631. var radius;
  632. var tan = (diameterBottom - diameterTop) / 2 / height;
  633. var ringVertex = BABYLON.Vector3.Zero();
  634. var ringNormal = BABYLON.Vector3.Zero();
  635. // positions, normals, uvs
  636. var i;
  637. var j;
  638. for (i = 0; i <= subdivisions; i++) {
  639. h = i / subdivisions;
  640. radius = (h * (diameterTop - diameterBottom) + diameterBottom) / 2;
  641. for (j = 0; j <= tessellation; j++) {
  642. angle = j * angle_step;
  643. ringVertex.x = Math.cos(-angle) * radius;
  644. ringVertex.y = -height / 2 + h * height;
  645. ringVertex.z = Math.sin(-angle) * radius;
  646. if (diameterTop === 0 && i === subdivisions) {
  647. // if no top cap, reuse former normals
  648. ringNormal.x = normals[normals.length - (tessellation + 1) * 3];
  649. ringNormal.y = normals[normals.length - (tessellation + 1) * 3 + 1];
  650. ringNormal.z = normals[normals.length - (tessellation + 1) * 3 + 2];
  651. }
  652. else {
  653. ringNormal.x = ringVertex.x;
  654. ringNormal.z = ringVertex.z;
  655. ringNormal.y = Math.sqrt(ringNormal.x * ringNormal.x + ringNormal.z * ringNormal.z) * tan;
  656. ringNormal.normalize();
  657. }
  658. positions.push(ringVertex.x, ringVertex.y, ringVertex.z);
  659. normals.push(ringNormal.x, ringNormal.y, ringNormal.z);
  660. uvs.push(j / tessellation, 1 - h);
  661. }
  662. }
  663. // indices
  664. for (i = 0; i < subdivisions; i++) {
  665. for (j = 0; j < tessellation; j++) {
  666. var i0 = i * (tessellation + 1) + j;
  667. var i1 = (i + 1) * (tessellation + 1) + j;
  668. var i2 = i * (tessellation + 1) + (j + 1);
  669. var i3 = (i + 1) * (tessellation + 1) + (j + 1);
  670. indices.push(i0, i1, i2);
  671. indices.push(i3, i2, i1);
  672. }
  673. }
  674. // Caps
  675. var createCylinderCap = function (isTop) {
  676. var radius = isTop ? diameterTop / 2 : diameterBottom / 2;
  677. if (radius === 0) {
  678. return;
  679. }
  680. var vbase = positions.length / 3;
  681. var offset = new BABYLON.Vector3(0, isTop ? height / 2 : -height / 2, 0);
  682. var textureScale = new BABYLON.Vector2(0.5, 0.5);
  683. // Cap positions, normals & uvs
  684. var angle;
  685. var circleVector;
  686. var i;
  687. for (i = 0; i < tessellation; i++) {
  688. angle = Math.PI * 2 * i / tessellation;
  689. circleVector = new BABYLON.Vector3(Math.cos(-angle), 0, Math.sin(-angle));
  690. var position = circleVector.scale(radius).add(offset);
  691. var textureCoordinate = new BABYLON.Vector2(circleVector.x * textureScale.x + 0.5, circleVector.z * textureScale.y + 0.5);
  692. positions.push(position.x, position.y, position.z);
  693. normals.push(0, isTop ? 1 : -1, 0);
  694. uvs.push(textureCoordinate.x, textureCoordinate.y);
  695. }
  696. // Cap indices
  697. for (i = 0; i < tessellation - 2; i++) {
  698. if (!isTop) {
  699. indices.push(vbase);
  700. indices.push(vbase + (i + 1) % tessellation);
  701. indices.push(vbase + (i + 2) % tessellation);
  702. }
  703. else {
  704. indices.push(vbase);
  705. indices.push(vbase + (i + 2) % tessellation);
  706. indices.push(vbase + (i + 1) % tessellation);
  707. }
  708. }
  709. };
  710. // add caps to geometry
  711. createCylinderCap(true);
  712. createCylinderCap(false);
  713. // Sides
  714. VertexData._ComputeSides(sideOrientation, positions, indices, normals, uvs);
  715. var vertexData = new VertexData();
  716. vertexData.indices = indices;
  717. vertexData.positions = positions;
  718. vertexData.normals = normals;
  719. vertexData.uvs = uvs;
  720. return vertexData;
  721. };
  722. VertexData.CreateTorus = function (options) {
  723. var indices = [];
  724. var positions = [];
  725. var normals = [];
  726. var uvs = [];
  727. var diameter = options.diameter || 1;
  728. var thickness = options.thickness || 0.5;
  729. var tessellation = options.tessellation || 16;
  730. var sideOrientation = (options.sideOrientation === 0) ? 0 : options.sideOrientation || BABYLON.Mesh.DEFAULTSIDE;
  731. var stride = tessellation + 1;
  732. for (var i = 0; i <= tessellation; i++) {
  733. var u = i / tessellation;
  734. var outerAngle = i * Math.PI * 2.0 / tessellation - Math.PI / 2.0;
  735. var transform = BABYLON.Matrix.Translation(diameter / 2.0, 0, 0).multiply(BABYLON.Matrix.RotationY(outerAngle));
  736. for (var j = 0; j <= tessellation; j++) {
  737. var v = 1 - j / tessellation;
  738. var innerAngle = j * Math.PI * 2.0 / tessellation + Math.PI;
  739. var dx = Math.cos(innerAngle);
  740. var dy = Math.sin(innerAngle);
  741. // Create a vertex.
  742. var normal = new BABYLON.Vector3(dx, dy, 0);
  743. var position = normal.scale(thickness / 2);
  744. var textureCoordinate = new BABYLON.Vector2(u, v);
  745. position = BABYLON.Vector3.TransformCoordinates(position, transform);
  746. normal = BABYLON.Vector3.TransformNormal(normal, transform);
  747. positions.push(position.x, position.y, position.z);
  748. normals.push(normal.x, normal.y, normal.z);
  749. uvs.push(textureCoordinate.x, textureCoordinate.y);
  750. // And create indices for two triangles.
  751. var nextI = (i + 1) % stride;
  752. var nextJ = (j + 1) % stride;
  753. indices.push(i * stride + j);
  754. indices.push(i * stride + nextJ);
  755. indices.push(nextI * stride + j);
  756. indices.push(i * stride + nextJ);
  757. indices.push(nextI * stride + nextJ);
  758. indices.push(nextI * stride + j);
  759. }
  760. }
  761. // Sides
  762. VertexData._ComputeSides(sideOrientation, positions, indices, normals, uvs);
  763. // Result
  764. var vertexData = new VertexData();
  765. vertexData.indices = indices;
  766. vertexData.positions = positions;
  767. vertexData.normals = normals;
  768. vertexData.uvs = uvs;
  769. return vertexData;
  770. };
  771. VertexData.CreateLines = function (options) {
  772. var indices = [];
  773. var positions = [];
  774. var points = options.points;
  775. for (var index = 0; index < points.length; index++) {
  776. positions.push(points[index].x, points[index].y, points[index].z);
  777. if (index > 0) {
  778. indices.push(index - 1);
  779. indices.push(index);
  780. }
  781. }
  782. // Result
  783. var vertexData = new VertexData();
  784. vertexData.indices = indices;
  785. vertexData.positions = positions;
  786. return vertexData;
  787. };
  788. VertexData.CreateDashedLines = function (options) {
  789. var dashSize = options.dashSize || 3;
  790. var gapSize = options.gapSize || 1;
  791. var dashNb = options.dashNb || 200;
  792. var points = options.points;
  793. var positions = new Array();
  794. var indices = new Array();
  795. var curvect = BABYLON.Vector3.Zero();
  796. var lg = 0;
  797. var nb = 0;
  798. var shft = 0;
  799. var dashshft = 0;
  800. var curshft = 0;
  801. var idx = 0;
  802. var i = 0;
  803. for (i = 0; i < points.length - 1; i++) {
  804. points[i + 1].subtractToRef(points[i], curvect);
  805. lg += curvect.length();
  806. }
  807. shft = lg / dashNb;
  808. dashshft = dashSize * shft / (dashSize + gapSize);
  809. for (i = 0; i < points.length - 1; i++) {
  810. points[i + 1].subtractToRef(points[i], curvect);
  811. nb = Math.floor(curvect.length() / shft);
  812. curvect.normalize();
  813. for (var j = 0; j < nb; j++) {
  814. curshft = shft * j;
  815. positions.push(points[i].x + curshft * curvect.x, points[i].y + curshft * curvect.y, points[i].z + curshft * curvect.z);
  816. positions.push(points[i].x + (curshft + dashshft) * curvect.x, points[i].y + (curshft + dashshft) * curvect.y, points[i].z + (curshft + dashshft) * curvect.z);
  817. indices.push(idx, idx + 1);
  818. idx += 2;
  819. }
  820. }
  821. // Result
  822. var vertexData = new VertexData();
  823. vertexData.positions = positions;
  824. vertexData.indices = indices;
  825. return vertexData;
  826. };
  827. VertexData.CreateGround = function (options) {
  828. var indices = [];
  829. var positions = [];
  830. var normals = [];
  831. var uvs = [];
  832. var row, col;
  833. var width = options.width || 1;
  834. var height = options.height || 1;
  835. var subdivisions = options.subdivisions || 1;
  836. for (row = 0; row <= subdivisions; row++) {
  837. for (col = 0; col <= subdivisions; col++) {
  838. var position = new BABYLON.Vector3((col * width) / subdivisions - (width / 2.0), 0, ((subdivisions - row) * height) / subdivisions - (height / 2.0));
  839. var normal = new BABYLON.Vector3(0, 1.0, 0);
  840. positions.push(position.x, position.y, position.z);
  841. normals.push(normal.x, normal.y, normal.z);
  842. uvs.push(col / subdivisions, 1.0 - row / subdivisions);
  843. }
  844. }
  845. for (row = 0; row < subdivisions; row++) {
  846. for (col = 0; col < subdivisions; col++) {
  847. indices.push(col + 1 + (row + 1) * (subdivisions + 1));
  848. indices.push(col + 1 + row * (subdivisions + 1));
  849. indices.push(col + row * (subdivisions + 1));
  850. indices.push(col + (row + 1) * (subdivisions + 1));
  851. indices.push(col + 1 + (row + 1) * (subdivisions + 1));
  852. indices.push(col + row * (subdivisions + 1));
  853. }
  854. }
  855. // Result
  856. var vertexData = new VertexData();
  857. vertexData.indices = indices;
  858. vertexData.positions = positions;
  859. vertexData.normals = normals;
  860. vertexData.uvs = uvs;
  861. return vertexData;
  862. };
  863. VertexData.CreateTiledGround = function (xmin, zmin, xmax, zmax, subdivisions, precision) {
  864. if (subdivisions === void 0) { subdivisions = { w: 1, h: 1 }; }
  865. if (precision === void 0) { precision = { w: 1, h: 1 }; }
  866. var indices = [];
  867. var positions = [];
  868. var normals = [];
  869. var uvs = [];
  870. var row, col, tileRow, tileCol;
  871. subdivisions.h = (subdivisions.w < 1) ? 1 : subdivisions.h;
  872. subdivisions.w = (subdivisions.w < 1) ? 1 : subdivisions.w;
  873. precision.w = (precision.w < 1) ? 1 : precision.w;
  874. precision.h = (precision.h < 1) ? 1 : precision.h;
  875. var tileSize = {
  876. 'w': (xmax - xmin) / subdivisions.w,
  877. 'h': (zmax - zmin) / subdivisions.h
  878. };
  879. function applyTile(xTileMin, zTileMin, xTileMax, zTileMax) {
  880. // Indices
  881. var base = positions.length / 3;
  882. var rowLength = precision.w + 1;
  883. for (row = 0; row < precision.h; row++) {
  884. for (col = 0; col < precision.w; col++) {
  885. var square = [
  886. base + col + row * rowLength,
  887. base + (col + 1) + row * rowLength,
  888. base + (col + 1) + (row + 1) * rowLength,
  889. base + col + (row + 1) * rowLength
  890. ];
  891. indices.push(square[1]);
  892. indices.push(square[2]);
  893. indices.push(square[3]);
  894. indices.push(square[0]);
  895. indices.push(square[1]);
  896. indices.push(square[3]);
  897. }
  898. }
  899. // Position, normals and uvs
  900. var position = BABYLON.Vector3.Zero();
  901. var normal = new BABYLON.Vector3(0, 1.0, 0);
  902. for (row = 0; row <= precision.h; row++) {
  903. position.z = (row * (zTileMax - zTileMin)) / precision.h + zTileMin;
  904. for (col = 0; col <= precision.w; col++) {
  905. position.x = (col * (xTileMax - xTileMin)) / precision.w + xTileMin;
  906. position.y = 0;
  907. positions.push(position.x, position.y, position.z);
  908. normals.push(normal.x, normal.y, normal.z);
  909. uvs.push(col / precision.w, row / precision.h);
  910. }
  911. }
  912. }
  913. for (tileRow = 0; tileRow < subdivisions.h; tileRow++) {
  914. for (tileCol = 0; tileCol < subdivisions.w; tileCol++) {
  915. applyTile(xmin + tileCol * tileSize.w, zmin + tileRow * tileSize.h, xmin + (tileCol + 1) * tileSize.w, zmin + (tileRow + 1) * tileSize.h);
  916. }
  917. }
  918. // Result
  919. var vertexData = new VertexData();
  920. vertexData.indices = indices;
  921. vertexData.positions = positions;
  922. vertexData.normals = normals;
  923. vertexData.uvs = uvs;
  924. return vertexData;
  925. };
  926. VertexData.CreateGroundFromHeightMap = function (width, height, subdivisions, minHeight, maxHeight, buffer, bufferWidth, bufferHeight) {
  927. var indices = [];
  928. var positions = [];
  929. var normals = [];
  930. var uvs = [];
  931. var row, col;
  932. // Vertices
  933. for (row = 0; row <= subdivisions; row++) {
  934. for (col = 0; col <= subdivisions; col++) {
  935. var position = new BABYLON.Vector3((col * width) / subdivisions - (width / 2.0), 0, ((subdivisions - row) * height) / subdivisions - (height / 2.0));
  936. // Compute height
  937. var heightMapX = (((position.x + width / 2) / width) * (bufferWidth - 1)) | 0;
  938. var heightMapY = ((1.0 - (position.z + height / 2) / height) * (bufferHeight - 1)) | 0;
  939. var pos = (heightMapX + heightMapY * bufferWidth) * 4;
  940. var r = buffer[pos] / 255.0;
  941. var g = buffer[pos + 1] / 255.0;
  942. var b = buffer[pos + 2] / 255.0;
  943. var gradient = r * 0.3 + g * 0.59 + b * 0.11;
  944. position.y = minHeight + (maxHeight - minHeight) * gradient;
  945. // Add vertex
  946. positions.push(position.x, position.y, position.z);
  947. normals.push(0, 0, 0);
  948. uvs.push(col / subdivisions, 1.0 - row / subdivisions);
  949. }
  950. }
  951. // Indices
  952. for (row = 0; row < subdivisions; row++) {
  953. for (col = 0; col < subdivisions; col++) {
  954. indices.push(col + 1 + (row + 1) * (subdivisions + 1));
  955. indices.push(col + 1 + row * (subdivisions + 1));
  956. indices.push(col + row * (subdivisions + 1));
  957. indices.push(col + (row + 1) * (subdivisions + 1));
  958. indices.push(col + 1 + (row + 1) * (subdivisions + 1));
  959. indices.push(col + row * (subdivisions + 1));
  960. }
  961. }
  962. // Normals
  963. VertexData.ComputeNormals(positions, indices, normals);
  964. // Result
  965. var vertexData = new VertexData();
  966. vertexData.indices = indices;
  967. vertexData.positions = positions;
  968. vertexData.normals = normals;
  969. vertexData.uvs = uvs;
  970. return vertexData;
  971. };
  972. VertexData.CreatePlane = function (options) {
  973. var indices = [];
  974. var positions = [];
  975. var normals = [];
  976. var uvs = [];
  977. var width = options.width || options.size || 1;
  978. var height = options.height || options.size || 1;
  979. var sideOrientation = (options.sideOrientation === 0) ? 0 : options.sideOrientation || BABYLON.Mesh.DEFAULTSIDE;
  980. // Vertices
  981. var halfWidth = width / 2.0;
  982. var halfHeight = height / 2.0;
  983. positions.push(-halfWidth, -halfHeight, 0);
  984. normals.push(0, 0, -1.0);
  985. uvs.push(0.0, 0.0);
  986. positions.push(halfWidth, -halfHeight, 0);
  987. normals.push(0, 0, -1.0);
  988. uvs.push(1.0, 0.0);
  989. positions.push(halfWidth, halfHeight, 0);
  990. normals.push(0, 0, -1.0);
  991. uvs.push(1.0, 1.0);
  992. positions.push(-halfWidth, halfHeight, 0);
  993. normals.push(0, 0, -1.0);
  994. uvs.push(0.0, 1.0);
  995. // Indices
  996. indices.push(0);
  997. indices.push(1);
  998. indices.push(2);
  999. indices.push(0);
  1000. indices.push(2);
  1001. indices.push(3);
  1002. // Sides
  1003. VertexData._ComputeSides(sideOrientation, positions, indices, normals, uvs);
  1004. // Result
  1005. var vertexData = new VertexData();
  1006. vertexData.indices = indices;
  1007. vertexData.positions = positions;
  1008. vertexData.normals = normals;
  1009. vertexData.uvs = uvs;
  1010. return vertexData;
  1011. };
  1012. VertexData.CreateDisc = function (options) {
  1013. var positions = [];
  1014. var indices = [];
  1015. var normals = [];
  1016. var uvs = [];
  1017. var radius = options.radius || 0.5;
  1018. var tessellation = options.tessellation || 64;
  1019. var sideOrientation = (options.sideOrientation === 0) ? 0 : options.sideOrientation || BABYLON.Mesh.DEFAULTSIDE;
  1020. // positions and uvs
  1021. positions.push(0, 0, 0); // disc center first
  1022. uvs.push(0.5, 0.5);
  1023. var step = Math.PI * 2 / tessellation;
  1024. for (var a = 0; a < Math.PI * 2; a += step) {
  1025. var x = Math.cos(a);
  1026. var y = Math.sin(a);
  1027. var u = (x + 1) / 2;
  1028. var v = (1 - y) / 2;
  1029. positions.push(radius * x, radius * y, 0);
  1030. uvs.push(u, v);
  1031. }
  1032. positions.push(positions[3], positions[4], positions[5]); // close the circle
  1033. uvs.push(uvs[2], uvs[3]);
  1034. //indices
  1035. var vertexNb = positions.length / 3;
  1036. for (var i = 1; i < vertexNb - 1; i++) {
  1037. indices.push(i + 1, 0, i);
  1038. }
  1039. // result
  1040. VertexData.ComputeNormals(positions, indices, normals);
  1041. VertexData._ComputeSides(sideOrientation, positions, indices, normals, uvs);
  1042. var vertexData = new VertexData();
  1043. vertexData.indices = indices;
  1044. vertexData.positions = positions;
  1045. vertexData.normals = normals;
  1046. vertexData.uvs = uvs;
  1047. return vertexData;
  1048. };
  1049. // based on http://code.google.com/p/away3d/source/browse/trunk/fp10/Away3D/src/away3d/primitives/TorusKnot.as?spec=svn2473&r=2473
  1050. VertexData.CreateTorusKnot = function (options) {
  1051. var indices = [];
  1052. var positions = [];
  1053. var normals = [];
  1054. var uvs = [];
  1055. var radius = options.radius || 2;
  1056. var tube = options.tube || 0.5;
  1057. var radialSegments = options.radialSegments || 32;
  1058. var tubularSegments = options.tubularSegments || 32;
  1059. var p = options.p || 2;
  1060. var q = options.q || 3;
  1061. var sideOrientation = (options.sideOrientation === 0) ? 0 : options.sideOrientation || BABYLON.Mesh.DEFAULTSIDE;
  1062. // Helper
  1063. var getPos = function (angle) {
  1064. var cu = Math.cos(angle);
  1065. var su = Math.sin(angle);
  1066. var quOverP = q / p * angle;
  1067. var cs = Math.cos(quOverP);
  1068. var tx = radius * (2 + cs) * 0.5 * cu;
  1069. var ty = radius * (2 + cs) * su * 0.5;
  1070. var tz = radius * Math.sin(quOverP) * 0.5;
  1071. return new BABYLON.Vector3(tx, ty, tz);
  1072. };
  1073. // Vertices
  1074. var i;
  1075. var j;
  1076. for (i = 0; i <= radialSegments; i++) {
  1077. var modI = i % radialSegments;
  1078. var u = modI / radialSegments * 2 * p * Math.PI;
  1079. var p1 = getPos(u);
  1080. var p2 = getPos(u + 0.01);
  1081. var tang = p2.subtract(p1);
  1082. var n = p2.add(p1);
  1083. var bitan = BABYLON.Vector3.Cross(tang, n);
  1084. n = BABYLON.Vector3.Cross(bitan, tang);
  1085. bitan.normalize();
  1086. n.normalize();
  1087. for (j = 0; j < tubularSegments; j++) {
  1088. var modJ = j % tubularSegments;
  1089. var v = modJ / tubularSegments * 2 * Math.PI;
  1090. var cx = -tube * Math.cos(v);
  1091. var cy = tube * Math.sin(v);
  1092. positions.push(p1.x + cx * n.x + cy * bitan.x);
  1093. positions.push(p1.y + cx * n.y + cy * bitan.y);
  1094. positions.push(p1.z + cx * n.z + cy * bitan.z);
  1095. uvs.push(i / radialSegments);
  1096. uvs.push(j / tubularSegments);
  1097. }
  1098. }
  1099. for (i = 0; i < radialSegments; i++) {
  1100. for (j = 0; j < tubularSegments; j++) {
  1101. var jNext = (j + 1) % tubularSegments;
  1102. var a = i * tubularSegments + j;
  1103. var b = (i + 1) * tubularSegments + j;
  1104. var c = (i + 1) * tubularSegments + jNext;
  1105. var d = i * tubularSegments + jNext;
  1106. indices.push(d);
  1107. indices.push(b);
  1108. indices.push(a);
  1109. indices.push(d);
  1110. indices.push(c);
  1111. indices.push(b);
  1112. }
  1113. }
  1114. // Normals
  1115. VertexData.ComputeNormals(positions, indices, normals);
  1116. // Sides
  1117. VertexData._ComputeSides(sideOrientation, positions, indices, normals, uvs);
  1118. // Result
  1119. var vertexData = new VertexData();
  1120. vertexData.indices = indices;
  1121. vertexData.positions = positions;
  1122. vertexData.normals = normals;
  1123. vertexData.uvs = uvs;
  1124. return vertexData;
  1125. };
  1126. // Tools
  1127. /**
  1128. * @param {any} - positions (number[] or Float32Array)
  1129. * @param {any} - indices (number[] or Uint16Array)
  1130. * @param {any} - normals (number[] or Float32Array)
  1131. */
  1132. VertexData.ComputeNormals = function (positions, indices, normals) {
  1133. var index = 0;
  1134. // temp Vector3
  1135. var p1p2 = BABYLON.Vector3.Zero();
  1136. var p3p2 = BABYLON.Vector3.Zero();
  1137. var faceNormal = BABYLON.Vector3.Zero();
  1138. var vertexNormali1 = BABYLON.Vector3.Zero();
  1139. for (index = 0; index < positions.length; index++) {
  1140. normals[index] = 0.0;
  1141. }
  1142. // indice triplet = 1 face
  1143. var nbFaces = indices.length / 3;
  1144. for (index = 0; index < nbFaces; index++) {
  1145. var i1 = indices[index * 3];
  1146. var i2 = indices[index * 3 + 1];
  1147. var i3 = indices[index * 3 + 2];
  1148. p1p2.x = positions[i1 * 3] - positions[i2 * 3];
  1149. p1p2.y = positions[i1 * 3 + 1] - positions[i2 * 3 + 1];
  1150. p1p2.z = positions[i1 * 3 + 2] - positions[i2 * 3 + 2];
  1151. p3p2.x = positions[i3 * 3] - positions[i2 * 3];
  1152. p3p2.y = positions[i3 * 3 + 1] - positions[i2 * 3 + 1];
  1153. p3p2.z = positions[i3 * 3 + 2] - positions[i2 * 3 + 2];
  1154. BABYLON.Vector3.CrossToRef(p1p2, p3p2, faceNormal);
  1155. faceNormal.normalize();
  1156. normals[i1 * 3] += faceNormal.x;
  1157. normals[i1 * 3 + 1] += faceNormal.y;
  1158. normals[i1 * 3 + 2] += faceNormal.z;
  1159. normals[i2 * 3] += faceNormal.x;
  1160. normals[i2 * 3 + 1] += faceNormal.y;
  1161. normals[i2 * 3 + 2] += faceNormal.z;
  1162. normals[i3 * 3] += faceNormal.x;
  1163. normals[i3 * 3 + 1] += faceNormal.y;
  1164. normals[i3 * 3 + 2] += faceNormal.z;
  1165. }
  1166. // last normalization
  1167. for (index = 0; index < normals.length / 3; index++) {
  1168. BABYLON.Vector3.FromFloatsToRef(normals[index * 3], normals[index * 3 + 1], normals[index * 3 + 2], vertexNormali1);
  1169. vertexNormali1.normalize();
  1170. normals[index * 3] = vertexNormali1.x;
  1171. normals[index * 3 + 1] = vertexNormali1.y;
  1172. normals[index * 3 + 2] = vertexNormali1.z;
  1173. }
  1174. };
  1175. VertexData._ComputeSides = function (sideOrientation, positions, indices, normals, uvs) {
  1176. var li = indices.length;
  1177. var ln = normals.length;
  1178. var i;
  1179. var n;
  1180. sideOrientation = sideOrientation || BABYLON.Mesh.DEFAULTSIDE;
  1181. switch (sideOrientation) {
  1182. case BABYLON.Mesh.FRONTSIDE:
  1183. // nothing changed
  1184. break;
  1185. case BABYLON.Mesh.BACKSIDE:
  1186. var tmp;
  1187. // indices
  1188. for (i = 0; i < li; i += 3) {
  1189. tmp = indices[i];
  1190. indices[i] = indices[i + 2];
  1191. indices[i + 2] = tmp;
  1192. }
  1193. // normals
  1194. for (n = 0; n < ln; n++) {
  1195. normals[n] = -normals[n];
  1196. }
  1197. break;
  1198. case BABYLON.Mesh.DOUBLESIDE:
  1199. // positions
  1200. var lp = positions.length;
  1201. var l = lp / 3;
  1202. for (var p = 0; p < lp; p++) {
  1203. positions[lp + p] = positions[p];
  1204. }
  1205. // indices
  1206. for (i = 0; i < li; i += 3) {
  1207. indices[i + li] = indices[i + 2] + l;
  1208. indices[i + 1 + li] = indices[i + 1] + l;
  1209. indices[i + 2 + li] = indices[i] + l;
  1210. }
  1211. // normals
  1212. for (n = 0; n < ln; n++) {
  1213. normals[ln + n] = -normals[n];
  1214. }
  1215. // uvs
  1216. var lu = uvs.length;
  1217. for (var u = 0; u < lu; u++) {
  1218. uvs[u + lu] = uvs[u];
  1219. }
  1220. break;
  1221. }
  1222. };
  1223. return VertexData;
  1224. })();
  1225. BABYLON.VertexData = VertexData;
  1226. })(BABYLON || (BABYLON = {}));