babylon.mesh.js 26 KB

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