babylon.engine.js 43 KB

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