2017-04-18 22:15:10 +00:00
|
|
|
// RenderPass Will only work with Sprite and Image GameObjects.
|
|
|
|
|
2017-04-11 13:15:38 +00:00
|
|
|
var Class = require('../../utils/Class');
|
|
|
|
var GameObject = require('../GameObject');
|
|
|
|
var Components = require('../../components');
|
|
|
|
var Render = require('./RenderPassRender');
|
2017-04-18 22:15:10 +00:00
|
|
|
var TexturedAndNormalizedTintedShader = require('../../renderer/webgl/shaders/TexturedAndNormalizedTintedShader');
|
2017-04-11 13:15:38 +00:00
|
|
|
|
|
|
|
var RenderPass = new Class({
|
|
|
|
|
|
|
|
Extends: GameObject,
|
|
|
|
|
|
|
|
Mixins: [
|
|
|
|
Components.Alpha,
|
|
|
|
Components.BlendMode,
|
|
|
|
Components.Flip,
|
|
|
|
Components.GetBounds,
|
|
|
|
Components.Origin,
|
|
|
|
Components.RenderTarget,
|
|
|
|
Components.ScaleMode,
|
|
|
|
Components.Size,
|
|
|
|
Components.Transform,
|
|
|
|
Components.Visible,
|
|
|
|
Render
|
|
|
|
],
|
|
|
|
|
|
|
|
initialize:
|
|
|
|
|
2017-04-12 23:05:12 +00:00
|
|
|
function RenderPass (state, x, y, width, height, shaderName, fragmentShader)
|
2017-04-11 13:15:38 +00:00
|
|
|
{
|
2017-04-12 23:05:12 +00:00
|
|
|
GameObject.call(this, state, 'RenderPass');
|
2017-04-11 13:15:38 +00:00
|
|
|
|
|
|
|
var resourceManager = state.game.renderer.resourceManager;
|
|
|
|
var gl;
|
|
|
|
|
2017-04-18 22:15:10 +00:00
|
|
|
this.renderer = state.game.renderer;
|
|
|
|
this.passRenderTarget = null
|
2017-04-20 23:50:47 +00:00
|
|
|
this.renderTexture = null;
|
2017-04-18 22:15:10 +00:00
|
|
|
this.passShader = null;
|
2017-04-11 13:15:38 +00:00
|
|
|
this.uniforms = {};
|
|
|
|
|
|
|
|
if (resourceManager !== undefined)
|
|
|
|
{
|
2017-04-18 22:15:10 +00:00
|
|
|
gl = state.game.renderer.gl;
|
|
|
|
this.passShader = resourceManager.createShader(shaderName, {vert: TexturedAndNormalizedTintedShader.vert, frag: fragmentShader});
|
2017-04-20 23:50:47 +00:00
|
|
|
this.renderTexture = resourceManager.createTexture(0, gl.LINEAR, gl.LINEAR, gl.CLAMP_TO_EDGE, gl.CLAMP_TO_EDGE, gl.RGBA, null, width, height);
|
|
|
|
this.passRenderTarget = resourceManager.createRenderTarget(width, height, this.renderTexture, null);
|
2017-04-11 13:15:38 +00:00
|
|
|
state.game.renderer.currentTexture = null; // force rebinding of prev texture
|
|
|
|
}
|
2017-04-18 22:15:10 +00:00
|
|
|
|
2017-04-20 16:18:14 +00:00
|
|
|
this.flipY = true;
|
2017-04-11 13:15:38 +00:00
|
|
|
this.setPosition(x, y);
|
|
|
|
this.setSize(width, height);
|
|
|
|
this.setOrigin(0, 0);
|
2017-04-18 22:15:10 +00:00
|
|
|
|
2017-04-11 13:15:38 +00:00
|
|
|
},
|
|
|
|
|
2017-04-20 16:18:14 +00:00
|
|
|
clearColorBuffer: function (r, g, b, a)
|
|
|
|
{
|
|
|
|
var gl = this.renderer.gl;
|
|
|
|
if (gl)
|
|
|
|
{
|
2017-04-20 18:06:57 +00:00
|
|
|
gl.bindFramebuffer(gl.FRAMEBUFFER, this.passRenderTarget.framebufferObject);
|
2017-04-20 16:18:14 +00:00
|
|
|
gl.clearColor(r, g, b, a);
|
|
|
|
gl.clear(gl.COLOR_BUFFER_BIT);
|
2017-04-20 18:06:57 +00:00
|
|
|
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
|
2017-04-20 16:18:14 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
clearDepthStencilBuffers: function (depth, stencil)
|
|
|
|
{
|
|
|
|
var gl = this.renderer.gl;
|
|
|
|
if (gl)
|
|
|
|
{
|
2017-04-20 18:06:57 +00:00
|
|
|
gl.bindFramebuffer(gl.FRAMEBUFFER, this.passRenderTarget.framebufferObject);
|
2017-04-20 16:18:14 +00:00
|
|
|
gl.clearDepth(depth);
|
|
|
|
gl.clearStencil(stencil);
|
|
|
|
gl.clear(gl.DEPTH_BUFFER_BIT | gl.STENCIL_BUFFER_BIT);
|
2017-04-20 18:06:57 +00:00
|
|
|
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
|
2017-04-20 16:18:14 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-04-20 18:06:57 +00:00
|
|
|
clearAllBuffers: function (r, g, b, a, depth, stencil)
|
2017-04-20 16:18:14 +00:00
|
|
|
{
|
|
|
|
var gl = this.renderer.gl;
|
|
|
|
if (gl)
|
|
|
|
{
|
2017-04-20 18:06:57 +00:00
|
|
|
gl.bindFramebuffer(gl.FRAMEBUFFER, this.passRenderTarget.framebufferObject);
|
2017-04-20 16:18:14 +00:00
|
|
|
gl.clearColor(r, g, b, a);
|
|
|
|
gl.clearDepth(depth);
|
|
|
|
gl.clearStencil(stencil);
|
|
|
|
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT | gl.STENCIL_BUFFER_BIT);
|
2017-04-20 18:06:57 +00:00
|
|
|
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
|
2017-04-20 16:18:14 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-04-18 22:15:10 +00:00
|
|
|
render: function (gameObject, camera)
|
2017-04-11 13:15:38 +00:00
|
|
|
{
|
2017-04-18 22:15:10 +00:00
|
|
|
var gl = this.renderer.gl;
|
2017-04-20 16:18:14 +00:00
|
|
|
if (gl)
|
|
|
|
{
|
|
|
|
this.renderer.spriteBatch.addSprite(gameObject, camera);
|
|
|
|
this.renderer.spriteBatch.flush(this.passShader, this.passRenderTarget.framebufferObject);
|
|
|
|
this.renderer.setRenderer(null, null, null);
|
|
|
|
}
|
2017-04-11 13:15:38 +00:00
|
|
|
},
|
|
|
|
|
2017-04-20 18:06:57 +00:00
|
|
|
setRenderTextureAt: function (renderTexture, samplerName, unit)
|
|
|
|
{
|
|
|
|
var gl = this.renderer.gl;
|
|
|
|
if (gl)
|
|
|
|
{
|
|
|
|
/* Texture 1 is reserved for Phaser's Main Renderer */
|
|
|
|
unit = unit > 0 ? unit : 1;
|
|
|
|
this.setInt(samplerName, unit);
|
|
|
|
gl.activeTexture(gl.TEXTURE0 + unit);
|
|
|
|
gl.bindTexture(gl.TEXTURE_2D, renderTexture.texture);
|
|
|
|
gl.activeTexture(gl.TEXTURE0);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-04-11 13:15:38 +00:00
|
|
|
getUniformLocation: function (uniformName)
|
|
|
|
{
|
2017-04-20 16:18:14 +00:00
|
|
|
var passShader = this.passShader;
|
2017-04-11 13:15:38 +00:00
|
|
|
var uniforms = this.uniforms;
|
|
|
|
var location;
|
|
|
|
|
|
|
|
if (uniformName in uniforms)
|
|
|
|
{
|
|
|
|
location = uniforms[uniformName];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-04-20 16:18:14 +00:00
|
|
|
location = passShader.getUniformLocation(uniformName);
|
2017-04-11 13:15:38 +00:00
|
|
|
uniforms[uniformName] = location;
|
|
|
|
}
|
|
|
|
|
|
|
|
return location;
|
|
|
|
},
|
|
|
|
|
|
|
|
setFloat: function (uniformName, x)
|
|
|
|
{
|
2017-04-20 16:18:14 +00:00
|
|
|
var passShader = this.passShader;
|
2017-04-11 13:15:38 +00:00
|
|
|
|
2017-04-20 16:18:14 +00:00
|
|
|
if (passShader === null)
|
2017-04-11 13:15:38 +00:00
|
|
|
return;
|
|
|
|
|
2017-04-20 16:18:14 +00:00
|
|
|
passShader.setConstantFloat1(this.getUniformLocation(uniformName), x);
|
2017-04-11 13:15:38 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
setFloat2: function (uniformName, x, y)
|
|
|
|
{
|
2017-04-20 16:18:14 +00:00
|
|
|
var passShader = this.passShader;
|
2017-04-11 13:15:38 +00:00
|
|
|
|
2017-04-20 16:18:14 +00:00
|
|
|
if (passShader === null)
|
2017-04-11 13:15:38 +00:00
|
|
|
return;
|
|
|
|
|
2017-04-20 16:18:14 +00:00
|
|
|
passShader.setConstantFloat2(this.getUniformLocation(uniformName), x, y);
|
2017-04-11 13:15:38 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
setFloat3: function (uniformName, x, y, z)
|
|
|
|
{
|
2017-04-20 16:18:14 +00:00
|
|
|
var passShader = this.passShader;
|
2017-04-11 13:15:38 +00:00
|
|
|
|
2017-04-20 16:18:14 +00:00
|
|
|
if (passShader === null)
|
2017-04-11 13:15:38 +00:00
|
|
|
return;
|
|
|
|
|
2017-04-20 16:18:14 +00:00
|
|
|
passShader.setConstantFloat3(this.getUniformLocation(uniformName), x, y, z);
|
2017-04-11 13:15:38 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
setFloat4: function (uniformName, x, y, z, w)
|
|
|
|
{
|
2017-04-20 16:18:14 +00:00
|
|
|
var passShader = this.passShader;
|
2017-04-11 13:15:38 +00:00
|
|
|
|
2017-04-20 16:18:14 +00:00
|
|
|
if (passShader === null)
|
2017-04-11 13:15:38 +00:00
|
|
|
return;
|
|
|
|
|
2017-04-20 16:18:14 +00:00
|
|
|
passShader.setConstantFloat4(this.getUniformLocation(uniformName), x, y, z, w);
|
2017-04-11 13:15:38 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
setInt: function (uniformName, x)
|
|
|
|
{
|
2017-04-20 16:18:14 +00:00
|
|
|
var passShader = this.passShader;
|
2017-04-11 13:15:38 +00:00
|
|
|
|
2017-04-20 16:18:14 +00:00
|
|
|
if (passShader === null)
|
2017-04-11 13:15:38 +00:00
|
|
|
return;
|
|
|
|
|
2017-04-20 16:18:14 +00:00
|
|
|
passShader.setConstantInt1(this.getUniformLocation(uniformName), x);
|
2017-04-11 13:15:38 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
setInt2: function (uniformName, x, y)
|
|
|
|
{
|
2017-04-20 16:18:14 +00:00
|
|
|
var passShader = this.passShader;
|
2017-04-11 13:15:38 +00:00
|
|
|
|
2017-04-20 16:18:14 +00:00
|
|
|
if (passShader === null)
|
2017-04-11 13:15:38 +00:00
|
|
|
return;
|
|
|
|
|
2017-04-20 16:18:14 +00:00
|
|
|
passShader.setConstantInt2(this.getUniformLocation(uniformName), x, y);
|
2017-04-11 13:15:38 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
setInt3: function (uniformName, x, y, z)
|
|
|
|
{
|
2017-04-20 16:18:14 +00:00
|
|
|
var passShader = this.passShader;
|
2017-04-11 13:15:38 +00:00
|
|
|
|
2017-04-20 16:18:14 +00:00
|
|
|
if (passShader === null)
|
2017-04-11 13:15:38 +00:00
|
|
|
return;
|
|
|
|
|
2017-04-20 16:18:14 +00:00
|
|
|
passShader.setConstantInt3(this.getUniformLocation(uniformName), x, y, z);
|
2017-04-11 13:15:38 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
setInt4: function (uniformName, x, y, z, w)
|
|
|
|
{
|
2017-04-20 16:18:14 +00:00
|
|
|
var passShader = this.passShader;
|
2017-04-11 13:15:38 +00:00
|
|
|
|
2017-04-20 16:18:14 +00:00
|
|
|
if (passShader === null)
|
2017-04-11 13:15:38 +00:00
|
|
|
return;
|
|
|
|
|
2017-04-20 16:18:14 +00:00
|
|
|
passShader.setConstantInt4(this.getUniformLocation(uniformName), x, y, z, w);
|
2017-04-11 13:15:38 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
setMatrix2x2: function (uniformName, matrix)
|
|
|
|
{
|
2017-04-20 16:18:14 +00:00
|
|
|
var passShader = this.passShader;
|
2017-04-11 13:15:38 +00:00
|
|
|
|
2017-04-20 16:18:14 +00:00
|
|
|
if (passShader === null)
|
2017-04-11 13:15:38 +00:00
|
|
|
return;
|
|
|
|
|
2017-04-20 16:18:14 +00:00
|
|
|
passShader.setConstantMatrix2x2(this.getUniformLocation(uniformName), matrix);
|
2017-04-11 13:15:38 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
setMatrix3x3: function (uniformName, matrix)
|
|
|
|
{
|
2017-04-20 16:18:14 +00:00
|
|
|
var passShader = this.passShader;
|
2017-04-11 13:15:38 +00:00
|
|
|
|
2017-04-20 16:18:14 +00:00
|
|
|
if (passShader === null)
|
2017-04-11 13:15:38 +00:00
|
|
|
return;
|
|
|
|
|
2017-04-20 16:18:14 +00:00
|
|
|
passShader.setConstantMatrix3x3(this.getUniformLocation(uniformName), matrix);
|
2017-04-11 13:15:38 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
setMatrix4x4: function (uniformName, matrix)
|
|
|
|
{
|
2017-04-20 16:18:14 +00:00
|
|
|
var passShader = this.passShader;
|
2017-04-11 13:15:38 +00:00
|
|
|
|
2017-04-20 16:18:14 +00:00
|
|
|
if (passShader === null)
|
2017-04-11 13:15:38 +00:00
|
|
|
return;
|
|
|
|
|
2017-04-20 16:18:14 +00:00
|
|
|
passShader.setConstantMatrix4x4(this.getUniformLocation(uniformName), matrix);
|
2017-04-11 13:15:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = RenderPass;
|