2014-06-24 07:20:07 +00:00
|
|
|
/**
|
2014-07-01 15:26:23 +00:00
|
|
|
* Original author of PixelateFilter: Mat Groves http://matgroves.com/ @Doormat23
|
|
|
|
* adapted for Phaser.js
|
|
|
|
*/
|
2014-06-24 07:20:07 +00:00
|
|
|
|
|
|
|
/**
|
2015-05-19 15:09:00 +00:00
|
|
|
* This filter applies a pixelate effect making display objects appear 'blocky'.
|
2014-07-01 15:26:23 +00:00
|
|
|
* @class PixelateFilter
|
2015-05-19 15:09:00 +00:00
|
|
|
* @constructor
|
2014-07-01 15:26:23 +00:00
|
|
|
*/
|
|
|
|
Phaser.Filter.Pixelate = function(game) {
|
|
|
|
|
2014-06-24 07:20:07 +00:00
|
|
|
Phaser.Filter.call(this, game);
|
|
|
|
|
2014-07-01 15:26:23 +00:00
|
|
|
this.uniforms.invert = { type: '1f', value: 0 };
|
2015-05-19 15:09:00 +00:00
|
|
|
this.uniforms.pixelSize = { type: '2f', value: { x: 1.0, y: 1.0 } };
|
|
|
|
this.uniforms.dimensions = { type: '2f', value: { x: 1000.0, y: 1000.0 } };
|
2014-06-24 07:20:07 +00:00
|
|
|
|
|
|
|
this.fragmentSrc = [
|
2014-07-01 15:26:23 +00:00
|
|
|
|
|
|
|
"precision mediump float;",
|
|
|
|
"varying vec2 vTextureCoord;",
|
2015-05-19 15:09:00 +00:00
|
|
|
"uniform vec2 dimensions;",
|
2014-07-01 15:26:23 +00:00
|
|
|
"uniform vec2 pixelSize;",
|
|
|
|
"uniform sampler2D uSampler;",
|
|
|
|
|
|
|
|
"void main(void)",
|
|
|
|
"{",
|
|
|
|
|
|
|
|
"vec2 coord = vTextureCoord;",
|
|
|
|
"vec2 size = dimensions.xy/pixelSize;",
|
|
|
|
"vec2 color = floor( ( vTextureCoord * size ) ) / size + pixelSize/dimensions.xy * 0.5;",
|
|
|
|
"gl_FragColor = texture2D(uSampler, color);",
|
|
|
|
"}"
|
2014-06-24 07:20:07 +00:00
|
|
|
];
|
|
|
|
};
|
|
|
|
|
|
|
|
Phaser.Filter.Pixelate.prototype = Object.create(Phaser.Filter.prototype);
|
|
|
|
Phaser.Filter.Pixelate.prototype.constructor = Phaser.Filter.Pixelate;
|
|
|
|
|
|
|
|
/**
|
2015-05-19 15:09:00 +00:00
|
|
|
* An object with visible x and y properties that are used to define the size of the filter effect per pixel.
|
|
|
|
*
|
|
|
|
* @property size
|
|
|
|
* @type Phaser.Point
|
|
|
|
*/
|
|
|
|
Object.defineProperty(Phaser.Filter.Pixelate.prototype, 'size', {
|
2014-07-01 15:26:23 +00:00
|
|
|
|
2014-06-24 07:20:07 +00:00
|
|
|
get: function() {
|
2014-07-01 15:26:23 +00:00
|
|
|
|
2014-06-24 07:20:07 +00:00
|
|
|
return this.uniforms.pixelSize.value;
|
2014-07-01 15:26:23 +00:00
|
|
|
|
2014-06-24 07:20:07 +00:00
|
|
|
},
|
2014-07-01 15:26:23 +00:00
|
|
|
|
2014-06-24 07:20:07 +00:00
|
|
|
set: function(value) {
|
2014-07-01 15:26:23 +00:00
|
|
|
|
2014-06-24 07:20:07 +00:00
|
|
|
this.dirty = true;
|
|
|
|
this.uniforms.pixelSize.value = value;
|
2014-07-01 15:26:23 +00:00
|
|
|
|
2014-06-24 07:20:07 +00:00
|
|
|
}
|
|
|
|
|
2014-07-01 15:26:23 +00:00
|
|
|
});
|
2015-05-19 15:09:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A value that defines the horizontal size of the filter effect per pixel.
|
|
|
|
*
|
|
|
|
* @property sizeX
|
|
|
|
* @type number
|
|
|
|
*/
|
|
|
|
Object.defineProperty(Phaser.Filter.Pixelate.prototype, 'sizeX', {
|
|
|
|
|
|
|
|
get: function() {
|
|
|
|
|
|
|
|
return this.uniforms.pixelSize.value.x;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
set: function(value) {
|
|
|
|
|
|
|
|
this.dirty = true;
|
|
|
|
this.uniforms.pixelSize.value.x = value;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A value that defines the vertical size of the filter effect per pixel.
|
|
|
|
*
|
|
|
|
* @property sizeY
|
|
|
|
* @type number
|
|
|
|
*/
|
|
|
|
Object.defineProperty(Phaser.Filter.Pixelate.prototype, 'sizeY', {
|
|
|
|
|
|
|
|
get: function() {
|
|
|
|
|
|
|
|
return this.uniforms.pixelSize.value.y;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
set: function(value) {
|
|
|
|
|
|
|
|
this.dirty = true;
|
|
|
|
this.uniforms.pixelSize.value.y = value;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|