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');
|
2017-07-04 00:59:31 +00:00
|
|
|
var Components = require('../components');
|
2017-04-11 13:15:38 +00:00
|
|
|
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
|
|
|
|
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.
|
|
|
|
|
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,
|
2017-07-04 11:25:31 +00:00
|
|
|
Components.ScrollFactor,
|
2017-04-11 13:15:38 +00:00
|
|
|
Components.Size,
|
2017-07-04 11:25:31 +00:00
|
|
|
Components.Tint,
|
2017-04-11 13:15:38 +00:00
|
|
|
Components.Transform,
|
|
|
|
Components.Visible,
|
|
|
|
Render
|
|
|
|
],
|
|
|
|
|
|
|
|
initialize:
|
|
|
|
|
2017-07-14 13:30:20 +00:00
|
|
|
function RenderPass (scene, x, y, width, height, shaderName, fragmentShader)
|
2017-04-11 13:15:38 +00:00
|
|
|
{
|
2017-07-14 13:30:20 +00:00
|
|
|
GameObject.call(this, scene, 'RenderPass');
|
2017-04-11 13:15:38 +00:00
|
|
|
|
2017-08-16 16:15:12 +00:00
|
|
|
var resourceManager = scene.sys.game.renderer.resourceManager;
|
2017-05-20 01:16:45 +00:00
|
|
|
var pot = ((width & (width - 1)) == 0 && (height & (height - 1)) == 0);
|
2017-04-11 13:15:38 +00:00
|
|
|
var gl;
|
2017-06-02 16:27:51 +00:00
|
|
|
var wrap;
|
|
|
|
|
2017-08-16 16:15:12 +00:00
|
|
|
this.renderer = scene.sys.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;
|
2017-04-11 13:15:38 +00:00
|
|
|
this.uniforms = {};
|
2017-06-28 09:22:48 +00:00
|
|
|
this.textures = {};
|
2017-04-11 13:15:38 +00:00
|
|
|
|
|
|
|
if (resourceManager !== undefined)
|
|
|
|
{
|
2017-08-16 16:15:12 +00:00
|
|
|
gl = scene.sys.game.renderer.gl;
|
2017-06-02 16:27:51 +00:00
|
|
|
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);
|
2017-08-16 16:15:12 +00:00
|
|
|
scene.sys.game.renderer.currentTexture[0] = null; // force rebinding of prev texture
|
2017-04-11 13:15:38 +00:00
|
|
|
}
|
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-09-13 20:54:32 +00:00
|
|
|
|
|
|
|
var _this = this;
|
|
|
|
scene.sys.game.renderer.addContextRestoredCallback(function (renderer) {
|
|
|
|
var gl = renderer.gl;
|
|
|
|
var wrap = pot ? gl.REPEAT : gl.CLAMP_TO_EDGE;
|
|
|
|
_this.passShader = resourceManager.createShader(shaderName, {vert: TexturedAndNormalizedTintedShader.vert, frag: fragmentShader});
|
|
|
|
_this.renderTexture = resourceManager.createTexture(0, gl.LINEAR, gl.LINEAR, wrap, wrap, gl.RGBA, null, _this.width, _this.height);
|
|
|
|
_this.passRenderTarget = resourceManager.createRenderTarget(_this.width, _this.height, _this.renderTexture, null);
|
|
|
|
_this.uniforms = {};
|
|
|
|
_this.textures = {};
|
|
|
|
scene.sys.game.renderer.currentTexture[0] = null; // force rebinding of prev texture
|
|
|
|
});
|
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;
|
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-11 13:15:38 +00:00
|
|
|
{
|
2017-09-13 20:54:32 +00:00
|
|
|
var renderer = this.renderer;
|
|
|
|
var gl = renderer.gl;
|
2017-05-09 23:43:12 +00:00
|
|
|
|
2017-09-13 20:54:32 +00:00
|
|
|
if (gl && !renderer.contextLost)
|
2017-04-20 16:18:14 +00:00
|
|
|
{
|
2017-09-13 20:54:32 +00:00
|
|
|
renderer.setRenderer(this.renderer.spriteBatch, null, null);
|
|
|
|
renderer.spriteBatch.addSprite(gameObject, camera);
|
2017-06-28 09:22:48 +00:00
|
|
|
for (var key in this.textures)
|
|
|
|
{
|
|
|
|
var textureData = this.textures[key];
|
|
|
|
this.setInt(key, textureData.unit);
|
|
|
|
gl.activeTexture(gl.TEXTURE0 + textureData.unit);
|
|
|
|
gl.bindTexture(gl.TEXTURE_2D, textureData.texture);
|
|
|
|
gl.activeTexture(gl.TEXTURE0);
|
|
|
|
}
|
2017-11-14 17:30:34 +00:00
|
|
|
renderer.spriteBatch.flush(this.passShader, this.passRenderTarget);
|
2017-06-28 09:22:48 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
renderRect: function (x, y, width, height, camera)
|
|
|
|
{
|
2017-09-13 20:54:32 +00:00
|
|
|
var renderer = this.renderer;
|
|
|
|
var gl = renderer.gl;
|
2017-06-28 09:22:48 +00:00
|
|
|
|
2017-09-13 20:54:32 +00:00
|
|
|
if (gl && !renderer.contextLost)
|
2017-06-28 09:22:48 +00:00
|
|
|
{
|
2017-09-13 20:54:32 +00:00
|
|
|
renderer.setRenderer(this.renderer.spriteBatch, null, null);
|
|
|
|
renderer.spriteBatch.addTileTextureRect(
|
2017-06-28 09:22:48 +00:00
|
|
|
null, x, y, width, height, 1.0, 0xFFFFFFFF, this.scrollFactorX, this.scrollFactorY,
|
|
|
|
width, height, 0, 0, width, height, camera, null
|
|
|
|
);
|
|
|
|
for (var key in this.textures)
|
|
|
|
{
|
|
|
|
var textureData = this.textures[key];
|
|
|
|
this.setInt(key, textureData.unit);
|
|
|
|
gl.activeTexture(gl.TEXTURE0 + textureData.unit);
|
|
|
|
gl.bindTexture(gl.TEXTURE_2D, textureData.texture);
|
|
|
|
gl.activeTexture(gl.TEXTURE0);
|
|
|
|
}
|
2017-09-13 20:54:32 +00:00
|
|
|
renderer.spriteBatch.flush(this.passShader, this.passRenderTarget.framebufferObject);
|
2017-04-20 16:18:14 +00:00
|
|
|
}
|
2017-04-11 13:15:38 +00:00
|
|
|
},
|
|
|
|
|
2017-04-20 18:06:57 +00:00
|
|
|
setRenderTextureAt: function (renderTexture, samplerName, unit)
|
|
|
|
{
|
2017-09-13 20:54:32 +00:00
|
|
|
var renderer = this.renderer;
|
|
|
|
var gl = renderer.gl;
|
2017-05-09 23:43:12 +00:00
|
|
|
|
2017-09-13 20:54:32 +00:00
|
|
|
if (gl && !renderer.contextLost)
|
2017-04-20 18:06:57 +00:00
|
|
|
{
|
2017-05-09 23:43:12 +00:00
|
|
|
/* Texture 1 is reserved for Phasers Main Renderer */
|
|
|
|
unit = (unit > 0) ? unit : 1;
|
2017-06-28 09:22:48 +00:00
|
|
|
this.textures[samplerName] = { texture: renderTexture.texture, unit: unit };
|
|
|
|
//this.setInt(samplerName, unit);
|
|
|
|
//gl.activeTexture(gl.TEXTURE0 + unit);
|
|
|
|
//gl.bindTexture(gl.TEXTURE_2D, renderTexture.texture);
|
|
|
|
//gl.activeTexture(gl.TEXTURE0);
|
2017-04-20 18:06:57 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
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-05-09 23:43:12 +00:00
|
|
|
{
|
2017-04-11 13:15:38 +00:00
|
|
|
return;
|
2017-05-09 23:43:12 +00:00
|
|
|
}
|
2017-04-11 13:15:38 +00:00
|
|
|
|
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-05-09 23:43:12 +00:00
|
|
|
{
|
2017-04-11 13:15:38 +00:00
|
|
|
return;
|
2017-05-09 23:43:12 +00:00
|
|
|
}
|
2017-04-11 13:15:38 +00:00
|
|
|
|
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-05-09 23:43:12 +00:00
|
|
|
{
|
2017-04-11 13:15:38 +00:00
|
|
|
return;
|
2017-05-09 23:43:12 +00:00
|
|
|
}
|
2017-04-11 13:15:38 +00:00
|
|
|
|
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-05-09 23:43:12 +00:00
|
|
|
{
|
2017-04-11 13:15:38 +00:00
|
|
|
return;
|
2017-05-09 23:43:12 +00:00
|
|
|
}
|
2017-04-11 13:15:38 +00:00
|
|
|
|
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-05-09 23:43:12 +00:00
|
|
|
{
|
2017-04-11 13:15:38 +00:00
|
|
|
return;
|
2017-05-09 23:43:12 +00:00
|
|
|
}
|
2017-04-11 13:15:38 +00:00
|
|
|
|
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-05-09 23:43:12 +00:00
|
|
|
{
|
2017-04-11 13:15:38 +00:00
|
|
|
return;
|
2017-05-09 23:43:12 +00:00
|
|
|
}
|
2017-04-11 13:15:38 +00:00
|
|
|
|
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-05-09 23:43:12 +00:00
|
|
|
{
|
2017-04-11 13:15:38 +00:00
|
|
|
return;
|
2017-05-09 23:43:12 +00:00
|
|
|
}
|
2017-04-11 13:15:38 +00:00
|
|
|
|
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-05-09 23:43:12 +00:00
|
|
|
{
|
2017-04-11 13:15:38 +00:00
|
|
|
return;
|
2017-05-09 23:43:12 +00:00
|
|
|
}
|
2017-04-11 13:15:38 +00:00
|
|
|
|
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-05-09 23:43:12 +00:00
|
|
|
{
|
2017-04-11 13:15:38 +00:00
|
|
|
return;
|
2017-05-09 23:43:12 +00:00
|
|
|
}
|
2017-04-11 13:15:38 +00:00
|
|
|
|
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-05-09 23:43:12 +00:00
|
|
|
{
|
2017-04-11 13:15:38 +00:00
|
|
|
return;
|
2017-05-09 23:43:12 +00:00
|
|
|
}
|
2017-04-11 13:15:38 +00:00
|
|
|
|
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-05-09 23:43:12 +00:00
|
|
|
{
|
2017-04-11 13:15:38 +00:00
|
|
|
return;
|
2017-05-09 23:43:12 +00:00
|
|
|
}
|
2017-04-11 13:15:38 +00:00
|
|
|
|
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;
|