optimized blurs
This commit is contained in:
+18
-14
@@ -6,24 +6,28 @@ uniform float radius;
|
||||
uniform vec2 resolution;
|
||||
uniform bool horiz_blur;
|
||||
uniform bool vert_blur;
|
||||
uniform int iteration;
|
||||
|
||||
varying vec2 vTexCoord;
|
||||
|
||||
void main(void) {
|
||||
float rad = ceil(radius);
|
||||
float x_rad = (horiz_blur) ? rad : 0.5;
|
||||
float y_rad = (vert_blur) ? rad : 0.5;
|
||||
vec2 texCoord = gl_FragCoord.xy/resolution;
|
||||
if (radius == 0.0 || (!horiz_blur && !vert_blur)) {
|
||||
gl_FragColor = texture2D(image, texCoord);
|
||||
} else {
|
||||
float divider = 1.0;
|
||||
if (horiz_blur) divider /= rad;
|
||||
if (vert_blur) divider /= rad;
|
||||
vec4 color = vec4(0.0);
|
||||
for (float x=-x_rad+0.5;x<=x_rad;x+=2.0) {
|
||||
for (float y=-y_rad+0.5;y<=y_rad;y+=2.0) {
|
||||
color += texture2D(image, (vec2(gl_FragCoord.x+x, gl_FragCoord.y+y))/resolution)*(divider);
|
||||
}
|
||||
|
||||
float divider = 1.0 / rad;
|
||||
vec4 color = vec4(0.0);
|
||||
bool radius_is_zero = (rad == 0.0);
|
||||
|
||||
if (iteration == 0 && horiz_blur && !radius_is_zero) {
|
||||
for (float x=-rad+0.5;x<=rad;x+=2.0) {
|
||||
color += texture2D(image, (vec2(gl_FragCoord.x+x, gl_FragCoord.y))/resolution)*(divider);
|
||||
}
|
||||
gl_FragColor = color;
|
||||
} else if (iteration == 1 && vert_blur && !radius_is_zero) {
|
||||
for (float x=-rad+0.5;x<=rad;x+=2.0) {
|
||||
color += texture2D(image, (vec2(gl_FragCoord.x, gl_FragCoord.y+x))/resolution)*(divider);
|
||||
}
|
||||
gl_FragColor = color;
|
||||
} else {
|
||||
gl_FragColor = texture2D(image, vTexCoord);
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -9,5 +9,5 @@
|
||||
<row name="Vertical">
|
||||
<field type="bool" default="1" id="vert_blur"/>
|
||||
</row>
|
||||
<shader vert="common.vert" frag="boxblur.frag"/>
|
||||
<shader vert="common.vert" frag="boxblur.frag" iterations="2"/>
|
||||
</effect>
|
||||
+26
-20
@@ -4,13 +4,14 @@
|
||||
|
||||
uniform sampler2D image;
|
||||
|
||||
uniform float radius;
|
||||
// uniform float radius;
|
||||
uniform float sigma;
|
||||
uniform vec2 resolution;
|
||||
uniform bool horiz_blur;
|
||||
uniform bool vert_blur;
|
||||
uniform int iteration;
|
||||
|
||||
uniform bool opt;
|
||||
varying vec2 vTexCoord;
|
||||
|
||||
float gaussian(float x, float sigma) {
|
||||
return (1.0/(sigma*sqrt(2.0*M_PI)))*exp(-0.5*pow(x/sigma, 2.0));
|
||||
@@ -21,28 +22,33 @@ float gaussian2(float x, float y, float sigma) {
|
||||
}
|
||||
|
||||
void main(void) {
|
||||
if (radius == 0.0 || sigma == 0.0 || (!horiz_blur && !vert_blur)) {
|
||||
gl_FragColor = texture2D(image, gl_FragCoord.xy/resolution);
|
||||
} else {
|
||||
float rad = ceil(radius);
|
||||
float x_rad = horiz_blur ? rad : 0.5;
|
||||
float y_rad = vert_blur ? rad : 0.5;
|
||||
float rad = ceil(sigma);
|
||||
|
||||
float sum = 0.0;
|
||||
float sum = 0.0;
|
||||
|
||||
for (float x=-x_rad+0.5;x<=x_rad;x+=2.0) {
|
||||
for (float y=-y_rad+0.5;y<=y_rad;y+=2.0) {
|
||||
sum += gaussian2(x, y, sigma);
|
||||
}
|
||||
vec4 color = vec4(0.0);
|
||||
|
||||
bool radius_is_zero = (rad == 0.0);
|
||||
|
||||
if (!radius_is_zero) {
|
||||
for (float x=-rad+0.5;x<=rad;x+=2.0) {
|
||||
sum += gaussian2(x, 0.0, sigma);
|
||||
}
|
||||
}
|
||||
|
||||
vec4 color = vec4(0.0);
|
||||
for (float x=-x_rad+0.5;x<=x_rad;x+=2.0) {
|
||||
for (float y=-y_rad+0.5;y<=y_rad;y+=2.0) {
|
||||
float weight = (gaussian2(x, y, sigma)/sum);
|
||||
color += texture2D(image, (vec2(gl_FragCoord.x+x, gl_FragCoord.y+y))/resolution)*(weight);
|
||||
}
|
||||
if (iteration == 0 && horiz_blur && !radius_is_zero) {
|
||||
for (float x=-rad+0.5;x<=rad;x+=2.0) {
|
||||
float weight = (gaussian2(x, 0.0, sigma)/sum);
|
||||
color += texture2D(image, (vec2(gl_FragCoord.x+x, gl_FragCoord.y))/resolution)*(weight);
|
||||
}
|
||||
gl_FragColor = color;
|
||||
}
|
||||
} else if (iteration == 1 && vert_blur && !radius_is_zero) {
|
||||
for (float x=-rad+0.5;x<=rad;x+=2.0) {
|
||||
float weight = (gaussian2(0.0, x, sigma)/sum);
|
||||
color += texture2D(image, (vec2(gl_FragCoord.x, gl_FragCoord.y+x))/resolution)*(weight);
|
||||
}
|
||||
gl_FragColor = color;
|
||||
} else {
|
||||
gl_FragColor = texture2D(image, vTexCoord);
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<effect name="Gaussian Blur" category="Blur">
|
||||
<row name="Radius">
|
||||
<!-- <row name="Radius">
|
||||
<field type="double" min="0" default="10" id="radius"/>
|
||||
</row>
|
||||
</row> -->
|
||||
<row name="Sigma">
|
||||
<field type="double" min="0" default="5.5" id="sigma"/>
|
||||
</row>
|
||||
@@ -12,8 +12,5 @@
|
||||
<row name="Vertical">
|
||||
<field type="bool" default="1" id="vert_blur"/>
|
||||
</row>
|
||||
<!-- <row name="Opt">
|
||||
<field type="bool" default="0" id="opt"/>
|
||||
</row> -->
|
||||
<shader vert="common.vert" frag="gaussianblur.frag"/>
|
||||
<shader vert="common.vert" frag="gaussianblur.frag" iterations="2"/>
|
||||
</effect>
|
||||
Reference in New Issue
Block a user