babylon.engine.js 90 KB

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