babylon.engine.js 88 KB

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