babylon.engine.js 91 KB

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