babylon.engine.js 90 KB

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