babylon.engine.js 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144
  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. if (length && length != vertices.length) {
  247. this._gl.bufferSubData(this._gl.ARRAY_BUFFER, 0, new Float32Array(vertices, 0, length));
  248. } else {
  249. if (vertices instanceof Float32Array) {
  250. this._gl.bufferSubData(this._gl.ARRAY_BUFFER, 0, vertices);
  251. } else {
  252. this._gl.bufferSubData(this._gl.ARRAY_BUFFER, 0, new Float32Array(vertices));
  253. }
  254. }
  255. this._gl.bindBuffer(this._gl.ARRAY_BUFFER, null);
  256. };
  257. BABYLON.Engine.prototype.createIndexBuffer = function (indices) {
  258. var vbo = this._gl.createBuffer();
  259. this._gl.bindBuffer(this._gl.ELEMENT_ARRAY_BUFFER, vbo);
  260. this._gl.bufferData(this._gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(indices), this._gl.STATIC_DRAW);
  261. this._gl.bindBuffer(this._gl.ELEMENT_ARRAY_BUFFER, null);
  262. vbo.references = 1;
  263. return vbo;
  264. };
  265. BABYLON.Engine.prototype.bindBuffers = function (vertexBuffer, indexBuffer, vertexDeclaration, vertexStrideSize, effect) {
  266. if (this._cachedVertexBuffers !== vertexBuffer || this._cachedEffectForVertexBuffers !== effect) {
  267. this._cachedVertexBuffers = vertexBuffer;
  268. this._cachedEffectForVertexBuffers = effect;
  269. this._gl.bindBuffer(this._gl.ARRAY_BUFFER, vertexBuffer);
  270. var offset = 0;
  271. for (var index = 0; index < vertexDeclaration.length; index++) {
  272. var order = effect.getAttribute(index);
  273. if (order >= 0) {
  274. this._gl.vertexAttribPointer(order, vertexDeclaration[index], this._gl.FLOAT, false, vertexStrideSize, offset);
  275. }
  276. offset += vertexDeclaration[index] * 4;
  277. }
  278. }
  279. if (this._cachedIndexBuffer !== indexBuffer) {
  280. this._cachedIndexBuffer = indexBuffer;
  281. this._gl.bindBuffer(this._gl.ELEMENT_ARRAY_BUFFER, indexBuffer);
  282. }
  283. };
  284. BABYLON.Engine.prototype.bindMultiBuffers = function (vertexBuffers, indexBuffer, effect) {
  285. if (this._cachedVertexBuffers !== vertexBuffers || this._cachedEffectForVertexBuffers !== effect) {
  286. this._cachedVertexBuffers = vertexBuffers;
  287. this._cachedEffectForVertexBuffers = effect;
  288. var attributes = effect.getAttributesNames();
  289. for (var index = 0; index < attributes.length; index++) {
  290. var order = effect.getAttribute(index);
  291. if (order >= 0) {
  292. var vertexBuffer = vertexBuffers[attributes[index]];
  293. var stride = vertexBuffer.getStrideSize();
  294. this._gl.bindBuffer(this._gl.ARRAY_BUFFER, vertexBuffer._buffer);
  295. this._gl.vertexAttribPointer(order, stride, this._gl.FLOAT, false, stride * 4, 0);
  296. }
  297. }
  298. }
  299. if (this._cachedIndexBuffer !== indexBuffer) {
  300. this._cachedIndexBuffer = indexBuffer;
  301. this._gl.bindBuffer(this._gl.ELEMENT_ARRAY_BUFFER, indexBuffer);
  302. }
  303. };
  304. BABYLON.Engine.prototype._releaseBuffer = function (buffer) {
  305. buffer.references--;
  306. if (buffer.references === 0) {
  307. this._gl.deleteBuffer(buffer);
  308. }
  309. };
  310. BABYLON.Engine.prototype.draw = function (useTriangles, indexStart, indexCount) {
  311. this._gl.drawElements(useTriangles ? this._gl.TRIANGLES : this._gl.LINES, indexCount, this._gl.UNSIGNED_SHORT, indexStart * 2);
  312. };
  313. // Shaders
  314. BABYLON.Engine.prototype._releaseEffect = function (effect) {
  315. if (this._compiledEffects[effect._key]) {
  316. delete this._compiledEffects[effect._key];
  317. if (effect._program) {
  318. this._gl.deleteProgram(effect._program);
  319. }
  320. }
  321. };
  322. BABYLON.Engine.prototype.createEffect = function (baseName, attributesNames, uniformsNames, samplers, defines, optionalDefines, onCompiled, onError) {
  323. var vertex = baseName.vertexElement || baseName.vertex || baseName;
  324. var fragment = baseName.fragmentElement || baseName.fragment || baseName;
  325. var name = vertex + "+" + fragment + "@" + defines;
  326. if (this._compiledEffects[name]) {
  327. return this._compiledEffects[name];
  328. }
  329. var effect = new BABYLON.Effect(baseName, attributesNames, uniformsNames, samplers, this, defines, optionalDefines, onCompiled, onError);
  330. effect._key = name;
  331. this._compiledEffects[name] = effect;
  332. return effect;
  333. };
  334. var compileShader = function (gl, source, type, defines) {
  335. var shader = gl.createShader(type === "vertex" ? gl.VERTEX_SHADER : gl.FRAGMENT_SHADER);
  336. gl.shaderSource(shader, (defines ? defines + "\n" : "") + source);
  337. gl.compileShader(shader);
  338. if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
  339. throw new Error(gl.getShaderInfoLog(shader));
  340. }
  341. return shader;
  342. };
  343. BABYLON.Engine.prototype.createShaderProgram = function (vertexCode, fragmentCode, defines) {
  344. var vertexShader = compileShader(this._gl, vertexCode, "vertex", defines);
  345. var fragmentShader = compileShader(this._gl, fragmentCode, "fragment", defines);
  346. var shaderProgram = this._gl.createProgram();
  347. this._gl.attachShader(shaderProgram, vertexShader);
  348. this._gl.attachShader(shaderProgram, fragmentShader);
  349. this._gl.linkProgram(shaderProgram);
  350. var error = this._gl.getProgramInfoLog(shaderProgram);
  351. if (error) {
  352. throw new Error(error);
  353. }
  354. this._gl.deleteShader(vertexShader);
  355. this._gl.deleteShader(fragmentShader);
  356. return shaderProgram;
  357. };
  358. BABYLON.Engine.prototype.getUniforms = function (shaderProgram, uniformsNames) {
  359. var results = [];
  360. for (var index = 0; index < uniformsNames.length; index++) {
  361. results.push(this._gl.getUniformLocation(shaderProgram, uniformsNames[index]));
  362. }
  363. return results;
  364. };
  365. BABYLON.Engine.prototype.getAttributes = function (shaderProgram, attributesNames) {
  366. var results = [];
  367. for (var index = 0; index < attributesNames.length; index++) {
  368. try {
  369. results.push(this._gl.getAttribLocation(shaderProgram, attributesNames[index]));
  370. } catch (e) {
  371. results.push(-1);
  372. }
  373. }
  374. return results;
  375. };
  376. BABYLON.Engine.prototype.enableEffect = function (effect) {
  377. if (!effect || !effect.getAttributesCount() || this._currentEffect === effect) {
  378. return;
  379. }
  380. // Use program
  381. this._gl.useProgram(effect.getProgram());
  382. for (var index = 0; index < effect.getAttributesCount() ; index++) {
  383. // Attributes
  384. var order = effect.getAttribute(index);
  385. if (order >= 0) {
  386. this._gl.enableVertexAttribArray(effect.getAttribute(index));
  387. }
  388. }
  389. this._currentEffect = effect;
  390. };
  391. BABYLON.Engine.prototype.setArray = function (uniform, array) {
  392. if (!uniform)
  393. return;
  394. this._gl.uniform1fv(uniform, array);
  395. };
  396. BABYLON.Engine.prototype.setMatrices = function (uniform, matrices) {
  397. if (!uniform)
  398. return;
  399. this._gl.uniformMatrix4fv(uniform, false, matrices);
  400. };
  401. BABYLON.Engine.prototype.setMatrix = function (uniform, matrix) {
  402. if (!uniform)
  403. return;
  404. this._gl.uniformMatrix4fv(uniform, false, matrix.toArray());
  405. };
  406. BABYLON.Engine.prototype.setFloat = function (uniform, value) {
  407. if (!uniform)
  408. return;
  409. this._gl.uniform1f(uniform, value);
  410. };
  411. BABYLON.Engine.prototype.setFloat2 = function (uniform, x, y) {
  412. if (!uniform)
  413. return;
  414. this._gl.uniform2f(uniform, x, y);
  415. };
  416. BABYLON.Engine.prototype.setFloat3 = function (uniform, x, y, z) {
  417. if (!uniform)
  418. return;
  419. this._gl.uniform3f(uniform, x, y, z);
  420. };
  421. BABYLON.Engine.prototype.setBool = function (uniform, bool) {
  422. if (!uniform)
  423. return;
  424. this._gl.uniform1i(uniform, bool);
  425. };
  426. BABYLON.Engine.prototype.setFloat4 = function (uniform, x, y, z, w) {
  427. if (!uniform)
  428. return;
  429. this._gl.uniform4f(uniform, x, y, z, w);
  430. };
  431. BABYLON.Engine.prototype.setColor3 = function (uniform, color3) {
  432. if (!uniform)
  433. return;
  434. this._gl.uniform3f(uniform, color3.r, color3.g, color3.b);
  435. };
  436. BABYLON.Engine.prototype.setColor4 = function (uniform, color3, alpha) {
  437. if (!uniform)
  438. return;
  439. this._gl.uniform4f(uniform, color3.r, color3.g, color3.b, alpha);
  440. };
  441. // States
  442. BABYLON.Engine.prototype.setState = function (culling) {
  443. // Culling
  444. if (this._currentState.culling !== culling) {
  445. if (culling) {
  446. this._gl.cullFace(this.cullBackFaces ? this._gl.BACK : this._gl.FRONT);
  447. this._gl.enable(this._gl.CULL_FACE);
  448. } else {
  449. this._gl.disable(this._gl.CULL_FACE);
  450. }
  451. this._currentState.culling = culling;
  452. }
  453. };
  454. BABYLON.Engine.prototype.setDepthBuffer = function (enable) {
  455. if (enable) {
  456. this._gl.enable(this._gl.DEPTH_TEST);
  457. } else {
  458. this._gl.disable(this._gl.DEPTH_TEST);
  459. }
  460. };
  461. BABYLON.Engine.prototype.setDepthWrite = function (enable) {
  462. this._gl.depthMask(enable);
  463. };
  464. BABYLON.Engine.prototype.setColorWrite = function (enable) {
  465. this._gl.colorMask(enable, enable, enable, enable);
  466. };
  467. BABYLON.Engine.prototype.setAlphaMode = function (mode) {
  468. switch (mode) {
  469. case BABYLON.Engine.ALPHA_DISABLE:
  470. this.setDepthWrite(true);
  471. this._gl.disable(this._gl.BLEND);
  472. break;
  473. case BABYLON.Engine.ALPHA_COMBINE:
  474. this.setDepthWrite(false);
  475. this._gl.blendFuncSeparate(this._gl.SRC_ALPHA, this._gl.ONE_MINUS_SRC_ALPHA, this._gl.ONE, this._gl.ONE);
  476. this._gl.enable(this._gl.BLEND);
  477. break;
  478. case BABYLON.Engine.ALPHA_ADD:
  479. this.setDepthWrite(false);
  480. this._gl.blendFuncSeparate(this._gl.ONE, this._gl.ONE, this._gl.ZERO, this._gl.ONE);
  481. this._gl.enable(this._gl.BLEND);
  482. break;
  483. }
  484. };
  485. BABYLON.Engine.prototype.setAlphaTesting = function (enable) {
  486. this._alphaTest = enable;
  487. };
  488. BABYLON.Engine.prototype.getAlphaTesting = function () {
  489. return this._alphaTest;
  490. };
  491. // Textures
  492. BABYLON.Engine.prototype.wipeCaches = function () {
  493. this._activeTexturesCache = [];
  494. this._currentEffect = null;
  495. this._currentState = {
  496. culling: null
  497. };
  498. this._cachedVertexBuffers = null;
  499. this._cachedVertexBuffers = null;
  500. this._cachedEffectForVertexBuffers = null;
  501. };
  502. var getExponantOfTwo = function (value, max) {
  503. var count = 1;
  504. do {
  505. count *= 2;
  506. } while (count < value);
  507. if (count > max)
  508. count = max;
  509. return count;
  510. };
  511. var prepareWebGLTexture = function (texture, scene, width, height, invertY, noMipmap, processFunction) {
  512. var engine = scene.getEngine();
  513. var potWidth = getExponantOfTwo(width, engine._caps.maxTextureSize);
  514. var potHeight = getExponantOfTwo(height, engine._caps.maxTextureSize);
  515. engine._gl.bindTexture(engine._gl.TEXTURE_2D, texture);
  516. engine._gl.pixelStorei(engine._gl.UNPACK_FLIP_Y_WEBGL, invertY === undefined ? true : invertY);
  517. processFunction(potWidth, potHeight);
  518. engine._gl.texParameteri(engine._gl.TEXTURE_2D, engine._gl.TEXTURE_MAG_FILTER, engine._gl.LINEAR);
  519. if (noMipmap) {
  520. engine._gl.texParameteri(engine._gl.TEXTURE_2D, engine._gl.TEXTURE_MIN_FILTER, engine._gl.LINEAR);
  521. } else {
  522. engine._gl.texParameteri(engine._gl.TEXTURE_2D, engine._gl.TEXTURE_MIN_FILTER, engine._gl.LINEAR_MIPMAP_LINEAR);
  523. engine._gl.generateMipmap(engine._gl.TEXTURE_2D);
  524. }
  525. engine._gl.bindTexture(engine._gl.TEXTURE_2D, null);
  526. engine._activeTexturesCache = [];
  527. texture._baseWidth = width;
  528. texture._baseHeight = height;
  529. texture._width = potWidth;
  530. texture._height = potHeight;
  531. texture.isReady = true;
  532. scene._removePendingData(texture);
  533. };
  534. BABYLON.Engine.prototype.createTexture = function (url, noMipmap, invertY, scene) {
  535. var texture = this._gl.createTexture();
  536. var that = this;
  537. var isDDS = this.getCaps().s3tc && (url.substr(url.length - 4, 4).toLowerCase() === ".dds");
  538. scene._addPendingData(texture);
  539. texture.url = url;
  540. texture.noMipmap = noMipmap;
  541. texture.references = 1;
  542. this._loadedTexturesCache.push(texture);
  543. if (isDDS) {
  544. BABYLON.Tools.LoadFile(url, function (data) {
  545. var info = BABYLON.Tools.GetDDSInfo(data);
  546. var loadMipmap = info.mipmapCount > 1 && !noMipmap;
  547. prepareWebGLTexture(texture, scene, info.width, info.height, invertY, !loadMipmap, function (potWidth, potHeight) {
  548. BABYLON.Tools.UploadDDSLevels(that._gl, that.getCaps().s3tc, data, loadMipmap);
  549. });
  550. }, null, scene.database, true);
  551. } else {
  552. var onload = function(img) {
  553. prepareWebGLTexture(texture, scene, img.width, img.height, invertY, noMipmap, function (potWidth, potHeight) {
  554. var isPot = (img.width == potWidth && img.height == potHeight);
  555. if (!isPot) {
  556. that._workingCanvas.width = potWidth;
  557. that._workingCanvas.height = potHeight;
  558. that._workingContext.drawImage(img, 0, 0, img.width, img.height, 0, 0, potWidth, potHeight);
  559. }
  560. that._gl.texImage2D(that._gl.TEXTURE_2D, 0, that._gl.RGBA, that._gl.RGBA, that._gl.UNSIGNED_BYTE, isPot ? img : that._workingCanvas);
  561. });
  562. };
  563. var onerror = function() {
  564. scene._removePendingData(texture);
  565. };
  566. BABYLON.Tools.LoadImage(url, onload, onerror, scene.database);
  567. }
  568. return texture;
  569. };
  570. BABYLON.Engine.prototype.createDynamicTexture = function (width, height, generateMipMaps) {
  571. var texture = this._gl.createTexture();
  572. width = getExponantOfTwo(width, this._caps.maxTextureSize);
  573. height = getExponantOfTwo(height, this._caps.maxTextureSize);
  574. this._gl.bindTexture(this._gl.TEXTURE_2D, texture);
  575. this._gl.texParameteri(this._gl.TEXTURE_2D, this._gl.TEXTURE_MAG_FILTER, this._gl.LINEAR);
  576. if (!generateMipMaps) {
  577. this._gl.texParameteri(this._gl.TEXTURE_2D, this._gl.TEXTURE_MIN_FILTER, this._gl.LINEAR);
  578. } else {
  579. this._gl.texParameteri(this._gl.TEXTURE_2D, this._gl.TEXTURE_MIN_FILTER, this._gl.LINEAR_MIPMAP_LINEAR);
  580. }
  581. this._gl.bindTexture(this._gl.TEXTURE_2D, null);
  582. this._activeTexturesCache = [];
  583. texture._baseWidth = width;
  584. texture._baseHeight = height;
  585. texture._width = width;
  586. texture._height = height;
  587. texture.isReady = false;
  588. texture.generateMipMaps = generateMipMaps;
  589. texture.references = 1;
  590. this._loadedTexturesCache.push(texture);
  591. return texture;
  592. };
  593. BABYLON.Engine.prototype.updateDynamicTexture = function (texture, canvas, invertY) {
  594. this._gl.bindTexture(this._gl.TEXTURE_2D, texture);
  595. this._gl.pixelStorei(this._gl.UNPACK_FLIP_Y_WEBGL, invertY);
  596. this._gl.texImage2D(this._gl.TEXTURE_2D, 0, this._gl.RGBA, this._gl.RGBA, this._gl.UNSIGNED_BYTE, canvas);
  597. if (texture.generateMipMaps) {
  598. this._gl.generateMipmap(this._gl.TEXTURE_2D);
  599. }
  600. this._gl.bindTexture(this._gl.TEXTURE_2D, null);
  601. this._activeTexturesCache = [];
  602. texture.isReady = true;
  603. };
  604. BABYLON.Engine.prototype.updateVideoTexture = function (texture, video, invertY) {
  605. this._gl.bindTexture(this._gl.TEXTURE_2D, texture);
  606. this._gl.pixelStorei(this._gl.UNPACK_FLIP_Y_WEBGL, invertY ? false : true); // Video are upside down by default
  607. // Scale the video if it is a NPOT
  608. if (video.videoWidth !== texture._width || video.videoHeight !== texture._height) {
  609. if (!texture._workingCanvas) {
  610. texture._workingCanvas = document.createElement("canvas");
  611. texture._workingContext = texture._workingCanvas.getContext("2d");
  612. texture._workingCanvas.width = texture._width;
  613. texture._workingCanvas.height = texture._height;
  614. }
  615. texture._workingContext.drawImage(video, 0, 0, video.videoWidth, video.videoHeight, 0, 0, texture._width, texture._height);
  616. this._gl.texImage2D(this._gl.TEXTURE_2D, 0, this._gl.RGBA, this._gl.RGBA, this._gl.UNSIGNED_BYTE, texture._workingCanvas);
  617. } else {
  618. this._gl.texImage2D(this._gl.TEXTURE_2D, 0, this._gl.RGBA, this._gl.RGBA, this._gl.UNSIGNED_BYTE, video);
  619. }
  620. if (texture.generateMipMaps) {
  621. this._gl.generateMipmap(this._gl.TEXTURE_2D);
  622. }
  623. this._gl.bindTexture(this._gl.TEXTURE_2D, null);
  624. this._activeTexturesCache = [];
  625. texture.isReady = true;
  626. };
  627. BABYLON.Engine.prototype.createRenderTargetTexture = function (size, options) {
  628. // old version had a "generateMipMaps" arg instead of options.
  629. // if options.generateMipMaps is undefined, consider that options itself if the generateMipmaps value
  630. // in the same way, generateDepthBuffer is defaulted to true
  631. var generateMipMaps = false;
  632. var generateDepthBuffer = true;
  633. var samplingMode = BABYLON.Texture.TRILINEAR_SAMPLINGMODE;
  634. if (options !== undefined) {
  635. generateMipMaps = options.generateMipMaps === undefined ? options : options.generateMipmaps;
  636. generateDepthBuffer = options.generateDepthBuffer === undefined ? true : options.generateDepthBuffer;
  637. if (options.samplingMode !== undefined) {
  638. samplingMode = options.samplingMode;
  639. }
  640. }
  641. var gl = this._gl;
  642. var texture = gl.createTexture();
  643. gl.bindTexture(gl.TEXTURE_2D, texture);
  644. var width = size.width || size;
  645. var height = size.height || size;
  646. var magFilter = gl.NEAREST;
  647. var minFilter = gl.NEAREST;
  648. if (samplingMode === BABYLON.Texture.BILINEAR_SAMPLINGMODE) {
  649. magFilter = gl.LINEAR;
  650. if (generateMipMaps) {
  651. minFilter = gl.LINEAR_MIPMAP_NEAREST;
  652. } else {
  653. minFilter = gl.LINEAR;
  654. }
  655. } else if (samplingMode === BABYLON.Texture.TRILINEAR_SAMPLINGMODE) {
  656. magFilter = gl.LINEAR;
  657. if (generateMipMaps) {
  658. minFilter = gl.LINEAR_MIPMAP_LINEAR;
  659. } else {
  660. minFilter = gl.LINEAR;
  661. }
  662. }
  663. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, magFilter);
  664. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, minFilter);
  665. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
  666. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
  667. gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, width, height, 0, gl.RGBA, gl.UNSIGNED_BYTE, null);
  668. var depthBuffer;
  669. // Create the depth buffer
  670. if (generateDepthBuffer) {
  671. depthBuffer = gl.createRenderbuffer();
  672. gl.bindRenderbuffer(gl.RENDERBUFFER, depthBuffer);
  673. gl.renderbufferStorage(gl.RENDERBUFFER, gl.DEPTH_COMPONENT16, width, height);
  674. }
  675. // Create the framebuffer
  676. var framebuffer = gl.createFramebuffer();
  677. gl.bindFramebuffer(gl.FRAMEBUFFER, framebuffer);
  678. gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, texture, 0);
  679. if (generateDepthBuffer) {
  680. gl.framebufferRenderbuffer(gl.FRAMEBUFFER, gl.DEPTH_ATTACHMENT, gl.RENDERBUFFER, depthBuffer);
  681. }
  682. // Unbind
  683. gl.bindTexture(gl.TEXTURE_2D, null);
  684. gl.bindRenderbuffer(gl.RENDERBUFFER, null);
  685. gl.bindFramebuffer(gl.FRAMEBUFFER, null);
  686. texture._framebuffer = framebuffer;
  687. if (generateDepthBuffer) {
  688. texture._depthBuffer = depthBuffer;
  689. }
  690. texture._width = width;
  691. texture._height = height;
  692. texture.isReady = true;
  693. texture.generateMipMaps = generateMipMaps;
  694. texture.references = 1;
  695. this._activeTexturesCache = [];
  696. this._loadedTexturesCache.push(texture);
  697. return texture;
  698. };
  699. var cascadeLoad = function (rootUrl, index, loadedImages, scene, onfinish, extensions) {
  700. var img;
  701. var onload = function () {
  702. loadedImages.push(img);
  703. scene._removePendingData(img);
  704. if (index != extensions.length - 1) {
  705. cascadeLoad(rootUrl, index + 1, loadedImages, scene, onfinish, extensions);
  706. } else {
  707. onfinish(loadedImages);
  708. }
  709. };
  710. var onerror = function () {
  711. scene._removePendingData(img);
  712. };
  713. img = BABYLON.Tools.LoadImage(rootUrl + extensions[index], onload, onerror, scene.database);
  714. scene._addPendingData(img);
  715. };
  716. BABYLON.Engine.prototype.createCubeTexture = function (rootUrl, scene, extensions, noMipmap) {
  717. var gl = this._gl;
  718. var texture = gl.createTexture();
  719. texture.isCube = true;
  720. texture.url = rootUrl;
  721. texture.references = 1;
  722. this._loadedTexturesCache.push(texture);
  723. var that = this;
  724. cascadeLoad(rootUrl, 0, [], scene, function (imgs) {
  725. var width = getExponantOfTwo(imgs[0].width, that._caps.maxCubemapTextureSize);
  726. var height = width;
  727. that._workingCanvas.width = width;
  728. that._workingCanvas.height = height;
  729. var faces = [
  730. gl.TEXTURE_CUBE_MAP_POSITIVE_X, gl.TEXTURE_CUBE_MAP_POSITIVE_Y, gl.TEXTURE_CUBE_MAP_POSITIVE_Z,
  731. gl.TEXTURE_CUBE_MAP_NEGATIVE_X, gl.TEXTURE_CUBE_MAP_NEGATIVE_Y, gl.TEXTURE_CUBE_MAP_NEGATIVE_Z
  732. ];
  733. gl.bindTexture(gl.TEXTURE_CUBE_MAP, texture);
  734. gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, false);
  735. for (var index = 0; index < faces.length; index++) {
  736. that._workingContext.drawImage(imgs[index], 0, 0, imgs[index].width, imgs[index].height, 0, 0, width, height);
  737. gl.texImage2D(faces[index], 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, that._workingCanvas);
  738. }
  739. if (!noMipmap) {
  740. gl.generateMipmap(gl.TEXTURE_CUBE_MAP);
  741. }
  742. gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
  743. gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_MIN_FILTER, noMipmap ? gl.LINEAR : gl.LINEAR_MIPMAP_LINEAR);
  744. gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
  745. gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
  746. gl.bindTexture(gl.TEXTURE_CUBE_MAP, null);
  747. that._activeTexturesCache = [];
  748. texture._width = width;
  749. texture._height = height;
  750. texture.isReady = true;
  751. }, extensions);
  752. return texture;
  753. };
  754. BABYLON.Engine.prototype._releaseTexture = function (texture) {
  755. var gl = this._gl;
  756. if (texture._framebuffer) {
  757. gl.deleteFramebuffer(texture._framebuffer);
  758. }
  759. if (texture._depthBuffer) {
  760. gl.deleteRenderbuffer(texture._depthBuffer);
  761. }
  762. gl.deleteTexture(texture);
  763. // Unbind channels
  764. for (var channel = 0; channel < this._caps.maxTexturesImageUnits; channel++) {
  765. this._gl.activeTexture(this._gl["TEXTURE" + channel]);
  766. this._gl.bindTexture(this._gl.TEXTURE_2D, null);
  767. this._gl.bindTexture(this._gl.TEXTURE_CUBE_MAP, null);
  768. this._activeTexturesCache[channel] = null;
  769. }
  770. var index = this._loadedTexturesCache.indexOf(texture);
  771. if (index !== -1) {
  772. this._loadedTexturesCache.splice(index, 1);
  773. }
  774. };
  775. BABYLON.Engine.prototype.bindSamplers = function (effect) {
  776. this._gl.useProgram(effect.getProgram());
  777. var samplers = effect.getSamplers();
  778. for (var index = 0; index < samplers.length; index++) {
  779. var uniform = effect.getUniform(samplers[index]);
  780. this._gl.uniform1i(uniform, index);
  781. }
  782. this._currentEffect = null;
  783. };
  784. BABYLON.Engine.prototype._bindTexture = function (channel, texture) {
  785. this._gl.activeTexture(this._gl["TEXTURE" + channel]);
  786. this._gl.bindTexture(this._gl.TEXTURE_2D, texture);
  787. this._activeTexturesCache[channel] = null;
  788. };
  789. BABYLON.Engine.prototype.setTextureFromPostProcess = function (channel, postProcess) {
  790. this._bindTexture(channel, postProcess._textures.data[postProcess._currentRenderTextureInd]);
  791. };
  792. BABYLON.Engine.prototype.setTexture = function (channel, texture) {
  793. if (channel < 0) {
  794. return;
  795. }
  796. // Not ready?
  797. if (!texture || !texture.isReady()) {
  798. if (this._activeTexturesCache[channel] != null) {
  799. this._gl.activeTexture(this._gl["TEXTURE" + channel]);
  800. this._gl.bindTexture(this._gl.TEXTURE_2D, null);
  801. this._gl.bindTexture(this._gl.TEXTURE_CUBE_MAP, null);
  802. this._activeTexturesCache[channel] = null;
  803. }
  804. return;
  805. }
  806. // Video
  807. if (texture instanceof BABYLON.VideoTexture) {
  808. if (texture._update()) {
  809. this._activeTexturesCache[channel] = null;
  810. }
  811. } else if (texture.delayLoadState == BABYLON.Engine.DELAYLOADSTATE_NOTLOADED) { // Delay loading
  812. texture.delayLoad();
  813. return;
  814. }
  815. if (this._activeTexturesCache[channel] == texture) {
  816. return;
  817. }
  818. this._activeTexturesCache[channel] = texture;
  819. var internalTexture = texture.getInternalTexture();
  820. this._gl.activeTexture(this._gl["TEXTURE" + channel]);
  821. if (internalTexture.isCube) {
  822. this._gl.bindTexture(this._gl.TEXTURE_CUBE_MAP, internalTexture);
  823. if (internalTexture._cachedCoordinatesMode !== texture.coordinatesMode) {
  824. internalTexture._cachedCoordinatesMode = texture.coordinatesMode;
  825. 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);
  826. 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);
  827. }
  828. this._setAnisotropicLevel(this._gl.TEXTURE_CUBE_MAP, texture);
  829. } else {
  830. this._gl.bindTexture(this._gl.TEXTURE_2D, internalTexture);
  831. if (internalTexture._cachedWrapU !== texture.wrapU) {
  832. internalTexture._cachedWrapU = texture.wrapU;
  833. switch (texture.wrapU) {
  834. case BABYLON.Texture.WRAP_ADDRESSMODE:
  835. this._gl.texParameteri(this._gl.TEXTURE_2D, this._gl.TEXTURE_WRAP_S, this._gl.REPEAT);
  836. break;
  837. case BABYLON.Texture.CLAMP_ADDRESSMODE:
  838. this._gl.texParameteri(this._gl.TEXTURE_2D, this._gl.TEXTURE_WRAP_S, this._gl.CLAMP_TO_EDGE);
  839. break;
  840. case BABYLON.Texture.MIRROR_ADDRESSMODE:
  841. this._gl.texParameteri(this._gl.TEXTURE_2D, this._gl.TEXTURE_WRAP_S, this._gl.MIRRORED_REPEAT);
  842. break;
  843. }
  844. }
  845. if (internalTexture._cachedWrapV !== texture.wrapV) {
  846. internalTexture._cachedWrapV = texture.wrapV;
  847. switch (texture.wrapV) {
  848. case BABYLON.Texture.WRAP_ADDRESSMODE:
  849. this._gl.texParameteri(this._gl.TEXTURE_2D, this._gl.TEXTURE_WRAP_T, this._gl.REPEAT);
  850. break;
  851. case BABYLON.Texture.CLAMP_ADDRESSMODE:
  852. this._gl.texParameteri(this._gl.TEXTURE_2D, this._gl.TEXTURE_WRAP_T, this._gl.CLAMP_TO_EDGE);
  853. break;
  854. case BABYLON.Texture.MIRROR_ADDRESSMODE:
  855. this._gl.texParameteri(this._gl.TEXTURE_2D, this._gl.TEXTURE_WRAP_T, this._gl.MIRRORED_REPEAT);
  856. break;
  857. }
  858. }
  859. this._setAnisotropicLevel(this._gl.TEXTURE_2D, texture);
  860. }
  861. };
  862. BABYLON.Engine.prototype._setAnisotropicLevel = function (key, texture) {
  863. var anisotropicFilterExtension = this._caps.textureAnisotropicFilterExtension;
  864. if (anisotropicFilterExtension && texture._cachedAnisotropicFilteringLevel !== texture.anisotropicFilteringLevel) {
  865. this._gl.texParameterf(key, anisotropicFilterExtension.TEXTURE_MAX_ANISOTROPY_EXT, Math.min(texture.anisotropicFilteringLevel, this._caps.maxAnisotropy));
  866. texture._cachedAnisotropicFilteringLevel = texture.anisotropicFilteringLevel;
  867. }
  868. };
  869. // Dispose
  870. BABYLON.Engine.prototype.dispose = function () {
  871. // Release scenes
  872. while (this.scenes.length) {
  873. this.scenes[0].dispose();
  874. }
  875. // Release effects
  876. for (var name in this._compiledEffects.length) {
  877. this._gl.deleteProgram(this._compiledEffects[name]._program);
  878. }
  879. // Events
  880. window.removeEventListener("blur", this._onBlur);
  881. window.removeEventListener("focus", this._onFocus);
  882. document.removeEventListener("fullscreenchange", this._onFullscreenChange);
  883. document.removeEventListener("mozfullscreenchange", this._onFullscreenChange);
  884. document.removeEventListener("webkitfullscreenchange", this._onFullscreenChange);
  885. document.removeEventListener("msfullscreenchange", this._onFullscreenChange);
  886. document.removeEventListener("pointerlockchange", this._onPointerLockChange);
  887. document.removeEventListener("mspointerlockchange", this._onPointerLockChange);
  888. document.removeEventListener("mozpointerlockchange", this._onPointerLockChange);
  889. document.removeEventListener("webkitpointerlockchange", this._onPointerLockChange);
  890. };
  891. // Statics
  892. BABYLON.Engine.ShadersRepository = "Babylon/Shaders/";
  893. BABYLON.Engine.ALPHA_DISABLE = 0;
  894. BABYLON.Engine.ALPHA_ADD = 1;
  895. BABYLON.Engine.ALPHA_COMBINE = 2;
  896. // Statics
  897. BABYLON.Engine.DELAYLOADSTATE_NONE = 0;
  898. BABYLON.Engine.DELAYLOADSTATE_LOADED = 1;
  899. BABYLON.Engine.DELAYLOADSTATE_LOADING = 2;
  900. BABYLON.Engine.DELAYLOADSTATE_NOTLOADED = 4;
  901. BABYLON.Engine.epsilon = 0.001;
  902. BABYLON.Engine.collisionsEpsilon = 0.001;
  903. BABYLON.Engine.isSupported = function () {
  904. try {
  905. var tempcanvas = document.createElement("canvas");
  906. var gl = tempcanvas.getContext("webgl") || tempcanvas.getContext("experimental-webgl");
  907. return gl != null && !!window.WebGLRenderingContext;
  908. } catch (e) {
  909. return false;
  910. }
  911. };
  912. })();