babylon.mesh.js 32 KB

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