2018-02-12 16:01:20 +00:00
|
|
|
/**
|
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
|
|
|
* @copyright 2018 Photon Storm Ltd.
|
|
|
|
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
|
|
|
|
*/
|
|
|
|
|
2018-01-25 18:43:19 +00:00
|
|
|
var Class = require('../../../utils/Class');
|
|
|
|
var ShaderSourceFS = require('../shaders/BitmapMask.frag');
|
2018-02-16 18:17:51 +00:00
|
|
|
var ShaderSourceVS = require('../shaders/BitmapMask.vert');
|
2018-02-09 19:19:21 +00:00
|
|
|
var WebGLPipeline = require('../WebGLPipeline');
|
2018-01-25 18:43:19 +00:00
|
|
|
|
2018-02-09 19:19:21 +00:00
|
|
|
/**
|
|
|
|
* @classdesc
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @class BitmapMaskPipeline
|
|
|
|
* @extends Phaser.Renderer.WebGL.WebGLPipeline
|
|
|
|
* @memberOf Phaser.Renderer.WebGL
|
|
|
|
* @constructor
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @param {Phaser.Game} game - [description]
|
2018-02-13 00:12:17 +00:00
|
|
|
* @param {WebGLRenderingContext} gl - [description]
|
2018-02-09 19:19:21 +00:00
|
|
|
* @param {Phaser.Renderer.WebGL.WebGLRenderer} renderer - [description]
|
|
|
|
*/
|
2018-01-25 18:43:19 +00:00
|
|
|
var BitmapMaskPipeline = new Class({
|
|
|
|
|
|
|
|
Extends: WebGLPipeline,
|
|
|
|
|
|
|
|
initialize:
|
|
|
|
|
2018-02-09 19:19:21 +00:00
|
|
|
function BitmapMaskPipeline (game, gl, renderer)
|
2018-01-25 18:43:19 +00:00
|
|
|
{
|
|
|
|
WebGLPipeline.call(this, {
|
|
|
|
game: game,
|
|
|
|
gl: gl,
|
|
|
|
renderer: renderer,
|
|
|
|
topology: gl.TRIANGLES,
|
|
|
|
vertShader: ShaderSourceVS,
|
|
|
|
fragShader: ShaderSourceFS,
|
|
|
|
vertexCapacity: 3,
|
|
|
|
|
2018-02-16 18:17:51 +00:00
|
|
|
vertexSize:
|
2018-01-25 18:43:19 +00:00
|
|
|
Float32Array.BYTES_PER_ELEMENT * 2,
|
|
|
|
|
|
|
|
vertices: new Float32Array([
|
|
|
|
-1, +1, -1, -7, +7, +1
|
|
|
|
]).buffer,
|
|
|
|
|
|
|
|
attributes: [
|
|
|
|
{
|
|
|
|
name: 'inPosition',
|
|
|
|
size: 2,
|
|
|
|
type: gl.FLOAT,
|
|
|
|
normalized: false,
|
|
|
|
offset: 0
|
|
|
|
}
|
|
|
|
]
|
|
|
|
});
|
|
|
|
|
2018-02-09 19:19:21 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @name Phaser.Renderer.WebGL.BitmapMaskPipeline#vertexViewF32
|
|
|
|
* @type {Float32Array}
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2018-01-25 18:43:19 +00:00
|
|
|
this.vertexViewF32 = new Float32Array(this.vertexData);
|
2018-02-09 19:19:21 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @name Phaser.Renderer.WebGL.BitmapMaskPipeline#maxQuads
|
|
|
|
* @type {number}
|
|
|
|
* @default 1
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2018-01-25 18:43:19 +00:00
|
|
|
this.maxQuads = 1;
|
2018-02-09 19:19:21 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @name Phaser.Renderer.WebGL.BitmapMaskPipeline#resolutionDirty
|
|
|
|
* @type {boolean}
|
|
|
|
* @default true
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2018-01-25 18:43:19 +00:00
|
|
|
this.resolutionDirty = true;
|
|
|
|
},
|
|
|
|
|
2018-02-09 19:19:21 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @method Phaser.Renderer.WebGL.BitmapMaskPipeline#onBind
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @return {Phaser.Renderer.WebGL.BitmapMaskPipeline} [description]
|
|
|
|
*/
|
2018-01-25 18:43:19 +00:00
|
|
|
onBind: function ()
|
|
|
|
{
|
|
|
|
WebGLPipeline.prototype.onBind.call(this);
|
|
|
|
|
|
|
|
var renderer = this.renderer;
|
2018-01-30 03:38:31 +00:00
|
|
|
var program = this.program;
|
2018-01-25 18:43:19 +00:00
|
|
|
|
2018-01-30 03:38:31 +00:00
|
|
|
if (this.resolutionDirty)
|
2018-01-25 18:43:19 +00:00
|
|
|
{
|
|
|
|
renderer.setFloat2(program, 'uResolution', this.width, this.height);
|
|
|
|
renderer.setInt1(program, 'uMainSampler', 0);
|
|
|
|
renderer.setInt1(program, 'uMaskSampler', 1);
|
|
|
|
this.resolutionDirty = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2018-02-09 19:19:21 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @method Phaser.Renderer.WebGL.BitmapMaskPipeline#resize
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @param {number} width - [description]
|
|
|
|
* @param {number} height - [description]
|
|
|
|
* @param {number} resolution - [description]
|
|
|
|
*
|
|
|
|
* @return {Phaser.Renderer.WebGL.BitmapMaskPipeline} [description]
|
|
|
|
*/
|
2018-01-25 18:43:19 +00:00
|
|
|
resize: function (width, height, resolution)
|
|
|
|
{
|
|
|
|
WebGLPipeline.prototype.resize.call(this, width, height, resolution);
|
|
|
|
this.resolutionDirty = true;
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2018-02-09 19:19:21 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @method Phaser.Renderer.WebGL.BitmapMaskPipeline#beginMask
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2018-02-13 00:12:17 +00:00
|
|
|
* @param {Phaser.GameObjects.GameObject} mask - [description]
|
|
|
|
* @param {Phaser.GameObjects.GameObject} maskedObject - [description]
|
2018-02-09 19:19:21 +00:00
|
|
|
* @param {Phaser.Cameras.Scene2D.Camera} camera - [description]
|
|
|
|
*/
|
2018-01-25 18:43:19 +00:00
|
|
|
beginMask: function (mask, maskedObject, camera)
|
|
|
|
{
|
|
|
|
var bitmapMask = mask.bitmapMask;
|
|
|
|
var renderer = this.renderer;
|
|
|
|
var gl = this.gl;
|
|
|
|
var visible = bitmapMask.visible;
|
|
|
|
|
|
|
|
if (bitmapMask && gl)
|
|
|
|
{
|
|
|
|
// First we clear the mask framebuffer
|
2018-02-14 19:45:22 +00:00
|
|
|
renderer.setFramebuffer(mask.maskFramebuffer);
|
2018-01-25 18:43:19 +00:00
|
|
|
gl.clearColor(0, 0, 0, 0);
|
|
|
|
gl.clear(gl.COLOR_BUFFER_BIT);
|
|
|
|
|
|
|
|
// We render out mask source
|
|
|
|
bitmapMask.visible = true;
|
|
|
|
bitmapMask.renderWebGL(renderer, bitmapMask, 0.0, camera);
|
|
|
|
bitmapMask.visible = visible;
|
|
|
|
renderer.flush();
|
|
|
|
|
|
|
|
// Bind and clear our main source (masked object)
|
2018-02-14 19:45:22 +00:00
|
|
|
renderer.setFramebuffer(mask.mainFramebuffer);
|
2018-01-25 18:43:19 +00:00
|
|
|
gl.clearColor(0, 0, 0, 0);
|
|
|
|
gl.clear(gl.COLOR_BUFFER_BIT);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-02-09 19:19:21 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @method Phaser.Renderer.WebGL.BitmapMaskPipeline#endMask
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2018-02-13 00:12:17 +00:00
|
|
|
* @param {Phaser.GameObjects.GameObject} mask - [description]
|
2018-02-09 19:19:21 +00:00
|
|
|
*/
|
2018-01-25 18:43:19 +00:00
|
|
|
endMask: function (mask)
|
|
|
|
{
|
|
|
|
var bitmapMask = mask.bitmapMask;
|
|
|
|
var renderer = this.renderer;
|
|
|
|
var gl = this.gl;
|
|
|
|
|
|
|
|
if (bitmapMask)
|
|
|
|
{
|
|
|
|
// Return to default framebuffer
|
2018-02-14 19:45:22 +00:00
|
|
|
renderer.setFramebuffer(null);
|
2018-01-25 18:43:19 +00:00
|
|
|
|
|
|
|
// Bind bitmap mask pipeline and draw
|
|
|
|
renderer.setPipeline(this);
|
2018-02-14 16:20:56 +00:00
|
|
|
|
2018-02-14 19:45:22 +00:00
|
|
|
renderer.setTexture2D(mask.maskTexture, 1);
|
|
|
|
renderer.setTexture2D(mask.mainTexture, 0);
|
2018-02-23 17:09:27 +00:00
|
|
|
renderer.setInt1(this.program, 'uInvertMaskAlpha', mask.invertAlpha);
|
|
|
|
|
2018-01-25 18:43:19 +00:00
|
|
|
// Finally draw a triangle filling the whole screen
|
|
|
|
gl.drawArrays(this.topology, 0, 3);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = BitmapMaskPipeline;
|