babylon.mesh.js 31 KB

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