babylon.mesh.vertexData.js 32 KB

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