babylon.mesh.js 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891
  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. if (visibleInstances) {
  268. for (var instanceIndex = 0; instanceIndex < visibleInstances.length; instanceIndex++) {
  269. var instance = visibleInstances[instanceIndex];
  270. instance.getWorldMatrix().copyToArray(this._worldMatricesInstancesArray, offset);
  271. offset += 16;
  272. instancesCount++;
  273. }
  274. }
  275. var offsetLocation0 = effect.getAttributeLocationByName("world0");
  276. var offsetLocation1 = effect.getAttributeLocationByName("world1");
  277. var offsetLocation2 = effect.getAttributeLocationByName("world2");
  278. var offsetLocation3 = effect.getAttributeLocationByName("world3");
  279. var offsetLocations = [offsetLocation0, offsetLocation1, offsetLocation2, offsetLocation3];
  280. engine.updateAndBindInstancesBuffer(this._worldMatricesInstancesBuffer, this._worldMatricesInstancesArray, offsetLocations);
  281. this._draw(subMesh, !wireFrame, instancesCount);
  282. engine.unBindInstancesBuffer(this._worldMatricesInstancesBuffer, offsetLocations);
  283. };
  284. Mesh.prototype.render = function (subMesh) {
  285. var scene = this.getScene();
  286. // Managing instances
  287. var batch = this._getInstancesRenderList(subMesh._id);
  288. if (batch.mustReturn) {
  289. return;
  290. }
  291. // Checking geometry state
  292. if (!this._geometry || !this._geometry.getVertexBuffers() || !this._geometry.getIndexBuffer()) {
  293. return;
  294. }
  295. for (var callbackIndex = 0; callbackIndex < this._onBeforeRenderCallbacks.length; callbackIndex++) {
  296. this._onBeforeRenderCallbacks[callbackIndex]();
  297. }
  298. var engine = scene.getEngine();
  299. var hardwareInstancedRendering = (engine.getCaps().instancedArrays !== null) && (batch.visibleInstances[subMesh._id] !== null);
  300. // Material
  301. var effectiveMaterial = subMesh.getMaterial();
  302. if (!effectiveMaterial || !effectiveMaterial.isReady(this, hardwareInstancedRendering)) {
  303. return;
  304. }
  305. effectiveMaterial._preBind();
  306. var effect = effectiveMaterial.getEffect();
  307. // Bind
  308. var wireFrame = engine.forceWireframe || effectiveMaterial.wireframe;
  309. this._bind(subMesh, effect, wireFrame);
  310. var world = this.getWorldMatrix();
  311. effectiveMaterial.bind(world, this);
  312. // Instances rendering
  313. if (hardwareInstancedRendering) {
  314. this._renderWithInstances(subMesh, wireFrame, batch, effect, engine);
  315. } else {
  316. if (batch.renderSelf[subMesh._id]) {
  317. // Draw
  318. this._draw(subMesh, !wireFrame);
  319. }
  320. if (batch.visibleInstances[subMesh._id]) {
  321. for (var instanceIndex = 0; instanceIndex < batch.visibleInstances[subMesh._id].length; instanceIndex++) {
  322. var instance = batch.visibleInstances[subMesh._id][instanceIndex];
  323. // World
  324. world = instance.getWorldMatrix();
  325. effectiveMaterial.bindOnlyWorldMatrix(world);
  326. // Draw
  327. this._draw(subMesh, !wireFrame);
  328. }
  329. }
  330. }
  331. // Unbind
  332. effectiveMaterial.unbind();
  333. };
  334. Mesh.prototype.getEmittedParticleSystems = function () {
  335. var results = new Array();
  336. for (var index = 0; index < this.getScene().particleSystems.length; index++) {
  337. var particleSystem = this.getScene().particleSystems[index];
  338. if (particleSystem.emitter === this) {
  339. results.push(particleSystem);
  340. }
  341. }
  342. return results;
  343. };
  344. Mesh.prototype.getHierarchyEmittedParticleSystems = function () {
  345. var results = new Array();
  346. var descendants = this.getDescendants();
  347. descendants.push(this);
  348. for (var index = 0; index < this.getScene().particleSystems.length; index++) {
  349. var particleSystem = this.getScene().particleSystems[index];
  350. if (descendants.indexOf(particleSystem.emitter) !== -1) {
  351. results.push(particleSystem);
  352. }
  353. }
  354. return results;
  355. };
  356. Mesh.prototype.getChildren = function () {
  357. var results = [];
  358. for (var index = 0; index < this.getScene().meshes.length; index++) {
  359. var mesh = this.getScene().meshes[index];
  360. if (mesh.parent == this) {
  361. results.push(mesh);
  362. }
  363. }
  364. return results;
  365. };
  366. Mesh.prototype._checkDelayState = function () {
  367. var _this = this;
  368. var that = this;
  369. var scene = this.getScene();
  370. if (this._geometry) {
  371. this._geometry.load(scene);
  372. } else if (that.delayLoadState === BABYLON.Engine.DELAYLOADSTATE_NOTLOADED) {
  373. that.delayLoadState = BABYLON.Engine.DELAYLOADSTATE_LOADING;
  374. scene._addPendingData(that);
  375. BABYLON.Tools.LoadFile(this.delayLoadingFile, function (data) {
  376. _this._delayLoadingFunction(JSON.parse(data), _this);
  377. _this.delayLoadState = BABYLON.Engine.DELAYLOADSTATE_LOADED;
  378. scene._removePendingData(_this);
  379. }, function () {
  380. }, scene.database);
  381. }
  382. };
  383. Mesh.prototype.isInFrustum = function (frustumPlanes) {
  384. if (this.delayLoadState === BABYLON.Engine.DELAYLOADSTATE_LOADING) {
  385. return false;
  386. }
  387. if (!_super.prototype.isInFrustum.call(this, frustumPlanes)) {
  388. return false;
  389. }
  390. this._checkDelayState();
  391. return true;
  392. };
  393. Mesh.prototype.setMaterialByID = function (id) {
  394. var materials = this.getScene().materials;
  395. for (var index = 0; index < materials.length; index++) {
  396. if (materials[index].id == id) {
  397. this.material = materials[index];
  398. return;
  399. }
  400. }
  401. // Multi
  402. var multiMaterials = this.getScene().multiMaterials;
  403. for (index = 0; index < multiMaterials.length; index++) {
  404. if (multiMaterials[index].id == id) {
  405. this.material = multiMaterials[index];
  406. return;
  407. }
  408. }
  409. };
  410. Mesh.prototype.getAnimatables = function () {
  411. var results = [];
  412. if (this.material) {
  413. results.push(this.material);
  414. }
  415. return results;
  416. };
  417. // Geometry
  418. Mesh.prototype.bakeTransformIntoVertices = function (transform) {
  419. // Position
  420. if (!this.isVerticesDataPresent(BABYLON.VertexBuffer.PositionKind)) {
  421. return;
  422. }
  423. this._resetPointsArrayCache();
  424. var data = this.getVerticesData(BABYLON.VertexBuffer.PositionKind);
  425. var temp = [];
  426. for (var index = 0; index < data.length; index += 3) {
  427. BABYLON.Vector3.TransformCoordinates(BABYLON.Vector3.FromArray(data, index), transform).toArray(temp, index);
  428. }
  429. this.setVerticesData(BABYLON.VertexBuffer.PositionKind, temp, this.getVertexBuffer(BABYLON.VertexBuffer.PositionKind).isUpdatable());
  430. // Normals
  431. if (!this.isVerticesDataPresent(BABYLON.VertexBuffer.NormalKind)) {
  432. return;
  433. }
  434. data = this.getVerticesData(BABYLON.VertexBuffer.NormalKind);
  435. for (index = 0; index < data.length; index += 3) {
  436. BABYLON.Vector3.TransformNormal(BABYLON.Vector3.FromArray(data, index), transform).toArray(temp, index);
  437. }
  438. this.setVerticesData(BABYLON.VertexBuffer.NormalKind, temp, this.getVertexBuffer(BABYLON.VertexBuffer.NormalKind).isUpdatable());
  439. };
  440. // Cache
  441. Mesh.prototype._resetPointsArrayCache = function () {
  442. this._positions = null;
  443. };
  444. Mesh.prototype._generatePointsArray = function () {
  445. if (this._positions)
  446. return true;
  447. this._positions = [];
  448. var data = this.getVerticesData(BABYLON.VertexBuffer.PositionKind);
  449. if (!data) {
  450. return false;
  451. }
  452. for (var index = 0; index < data.length; index += 3) {
  453. this._positions.push(BABYLON.Vector3.FromArray(data, index));
  454. }
  455. return true;
  456. };
  457. // Clone
  458. Mesh.prototype.clone = function (name, newParent, doNotCloneChildren) {
  459. var result = new BABYLON.Mesh(name, this.getScene());
  460. // Geometry
  461. this._geometry.applyToMesh(result);
  462. // Deep copy
  463. BABYLON.Tools.DeepCopy(this, result, ["name", "material", "skeleton"], []);
  464. // Material
  465. result.material = this.material;
  466. // Parent
  467. if (newParent) {
  468. result.parent = newParent;
  469. }
  470. if (!doNotCloneChildren) {
  471. for (var index = 0; index < this.getScene().meshes.length; index++) {
  472. var mesh = this.getScene().meshes[index];
  473. if (mesh.parent == this) {
  474. mesh.clone(mesh.name, result);
  475. }
  476. }
  477. }
  478. for (index = 0; index < this.getScene().particleSystems.length; index++) {
  479. var system = this.getScene().particleSystems[index];
  480. if (system.emitter == this) {
  481. system.clone(system.name, result);
  482. }
  483. }
  484. result.computeWorldMatrix(true);
  485. return result;
  486. };
  487. // Dispose
  488. Mesh.prototype.dispose = function (doNotRecurse) {
  489. if (this._geometry) {
  490. this._geometry.releaseForMesh(this, true);
  491. }
  492. // Instances
  493. if (this._worldMatricesInstancesBuffer) {
  494. this.getEngine().deleteInstancesBuffer(this._worldMatricesInstancesBuffer);
  495. this._worldMatricesInstancesBuffer = null;
  496. }
  497. while (this.instances.length) {
  498. this.instances[0].dispose();
  499. }
  500. _super.prototype.dispose.call(this, doNotRecurse);
  501. };
  502. // Geometric tools
  503. Mesh.prototype.convertToFlatShadedMesh = function () {
  504. /// <summary>Update normals and vertices to get a flat shading rendering.</summary>
  505. /// <summary>Warning: This may imply adding vertices to the mesh in order to get exactly 3 vertices per face</summary>
  506. var kinds = this.getVerticesDataKinds();
  507. var vbs = [];
  508. var data = [];
  509. var newdata = [];
  510. var updatableNormals = false;
  511. for (var kindIndex = 0; kindIndex < kinds.length; kindIndex++) {
  512. var kind = kinds[kindIndex];
  513. var vertexBuffer = this.getVertexBuffer(kind);
  514. if (kind === BABYLON.VertexBuffer.NormalKind) {
  515. updatableNormals = vertexBuffer.isUpdatable();
  516. kinds.splice(kindIndex, 1);
  517. kindIndex--;
  518. continue;
  519. }
  520. vbs[kind] = vertexBuffer;
  521. data[kind] = vbs[kind].getData();
  522. newdata[kind] = [];
  523. }
  524. // Save previous submeshes
  525. var previousSubmeshes = this.subMeshes.slice(0);
  526. var indices = this.getIndices();
  527. var totalIndices = this.getTotalIndices();
  528. for (index = 0; index < totalIndices; index++) {
  529. var vertexIndex = indices[index];
  530. for (kindIndex = 0; kindIndex < kinds.length; kindIndex++) {
  531. kind = kinds[kindIndex];
  532. var stride = vbs[kind].getStrideSize();
  533. for (var offset = 0; offset < stride; offset++) {
  534. newdata[kind].push(data[kind][vertexIndex * stride + offset]);
  535. }
  536. }
  537. }
  538. // Updating faces & normal
  539. var normals = [];
  540. var positions = newdata[BABYLON.VertexBuffer.PositionKind];
  541. for (var index = 0; index < totalIndices; index += 3) {
  542. indices[index] = index;
  543. indices[index + 1] = index + 1;
  544. indices[index + 2] = index + 2;
  545. var p1 = BABYLON.Vector3.FromArray(positions, index * 3);
  546. var p2 = BABYLON.Vector3.FromArray(positions, (index + 1) * 3);
  547. var p3 = BABYLON.Vector3.FromArray(positions, (index + 2) * 3);
  548. var p1p2 = p1.subtract(p2);
  549. var p3p2 = p3.subtract(p2);
  550. var normal = BABYLON.Vector3.Normalize(BABYLON.Vector3.Cross(p1p2, p3p2));
  551. for (var localIndex = 0; localIndex < 3; localIndex++) {
  552. normals.push(normal.x);
  553. normals.push(normal.y);
  554. normals.push(normal.z);
  555. }
  556. }
  557. this.setIndices(indices);
  558. this.setVerticesData(BABYLON.VertexBuffer.NormalKind, normals, updatableNormals);
  559. for (kindIndex = 0; kindIndex < kinds.length; kindIndex++) {
  560. kind = kinds[kindIndex];
  561. this.setVerticesData(kind, newdata[kind], vbs[kind].isUpdatable());
  562. }
  563. // Updating submeshes
  564. this.releaseSubMeshes();
  565. for (var submeshIndex = 0; submeshIndex < previousSubmeshes.length; submeshIndex++) {
  566. var previousOne = previousSubmeshes[submeshIndex];
  567. var subMesh = new BABYLON.SubMesh(previousOne.materialIndex, previousOne.indexStart, previousOne.indexCount, previousOne.indexStart, previousOne.indexCount, this);
  568. }
  569. this.synchronizeInstances();
  570. };
  571. // Instances
  572. Mesh.prototype.createInstance = function (name) {
  573. return new BABYLON.InstancedMesh(name, this);
  574. };
  575. Mesh.prototype.synchronizeInstances = function () {
  576. for (var instanceIndex = 0; instanceIndex < this.instances.length; instanceIndex++) {
  577. var instance = this.instances[instanceIndex];
  578. instance._syncSubMeshes();
  579. }
  580. };
  581. // Statics
  582. Mesh.CreateBox = function (name, size, scene, updatable) {
  583. var box = new BABYLON.Mesh(name, scene);
  584. var vertexData = BABYLON.VertexData.CreateBox(size);
  585. vertexData.applyToMesh(box, updatable);
  586. return box;
  587. };
  588. Mesh.CreateSphere = function (name, segments, diameter, scene, updatable) {
  589. var sphere = new BABYLON.Mesh(name, scene);
  590. var vertexData = BABYLON.VertexData.CreateSphere(segments, diameter);
  591. vertexData.applyToMesh(sphere, updatable);
  592. return sphere;
  593. };
  594. // Cylinder and cone (Code inspired by SharpDX.org)
  595. Mesh.CreateCylinder = function (name, height, diameterTop, diameterBottom, tessellation, subdivisions, scene, updatable) {
  596. var cylinder = new BABYLON.Mesh(name, scene);
  597. var vertexData = BABYLON.VertexData.CreateCylinder(height, diameterTop, diameterBottom, tessellation, subdivisions);
  598. vertexData.applyToMesh(cylinder, updatable);
  599. return cylinder;
  600. };
  601. // Torus (Code from SharpDX.org)
  602. Mesh.CreateTorus = function (name, diameter, thickness, tessellation, scene, updatable) {
  603. var torus = new BABYLON.Mesh(name, scene);
  604. var vertexData = BABYLON.VertexData.CreateTorus(diameter, thickness, tessellation);
  605. vertexData.applyToMesh(torus, updatable);
  606. return torus;
  607. };
  608. Mesh.CreateTorusKnot = function (name, radius, tube, radialSegments, tubularSegments, p, q, scene, updatable) {
  609. var torusKnot = new BABYLON.Mesh(name, scene);
  610. var vertexData = BABYLON.VertexData.CreateTorusKnot(radius, tube, radialSegments, tubularSegments, p, q);
  611. vertexData.applyToMesh(torusKnot, updatable);
  612. return torusKnot;
  613. };
  614. // Lines
  615. Mesh.CreateLines = function (name, points, scene, updatable) {
  616. var lines = new BABYLON.LinesMesh(name, scene, updatable);
  617. var vertexData = BABYLON.VertexData.CreateLines(points);
  618. vertexData.applyToMesh(lines, updatable);
  619. return lines;
  620. };
  621. // Plane & ground
  622. Mesh.CreatePlane = function (name, size, scene, updatable) {
  623. var plane = new BABYLON.Mesh(name, scene);
  624. var vertexData = BABYLON.VertexData.CreatePlane(size);
  625. vertexData.applyToMesh(plane, updatable);
  626. return plane;
  627. };
  628. Mesh.CreateGround = function (name, width, height, subdivisions, scene, updatable) {
  629. var ground = new BABYLON.GroundMesh(name, scene);
  630. ground._setReady(false);
  631. ground._subdivisions = subdivisions;
  632. var vertexData = BABYLON.VertexData.CreateGround(width, height, subdivisions);
  633. vertexData.applyToMesh(ground, updatable);
  634. ground._setReady(true);
  635. return ground;
  636. };
  637. Mesh.CreateTiledGround = function (name, xmin, zmin, xmax, zmax, subdivisions, precision, scene, updatable) {
  638. var tiledGround = new BABYLON.GroundMesh(name, scene);
  639. tiledGround._setReady(false);
  640. tiledGround._subdivisions = subdivisions;
  641. var vertexData = BABYLON.VertexData.CreateTiledGround(xmin, zmin, xmax, zmax, subdivisions, precision);
  642. vertexData.applyToMesh(tiledGround, updatable);
  643. tiledGround._setReady(true);
  644. return tiledGround;
  645. };
  646. Mesh.CreateGroundFromHeightMap = function (name, url, width, height, subdivisions, minHeight, maxHeight, scene, updatable) {
  647. var ground = new BABYLON.GroundMesh(name, scene);
  648. ground._subdivisions = subdivisions;
  649. ground._setReady(false);
  650. var onload = function (img) {
  651. // Getting height map data
  652. var canvas = document.createElement("canvas");
  653. var context = canvas.getContext("2d");
  654. var heightMapWidth = img.width;
  655. var heightMapHeight = img.height;
  656. canvas.width = heightMapWidth;
  657. canvas.height = heightMapHeight;
  658. context.drawImage(img, 0, 0);
  659. // Create VertexData from map data
  660. var buffer = context.getImageData(0, 0, heightMapWidth, heightMapHeight).data;
  661. var vertexData = BABYLON.VertexData.CreateGroundFromHeightMap(width, height, subdivisions, minHeight, maxHeight, buffer, heightMapWidth, heightMapHeight);
  662. vertexData.applyToMesh(ground, updatable);
  663. ground._setReady(true);
  664. };
  665. BABYLON.Tools.LoadImage(url, onload, function () {
  666. }, scene.database);
  667. return ground;
  668. };
  669. // Tools
  670. Mesh.MinMax = function (meshes) {
  671. var minVector = null;
  672. var maxVector = null;
  673. for (var i in meshes) {
  674. var mesh = meshes[i];
  675. var boundingBox = mesh.getBoundingInfo().boundingBox;
  676. if (!minVector) {
  677. minVector = boundingBox.minimumWorld;
  678. maxVector = boundingBox.maximumWorld;
  679. continue;
  680. }
  681. minVector.MinimizeInPlace(boundingBox.minimumWorld);
  682. maxVector.MaximizeInPlace(boundingBox.maximumWorld);
  683. }
  684. return {
  685. min: minVector,
  686. max: maxVector
  687. };
  688. };
  689. Mesh.Center = function (meshesOrMinMaxVector) {
  690. var minMaxVector = meshesOrMinMaxVector.min !== undefined ? meshesOrMinMaxVector : BABYLON.Mesh.MinMax(meshesOrMinMaxVector);
  691. return BABYLON.Vector3.Center(minMaxVector.min, minMaxVector.max);
  692. };
  693. return Mesh;
  694. })(BABYLON.AbstractMesh);
  695. BABYLON.Mesh = Mesh;
  696. })(BABYLON || (BABYLON = {}));
  697. //# sourceMappingURL=babylon.mesh.js.map