Skip to content

Instantly share code, notes, and snippets.

@robojiannis
Created February 28, 2018 05:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robojiannis/bd31ab833aec776791f9bcae62e123b8 to your computer and use it in GitHub Desktop.
Save robojiannis/bd31ab833aec776791f9bcae62e123b8 to your computer and use it in GitHub Desktop.
Phaser.FIlter.Zoomblur
Phaser.Filter.ZoomBlur = function (game) {
Phaser.Filter.call(this, game);
this.uniforms.center = {type: '2f', value:{x:0, y:0}};
this.uniforms.strength = {type: '1f', value:0.1};
this.uniforms.innerRadius = {type: '1f', value:0};
this.uniforms.radius = {type: '1f', value:1};
//this.uniforms.blur = {type: '1f', value:1};
//this.uniforms.filterArea = {type: '4f', value:{x:0, y:0, width:1280,height:720}};
this.fragmentSrc = [
"precision mediump float;",
"varying vec2 vTextureCoord;",
"uniform sampler2D uSampler;",
"uniform vec4 filterArea;",
"uniform vec2 center;",
"uniform float strength;",
"uniform float innerRadius;",
"uniform float radius;",
"const float MAX_KERNEL_SIZE = 32.0;",
"float random(vec3 scale, float seed) {",
"return fract(sin(dot(gl_FragCoord.xyz + seed, scale)) * 43758.5453 + seed);",
"}",
"void main() {",
"float minGradient = innerRadius * 0.3;",
"float innerRadius = (innerRadius + minGradient * 0.5) / filterArea.x;",
"float gradient = radius * 0.3;",
"float radius = (radius - gradient * 0.5) / filterArea.x;",
"float countLimit = MAX_KERNEL_SIZE;",
"vec2 dir = vec2(center.xy / filterArea.xy - vTextureCoord);",
"float dist = length(vec2(dir.x, dir.y * filterArea.y / filterArea.x));",
"float strength = strength;",
"float delta = 0.0;",
"float gap;",
"if (dist < innerRadius) {",
"delta = innerRadius - dist;",
"gap = minGradient;",
"} else if (radius >= 0.0 && dist > radius) {",
"delta = dist - radius;",
"gap = gradient;",
"}",
"if (delta > 0.0) {",
"float normalCount = gap / filterArea.x;",
"delta = (normalCount - delta) / normalCount;",
"countLimit *= delta;",
"strength *= delta;",
"if (countLimit < 1.0)",
"{",
"gl_FragColor = texture2D(uSampler, vTextureCoord);",
"return;",
"}",
"}",
"float offset = random(vec3(12.9898, 78.233, 151.7182), 0.0);",
"float total = 0.0;",
"vec4 color = vec4(0.0);",
"dir *= strength;",
"for (float t = 0.0; t < MAX_KERNEL_SIZE; t++) {",
"float percent = (t + offset) / MAX_KERNEL_SIZE;",
"float weight = 4.0 * (percent - percent * percent);",
"vec2 p = vTextureCoord + dir * percent;",
"vec4 sample = texture2D(uSampler, p);",
"color += sample * weight;",
"total += weight;",
"if (t > countLimit){",
"break;",
"}",
"}",
"color /= total;",
"color.rgb /= color.a + 0.00001;",
"gl_FragColor = color;",
"}"
]
/*
this.fragmentSrc = [
"precision mediump float;",
"varying vec2 vTextureCoord;",
"varying vec4 vColor;",
"uniform float blur;",
"uniform sampler2D uSampler;",
"void main(void) {",
"vec4 sum = vec4(0.0);",
"sum += texture2D(uSampler, vec2(vTextureCoord.x - 4.0*blur, vTextureCoord.y)) * 0.05;",
"sum += texture2D(uSampler, vec2(vTextureCoord.x - 3.0*blur, vTextureCoord.y)) * 0.09;",
"sum += texture2D(uSampler, vec2(vTextureCoord.x - 2.0*blur, vTextureCoord.y)) * 0.12;",
"sum += texture2D(uSampler, vec2(vTextureCoord.x - blur, vTextureCoord.y)) * 0.15;",
"sum += texture2D(uSampler, vec2(vTextureCoord.x, vTextureCoord.y)) * 0.16;",
"sum += texture2D(uSampler, vec2(vTextureCoord.x + blur, vTextureCoord.y)) * 0.15;",
"sum += texture2D(uSampler, vec2(vTextureCoord.x + 2.0*blur, vTextureCoord.y)) * 0.12;",
"sum += texture2D(uSampler, vec2(vTextureCoord.x + 3.0*blur, vTextureCoord.y)) * 0.09;",
"sum += texture2D(uSampler, vec2(vTextureCoord.x + 4.0*blur, vTextureCoord.y)) * 0.05;",
"gl_FragColor = sum;",
"}"
];
*/
};
Phaser.Filter.ZoomBlur.prototype = Object.create(Phaser.Filter.prototype);
Phaser.Filter.ZoomBlur.prototype.constructor = Phaser.Filter.ZoomBlur;
Object.defineProperty(Phaser.Filter.ZoomBlur.prototype, 'strength', {
get: function() {
return this.uniforms.strength.value;
},
set: function(value) {
this.dirty = true;
this.uniforms.strength.value = value;
}
});
Object.defineProperty(Phaser.Filter.ZoomBlur.prototype, 'center', {
get: function() {
return this.uniforms.center.value;
},
set: function(value) {
this.dirty = true;
this.uniforms.center.value = value;
}
});
Object.defineProperty(Phaser.Filter.ZoomBlur.prototype, 'innerRadius', {
get: function() {
return this.uniforms.innerRadius.value;
},
set: function(value) {
this.dirty = true;
this.uniforms.innerRadius.value = value;
}
});
Object.defineProperty(Phaser.Filter.ZoomBlur.prototype, 'radius', {
get: function() {
return this.uniforms.radius.value;
},
set: function(value) {
this.dirty = true;
this.uniforms.radius.value = value;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment