babylon.mesh.js 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830
  1. var __extends = this.__extends || function (d, b) {
  2. for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
  3. function __() { this.constructor = d; }
  4. __.prototype = b.prototype;
  5. d.prototype = new __();
  6. };
  7. var BABYLON;
  8. (function (BABYLON) {
  9. var _InstancesBatch = (function () {
  10. function _InstancesBatch() {
  11. this.mustReturn = false;
  12. this.renderSelf = true;
  13. }
  14. return _InstancesBatch;
  15. })();
  16. BABYLON._InstancesBatch = _InstancesBatch;
  17. var Mesh = (function (_super) {
  18. __extends(Mesh, _super);
  19. function Mesh(name, scene) {
  20. _super.call(this, name, scene);
  21. // Members
  22. this.delayLoadState = BABYLON.Engine.DELAYLOADSTATE_NONE;
  23. this.instances = new Array();
  24. this._onBeforeRenderCallbacks = [];
  25. this._visibleInstances = {};
  26. this._renderIdForInstances = -1;
  27. this._batchCache = new _InstancesBatch();
  28. }
  29. Mesh.prototype.getTotalVertices = function () {
  30. if (!this._geometry) {
  31. return 0;
  32. }
  33. return this._geometry.getTotalVertices();
  34. };
  35. Mesh.prototype.getVerticesData = function (kind) {
  36. if (!this._geometry) {
  37. return null;
  38. }
  39. return this._geometry.getVerticesData(kind);
  40. };
  41. Mesh.prototype.getVertexBuffer = function (kind) {
  42. if (!this._geometry) {
  43. return undefined;
  44. }
  45. return this._geometry.getVertexBuffer(kind);
  46. };
  47. Mesh.prototype.isVerticesDataPresent = function (kind) {
  48. if (!this._geometry) {
  49. if (this._delayInfo) {
  50. return this._delayInfo.indexOf(kind) !== -1;
  51. }
  52. return false;
  53. }
  54. return this._geometry.isVerticesDataPresent(kind);
  55. };
  56. Mesh.prototype.getVerticesDataKinds = function () {
  57. if (!this._geometry) {
  58. var result = [];
  59. if (this._delayInfo) {
  60. for (var kind in this._delayInfo) {
  61. result.push(kind);
  62. }
  63. }
  64. return result;
  65. }
  66. return this._geometry.getVerticesDataKinds();
  67. };
  68. Mesh.prototype.getTotalIndices = function () {
  69. if (!this._geometry) {
  70. return 0;
  71. }
  72. return this._geometry.getTotalIndices();
  73. };
  74. Mesh.prototype.getIndices = function () {
  75. if (!this._geometry) {
  76. return [];
  77. }
  78. return this._geometry.getIndices();
  79. };
  80. Mesh.prototype.isReady = function () {
  81. if (this.delayLoadState === BABYLON.Engine.DELAYLOADSTATE_LOADING) {
  82. return false;
  83. }
  84. return _super.prototype.isReady.call(this);
  85. };
  86. Mesh.prototype.isDisposed = function () {
  87. return this._isDisposed;
  88. };
  89. // Methods
  90. Mesh.prototype._preActivate = function () {
  91. this._visibleInstances = null;
  92. };
  93. Mesh.prototype._registerInstanceForRenderId = function (instance, renderId) {
  94. if (!this._visibleInstances) {
  95. this._visibleInstances = {};
  96. this._visibleInstances.defaultRenderId = renderId;
  97. this._visibleInstances.selfDefaultRenderId = this._renderId;
  98. }
  99. if (!this._visibleInstances[renderId]) {
  100. this._visibleInstances[renderId] = new Array();
  101. }
  102. this._visibleInstances[renderId].push(instance);
  103. };
  104. Mesh.prototype.refreshBoundingInfo = function () {
  105. var data = this.getVerticesData(BABYLON.VertexBuffer.PositionKind);
  106. if (data) {
  107. var extend = BABYLON.Tools.ExtractMinAndMax(data, 0, this.getTotalVertices());
  108. this._boundingInfo = new BABYLON.BoundingInfo(extend.minimum, extend.maximum);
  109. }
  110. if (this.subMeshes) {
  111. for (var index = 0; index < this.subMeshes.length; index++) {
  112. this.subMeshes[index].refreshBoundingInfo();
  113. }
  114. }
  115. this._updateBoundingInfo();
  116. };
  117. Mesh.prototype._createGlobalSubMesh = function () {
  118. var totalVertices = this.getTotalVertices();
  119. if (!totalVertices || !this.getIndices()) {
  120. return null;
  121. }
  122. this.releaseSubMeshes();
  123. return new BABYLON.SubMesh(0, 0, totalVertices, 0, this.getTotalIndices(), this);
  124. };
  125. Mesh.prototype.subdivide = function (count) {
  126. if (count < 1) {
  127. return;
  128. }
  129. var totalIndices = this.getTotalIndices();
  130. var subdivisionSize = (totalIndices / count) | 0;
  131. var offset = 0;
  132. while (subdivisionSize % 3 != 0) {
  133. subdivisionSize++;
  134. }
  135. this.releaseSubMeshes();
  136. for (var index = 0; index < count; index++) {
  137. if (offset >= totalIndices) {
  138. break;
  139. }
  140. BABYLON.SubMesh.CreateFromIndices(0, offset, Math.min(subdivisionSize, totalIndices - offset), this);
  141. offset += subdivisionSize;
  142. }
  143. this.synchronizeInstances();
  144. };
  145. Mesh.prototype.setVerticesData = function (kind, data, updatable) {
  146. if (!this._geometry) {
  147. var vertexData = new BABYLON.VertexData();
  148. vertexData.set(data, kind);
  149. var scene = this.getScene();
  150. new BABYLON.Geometry(BABYLON.Geometry.RandomId(), scene.getEngine(), vertexData, updatable, this);
  151. } else {
  152. this._geometry.setVerticesData(kind, data, updatable);
  153. }
  154. };
  155. Mesh.prototype.updateVerticesData = function (kind, data, updateExtends, makeItUnique) {
  156. if (!this._geometry) {
  157. return;
  158. }
  159. if (!makeItUnique) {
  160. this._geometry.updateVerticesData(kind, data, updateExtends);
  161. } else {
  162. this.makeGeometryUnique();
  163. this.updateVerticesData(kind, data, updateExtends, false);
  164. }
  165. };
  166. Mesh.prototype.makeGeometryUnique = function () {
  167. if (!this._geometry) {
  168. return;
  169. }
  170. var geometry = this._geometry.copy(BABYLON.Geometry.RandomId());
  171. geometry.applyToMesh(this);
  172. };
  173. Mesh.prototype.setIndices = function (indices) {
  174. if (!this._geometry) {
  175. var vertexData = new BABYLON.VertexData();
  176. vertexData.indices = indices;
  177. var scene = this.getScene();
  178. new BABYLON.Geometry(BABYLON.Geometry.RandomId(), scene.getEngine(), vertexData, false, this);
  179. } else {
  180. this._geometry.setIndices(indices);
  181. }
  182. };
  183. Mesh.prototype._bind = function (subMesh, effect, wireframe) {
  184. var engine = this.getScene().getEngine();
  185. // Wireframe
  186. var indexToBind = this._geometry.getIndexBuffer();
  187. if (wireframe) {
  188. indexToBind = subMesh.getLinesIndexBuffer(this.getIndices(), engine);
  189. }
  190. // VBOs
  191. engine.bindMultiBuffers(this._geometry.getVertexBuffers(), indexToBind, effect);
  192. };
  193. Mesh.prototype._draw = function (subMesh, useTriangles, instancesCount) {
  194. if (!this._geometry || !this._geometry.getVertexBuffers() || !this._geometry.getIndexBuffer()) {
  195. return;
  196. }
  197. var engine = this.getScene().getEngine();
  198. // Draw order
  199. engine.draw(useTriangles, useTriangles ? subMesh.indexStart : 0, useTriangles ? subMesh.indexCount : subMesh.linesIndexCount, instancesCount);
  200. };
  201. Mesh.prototype.registerBeforeRender = function (func) {
  202. this._onBeforeRenderCallbacks.push(func);
  203. };
  204. Mesh.prototype.unregisterBeforeRender = function (func) {
  205. var index = this._onBeforeRenderCallbacks.indexOf(func);
  206. if (index > -1) {
  207. this._onBeforeRenderCallbacks.splice(index, 1);
  208. }
  209. };
  210. Mesh.prototype._getInstancesRenderList = function () {
  211. var scene = this.getScene();
  212. this._batchCache.mustReturn = false;
  213. this._batchCache.renderSelf = true;
  214. this._batchCache.visibleInstances = null;
  215. if (this._visibleInstances) {
  216. var currentRenderId = scene.getRenderId();
  217. this._batchCache.visibleInstances = this._visibleInstances[currentRenderId];
  218. var selfRenderId = this._renderId;
  219. if (!this._batchCache.visibleInstances && this._visibleInstances.defaultRenderId) {
  220. this._batchCache.visibleInstances = this._visibleInstances[this._visibleInstances.defaultRenderId];
  221. currentRenderId = this._visibleInstances.defaultRenderId;
  222. selfRenderId = this._visibleInstances.selfDefaultRenderId;
  223. }
  224. if (this._batchCache.visibleInstances && this._batchCache.visibleInstances.length) {
  225. if (this._renderIdForInstances === currentRenderId) {
  226. this._batchCache.mustReturn = true;
  227. return this._batchCache;
  228. }
  229. if (currentRenderId !== selfRenderId) {
  230. this._batchCache.renderSelf = false;
  231. }
  232. }
  233. this._renderIdForInstances = currentRenderId;
  234. }
  235. return this._batchCache;
  236. };
  237. Mesh.prototype._renderWithInstances = function (subMesh, wireFrame, batch, effect, engine) {
  238. if (!this._worldMatricesInstancesBuffer) {
  239. var matricesCount = this.instances.length + 1;
  240. this._worldMatricesInstancesBuffer = engine.createInstancesBuffer(matricesCount * 16 * 4);
  241. this._worldMatricesInstancesArray = new Float32Array(16 * matricesCount);
  242. }
  243. var offset = 0;
  244. var instancesCount = 0;
  245. var world = this.getWorldMatrix();
  246. if (batch.renderSelf) {
  247. world.copyToArray(this._worldMatricesInstancesArray, offset);
  248. offset += 16;
  249. instancesCount++;
  250. }
  251. for (var instanceIndex = 0; instanceIndex < batch.visibleInstances.length; instanceIndex++) {
  252. var instance = batch.visibleInstances[instanceIndex];
  253. instance.getWorldMatrix().copyToArray(this._worldMatricesInstancesArray, offset);
  254. offset += 16;
  255. instancesCount++;
  256. }
  257. var offsetLocation = effect.getAttributeLocationByName("world");
  258. engine.updateAndBindInstancesBuffer(this._worldMatricesInstancesBuffer, this._worldMatricesInstancesArray, offsetLocation);
  259. this._draw(subMesh, !wireFrame, instancesCount);
  260. engine.unBindInstancesBuffer(this._worldMatricesInstancesBuffer, offsetLocation);
  261. };
  262. Mesh.prototype.render = function (subMesh) {
  263. var scene = this.getScene();
  264. // Managing instances
  265. var batch = this._getInstancesRenderList();
  266. if (batch.mustReturn) {
  267. return;
  268. }
  269. // Checking geometry state
  270. if (!this._geometry || !this._geometry.getVertexBuffers() || !this._geometry.getIndexBuffer()) {
  271. return;
  272. }
  273. for (var callbackIndex = 0; callbackIndex < this._onBeforeRenderCallbacks.length; callbackIndex++) {
  274. this._onBeforeRenderCallbacks[callbackIndex]();
  275. }
  276. var engine = scene.getEngine();
  277. var hardwareInstancedRendering = (engine.getCaps().instancedArrays !== null) && (batch.visibleInstances !== null);
  278. // Material
  279. var effectiveMaterial = subMesh.getMaterial();
  280. if (!effectiveMaterial || !effectiveMaterial.isReady(this, hardwareInstancedRendering)) {
  281. return;
  282. }
  283. effectiveMaterial._preBind();
  284. var effect = effectiveMaterial.getEffect();
  285. // Bind
  286. var wireFrame = engine.forceWireframe || effectiveMaterial.wireframe;
  287. this._bind(subMesh, effect, wireFrame);
  288. var world = this.getWorldMatrix();
  289. effectiveMaterial.bind(world, this);
  290. // Instances rendering
  291. if (hardwareInstancedRendering) {
  292. this._renderWithInstances(subMesh, wireFrame, batch, effect, engine);
  293. } else {
  294. if (batch.renderSelf) {
  295. // Draw
  296. this._draw(subMesh, !wireFrame);
  297. }
  298. if (batch.visibleInstances) {
  299. for (var instanceIndex = 0; instanceIndex < batch.visibleInstances.length; instanceIndex++) {
  300. var instance = batch.visibleInstances[instanceIndex];
  301. // World
  302. world = instance.getWorldMatrix();
  303. effectiveMaterial.bindOnlyWorldMatrix(world);
  304. // Draw
  305. this._draw(subMesh, !wireFrame);
  306. }
  307. }
  308. }
  309. // Unbind
  310. effectiveMaterial.unbind();
  311. };
  312. Mesh.prototype.getEmittedParticleSystems = function () {
  313. var results = new Array();
  314. for (var index = 0; index < this.getScene().particleSystems.length; index++) {
  315. var particleSystem = this.getScene().particleSystems[index];
  316. if (particleSystem.emitter === this) {
  317. results.push(particleSystem);
  318. }
  319. }
  320. return results;
  321. };
  322. Mesh.prototype.getHierarchyEmittedParticleSystems = function () {
  323. var results = new Array();
  324. var descendants = this.getDescendants();
  325. descendants.push(this);
  326. for (var index = 0; index < this.getScene().particleSystems.length; index++) {
  327. var particleSystem = this.getScene().particleSystems[index];
  328. if (descendants.indexOf(particleSystem.emitter) !== -1) {
  329. results.push(particleSystem);
  330. }
  331. }
  332. return results;
  333. };
  334. Mesh.prototype.getChildren = function () {
  335. var results = [];
  336. for (var index = 0; index < this.getScene().meshes.length; index++) {
  337. var mesh = this.getScene().meshes[index];
  338. if (mesh.parent == this) {
  339. results.push(mesh);
  340. }
  341. }
  342. return results;
  343. };
  344. Mesh.prototype._checkDelayState = function () {
  345. var _this = this;
  346. var that = this;
  347. var scene = this.getScene();
  348. if (this._geometry) {
  349. this._geometry.load(scene);
  350. } else if (that.delayLoadState === BABYLON.Engine.DELAYLOADSTATE_NOTLOADED) {
  351. that.delayLoadState = BABYLON.Engine.DELAYLOADSTATE_LOADING;
  352. scene._addPendingData(that);
  353. BABYLON.Tools.LoadFile(this.delayLoadingFile, function (data) {
  354. _this._delayLoadingFunction(JSON.parse(data), _this);
  355. _this.delayLoadState = BABYLON.Engine.DELAYLOADSTATE_LOADED;
  356. scene._removePendingData(_this);
  357. }, function () {
  358. }, scene.database);
  359. }
  360. };
  361. Mesh.prototype.isInFrustum = function (frustumPlanes) {
  362. if (this.delayLoadState === BABYLON.Engine.DELAYLOADSTATE_LOADING) {
  363. return false;
  364. }
  365. if (!_super.prototype.isInFrustum.call(this, frustumPlanes)) {
  366. return false;
  367. }
  368. this._checkDelayState();
  369. return true;
  370. };
  371. Mesh.prototype.setMaterialByID = function (id) {
  372. var materials = this.getScene().materials;
  373. for (var index = 0; index < materials.length; index++) {
  374. if (materials[index].id == id) {
  375. this.material = materials[index];
  376. return;
  377. }
  378. }
  379. // Multi
  380. var multiMaterials = this.getScene().multiMaterials;
  381. for (index = 0; index < multiMaterials.length; index++) {
  382. if (multiMaterials[index].id == id) {
  383. this.material = multiMaterials[index];
  384. return;
  385. }
  386. }
  387. };
  388. Mesh.prototype.getAnimatables = function () {
  389. var results = [];
  390. if (this.material) {
  391. results.push(this.material);
  392. }
  393. return results;
  394. };
  395. // Geometry
  396. Mesh.prototype.bakeTransformIntoVertices = function (transform) {
  397. // Position
  398. if (!this.isVerticesDataPresent(BABYLON.VertexBuffer.PositionKind)) {
  399. return;
  400. }
  401. this._resetPointsArrayCache();
  402. var data = this.getVerticesData(BABYLON.VertexBuffer.PositionKind);
  403. var temp = [];
  404. for (var index = 0; index < data.length; index += 3) {
  405. BABYLON.Vector3.TransformCoordinates(BABYLON.Vector3.FromArray(data, index), transform).toArray(temp, index);
  406. }
  407. this.setVerticesData(BABYLON.VertexBuffer.PositionKind, temp, this.getVertexBuffer(BABYLON.VertexBuffer.PositionKind).isUpdatable());
  408. // Normals
  409. if (!this.isVerticesDataPresent(BABYLON.VertexBuffer.NormalKind)) {
  410. return;
  411. }
  412. data = this.getVerticesData(BABYLON.VertexBuffer.NormalKind);
  413. for (index = 0; index < data.length; index += 3) {
  414. BABYLON.Vector3.TransformNormal(BABYLON.Vector3.FromArray(data, index), transform).toArray(temp, index);
  415. }
  416. this.setVerticesData(BABYLON.VertexBuffer.NormalKind, temp, this.getVertexBuffer(BABYLON.VertexBuffer.NormalKind).isUpdatable());
  417. };
  418. // Cache
  419. Mesh.prototype._resetPointsArrayCache = function () {
  420. this._positions = null;
  421. };
  422. Mesh.prototype._generatePointsArray = function () {
  423. if (this._positions)
  424. return true;
  425. this._positions = [];
  426. var data = this.getVerticesData(BABYLON.VertexBuffer.PositionKind);
  427. if (!data) {
  428. return false;
  429. }
  430. for (var index = 0; index < data.length; index += 3) {
  431. this._positions.push(BABYLON.Vector3.FromArray(data, index));
  432. }
  433. return true;
  434. };
  435. // Clone
  436. Mesh.prototype.clone = function (name, newParent, doNotCloneChildren) {
  437. var result = new BABYLON.Mesh(name, this.getScene());
  438. // Geometry
  439. this._geometry.applyToMesh(result);
  440. // Deep copy
  441. BABYLON.Tools.DeepCopy(this, result, ["name", "material", "skeleton"], []);
  442. // Material
  443. result.material = this.material;
  444. // Parent
  445. if (newParent) {
  446. result.parent = newParent;
  447. }
  448. if (!doNotCloneChildren) {
  449. for (var index = 0; index < this.getScene().meshes.length; index++) {
  450. var mesh = this.getScene().meshes[index];
  451. if (mesh.parent == this) {
  452. mesh.clone(mesh.name, result);
  453. }
  454. }
  455. }
  456. for (index = 0; index < this.getScene().particleSystems.length; index++) {
  457. var system = this.getScene().particleSystems[index];
  458. if (system.emitter == this) {
  459. system.clone(system.name, result);
  460. }
  461. }
  462. result.computeWorldMatrix(true);
  463. return result;
  464. };
  465. // Dispose
  466. Mesh.prototype.dispose = function (doNotRecurse) {
  467. if (this._geometry) {
  468. this._geometry.releaseForMesh(this);
  469. }
  470. while (this.instances.length) {
  471. this.instances[0].dispose();
  472. }
  473. _super.prototype.dispose.call(this, doNotRecurse);
  474. };
  475. // Geometric tools
  476. Mesh.prototype.convertToFlatShadedMesh = function () {
  477. /// <summary>Update normals and vertices to get a flat shading rendering.</summary>
  478. /// <summary>Warning: This may imply adding vertices to the mesh in order to get exactly 3 vertices per face</summary>
  479. var kinds = this.getVerticesDataKinds();
  480. var vbs = [];
  481. var data = [];
  482. var newdata = [];
  483. var updatableNormals = false;
  484. for (var kindIndex = 0; kindIndex < kinds.length; kindIndex++) {
  485. var kind = kinds[kindIndex];
  486. var vertexBuffer = this.getVertexBuffer(kind);
  487. if (kind === BABYLON.VertexBuffer.NormalKind) {
  488. updatableNormals = vertexBuffer.isUpdatable();
  489. kinds.splice(kindIndex, 1);
  490. kindIndex--;
  491. continue;
  492. }
  493. vbs[kind] = vertexBuffer;
  494. data[kind] = vbs[kind].getData();
  495. newdata[kind] = [];
  496. }
  497. // Save previous submeshes
  498. var previousSubmeshes = this.subMeshes.slice(0);
  499. var indices = this.getIndices();
  500. var totalIndices = this.getTotalIndices();
  501. for (index = 0; index < totalIndices; index++) {
  502. var vertexIndex = indices[index];
  503. for (kindIndex = 0; kindIndex < kinds.length; kindIndex++) {
  504. kind = kinds[kindIndex];
  505. var stride = vbs[kind].getStrideSize();
  506. for (var offset = 0; offset < stride; offset++) {
  507. newdata[kind].push(data[kind][vertexIndex * stride + offset]);
  508. }
  509. }
  510. }
  511. // Updating faces & normal
  512. var normals = [];
  513. var positions = newdata[BABYLON.VertexBuffer.PositionKind];
  514. for (var index = 0; index < totalIndices; index += 3) {
  515. indices[index] = index;
  516. indices[index + 1] = index + 1;
  517. indices[index + 2] = index + 2;
  518. var p1 = BABYLON.Vector3.FromArray(positions, index * 3);
  519. var p2 = BABYLON.Vector3.FromArray(positions, (index + 1) * 3);
  520. var p3 = BABYLON.Vector3.FromArray(positions, (index + 2) * 3);
  521. var p1p2 = p1.subtract(p2);
  522. var p3p2 = p3.subtract(p2);
  523. var normal = BABYLON.Vector3.Normalize(BABYLON.Vector3.Cross(p1p2, p3p2));
  524. for (var localIndex = 0; localIndex < 3; localIndex++) {
  525. normals.push(normal.x);
  526. normals.push(normal.y);
  527. normals.push(normal.z);
  528. }
  529. }
  530. this.setIndices(indices);
  531. this.setVerticesData(BABYLON.VertexBuffer.NormalKind, normals, updatableNormals);
  532. for (kindIndex = 0; kindIndex < kinds.length; kindIndex++) {
  533. kind = kinds[kindIndex];
  534. this.setVerticesData(kind, newdata[kind], vbs[kind].isUpdatable());
  535. }
  536. // Updating submeshes
  537. this.releaseSubMeshes();
  538. for (var submeshIndex = 0; submeshIndex < previousSubmeshes.length; submeshIndex++) {
  539. var previousOne = previousSubmeshes[submeshIndex];
  540. var subMesh = new BABYLON.SubMesh(previousOne.materialIndex, previousOne.indexStart, previousOne.indexCount, previousOne.indexStart, previousOne.indexCount, this);
  541. }
  542. this.synchronizeInstances();
  543. };
  544. // Instances
  545. Mesh.prototype.createInstance = function (name) {
  546. return new BABYLON.InstancedMesh(name, this);
  547. };
  548. Mesh.prototype.synchronizeInstances = function () {
  549. for (var instanceIndex = 0; instanceIndex < this.instances.length; instanceIndex++) {
  550. var instance = this.instances[instanceIndex];
  551. instance._syncSubMeshes();
  552. }
  553. };
  554. // Statics
  555. Mesh.CreateBox = function (name, size, scene, updatable) {
  556. var box = new BABYLON.Mesh(name, scene);
  557. var vertexData = BABYLON.VertexData.CreateBox(size);
  558. vertexData.applyToMesh(box, updatable);
  559. return box;
  560. };
  561. Mesh.CreateSphere = function (name, segments, diameter, scene, updatable) {
  562. var sphere = new BABYLON.Mesh(name, scene);
  563. var vertexData = BABYLON.VertexData.CreateSphere(segments, diameter);
  564. vertexData.applyToMesh(sphere, updatable);
  565. return sphere;
  566. };
  567. // Cylinder and cone (Code inspired by SharpDX.org)
  568. Mesh.CreateCylinder = function (name, height, diameterTop, diameterBottom, tessellation, scene, updatable) {
  569. var cylinder = new BABYLON.Mesh(name, scene);
  570. var vertexData = BABYLON.VertexData.CreateCylinder(height, diameterTop, diameterBottom, tessellation);
  571. vertexData.applyToMesh(cylinder, updatable);
  572. return cylinder;
  573. };
  574. // Torus (Code from SharpDX.org)
  575. Mesh.CreateTorus = function (name, diameter, thickness, tessellation, scene, updatable) {
  576. var torus = new BABYLON.Mesh(name, scene);
  577. var vertexData = BABYLON.VertexData.CreateTorus(diameter, thickness, tessellation);
  578. vertexData.applyToMesh(torus, updatable);
  579. return torus;
  580. };
  581. Mesh.CreateTorusKnot = function (name, radius, tube, radialSegments, tubularSegments, p, q, scene, updatable) {
  582. var torusKnot = new BABYLON.Mesh(name, scene);
  583. var vertexData = BABYLON.VertexData.CreateTorusKnot(radius, tube, radialSegments, tubularSegments, p, q);
  584. vertexData.applyToMesh(torusKnot, updatable);
  585. return torusKnot;
  586. };
  587. // Plane & ground
  588. Mesh.CreatePlane = function (name, size, scene, updatable) {
  589. var plane = new BABYLON.Mesh(name, scene);
  590. var vertexData = BABYLON.VertexData.CreatePlane(size);
  591. vertexData.applyToMesh(plane, updatable);
  592. return plane;
  593. };
  594. Mesh.CreateGround = function (name, width, height, subdivisions, scene, updatable) {
  595. var ground = new BABYLON.GroundMesh(name, scene);
  596. ground._setReady(false);
  597. ground._subdivisions = subdivisions;
  598. var vertexData = BABYLON.VertexData.CreateGround(width, height, subdivisions);
  599. vertexData.applyToMesh(ground, updatable);
  600. ground._setReady(true);
  601. return ground;
  602. };
  603. Mesh.CreateGroundFromHeightMap = function (name, url, width, height, subdivisions, minHeight, maxHeight, scene, updatable) {
  604. var ground = new BABYLON.GroundMesh(name, scene);
  605. ground._subdivisions = subdivisions;
  606. ground._setReady(false);
  607. var onload = function (img) {
  608. // Getting height map data
  609. var canvas = document.createElement("canvas");
  610. var context = canvas.getContext("2d");
  611. var heightMapWidth = img.width;
  612. var heightMapHeight = img.height;
  613. canvas.width = heightMapWidth;
  614. canvas.height = heightMapHeight;
  615. context.drawImage(img, 0, 0);
  616. // Create VertexData from map data
  617. var buffer = context.getImageData(0, 0, heightMapWidth, heightMapHeight).data;
  618. var vertexData = BABYLON.VertexData.CreateGroundFromHeightMap(width, height, subdivisions, minHeight, maxHeight, buffer, heightMapWidth, heightMapHeight);
  619. vertexData.applyToMesh(ground, updatable);
  620. ground._setReady(true);
  621. };
  622. BABYLON.Tools.LoadImage(url, onload, function () {
  623. }, scene.database);
  624. return ground;
  625. };
  626. // Tools
  627. Mesh.MinMax = function (meshes) {
  628. var minVector = null;
  629. var maxVector = null;
  630. for (var i in meshes) {
  631. var mesh = meshes[i];
  632. var boundingBox = mesh.getBoundingInfo().boundingBox;
  633. if (!minVector) {
  634. minVector = boundingBox.minimumWorld;
  635. maxVector = boundingBox.maximumWorld;
  636. continue;
  637. }
  638. minVector.MinimizeInPlace(boundingBox.minimumWorld);
  639. maxVector.MaximizeInPlace(boundingBox.maximumWorld);
  640. }
  641. return {
  642. min: minVector,
  643. max: maxVector
  644. };
  645. };
  646. Mesh.Center = function (meshesOrMinMaxVector) {
  647. var minMaxVector = meshesOrMinMaxVector.min !== undefined ? meshesOrMinMaxVector : BABYLON.Mesh.MinMax(meshesOrMinMaxVector);
  648. return BABYLON.Vector3.Center(minMaxVector.min, minMaxVector.max);
  649. };
  650. return Mesh;
  651. })(BABYLON.AbstractMesh);
  652. BABYLON.Mesh = Mesh;
  653. })(BABYLON || (BABYLON = {}));
  654. //# sourceMappingURL=babylon.mesh.js.map