babylon.mesh.vertexData.js 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268
  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. if (i === minlg) {
  397. vertex2 = path2[0];
  398. }
  399. vectlg = vertex2.subtract(vertex1).length();
  400. dist = vectlg + vTotalDistance[i];
  401. vTotalDistance[i] = dist;
  402. }
  403. }
  404. // uvs
  405. var u;
  406. var v;
  407. for (p = 0; p < pathArray.length; p++) {
  408. for (i = 0; i < minlg + closePathCorr; i++) {
  409. u = us[p][i] / uTotalDistance[p];
  410. v = vs[i][p] / vTotalDistance[i];
  411. uvs.push(u, v);
  412. }
  413. }
  414. // indices
  415. p = 0; // path index
  416. var pi = 0; // positions array index
  417. var l1 = lg[p] - 1; // path1 length
  418. var l2 = lg[p + 1] - 1; // path2 length
  419. var min = (l1 < l2) ? l1 : l2; // current path stop index
  420. var shft = idx[1] - idx[0]; // shift
  421. var path1nb = closeArray ? lg.length : lg.length - 1; // number of path1 to iterate on
  422. while (pi <= min && p < path1nb) {
  423. // 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
  424. indices.push(pi, pi + shft, pi + 1);
  425. indices.push(pi + shft + 1, pi + 1, pi + shft);
  426. pi += 1;
  427. if (pi === min) {
  428. p++;
  429. if (p === lg.length - 1) {
  430. shft = idx[0] - idx[p];
  431. l1 = lg[p] - 1;
  432. l2 = lg[0] - 1;
  433. }
  434. else {
  435. shft = idx[p + 1] - idx[p];
  436. l1 = lg[p] - 1;
  437. l2 = lg[p + 1] - 1;
  438. }
  439. pi = idx[p];
  440. min = (l1 < l2) ? l1 + pi : l2 + pi;
  441. }
  442. }
  443. // normals
  444. VertexData.ComputeNormals(positions, indices, normals);
  445. if (closePath) {
  446. var indexFirst = 0;
  447. var indexLast = 0;
  448. for (p = 0; p < pathArray.length; p++) {
  449. indexFirst = idx[p] * 3;
  450. if (p + 1 < pathArray.length) {
  451. indexLast = (idx[p + 1] - 1) * 3;
  452. }
  453. else {
  454. indexLast = normals.length - 3;
  455. }
  456. normals[indexFirst] = (normals[indexFirst] + normals[indexLast]) * 0.5;
  457. normals[indexFirst + 1] = (normals[indexFirst + 1] + normals[indexLast + 1]) * 0.5;
  458. normals[indexFirst + 2] = (normals[indexFirst + 2] + normals[indexLast + 2]) * 0.5;
  459. normals[indexLast] = normals[indexFirst];
  460. normals[indexLast + 1] = normals[indexFirst + 1];
  461. normals[indexLast + 2] = normals[indexFirst + 2];
  462. }
  463. }
  464. // sides
  465. VertexData._ComputeSides(sideOrientation, positions, indices, normals, uvs);
  466. // Result
  467. var vertexData = new VertexData();
  468. vertexData.indices = indices;
  469. vertexData.positions = positions;
  470. vertexData.normals = normals;
  471. vertexData.uvs = uvs;
  472. if (closePath) {
  473. vertexData._idx = idx;
  474. }
  475. return vertexData;
  476. };
  477. VertexData.CreateBox = function (options) {
  478. var normalsSource = [
  479. new BABYLON.Vector3(0, 0, 1),
  480. new BABYLON.Vector3(0, 0, -1),
  481. new BABYLON.Vector3(1, 0, 0),
  482. new BABYLON.Vector3(-1, 0, 0),
  483. new BABYLON.Vector3(0, 1, 0),
  484. new BABYLON.Vector3(0, -1, 0)
  485. ];
  486. var indices = [];
  487. var positions = [];
  488. var normals = [];
  489. var uvs = [];
  490. var width = options.width || options.size || 1;
  491. var height = options.height || options.size || 1;
  492. var depth = options.depth || options.size || 1;
  493. var sideOrientation = (options.sideOrientation === 0) ? 0 : options.sideOrientation || BABYLON.Mesh.DEFAULTSIDE;
  494. var faceUV = options.faceUV || new Array(6);
  495. var faceColors = options.faceColors;
  496. var colors = [];
  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 arc = (options.arc <= 0) ? 1.0 : options.arc || 1.0;
  571. var slice = (options.slice <= 0) ? 1.0 : options.slice || 1.0;
  572. var sideOrientation = (options.sideOrientation === 0) ? 0 : options.sideOrientation || BABYLON.Mesh.DEFAULTSIDE;
  573. var radius = new BABYLON.Vector3(diameterX / 2, diameterY / 2, diameterZ / 2);
  574. var totalZRotationSteps = 2 + segments;
  575. var totalYRotationSteps = 2 * totalZRotationSteps;
  576. var indices = [];
  577. var positions = [];
  578. var normals = [];
  579. var uvs = [];
  580. for (var zRotationStep = 0; zRotationStep <= totalZRotationSteps; zRotationStep++) {
  581. var normalizedZ = zRotationStep / totalZRotationSteps;
  582. var angleZ = normalizedZ * Math.PI * slice;
  583. for (var yRotationStep = 0; yRotationStep <= totalYRotationSteps; yRotationStep++) {
  584. var normalizedY = yRotationStep / totalYRotationSteps;
  585. var angleY = normalizedY * Math.PI * 2 * arc;
  586. var rotationZ = BABYLON.Matrix.RotationZ(-angleZ);
  587. var rotationY = BABYLON.Matrix.RotationY(angleY);
  588. var afterRotZ = BABYLON.Vector3.TransformCoordinates(BABYLON.Vector3.Up(), rotationZ);
  589. var complete = BABYLON.Vector3.TransformCoordinates(afterRotZ, rotationY);
  590. var vertex = complete.multiply(radius);
  591. var normal = BABYLON.Vector3.Normalize(vertex);
  592. positions.push(vertex.x, vertex.y, vertex.z);
  593. normals.push(normal.x, normal.y, normal.z);
  594. uvs.push(normalizedY, normalizedZ);
  595. }
  596. if (zRotationStep > 0) {
  597. var verticesCount = positions.length / 3;
  598. for (var firstIndex = verticesCount - 2 * (totalYRotationSteps + 1); (firstIndex + totalYRotationSteps + 2) < verticesCount; firstIndex++) {
  599. indices.push((firstIndex));
  600. indices.push((firstIndex + 1));
  601. indices.push(firstIndex + totalYRotationSteps + 1);
  602. indices.push((firstIndex + totalYRotationSteps + 1));
  603. indices.push((firstIndex + 1));
  604. indices.push((firstIndex + totalYRotationSteps + 2));
  605. }
  606. }
  607. }
  608. // Sides
  609. VertexData._ComputeSides(sideOrientation, positions, indices, normals, uvs);
  610. // Result
  611. var vertexData = new VertexData();
  612. vertexData.indices = indices;
  613. vertexData.positions = positions;
  614. vertexData.normals = normals;
  615. vertexData.uvs = uvs;
  616. return vertexData;
  617. };
  618. // Cylinder and cone
  619. VertexData.CreateCylinder = function (options) {
  620. var height = options.height || 2;
  621. var diameterTop = (options.diameterTop === 0) ? 0 : options.diameterTop || options.diameter || 1;
  622. var diameterBottom = options.diameterBottom || options.diameter || 1;
  623. var tessellation = options.tessellation || 24;
  624. var subdivisions = options.subdivisions || 1;
  625. var arc = (options.arc <= 0) ? 1.0 : options.arc || 1.0;
  626. var sideOrientation = (options.sideOrientation === 0) ? 0 : options.sideOrientation || BABYLON.Mesh.DEFAULTSIDE;
  627. var faceUV = options.faceUV || new Array(3);
  628. var faceColors = options.faceColors;
  629. // default face colors and UV if undefined
  630. for (var f = 0; f < 3; f++) {
  631. if (faceColors && faceColors[f] === undefined) {
  632. faceColors[f] = new BABYLON.Color4(1, 1, 1, 1);
  633. }
  634. if (faceUV && faceUV[f] === undefined) {
  635. faceUV[f] = new BABYLON.Vector4(0, 0, 1, 1);
  636. }
  637. }
  638. var indices = [];
  639. var positions = [];
  640. var normals = [];
  641. var uvs = [];
  642. var colors = [];
  643. var angle_step = Math.PI * 2 * arc / tessellation;
  644. var angle;
  645. var h;
  646. var radius;
  647. var tan = (diameterBottom - diameterTop) / 2 / height;
  648. var ringVertex = BABYLON.Vector3.Zero();
  649. var ringNormal = BABYLON.Vector3.Zero();
  650. // positions, normals, uvs
  651. var i;
  652. var j;
  653. for (i = 0; i <= subdivisions; i++) {
  654. h = i / subdivisions;
  655. radius = (h * (diameterTop - diameterBottom) + diameterBottom) / 2;
  656. for (j = 0; j <= tessellation; j++) {
  657. angle = j * angle_step;
  658. ringVertex.x = Math.cos(-angle) * radius;
  659. ringVertex.y = -height / 2 + h * height;
  660. ringVertex.z = Math.sin(-angle) * radius;
  661. if (diameterTop === 0 && i === subdivisions) {
  662. // if no top cap, reuse former normals
  663. ringNormal.x = normals[normals.length - (tessellation + 1) * 3];
  664. ringNormal.y = normals[normals.length - (tessellation + 1) * 3 + 1];
  665. ringNormal.z = normals[normals.length - (tessellation + 1) * 3 + 2];
  666. }
  667. else {
  668. ringNormal.x = ringVertex.x;
  669. ringNormal.z = ringVertex.z;
  670. ringNormal.y = Math.sqrt(ringNormal.x * ringNormal.x + ringNormal.z * ringNormal.z) * tan;
  671. ringNormal.normalize();
  672. }
  673. positions.push(ringVertex.x, ringVertex.y, ringVertex.z);
  674. normals.push(ringNormal.x, ringNormal.y, ringNormal.z);
  675. uvs.push(faceUV[1].x + (faceUV[1].z - faceUV[1].x) * j / tessellation, faceUV[1].y + (faceUV[1].w - faceUV[1].y) * h);
  676. if (faceColors) {
  677. colors.push(faceColors[1].r, faceColors[1].g, faceColors[1].b, faceColors[1].a);
  678. }
  679. }
  680. }
  681. // indices
  682. for (i = 0; i < subdivisions; i++) {
  683. for (j = 0; j < tessellation; j++) {
  684. var i0 = i * (tessellation + 1) + j;
  685. var i1 = (i + 1) * (tessellation + 1) + j;
  686. var i2 = i * (tessellation + 1) + (j + 1);
  687. var i3 = (i + 1) * (tessellation + 1) + (j + 1);
  688. indices.push(i0, i1, i2);
  689. indices.push(i3, i2, i1);
  690. }
  691. }
  692. // Caps
  693. var createCylinderCap = function (isTop) {
  694. var radius = isTop ? diameterTop / 2 : diameterBottom / 2;
  695. if (radius === 0) {
  696. return;
  697. }
  698. // Cap positions, normals & uvs
  699. var angle;
  700. var circleVector;
  701. var i;
  702. var u = (isTop) ? faceUV[2] : faceUV[0];
  703. var c;
  704. if (faceColors) {
  705. c = (isTop) ? faceColors[2] : faceColors[0];
  706. }
  707. // cap center
  708. var vbase = positions.length / 3;
  709. var offset = isTop ? height / 2 : -height / 2;
  710. var center = new BABYLON.Vector3(0, offset, 0);
  711. positions.push(center.x, center.y, center.z);
  712. normals.push(0, isTop ? 1 : -1, 0);
  713. uvs.push(u.x + (u.z - u.x) * 0.5, u.y + (u.w - u.y) * 0.5);
  714. if (faceColors) {
  715. colors.push(c.r, c.g, c.b, c.a);
  716. }
  717. var textureScale = new BABYLON.Vector2(0.5, 0.5);
  718. for (i = 0; i <= tessellation; i++) {
  719. angle = Math.PI * 2 * i * arc / tessellation;
  720. var cos = Math.cos(-angle);
  721. var sin = Math.sin(-angle);
  722. circleVector = new BABYLON.Vector3(cos * radius, offset, sin * radius);
  723. var textureCoordinate = new BABYLON.Vector2(cos * textureScale.x + 0.5, sin * textureScale.y + 0.5);
  724. positions.push(circleVector.x, circleVector.y, circleVector.z);
  725. normals.push(0, isTop ? 1 : -1, 0);
  726. uvs.push(u.x + (u.z - u.x) * textureCoordinate.x, u.y + (u.w - u.y) * textureCoordinate.y);
  727. if (faceColors) {
  728. colors.push(c.r, c.g, c.b, c.a);
  729. }
  730. }
  731. // Cap indices
  732. for (i = 0; i < tessellation; i++) {
  733. if (!isTop) {
  734. indices.push(vbase);
  735. indices.push(vbase + (i + 1));
  736. indices.push(vbase + (i + 2));
  737. }
  738. else {
  739. indices.push(vbase);
  740. indices.push(vbase + (i + 2));
  741. indices.push(vbase + (i + 1));
  742. }
  743. }
  744. };
  745. // add caps to geometry
  746. createCylinderCap(false);
  747. createCylinderCap(true);
  748. // Sides
  749. VertexData._ComputeSides(sideOrientation, positions, indices, normals, uvs);
  750. var vertexData = new VertexData();
  751. vertexData.indices = indices;
  752. vertexData.positions = positions;
  753. vertexData.normals = normals;
  754. vertexData.uvs = uvs;
  755. if (faceColors) {
  756. vertexData.colors = colors;
  757. }
  758. return vertexData;
  759. };
  760. VertexData.CreateTorus = function (options) {
  761. var indices = [];
  762. var positions = [];
  763. var normals = [];
  764. var uvs = [];
  765. var diameter = options.diameter || 1;
  766. var thickness = options.thickness || 0.5;
  767. var tessellation = options.tessellation || 16;
  768. var sideOrientation = (options.sideOrientation === 0) ? 0 : options.sideOrientation || BABYLON.Mesh.DEFAULTSIDE;
  769. var stride = tessellation + 1;
  770. for (var i = 0; i <= tessellation; i++) {
  771. var u = i / tessellation;
  772. var outerAngle = i * Math.PI * 2.0 / tessellation - Math.PI / 2.0;
  773. var transform = BABYLON.Matrix.Translation(diameter / 2.0, 0, 0).multiply(BABYLON.Matrix.RotationY(outerAngle));
  774. for (var j = 0; j <= tessellation; j++) {
  775. var v = 1 - j / tessellation;
  776. var innerAngle = j * Math.PI * 2.0 / tessellation + Math.PI;
  777. var dx = Math.cos(innerAngle);
  778. var dy = Math.sin(innerAngle);
  779. // Create a vertex.
  780. var normal = new BABYLON.Vector3(dx, dy, 0);
  781. var position = normal.scale(thickness / 2);
  782. var textureCoordinate = new BABYLON.Vector2(u, v);
  783. position = BABYLON.Vector3.TransformCoordinates(position, transform);
  784. normal = BABYLON.Vector3.TransformNormal(normal, transform);
  785. positions.push(position.x, position.y, position.z);
  786. normals.push(normal.x, normal.y, normal.z);
  787. uvs.push(textureCoordinate.x, textureCoordinate.y);
  788. // And create indices for two triangles.
  789. var nextI = (i + 1) % stride;
  790. var nextJ = (j + 1) % stride;
  791. indices.push(i * stride + j);
  792. indices.push(i * stride + nextJ);
  793. indices.push(nextI * stride + j);
  794. indices.push(i * stride + nextJ);
  795. indices.push(nextI * stride + nextJ);
  796. indices.push(nextI * stride + j);
  797. }
  798. }
  799. // Sides
  800. VertexData._ComputeSides(sideOrientation, positions, indices, normals, uvs);
  801. // Result
  802. var vertexData = new VertexData();
  803. vertexData.indices = indices;
  804. vertexData.positions = positions;
  805. vertexData.normals = normals;
  806. vertexData.uvs = uvs;
  807. return vertexData;
  808. };
  809. VertexData.CreateLines = function (options) {
  810. var indices = [];
  811. var positions = [];
  812. var points = options.points;
  813. for (var index = 0; index < points.length; index++) {
  814. positions.push(points[index].x, points[index].y, points[index].z);
  815. if (index > 0) {
  816. indices.push(index - 1);
  817. indices.push(index);
  818. }
  819. }
  820. // Result
  821. var vertexData = new VertexData();
  822. vertexData.indices = indices;
  823. vertexData.positions = positions;
  824. return vertexData;
  825. };
  826. VertexData.CreateDashedLines = function (options) {
  827. var dashSize = options.dashSize || 3;
  828. var gapSize = options.gapSize || 1;
  829. var dashNb = options.dashNb || 200;
  830. var points = options.points;
  831. var positions = new Array();
  832. var indices = new Array();
  833. var curvect = BABYLON.Vector3.Zero();
  834. var lg = 0;
  835. var nb = 0;
  836. var shft = 0;
  837. var dashshft = 0;
  838. var curshft = 0;
  839. var idx = 0;
  840. var i = 0;
  841. for (i = 0; i < points.length - 1; i++) {
  842. points[i + 1].subtractToRef(points[i], curvect);
  843. lg += curvect.length();
  844. }
  845. shft = lg / dashNb;
  846. dashshft = dashSize * shft / (dashSize + gapSize);
  847. for (i = 0; i < points.length - 1; i++) {
  848. points[i + 1].subtractToRef(points[i], curvect);
  849. nb = Math.floor(curvect.length() / shft);
  850. curvect.normalize();
  851. for (var j = 0; j < nb; j++) {
  852. curshft = shft * j;
  853. positions.push(points[i].x + curshft * curvect.x, points[i].y + curshft * curvect.y, points[i].z + curshft * curvect.z);
  854. positions.push(points[i].x + (curshft + dashshft) * curvect.x, points[i].y + (curshft + dashshft) * curvect.y, points[i].z + (curshft + dashshft) * curvect.z);
  855. indices.push(idx, idx + 1);
  856. idx += 2;
  857. }
  858. }
  859. // Result
  860. var vertexData = new VertexData();
  861. vertexData.positions = positions;
  862. vertexData.indices = indices;
  863. return vertexData;
  864. };
  865. VertexData.CreateGround = function (options) {
  866. var indices = [];
  867. var positions = [];
  868. var normals = [];
  869. var uvs = [];
  870. var row, col;
  871. var width = options.width || 1;
  872. var height = options.height || 1;
  873. var subdivisions = options.subdivisions || 1;
  874. for (row = 0; row <= subdivisions; row++) {
  875. for (col = 0; col <= subdivisions; col++) {
  876. var position = new BABYLON.Vector3((col * width) / subdivisions - (width / 2.0), 0, ((subdivisions - row) * height) / subdivisions - (height / 2.0));
  877. var normal = new BABYLON.Vector3(0, 1.0, 0);
  878. positions.push(position.x, position.y, position.z);
  879. normals.push(normal.x, normal.y, normal.z);
  880. uvs.push(col / subdivisions, 1.0 - row / subdivisions);
  881. }
  882. }
  883. for (row = 0; row < subdivisions; row++) {
  884. for (col = 0; col < subdivisions; col++) {
  885. indices.push(col + 1 + (row + 1) * (subdivisions + 1));
  886. indices.push(col + 1 + row * (subdivisions + 1));
  887. indices.push(col + row * (subdivisions + 1));
  888. indices.push(col + (row + 1) * (subdivisions + 1));
  889. indices.push(col + 1 + (row + 1) * (subdivisions + 1));
  890. indices.push(col + row * (subdivisions + 1));
  891. }
  892. }
  893. // Result
  894. var vertexData = new VertexData();
  895. vertexData.indices = indices;
  896. vertexData.positions = positions;
  897. vertexData.normals = normals;
  898. vertexData.uvs = uvs;
  899. return vertexData;
  900. };
  901. VertexData.CreateTiledGround = function (options) {
  902. var xmin = options.xmin;
  903. var zmin = options.zmin;
  904. var xmax = options.xmax;
  905. var zmax = options.zmax;
  906. var subdivisions = options.subdivisions || { w: 1, h: 1 };
  907. var precision = options.precision || { w: 1, h: 1 };
  908. var indices = [];
  909. var positions = [];
  910. var normals = [];
  911. var uvs = [];
  912. var row, col, tileRow, tileCol;
  913. subdivisions.h = (subdivisions.w < 1) ? 1 : subdivisions.h;
  914. subdivisions.w = (subdivisions.w < 1) ? 1 : subdivisions.w;
  915. precision.w = (precision.w < 1) ? 1 : precision.w;
  916. precision.h = (precision.h < 1) ? 1 : precision.h;
  917. var tileSize = {
  918. 'w': (xmax - xmin) / subdivisions.w,
  919. 'h': (zmax - zmin) / subdivisions.h
  920. };
  921. function applyTile(xTileMin, zTileMin, xTileMax, zTileMax) {
  922. // Indices
  923. var base = positions.length / 3;
  924. var rowLength = precision.w + 1;
  925. for (row = 0; row < precision.h; row++) {
  926. for (col = 0; col < precision.w; col++) {
  927. var square = [
  928. base + col + row * rowLength,
  929. base + (col + 1) + row * rowLength,
  930. base + (col + 1) + (row + 1) * rowLength,
  931. base + col + (row + 1) * rowLength
  932. ];
  933. indices.push(square[1]);
  934. indices.push(square[2]);
  935. indices.push(square[3]);
  936. indices.push(square[0]);
  937. indices.push(square[1]);
  938. indices.push(square[3]);
  939. }
  940. }
  941. // Position, normals and uvs
  942. var position = BABYLON.Vector3.Zero();
  943. var normal = new BABYLON.Vector3(0, 1.0, 0);
  944. for (row = 0; row <= precision.h; row++) {
  945. position.z = (row * (zTileMax - zTileMin)) / precision.h + zTileMin;
  946. for (col = 0; col <= precision.w; col++) {
  947. position.x = (col * (xTileMax - xTileMin)) / precision.w + xTileMin;
  948. position.y = 0;
  949. positions.push(position.x, position.y, position.z);
  950. normals.push(normal.x, normal.y, normal.z);
  951. uvs.push(col / precision.w, row / precision.h);
  952. }
  953. }
  954. }
  955. for (tileRow = 0; tileRow < subdivisions.h; tileRow++) {
  956. for (tileCol = 0; tileCol < subdivisions.w; tileCol++) {
  957. applyTile(xmin + tileCol * tileSize.w, zmin + tileRow * tileSize.h, xmin + (tileCol + 1) * tileSize.w, zmin + (tileRow + 1) * tileSize.h);
  958. }
  959. }
  960. // Result
  961. var vertexData = new VertexData();
  962. vertexData.indices = indices;
  963. vertexData.positions = positions;
  964. vertexData.normals = normals;
  965. vertexData.uvs = uvs;
  966. return vertexData;
  967. };
  968. VertexData.CreateGroundFromHeightMap = function (options) {
  969. var indices = [];
  970. var positions = [];
  971. var normals = [];
  972. var uvs = [];
  973. var row, col;
  974. // Vertices
  975. for (row = 0; row <= options.subdivisions; row++) {
  976. for (col = 0; col <= options.subdivisions; col++) {
  977. var position = new BABYLON.Vector3((col * options.width) / options.subdivisions - (options.width / 2.0), 0, ((options.subdivisions - row) * options.height) / options.subdivisions - (options.height / 2.0));
  978. // Compute height
  979. var heightMapX = (((position.x + options.width / 2) / options.width) * (options.bufferWidth - 1)) | 0;
  980. var heightMapY = ((1.0 - (position.z + options.height / 2) / options.height) * (options.bufferHeight - 1)) | 0;
  981. var pos = (heightMapX + heightMapY * options.bufferWidth) * 4;
  982. var r = options.buffer[pos] / 255.0;
  983. var g = options.buffer[pos + 1] / 255.0;
  984. var b = options.buffer[pos + 2] / 255.0;
  985. var gradient = r * 0.3 + g * 0.59 + b * 0.11;
  986. position.y = options.minHeight + (options.maxHeight - options.minHeight) * gradient;
  987. // Add vertex
  988. positions.push(position.x, position.y, position.z);
  989. normals.push(0, 0, 0);
  990. uvs.push(col / options.subdivisions, 1.0 - row / options.subdivisions);
  991. }
  992. }
  993. // Indices
  994. for (row = 0; row < options.subdivisions; row++) {
  995. for (col = 0; col < options.subdivisions; col++) {
  996. indices.push(col + 1 + (row + 1) * (options.subdivisions + 1));
  997. indices.push(col + 1 + row * (options.subdivisions + 1));
  998. indices.push(col + row * (options.subdivisions + 1));
  999. indices.push(col + (row + 1) * (options.subdivisions + 1));
  1000. indices.push(col + 1 + (row + 1) * (options.subdivisions + 1));
  1001. indices.push(col + row * (options.subdivisions + 1));
  1002. }
  1003. }
  1004. // Normals
  1005. VertexData.ComputeNormals(positions, indices, normals);
  1006. // Result
  1007. var vertexData = new VertexData();
  1008. vertexData.indices = indices;
  1009. vertexData.positions = positions;
  1010. vertexData.normals = normals;
  1011. vertexData.uvs = uvs;
  1012. return vertexData;
  1013. };
  1014. VertexData.CreatePlane = function (options) {
  1015. var indices = [];
  1016. var positions = [];
  1017. var normals = [];
  1018. var uvs = [];
  1019. var width = options.width || options.size || 1;
  1020. var height = options.height || options.size || 1;
  1021. var sideOrientation = (options.sideOrientation === 0) ? 0 : options.sideOrientation || BABYLON.Mesh.DEFAULTSIDE;
  1022. // Vertices
  1023. var halfWidth = width / 2.0;
  1024. var halfHeight = height / 2.0;
  1025. positions.push(-halfWidth, -halfHeight, 0);
  1026. normals.push(0, 0, -1.0);
  1027. uvs.push(0.0, 0.0);
  1028. positions.push(halfWidth, -halfHeight, 0);
  1029. normals.push(0, 0, -1.0);
  1030. uvs.push(1.0, 0.0);
  1031. positions.push(halfWidth, halfHeight, 0);
  1032. normals.push(0, 0, -1.0);
  1033. uvs.push(1.0, 1.0);
  1034. positions.push(-halfWidth, halfHeight, 0);
  1035. normals.push(0, 0, -1.0);
  1036. uvs.push(0.0, 1.0);
  1037. // Indices
  1038. indices.push(0);
  1039. indices.push(1);
  1040. indices.push(2);
  1041. indices.push(0);
  1042. indices.push(2);
  1043. indices.push(3);
  1044. // Sides
  1045. VertexData._ComputeSides(sideOrientation, positions, indices, normals, uvs);
  1046. // Result
  1047. var vertexData = new VertexData();
  1048. vertexData.indices = indices;
  1049. vertexData.positions = positions;
  1050. vertexData.normals = normals;
  1051. vertexData.uvs = uvs;
  1052. return vertexData;
  1053. };
  1054. VertexData.CreateDisc = function (options) {
  1055. var positions = [];
  1056. var indices = [];
  1057. var normals = [];
  1058. var uvs = [];
  1059. var radius = options.radius || 0.5;
  1060. var tessellation = options.tessellation || 64;
  1061. var sideOrientation = (options.sideOrientation === 0) ? 0 : options.sideOrientation || BABYLON.Mesh.DEFAULTSIDE;
  1062. // positions and uvs
  1063. positions.push(0, 0, 0); // disc center first
  1064. uvs.push(0.5, 0.5);
  1065. var step = Math.PI * 2 / tessellation;
  1066. for (var a = 0; a < Math.PI * 2; a += step) {
  1067. var x = Math.cos(a);
  1068. var y = Math.sin(a);
  1069. var u = (x + 1) / 2;
  1070. var v = (1 - y) / 2;
  1071. positions.push(radius * x, radius * y, 0);
  1072. uvs.push(u, v);
  1073. }
  1074. positions.push(positions[3], positions[4], positions[5]); // close the circle
  1075. uvs.push(uvs[2], uvs[3]);
  1076. //indices
  1077. var vertexNb = positions.length / 3;
  1078. for (var i = 1; i < vertexNb - 1; i++) {
  1079. indices.push(i + 1, 0, i);
  1080. }
  1081. // result
  1082. VertexData.ComputeNormals(positions, indices, normals);
  1083. VertexData._ComputeSides(sideOrientation, positions, indices, normals, uvs);
  1084. var vertexData = new VertexData();
  1085. vertexData.indices = indices;
  1086. vertexData.positions = positions;
  1087. vertexData.normals = normals;
  1088. vertexData.uvs = uvs;
  1089. return vertexData;
  1090. };
  1091. // based on http://code.google.com/p/away3d/source/browse/trunk/fp10/Away3D/src/away3d/primitives/TorusKnot.as?spec=svn2473&r=2473
  1092. VertexData.CreateTorusKnot = function (options) {
  1093. var indices = [];
  1094. var positions = [];
  1095. var normals = [];
  1096. var uvs = [];
  1097. var radius = options.radius || 2;
  1098. var tube = options.tube || 0.5;
  1099. var radialSegments = options.radialSegments || 32;
  1100. var tubularSegments = options.tubularSegments || 32;
  1101. var p = options.p || 2;
  1102. var q = options.q || 3;
  1103. var sideOrientation = (options.sideOrientation === 0) ? 0 : options.sideOrientation || BABYLON.Mesh.DEFAULTSIDE;
  1104. // Helper
  1105. var getPos = function (angle) {
  1106. var cu = Math.cos(angle);
  1107. var su = Math.sin(angle);
  1108. var quOverP = q / p * angle;
  1109. var cs = Math.cos(quOverP);
  1110. var tx = radius * (2 + cs) * 0.5 * cu;
  1111. var ty = radius * (2 + cs) * su * 0.5;
  1112. var tz = radius * Math.sin(quOverP) * 0.5;
  1113. return new BABYLON.Vector3(tx, ty, tz);
  1114. };
  1115. // Vertices
  1116. var i;
  1117. var j;
  1118. for (i = 0; i <= radialSegments; i++) {
  1119. var modI = i % radialSegments;
  1120. var u = modI / radialSegments * 2 * p * Math.PI;
  1121. var p1 = getPos(u);
  1122. var p2 = getPos(u + 0.01);
  1123. var tang = p2.subtract(p1);
  1124. var n = p2.add(p1);
  1125. var bitan = BABYLON.Vector3.Cross(tang, n);
  1126. n = BABYLON.Vector3.Cross(bitan, tang);
  1127. bitan.normalize();
  1128. n.normalize();
  1129. for (j = 0; j < tubularSegments; j++) {
  1130. var modJ = j % tubularSegments;
  1131. var v = modJ / tubularSegments * 2 * Math.PI;
  1132. var cx = -tube * Math.cos(v);
  1133. var cy = tube * Math.sin(v);
  1134. positions.push(p1.x + cx * n.x + cy * bitan.x);
  1135. positions.push(p1.y + cx * n.y + cy * bitan.y);
  1136. positions.push(p1.z + cx * n.z + cy * bitan.z);
  1137. uvs.push(i / radialSegments);
  1138. uvs.push(j / tubularSegments);
  1139. }
  1140. }
  1141. for (i = 0; i < radialSegments; i++) {
  1142. for (j = 0; j < tubularSegments; j++) {
  1143. var jNext = (j + 1) % tubularSegments;
  1144. var a = i * tubularSegments + j;
  1145. var b = (i + 1) * tubularSegments + j;
  1146. var c = (i + 1) * tubularSegments + jNext;
  1147. var d = i * tubularSegments + jNext;
  1148. indices.push(d);
  1149. indices.push(b);
  1150. indices.push(a);
  1151. indices.push(d);
  1152. indices.push(c);
  1153. indices.push(b);
  1154. }
  1155. }
  1156. // Normals
  1157. VertexData.ComputeNormals(positions, indices, normals);
  1158. // Sides
  1159. VertexData._ComputeSides(sideOrientation, positions, indices, normals, uvs);
  1160. // Result
  1161. var vertexData = new VertexData();
  1162. vertexData.indices = indices;
  1163. vertexData.positions = positions;
  1164. vertexData.normals = normals;
  1165. vertexData.uvs = uvs;
  1166. return vertexData;
  1167. };
  1168. // Tools
  1169. /**
  1170. * @param {any} - positions (number[] or Float32Array)
  1171. * @param {any} - indices (number[] or Uint16Array)
  1172. * @param {any} - normals (number[] or Float32Array)
  1173. */
  1174. VertexData.ComputeNormals = function (positions, indices, normals) {
  1175. var index = 0;
  1176. // temp Vector3
  1177. var p1p2 = BABYLON.Vector3.Zero();
  1178. var p3p2 = BABYLON.Vector3.Zero();
  1179. var faceNormal = BABYLON.Vector3.Zero();
  1180. var vertexNormali1 = BABYLON.Vector3.Zero();
  1181. for (index = 0; index < positions.length; index++) {
  1182. normals[index] = 0.0;
  1183. }
  1184. // indice triplet = 1 face
  1185. var nbFaces = indices.length / 3;
  1186. for (index = 0; index < nbFaces; index++) {
  1187. var i1 = indices[index * 3];
  1188. var i2 = indices[index * 3 + 1];
  1189. var i3 = indices[index * 3 + 2];
  1190. p1p2.x = positions[i1 * 3] - positions[i2 * 3];
  1191. p1p2.y = positions[i1 * 3 + 1] - positions[i2 * 3 + 1];
  1192. p1p2.z = positions[i1 * 3 + 2] - positions[i2 * 3 + 2];
  1193. p3p2.x = positions[i3 * 3] - positions[i2 * 3];
  1194. p3p2.y = positions[i3 * 3 + 1] - positions[i2 * 3 + 1];
  1195. p3p2.z = positions[i3 * 3 + 2] - positions[i2 * 3 + 2];
  1196. BABYLON.Vector3.CrossToRef(p1p2, p3p2, faceNormal);
  1197. faceNormal.normalize();
  1198. normals[i1 * 3] += faceNormal.x;
  1199. normals[i1 * 3 + 1] += faceNormal.y;
  1200. normals[i1 * 3 + 2] += faceNormal.z;
  1201. normals[i2 * 3] += faceNormal.x;
  1202. normals[i2 * 3 + 1] += faceNormal.y;
  1203. normals[i2 * 3 + 2] += faceNormal.z;
  1204. normals[i3 * 3] += faceNormal.x;
  1205. normals[i3 * 3 + 1] += faceNormal.y;
  1206. normals[i3 * 3 + 2] += faceNormal.z;
  1207. }
  1208. // last normalization
  1209. for (index = 0; index < normals.length / 3; index++) {
  1210. BABYLON.Vector3.FromFloatsToRef(normals[index * 3], normals[index * 3 + 1], normals[index * 3 + 2], vertexNormali1);
  1211. vertexNormali1.normalize();
  1212. normals[index * 3] = vertexNormali1.x;
  1213. normals[index * 3 + 1] = vertexNormali1.y;
  1214. normals[index * 3 + 2] = vertexNormali1.z;
  1215. }
  1216. };
  1217. VertexData._ComputeSides = function (sideOrientation, positions, indices, normals, uvs) {
  1218. var li = indices.length;
  1219. var ln = normals.length;
  1220. var i;
  1221. var n;
  1222. sideOrientation = sideOrientation || BABYLON.Mesh.DEFAULTSIDE;
  1223. switch (sideOrientation) {
  1224. case BABYLON.Mesh.FRONTSIDE:
  1225. // nothing changed
  1226. break;
  1227. case BABYLON.Mesh.BACKSIDE:
  1228. var tmp;
  1229. // indices
  1230. for (i = 0; i < li; i += 3) {
  1231. tmp = indices[i];
  1232. indices[i] = indices[i + 2];
  1233. indices[i + 2] = tmp;
  1234. }
  1235. // normals
  1236. for (n = 0; n < ln; n++) {
  1237. normals[n] = -normals[n];
  1238. }
  1239. break;
  1240. case BABYLON.Mesh.DOUBLESIDE:
  1241. // positions
  1242. var lp = positions.length;
  1243. var l = lp / 3;
  1244. for (var p = 0; p < lp; p++) {
  1245. positions[lp + p] = positions[p];
  1246. }
  1247. // indices
  1248. for (i = 0; i < li; i += 3) {
  1249. indices[i + li] = indices[i + 2] + l;
  1250. indices[i + 1 + li] = indices[i + 1] + l;
  1251. indices[i + 2 + li] = indices[i] + l;
  1252. }
  1253. // normals
  1254. for (n = 0; n < ln; n++) {
  1255. normals[ln + n] = -normals[n];
  1256. }
  1257. // uvs
  1258. var lu = uvs.length;
  1259. for (var u = 0; u < lu; u++) {
  1260. uvs[u + lu] = uvs[u];
  1261. }
  1262. break;
  1263. }
  1264. };
  1265. return VertexData;
  1266. })();
  1267. BABYLON.VertexData = VertexData;
  1268. })(BABYLON || (BABYLON = {}));