babylon.mesh.vertexData.js 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889
  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. var radiusTop = diameterTop / 2;
  319. var radiusBottom = diameterBottom / 2;
  320. var indices = [];
  321. var positions = [];
  322. var normals = [];
  323. var uvs = [];
  324. height = height || 1;
  325. diameterTop = diameterTop || 0.5;
  326. diameterBottom = diameterBottom || 1;
  327. tessellation = tessellation || 16;
  328. subdivisions = subdivisions || 1;
  329. subdivisions = (subdivisions < 1) ? 1 : subdivisions;
  330. var getCircleVector = function (i) {
  331. var angle = (i * 2.0 * Math.PI / tessellation);
  332. var dx = Math.cos(angle);
  333. var dz = Math.sin(angle);
  334. return new BABYLON.Vector3(dx, 0, dz);
  335. };
  336. var createCylinderCap = function (isTop) {
  337. var radius = isTop ? radiusTop : radiusBottom;
  338. if (radius == 0) {
  339. return;
  340. }
  341. var vbase = positions.length / 3;
  342. var offset = new BABYLON.Vector3(0, height / 2, 0);
  343. var textureScale = new BABYLON.Vector2(0.5, 0.5);
  344. if (!isTop) {
  345. offset.scaleInPlace(-1);
  346. textureScale.x = -textureScale.x;
  347. }
  348. // Positions, normals & uvs
  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(
  353. circleVector.x * textureScale.x + 0.5,
  354. circleVector.z * textureScale.y + 0.5
  355. );
  356. positions.push(position.x, position.y, position.z);
  357. uvs.push(textureCoordinate.x, textureCoordinate.y);
  358. }
  359. // Indices
  360. for (var i = 0; i < tessellation - 2; i++) {
  361. if (!isTop) {
  362. indices.push(vbase);
  363. indices.push(vbase + (i + 2) % tessellation);
  364. indices.push(vbase + (i + 1) % tessellation);
  365. } else {
  366. indices.push(vbase);
  367. indices.push(vbase + (i + 1) % tessellation);
  368. indices.push(vbase + (i + 2) % tessellation);
  369. }
  370. }
  371. };
  372. var base = new BABYLON.Vector3(0, -1, 0).scale(height / 2);
  373. var offset = new BABYLON.Vector3(0, 1, 0).scale(height / subdivisions);
  374. var stride = tessellation + 1;
  375. // Positions, normals & uvs
  376. for (var i = 0; i <= tessellation; i++) {
  377. var circleVector = getCircleVector(i);
  378. var textureCoordinate = new BABYLON.Vector2(i / tessellation, 0);
  379. var position, radius = radiusBottom;
  380. for (var s = 0; s <= subdivisions; s++) {
  381. // Update variables
  382. position = circleVector.scale(radius);
  383. position.addInPlace(base.add(offset.scale(s)));
  384. textureCoordinate.y += 1 / subdivisions;
  385. radius += (radiusTop - radiusBottom)/subdivisions;
  386. // Push in arrays
  387. positions.push(position.x, position.y, position.z);
  388. uvs.push(textureCoordinate.x, textureCoordinate.y);
  389. }
  390. }
  391. subdivisions += 1;
  392. // Indices
  393. for (var s = 0; s < subdivisions - 1; s++) {
  394. for (var i = 0; i <= tessellation; i++) {
  395. indices.push( i * subdivisions + s);
  396. indices.push((i * subdivisions + (s + subdivisions)) % (stride * subdivisions));
  397. indices.push( i * subdivisions + (s + 1));
  398. indices.push( i * subdivisions + (s + 1));
  399. indices.push((i * subdivisions + (s + subdivisions)) % (stride * subdivisions));
  400. indices.push((i * subdivisions + (s + subdivisions + 1)) % (stride * subdivisions));
  401. }
  402. }
  403. // Create flat triangle fan caps to seal the top and bottom.
  404. createCylinderCap(true);
  405. createCylinderCap(false);
  406. // Normals
  407. BABYLON.VertexData.ComputeNormals(positions, indices, normals);
  408. // Result
  409. var vertexData = new BABYLON.VertexData();
  410. vertexData.indices = indices;
  411. vertexData.positions = positions;
  412. vertexData.normals = normals;
  413. vertexData.uvs = uvs;
  414. return vertexData;
  415. };
  416. VertexData.CreateTorus = function (diameter, thickness, tessellation) {
  417. var indices = [];
  418. var positions = [];
  419. var normals = [];
  420. var uvs = [];
  421. diameter = diameter || 1;
  422. thickness = thickness || 0.5;
  423. tessellation = tessellation || 16;
  424. var stride = tessellation + 1;
  425. for (var i = 0; i <= tessellation; i++) {
  426. var u = i / tessellation;
  427. var outerAngle = i * Math.PI * 2.0 / tessellation - Math.PI / 2.0;
  428. var transform = BABYLON.Matrix.Translation(diameter / 2.0, 0, 0).multiply(BABYLON.Matrix.RotationY(outerAngle));
  429. for (var j = 0; j <= tessellation; j++) {
  430. var v = 1 - j / tessellation;
  431. var innerAngle = j * Math.PI * 2.0 / tessellation + Math.PI;
  432. var dx = Math.cos(innerAngle);
  433. var dy = Math.sin(innerAngle);
  434. // Create a vertex.
  435. var normal = new BABYLON.Vector3(dx, dy, 0);
  436. var position = normal.scale(thickness / 2);
  437. var textureCoordinate = new BABYLON.Vector2(u, v);
  438. position = BABYLON.Vector3.TransformCoordinates(position, transform);
  439. normal = BABYLON.Vector3.TransformNormal(normal, transform);
  440. positions.push(position.x, position.y, position.z);
  441. normals.push(normal.x, normal.y, normal.z);
  442. uvs.push(textureCoordinate.x, textureCoordinate.y);
  443. // And create indices for two triangles.
  444. var nextI = (i + 1) % stride;
  445. var nextJ = (j + 1) % stride;
  446. indices.push(i * stride + j);
  447. indices.push(i * stride + nextJ);
  448. indices.push(nextI * stride + j);
  449. indices.push(i * stride + nextJ);
  450. indices.push(nextI * stride + nextJ);
  451. indices.push(nextI * stride + j);
  452. }
  453. }
  454. // Result
  455. var vertexData = new BABYLON.VertexData();
  456. vertexData.indices = indices;
  457. vertexData.positions = positions;
  458. vertexData.normals = normals;
  459. vertexData.uvs = uvs;
  460. return vertexData;
  461. };
  462. VertexData.CreateLines = function (points) {
  463. var indices = [];
  464. var positions = [];
  465. for (var index = 0; index < points.length; index++) {
  466. positions.push(points[index].x, points[index].y, points[index].z);
  467. if (index > 0) {
  468. indices.push(index - 1);
  469. indices.push(index);
  470. }
  471. }
  472. // Result
  473. var vertexData = new BABYLON.VertexData();
  474. vertexData.indices = indices;
  475. vertexData.positions = positions;
  476. return vertexData;
  477. };
  478. VertexData.CreateGround = function (width, height, subdivisions) {
  479. var indices = [];
  480. var positions = [];
  481. var normals = [];
  482. var uvs = [];
  483. var row, col;
  484. width = width || 1;
  485. height = height || 1;
  486. subdivisions = subdivisions || 1;
  487. for (row = 0; row <= subdivisions; row++) {
  488. for (col = 0; col <= subdivisions; col++) {
  489. var position = new BABYLON.Vector3((col * width) / subdivisions - (width / 2.0), 0, ((subdivisions - row) * height) / subdivisions - (height / 2.0));
  490. var normal = new BABYLON.Vector3(0, 1.0, 0);
  491. positions.push(position.x, position.y, position.z);
  492. normals.push(normal.x, normal.y, normal.z);
  493. uvs.push(col / subdivisions, 1.0 - row / subdivisions);
  494. }
  495. }
  496. for (row = 0; row < subdivisions; row++) {
  497. for (col = 0; col < subdivisions; col++) {
  498. indices.push(col + 1 + (row + 1) * (subdivisions + 1));
  499. indices.push(col + 1 + row * (subdivisions + 1));
  500. indices.push(col + row * (subdivisions + 1));
  501. indices.push(col + (row + 1) * (subdivisions + 1));
  502. indices.push(col + 1 + (row + 1) * (subdivisions + 1));
  503. indices.push(col + row * (subdivisions + 1));
  504. }
  505. }
  506. // Result
  507. var vertexData = new BABYLON.VertexData();
  508. vertexData.indices = indices;
  509. vertexData.positions = positions;
  510. vertexData.normals = normals;
  511. vertexData.uvs = uvs;
  512. return vertexData;
  513. };
  514. VertexData.CreateGroundFromHeightMap = function (width, height, subdivisions, minHeight, maxHeight, buffer, bufferWidth, bufferHeight) {
  515. var indices = [];
  516. var positions = [];
  517. var normals = [];
  518. var uvs = [];
  519. var row, col;
  520. for (row = 0; row <= subdivisions; row++) {
  521. for (col = 0; col <= subdivisions; col++) {
  522. var position = new BABYLON.Vector3((col * width) / subdivisions - (width / 2.0), 0, ((subdivisions - row) * height) / subdivisions - (height / 2.0));
  523. // Compute height
  524. var heightMapX = (((position.x + width / 2) / width) * (bufferWidth - 1)) | 0;
  525. var heightMapY = ((1.0 - (position.z + height / 2) / height) * (bufferHeight - 1)) | 0;
  526. var pos = (heightMapX + heightMapY * bufferWidth) * 4;
  527. var r = buffer[pos] / 255.0;
  528. var g = buffer[pos + 1] / 255.0;
  529. var b = buffer[pos + 2] / 255.0;
  530. var gradient = r * 0.3 + g * 0.59 + b * 0.11;
  531. position.y = minHeight + (maxHeight - minHeight) * gradient;
  532. // Add vertex
  533. positions.push(position.x, position.y, position.z);
  534. normals.push(0, 0, 0);
  535. uvs.push(col / subdivisions, 1.0 - row / subdivisions);
  536. }
  537. }
  538. for (row = 0; row < subdivisions; row++) {
  539. for (col = 0; col < subdivisions; col++) {
  540. indices.push(col + 1 + (row + 1) * (subdivisions + 1));
  541. indices.push(col + 1 + row * (subdivisions + 1));
  542. indices.push(col + row * (subdivisions + 1));
  543. indices.push(col + (row + 1) * (subdivisions + 1));
  544. indices.push(col + 1 + (row + 1) * (subdivisions + 1));
  545. indices.push(col + row * (subdivisions + 1));
  546. }
  547. }
  548. // Normals
  549. BABYLON.VertexData.ComputeNormals(positions, indices, normals);
  550. // Result
  551. var vertexData = new BABYLON.VertexData();
  552. vertexData.indices = indices;
  553. vertexData.positions = positions;
  554. vertexData.normals = normals;
  555. vertexData.uvs = uvs;
  556. return vertexData;
  557. };
  558. VertexData.CreatePlane = function (size) {
  559. var indices = [];
  560. var positions = [];
  561. var normals = [];
  562. var uvs = [];
  563. size = size || 1;
  564. // Vertices
  565. var halfSize = size / 2.0;
  566. positions.push(-halfSize, -halfSize, 0);
  567. normals.push(0, 0, -1.0);
  568. uvs.push(0.0, 0.0);
  569. positions.push(halfSize, -halfSize, 0);
  570. normals.push(0, 0, -1.0);
  571. uvs.push(1.0, 0.0);
  572. positions.push(halfSize, halfSize, 0);
  573. normals.push(0, 0, -1.0);
  574. uvs.push(1.0, 1.0);
  575. positions.push(-halfSize, halfSize, 0);
  576. normals.push(0, 0, -1.0);
  577. uvs.push(0.0, 1.0);
  578. // Indices
  579. indices.push(0);
  580. indices.push(1);
  581. indices.push(2);
  582. indices.push(0);
  583. indices.push(2);
  584. indices.push(3);
  585. // Result
  586. var vertexData = new BABYLON.VertexData();
  587. vertexData.indices = indices;
  588. vertexData.positions = positions;
  589. vertexData.normals = normals;
  590. vertexData.uvs = uvs;
  591. return vertexData;
  592. };
  593. // based on http://code.google.com/p/away3d/source/browse/trunk/fp10/Away3D/src/away3d/primitives/TorusKnot.as?spec=svn2473&r=2473
  594. VertexData.CreateTorusKnot = function (radius, tube, radialSegments, tubularSegments, p, q) {
  595. var indices = [];
  596. var positions = [];
  597. var normals = [];
  598. var uvs = [];
  599. radius = radius || 2;
  600. tube = tube || 0.5;
  601. radialSegments = radialSegments || 32;
  602. tubularSegments = tubularSegments || 32;
  603. p = p || 2;
  604. q = q || 3;
  605. // Helper
  606. var getPos = function (angle) {
  607. var cu = Math.cos(angle);
  608. var su = Math.sin(angle);
  609. var quOverP = q / p * angle;
  610. var cs = Math.cos(quOverP);
  611. var tx = radius * (2 + cs) * 0.5 * cu;
  612. var ty = radius * (2 + cs) * su * 0.5;
  613. var tz = radius * Math.sin(quOverP) * 0.5;
  614. return new BABYLON.Vector3(tx, ty, tz);
  615. };
  616. for (var i = 0; i <= radialSegments; i++) {
  617. var modI = i % radialSegments;
  618. var u = modI / radialSegments * 2 * p * Math.PI;
  619. var p1 = getPos(u);
  620. var p2 = getPos(u + 0.01);
  621. var tang = p2.subtract(p1);
  622. var n = p2.add(p1);
  623. var bitan = BABYLON.Vector3.Cross(tang, n);
  624. n = BABYLON.Vector3.Cross(bitan, tang);
  625. bitan.normalize();
  626. n.normalize();
  627. for (var j = 0; j < tubularSegments; j++) {
  628. var modJ = j % tubularSegments;
  629. var v = modJ / tubularSegments * 2 * Math.PI;
  630. var cx = -tube * Math.cos(v);
  631. var cy = tube * Math.sin(v);
  632. positions.push(p1.x + cx * n.x + cy * bitan.x);
  633. positions.push(p1.y + cx * n.y + cy * bitan.y);
  634. positions.push(p1.z + cx * n.z + cy * bitan.z);
  635. uvs.push(i / radialSegments);
  636. uvs.push(j / tubularSegments);
  637. }
  638. }
  639. for (i = 0; i < radialSegments; i++) {
  640. for (j = 0; j < tubularSegments; j++) {
  641. var jNext = (j + 1) % tubularSegments;
  642. var a = i * tubularSegments + j;
  643. var b = (i + 1) * tubularSegments + j;
  644. var c = (i + 1) * tubularSegments + jNext;
  645. var d = i * tubularSegments + jNext;
  646. indices.push(d);
  647. indices.push(b);
  648. indices.push(a);
  649. indices.push(d);
  650. indices.push(c);
  651. indices.push(b);
  652. }
  653. }
  654. // Normals
  655. BABYLON.VertexData.ComputeNormals(positions, indices, normals);
  656. // Result
  657. var vertexData = new BABYLON.VertexData();
  658. vertexData.indices = indices;
  659. vertexData.positions = positions;
  660. vertexData.normals = normals;
  661. vertexData.uvs = uvs;
  662. return vertexData;
  663. };
  664. // Tools
  665. VertexData.ComputeNormals = function (positions, indices, normals) {
  666. var positionVectors = [];
  667. var facesOfVertices = [];
  668. var index;
  669. for (index = 0; index < positions.length; index += 3) {
  670. var vector3 = new BABYLON.Vector3(positions[index], positions[index + 1], positions[index + 2]);
  671. positionVectors.push(vector3);
  672. facesOfVertices.push([]);
  673. }
  674. // Compute normals
  675. var facesNormals = [];
  676. for (index = 0; index < indices.length / 3; index++) {
  677. var i1 = indices[index * 3];
  678. var i2 = indices[index * 3 + 1];
  679. var i3 = indices[index * 3 + 2];
  680. var p1 = positionVectors[i1];
  681. var p2 = positionVectors[i2];
  682. var p3 = positionVectors[i3];
  683. var p1p2 = p1.subtract(p2);
  684. var p3p2 = p3.subtract(p2);
  685. facesNormals[index] = BABYLON.Vector3.Normalize(BABYLON.Vector3.Cross(p1p2, p3p2));
  686. facesOfVertices[i1].push(index);
  687. facesOfVertices[i2].push(index);
  688. facesOfVertices[i3].push(index);
  689. }
  690. for (index = 0; index < positionVectors.length; index++) {
  691. var faces = facesOfVertices[index];
  692. var normal = BABYLON.Vector3.Zero();
  693. for (var faceIndex = 0; faceIndex < faces.length; faceIndex++) {
  694. normal.addInPlace(facesNormals[faces[faceIndex]]);
  695. }
  696. normal = BABYLON.Vector3.Normalize(normal.scale(1.0 / faces.length));
  697. normals[index * 3] = normal.x;
  698. normals[index * 3 + 1] = normal.y;
  699. normals[index * 3 + 2] = normal.z;
  700. }
  701. };
  702. return VertexData;
  703. })();
  704. BABYLON.VertexData = VertexData;
  705. })(BABYLON || (BABYLON = {}));
  706. //# sourceMappingURL=babylon.mesh.vertexData.js.map