mirror of
https://github.com/photonstorm/phaser
synced 2024-11-24 13:43:26 +00:00
Light Layer restore state on gl context lost
This commit is contained in:
parent
a4e33f111b
commit
de507de120
2 changed files with 59 additions and 12 deletions
|
@ -33,12 +33,10 @@ var LightLayer = new Class({
|
|||
|
||||
function LightLayer (scene)
|
||||
{
|
||||
var _this = this;
|
||||
|
||||
GameObject.call(this, scene, 'LightLayer');
|
||||
|
||||
var resourceManager = scene.sys.game.renderer.resourceManager;
|
||||
this._isDeferred = WebGLSupportedExtensions.has('WEBGL_draw_buffers');
|
||||
|
||||
this.renderer = scene.sys.game.renderer;
|
||||
this.passShader = null;
|
||||
this.gl = null;
|
||||
this.ambientLightColorR = 0.0;
|
||||
|
@ -48,12 +46,35 @@ var LightLayer = new Class({
|
|||
this.spritePool = [];
|
||||
this.lights = [];
|
||||
this.sprites = [];
|
||||
this.lightsLocations = [];
|
||||
this._z = 0;
|
||||
this.setOrigin(0, 0);
|
||||
|
||||
scene.sys.game.renderer.addContextRestoredCallback(function (renderer) {
|
||||
_this.onContextRestored(renderer);
|
||||
});
|
||||
|
||||
this.init(scene.sys.game.renderer, WebGLSupportedExtensions.has('WEBGL_draw_buffers'));
|
||||
},
|
||||
|
||||
onContextRestored: function (renderer)
|
||||
{
|
||||
/* It won't allow the use of drawBuffers on restored context */
|
||||
this.init(renderer, false);
|
||||
this.renderWebGL = require('./ForwardRenderer');
|
||||
this.lights.length = Math.min(this.lights.length, Const.MAX_LIGHTS);
|
||||
},
|
||||
|
||||
init: function (renderer, deferred)
|
||||
{
|
||||
var resourceManager = renderer.resourceManager;
|
||||
|
||||
this._isDeferred = deferred;
|
||||
this.renderer = renderer;
|
||||
this.lightsLocations = [];
|
||||
|
||||
if (resourceManager !== undefined && !this._isDeferred)
|
||||
{
|
||||
this.gl = scene.sys.game.renderer.gl;
|
||||
this.gl = renderer.gl;
|
||||
|
||||
this.passShader = resourceManager.createShader('Phong2DShaderForward', {
|
||||
vert: TexturedAndNormalizedTintedShader.vert,
|
||||
|
@ -81,9 +102,9 @@ var LightLayer = new Class({
|
|||
}
|
||||
else
|
||||
{
|
||||
var gl = this.gl = scene.sys.game.renderer.gl;
|
||||
var gl = this.gl = renderer.gl;
|
||||
|
||||
this.ext = scene.sys.game.renderer.getExtension('WEBGL_draw_buffers');
|
||||
this.ext = renderer.getExtension('WEBGL_draw_buffers');
|
||||
|
||||
this.gBufferShaderPass = resourceManager.createShader('GBufferShader', {
|
||||
vert: TexturedAndNormalizedTintedShader.vert,
|
||||
|
@ -138,14 +159,14 @@ var LightLayer = new Class({
|
|||
|
||||
gl.bindFramebuffer(gl.FRAMEBUFFER, this.gBufferFbo);
|
||||
gl.bindTexture(gl.TEXTURE_2D, this.gBufferColorTex);
|
||||
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, scene.sys.game.renderer.width, scene.sys.game.renderer.height, 0, gl.RGBA, gl.UNSIGNED_BYTE, null);
|
||||
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, renderer.width, renderer.height, 0, gl.RGBA, gl.UNSIGNED_BYTE, null);
|
||||
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
|
||||
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
|
||||
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
|
||||
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
|
||||
|
||||
gl.bindTexture(gl.TEXTURE_2D, this.gBufferNormalTex);
|
||||
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, scene.sys.game.renderer.width, scene.sys.game.renderer.height, 0, gl.RGBA, gl.UNSIGNED_BYTE, null);
|
||||
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, renderer.width, renderer.height, 0, gl.RGBA, gl.UNSIGNED_BYTE, null);
|
||||
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
|
||||
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
|
||||
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
|
||||
|
@ -172,8 +193,6 @@ var LightLayer = new Class({
|
|||
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
|
||||
VertexBuffer.SetDirty();
|
||||
}
|
||||
|
||||
this.setOrigin(0, 0);
|
||||
},
|
||||
|
||||
forEachLight: function (callback)
|
||||
|
|
|
@ -29,27 +29,39 @@ var WebGLRenderer = new Class({
|
|||
{
|
||||
var _this = this;
|
||||
this.game = game;
|
||||
this.onContextLostCallbacks = [];
|
||||
this.onContextRestoredCallbacks = [];
|
||||
this.type = CONST.WEBGL;
|
||||
this.width = game.config.width * game.config.resolution;
|
||||
this.height = game.config.height * game.config.resolution;
|
||||
this.resolution = game.config.resolution;
|
||||
this.view = game.canvas;
|
||||
this.view.addEventListener('webglcontextlost', function (evt) {
|
||||
var callbacks = _this.onContextLostCallbacks;
|
||||
var renderers = _this.rendererArray;
|
||||
for (var index = 0; index < renderers.length; ++index)
|
||||
{
|
||||
renderers[index].destroy();
|
||||
}
|
||||
_this.contextLost = true;
|
||||
for (var index = 0; index < callbacks.length; ++index)
|
||||
{
|
||||
callbacks[index](_this);
|
||||
}
|
||||
evt.preventDefault();
|
||||
}, false);
|
||||
|
||||
this.view.addEventListener('webglcontextrestored', function (evt) {
|
||||
var callbacks = _this.onContextRestoredCallbacks;
|
||||
_this.rendererArray.length = 0;
|
||||
_this.resourceManager.shaderCache = {};
|
||||
_this.resourceManager.shaderCount = 0;
|
||||
_this.contextLost = false;
|
||||
_this.init();
|
||||
for (var index = 0; index < callbacks.length; ++index)
|
||||
{
|
||||
callbacks[index](_this);
|
||||
}
|
||||
}, false);
|
||||
|
||||
// All of these settings will be able to be controlled via the Game Config
|
||||
|
@ -201,6 +213,22 @@ var WebGLRenderer = new Class({
|
|||
return this.extensionList[name];
|
||||
},
|
||||
|
||||
addContextLostCallback: function (callback)
|
||||
{
|
||||
if (this.onContextLostCallbacks.indexOf(callback) === -1)
|
||||
{
|
||||
this.onContextLostCallbacks.push(callback);
|
||||
}
|
||||
},
|
||||
|
||||
addContextRestoredCallback: function (callback)
|
||||
{
|
||||
if (this.onContextRestoredCallbacks.indexOf(callback) === -1)
|
||||
{
|
||||
this.onContextRestoredCallbacks.push(callback);
|
||||
}
|
||||
},
|
||||
|
||||
createTexture: function (source, width, height)
|
||||
{
|
||||
width = source ? source.width : width;
|
||||
|
|
Loading…
Reference in a new issue