babylon.engine.js 90 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849
  1. var BABYLON;
  2. (function (BABYLON) {
  3. var _DepthCullingState = (function () {
  4. function _DepthCullingState() {
  5. this._isDepthTestDirty = false;
  6. this._isDepthMaskDirty = false;
  7. this._isDepthFuncDirty = false;
  8. this._isCullFaceDirty = false;
  9. this._isCullDirty = false;
  10. }
  11. Object.defineProperty(_DepthCullingState.prototype, "isDirty", {
  12. get: function () {
  13. return this._isDepthFuncDirty || this._isDepthTestDirty || this._isDepthMaskDirty || this._isCullFaceDirty || this._isCullDirty;
  14. },
  15. enumerable: true,
  16. configurable: true
  17. });
  18. Object.defineProperty(_DepthCullingState.prototype, "cullFace", {
  19. get: function () {
  20. return this._cullFace;
  21. },
  22. set: function (value) {
  23. if (this._cullFace === value) {
  24. return;
  25. }
  26. this._cullFace = value;
  27. this._isCullFaceDirty = true;
  28. },
  29. enumerable: true,
  30. configurable: true
  31. });
  32. Object.defineProperty(_DepthCullingState.prototype, "cull", {
  33. get: function () {
  34. return this._cull;
  35. },
  36. set: function (value) {
  37. if (this._cull === value) {
  38. return;
  39. }
  40. this._cull = value;
  41. this._isCullDirty = true;
  42. },
  43. enumerable: true,
  44. configurable: true
  45. });
  46. Object.defineProperty(_DepthCullingState.prototype, "depthFunc", {
  47. get: function () {
  48. return this._depthFunc;
  49. },
  50. set: function (value) {
  51. if (this._depthFunc === value) {
  52. return;
  53. }
  54. this._depthFunc = value;
  55. this._isDepthFuncDirty = true;
  56. },
  57. enumerable: true,
  58. configurable: true
  59. });
  60. Object.defineProperty(_DepthCullingState.prototype, "depthMask", {
  61. get: function () {
  62. return this._depthMask;
  63. },
  64. set: function (value) {
  65. if (this._depthMask === value) {
  66. return;
  67. }
  68. this._depthMask = value;
  69. this._isDepthMaskDirty = true;
  70. },
  71. enumerable: true,
  72. configurable: true
  73. });
  74. Object.defineProperty(_DepthCullingState.prototype, "depthTest", {
  75. get: function () {
  76. return this._depthTest;
  77. },
  78. set: function (value) {
  79. if (this._depthTest === value) {
  80. return;
  81. }
  82. this._depthTest = value;
  83. this._isDepthTestDirty = true;
  84. },
  85. enumerable: true,
  86. configurable: true
  87. });
  88. _DepthCullingState.prototype.reset = function () {
  89. this._depthMask = true;
  90. this._depthTest = true;
  91. this._depthFunc = null;
  92. this._cull = null;
  93. this._cullFace = null;
  94. this._isDepthTestDirty = true;
  95. this._isDepthMaskDirty = true;
  96. this._isDepthFuncDirty = false;
  97. this._isCullFaceDirty = false;
  98. this._isCullDirty = false;
  99. };
  100. _DepthCullingState.prototype.apply = function (gl) {
  101. if (!this.isDirty) {
  102. return;
  103. }
  104. // Cull
  105. if (this._isCullDirty) {
  106. if (this.cull) {
  107. gl.enable(gl.CULL_FACE);
  108. }
  109. else {
  110. gl.disable(gl.CULL_FACE);
  111. }
  112. this._isCullDirty = false;
  113. }
  114. // Cull face
  115. if (this._isCullFaceDirty) {
  116. gl.cullFace(this.cullFace);
  117. this._isCullFaceDirty = false;
  118. }
  119. // Depth mask
  120. if (this._isDepthMaskDirty) {
  121. gl.depthMask(this.depthMask);
  122. this._isDepthMaskDirty = false;
  123. }
  124. // Depth test
  125. if (this._isDepthTestDirty) {
  126. if (this.depthTest) {
  127. gl.enable(gl.DEPTH_TEST);
  128. }
  129. else {
  130. gl.disable(gl.DEPTH_TEST);
  131. }
  132. this._isDepthTestDirty = false;
  133. }
  134. // Depth func
  135. if (this._isDepthFuncDirty) {
  136. gl.depthFunc(this.depthFunc);
  137. this._isDepthFuncDirty = false;
  138. }
  139. };
  140. return _DepthCullingState;
  141. })();
  142. BABYLON._DepthCullingState = _DepthCullingState;
  143. var _AlphaState = (function () {
  144. function _AlphaState() {
  145. this._isAlphaBlendDirty = false;
  146. this._isBlendFunctionParametersDirty = false;
  147. this._alphaBlend = false;
  148. this._blendFunctionParameters = new Array(4);
  149. }
  150. Object.defineProperty(_AlphaState.prototype, "isDirty", {
  151. get: function () {
  152. return this._isAlphaBlendDirty || this._isBlendFunctionParametersDirty;
  153. },
  154. enumerable: true,
  155. configurable: true
  156. });
  157. Object.defineProperty(_AlphaState.prototype, "alphaBlend", {
  158. get: function () {
  159. return this._alphaBlend;
  160. },
  161. set: function (value) {
  162. if (this._alphaBlend === value) {
  163. return;
  164. }
  165. this._alphaBlend = value;
  166. this._isAlphaBlendDirty = true;
  167. },
  168. enumerable: true,
  169. configurable: true
  170. });
  171. _AlphaState.prototype.setAlphaBlendFunctionParameters = function (value0, value1, value2, value3) {
  172. if (this._blendFunctionParameters[0] === value0 && this._blendFunctionParameters[1] === value1 && this._blendFunctionParameters[2] === value2 && this._blendFunctionParameters[3] === value3) {
  173. return;
  174. }
  175. this._blendFunctionParameters[0] = value0;
  176. this._blendFunctionParameters[1] = value1;
  177. this._blendFunctionParameters[2] = value2;
  178. this._blendFunctionParameters[3] = value3;
  179. this._isBlendFunctionParametersDirty = true;
  180. };
  181. _AlphaState.prototype.reset = function () {
  182. this._alphaBlend = false;
  183. this._blendFunctionParameters[0] = null;
  184. this._blendFunctionParameters[1] = null;
  185. this._blendFunctionParameters[2] = null;
  186. this._blendFunctionParameters[3] = null;
  187. this._isAlphaBlendDirty = true;
  188. this._isBlendFunctionParametersDirty = false;
  189. };
  190. _AlphaState.prototype.apply = function (gl) {
  191. if (!this.isDirty) {
  192. return;
  193. }
  194. // Alpha blend
  195. if (this._isAlphaBlendDirty) {
  196. if (this._alphaBlend) {
  197. gl.enable(gl.BLEND);
  198. }
  199. else {
  200. gl.disable(gl.BLEND);
  201. }
  202. this._isAlphaBlendDirty = false;
  203. }
  204. // Alpha function
  205. if (this._isBlendFunctionParametersDirty) {
  206. gl.blendFuncSeparate(this._blendFunctionParameters[0], this._blendFunctionParameters[1], this._blendFunctionParameters[2], this._blendFunctionParameters[3]);
  207. this._isBlendFunctionParametersDirty = false;
  208. }
  209. };
  210. return _AlphaState;
  211. })();
  212. BABYLON._AlphaState = _AlphaState;
  213. var compileShader = function (gl, source, type, defines) {
  214. var shader = gl.createShader(type === "vertex" ? gl.VERTEX_SHADER : gl.FRAGMENT_SHADER);
  215. gl.shaderSource(shader, (defines ? defines + "\n" : "") + source);
  216. gl.compileShader(shader);
  217. if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
  218. throw new Error(gl.getShaderInfoLog(shader));
  219. }
  220. return shader;
  221. };
  222. var getWebGLTextureType = function (gl, type) {
  223. var textureType = gl.UNSIGNED_BYTE;
  224. if (type === Engine.TEXTURETYPE_FLOAT)
  225. textureType = gl.FLOAT;
  226. return textureType;
  227. };
  228. var getSamplingParameters = function (samplingMode, generateMipMaps, gl) {
  229. var magFilter = gl.NEAREST;
  230. var minFilter = gl.NEAREST;
  231. if (samplingMode === BABYLON.Texture.BILINEAR_SAMPLINGMODE) {
  232. magFilter = gl.LINEAR;
  233. if (generateMipMaps) {
  234. minFilter = gl.LINEAR_MIPMAP_NEAREST;
  235. }
  236. else {
  237. minFilter = gl.LINEAR;
  238. }
  239. }
  240. else if (samplingMode === BABYLON.Texture.TRILINEAR_SAMPLINGMODE) {
  241. magFilter = gl.LINEAR;
  242. if (generateMipMaps) {
  243. minFilter = gl.LINEAR_MIPMAP_LINEAR;
  244. }
  245. else {
  246. minFilter = gl.LINEAR;
  247. }
  248. }
  249. else if (samplingMode === BABYLON.Texture.NEAREST_SAMPLINGMODE) {
  250. magFilter = gl.NEAREST;
  251. if (generateMipMaps) {
  252. minFilter = gl.NEAREST_MIPMAP_LINEAR;
  253. }
  254. else {
  255. minFilter = gl.NEAREST;
  256. }
  257. }
  258. return {
  259. min: minFilter,
  260. mag: magFilter
  261. };
  262. };
  263. var prepareWebGLTexture = function (texture, gl, scene, width, height, invertY, noMipmap, isCompressed, processFunction, samplingMode) {
  264. if (samplingMode === void 0) { samplingMode = BABYLON.Texture.TRILINEAR_SAMPLINGMODE; }
  265. var engine = scene.getEngine();
  266. var potWidth = BABYLON.Tools.GetExponantOfTwo(width, engine.getCaps().maxTextureSize);
  267. var potHeight = BABYLON.Tools.GetExponantOfTwo(height, engine.getCaps().maxTextureSize);
  268. gl.bindTexture(gl.TEXTURE_2D, texture);
  269. gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, invertY === undefined ? 1 : (invertY ? 1 : 0));
  270. processFunction(potWidth, potHeight);
  271. var filters = getSamplingParameters(samplingMode, !noMipmap, gl);
  272. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, filters.mag);
  273. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, filters.min);
  274. if (!noMipmap && !isCompressed) {
  275. gl.generateMipmap(gl.TEXTURE_2D);
  276. }
  277. gl.bindTexture(gl.TEXTURE_2D, null);
  278. engine._activeTexturesCache = [];
  279. texture._baseWidth = width;
  280. texture._baseHeight = height;
  281. texture._width = potWidth;
  282. texture._height = potHeight;
  283. texture.isReady = true;
  284. texture.samplingMode = samplingMode;
  285. scene._removePendingData(texture);
  286. };
  287. var partialLoad = function (url, index, loadedImages, scene, onfinish) {
  288. var onload = function () {
  289. loadedImages[index] = img;
  290. loadedImages._internalCount++;
  291. scene._removePendingData(img);
  292. if (loadedImages._internalCount === 6) {
  293. onfinish(loadedImages);
  294. }
  295. };
  296. var onerror = function () {
  297. scene._removePendingData(img);
  298. };
  299. var img = BABYLON.Tools.LoadImage(url, onload, onerror, scene.database);
  300. scene._addPendingData(img);
  301. };
  302. var cascadeLoad = function (rootUrl, scene, onfinish, extensions) {
  303. var loadedImages = [];
  304. loadedImages._internalCount = 0;
  305. for (var index = 0; index < 6; index++) {
  306. partialLoad(rootUrl + extensions[index], index, loadedImages, scene, onfinish);
  307. }
  308. };
  309. var EngineCapabilities = (function () {
  310. function EngineCapabilities() {
  311. }
  312. return EngineCapabilities;
  313. })();
  314. BABYLON.EngineCapabilities = EngineCapabilities;
  315. /**
  316. * The engine class is responsible for interfacing with all lower-level APIs such as WebGL and Audio.
  317. */
  318. var Engine = (function () {
  319. /**
  320. * @constructor
  321. * @param {HTMLCanvasElement} canvas - the canvas to be used for rendering
  322. * @param {boolean} [antialias] - enable antialias
  323. * @param options - further options to be sent to the getContext function
  324. */
  325. function Engine(canvas, antialias, options) {
  326. var _this = this;
  327. // Public members
  328. this.isFullscreen = false;
  329. this.isPointerLock = false;
  330. this.cullBackFaces = true;
  331. this.renderEvenInBackground = true;
  332. this.scenes = new Array();
  333. this._windowIsBackground = false;
  334. this._loadingDivBackgroundColor = "black";
  335. this._drawCalls = 0;
  336. this._renderingQueueLaunched = false;
  337. this._activeRenderLoops = [];
  338. // FPS
  339. this.fpsRange = 60;
  340. this.previousFramesDuration = [];
  341. this.fps = 60;
  342. this.deltaTime = 0;
  343. // States
  344. this._depthCullingState = new _DepthCullingState();
  345. this._alphaState = new _AlphaState();
  346. this._alphaMode = Engine.ALPHA_DISABLE;
  347. // Cache
  348. this._loadedTexturesCache = new Array();
  349. this._activeTexturesCache = new Array();
  350. this._compiledEffects = {};
  351. this._uintIndicesCurrentlySet = false;
  352. this._renderingCanvas = canvas;
  353. this._canvasClientRect = this._renderingCanvas.getBoundingClientRect();
  354. options = options || {};
  355. options.antialias = antialias;
  356. try {
  357. this._gl = canvas.getContext("webgl", options) || canvas.getContext("experimental-webgl", options);
  358. }
  359. catch (e) {
  360. throw new Error("WebGL not supported");
  361. }
  362. if (!this._gl) {
  363. throw new Error("WebGL not supported");
  364. }
  365. this._onBlur = function () {
  366. _this._windowIsBackground = true;
  367. };
  368. this._onFocus = function () {
  369. _this._windowIsBackground = false;
  370. };
  371. window.addEventListener("blur", this._onBlur);
  372. window.addEventListener("focus", this._onFocus);
  373. // Textures
  374. this._workingCanvas = document.createElement("canvas");
  375. this._workingContext = this._workingCanvas.getContext("2d");
  376. // Viewport
  377. this._hardwareScalingLevel = 1.0 / (window.devicePixelRatio || 1.0);
  378. this.resize();
  379. // Caps
  380. this._caps = new EngineCapabilities();
  381. this._caps.maxTexturesImageUnits = this._gl.getParameter(this._gl.MAX_TEXTURE_IMAGE_UNITS);
  382. this._caps.maxTextureSize = this._gl.getParameter(this._gl.MAX_TEXTURE_SIZE);
  383. this._caps.maxCubemapTextureSize = this._gl.getParameter(this._gl.MAX_CUBE_MAP_TEXTURE_SIZE);
  384. this._caps.maxRenderTextureSize = this._gl.getParameter(this._gl.MAX_RENDERBUFFER_SIZE);
  385. // Infos
  386. this._glVersion = this._gl.getParameter(this._gl.VERSION);
  387. var rendererInfo = this._gl.getExtension("WEBGL_debug_renderer_info");
  388. if (rendererInfo != null) {
  389. this._glRenderer = this._gl.getParameter(rendererInfo.UNMASKED_RENDERER_WEBGL);
  390. this._glVendor = this._gl.getParameter(rendererInfo.UNMASKED_VENDOR_WEBGL);
  391. }
  392. if (!this._glVendor) {
  393. this._glVendor = "Unknown vendor";
  394. }
  395. if (!this._glRenderer) {
  396. this._glRenderer = "Unknown renderer";
  397. }
  398. // Extensions
  399. this._caps.standardDerivatives = (this._gl.getExtension('OES_standard_derivatives') !== null);
  400. this._caps.s3tc = this._gl.getExtension('WEBGL_compressed_texture_s3tc');
  401. this._caps.textureFloat = (this._gl.getExtension('OES_texture_float') !== null);
  402. this._caps.textureAnisotropicFilterExtension = this._gl.getExtension('EXT_texture_filter_anisotropic') || this._gl.getExtension('WEBKIT_EXT_texture_filter_anisotropic') || this._gl.getExtension('MOZ_EXT_texture_filter_anisotropic');
  403. this._caps.maxAnisotropy = this._caps.textureAnisotropicFilterExtension ? this._gl.getParameter(this._caps.textureAnisotropicFilterExtension.MAX_TEXTURE_MAX_ANISOTROPY_EXT) : 0;
  404. this._caps.instancedArrays = this._gl.getExtension('ANGLE_instanced_arrays');
  405. this._caps.uintIndices = this._gl.getExtension('OES_element_index_uint') !== null;
  406. // Depth buffer
  407. this.setDepthBuffer(true);
  408. this.setDepthFunctionToLessOrEqual();
  409. this.setDepthWrite(true);
  410. // Fullscreen
  411. this._onFullscreenChange = function () {
  412. if (document.fullscreen !== undefined) {
  413. _this.isFullscreen = document.fullscreen;
  414. }
  415. else if (document.mozFullScreen !== undefined) {
  416. _this.isFullscreen = document.mozFullScreen;
  417. }
  418. else if (document.webkitIsFullScreen !== undefined) {
  419. _this.isFullscreen = document.webkitIsFullScreen;
  420. }
  421. else if (document.msIsFullScreen !== undefined) {
  422. _this.isFullscreen = document.msIsFullScreen;
  423. }
  424. // Pointer lock
  425. if (_this.isFullscreen && _this._pointerLockRequested) {
  426. canvas.requestPointerLock = canvas.requestPointerLock || canvas.msRequestPointerLock || canvas.mozRequestPointerLock || canvas.webkitRequestPointerLock;
  427. if (canvas.requestPointerLock) {
  428. canvas.requestPointerLock();
  429. }
  430. }
  431. };
  432. document.addEventListener("fullscreenchange", this._onFullscreenChange, false);
  433. document.addEventListener("mozfullscreenchange", this._onFullscreenChange, false);
  434. document.addEventListener("webkitfullscreenchange", this._onFullscreenChange, false);
  435. document.addEventListener("msfullscreenchange", this._onFullscreenChange, false);
  436. // Pointer lock
  437. this._onPointerLockChange = function () {
  438. _this.isPointerLock = (document.mozPointerLockElement === canvas || document.webkitPointerLockElement === canvas || document.msPointerLockElement === canvas || document.pointerLockElement === canvas);
  439. };
  440. document.addEventListener("pointerlockchange", this._onPointerLockChange, false);
  441. document.addEventListener("mspointerlockchange", this._onPointerLockChange, false);
  442. document.addEventListener("mozpointerlockchange", this._onPointerLockChange, false);
  443. document.addEventListener("webkitpointerlockchange", this._onPointerLockChange, false);
  444. if (!Engine.audioEngine) {
  445. Engine.audioEngine = new BABYLON.AudioEngine();
  446. }
  447. BABYLON.Tools.Log("Babylon.js engine (v" + Engine.Version + ") launched");
  448. }
  449. Object.defineProperty(Engine, "ALPHA_DISABLE", {
  450. get: function () {
  451. return Engine._ALPHA_DISABLE;
  452. },
  453. enumerable: true,
  454. configurable: true
  455. });
  456. Object.defineProperty(Engine, "ALPHA_ADD", {
  457. get: function () {
  458. return Engine._ALPHA_ADD;
  459. },
  460. enumerable: true,
  461. configurable: true
  462. });
  463. Object.defineProperty(Engine, "ALPHA_COMBINE", {
  464. get: function () {
  465. return Engine._ALPHA_COMBINE;
  466. },
  467. enumerable: true,
  468. configurable: true
  469. });
  470. Object.defineProperty(Engine, "DELAYLOADSTATE_NONE", {
  471. get: function () {
  472. return Engine._DELAYLOADSTATE_NONE;
  473. },
  474. enumerable: true,
  475. configurable: true
  476. });
  477. Object.defineProperty(Engine, "DELAYLOADSTATE_LOADED", {
  478. get: function () {
  479. return Engine._DELAYLOADSTATE_LOADED;
  480. },
  481. enumerable: true,
  482. configurable: true
  483. });
  484. Object.defineProperty(Engine, "DELAYLOADSTATE_LOADING", {
  485. get: function () {
  486. return Engine._DELAYLOADSTATE_LOADING;
  487. },
  488. enumerable: true,
  489. configurable: true
  490. });
  491. Object.defineProperty(Engine, "DELAYLOADSTATE_NOTLOADED", {
  492. get: function () {
  493. return Engine._DELAYLOADSTATE_NOTLOADED;
  494. },
  495. enumerable: true,
  496. configurable: true
  497. });
  498. Object.defineProperty(Engine, "TEXTUREFORMAT_ALPHA", {
  499. get: function () {
  500. return Engine._TEXTUREFORMAT_ALPHA;
  501. },
  502. enumerable: true,
  503. configurable: true
  504. });
  505. Object.defineProperty(Engine, "TEXTUREFORMAT_LUMINANCE", {
  506. get: function () {
  507. return Engine._TEXTUREFORMAT_LUMINANCE;
  508. },
  509. enumerable: true,
  510. configurable: true
  511. });
  512. Object.defineProperty(Engine, "TEXTUREFORMAT_LUMINANCE_ALPHA", {
  513. get: function () {
  514. return Engine._TEXTUREFORMAT_LUMINANCE_ALPHA;
  515. },
  516. enumerable: true,
  517. configurable: true
  518. });
  519. Object.defineProperty(Engine, "TEXTUREFORMAT_RGB", {
  520. get: function () {
  521. return Engine._TEXTUREFORMAT_RGB;
  522. },
  523. enumerable: true,
  524. configurable: true
  525. });
  526. Object.defineProperty(Engine, "TEXTUREFORMAT_RGBA", {
  527. get: function () {
  528. return Engine._TEXTUREFORMAT_RGBA;
  529. },
  530. enumerable: true,
  531. configurable: true
  532. });
  533. Object.defineProperty(Engine, "TEXTURETYPE_UNSIGNED_INT", {
  534. get: function () {
  535. return Engine._TEXTURETYPE_UNSIGNED_INT;
  536. },
  537. enumerable: true,
  538. configurable: true
  539. });
  540. Object.defineProperty(Engine, "TEXTURETYPE_FLOAT", {
  541. get: function () {
  542. return Engine._TEXTURETYPE_FLOAT;
  543. },
  544. enumerable: true,
  545. configurable: true
  546. });
  547. Object.defineProperty(Engine, "Version", {
  548. get: function () {
  549. return "2.0.0";
  550. },
  551. enumerable: true,
  552. configurable: true
  553. });
  554. Engine.prototype.getGlInfo = function () {
  555. return {
  556. vendor: this._glVendor,
  557. renderer: this._glRenderer,
  558. version: this._glVersion
  559. };
  560. };
  561. Engine.prototype.getAspectRatio = function (camera) {
  562. var viewport = camera.viewport;
  563. return (this.getRenderWidth() * viewport.width) / (this.getRenderHeight() * viewport.height);
  564. };
  565. Engine.prototype.getRenderWidth = function () {
  566. if (this._currentRenderTarget) {
  567. return this._currentRenderTarget._width;
  568. }
  569. return this._renderingCanvas.width;
  570. };
  571. Engine.prototype.getRenderHeight = function () {
  572. if (this._currentRenderTarget) {
  573. return this._currentRenderTarget._height;
  574. }
  575. return this._renderingCanvas.height;
  576. };
  577. Engine.prototype.getRenderingCanvas = function () {
  578. return this._renderingCanvas;
  579. };
  580. Engine.prototype.getRenderingCanvasClientRect = function () {
  581. return this._renderingCanvas.getBoundingClientRect();
  582. };
  583. Engine.prototype.setHardwareScalingLevel = function (level) {
  584. this._hardwareScalingLevel = level;
  585. this.resize();
  586. };
  587. Engine.prototype.getHardwareScalingLevel = function () {
  588. return this._hardwareScalingLevel;
  589. };
  590. Engine.prototype.getLoadedTexturesCache = function () {
  591. return this._loadedTexturesCache;
  592. };
  593. Engine.prototype.getCaps = function () {
  594. return this._caps;
  595. };
  596. Object.defineProperty(Engine.prototype, "drawCalls", {
  597. get: function () {
  598. return this._drawCalls;
  599. },
  600. enumerable: true,
  601. configurable: true
  602. });
  603. // Methods
  604. Engine.prototype.resetDrawCalls = function () {
  605. this._drawCalls = 0;
  606. };
  607. Engine.prototype.setDepthFunctionToGreater = function () {
  608. this._depthCullingState.depthFunc = this._gl.GREATER;
  609. };
  610. Engine.prototype.setDepthFunctionToGreaterOrEqual = function () {
  611. this._depthCullingState.depthFunc = this._gl.GEQUAL;
  612. };
  613. Engine.prototype.setDepthFunctionToLess = function () {
  614. this._depthCullingState.depthFunc = this._gl.LESS;
  615. };
  616. Engine.prototype.setDepthFunctionToLessOrEqual = function () {
  617. this._depthCullingState.depthFunc = this._gl.LEQUAL;
  618. };
  619. /**
  620. * stop executing a render loop function and remove it from the execution array
  621. * @param {Function} [renderFunction] the function to be removed. If not provided all functions will be removed.
  622. */
  623. Engine.prototype.stopRenderLoop = function (renderFunction) {
  624. if (!renderFunction) {
  625. this._activeRenderLoops = [];
  626. return;
  627. }
  628. var index = this._activeRenderLoops.indexOf(renderFunction);
  629. if (index >= 0) {
  630. this._activeRenderLoops.splice(index, 1);
  631. }
  632. };
  633. Engine.prototype._renderLoop = function () {
  634. var _this = this;
  635. var shouldRender = true;
  636. if (!this.renderEvenInBackground && this._windowIsBackground) {
  637. shouldRender = false;
  638. }
  639. if (shouldRender) {
  640. // Start new frame
  641. this.beginFrame();
  642. for (var index = 0; index < this._activeRenderLoops.length; index++) {
  643. var renderFunction = this._activeRenderLoops[index];
  644. renderFunction();
  645. }
  646. // Present
  647. this.endFrame();
  648. }
  649. if (this._activeRenderLoops.length > 0) {
  650. // Register new frame
  651. BABYLON.Tools.QueueNewFrame(function () {
  652. _this._renderLoop();
  653. });
  654. }
  655. else {
  656. this._renderingQueueLaunched = false;
  657. }
  658. };
  659. /**
  660. * Register and execute a render loop. The engine can have more than one render function.
  661. * @param {Function} renderFunction - the function to continuesly execute starting the next render loop.
  662. * @example
  663. * engine.runRenderLoop(function () {
  664. * scene.render()
  665. * })
  666. */
  667. Engine.prototype.runRenderLoop = function (renderFunction) {
  668. var _this = this;
  669. if (this._activeRenderLoops.indexOf(renderFunction) !== -1) {
  670. return;
  671. }
  672. this._activeRenderLoops.push(renderFunction);
  673. if (!this._renderingQueueLaunched) {
  674. this._renderingQueueLaunched = true;
  675. BABYLON.Tools.QueueNewFrame(function () {
  676. _this._renderLoop();
  677. });
  678. }
  679. };
  680. /**
  681. * Toggle full screen mode.
  682. * @param {boolean} requestPointerLock - should a pointer lock be requested from the user
  683. */
  684. Engine.prototype.switchFullscreen = function (requestPointerLock) {
  685. if (this.isFullscreen) {
  686. BABYLON.Tools.ExitFullscreen();
  687. }
  688. else {
  689. this._pointerLockRequested = requestPointerLock;
  690. BABYLON.Tools.RequestFullscreen(this._renderingCanvas);
  691. }
  692. };
  693. Engine.prototype.clear = function (color, backBuffer, depthStencil) {
  694. this.applyStates();
  695. this._gl.clearColor(color.r, color.g, color.b, color.a !== undefined ? color.a : 1.0);
  696. if (this._depthCullingState.depthMask) {
  697. this._gl.clearDepth(1.0);
  698. }
  699. var mode = 0;
  700. if (backBuffer)
  701. mode |= this._gl.COLOR_BUFFER_BIT;
  702. if (depthStencil && this._depthCullingState.depthMask)
  703. mode |= this._gl.DEPTH_BUFFER_BIT;
  704. this._gl.clear(mode);
  705. };
  706. /**
  707. * Set the WebGL's viewport
  708. * @param {BABYLON.Viewport} viewport - the viewport element to be used.
  709. * @param {number} [requiredWidth] - the width required for rendering. If not provided the rendering canvas' width is used.
  710. * @param {number} [requiredHeight] - the height required for rendering. If not provided the rendering canvas' height is used.
  711. */
  712. Engine.prototype.setViewport = function (viewport, requiredWidth, requiredHeight) {
  713. var width = requiredWidth || this._renderingCanvas.width;
  714. var height = requiredHeight || this._renderingCanvas.height;
  715. var x = viewport.x || 0;
  716. var y = viewport.y || 0;
  717. this._cachedViewport = viewport;
  718. this._gl.viewport(x * width, y * height, width * viewport.width, height * viewport.height);
  719. };
  720. Engine.prototype.setDirectViewport = function (x, y, width, height) {
  721. this._cachedViewport = null;
  722. this._gl.viewport(x, y, width, height);
  723. };
  724. Engine.prototype.beginFrame = function () {
  725. this._measureFps();
  726. };
  727. Engine.prototype.endFrame = function () {
  728. //this.flushFramebuffer();
  729. };
  730. /**
  731. * resize the view according to the canvas' size.
  732. * @example
  733. * window.addEventListener("resize", function () {
  734. * engine.resize();
  735. * });
  736. */
  737. Engine.prototype.resize = function () {
  738. this.setSize(this._renderingCanvas.clientWidth / this._hardwareScalingLevel, this._renderingCanvas.clientHeight / this._hardwareScalingLevel);
  739. };
  740. /**
  741. * force a specific size of the canvas
  742. * @param {number} width - the new canvas' width
  743. * @param {number} height - the new canvas' height
  744. */
  745. Engine.prototype.setSize = function (width, height) {
  746. this._renderingCanvas.width = width;
  747. this._renderingCanvas.height = height;
  748. this._canvasClientRect = this._renderingCanvas.getBoundingClientRect();
  749. };
  750. Engine.prototype.bindFramebuffer = function (texture) {
  751. this._currentRenderTarget = texture;
  752. var gl = this._gl;
  753. gl.bindFramebuffer(gl.FRAMEBUFFER, texture._framebuffer);
  754. this._gl.viewport(0, 0, texture._width, texture._height);
  755. this.wipeCaches();
  756. };
  757. Engine.prototype.unBindFramebuffer = function (texture) {
  758. this._currentRenderTarget = null;
  759. if (texture.generateMipMaps) {
  760. var gl = this._gl;
  761. gl.bindTexture(gl.TEXTURE_2D, texture);
  762. gl.generateMipmap(gl.TEXTURE_2D);
  763. gl.bindTexture(gl.TEXTURE_2D, null);
  764. }
  765. this._gl.bindFramebuffer(this._gl.FRAMEBUFFER, null);
  766. };
  767. Engine.prototype.flushFramebuffer = function () {
  768. this._gl.flush();
  769. };
  770. Engine.prototype.restoreDefaultFramebuffer = function () {
  771. this._currentRenderTarget = null;
  772. this._gl.bindFramebuffer(this._gl.FRAMEBUFFER, null);
  773. this.setViewport(this._cachedViewport);
  774. this.wipeCaches();
  775. };
  776. // VBOs
  777. Engine.prototype._resetVertexBufferBinding = function () {
  778. this._gl.bindBuffer(this._gl.ARRAY_BUFFER, null);
  779. this._cachedVertexBuffers = null;
  780. };
  781. Engine.prototype.createVertexBuffer = function (vertices) {
  782. var vbo = this._gl.createBuffer();
  783. this._gl.bindBuffer(this._gl.ARRAY_BUFFER, vbo);
  784. this._gl.bufferData(this._gl.ARRAY_BUFFER, new Float32Array(vertices), this._gl.STATIC_DRAW);
  785. this._resetVertexBufferBinding();
  786. vbo.references = 1;
  787. return vbo;
  788. };
  789. Engine.prototype.createDynamicVertexBuffer = function (capacity) {
  790. var vbo = this._gl.createBuffer();
  791. this._gl.bindBuffer(this._gl.ARRAY_BUFFER, vbo);
  792. this._gl.bufferData(this._gl.ARRAY_BUFFER, capacity, this._gl.DYNAMIC_DRAW);
  793. this._resetVertexBufferBinding();
  794. vbo.references = 1;
  795. return vbo;
  796. };
  797. Engine.prototype.updateDynamicVertexBuffer = function (vertexBuffer, vertices, offset) {
  798. this._gl.bindBuffer(this._gl.ARRAY_BUFFER, vertexBuffer);
  799. if (offset === undefined) {
  800. offset = 0;
  801. }
  802. if (vertices instanceof Float32Array) {
  803. this._gl.bufferSubData(this._gl.ARRAY_BUFFER, offset, vertices);
  804. }
  805. else {
  806. this._gl.bufferSubData(this._gl.ARRAY_BUFFER, offset, new Float32Array(vertices));
  807. }
  808. this._resetVertexBufferBinding();
  809. };
  810. Engine.prototype._resetIndexBufferBinding = function () {
  811. this._gl.bindBuffer(this._gl.ELEMENT_ARRAY_BUFFER, null);
  812. this._cachedIndexBuffer = null;
  813. };
  814. Engine.prototype.createIndexBuffer = function (indices) {
  815. var vbo = this._gl.createBuffer();
  816. this._gl.bindBuffer(this._gl.ELEMENT_ARRAY_BUFFER, vbo);
  817. // Check for 32 bits indices
  818. var arrayBuffer;
  819. var need32Bits = false;
  820. if (this._caps.uintIndices) {
  821. for (var index = 0; index < indices.length; index++) {
  822. if (indices[index] > 65535) {
  823. need32Bits = true;
  824. break;
  825. }
  826. }
  827. arrayBuffer = need32Bits ? new Uint32Array(indices) : new Uint16Array(indices);
  828. }
  829. else {
  830. arrayBuffer = new Uint16Array(indices);
  831. }
  832. this._gl.bufferData(this._gl.ELEMENT_ARRAY_BUFFER, arrayBuffer, this._gl.STATIC_DRAW);
  833. this._resetIndexBufferBinding();
  834. vbo.references = 1;
  835. vbo.is32Bits = need32Bits;
  836. return vbo;
  837. };
  838. Engine.prototype.bindBuffers = function (vertexBuffer, indexBuffer, vertexDeclaration, vertexStrideSize, effect) {
  839. if (this._cachedVertexBuffers !== vertexBuffer || this._cachedEffectForVertexBuffers !== effect) {
  840. this._cachedVertexBuffers = vertexBuffer;
  841. this._cachedEffectForVertexBuffers = effect;
  842. this._gl.bindBuffer(this._gl.ARRAY_BUFFER, vertexBuffer);
  843. var offset = 0;
  844. for (var index = 0; index < vertexDeclaration.length; index++) {
  845. var order = effect.getAttributeLocation(index);
  846. if (order >= 0) {
  847. this._gl.vertexAttribPointer(order, vertexDeclaration[index], this._gl.FLOAT, false, vertexStrideSize, offset);
  848. }
  849. offset += vertexDeclaration[index] * 4;
  850. }
  851. }
  852. if (this._cachedIndexBuffer !== indexBuffer) {
  853. this._cachedIndexBuffer = indexBuffer;
  854. this._gl.bindBuffer(this._gl.ELEMENT_ARRAY_BUFFER, indexBuffer);
  855. this._uintIndicesCurrentlySet = indexBuffer.is32Bits;
  856. }
  857. };
  858. Engine.prototype.bindMultiBuffers = function (vertexBuffers, indexBuffer, effect) {
  859. if (this._cachedVertexBuffers !== vertexBuffers || this._cachedEffectForVertexBuffers !== effect) {
  860. this._cachedVertexBuffers = vertexBuffers;
  861. this._cachedEffectForVertexBuffers = effect;
  862. var attributes = effect.getAttributesNames();
  863. for (var index = 0; index < attributes.length; index++) {
  864. var order = effect.getAttributeLocation(index);
  865. if (order >= 0) {
  866. var vertexBuffer = vertexBuffers[attributes[index]];
  867. if (!vertexBuffer) {
  868. continue;
  869. }
  870. var stride = vertexBuffer.getStrideSize();
  871. this._gl.bindBuffer(this._gl.ARRAY_BUFFER, vertexBuffer.getBuffer());
  872. this._gl.vertexAttribPointer(order, stride, this._gl.FLOAT, false, stride * 4, 0);
  873. }
  874. }
  875. }
  876. if (indexBuffer != null && this._cachedIndexBuffer !== indexBuffer) {
  877. this._cachedIndexBuffer = indexBuffer;
  878. this._gl.bindBuffer(this._gl.ELEMENT_ARRAY_BUFFER, indexBuffer);
  879. this._uintIndicesCurrentlySet = indexBuffer.is32Bits;
  880. }
  881. };
  882. Engine.prototype._releaseBuffer = function (buffer) {
  883. buffer.references--;
  884. if (buffer.references === 0) {
  885. this._gl.deleteBuffer(buffer);
  886. return true;
  887. }
  888. return false;
  889. };
  890. Engine.prototype.createInstancesBuffer = function (capacity) {
  891. var buffer = this._gl.createBuffer();
  892. buffer.capacity = capacity;
  893. this._gl.bindBuffer(this._gl.ARRAY_BUFFER, buffer);
  894. this._gl.bufferData(this._gl.ARRAY_BUFFER, capacity, this._gl.DYNAMIC_DRAW);
  895. return buffer;
  896. };
  897. Engine.prototype.deleteInstancesBuffer = function (buffer) {
  898. this._gl.deleteBuffer(buffer);
  899. };
  900. Engine.prototype.updateAndBindInstancesBuffer = function (instancesBuffer, data, offsetLocations) {
  901. this._gl.bindBuffer(this._gl.ARRAY_BUFFER, instancesBuffer);
  902. this._gl.bufferSubData(this._gl.ARRAY_BUFFER, 0, data);
  903. for (var index = 0; index < 4; index++) {
  904. var offsetLocation = offsetLocations[index];
  905. this._gl.enableVertexAttribArray(offsetLocation);
  906. this._gl.vertexAttribPointer(offsetLocation, 4, this._gl.FLOAT, false, 64, index * 16);
  907. this._caps.instancedArrays.vertexAttribDivisorANGLE(offsetLocation, 1);
  908. }
  909. };
  910. Engine.prototype.unBindInstancesBuffer = function (instancesBuffer, offsetLocations) {
  911. this._gl.bindBuffer(this._gl.ARRAY_BUFFER, instancesBuffer);
  912. for (var index = 0; index < 4; index++) {
  913. var offsetLocation = offsetLocations[index];
  914. this._gl.disableVertexAttribArray(offsetLocation);
  915. this._caps.instancedArrays.vertexAttribDivisorANGLE(offsetLocation, 0);
  916. }
  917. };
  918. Engine.prototype.applyStates = function () {
  919. this._depthCullingState.apply(this._gl);
  920. this._alphaState.apply(this._gl);
  921. };
  922. Engine.prototype.draw = function (useTriangles, indexStart, indexCount, instancesCount) {
  923. // Apply states
  924. this.applyStates();
  925. this._drawCalls++;
  926. // Render
  927. var indexFormat = this._uintIndicesCurrentlySet ? this._gl.UNSIGNED_INT : this._gl.UNSIGNED_SHORT;
  928. if (instancesCount) {
  929. this._caps.instancedArrays.drawElementsInstancedANGLE(useTriangles ? this._gl.TRIANGLES : this._gl.LINES, indexCount, indexFormat, indexStart * 2, instancesCount);
  930. return;
  931. }
  932. this._gl.drawElements(useTriangles ? this._gl.TRIANGLES : this._gl.LINES, indexCount, indexFormat, indexStart * 2);
  933. };
  934. Engine.prototype.drawPointClouds = function (verticesStart, verticesCount, instancesCount) {
  935. // Apply states
  936. this.applyStates();
  937. this._drawCalls++;
  938. if (instancesCount) {
  939. this._caps.instancedArrays.drawArraysInstancedANGLE(this._gl.POINTS, verticesStart, verticesCount, instancesCount);
  940. return;
  941. }
  942. this._gl.drawArrays(this._gl.POINTS, verticesStart, verticesCount);
  943. };
  944. // Shaders
  945. Engine.prototype._releaseEffect = function (effect) {
  946. if (this._compiledEffects[effect._key]) {
  947. delete this._compiledEffects[effect._key];
  948. if (effect.getProgram()) {
  949. this._gl.deleteProgram(effect.getProgram());
  950. }
  951. }
  952. };
  953. Engine.prototype.createEffect = function (baseName, attributesNames, uniformsNames, samplers, defines, fallbacks, onCompiled, onError) {
  954. var vertex = baseName.vertexElement || baseName.vertex || baseName;
  955. var fragment = baseName.fragmentElement || baseName.fragment || baseName;
  956. var name = vertex + "+" + fragment + "@" + defines;
  957. if (this._compiledEffects[name]) {
  958. return this._compiledEffects[name];
  959. }
  960. var effect = new BABYLON.Effect(baseName, attributesNames, uniformsNames, samplers, this, defines, fallbacks, onCompiled, onError);
  961. effect._key = name;
  962. this._compiledEffects[name] = effect;
  963. return effect;
  964. };
  965. Engine.prototype.createEffectForParticles = function (fragmentName, uniformsNames, samplers, defines, fallbacks, onCompiled, onError) {
  966. if (uniformsNames === void 0) { uniformsNames = []; }
  967. if (samplers === void 0) { samplers = []; }
  968. if (defines === void 0) { defines = ""; }
  969. return this.createEffect({
  970. vertex: "particles",
  971. fragmentElement: fragmentName
  972. }, ["position", "color", "options"], ["view", "projection"].concat(uniformsNames), ["diffuseSampler"].concat(samplers), defines, fallbacks, onCompiled, onError);
  973. };
  974. Engine.prototype.createShaderProgram = function (vertexCode, fragmentCode, defines) {
  975. var vertexShader = compileShader(this._gl, vertexCode, "vertex", defines);
  976. var fragmentShader = compileShader(this._gl, fragmentCode, "fragment", defines);
  977. var shaderProgram = this._gl.createProgram();
  978. this._gl.attachShader(shaderProgram, vertexShader);
  979. this._gl.attachShader(shaderProgram, fragmentShader);
  980. this._gl.linkProgram(shaderProgram);
  981. var linked = this._gl.getProgramParameter(shaderProgram, this._gl.LINK_STATUS);
  982. if (!linked) {
  983. var error = this._gl.getProgramInfoLog(shaderProgram);
  984. if (error) {
  985. throw new Error(error);
  986. }
  987. }
  988. this._gl.deleteShader(vertexShader);
  989. this._gl.deleteShader(fragmentShader);
  990. return shaderProgram;
  991. };
  992. Engine.prototype.getUniforms = function (shaderProgram, uniformsNames) {
  993. var results = [];
  994. for (var index = 0; index < uniformsNames.length; index++) {
  995. results.push(this._gl.getUniformLocation(shaderProgram, uniformsNames[index]));
  996. }
  997. return results;
  998. };
  999. Engine.prototype.getAttributes = function (shaderProgram, attributesNames) {
  1000. var results = [];
  1001. for (var index = 0; index < attributesNames.length; index++) {
  1002. try {
  1003. results.push(this._gl.getAttribLocation(shaderProgram, attributesNames[index]));
  1004. }
  1005. catch (e) {
  1006. results.push(-1);
  1007. }
  1008. }
  1009. return results;
  1010. };
  1011. Engine.prototype.enableEffect = function (effect) {
  1012. if (!effect || !effect.getAttributesCount() || this._currentEffect === effect) {
  1013. if (effect && effect.onBind) {
  1014. effect.onBind(effect);
  1015. }
  1016. return;
  1017. }
  1018. this._vertexAttribArrays = this._vertexAttribArrays || [];
  1019. // Use program
  1020. this._gl.useProgram(effect.getProgram());
  1021. for (var i in this._vertexAttribArrays) {
  1022. if (i > this._gl.VERTEX_ATTRIB_ARRAY_ENABLED || !this._vertexAttribArrays[i]) {
  1023. continue;
  1024. }
  1025. this._vertexAttribArrays[i] = false;
  1026. this._gl.disableVertexAttribArray(i);
  1027. }
  1028. var attributesCount = effect.getAttributesCount();
  1029. for (var index = 0; index < attributesCount; index++) {
  1030. // Attributes
  1031. var order = effect.getAttributeLocation(index);
  1032. if (order >= 0) {
  1033. this._vertexAttribArrays[order] = true;
  1034. this._gl.enableVertexAttribArray(order);
  1035. }
  1036. }
  1037. this._currentEffect = effect;
  1038. if (effect.onBind) {
  1039. effect.onBind(effect);
  1040. }
  1041. };
  1042. Engine.prototype.setArray = function (uniform, array) {
  1043. if (!uniform)
  1044. return;
  1045. this._gl.uniform1fv(uniform, array);
  1046. };
  1047. Engine.prototype.setArray2 = function (uniform, array) {
  1048. if (!uniform || array.length % 2 !== 0)
  1049. return;
  1050. this._gl.uniform2fv(uniform, array);
  1051. };
  1052. Engine.prototype.setArray3 = function (uniform, array) {
  1053. if (!uniform || array.length % 3 !== 0)
  1054. return;
  1055. this._gl.uniform3fv(uniform, array);
  1056. };
  1057. Engine.prototype.setArray4 = function (uniform, array) {
  1058. if (!uniform || array.length % 4 !== 0)
  1059. return;
  1060. this._gl.uniform4fv(uniform, array);
  1061. };
  1062. Engine.prototype.setMatrices = function (uniform, matrices) {
  1063. if (!uniform)
  1064. return;
  1065. this._gl.uniformMatrix4fv(uniform, false, matrices);
  1066. };
  1067. Engine.prototype.setMatrix = function (uniform, matrix) {
  1068. if (!uniform)
  1069. return;
  1070. this._gl.uniformMatrix4fv(uniform, false, matrix.toArray());
  1071. };
  1072. Engine.prototype.setFloat = function (uniform, value) {
  1073. if (!uniform)
  1074. return;
  1075. this._gl.uniform1f(uniform, value);
  1076. };
  1077. Engine.prototype.setFloat2 = function (uniform, x, y) {
  1078. if (!uniform)
  1079. return;
  1080. this._gl.uniform2f(uniform, x, y);
  1081. };
  1082. Engine.prototype.setFloat3 = function (uniform, x, y, z) {
  1083. if (!uniform)
  1084. return;
  1085. this._gl.uniform3f(uniform, x, y, z);
  1086. };
  1087. Engine.prototype.setBool = function (uniform, bool) {
  1088. if (!uniform)
  1089. return;
  1090. this._gl.uniform1i(uniform, bool);
  1091. };
  1092. Engine.prototype.setFloat4 = function (uniform, x, y, z, w) {
  1093. if (!uniform)
  1094. return;
  1095. this._gl.uniform4f(uniform, x, y, z, w);
  1096. };
  1097. Engine.prototype.setColor3 = function (uniform, color3) {
  1098. if (!uniform)
  1099. return;
  1100. this._gl.uniform3f(uniform, color3.r, color3.g, color3.b);
  1101. };
  1102. Engine.prototype.setColor4 = function (uniform, color3, alpha) {
  1103. if (!uniform)
  1104. return;
  1105. this._gl.uniform4f(uniform, color3.r, color3.g, color3.b, alpha);
  1106. };
  1107. // States
  1108. Engine.prototype.setState = function (culling, force) {
  1109. // Culling
  1110. if (this._depthCullingState.cull !== culling || force) {
  1111. if (culling) {
  1112. this._depthCullingState.cullFace = this.cullBackFaces ? this._gl.BACK : this._gl.FRONT;
  1113. this._depthCullingState.cull = true;
  1114. }
  1115. else {
  1116. this._depthCullingState.cull = false;
  1117. }
  1118. }
  1119. };
  1120. Engine.prototype.setDepthBuffer = function (enable) {
  1121. this._depthCullingState.depthTest = enable;
  1122. };
  1123. Engine.prototype.getDepthWrite = function () {
  1124. return this._depthCullingState.depthMask;
  1125. };
  1126. Engine.prototype.setDepthWrite = function (enable) {
  1127. this._depthCullingState.depthMask = enable;
  1128. };
  1129. Engine.prototype.setColorWrite = function (enable) {
  1130. this._gl.colorMask(enable, enable, enable, enable);
  1131. };
  1132. Engine.prototype.setAlphaMode = function (mode) {
  1133. switch (mode) {
  1134. case Engine.ALPHA_DISABLE:
  1135. this.setDepthWrite(true);
  1136. this._alphaState.alphaBlend = false;
  1137. break;
  1138. case Engine.ALPHA_COMBINE:
  1139. this.setDepthWrite(false);
  1140. this._alphaState.setAlphaBlendFunctionParameters(this._gl.SRC_ALPHA, this._gl.ONE_MINUS_SRC_ALPHA, this._gl.ONE, this._gl.ONE);
  1141. this._alphaState.alphaBlend = true;
  1142. break;
  1143. case Engine.ALPHA_ADD:
  1144. this.setDepthWrite(false);
  1145. this._alphaState.setAlphaBlendFunctionParameters(this._gl.ONE, this._gl.ONE, this._gl.ZERO, this._gl.ONE);
  1146. this._alphaState.alphaBlend = true;
  1147. break;
  1148. }
  1149. this._alphaMode = mode;
  1150. };
  1151. Engine.prototype.getAlphaMode = function () {
  1152. return this._alphaMode;
  1153. };
  1154. Engine.prototype.setAlphaTesting = function (enable) {
  1155. this._alphaTest = enable;
  1156. };
  1157. Engine.prototype.getAlphaTesting = function () {
  1158. return this._alphaTest;
  1159. };
  1160. // Textures
  1161. Engine.prototype.wipeCaches = function () {
  1162. this._activeTexturesCache = [];
  1163. this._currentEffect = null;
  1164. this._depthCullingState.reset();
  1165. this._alphaState.reset();
  1166. this._cachedVertexBuffers = null;
  1167. this._cachedIndexBuffer = null;
  1168. this._cachedEffectForVertexBuffers = null;
  1169. };
  1170. Engine.prototype.setSamplingMode = function (texture, samplingMode) {
  1171. var gl = this._gl;
  1172. gl.bindTexture(gl.TEXTURE_2D, texture);
  1173. var magFilter = gl.NEAREST;
  1174. var minFilter = gl.NEAREST;
  1175. if (samplingMode === BABYLON.Texture.BILINEAR_SAMPLINGMODE) {
  1176. magFilter = gl.LINEAR;
  1177. minFilter = gl.LINEAR;
  1178. }
  1179. else if (samplingMode === BABYLON.Texture.TRILINEAR_SAMPLINGMODE) {
  1180. magFilter = gl.LINEAR;
  1181. minFilter = gl.LINEAR_MIPMAP_LINEAR;
  1182. }
  1183. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, magFilter);
  1184. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, minFilter);
  1185. gl.bindTexture(gl.TEXTURE_2D, null);
  1186. texture.samplingMode = samplingMode;
  1187. };
  1188. Engine.prototype.createTexture = function (url, noMipmap, invertY, scene, samplingMode, onLoad, onError, buffer) {
  1189. var _this = this;
  1190. if (samplingMode === void 0) { samplingMode = BABYLON.Texture.TRILINEAR_SAMPLINGMODE; }
  1191. if (onLoad === void 0) { onLoad = null; }
  1192. if (onError === void 0) { onError = null; }
  1193. if (buffer === void 0) { buffer = null; }
  1194. var texture = this._gl.createTexture();
  1195. var extension;
  1196. var fromData = false;
  1197. if (url.substr(0, 5) === "data:") {
  1198. fromData = true;
  1199. }
  1200. if (!fromData)
  1201. extension = url.substr(url.length - 4, 4).toLowerCase();
  1202. else {
  1203. var oldUrl = url;
  1204. fromData = oldUrl.split(':');
  1205. url = oldUrl;
  1206. extension = fromData[1].substr(fromData[1].length - 4, 4).toLowerCase();
  1207. }
  1208. var isDDS = this.getCaps().s3tc && (extension === ".dds");
  1209. var isTGA = (extension === ".tga");
  1210. scene._addPendingData(texture);
  1211. texture.url = url;
  1212. texture.noMipmap = noMipmap;
  1213. texture.references = 1;
  1214. this._loadedTexturesCache.push(texture);
  1215. var onerror = function () {
  1216. scene._removePendingData(texture);
  1217. if (onError) {
  1218. onError();
  1219. }
  1220. };
  1221. if (isTGA) {
  1222. var callback = function (arrayBuffer) {
  1223. var data = new Uint8Array(arrayBuffer);
  1224. var header = BABYLON.Internals.TGATools.GetTGAHeader(data);
  1225. prepareWebGLTexture(texture, _this._gl, scene, header.width, header.height, invertY, noMipmap, false, function () {
  1226. BABYLON.Internals.TGATools.UploadContent(_this._gl, data);
  1227. if (onLoad) {
  1228. onLoad();
  1229. }
  1230. }, samplingMode);
  1231. };
  1232. if (!(fromData instanceof Array))
  1233. BABYLON.Tools.LoadFile(url, function (arrayBuffer) {
  1234. callback(arrayBuffer);
  1235. }, onerror, scene.database, true);
  1236. else
  1237. callback(buffer);
  1238. }
  1239. else if (isDDS) {
  1240. callback = function (data) {
  1241. var info = BABYLON.Internals.DDSTools.GetDDSInfo(data);
  1242. var loadMipmap = (info.isRGB || info.isLuminance || info.mipmapCount > 1) && !noMipmap && ((info.width >> (info.mipmapCount - 1)) === 1);
  1243. prepareWebGLTexture(texture, _this._gl, scene, info.width, info.height, invertY, !loadMipmap, info.isFourCC, function () {
  1244. BABYLON.Internals.DDSTools.UploadDDSLevels(_this._gl, _this.getCaps().s3tc, data, info, loadMipmap, 1);
  1245. if (onLoad) {
  1246. onLoad();
  1247. }
  1248. }, samplingMode);
  1249. };
  1250. if (!(fromData instanceof Array))
  1251. BABYLON.Tools.LoadFile(url, function (data) {
  1252. callback(data);
  1253. }, onerror, scene.database, true);
  1254. else
  1255. callback(buffer);
  1256. }
  1257. else {
  1258. var onload = function (img) {
  1259. prepareWebGLTexture(texture, _this._gl, scene, img.width, img.height, invertY, noMipmap, false, function (potWidth, potHeight) {
  1260. var isPot = (img.width === potWidth && img.height === potHeight);
  1261. if (!isPot) {
  1262. _this._workingCanvas.width = potWidth;
  1263. _this._workingCanvas.height = potHeight;
  1264. if (samplingMode === BABYLON.Texture.NEAREST_SAMPLINGMODE) {
  1265. _this._workingContext.imageSmoothingEnabled = false;
  1266. _this._workingContext.mozImageSmoothingEnabled = false;
  1267. _this._workingContext.oImageSmoothingEnabled = false;
  1268. _this._workingContext.webkitImageSmoothingEnabled = false;
  1269. _this._workingContext.msImageSmoothingEnabled = false;
  1270. }
  1271. _this._workingContext.drawImage(img, 0, 0, img.width, img.height, 0, 0, potWidth, potHeight);
  1272. if (samplingMode === BABYLON.Texture.NEAREST_SAMPLINGMODE) {
  1273. _this._workingContext.imageSmoothingEnabled = true;
  1274. _this._workingContext.mozImageSmoothingEnabled = true;
  1275. _this._workingContext.oImageSmoothingEnabled = true;
  1276. _this._workingContext.webkitImageSmoothingEnabled = true;
  1277. _this._workingContext.msImageSmoothingEnabled = true;
  1278. }
  1279. }
  1280. _this._gl.texImage2D(_this._gl.TEXTURE_2D, 0, _this._gl.RGBA, _this._gl.RGBA, _this._gl.UNSIGNED_BYTE, isPot ? img : _this._workingCanvas);
  1281. if (onLoad) {
  1282. onLoad();
  1283. }
  1284. }, samplingMode);
  1285. };
  1286. if (!(fromData instanceof Array))
  1287. BABYLON.Tools.LoadImage(url, onload, onerror, scene.database);
  1288. else
  1289. BABYLON.Tools.LoadImage(buffer, onload, onerror, scene.database);
  1290. }
  1291. return texture;
  1292. };
  1293. Engine.prototype.createRawTexture = function (data, width, height, format, generateMipMaps, invertY, samplingMode) {
  1294. var texture = this._gl.createTexture();
  1295. this._gl.bindTexture(this._gl.TEXTURE_2D, texture);
  1296. this._gl.pixelStorei(this._gl.UNPACK_FLIP_Y_WEBGL, invertY === undefined ? 1 : (invertY ? 1 : 0));
  1297. // Format
  1298. var internalFormat = this._gl.RGBA;
  1299. switch (format) {
  1300. case Engine.TEXTUREFORMAT_ALPHA:
  1301. internalFormat = this._gl.ALPHA;
  1302. break;
  1303. case Engine.TEXTUREFORMAT_LUMINANCE:
  1304. internalFormat = this._gl.LUMINANCE;
  1305. break;
  1306. case Engine.TEXTUREFORMAT_LUMINANCE_ALPHA:
  1307. internalFormat = this._gl.LUMINANCE_ALPHA;
  1308. break;
  1309. case Engine.TEXTUREFORMAT_RGB:
  1310. internalFormat = this._gl.RGB;
  1311. break;
  1312. case Engine.TEXTUREFORMAT_RGBA:
  1313. internalFormat = this._gl.RGBA;
  1314. break;
  1315. }
  1316. this._gl.texImage2D(this._gl.TEXTURE_2D, 0, internalFormat, width, height, 0, internalFormat, this._gl.UNSIGNED_BYTE, data);
  1317. if (generateMipMaps) {
  1318. this._gl.generateMipmap(this._gl.TEXTURE_2D);
  1319. }
  1320. // Filters
  1321. var filters = getSamplingParameters(samplingMode, generateMipMaps, this._gl);
  1322. this._gl.texParameteri(this._gl.TEXTURE_2D, this._gl.TEXTURE_MAG_FILTER, filters.mag);
  1323. this._gl.texParameteri(this._gl.TEXTURE_2D, this._gl.TEXTURE_MIN_FILTER, filters.min);
  1324. this._gl.bindTexture(this._gl.TEXTURE_2D, null);
  1325. this._activeTexturesCache = [];
  1326. texture._baseWidth = width;
  1327. texture._baseHeight = height;
  1328. texture._width = width;
  1329. texture._height = height;
  1330. texture.isReady = true;
  1331. texture.references = 1;
  1332. texture.samplingMode = samplingMode;
  1333. this._loadedTexturesCache.push(texture);
  1334. return texture;
  1335. };
  1336. Engine.prototype.createDynamicTexture = function (width, height, generateMipMaps, samplingMode) {
  1337. var texture = this._gl.createTexture();
  1338. width = BABYLON.Tools.GetExponantOfTwo(width, this._caps.maxTextureSize);
  1339. height = BABYLON.Tools.GetExponantOfTwo(height, this._caps.maxTextureSize);
  1340. this._gl.bindTexture(this._gl.TEXTURE_2D, texture);
  1341. var filters = getSamplingParameters(samplingMode, generateMipMaps, this._gl);
  1342. this._gl.texParameteri(this._gl.TEXTURE_2D, this._gl.TEXTURE_MAG_FILTER, filters.mag);
  1343. this._gl.texParameteri(this._gl.TEXTURE_2D, this._gl.TEXTURE_MIN_FILTER, filters.min);
  1344. this._gl.bindTexture(this._gl.TEXTURE_2D, null);
  1345. this._activeTexturesCache = [];
  1346. texture._baseWidth = width;
  1347. texture._baseHeight = height;
  1348. texture._width = width;
  1349. texture._height = height;
  1350. texture.isReady = false;
  1351. texture.generateMipMaps = generateMipMaps;
  1352. texture.references = 1;
  1353. texture.samplingMode = samplingMode;
  1354. this._loadedTexturesCache.push(texture);
  1355. return texture;
  1356. };
  1357. Engine.prototype.updateDynamicTexture = function (texture, canvas, invertY) {
  1358. this._gl.bindTexture(this._gl.TEXTURE_2D, texture);
  1359. this._gl.pixelStorei(this._gl.UNPACK_FLIP_Y_WEBGL, invertY ? 1 : 0);
  1360. this._gl.texImage2D(this._gl.TEXTURE_2D, 0, this._gl.RGBA, this._gl.RGBA, this._gl.UNSIGNED_BYTE, canvas);
  1361. if (texture.generateMipMaps) {
  1362. this._gl.generateMipmap(this._gl.TEXTURE_2D);
  1363. }
  1364. this._gl.bindTexture(this._gl.TEXTURE_2D, null);
  1365. this._activeTexturesCache = [];
  1366. texture.isReady = true;
  1367. };
  1368. Engine.prototype.updateVideoTexture = function (texture, video, invertY) {
  1369. this._gl.bindTexture(this._gl.TEXTURE_2D, texture);
  1370. this._gl.pixelStorei(this._gl.UNPACK_FLIP_Y_WEBGL, invertY ? 0 : 1); // Video are upside down by default
  1371. // Scale the video if it is a NPOT using the current working canvas
  1372. if (video.videoWidth !== texture._width || video.videoHeight !== texture._height) {
  1373. if (!texture._workingCanvas) {
  1374. texture._workingCanvas = document.createElement("canvas");
  1375. texture._workingContext = texture._workingCanvas.getContext("2d");
  1376. texture._workingCanvas.width = texture._width;
  1377. texture._workingCanvas.height = texture._height;
  1378. }
  1379. texture._workingContext.drawImage(video, 0, 0, video.videoWidth, video.videoHeight, 0, 0, texture._width, texture._height);
  1380. this._gl.texImage2D(this._gl.TEXTURE_2D, 0, this._gl.RGBA, this._gl.RGBA, this._gl.UNSIGNED_BYTE, texture._workingCanvas);
  1381. }
  1382. else {
  1383. this._gl.texImage2D(this._gl.TEXTURE_2D, 0, this._gl.RGBA, this._gl.RGBA, this._gl.UNSIGNED_BYTE, video);
  1384. }
  1385. if (texture.generateMipMaps) {
  1386. this._gl.generateMipmap(this._gl.TEXTURE_2D);
  1387. }
  1388. this._gl.bindTexture(this._gl.TEXTURE_2D, null);
  1389. this._activeTexturesCache = [];
  1390. texture.isReady = true;
  1391. };
  1392. Engine.prototype.createRenderTargetTexture = function (size, options) {
  1393. // old version had a "generateMipMaps" arg instead of options.
  1394. // if options.generateMipMaps is undefined, consider that options itself if the generateMipmaps value
  1395. // in the same way, generateDepthBuffer is defaulted to true
  1396. var generateMipMaps = false;
  1397. var generateDepthBuffer = true;
  1398. var type = Engine.TEXTURETYPE_UNSIGNED_INT;
  1399. var samplingMode = BABYLON.Texture.TRILINEAR_SAMPLINGMODE;
  1400. if (options !== undefined) {
  1401. generateMipMaps = options.generateMipMaps === undefined ? options : options.generateMipmaps;
  1402. generateDepthBuffer = options.generateDepthBuffer === undefined ? true : options.generateDepthBuffer;
  1403. type = options.type === undefined ? type : options.type;
  1404. if (options.samplingMode !== undefined) {
  1405. samplingMode = options.samplingMode;
  1406. }
  1407. if (type === Engine.TEXTURETYPE_FLOAT) {
  1408. // if floating point (gl.FLOAT) then force to NEAREST_SAMPLINGMODE
  1409. samplingMode = BABYLON.Texture.NEAREST_SAMPLINGMODE;
  1410. }
  1411. }
  1412. var gl = this._gl;
  1413. var texture = gl.createTexture();
  1414. gl.bindTexture(gl.TEXTURE_2D, texture);
  1415. var width = size.width || size;
  1416. var height = size.height || size;
  1417. var filters = getSamplingParameters(samplingMode, generateMipMaps, gl);
  1418. if (type === Engine.TEXTURETYPE_FLOAT && !this._caps.textureFloat) {
  1419. type = Engine.TEXTURETYPE_UNSIGNED_INT;
  1420. BABYLON.Tools.Warn("Float textures are not supported. Render target forced to TEXTURETYPE_UNSIGNED_BYTE type");
  1421. }
  1422. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, filters.mag);
  1423. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, filters.min);
  1424. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
  1425. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
  1426. gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, width, height, 0, gl.RGBA, getWebGLTextureType(gl, type), null);
  1427. var depthBuffer;
  1428. // Create the depth buffer
  1429. if (generateDepthBuffer) {
  1430. depthBuffer = gl.createRenderbuffer();
  1431. gl.bindRenderbuffer(gl.RENDERBUFFER, depthBuffer);
  1432. gl.renderbufferStorage(gl.RENDERBUFFER, gl.DEPTH_COMPONENT16, width, height);
  1433. }
  1434. // Create the framebuffer
  1435. var framebuffer = gl.createFramebuffer();
  1436. gl.bindFramebuffer(gl.FRAMEBUFFER, framebuffer);
  1437. gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, texture, 0);
  1438. if (generateDepthBuffer) {
  1439. gl.framebufferRenderbuffer(gl.FRAMEBUFFER, gl.DEPTH_ATTACHMENT, gl.RENDERBUFFER, depthBuffer);
  1440. }
  1441. // Unbind
  1442. gl.bindTexture(gl.TEXTURE_2D, null);
  1443. gl.bindRenderbuffer(gl.RENDERBUFFER, null);
  1444. gl.bindFramebuffer(gl.FRAMEBUFFER, null);
  1445. texture._framebuffer = framebuffer;
  1446. if (generateDepthBuffer) {
  1447. texture._depthBuffer = depthBuffer;
  1448. }
  1449. texture._width = width;
  1450. texture._height = height;
  1451. texture.isReady = true;
  1452. texture.generateMipMaps = generateMipMaps;
  1453. texture.references = 1;
  1454. texture.samplingMode = samplingMode;
  1455. this._activeTexturesCache = [];
  1456. this._loadedTexturesCache.push(texture);
  1457. return texture;
  1458. };
  1459. Engine.prototype.createCubeTexture = function (rootUrl, scene, extensions, noMipmap) {
  1460. var _this = this;
  1461. var gl = this._gl;
  1462. var texture = gl.createTexture();
  1463. texture.isCube = true;
  1464. texture.url = rootUrl;
  1465. texture.references = 1;
  1466. this._loadedTexturesCache.push(texture);
  1467. var extension = rootUrl.substr(rootUrl.length - 4, 4).toLowerCase();
  1468. var isDDS = this.getCaps().s3tc && (extension === ".dds");
  1469. if (isDDS) {
  1470. BABYLON.Tools.LoadFile(rootUrl, function (data) {
  1471. var info = BABYLON.Internals.DDSTools.GetDDSInfo(data);
  1472. var loadMipmap = (info.isRGB || info.isLuminance || info.mipmapCount > 1) && !noMipmap;
  1473. gl.bindTexture(gl.TEXTURE_CUBE_MAP, texture);
  1474. gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, 1);
  1475. BABYLON.Internals.DDSTools.UploadDDSLevels(_this._gl, _this.getCaps().s3tc, data, info, loadMipmap, 6);
  1476. if (!noMipmap && !info.isFourCC && info.mipmapCount === 1) {
  1477. gl.generateMipmap(gl.TEXTURE_CUBE_MAP);
  1478. }
  1479. gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
  1480. gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_MIN_FILTER, loadMipmap ? gl.LINEAR_MIPMAP_LINEAR : gl.LINEAR);
  1481. gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
  1482. gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
  1483. gl.bindTexture(gl.TEXTURE_CUBE_MAP, null);
  1484. _this._activeTexturesCache = [];
  1485. texture._width = info.width;
  1486. texture._height = info.height;
  1487. texture.isReady = true;
  1488. }, null, null, true);
  1489. }
  1490. else {
  1491. cascadeLoad(rootUrl, scene, function (imgs) {
  1492. var width = BABYLON.Tools.GetExponantOfTwo(imgs[0].width, _this._caps.maxCubemapTextureSize);
  1493. var height = width;
  1494. _this._workingCanvas.width = width;
  1495. _this._workingCanvas.height = height;
  1496. var faces = [
  1497. gl.TEXTURE_CUBE_MAP_POSITIVE_X,
  1498. gl.TEXTURE_CUBE_MAP_POSITIVE_Y,
  1499. gl.TEXTURE_CUBE_MAP_POSITIVE_Z,
  1500. gl.TEXTURE_CUBE_MAP_NEGATIVE_X,
  1501. gl.TEXTURE_CUBE_MAP_NEGATIVE_Y,
  1502. gl.TEXTURE_CUBE_MAP_NEGATIVE_Z
  1503. ];
  1504. gl.bindTexture(gl.TEXTURE_CUBE_MAP, texture);
  1505. gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, 0);
  1506. for (var index = 0; index < faces.length; index++) {
  1507. _this._workingContext.drawImage(imgs[index], 0, 0, imgs[index].width, imgs[index].height, 0, 0, width, height);
  1508. gl.texImage2D(faces[index], 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, _this._workingCanvas);
  1509. }
  1510. if (!noMipmap) {
  1511. gl.generateMipmap(gl.TEXTURE_CUBE_MAP);
  1512. }
  1513. gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
  1514. gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_MIN_FILTER, noMipmap ? gl.LINEAR : gl.LINEAR_MIPMAP_LINEAR);
  1515. gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
  1516. gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
  1517. gl.bindTexture(gl.TEXTURE_CUBE_MAP, null);
  1518. _this._activeTexturesCache = [];
  1519. texture._width = width;
  1520. texture._height = height;
  1521. texture.isReady = true;
  1522. }, extensions);
  1523. }
  1524. return texture;
  1525. };
  1526. Engine.prototype._releaseTexture = function (texture) {
  1527. var gl = this._gl;
  1528. if (texture._framebuffer) {
  1529. gl.deleteFramebuffer(texture._framebuffer);
  1530. }
  1531. if (texture._depthBuffer) {
  1532. gl.deleteRenderbuffer(texture._depthBuffer);
  1533. }
  1534. gl.deleteTexture(texture);
  1535. for (var channel = 0; channel < this._caps.maxTexturesImageUnits; channel++) {
  1536. this._gl.activeTexture(this._gl["TEXTURE" + channel]);
  1537. this._gl.bindTexture(this._gl.TEXTURE_2D, null);
  1538. this._gl.bindTexture(this._gl.TEXTURE_CUBE_MAP, null);
  1539. this._activeTexturesCache[channel] = null;
  1540. }
  1541. var index = this._loadedTexturesCache.indexOf(texture);
  1542. if (index !== -1) {
  1543. this._loadedTexturesCache.splice(index, 1);
  1544. }
  1545. };
  1546. Engine.prototype.bindSamplers = function (effect) {
  1547. this._gl.useProgram(effect.getProgram());
  1548. var samplers = effect.getSamplers();
  1549. for (var index = 0; index < samplers.length; index++) {
  1550. var uniform = effect.getUniform(samplers[index]);
  1551. this._gl.uniform1i(uniform, index);
  1552. }
  1553. this._currentEffect = null;
  1554. };
  1555. Engine.prototype._bindTexture = function (channel, texture) {
  1556. this._gl.activeTexture(this._gl["TEXTURE" + channel]);
  1557. this._gl.bindTexture(this._gl.TEXTURE_2D, texture);
  1558. this._activeTexturesCache[channel] = null;
  1559. };
  1560. Engine.prototype.setTextureFromPostProcess = function (channel, postProcess) {
  1561. this._bindTexture(channel, postProcess._textures.data[postProcess._currentRenderTextureInd]);
  1562. };
  1563. Engine.prototype.setTexture = function (channel, texture) {
  1564. if (channel < 0) {
  1565. return;
  1566. }
  1567. // Not ready?
  1568. if (!texture || !texture.isReady()) {
  1569. if (this._activeTexturesCache[channel] != null) {
  1570. this._gl.activeTexture(this._gl["TEXTURE" + channel]);
  1571. this._gl.bindTexture(this._gl.TEXTURE_2D, null);
  1572. this._gl.bindTexture(this._gl.TEXTURE_CUBE_MAP, null);
  1573. this._activeTexturesCache[channel] = null;
  1574. }
  1575. return;
  1576. }
  1577. // Video
  1578. if (texture instanceof BABYLON.VideoTexture) {
  1579. if (texture.update()) {
  1580. this._activeTexturesCache[channel] = null;
  1581. }
  1582. }
  1583. else if (texture.delayLoadState === Engine.DELAYLOADSTATE_NOTLOADED) {
  1584. texture.delayLoad();
  1585. return;
  1586. }
  1587. if (this._activeTexturesCache[channel] === texture) {
  1588. return;
  1589. }
  1590. this._activeTexturesCache[channel] = texture;
  1591. var internalTexture = texture.getInternalTexture();
  1592. this._gl.activeTexture(this._gl["TEXTURE" + channel]);
  1593. if (internalTexture.isCube) {
  1594. this._gl.bindTexture(this._gl.TEXTURE_CUBE_MAP, internalTexture);
  1595. if (internalTexture._cachedCoordinatesMode !== texture.coordinatesMode) {
  1596. internalTexture._cachedCoordinatesMode = texture.coordinatesMode;
  1597. // CUBIC_MODE and SKYBOX_MODE both require CLAMP_TO_EDGE. All other modes use REPEAT.
  1598. var textureWrapMode = (texture.coordinatesMode !== BABYLON.Texture.CUBIC_MODE && texture.coordinatesMode !== BABYLON.Texture.SKYBOX_MODE) ? this._gl.REPEAT : this._gl.CLAMP_TO_EDGE;
  1599. this._gl.texParameteri(this._gl.TEXTURE_CUBE_MAP, this._gl.TEXTURE_WRAP_S, textureWrapMode);
  1600. this._gl.texParameteri(this._gl.TEXTURE_CUBE_MAP, this._gl.TEXTURE_WRAP_T, textureWrapMode);
  1601. }
  1602. this._setAnisotropicLevel(this._gl.TEXTURE_CUBE_MAP, texture);
  1603. }
  1604. else {
  1605. this._gl.bindTexture(this._gl.TEXTURE_2D, internalTexture);
  1606. if (internalTexture._cachedWrapU !== texture.wrapU) {
  1607. internalTexture._cachedWrapU = texture.wrapU;
  1608. switch (texture.wrapU) {
  1609. case BABYLON.Texture.WRAP_ADDRESSMODE:
  1610. this._gl.texParameteri(this._gl.TEXTURE_2D, this._gl.TEXTURE_WRAP_S, this._gl.REPEAT);
  1611. break;
  1612. case BABYLON.Texture.CLAMP_ADDRESSMODE:
  1613. this._gl.texParameteri(this._gl.TEXTURE_2D, this._gl.TEXTURE_WRAP_S, this._gl.CLAMP_TO_EDGE);
  1614. break;
  1615. case BABYLON.Texture.MIRROR_ADDRESSMODE:
  1616. this._gl.texParameteri(this._gl.TEXTURE_2D, this._gl.TEXTURE_WRAP_S, this._gl.MIRRORED_REPEAT);
  1617. break;
  1618. }
  1619. }
  1620. if (internalTexture._cachedWrapV !== texture.wrapV) {
  1621. internalTexture._cachedWrapV = texture.wrapV;
  1622. switch (texture.wrapV) {
  1623. case BABYLON.Texture.WRAP_ADDRESSMODE:
  1624. this._gl.texParameteri(this._gl.TEXTURE_2D, this._gl.TEXTURE_WRAP_T, this._gl.REPEAT);
  1625. break;
  1626. case BABYLON.Texture.CLAMP_ADDRESSMODE:
  1627. this._gl.texParameteri(this._gl.TEXTURE_2D, this._gl.TEXTURE_WRAP_T, this._gl.CLAMP_TO_EDGE);
  1628. break;
  1629. case BABYLON.Texture.MIRROR_ADDRESSMODE:
  1630. this._gl.texParameteri(this._gl.TEXTURE_2D, this._gl.TEXTURE_WRAP_T, this._gl.MIRRORED_REPEAT);
  1631. break;
  1632. }
  1633. }
  1634. this._setAnisotropicLevel(this._gl.TEXTURE_2D, texture);
  1635. }
  1636. };
  1637. Engine.prototype._setAnisotropicLevel = function (key, texture) {
  1638. var anisotropicFilterExtension = this._caps.textureAnisotropicFilterExtension;
  1639. if (anisotropicFilterExtension && texture._cachedAnisotropicFilteringLevel !== texture.anisotropicFilteringLevel) {
  1640. this._gl.texParameterf(key, anisotropicFilterExtension.TEXTURE_MAX_ANISOTROPY_EXT, Math.min(texture.anisotropicFilteringLevel, this._caps.maxAnisotropy));
  1641. texture._cachedAnisotropicFilteringLevel = texture.anisotropicFilteringLevel;
  1642. }
  1643. };
  1644. Engine.prototype.readPixels = function (x, y, width, height) {
  1645. var data = new Uint8Array(height * width * 4);
  1646. this._gl.readPixels(0, 0, width, height, this._gl.RGBA, this._gl.UNSIGNED_BYTE, data);
  1647. return data;
  1648. };
  1649. // Dispose
  1650. Engine.prototype.dispose = function () {
  1651. this.hideLoadingUI();
  1652. this.stopRenderLoop();
  1653. while (this.scenes.length) {
  1654. this.scenes[0].dispose();
  1655. }
  1656. // Release audio engine
  1657. Engine.audioEngine.dispose();
  1658. for (var name in this._compiledEffects) {
  1659. this._gl.deleteProgram(this._compiledEffects[name]._program);
  1660. }
  1661. for (var i in this._vertexAttribArrays) {
  1662. if (i > this._gl.VERTEX_ATTRIB_ARRAY_ENABLED || !this._vertexAttribArrays[i]) {
  1663. continue;
  1664. }
  1665. this._gl.disableVertexAttribArray(i);
  1666. }
  1667. // Events
  1668. window.removeEventListener("blur", this._onBlur);
  1669. window.removeEventListener("focus", this._onFocus);
  1670. document.removeEventListener("fullscreenchange", this._onFullscreenChange);
  1671. document.removeEventListener("mozfullscreenchange", this._onFullscreenChange);
  1672. document.removeEventListener("webkitfullscreenchange", this._onFullscreenChange);
  1673. document.removeEventListener("msfullscreenchange", this._onFullscreenChange);
  1674. document.removeEventListener("pointerlockchange", this._onPointerLockChange);
  1675. document.removeEventListener("mspointerlockchange", this._onPointerLockChange);
  1676. document.removeEventListener("mozpointerlockchange", this._onPointerLockChange);
  1677. document.removeEventListener("webkitpointerlockchange", this._onPointerLockChange);
  1678. };
  1679. // Loading screen
  1680. Engine.prototype.displayLoadingUI = function () {
  1681. var _this = this;
  1682. this._loadingDiv = document.createElement("div");
  1683. this._loadingDiv.style.opacity = "0";
  1684. this._loadingDiv.style.transition = "opacity 1.5s ease";
  1685. // Loading text
  1686. this._loadingTextDiv = document.createElement("div");
  1687. this._loadingTextDiv.style.position = "absolute";
  1688. this._loadingTextDiv.style.left = "0";
  1689. this._loadingTextDiv.style.top = "50%";
  1690. this._loadingTextDiv.style.marginTop = "80px";
  1691. this._loadingTextDiv.style.width = "100%";
  1692. this._loadingTextDiv.style.height = "20px";
  1693. this._loadingTextDiv.style.fontFamily = "Arial";
  1694. this._loadingTextDiv.style.fontSize = "14px";
  1695. this._loadingTextDiv.style.color = "white";
  1696. this._loadingTextDiv.style.textAlign = "center";
  1697. this._loadingTextDiv.innerHTML = "Loading";
  1698. this._loadingDiv.appendChild(this._loadingTextDiv);
  1699. // Loading img
  1700. var imgBack = new Image();
  1701. imgBack.src = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGQAAABkCAYAAABw4pVUAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAAYdEVYdFNvZnR3YXJlAHBhaW50Lm5ldCA0LjAuM4zml1AAAARbSURBVHhe7Z09aFNRFMc716kuLrq4FdyLq4Wi4CAoRQcR0UJBUBdRiuLSIYMo6CA4FF2sgw6CFAdFUOpSQYcWO4hD26UQCfXrIQrx/JJzw1OSWq3NPeL/B4Fy+0jg/HO+7j3vpUcI8b/Q39+/49ihfWdPHT94Yf/e3Se3bd263f8lus218TPn6vV6Ya8Wi/MzNRNmj18iusX9W1evmP1/EKNEIVG6CMbG6E3bt+fT++pHha8NoHdT72bLE8NDg7tGU64gLLndV4Wc4m8j/pS+vr4tGB/DT16v3Fyr8dvBe/jbit8BL0AES9LX1iPAz+BR/hFiLVCynj95dPzNy6fv3IZ/k4L3948Sq7FzYGBg4vLFGxitabuOFCbWNKGrMnbiUuo18KaV6tIHv6YtvL9/nOgE31jCktmrY7k6+/zhE4yP4Vf7hiNqh/BWWEl8mzDol4p22Lf7cIdvdUMEvv0Y2S9fE5S1hLzpqTsPkiep//gFGPnR3Yl7GL5p/xYFBrTwM+iXio3GqpwDGL5p/xYNIX7XG8Q6IJRgdIzf1KBBgafII7oMidhyQtVFaMA2Bt7il4huQRhaXphbcR2g4RXqBzKAGHiCCwGFVUAj/m/RTRDj29cvn10I0PZ3LghH5f4CL1EFlQmqqXK3jDDKFxmhQ3Yt6oQseUZGKmMnTpsOqc8o1F9kBOMjQlOLeqEeIyOc6JV6jYLJD/+XyIFvnzdgl9aXRQ5I2qZDK1SpospMqaoqON/wZZGDciLnMMiXRS7IF4hhqMTNTdk7CFu+LHLhR7BQqBvPDJUUQqCGvCMATHUgBmhWNgApmdOda9YpM+VwRYfuyyIXDK8hBlilNerLIheMZCKGwlUAyru6GlwOgPUbRxADdJ9FAChxXY864viyyEXqPxhc0M2TAfAbatSdRyHtXymhByEdRnE3ky+JnHAIhSA0h74kckETmHoQbSgGwJrCIRMEPSRIBCRIMAhZaYhaggQhJXUJEoRU9mofKwh+F22dLRRfEjlJM7w6KQwCoQpBOKTyJZETjmwRxKqtGV8SOSkNOGjKPQppBEgDDkFgpxdBVGkFgaYQQXRIFQSObk0P5ZFIpAZRHXsQ0r0hCluBWKkuvVbYCkQaCdL5ehBScudJP4yY+rLISdps1NBDEJKXMMmoSfggWC4ZQRR17oFYXph7hSiquIKQ+hJGTX1J5MYSPD/GVdNzsgLBwZVCVyAQAkF0ohiI/c1fS6tNXq9UfEnkhudmIQolsS+J3Hh/UtNDzQLhj42VKJFInqLwFYiUU5ToA+HdfI0JevUpQUAIn+vSz2lHIuUV/dJOIHhOY/IWVWGBIHQtzs88s9zyWBuTgcBLzGOmeNnfF/QslSDgMeQW85i3DOQxuipxAkCyZ8SIm4Omp+7MMlCB59j6sKZcMoM4iIEoeI2J9AKxrFobZx0v4vYInuHFS4J1GQRCAGaLEYQXfyMML5XSQgghhBBCCCH+cXp6vgNhKpSKX/XdOAAAAABJRU5ErkJggg==";
  1702. imgBack.style.position = "absolute";
  1703. imgBack.style.left = "50%";
  1704. imgBack.style.top = "50%";
  1705. imgBack.style.marginLeft = "-50px";
  1706. imgBack.style.marginTop = "-50px";
  1707. imgBack.style.transition = "transform 1.0s ease";
  1708. imgBack.style.webkitTransition = "-webkit-transform 1.0s ease";
  1709. var deg = 360;
  1710. var onTransitionEnd = function () {
  1711. deg += 360;
  1712. imgBack.style.transform = "rotateZ(" + deg + "deg)";
  1713. imgBack.style.webkitTransform = "rotateZ(" + deg + "deg)";
  1714. };
  1715. imgBack.addEventListener("transitionend", onTransitionEnd);
  1716. imgBack.addEventListener("webkitTransitionEnd", onTransitionEnd);
  1717. this._loadingDiv.appendChild(imgBack);
  1718. // front image
  1719. var imgFront = new Image();
  1720. imgFront.src = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGQAAABkCAYAAABw4pVUAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAAYdEVYdFNvZnR3YXJlAHBhaW50Lm5ldCA0LjAuM4zml1AAAAYJSURBVHhe7Zy/qx1FFMff/2Av2Nvbi4WFiiAEY/OQ2IgQsbCJQoqkCAgpFLXyoZURLfwBIiIpgqZJoYQYlWelNsIrNOxDJcrzfHe+G97dnTl75u7euzv7zgcWHrlnZmfOmXPmzI/NjuM4juM4juM4juM4juM4juM4juM4juM45fPic08/uHf5/CvffH7lnT8PfrtxdHS0n3p+/fHGl5+89/prr5599iEWd8bg0rkXHoFyqehKnlxQpjYSDHTm9JMPsGrHylOPPXofvICKXMcIGtXdf/76AYbm6xyNW9e/eAtKC7rbKLXnvHHx5Sf4auc4Ek7OQkFU1Dap/vv37k/wSjblZANFiFIGzw98hhizwqBgs04mCBdQRNCHidoAEtY+lLIvtSdoGFeyql2ZH57HBH4sE7O+o/r9l+8/ZXUni68+2jsHBQQ9qNRGeP/tSxdSYQX/roUcpL4/f3vtM9TD+jTq92n1LQ7jxF1hhGPtwWL3gGccy8JuS1r8sVWBGXNVdSKMYjBGPUJjCzooiGuSpnwlnnOGP2dhHRSLNgpHp2oMKIriK8TmG4Qh/rwW8D6pps9b9im+LDDipXOqMVJrAngBfg9i98gevWKA+/nnCod3Dr5GfaHaDgidVym6HKRjGIkpqthcAVKGxNqBImbEo66kjCih8AOpNmkUmbMuUrR8kEqiU6FvHZLGAPJ71JCYSyhiBqmwFE2GoD6jLGIfDHtG6EzoU4dK21PCqIRMEF0FGRjFzGDtIkXVAdATvsqfT9CJ0JcOFdYiFIsiMlqYy1YOFpQo2OddqBtyEaq9y+efoVh5oPHoROjLKn0j3JIE5Ka8UqZRtGrMnneX6yVofOhDh94MSbznTcpqmDOt1vyQzOgaJAF4F3JBfIXesrNEGWWmjIX7UBZ6jRJbBMLg/DmJiKUGVHleIpnVNTa+jakzkAviJqLhi4MC9XQGBrZeKJZESSrKy7ik0VGFWhQBRDTHIACKQ5l9nAjy75gya4a2w+Jhs0FJdc0xX/GwUbAqFBkZi7QpJ2w16WUbjFyK9MJF3KaoEM74KhVtLrQOrsmRxkbdHEqmSC/c+EuGnIFkjW7Ih2Kr4CCMIvNG2hrrgLpCjiFloooYCjyYrzCRyvhyBthkIPuQtsZGdnbMTezyDiU71KTC5zr7aVsHbsz2tllrEkS5UHwU1tq1HbtPW4UbeB0O7xx8R5EsMJql+BheUmHjkNVmIRP7LutoM3+D4O4tG7vCkNO9ESZ4lL3J6rKRMPx4qKbD/A0icf8CG7tC7kTahnMTwleuYSrsS7GatRAvfZh1tTm5BmmQCdZ8a0Sefe28xUrRBkmFLKy8KTIKUDRX0Y1xagPgwbaIdeFnQULmKak3xvwNMkVGgok/N5XNoehJvejRlCDl9escI28dJU0tZ++nBTJE9mEF647x5Ehbo4s5hDOKFIU0PdofeA5F5k1q63zIWmQqNI/P3ZubjFTqKxQ3jyjHAOX0RdlgVO9hzRFpczRcjZ3Gbxxpc7Qj6+5pTYF2OFXawNI+yDGf1k2NcvOlzBQeDQ/t7zD7DsEDpJ2xATXaNtDWUS4IzP4DS2ljajAVu57SUkYw245ptxZxA5JiZaJ0DswudGn3kYUy54426EjoT4dZfYbccxC2nI92cDkZHQr96jD4AGkMDKeSy/COBsRe6VTSKFN6irLeaCh3IteQjt1E5+oudsG/b/2DfZ5AqsYo8vMDK9LB1HzSsLWvlGThdxXvC6+NsqyPPWP0pMINtbdsajfVeC6f/GZ+cdAofQoB1d+Hf9waY98I7+RXWab3Lt4zYkjHtTnlOLXHYMsCh1zWeQYehu1zfNPOOiys/d91LAKEBSgh6MJMbSA82AaHofDgAIwbgvVvlLNS11nModMm4UZergLHZBZrodmBuA3lBB1thdorSjkOmATMDwg/UBQVtglqQyx6fbEJ+H3IWIapjYAjAfeIgeCMHldueJvFaqDaAHhwf8qNsEEQ1iQbOoUUGIbCLRc8+Bvfp4jyd2FEijuO4ziO4ziO4ziO4ziO4ziO4ziO4ziOUzw7O/8D0P7rcZ/GEboAAAAASUVORK5CYII=";
  1721. imgFront.style.position = "absolute";
  1722. imgFront.style.left = "50%";
  1723. imgFront.style.top = "50%";
  1724. imgFront.style.marginLeft = "-50px";
  1725. imgFront.style.marginTop = "-50px";
  1726. this._loadingDiv.appendChild(imgFront);
  1727. // Resize
  1728. this._resizeLoadingUI = function () {
  1729. var canvasRect = _this.getRenderingCanvasClientRect();
  1730. _this._loadingDiv.style.position = "absolute";
  1731. _this._loadingDiv.style.left = canvasRect.left + "px";
  1732. _this._loadingDiv.style.top = canvasRect.top + "px";
  1733. _this._loadingDiv.style.width = canvasRect.width + "px";
  1734. _this._loadingDiv.style.height = canvasRect.height + "px";
  1735. };
  1736. this._resizeLoadingUI();
  1737. window.addEventListener("resize", this._resizeLoadingUI);
  1738. this._loadingDiv.style.backgroundColor = this._loadingDivBackgroundColor;
  1739. document.body.appendChild(this._loadingDiv);
  1740. setTimeout(function () {
  1741. _this._loadingDiv.style.opacity = "1";
  1742. imgBack.style.transform = "rotateZ(360deg)";
  1743. imgBack.style.webkitTransform = "rotateZ(360deg)";
  1744. }, 0);
  1745. };
  1746. Object.defineProperty(Engine.prototype, "loadingUIText", {
  1747. set: function (text) {
  1748. if (!this._loadingDiv) {
  1749. return;
  1750. }
  1751. this._loadingTextDiv.innerHTML = text;
  1752. },
  1753. enumerable: true,
  1754. configurable: true
  1755. });
  1756. Object.defineProperty(Engine.prototype, "loadingUIBackgroundColor", {
  1757. get: function () {
  1758. return this._loadingDivBackgroundColor;
  1759. },
  1760. set: function (color) {
  1761. this._loadingDivBackgroundColor = color;
  1762. if (!this._loadingDiv) {
  1763. return;
  1764. }
  1765. this._loadingDiv.style.backgroundColor = this._loadingDivBackgroundColor;
  1766. },
  1767. enumerable: true,
  1768. configurable: true
  1769. });
  1770. Engine.prototype.hideLoadingUI = function () {
  1771. var _this = this;
  1772. if (!this._loadingDiv) {
  1773. return;
  1774. }
  1775. var onTransitionEnd = function () {
  1776. if (!_this._loadingDiv) {
  1777. return;
  1778. }
  1779. document.body.removeChild(_this._loadingDiv);
  1780. window.removeEventListener("resize", _this._resizeLoadingUI);
  1781. _this._loadingDiv = null;
  1782. };
  1783. this._loadingDiv.style.opacity = "0";
  1784. this._loadingDiv.addEventListener("transitionend", onTransitionEnd);
  1785. };
  1786. // FPS
  1787. Engine.prototype.getFps = function () {
  1788. return this.fps;
  1789. };
  1790. Engine.prototype.getDeltaTime = function () {
  1791. return this.deltaTime;
  1792. };
  1793. Engine.prototype._measureFps = function () {
  1794. this.previousFramesDuration.push(BABYLON.Tools.Now);
  1795. var length = this.previousFramesDuration.length;
  1796. if (length >= 2) {
  1797. this.deltaTime = this.previousFramesDuration[length - 1] - this.previousFramesDuration[length - 2];
  1798. }
  1799. if (length >= this.fpsRange) {
  1800. if (length > this.fpsRange) {
  1801. this.previousFramesDuration.splice(0, 1);
  1802. length = this.previousFramesDuration.length;
  1803. }
  1804. var sum = 0;
  1805. for (var id = 0; id < length - 1; id++) {
  1806. sum += this.previousFramesDuration[id + 1] - this.previousFramesDuration[id];
  1807. }
  1808. this.fps = 1000.0 / (sum / (length - 1));
  1809. }
  1810. };
  1811. // Statics
  1812. Engine.isSupported = function () {
  1813. try {
  1814. // Avoid creating an unsized context for CocoonJS, since size determined on first creation. Is not resizable
  1815. if (navigator.isCocoonJS) {
  1816. return true;
  1817. }
  1818. var tempcanvas = document.createElement("canvas");
  1819. var gl = tempcanvas.getContext("webgl") || tempcanvas.getContext("experimental-webgl");
  1820. return gl != null && !!window.WebGLRenderingContext;
  1821. }
  1822. catch (e) {
  1823. return false;
  1824. }
  1825. };
  1826. // Const statics
  1827. Engine._ALPHA_DISABLE = 0;
  1828. Engine._ALPHA_ADD = 1;
  1829. Engine._ALPHA_COMBINE = 2;
  1830. Engine._DELAYLOADSTATE_NONE = 0;
  1831. Engine._DELAYLOADSTATE_LOADED = 1;
  1832. Engine._DELAYLOADSTATE_LOADING = 2;
  1833. Engine._DELAYLOADSTATE_NOTLOADED = 4;
  1834. Engine._TEXTUREFORMAT_ALPHA = 0;
  1835. Engine._TEXTUREFORMAT_LUMINANCE = 1;
  1836. Engine._TEXTUREFORMAT_LUMINANCE_ALPHA = 2;
  1837. Engine._TEXTUREFORMAT_RGB = 4;
  1838. Engine._TEXTUREFORMAT_RGBA = 4;
  1839. Engine._TEXTURETYPE_UNSIGNED_INT = 0;
  1840. Engine._TEXTURETYPE_FLOAT = 1;
  1841. // Updatable statics so stick with vars here
  1842. Engine.Epsilon = 0.001;
  1843. Engine.CollisionsEpsilon = 0.001;
  1844. Engine.ShadersRepository = "Babylon/Shaders/";
  1845. return Engine;
  1846. })();
  1847. BABYLON.Engine = Engine;
  1848. })(BABYLON || (BABYLON = {}));
  1849. //# sourceMappingURL=babylon.engine.js.map