babylon.mesh.vertexData.js 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764
  1. var BABYLON;
  2. (function (BABYLON) {
  3. var VertexData = (function () {
  4. function VertexData() {
  5. }
  6. VertexData.prototype.set = function (data, kind) {
  7. switch (kind) {
  8. case BABYLON.VertexBuffer.PositionKind:
  9. this.positions = data;
  10. break;
  11. case BABYLON.VertexBuffer.NormalKind:
  12. this.normals = data;
  13. break;
  14. case BABYLON.VertexBuffer.UVKind:
  15. this.uvs = data;
  16. break;
  17. case BABYLON.VertexBuffer.UV2Kind:
  18. this.uv2s = data;
  19. break;
  20. case BABYLON.VertexBuffer.ColorKind:
  21. this.colors = data;
  22. break;
  23. case BABYLON.VertexBuffer.MatricesIndicesKind:
  24. this.matricesIndices = data;
  25. break;
  26. case BABYLON.VertexBuffer.MatricesWeightsKind:
  27. this.matricesWeights = data;
  28. break;
  29. }
  30. };
  31. VertexData.prototype.applyToMesh = function (mesh, updatable) {
  32. this._applyTo(mesh, updatable);
  33. };
  34. VertexData.prototype.applyToGeometry = function (geometry, updatable) {
  35. this._applyTo(geometry, updatable);
  36. };
  37. VertexData.prototype.updateMesh = function (mesh, updateExtends, makeItUnique) {
  38. this._update(mesh);
  39. };
  40. VertexData.prototype.updateGeometry = function (geometry, updateExtends, makeItUnique) {
  41. this._update(geometry);
  42. };
  43. VertexData.prototype._applyTo = function (meshOrGeometry, updatable) {
  44. if (this.positions) {
  45. meshOrGeometry.setVerticesData(BABYLON.VertexBuffer.PositionKind, this.positions, updatable);
  46. }
  47. if (this.normals) {
  48. meshOrGeometry.setVerticesData(BABYLON.VertexBuffer.NormalKind, this.normals, updatable);
  49. }
  50. if (this.uvs) {
  51. meshOrGeometry.setVerticesData(BABYLON.VertexBuffer.UVKind, this.uvs, updatable);
  52. }
  53. if (this.uv2s) {
  54. meshOrGeometry.setVerticesData(BABYLON.VertexBuffer.UV2Kind, this.uv2s, updatable);
  55. }
  56. if (this.colors) {
  57. meshOrGeometry.setVerticesData(BABYLON.VertexBuffer.ColorKind, this.colors, updatable);
  58. }
  59. if (this.matricesIndices) {
  60. meshOrGeometry.setVerticesData(BABYLON.VertexBuffer.MatricesIndicesKind, this.matricesIndices, updatable);
  61. }
  62. if (this.matricesWeights) {
  63. meshOrGeometry.setVerticesData(BABYLON.VertexBuffer.MatricesWeightsKind, this.matricesWeights, updatable);
  64. }
  65. if (this.indices) {
  66. meshOrGeometry.setIndices(this.indices);
  67. }
  68. };
  69. VertexData.prototype._update = function (meshOrGeometry, updateExtends, makeItUnique) {
  70. if (this.positions) {
  71. meshOrGeometry.updateVerticesData(BABYLON.VertexBuffer.PositionKind, this.positions, updateExtends, makeItUnique);
  72. }
  73. if (this.normals) {
  74. meshOrGeometry.updateVerticesData(BABYLON.VertexBuffer.NormalKind, this.normals, updateExtends, makeItUnique);
  75. }
  76. if (this.uvs) {
  77. meshOrGeometry.updateVerticesData(BABYLON.VertexBuffer.UVKind, this.uvs, updateExtends, makeItUnique);
  78. }
  79. if (this.uv2s) {
  80. meshOrGeometry.updateVerticesData(BABYLON.VertexBuffer.UV2Kind, this.uv2s, updateExtends, makeItUnique);
  81. }
  82. if (this.colors) {
  83. meshOrGeometry.updateVerticesData(BABYLON.VertexBuffer.ColorKind, this.colors, updateExtends, makeItUnique);
  84. }
  85. if (this.matricesIndices) {
  86. meshOrGeometry.updateVerticesData(BABYLON.VertexBuffer.MatricesIndicesKind, this.matricesIndices, updateExtends, makeItUnique);
  87. }
  88. if (this.matricesWeights) {
  89. meshOrGeometry.updateVerticesData(BABYLON.VertexBuffer.MatricesWeightsKind, this.matricesWeights, updateExtends, makeItUnique);
  90. }
  91. if (this.indices) {
  92. meshOrGeometry.setIndices(this.indices);
  93. }
  94. };
  95. VertexData.prototype.transform = function (matrix) {
  96. var transformed = BABYLON.Vector3.Zero();
  97. if (this.positions) {
  98. var position = BABYLON.Vector3.Zero();
  99. for (var index = 0; index < this.positions.length; index += 3) {
  100. BABYLON.Vector3.FromArrayToRef(this.positions, index, position);
  101. BABYLON.Vector3.TransformCoordinatesToRef(position, matrix, transformed);
  102. this.positions[index] = transformed.x;
  103. this.positions[index + 1] = transformed.y;
  104. this.positions[index + 2] = transformed.z;
  105. }
  106. }
  107. if (this.normals) {
  108. var normal = BABYLON.Vector3.Zero();
  109. for (index = 0; index < this.normals.length; index += 3) {
  110. BABYLON.Vector3.FromArrayToRef(this.normals, index, normal);
  111. BABYLON.Vector3.TransformNormalToRef(normal, matrix, transformed);
  112. this.normals[index] = transformed.x;
  113. this.normals[index + 1] = transformed.y;
  114. this.normals[index + 2] = transformed.z;
  115. }
  116. }
  117. };
  118. VertexData.prototype.merge = function (other) {
  119. if (other.indices) {
  120. if (!this.indices) {
  121. this.indices = [];
  122. }
  123. var offset = this.positions ? this.positions.length / 3 : 0;
  124. for (var index = 0; index < other.indices.length; index++) {
  125. this.indices.push(other.indices[index] + offset);
  126. }
  127. }
  128. if (other.positions) {
  129. if (!this.positions) {
  130. this.positions = [];
  131. }
  132. for (index = 0; index < other.positions.length; index++) {
  133. this.positions.push(other.positions[index]);
  134. }
  135. }
  136. if (other.normals) {
  137. if (!this.normals) {
  138. this.normals = [];
  139. }
  140. for (index = 0; index < other.normals.length; index++) {
  141. this.normals.push(other.normals[index]);
  142. }
  143. }
  144. if (other.uvs) {
  145. if (!this.uvs) {
  146. this.uvs = [];
  147. }
  148. for (index = 0; index < other.uvs.length; index++) {
  149. this.uvs.push(other.uvs[index]);
  150. }
  151. }
  152. if (other.uv2s) {
  153. if (!this.uv2s) {
  154. this.uv2s = [];
  155. }
  156. for (index = 0; index < other.uv2s.length; index++) {
  157. this.uv2s.push(other.uv2s[index]);
  158. }
  159. }
  160. if (other.matricesIndices) {
  161. if (!this.matricesIndices) {
  162. this.matricesIndices = [];
  163. }
  164. for (index = 0; index < other.matricesIndices.length; index++) {
  165. this.matricesIndices.push(other.matricesIndices[index]);
  166. }
  167. }
  168. if (other.matricesWeights) {
  169. if (!this.matricesWeights) {
  170. this.matricesWeights = [];
  171. }
  172. for (index = 0; index < other.matricesWeights.length; index++) {
  173. this.matricesWeights.push(other.matricesWeights[index]);
  174. }
  175. }
  176. if (other.colors) {
  177. if (!this.colors) {
  178. this.colors = [];
  179. }
  180. for (index = 0; index < other.colors.length; index++) {
  181. this.colors.push(other.colors[index]);
  182. }
  183. }
  184. };
  185. // Statics
  186. VertexData.ExtractFromMesh = function (mesh) {
  187. return VertexData._ExtractFrom(mesh);
  188. };
  189. VertexData.ExtractFromGeometry = function (geometry) {
  190. return VertexData._ExtractFrom(geometry);
  191. };
  192. VertexData._ExtractFrom = function (meshOrGeometry) {
  193. var result = new BABYLON.VertexData();
  194. if (meshOrGeometry.isVerticesDataPresent(BABYLON.VertexBuffer.PositionKind)) {
  195. result.positions = meshOrGeometry.getVerticesData(BABYLON.VertexBuffer.PositionKind);
  196. }
  197. if (meshOrGeometry.isVerticesDataPresent(BABYLON.VertexBuffer.NormalKind)) {
  198. result.normals = meshOrGeometry.getVerticesData(BABYLON.VertexBuffer.NormalKind);
  199. }
  200. if (meshOrGeometry.isVerticesDataPresent(BABYLON.VertexBuffer.UVKind)) {
  201. result.uvs = meshOrGeometry.getVerticesData(BABYLON.VertexBuffer.UVKind);
  202. }
  203. if (meshOrGeometry.isVerticesDataPresent(BABYLON.VertexBuffer.UV2Kind)) {
  204. result.uv2s = meshOrGeometry.getVerticesData(BABYLON.VertexBuffer.UV2Kind);
  205. }
  206. if (meshOrGeometry.isVerticesDataPresent(BABYLON.VertexBuffer.ColorKind)) {
  207. result.colors = meshOrGeometry.getVerticesData(BABYLON.VertexBuffer.ColorKind);
  208. }
  209. if (meshOrGeometry.isVerticesDataPresent(BABYLON.VertexBuffer.MatricesIndicesKind)) {
  210. result.matricesIndices = meshOrGeometry.getVerticesData(BABYLON.VertexBuffer.MatricesIndicesKind);
  211. }
  212. if (meshOrGeometry.isVerticesDataPresent(BABYLON.VertexBuffer.MatricesWeightsKind)) {
  213. result.matricesWeights = meshOrGeometry.getVerticesData(BABYLON.VertexBuffer.MatricesWeightsKind);
  214. }
  215. result.indices = meshOrGeometry.getIndices();
  216. return result;
  217. };
  218. VertexData.CreateBox = function (size) {
  219. var normalsSource = [
  220. new BABYLON.Vector3(0, 0, 1),
  221. new BABYLON.Vector3(0, 0, -1),
  222. new BABYLON.Vector3(1, 0, 0),
  223. new BABYLON.Vector3(-1, 0, 0),
  224. new BABYLON.Vector3(0, 1, 0),
  225. new BABYLON.Vector3(0, -1, 0)
  226. ];
  227. var indices = [];
  228. var positions = [];
  229. var normals = [];
  230. var uvs = [];
  231. size = size || 1;
  232. for (var index = 0; index < normalsSource.length; index++) {
  233. var normal = normalsSource[index];
  234. // Get two vectors perpendicular to the face normal and to each other.
  235. var side1 = new BABYLON.Vector3(normal.y, normal.z, normal.x);
  236. var side2 = BABYLON.Vector3.Cross(normal, side1);
  237. // Six indices (two triangles) per face.
  238. var verticesLength = positions.length / 3;
  239. indices.push(verticesLength);
  240. indices.push(verticesLength + 1);
  241. indices.push(verticesLength + 2);
  242. indices.push(verticesLength);
  243. indices.push(verticesLength + 2);
  244. indices.push(verticesLength + 3);
  245. // Four vertices per face.
  246. var vertex = normal.subtract(side1).subtract(side2).scale(size / 2);
  247. positions.push(vertex.x, vertex.y, vertex.z);
  248. normals.push(normal.x, normal.y, normal.z);
  249. uvs.push(1.0, 1.0);
  250. vertex = normal.subtract(side1).add(side2).scale(size / 2);
  251. positions.push(vertex.x, vertex.y, vertex.z);
  252. normals.push(normal.x, normal.y, normal.z);
  253. uvs.push(0.0, 1.0);
  254. vertex = normal.add(side1).add(side2).scale(size / 2);
  255. positions.push(vertex.x, vertex.y, vertex.z);
  256. normals.push(normal.x, normal.y, normal.z);
  257. uvs.push(0.0, 0.0);
  258. vertex = normal.add(side1).subtract(side2).scale(size / 2);
  259. positions.push(vertex.x, vertex.y, vertex.z);
  260. normals.push(normal.x, normal.y, normal.z);
  261. uvs.push(1.0, 0.0);
  262. }
  263. // Result
  264. var vertexData = new BABYLON.VertexData();
  265. vertexData.indices = indices;
  266. vertexData.positions = positions;
  267. vertexData.normals = normals;
  268. vertexData.uvs = uvs;
  269. return vertexData;
  270. };
  271. VertexData.CreateSphere = function (segments, diameter) {
  272. segments = segments || 32;
  273. diameter = diameter || 1;
  274. var radius = diameter / 2;
  275. var totalZRotationSteps = 2 + segments;
  276. var totalYRotationSteps = 2 * totalZRotationSteps;
  277. var indices = [];
  278. var positions = [];
  279. var normals = [];
  280. var uvs = [];
  281. for (var zRotationStep = 0; zRotationStep <= totalZRotationSteps; zRotationStep++) {
  282. var normalizedZ = zRotationStep / totalZRotationSteps;
  283. var angleZ = (normalizedZ * Math.PI);
  284. for (var yRotationStep = 0; yRotationStep <= totalYRotationSteps; yRotationStep++) {
  285. var normalizedY = yRotationStep / totalYRotationSteps;
  286. var angleY = normalizedY * Math.PI * 2;
  287. var rotationZ = BABYLON.Matrix.RotationZ(-angleZ);
  288. var rotationY = BABYLON.Matrix.RotationY(angleY);
  289. var afterRotZ = BABYLON.Vector3.TransformCoordinates(BABYLON.Vector3.Up(), rotationZ);
  290. var complete = BABYLON.Vector3.TransformCoordinates(afterRotZ, rotationY);
  291. var vertex = complete.scale(radius);
  292. var normal = BABYLON.Vector3.Normalize(vertex);
  293. positions.push(vertex.x, vertex.y, vertex.z);
  294. normals.push(normal.x, normal.y, normal.z);
  295. uvs.push(normalizedZ, normalizedY);
  296. }
  297. if (zRotationStep > 0) {
  298. var verticesCount = positions.length / 3;
  299. for (var firstIndex = verticesCount - 2 * (totalYRotationSteps + 1); (firstIndex + totalYRotationSteps + 2) < verticesCount; firstIndex++) {
  300. indices.push((firstIndex));
  301. indices.push((firstIndex + 1));
  302. indices.push(firstIndex + totalYRotationSteps + 1);
  303. indices.push((firstIndex + totalYRotationSteps + 1));
  304. indices.push((firstIndex + 1));
  305. indices.push((firstIndex + totalYRotationSteps + 2));
  306. }
  307. }
  308. }
  309. // Result
  310. var vertexData = new BABYLON.VertexData();
  311. vertexData.indices = indices;
  312. vertexData.positions = positions;
  313. vertexData.normals = normals;
  314. vertexData.uvs = uvs;
  315. return vertexData;
  316. };
  317. VertexData.CreateCylinder = function (height, diameterTop, diameterBottom, tessellation, subdivisions) {
  318. if (subdivisions === void 0) { subdivisions = 1; }
  319. var radiusTop = diameterTop / 2;
  320. var radiusBottom = diameterBottom / 2;
  321. var indices = [];
  322. var positions = [];
  323. var normals = [];
  324. var uvs = [];
  325. height = height || 1;
  326. diameterTop = diameterTop || 0.5;
  327. diameterBottom = diameterBottom || 1;
  328. tessellation = tessellation || 16;
  329. subdivisions = subdivisions || 1;
  330. subdivisions = (subdivisions < 1) ? 1 : subdivisions;
  331. var getCircleVector = function (i) {
  332. var angle = (i * 2.0 * Math.PI / tessellation);
  333. var dx = Math.cos(angle);
  334. var dz = Math.sin(angle);
  335. return new BABYLON.Vector3(dx, 0, dz);
  336. };
  337. var createCylinderCap = function (isTop) {
  338. var radius = isTop ? radiusTop : radiusBottom;
  339. if (radius == 0) {
  340. return;
  341. }
  342. var vbase = positions.length / 3;
  343. var offset = new BABYLON.Vector3(0, height / 2, 0);
  344. var textureScale = new BABYLON.Vector2(0.5, 0.5);
  345. if (!isTop) {
  346. offset.scaleInPlace(-1);
  347. textureScale.x = -textureScale.x;
  348. }
  349. for (i = 0; i < tessellation; i++) {
  350. var circleVector = getCircleVector(i);
  351. var position = circleVector.scale(radius).add(offset);
  352. var textureCoordinate = new BABYLON.Vector2(circleVector.x * textureScale.x + 0.5, circleVector.z * textureScale.y + 0.5);
  353. positions.push(position.x, position.y, position.z);
  354. uvs.push(textureCoordinate.x, textureCoordinate.y);
  355. }
  356. for (var i = 0; i < tessellation - 2; i++) {
  357. if (!isTop) {
  358. indices.push(vbase);
  359. indices.push(vbase + (i + 2) % tessellation);
  360. indices.push(vbase + (i + 1) % tessellation);
  361. }
  362. else {
  363. indices.push(vbase);
  364. indices.push(vbase + (i + 1) % tessellation);
  365. indices.push(vbase + (i + 2) % tessellation);
  366. }
  367. }
  368. };
  369. var base = new BABYLON.Vector3(0, -1, 0).scale(height / 2);
  370. var offset = new BABYLON.Vector3(0, 1, 0).scale(height / subdivisions);
  371. var stride = tessellation + 1;
  372. for (var i = 0; i <= tessellation; i++) {
  373. var circleVector = getCircleVector(i);
  374. var textureCoordinate = new BABYLON.Vector2(i / tessellation, 0);
  375. var position, radius = radiusBottom;
  376. for (var s = 0; s <= subdivisions; s++) {
  377. // Update variables
  378. position = circleVector.scale(radius);
  379. position.addInPlace(base.add(offset.scale(s)));
  380. textureCoordinate.y += 1 / subdivisions;
  381. radius += (radiusTop - radiusBottom) / subdivisions;
  382. // Push in arrays
  383. positions.push(position.x, position.y, position.z);
  384. uvs.push(textureCoordinate.x, textureCoordinate.y);
  385. }
  386. }
  387. subdivisions += 1;
  388. for (var s = 0; s < subdivisions - 1; s++) {
  389. for (var i = 0; i <= tessellation; i++) {
  390. indices.push(i * subdivisions + s);
  391. indices.push((i * subdivisions + (s + subdivisions)) % (stride * subdivisions));
  392. indices.push(i * subdivisions + (s + 1));
  393. indices.push(i * subdivisions + (s + 1));
  394. indices.push((i * subdivisions + (s + subdivisions)) % (stride * subdivisions));
  395. indices.push((i * subdivisions + (s + subdivisions + 1)) % (stride * subdivisions));
  396. }
  397. }
  398. // Create flat triangle fan caps to seal the top and bottom.
  399. createCylinderCap(true);
  400. createCylinderCap(false);
  401. // Normals
  402. BABYLON.VertexData.ComputeNormals(positions, indices, normals);
  403. // Result
  404. var vertexData = new BABYLON.VertexData();
  405. vertexData.indices = indices;
  406. vertexData.positions = positions;
  407. vertexData.normals = normals;
  408. vertexData.uvs = uvs;
  409. return vertexData;
  410. };
  411. VertexData.CreateTorus = function (diameter, thickness, tessellation) {
  412. var indices = [];
  413. var positions = [];
  414. var normals = [];
  415. var uvs = [];
  416. diameter = diameter || 1;
  417. thickness = thickness || 0.5;
  418. tessellation = tessellation || 16;
  419. var stride = tessellation + 1;
  420. for (var i = 0; i <= tessellation; i++) {
  421. var u = i / tessellation;
  422. var outerAngle = i * Math.PI * 2.0 / tessellation - Math.PI / 2.0;
  423. var transform = BABYLON.Matrix.Translation(diameter / 2.0, 0, 0).multiply(BABYLON.Matrix.RotationY(outerAngle));
  424. for (var j = 0; j <= tessellation; j++) {
  425. var v = 1 - j / tessellation;
  426. var innerAngle = j * Math.PI * 2.0 / tessellation + Math.PI;
  427. var dx = Math.cos(innerAngle);
  428. var dy = Math.sin(innerAngle);
  429. // Create a vertex.
  430. var normal = new BABYLON.Vector3(dx, dy, 0);
  431. var position = normal.scale(thickness / 2);
  432. var textureCoordinate = new BABYLON.Vector2(u, v);
  433. position = BABYLON.Vector3.TransformCoordinates(position, transform);
  434. normal = BABYLON.Vector3.TransformNormal(normal, transform);
  435. positions.push(position.x, position.y, position.z);
  436. normals.push(normal.x, normal.y, normal.z);
  437. uvs.push(textureCoordinate.x, textureCoordinate.y);
  438. // And create indices for two triangles.
  439. var nextI = (i + 1) % stride;
  440. var nextJ = (j + 1) % stride;
  441. indices.push(i * stride + j);
  442. indices.push(i * stride + nextJ);
  443. indices.push(nextI * stride + j);
  444. indices.push(i * stride + nextJ);
  445. indices.push(nextI * stride + nextJ);
  446. indices.push(nextI * stride + j);
  447. }
  448. }
  449. // Result
  450. var vertexData = new BABYLON.VertexData();
  451. vertexData.indices = indices;
  452. vertexData.positions = positions;
  453. vertexData.normals = normals;
  454. vertexData.uvs = uvs;
  455. return vertexData;
  456. };
  457. VertexData.CreateLines = function (points) {
  458. var indices = [];
  459. var positions = [];
  460. for (var index = 0; index < points.length; index++) {
  461. positions.push(points[index].x, points[index].y, points[index].z);
  462. if (index > 0) {
  463. indices.push(index - 1);
  464. indices.push(index);
  465. }
  466. }
  467. // Result
  468. var vertexData = new BABYLON.VertexData();
  469. vertexData.indices = indices;
  470. vertexData.positions = positions;
  471. return vertexData;
  472. };
  473. VertexData.CreateGround = function (width, height, subdivisions) {
  474. var indices = [];
  475. var positions = [];
  476. var normals = [];
  477. var uvs = [];
  478. var row, col;
  479. width = width || 1;
  480. height = height || 1;
  481. subdivisions = subdivisions || 1;
  482. for (row = 0; row <= subdivisions; row++) {
  483. for (col = 0; col <= subdivisions; col++) {
  484. var position = new BABYLON.Vector3((col * width) / subdivisions - (width / 2.0), 0, ((subdivisions - row) * height) / subdivisions - (height / 2.0));
  485. var normal = new BABYLON.Vector3(0, 1.0, 0);
  486. positions.push(position.x, position.y, position.z);
  487. normals.push(normal.x, normal.y, normal.z);
  488. uvs.push(col / subdivisions, 1.0 - row / subdivisions);
  489. }
  490. }
  491. for (row = 0; row < subdivisions; row++) {
  492. for (col = 0; col < subdivisions; col++) {
  493. indices.push(col + 1 + (row + 1) * (subdivisions + 1));
  494. indices.push(col + 1 + row * (subdivisions + 1));
  495. indices.push(col + row * (subdivisions + 1));
  496. indices.push(col + (row + 1) * (subdivisions + 1));
  497. indices.push(col + 1 + (row + 1) * (subdivisions + 1));
  498. indices.push(col + row * (subdivisions + 1));
  499. }
  500. }
  501. // Result
  502. var vertexData = new BABYLON.VertexData();
  503. vertexData.indices = indices;
  504. vertexData.positions = positions;
  505. vertexData.normals = normals;
  506. vertexData.uvs = uvs;
  507. return vertexData;
  508. };
  509. VertexData.CreateTiledGround = function (xmin, zmin, xmax, zmax, subdivisions, precision) {
  510. if (subdivisions === void 0) { subdivisions = { w: 1, h: 1 }; }
  511. if (precision === void 0) { precision = { w: 1, h: 1 }; }
  512. var indices = [];
  513. var positions = [];
  514. var normals = [];
  515. var uvs = [];
  516. var row, col, tileRow, tileCol;
  517. subdivisions.h = (subdivisions.w < 1) ? 1 : subdivisions.h;
  518. subdivisions.w = (subdivisions.w < 1) ? 1 : subdivisions.w;
  519. precision.w = (precision.w < 1) ? 1 : precision.w;
  520. precision.h = (precision.h < 1) ? 1 : precision.h;
  521. var tileSize = {
  522. 'w': (xmax - xmin) / subdivisions.w,
  523. 'h': (zmax - zmin) / subdivisions.h
  524. };
  525. for (tileRow = 0; tileRow < subdivisions.h; tileRow++) {
  526. for (tileCol = 0; tileCol < subdivisions.w; tileCol++) {
  527. applyTile(xmin + tileCol * tileSize.w, zmin + tileRow * tileSize.h, xmin + (tileCol + 1) * tileSize.w, zmin + (tileRow + 1) * tileSize.h);
  528. }
  529. }
  530. function applyTile(xTileMin, zTileMin, xTileMax, zTileMax) {
  531. // Indices
  532. var base = positions.length / 3;
  533. var rowLength = precision.w + 1;
  534. for (row = 0; row < precision.h; row++) {
  535. for (col = 0; col < precision.w; col++) {
  536. var square = [
  537. base + col + row * rowLength,
  538. base + (col + 1) + row * rowLength,
  539. base + (col + 1) + (row + 1) * rowLength,
  540. base + col + (row + 1) * rowLength
  541. ];
  542. indices.push(square[1]);
  543. indices.push(square[2]);
  544. indices.push(square[3]);
  545. indices.push(square[0]);
  546. indices.push(square[1]);
  547. indices.push(square[3]);
  548. }
  549. }
  550. // Position, normals and uvs
  551. var position = BABYLON.Vector3.Zero();
  552. var normal = new BABYLON.Vector3(0, 1.0, 0);
  553. for (row = 0; row <= precision.h; row++) {
  554. position.z = (row * (zTileMax - zTileMin)) / precision.h + zTileMin;
  555. for (col = 0; col <= precision.w; col++) {
  556. position.x = (col * (xTileMax - xTileMin)) / precision.w + xTileMin;
  557. position.y = 0;
  558. positions.push(position.x, position.y, position.z);
  559. normals.push(normal.x, normal.y, normal.z);
  560. uvs.push(col / precision.w, row / precision.h);
  561. }
  562. }
  563. }
  564. // Result
  565. var vertexData = new BABYLON.VertexData();
  566. vertexData.indices = indices;
  567. vertexData.positions = positions;
  568. vertexData.normals = normals;
  569. vertexData.uvs = uvs;
  570. return vertexData;
  571. };
  572. VertexData.CreateGroundFromHeightMap = function (width, height, subdivisions, minHeight, maxHeight, buffer, bufferWidth, bufferHeight) {
  573. var indices = [];
  574. var positions = [];
  575. var normals = [];
  576. var uvs = [];
  577. var row, col;
  578. for (row = 0; row <= subdivisions; row++) {
  579. for (col = 0; col <= subdivisions; col++) {
  580. var position = new BABYLON.Vector3((col * width) / subdivisions - (width / 2.0), 0, ((subdivisions - row) * height) / subdivisions - (height / 2.0));
  581. // Compute height
  582. var heightMapX = (((position.x + width / 2) / width) * (bufferWidth - 1)) | 0;
  583. var heightMapY = ((1.0 - (position.z + height / 2) / height) * (bufferHeight - 1)) | 0;
  584. var pos = (heightMapX + heightMapY * bufferWidth) * 4;
  585. var r = buffer[pos] / 255.0;
  586. var g = buffer[pos + 1] / 255.0;
  587. var b = buffer[pos + 2] / 255.0;
  588. var gradient = r * 0.3 + g * 0.59 + b * 0.11;
  589. position.y = minHeight + (maxHeight - minHeight) * gradient;
  590. // Add vertex
  591. positions.push(position.x, position.y, position.z);
  592. normals.push(0, 0, 0);
  593. uvs.push(col / subdivisions, 1.0 - row / subdivisions);
  594. }
  595. }
  596. for (row = 0; row < subdivisions; row++) {
  597. for (col = 0; col < subdivisions; col++) {
  598. indices.push(col + 1 + (row + 1) * (subdivisions + 1));
  599. indices.push(col + 1 + row * (subdivisions + 1));
  600. indices.push(col + row * (subdivisions + 1));
  601. indices.push(col + (row + 1) * (subdivisions + 1));
  602. indices.push(col + 1 + (row + 1) * (subdivisions + 1));
  603. indices.push(col + row * (subdivisions + 1));
  604. }
  605. }
  606. // Normals
  607. BABYLON.VertexData.ComputeNormals(positions, indices, normals);
  608. // Result
  609. var vertexData = new BABYLON.VertexData();
  610. vertexData.indices = indices;
  611. vertexData.positions = positions;
  612. vertexData.normals = normals;
  613. vertexData.uvs = uvs;
  614. return vertexData;
  615. };
  616. VertexData.CreatePlane = function (size) {
  617. var indices = [];
  618. var positions = [];
  619. var normals = [];
  620. var uvs = [];
  621. size = size || 1;
  622. // Vertices
  623. var halfSize = size / 2.0;
  624. positions.push(-halfSize, -halfSize, 0);
  625. normals.push(0, 0, -1.0);
  626. uvs.push(0.0, 0.0);
  627. positions.push(halfSize, -halfSize, 0);
  628. normals.push(0, 0, -1.0);
  629. uvs.push(1.0, 0.0);
  630. positions.push(halfSize, halfSize, 0);
  631. normals.push(0, 0, -1.0);
  632. uvs.push(1.0, 1.0);
  633. positions.push(-halfSize, halfSize, 0);
  634. normals.push(0, 0, -1.0);
  635. uvs.push(0.0, 1.0);
  636. // Indices
  637. indices.push(0);
  638. indices.push(1);
  639. indices.push(2);
  640. indices.push(0);
  641. indices.push(2);
  642. indices.push(3);
  643. // Result
  644. var vertexData = new BABYLON.VertexData();
  645. vertexData.indices = indices;
  646. vertexData.positions = positions;
  647. vertexData.normals = normals;
  648. vertexData.uvs = uvs;
  649. return vertexData;
  650. };
  651. // based on http://code.google.com/p/away3d/source/browse/trunk/fp10/Away3D/src/away3d/primitives/TorusKnot.as?spec=svn2473&r=2473
  652. VertexData.CreateTorusKnot = function (radius, tube, radialSegments, tubularSegments, p, q) {
  653. var indices = [];
  654. var positions = [];
  655. var normals = [];
  656. var uvs = [];
  657. radius = radius || 2;
  658. tube = tube || 0.5;
  659. radialSegments = radialSegments || 32;
  660. tubularSegments = tubularSegments || 32;
  661. p = p || 2;
  662. q = q || 3;
  663. // Helper
  664. var getPos = function (angle) {
  665. var cu = Math.cos(angle);
  666. var su = Math.sin(angle);
  667. var quOverP = q / p * angle;
  668. var cs = Math.cos(quOverP);
  669. var tx = radius * (2 + cs) * 0.5 * cu;
  670. var ty = radius * (2 + cs) * su * 0.5;
  671. var tz = radius * Math.sin(quOverP) * 0.5;
  672. return new BABYLON.Vector3(tx, ty, tz);
  673. };
  674. for (var i = 0; i <= radialSegments; i++) {
  675. var modI = i % radialSegments;
  676. var u = modI / radialSegments * 2 * p * Math.PI;
  677. var p1 = getPos(u);
  678. var p2 = getPos(u + 0.01);
  679. var tang = p2.subtract(p1);
  680. var n = p2.add(p1);
  681. var bitan = BABYLON.Vector3.Cross(tang, n);
  682. n = BABYLON.Vector3.Cross(bitan, tang);
  683. bitan.normalize();
  684. n.normalize();
  685. for (var j = 0; j < tubularSegments; j++) {
  686. var modJ = j % tubularSegments;
  687. var v = modJ / tubularSegments * 2 * Math.PI;
  688. var cx = -tube * Math.cos(v);
  689. var cy = tube * Math.sin(v);
  690. positions.push(p1.x + cx * n.x + cy * bitan.x);
  691. positions.push(p1.y + cx * n.y + cy * bitan.y);
  692. positions.push(p1.z + cx * n.z + cy * bitan.z);
  693. uvs.push(i / radialSegments);
  694. uvs.push(j / tubularSegments);
  695. }
  696. }
  697. for (i = 0; i < radialSegments; i++) {
  698. for (j = 0; j < tubularSegments; j++) {
  699. var jNext = (j + 1) % tubularSegments;
  700. var a = i * tubularSegments + j;
  701. var b = (i + 1) * tubularSegments + j;
  702. var c = (i + 1) * tubularSegments + jNext;
  703. var d = i * tubularSegments + jNext;
  704. indices.push(d);
  705. indices.push(b);
  706. indices.push(a);
  707. indices.push(d);
  708. indices.push(c);
  709. indices.push(b);
  710. }
  711. }
  712. // Normals
  713. BABYLON.VertexData.ComputeNormals(positions, indices, normals);
  714. // Result
  715. var vertexData = new BABYLON.VertexData();
  716. vertexData.indices = indices;
  717. vertexData.positions = positions;
  718. vertexData.normals = normals;
  719. vertexData.uvs = uvs;
  720. return vertexData;
  721. };
  722. // Tools
  723. VertexData.ComputeNormals = function (positions, indices, normals) {
  724. var positionVectors = [];
  725. var facesOfVertices = [];
  726. var index;
  727. for (index = 0; index < positions.length; index += 3) {
  728. var vector3 = new BABYLON.Vector3(positions[index], positions[index + 1], positions[index + 2]);
  729. positionVectors.push(vector3);
  730. facesOfVertices.push([]);
  731. }
  732. // Compute normals
  733. var facesNormals = [];
  734. for (index = 0; index < indices.length / 3; index++) {
  735. var i1 = indices[index * 3];
  736. var i2 = indices[index * 3 + 1];
  737. var i3 = indices[index * 3 + 2];
  738. var p1 = positionVectors[i1];
  739. var p2 = positionVectors[i2];
  740. var p3 = positionVectors[i3];
  741. var p1p2 = p1.subtract(p2);
  742. var p3p2 = p3.subtract(p2);
  743. facesNormals[index] = BABYLON.Vector3.Normalize(BABYLON.Vector3.Cross(p1p2, p3p2));
  744. facesOfVertices[i1].push(index);
  745. facesOfVertices[i2].push(index);
  746. facesOfVertices[i3].push(index);
  747. }
  748. for (index = 0; index < positionVectors.length; index++) {
  749. var faces = facesOfVertices[index];
  750. var normal = BABYLON.Vector3.Zero();
  751. for (var faceIndex = 0; faceIndex < faces.length; faceIndex++) {
  752. normal.addInPlace(facesNormals[faces[faceIndex]]);
  753. }
  754. normal = BABYLON.Vector3.Normalize(normal.scale(1.0 / faces.length));
  755. normals[index * 3] = normal.x;
  756. normals[index * 3 + 1] = normal.y;
  757. normals[index * 3 + 2] = normal.z;
  758. }
  759. };
  760. return VertexData;
  761. })();
  762. BABYLON.VertexData = VertexData;
  763. })(BABYLON || (BABYLON = {}));
  764. //# sourceMappingURL=babylon.mesh.vertexData.js.map