babylon.mesh.vertexData.js 30 KB

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