phaser/v3/src/gameobjects/effectlayer/EffectLayer.js

253 lines
6 KiB
JavaScript
Raw Normal View History

2017-04-07 01:49:15 +00:00
var Class = require('../../utils/Class');
var GameObject = require('../GameObject');
var Components = require('../../components');
var Render = require('./EffectLayerRender');
var TexturedAndNormalizedTintedShader = require('../../renderer/webgl/shaders/TexturedAndNormalizedTintedShader');
2017-05-09 23:43:12 +00:00
// EffectLayer renders all elements on the layer to an offscreen render target
// and then when rendering the color buffer of that render target to the main screen
// it applies the effect layer shader.
2017-04-07 01:49:15 +00:00
var EffectLayer = new Class({
Extends: GameObject,
Mixins: [
Components.Alpha,
Components.BlendMode,
Components.Flip,
Components.GetBounds,
2017-04-07 01:49:15 +00:00
Components.Origin,
Components.RenderTarget,
Components.ScaleMode,
Components.Size,
Components.Transform,
Components.Visible,
2017-06-22 02:19:03 +00:00
Components.ScrollFactor,
2017-04-07 01:49:15 +00:00
Render
],
initialize:
function EffectLayer (state, x, y, width, height, effectName, fragmentShader)
2017-04-07 01:49:15 +00:00
{
GameObject.call(this, state, 'EffectLayer');
2017-05-20 01:16:45 +00:00
var pot = ((width & (width - 1)) == 0 && (height & (height - 1)) == 0);
2017-04-07 01:49:15 +00:00
var resourceManager = state.game.renderer.resourceManager;
var wrap;
2017-04-07 01:49:15 +00:00
var gl;
this.dstRenderTarget = null;
2017-04-20 23:50:47 +00:00
this.renderTexture = null;
2017-04-07 01:49:15 +00:00
this.dstShader = null;
2017-04-07 18:50:44 +00:00
this.uniforms = {};
2017-04-07 01:49:15 +00:00
if (resourceManager !== undefined)
{
gl = state.game.renderer.gl;
wrap = pot ? gl.REPEAT : gl.CLAMP_TO_EDGE;
2017-04-07 01:49:15 +00:00
this.dstShader = resourceManager.createShader(effectName, {
vert: TexturedAndNormalizedTintedShader.vert,
frag: fragmentShader
});
2017-04-20 23:50:47 +00:00
this.renderTexture = resourceManager.createTexture(
0,
gl.LINEAR, gl.LINEAR,
2017-05-20 01:16:45 +00:00
wrap, wrap,
gl.RGBA,
2017-04-07 01:49:15 +00:00
null, width, height
);
2017-04-20 23:50:47 +00:00
this.dstRenderTarget = resourceManager.createRenderTarget(width, height, this.renderTexture, null);
state.game.renderer.currentTexture = null; // force rebinding of prev texture
2017-04-07 01:49:15 +00:00
}
2017-04-07 01:49:15 +00:00
this.flipY = true;
this.setPosition(x, y);
this.setSize(width, height);
this.setOrigin(0, 0);
},
2017-05-09 23:43:12 +00:00
renderOffScreen: function ()
{
this.renderTarget = this.dstRenderTarget;
},
renderOnScreen: function ()
{
this.renderTarget = null;
},
2017-04-07 01:49:15 +00:00
add: function (gameObject)
{
if (gameObject.renderTarget !== undefined)
{
gameObject.renderTarget = this.dstRenderTarget;
}
},
remove: function (gameObject)
{
if (gameObject.renderTarget !== undefined)
{
gameObject.renderTarget = null;
}
2017-04-07 18:50:44 +00:00
},
getUniformLocation: function (uniformName)
{
var dstShader = this.dstShader;
var uniforms = this.uniforms;
var location;
if (uniformName in uniforms)
{
location = uniforms[uniformName];
}
else
{
location = dstShader.getUniformLocation(uniformName);
uniforms[uniformName] = location;
}
return location;
},
setFloat: function (uniformName, x)
{
var dstShader = this.dstShader;
if (dstShader === null)
{
2017-04-07 18:50:44 +00:00
return;
}
2017-04-07 18:50:44 +00:00
dstShader.setConstantFloat1(this.getUniformLocation(uniformName), x);
},
setFloat2: function (uniformName, x, y)
{
var dstShader = this.dstShader;
if (dstShader === null)
{
2017-04-07 18:50:44 +00:00
return;
}
2017-04-07 18:50:44 +00:00
dstShader.setConstantFloat2(this.getUniformLocation(uniformName), x, y);
},
setFloat3: function (uniformName, x, y, z)
{
var dstShader = this.dstShader;
if (dstShader === null)
{
2017-04-07 18:50:44 +00:00
return;
}
2017-04-07 18:50:44 +00:00
dstShader.setConstantFloat3(this.getUniformLocation(uniformName), x, y, z);
},
setFloat4: function (uniformName, x, y, z, w)
{
var dstShader = this.dstShader;
if (dstShader === null)
{
2017-04-07 18:50:44 +00:00
return;
}
2017-04-07 18:50:44 +00:00
dstShader.setConstantFloat4(this.getUniformLocation(uniformName), x, y, z, w);
2017-04-10 15:05:56 +00:00
},
setInt: function (uniformName, x)
{
var dstShader = this.dstShader;
if (dstShader === null)
{
2017-04-10 15:05:56 +00:00
return;
}
2017-04-10 15:05:56 +00:00
dstShader.setConstantInt1(this.getUniformLocation(uniformName), x);
},
setInt2: function (uniformName, x, y)
{
var dstShader = this.dstShader;
if (dstShader === null)
{
2017-04-10 15:05:56 +00:00
return;
}
2017-04-10 15:05:56 +00:00
dstShader.setConstantInt2(this.getUniformLocation(uniformName), x, y);
},
setInt3: function (uniformName, x, y, z)
{
var dstShader = this.dstShader;
if (dstShader === null)
{
2017-04-10 15:05:56 +00:00
return;
}
2017-04-10 15:05:56 +00:00
dstShader.setConstantInt3(this.getUniformLocation(uniformName), x, y, z);
},
setInt4: function (uniformName, x, y, z, w)
{
var dstShader = this.dstShader;
if (dstShader === null)
{
2017-04-10 15:05:56 +00:00
return;
}
2017-04-10 15:05:56 +00:00
dstShader.setConstantInt4(this.getUniformLocation(uniformName), x, y, z, w);
},
setMatrix2x2: function (uniformName, matrix)
{
var dstShader = this.dstShader;
if (dstShader === null)
{
2017-04-10 15:05:56 +00:00
return;
}
2017-04-10 15:05:56 +00:00
dstShader.setConstantMatrix2x2(this.getUniformLocation(uniformName), matrix);
},
setMatrix3x3: function (uniformName, matrix)
{
var dstShader = this.dstShader;
if (dstShader === null)
{
2017-04-10 15:05:56 +00:00
return;
}
2017-04-10 15:05:56 +00:00
dstShader.setConstantMatrix3x3(this.getUniformLocation(uniformName), matrix);
},
setMatrix4x4: function (uniformName, matrix)
{
var dstShader = this.dstShader;
if (dstShader === null)
{
2017-04-10 15:05:56 +00:00
return;
}
2017-04-10 15:05:56 +00:00
dstShader.setConstantMatrix4x4(this.getUniformLocation(uniformName), matrix);
2017-04-07 01:49:15 +00:00
}
});
module.exports = EffectLayer;