babylon.mesh.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703
  1. var BABYLON = BABYLON || {};
  2. (function () {
  3. BABYLON.Mesh = function (name, vertexDeclaration, scene) {
  4. this.name = name;
  5. this.id = name;
  6. this._scene = scene;
  7. this._vertexDeclaration = vertexDeclaration;
  8. this._vertexStrideSize = 0;
  9. for (var index = 0; index < vertexDeclaration.length; index++) {
  10. this._vertexStrideSize += vertexDeclaration[index];
  11. }
  12. this._vertexStrideSize *= 4; // sizeof(float)
  13. this._totalVertices = 0;
  14. this._worldMatrix = BABYLON.Matrix.Identity();
  15. scene.meshes.push(this);
  16. this.position = new BABYLON.Vector3(0, 0, 0);
  17. this.rotation = new BABYLON.Vector3(0, 0, 0);
  18. this.scaling = new BABYLON.Vector3(1, 1, 1);
  19. this._vertices = [];
  20. this._indices = [];
  21. this.subMeshes = [];
  22. // Animations
  23. this.animations = [];
  24. // Cache
  25. this._positions = null;
  26. this._cache = {
  27. position: null,
  28. scaling: null,
  29. rotation: null
  30. };
  31. this._childrenFlag = false;
  32. };
  33. // Constants
  34. BABYLON.Mesh.BILLBOARDMODE_NONE = 0;
  35. BABYLON.Mesh.BILLBOARDMODE_X = 1;
  36. BABYLON.Mesh.BILLBOARDMODE_Y = 2;
  37. BABYLON.Mesh.BILLBOARDMODE_Z = 4;
  38. BABYLON.Mesh.BILLBOARDMODE_ALL = 7;
  39. // Members
  40. BABYLON.Mesh.prototype.material = null;
  41. BABYLON.Mesh.prototype.parent = null;
  42. BABYLON.Mesh.prototype._isEnabled = true;
  43. BABYLON.Mesh.prototype.isVisible = true;
  44. BABYLON.Mesh.prototype.visibility = 1.0;
  45. BABYLON.Mesh.prototype.billboardMode = BABYLON.Mesh.BILLBOARDMODE_NONE;
  46. BABYLON.Mesh.prototype.checkCollisions = false;
  47. BABYLON.Mesh.prototype.onDispose = false;
  48. // Properties
  49. BABYLON.Mesh.prototype.getScene = function () {
  50. return this._scene;
  51. };
  52. BABYLON.Mesh.prototype.getWorldMatrix = function () {
  53. return this._worldMatrix;
  54. };
  55. BABYLON.Mesh.prototype.getTotalVertices = function () {
  56. return this._totalVertices;
  57. };
  58. BABYLON.Mesh.prototype.getVertices = function () {
  59. return this._vertices;
  60. };
  61. BABYLON.Mesh.prototype.getVertexStrideSize = function () {
  62. return this._vertexStrideSize;
  63. };
  64. BABYLON.Mesh.prototype.getFloatVertexStrideSize = function () {
  65. return this._vertexStrideSize / 4;
  66. };
  67. BABYLON.Mesh.prototype._needToSynchonizeChildren = function () {
  68. return this._childrenFlag;
  69. };
  70. BABYLON.Mesh.prototype.isSynchronized = function () {
  71. if (this.billboardMode !== BABYLON.Mesh.BILLBOARDMODE_NONE)
  72. return false;
  73. if (!this._cache.position || !this._cache.rotation || !this._cache.scaling) {
  74. return false;
  75. }
  76. if (!this._cache.position.equals(this.position))
  77. return false;
  78. if (!this._cache.rotation.equals(this.rotation))
  79. return false;
  80. if (!this._cache.scaling.equals(this.scaling))
  81. return false;
  82. if (this.parent)
  83. return !this.parent._needToSynchonizeChildren();
  84. return true;
  85. };
  86. BABYLON.Mesh.prototype.isEnabled = function () {
  87. if (!this._isEnabled) {
  88. return false;
  89. }
  90. if (this.parent) {
  91. return this.parent.isEnabled();
  92. }
  93. return true;
  94. };
  95. BABYLON.Mesh.prototype.setEnabled = function (value) {
  96. this._isEnabled = value;
  97. };
  98. BABYLON.Mesh.prototype.isAnimated = function () {
  99. return this._animationStarted;
  100. };
  101. // Methods
  102. BABYLON.Mesh.prototype.computeWorldMatrix = function () {
  103. if (this.isSynchronized()) {
  104. this._childrenFlag = false;
  105. return this._worldMatrix;
  106. }
  107. this._childrenFlag = true;
  108. this._cache.position = this.position.clone();
  109. this._cache.rotation = this.rotation.clone();
  110. this._cache.scaling = this.scaling.clone();
  111. var localWorld = BABYLON.Matrix.Scaling(this.scaling.x, this.scaling.y, this.scaling.z).multiply(
  112. BABYLON.Matrix.RotationYawPitchRoll(this.rotation.y, this.rotation.x, this.rotation.z));
  113. // Billboarding
  114. var matTranslation = BABYLON.Matrix.Translation(this.position.x, this.position.y, this.position.z);
  115. if (this.billboardMode !== BABYLON.Mesh.BILLBOARDMODE_NONE) {
  116. var localPosition = this.position.clone();
  117. var zero = this._scene.activeCamera.position.clone();
  118. if (this.parent) {
  119. localPosition = localPosition.add(this.parent.position);
  120. matTranslation = BABYLON.Matrix.Translation(localPosition.x, localPosition.y, localPosition.z);
  121. }
  122. if (this.billboardMode & BABYLON.Mesh.BILLBOARDMODE_ALL === BABYLON.Mesh.BILLBOARDMODE_ALL) {
  123. zero = this._scene.activeCamera.position;
  124. } else {
  125. if (this.billboardMode & BABYLON.Mesh.BILLBOARDMODE_X)
  126. zero.x = localPosition.x + BABYLON.Engine.epsilon;
  127. if (this.billboardMode & BABYLON.Mesh.BILLBOARDMODE_Y)
  128. zero.y = localPosition.y + BABYLON.Engine.epsilon;
  129. if (this.billboardMode & BABYLON.Mesh.BILLBOARDMODE_Z)
  130. zero.z = localPosition.z + BABYLON.Engine.epsilon;
  131. }
  132. var matBillboard = BABYLON.Matrix.LookAtLH(localPosition, zero, BABYLON.Vector3.Up());
  133. matBillboard.m[12] = matBillboard.m[13] = matBillboard.m[14] = 0;
  134. matBillboard.invert();
  135. localWorld = BABYLON.Matrix.RotationY(Math.PI).multiply(localWorld.multiply(matBillboard));
  136. }
  137. localWorld = localWorld.multiply(matTranslation);
  138. // Parent
  139. if (this.parent && this.billboardMode === BABYLON.Mesh.BILLBOARDMODE_NONE) {
  140. var parentWorld = this.parent.getWorldMatrix();
  141. localWorld = localWorld.multiply(parentWorld);
  142. }
  143. this._worldMatrix = localWorld;
  144. // Bounding info
  145. if (this._boundingInfo) {
  146. this._scaleFactor = Math.max(this.scaling.x, this.scaling.y);
  147. this._scaleFactor = Math.max(this._scaleFactor, this.scaling.z);
  148. if (this.parent)
  149. this._scaleFactor = this._scaleFactor * this.parent._scaleFactor;
  150. this._boundingInfo._update(localWorld, this._scaleFactor);
  151. for (var subIndex = 0; subIndex < this.subMeshes.length; subIndex++) {
  152. var subMesh = this.subMeshes[subIndex];
  153. subMesh.updateBoundingInfo(localWorld, this._scaleFactor);
  154. }
  155. }
  156. return localWorld;
  157. };
  158. BABYLON.Mesh.prototype._createGlobalSubMesh = function () {
  159. if (!this._totalVertices || !this._indices) {
  160. return null;
  161. }
  162. this.subMeshes = [];
  163. return new BABYLON.SubMesh(0, 0, this._totalVertices, 0, this._indices.length, this);
  164. };
  165. BABYLON.Mesh.prototype.setVertices = function (vertices, uvCount) {
  166. if (this._vertexBuffer) {
  167. this._scene.getEngine()._releaseBuffer(this._vertexBuffer);
  168. }
  169. this._uvCount = uvCount;
  170. this._vertexBuffer = this._scene.getEngine().createVertexBuffer(vertices);
  171. this._vertices = vertices;
  172. this._totalVertices = vertices.length / this.getFloatVertexStrideSize();
  173. this._boundingInfo = new BABYLON.BoundingInfo(vertices, this.getFloatVertexStrideSize(), 0, vertices.length);
  174. this._createGlobalSubMesh();
  175. this._positions = null;
  176. };
  177. BABYLON.Mesh.prototype.setIndices = function (indices) {
  178. if (this._indexBuffer) {
  179. this._scene.getEngine()._releaseBuffer(this._indexBuffer);
  180. }
  181. this._indexBuffer = this._scene.getEngine().createIndexBuffer(indices);
  182. this._indices = indices;
  183. this._createGlobalSubMesh();
  184. };
  185. BABYLON.Mesh.prototype.render = function (subMesh) {
  186. if (!this._vertexBuffer || !this._indexBuffer) {
  187. return;
  188. }
  189. var engine = this._scene.getEngine();
  190. // World
  191. var world = this.getWorldMatrix();
  192. // Material
  193. var effectiveMaterial = subMesh.getMaterial();
  194. if (!effectiveMaterial || !effectiveMaterial.isReady(this)) {
  195. return;
  196. }
  197. effectiveMaterial._preBind();
  198. effectiveMaterial.bind(world, this);
  199. // Wireframe
  200. var indexToBind = this._indexBuffer;
  201. var useTriangles = true;
  202. if (engine.forceWireframe || effectiveMaterial.wireframe) {
  203. indexToBind = subMesh.getLinesIndexBuffer(this._indices, engine);
  204. useTriangles = false;
  205. }
  206. // VBOs
  207. engine.bindBuffers(this._vertexBuffer, indexToBind, this._vertexDeclaration, this._vertexStrideSize, effectiveMaterial.getEffect());
  208. // Draw order
  209. engine.draw(useTriangles, useTriangles ? subMesh.indexStart : 0, useTriangles ? subMesh.indexCount : subMesh.linesIndexCount);
  210. // Unbind
  211. effectiveMaterial.unbind();
  212. };
  213. BABYLON.Mesh.prototype.isDescendantOf = function (ancestor) {
  214. if (this.parent) {
  215. if (this.parent === ancestor) {
  216. return true;
  217. }
  218. return this.parent.isDescendantOf(ancestor);
  219. }
  220. return false;
  221. };
  222. BABYLON.Mesh.prototype.getDescendants = function () {
  223. var results = [];
  224. for (var index = 0; index < this._scene.meshes.length; index++) {
  225. var mesh = this._scene.meshes[index];
  226. if (mesh.isDescendantOf(this)) {
  227. results.push(mesh);
  228. }
  229. }
  230. return results;
  231. };
  232. BABYLON.Mesh.prototype.getEmittedParticleSystems = function () {
  233. var results = [];
  234. for (var index = 0; index < this._scene.particleSystems.length; index++) {
  235. var particleSystem = this._scene.particleSystems[index];
  236. if (particleSystem.emitter === this) {
  237. results.push(particleSystem);
  238. }
  239. }
  240. return results;
  241. };
  242. BABYLON.Mesh.prototype.getHierarchyEmittedParticleSystems = function () {
  243. var results = [];
  244. var descendants = this.getDescendants();
  245. descendants.push(this);
  246. for (var index = 0; index < this._scene.particleSystems.length; index++) {
  247. var particleSystem = this._scene.particleSystems[index];
  248. if (descendants.indexOf(particleSystem.emitter) !== -1) {
  249. results.push(particleSystem);
  250. }
  251. }
  252. return results;
  253. };
  254. BABYLON.Mesh.prototype.getChildren = function () {
  255. var results = [];
  256. for (var index = 0; index < this._scene.meshes.length; index++) {
  257. var mesh = this._scene.meshes[index];
  258. if (mesh.parent == this) {
  259. results.push(mesh);
  260. }
  261. }
  262. return results;
  263. };
  264. BABYLON.Mesh.prototype.isInFrustrum = function (frustumPlanes) {
  265. return this._boundingInfo.isInFrustrum(frustumPlanes);
  266. };
  267. BABYLON.Mesh.prototype.setMaterialByID = function (id) {
  268. var materials = this._scene.materials;
  269. for (var index = 0; index < materials.length; index++) {
  270. if (materials[index].id == id) {
  271. this.material = materials[index];
  272. return;
  273. }
  274. }
  275. // Multi
  276. var multiMaterials = this._scene.multiMaterials;
  277. for (var index = 0; index < multiMaterials.length; index++) {
  278. if (multiMaterials[index].id == id) {
  279. this.material = multiMaterials[index];
  280. return;
  281. }
  282. }
  283. };
  284. BABYLON.Mesh.prototype.getAnimatables = function () {
  285. var results = [];
  286. if (this.material) {
  287. results.push(this.material);
  288. }
  289. return results;
  290. };
  291. // Cache
  292. BABYLON.Mesh.prototype._generatePointsArray = function () {
  293. if (this._positions)
  294. return;
  295. this._positions = [];
  296. var stride = this.getFloatVertexStrideSize();
  297. for (var index = 0; index < this._vertices.length; index += stride) {
  298. this._positions.push(BABYLON.Vector3.FromArray(this._vertices, index));
  299. }
  300. };
  301. // Collisions
  302. BABYLON.Mesh.prototype._collideForSubMesh = function (subMesh, transformMatrix, collider) {
  303. this._generatePointsArray();
  304. // Transformation
  305. if (!subMesh._lastColliderWorldVertices || !subMesh._lastColliderTransformMatrix.equals(transformMatrix)) {
  306. subMesh._lastColliderTransformMatrix = transformMatrix;
  307. subMesh._lastColliderWorldVertices = [];
  308. var start = subMesh.verticesStart;
  309. var end = (subMesh.verticesStart + subMesh.verticesCount);
  310. for (var i = start; i < end; i++) {
  311. subMesh._lastColliderWorldVertices.push(BABYLON.Vector3.TransformCoordinates(this._positions[i], transformMatrix));
  312. }
  313. }
  314. // Collide
  315. collider._collide(subMesh, subMesh._lastColliderWorldVertices, this._indices, subMesh.indexStart, subMesh.indexStart + subMesh.indexCount, subMesh.verticesStart);
  316. };
  317. BABYLON.Mesh.prototype._processCollisionsForSubModels = function (collider, transformMatrix) {
  318. for (var index = 0; index < this.subMeshes.length; index++) {
  319. var subMesh = this.subMeshes[index];
  320. // Bounding test
  321. if (this.subMeshes.length > 1 && !subMesh._checkCollision(collider))
  322. continue;
  323. this._collideForSubMesh(subMesh, transformMatrix, collider);
  324. }
  325. };
  326. BABYLON.Mesh.prototype._checkCollision = function (collider) {
  327. // Bounding box test
  328. if (!this._boundingInfo._checkCollision(collider))
  329. return;
  330. // Transformation matrix
  331. var transformMatrix = this._worldMatrix.multiply(BABYLON.Matrix.Scaling(1.0 / collider.radius.x, 1.0 / collider.radius.y, 1.0 / collider.radius.z));
  332. this._processCollisionsForSubModels(collider, transformMatrix);
  333. };
  334. BABYLON.Mesh.prototype.intersectsMesh = function (mesh, precise) {
  335. if (!this._boundingInfo || !mesh._boundingInfo) {
  336. return false;
  337. }
  338. return this._boundingInfo.intersects(mesh._boundingInfo, precise);
  339. };
  340. BABYLON.Mesh.prototype.intersectsPoint = function (point) {
  341. if (!this._boundingInfo) {
  342. return false;
  343. }
  344. return this._boundingInfo.intersectsPoint(point);
  345. };
  346. // Picking
  347. BABYLON.Mesh.prototype.intersects = function (ray) {
  348. if (!this._boundingInfo || !ray.intersectsSphere(this._boundingInfo.boundingSphere)) {
  349. return { hit: false, distance: 0 };
  350. }
  351. this._generatePointsArray();
  352. var distance = Number.MAX_VALUE;
  353. for (var index = 0; index < this.subMeshes.length; index++) {
  354. var subMesh = this.subMeshes[index];
  355. // Bounding test
  356. if (this.subMeshes.length > 1 && !subMesh.canIntersects(ray))
  357. continue;
  358. var result = subMesh.intersects(ray, this._positions, this._indices);
  359. if (result.hit) {
  360. if (result.distance < distance && result.distance >= 0) {
  361. distance = result.distance;
  362. }
  363. }
  364. }
  365. if (distance >= 0)
  366. return { hit: true, distance: distance };
  367. return { hit: false, distance: 0 };
  368. };
  369. // Clone
  370. BABYLON.Mesh.prototype.clone = function (name, newParent) {
  371. var result = new BABYLON.Mesh(name, this._vertexDeclaration, this._scene);
  372. BABYLON.Tools.DeepCopy(this, result, ["name", "material"], ["_uvCount", "_vertices", "_indices", "_totalVertices"]);
  373. // Bounding info
  374. result._boundingInfo = new BABYLON.BoundingInfo(result._vertices, result.getFloatVertexStrideSize(), 0, result._vertices.length);
  375. // Material
  376. result.material = this.material;
  377. // Buffers
  378. result._vertexBuffer = this._vertexBuffer;
  379. this._vertexBuffer.references++;
  380. result._indexBuffer = this._indexBuffer;
  381. this._indexBuffer.references++;
  382. // Parent
  383. if (newParent) {
  384. result.parent = newParent;
  385. }
  386. // Children
  387. for (var index = 0; index < this._scene.meshes.length; index++) {
  388. var mesh = this._scene.meshes[index];
  389. if (mesh.parent == this) {
  390. mesh.clone(mesh.name, result);
  391. }
  392. }
  393. // Particles
  394. for (var index = 0; index < this._scene.particleSystems.length; index++) {
  395. var system = this._scene.particleSystems[index];
  396. if (system.emitter == this) {
  397. system.clone(system.name, result);
  398. }
  399. }
  400. return result;
  401. };
  402. // Dispose
  403. BABYLON.Mesh.prototype.dispose = function (doNotRecurse) {
  404. if (this._vertexBuffer) {
  405. //this._scene.getEngine()._releaseBuffer(this._vertexBuffer);
  406. this._vertexBuffer = null;
  407. }
  408. if (this._indexBuffer) {
  409. this._scene.getEngine()._releaseBuffer(this._indexBuffer);
  410. this._indexBuffer = null;
  411. }
  412. // Remove from scene
  413. var index = this._scene.meshes.indexOf(this);
  414. this._scene.meshes.splice(index, 1);
  415. if (doNotRecurse) {
  416. return;
  417. }
  418. // Particles
  419. for (var index = 0; index < this._scene.particleSystems.length; index++) {
  420. if (this._scene.particleSystems[index].emitter == this) {
  421. this._scene.particleSystems[index].dispose();
  422. index--;
  423. }
  424. }
  425. // Children
  426. var objects = this._scene.meshes.slice(0);
  427. for (var index = 0; index < objects.length; index++) {
  428. if (objects[index].parent == this) {
  429. objects[index].dispose();
  430. }
  431. }
  432. // Callback
  433. if (this.onDispose) {
  434. this.onDispose();
  435. }
  436. };
  437. // Statics
  438. BABYLON.Mesh.createBox = function (name, size, scene) {
  439. var box = new BABYLON.Mesh(name, [3, 3, 2], scene);
  440. var normals = [
  441. new BABYLON.Vector3(0, 0, 1),
  442. new BABYLON.Vector3(0, 0, -1),
  443. new BABYLON.Vector3(1, 0, 0),
  444. new BABYLON.Vector3(-1, 0, 0),
  445. new BABYLON.Vector3(0, 1, 0),
  446. new BABYLON.Vector3(0, -1, 0)
  447. ];
  448. var indices = [];
  449. var vertices = [];
  450. // Create each face in turn.
  451. for (var index = 0; index < normals.length; index++) {
  452. var normal = normals[index];
  453. // Get two vectors perpendicular to the face normal and to each other.
  454. var side1 = new BABYLON.Vector3(normal.y, normal.z, normal.x);
  455. var side2 = BABYLON.Vector3.Cross(normal, side1);
  456. // Six indices (two triangles) per face.
  457. var verticesLength = vertices.length / 8;
  458. indices.push(verticesLength);
  459. indices.push(verticesLength + 1);
  460. indices.push(verticesLength + 2);
  461. indices.push(verticesLength);
  462. indices.push(verticesLength + 2);
  463. indices.push(verticesLength + 3);
  464. // Four vertices per face.
  465. var vertex = normal.subtract(side1).subtract(side2).scale(size / 2);
  466. vertices.push(vertex.x, vertex.y, vertex.z, normal.x, normal.y, normal.z, 1.0, 1.0);
  467. vertex = normal.subtract(side1).add(side2).scale(size / 2);
  468. vertices.push(vertex.x, vertex.y, vertex.z, normal.x, normal.y, normal.z, 0.0, 1.0);
  469. vertex = normal.add(side1).add(side2).scale(size / 2);
  470. vertices.push(vertex.x, vertex.y, vertex.z, normal.x, normal.y, normal.z, 0.0, 0.0);
  471. vertex = normal.add(side1).subtract(side2).scale(size / 2);
  472. vertices.push(vertex.x, vertex.y, vertex.z, normal.x, normal.y, normal.z, 1.0, 0.0);
  473. }
  474. box.setVertices(vertices, 1);
  475. box.setIndices(indices);
  476. return box;
  477. };
  478. BABYLON.Mesh.createSphere = function (name, segments, diameter, scene) {
  479. var sphere = new BABYLON.Mesh(name, [3, 3, 2], scene);
  480. var radius = diameter / 2;
  481. var totalZRotationSteps = 2 + segments;
  482. var totalYRotationSteps = 2 * totalZRotationSteps;
  483. var indices = [];
  484. var vertices = [];
  485. for (var zRotationStep = 0; zRotationStep <= totalZRotationSteps; zRotationStep++) {
  486. var normalizedZ = zRotationStep / totalZRotationSteps;
  487. var angleZ = (normalizedZ * Math.PI);
  488. for (var yRotationStep = 0; yRotationStep <= totalYRotationSteps; yRotationStep++) {
  489. var normalizedY = yRotationStep / totalYRotationSteps;
  490. var angleY = normalizedY * Math.PI * 2;
  491. var rotationZ = BABYLON.Matrix.RotationZ(-angleZ);
  492. var rotationY = BABYLON.Matrix.RotationY(angleY);
  493. var afterRotZ = BABYLON.Vector3.TransformCoordinates(BABYLON.Vector3.Up(), rotationZ);
  494. var complete = BABYLON.Vector3.TransformCoordinates(afterRotZ, rotationY);
  495. var vertex = complete.scale(radius);
  496. var normal = BABYLON.Vector3.Normalize(vertex);
  497. vertices.push(vertex.x, vertex.y, vertex.z, normal.x, normal.y, normal.z, normalizedZ, normalizedY);
  498. }
  499. if (zRotationStep > 0) {
  500. var verticesCount = vertices.length / 8;
  501. for (var firstIndex = verticesCount - 2 * (totalYRotationSteps + 1) ; (firstIndex + totalYRotationSteps + 2) < verticesCount; firstIndex++) {
  502. indices.push((firstIndex));
  503. indices.push((firstIndex + 1));
  504. indices.push(firstIndex + totalYRotationSteps + 1);
  505. indices.push((firstIndex + totalYRotationSteps + 1));
  506. indices.push((firstIndex + 1));
  507. indices.push((firstIndex + totalYRotationSteps + 2));
  508. }
  509. }
  510. }
  511. sphere.setVertices(vertices, 1);
  512. sphere.setIndices(indices);
  513. return sphere;
  514. };
  515. // Plane
  516. BABYLON.Mesh.createPlane = function (name, size, scene) {
  517. var plane = new BABYLON.Mesh(name, [3, 3, 2], scene);
  518. var indices = [];
  519. var vertices = [];
  520. // Vertices
  521. var halfSize = size / 2.0;
  522. vertices.push(-halfSize, -halfSize, 0, 0, 1.0, 0, 0.0, 0.0);
  523. vertices.push(halfSize, -halfSize, 0, 0, 1.0, 0, 1.0, 0.0);
  524. vertices.push(halfSize, halfSize, 0, 0, 1.0, 0, 1.0, 1.0);
  525. vertices.push(-halfSize, halfSize, 0, 0, 1.0, 0, 0.0, 1.0);
  526. // Indices
  527. indices.push(0);
  528. indices.push(1);
  529. indices.push(2);
  530. indices.push(0);
  531. indices.push(2);
  532. indices.push(3);
  533. plane.setVertices(vertices, 1);
  534. plane.setIndices(indices);
  535. return plane;
  536. };
  537. })();