babylon.mesh.js 31 KB

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