phaser/v3/src/gameobjects/renderpass/RenderPass.js

288 lines
7.7 KiB
JavaScript
Raw Normal View History

2017-04-18 22:15:10 +00:00
// RenderPass Will only work with Sprite and Image GameObjects.
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-05-09 23:43:12 +00:00
// RenderPass - the user has a higher control on the rendering since you explicitly
// indicate what is rendered. RenderPass also has a render target but the difference
// is that when explicitly rendering an object to the render pass the shader from that
// render pass is applied. This is useful for additive passes and specific object effects.
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:
function RenderPass (state, x, y, width, height, shaderName, fragmentShader)
{
GameObject.call(this, state, 'RenderPass');
var resourceManager = state.game.renderer.resourceManager;
2017-05-20 01:16:45 +00:00
var pot = ((width & (width - 1)) == 0 && (height & (height - 1)) == 0);
var gl;
var wrap;
2017-04-18 22:15:10 +00:00
this.renderer = state.game.renderer;
2017-05-09 23:43:12 +00:00
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;
this.uniforms = {};
if (resourceManager !== undefined)
{
2017-04-18 22:15:10 +00:00
gl = state.game.renderer.gl;
wrap = pot ? gl.REPEAT : gl.CLAMP_TO_EDGE;
2017-04-18 22:15:10 +00:00
this.passShader = resourceManager.createShader(shaderName, {vert: TexturedAndNormalizedTintedShader.vert, frag: fragmentShader});
2017-05-20 01:16:45 +00:00
this.renderTexture = resourceManager.createTexture(0, gl.LINEAR, gl.LINEAR, wrap, wrap, gl.RGBA, null, width, height);
2017-04-20 23:50:47 +00:00
this.passRenderTarget = resourceManager.createRenderTarget(width, height, this.renderTexture, null);
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;
this.setPosition(x, y);
this.setSize(width, height);
this.setOrigin(0, 0);
},
2017-04-20 16:18:14 +00:00
clearColorBuffer: function (r, g, b, a)
{
var gl = this.renderer.gl;
2017-05-09 23:43:12 +00:00
2017-04-20 16:18:14 +00:00
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;
2017-05-09 23:43:12 +00:00
2017-04-20 16:18:14 +00:00
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;
2017-05-09 23:43:12 +00:00
2017-04-20 16:18:14 +00:00
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-18 22:15:10 +00:00
var gl = this.renderer.gl;
2017-05-09 23:43:12 +00:00
2017-04-20 16:18:14 +00:00
if (gl)
{
this.renderer.setRenderer(this.renderer.spriteBatch, null, null);
2017-04-20 16:18:14 +00:00
this.renderer.spriteBatch.addSprite(gameObject, camera);
this.renderer.spriteBatch.flush(this.passShader, this.passRenderTarget.framebufferObject);
}
},
2017-04-20 18:06:57 +00:00
setRenderTextureAt: function (renderTexture, samplerName, unit)
{
var gl = this.renderer.gl;
2017-05-09 23:43:12 +00:00
2017-04-20 18:06:57 +00:00
if (gl)
{
2017-05-09 23:43:12 +00:00
/* Texture 1 is reserved for Phasers Main Renderer */
unit = (unit > 0) ? unit : 1;
2017-04-20 18:06:57 +00:00
this.setInt(samplerName, unit);
gl.activeTexture(gl.TEXTURE0 + unit);
gl.bindTexture(gl.TEXTURE_2D, renderTexture.texture);
gl.activeTexture(gl.TEXTURE0);
}
},
getUniformLocation: function (uniformName)
{
2017-04-20 16:18:14 +00:00
var passShader = this.passShader;
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);
uniforms[uniformName] = location;
}
return location;
},
setFloat: function (uniformName, x)
{
2017-04-20 16:18:14 +00:00
var passShader = this.passShader;
2017-04-20 16:18:14 +00:00
if (passShader === null)
2017-05-09 23:43:12 +00:00
{
return;
2017-05-09 23:43:12 +00:00
}
2017-04-20 16:18:14 +00:00
passShader.setConstantFloat1(this.getUniformLocation(uniformName), x);
},
setFloat2: function (uniformName, x, y)
{
2017-04-20 16:18:14 +00:00
var passShader = this.passShader;
2017-04-20 16:18:14 +00:00
if (passShader === null)
2017-05-09 23:43:12 +00:00
{
return;
2017-05-09 23:43:12 +00:00
}
2017-04-20 16:18:14 +00:00
passShader.setConstantFloat2(this.getUniformLocation(uniformName), x, y);
},
setFloat3: function (uniformName, x, y, z)
{
2017-04-20 16:18:14 +00:00
var passShader = this.passShader;
2017-04-20 16:18:14 +00:00
if (passShader === null)
2017-05-09 23:43:12 +00:00
{
return;
2017-05-09 23:43:12 +00:00
}
2017-04-20 16:18:14 +00:00
passShader.setConstantFloat3(this.getUniformLocation(uniformName), x, y, z);
},
setFloat4: function (uniformName, x, y, z, w)
{
2017-04-20 16:18:14 +00:00
var passShader = this.passShader;
2017-04-20 16:18:14 +00:00
if (passShader === null)
2017-05-09 23:43:12 +00:00
{
return;
2017-05-09 23:43:12 +00:00
}
2017-04-20 16:18:14 +00:00
passShader.setConstantFloat4(this.getUniformLocation(uniformName), x, y, z, w);
},
setInt: function (uniformName, x)
{
2017-04-20 16:18:14 +00:00
var passShader = this.passShader;
2017-04-20 16:18:14 +00:00
if (passShader === null)
2017-05-09 23:43:12 +00:00
{
return;
2017-05-09 23:43:12 +00:00
}
2017-04-20 16:18:14 +00:00
passShader.setConstantInt1(this.getUniformLocation(uniformName), x);
},
setInt2: function (uniformName, x, y)
{
2017-04-20 16:18:14 +00:00
var passShader = this.passShader;
2017-04-20 16:18:14 +00:00
if (passShader === null)
2017-05-09 23:43:12 +00:00
{
return;
2017-05-09 23:43:12 +00:00
}
2017-04-20 16:18:14 +00:00
passShader.setConstantInt2(this.getUniformLocation(uniformName), x, y);
},
setInt3: function (uniformName, x, y, z)
{
2017-04-20 16:18:14 +00:00
var passShader = this.passShader;
2017-04-20 16:18:14 +00:00
if (passShader === null)
2017-05-09 23:43:12 +00:00
{
return;
2017-05-09 23:43:12 +00:00
}
2017-04-20 16:18:14 +00:00
passShader.setConstantInt3(this.getUniformLocation(uniformName), x, y, z);
},
setInt4: function (uniformName, x, y, z, w)
{
2017-04-20 16:18:14 +00:00
var passShader = this.passShader;
2017-04-20 16:18:14 +00:00
if (passShader === null)
2017-05-09 23:43:12 +00:00
{
return;
2017-05-09 23:43:12 +00:00
}
2017-04-20 16:18:14 +00:00
passShader.setConstantInt4(this.getUniformLocation(uniformName), x, y, z, w);
},
setMatrix2x2: function (uniformName, matrix)
{
2017-04-20 16:18:14 +00:00
var passShader = this.passShader;
2017-04-20 16:18:14 +00:00
if (passShader === null)
2017-05-09 23:43:12 +00:00
{
return;
2017-05-09 23:43:12 +00:00
}
2017-04-20 16:18:14 +00:00
passShader.setConstantMatrix2x2(this.getUniformLocation(uniformName), matrix);
},
setMatrix3x3: function (uniformName, matrix)
{
2017-04-20 16:18:14 +00:00
var passShader = this.passShader;
2017-04-20 16:18:14 +00:00
if (passShader === null)
2017-05-09 23:43:12 +00:00
{
return;
2017-05-09 23:43:12 +00:00
}
2017-04-20 16:18:14 +00:00
passShader.setConstantMatrix3x3(this.getUniformLocation(uniformName), matrix);
},
setMatrix4x4: function (uniformName, matrix)
{
2017-04-20 16:18:14 +00:00
var passShader = this.passShader;
2017-04-20 16:18:14 +00:00
if (passShader === null)
2017-05-09 23:43:12 +00:00
{
return;
2017-05-09 23:43:12 +00:00
}
2017-04-20 16:18:14 +00:00
passShader.setConstantMatrix4x4(this.getUniformLocation(uniformName), matrix);
}
});
module.exports = RenderPass;