babylon.engine.js 76 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846
  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 === true) {
  107. gl.enable(gl.CULL_FACE);
  108. } else if (this.cull === false) {
  109. gl.disable(gl.CULL_FACE);
  110. }
  111. this._isCullDirty = false;
  112. }
  113. // Cull face
  114. if (this._isCullFaceDirty) {
  115. gl.cullFace(this.cullFace);
  116. this._isCullFaceDirty = false;
  117. }
  118. // Depth mask
  119. if (this._isDepthMaskDirty) {
  120. gl.depthMask(this.depthMask);
  121. this._isDepthMaskDirty = false;
  122. }
  123. // Depth test
  124. if (this._isDepthTestDirty) {
  125. if (this.depthTest === true) {
  126. gl.enable(gl.DEPTH_TEST);
  127. } else if (this.depthTest === false) {
  128. gl.disable(gl.DEPTH_TEST);
  129. }
  130. this._isDepthTestDirty = false;
  131. }
  132. // Depth func
  133. if (this._isDepthFuncDirty) {
  134. gl.depthFunc(this.depthFunc);
  135. this._isDepthFuncDirty = false;
  136. }
  137. };
  138. return _DepthCullingState;
  139. })();
  140. BABYLON._DepthCullingState = _DepthCullingState;
  141. var _AlphaState = (function () {
  142. function _AlphaState() {
  143. this._isAlphaBlendDirty = false;
  144. this._isBlendFunctionParametersDirty = false;
  145. this._alphaBlend = false;
  146. this._blendFunctionParameters = new Array(4);
  147. }
  148. Object.defineProperty(_AlphaState.prototype, "isDirty", {
  149. get: function () {
  150. return this._isAlphaBlendDirty || this._isBlendFunctionParametersDirty;
  151. },
  152. enumerable: true,
  153. configurable: true
  154. });
  155. Object.defineProperty(_AlphaState.prototype, "alphaBlend", {
  156. get: function () {
  157. return this._alphaBlend;
  158. },
  159. set: function (value) {
  160. if (this._alphaBlend === value) {
  161. return;
  162. }
  163. this._alphaBlend = value;
  164. this._isAlphaBlendDirty = true;
  165. },
  166. enumerable: true,
  167. configurable: true
  168. });
  169. _AlphaState.prototype.setAlphaBlendFunctionParameters = function (value0, value1, value2, value3) {
  170. if (this._blendFunctionParameters[0] === value0 && this._blendFunctionParameters[1] === value1 && this._blendFunctionParameters[2] === value2 && this._blendFunctionParameters[3] === value3) {
  171. return;
  172. }
  173. this._blendFunctionParameters[0] = value0;
  174. this._blendFunctionParameters[1] = value1;
  175. this._blendFunctionParameters[2] = value2;
  176. this._blendFunctionParameters[3] = value3;
  177. this._isBlendFunctionParametersDirty = true;
  178. };
  179. _AlphaState.prototype.reset = function () {
  180. this._alphaBlend = false;
  181. this._blendFunctionParameters[0] = null;
  182. this._blendFunctionParameters[1] = null;
  183. this._blendFunctionParameters[2] = null;
  184. this._blendFunctionParameters[3] = null;
  185. this._isAlphaBlendDirty = true;
  186. this._isBlendFunctionParametersDirty = false;
  187. };
  188. _AlphaState.prototype.apply = function (gl) {
  189. if (!this.isDirty) {
  190. return;
  191. }
  192. // Alpha blend
  193. if (this._isAlphaBlendDirty) {
  194. if (this._alphaBlend === true) {
  195. gl.enable(gl.BLEND);
  196. } else if (this._alphaBlend === false) {
  197. gl.disable(gl.BLEND);
  198. }
  199. this._isAlphaBlendDirty = false;
  200. }
  201. // Alpha function
  202. if (this._isBlendFunctionParametersDirty) {
  203. gl.blendFuncSeparate(this._blendFunctionParameters[0], this._blendFunctionParameters[1], this._blendFunctionParameters[2], this._blendFunctionParameters[3]);
  204. this._isBlendFunctionParametersDirty = false;
  205. }
  206. };
  207. return _AlphaState;
  208. })();
  209. BABYLON._AlphaState = _AlphaState;
  210. var compileShader = function (gl, source, type, defines) {
  211. var shader = gl.createShader(type === "vertex" ? gl.VERTEX_SHADER : gl.FRAGMENT_SHADER);
  212. gl.shaderSource(shader, (defines ? defines + "\n" : "") + source);
  213. gl.compileShader(shader);
  214. if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
  215. throw new Error(gl.getShaderInfoLog(shader));
  216. }
  217. return shader;
  218. };
  219. var getSamplingParameters = function (samplingMode, generateMipMaps, gl) {
  220. var magFilter = gl.NEAREST;
  221. var minFilter = gl.NEAREST;
  222. if (samplingMode === BABYLON.Texture.BILINEAR_SAMPLINGMODE) {
  223. magFilter = gl.LINEAR;
  224. if (generateMipMaps) {
  225. minFilter = gl.LINEAR_MIPMAP_NEAREST;
  226. } else {
  227. minFilter = gl.LINEAR;
  228. }
  229. } else if (samplingMode === BABYLON.Texture.TRILINEAR_SAMPLINGMODE) {
  230. magFilter = gl.LINEAR;
  231. if (generateMipMaps) {
  232. minFilter = gl.LINEAR_MIPMAP_LINEAR;
  233. } else {
  234. minFilter = gl.LINEAR;
  235. }
  236. } else if (samplingMode === BABYLON.Texture.NEAREST_SAMPLINGMODE) {
  237. magFilter = gl.NEAREST;
  238. if (generateMipMaps) {
  239. minFilter = gl.NEAREST_MIPMAP_LINEAR;
  240. } else {
  241. minFilter = gl.NEAREST;
  242. }
  243. }
  244. return {
  245. min: minFilter,
  246. mag: magFilter
  247. };
  248. };
  249. var prepareWebGLTexture = function (texture, gl, scene, width, height, invertY, noMipmap, isCompressed, processFunction, samplingMode) {
  250. if (typeof samplingMode === "undefined") { samplingMode = BABYLON.Texture.TRILINEAR_SAMPLINGMODE; }
  251. var engine = scene.getEngine();
  252. var potWidth = BABYLON.Tools.GetExponantOfTwo(width, engine.getCaps().maxTextureSize);
  253. var potHeight = BABYLON.Tools.GetExponantOfTwo(height, engine.getCaps().maxTextureSize);
  254. gl.bindTexture(gl.TEXTURE_2D, texture);
  255. gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, invertY === undefined ? 1 : (invertY ? 1 : 0));
  256. processFunction(potWidth, potHeight);
  257. var filters = getSamplingParameters(samplingMode, !noMipmap, gl);
  258. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, filters.mag);
  259. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, filters.min);
  260. if (!noMipmap && !isCompressed) {
  261. gl.generateMipmap(gl.TEXTURE_2D);
  262. }
  263. gl.bindTexture(gl.TEXTURE_2D, null);
  264. engine._activeTexturesCache = [];
  265. texture._baseWidth = width;
  266. texture._baseHeight = height;
  267. texture._width = potWidth;
  268. texture._height = potHeight;
  269. texture.isReady = true;
  270. scene._removePendingData(texture);
  271. };
  272. // ANY
  273. var cascadeLoad = function (rootUrl, index, loadedImages, scene, onfinish, extensions) {
  274. var img;
  275. var onload = function () {
  276. loadedImages.push(img);
  277. scene._removePendingData(img);
  278. if (index != extensions.length - 1) {
  279. cascadeLoad(rootUrl, index + 1, loadedImages, scene, onfinish, extensions);
  280. } else {
  281. onfinish(loadedImages);
  282. }
  283. };
  284. var onerror = function () {
  285. scene._removePendingData(img);
  286. };
  287. img = BABYLON.Tools.LoadImage(rootUrl + extensions[index], onload, onerror, scene.database);
  288. scene._addPendingData(img);
  289. };
  290. var EngineCapabilities = (function () {
  291. function EngineCapabilities() {
  292. }
  293. return EngineCapabilities;
  294. })();
  295. BABYLON.EngineCapabilities = EngineCapabilities;
  296. var Engine = (function () {
  297. function Engine(canvas, antialias, options) {
  298. var _this = this;
  299. // Public members
  300. this.isFullscreen = false;
  301. this.isPointerLock = false;
  302. this.forceWireframe = false;
  303. this.cullBackFaces = true;
  304. this.renderEvenInBackground = true;
  305. this.scenes = new Array();
  306. this._windowIsBackground = false;
  307. this._runningLoop = false;
  308. this._loadingDivBackgroundColor = "black";
  309. // States
  310. this._depthCullingState = new _DepthCullingState();
  311. this._alphaState = new _AlphaState();
  312. // Cache
  313. this._loadedTexturesCache = new Array();
  314. this._activeTexturesCache = new Array();
  315. this._compiledEffects = {};
  316. this._renderingCanvas = canvas;
  317. this._canvasClientRect = this._renderingCanvas.getBoundingClientRect();
  318. options = options || {};
  319. options.antialias = antialias;
  320. try {
  321. this._gl = canvas.getContext("webgl", options) || canvas.getContext("experimental-webgl", options);
  322. } catch (e) {
  323. throw new Error("WebGL not supported");
  324. }
  325. if (!this._gl) {
  326. throw new Error("WebGL not supported");
  327. }
  328. this._onBlur = function () {
  329. _this._windowIsBackground = true;
  330. };
  331. this._onFocus = function () {
  332. _this._windowIsBackground = false;
  333. };
  334. window.addEventListener("blur", this._onBlur);
  335. window.addEventListener("focus", this._onFocus);
  336. // Textures
  337. this._workingCanvas = document.createElement("canvas");
  338. this._workingContext = this._workingCanvas.getContext("2d");
  339. // Viewport
  340. this._hardwareScalingLevel = 1.0 / (window.devicePixelRatio || 1.0);
  341. this.resize();
  342. // Caps
  343. this._caps = new EngineCapabilities();
  344. this._caps.maxTexturesImageUnits = this._gl.getParameter(this._gl.MAX_TEXTURE_IMAGE_UNITS);
  345. this._caps.maxTextureSize = this._gl.getParameter(this._gl.MAX_TEXTURE_SIZE);
  346. this._caps.maxCubemapTextureSize = this._gl.getParameter(this._gl.MAX_CUBE_MAP_TEXTURE_SIZE);
  347. this._caps.maxRenderTextureSize = this._gl.getParameter(this._gl.MAX_RENDERBUFFER_SIZE);
  348. // Extensions
  349. this._caps.standardDerivatives = (this._gl.getExtension('OES_standard_derivatives') !== null);
  350. this._caps.s3tc = this._gl.getExtension('WEBGL_compressed_texture_s3tc');
  351. this._caps.textureFloat = (this._gl.getExtension('OES_texture_float') !== null);
  352. 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');
  353. this._caps.maxAnisotropy = this._caps.textureAnisotropicFilterExtension ? this._gl.getParameter(this._caps.textureAnisotropicFilterExtension.MAX_TEXTURE_MAX_ANISOTROPY_EXT) : 0;
  354. this._caps.instancedArrays = this._gl.getExtension('ANGLE_instanced_arrays');
  355. // Depth buffer
  356. this.setDepthBuffer(true);
  357. this.setDepthFunctionToLessOrEqual();
  358. this.setDepthWrite(true);
  359. // Fullscreen
  360. this._onFullscreenChange = function () {
  361. if (document.fullscreen !== undefined) {
  362. _this.isFullscreen = document.fullscreen;
  363. } else if (document.mozFullScreen !== undefined) {
  364. _this.isFullscreen = document.mozFullScreen;
  365. } else if (document.webkitIsFullScreen !== undefined) {
  366. _this.isFullscreen = document.webkitIsFullScreen;
  367. } else if (document.msIsFullScreen !== undefined) {
  368. _this.isFullscreen = document.msIsFullScreen;
  369. }
  370. // Pointer lock
  371. if (_this.isFullscreen && _this._pointerLockRequested) {
  372. canvas.requestPointerLock = canvas.requestPointerLock || canvas.msRequestPointerLock || canvas.mozRequestPointerLock || canvas.webkitRequestPointerLock;
  373. if (canvas.requestPointerLock) {
  374. canvas.requestPointerLock();
  375. }
  376. }
  377. };
  378. document.addEventListener("fullscreenchange", this._onFullscreenChange, false);
  379. document.addEventListener("mozfullscreenchange", this._onFullscreenChange, false);
  380. document.addEventListener("webkitfullscreenchange", this._onFullscreenChange, false);
  381. document.addEventListener("msfullscreenchange", this._onFullscreenChange, false);
  382. // Pointer lock
  383. this._onPointerLockChange = function () {
  384. _this.isPointerLock = (document.mozPointerLockElement === canvas || document.webkitPointerLockElement === canvas || document.msPointerLockElement === canvas || document.pointerLockElement === canvas);
  385. };
  386. document.addEventListener("pointerlockchange", this._onPointerLockChange, false);
  387. document.addEventListener("mspointerlockchange", this._onPointerLockChange, false);
  388. document.addEventListener("mozpointerlockchange", this._onPointerLockChange, false);
  389. document.addEventListener("webkitpointerlockchange", this._onPointerLockChange, false);
  390. }
  391. Object.defineProperty(Engine, "ALPHA_DISABLE", {
  392. get: function () {
  393. return Engine._ALPHA_DISABLE;
  394. },
  395. enumerable: true,
  396. configurable: true
  397. });
  398. Object.defineProperty(Engine, "ALPHA_ADD", {
  399. get: function () {
  400. return Engine._ALPHA_ADD;
  401. },
  402. enumerable: true,
  403. configurable: true
  404. });
  405. Object.defineProperty(Engine, "ALPHA_COMBINE", {
  406. get: function () {
  407. return Engine._ALPHA_COMBINE;
  408. },
  409. enumerable: true,
  410. configurable: true
  411. });
  412. Object.defineProperty(Engine, "DELAYLOADSTATE_NONE", {
  413. get: function () {
  414. return Engine._DELAYLOADSTATE_NONE;
  415. },
  416. enumerable: true,
  417. configurable: true
  418. });
  419. Object.defineProperty(Engine, "DELAYLOADSTATE_LOADED", {
  420. get: function () {
  421. return Engine._DELAYLOADSTATE_LOADED;
  422. },
  423. enumerable: true,
  424. configurable: true
  425. });
  426. Object.defineProperty(Engine, "DELAYLOADSTATE_LOADING", {
  427. get: function () {
  428. return Engine._DELAYLOADSTATE_LOADING;
  429. },
  430. enumerable: true,
  431. configurable: true
  432. });
  433. Object.defineProperty(Engine, "DELAYLOADSTATE_NOTLOADED", {
  434. get: function () {
  435. return Engine._DELAYLOADSTATE_NOTLOADED;
  436. },
  437. enumerable: true,
  438. configurable: true
  439. });
  440. Object.defineProperty(Engine, "Version", {
  441. get: function () {
  442. return "1.14.0";
  443. },
  444. enumerable: true,
  445. configurable: true
  446. });
  447. Engine.prototype.getAspectRatio = function (camera) {
  448. var viewport = camera.viewport;
  449. return (this.getRenderWidth() * viewport.width) / (this.getRenderHeight() * viewport.height);
  450. };
  451. Engine.prototype.getRenderWidth = function () {
  452. if (this._currentRenderTarget) {
  453. return this._currentRenderTarget._width;
  454. }
  455. return this._renderingCanvas.width;
  456. };
  457. Engine.prototype.getRenderHeight = function () {
  458. if (this._currentRenderTarget) {
  459. return this._currentRenderTarget._height;
  460. }
  461. return this._renderingCanvas.height;
  462. };
  463. Engine.prototype.getRenderingCanvas = function () {
  464. return this._renderingCanvas;
  465. };
  466. Engine.prototype.getRenderingCanvasClientRect = function () {
  467. return this._renderingCanvas.getBoundingClientRect();
  468. };
  469. Engine.prototype.setHardwareScalingLevel = function (level) {
  470. this._hardwareScalingLevel = level;
  471. this.resize();
  472. };
  473. Engine.prototype.getHardwareScalingLevel = function () {
  474. return this._hardwareScalingLevel;
  475. };
  476. Engine.prototype.getLoadedTexturesCache = function () {
  477. return this._loadedTexturesCache;
  478. };
  479. Engine.prototype.getCaps = function () {
  480. return this._caps;
  481. };
  482. // Methods
  483. Engine.prototype.setDepthFunctionToGreater = function () {
  484. this._depthCullingState.depthFunc = this._gl.GREATER;
  485. };
  486. Engine.prototype.setDepthFunctionToGreaterOrEqual = function () {
  487. this._depthCullingState.depthFunc = this._gl.GEQUAL;
  488. };
  489. Engine.prototype.setDepthFunctionToLess = function () {
  490. this._depthCullingState.depthFunc = this._gl.LESS;
  491. };
  492. Engine.prototype.setDepthFunctionToLessOrEqual = function () {
  493. this._depthCullingState.depthFunc = this._gl.LEQUAL;
  494. };
  495. Engine.prototype.stopRenderLoop = function () {
  496. this._renderFunction = null;
  497. this._runningLoop = false;
  498. };
  499. Engine.prototype._renderLoop = function () {
  500. var _this = this;
  501. var shouldRender = true;
  502. if (!this.renderEvenInBackground && this._windowIsBackground) {
  503. shouldRender = false;
  504. }
  505. if (shouldRender) {
  506. // Start new frame
  507. this.beginFrame();
  508. if (this._renderFunction) {
  509. this._renderFunction();
  510. }
  511. // Present
  512. this.endFrame();
  513. }
  514. if (this._runningLoop) {
  515. // Register new frame
  516. BABYLON.Tools.QueueNewFrame(function () {
  517. _this._renderLoop();
  518. });
  519. }
  520. };
  521. Engine.prototype.runRenderLoop = function (renderFunction) {
  522. var _this = this;
  523. this._runningLoop = true;
  524. this._renderFunction = renderFunction;
  525. BABYLON.Tools.QueueNewFrame(function () {
  526. _this._renderLoop();
  527. });
  528. };
  529. Engine.prototype.switchFullscreen = function (requestPointerLock) {
  530. if (this.isFullscreen) {
  531. BABYLON.Tools.ExitFullscreen();
  532. } else {
  533. this._pointerLockRequested = requestPointerLock;
  534. BABYLON.Tools.RequestFullscreen(this._renderingCanvas);
  535. }
  536. };
  537. Engine.prototype.clear = function (color, backBuffer, depthStencil) {
  538. this.applyStates();
  539. this._gl.clearColor(color.r, color.g, color.b, color.a !== undefined ? color.a : 1.0);
  540. if (this._depthCullingState.depthMask) {
  541. this._gl.clearDepth(1.0);
  542. }
  543. var mode = 0;
  544. if (backBuffer)
  545. mode |= this._gl.COLOR_BUFFER_BIT;
  546. if (depthStencil && this._depthCullingState.depthMask)
  547. mode |= this._gl.DEPTH_BUFFER_BIT;
  548. this._gl.clear(mode);
  549. };
  550. Engine.prototype.setViewport = function (viewport, requiredWidth, requiredHeight) {
  551. var width = requiredWidth || this._renderingCanvas.width;
  552. var height = requiredHeight || this._renderingCanvas.height;
  553. var x = viewport.x || 0;
  554. var y = viewport.y || 0;
  555. this._cachedViewport = viewport;
  556. this._gl.viewport(x * width, y * height, width * viewport.width, height * viewport.height);
  557. };
  558. Engine.prototype.setDirectViewport = function (x, y, width, height) {
  559. this._cachedViewport = null;
  560. this._gl.viewport(x, y, width, height);
  561. };
  562. Engine.prototype.beginFrame = function () {
  563. BABYLON.Tools._MeasureFps();
  564. };
  565. Engine.prototype.endFrame = function () {
  566. this.flushFramebuffer();
  567. };
  568. Engine.prototype.resize = function () {
  569. this._renderingCanvas.width = this._renderingCanvas.clientWidth / this._hardwareScalingLevel;
  570. this._renderingCanvas.height = this._renderingCanvas.clientHeight / this._hardwareScalingLevel;
  571. this._canvasClientRect = this._renderingCanvas.getBoundingClientRect();
  572. };
  573. Engine.prototype.bindFramebuffer = function (texture) {
  574. this._currentRenderTarget = texture;
  575. var gl = this._gl;
  576. gl.bindFramebuffer(gl.FRAMEBUFFER, texture._framebuffer);
  577. this._gl.viewport(0, 0, texture._width, texture._height);
  578. this.wipeCaches();
  579. };
  580. Engine.prototype.unBindFramebuffer = function (texture) {
  581. this._currentRenderTarget = null;
  582. if (texture.generateMipMaps) {
  583. var gl = this._gl;
  584. gl.bindTexture(gl.TEXTURE_2D, texture);
  585. gl.generateMipmap(gl.TEXTURE_2D);
  586. gl.bindTexture(gl.TEXTURE_2D, null);
  587. }
  588. this._gl.bindFramebuffer(this._gl.FRAMEBUFFER, null);
  589. };
  590. Engine.prototype.flushFramebuffer = function () {
  591. this._gl.flush();
  592. };
  593. Engine.prototype.restoreDefaultFramebuffer = function () {
  594. this._gl.bindFramebuffer(this._gl.FRAMEBUFFER, null);
  595. this.setViewport(this._cachedViewport);
  596. this.wipeCaches();
  597. };
  598. // VBOs
  599. Engine.prototype._resetVertexBufferBinding = function () {
  600. this._gl.bindBuffer(this._gl.ARRAY_BUFFER, null);
  601. this._cachedVertexBuffers = null;
  602. };
  603. Engine.prototype.createVertexBuffer = function (vertices) {
  604. var vbo = this._gl.createBuffer();
  605. this._gl.bindBuffer(this._gl.ARRAY_BUFFER, vbo);
  606. this._gl.bufferData(this._gl.ARRAY_BUFFER, new Float32Array(vertices), this._gl.STATIC_DRAW);
  607. this._resetVertexBufferBinding();
  608. vbo.references = 1;
  609. return vbo;
  610. };
  611. Engine.prototype.createDynamicVertexBuffer = function (capacity) {
  612. var vbo = this._gl.createBuffer();
  613. this._gl.bindBuffer(this._gl.ARRAY_BUFFER, vbo);
  614. this._gl.bufferData(this._gl.ARRAY_BUFFER, capacity, this._gl.DYNAMIC_DRAW);
  615. this._resetVertexBufferBinding();
  616. vbo.references = 1;
  617. return vbo;
  618. };
  619. Engine.prototype.updateDynamicVertexBuffer = function (vertexBuffer, vertices, length) {
  620. this._gl.bindBuffer(this._gl.ARRAY_BUFFER, vertexBuffer);
  621. //if (length && length != vertices.length) {
  622. // this._gl.bufferSubData(this._gl.ARRAY_BUFFER, 0, new Float32Array(vertices, 0, length));
  623. //} else {
  624. if (vertices instanceof Float32Array) {
  625. this._gl.bufferSubData(this._gl.ARRAY_BUFFER, 0, vertices);
  626. } else {
  627. this._gl.bufferSubData(this._gl.ARRAY_BUFFER, 0, new Float32Array(vertices));
  628. }
  629. // }
  630. this._resetVertexBufferBinding();
  631. };
  632. Engine.prototype._resetIndexBufferBinding = function () {
  633. this._gl.bindBuffer(this._gl.ELEMENT_ARRAY_BUFFER, null);
  634. this._cachedIndexBuffer = null;
  635. };
  636. Engine.prototype.createIndexBuffer = function (indices) {
  637. var vbo = this._gl.createBuffer();
  638. this._gl.bindBuffer(this._gl.ELEMENT_ARRAY_BUFFER, vbo);
  639. this._gl.bufferData(this._gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(indices), this._gl.STATIC_DRAW);
  640. this._resetIndexBufferBinding();
  641. vbo.references = 1;
  642. return vbo;
  643. };
  644. Engine.prototype.bindBuffers = function (vertexBuffer, indexBuffer, vertexDeclaration, vertexStrideSize, effect) {
  645. if (this._cachedVertexBuffers !== vertexBuffer || this._cachedEffectForVertexBuffers !== effect) {
  646. this._cachedVertexBuffers = vertexBuffer;
  647. this._cachedEffectForVertexBuffers = effect;
  648. this._gl.bindBuffer(this._gl.ARRAY_BUFFER, vertexBuffer);
  649. var offset = 0;
  650. for (var index = 0; index < vertexDeclaration.length; index++) {
  651. var order = effect.getAttributeLocation(index);
  652. if (order >= 0) {
  653. this._gl.vertexAttribPointer(order, vertexDeclaration[index], this._gl.FLOAT, false, vertexStrideSize, offset);
  654. }
  655. offset += vertexDeclaration[index] * 4;
  656. }
  657. }
  658. if (this._cachedIndexBuffer !== indexBuffer) {
  659. this._cachedIndexBuffer = indexBuffer;
  660. this._gl.bindBuffer(this._gl.ELEMENT_ARRAY_BUFFER, indexBuffer);
  661. }
  662. };
  663. Engine.prototype.bindMultiBuffers = function (vertexBuffers, indexBuffer, effect) {
  664. if (this._cachedVertexBuffers !== vertexBuffers || this._cachedEffectForVertexBuffers !== effect) {
  665. this._cachedVertexBuffers = vertexBuffers;
  666. this._cachedEffectForVertexBuffers = effect;
  667. var attributes = effect.getAttributesNames();
  668. for (var index = 0; index < attributes.length; index++) {
  669. var order = effect.getAttributeLocation(index);
  670. if (order >= 0) {
  671. var vertexBuffer = vertexBuffers[attributes[index]];
  672. if (!vertexBuffer) {
  673. continue;
  674. }
  675. var stride = vertexBuffer.getStrideSize();
  676. this._gl.bindBuffer(this._gl.ARRAY_BUFFER, vertexBuffer.getBuffer());
  677. this._gl.vertexAttribPointer(order, stride, this._gl.FLOAT, false, stride * 4, 0);
  678. }
  679. }
  680. }
  681. if (this._cachedIndexBuffer !== indexBuffer) {
  682. this._cachedIndexBuffer = indexBuffer;
  683. this._gl.bindBuffer(this._gl.ELEMENT_ARRAY_BUFFER, indexBuffer);
  684. }
  685. };
  686. Engine.prototype._releaseBuffer = function (buffer) {
  687. buffer.references--;
  688. if (buffer.references === 0) {
  689. this._gl.deleteBuffer(buffer);
  690. return true;
  691. }
  692. return false;
  693. };
  694. Engine.prototype.createInstancesBuffer = function (capacity) {
  695. var buffer = this._gl.createBuffer();
  696. buffer.capacity = capacity;
  697. this._gl.bindBuffer(this._gl.ARRAY_BUFFER, buffer);
  698. this._gl.bufferData(this._gl.ARRAY_BUFFER, capacity, this._gl.DYNAMIC_DRAW);
  699. return buffer;
  700. };
  701. Engine.prototype.deleteInstancesBuffer = function (buffer) {
  702. this._gl.deleteBuffer(buffer);
  703. };
  704. Engine.prototype.updateAndBindInstancesBuffer = function (instancesBuffer, data, offsetLocations) {
  705. this._gl.bindBuffer(this._gl.ARRAY_BUFFER, instancesBuffer);
  706. this._gl.bufferSubData(this._gl.ARRAY_BUFFER, 0, data);
  707. for (var index = 0; index < 4; index++) {
  708. var offsetLocation = offsetLocations[index];
  709. this._gl.enableVertexAttribArray(offsetLocation);
  710. this._gl.vertexAttribPointer(offsetLocation, 4, this._gl.FLOAT, false, 64, index * 16);
  711. this._caps.instancedArrays.vertexAttribDivisorANGLE(offsetLocation, 1);
  712. }
  713. };
  714. Engine.prototype.unBindInstancesBuffer = function (instancesBuffer, offsetLocations) {
  715. this._gl.bindBuffer(this._gl.ARRAY_BUFFER, instancesBuffer);
  716. for (var index = 0; index < 4; index++) {
  717. var offsetLocation = offsetLocations[index];
  718. this._gl.disableVertexAttribArray(offsetLocation);
  719. this._caps.instancedArrays.vertexAttribDivisorANGLE(offsetLocation, 0);
  720. }
  721. };
  722. Engine.prototype.applyStates = function () {
  723. this._depthCullingState.apply(this._gl);
  724. this._alphaState.apply(this._gl);
  725. };
  726. Engine.prototype.draw = function (useTriangles, indexStart, indexCount, instancesCount) {
  727. // Apply states
  728. this.applyStates();
  729. // Render
  730. if (instancesCount) {
  731. this._caps.instancedArrays.drawElementsInstancedANGLE(useTriangles ? this._gl.TRIANGLES : this._gl.LINES, indexCount, this._gl.UNSIGNED_SHORT, indexStart * 2, instancesCount);
  732. return;
  733. }
  734. this._gl.drawElements(useTriangles ? this._gl.TRIANGLES : this._gl.LINES, indexCount, this._gl.UNSIGNED_SHORT, indexStart * 2);
  735. };
  736. // Shaders
  737. Engine.prototype._releaseEffect = function (effect) {
  738. if (this._compiledEffects[effect._key]) {
  739. delete this._compiledEffects[effect._key];
  740. if (effect.getProgram()) {
  741. this._gl.deleteProgram(effect.getProgram());
  742. }
  743. }
  744. };
  745. Engine.prototype.createEffect = function (baseName, attributesNames, uniformsNames, samplers, defines, fallbacks, onCompiled, onError) {
  746. var vertex = baseName.vertexElement || baseName.vertex || baseName;
  747. var fragment = baseName.fragmentElement || baseName.fragment || baseName;
  748. var name = vertex + "+" + fragment + "@" + defines;
  749. if (this._compiledEffects[name]) {
  750. return this._compiledEffects[name];
  751. }
  752. var effect = new BABYLON.Effect(baseName, attributesNames, uniformsNames, samplers, this, defines, fallbacks, onCompiled, onError);
  753. effect._key = name;
  754. this._compiledEffects[name] = effect;
  755. return effect;
  756. };
  757. Engine.prototype.createEffectForParticles = function (fragmentName, uniformsNames, samplers, defines, fallbacks, onCompiled, onError) {
  758. if (typeof uniformsNames === "undefined") { uniformsNames = []; }
  759. if (typeof samplers === "undefined") { samplers = []; }
  760. if (typeof defines === "undefined") { defines = ""; }
  761. return this.createEffect({
  762. vertex: "particles",
  763. fragmentElement: fragmentName
  764. }, ["position", "color", "options"], ["view", "projection"].concat(uniformsNames), ["diffuseSampler"].concat(samplers), defines, fallbacks, onCompiled, onError);
  765. };
  766. Engine.prototype.createShaderProgram = function (vertexCode, fragmentCode, defines) {
  767. var vertexShader = compileShader(this._gl, vertexCode, "vertex", defines);
  768. var fragmentShader = compileShader(this._gl, fragmentCode, "fragment", defines);
  769. var shaderProgram = this._gl.createProgram();
  770. this._gl.attachShader(shaderProgram, vertexShader);
  771. this._gl.attachShader(shaderProgram, fragmentShader);
  772. this._gl.linkProgram(shaderProgram);
  773. var linked = this._gl.getProgramParameter(shaderProgram, this._gl.LINK_STATUS);
  774. if (!linked) {
  775. var error = this._gl.getProgramInfoLog(shaderProgram);
  776. if (error) {
  777. throw new Error(error);
  778. }
  779. }
  780. this._gl.deleteShader(vertexShader);
  781. this._gl.deleteShader(fragmentShader);
  782. return shaderProgram;
  783. };
  784. Engine.prototype.getUniforms = function (shaderProgram, uniformsNames) {
  785. var results = [];
  786. for (var index = 0; index < uniformsNames.length; index++) {
  787. results.push(this._gl.getUniformLocation(shaderProgram, uniformsNames[index]));
  788. }
  789. return results;
  790. };
  791. Engine.prototype.getAttributes = function (shaderProgram, attributesNames) {
  792. var results = [];
  793. for (var index = 0; index < attributesNames.length; index++) {
  794. try {
  795. results.push(this._gl.getAttribLocation(shaderProgram, attributesNames[index]));
  796. } catch (e) {
  797. results.push(-1);
  798. }
  799. }
  800. return results;
  801. };
  802. Engine.prototype.enableEffect = function (effect) {
  803. if (!effect || !effect.getAttributesCount() || this._currentEffect === effect) {
  804. return;
  805. }
  806. this._vertexAttribArrays = this._vertexAttribArrays || [];
  807. // Use program
  808. this._gl.useProgram(effect.getProgram());
  809. for (var i in this._vertexAttribArrays) {
  810. if (i > this._gl.VERTEX_ATTRIB_ARRAY_ENABLED || !this._vertexAttribArrays[i]) {
  811. continue;
  812. }
  813. this._vertexAttribArrays[i] = false;
  814. this._gl.disableVertexAttribArray(i);
  815. }
  816. var attributesCount = effect.getAttributesCount();
  817. for (var index = 0; index < attributesCount; index++) {
  818. // Attributes
  819. var order = effect.getAttributeLocation(index);
  820. if (order >= 0) {
  821. this._vertexAttribArrays[order] = true;
  822. this._gl.enableVertexAttribArray(order);
  823. }
  824. }
  825. this._currentEffect = effect;
  826. };
  827. Engine.prototype.setArray = function (uniform, array) {
  828. if (!uniform)
  829. return;
  830. this._gl.uniform1fv(uniform, array);
  831. };
  832. Engine.prototype.setMatrices = function (uniform, matrices) {
  833. if (!uniform)
  834. return;
  835. this._gl.uniformMatrix4fv(uniform, false, matrices);
  836. };
  837. Engine.prototype.setMatrix = function (uniform, matrix) {
  838. if (!uniform)
  839. return;
  840. this._gl.uniformMatrix4fv(uniform, false, matrix.toArray());
  841. };
  842. Engine.prototype.setFloat = function (uniform, value) {
  843. if (!uniform)
  844. return;
  845. this._gl.uniform1f(uniform, value);
  846. };
  847. Engine.prototype.setFloat2 = function (uniform, x, y) {
  848. if (!uniform)
  849. return;
  850. this._gl.uniform2f(uniform, x, y);
  851. };
  852. Engine.prototype.setFloat3 = function (uniform, x, y, z) {
  853. if (!uniform)
  854. return;
  855. this._gl.uniform3f(uniform, x, y, z);
  856. };
  857. Engine.prototype.setBool = function (uniform, bool) {
  858. if (!uniform)
  859. return;
  860. this._gl.uniform1i(uniform, bool);
  861. };
  862. Engine.prototype.setFloat4 = function (uniform, x, y, z, w) {
  863. if (!uniform)
  864. return;
  865. this._gl.uniform4f(uniform, x, y, z, w);
  866. };
  867. Engine.prototype.setColor3 = function (uniform, color3) {
  868. if (!uniform)
  869. return;
  870. this._gl.uniform3f(uniform, color3.r, color3.g, color3.b);
  871. };
  872. Engine.prototype.setColor4 = function (uniform, color3, alpha) {
  873. if (!uniform)
  874. return;
  875. this._gl.uniform4f(uniform, color3.r, color3.g, color3.b, alpha);
  876. };
  877. // States
  878. Engine.prototype.setState = function (culling, force) {
  879. // Culling
  880. if (this._depthCullingState.cull !== culling || force) {
  881. if (culling) {
  882. this._depthCullingState.cullFace = this.cullBackFaces ? this._gl.BACK : this._gl.FRONT;
  883. this._depthCullingState.cull = true;
  884. } else {
  885. this._depthCullingState.cull = false;
  886. }
  887. }
  888. };
  889. Engine.prototype.setDepthBuffer = function (enable) {
  890. this._depthCullingState.depthTest = enable;
  891. };
  892. Engine.prototype.getDepthWrite = function () {
  893. return this._depthCullingState.depthMask;
  894. };
  895. Engine.prototype.setDepthWrite = function (enable) {
  896. this._depthCullingState.depthMask = enable;
  897. };
  898. Engine.prototype.setColorWrite = function (enable) {
  899. this._gl.colorMask(enable, enable, enable, enable);
  900. };
  901. Engine.prototype.setAlphaMode = function (mode) {
  902. switch (mode) {
  903. case BABYLON.Engine.ALPHA_DISABLE:
  904. this.setDepthWrite(true);
  905. this._alphaState.alphaBlend = false;
  906. break;
  907. case BABYLON.Engine.ALPHA_COMBINE:
  908. this.setDepthWrite(false);
  909. this._alphaState.setAlphaBlendFunctionParameters(this._gl.SRC_ALPHA, this._gl.ONE_MINUS_SRC_ALPHA, this._gl.ONE, this._gl.ONE);
  910. this._alphaState.alphaBlend = true;
  911. break;
  912. case BABYLON.Engine.ALPHA_ADD:
  913. this.setDepthWrite(false);
  914. this._alphaState.setAlphaBlendFunctionParameters(this._gl.ONE, this._gl.ONE, this._gl.ZERO, this._gl.ONE);
  915. this._alphaState.alphaBlend = true;
  916. break;
  917. }
  918. };
  919. Engine.prototype.setAlphaTesting = function (enable) {
  920. this._alphaTest = enable;
  921. };
  922. Engine.prototype.getAlphaTesting = function () {
  923. return this._alphaTest;
  924. };
  925. // Textures
  926. Engine.prototype.wipeCaches = function () {
  927. this._activeTexturesCache = [];
  928. this._currentEffect = null;
  929. this._depthCullingState.reset();
  930. this._alphaState.reset();
  931. this._cachedVertexBuffers = null;
  932. this._cachedIndexBuffer = null;
  933. this._cachedEffectForVertexBuffers = null;
  934. };
  935. Engine.prototype.setSamplingMode = function (texture, samplingMode) {
  936. var gl = this._gl;
  937. gl.bindTexture(gl.TEXTURE_2D, texture);
  938. var magFilter = gl.NEAREST;
  939. var minFilter = gl.NEAREST;
  940. if (samplingMode === BABYLON.Texture.BILINEAR_SAMPLINGMODE) {
  941. magFilter = gl.LINEAR;
  942. minFilter = gl.LINEAR;
  943. } else if (samplingMode === BABYLON.Texture.TRILINEAR_SAMPLINGMODE) {
  944. magFilter = gl.LINEAR;
  945. minFilter = gl.LINEAR_MIPMAP_LINEAR;
  946. }
  947. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, magFilter);
  948. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, minFilter);
  949. gl.bindTexture(gl.TEXTURE_2D, null);
  950. };
  951. Engine.prototype.createTexture = function (url, noMipmap, invertY, scene, samplingMode, onLoad, onError, buffer) {
  952. var _this = this;
  953. if (typeof samplingMode === "undefined") { samplingMode = BABYLON.Texture.TRILINEAR_SAMPLINGMODE; }
  954. if (typeof onLoad === "undefined") { onLoad = null; }
  955. if (typeof onError === "undefined") { onError = null; }
  956. var texture = this._gl.createTexture();
  957. var extension;
  958. var fromData = false;
  959. if (url.substr(0, 5) === "data:") {
  960. // data: -- name
  961. fromData = true;
  962. }
  963. if (!fromData)
  964. extension = url.substr(url.length - 4, 4).toLowerCase();
  965. else {
  966. var oldUrl = url;
  967. fromData = oldUrl.split(':');
  968. url = oldUrl;
  969. extension = fromData[1].substr(fromData[1].length - 4, 4).toLowerCase();
  970. }
  971. var isDDS = this.getCaps().s3tc && (extension === ".dds");
  972. var isTGA = (extension === ".tga");
  973. scene._addPendingData(texture);
  974. texture.url = url;
  975. texture.noMipmap = noMipmap;
  976. texture.references = 1;
  977. this._loadedTexturesCache.push(texture);
  978. var onerror = function () {
  979. scene._removePendingData(texture);
  980. if (onError) {
  981. onError();
  982. }
  983. };
  984. if (isTGA) {
  985. var callback = function (arrayBuffer) {
  986. var data = new Uint8Array(arrayBuffer);
  987. var header = BABYLON.Internals.TGATools.GetTGAHeader(data);
  988. prepareWebGLTexture(texture, _this._gl, scene, header.width, header.height, invertY, noMipmap, false, function () {
  989. BABYLON.Internals.TGATools.UploadContent(_this._gl, data);
  990. if (onLoad) {
  991. onLoad();
  992. }
  993. }, samplingMode);
  994. };
  995. if (!(fromData instanceof Array))
  996. BABYLON.Tools.LoadFile(url, callback, onerror, scene.database, true);
  997. else
  998. callback(buffer);
  999. } else if (isDDS) {
  1000. var callback = function (data) {
  1001. var info = BABYLON.Internals.DDSTools.GetDDSInfo(data);
  1002. var loadMipmap = (info.isRGB || info.isLuminance || info.mipmapCount > 1) && !noMipmap && ((info.width >> (info.mipmapCount - 1)) == 1);
  1003. prepareWebGLTexture(texture, _this._gl, scene, info.width, info.height, invertY, !loadMipmap, info.isFourCC, function () {
  1004. BABYLON.Internals.DDSTools.UploadDDSLevels(_this._gl, _this.getCaps().s3tc, data, info, loadMipmap, 1);
  1005. if (onLoad) {
  1006. onLoad();
  1007. }
  1008. }, samplingMode);
  1009. };
  1010. if (!(fromData instanceof Array))
  1011. BABYLON.Tools.LoadFile(url, callback, onerror, scene.database, true);
  1012. else
  1013. callback(buffer);
  1014. } else {
  1015. var onload = function (img) {
  1016. prepareWebGLTexture(texture, _this._gl, scene, img.width, img.height, invertY, noMipmap, false, function (potWidth, potHeight) {
  1017. var isPot = (img.width == potWidth && img.height == potHeight);
  1018. if (!isPot) {
  1019. _this._workingCanvas.width = potWidth;
  1020. _this._workingCanvas.height = potHeight;
  1021. _this._workingContext.drawImage(img, 0, 0, img.width, img.height, 0, 0, potWidth, potHeight);
  1022. }
  1023. _this._gl.texImage2D(_this._gl.TEXTURE_2D, 0, _this._gl.RGBA, _this._gl.RGBA, _this._gl.UNSIGNED_BYTE, isPot ? img : _this._workingCanvas);
  1024. if (onLoad) {
  1025. onLoad();
  1026. }
  1027. }, samplingMode);
  1028. };
  1029. if (!(fromData instanceof Array))
  1030. BABYLON.Tools.LoadImage(url, onload, onerror, scene.database);
  1031. else
  1032. BABYLON.Tools.LoadImage(buffer, onload, onerror, scene.database);
  1033. }
  1034. return texture;
  1035. };
  1036. Engine.prototype.createDynamicTexture = function (width, height, generateMipMaps, samplingMode) {
  1037. var texture = this._gl.createTexture();
  1038. width = BABYLON.Tools.GetExponantOfTwo(width, this._caps.maxTextureSize);
  1039. height = BABYLON.Tools.GetExponantOfTwo(height, this._caps.maxTextureSize);
  1040. this._gl.bindTexture(this._gl.TEXTURE_2D, texture);
  1041. var filters = getSamplingParameters(samplingMode, generateMipMaps, this._gl);
  1042. this._gl.texParameteri(this._gl.TEXTURE_2D, this._gl.TEXTURE_MAG_FILTER, filters.mag);
  1043. this._gl.texParameteri(this._gl.TEXTURE_2D, this._gl.TEXTURE_MIN_FILTER, filters.min);
  1044. this._gl.bindTexture(this._gl.TEXTURE_2D, null);
  1045. this._activeTexturesCache = [];
  1046. texture._baseWidth = width;
  1047. texture._baseHeight = height;
  1048. texture._width = width;
  1049. texture._height = height;
  1050. texture.isReady = false;
  1051. texture.generateMipMaps = generateMipMaps;
  1052. texture.references = 1;
  1053. this._loadedTexturesCache.push(texture);
  1054. return texture;
  1055. };
  1056. Engine.prototype.updateDynamicTexture = function (texture, canvas, invertY) {
  1057. this._gl.bindTexture(this._gl.TEXTURE_2D, texture);
  1058. this._gl.pixelStorei(this._gl.UNPACK_FLIP_Y_WEBGL, invertY ? 1 : 0);
  1059. this._gl.texImage2D(this._gl.TEXTURE_2D, 0, this._gl.RGBA, this._gl.RGBA, this._gl.UNSIGNED_BYTE, canvas);
  1060. if (texture.generateMipMaps) {
  1061. this._gl.generateMipmap(this._gl.TEXTURE_2D);
  1062. }
  1063. this._gl.bindTexture(this._gl.TEXTURE_2D, null);
  1064. this._activeTexturesCache = [];
  1065. texture.isReady = true;
  1066. };
  1067. Engine.prototype.updateVideoTexture = function (texture, video, invertY) {
  1068. this._gl.bindTexture(this._gl.TEXTURE_2D, texture);
  1069. this._gl.pixelStorei(this._gl.UNPACK_FLIP_Y_WEBGL, invertY ? 0 : 1); // Video are upside down by default
  1070. // Scale the video if it is a NPOT using the current working canvas
  1071. if (video.videoWidth !== texture._width || video.videoHeight !== texture._height) {
  1072. if (!texture._workingCanvas) {
  1073. texture._workingCanvas = document.createElement("canvas");
  1074. texture._workingContext = texture._workingCanvas.getContext("2d");
  1075. texture._workingCanvas.width = texture._width;
  1076. texture._workingCanvas.height = texture._height;
  1077. }
  1078. texture._workingContext.drawImage(video, 0, 0, video.videoWidth, video.videoHeight, 0, 0, texture._width, texture._height);
  1079. this._gl.texImage2D(this._gl.TEXTURE_2D, 0, this._gl.RGBA, this._gl.RGBA, this._gl.UNSIGNED_BYTE, texture._workingCanvas);
  1080. } else {
  1081. this._gl.texImage2D(this._gl.TEXTURE_2D, 0, this._gl.RGBA, this._gl.RGBA, this._gl.UNSIGNED_BYTE, video);
  1082. }
  1083. if (texture.generateMipMaps) {
  1084. this._gl.generateMipmap(this._gl.TEXTURE_2D);
  1085. }
  1086. this._gl.bindTexture(this._gl.TEXTURE_2D, null);
  1087. this._activeTexturesCache = [];
  1088. texture.isReady = true;
  1089. };
  1090. Engine.prototype.createRenderTargetTexture = function (size, options) {
  1091. // old version had a "generateMipMaps" arg instead of options.
  1092. // if options.generateMipMaps is undefined, consider that options itself if the generateMipmaps value
  1093. // in the same way, generateDepthBuffer is defaulted to true
  1094. var generateMipMaps = false;
  1095. var generateDepthBuffer = true;
  1096. var samplingMode = BABYLON.Texture.TRILINEAR_SAMPLINGMODE;
  1097. if (options !== undefined) {
  1098. generateMipMaps = options.generateMipMaps === undefined ? options : options.generateMipmaps;
  1099. generateDepthBuffer = options.generateDepthBuffer === undefined ? true : options.generateDepthBuffer;
  1100. if (options.samplingMode !== undefined) {
  1101. samplingMode = options.samplingMode;
  1102. }
  1103. }
  1104. var gl = this._gl;
  1105. var texture = gl.createTexture();
  1106. gl.bindTexture(gl.TEXTURE_2D, texture);
  1107. var width = size.width || size;
  1108. var height = size.height || size;
  1109. var filters = getSamplingParameters(samplingMode, generateMipMaps, gl);
  1110. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, filters.mag);
  1111. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, filters.min);
  1112. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
  1113. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
  1114. gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, width, height, 0, gl.RGBA, gl.UNSIGNED_BYTE, null);
  1115. var depthBuffer;
  1116. // Create the depth buffer
  1117. if (generateDepthBuffer) {
  1118. depthBuffer = gl.createRenderbuffer();
  1119. gl.bindRenderbuffer(gl.RENDERBUFFER, depthBuffer);
  1120. gl.renderbufferStorage(gl.RENDERBUFFER, gl.DEPTH_COMPONENT16, width, height);
  1121. }
  1122. // Create the framebuffer
  1123. var framebuffer = gl.createFramebuffer();
  1124. gl.bindFramebuffer(gl.FRAMEBUFFER, framebuffer);
  1125. gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, texture, 0);
  1126. if (generateDepthBuffer) {
  1127. gl.framebufferRenderbuffer(gl.FRAMEBUFFER, gl.DEPTH_ATTACHMENT, gl.RENDERBUFFER, depthBuffer);
  1128. }
  1129. // Unbind
  1130. gl.bindTexture(gl.TEXTURE_2D, null);
  1131. gl.bindRenderbuffer(gl.RENDERBUFFER, null);
  1132. gl.bindFramebuffer(gl.FRAMEBUFFER, null);
  1133. texture._framebuffer = framebuffer;
  1134. if (generateDepthBuffer) {
  1135. texture._depthBuffer = depthBuffer;
  1136. }
  1137. texture._width = width;
  1138. texture._height = height;
  1139. texture.isReady = true;
  1140. texture.generateMipMaps = generateMipMaps;
  1141. texture.references = 1;
  1142. this._activeTexturesCache = [];
  1143. this._loadedTexturesCache.push(texture);
  1144. return texture;
  1145. };
  1146. Engine.prototype.createCubeTexture = function (rootUrl, scene, extensions, noMipmap) {
  1147. var _this = this;
  1148. var gl = this._gl;
  1149. var texture = gl.createTexture();
  1150. texture.isCube = true;
  1151. texture.url = rootUrl;
  1152. texture.references = 1;
  1153. this._loadedTexturesCache.push(texture);
  1154. var extension = rootUrl.substr(rootUrl.length - 4, 4).toLowerCase();
  1155. var isDDS = this.getCaps().s3tc && (extension === ".dds");
  1156. if (isDDS) {
  1157. BABYLON.Tools.LoadFile(rootUrl, function (data) {
  1158. var info = BABYLON.Internals.DDSTools.GetDDSInfo(data);
  1159. var loadMipmap = (info.isRGB || info.isLuminance || info.mipmapCount > 1) && !noMipmap;
  1160. gl.bindTexture(gl.TEXTURE_CUBE_MAP, texture);
  1161. gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, 1);
  1162. BABYLON.Internals.DDSTools.UploadDDSLevels(_this._gl, _this.getCaps().s3tc, data, info, loadMipmap, 6);
  1163. if (!noMipmap && !info.isFourCC && info.mipmapCount == 1) {
  1164. gl.generateMipmap(gl.TEXTURE_CUBE_MAP);
  1165. }
  1166. gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
  1167. gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_MIN_FILTER, loadMipmap ? gl.LINEAR_MIPMAP_LINEAR : gl.LINEAR);
  1168. gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
  1169. gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
  1170. gl.bindTexture(gl.TEXTURE_CUBE_MAP, null);
  1171. _this._activeTexturesCache = [];
  1172. texture._width = info.width;
  1173. texture._height = info.height;
  1174. texture.isReady = true;
  1175. }, null, null, true);
  1176. } else {
  1177. cascadeLoad(rootUrl, 0, [], scene, function (imgs) {
  1178. var width = BABYLON.Tools.GetExponantOfTwo(imgs[0].width, _this._caps.maxCubemapTextureSize);
  1179. var height = width;
  1180. _this._workingCanvas.width = width;
  1181. _this._workingCanvas.height = height;
  1182. var faces = [
  1183. gl.TEXTURE_CUBE_MAP_POSITIVE_X, gl.TEXTURE_CUBE_MAP_POSITIVE_Y, gl.TEXTURE_CUBE_MAP_POSITIVE_Z,
  1184. gl.TEXTURE_CUBE_MAP_NEGATIVE_X, gl.TEXTURE_CUBE_MAP_NEGATIVE_Y, gl.TEXTURE_CUBE_MAP_NEGATIVE_Z
  1185. ];
  1186. gl.bindTexture(gl.TEXTURE_CUBE_MAP, texture);
  1187. gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, 0);
  1188. for (var index = 0; index < faces.length; index++) {
  1189. _this._workingContext.drawImage(imgs[index], 0, 0, imgs[index].width, imgs[index].height, 0, 0, width, height);
  1190. gl.texImage2D(faces[index], 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, _this._workingCanvas);
  1191. }
  1192. if (!noMipmap) {
  1193. gl.generateMipmap(gl.TEXTURE_CUBE_MAP);
  1194. }
  1195. gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
  1196. gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_MIN_FILTER, noMipmap ? gl.LINEAR : gl.LINEAR_MIPMAP_LINEAR);
  1197. gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
  1198. gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
  1199. gl.bindTexture(gl.TEXTURE_CUBE_MAP, null);
  1200. _this._activeTexturesCache = [];
  1201. texture._width = width;
  1202. texture._height = height;
  1203. texture.isReady = true;
  1204. }, extensions);
  1205. }
  1206. return texture;
  1207. };
  1208. Engine.prototype._releaseTexture = function (texture) {
  1209. var gl = this._gl;
  1210. if (texture._framebuffer) {
  1211. gl.deleteFramebuffer(texture._framebuffer);
  1212. }
  1213. if (texture._depthBuffer) {
  1214. gl.deleteRenderbuffer(texture._depthBuffer);
  1215. }
  1216. gl.deleteTexture(texture);
  1217. for (var channel = 0; channel < this._caps.maxTexturesImageUnits; channel++) {
  1218. this._gl.activeTexture(this._gl["TEXTURE" + channel]);
  1219. this._gl.bindTexture(this._gl.TEXTURE_2D, null);
  1220. this._gl.bindTexture(this._gl.TEXTURE_CUBE_MAP, null);
  1221. this._activeTexturesCache[channel] = null;
  1222. }
  1223. var index = this._loadedTexturesCache.indexOf(texture);
  1224. if (index !== -1) {
  1225. this._loadedTexturesCache.splice(index, 1);
  1226. }
  1227. };
  1228. Engine.prototype.bindSamplers = function (effect) {
  1229. this._gl.useProgram(effect.getProgram());
  1230. var samplers = effect.getSamplers();
  1231. for (var index = 0; index < samplers.length; index++) {
  1232. var uniform = effect.getUniform(samplers[index]);
  1233. this._gl.uniform1i(uniform, index);
  1234. }
  1235. this._currentEffect = null;
  1236. };
  1237. Engine.prototype._bindTexture = function (channel, texture) {
  1238. this._gl.activeTexture(this._gl["TEXTURE" + channel]);
  1239. this._gl.bindTexture(this._gl.TEXTURE_2D, texture);
  1240. this._activeTexturesCache[channel] = null;
  1241. };
  1242. Engine.prototype.setTextureFromPostProcess = function (channel, postProcess) {
  1243. this._bindTexture(channel, postProcess._textures.data[postProcess._currentRenderTextureInd]);
  1244. };
  1245. Engine.prototype.setTexture = function (channel, texture) {
  1246. if (channel < 0) {
  1247. return;
  1248. }
  1249. // Not ready?
  1250. if (!texture || !texture.isReady()) {
  1251. if (this._activeTexturesCache[channel] != null) {
  1252. this._gl.activeTexture(this._gl["TEXTURE" + channel]);
  1253. this._gl.bindTexture(this._gl.TEXTURE_2D, null);
  1254. this._gl.bindTexture(this._gl.TEXTURE_CUBE_MAP, null);
  1255. this._activeTexturesCache[channel] = null;
  1256. }
  1257. return;
  1258. }
  1259. // Video
  1260. if (texture instanceof BABYLON.VideoTexture) {
  1261. if (texture.update()) {
  1262. this._activeTexturesCache[channel] = null;
  1263. }
  1264. } else if (texture.delayLoadState == BABYLON.Engine.DELAYLOADSTATE_NOTLOADED) {
  1265. texture.delayLoad();
  1266. return;
  1267. }
  1268. if (this._activeTexturesCache[channel] == texture) {
  1269. return;
  1270. }
  1271. this._activeTexturesCache[channel] = texture;
  1272. var internalTexture = texture.getInternalTexture();
  1273. this._gl.activeTexture(this._gl["TEXTURE" + channel]);
  1274. if (internalTexture.isCube) {
  1275. this._gl.bindTexture(this._gl.TEXTURE_CUBE_MAP, internalTexture);
  1276. if (internalTexture._cachedCoordinatesMode !== texture.coordinatesMode) {
  1277. internalTexture._cachedCoordinatesMode = texture.coordinatesMode;
  1278. // CUBIC_MODE and SKYBOX_MODE both require CLAMP_TO_EDGE. All other modes use REPEAT.
  1279. var textureWrapMode = (texture.coordinatesMode !== BABYLON.Texture.CUBIC_MODE && texture.coordinatesMode !== BABYLON.Texture.SKYBOX_MODE) ? this._gl.REPEAT : this._gl.CLAMP_TO_EDGE;
  1280. this._gl.texParameteri(this._gl.TEXTURE_CUBE_MAP, this._gl.TEXTURE_WRAP_S, textureWrapMode);
  1281. this._gl.texParameteri(this._gl.TEXTURE_CUBE_MAP, this._gl.TEXTURE_WRAP_T, textureWrapMode);
  1282. }
  1283. this._setAnisotropicLevel(this._gl.TEXTURE_CUBE_MAP, texture);
  1284. } else {
  1285. this._gl.bindTexture(this._gl.TEXTURE_2D, internalTexture);
  1286. if (internalTexture._cachedWrapU !== texture.wrapU) {
  1287. internalTexture._cachedWrapU = texture.wrapU;
  1288. switch (texture.wrapU) {
  1289. case BABYLON.Texture.WRAP_ADDRESSMODE:
  1290. this._gl.texParameteri(this._gl.TEXTURE_2D, this._gl.TEXTURE_WRAP_S, this._gl.REPEAT);
  1291. break;
  1292. case BABYLON.Texture.CLAMP_ADDRESSMODE:
  1293. this._gl.texParameteri(this._gl.TEXTURE_2D, this._gl.TEXTURE_WRAP_S, this._gl.CLAMP_TO_EDGE);
  1294. break;
  1295. case BABYLON.Texture.MIRROR_ADDRESSMODE:
  1296. this._gl.texParameteri(this._gl.TEXTURE_2D, this._gl.TEXTURE_WRAP_S, this._gl.MIRRORED_REPEAT);
  1297. break;
  1298. }
  1299. }
  1300. if (internalTexture._cachedWrapV !== texture.wrapV) {
  1301. internalTexture._cachedWrapV = texture.wrapV;
  1302. switch (texture.wrapV) {
  1303. case BABYLON.Texture.WRAP_ADDRESSMODE:
  1304. this._gl.texParameteri(this._gl.TEXTURE_2D, this._gl.TEXTURE_WRAP_T, this._gl.REPEAT);
  1305. break;
  1306. case BABYLON.Texture.CLAMP_ADDRESSMODE:
  1307. this._gl.texParameteri(this._gl.TEXTURE_2D, this._gl.TEXTURE_WRAP_T, this._gl.CLAMP_TO_EDGE);
  1308. break;
  1309. case BABYLON.Texture.MIRROR_ADDRESSMODE:
  1310. this._gl.texParameteri(this._gl.TEXTURE_2D, this._gl.TEXTURE_WRAP_T, this._gl.MIRRORED_REPEAT);
  1311. break;
  1312. }
  1313. }
  1314. this._setAnisotropicLevel(this._gl.TEXTURE_2D, texture);
  1315. }
  1316. };
  1317. Engine.prototype._setAnisotropicLevel = function (key, texture) {
  1318. var anisotropicFilterExtension = this._caps.textureAnisotropicFilterExtension;
  1319. if (anisotropicFilterExtension && texture._cachedAnisotropicFilteringLevel !== texture.anisotropicFilteringLevel) {
  1320. this._gl.texParameterf(key, anisotropicFilterExtension.TEXTURE_MAX_ANISOTROPY_EXT, Math.min(texture.anisotropicFilteringLevel, this._caps.maxAnisotropy));
  1321. texture._cachedAnisotropicFilteringLevel = texture.anisotropicFilteringLevel;
  1322. }
  1323. };
  1324. Engine.prototype.readPixels = function (x, y, width, height) {
  1325. var data = new Uint8Array(height * width * 4);
  1326. this._gl.readPixels(0, 0, width, height, this._gl.RGBA, this._gl.UNSIGNED_BYTE, data);
  1327. return data;
  1328. };
  1329. // Dispose
  1330. Engine.prototype.dispose = function () {
  1331. this.hideLoadingUI();
  1332. this.stopRenderLoop();
  1333. while (this.scenes.length) {
  1334. this.scenes[0].dispose();
  1335. }
  1336. for (var name in this._compiledEffects) {
  1337. this._gl.deleteProgram(this._compiledEffects[name]._program);
  1338. }
  1339. for (var i in this._vertexAttribArrays) {
  1340. if (i > this._gl.VERTEX_ATTRIB_ARRAY_ENABLED || !this._vertexAttribArrays[i]) {
  1341. continue;
  1342. }
  1343. this._gl.disableVertexAttribArray(i);
  1344. }
  1345. // Events
  1346. window.removeEventListener("blur", this._onBlur);
  1347. window.removeEventListener("focus", this._onFocus);
  1348. document.removeEventListener("fullscreenchange", this._onFullscreenChange);
  1349. document.removeEventListener("mozfullscreenchange", this._onFullscreenChange);
  1350. document.removeEventListener("webkitfullscreenchange", this._onFullscreenChange);
  1351. document.removeEventListener("msfullscreenchange", this._onFullscreenChange);
  1352. document.removeEventListener("pointerlockchange", this._onPointerLockChange);
  1353. document.removeEventListener("mspointerlockchange", this._onPointerLockChange);
  1354. document.removeEventListener("mozpointerlockchange", this._onPointerLockChange);
  1355. document.removeEventListener("webkitpointerlockchange", this._onPointerLockChange);
  1356. };
  1357. // Loading screen
  1358. Engine.prototype.displayLoadingUI = function () {
  1359. var _this = this;
  1360. this._loadingDiv = document.createElement("div");
  1361. this._loadingDiv.style.opacity = "0";
  1362. this._loadingDiv.style.transition = "opacity 1.5s ease";
  1363. // Loading text
  1364. this._loadingTextDiv = document.createElement("div");
  1365. this._loadingTextDiv.style.position = "absolute";
  1366. this._loadingTextDiv.style.left = "0";
  1367. this._loadingTextDiv.style.top = "50%";
  1368. this._loadingTextDiv.style.marginTop = "80px";
  1369. this._loadingTextDiv.style.width = "100%";
  1370. this._loadingTextDiv.style.height = "20px";
  1371. this._loadingTextDiv.style.fontFamily = "Arial";
  1372. this._loadingTextDiv.style.fontSize = "14px";
  1373. this._loadingTextDiv.style.color = "white";
  1374. this._loadingTextDiv.style.textAlign = "center";
  1375. this._loadingTextDiv.innerHTML = "Loading";
  1376. this._loadingDiv.appendChild(this._loadingTextDiv);
  1377. // Loading img
  1378. var imgBack = new Image();
  1379. 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==";
  1380. imgBack.style.position = "absolute";
  1381. imgBack.style.left = "50%";
  1382. imgBack.style.top = "50%";
  1383. imgBack.style.marginLeft = "-50px";
  1384. imgBack.style.marginTop = "-50px";
  1385. imgBack.style.transition = "transform 1.0s ease";
  1386. imgBack.style.webkitTransition = "-webkit-transform 1.0s ease";
  1387. var deg = 360;
  1388. var onTransitionEnd = function () {
  1389. deg += 360;
  1390. imgBack.style.transform = "rotateZ(" + deg + "deg)";
  1391. imgBack.style.webkitTransform = "rotateZ(" + deg + "deg)";
  1392. };
  1393. imgBack.addEventListener("transitionend", onTransitionEnd);
  1394. imgBack.addEventListener("webkitTransitionEnd", onTransitionEnd);
  1395. this._loadingDiv.appendChild(imgBack);
  1396. // front image
  1397. var imgFront = new Image();
  1398. 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=";
  1399. imgFront.style.position = "absolute";
  1400. imgFront.style.left = "50%";
  1401. imgFront.style.top = "50%";
  1402. imgFront.style.marginLeft = "-50px";
  1403. imgFront.style.marginTop = "-50px";
  1404. this._loadingDiv.appendChild(imgFront);
  1405. // Resize
  1406. this._resizeLoadingUI = function () {
  1407. var canvasRect = _this.getRenderingCanvasClientRect();
  1408. _this._loadingDiv.style.position = "absolute";
  1409. _this._loadingDiv.style.left = canvasRect.left + "px";
  1410. _this._loadingDiv.style.top = canvasRect.top + "px";
  1411. _this._loadingDiv.style.width = canvasRect.width + "px";
  1412. _this._loadingDiv.style.height = canvasRect.height + "px";
  1413. };
  1414. this._resizeLoadingUI();
  1415. window.addEventListener("resize", this._resizeLoadingUI);
  1416. this._loadingDiv.style.backgroundColor = this._loadingDivBackgroundColor;
  1417. document.body.appendChild(this._loadingDiv);
  1418. setTimeout(function () {
  1419. _this._loadingDiv.style.opacity = "1";
  1420. imgBack.style.transform = "rotateZ(360deg)";
  1421. imgBack.style.webkitTransform = "rotateZ(360deg)";
  1422. }, 0);
  1423. };
  1424. Object.defineProperty(Engine.prototype, "loadingUIText", {
  1425. set: function (text) {
  1426. if (!this._loadingDiv) {
  1427. return;
  1428. }
  1429. this._loadingTextDiv.innerHTML = text;
  1430. },
  1431. enumerable: true,
  1432. configurable: true
  1433. });
  1434. Object.defineProperty(Engine.prototype, "loadingUIBackgroundColor", {
  1435. get: function () {
  1436. return this._loadingDivBackgroundColor;
  1437. },
  1438. set: function (color) {
  1439. this._loadingDivBackgroundColor = color;
  1440. if (!this._loadingDiv) {
  1441. return;
  1442. }
  1443. this._loadingDiv.style.backgroundColor = this._loadingDivBackgroundColor;
  1444. },
  1445. enumerable: true,
  1446. configurable: true
  1447. });
  1448. Engine.prototype.hideLoadingUI = function () {
  1449. var _this = this;
  1450. if (!this._loadingDiv) {
  1451. return;
  1452. }
  1453. var onTransitionEnd = function () {
  1454. if (!_this._loadingDiv) {
  1455. return;
  1456. }
  1457. document.body.removeChild(_this._loadingDiv);
  1458. window.removeEventListener("resize", _this._resizeLoadingUI);
  1459. _this._loadingDiv = null;
  1460. };
  1461. this._loadingDiv.style.opacity = "0";
  1462. this._loadingDiv.addEventListener("transitionend", onTransitionEnd);
  1463. };
  1464. // Statics
  1465. Engine.isSupported = function () {
  1466. try {
  1467. var tempcanvas = document.createElement("canvas");
  1468. var gl = tempcanvas.getContext("webgl") || tempcanvas.getContext("experimental-webgl");
  1469. return gl != null && !!window.WebGLRenderingContext;
  1470. } catch (e) {
  1471. return false;
  1472. }
  1473. };
  1474. Engine._ALPHA_DISABLE = 0;
  1475. Engine._ALPHA_ADD = 1;
  1476. Engine._ALPHA_COMBINE = 2;
  1477. Engine._DELAYLOADSTATE_NONE = 0;
  1478. Engine._DELAYLOADSTATE_LOADED = 1;
  1479. Engine._DELAYLOADSTATE_LOADING = 2;
  1480. Engine._DELAYLOADSTATE_NOTLOADED = 4;
  1481. Engine.Epsilon = 0.001;
  1482. Engine.CollisionsEpsilon = 0.001;
  1483. Engine.ShadersRepository = "Babylon/Shaders/";
  1484. return Engine;
  1485. })();
  1486. BABYLON.Engine = Engine;
  1487. })(BABYLON || (BABYLON = {}));
  1488. //# sourceMappingURL=babylon.engine.js.map