babylon.mesh.js 26 KB

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