babylon.engine.js 92 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932
  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";
  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, forceExponantOfTwo) {
  1398. if (forceExponantOfTwo === void 0) { forceExponantOfTwo = true; }
  1399. var texture = this._gl.createTexture();
  1400. texture._baseWidth = width;
  1401. texture._baseHeight = height;
  1402. if (forceExponantOfTwo) {
  1403. width = BABYLON.Tools.GetExponantOfTwo(width, this._caps.maxTextureSize);
  1404. height = BABYLON.Tools.GetExponantOfTwo(height, this._caps.maxTextureSize);
  1405. }
  1406. this._activeTexturesCache = [];
  1407. texture._width = width;
  1408. texture._height = height;
  1409. texture.isReady = false;
  1410. texture.generateMipMaps = generateMipMaps;
  1411. texture.references = 1;
  1412. texture.samplingMode = samplingMode;
  1413. this.updateTextureSamplingMode(samplingMode, texture);
  1414. this._loadedTexturesCache.push(texture);
  1415. return texture;
  1416. };
  1417. Engine.prototype.updateTextureSamplingMode = function (samplingMode, texture) {
  1418. var filters = getSamplingParameters(samplingMode, texture.generateMipMaps, this._gl);
  1419. this._gl.bindTexture(this._gl.TEXTURE_2D, texture);
  1420. this._gl.texParameteri(this._gl.TEXTURE_2D, this._gl.TEXTURE_MAG_FILTER, filters.mag);
  1421. this._gl.texParameteri(this._gl.TEXTURE_2D, this._gl.TEXTURE_MIN_FILTER, filters.min);
  1422. this._gl.bindTexture(this._gl.TEXTURE_2D, null);
  1423. };
  1424. Engine.prototype.updateDynamicTexture = function (texture, canvas, invertY) {
  1425. this._gl.bindTexture(this._gl.TEXTURE_2D, texture);
  1426. this._gl.pixelStorei(this._gl.UNPACK_FLIP_Y_WEBGL, invertY ? 1 : 0);
  1427. this._gl.texImage2D(this._gl.TEXTURE_2D, 0, this._gl.RGBA, this._gl.RGBA, this._gl.UNSIGNED_BYTE, canvas);
  1428. if (texture.generateMipMaps) {
  1429. this._gl.generateMipmap(this._gl.TEXTURE_2D);
  1430. }
  1431. this._gl.bindTexture(this._gl.TEXTURE_2D, null);
  1432. this._activeTexturesCache = [];
  1433. texture.isReady = true;
  1434. };
  1435. Engine.prototype.updateVideoTexture = function (texture, video, invertY) {
  1436. this._gl.bindTexture(this._gl.TEXTURE_2D, texture);
  1437. this._gl.pixelStorei(this._gl.UNPACK_FLIP_Y_WEBGL, invertY ? 0 : 1); // Video are upside down by default
  1438. // Testing video texture support
  1439. if (this._videoTextureSupported === undefined) {
  1440. this._gl.texImage2D(this._gl.TEXTURE_2D, 0, this._gl.RGBA, this._gl.RGBA, this._gl.UNSIGNED_BYTE, video);
  1441. if (this._gl.getError() !== 0) {
  1442. this._videoTextureSupported = false;
  1443. }
  1444. else {
  1445. this._videoTextureSupported = true;
  1446. }
  1447. }
  1448. // Copy video through the current working canvas if video texture is not supported
  1449. if (!this._videoTextureSupported) {
  1450. if (!texture._workingCanvas) {
  1451. texture._workingCanvas = document.createElement("canvas");
  1452. texture._workingContext = texture._workingCanvas.getContext("2d");
  1453. texture._workingCanvas.width = texture._width;
  1454. texture._workingCanvas.height = texture._height;
  1455. }
  1456. texture._workingContext.drawImage(video, 0, 0, video.videoWidth, video.videoHeight, 0, 0, texture._width, texture._height);
  1457. this._gl.texImage2D(this._gl.TEXTURE_2D, 0, this._gl.RGBA, this._gl.RGBA, this._gl.UNSIGNED_BYTE, texture._workingCanvas);
  1458. }
  1459. else {
  1460. this._gl.texImage2D(this._gl.TEXTURE_2D, 0, this._gl.RGBA, this._gl.RGBA, this._gl.UNSIGNED_BYTE, video);
  1461. }
  1462. if (texture.generateMipMaps) {
  1463. this._gl.generateMipmap(this._gl.TEXTURE_2D);
  1464. }
  1465. this._gl.bindTexture(this._gl.TEXTURE_2D, null);
  1466. this._activeTexturesCache = [];
  1467. texture.isReady = true;
  1468. };
  1469. Engine.prototype.createRenderTargetTexture = function (size, options) {
  1470. // old version had a "generateMipMaps" arg instead of options.
  1471. // if options.generateMipMaps is undefined, consider that options itself if the generateMipmaps value
  1472. // in the same way, generateDepthBuffer is defaulted to true
  1473. var generateMipMaps = false;
  1474. var generateDepthBuffer = true;
  1475. var type = Engine.TEXTURETYPE_UNSIGNED_INT;
  1476. var samplingMode = BABYLON.Texture.TRILINEAR_SAMPLINGMODE;
  1477. if (options !== undefined) {
  1478. generateMipMaps = options.generateMipMaps === undefined ? options : options.generateMipmaps;
  1479. generateDepthBuffer = options.generateDepthBuffer === undefined ? true : options.generateDepthBuffer;
  1480. type = options.type === undefined ? type : options.type;
  1481. if (options.samplingMode !== undefined) {
  1482. samplingMode = options.samplingMode;
  1483. }
  1484. if (type === Engine.TEXTURETYPE_FLOAT) {
  1485. // if floating point (gl.FLOAT) then force to NEAREST_SAMPLINGMODE
  1486. samplingMode = BABYLON.Texture.NEAREST_SAMPLINGMODE;
  1487. }
  1488. }
  1489. var gl = this._gl;
  1490. var texture = gl.createTexture();
  1491. gl.bindTexture(gl.TEXTURE_2D, texture);
  1492. var width = size.width || size;
  1493. var height = size.height || size;
  1494. var filters = getSamplingParameters(samplingMode, generateMipMaps, gl);
  1495. if (type === Engine.TEXTURETYPE_FLOAT && !this._caps.textureFloat) {
  1496. type = Engine.TEXTURETYPE_UNSIGNED_INT;
  1497. BABYLON.Tools.Warn("Float textures are not supported. Render target forced to TEXTURETYPE_UNSIGNED_BYTE type");
  1498. }
  1499. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, filters.mag);
  1500. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, filters.min);
  1501. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
  1502. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
  1503. gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, width, height, 0, gl.RGBA, getWebGLTextureType(gl, type), null);
  1504. var depthBuffer;
  1505. // Create the depth buffer
  1506. if (generateDepthBuffer) {
  1507. depthBuffer = gl.createRenderbuffer();
  1508. gl.bindRenderbuffer(gl.RENDERBUFFER, depthBuffer);
  1509. gl.renderbufferStorage(gl.RENDERBUFFER, gl.DEPTH_COMPONENT16, width, height);
  1510. }
  1511. // Create the framebuffer
  1512. var framebuffer = gl.createFramebuffer();
  1513. gl.bindFramebuffer(gl.FRAMEBUFFER, framebuffer);
  1514. gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, texture, 0);
  1515. if (generateDepthBuffer) {
  1516. gl.framebufferRenderbuffer(gl.FRAMEBUFFER, gl.DEPTH_ATTACHMENT, gl.RENDERBUFFER, depthBuffer);
  1517. }
  1518. // Unbind
  1519. gl.bindTexture(gl.TEXTURE_2D, null);
  1520. gl.bindRenderbuffer(gl.RENDERBUFFER, null);
  1521. gl.bindFramebuffer(gl.FRAMEBUFFER, null);
  1522. texture._framebuffer = framebuffer;
  1523. if (generateDepthBuffer) {
  1524. texture._depthBuffer = depthBuffer;
  1525. }
  1526. texture._width = width;
  1527. texture._height = height;
  1528. texture.isReady = true;
  1529. texture.generateMipMaps = generateMipMaps;
  1530. texture.references = 1;
  1531. texture.samplingMode = samplingMode;
  1532. this._activeTexturesCache = [];
  1533. this._loadedTexturesCache.push(texture);
  1534. return texture;
  1535. };
  1536. Engine.prototype.createCubeTexture = function (rootUrl, scene, extensions, noMipmap) {
  1537. var _this = this;
  1538. var gl = this._gl;
  1539. var texture = gl.createTexture();
  1540. texture.isCube = true;
  1541. texture.url = rootUrl;
  1542. texture.references = 1;
  1543. this._loadedTexturesCache.push(texture);
  1544. var extension = rootUrl.substr(rootUrl.length - 4, 4).toLowerCase();
  1545. var isDDS = this.getCaps().s3tc && (extension === ".dds");
  1546. if (isDDS) {
  1547. BABYLON.Tools.LoadFile(rootUrl, function (data) {
  1548. var info = BABYLON.Internals.DDSTools.GetDDSInfo(data);
  1549. var loadMipmap = (info.isRGB || info.isLuminance || info.mipmapCount > 1) && !noMipmap;
  1550. gl.bindTexture(gl.TEXTURE_CUBE_MAP, texture);
  1551. gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, 1);
  1552. BABYLON.Internals.DDSTools.UploadDDSLevels(_this._gl, _this.getCaps().s3tc, data, info, loadMipmap, 6);
  1553. if (!noMipmap && !info.isFourCC && info.mipmapCount === 1) {
  1554. gl.generateMipmap(gl.TEXTURE_CUBE_MAP);
  1555. }
  1556. gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
  1557. gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_MIN_FILTER, loadMipmap ? gl.LINEAR_MIPMAP_LINEAR : gl.LINEAR);
  1558. gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
  1559. gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
  1560. gl.bindTexture(gl.TEXTURE_CUBE_MAP, null);
  1561. _this._activeTexturesCache = [];
  1562. texture._width = info.width;
  1563. texture._height = info.height;
  1564. texture.isReady = true;
  1565. }, null, null, true);
  1566. }
  1567. else {
  1568. cascadeLoad(rootUrl, scene, function (imgs) {
  1569. var width = BABYLON.Tools.GetExponantOfTwo(imgs[0].width, _this._caps.maxCubemapTextureSize);
  1570. var height = width;
  1571. _this._prepareWorkingCanvas();
  1572. _this._workingCanvas.width = width;
  1573. _this._workingCanvas.height = height;
  1574. var faces = [
  1575. gl.TEXTURE_CUBE_MAP_POSITIVE_X, gl.TEXTURE_CUBE_MAP_POSITIVE_Y, gl.TEXTURE_CUBE_MAP_POSITIVE_Z,
  1576. gl.TEXTURE_CUBE_MAP_NEGATIVE_X, gl.TEXTURE_CUBE_MAP_NEGATIVE_Y, gl.TEXTURE_CUBE_MAP_NEGATIVE_Z
  1577. ];
  1578. gl.bindTexture(gl.TEXTURE_CUBE_MAP, texture);
  1579. gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, 0);
  1580. for (var index = 0; index < faces.length; index++) {
  1581. _this._workingContext.drawImage(imgs[index], 0, 0, imgs[index].width, imgs[index].height, 0, 0, width, height);
  1582. gl.texImage2D(faces[index], 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, _this._workingCanvas);
  1583. }
  1584. if (!noMipmap) {
  1585. gl.generateMipmap(gl.TEXTURE_CUBE_MAP);
  1586. }
  1587. gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
  1588. gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_MIN_FILTER, noMipmap ? gl.LINEAR : gl.LINEAR_MIPMAP_LINEAR);
  1589. gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
  1590. gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
  1591. gl.bindTexture(gl.TEXTURE_CUBE_MAP, null);
  1592. _this._activeTexturesCache = [];
  1593. texture._width = width;
  1594. texture._height = height;
  1595. texture.isReady = true;
  1596. }, extensions);
  1597. }
  1598. return texture;
  1599. };
  1600. Engine.prototype._releaseTexture = function (texture) {
  1601. var gl = this._gl;
  1602. if (texture._framebuffer) {
  1603. gl.deleteFramebuffer(texture._framebuffer);
  1604. }
  1605. if (texture._depthBuffer) {
  1606. gl.deleteRenderbuffer(texture._depthBuffer);
  1607. }
  1608. gl.deleteTexture(texture);
  1609. // Unbind channels
  1610. for (var channel = 0; channel < this._caps.maxTexturesImageUnits; channel++) {
  1611. this._gl.activeTexture(this._gl["TEXTURE" + channel]);
  1612. this._gl.bindTexture(this._gl.TEXTURE_2D, null);
  1613. this._gl.bindTexture(this._gl.TEXTURE_CUBE_MAP, null);
  1614. this._activeTexturesCache[channel] = null;
  1615. }
  1616. var index = this._loadedTexturesCache.indexOf(texture);
  1617. if (index !== -1) {
  1618. this._loadedTexturesCache.splice(index, 1);
  1619. }
  1620. };
  1621. Engine.prototype.bindSamplers = function (effect) {
  1622. this._gl.useProgram(effect.getProgram());
  1623. var samplers = effect.getSamplers();
  1624. for (var index = 0; index < samplers.length; index++) {
  1625. var uniform = effect.getUniform(samplers[index]);
  1626. this._gl.uniform1i(uniform, index);
  1627. }
  1628. this._currentEffect = null;
  1629. };
  1630. Engine.prototype._bindTexture = function (channel, texture) {
  1631. this._gl.activeTexture(this._gl["TEXTURE" + channel]);
  1632. this._gl.bindTexture(this._gl.TEXTURE_2D, texture);
  1633. this._activeTexturesCache[channel] = null;
  1634. };
  1635. Engine.prototype.setTextureFromPostProcess = function (channel, postProcess) {
  1636. this._bindTexture(channel, postProcess._textures.data[postProcess._currentRenderTextureInd]);
  1637. };
  1638. Engine.prototype.setTexture = function (channel, texture) {
  1639. if (channel < 0) {
  1640. return;
  1641. }
  1642. // Not ready?
  1643. if (!texture || !texture.isReady()) {
  1644. if (this._activeTexturesCache[channel] != null) {
  1645. this._gl.activeTexture(this._gl["TEXTURE" + channel]);
  1646. this._gl.bindTexture(this._gl.TEXTURE_2D, null);
  1647. this._gl.bindTexture(this._gl.TEXTURE_CUBE_MAP, null);
  1648. this._activeTexturesCache[channel] = null;
  1649. }
  1650. return;
  1651. }
  1652. // Video
  1653. if (texture instanceof BABYLON.VideoTexture) {
  1654. if (texture.update()) {
  1655. this._activeTexturesCache[channel] = null;
  1656. }
  1657. }
  1658. else if (texture.delayLoadState === Engine.DELAYLOADSTATE_NOTLOADED) {
  1659. texture.delayLoad();
  1660. return;
  1661. }
  1662. if (this._activeTexturesCache[channel] === texture) {
  1663. return;
  1664. }
  1665. this._activeTexturesCache[channel] = texture;
  1666. var internalTexture = texture.getInternalTexture();
  1667. this._gl.activeTexture(this._gl["TEXTURE" + channel]);
  1668. if (internalTexture.isCube) {
  1669. this._gl.bindTexture(this._gl.TEXTURE_CUBE_MAP, internalTexture);
  1670. if (internalTexture._cachedCoordinatesMode !== texture.coordinatesMode) {
  1671. internalTexture._cachedCoordinatesMode = texture.coordinatesMode;
  1672. // CUBIC_MODE and SKYBOX_MODE both require CLAMP_TO_EDGE. All other modes use REPEAT.
  1673. var textureWrapMode = (texture.coordinatesMode !== BABYLON.Texture.CUBIC_MODE && texture.coordinatesMode !== BABYLON.Texture.SKYBOX_MODE) ? this._gl.REPEAT : this._gl.CLAMP_TO_EDGE;
  1674. this._gl.texParameteri(this._gl.TEXTURE_CUBE_MAP, this._gl.TEXTURE_WRAP_S, textureWrapMode);
  1675. this._gl.texParameteri(this._gl.TEXTURE_CUBE_MAP, this._gl.TEXTURE_WRAP_T, textureWrapMode);
  1676. }
  1677. this._setAnisotropicLevel(this._gl.TEXTURE_CUBE_MAP, texture);
  1678. }
  1679. else {
  1680. this._gl.bindTexture(this._gl.TEXTURE_2D, internalTexture);
  1681. if (internalTexture._cachedWrapU !== texture.wrapU) {
  1682. internalTexture._cachedWrapU = texture.wrapU;
  1683. switch (texture.wrapU) {
  1684. case BABYLON.Texture.WRAP_ADDRESSMODE:
  1685. this._gl.texParameteri(this._gl.TEXTURE_2D, this._gl.TEXTURE_WRAP_S, this._gl.REPEAT);
  1686. break;
  1687. case BABYLON.Texture.CLAMP_ADDRESSMODE:
  1688. this._gl.texParameteri(this._gl.TEXTURE_2D, this._gl.TEXTURE_WRAP_S, this._gl.CLAMP_TO_EDGE);
  1689. break;
  1690. case BABYLON.Texture.MIRROR_ADDRESSMODE:
  1691. this._gl.texParameteri(this._gl.TEXTURE_2D, this._gl.TEXTURE_WRAP_S, this._gl.MIRRORED_REPEAT);
  1692. break;
  1693. }
  1694. }
  1695. if (internalTexture._cachedWrapV !== texture.wrapV) {
  1696. internalTexture._cachedWrapV = texture.wrapV;
  1697. switch (texture.wrapV) {
  1698. case BABYLON.Texture.WRAP_ADDRESSMODE:
  1699. this._gl.texParameteri(this._gl.TEXTURE_2D, this._gl.TEXTURE_WRAP_T, this._gl.REPEAT);
  1700. break;
  1701. case BABYLON.Texture.CLAMP_ADDRESSMODE:
  1702. this._gl.texParameteri(this._gl.TEXTURE_2D, this._gl.TEXTURE_WRAP_T, this._gl.CLAMP_TO_EDGE);
  1703. break;
  1704. case BABYLON.Texture.MIRROR_ADDRESSMODE:
  1705. this._gl.texParameteri(this._gl.TEXTURE_2D, this._gl.TEXTURE_WRAP_T, this._gl.MIRRORED_REPEAT);
  1706. break;
  1707. }
  1708. }
  1709. this._setAnisotropicLevel(this._gl.TEXTURE_2D, texture);
  1710. }
  1711. };
  1712. Engine.prototype._setAnisotropicLevel = function (key, texture) {
  1713. var anisotropicFilterExtension = this._caps.textureAnisotropicFilterExtension;
  1714. var value = texture.anisotropicFilteringLevel;
  1715. if (texture.getInternalTexture().samplingMode === BABYLON.Texture.NEAREST_SAMPLINGMODE) {
  1716. value = 1;
  1717. }
  1718. if (anisotropicFilterExtension && texture._cachedAnisotropicFilteringLevel !== value) {
  1719. this._gl.texParameterf(key, anisotropicFilterExtension.TEXTURE_MAX_ANISOTROPY_EXT, Math.min(value, this._caps.maxAnisotropy));
  1720. texture._cachedAnisotropicFilteringLevel = value;
  1721. }
  1722. };
  1723. Engine.prototype.readPixels = function (x, y, width, height) {
  1724. var data = new Uint8Array(height * width * 4);
  1725. this._gl.readPixels(0, 0, width, height, this._gl.RGBA, this._gl.UNSIGNED_BYTE, data);
  1726. return data;
  1727. };
  1728. // Dispose
  1729. Engine.prototype.dispose = function () {
  1730. this.hideLoadingUI();
  1731. this.stopRenderLoop();
  1732. // Release scenes
  1733. while (this.scenes.length) {
  1734. this.scenes[0].dispose();
  1735. }
  1736. // Release audio engine
  1737. Engine.audioEngine.dispose();
  1738. // Release effects
  1739. for (var name in this._compiledEffects) {
  1740. this._gl.deleteProgram(this._compiledEffects[name]._program);
  1741. }
  1742. // Unbind
  1743. for (var i in this._vertexAttribArrays) {
  1744. if (i > this._gl.VERTEX_ATTRIB_ARRAY_ENABLED || !this._vertexAttribArrays[i]) {
  1745. continue;
  1746. }
  1747. this._gl.disableVertexAttribArray(i);
  1748. }
  1749. // Events
  1750. window.removeEventListener("blur", this._onBlur);
  1751. window.removeEventListener("focus", this._onFocus);
  1752. document.removeEventListener("fullscreenchange", this._onFullscreenChange);
  1753. document.removeEventListener("mozfullscreenchange", this._onFullscreenChange);
  1754. document.removeEventListener("webkitfullscreenchange", this._onFullscreenChange);
  1755. document.removeEventListener("msfullscreenchange", this._onFullscreenChange);
  1756. document.removeEventListener("pointerlockchange", this._onPointerLockChange);
  1757. document.removeEventListener("mspointerlockchange", this._onPointerLockChange);
  1758. document.removeEventListener("mozpointerlockchange", this._onPointerLockChange);
  1759. document.removeEventListener("webkitpointerlockchange", this._onPointerLockChange);
  1760. };
  1761. // Loading screen
  1762. Engine.prototype.displayLoadingUI = function () {
  1763. var _this = this;
  1764. this._loadingDiv = document.createElement("div");
  1765. this._loadingDiv.style.opacity = "0";
  1766. this._loadingDiv.style.transition = "opacity 1.5s ease";
  1767. // Loading text
  1768. this._loadingTextDiv = document.createElement("div");
  1769. this._loadingTextDiv.style.position = "absolute";
  1770. this._loadingTextDiv.style.left = "0";
  1771. this._loadingTextDiv.style.top = "50%";
  1772. this._loadingTextDiv.style.marginTop = "80px";
  1773. this._loadingTextDiv.style.width = "100%";
  1774. this._loadingTextDiv.style.height = "20px";
  1775. this._loadingTextDiv.style.fontFamily = "Arial";
  1776. this._loadingTextDiv.style.fontSize = "14px";
  1777. this._loadingTextDiv.style.color = "white";
  1778. this._loadingTextDiv.style.textAlign = "center";
  1779. this._loadingTextDiv.innerHTML = "Loading";
  1780. this._loadingDiv.appendChild(this._loadingTextDiv);
  1781. // Loading img
  1782. var imgBack = new Image();
  1783. 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==";
  1784. imgBack.style.position = "absolute";
  1785. imgBack.style.left = "50%";
  1786. imgBack.style.top = "50%";
  1787. imgBack.style.marginLeft = "-50px";
  1788. imgBack.style.marginTop = "-50px";
  1789. imgBack.style.transition = "transform 1.0s ease";
  1790. imgBack.style.webkitTransition = "-webkit-transform 1.0s ease";
  1791. var deg = 360;
  1792. var onTransitionEnd = function () {
  1793. deg += 360;
  1794. imgBack.style.transform = "rotateZ(" + deg + "deg)";
  1795. imgBack.style.webkitTransform = "rotateZ(" + deg + "deg)";
  1796. };
  1797. imgBack.addEventListener("transitionend", onTransitionEnd);
  1798. imgBack.addEventListener("webkitTransitionEnd", onTransitionEnd);
  1799. this._loadingDiv.appendChild(imgBack);
  1800. // front image
  1801. var imgFront = new Image();
  1802. 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=";
  1803. imgFront.style.position = "absolute";
  1804. imgFront.style.left = "50%";
  1805. imgFront.style.top = "50%";
  1806. imgFront.style.marginLeft = "-50px";
  1807. imgFront.style.marginTop = "-50px";
  1808. this._loadingDiv.appendChild(imgFront);
  1809. // Resize
  1810. this._resizeLoadingUI = function () {
  1811. var canvasRect = _this.getRenderingCanvasClientRect();
  1812. _this._loadingDiv.style.position = "absolute";
  1813. _this._loadingDiv.style.left = canvasRect.left + "px";
  1814. _this._loadingDiv.style.top = canvasRect.top + "px";
  1815. _this._loadingDiv.style.width = canvasRect.width + "px";
  1816. _this._loadingDiv.style.height = canvasRect.height + "px";
  1817. };
  1818. this._resizeLoadingUI();
  1819. window.addEventListener("resize", this._resizeLoadingUI);
  1820. this._loadingDiv.style.backgroundColor = this._loadingDivBackgroundColor;
  1821. document.body.appendChild(this._loadingDiv);
  1822. setTimeout(function () {
  1823. _this._loadingDiv.style.opacity = "1";
  1824. imgBack.style.transform = "rotateZ(360deg)";
  1825. imgBack.style.webkitTransform = "rotateZ(360deg)";
  1826. }, 0);
  1827. };
  1828. Object.defineProperty(Engine.prototype, "loadingUIText", {
  1829. set: function (text) {
  1830. if (!this._loadingDiv) {
  1831. return;
  1832. }
  1833. this._loadingTextDiv.innerHTML = text;
  1834. },
  1835. enumerable: true,
  1836. configurable: true
  1837. });
  1838. Object.defineProperty(Engine.prototype, "loadingUIBackgroundColor", {
  1839. get: function () {
  1840. return this._loadingDivBackgroundColor;
  1841. },
  1842. set: function (color) {
  1843. this._loadingDivBackgroundColor = color;
  1844. if (!this._loadingDiv) {
  1845. return;
  1846. }
  1847. this._loadingDiv.style.backgroundColor = this._loadingDivBackgroundColor;
  1848. },
  1849. enumerable: true,
  1850. configurable: true
  1851. });
  1852. Engine.prototype.hideLoadingUI = function () {
  1853. var _this = this;
  1854. if (!this._loadingDiv) {
  1855. return;
  1856. }
  1857. var onTransitionEnd = function () {
  1858. if (!_this._loadingDiv) {
  1859. return;
  1860. }
  1861. document.body.removeChild(_this._loadingDiv);
  1862. window.removeEventListener("resize", _this._resizeLoadingUI);
  1863. _this._loadingDiv = null;
  1864. };
  1865. this._loadingDiv.style.opacity = "0";
  1866. this._loadingDiv.addEventListener("transitionend", onTransitionEnd);
  1867. };
  1868. // FPS
  1869. Engine.prototype.getFps = function () {
  1870. return this.fps;
  1871. };
  1872. Engine.prototype.getDeltaTime = function () {
  1873. return this.deltaTime;
  1874. };
  1875. Engine.prototype._measureFps = function () {
  1876. this.previousFramesDuration.push(BABYLON.Tools.Now);
  1877. var length = this.previousFramesDuration.length;
  1878. if (length >= 2) {
  1879. this.deltaTime = this.previousFramesDuration[length - 1] - this.previousFramesDuration[length - 2];
  1880. }
  1881. if (length >= this.fpsRange) {
  1882. if (length > this.fpsRange) {
  1883. this.previousFramesDuration.splice(0, 1);
  1884. length = this.previousFramesDuration.length;
  1885. }
  1886. var sum = 0;
  1887. for (var id = 0; id < length - 1; id++) {
  1888. sum += this.previousFramesDuration[id + 1] - this.previousFramesDuration[id];
  1889. }
  1890. this.fps = 1000.0 / (sum / (length - 1));
  1891. }
  1892. };
  1893. // Statics
  1894. Engine.isSupported = function () {
  1895. try {
  1896. // Avoid creating an unsized context for CocoonJS, since size determined on first creation. Is not resizable
  1897. if (navigator.isCocoonJS) {
  1898. return true;
  1899. }
  1900. var tempcanvas = document.createElement("canvas");
  1901. var gl = tempcanvas.getContext("webgl") || tempcanvas.getContext("experimental-webgl");
  1902. return gl != null && !!window.WebGLRenderingContext;
  1903. }
  1904. catch (e) {
  1905. return false;
  1906. }
  1907. };
  1908. // Const statics
  1909. Engine._ALPHA_DISABLE = 0;
  1910. Engine._ALPHA_ADD = 1;
  1911. Engine._ALPHA_COMBINE = 2;
  1912. Engine._DELAYLOADSTATE_NONE = 0;
  1913. Engine._DELAYLOADSTATE_LOADED = 1;
  1914. Engine._DELAYLOADSTATE_LOADING = 2;
  1915. Engine._DELAYLOADSTATE_NOTLOADED = 4;
  1916. Engine._TEXTUREFORMAT_ALPHA = 0;
  1917. Engine._TEXTUREFORMAT_LUMINANCE = 1;
  1918. Engine._TEXTUREFORMAT_LUMINANCE_ALPHA = 2;
  1919. Engine._TEXTUREFORMAT_RGB = 4;
  1920. Engine._TEXTUREFORMAT_RGBA = 4;
  1921. Engine._TEXTURETYPE_UNSIGNED_INT = 0;
  1922. Engine._TEXTURETYPE_FLOAT = 1;
  1923. // Updatable statics so stick with vars here
  1924. Engine.Epsilon = 0.001;
  1925. Engine.CollisionsEpsilon = 0.001;
  1926. Engine.CodeRepository = "Babylon/";
  1927. Engine.ShadersRepository = "Babylon/Shaders/";
  1928. return Engine;
  1929. })();
  1930. BABYLON.Engine = Engine;
  1931. })(BABYLON || (BABYLON = {}));
  1932. //# sourceMappingURL=babylon.engine.js.map