babylon.mesh.vertexData.js 16 KB

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