babylon.mesh.js 26 KB

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