babylon.mesh.js 33 KB

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