babylon.mesh.js 73 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506
  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. /**
  21. * @constructor
  22. * @param {string} name - The value used by scene.getMeshByName() to do a lookup.
  23. * @param {Scene} scene - The scene to add this mesh to.
  24. * @param {Node} parent - The parent of this mesh, if it has one
  25. * @param {Mesh} source - An optional Mesh from which geometry is shared, cloned.
  26. * @param {boolean} doNotCloneChildren - When cloning, skip cloning child meshes of source, default False.
  27. * When false, achieved by calling a clone(), also passing False.
  28. * This will make creation of children, recursive.
  29. */
  30. function Mesh(name, scene, parent, source, doNotCloneChildren) {
  31. if (parent === void 0) { parent = null; }
  32. _super.call(this, name, scene);
  33. // Members
  34. this.delayLoadState = BABYLON.Engine.DELAYLOADSTATE_NONE;
  35. this.instances = new Array();
  36. this._LODLevels = new Array();
  37. this._onBeforeRenderCallbacks = new Array();
  38. this._onAfterRenderCallbacks = new Array();
  39. this._visibleInstances = {};
  40. this._renderIdForInstances = new Array();
  41. this._batchCache = new _InstancesBatch();
  42. this._instancesBufferSize = 32 * 16 * 4; // let's start with a maximum of 32 instances
  43. this._sideOrientation = Mesh._DEFAULTSIDE;
  44. if (source) {
  45. // Geometry
  46. if (source._geometry) {
  47. source._geometry.applyToMesh(this);
  48. }
  49. // Deep copy
  50. BABYLON.Tools.DeepCopy(source, this, ["name", "material", "skeleton", "instances"], []);
  51. // Material
  52. this.material = source.material;
  53. if (!doNotCloneChildren) {
  54. for (var index = 0; index < scene.meshes.length; index++) {
  55. var mesh = scene.meshes[index];
  56. if (mesh.parent === source) {
  57. // doNotCloneChildren is always going to be False
  58. var newChild = mesh.clone(name + "." + mesh.name, this, doNotCloneChildren);
  59. }
  60. }
  61. }
  62. for (index = 0; index < scene.particleSystems.length; index++) {
  63. var system = scene.particleSystems[index];
  64. if (system.emitter === source) {
  65. system.clone(system.name, this);
  66. }
  67. }
  68. this.computeWorldMatrix(true);
  69. }
  70. // Parent
  71. if (parent !== null) {
  72. this.parent = parent;
  73. }
  74. }
  75. Object.defineProperty(Mesh, "FRONTSIDE", {
  76. get: function () {
  77. return Mesh._FRONTSIDE;
  78. },
  79. enumerable: true,
  80. configurable: true
  81. });
  82. Object.defineProperty(Mesh, "BACKSIDE", {
  83. get: function () {
  84. return Mesh._BACKSIDE;
  85. },
  86. enumerable: true,
  87. configurable: true
  88. });
  89. Object.defineProperty(Mesh, "DOUBLESIDE", {
  90. get: function () {
  91. return Mesh._DOUBLESIDE;
  92. },
  93. enumerable: true,
  94. configurable: true
  95. });
  96. Object.defineProperty(Mesh, "DEFAULTSIDE", {
  97. get: function () {
  98. return Mesh._DEFAULTSIDE;
  99. },
  100. enumerable: true,
  101. configurable: true
  102. });
  103. Object.defineProperty(Mesh.prototype, "hasLODLevels", {
  104. // Methods
  105. get: function () {
  106. return this._LODLevels.length > 0;
  107. },
  108. enumerable: true,
  109. configurable: true
  110. });
  111. Mesh.prototype._sortLODLevels = function () {
  112. this._LODLevels.sort(function (a, b) {
  113. if (a.distance < b.distance) {
  114. return 1;
  115. }
  116. if (a.distance > b.distance) {
  117. return -1;
  118. }
  119. return 0;
  120. });
  121. };
  122. /**
  123. * Add a mesh as LOD level triggered at the given distance.
  124. * @param {number} distance - the distance from the center of the object to show this level
  125. * @param {BABYLON.Mesh} mesh - the mesh to be added as LOD level
  126. * @return {BABYLON.Mesh} this mesh (for chaining)
  127. */
  128. Mesh.prototype.addLODLevel = function (distance, mesh) {
  129. if (mesh && mesh._masterMesh) {
  130. BABYLON.Tools.Warn("You cannot use a mesh as LOD level twice");
  131. return this;
  132. }
  133. var level = new BABYLON.Internals.MeshLODLevel(distance, mesh);
  134. this._LODLevels.push(level);
  135. if (mesh) {
  136. mesh._masterMesh = this;
  137. }
  138. this._sortLODLevels();
  139. return this;
  140. };
  141. Mesh.prototype.getLODLevelAtDistance = function (distance) {
  142. for (var index = 0; index < this._LODLevels.length; index++) {
  143. var level = this._LODLevels[index];
  144. if (level.distance === distance) {
  145. return level.mesh;
  146. }
  147. }
  148. return null;
  149. };
  150. /**
  151. * Remove a mesh from the LOD array
  152. * @param {BABYLON.Mesh} mesh - the mesh to be removed.
  153. * @return {BABYLON.Mesh} this mesh (for chaining)
  154. */
  155. Mesh.prototype.removeLODLevel = function (mesh) {
  156. for (var index = 0; index < this._LODLevels.length; index++) {
  157. if (this._LODLevels[index].mesh === mesh) {
  158. this._LODLevels.splice(index, 1);
  159. if (mesh) {
  160. mesh._masterMesh = null;
  161. }
  162. }
  163. }
  164. this._sortLODLevels();
  165. return this;
  166. };
  167. Mesh.prototype.getLOD = function (camera, boundingSphere) {
  168. if (!this._LODLevels || this._LODLevels.length === 0) {
  169. return this;
  170. }
  171. var distanceToCamera = (boundingSphere ? boundingSphere : this.getBoundingInfo().boundingSphere).centerWorld.subtract(camera.position).length();
  172. if (this._LODLevels[this._LODLevels.length - 1].distance > distanceToCamera) {
  173. if (this.onLODLevelSelection) {
  174. this.onLODLevelSelection(distanceToCamera, this, this._LODLevels[this._LODLevels.length - 1].mesh);
  175. }
  176. return this;
  177. }
  178. for (var index = 0; index < this._LODLevels.length; index++) {
  179. var level = this._LODLevels[index];
  180. if (level.distance < distanceToCamera) {
  181. if (level.mesh) {
  182. level.mesh._preActivate();
  183. level.mesh._updateSubMeshesBoundingInfo(this.worldMatrixFromCache);
  184. }
  185. if (this.onLODLevelSelection) {
  186. this.onLODLevelSelection(distanceToCamera, this, level.mesh);
  187. }
  188. return level.mesh;
  189. }
  190. }
  191. if (this.onLODLevelSelection) {
  192. this.onLODLevelSelection(distanceToCamera, this, this);
  193. }
  194. return this;
  195. };
  196. Object.defineProperty(Mesh.prototype, "geometry", {
  197. get: function () {
  198. return this._geometry;
  199. },
  200. enumerable: true,
  201. configurable: true
  202. });
  203. Mesh.prototype.getTotalVertices = function () {
  204. if (!this._geometry) {
  205. return 0;
  206. }
  207. return this._geometry.getTotalVertices();
  208. };
  209. Mesh.prototype.getVerticesData = function (kind, copyWhenShared) {
  210. if (!this._geometry) {
  211. return null;
  212. }
  213. return this._geometry.getVerticesData(kind, copyWhenShared);
  214. };
  215. Mesh.prototype.getVertexBuffer = function (kind) {
  216. if (!this._geometry) {
  217. return undefined;
  218. }
  219. return this._geometry.getVertexBuffer(kind);
  220. };
  221. Mesh.prototype.isVerticesDataPresent = function (kind) {
  222. if (!this._geometry) {
  223. if (this._delayInfo) {
  224. return this._delayInfo.indexOf(kind) !== -1;
  225. }
  226. return false;
  227. }
  228. return this._geometry.isVerticesDataPresent(kind);
  229. };
  230. Mesh.prototype.getVerticesDataKinds = function () {
  231. if (!this._geometry) {
  232. var result = [];
  233. if (this._delayInfo) {
  234. for (var kind in this._delayInfo) {
  235. result.push(kind);
  236. }
  237. }
  238. return result;
  239. }
  240. return this._geometry.getVerticesDataKinds();
  241. };
  242. Mesh.prototype.getTotalIndices = function () {
  243. if (!this._geometry) {
  244. return 0;
  245. }
  246. return this._geometry.getTotalIndices();
  247. };
  248. Mesh.prototype.getIndices = function (copyWhenShared) {
  249. if (!this._geometry) {
  250. return [];
  251. }
  252. return this._geometry.getIndices(copyWhenShared);
  253. };
  254. Object.defineProperty(Mesh.prototype, "isBlocked", {
  255. get: function () {
  256. return this._masterMesh !== null && this._masterMesh !== undefined;
  257. },
  258. enumerable: true,
  259. configurable: true
  260. });
  261. Mesh.prototype.isReady = function () {
  262. if (this.delayLoadState === BABYLON.Engine.DELAYLOADSTATE_LOADING) {
  263. return false;
  264. }
  265. return _super.prototype.isReady.call(this);
  266. };
  267. Mesh.prototype.isDisposed = function () {
  268. return this._isDisposed;
  269. };
  270. Object.defineProperty(Mesh.prototype, "sideOrientation", {
  271. get: function () {
  272. return this._sideOrientation;
  273. },
  274. set: function (sideO) {
  275. this._sideOrientation = sideO;
  276. },
  277. enumerable: true,
  278. configurable: true
  279. });
  280. // Methods
  281. Mesh.prototype._preActivate = function () {
  282. var sceneRenderId = this.getScene().getRenderId();
  283. if (this._preActivateId === sceneRenderId) {
  284. return;
  285. }
  286. this._preActivateId = sceneRenderId;
  287. this._visibleInstances = null;
  288. };
  289. Mesh.prototype._registerInstanceForRenderId = function (instance, renderId) {
  290. if (!this._visibleInstances) {
  291. this._visibleInstances = {};
  292. this._visibleInstances.defaultRenderId = renderId;
  293. this._visibleInstances.selfDefaultRenderId = this._renderId;
  294. }
  295. if (!this._visibleInstances[renderId]) {
  296. this._visibleInstances[renderId] = new Array();
  297. }
  298. this._visibleInstances[renderId].push(instance);
  299. };
  300. Mesh.prototype.refreshBoundingInfo = function () {
  301. var data = this.getVerticesData(BABYLON.VertexBuffer.PositionKind);
  302. if (data) {
  303. var extend = BABYLON.Tools.ExtractMinAndMax(data, 0, this.getTotalVertices());
  304. this._boundingInfo = new BABYLON.BoundingInfo(extend.minimum, extend.maximum);
  305. }
  306. if (this.subMeshes) {
  307. for (var index = 0; index < this.subMeshes.length; index++) {
  308. this.subMeshes[index].refreshBoundingInfo();
  309. }
  310. }
  311. this._updateBoundingInfo();
  312. };
  313. Mesh.prototype._createGlobalSubMesh = function () {
  314. var totalVertices = this.getTotalVertices();
  315. if (!totalVertices || !this.getIndices()) {
  316. return null;
  317. }
  318. this.releaseSubMeshes();
  319. return new BABYLON.SubMesh(0, 0, totalVertices, 0, this.getTotalIndices(), this);
  320. };
  321. Mesh.prototype.subdivide = function (count) {
  322. if (count < 1) {
  323. return;
  324. }
  325. var totalIndices = this.getTotalIndices();
  326. var subdivisionSize = (totalIndices / count) | 0;
  327. var offset = 0;
  328. while (subdivisionSize % 3 !== 0) {
  329. subdivisionSize++;
  330. }
  331. this.releaseSubMeshes();
  332. for (var index = 0; index < count; index++) {
  333. if (offset >= totalIndices) {
  334. break;
  335. }
  336. BABYLON.SubMesh.CreateFromIndices(0, offset, Math.min(subdivisionSize, totalIndices - offset), this);
  337. offset += subdivisionSize;
  338. }
  339. this.synchronizeInstances();
  340. };
  341. Mesh.prototype.setVerticesData = function (kind, data, updatable, stride) {
  342. if (kind instanceof Array) {
  343. var temp = data;
  344. data = kind;
  345. kind = temp;
  346. BABYLON.Tools.Warn("Deprecated usage of setVerticesData detected (since v1.12). Current signature is setVerticesData(kind, data, updatable).");
  347. }
  348. if (!this._geometry) {
  349. var vertexData = new BABYLON.VertexData();
  350. vertexData.set(data, kind);
  351. var scene = this.getScene();
  352. new BABYLON.Geometry(BABYLON.Geometry.RandomId(), scene, vertexData, updatable, this);
  353. }
  354. else {
  355. this._geometry.setVerticesData(kind, data, updatable, stride);
  356. }
  357. };
  358. Mesh.prototype.updateVerticesData = function (kind, data, updateExtends, makeItUnique) {
  359. if (!this._geometry) {
  360. return;
  361. }
  362. if (!makeItUnique) {
  363. this._geometry.updateVerticesData(kind, data, updateExtends);
  364. }
  365. else {
  366. this.makeGeometryUnique();
  367. this.updateVerticesData(kind, data, updateExtends, false);
  368. }
  369. };
  370. Mesh.prototype.updateVerticesDataDirectly = function (kind, data, offset, makeItUnique) {
  371. if (!this._geometry) {
  372. return;
  373. }
  374. if (!makeItUnique) {
  375. this._geometry.updateVerticesDataDirectly(kind, data, offset);
  376. }
  377. else {
  378. this.makeGeometryUnique();
  379. this.updateVerticesDataDirectly(kind, data, offset, false);
  380. }
  381. };
  382. // Mesh positions update function :
  383. // updates the mesh positions according to the positionFunction returned values.
  384. // The positionFunction argument must be a javascript function accepting the mesh "positions" array as parameter.
  385. // This dedicated positionFunction computes new mesh positions according to the given mesh type.
  386. Mesh.prototype.updateMeshPositions = function (positionFunction, computeNormals) {
  387. if (computeNormals === void 0) { computeNormals = true; }
  388. var positions = this.getVerticesData(BABYLON.VertexBuffer.PositionKind);
  389. positionFunction(positions);
  390. this.updateVerticesData(BABYLON.VertexBuffer.PositionKind, positions, false, false);
  391. if (computeNormals) {
  392. var indices = this.getIndices();
  393. var normals = this.getVerticesData(BABYLON.VertexBuffer.NormalKind);
  394. BABYLON.VertexData.ComputeNormals(positions, indices, normals);
  395. this.updateVerticesData(BABYLON.VertexBuffer.NormalKind, normals, false, false);
  396. }
  397. };
  398. Mesh.prototype.makeGeometryUnique = function () {
  399. if (!this._geometry) {
  400. return;
  401. }
  402. var geometry = this._geometry.copy(BABYLON.Geometry.RandomId());
  403. geometry.applyToMesh(this);
  404. };
  405. Mesh.prototype.setIndices = function (indices, totalVertices) {
  406. if (!this._geometry) {
  407. var vertexData = new BABYLON.VertexData();
  408. vertexData.indices = indices;
  409. var scene = this.getScene();
  410. new BABYLON.Geometry(BABYLON.Geometry.RandomId(), scene, vertexData, false, this);
  411. }
  412. else {
  413. this._geometry.setIndices(indices, totalVertices);
  414. }
  415. };
  416. Mesh.prototype._bind = function (subMesh, effect, fillMode) {
  417. var engine = this.getScene().getEngine();
  418. // Wireframe
  419. var indexToBind;
  420. switch (fillMode) {
  421. case BABYLON.Material.PointFillMode:
  422. indexToBind = null;
  423. break;
  424. case BABYLON.Material.WireFrameFillMode:
  425. indexToBind = subMesh.getLinesIndexBuffer(this.getIndices(), engine);
  426. break;
  427. default:
  428. case BABYLON.Material.TriangleFillMode:
  429. indexToBind = this._geometry.getIndexBuffer();
  430. break;
  431. }
  432. // VBOs
  433. engine.bindMultiBuffers(this._geometry.getVertexBuffers(), indexToBind, effect);
  434. };
  435. Mesh.prototype._draw = function (subMesh, fillMode, instancesCount) {
  436. if (!this._geometry || !this._geometry.getVertexBuffers() || !this._geometry.getIndexBuffer()) {
  437. return;
  438. }
  439. var engine = this.getScene().getEngine();
  440. switch (fillMode) {
  441. case BABYLON.Material.PointFillMode:
  442. engine.drawPointClouds(subMesh.verticesStart, subMesh.verticesCount, instancesCount);
  443. break;
  444. case BABYLON.Material.WireFrameFillMode:
  445. engine.draw(false, 0, subMesh.linesIndexCount, instancesCount);
  446. break;
  447. default:
  448. engine.draw(true, subMesh.indexStart, subMesh.indexCount, instancesCount);
  449. }
  450. };
  451. Mesh.prototype.registerBeforeRender = function (func) {
  452. this._onBeforeRenderCallbacks.push(func);
  453. };
  454. Mesh.prototype.unregisterBeforeRender = function (func) {
  455. var index = this._onBeforeRenderCallbacks.indexOf(func);
  456. if (index > -1) {
  457. this._onBeforeRenderCallbacks.splice(index, 1);
  458. }
  459. };
  460. Mesh.prototype.registerAfterRender = function (func) {
  461. this._onAfterRenderCallbacks.push(func);
  462. };
  463. Mesh.prototype.unregisterAfterRender = function (func) {
  464. var index = this._onAfterRenderCallbacks.indexOf(func);
  465. if (index > -1) {
  466. this._onAfterRenderCallbacks.splice(index, 1);
  467. }
  468. };
  469. Mesh.prototype._getInstancesRenderList = function (subMeshId) {
  470. var scene = this.getScene();
  471. this._batchCache.mustReturn = false;
  472. this._batchCache.renderSelf[subMeshId] = this.isEnabled() && this.isVisible;
  473. this._batchCache.visibleInstances[subMeshId] = null;
  474. if (this._visibleInstances) {
  475. var currentRenderId = scene.getRenderId();
  476. this._batchCache.visibleInstances[subMeshId] = this._visibleInstances[currentRenderId];
  477. var selfRenderId = this._renderId;
  478. if (!this._batchCache.visibleInstances[subMeshId] && this._visibleInstances.defaultRenderId) {
  479. this._batchCache.visibleInstances[subMeshId] = this._visibleInstances[this._visibleInstances.defaultRenderId];
  480. currentRenderId = Math.max(this._visibleInstances.defaultRenderId, currentRenderId);
  481. selfRenderId = Math.max(this._visibleInstances.selfDefaultRenderId, currentRenderId);
  482. }
  483. if (this._batchCache.visibleInstances[subMeshId] && this._batchCache.visibleInstances[subMeshId].length) {
  484. if (this._renderIdForInstances[subMeshId] === currentRenderId) {
  485. this._batchCache.mustReturn = true;
  486. return this._batchCache;
  487. }
  488. if (currentRenderId !== selfRenderId) {
  489. this._batchCache.renderSelf[subMeshId] = false;
  490. }
  491. }
  492. this._renderIdForInstances[subMeshId] = currentRenderId;
  493. }
  494. return this._batchCache;
  495. };
  496. Mesh.prototype._renderWithInstances = function (subMesh, fillMode, batch, effect, engine) {
  497. var visibleInstances = batch.visibleInstances[subMesh._id];
  498. var matricesCount = visibleInstances.length + 1;
  499. var bufferSize = matricesCount * 16 * 4;
  500. while (this._instancesBufferSize < bufferSize) {
  501. this._instancesBufferSize *= 2;
  502. }
  503. if (!this._worldMatricesInstancesBuffer || this._worldMatricesInstancesBuffer.capacity < this._instancesBufferSize) {
  504. if (this._worldMatricesInstancesBuffer) {
  505. engine.deleteInstancesBuffer(this._worldMatricesInstancesBuffer);
  506. }
  507. this._worldMatricesInstancesBuffer = engine.createInstancesBuffer(this._instancesBufferSize);
  508. this._worldMatricesInstancesArray = new Float32Array(this._instancesBufferSize / 4);
  509. }
  510. var offset = 0;
  511. var instancesCount = 0;
  512. var world = this.getWorldMatrix();
  513. if (batch.renderSelf[subMesh._id]) {
  514. world.copyToArray(this._worldMatricesInstancesArray, offset);
  515. offset += 16;
  516. instancesCount++;
  517. }
  518. if (visibleInstances) {
  519. for (var instanceIndex = 0; instanceIndex < visibleInstances.length; instanceIndex++) {
  520. var instance = visibleInstances[instanceIndex];
  521. instance.getWorldMatrix().copyToArray(this._worldMatricesInstancesArray, offset);
  522. offset += 16;
  523. instancesCount++;
  524. }
  525. }
  526. var offsetLocation0 = effect.getAttributeLocationByName("world0");
  527. var offsetLocation1 = effect.getAttributeLocationByName("world1");
  528. var offsetLocation2 = effect.getAttributeLocationByName("world2");
  529. var offsetLocation3 = effect.getAttributeLocationByName("world3");
  530. var offsetLocations = [offsetLocation0, offsetLocation1, offsetLocation2, offsetLocation3];
  531. engine.updateAndBindInstancesBuffer(this._worldMatricesInstancesBuffer, this._worldMatricesInstancesArray, offsetLocations);
  532. this._draw(subMesh, fillMode, instancesCount);
  533. engine.unBindInstancesBuffer(this._worldMatricesInstancesBuffer, offsetLocations);
  534. };
  535. Mesh.prototype._processRendering = function (subMesh, effect, fillMode, batch, hardwareInstancedRendering, onBeforeDraw) {
  536. var scene = this.getScene();
  537. var engine = scene.getEngine();
  538. if (hardwareInstancedRendering) {
  539. this._renderWithInstances(subMesh, fillMode, batch, effect, engine);
  540. }
  541. else {
  542. if (batch.renderSelf[subMesh._id]) {
  543. // Draw
  544. if (onBeforeDraw) {
  545. onBeforeDraw(false, this.getWorldMatrix());
  546. }
  547. this._draw(subMesh, fillMode);
  548. }
  549. if (batch.visibleInstances[subMesh._id]) {
  550. for (var instanceIndex = 0; instanceIndex < batch.visibleInstances[subMesh._id].length; instanceIndex++) {
  551. var instance = batch.visibleInstances[subMesh._id][instanceIndex];
  552. // World
  553. var world = instance.getWorldMatrix();
  554. if (onBeforeDraw) {
  555. onBeforeDraw(true, world);
  556. }
  557. // Draw
  558. this._draw(subMesh, fillMode);
  559. }
  560. }
  561. }
  562. };
  563. Mesh.prototype.render = function (subMesh) {
  564. var scene = this.getScene();
  565. // Managing instances
  566. var batch = this._getInstancesRenderList(subMesh._id);
  567. if (batch.mustReturn) {
  568. return;
  569. }
  570. // Checking geometry state
  571. if (!this._geometry || !this._geometry.getVertexBuffers() || !this._geometry.getIndexBuffer()) {
  572. return;
  573. }
  574. for (var callbackIndex = 0; callbackIndex < this._onBeforeRenderCallbacks.length; callbackIndex++) {
  575. this._onBeforeRenderCallbacks[callbackIndex](this);
  576. }
  577. var engine = scene.getEngine();
  578. var hardwareInstancedRendering = (engine.getCaps().instancedArrays !== null) && (batch.visibleInstances[subMesh._id] !== null) && (batch.visibleInstances[subMesh._id] !== undefined);
  579. // Material
  580. var effectiveMaterial = subMesh.getMaterial();
  581. if (!effectiveMaterial || !effectiveMaterial.isReady(this, hardwareInstancedRendering)) {
  582. return;
  583. }
  584. // Outline - step 1
  585. var savedDepthWrite = engine.getDepthWrite();
  586. if (this.renderOutline) {
  587. engine.setDepthWrite(false);
  588. scene.getOutlineRenderer().render(subMesh, batch);
  589. engine.setDepthWrite(savedDepthWrite);
  590. }
  591. effectiveMaterial._preBind();
  592. var effect = effectiveMaterial.getEffect();
  593. // Bind
  594. var fillMode = scene.forcePointsCloud ? BABYLON.Material.PointFillMode : (scene.forceWireframe ? BABYLON.Material.WireFrameFillMode : effectiveMaterial.fillMode);
  595. this._bind(subMesh, effect, fillMode);
  596. var world = this.getWorldMatrix();
  597. effectiveMaterial.bind(world, this);
  598. // Draw
  599. this._processRendering(subMesh, effect, fillMode, batch, hardwareInstancedRendering, function (isInstance, world) {
  600. if (isInstance) {
  601. effectiveMaterial.bindOnlyWorldMatrix(world);
  602. }
  603. });
  604. // Unbind
  605. effectiveMaterial.unbind();
  606. // Outline - step 2
  607. if (this.renderOutline && savedDepthWrite) {
  608. engine.setDepthWrite(true);
  609. engine.setColorWrite(false);
  610. scene.getOutlineRenderer().render(subMesh, batch);
  611. engine.setColorWrite(true);
  612. }
  613. // Overlay
  614. if (this.renderOverlay) {
  615. var currentMode = engine.getAlphaMode();
  616. engine.setAlphaMode(BABYLON.Engine.ALPHA_COMBINE);
  617. scene.getOutlineRenderer().render(subMesh, batch, true);
  618. engine.setAlphaMode(currentMode);
  619. }
  620. for (callbackIndex = 0; callbackIndex < this._onAfterRenderCallbacks.length; callbackIndex++) {
  621. this._onAfterRenderCallbacks[callbackIndex](this);
  622. }
  623. };
  624. Mesh.prototype.getEmittedParticleSystems = function () {
  625. var results = new Array();
  626. for (var index = 0; index < this.getScene().particleSystems.length; index++) {
  627. var particleSystem = this.getScene().particleSystems[index];
  628. if (particleSystem.emitter === this) {
  629. results.push(particleSystem);
  630. }
  631. }
  632. return results;
  633. };
  634. Mesh.prototype.getHierarchyEmittedParticleSystems = function () {
  635. var results = new Array();
  636. var descendants = this.getDescendants();
  637. descendants.push(this);
  638. for (var index = 0; index < this.getScene().particleSystems.length; index++) {
  639. var particleSystem = this.getScene().particleSystems[index];
  640. if (descendants.indexOf(particleSystem.emitter) !== -1) {
  641. results.push(particleSystem);
  642. }
  643. }
  644. return results;
  645. };
  646. Mesh.prototype.getChildren = function () {
  647. var results = [];
  648. for (var index = 0; index < this.getScene().meshes.length; index++) {
  649. var mesh = this.getScene().meshes[index];
  650. if (mesh.parent === this) {
  651. results.push(mesh);
  652. }
  653. }
  654. return results;
  655. };
  656. Mesh.prototype._checkDelayState = function () {
  657. var _this = this;
  658. var that = this;
  659. var scene = this.getScene();
  660. if (this._geometry) {
  661. this._geometry.load(scene);
  662. }
  663. else if (that.delayLoadState === BABYLON.Engine.DELAYLOADSTATE_NOTLOADED) {
  664. that.delayLoadState = BABYLON.Engine.DELAYLOADSTATE_LOADING;
  665. scene._addPendingData(that);
  666. var getBinaryData = (this.delayLoadingFile.indexOf(".babylonbinarymeshdata") !== -1);
  667. BABYLON.Tools.LoadFile(this.delayLoadingFile, function (data) {
  668. if (data instanceof ArrayBuffer) {
  669. _this._delayLoadingFunction(data, _this);
  670. }
  671. else {
  672. _this._delayLoadingFunction(JSON.parse(data), _this);
  673. }
  674. _this.delayLoadState = BABYLON.Engine.DELAYLOADSTATE_LOADED;
  675. scene._removePendingData(_this);
  676. }, function () {
  677. }, scene.database, getBinaryData);
  678. }
  679. };
  680. Mesh.prototype.isInFrustum = function (frustumPlanes) {
  681. if (this.delayLoadState === BABYLON.Engine.DELAYLOADSTATE_LOADING) {
  682. return false;
  683. }
  684. if (!_super.prototype.isInFrustum.call(this, frustumPlanes)) {
  685. return false;
  686. }
  687. this._checkDelayState();
  688. return true;
  689. };
  690. Mesh.prototype.setMaterialByID = function (id) {
  691. var materials = this.getScene().materials;
  692. for (var index = 0; index < materials.length; index++) {
  693. if (materials[index].id === id) {
  694. this.material = materials[index];
  695. return;
  696. }
  697. }
  698. // Multi
  699. var multiMaterials = this.getScene().multiMaterials;
  700. for (index = 0; index < multiMaterials.length; index++) {
  701. if (multiMaterials[index].id === id) {
  702. this.material = multiMaterials[index];
  703. return;
  704. }
  705. }
  706. };
  707. Mesh.prototype.getAnimatables = function () {
  708. var results = [];
  709. if (this.material) {
  710. results.push(this.material);
  711. }
  712. if (this.skeleton) {
  713. results.push(this.skeleton);
  714. }
  715. return results;
  716. };
  717. // Geometry
  718. Mesh.prototype.bakeTransformIntoVertices = function (transform) {
  719. // Position
  720. if (!this.isVerticesDataPresent(BABYLON.VertexBuffer.PositionKind)) {
  721. return;
  722. }
  723. this._resetPointsArrayCache();
  724. var data = this.getVerticesData(BABYLON.VertexBuffer.PositionKind);
  725. var temp = [];
  726. for (var index = 0; index < data.length; index += 3) {
  727. BABYLON.Vector3.TransformCoordinates(BABYLON.Vector3.FromArray(data, index), transform).toArray(temp, index);
  728. }
  729. this.setVerticesData(BABYLON.VertexBuffer.PositionKind, temp, this.getVertexBuffer(BABYLON.VertexBuffer.PositionKind).isUpdatable());
  730. // Normals
  731. if (!this.isVerticesDataPresent(BABYLON.VertexBuffer.NormalKind)) {
  732. return;
  733. }
  734. data = this.getVerticesData(BABYLON.VertexBuffer.NormalKind);
  735. temp = [];
  736. for (index = 0; index < data.length; index += 3) {
  737. BABYLON.Vector3.TransformNormal(BABYLON.Vector3.FromArray(data, index), transform).toArray(temp, index);
  738. }
  739. this.setVerticesData(BABYLON.VertexBuffer.NormalKind, temp, this.getVertexBuffer(BABYLON.VertexBuffer.NormalKind).isUpdatable());
  740. };
  741. // Cache
  742. Mesh.prototype._resetPointsArrayCache = function () {
  743. this._positions = null;
  744. };
  745. Mesh.prototype._generatePointsArray = function () {
  746. if (this._positions)
  747. return true;
  748. this._positions = [];
  749. var data = this.getVerticesData(BABYLON.VertexBuffer.PositionKind);
  750. if (!data) {
  751. return false;
  752. }
  753. for (var index = 0; index < data.length; index += 3) {
  754. this._positions.push(BABYLON.Vector3.FromArray(data, index));
  755. }
  756. return true;
  757. };
  758. // Clone
  759. Mesh.prototype.clone = function (name, newParent, doNotCloneChildren) {
  760. return new Mesh(name, this.getScene(), newParent, this, doNotCloneChildren);
  761. };
  762. // Dispose
  763. Mesh.prototype.dispose = function (doNotRecurse) {
  764. if (this._geometry) {
  765. this._geometry.releaseForMesh(this, true);
  766. }
  767. // Instances
  768. if (this._worldMatricesInstancesBuffer) {
  769. this.getEngine().deleteInstancesBuffer(this._worldMatricesInstancesBuffer);
  770. this._worldMatricesInstancesBuffer = null;
  771. }
  772. while (this.instances.length) {
  773. this.instances[0].dispose();
  774. }
  775. _super.prototype.dispose.call(this, doNotRecurse);
  776. };
  777. // Geometric tools
  778. Mesh.prototype.applyDisplacementMap = function (url, minHeight, maxHeight, onSuccess) {
  779. var _this = this;
  780. var scene = this.getScene();
  781. var onload = function (img) {
  782. // Getting height map data
  783. var canvas = document.createElement("canvas");
  784. var context = canvas.getContext("2d");
  785. var heightMapWidth = img.width;
  786. var heightMapHeight = img.height;
  787. canvas.width = heightMapWidth;
  788. canvas.height = heightMapHeight;
  789. context.drawImage(img, 0, 0);
  790. // Create VertexData from map data
  791. //Cast is due to wrong definition in lib.d.ts from ts 1.3 - https://github.com/Microsoft/TypeScript/issues/949
  792. var buffer = context.getImageData(0, 0, heightMapWidth, heightMapHeight).data;
  793. _this.applyDisplacementMapFromBuffer(buffer, heightMapWidth, heightMapHeight, minHeight, maxHeight);
  794. //execute success callback, if set
  795. if (onSuccess) {
  796. onSuccess(_this);
  797. }
  798. };
  799. BABYLON.Tools.LoadImage(url, onload, function () {
  800. }, scene.database);
  801. };
  802. Mesh.prototype.applyDisplacementMapFromBuffer = function (buffer, heightMapWidth, heightMapHeight, minHeight, maxHeight) {
  803. if (!this.isVerticesDataPresent(BABYLON.VertexBuffer.PositionKind) || !this.isVerticesDataPresent(BABYLON.VertexBuffer.NormalKind) || !this.isVerticesDataPresent(BABYLON.VertexBuffer.UVKind)) {
  804. BABYLON.Tools.Warn("Cannot call applyDisplacementMap: Given mesh is not complete. Position, Normal or UV are missing");
  805. return;
  806. }
  807. var positions = this.getVerticesData(BABYLON.VertexBuffer.PositionKind);
  808. var normals = this.getVerticesData(BABYLON.VertexBuffer.NormalKind);
  809. var uvs = this.getVerticesData(BABYLON.VertexBuffer.UVKind);
  810. var position = BABYLON.Vector3.Zero();
  811. var normal = BABYLON.Vector3.Zero();
  812. var uv = BABYLON.Vector2.Zero();
  813. for (var index = 0; index < positions.length; index += 3) {
  814. BABYLON.Vector3.FromArrayToRef(positions, index, position);
  815. BABYLON.Vector3.FromArrayToRef(normals, index, normal);
  816. BABYLON.Vector2.FromArrayToRef(uvs, (index / 3) * 2, uv);
  817. // Compute height
  818. var u = ((Math.abs(uv.x) * heightMapWidth) % heightMapWidth) | 0;
  819. var v = ((Math.abs(uv.y) * heightMapHeight) % heightMapHeight) | 0;
  820. var pos = (u + v * heightMapWidth) * 4;
  821. var r = buffer[pos] / 255.0;
  822. var g = buffer[pos + 1] / 255.0;
  823. var b = buffer[pos + 2] / 255.0;
  824. var gradient = r * 0.3 + g * 0.59 + b * 0.11;
  825. normal.normalize();
  826. normal.scaleInPlace(minHeight + (maxHeight - minHeight) * gradient);
  827. position = position.add(normal);
  828. position.toArray(positions, index);
  829. }
  830. BABYLON.VertexData.ComputeNormals(positions, this.getIndices(), normals);
  831. this.updateVerticesData(BABYLON.VertexBuffer.PositionKind, positions);
  832. this.updateVerticesData(BABYLON.VertexBuffer.NormalKind, normals);
  833. };
  834. Mesh.prototype.convertToFlatShadedMesh = function () {
  835. /// <summary>Update normals and vertices to get a flat shading rendering.</summary>
  836. /// <summary>Warning: This may imply adding vertices to the mesh in order to get exactly 3 vertices per face</summary>
  837. var kinds = this.getVerticesDataKinds();
  838. var vbs = [];
  839. var data = [];
  840. var newdata = [];
  841. var updatableNormals = false;
  842. for (var kindIndex = 0; kindIndex < kinds.length; kindIndex++) {
  843. var kind = kinds[kindIndex];
  844. var vertexBuffer = this.getVertexBuffer(kind);
  845. if (kind === BABYLON.VertexBuffer.NormalKind) {
  846. updatableNormals = vertexBuffer.isUpdatable();
  847. kinds.splice(kindIndex, 1);
  848. kindIndex--;
  849. continue;
  850. }
  851. vbs[kind] = vertexBuffer;
  852. data[kind] = vbs[kind].getData();
  853. newdata[kind] = [];
  854. }
  855. // Save previous submeshes
  856. var previousSubmeshes = this.subMeshes.slice(0);
  857. var indices = this.getIndices();
  858. var totalIndices = this.getTotalIndices();
  859. for (var index = 0; index < totalIndices; index++) {
  860. var vertexIndex = indices[index];
  861. for (kindIndex = 0; kindIndex < kinds.length; kindIndex++) {
  862. kind = kinds[kindIndex];
  863. var stride = vbs[kind].getStrideSize();
  864. for (var offset = 0; offset < stride; offset++) {
  865. newdata[kind].push(data[kind][vertexIndex * stride + offset]);
  866. }
  867. }
  868. }
  869. // Updating faces & normal
  870. var normals = [];
  871. var positions = newdata[BABYLON.VertexBuffer.PositionKind];
  872. for (index = 0; index < totalIndices; index += 3) {
  873. indices[index] = index;
  874. indices[index + 1] = index + 1;
  875. indices[index + 2] = index + 2;
  876. var p1 = BABYLON.Vector3.FromArray(positions, index * 3);
  877. var p2 = BABYLON.Vector3.FromArray(positions, (index + 1) * 3);
  878. var p3 = BABYLON.Vector3.FromArray(positions, (index + 2) * 3);
  879. var p1p2 = p1.subtract(p2);
  880. var p3p2 = p3.subtract(p2);
  881. var normal = BABYLON.Vector3.Normalize(BABYLON.Vector3.Cross(p1p2, p3p2));
  882. for (var localIndex = 0; localIndex < 3; localIndex++) {
  883. normals.push(normal.x);
  884. normals.push(normal.y);
  885. normals.push(normal.z);
  886. }
  887. }
  888. this.setIndices(indices);
  889. this.setVerticesData(BABYLON.VertexBuffer.NormalKind, normals, updatableNormals);
  890. for (kindIndex = 0; kindIndex < kinds.length; kindIndex++) {
  891. kind = kinds[kindIndex];
  892. this.setVerticesData(kind, newdata[kind], vbs[kind].isUpdatable());
  893. }
  894. // Updating submeshes
  895. this.releaseSubMeshes();
  896. for (var submeshIndex = 0; submeshIndex < previousSubmeshes.length; submeshIndex++) {
  897. var previousOne = previousSubmeshes[submeshIndex];
  898. var subMesh = new BABYLON.SubMesh(previousOne.materialIndex, previousOne.indexStart, previousOne.indexCount, previousOne.indexStart, previousOne.indexCount, this);
  899. }
  900. this.synchronizeInstances();
  901. };
  902. // Instances
  903. Mesh.prototype.createInstance = function (name) {
  904. return new BABYLON.InstancedMesh(name, this);
  905. };
  906. Mesh.prototype.synchronizeInstances = function () {
  907. for (var instanceIndex = 0; instanceIndex < this.instances.length; instanceIndex++) {
  908. var instance = this.instances[instanceIndex];
  909. instance._syncSubMeshes();
  910. }
  911. };
  912. /**
  913. * Simplify the mesh according to the given array of settings.
  914. * Function will return immediately and will simplify async.
  915. * @param settings a collection of simplification settings.
  916. * @param parallelProcessing should all levels calculate parallel or one after the other.
  917. * @param type the type of simplification to run.
  918. * @param successCallback optional success callback to be called after the simplification finished processing all settings.
  919. */
  920. Mesh.prototype.simplify = function (settings, parallelProcessing, simplificationType, successCallback) {
  921. if (parallelProcessing === void 0) { parallelProcessing = true; }
  922. if (simplificationType === void 0) { simplificationType = 0 /* QUADRATIC */; }
  923. this.getScene().simplificationQueue.addTask({
  924. settings: settings,
  925. parallelProcessing: parallelProcessing,
  926. mesh: this,
  927. simplificationType: simplificationType,
  928. successCallback: successCallback
  929. });
  930. };
  931. /**
  932. * Optimization of the mesh's indices, in case a mesh has duplicated vertices.
  933. * The function will only reorder the indices and will not remove unused vertices to avoid problems with submeshes.
  934. * This should be used together with the simplification to avoid disappearing triangles.
  935. * @param successCallback an optional success callback to be called after the optimization finished.
  936. */
  937. Mesh.prototype.optimizeIndices = function (successCallback) {
  938. var _this = this;
  939. var indices = this.getIndices();
  940. var positions = this.getVerticesData(BABYLON.VertexBuffer.PositionKind);
  941. var vectorPositions = [];
  942. for (var pos = 0; pos < positions.length; pos = pos + 3) {
  943. vectorPositions.push(BABYLON.Vector3.FromArray(positions, pos));
  944. }
  945. var dupes = [];
  946. BABYLON.AsyncLoop.SyncAsyncForLoop(vectorPositions.length, 40, function (iteration) {
  947. var realPos = vectorPositions.length - 1 - iteration;
  948. var testedPosition = vectorPositions[realPos];
  949. for (var j = 0; j < realPos; ++j) {
  950. var againstPosition = vectorPositions[j];
  951. if (testedPosition.equals(againstPosition)) {
  952. dupes[realPos] = j;
  953. break;
  954. }
  955. }
  956. }, function () {
  957. for (var i = 0; i < indices.length; ++i) {
  958. indices[i] = dupes[indices[i]] || indices[i];
  959. }
  960. //indices are now reordered
  961. var originalSubMeshes = _this.subMeshes.slice(0);
  962. _this.setIndices(indices);
  963. _this.subMeshes = originalSubMeshes;
  964. if (successCallback) {
  965. successCallback(_this);
  966. }
  967. });
  968. };
  969. // Statics
  970. Mesh.CreateRibbon = function (name, pathArray, closeArray, closePath, offset, scene, updatable, sideOrientation, ribbonInstance) {
  971. if (sideOrientation === void 0) { sideOrientation = Mesh.DEFAULTSIDE; }
  972. if (ribbonInstance === void 0) { ribbonInstance = null; }
  973. if (ribbonInstance) {
  974. // positionFunction : ribbon case
  975. // only pathArray and sideOrientation parameters are taken into account for positions update
  976. var positionsOfRibbon = function (pathArray, sideOrientation) {
  977. var positionFunction = function (positions) {
  978. var minlg = pathArray[0].length;
  979. var i = 0;
  980. var ns = (sideOrientation == BABYLON.Mesh.DOUBLESIDE) ? 2 : 1;
  981. for (var si = 1; si <= ns; si++) {
  982. for (var p = 0; p < pathArray.length; p++) {
  983. var path = pathArray[p];
  984. var l = path.length;
  985. minlg = (minlg < l) ? minlg : l;
  986. var j = 0;
  987. while (j < minlg) {
  988. positions[i] = path[j].x;
  989. positions[i + 1] = path[j].y;
  990. positions[i + 2] = path[j].z;
  991. j++;
  992. i += 3;
  993. }
  994. }
  995. }
  996. };
  997. return positionFunction;
  998. };
  999. var sideOrientation = ribbonInstance.sideOrientation;
  1000. var positionFunction = positionsOfRibbon(pathArray, sideOrientation);
  1001. ribbonInstance.updateMeshPositions(positionFunction, true);
  1002. return ribbonInstance;
  1003. }
  1004. else {
  1005. var ribbon = new Mesh(name, scene);
  1006. ribbon.sideOrientation = sideOrientation;
  1007. var vertexData = BABYLON.VertexData.CreateRibbon(pathArray, closeArray, closePath, offset, sideOrientation);
  1008. vertexData.applyToMesh(ribbon, updatable);
  1009. return ribbon;
  1010. }
  1011. };
  1012. Mesh.CreateDisc = function (name, radius, tessellation, scene, updatable, sideOrientation) {
  1013. if (sideOrientation === void 0) { sideOrientation = Mesh.DEFAULTSIDE; }
  1014. var disc = new Mesh(name, scene);
  1015. var vertexData = BABYLON.VertexData.CreateDisc(radius, tessellation, sideOrientation);
  1016. vertexData.applyToMesh(disc, updatable);
  1017. return disc;
  1018. };
  1019. Mesh.CreateBox = function (name, size, scene, updatable, sideOrientation) {
  1020. if (sideOrientation === void 0) { sideOrientation = Mesh.DEFAULTSIDE; }
  1021. var box = new Mesh(name, scene);
  1022. var vertexData = BABYLON.VertexData.CreateBox(size, sideOrientation);
  1023. vertexData.applyToMesh(box, updatable);
  1024. return box;
  1025. };
  1026. Mesh.CreateSphere = function (name, segments, diameter, scene, updatable, sideOrientation) {
  1027. if (sideOrientation === void 0) { sideOrientation = Mesh.DEFAULTSIDE; }
  1028. var sphere = new Mesh(name, scene);
  1029. var vertexData = BABYLON.VertexData.CreateSphere(segments, diameter, sideOrientation);
  1030. vertexData.applyToMesh(sphere, updatable);
  1031. return sphere;
  1032. };
  1033. // Cylinder and cone (Code inspired by SharpDX.org)
  1034. Mesh.CreateCylinder = function (name, height, diameterTop, diameterBottom, tessellation, subdivisions, scene, updatable, sideOrientation) {
  1035. if (sideOrientation === void 0) { sideOrientation = Mesh.DEFAULTSIDE; }
  1036. // subdivisions is a new parameter, we need to support old signature
  1037. if (scene === undefined || !(scene instanceof BABYLON.Scene)) {
  1038. if (scene !== undefined) {
  1039. updatable = scene;
  1040. }
  1041. scene = subdivisions;
  1042. subdivisions = 1;
  1043. }
  1044. var cylinder = new Mesh(name, scene);
  1045. var vertexData = BABYLON.VertexData.CreateCylinder(height, diameterTop, diameterBottom, tessellation, subdivisions);
  1046. vertexData.applyToMesh(cylinder, updatable);
  1047. return cylinder;
  1048. };
  1049. // Torus (Code from SharpDX.org)
  1050. Mesh.CreateTorus = function (name, diameter, thickness, tessellation, scene, updatable, sideOrientation) {
  1051. if (sideOrientation === void 0) { sideOrientation = Mesh.DEFAULTSIDE; }
  1052. var torus = new Mesh(name, scene);
  1053. var vertexData = BABYLON.VertexData.CreateTorus(diameter, thickness, tessellation, sideOrientation);
  1054. vertexData.applyToMesh(torus, updatable);
  1055. return torus;
  1056. };
  1057. Mesh.CreateTorusKnot = function (name, radius, tube, radialSegments, tubularSegments, p, q, scene, updatable, sideOrientation) {
  1058. if (sideOrientation === void 0) { sideOrientation = Mesh.DEFAULTSIDE; }
  1059. var torusKnot = new Mesh(name, scene);
  1060. var vertexData = BABYLON.VertexData.CreateTorusKnot(radius, tube, radialSegments, tubularSegments, p, q, sideOrientation);
  1061. vertexData.applyToMesh(torusKnot, updatable);
  1062. return torusKnot;
  1063. };
  1064. // Lines
  1065. Mesh.CreateLines = function (name, points, scene, updatable, linesInstance) {
  1066. if (linesInstance === void 0) { linesInstance = null; }
  1067. if (linesInstance) {
  1068. var positionsOfLines = function (points) {
  1069. var positionFunction = function (positions) {
  1070. var i = 0;
  1071. for (var p = 0; p < points.length; p++) {
  1072. positions[i] = points[p].x;
  1073. positions[i + 1] = points[p].y;
  1074. positions[i + 2] = points[p].z;
  1075. i += 3;
  1076. }
  1077. };
  1078. return positionFunction;
  1079. };
  1080. var positionFunction = positionsOfLines(points);
  1081. linesInstance.updateMeshPositions(positionFunction, false);
  1082. return linesInstance;
  1083. }
  1084. // lines creation
  1085. var lines = new BABYLON.LinesMesh(name, scene, updatable);
  1086. var vertexData = BABYLON.VertexData.CreateLines(points);
  1087. vertexData.applyToMesh(lines, updatable);
  1088. return lines;
  1089. };
  1090. // Extrusion
  1091. Mesh.ExtrudeShape = function (name, shape, path, scale, rotation, scene, updatable, sideOrientation, extrudedInstance) {
  1092. if (sideOrientation === void 0) { sideOrientation = Mesh.DEFAULTSIDE; }
  1093. if (extrudedInstance === void 0) { extrudedInstance = null; }
  1094. scale = scale || 1;
  1095. rotation = rotation || 0;
  1096. var extruded = Mesh._ExtrudeShapeGeneric(name, shape, path, scale, rotation, null, null, false, false, false, scene, updatable, sideOrientation, extrudedInstance);
  1097. return extruded;
  1098. };
  1099. Mesh.ExtrudeShapeCustom = function (name, shape, path, scaleFunction, rotationFunction, ribbonCloseArray, ribbonClosePath, scene, updatable, sideOrientation, extrudedInstance) {
  1100. if (sideOrientation === void 0) { sideOrientation = Mesh.DEFAULTSIDE; }
  1101. if (extrudedInstance === void 0) { extrudedInstance = null; }
  1102. var extrudedCustom = Mesh._ExtrudeShapeGeneric(name, shape, path, null, null, scaleFunction, rotationFunction, ribbonCloseArray, ribbonClosePath, true, scene, updatable, sideOrientation, extrudedInstance);
  1103. return extrudedCustom;
  1104. };
  1105. Mesh._ExtrudeShapeGeneric = function (name, shape, curve, scale, rotation, scaleFunction, rotateFunction, rbCA, rbCP, custom, scene, updtbl, side, instance) {
  1106. // extrusion geometry
  1107. var extrusionPathArray = function (shape, curve, path3D, shapePaths, scale, rotation, scaleFunction, rotateFunction, custom) {
  1108. var tangents = path3D.getTangents();
  1109. var normals = path3D.getNormals();
  1110. var binormals = path3D.getBinormals();
  1111. var distances = path3D.getDistances();
  1112. var angle = 0;
  1113. var returnScale = function (i, distance) {
  1114. return scale;
  1115. };
  1116. var returnRotation = function (i, distance) {
  1117. return rotation;
  1118. };
  1119. var rotate = custom ? rotateFunction : returnRotation;
  1120. var scl = custom ? scaleFunction : returnScale;
  1121. var index = 0;
  1122. for (var i = 0; i < curve.length; i++) {
  1123. var shapePath = new Array();
  1124. var angleStep = rotate(i, distances[i]);
  1125. var scaleRatio = scl(i, distances[i]);
  1126. for (var p = 0; p < shape.length; p++) {
  1127. var rotationMatrix = BABYLON.Matrix.RotationAxis(tangents[i], angle);
  1128. var planed = ((tangents[i].scale(shape[p].z)).add(normals[i].scale(shape[p].x)).add(binormals[i].scale(shape[p].y)));
  1129. var rotated = BABYLON.Vector3.TransformCoordinates(planed, rotationMatrix).scaleInPlace(scaleRatio).add(curve[i]);
  1130. shapePath.push(rotated);
  1131. }
  1132. shapePaths[index] = shapePath;
  1133. angle += angleStep;
  1134. index++;
  1135. }
  1136. return shapePaths;
  1137. };
  1138. if (instance) {
  1139. var path3D = (instance.path3D).update(curve);
  1140. var pathArray = extrusionPathArray(shape, curve, instance.path3D, instance.pathArray, scale, rotation, scaleFunction, rotateFunction, custom);
  1141. instance = Mesh.CreateRibbon(null, pathArray, null, null, null, null, null, null, instance);
  1142. return instance;
  1143. }
  1144. // extruded shape creation
  1145. var path3D = new BABYLON.Path3D(curve);
  1146. var newShapePaths = new Array();
  1147. var pathArray = extrusionPathArray(shape, curve, path3D, newShapePaths, scale, rotation, scaleFunction, rotateFunction, custom);
  1148. var extrudedGeneric = Mesh.CreateRibbon(name, pathArray, rbCA, rbCP, 0, scene, updtbl, side);
  1149. extrudedGeneric.pathArray = pathArray;
  1150. extrudedGeneric.path3D = path3D;
  1151. return extrudedGeneric;
  1152. };
  1153. // Plane & ground
  1154. Mesh.CreatePlane = function (name, size, scene, updatable, sideOrientation) {
  1155. if (sideOrientation === void 0) { sideOrientation = Mesh.DEFAULTSIDE; }
  1156. var plane = new Mesh(name, scene);
  1157. var vertexData = BABYLON.VertexData.CreatePlane(size, sideOrientation);
  1158. vertexData.applyToMesh(plane, updatable);
  1159. return plane;
  1160. };
  1161. Mesh.CreateGround = function (name, width, height, subdivisions, scene, updatable) {
  1162. var ground = new BABYLON.GroundMesh(name, scene);
  1163. ground._setReady(false);
  1164. ground._subdivisions = subdivisions;
  1165. var vertexData = BABYLON.VertexData.CreateGround(width, height, subdivisions);
  1166. vertexData.applyToMesh(ground, updatable);
  1167. ground._setReady(true);
  1168. return ground;
  1169. };
  1170. Mesh.CreateTiledGround = function (name, xmin, zmin, xmax, zmax, subdivisions, precision, scene, updatable) {
  1171. var tiledGround = new Mesh(name, scene);
  1172. var vertexData = BABYLON.VertexData.CreateTiledGround(xmin, zmin, xmax, zmax, subdivisions, precision);
  1173. vertexData.applyToMesh(tiledGround, updatable);
  1174. return tiledGround;
  1175. };
  1176. Mesh.CreateGroundFromHeightMap = function (name, url, width, height, subdivisions, minHeight, maxHeight, scene, updatable, onReady) {
  1177. var ground = new BABYLON.GroundMesh(name, scene);
  1178. ground._subdivisions = subdivisions;
  1179. ground._setReady(false);
  1180. var onload = function (img) {
  1181. // Getting height map data
  1182. var canvas = document.createElement("canvas");
  1183. var context = canvas.getContext("2d");
  1184. var heightMapWidth = img.width;
  1185. var heightMapHeight = img.height;
  1186. canvas.width = heightMapWidth;
  1187. canvas.height = heightMapHeight;
  1188. context.drawImage(img, 0, 0);
  1189. // Create VertexData from map data
  1190. // Cast is due to wrong definition in lib.d.ts from ts 1.3 - https://github.com/Microsoft/TypeScript/issues/949
  1191. var buffer = context.getImageData(0, 0, heightMapWidth, heightMapHeight).data;
  1192. var vertexData = BABYLON.VertexData.CreateGroundFromHeightMap(width, height, subdivisions, minHeight, maxHeight, buffer, heightMapWidth, heightMapHeight);
  1193. vertexData.applyToMesh(ground, updatable);
  1194. ground._setReady(true);
  1195. //execute ready callback, if set
  1196. if (onReady) {
  1197. onReady(ground);
  1198. }
  1199. };
  1200. BABYLON.Tools.LoadImage(url, onload, function () {
  1201. }, scene.database);
  1202. return ground;
  1203. };
  1204. Mesh.CreateTube = function (name, path, radius, tessellation, radiusFunction, scene, updatable, sideOrientation, tubeInstance) {
  1205. if (sideOrientation === void 0) { sideOrientation = Mesh.DEFAULTSIDE; }
  1206. if (tubeInstance === void 0) { tubeInstance = null; }
  1207. // tube geometry
  1208. var tubePathArray = function (path, path3D, circlePaths, radius, tessellation, radiusFunction) {
  1209. var tangents = path3D.getTangents();
  1210. var normals = path3D.getNormals();
  1211. var distances = path3D.getDistances();
  1212. var pi2 = Math.PI * 2;
  1213. var step = pi2 / tessellation;
  1214. var returnRadius = function (i, distance) { return radius; };
  1215. var radiusFunctionFinal = radiusFunction || returnRadius;
  1216. var circlePath;
  1217. var rad;
  1218. var normal;
  1219. var rotated;
  1220. var rotationMatrix;
  1221. var index = 0;
  1222. for (var i = 0; i < path.length; i++) {
  1223. rad = radiusFunctionFinal(i, distances[i]); // current radius
  1224. circlePath = Array(); // current circle array
  1225. normal = normals[i]; // current normal
  1226. for (var ang = 0; ang < pi2; ang += step) {
  1227. rotationMatrix = BABYLON.Matrix.RotationAxis(tangents[i], ang);
  1228. rotated = BABYLON.Vector3.TransformCoordinates(normal, rotationMatrix).scaleInPlace(rad).add(path[i]);
  1229. circlePath.push(rotated);
  1230. }
  1231. circlePaths[index] = circlePath;
  1232. index++;
  1233. }
  1234. return circlePaths;
  1235. };
  1236. if (tubeInstance) {
  1237. var path3D = (tubeInstance.path3D).update(path);
  1238. var pathArray = tubePathArray(path, path3D, tubeInstance.pathArray, radius, tubeInstance.tessellation, radiusFunction);
  1239. tubeInstance = Mesh.CreateRibbon(null, pathArray, null, null, null, null, null, null, tubeInstance);
  1240. return tubeInstance;
  1241. }
  1242. // tube creation
  1243. var path3D = new BABYLON.Path3D(path);
  1244. var newPathArray = new Array();
  1245. var pathArray = tubePathArray(path, path3D, newPathArray, radius, tessellation, radiusFunction);
  1246. var tube = Mesh.CreateRibbon(name, pathArray, false, true, 0, scene, updatable, sideOrientation);
  1247. tube.pathArray = pathArray;
  1248. tube.path3D = path3D;
  1249. tube.tessellation = tessellation;
  1250. return tube;
  1251. };
  1252. // Decals
  1253. Mesh.CreateDecal = function (name, sourceMesh, position, normal, size, angle) {
  1254. if (angle === void 0) { angle = 0; }
  1255. var indices = sourceMesh.getIndices();
  1256. var positions = sourceMesh.getVerticesData(BABYLON.VertexBuffer.PositionKind);
  1257. var normals = sourceMesh.getVerticesData(BABYLON.VertexBuffer.NormalKind);
  1258. // Getting correct rotation
  1259. if (!normal) {
  1260. var target = new BABYLON.Vector3(0, 0, 1);
  1261. var camera = sourceMesh.getScene().activeCamera;
  1262. var cameraWorldTarget = BABYLON.Vector3.TransformCoordinates(target, camera.getWorldMatrix());
  1263. normal = camera.globalPosition.subtract(cameraWorldTarget);
  1264. }
  1265. var yaw = -Math.atan2(normal.z, normal.x) - Math.PI / 2;
  1266. var len = Math.sqrt(normal.x * normal.x + normal.z * normal.z);
  1267. var pitch = Math.atan2(normal.y, len);
  1268. // Matrix
  1269. var decalWorldMatrix = BABYLON.Matrix.RotationYawPitchRoll(yaw, pitch, angle).multiply(BABYLON.Matrix.Translation(position.x, position.y, position.z));
  1270. var inverseDecalWorldMatrix = BABYLON.Matrix.Invert(decalWorldMatrix);
  1271. var meshWorldMatrix = sourceMesh.getWorldMatrix();
  1272. var transformMatrix = meshWorldMatrix.multiply(inverseDecalWorldMatrix);
  1273. var vertexData = new BABYLON.VertexData();
  1274. vertexData.indices = [];
  1275. vertexData.positions = [];
  1276. vertexData.normals = [];
  1277. vertexData.uvs = [];
  1278. var currentVertexDataIndex = 0;
  1279. var extractDecalVector3 = function (indexId) {
  1280. var vertexId = indices[indexId];
  1281. var result = new BABYLON.PositionNormalVertex();
  1282. result.position = new BABYLON.Vector3(positions[vertexId * 3], positions[vertexId * 3 + 1], positions[vertexId * 3 + 2]);
  1283. // Send vector to decal local world
  1284. result.position = BABYLON.Vector3.TransformCoordinates(result.position, transformMatrix);
  1285. // Get normal
  1286. result.normal = new BABYLON.Vector3(normals[vertexId * 3], normals[vertexId * 3 + 1], normals[vertexId * 3 + 2]);
  1287. return result;
  1288. };
  1289. // Inspired by https://github.com/mrdoob/three.js/blob/eee231960882f6f3b6113405f524956145148146/examples/js/geometries/DecalGeometry.js
  1290. var clip = function (vertices, axis) {
  1291. if (vertices.length === 0) {
  1292. return vertices;
  1293. }
  1294. var clipSize = 0.5 * Math.abs(BABYLON.Vector3.Dot(size, axis));
  1295. var clipVertices = function (v0, v1) {
  1296. var clipFactor = BABYLON.Vector3.GetClipFactor(v0.position, v1.position, axis, clipSize);
  1297. return new BABYLON.PositionNormalVertex(BABYLON.Vector3.Lerp(v0.position, v1.position, clipFactor), BABYLON.Vector3.Lerp(v0.normal, v1.normal, clipFactor));
  1298. };
  1299. var result = new Array();
  1300. for (var index = 0; index < vertices.length; index += 3) {
  1301. var v1Out;
  1302. var v2Out;
  1303. var v3Out;
  1304. var total = 0;
  1305. var nV1, nV2, nV3, nV4;
  1306. var d1 = BABYLON.Vector3.Dot(vertices[index].position, axis) - clipSize;
  1307. var d2 = BABYLON.Vector3.Dot(vertices[index + 1].position, axis) - clipSize;
  1308. var d3 = BABYLON.Vector3.Dot(vertices[index + 2].position, axis) - clipSize;
  1309. v1Out = d1 > 0;
  1310. v2Out = d2 > 0;
  1311. v3Out = d3 > 0;
  1312. total = (v1Out ? 1 : 0) + (v2Out ? 1 : 0) + (v3Out ? 1 : 0);
  1313. switch (total) {
  1314. case 0:
  1315. result.push(vertices[index]);
  1316. result.push(vertices[index + 1]);
  1317. result.push(vertices[index + 2]);
  1318. break;
  1319. case 1:
  1320. if (v1Out) {
  1321. nV1 = vertices[index + 1];
  1322. nV2 = vertices[index + 2];
  1323. nV3 = clipVertices(vertices[index], nV1);
  1324. nV4 = clipVertices(vertices[index], nV2);
  1325. }
  1326. if (v2Out) {
  1327. nV1 = vertices[index];
  1328. nV2 = vertices[index + 2];
  1329. nV3 = clipVertices(vertices[index + 1], nV1);
  1330. nV4 = clipVertices(vertices[index + 1], nV2);
  1331. result.push(nV3);
  1332. result.push(nV2.clone());
  1333. result.push(nV1.clone());
  1334. result.push(nV2.clone());
  1335. result.push(nV3.clone());
  1336. result.push(nV4);
  1337. break;
  1338. }
  1339. if (v3Out) {
  1340. nV1 = vertices[index];
  1341. nV2 = vertices[index + 1];
  1342. nV3 = clipVertices(vertices[index + 2], nV1);
  1343. nV4 = clipVertices(vertices[index + 2], nV2);
  1344. }
  1345. result.push(nV1.clone());
  1346. result.push(nV2.clone());
  1347. result.push(nV3);
  1348. result.push(nV4);
  1349. result.push(nV3.clone());
  1350. result.push(nV2.clone());
  1351. break;
  1352. case 2:
  1353. if (!v1Out) {
  1354. nV1 = vertices[index].clone();
  1355. nV2 = clipVertices(nV1, vertices[index + 1]);
  1356. nV3 = clipVertices(nV1, vertices[index + 2]);
  1357. result.push(nV1);
  1358. result.push(nV2);
  1359. result.push(nV3);
  1360. }
  1361. if (!v2Out) {
  1362. nV1 = vertices[index + 1].clone();
  1363. nV2 = clipVertices(nV1, vertices[index + 2]);
  1364. nV3 = clipVertices(nV1, vertices[index]);
  1365. result.push(nV1);
  1366. result.push(nV2);
  1367. result.push(nV3);
  1368. }
  1369. if (!v3Out) {
  1370. nV1 = vertices[index + 2].clone();
  1371. nV2 = clipVertices(nV1, vertices[index]);
  1372. nV3 = clipVertices(nV1, vertices[index + 1]);
  1373. result.push(nV1);
  1374. result.push(nV2);
  1375. result.push(nV3);
  1376. }
  1377. break;
  1378. case 3:
  1379. break;
  1380. }
  1381. }
  1382. return result;
  1383. };
  1384. for (var index = 0; index < indices.length; index += 3) {
  1385. var faceVertices = new Array();
  1386. faceVertices.push(extractDecalVector3(index));
  1387. faceVertices.push(extractDecalVector3(index + 1));
  1388. faceVertices.push(extractDecalVector3(index + 2));
  1389. // Clip
  1390. faceVertices = clip(faceVertices, new BABYLON.Vector3(1, 0, 0));
  1391. faceVertices = clip(faceVertices, new BABYLON.Vector3(-1, 0, 0));
  1392. faceVertices = clip(faceVertices, new BABYLON.Vector3(0, 1, 0));
  1393. faceVertices = clip(faceVertices, new BABYLON.Vector3(0, -1, 0));
  1394. faceVertices = clip(faceVertices, new BABYLON.Vector3(0, 0, 1));
  1395. faceVertices = clip(faceVertices, new BABYLON.Vector3(0, 0, -1));
  1396. if (faceVertices.length === 0) {
  1397. continue;
  1398. }
  1399. // Add UVs and get back to world
  1400. var localRotationMatrix = BABYLON.Matrix.RotationYawPitchRoll(yaw, pitch, angle);
  1401. for (var vIndex = 0; vIndex < faceVertices.length; vIndex++) {
  1402. var vertex = faceVertices[vIndex];
  1403. vertexData.indices.push(currentVertexDataIndex);
  1404. vertex.position.toArray(vertexData.positions, currentVertexDataIndex * 3);
  1405. vertex.normal.toArray(vertexData.normals, currentVertexDataIndex * 3);
  1406. vertexData.uvs.push(0.5 + vertex.position.x / size.x);
  1407. vertexData.uvs.push(0.5 + vertex.position.y / size.y);
  1408. currentVertexDataIndex++;
  1409. }
  1410. }
  1411. // Return mesh
  1412. var decal = new Mesh(name, sourceMesh.getScene());
  1413. vertexData.applyToMesh(decal);
  1414. decal.position = position.clone();
  1415. decal.rotation = new BABYLON.Vector3(pitch, yaw, angle);
  1416. return decal;
  1417. };
  1418. // Tools
  1419. Mesh.MinMax = function (meshes) {
  1420. var minVector = null;
  1421. var maxVector = null;
  1422. for (var i in meshes) {
  1423. var mesh = meshes[i];
  1424. var boundingBox = mesh.getBoundingInfo().boundingBox;
  1425. if (!minVector) {
  1426. minVector = boundingBox.minimumWorld;
  1427. maxVector = boundingBox.maximumWorld;
  1428. continue;
  1429. }
  1430. minVector.MinimizeInPlace(boundingBox.minimumWorld);
  1431. maxVector.MaximizeInPlace(boundingBox.maximumWorld);
  1432. }
  1433. return {
  1434. min: minVector,
  1435. max: maxVector
  1436. };
  1437. };
  1438. Mesh.Center = function (meshesOrMinMaxVector) {
  1439. var minMaxVector = meshesOrMinMaxVector.min !== undefined ? meshesOrMinMaxVector : Mesh.MinMax(meshesOrMinMaxVector);
  1440. return BABYLON.Vector3.Center(minMaxVector.min, minMaxVector.max);
  1441. };
  1442. /**
  1443. * Merge the array of meshes into a single mesh for performance reasons.
  1444. * @param {Array<Mesh>} meshes - The vertices source. They should all be of the same material. Entries can empty
  1445. * @param {boolean} disposeSource - When true (default), dispose of the vertices from the source meshes
  1446. * @param {boolean} allow32BitsIndices - When the sum of the vertices > 64k, this must be set to true.
  1447. * @param {Mesh} meshSubclass - When set, vertices inserted into this Mesh. Meshes can then be merged into a Mesh sub-class.
  1448. */
  1449. Mesh.MergeMeshes = function (meshes, disposeSource, allow32BitsIndices, meshSubclass) {
  1450. if (disposeSource === void 0) { disposeSource = true; }
  1451. if (!allow32BitsIndices) {
  1452. var totalVertices = 0;
  1453. for (var index = 0; index < meshes.length; index++) {
  1454. if (meshes[index]) {
  1455. totalVertices += meshes[index].getTotalVertices();
  1456. if (totalVertices > 65536) {
  1457. BABYLON.Tools.Warn("Cannot merge meshes because resulting mesh will have more than 65536 vertices. Please use allow32BitsIndices = true to use 32 bits indices");
  1458. return null;
  1459. }
  1460. }
  1461. }
  1462. }
  1463. // Merge
  1464. var vertexData;
  1465. var otherVertexData;
  1466. var source;
  1467. for (index = 0; index < meshes.length; index++) {
  1468. if (meshes[index]) {
  1469. otherVertexData = BABYLON.VertexData.ExtractFromMesh(meshes[index], true);
  1470. otherVertexData.transform(meshes[index].getWorldMatrix());
  1471. if (vertexData) {
  1472. vertexData.merge(otherVertexData);
  1473. }
  1474. else {
  1475. vertexData = otherVertexData;
  1476. source = meshes[index];
  1477. }
  1478. }
  1479. }
  1480. if (!meshSubclass) {
  1481. meshSubclass = new Mesh(source.name + "_merged", source.getScene());
  1482. }
  1483. vertexData.applyToMesh(meshSubclass);
  1484. // Setting properties
  1485. meshSubclass.material = source.material;
  1486. meshSubclass.checkCollisions = source.checkCollisions;
  1487. // Cleaning
  1488. if (disposeSource) {
  1489. for (index = 0; index < meshes.length; index++) {
  1490. if (meshes[index]) {
  1491. meshes[index].dispose();
  1492. }
  1493. }
  1494. }
  1495. return meshSubclass;
  1496. };
  1497. // Consts
  1498. Mesh._FRONTSIDE = 0;
  1499. Mesh._BACKSIDE = 1;
  1500. Mesh._DOUBLESIDE = 2;
  1501. Mesh._DEFAULTSIDE = 0;
  1502. return Mesh;
  1503. })(BABYLON.AbstractMesh);
  1504. BABYLON.Mesh = Mesh;
  1505. })(BABYLON || (BABYLON = {}));
  1506. //# sourceMappingURL=babylon.mesh.js.map