2016-11-03 04:04:34 +00:00
|
|
|
/**
|
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
|
|
|
* @copyright 2016 Photon Storm Ltd.
|
|
|
|
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Frame Buffer Object with drawing quad + shader
|
|
|
|
*
|
|
|
|
* @class Phaser.Renderer.Canvas
|
|
|
|
* @constructor
|
|
|
|
* @param {Phaser.Game} game - Game reference to the currently running game.
|
|
|
|
*/
|
|
|
|
Phaser.Renderer.WebGL.QuadFBO = function (renderer, x, y, width, height)
|
|
|
|
{
|
|
|
|
this.renderer = renderer;
|
|
|
|
|
2016-11-04 03:09:48 +00:00
|
|
|
this.gl = renderer.gl;
|
2016-11-03 04:04:34 +00:00
|
|
|
|
|
|
|
this._x = x;
|
|
|
|
this._y = y;
|
|
|
|
this._width = width;
|
|
|
|
this._height = height;
|
|
|
|
|
|
|
|
this.textureIndex = 0;
|
|
|
|
|
|
|
|
this.clipX = function (x)
|
|
|
|
{
|
|
|
|
return (renderer.clipUnitX * x) - 1;
|
|
|
|
};
|
|
|
|
|
|
|
|
this.clipY = function (y)
|
|
|
|
{
|
|
|
|
return 1 - (renderer.clipUnitY * y);
|
|
|
|
};
|
|
|
|
|
2016-11-03 04:22:07 +00:00
|
|
|
this.vertexBuffer;
|
2016-11-03 05:10:50 +00:00
|
|
|
this.indexBuffer;
|
2016-11-03 04:22:07 +00:00
|
|
|
this.textureBuffer;
|
|
|
|
|
2016-11-03 04:04:34 +00:00
|
|
|
this.vertices;
|
|
|
|
|
2016-11-03 04:22:07 +00:00
|
|
|
this.texture;
|
|
|
|
this.renderBuffer;
|
|
|
|
this.frameBuffer;
|
2016-11-03 04:04:34 +00:00
|
|
|
|
|
|
|
this.program;
|
|
|
|
this.aVertexPosition;
|
|
|
|
this.aTextureCoord;
|
2016-11-04 02:24:49 +00:00
|
|
|
|
|
|
|
this.init();
|
2016-11-03 04:04:34 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Phaser.Renderer.WebGL.QuadFBO.prototype.constructor = Phaser.Renderer.WebGL.QuadFBO;
|
|
|
|
|
|
|
|
Phaser.Renderer.WebGL.QuadFBO.prototype = {
|
|
|
|
|
|
|
|
init: function ()
|
|
|
|
{
|
|
|
|
var gl = this.gl;
|
|
|
|
|
2016-11-03 05:10:50 +00:00
|
|
|
// An FBO quad is made up of 2 triangles (A and B in the image below)
|
|
|
|
//
|
2016-11-04 04:11:47 +00:00
|
|
|
// 0 = Bottom Left (-1, -1)
|
|
|
|
// 1 = Bottom Right (1, -1)
|
|
|
|
// 2 = Top Left (-1, 1)
|
|
|
|
// 3 = Top Right (1, 1)
|
2016-11-03 05:10:50 +00:00
|
|
|
//
|
|
|
|
// 2----3
|
|
|
|
// |\ B|
|
|
|
|
// | \ |
|
|
|
|
// | \ |
|
|
|
|
// | A \|
|
|
|
|
// | \
|
|
|
|
// 0----1
|
|
|
|
|
2016-11-04 04:11:47 +00:00
|
|
|
var width = this.renderer.width;
|
|
|
|
var height = this.renderer.height;
|
|
|
|
|
2016-11-03 17:19:16 +00:00
|
|
|
this.indexBuffer = gl.createBuffer();
|
2016-11-03 05:10:50 +00:00
|
|
|
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.indexBuffer);
|
2016-11-03 04:04:34 +00:00
|
|
|
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array([ 0, 1, 2, 2, 1, 3 ]), gl.STATIC_DRAW);
|
|
|
|
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, null);
|
|
|
|
|
|
|
|
this.vertices = new Float32Array(8);
|
|
|
|
|
|
|
|
this.updateVerts();
|
|
|
|
|
2016-11-03 17:19:16 +00:00
|
|
|
this.vertexBuffer = gl.createBuffer();
|
2016-11-03 04:22:07 +00:00
|
|
|
gl.bindBuffer(gl.ARRAY_BUFFER, this.vertexBuffer);
|
2016-11-03 04:04:34 +00:00
|
|
|
gl.bufferData(gl.ARRAY_BUFFER, this.vertices, gl.DYNAMIC_DRAW);
|
|
|
|
|
2016-11-03 17:19:16 +00:00
|
|
|
this.textureBuffer = gl.createBuffer();
|
2016-11-03 04:22:07 +00:00
|
|
|
gl.bindBuffer(gl.ARRAY_BUFFER, this.textureBuffer);
|
2016-11-03 04:04:34 +00:00
|
|
|
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([ 0, 0, 1, 0, 0, 1, 1, 1 ]), gl.STATIC_DRAW);
|
|
|
|
|
2016-11-03 17:19:16 +00:00
|
|
|
// Create a texture for our color buffer
|
2016-11-04 04:11:47 +00:00
|
|
|
this.texture = this.renderer.createEmptyTexture(width, height, 0, 0);
|
2016-11-03 17:19:16 +00:00
|
|
|
|
|
|
|
// The FBO's depth buffer
|
|
|
|
this.renderBuffer = gl.createRenderbuffer();
|
2016-11-03 04:22:07 +00:00
|
|
|
gl.bindRenderbuffer(gl.RENDERBUFFER, this.renderBuffer);
|
2016-11-04 04:11:47 +00:00
|
|
|
gl.renderbufferStorage(gl.RENDERBUFFER, gl.DEPTH_COMPONENT16, width, height);
|
2016-11-03 04:04:34 +00:00
|
|
|
|
2016-11-03 04:22:07 +00:00
|
|
|
this.frameBuffer = gl.createFramebuffer();
|
|
|
|
gl.bindFramebuffer(gl.FRAMEBUFFER, this.frameBuffer);
|
|
|
|
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, this.texture, 0);
|
2016-11-03 04:04:34 +00:00
|
|
|
|
2016-11-03 04:22:07 +00:00
|
|
|
// May need to optionally be: gl.DEPTH_STENCIL_ATTACHMENT
|
|
|
|
gl.framebufferRenderbuffer(gl.FRAMEBUFFER, gl.DEPTH_ATTACHMENT, gl.RENDERBUFFER, this.renderBuffer);
|
2016-11-03 04:04:34 +00:00
|
|
|
|
|
|
|
var fbStatus = gl.checkFramebufferStatus(gl.FRAMEBUFFER);
|
|
|
|
|
|
|
|
if (fbStatus !== gl.FRAMEBUFFER_COMPLETE)
|
|
|
|
{
|
|
|
|
window.console.error('FrameBuffer Error: ', this.renderer._fbErrors[fbStatus]);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.createShader();
|
|
|
|
},
|
|
|
|
|
|
|
|
// This whole function ought to be split out into the Shader Manager
|
|
|
|
// so they can easily change the shader being used for an FBO.
|
|
|
|
// This class will have to expose those shader attribs though.
|
|
|
|
createShader: function ()
|
|
|
|
{
|
|
|
|
// Create the quad shader
|
|
|
|
|
|
|
|
var gl = this.gl;
|
|
|
|
|
|
|
|
var vertexSrc = [
|
|
|
|
'attribute vec2 aVertexPosition;',
|
|
|
|
'attribute vec2 aTextureCoord;',
|
|
|
|
|
|
|
|
'varying vec2 vTextureCoord;',
|
|
|
|
|
|
|
|
'void main(void) {',
|
|
|
|
' vTextureCoord = aTextureCoord;',
|
|
|
|
' gl_Position = vec4(aVertexPosition, 0.0, 1.0);',
|
|
|
|
'}'
|
|
|
|
];
|
|
|
|
|
|
|
|
var fragmentSrc = [
|
|
|
|
'precision mediump float;',
|
|
|
|
|
|
|
|
'uniform sampler2D uSampler;',
|
|
|
|
'varying vec2 vTextureCoord;',
|
|
|
|
|
|
|
|
'void main(void) {',
|
|
|
|
' gl_FragColor = texture2D(uSampler, vTextureCoord);',
|
|
|
|
'}'
|
|
|
|
];
|
|
|
|
|
2016-11-03 04:22:07 +00:00
|
|
|
var twirlFragmentSrc = [
|
|
|
|
'precision mediump float;',
|
|
|
|
|
|
|
|
'uniform sampler2D uSampler;',
|
|
|
|
'varying vec2 vTextureCoord;',
|
|
|
|
|
|
|
|
'const float radius = 0.5;',
|
|
|
|
'const float angle = 5.0;',
|
|
|
|
'const vec2 offset = vec2(0.5, 0.5);',
|
|
|
|
|
|
|
|
'void main(void) {',
|
|
|
|
' vec2 coord = vTextureCoord - offset;',
|
|
|
|
' float distance = length(coord);',
|
|
|
|
|
|
|
|
' if (distance < radius) {',
|
|
|
|
' float ratio = (radius - distance) / radius;',
|
|
|
|
' float angleMod = ratio * ratio * angle;',
|
|
|
|
' float s = sin(angleMod);',
|
|
|
|
' float c = cos(angleMod);',
|
|
|
|
' coord = vec2(coord.x * c - coord.y * s, coord.x * s + coord.y * c);',
|
|
|
|
' }',
|
|
|
|
|
|
|
|
' gl_FragColor = texture2D(uSampler, coord + offset);',
|
|
|
|
'}'
|
|
|
|
];
|
|
|
|
|
2016-11-03 04:04:34 +00:00
|
|
|
// This compiles, attaches and links the shader
|
|
|
|
this.program = this.renderer.compileProgram(vertexSrc, fragmentSrc);
|
2016-11-03 22:25:55 +00:00
|
|
|
// this.program = this.renderer.compileProgram(vertexSrc, twirlFragmentSrc);
|
2016-11-03 04:04:34 +00:00
|
|
|
|
|
|
|
this.aVertexPosition = gl.getAttribLocation(this.program, 'aVertexPosition');
|
|
|
|
this.aTextureCoord = gl.getAttribLocation(this.program, 'aTextureCoord');
|
|
|
|
},
|
|
|
|
|
|
|
|
setPosition: function (x, y)
|
|
|
|
{
|
|
|
|
if (x === undefined) { x = 0; }
|
|
|
|
if (y === undefined) { y = 0; }
|
|
|
|
|
|
|
|
if (x !== this._x || y !== this._y)
|
|
|
|
{
|
|
|
|
this._x = x;
|
|
|
|
this._y = y;
|
|
|
|
|
|
|
|
this.updateVerts();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
setSize: function (width, height)
|
|
|
|
{
|
|
|
|
if (width === undefined) { width = this.renderer.width; }
|
|
|
|
if (height === undefined) { height = this.renderer.height; }
|
|
|
|
|
|
|
|
if (width !== this._width || height !== this._height)
|
|
|
|
{
|
|
|
|
this._width = width;
|
|
|
|
this._height = height;
|
|
|
|
|
|
|
|
this.updateVerts();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
updateVerts: function ()
|
|
|
|
{
|
|
|
|
var x = this._x;
|
|
|
|
var y = this._y;
|
|
|
|
|
|
|
|
var width = this._width;
|
|
|
|
var height = this._height;
|
|
|
|
|
|
|
|
// Bottom Left
|
|
|
|
this.vertices[0] = this.clipX(x);
|
|
|
|
this.vertices[1] = this.clipY(y + height);
|
|
|
|
|
|
|
|
// Bottom Right
|
|
|
|
this.vertices[2] = this.clipX(x + width);
|
|
|
|
this.vertices[3] = this.clipY(y + height);
|
|
|
|
|
|
|
|
// Top Left
|
|
|
|
this.vertices[4] = this.clipX(x);
|
|
|
|
this.vertices[5] = this.clipY(y);
|
|
|
|
|
|
|
|
// Top Right
|
|
|
|
this.vertices[6] = this.clipX(x + width);
|
|
|
|
this.vertices[7] = this.clipY(y);
|
|
|
|
},
|
|
|
|
|
|
|
|
activate: function ()
|
|
|
|
{
|
|
|
|
var gl = this.gl;
|
|
|
|
|
2016-11-03 04:22:07 +00:00
|
|
|
gl.bindFramebuffer(gl.FRAMEBUFFER, this.frameBuffer);
|
2016-11-03 17:19:16 +00:00
|
|
|
|
2016-11-03 21:10:53 +00:00
|
|
|
// FBO textures always use index zero
|
2016-11-03 17:19:16 +00:00
|
|
|
this.renderer.textureArray[0] = this.texture;
|
2016-11-03 04:04:34 +00:00
|
|
|
},
|
|
|
|
|
2016-11-03 17:19:16 +00:00
|
|
|
bindShader: function ()
|
2016-11-03 04:04:34 +00:00
|
|
|
{
|
2016-11-03 21:10:53 +00:00
|
|
|
var program = this.program;
|
2016-11-03 04:22:07 +00:00
|
|
|
|
2016-11-03 17:19:16 +00:00
|
|
|
var gl = this.gl;
|
2016-11-03 05:10:50 +00:00
|
|
|
|
2016-11-03 21:32:06 +00:00
|
|
|
gl.useProgram(program);
|
|
|
|
|
2016-11-03 04:04:34 +00:00
|
|
|
gl.uniform1i(gl.getUniformLocation(program, 'uSampler'), 0);
|
|
|
|
|
|
|
|
gl.enableVertexAttribArray(this.aTextureCoord);
|
|
|
|
gl.enableVertexAttribArray(this.aVertexPosition);
|
|
|
|
|
2016-11-03 17:19:16 +00:00
|
|
|
gl.bindBuffer(gl.ARRAY_BUFFER, this.vertexBuffer);
|
2016-11-03 04:04:34 +00:00
|
|
|
gl.bufferSubData(gl.ARRAY_BUFFER, 0, this.vertices);
|
|
|
|
gl.vertexAttribPointer(this.aVertexPosition, 2, gl.FLOAT, false, 0, 0);
|
|
|
|
|
2016-11-03 17:19:16 +00:00
|
|
|
gl.bindBuffer(gl.ARRAY_BUFFER, this.textureBuffer);
|
2016-11-03 04:04:34 +00:00
|
|
|
gl.vertexAttribPointer(this.aTextureCoord, 2, gl.FLOAT, false, 0, 0);
|
2016-11-03 17:19:16 +00:00
|
|
|
},
|
|
|
|
|
2016-11-03 17:25:14 +00:00
|
|
|
// destinationBuffer MUST be set, even if just to 'null'
|
2016-11-03 17:19:16 +00:00
|
|
|
render: function (destinationBuffer)
|
|
|
|
{
|
|
|
|
var gl = this.gl;
|
|
|
|
|
2016-11-03 21:13:48 +00:00
|
|
|
// Set the framebuffer to render to
|
2016-11-03 17:19:16 +00:00
|
|
|
gl.bindFramebuffer(gl.FRAMEBUFFER, destinationBuffer);
|
|
|
|
|
2016-11-03 21:10:53 +00:00
|
|
|
// Bind the texture we rendered to, for reading, always TEXTURE0
|
2016-11-03 17:19:16 +00:00
|
|
|
gl.activeTexture(gl.TEXTURE0);
|
|
|
|
gl.bindTexture(gl.TEXTURE_2D, this.texture);
|
|
|
|
|
|
|
|
// The shader that will read from the fbo texture
|
2016-11-03 21:10:53 +00:00
|
|
|
if (this.renderer.shaderManager.setShader(this.program))
|
2016-11-03 17:19:16 +00:00
|
|
|
{
|
|
|
|
this.bindShader();
|
|
|
|
}
|
|
|
|
|
|
|
|
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.indexBuffer);
|
2016-11-03 04:04:34 +00:00
|
|
|
|
|
|
|
gl.drawElements(gl.TRIANGLES, 6, gl.UNSIGNED_SHORT, 0);
|
|
|
|
|
2016-11-03 17:19:16 +00:00
|
|
|
this.renderer.drawCount++;
|
2016-11-03 04:04:34 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
destroy: function ()
|
|
|
|
{
|
|
|
|
|
|
|
|
// TODO!
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
Object.defineProperties(Phaser.Renderer.WebGL.QuadFBO.prototype, {
|
|
|
|
|
|
|
|
x: {
|
|
|
|
|
|
|
|
enumerable: true,
|
|
|
|
|
|
|
|
get: function ()
|
|
|
|
{
|
|
|
|
return this._x;
|
|
|
|
},
|
|
|
|
|
|
|
|
set: function (value)
|
|
|
|
{
|
|
|
|
if (value !== this._x)
|
|
|
|
{
|
|
|
|
this._x = value;
|
|
|
|
this.updateVerts();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
y: {
|
|
|
|
|
|
|
|
enumerable: true,
|
|
|
|
|
|
|
|
get: function ()
|
|
|
|
{
|
|
|
|
return this._y;
|
|
|
|
},
|
|
|
|
|
|
|
|
set: function (value)
|
|
|
|
{
|
|
|
|
if (value !== this._y)
|
|
|
|
{
|
|
|
|
this._y = value;
|
|
|
|
this.updateVerts();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
width: {
|
|
|
|
|
|
|
|
enumerable: true,
|
|
|
|
|
|
|
|
get: function ()
|
|
|
|
{
|
|
|
|
return this._width;
|
|
|
|
},
|
|
|
|
|
|
|
|
set: function (value)
|
|
|
|
{
|
|
|
|
if (value !== this._width)
|
|
|
|
{
|
|
|
|
this._width = value;
|
|
|
|
this.updateVerts();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
height: {
|
|
|
|
|
|
|
|
enumerable: true,
|
|
|
|
|
|
|
|
get: function ()
|
|
|
|
{
|
|
|
|
return this._height;
|
|
|
|
},
|
|
|
|
|
|
|
|
set: function (value)
|
|
|
|
{
|
|
|
|
if (value !== this._height)
|
|
|
|
{
|
|
|
|
this._height = value;
|
|
|
|
this.updateVerts();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|