babylon.engine.js 90 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852
  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.getAudioEngine = function () {
  562. return Engine.audioEngine;
  563. };
  564. Engine.prototype.getAspectRatio = function (camera) {
  565. var viewport = camera.viewport;
  566. return (this.getRenderWidth() * viewport.width) / (this.getRenderHeight() * viewport.height);
  567. };
  568. Engine.prototype.getRenderWidth = function () {
  569. if (this._currentRenderTarget) {
  570. return this._currentRenderTarget._width;
  571. }
  572. return this._renderingCanvas.width;
  573. };
  574. Engine.prototype.getRenderHeight = function () {
  575. if (this._currentRenderTarget) {
  576. return this._currentRenderTarget._height;
  577. }
  578. return this._renderingCanvas.height;
  579. };
  580. Engine.prototype.getRenderingCanvas = function () {
  581. return this._renderingCanvas;
  582. };
  583. Engine.prototype.getRenderingCanvasClientRect = function () {
  584. return this._renderingCanvas.getBoundingClientRect();
  585. };
  586. Engine.prototype.setHardwareScalingLevel = function (level) {
  587. this._hardwareScalingLevel = level;
  588. this.resize();
  589. };
  590. Engine.prototype.getHardwareScalingLevel = function () {
  591. return this._hardwareScalingLevel;
  592. };
  593. Engine.prototype.getLoadedTexturesCache = function () {
  594. return this._loadedTexturesCache;
  595. };
  596. Engine.prototype.getCaps = function () {
  597. return this._caps;
  598. };
  599. Object.defineProperty(Engine.prototype, "drawCalls", {
  600. get: function () {
  601. return this._drawCalls;
  602. },
  603. enumerable: true,
  604. configurable: true
  605. });
  606. // Methods
  607. Engine.prototype.resetDrawCalls = function () {
  608. this._drawCalls = 0;
  609. };
  610. Engine.prototype.setDepthFunctionToGreater = function () {
  611. this._depthCullingState.depthFunc = this._gl.GREATER;
  612. };
  613. Engine.prototype.setDepthFunctionToGreaterOrEqual = function () {
  614. this._depthCullingState.depthFunc = this._gl.GEQUAL;
  615. };
  616. Engine.prototype.setDepthFunctionToLess = function () {
  617. this._depthCullingState.depthFunc = this._gl.LESS;
  618. };
  619. Engine.prototype.setDepthFunctionToLessOrEqual = function () {
  620. this._depthCullingState.depthFunc = this._gl.LEQUAL;
  621. };
  622. /**
  623. * stop executing a render loop function and remove it from the execution array
  624. * @param {Function} [renderFunction] the function to be removed. If not provided all functions will be removed.
  625. */
  626. Engine.prototype.stopRenderLoop = function (renderFunction) {
  627. if (!renderFunction) {
  628. this._activeRenderLoops = [];
  629. return;
  630. }
  631. var index = this._activeRenderLoops.indexOf(renderFunction);
  632. if (index >= 0) {
  633. this._activeRenderLoops.splice(index, 1);
  634. }
  635. };
  636. Engine.prototype._renderLoop = function () {
  637. var _this = this;
  638. var shouldRender = true;
  639. if (!this.renderEvenInBackground && this._windowIsBackground) {
  640. shouldRender = false;
  641. }
  642. if (shouldRender) {
  643. // Start new frame
  644. this.beginFrame();
  645. for (var index = 0; index < this._activeRenderLoops.length; index++) {
  646. var renderFunction = this._activeRenderLoops[index];
  647. renderFunction();
  648. }
  649. // Present
  650. this.endFrame();
  651. }
  652. if (this._activeRenderLoops.length > 0) {
  653. // Register new frame
  654. BABYLON.Tools.QueueNewFrame(function () {
  655. _this._renderLoop();
  656. });
  657. }
  658. else {
  659. this._renderingQueueLaunched = false;
  660. }
  661. };
  662. /**
  663. * Register and execute a render loop. The engine can have more than one render function.
  664. * @param {Function} renderFunction - the function to continuesly execute starting the next render loop.
  665. * @example
  666. * engine.runRenderLoop(function () {
  667. * scene.render()
  668. * })
  669. */
  670. Engine.prototype.runRenderLoop = function (renderFunction) {
  671. var _this = this;
  672. if (this._activeRenderLoops.indexOf(renderFunction) !== -1) {
  673. return;
  674. }
  675. this._activeRenderLoops.push(renderFunction);
  676. if (!this._renderingQueueLaunched) {
  677. this._renderingQueueLaunched = true;
  678. BABYLON.Tools.QueueNewFrame(function () {
  679. _this._renderLoop();
  680. });
  681. }
  682. };
  683. /**
  684. * Toggle full screen mode.
  685. * @param {boolean} requestPointerLock - should a pointer lock be requested from the user
  686. */
  687. Engine.prototype.switchFullscreen = function (requestPointerLock) {
  688. if (this.isFullscreen) {
  689. BABYLON.Tools.ExitFullscreen();
  690. }
  691. else {
  692. this._pointerLockRequested = requestPointerLock;
  693. BABYLON.Tools.RequestFullscreen(this._renderingCanvas);
  694. }
  695. };
  696. Engine.prototype.clear = function (color, backBuffer, depthStencil) {
  697. this.applyStates();
  698. this._gl.clearColor(color.r, color.g, color.b, color.a !== undefined ? color.a : 1.0);
  699. if (this._depthCullingState.depthMask) {
  700. this._gl.clearDepth(1.0);
  701. }
  702. var mode = 0;
  703. if (backBuffer)
  704. mode |= this._gl.COLOR_BUFFER_BIT;
  705. if (depthStencil && this._depthCullingState.depthMask)
  706. mode |= this._gl.DEPTH_BUFFER_BIT;
  707. this._gl.clear(mode);
  708. };
  709. /**
  710. * Set the WebGL's viewport
  711. * @param {BABYLON.Viewport} viewport - the viewport element to be used.
  712. * @param {number} [requiredWidth] - the width required for rendering. If not provided the rendering canvas' width is used.
  713. * @param {number} [requiredHeight] - the height required for rendering. If not provided the rendering canvas' height is used.
  714. */
  715. Engine.prototype.setViewport = function (viewport, requiredWidth, requiredHeight) {
  716. var width = requiredWidth || this._renderingCanvas.width;
  717. var height = requiredHeight || this._renderingCanvas.height;
  718. var x = viewport.x || 0;
  719. var y = viewport.y || 0;
  720. this._cachedViewport = viewport;
  721. this._gl.viewport(x * width, y * height, width * viewport.width, height * viewport.height);
  722. };
  723. Engine.prototype.setDirectViewport = function (x, y, width, height) {
  724. this._cachedViewport = null;
  725. this._gl.viewport(x, y, width, height);
  726. };
  727. Engine.prototype.beginFrame = function () {
  728. this._measureFps();
  729. };
  730. Engine.prototype.endFrame = function () {
  731. this.flushFramebuffer();
  732. };
  733. /**
  734. * resize the view according to the canvas' size.
  735. * @example
  736. * window.addEventListener("resize", function () {
  737. * engine.resize();
  738. * });
  739. */
  740. Engine.prototype.resize = function () {
  741. this.setSize(this._renderingCanvas.clientWidth / this._hardwareScalingLevel, this._renderingCanvas.clientHeight / this._hardwareScalingLevel);
  742. };
  743. /**
  744. * force a specific size of the canvas
  745. * @param {number} width - the new canvas' width
  746. * @param {number} height - the new canvas' height
  747. */
  748. Engine.prototype.setSize = function (width, height) {
  749. this._renderingCanvas.width = width;
  750. this._renderingCanvas.height = height;
  751. this._canvasClientRect = this._renderingCanvas.getBoundingClientRect();
  752. };
  753. Engine.prototype.bindFramebuffer = function (texture) {
  754. this._currentRenderTarget = texture;
  755. var gl = this._gl;
  756. gl.bindFramebuffer(gl.FRAMEBUFFER, texture._framebuffer);
  757. this._gl.viewport(0, 0, texture._width, texture._height);
  758. this.wipeCaches();
  759. };
  760. Engine.prototype.unBindFramebuffer = function (texture) {
  761. this._currentRenderTarget = null;
  762. if (texture.generateMipMaps) {
  763. var gl = this._gl;
  764. gl.bindTexture(gl.TEXTURE_2D, texture);
  765. gl.generateMipmap(gl.TEXTURE_2D);
  766. gl.bindTexture(gl.TEXTURE_2D, null);
  767. }
  768. this._gl.bindFramebuffer(this._gl.FRAMEBUFFER, null);
  769. };
  770. Engine.prototype.flushFramebuffer = function () {
  771. // this._gl.flush();
  772. };
  773. Engine.prototype.restoreDefaultFramebuffer = function () {
  774. this._currentRenderTarget = null;
  775. this._gl.bindFramebuffer(this._gl.FRAMEBUFFER, null);
  776. this.setViewport(this._cachedViewport);
  777. this.wipeCaches();
  778. };
  779. // VBOs
  780. Engine.prototype._resetVertexBufferBinding = function () {
  781. this._gl.bindBuffer(this._gl.ARRAY_BUFFER, null);
  782. this._cachedVertexBuffers = null;
  783. };
  784. Engine.prototype.createVertexBuffer = function (vertices) {
  785. var vbo = this._gl.createBuffer();
  786. this._gl.bindBuffer(this._gl.ARRAY_BUFFER, vbo);
  787. this._gl.bufferData(this._gl.ARRAY_BUFFER, new Float32Array(vertices), this._gl.STATIC_DRAW);
  788. this._resetVertexBufferBinding();
  789. vbo.references = 1;
  790. return vbo;
  791. };
  792. Engine.prototype.createDynamicVertexBuffer = function (capacity) {
  793. var vbo = this._gl.createBuffer();
  794. this._gl.bindBuffer(this._gl.ARRAY_BUFFER, vbo);
  795. this._gl.bufferData(this._gl.ARRAY_BUFFER, capacity, this._gl.DYNAMIC_DRAW);
  796. this._resetVertexBufferBinding();
  797. vbo.references = 1;
  798. return vbo;
  799. };
  800. Engine.prototype.updateDynamicVertexBuffer = function (vertexBuffer, vertices, offset) {
  801. this._gl.bindBuffer(this._gl.ARRAY_BUFFER, vertexBuffer);
  802. if (offset === undefined) {
  803. offset = 0;
  804. }
  805. if (vertices instanceof Float32Array) {
  806. this._gl.bufferSubData(this._gl.ARRAY_BUFFER, offset, vertices);
  807. }
  808. else {
  809. this._gl.bufferSubData(this._gl.ARRAY_BUFFER, offset, new Float32Array(vertices));
  810. }
  811. this._resetVertexBufferBinding();
  812. };
  813. Engine.prototype._resetIndexBufferBinding = function () {
  814. this._gl.bindBuffer(this._gl.ELEMENT_ARRAY_BUFFER, null);
  815. this._cachedIndexBuffer = null;
  816. };
  817. Engine.prototype.createIndexBuffer = function (indices) {
  818. var vbo = this._gl.createBuffer();
  819. this._gl.bindBuffer(this._gl.ELEMENT_ARRAY_BUFFER, vbo);
  820. // Check for 32 bits indices
  821. var arrayBuffer;
  822. var need32Bits = false;
  823. if (this._caps.uintIndices) {
  824. for (var index = 0; index < indices.length; index++) {
  825. if (indices[index] > 65535) {
  826. need32Bits = true;
  827. break;
  828. }
  829. }
  830. arrayBuffer = need32Bits ? new Uint32Array(indices) : new Uint16Array(indices);
  831. }
  832. else {
  833. arrayBuffer = new Uint16Array(indices);
  834. }
  835. this._gl.bufferData(this._gl.ELEMENT_ARRAY_BUFFER, arrayBuffer, this._gl.STATIC_DRAW);
  836. this._resetIndexBufferBinding();
  837. vbo.references = 1;
  838. vbo.is32Bits = need32Bits;
  839. return vbo;
  840. };
  841. Engine.prototype.bindBuffers = function (vertexBuffer, indexBuffer, vertexDeclaration, vertexStrideSize, effect) {
  842. if (this._cachedVertexBuffers !== vertexBuffer || this._cachedEffectForVertexBuffers !== effect) {
  843. this._cachedVertexBuffers = vertexBuffer;
  844. this._cachedEffectForVertexBuffers = effect;
  845. this._gl.bindBuffer(this._gl.ARRAY_BUFFER, vertexBuffer);
  846. var offset = 0;
  847. for (var index = 0; index < vertexDeclaration.length; index++) {
  848. var order = effect.getAttributeLocation(index);
  849. if (order >= 0) {
  850. this._gl.vertexAttribPointer(order, vertexDeclaration[index], this._gl.FLOAT, false, vertexStrideSize, offset);
  851. }
  852. offset += vertexDeclaration[index] * 4;
  853. }
  854. }
  855. if (this._cachedIndexBuffer !== indexBuffer) {
  856. this._cachedIndexBuffer = indexBuffer;
  857. this._gl.bindBuffer(this._gl.ELEMENT_ARRAY_BUFFER, indexBuffer);
  858. this._uintIndicesCurrentlySet = indexBuffer.is32Bits;
  859. }
  860. };
  861. Engine.prototype.bindMultiBuffers = function (vertexBuffers, indexBuffer, effect) {
  862. if (this._cachedVertexBuffers !== vertexBuffers || this._cachedEffectForVertexBuffers !== effect) {
  863. this._cachedVertexBuffers = vertexBuffers;
  864. this._cachedEffectForVertexBuffers = effect;
  865. var attributes = effect.getAttributesNames();
  866. for (var index = 0; index < attributes.length; index++) {
  867. var order = effect.getAttributeLocation(index);
  868. if (order >= 0) {
  869. var vertexBuffer = vertexBuffers[attributes[index]];
  870. if (!vertexBuffer) {
  871. continue;
  872. }
  873. var stride = vertexBuffer.getStrideSize();
  874. this._gl.bindBuffer(this._gl.ARRAY_BUFFER, vertexBuffer.getBuffer());
  875. this._gl.vertexAttribPointer(order, stride, this._gl.FLOAT, false, stride * 4, 0);
  876. }
  877. }
  878. }
  879. if (indexBuffer != null && this._cachedIndexBuffer !== indexBuffer) {
  880. this._cachedIndexBuffer = indexBuffer;
  881. this._gl.bindBuffer(this._gl.ELEMENT_ARRAY_BUFFER, indexBuffer);
  882. this._uintIndicesCurrentlySet = indexBuffer.is32Bits;
  883. }
  884. };
  885. Engine.prototype._releaseBuffer = function (buffer) {
  886. buffer.references--;
  887. if (buffer.references === 0) {
  888. this._gl.deleteBuffer(buffer);
  889. return true;
  890. }
  891. return false;
  892. };
  893. Engine.prototype.createInstancesBuffer = function (capacity) {
  894. var buffer = this._gl.createBuffer();
  895. buffer.capacity = capacity;
  896. this._gl.bindBuffer(this._gl.ARRAY_BUFFER, buffer);
  897. this._gl.bufferData(this._gl.ARRAY_BUFFER, capacity, this._gl.DYNAMIC_DRAW);
  898. return buffer;
  899. };
  900. Engine.prototype.deleteInstancesBuffer = function (buffer) {
  901. this._gl.deleteBuffer(buffer);
  902. };
  903. Engine.prototype.updateAndBindInstancesBuffer = function (instancesBuffer, data, offsetLocations) {
  904. this._gl.bindBuffer(this._gl.ARRAY_BUFFER, instancesBuffer);
  905. this._gl.bufferSubData(this._gl.ARRAY_BUFFER, 0, data);
  906. for (var index = 0; index < 4; index++) {
  907. var offsetLocation = offsetLocations[index];
  908. this._gl.enableVertexAttribArray(offsetLocation);
  909. this._gl.vertexAttribPointer(offsetLocation, 4, this._gl.FLOAT, false, 64, index * 16);
  910. this._caps.instancedArrays.vertexAttribDivisorANGLE(offsetLocation, 1);
  911. }
  912. };
  913. Engine.prototype.unBindInstancesBuffer = function (instancesBuffer, offsetLocations) {
  914. this._gl.bindBuffer(this._gl.ARRAY_BUFFER, instancesBuffer);
  915. for (var index = 0; index < 4; index++) {
  916. var offsetLocation = offsetLocations[index];
  917. this._gl.disableVertexAttribArray(offsetLocation);
  918. this._caps.instancedArrays.vertexAttribDivisorANGLE(offsetLocation, 0);
  919. }
  920. };
  921. Engine.prototype.applyStates = function () {
  922. this._depthCullingState.apply(this._gl);
  923. this._alphaState.apply(this._gl);
  924. };
  925. Engine.prototype.draw = function (useTriangles, indexStart, indexCount, instancesCount) {
  926. // Apply states
  927. this.applyStates();
  928. this._drawCalls++;
  929. // Render
  930. var indexFormat = this._uintIndicesCurrentlySet ? this._gl.UNSIGNED_INT : this._gl.UNSIGNED_SHORT;
  931. if (instancesCount) {
  932. this._caps.instancedArrays.drawElementsInstancedANGLE(useTriangles ? this._gl.TRIANGLES : this._gl.LINES, indexCount, indexFormat, indexStart * 2, instancesCount);
  933. return;
  934. }
  935. this._gl.drawElements(useTriangles ? this._gl.TRIANGLES : this._gl.LINES, indexCount, indexFormat, indexStart * 2);
  936. };
  937. Engine.prototype.drawPointClouds = function (verticesStart, verticesCount, instancesCount) {
  938. // Apply states
  939. this.applyStates();
  940. this._drawCalls++;
  941. if (instancesCount) {
  942. this._caps.instancedArrays.drawArraysInstancedANGLE(this._gl.POINTS, verticesStart, verticesCount, instancesCount);
  943. return;
  944. }
  945. this._gl.drawArrays(this._gl.POINTS, verticesStart, verticesCount);
  946. };
  947. // Shaders
  948. Engine.prototype._releaseEffect = function (effect) {
  949. if (this._compiledEffects[effect._key]) {
  950. delete this._compiledEffects[effect._key];
  951. if (effect.getProgram()) {
  952. this._gl.deleteProgram(effect.getProgram());
  953. }
  954. }
  955. };
  956. Engine.prototype.createEffect = function (baseName, attributesNames, uniformsNames, samplers, defines, fallbacks, onCompiled, onError) {
  957. var vertex = baseName.vertexElement || baseName.vertex || baseName;
  958. var fragment = baseName.fragmentElement || baseName.fragment || baseName;
  959. var name = vertex + "+" + fragment + "@" + defines;
  960. if (this._compiledEffects[name]) {
  961. return this._compiledEffects[name];
  962. }
  963. var effect = new BABYLON.Effect(baseName, attributesNames, uniformsNames, samplers, this, defines, fallbacks, onCompiled, onError);
  964. effect._key = name;
  965. this._compiledEffects[name] = effect;
  966. return effect;
  967. };
  968. Engine.prototype.createEffectForParticles = function (fragmentName, uniformsNames, samplers, defines, fallbacks, onCompiled, onError) {
  969. if (uniformsNames === void 0) { uniformsNames = []; }
  970. if (samplers === void 0) { samplers = []; }
  971. if (defines === void 0) { defines = ""; }
  972. return this.createEffect({
  973. vertex: "particles",
  974. fragmentElement: fragmentName
  975. }, ["position", "color", "options"], ["view", "projection"].concat(uniformsNames), ["diffuseSampler"].concat(samplers), defines, fallbacks, onCompiled, onError);
  976. };
  977. Engine.prototype.createShaderProgram = function (vertexCode, fragmentCode, defines) {
  978. var vertexShader = compileShader(this._gl, vertexCode, "vertex", defines);
  979. var fragmentShader = compileShader(this._gl, fragmentCode, "fragment", defines);
  980. var shaderProgram = this._gl.createProgram();
  981. this._gl.attachShader(shaderProgram, vertexShader);
  982. this._gl.attachShader(shaderProgram, fragmentShader);
  983. this._gl.linkProgram(shaderProgram);
  984. var linked = this._gl.getProgramParameter(shaderProgram, this._gl.LINK_STATUS);
  985. if (!linked) {
  986. var error = this._gl.getProgramInfoLog(shaderProgram);
  987. if (error) {
  988. throw new Error(error);
  989. }
  990. }
  991. this._gl.deleteShader(vertexShader);
  992. this._gl.deleteShader(fragmentShader);
  993. return shaderProgram;
  994. };
  995. Engine.prototype.getUniforms = function (shaderProgram, uniformsNames) {
  996. var results = [];
  997. for (var index = 0; index < uniformsNames.length; index++) {
  998. results.push(this._gl.getUniformLocation(shaderProgram, uniformsNames[index]));
  999. }
  1000. return results;
  1001. };
  1002. Engine.prototype.getAttributes = function (shaderProgram, attributesNames) {
  1003. var results = [];
  1004. for (var index = 0; index < attributesNames.length; index++) {
  1005. try {
  1006. results.push(this._gl.getAttribLocation(shaderProgram, attributesNames[index]));
  1007. }
  1008. catch (e) {
  1009. results.push(-1);
  1010. }
  1011. }
  1012. return results;
  1013. };
  1014. Engine.prototype.enableEffect = function (effect) {
  1015. if (!effect || !effect.getAttributesCount() || this._currentEffect === effect) {
  1016. if (effect && effect.onBind) {
  1017. effect.onBind(effect);
  1018. }
  1019. return;
  1020. }
  1021. this._vertexAttribArrays = this._vertexAttribArrays || [];
  1022. // Use program
  1023. this._gl.useProgram(effect.getProgram());
  1024. for (var i in this._vertexAttribArrays) {
  1025. if (i > this._gl.VERTEX_ATTRIB_ARRAY_ENABLED || !this._vertexAttribArrays[i]) {
  1026. continue;
  1027. }
  1028. this._vertexAttribArrays[i] = false;
  1029. this._gl.disableVertexAttribArray(i);
  1030. }
  1031. var attributesCount = effect.getAttributesCount();
  1032. for (var index = 0; index < attributesCount; index++) {
  1033. // Attributes
  1034. var order = effect.getAttributeLocation(index);
  1035. if (order >= 0) {
  1036. this._vertexAttribArrays[order] = true;
  1037. this._gl.enableVertexAttribArray(order);
  1038. }
  1039. }
  1040. this._currentEffect = effect;
  1041. if (effect.onBind) {
  1042. effect.onBind(effect);
  1043. }
  1044. };
  1045. Engine.prototype.setArray = function (uniform, array) {
  1046. if (!uniform)
  1047. return;
  1048. this._gl.uniform1fv(uniform, array);
  1049. };
  1050. Engine.prototype.setArray2 = function (uniform, array) {
  1051. if (!uniform || array.length % 2 !== 0)
  1052. return;
  1053. this._gl.uniform2fv(uniform, array);
  1054. };
  1055. Engine.prototype.setArray3 = function (uniform, array) {
  1056. if (!uniform || array.length % 3 !== 0)
  1057. return;
  1058. this._gl.uniform3fv(uniform, array);
  1059. };
  1060. Engine.prototype.setArray4 = function (uniform, array) {
  1061. if (!uniform || array.length % 4 !== 0)
  1062. return;
  1063. this._gl.uniform4fv(uniform, array);
  1064. };
  1065. Engine.prototype.setMatrices = function (uniform, matrices) {
  1066. if (!uniform)
  1067. return;
  1068. this._gl.uniformMatrix4fv(uniform, false, matrices);
  1069. };
  1070. Engine.prototype.setMatrix = function (uniform, matrix) {
  1071. if (!uniform)
  1072. return;
  1073. this._gl.uniformMatrix4fv(uniform, false, matrix.toArray());
  1074. };
  1075. Engine.prototype.setFloat = function (uniform, value) {
  1076. if (!uniform)
  1077. return;
  1078. this._gl.uniform1f(uniform, value);
  1079. };
  1080. Engine.prototype.setFloat2 = function (uniform, x, y) {
  1081. if (!uniform)
  1082. return;
  1083. this._gl.uniform2f(uniform, x, y);
  1084. };
  1085. Engine.prototype.setFloat3 = function (uniform, x, y, z) {
  1086. if (!uniform)
  1087. return;
  1088. this._gl.uniform3f(uniform, x, y, z);
  1089. };
  1090. Engine.prototype.setBool = function (uniform, bool) {
  1091. if (!uniform)
  1092. return;
  1093. this._gl.uniform1i(uniform, bool);
  1094. };
  1095. Engine.prototype.setFloat4 = function (uniform, x, y, z, w) {
  1096. if (!uniform)
  1097. return;
  1098. this._gl.uniform4f(uniform, x, y, z, w);
  1099. };
  1100. Engine.prototype.setColor3 = function (uniform, color3) {
  1101. if (!uniform)
  1102. return;
  1103. this._gl.uniform3f(uniform, color3.r, color3.g, color3.b);
  1104. };
  1105. Engine.prototype.setColor4 = function (uniform, color3, alpha) {
  1106. if (!uniform)
  1107. return;
  1108. this._gl.uniform4f(uniform, color3.r, color3.g, color3.b, alpha);
  1109. };
  1110. // States
  1111. Engine.prototype.setState = function (culling, force) {
  1112. // Culling
  1113. if (this._depthCullingState.cull !== culling || force) {
  1114. if (culling) {
  1115. this._depthCullingState.cullFace = this.cullBackFaces ? this._gl.BACK : this._gl.FRONT;
  1116. this._depthCullingState.cull = true;
  1117. }
  1118. else {
  1119. this._depthCullingState.cull = false;
  1120. }
  1121. }
  1122. };
  1123. Engine.prototype.setDepthBuffer = function (enable) {
  1124. this._depthCullingState.depthTest = enable;
  1125. };
  1126. Engine.prototype.getDepthWrite = function () {
  1127. return this._depthCullingState.depthMask;
  1128. };
  1129. Engine.prototype.setDepthWrite = function (enable) {
  1130. this._depthCullingState.depthMask = enable;
  1131. };
  1132. Engine.prototype.setColorWrite = function (enable) {
  1133. this._gl.colorMask(enable, enable, enable, enable);
  1134. };
  1135. Engine.prototype.setAlphaMode = function (mode) {
  1136. switch (mode) {
  1137. case Engine.ALPHA_DISABLE:
  1138. this.setDepthWrite(true);
  1139. this._alphaState.alphaBlend = false;
  1140. break;
  1141. case Engine.ALPHA_COMBINE:
  1142. this.setDepthWrite(false);
  1143. this._alphaState.setAlphaBlendFunctionParameters(this._gl.SRC_ALPHA, this._gl.ONE_MINUS_SRC_ALPHA, this._gl.ONE, this._gl.ONE);
  1144. this._alphaState.alphaBlend = true;
  1145. break;
  1146. case Engine.ALPHA_ADD:
  1147. this.setDepthWrite(false);
  1148. this._alphaState.setAlphaBlendFunctionParameters(this._gl.ONE, this._gl.ONE, this._gl.ZERO, this._gl.ONE);
  1149. this._alphaState.alphaBlend = true;
  1150. break;
  1151. }
  1152. this._alphaMode = mode;
  1153. };
  1154. Engine.prototype.getAlphaMode = function () {
  1155. return this._alphaMode;
  1156. };
  1157. Engine.prototype.setAlphaTesting = function (enable) {
  1158. this._alphaTest = enable;
  1159. };
  1160. Engine.prototype.getAlphaTesting = function () {
  1161. return this._alphaTest;
  1162. };
  1163. // Textures
  1164. Engine.prototype.wipeCaches = function () {
  1165. this._activeTexturesCache = [];
  1166. this._currentEffect = null;
  1167. this._depthCullingState.reset();
  1168. this._alphaState.reset();
  1169. this._cachedVertexBuffers = null;
  1170. this._cachedIndexBuffer = null;
  1171. this._cachedEffectForVertexBuffers = null;
  1172. };
  1173. Engine.prototype.setSamplingMode = function (texture, samplingMode) {
  1174. var gl = this._gl;
  1175. gl.bindTexture(gl.TEXTURE_2D, texture);
  1176. var magFilter = gl.NEAREST;
  1177. var minFilter = gl.NEAREST;
  1178. if (samplingMode === BABYLON.Texture.BILINEAR_SAMPLINGMODE) {
  1179. magFilter = gl.LINEAR;
  1180. minFilter = gl.LINEAR;
  1181. }
  1182. else if (samplingMode === BABYLON.Texture.TRILINEAR_SAMPLINGMODE) {
  1183. magFilter = gl.LINEAR;
  1184. minFilter = gl.LINEAR_MIPMAP_LINEAR;
  1185. }
  1186. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, magFilter);
  1187. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, minFilter);
  1188. gl.bindTexture(gl.TEXTURE_2D, null);
  1189. texture.samplingMode = samplingMode;
  1190. };
  1191. Engine.prototype.createTexture = function (url, noMipmap, invertY, scene, samplingMode, onLoad, onError, buffer) {
  1192. var _this = this;
  1193. if (samplingMode === void 0) { samplingMode = BABYLON.Texture.TRILINEAR_SAMPLINGMODE; }
  1194. if (onLoad === void 0) { onLoad = null; }
  1195. if (onError === void 0) { onError = null; }
  1196. if (buffer === void 0) { buffer = null; }
  1197. var texture = this._gl.createTexture();
  1198. var extension;
  1199. var fromData = false;
  1200. if (url.substr(0, 5) === "data:") {
  1201. fromData = true;
  1202. }
  1203. if (!fromData)
  1204. extension = url.substr(url.length - 4, 4).toLowerCase();
  1205. else {
  1206. var oldUrl = url;
  1207. fromData = oldUrl.split(':');
  1208. url = oldUrl;
  1209. extension = fromData[1].substr(fromData[1].length - 4, 4).toLowerCase();
  1210. }
  1211. var isDDS = this.getCaps().s3tc && (extension === ".dds");
  1212. var isTGA = (extension === ".tga");
  1213. scene._addPendingData(texture);
  1214. texture.url = url;
  1215. texture.noMipmap = noMipmap;
  1216. texture.references = 1;
  1217. this._loadedTexturesCache.push(texture);
  1218. var onerror = function () {
  1219. scene._removePendingData(texture);
  1220. if (onError) {
  1221. onError();
  1222. }
  1223. };
  1224. if (isTGA) {
  1225. var callback = function (arrayBuffer) {
  1226. var data = new Uint8Array(arrayBuffer);
  1227. var header = BABYLON.Internals.TGATools.GetTGAHeader(data);
  1228. prepareWebGLTexture(texture, _this._gl, scene, header.width, header.height, invertY, noMipmap, false, function () {
  1229. BABYLON.Internals.TGATools.UploadContent(_this._gl, data);
  1230. if (onLoad) {
  1231. onLoad();
  1232. }
  1233. }, samplingMode);
  1234. };
  1235. if (!(fromData instanceof Array))
  1236. BABYLON.Tools.LoadFile(url, function (arrayBuffer) {
  1237. callback(arrayBuffer);
  1238. }, onerror, scene.database, true);
  1239. else
  1240. callback(buffer);
  1241. }
  1242. else if (isDDS) {
  1243. callback = function (data) {
  1244. var info = BABYLON.Internals.DDSTools.GetDDSInfo(data);
  1245. var loadMipmap = (info.isRGB || info.isLuminance || info.mipmapCount > 1) && !noMipmap && ((info.width >> (info.mipmapCount - 1)) === 1);
  1246. prepareWebGLTexture(texture, _this._gl, scene, info.width, info.height, invertY, !loadMipmap, info.isFourCC, function () {
  1247. BABYLON.Internals.DDSTools.UploadDDSLevels(_this._gl, _this.getCaps().s3tc, data, info, loadMipmap, 1);
  1248. if (onLoad) {
  1249. onLoad();
  1250. }
  1251. }, samplingMode);
  1252. };
  1253. if (!(fromData instanceof Array))
  1254. BABYLON.Tools.LoadFile(url, function (data) {
  1255. callback(data);
  1256. }, onerror, scene.database, true);
  1257. else
  1258. callback(buffer);
  1259. }
  1260. else {
  1261. var onload = function (img) {
  1262. prepareWebGLTexture(texture, _this._gl, scene, img.width, img.height, invertY, noMipmap, false, function (potWidth, potHeight) {
  1263. var isPot = (img.width === potWidth && img.height === potHeight);
  1264. if (!isPot) {
  1265. _this._workingCanvas.width = potWidth;
  1266. _this._workingCanvas.height = potHeight;
  1267. if (samplingMode === BABYLON.Texture.NEAREST_SAMPLINGMODE) {
  1268. _this._workingContext.imageSmoothingEnabled = false;
  1269. _this._workingContext.mozImageSmoothingEnabled = false;
  1270. _this._workingContext.oImageSmoothingEnabled = false;
  1271. _this._workingContext.webkitImageSmoothingEnabled = false;
  1272. _this._workingContext.msImageSmoothingEnabled = false;
  1273. }
  1274. _this._workingContext.drawImage(img, 0, 0, img.width, img.height, 0, 0, potWidth, potHeight);
  1275. if (samplingMode === BABYLON.Texture.NEAREST_SAMPLINGMODE) {
  1276. _this._workingContext.imageSmoothingEnabled = true;
  1277. _this._workingContext.mozImageSmoothingEnabled = true;
  1278. _this._workingContext.oImageSmoothingEnabled = true;
  1279. _this._workingContext.webkitImageSmoothingEnabled = true;
  1280. _this._workingContext.msImageSmoothingEnabled = true;
  1281. }
  1282. }
  1283. _this._gl.texImage2D(_this._gl.TEXTURE_2D, 0, _this._gl.RGBA, _this._gl.RGBA, _this._gl.UNSIGNED_BYTE, isPot ? img : _this._workingCanvas);
  1284. if (onLoad) {
  1285. onLoad();
  1286. }
  1287. }, samplingMode);
  1288. };
  1289. if (!(fromData instanceof Array))
  1290. BABYLON.Tools.LoadImage(url, onload, onerror, scene.database);
  1291. else
  1292. BABYLON.Tools.LoadImage(buffer, onload, onerror, scene.database);
  1293. }
  1294. return texture;
  1295. };
  1296. Engine.prototype.createRawTexture = function (data, width, height, format, generateMipMaps, invertY, samplingMode) {
  1297. var texture = this._gl.createTexture();
  1298. this._gl.bindTexture(this._gl.TEXTURE_2D, texture);
  1299. this._gl.pixelStorei(this._gl.UNPACK_FLIP_Y_WEBGL, invertY === undefined ? 1 : (invertY ? 1 : 0));
  1300. // Format
  1301. var internalFormat = this._gl.RGBA;
  1302. switch (format) {
  1303. case Engine.TEXTUREFORMAT_ALPHA:
  1304. internalFormat = this._gl.ALPHA;
  1305. break;
  1306. case Engine.TEXTUREFORMAT_LUMINANCE:
  1307. internalFormat = this._gl.LUMINANCE;
  1308. break;
  1309. case Engine.TEXTUREFORMAT_LUMINANCE_ALPHA:
  1310. internalFormat = this._gl.LUMINANCE_ALPHA;
  1311. break;
  1312. case Engine.TEXTUREFORMAT_RGB:
  1313. internalFormat = this._gl.RGB;
  1314. break;
  1315. case Engine.TEXTUREFORMAT_RGBA:
  1316. internalFormat = this._gl.RGBA;
  1317. break;
  1318. }
  1319. this._gl.texImage2D(this._gl.TEXTURE_2D, 0, internalFormat, width, height, 0, internalFormat, this._gl.UNSIGNED_BYTE, data);
  1320. if (generateMipMaps) {
  1321. this._gl.generateMipmap(this._gl.TEXTURE_2D);
  1322. }
  1323. // Filters
  1324. var filters = getSamplingParameters(samplingMode, generateMipMaps, this._gl);
  1325. this._gl.texParameteri(this._gl.TEXTURE_2D, this._gl.TEXTURE_MAG_FILTER, filters.mag);
  1326. this._gl.texParameteri(this._gl.TEXTURE_2D, this._gl.TEXTURE_MIN_FILTER, filters.min);
  1327. this._gl.bindTexture(this._gl.TEXTURE_2D, null);
  1328. this._activeTexturesCache = [];
  1329. texture._baseWidth = width;
  1330. texture._baseHeight = height;
  1331. texture._width = width;
  1332. texture._height = height;
  1333. texture.isReady = true;
  1334. texture.references = 1;
  1335. texture.samplingMode = samplingMode;
  1336. this._loadedTexturesCache.push(texture);
  1337. return texture;
  1338. };
  1339. Engine.prototype.createDynamicTexture = function (width, height, generateMipMaps, samplingMode) {
  1340. var texture = this._gl.createTexture();
  1341. width = BABYLON.Tools.GetExponantOfTwo(width, this._caps.maxTextureSize);
  1342. height = BABYLON.Tools.GetExponantOfTwo(height, this._caps.maxTextureSize);
  1343. this._gl.bindTexture(this._gl.TEXTURE_2D, texture);
  1344. var filters = getSamplingParameters(samplingMode, generateMipMaps, this._gl);
  1345. this._gl.texParameteri(this._gl.TEXTURE_2D, this._gl.TEXTURE_MAG_FILTER, filters.mag);
  1346. this._gl.texParameteri(this._gl.TEXTURE_2D, this._gl.TEXTURE_MIN_FILTER, filters.min);
  1347. this._gl.bindTexture(this._gl.TEXTURE_2D, null);
  1348. this._activeTexturesCache = [];
  1349. texture._baseWidth = width;
  1350. texture._baseHeight = height;
  1351. texture._width = width;
  1352. texture._height = height;
  1353. texture.isReady = false;
  1354. texture.generateMipMaps = generateMipMaps;
  1355. texture.references = 1;
  1356. texture.samplingMode = samplingMode;
  1357. this._loadedTexturesCache.push(texture);
  1358. return texture;
  1359. };
  1360. Engine.prototype.updateDynamicTexture = function (texture, canvas, invertY) {
  1361. this._gl.bindTexture(this._gl.TEXTURE_2D, texture);
  1362. this._gl.pixelStorei(this._gl.UNPACK_FLIP_Y_WEBGL, invertY ? 1 : 0);
  1363. this._gl.texImage2D(this._gl.TEXTURE_2D, 0, this._gl.RGBA, this._gl.RGBA, this._gl.UNSIGNED_BYTE, canvas);
  1364. if (texture.generateMipMaps) {
  1365. this._gl.generateMipmap(this._gl.TEXTURE_2D);
  1366. }
  1367. this._gl.bindTexture(this._gl.TEXTURE_2D, null);
  1368. this._activeTexturesCache = [];
  1369. texture.isReady = true;
  1370. };
  1371. Engine.prototype.updateVideoTexture = function (texture, video, invertY) {
  1372. this._gl.bindTexture(this._gl.TEXTURE_2D, texture);
  1373. this._gl.pixelStorei(this._gl.UNPACK_FLIP_Y_WEBGL, invertY ? 0 : 1); // Video are upside down by default
  1374. // Scale the video if it is a NPOT using the current working canvas
  1375. if (video.videoWidth !== texture._width || video.videoHeight !== texture._height) {
  1376. if (!texture._workingCanvas) {
  1377. texture._workingCanvas = document.createElement("canvas");
  1378. texture._workingContext = texture._workingCanvas.getContext("2d");
  1379. texture._workingCanvas.width = texture._width;
  1380. texture._workingCanvas.height = texture._height;
  1381. }
  1382. texture._workingContext.drawImage(video, 0, 0, video.videoWidth, video.videoHeight, 0, 0, texture._width, texture._height);
  1383. this._gl.texImage2D(this._gl.TEXTURE_2D, 0, this._gl.RGBA, this._gl.RGBA, this._gl.UNSIGNED_BYTE, texture._workingCanvas);
  1384. }
  1385. else {
  1386. this._gl.texImage2D(this._gl.TEXTURE_2D, 0, this._gl.RGBA, this._gl.RGBA, this._gl.UNSIGNED_BYTE, video);
  1387. }
  1388. if (texture.generateMipMaps) {
  1389. this._gl.generateMipmap(this._gl.TEXTURE_2D);
  1390. }
  1391. this._gl.bindTexture(this._gl.TEXTURE_2D, null);
  1392. this._activeTexturesCache = [];
  1393. texture.isReady = true;
  1394. };
  1395. Engine.prototype.createRenderTargetTexture = function (size, options) {
  1396. // old version had a "generateMipMaps" arg instead of options.
  1397. // if options.generateMipMaps is undefined, consider that options itself if the generateMipmaps value
  1398. // in the same way, generateDepthBuffer is defaulted to true
  1399. var generateMipMaps = false;
  1400. var generateDepthBuffer = true;
  1401. var type = Engine.TEXTURETYPE_UNSIGNED_INT;
  1402. var samplingMode = BABYLON.Texture.TRILINEAR_SAMPLINGMODE;
  1403. if (options !== undefined) {
  1404. generateMipMaps = options.generateMipMaps === undefined ? options : options.generateMipmaps;
  1405. generateDepthBuffer = options.generateDepthBuffer === undefined ? true : options.generateDepthBuffer;
  1406. type = options.type === undefined ? type : options.type;
  1407. if (options.samplingMode !== undefined) {
  1408. samplingMode = options.samplingMode;
  1409. }
  1410. if (type === Engine.TEXTURETYPE_FLOAT) {
  1411. // if floating point (gl.FLOAT) then force to NEAREST_SAMPLINGMODE
  1412. samplingMode = BABYLON.Texture.NEAREST_SAMPLINGMODE;
  1413. }
  1414. }
  1415. var gl = this._gl;
  1416. var texture = gl.createTexture();
  1417. gl.bindTexture(gl.TEXTURE_2D, texture);
  1418. var width = size.width || size;
  1419. var height = size.height || size;
  1420. var filters = getSamplingParameters(samplingMode, generateMipMaps, gl);
  1421. if (type === Engine.TEXTURETYPE_FLOAT && !this._caps.textureFloat) {
  1422. type = Engine.TEXTURETYPE_UNSIGNED_INT;
  1423. BABYLON.Tools.Warn("Float textures are not supported. Render target forced to TEXTURETYPE_UNSIGNED_BYTE type");
  1424. }
  1425. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, filters.mag);
  1426. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, filters.min);
  1427. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
  1428. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
  1429. gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, width, height, 0, gl.RGBA, getWebGLTextureType(gl, type), null);
  1430. var depthBuffer;
  1431. // Create the depth buffer
  1432. if (generateDepthBuffer) {
  1433. depthBuffer = gl.createRenderbuffer();
  1434. gl.bindRenderbuffer(gl.RENDERBUFFER, depthBuffer);
  1435. gl.renderbufferStorage(gl.RENDERBUFFER, gl.DEPTH_COMPONENT16, width, height);
  1436. }
  1437. // Create the framebuffer
  1438. var framebuffer = gl.createFramebuffer();
  1439. gl.bindFramebuffer(gl.FRAMEBUFFER, framebuffer);
  1440. gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, texture, 0);
  1441. if (generateDepthBuffer) {
  1442. gl.framebufferRenderbuffer(gl.FRAMEBUFFER, gl.DEPTH_ATTACHMENT, gl.RENDERBUFFER, depthBuffer);
  1443. }
  1444. // Unbind
  1445. gl.bindTexture(gl.TEXTURE_2D, null);
  1446. gl.bindRenderbuffer(gl.RENDERBUFFER, null);
  1447. gl.bindFramebuffer(gl.FRAMEBUFFER, null);
  1448. texture._framebuffer = framebuffer;
  1449. if (generateDepthBuffer) {
  1450. texture._depthBuffer = depthBuffer;
  1451. }
  1452. texture._width = width;
  1453. texture._height = height;
  1454. texture.isReady = true;
  1455. texture.generateMipMaps = generateMipMaps;
  1456. texture.references = 1;
  1457. texture.samplingMode = samplingMode;
  1458. this._activeTexturesCache = [];
  1459. this._loadedTexturesCache.push(texture);
  1460. return texture;
  1461. };
  1462. Engine.prototype.createCubeTexture = function (rootUrl, scene, extensions, noMipmap) {
  1463. var _this = this;
  1464. var gl = this._gl;
  1465. var texture = gl.createTexture();
  1466. texture.isCube = true;
  1467. texture.url = rootUrl;
  1468. texture.references = 1;
  1469. this._loadedTexturesCache.push(texture);
  1470. var extension = rootUrl.substr(rootUrl.length - 4, 4).toLowerCase();
  1471. var isDDS = this.getCaps().s3tc && (extension === ".dds");
  1472. if (isDDS) {
  1473. BABYLON.Tools.LoadFile(rootUrl, function (data) {
  1474. var info = BABYLON.Internals.DDSTools.GetDDSInfo(data);
  1475. var loadMipmap = (info.isRGB || info.isLuminance || info.mipmapCount > 1) && !noMipmap;
  1476. gl.bindTexture(gl.TEXTURE_CUBE_MAP, texture);
  1477. gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, 1);
  1478. BABYLON.Internals.DDSTools.UploadDDSLevels(_this._gl, _this.getCaps().s3tc, data, info, loadMipmap, 6);
  1479. if (!noMipmap && !info.isFourCC && info.mipmapCount === 1) {
  1480. gl.generateMipmap(gl.TEXTURE_CUBE_MAP);
  1481. }
  1482. gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
  1483. gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_MIN_FILTER, loadMipmap ? gl.LINEAR_MIPMAP_LINEAR : gl.LINEAR);
  1484. gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
  1485. gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
  1486. gl.bindTexture(gl.TEXTURE_CUBE_MAP, null);
  1487. _this._activeTexturesCache = [];
  1488. texture._width = info.width;
  1489. texture._height = info.height;
  1490. texture.isReady = true;
  1491. }, null, null, true);
  1492. }
  1493. else {
  1494. cascadeLoad(rootUrl, scene, function (imgs) {
  1495. var width = BABYLON.Tools.GetExponantOfTwo(imgs[0].width, _this._caps.maxCubemapTextureSize);
  1496. var height = width;
  1497. _this._workingCanvas.width = width;
  1498. _this._workingCanvas.height = height;
  1499. var faces = [
  1500. gl.TEXTURE_CUBE_MAP_POSITIVE_X,
  1501. gl.TEXTURE_CUBE_MAP_POSITIVE_Y,
  1502. gl.TEXTURE_CUBE_MAP_POSITIVE_Z,
  1503. gl.TEXTURE_CUBE_MAP_NEGATIVE_X,
  1504. gl.TEXTURE_CUBE_MAP_NEGATIVE_Y,
  1505. gl.TEXTURE_CUBE_MAP_NEGATIVE_Z
  1506. ];
  1507. gl.bindTexture(gl.TEXTURE_CUBE_MAP, texture);
  1508. gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, 0);
  1509. for (var index = 0; index < faces.length; index++) {
  1510. _this._workingContext.drawImage(imgs[index], 0, 0, imgs[index].width, imgs[index].height, 0, 0, width, height);
  1511. gl.texImage2D(faces[index], 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, _this._workingCanvas);
  1512. }
  1513. if (!noMipmap) {
  1514. gl.generateMipmap(gl.TEXTURE_CUBE_MAP);
  1515. }
  1516. gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
  1517. gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_MIN_FILTER, noMipmap ? gl.LINEAR : gl.LINEAR_MIPMAP_LINEAR);
  1518. gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
  1519. gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
  1520. gl.bindTexture(gl.TEXTURE_CUBE_MAP, null);
  1521. _this._activeTexturesCache = [];
  1522. texture._width = width;
  1523. texture._height = height;
  1524. texture.isReady = true;
  1525. }, extensions);
  1526. }
  1527. return texture;
  1528. };
  1529. Engine.prototype._releaseTexture = function (texture) {
  1530. var gl = this._gl;
  1531. if (texture._framebuffer) {
  1532. gl.deleteFramebuffer(texture._framebuffer);
  1533. }
  1534. if (texture._depthBuffer) {
  1535. gl.deleteRenderbuffer(texture._depthBuffer);
  1536. }
  1537. gl.deleteTexture(texture);
  1538. for (var channel = 0; channel < this._caps.maxTexturesImageUnits; channel++) {
  1539. this._gl.activeTexture(this._gl["TEXTURE" + channel]);
  1540. this._gl.bindTexture(this._gl.TEXTURE_2D, null);
  1541. this._gl.bindTexture(this._gl.TEXTURE_CUBE_MAP, null);
  1542. this._activeTexturesCache[channel] = null;
  1543. }
  1544. var index = this._loadedTexturesCache.indexOf(texture);
  1545. if (index !== -1) {
  1546. this._loadedTexturesCache.splice(index, 1);
  1547. }
  1548. };
  1549. Engine.prototype.bindSamplers = function (effect) {
  1550. this._gl.useProgram(effect.getProgram());
  1551. var samplers = effect.getSamplers();
  1552. for (var index = 0; index < samplers.length; index++) {
  1553. var uniform = effect.getUniform(samplers[index]);
  1554. this._gl.uniform1i(uniform, index);
  1555. }
  1556. this._currentEffect = null;
  1557. };
  1558. Engine.prototype._bindTexture = function (channel, texture) {
  1559. this._gl.activeTexture(this._gl["TEXTURE" + channel]);
  1560. this._gl.bindTexture(this._gl.TEXTURE_2D, texture);
  1561. this._activeTexturesCache[channel] = null;
  1562. };
  1563. Engine.prototype.setTextureFromPostProcess = function (channel, postProcess) {
  1564. this._bindTexture(channel, postProcess._textures.data[postProcess._currentRenderTextureInd]);
  1565. };
  1566. Engine.prototype.setTexture = function (channel, texture) {
  1567. if (channel < 0) {
  1568. return;
  1569. }
  1570. // Not ready?
  1571. if (!texture || !texture.isReady()) {
  1572. if (this._activeTexturesCache[channel] != null) {
  1573. this._gl.activeTexture(this._gl["TEXTURE" + channel]);
  1574. this._gl.bindTexture(this._gl.TEXTURE_2D, null);
  1575. this._gl.bindTexture(this._gl.TEXTURE_CUBE_MAP, null);
  1576. this._activeTexturesCache[channel] = null;
  1577. }
  1578. return;
  1579. }
  1580. // Video
  1581. if (texture instanceof BABYLON.VideoTexture) {
  1582. if (texture.update()) {
  1583. this._activeTexturesCache[channel] = null;
  1584. }
  1585. }
  1586. else if (texture.delayLoadState === Engine.DELAYLOADSTATE_NOTLOADED) {
  1587. texture.delayLoad();
  1588. return;
  1589. }
  1590. if (this._activeTexturesCache[channel] === texture) {
  1591. return;
  1592. }
  1593. this._activeTexturesCache[channel] = texture;
  1594. var internalTexture = texture.getInternalTexture();
  1595. this._gl.activeTexture(this._gl["TEXTURE" + channel]);
  1596. if (internalTexture.isCube) {
  1597. this._gl.bindTexture(this._gl.TEXTURE_CUBE_MAP, internalTexture);
  1598. if (internalTexture._cachedCoordinatesMode !== texture.coordinatesMode) {
  1599. internalTexture._cachedCoordinatesMode = texture.coordinatesMode;
  1600. // CUBIC_MODE and SKYBOX_MODE both require CLAMP_TO_EDGE. All other modes use REPEAT.
  1601. var textureWrapMode = (texture.coordinatesMode !== BABYLON.Texture.CUBIC_MODE && texture.coordinatesMode !== BABYLON.Texture.SKYBOX_MODE) ? this._gl.REPEAT : this._gl.CLAMP_TO_EDGE;
  1602. this._gl.texParameteri(this._gl.TEXTURE_CUBE_MAP, this._gl.TEXTURE_WRAP_S, textureWrapMode);
  1603. this._gl.texParameteri(this._gl.TEXTURE_CUBE_MAP, this._gl.TEXTURE_WRAP_T, textureWrapMode);
  1604. }
  1605. this._setAnisotropicLevel(this._gl.TEXTURE_CUBE_MAP, texture);
  1606. }
  1607. else {
  1608. this._gl.bindTexture(this._gl.TEXTURE_2D, internalTexture);
  1609. if (internalTexture._cachedWrapU !== texture.wrapU) {
  1610. internalTexture._cachedWrapU = texture.wrapU;
  1611. switch (texture.wrapU) {
  1612. case BABYLON.Texture.WRAP_ADDRESSMODE:
  1613. this._gl.texParameteri(this._gl.TEXTURE_2D, this._gl.TEXTURE_WRAP_S, this._gl.REPEAT);
  1614. break;
  1615. case BABYLON.Texture.CLAMP_ADDRESSMODE:
  1616. this._gl.texParameteri(this._gl.TEXTURE_2D, this._gl.TEXTURE_WRAP_S, this._gl.CLAMP_TO_EDGE);
  1617. break;
  1618. case BABYLON.Texture.MIRROR_ADDRESSMODE:
  1619. this._gl.texParameteri(this._gl.TEXTURE_2D, this._gl.TEXTURE_WRAP_S, this._gl.MIRRORED_REPEAT);
  1620. break;
  1621. }
  1622. }
  1623. if (internalTexture._cachedWrapV !== texture.wrapV) {
  1624. internalTexture._cachedWrapV = texture.wrapV;
  1625. switch (texture.wrapV) {
  1626. case BABYLON.Texture.WRAP_ADDRESSMODE:
  1627. this._gl.texParameteri(this._gl.TEXTURE_2D, this._gl.TEXTURE_WRAP_T, this._gl.REPEAT);
  1628. break;
  1629. case BABYLON.Texture.CLAMP_ADDRESSMODE:
  1630. this._gl.texParameteri(this._gl.TEXTURE_2D, this._gl.TEXTURE_WRAP_T, this._gl.CLAMP_TO_EDGE);
  1631. break;
  1632. case BABYLON.Texture.MIRROR_ADDRESSMODE:
  1633. this._gl.texParameteri(this._gl.TEXTURE_2D, this._gl.TEXTURE_WRAP_T, this._gl.MIRRORED_REPEAT);
  1634. break;
  1635. }
  1636. }
  1637. this._setAnisotropicLevel(this._gl.TEXTURE_2D, texture);
  1638. }
  1639. };
  1640. Engine.prototype._setAnisotropicLevel = function (key, texture) {
  1641. var anisotropicFilterExtension = this._caps.textureAnisotropicFilterExtension;
  1642. if (anisotropicFilterExtension && texture._cachedAnisotropicFilteringLevel !== texture.anisotropicFilteringLevel) {
  1643. this._gl.texParameterf(key, anisotropicFilterExtension.TEXTURE_MAX_ANISOTROPY_EXT, Math.min(texture.anisotropicFilteringLevel, this._caps.maxAnisotropy));
  1644. texture._cachedAnisotropicFilteringLevel = texture.anisotropicFilteringLevel;
  1645. }
  1646. };
  1647. Engine.prototype.readPixels = function (x, y, width, height) {
  1648. var data = new Uint8Array(height * width * 4);
  1649. this._gl.readPixels(0, 0, width, height, this._gl.RGBA, this._gl.UNSIGNED_BYTE, data);
  1650. return data;
  1651. };
  1652. // Dispose
  1653. Engine.prototype.dispose = function () {
  1654. this.hideLoadingUI();
  1655. this.stopRenderLoop();
  1656. while (this.scenes.length) {
  1657. this.scenes[0].dispose();
  1658. }
  1659. // Release audio engine
  1660. Engine.audioEngine.dispose();
  1661. for (var name in this._compiledEffects) {
  1662. this._gl.deleteProgram(this._compiledEffects[name]._program);
  1663. }
  1664. for (var i in this._vertexAttribArrays) {
  1665. if (i > this._gl.VERTEX_ATTRIB_ARRAY_ENABLED || !this._vertexAttribArrays[i]) {
  1666. continue;
  1667. }
  1668. this._gl.disableVertexAttribArray(i);
  1669. }
  1670. // Events
  1671. window.removeEventListener("blur", this._onBlur);
  1672. window.removeEventListener("focus", this._onFocus);
  1673. document.removeEventListener("fullscreenchange", this._onFullscreenChange);
  1674. document.removeEventListener("mozfullscreenchange", this._onFullscreenChange);
  1675. document.removeEventListener("webkitfullscreenchange", this._onFullscreenChange);
  1676. document.removeEventListener("msfullscreenchange", this._onFullscreenChange);
  1677. document.removeEventListener("pointerlockchange", this._onPointerLockChange);
  1678. document.removeEventListener("mspointerlockchange", this._onPointerLockChange);
  1679. document.removeEventListener("mozpointerlockchange", this._onPointerLockChange);
  1680. document.removeEventListener("webkitpointerlockchange", this._onPointerLockChange);
  1681. };
  1682. // Loading screen
  1683. Engine.prototype.displayLoadingUI = function () {
  1684. var _this = this;
  1685. this._loadingDiv = document.createElement("div");
  1686. this._loadingDiv.style.opacity = "0";
  1687. this._loadingDiv.style.transition = "opacity 1.5s ease";
  1688. // Loading text
  1689. this._loadingTextDiv = document.createElement("div");
  1690. this._loadingTextDiv.style.position = "absolute";
  1691. this._loadingTextDiv.style.left = "0";
  1692. this._loadingTextDiv.style.top = "50%";
  1693. this._loadingTextDiv.style.marginTop = "80px";
  1694. this._loadingTextDiv.style.width = "100%";
  1695. this._loadingTextDiv.style.height = "20px";
  1696. this._loadingTextDiv.style.fontFamily = "Arial";
  1697. this._loadingTextDiv.style.fontSize = "14px";
  1698. this._loadingTextDiv.style.color = "white";
  1699. this._loadingTextDiv.style.textAlign = "center";
  1700. this._loadingTextDiv.innerHTML = "Loading";
  1701. this._loadingDiv.appendChild(this._loadingTextDiv);
  1702. // Loading img
  1703. var imgBack = new Image();
  1704. 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==";
  1705. imgBack.style.position = "absolute";
  1706. imgBack.style.left = "50%";
  1707. imgBack.style.top = "50%";
  1708. imgBack.style.marginLeft = "-50px";
  1709. imgBack.style.marginTop = "-50px";
  1710. imgBack.style.transition = "transform 1.0s ease";
  1711. imgBack.style.webkitTransition = "-webkit-transform 1.0s ease";
  1712. var deg = 360;
  1713. var onTransitionEnd = function () {
  1714. deg += 360;
  1715. imgBack.style.transform = "rotateZ(" + deg + "deg)";
  1716. imgBack.style.webkitTransform = "rotateZ(" + deg + "deg)";
  1717. };
  1718. imgBack.addEventListener("transitionend", onTransitionEnd);
  1719. imgBack.addEventListener("webkitTransitionEnd", onTransitionEnd);
  1720. this._loadingDiv.appendChild(imgBack);
  1721. // front image
  1722. var imgFront = new Image();
  1723. 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=";
  1724. imgFront.style.position = "absolute";
  1725. imgFront.style.left = "50%";
  1726. imgFront.style.top = "50%";
  1727. imgFront.style.marginLeft = "-50px";
  1728. imgFront.style.marginTop = "-50px";
  1729. this._loadingDiv.appendChild(imgFront);
  1730. // Resize
  1731. this._resizeLoadingUI = function () {
  1732. var canvasRect = _this.getRenderingCanvasClientRect();
  1733. _this._loadingDiv.style.position = "absolute";
  1734. _this._loadingDiv.style.left = canvasRect.left + "px";
  1735. _this._loadingDiv.style.top = canvasRect.top + "px";
  1736. _this._loadingDiv.style.width = canvasRect.width + "px";
  1737. _this._loadingDiv.style.height = canvasRect.height + "px";
  1738. };
  1739. this._resizeLoadingUI();
  1740. window.addEventListener("resize", this._resizeLoadingUI);
  1741. this._loadingDiv.style.backgroundColor = this._loadingDivBackgroundColor;
  1742. document.body.appendChild(this._loadingDiv);
  1743. setTimeout(function () {
  1744. _this._loadingDiv.style.opacity = "1";
  1745. imgBack.style.transform = "rotateZ(360deg)";
  1746. imgBack.style.webkitTransform = "rotateZ(360deg)";
  1747. }, 0);
  1748. };
  1749. Object.defineProperty(Engine.prototype, "loadingUIText", {
  1750. set: function (text) {
  1751. if (!this._loadingDiv) {
  1752. return;
  1753. }
  1754. this._loadingTextDiv.innerHTML = text;
  1755. },
  1756. enumerable: true,
  1757. configurable: true
  1758. });
  1759. Object.defineProperty(Engine.prototype, "loadingUIBackgroundColor", {
  1760. get: function () {
  1761. return this._loadingDivBackgroundColor;
  1762. },
  1763. set: function (color) {
  1764. this._loadingDivBackgroundColor = color;
  1765. if (!this._loadingDiv) {
  1766. return;
  1767. }
  1768. this._loadingDiv.style.backgroundColor = this._loadingDivBackgroundColor;
  1769. },
  1770. enumerable: true,
  1771. configurable: true
  1772. });
  1773. Engine.prototype.hideLoadingUI = function () {
  1774. var _this = this;
  1775. if (!this._loadingDiv) {
  1776. return;
  1777. }
  1778. var onTransitionEnd = function () {
  1779. if (!_this._loadingDiv) {
  1780. return;
  1781. }
  1782. document.body.removeChild(_this._loadingDiv);
  1783. window.removeEventListener("resize", _this._resizeLoadingUI);
  1784. _this._loadingDiv = null;
  1785. };
  1786. this._loadingDiv.style.opacity = "0";
  1787. this._loadingDiv.addEventListener("transitionend", onTransitionEnd);
  1788. };
  1789. // FPS
  1790. Engine.prototype.getFps = function () {
  1791. return this.fps;
  1792. };
  1793. Engine.prototype.getDeltaTime = function () {
  1794. return this.deltaTime;
  1795. };
  1796. Engine.prototype._measureFps = function () {
  1797. this.previousFramesDuration.push(BABYLON.Tools.Now);
  1798. var length = this.previousFramesDuration.length;
  1799. if (length >= 2) {
  1800. this.deltaTime = this.previousFramesDuration[length - 1] - this.previousFramesDuration[length - 2];
  1801. }
  1802. if (length >= this.fpsRange) {
  1803. if (length > this.fpsRange) {
  1804. this.previousFramesDuration.splice(0, 1);
  1805. length = this.previousFramesDuration.length;
  1806. }
  1807. var sum = 0;
  1808. for (var id = 0; id < length - 1; id++) {
  1809. sum += this.previousFramesDuration[id + 1] - this.previousFramesDuration[id];
  1810. }
  1811. this.fps = 1000.0 / (sum / (length - 1));
  1812. }
  1813. };
  1814. // Statics
  1815. Engine.isSupported = function () {
  1816. try {
  1817. // Avoid creating an unsized context for CocoonJS, since size determined on first creation. Is not resizable
  1818. if (navigator.isCocoonJS) {
  1819. return true;
  1820. }
  1821. var tempcanvas = document.createElement("canvas");
  1822. var gl = tempcanvas.getContext("webgl") || tempcanvas.getContext("experimental-webgl");
  1823. return gl != null && !!window.WebGLRenderingContext;
  1824. }
  1825. catch (e) {
  1826. return false;
  1827. }
  1828. };
  1829. // Const statics
  1830. Engine._ALPHA_DISABLE = 0;
  1831. Engine._ALPHA_ADD = 1;
  1832. Engine._ALPHA_COMBINE = 2;
  1833. Engine._DELAYLOADSTATE_NONE = 0;
  1834. Engine._DELAYLOADSTATE_LOADED = 1;
  1835. Engine._DELAYLOADSTATE_LOADING = 2;
  1836. Engine._DELAYLOADSTATE_NOTLOADED = 4;
  1837. Engine._TEXTUREFORMAT_ALPHA = 0;
  1838. Engine._TEXTUREFORMAT_LUMINANCE = 1;
  1839. Engine._TEXTUREFORMAT_LUMINANCE_ALPHA = 2;
  1840. Engine._TEXTUREFORMAT_RGB = 4;
  1841. Engine._TEXTUREFORMAT_RGBA = 4;
  1842. Engine._TEXTURETYPE_UNSIGNED_INT = 0;
  1843. Engine._TEXTURETYPE_FLOAT = 1;
  1844. // Updatable statics so stick with vars here
  1845. Engine.Epsilon = 0.001;
  1846. Engine.CollisionsEpsilon = 0.001;
  1847. Engine.ShadersRepository = "Babylon/Shaders/";
  1848. return Engine;
  1849. })();
  1850. BABYLON.Engine = Engine;
  1851. })(BABYLON || (BABYLON = {}));
  1852. //# sourceMappingURL=babylon.engine.js.map