babylon.engine.js 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175
  1. var BABYLON;
  2. (function (BABYLON) {
  3. var compileShader = function (gl, source, type, defines) {
  4. var shader = gl.createShader(type === "vertex" ? gl.VERTEX_SHADER : gl.FRAGMENT_SHADER);
  5. gl.shaderSource(shader, (defines ? defines + "\n" : "") + source);
  6. gl.compileShader(shader);
  7. if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
  8. throw new Error(gl.getShaderInfoLog(shader));
  9. }
  10. return shader;
  11. };
  12. var getExponantOfTwo = function (value, max) {
  13. var count = 1;
  14. do {
  15. count *= 2;
  16. } while(count < value);
  17. if (count > max)
  18. count = max;
  19. return count;
  20. };
  21. var prepareWebGLTexture = function (texture, gl, scene, width, height, invertY, noMipmap, processFunction) {
  22. var engine = scene.getEngine();
  23. var potWidth = getExponantOfTwo(width, engine.getCaps().maxTextureSize);
  24. var potHeight = getExponantOfTwo(height, engine.getCaps().maxTextureSize);
  25. gl.bindTexture(gl.TEXTURE_2D, texture);
  26. gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, invertY === undefined ? 1 : (invertY ? 1 : 0));
  27. processFunction(potWidth, potHeight);
  28. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
  29. if (noMipmap) {
  30. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
  31. } else {
  32. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR_MIPMAP_LINEAR);
  33. gl.generateMipmap(gl.TEXTURE_2D);
  34. }
  35. gl.bindTexture(gl.TEXTURE_2D, null);
  36. engine._activeTexturesCache = [];
  37. texture._baseWidth = width;
  38. texture._baseHeight = height;
  39. texture._width = potWidth;
  40. texture._height = potHeight;
  41. texture.isReady = true;
  42. scene._removePendingData(texture);
  43. };
  44. // ANY
  45. var cascadeLoad = function (rootUrl, index, loadedImages, scene, onfinish, extensions) {
  46. var img;
  47. var onload = function () {
  48. loadedImages.push(img);
  49. scene._removePendingData(img);
  50. if (index != extensions.length - 1) {
  51. cascadeLoad(rootUrl, index + 1, loadedImages, scene, onfinish, extensions);
  52. } else {
  53. onfinish(loadedImages);
  54. }
  55. };
  56. var onerror = function () {
  57. scene._removePendingData(img);
  58. };
  59. img = BABYLON.Tools.LoadImage(rootUrl + extensions[index], onload, onerror, scene.database);
  60. scene._addPendingData(img);
  61. };
  62. var EngineCapabilities = (function () {
  63. function EngineCapabilities() {
  64. }
  65. return EngineCapabilities;
  66. })();
  67. BABYLON.EngineCapabilities = EngineCapabilities;
  68. var Engine = (function () {
  69. function Engine(canvas, antialias, options) {
  70. var _this = this;
  71. // Public members
  72. this.isFullscreen = false;
  73. this.isPointerLock = false;
  74. this.forceWireframe = false;
  75. this.cullBackFaces = true;
  76. this.renderEvenInBackground = true;
  77. this.scenes = new Array();
  78. this._windowIsBackground = false;
  79. this._runningLoop = false;
  80. // Cache
  81. this._loadedTexturesCache = new Array();
  82. this._activeTexturesCache = new Array();
  83. this._compiledEffects = {};
  84. this._lastVertexAttribIndex = 0;
  85. this._depthMask = false;
  86. this._renderingCanvas = canvas;
  87. options = options || {};
  88. options.antialias = antialias;
  89. try {
  90. this._gl = canvas.getContext("webgl", options) || canvas.getContext("experimental-webgl", options);
  91. } catch (e) {
  92. throw new Error("WebGL not supported");
  93. }
  94. if (!this._gl) {
  95. throw new Error("WebGL not supported");
  96. }
  97. this._onBlur = function () {
  98. _this._windowIsBackground = true;
  99. };
  100. this._onFocus = function () {
  101. _this._windowIsBackground = false;
  102. };
  103. window.addEventListener("blur", this._onBlur);
  104. window.addEventListener("focus", this._onFocus);
  105. // Textures
  106. this._workingCanvas = document.createElement("canvas");
  107. this._workingContext = this._workingCanvas.getContext("2d");
  108. // Viewport
  109. this._hardwareScalingLevel = 1.0 / (window.devicePixelRatio || 1.0);
  110. this.resize();
  111. // Caps
  112. this._caps = new EngineCapabilities();
  113. this._caps.maxTexturesImageUnits = this._gl.getParameter(this._gl.MAX_TEXTURE_IMAGE_UNITS);
  114. this._caps.maxTextureSize = this._gl.getParameter(this._gl.MAX_TEXTURE_SIZE);
  115. this._caps.maxCubemapTextureSize = this._gl.getParameter(this._gl.MAX_CUBE_MAP_TEXTURE_SIZE);
  116. this._caps.maxRenderTextureSize = this._gl.getParameter(this._gl.MAX_RENDERBUFFER_SIZE);
  117. // Extensions
  118. this._caps.standardDerivatives = (this._gl.getExtension('OES_standard_derivatives') !== null);
  119. this._caps.s3tc = this._gl.getExtension('WEBGL_compressed_texture_s3tc');
  120. this._caps.textureFloat = (this._gl.getExtension('OES_texture_float') !== null);
  121. 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');
  122. this._caps.maxAnisotropy = this._caps.textureAnisotropicFilterExtension ? this._gl.getParameter(this._caps.textureAnisotropicFilterExtension.MAX_TEXTURE_MAX_ANISOTROPY_EXT) : 0;
  123. // Depth buffer
  124. this.setDepthBuffer(true);
  125. this.setDepthFunctionToLessOrEqual();
  126. this.setDepthWrite(true);
  127. // Fullscreen
  128. this._onFullscreenChange = function () {
  129. if (document.fullscreen !== undefined) {
  130. _this.isFullscreen = document.fullscreen;
  131. } else if (document.mozFullScreen !== undefined) {
  132. _this.isFullscreen = document.mozFullScreen;
  133. } else if (document.webkitIsFullScreen !== undefined) {
  134. _this.isFullscreen = document.webkitIsFullScreen;
  135. } else if (document.msIsFullScreen !== undefined) {
  136. _this.isFullscreen = document.msIsFullScreen;
  137. }
  138. // Pointer lock
  139. if (_this.isFullscreen && _this._pointerLockRequested) {
  140. canvas.requestPointerLock = canvas.requestPointerLock || canvas.msRequestPointerLock || canvas.mozRequestPointerLock || canvas.webkitRequestPointerLock;
  141. if (canvas.requestPointerLock) {
  142. canvas.requestPointerLock();
  143. }
  144. }
  145. };
  146. document.addEventListener("fullscreenchange", this._onFullscreenChange, false);
  147. document.addEventListener("mozfullscreenchange", this._onFullscreenChange, false);
  148. document.addEventListener("webkitfullscreenchange", this._onFullscreenChange, false);
  149. document.addEventListener("msfullscreenchange", this._onFullscreenChange, false);
  150. // Pointer lock
  151. this._onPointerLockChange = function () {
  152. _this.isPointerLock = (document.mozPointerLockElement === canvas || document.webkitPointerLockElement === canvas || document.msPointerLockElement === canvas || document.pointerLockElement === canvas);
  153. };
  154. document.addEventListener("pointerlockchange", this._onPointerLockChange, false);
  155. document.addEventListener("mspointerlockchange", this._onPointerLockChange, false);
  156. document.addEventListener("mozpointerlockchange", this._onPointerLockChange, false);
  157. document.addEventListener("webkitpointerlockchange", this._onPointerLockChange, false);
  158. }
  159. Engine.prototype.getAspectRatio = function (camera) {
  160. var viewport = camera.viewport;
  161. return (this.getRenderWidth() * viewport.width) / (this.getRenderHeight() * viewport.height);
  162. };
  163. Engine.prototype.getRenderWidth = function () {
  164. if (this._currentRenderTarget) {
  165. return this._currentRenderTarget._width;
  166. }
  167. return this._renderingCanvas.width;
  168. };
  169. Engine.prototype.getRenderHeight = function () {
  170. if (this._currentRenderTarget) {
  171. return this._currentRenderTarget._height;
  172. }
  173. return this._renderingCanvas.height;
  174. };
  175. Engine.prototype.getRenderingCanvas = function () {
  176. return this._renderingCanvas;
  177. };
  178. Engine.prototype.setHardwareScalingLevel = function (level) {
  179. this._hardwareScalingLevel = level;
  180. this.resize();
  181. };
  182. Engine.prototype.getHardwareScalingLevel = function () {
  183. return this._hardwareScalingLevel;
  184. };
  185. Engine.prototype.getLoadedTexturesCache = function () {
  186. return this._loadedTexturesCache;
  187. };
  188. Engine.prototype.getCaps = function () {
  189. return this._caps;
  190. };
  191. // Methods
  192. Engine.prototype.setDepthFunctionToGreater = function () {
  193. this._gl.depthFunc(this._gl.GREATER);
  194. };
  195. Engine.prototype.setDepthFunctionToGreaterOrEqual = function () {
  196. this._gl.depthFunc(this._gl.GEQUAL);
  197. };
  198. Engine.prototype.setDepthFunctionToLess = function () {
  199. this._gl.depthFunc(this._gl.LESS);
  200. };
  201. Engine.prototype.setDepthFunctionToLessOrEqual = function () {
  202. this._gl.depthFunc(this._gl.LEQUAL);
  203. };
  204. Engine.prototype.stopRenderLoop = function () {
  205. this._renderFunction = null;
  206. this._runningLoop = false;
  207. };
  208. Engine.prototype._renderLoop = function () {
  209. var _this = this;
  210. var shouldRender = true;
  211. if (!this.renderEvenInBackground && this._windowIsBackground) {
  212. shouldRender = false;
  213. }
  214. if (shouldRender) {
  215. // Start new frame
  216. this.beginFrame();
  217. if (this._renderFunction) {
  218. this._renderFunction();
  219. }
  220. // Present
  221. this.endFrame();
  222. }
  223. if (this._runningLoop) {
  224. // Register new frame
  225. BABYLON.Tools.QueueNewFrame(function () {
  226. _this._renderLoop();
  227. });
  228. }
  229. };
  230. Engine.prototype.runRenderLoop = function (renderFunction) {
  231. var _this = this;
  232. this._runningLoop = true;
  233. this._renderFunction = renderFunction;
  234. BABYLON.Tools.QueueNewFrame(function () {
  235. _this._renderLoop();
  236. });
  237. };
  238. Engine.prototype.switchFullscreen = function (requestPointerLock) {
  239. if (this.isFullscreen) {
  240. BABYLON.Tools.ExitFullscreen();
  241. } else {
  242. this._pointerLockRequested = requestPointerLock;
  243. BABYLON.Tools.RequestFullscreen(this._renderingCanvas);
  244. }
  245. };
  246. Engine.prototype.clear = function (color, backBuffer, depthStencil) {
  247. this._gl.clearColor(color.r, color.g, color.b, color.a !== undefined ? color.a : 1.0);
  248. if (this._depthMask) {
  249. this._gl.clearDepth(1.0);
  250. }
  251. var mode = 0;
  252. if (backBuffer)
  253. mode |= this._gl.COLOR_BUFFER_BIT;
  254. if (depthStencil && this._depthMask)
  255. mode |= this._gl.DEPTH_BUFFER_BIT;
  256. this._gl.clear(mode);
  257. };
  258. Engine.prototype.setViewport = function (viewport, requiredWidth, requiredHeight) {
  259. var width = requiredWidth || this._renderingCanvas.width;
  260. var height = requiredHeight || this._renderingCanvas.height;
  261. var x = viewport.x || 0;
  262. var y = viewport.y || 0;
  263. this._cachedViewport = viewport;
  264. this._gl.viewport(x * width, y * height, width * viewport.width, height * viewport.height);
  265. };
  266. Engine.prototype.setDirectViewport = function (x, y, width, height) {
  267. this._cachedViewport = null;
  268. this._gl.viewport(x, y, width, height);
  269. };
  270. Engine.prototype.beginFrame = function () {
  271. BABYLON.Tools._MeasureFps();
  272. };
  273. Engine.prototype.endFrame = function () {
  274. this.flushFramebuffer();
  275. };
  276. Engine.prototype.resize = function () {
  277. this._renderingCanvas.width = this._renderingCanvas.clientWidth / this._hardwareScalingLevel;
  278. this._renderingCanvas.height = this._renderingCanvas.clientHeight / this._hardwareScalingLevel;
  279. };
  280. Engine.prototype.bindFramebuffer = function (texture) {
  281. this._currentRenderTarget = texture;
  282. var gl = this._gl;
  283. gl.bindFramebuffer(gl.FRAMEBUFFER, texture._framebuffer);
  284. this._gl.viewport(0, 0, texture._width, texture._height);
  285. this.wipeCaches();
  286. };
  287. Engine.prototype.unBindFramebuffer = function (texture) {
  288. this._currentRenderTarget = null;
  289. if (texture.generateMipMaps) {
  290. var gl = this._gl;
  291. gl.bindTexture(gl.TEXTURE_2D, texture);
  292. gl.generateMipmap(gl.TEXTURE_2D);
  293. gl.bindTexture(gl.TEXTURE_2D, null);
  294. }
  295. this._gl.bindFramebuffer(this._gl.FRAMEBUFFER, null);
  296. };
  297. Engine.prototype.flushFramebuffer = function () {
  298. this._gl.flush();
  299. };
  300. Engine.prototype.restoreDefaultFramebuffer = function () {
  301. this._gl.bindFramebuffer(this._gl.FRAMEBUFFER, null);
  302. this.setViewport(this._cachedViewport);
  303. this.wipeCaches();
  304. };
  305. // VBOs
  306. Engine.prototype.createVertexBuffer = function (vertices) {
  307. var vbo = this._gl.createBuffer();
  308. this._gl.bindBuffer(this._gl.ARRAY_BUFFER, vbo);
  309. this._gl.bufferData(this._gl.ARRAY_BUFFER, new Float32Array(vertices), this._gl.STATIC_DRAW);
  310. this._gl.bindBuffer(this._gl.ARRAY_BUFFER, null);
  311. vbo.references = 1;
  312. return vbo;
  313. };
  314. Engine.prototype.createDynamicVertexBuffer = function (capacity) {
  315. var vbo = this._gl.createBuffer();
  316. this._gl.bindBuffer(this._gl.ARRAY_BUFFER, vbo);
  317. this._gl.bufferData(this._gl.ARRAY_BUFFER, capacity, this._gl.DYNAMIC_DRAW);
  318. this._gl.bindBuffer(this._gl.ARRAY_BUFFER, null);
  319. vbo.references = 1;
  320. return vbo;
  321. };
  322. Engine.prototype.updateDynamicVertexBuffer = function (vertexBuffer, vertices, length) {
  323. this._gl.bindBuffer(this._gl.ARRAY_BUFFER, vertexBuffer);
  324. if (length && length != vertices.length) {
  325. this._gl.bufferSubData(this._gl.ARRAY_BUFFER, 0, new Float32Array(vertices, 0, length));
  326. } else {
  327. if (vertices instanceof Float32Array) {
  328. this._gl.bufferSubData(this._gl.ARRAY_BUFFER, 0, vertices);
  329. } else {
  330. this._gl.bufferSubData(this._gl.ARRAY_BUFFER, 0, new Float32Array(vertices));
  331. }
  332. }
  333. this._gl.bindBuffer(this._gl.ARRAY_BUFFER, null);
  334. };
  335. Engine.prototype.createIndexBuffer = function (indices) {
  336. var vbo = this._gl.createBuffer();
  337. this._gl.bindBuffer(this._gl.ELEMENT_ARRAY_BUFFER, vbo);
  338. this._gl.bufferData(this._gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(indices), this._gl.STATIC_DRAW);
  339. this._gl.bindBuffer(this._gl.ELEMENT_ARRAY_BUFFER, null);
  340. vbo.references = 1;
  341. return vbo;
  342. };
  343. Engine.prototype.bindBuffers = function (vertexBuffer, indexBuffer, vertexDeclaration, vertexStrideSize, effect) {
  344. if (this._cachedVertexBuffers !== vertexBuffer || this._cachedEffectForVertexBuffers !== effect) {
  345. this._cachedVertexBuffers = vertexBuffer;
  346. this._cachedEffectForVertexBuffers = effect;
  347. this._gl.bindBuffer(this._gl.ARRAY_BUFFER, vertexBuffer);
  348. var offset = 0;
  349. for (var index = 0; index < vertexDeclaration.length; index++) {
  350. var order = effect.getAttribute(index);
  351. if (order >= 0) {
  352. this._gl.vertexAttribPointer(order, vertexDeclaration[index], this._gl.FLOAT, false, vertexStrideSize, offset);
  353. }
  354. offset += vertexDeclaration[index] * 4;
  355. }
  356. }
  357. if (this._cachedIndexBuffer !== indexBuffer) {
  358. this._cachedIndexBuffer = indexBuffer;
  359. this._gl.bindBuffer(this._gl.ELEMENT_ARRAY_BUFFER, indexBuffer);
  360. }
  361. };
  362. Engine.prototype.bindMultiBuffers = function (vertexBuffers, indexBuffer, effect) {
  363. if (this._cachedVertexBuffers !== vertexBuffers || this._cachedEffectForVertexBuffers !== effect) {
  364. this._cachedVertexBuffers = vertexBuffers;
  365. this._cachedEffectForVertexBuffers = effect;
  366. var attributes = effect.getAttributesNames();
  367. for (var index = 0; index < attributes.length; index++) {
  368. var order = effect.getAttribute(index);
  369. if (order >= 0) {
  370. var vertexBuffer = vertexBuffers[attributes[index]];
  371. var stride = vertexBuffer.getStrideSize();
  372. this._gl.bindBuffer(this._gl.ARRAY_BUFFER, vertexBuffer.getBuffer());
  373. this._gl.vertexAttribPointer(order, stride, this._gl.FLOAT, false, stride * 4, 0);
  374. }
  375. }
  376. }
  377. if (this._cachedIndexBuffer !== indexBuffer) {
  378. this._cachedIndexBuffer = indexBuffer;
  379. this._gl.bindBuffer(this._gl.ELEMENT_ARRAY_BUFFER, indexBuffer);
  380. }
  381. };
  382. Engine.prototype._releaseBuffer = function (buffer) {
  383. buffer.references--;
  384. if (buffer.references === 0) {
  385. this._gl.deleteBuffer(buffer);
  386. }
  387. };
  388. Engine.prototype.draw = function (useTriangles, indexStart, indexCount) {
  389. this._gl.drawElements(useTriangles ? this._gl.TRIANGLES : this._gl.LINES, indexCount, this._gl.UNSIGNED_SHORT, indexStart * 2);
  390. this._gl.getError();
  391. };
  392. // Shaders
  393. Engine.prototype._releaseEffect = function (effect) {
  394. if (this._compiledEffects[effect._key]) {
  395. delete this._compiledEffects[effect._key];
  396. if (effect.getProgram()) {
  397. this._gl.deleteProgram(effect.getProgram());
  398. }
  399. }
  400. };
  401. Engine.prototype.createEffect = function (baseName, attributesNames, uniformsNames, samplers, defines, optionalDefines, onCompiled, onError) {
  402. var vertex = baseName.vertexElement || baseName.vertex || baseName;
  403. var fragment = baseName.fragmentElement || baseName.fragment || baseName;
  404. var name = vertex + "+" + fragment + "@" + defines;
  405. if (this._compiledEffects[name]) {
  406. return this._compiledEffects[name];
  407. }
  408. var effect = new BABYLON.Effect(baseName, attributesNames, uniformsNames, samplers, this, defines, optionalDefines, onCompiled, onError);
  409. effect._key = name;
  410. this._compiledEffects[name] = effect;
  411. return effect;
  412. };
  413. Engine.prototype.createShaderProgram = function (vertexCode, fragmentCode, defines) {
  414. var vertexShader = compileShader(this._gl, vertexCode, "vertex", defines);
  415. var fragmentShader = compileShader(this._gl, fragmentCode, "fragment", defines);
  416. var shaderProgram = this._gl.createProgram();
  417. this._gl.attachShader(shaderProgram, vertexShader);
  418. this._gl.attachShader(shaderProgram, fragmentShader);
  419. var linked = this._gl.linkProgram(shaderProgram);
  420. if (!linked) {
  421. var error = this._gl.getProgramInfoLog(shaderProgram);
  422. if (error) {
  423. throw new Error(error);
  424. }
  425. }
  426. this._gl.deleteShader(vertexShader);
  427. this._gl.deleteShader(fragmentShader);
  428. return shaderProgram;
  429. };
  430. Engine.prototype.getUniforms = function (shaderProgram, uniformsNames) {
  431. var results = [];
  432. for (var index = 0; index < uniformsNames.length; index++) {
  433. results.push(this._gl.getUniformLocation(shaderProgram, uniformsNames[index]));
  434. }
  435. return results;
  436. };
  437. Engine.prototype.getAttributes = function (shaderProgram, attributesNames) {
  438. var results = [];
  439. for (var index = 0; index < attributesNames.length; index++) {
  440. try {
  441. results.push(this._gl.getAttribLocation(shaderProgram, attributesNames[index]));
  442. } catch (e) {
  443. results.push(-1);
  444. }
  445. }
  446. return results;
  447. };
  448. Engine.prototype.enableEffect = function (effect) {
  449. if (!effect || !effect.getAttributesCount() || this._currentEffect === effect) {
  450. return;
  451. }
  452. // Use program
  453. this._gl.useProgram(effect.getProgram());
  454. var currentCount = effect.getAttributesCount();
  455. var maxIndex = 0;
  456. for (var index = 0; index < currentCount; index++) {
  457. // Attributes
  458. var order = effect.getAttribute(index);
  459. if (order >= 0) {
  460. this._gl.enableVertexAttribArray(effect.getAttribute(index));
  461. maxIndex = Math.max(maxIndex, order);
  462. }
  463. }
  464. for (index = maxIndex + 1; index <= this._lastVertexAttribIndex; index++) {
  465. this._gl.disableVertexAttribArray(index);
  466. }
  467. this._lastVertexAttribIndex = maxIndex;
  468. this._currentEffect = effect;
  469. };
  470. Engine.prototype.setArray = function (uniform, array) {
  471. if (!uniform)
  472. return;
  473. this._gl.uniform1fv(uniform, array);
  474. };
  475. Engine.prototype.setMatrices = function (uniform, matrices) {
  476. if (!uniform)
  477. return;
  478. this._gl.uniformMatrix4fv(uniform, false, matrices);
  479. };
  480. Engine.prototype.setMatrix = function (uniform, matrix) {
  481. if (!uniform)
  482. return;
  483. this._gl.uniformMatrix4fv(uniform, false, matrix.toArray());
  484. };
  485. Engine.prototype.setFloat = function (uniform, value) {
  486. if (!uniform)
  487. return;
  488. this._gl.uniform1f(uniform, value);
  489. };
  490. Engine.prototype.setFloat2 = function (uniform, x, y) {
  491. if (!uniform)
  492. return;
  493. this._gl.uniform2f(uniform, x, y);
  494. };
  495. Engine.prototype.setFloat3 = function (uniform, x, y, z) {
  496. if (!uniform)
  497. return;
  498. this._gl.uniform3f(uniform, x, y, z);
  499. };
  500. Engine.prototype.setBool = function (uniform, bool) {
  501. if (!uniform)
  502. return;
  503. this._gl.uniform1i(uniform, bool);
  504. };
  505. Engine.prototype.setFloat4 = function (uniform, x, y, z, w) {
  506. if (!uniform)
  507. return;
  508. this._gl.uniform4f(uniform, x, y, z, w);
  509. };
  510. Engine.prototype.setColor3 = function (uniform, color3) {
  511. if (!uniform)
  512. return;
  513. this._gl.uniform3f(uniform, color3.r, color3.g, color3.b);
  514. };
  515. Engine.prototype.setColor4 = function (uniform, color3, alpha) {
  516. if (!uniform)
  517. return;
  518. this._gl.uniform4f(uniform, color3.r, color3.g, color3.b, alpha);
  519. };
  520. // States
  521. Engine.prototype.setState = function (culling) {
  522. // Culling
  523. if (this._cullingState !== culling) {
  524. if (culling) {
  525. this._gl.cullFace(this.cullBackFaces ? this._gl.BACK : this._gl.FRONT);
  526. this._gl.enable(this._gl.CULL_FACE);
  527. } else {
  528. this._gl.disable(this._gl.CULL_FACE);
  529. }
  530. this._cullingState = culling;
  531. }
  532. };
  533. Engine.prototype.setDepthBuffer = function (enable) {
  534. if (enable) {
  535. this._gl.enable(this._gl.DEPTH_TEST);
  536. } else {
  537. this._gl.disable(this._gl.DEPTH_TEST);
  538. }
  539. };
  540. Engine.prototype.setDepthWrite = function (enable) {
  541. this._gl.depthMask(enable);
  542. this._depthMask = enable;
  543. };
  544. Engine.prototype.setColorWrite = function (enable) {
  545. this._gl.colorMask(enable, enable, enable, enable);
  546. };
  547. Engine.prototype.setAlphaMode = function (mode) {
  548. switch (mode) {
  549. case BABYLON.Engine.ALPHA_DISABLE:
  550. this.setDepthWrite(true);
  551. this._gl.disable(this._gl.BLEND);
  552. break;
  553. case BABYLON.Engine.ALPHA_COMBINE:
  554. this.setDepthWrite(false);
  555. this._gl.blendFuncSeparate(this._gl.SRC_ALPHA, this._gl.ONE_MINUS_SRC_ALPHA, this._gl.ONE, this._gl.ONE);
  556. this._gl.enable(this._gl.BLEND);
  557. break;
  558. case BABYLON.Engine.ALPHA_ADD:
  559. this.setDepthWrite(false);
  560. this._gl.blendFuncSeparate(this._gl.ONE, this._gl.ONE, this._gl.ZERO, this._gl.ONE);
  561. this._gl.enable(this._gl.BLEND);
  562. break;
  563. }
  564. };
  565. Engine.prototype.setAlphaTesting = function (enable) {
  566. this._alphaTest = enable;
  567. };
  568. Engine.prototype.getAlphaTesting = function () {
  569. return this._alphaTest;
  570. };
  571. // Textures
  572. Engine.prototype.wipeCaches = function () {
  573. this._activeTexturesCache = [];
  574. this._currentEffect = null;
  575. this._cullingState = null;
  576. this._cachedVertexBuffers = null;
  577. this._cachedIndexBuffer = null;
  578. this._cachedEffectForVertexBuffers = null;
  579. };
  580. Engine.prototype.createTexture = function (url, noMipmap, invertY, scene) {
  581. var _this = this;
  582. var texture = this._gl.createTexture();
  583. var isDDS = this.getCaps().s3tc && (url.substr(url.length - 4, 4).toLowerCase() === ".dds");
  584. scene._addPendingData(texture);
  585. texture.url = url;
  586. texture.noMipmap = noMipmap;
  587. texture.references = 1;
  588. this._loadedTexturesCache.push(texture);
  589. if (isDDS) {
  590. BABYLON.Tools.LoadFile(url, function (data) {
  591. var info = BABYLON.Internals.DDSTools.GetDDSInfo(data);
  592. var loadMipmap = info.mipmapCount > 1 && !noMipmap;
  593. prepareWebGLTexture(texture, _this._gl, scene, info.width, info.height, invertY, !loadMipmap, function () {
  594. BABYLON.Internals.DDSTools.UploadDDSLevels(_this._gl, _this.getCaps().s3tc, data, loadMipmap);
  595. });
  596. }, null, scene.database, true);
  597. } else {
  598. var onload = function (img) {
  599. prepareWebGLTexture(texture, _this._gl, scene, img.width, img.height, invertY, noMipmap, function (potWidth, potHeight) {
  600. var isPot = (img.width == potWidth && img.height == potHeight);
  601. if (!isPot) {
  602. _this._workingCanvas.width = potWidth;
  603. _this._workingCanvas.height = potHeight;
  604. _this._workingContext.drawImage(img, 0, 0, img.width, img.height, 0, 0, potWidth, potHeight);
  605. }
  606. _this._gl.texImage2D(_this._gl.TEXTURE_2D, 0, _this._gl.RGBA, _this._gl.RGBA, _this._gl.UNSIGNED_BYTE, isPot ? img : _this._workingCanvas);
  607. });
  608. };
  609. var onerror = function () {
  610. scene._removePendingData(texture);
  611. };
  612. BABYLON.Tools.LoadImage(url, onload, onerror, scene.database);
  613. }
  614. return texture;
  615. };
  616. Engine.prototype.createDynamicTexture = function (width, height, generateMipMaps) {
  617. var texture = this._gl.createTexture();
  618. width = getExponantOfTwo(width, this._caps.maxTextureSize);
  619. height = getExponantOfTwo(height, this._caps.maxTextureSize);
  620. this._gl.bindTexture(this._gl.TEXTURE_2D, texture);
  621. this._gl.texParameteri(this._gl.TEXTURE_2D, this._gl.TEXTURE_MAG_FILTER, this._gl.LINEAR);
  622. if (!generateMipMaps) {
  623. this._gl.texParameteri(this._gl.TEXTURE_2D, this._gl.TEXTURE_MIN_FILTER, this._gl.LINEAR);
  624. } else {
  625. this._gl.texParameteri(this._gl.TEXTURE_2D, this._gl.TEXTURE_MIN_FILTER, this._gl.LINEAR_MIPMAP_LINEAR);
  626. }
  627. this._gl.bindTexture(this._gl.TEXTURE_2D, null);
  628. this._activeTexturesCache = [];
  629. texture._baseWidth = width;
  630. texture._baseHeight = height;
  631. texture._width = width;
  632. texture._height = height;
  633. texture.isReady = false;
  634. texture.generateMipMaps = generateMipMaps;
  635. texture.references = 1;
  636. this._loadedTexturesCache.push(texture);
  637. return texture;
  638. };
  639. Engine.prototype.updateDynamicTexture = function (texture, canvas, invertY) {
  640. this._gl.bindTexture(this._gl.TEXTURE_2D, texture);
  641. this._gl.pixelStorei(this._gl.UNPACK_FLIP_Y_WEBGL, invertY ? 1 : 0);
  642. this._gl.texImage2D(this._gl.TEXTURE_2D, 0, this._gl.RGBA, this._gl.RGBA, this._gl.UNSIGNED_BYTE, canvas);
  643. if (texture.generateMipMaps) {
  644. this._gl.generateMipmap(this._gl.TEXTURE_2D);
  645. }
  646. this._gl.bindTexture(this._gl.TEXTURE_2D, null);
  647. this._activeTexturesCache = [];
  648. texture.isReady = true;
  649. };
  650. Engine.prototype.updateVideoTexture = function (texture, video, invertY) {
  651. this._gl.bindTexture(this._gl.TEXTURE_2D, texture);
  652. this._gl.pixelStorei(this._gl.UNPACK_FLIP_Y_WEBGL, invertY ? 0 : 1); // Video are upside down by default
  653. // Scale the video if it is a NPOT using the current working canvas
  654. if (video.videoWidth !== texture._width || video.videoHeight !== texture._height) {
  655. if (!texture._workingCanvas) {
  656. texture._workingCanvas = document.createElement("canvas");
  657. texture._workingContext = texture._workingCanvas.getContext("2d");
  658. texture._workingCanvas.width = texture._width;
  659. texture._workingCanvas.height = texture._height;
  660. }
  661. texture._workingContext.drawImage(video, 0, 0, video.videoWidth, video.videoHeight, 0, 0, texture._width, texture._height);
  662. this._gl.texImage2D(this._gl.TEXTURE_2D, 0, this._gl.RGBA, this._gl.RGBA, this._gl.UNSIGNED_BYTE, texture._workingCanvas);
  663. } else {
  664. this._gl.texImage2D(this._gl.TEXTURE_2D, 0, this._gl.RGBA, this._gl.RGBA, this._gl.UNSIGNED_BYTE, video);
  665. }
  666. if (texture.generateMipMaps) {
  667. this._gl.generateMipmap(this._gl.TEXTURE_2D);
  668. }
  669. this._gl.bindTexture(this._gl.TEXTURE_2D, null);
  670. this._activeTexturesCache = [];
  671. texture.isReady = true;
  672. };
  673. Engine.prototype.createRenderTargetTexture = function (size, options) {
  674. // old version had a "generateMipMaps" arg instead of options.
  675. // if options.generateMipMaps is undefined, consider that options itself if the generateMipmaps value
  676. // in the same way, generateDepthBuffer is defaulted to true
  677. var generateMipMaps = false;
  678. var generateDepthBuffer = true;
  679. var samplingMode = BABYLON.Texture.TRILINEAR_SAMPLINGMODE;
  680. if (options !== undefined) {
  681. generateMipMaps = options.generateMipMaps === undefined ? options : options.generateMipmaps;
  682. generateDepthBuffer = options.generateDepthBuffer === undefined ? true : options.generateDepthBuffer;
  683. if (options.samplingMode !== undefined) {
  684. samplingMode = options.samplingMode;
  685. }
  686. }
  687. var gl = this._gl;
  688. var texture = gl.createTexture();
  689. gl.bindTexture(gl.TEXTURE_2D, texture);
  690. var width = size.width || size;
  691. var height = size.height || size;
  692. var magFilter = gl.NEAREST;
  693. var minFilter = gl.NEAREST;
  694. if (samplingMode === BABYLON.Texture.BILINEAR_SAMPLINGMODE) {
  695. magFilter = gl.LINEAR;
  696. if (generateMipMaps) {
  697. minFilter = gl.LINEAR_MIPMAP_NEAREST;
  698. } else {
  699. minFilter = gl.LINEAR;
  700. }
  701. } else if (samplingMode === BABYLON.Texture.TRILINEAR_SAMPLINGMODE) {
  702. magFilter = gl.LINEAR;
  703. if (generateMipMaps) {
  704. minFilter = gl.LINEAR_MIPMAP_LINEAR;
  705. } else {
  706. minFilter = gl.LINEAR;
  707. }
  708. }
  709. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, magFilter);
  710. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, minFilter);
  711. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
  712. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
  713. gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, width, height, 0, gl.RGBA, gl.UNSIGNED_BYTE, null);
  714. var depthBuffer;
  715. // Create the depth buffer
  716. if (generateDepthBuffer) {
  717. depthBuffer = gl.createRenderbuffer();
  718. gl.bindRenderbuffer(gl.RENDERBUFFER, depthBuffer);
  719. gl.renderbufferStorage(gl.RENDERBUFFER, gl.DEPTH_COMPONENT16, width, height);
  720. }
  721. // Create the framebuffer
  722. var framebuffer = gl.createFramebuffer();
  723. gl.bindFramebuffer(gl.FRAMEBUFFER, framebuffer);
  724. gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, texture, 0);
  725. if (generateDepthBuffer) {
  726. gl.framebufferRenderbuffer(gl.FRAMEBUFFER, gl.DEPTH_ATTACHMENT, gl.RENDERBUFFER, depthBuffer);
  727. }
  728. // Unbind
  729. gl.bindTexture(gl.TEXTURE_2D, null);
  730. gl.bindRenderbuffer(gl.RENDERBUFFER, null);
  731. gl.bindFramebuffer(gl.FRAMEBUFFER, null);
  732. texture._framebuffer = framebuffer;
  733. if (generateDepthBuffer) {
  734. texture._depthBuffer = depthBuffer;
  735. }
  736. texture._width = width;
  737. texture._height = height;
  738. texture.isReady = true;
  739. texture.generateMipMaps = generateMipMaps;
  740. texture.references = 1;
  741. this._activeTexturesCache = [];
  742. this._loadedTexturesCache.push(texture);
  743. return texture;
  744. };
  745. Engine.prototype.createCubeTexture = function (rootUrl, scene, extensions, noMipmap) {
  746. var _this = this;
  747. var gl = this._gl;
  748. var texture = gl.createTexture();
  749. texture.isCube = true;
  750. texture.url = rootUrl;
  751. texture.references = 1;
  752. this._loadedTexturesCache.push(texture);
  753. cascadeLoad(rootUrl, 0, [], scene, function (imgs) {
  754. var width = getExponantOfTwo(imgs[0].width, _this._caps.maxCubemapTextureSize);
  755. var height = width;
  756. _this._workingCanvas.width = width;
  757. _this._workingCanvas.height = height;
  758. var faces = [
  759. gl.TEXTURE_CUBE_MAP_POSITIVE_X, gl.TEXTURE_CUBE_MAP_POSITIVE_Y, gl.TEXTURE_CUBE_MAP_POSITIVE_Z,
  760. gl.TEXTURE_CUBE_MAP_NEGATIVE_X, gl.TEXTURE_CUBE_MAP_NEGATIVE_Y, gl.TEXTURE_CUBE_MAP_NEGATIVE_Z
  761. ];
  762. gl.bindTexture(gl.TEXTURE_CUBE_MAP, texture);
  763. gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, 0);
  764. for (var index = 0; index < faces.length; index++) {
  765. _this._workingContext.drawImage(imgs[index], 0, 0, imgs[index].width, imgs[index].height, 0, 0, width, height);
  766. gl.texImage2D(faces[index], 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, _this._workingCanvas);
  767. }
  768. if (!noMipmap) {
  769. gl.generateMipmap(gl.TEXTURE_CUBE_MAP);
  770. }
  771. gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
  772. gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_MIN_FILTER, noMipmap ? gl.LINEAR : gl.LINEAR_MIPMAP_LINEAR);
  773. gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
  774. gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
  775. gl.bindTexture(gl.TEXTURE_CUBE_MAP, null);
  776. _this._activeTexturesCache = [];
  777. texture._width = width;
  778. texture._height = height;
  779. texture.isReady = true;
  780. }, extensions);
  781. return texture;
  782. };
  783. Engine.prototype._releaseTexture = function (texture) {
  784. var gl = this._gl;
  785. if (texture._framebuffer) {
  786. gl.deleteFramebuffer(texture._framebuffer);
  787. }
  788. if (texture._depthBuffer) {
  789. gl.deleteRenderbuffer(texture._depthBuffer);
  790. }
  791. gl.deleteTexture(texture);
  792. for (var channel = 0; channel < this._caps.maxTexturesImageUnits; channel++) {
  793. this._gl.activeTexture(this._gl["TEXTURE" + channel]);
  794. this._gl.bindTexture(this._gl.TEXTURE_2D, null);
  795. this._gl.bindTexture(this._gl.TEXTURE_CUBE_MAP, null);
  796. this._activeTexturesCache[channel] = null;
  797. }
  798. var index = this._loadedTexturesCache.indexOf(texture);
  799. if (index !== -1) {
  800. this._loadedTexturesCache.splice(index, 1);
  801. }
  802. };
  803. Engine.prototype.bindSamplers = function (effect) {
  804. this._gl.useProgram(effect.getProgram());
  805. var samplers = effect.getSamplers();
  806. for (var index = 0; index < samplers.length; index++) {
  807. var uniform = effect.getUniform(samplers[index]);
  808. this._gl.uniform1i(uniform, index);
  809. }
  810. this._currentEffect = null;
  811. };
  812. Engine.prototype._bindTexture = function (channel, texture) {
  813. this._gl.activeTexture(this._gl["TEXTURE" + channel]);
  814. this._gl.bindTexture(this._gl.TEXTURE_2D, texture);
  815. this._activeTexturesCache[channel] = null;
  816. };
  817. Engine.prototype.setTextureFromPostProcess = function (channel, postProcess) {
  818. this._bindTexture(channel, postProcess._textures.data[postProcess._currentRenderTextureInd]);
  819. };
  820. Engine.prototype.setTexture = function (channel, texture) {
  821. if (channel < 0) {
  822. return;
  823. }
  824. // Not ready?
  825. if (!texture || !texture.isReady()) {
  826. if (this._activeTexturesCache[channel] != null) {
  827. this._gl.activeTexture(this._gl["TEXTURE" + channel]);
  828. this._gl.bindTexture(this._gl.TEXTURE_2D, null);
  829. this._gl.bindTexture(this._gl.TEXTURE_CUBE_MAP, null);
  830. this._activeTexturesCache[channel] = null;
  831. }
  832. return;
  833. }
  834. // Video
  835. if (texture instanceof BABYLON.VideoTexture) {
  836. if (texture.update()) {
  837. this._activeTexturesCache[channel] = null;
  838. }
  839. } else if (texture.delayLoadState == BABYLON.Engine.DELAYLOADSTATE_NOTLOADED) {
  840. texture.delayLoad();
  841. return;
  842. }
  843. if (this._activeTexturesCache[channel] == texture) {
  844. return;
  845. }
  846. this._activeTexturesCache[channel] = texture;
  847. var internalTexture = texture.getInternalTexture();
  848. this._gl.activeTexture(this._gl["TEXTURE" + channel]);
  849. if (internalTexture.isCube) {
  850. this._gl.bindTexture(this._gl.TEXTURE_CUBE_MAP, internalTexture);
  851. if (internalTexture._cachedCoordinatesMode !== texture.coordinatesMode) {
  852. internalTexture._cachedCoordinatesMode = texture.coordinatesMode;
  853. // CUBIC_MODE and SKYBOX_MODE both require CLAMP_TO_EDGE. All other modes use REPEAT.
  854. var textureWrapMode = (texture.coordinatesMode !== BABYLON.Texture.CUBIC_MODE && texture.coordinatesMode !== BABYLON.Texture.SKYBOX_MODE) ? this._gl.REPEAT : this._gl.CLAMP_TO_EDGE;
  855. this._gl.texParameteri(this._gl.TEXTURE_CUBE_MAP, this._gl.TEXTURE_WRAP_S, textureWrapMode);
  856. this._gl.texParameteri(this._gl.TEXTURE_CUBE_MAP, this._gl.TEXTURE_WRAP_T, textureWrapMode);
  857. }
  858. this._setAnisotropicLevel(this._gl.TEXTURE_CUBE_MAP, texture);
  859. } else {
  860. this._gl.bindTexture(this._gl.TEXTURE_2D, internalTexture);
  861. if (internalTexture._cachedWrapU !== texture.wrapU) {
  862. internalTexture._cachedWrapU = texture.wrapU;
  863. switch (texture.wrapU) {
  864. case BABYLON.Texture.WRAP_ADDRESSMODE:
  865. this._gl.texParameteri(this._gl.TEXTURE_2D, this._gl.TEXTURE_WRAP_S, this._gl.REPEAT);
  866. break;
  867. case BABYLON.Texture.CLAMP_ADDRESSMODE:
  868. this._gl.texParameteri(this._gl.TEXTURE_2D, this._gl.TEXTURE_WRAP_S, this._gl.CLAMP_TO_EDGE);
  869. break;
  870. case BABYLON.Texture.MIRROR_ADDRESSMODE:
  871. this._gl.texParameteri(this._gl.TEXTURE_2D, this._gl.TEXTURE_WRAP_S, this._gl.MIRRORED_REPEAT);
  872. break;
  873. }
  874. }
  875. if (internalTexture._cachedWrapV !== texture.wrapV) {
  876. internalTexture._cachedWrapV = texture.wrapV;
  877. switch (texture.wrapV) {
  878. case BABYLON.Texture.WRAP_ADDRESSMODE:
  879. this._gl.texParameteri(this._gl.TEXTURE_2D, this._gl.TEXTURE_WRAP_T, this._gl.REPEAT);
  880. break;
  881. case BABYLON.Texture.CLAMP_ADDRESSMODE:
  882. this._gl.texParameteri(this._gl.TEXTURE_2D, this._gl.TEXTURE_WRAP_T, this._gl.CLAMP_TO_EDGE);
  883. break;
  884. case BABYLON.Texture.MIRROR_ADDRESSMODE:
  885. this._gl.texParameteri(this._gl.TEXTURE_2D, this._gl.TEXTURE_WRAP_T, this._gl.MIRRORED_REPEAT);
  886. break;
  887. }
  888. }
  889. this._setAnisotropicLevel(this._gl.TEXTURE_2D, texture);
  890. }
  891. };
  892. Engine.prototype._setAnisotropicLevel = function (key, texture) {
  893. var anisotropicFilterExtension = this._caps.textureAnisotropicFilterExtension;
  894. if (anisotropicFilterExtension && texture._cachedAnisotropicFilteringLevel !== texture.anisotropicFilteringLevel) {
  895. this._gl.texParameterf(key, anisotropicFilterExtension.TEXTURE_MAX_ANISOTROPY_EXT, Math.min(texture.anisotropicFilteringLevel, this._caps.maxAnisotropy));
  896. texture._cachedAnisotropicFilteringLevel = texture.anisotropicFilteringLevel;
  897. }
  898. };
  899. // Dispose
  900. Engine.prototype.dispose = function () {
  901. while (this.scenes.length) {
  902. this.scenes[0].dispose();
  903. }
  904. for (var name in this._compiledEffects) {
  905. this._gl.deleteProgram(this._compiledEffects[name]._program);
  906. }
  907. // Events
  908. window.removeEventListener("blur", this._onBlur);
  909. window.removeEventListener("focus", this._onFocus);
  910. document.removeEventListener("fullscreenchange", this._onFullscreenChange);
  911. document.removeEventListener("mozfullscreenchange", this._onFullscreenChange);
  912. document.removeEventListener("webkitfullscreenchange", this._onFullscreenChange);
  913. document.removeEventListener("msfullscreenchange", this._onFullscreenChange);
  914. document.removeEventListener("pointerlockchange", this._onPointerLockChange);
  915. document.removeEventListener("mspointerlockchange", this._onPointerLockChange);
  916. document.removeEventListener("mozpointerlockchange", this._onPointerLockChange);
  917. document.removeEventListener("webkitpointerlockchange", this._onPointerLockChange);
  918. };
  919. // Statics
  920. Engine.isSupported = function () {
  921. try {
  922. var tempcanvas = document.createElement("canvas");
  923. var gl = tempcanvas.getContext("webgl") || tempcanvas.getContext("experimental-webgl");
  924. return gl != null && !!window.WebGLRenderingContext;
  925. } catch (e) {
  926. return false;
  927. }
  928. };
  929. Engine.ShadersRepository = "Babylon/Shaders/";
  930. Engine.ALPHA_DISABLE = 0;
  931. Engine.ALPHA_ADD = 1;
  932. Engine.ALPHA_COMBINE = 2;
  933. Engine.DELAYLOADSTATE_NONE = 0;
  934. Engine.DELAYLOADSTATE_LOADED = 1;
  935. Engine.DELAYLOADSTATE_LOADING = 2;
  936. Engine.DELAYLOADSTATE_NOTLOADED = 4;
  937. Engine.Epsilon = 0.001;
  938. Engine.CollisionsEpsilon = 0.001;
  939. return Engine;
  940. })();
  941. BABYLON.Engine = Engine;
  942. })(BABYLON || (BABYLON = {}));
  943. //# sourceMappingURL=babylon.engine.js.map