babylon.engine.js 91 KB

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