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

323 lines
9.1 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,
2017-07-04 11:25:31 +00:00
Components.ScrollFactor,
Components.Size,
2017-07-04 11:25:31 +00:00
Components.Tint,
Components.Transform,
Components.Visible,
Render
],
initialize:
function RenderPass (scene, x, y, width, height, shaderName, fragmentShader)
{
GameObject.call(this, scene, 'RenderPass');
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);
var gl;
var wrap;
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;
this.uniforms = {};
2017-06-28 09:22:48 +00:00
this.textures = {};
if (resourceManager !== undefined)
{
gl = scene.sys.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);
scene.sys.game.renderer.currentTexture[0] = 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);
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);
}
this.renderer.spriteBatch.flush(this.passShader, this.passRenderTarget.framebufferObject);
}
},
renderRect: function (x, y, width, height, camera)
{
var gl = this.renderer.gl;
if (gl)
{
this.renderer.setRenderer(this.renderer.spriteBatch, null, null);
this.renderer.spriteBatch.addTileTextureRect(
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-04-20 16:18:14 +00:00
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-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
}
},
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;