babylon.mesh.vertexData.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  1. "use strict";
  2. var BABYLON = BABYLON || {};
  3. (function () {
  4. BABYLON.VertexData = function () {
  5. };
  6. // Methods
  7. BABYLON.VertexData.prototype.applyToMesh = function(mesh, updatable) {
  8. if (this.positions) {
  9. mesh.setVerticesData(this.positions, BABYLON.VertexBuffer.PositionKind, updatable);
  10. }
  11. if (this.normals) {
  12. mesh.setVerticesData(this.normals, BABYLON.VertexBuffer.NormalKind, updatable);
  13. }
  14. if (this.uvs) {
  15. mesh.setVerticesData(this.uvs, BABYLON.VertexBuffer.UVKind, updatable);
  16. }
  17. if (this.uv2s) {
  18. mesh.setVerticesData(this.uv2s, BABYLON.VertexBuffer.UV2Kind, updatable);
  19. }
  20. if (this.colors) {
  21. mesh.setVerticesData(this.colors, BABYLON.VertexBuffer.ColorKind, updatable);
  22. }
  23. if (this.matricesIndices) {
  24. mesh.setVerticesData(this.matricesIndices, BABYLON.VertexBuffer.MatricesIndicesKind, updatable);
  25. }
  26. if (this.matricesWeights) {
  27. mesh.setVerticesData(this.matricesWeights, BABYLON.VertexBuffer.MatricesWeightsKind, updatable);
  28. }
  29. if (this.indices) {
  30. mesh.setIndices(this.indices);
  31. }
  32. };
  33. BABYLON.VertexData.prototype.transform = function (matrix) {
  34. var transformed = BABYLON.Vector3.Zero();
  35. if (this.positions) {
  36. var position = BABYLON.Vector3.Zero();
  37. for (var index = 0; index < this.positions.length; index += 3) {
  38. BABYLON.Vector3.FromArrayToRef(this.positions, index, position);
  39. BABYLON.Vector3.TransformCoordinatesToRef(position, matrix, transformed);
  40. this.positions[index] = transformed.x;
  41. this.positions[index + 1] = transformed.y;
  42. this.positions[index + 2] = transformed.z;
  43. }
  44. }
  45. if (this.normals) {
  46. var normal = BABYLON.Vector3.Zero();
  47. for (index = 0; index < this.normals.length; index += 3) {
  48. BABYLON.Vector3.FromArrayToRef(this.normals, index, normal);
  49. BABYLON.Vector3.TransformNormalToRef(normal, matrix, transformed);
  50. this.normals[index] = transformed.x;
  51. this.normals[index + 1] = transformed.y;
  52. this.normals[index + 2] = transformed.z;
  53. }
  54. }
  55. };
  56. BABYLON.VertexData.prototype.merge = function (other) {
  57. if (other.indices) {
  58. if (!this.indices) {
  59. this.indices = [];
  60. }
  61. var offset = this.positions ? this.positions.length / 3 : 0;
  62. for (var index = 0; index < other.indices.length; index++) {
  63. this.indices.push(other.indices[index] + offset);
  64. }
  65. }
  66. if (other.positions) {
  67. if (!this.positions) {
  68. this.positions = [];
  69. }
  70. for (index = 0; index < other.positions.length; index++) {
  71. this.positions.push(other.positions[index]);
  72. }
  73. }
  74. if (other.normals) {
  75. if (!this.normals) {
  76. this.normals = [];
  77. }
  78. for (index = 0; index < other.normals.length; index++) {
  79. this.normals.push(other.normals[index]);
  80. }
  81. }
  82. if (other.uvs) {
  83. if (!this.uvs) {
  84. this.uvs = [];
  85. }
  86. for (index = 0; index < other.uvs.length; index++) {
  87. this.uvs.push(other.uvs[index]);
  88. }
  89. }
  90. if (other.uv2s) {
  91. if (!this.uv2s) {
  92. this.uv2s = [];
  93. }
  94. for (index = 0; index < other.uv2s.length; index++) {
  95. this.uv2s.push(other.uv2s[index]);
  96. }
  97. }
  98. if (other.matricesIndices) {
  99. if (!this.matricesIndices) {
  100. this.matricesIndices = [];
  101. }
  102. for (index = 0; index < other.matricesIndices.length; index++) {
  103. this.matricesIndices.push(other.matricesIndices[index]);
  104. }
  105. }
  106. if (other.matricesWeights) {
  107. if (!this.matricesWeights) {
  108. this.matricesWeights = [];
  109. }
  110. for (index = 0; index < other.matricesWeights.length; index++) {
  111. this.matricesWeights.push(other.matricesWeights[index]);
  112. }
  113. }
  114. if (other.colors) {
  115. if (!this.colors) {
  116. this.colors = [];
  117. }
  118. for (index = 0; index < other.colors.length; index++) {
  119. this.colors.push(other.colors[index]);
  120. }
  121. }
  122. };
  123. // Statics
  124. BABYLON.VertexData.ExtractFromMesh = function (mesh) {
  125. var result = new BABYLON.VertexData();
  126. if (mesh.isVerticesDataPresent(BABYLON.VertexBuffer.PositionKind)) {
  127. result.positions = mesh.getVerticesData(BABYLON.VertexBuffer.PositionKind);
  128. }
  129. if (mesh.isVerticesDataPresent(BABYLON.VertexBuffer.NormalKind)) {
  130. result.normals = mesh.getVerticesData(BABYLON.VertexBuffer.NormalKind);
  131. }
  132. if (mesh.isVerticesDataPresent(BABYLON.VertexBuffer.UVKind)) {
  133. result.uvs = mesh.getVerticesData(BABYLON.VertexBuffer.UVKind);
  134. }
  135. if (mesh.isVerticesDataPresent(BABYLON.VertexBuffer.UV2Kind)) {
  136. result.uv2s = mesh.getVerticesData(BABYLON.VertexBuffer.UV2Kind);
  137. }
  138. if (mesh.isVerticesDataPresent(BABYLON.VertexBuffer.ColorKind)) {
  139. result.colors = mesh.getVerticesData(BABYLON.VertexBuffer.ColorKind);
  140. }
  141. if (mesh.isVerticesDataPresent(BABYLON.VertexBuffer.MatricesIndicesKind)) {
  142. result.matricesIndices = mesh.getVerticesData(BABYLON.VertexBuffer.MatricesIndicesKind);
  143. }
  144. if (mesh.isVerticesDataPresent(BABYLON.VertexBuffer.MatricesWeightsKind)) {
  145. result.matricesWeights = mesh.getVerticesData(BABYLON.VertexBuffer.MatricesWeightsKind);
  146. }
  147. result.indices = mesh.getIndices();
  148. return result;
  149. };
  150. BABYLON.VertexData.CreateBox = function(size) {
  151. var normalsSource = [
  152. new BABYLON.Vector3(0, 0, 1),
  153. new BABYLON.Vector3(0, 0, -1),
  154. new BABYLON.Vector3(1, 0, 0),
  155. new BABYLON.Vector3(-1, 0, 0),
  156. new BABYLON.Vector3(0, 1, 0),
  157. new BABYLON.Vector3(0, -1, 0)
  158. ];
  159. var indices = [];
  160. var positions = [];
  161. var normals = [];
  162. var uvs = [];
  163. // Create each face in turn.
  164. for (var index = 0; index < normalsSource.length; index++) {
  165. var normal = normalsSource[index];
  166. // Get two vectors perpendicular to the face normal and to each other.
  167. var side1 = new BABYLON.Vector3(normal.y, normal.z, normal.x);
  168. var side2 = BABYLON.Vector3.Cross(normal, side1);
  169. // Six indices (two triangles) per face.
  170. var verticesLength = positions.length / 3;
  171. indices.push(verticesLength);
  172. indices.push(verticesLength + 1);
  173. indices.push(verticesLength + 2);
  174. indices.push(verticesLength);
  175. indices.push(verticesLength + 2);
  176. indices.push(verticesLength + 3);
  177. // Four vertices per face.
  178. var vertex = normal.subtract(side1).subtract(side2).scale(size / 2);
  179. positions.push(vertex.x, vertex.y, vertex.z);
  180. normals.push(normal.x, normal.y, normal.z);
  181. uvs.push(1.0, 1.0);
  182. vertex = normal.subtract(side1).add(side2).scale(size / 2);
  183. positions.push(vertex.x, vertex.y, vertex.z);
  184. normals.push(normal.x, normal.y, normal.z);
  185. uvs.push(0.0, 1.0);
  186. vertex = normal.add(side1).add(side2).scale(size / 2);
  187. positions.push(vertex.x, vertex.y, vertex.z);
  188. normals.push(normal.x, normal.y, normal.z);
  189. uvs.push(0.0, 0.0);
  190. vertex = normal.add(side1).subtract(side2).scale(size / 2);
  191. positions.push(vertex.x, vertex.y, vertex.z);
  192. normals.push(normal.x, normal.y, normal.z);
  193. uvs.push(1.0, 0.0);
  194. }
  195. // Result
  196. var vertexData = new BABYLON.VertexData();
  197. vertexData.indices = indices;
  198. vertexData.positions = positions;
  199. vertexData.normals = normals;
  200. vertexData.uvs = uvs;
  201. return vertexData;
  202. };
  203. BABYLON.VertexData.CreateSphere = function (segments, diameter) {
  204. var radius = diameter / 2;
  205. var totalZRotationSteps = 2 + segments;
  206. var totalYRotationSteps = 2 * totalZRotationSteps;
  207. var indices = [];
  208. var positions = [];
  209. var normals = [];
  210. var uvs = [];
  211. for (var zRotationStep = 0; zRotationStep <= totalZRotationSteps; zRotationStep++) {
  212. var normalizedZ = zRotationStep / totalZRotationSteps;
  213. var angleZ = (normalizedZ * Math.PI);
  214. for (var yRotationStep = 0; yRotationStep <= totalYRotationSteps; yRotationStep++) {
  215. var normalizedY = yRotationStep / totalYRotationSteps;
  216. var angleY = normalizedY * Math.PI * 2;
  217. var rotationZ = BABYLON.Matrix.RotationZ(-angleZ);
  218. var rotationY = BABYLON.Matrix.RotationY(angleY);
  219. var afterRotZ = BABYLON.Vector3.TransformCoordinates(BABYLON.Vector3.Up(), rotationZ);
  220. var complete = BABYLON.Vector3.TransformCoordinates(afterRotZ, rotationY);
  221. var vertex = complete.scale(radius);
  222. var normal = BABYLON.Vector3.Normalize(vertex);
  223. positions.push(vertex.x, vertex.y, vertex.z);
  224. normals.push(normal.x, normal.y, normal.z);
  225. uvs.push(normalizedZ, normalizedY);
  226. }
  227. if (zRotationStep > 0) {
  228. var verticesCount = positions.length / 3;
  229. for (var firstIndex = verticesCount - 2 * (totalYRotationSteps + 1) ; (firstIndex + totalYRotationSteps + 2) < verticesCount; firstIndex++) {
  230. indices.push((firstIndex));
  231. indices.push((firstIndex + 1));
  232. indices.push(firstIndex + totalYRotationSteps + 1);
  233. indices.push((firstIndex + totalYRotationSteps + 1));
  234. indices.push((firstIndex + 1));
  235. indices.push((firstIndex + totalYRotationSteps + 2));
  236. }
  237. }
  238. }
  239. // Result
  240. var vertexData = new BABYLON.VertexData();
  241. vertexData.indices = indices;
  242. vertexData.positions = positions;
  243. vertexData.normals = normals;
  244. vertexData.uvs = uvs;
  245. return vertexData;
  246. };
  247. BABYLON.VertexData.CreateCylinder = function (height, diameterTop, diameterBottom, tessellation) {
  248. var radiusTop = diameterTop / 2;
  249. var radiusBottom = diameterBottom / 2;
  250. var indices = [];
  251. var positions = [];
  252. var normals = [];
  253. var uvs = [];
  254. var getCircleVector = function (i) {
  255. var angle = (i * 2.0 * Math.PI / tessellation);
  256. var dx = Math.sin(angle);
  257. var dz = Math.cos(angle);
  258. return new BABYLON.Vector3(dx, 0, dz);
  259. };
  260. var createCylinderCap = function (isTop) {
  261. var radius = isTop ? radiusTop : radiusBottom;
  262. if (radius == 0) {
  263. return;
  264. }
  265. // Create cap indices.
  266. for (var i = 0; i < tessellation - 2; i++) {
  267. var i1 = (i + 1) % tessellation;
  268. var i2 = (i + 2) % tessellation;
  269. if (!isTop) {
  270. var tmp = i1;
  271. var i1 = i2;
  272. i2 = tmp;
  273. }
  274. var vbase = positions.length / 3;
  275. indices.push(vbase);
  276. indices.push(vbase + i1);
  277. indices.push(vbase + i2);
  278. }
  279. // Which end of the cylinder is this?
  280. var normal = new BABYLON.Vector3(0, -1, 0);
  281. var textureScale = new BABYLON.Vector2(-0.5, -0.5);
  282. if (!isTop) {
  283. normal = normal.scale(-1);
  284. textureScale.x = -textureScale.x;
  285. }
  286. // Create cap vertices.
  287. for (var i = 0; i < tessellation; i++) {
  288. var circleVector = getCircleVector(i);
  289. var position = circleVector.scale(radius).add(normal.scale(height));
  290. var textureCoordinate = new BABYLON.Vector2(circleVector.x * textureScale.x + 0.5, circleVector.z * textureScale.y + 0.5);
  291. positions.push(position.x, position.y, position.z);
  292. normals.push(normal.x, normal.y, normal.z);
  293. uvs.push(textureCoordinate.x, textureCoordinate.y);
  294. }
  295. };
  296. height /= 2;
  297. var topOffset = new BABYLON.Vector3(0, 1, 0).scale(height);
  298. var stride = tessellation + 1;
  299. // Create a ring of triangles around the outside of the cylinder.
  300. for (var i = 0; i <= tessellation; i++) {
  301. var normal = getCircleVector(i);
  302. var sideOffsetBottom = normal.scale(radiusBottom);
  303. var sideOffsetTop = normal.scale(radiusTop);
  304. var textureCoordinate = new BABYLON.Vector2(i / tessellation, 0);
  305. var position = sideOffsetBottom.add(topOffset);
  306. positions.push(position.x, position.y, position.z);
  307. normals.push(normal.x, normal.y, normal.z);
  308. uvs.push(textureCoordinate.x, textureCoordinate.y);
  309. position = sideOffsetTop.subtract(topOffset);
  310. textureCoordinate.y += 1;
  311. positions.push(position.x, position.y, position.z);
  312. normals.push(normal.x, normal.y, normal.z);
  313. uvs.push(textureCoordinate.x, textureCoordinate.y);
  314. indices.push(i * 2);
  315. indices.push((i * 2 + 2) % (stride * 2));
  316. indices.push(i * 2 + 1);
  317. indices.push(i * 2 + 1);
  318. indices.push((i * 2 + 2) % (stride * 2));
  319. indices.push((i * 2 + 3) % (stride * 2));
  320. }
  321. // Create flat triangle fan caps to seal the top and bottom.
  322. createCylinderCap(true);
  323. createCylinderCap(false);
  324. // Result
  325. var vertexData = new BABYLON.VertexData();
  326. vertexData.indices = indices;
  327. vertexData.positions = positions;
  328. vertexData.normals = normals;
  329. vertexData.uvs = uvs;
  330. return vertexData;
  331. };
  332. BABYLON.VertexData.CreateTorus = function (diameter, thickness, tessellation) {
  333. var indices = [];
  334. var positions = [];
  335. var normals = [];
  336. var uvs = [];
  337. var stride = tessellation + 1;
  338. for (var i = 0; i <= tessellation; i++) {
  339. var u = i / tessellation;
  340. var outerAngle = i * Math.PI * 2.0 / tessellation - Math.PI / 2.0;
  341. var transform = BABYLON.Matrix.Translation(diameter / 2.0, 0, 0).multiply(BABYLON.Matrix.RotationY(outerAngle));
  342. for (var j = 0; j <= tessellation; j++) {
  343. var v = 1 - j / tessellation;
  344. var innerAngle = j * Math.PI * 2.0 / tessellation + Math.PI;
  345. var dx = Math.cos(innerAngle);
  346. var dy = Math.sin(innerAngle);
  347. // Create a vertex.
  348. var normal = new BABYLON.Vector3(dx, dy, 0);
  349. var position = normal.scale(thickness / 2);
  350. var textureCoordinate = new BABYLON.Vector2(u, v);
  351. position = BABYLON.Vector3.TransformCoordinates(position, transform);
  352. normal = BABYLON.Vector3.TransformNormal(normal, transform);
  353. positions.push(position.x, position.y, position.z);
  354. normals.push(normal.x, normal.y, normal.z);
  355. uvs.push(textureCoordinate.x, textureCoordinate.y);
  356. // And create indices for two triangles.
  357. var nextI = (i + 1) % stride;
  358. var nextJ = (j + 1) % stride;
  359. indices.push(i * stride + j);
  360. indices.push(i * stride + nextJ);
  361. indices.push(nextI * stride + j);
  362. indices.push(i * stride + nextJ);
  363. indices.push(nextI * stride + nextJ);
  364. indices.push(nextI * stride + j);
  365. }
  366. }
  367. // Result
  368. var vertexData = new BABYLON.VertexData();
  369. vertexData.indices = indices;
  370. vertexData.positions = positions;
  371. vertexData.normals = normals;
  372. vertexData.uvs = uvs;
  373. return vertexData;
  374. };
  375. BABYLON.VertexData.CreateGround = function (width, height, subdivisions) {
  376. var indices = [];
  377. var positions = [];
  378. var normals = [];
  379. var uvs = [];
  380. var row, col;
  381. for (row = 0; row <= subdivisions; row++) {
  382. for (col = 0; col <= subdivisions; col++) {
  383. var position = new BABYLON.Vector3((col * width) / subdivisions - (width / 2.0), 0, ((subdivisions - row) * height) / subdivisions - (height / 2.0));
  384. var normal = new BABYLON.Vector3(0, 1.0, 0);
  385. positions.push(position.x, position.y, position.z);
  386. normals.push(normal.x, normal.y, normal.z);
  387. uvs.push(col / subdivisions, 1.0 - row / subdivisions);
  388. }
  389. }
  390. for (row = 0; row < subdivisions; row++) {
  391. for (col = 0; col < subdivisions; col++) {
  392. indices.push(col + 1 + (row + 1) * (subdivisions + 1));
  393. indices.push(col + 1 + row * (subdivisions + 1));
  394. indices.push(col + row * (subdivisions + 1));
  395. indices.push(col + (row + 1) * (subdivisions + 1));
  396. indices.push(col + 1 + (row + 1) * (subdivisions + 1));
  397. indices.push(col + row * (subdivisions + 1));
  398. }
  399. }
  400. // Result
  401. var vertexData = new BABYLON.VertexData();
  402. vertexData.indices = indices;
  403. vertexData.positions = positions;
  404. vertexData.normals = normals;
  405. vertexData.uvs = uvs;
  406. return vertexData;
  407. };
  408. BABYLON.VertexData.CreatePlane = function (size) {
  409. var indices = [];
  410. var positions = [];
  411. var normals = [];
  412. var uvs = [];
  413. // Vertices
  414. var halfSize = size / 2.0;
  415. positions.push(-halfSize, -halfSize, 0);
  416. normals.push(0, 0, -1.0);
  417. uvs.push(0.0, 0.0);
  418. positions.push(halfSize, -halfSize, 0);
  419. normals.push(0, 0, -1.0);
  420. uvs.push(1.0, 0.0);
  421. positions.push(halfSize, halfSize, 0);
  422. normals.push(0, 0, -1.0);
  423. uvs.push(1.0, 1.0);
  424. positions.push(-halfSize, halfSize, 0);
  425. normals.push(0, 0, -1.0);
  426. uvs.push(0.0, 1.0);
  427. // Indices
  428. indices.push(0);
  429. indices.push(1);
  430. indices.push(2);
  431. indices.push(0);
  432. indices.push(2);
  433. indices.push(3);
  434. // Result
  435. var vertexData = new BABYLON.VertexData();
  436. vertexData.indices = indices;
  437. vertexData.positions = positions;
  438. vertexData.normals = normals;
  439. vertexData.uvs = uvs;
  440. return vertexData;
  441. };
  442. })();