babylon.mesh.js 28 KB

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