babylon.engine.js 73 KB

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