From 0468b0619a4077ce5e940e9d8c67bc2d3914c7af Mon Sep 17 00:00:00 2001 From: itsmattkc Date: Fri, 16 Nov 2018 13:22:24 +1100 Subject: [PATCH] optimized blur and added effects --- effects/boxblur.frag | 2 -- effects/boxblur.xml | 3 -- effects/chromaticaberration.frag | 22 ++++++++++++ effects/chromaticaberration.xml | 13 +++++++ effects/crossstitch.frag | 44 +++++++++++++++++++++++ effects/crossstitch.xml | 10 ++++++ effects/directionalblur.frag | 23 ++++++------ effects/gaussianblur.frag | 61 ++++++++------------------------ effects/gaussianblur.xml | 3 ++ effects/radialblur.frag | 32 ++++++++--------- effects/vignette.frag | 27 ++++++++++++++ effects/vignette.xml | 13 +++++++ mainwindow.cpp | 4 +-- 13 files changed, 176 insertions(+), 81 deletions(-) create mode 100644 effects/chromaticaberration.frag create mode 100644 effects/chromaticaberration.xml create mode 100644 effects/crossstitch.frag create mode 100644 effects/crossstitch.xml create mode 100644 effects/vignette.frag create mode 100644 effects/vignette.xml diff --git a/effects/boxblur.frag b/effects/boxblur.frag index 4724fd74b..ee61b6fb7 100644 --- a/effects/boxblur.frag +++ b/effects/boxblur.frag @@ -9,8 +9,6 @@ uniform vec2 resolution; uniform bool horiz_blur; uniform bool vert_blur; -uniform bool opt; - void main(void) { float rad = floor(radius); float x_rad = (horiz_blur) ? rad : 0.5; diff --git a/effects/boxblur.xml b/effects/boxblur.xml index 2f5c04f60..1dd4cb45e 100644 --- a/effects/boxblur.xml +++ b/effects/boxblur.xml @@ -9,8 +9,5 @@ - - - \ No newline at end of file diff --git a/effects/chromaticaberration.frag b/effects/chromaticaberration.frag new file mode 100644 index 000000000..ff9ddfa96 --- /dev/null +++ b/effects/chromaticaberration.frag @@ -0,0 +1,22 @@ +#version 110 + +uniform vec2 resolution; + +uniform sampler2D tex; +varying vec2 vTexCoord; + +uniform float red_amount; +uniform float green_amount; +uniform float blue_amount; + +void main(void) { + vec2 rOffset = vec2(red_amount*0.01)/resolution; + vec2 gOffset = vec2(green_amount*0.01)/resolution; + vec2 bOffset = vec2(blue_amount*0.01)/resolution; + + vec4 rValue = texture2D(tex, vTexCoord - rOffset); + vec4 gValue = texture2D(tex, vTexCoord - gOffset); + vec4 bValue = texture2D(tex, vTexCoord - bOffset); + + gl_FragColor = vec4(rValue.r, gValue.g, bValue.b, texture2D(tex, vTexCoord).a); +} \ No newline at end of file diff --git a/effects/chromaticaberration.xml b/effects/chromaticaberration.xml new file mode 100644 index 000000000..0f48f123c --- /dev/null +++ b/effects/chromaticaberration.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/effects/crossstitch.frag b/effects/crossstitch.frag new file mode 100644 index 000000000..a7e6df237 --- /dev/null +++ b/effects/crossstitch.frag @@ -0,0 +1,44 @@ +#version 110 + +uniform sampler2D tex0; +uniform float time; +uniform vec2 resolution; +uniform float stitching_size; +uniform bool invert; + +varying vec2 vTexCoord; + +vec4 PostFX(sampler2D tex, vec2 uv, float time) { + vec4 c = vec4(0.0); + float size = stitching_size; + vec2 cPos = uv * resolution; + vec2 tlPos = floor(cPos / vec2(size, size)); + tlPos *= size; + int remX = int(mod(cPos.x, size)); + int remY = int(mod(cPos.y, size)); + if (remX == 0 && remY == 0) + tlPos = cPos; + vec2 blPos = tlPos; + blPos.y += (size - 1.0); + if ((remX == remY) || + (((int(cPos.x) - int(blPos.x)) == (int(blPos.y) - int(cPos.y))))) + { + if (!invert) + c = vec4(0.2, 0.15, 0.05, 1.0); + else + c = texture2D(tex, tlPos * vec2(1.0/resolution.x, 1.0/resolution.y)) * 1.4; + } + else + { + if (!invert) + c = texture2D(tex, tlPos * vec2(1.0/resolution.x, 1.0/resolution.y)) * 1.4; + else + c = vec4(0.0, 0.0, 0.0, 1.0); + } + return c; +} + +void main(void) { + vec2 uv = vTexCoord; + gl_FragColor = PostFX(tex0, uv, time); +} \ No newline at end of file diff --git a/effects/crossstitch.xml b/effects/crossstitch.xml new file mode 100644 index 000000000..e31158bcc --- /dev/null +++ b/effects/crossstitch.xml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/effects/directionalblur.frag b/effects/directionalblur.frag index 4a5f24a18..13bee421c 100644 --- a/effects/directionalblur.frag +++ b/effects/directionalblur.frag @@ -10,17 +10,18 @@ uniform float length; uniform vec2 resolution; void main(void) { - float radians = (angle*M_PI)/180.0; - float divider = 1.0 / ((length*2.0)+1.0); - float sin_angle = sin(radians); - float cos_angle = cos(radians); + if (length > 0.0) { + float radians = (angle*M_PI)/180.0; + float divider = 1.0 / length; + float sin_angle = sin(radians); + float cos_angle = cos(radians); - gl_FragColor = texture2D(image, gl_FragCoord.xy/resolution)*(divider); - for (float i=1.0;i<=length;i++) { - float y = sin_angle * i; - float x = cos_angle * i; - - gl_FragColor += texture2D(image, vec2(gl_FragCoord.x-x, gl_FragCoord.y-y)/resolution)*(divider); - gl_FragColor += texture2D(image, vec2(gl_FragCoord.x+x, gl_FragCoord.y+y)/resolution)*(divider); + for (float i=-length+0.5;i<=length;i+=2.0) { + float y = sin_angle * i; + float x = cos_angle * i; + gl_FragColor += texture2D(image, vec2(gl_FragCoord.x+x, gl_FragCoord.y+y)/resolution)*(divider); + } + } else { + gl_FragColor = texture2D(image, gl_FragCoord.xy/resolution); } } \ No newline at end of file diff --git a/effects/gaussianblur.frag b/effects/gaussianblur.frag index 6da8a136a..ac90c0563 100644 --- a/effects/gaussianblur.frag +++ b/effects/gaussianblur.frag @@ -10,6 +10,8 @@ uniform vec2 resolution; uniform bool horiz_blur; uniform bool vert_blur; +uniform bool opt; + float gaussian(float x, float sigma) { return (1.0/(sigma*sqrt(2.0*M_PI)))*exp(-0.5*pow(x/sigma, 2.0)); } @@ -22,56 +24,23 @@ void main(void) { if (radius == 0.0 || sigma == 0.0 || (!horiz_blur && !vert_blur)) { gl_FragColor = texture2D(image, gl_FragCoord.xy/resolution); } else { - float middleWeight = gaussian(0.0, sigma); - float sum = middleWeight; + float rad = floor(radius); + float x_rad = horiz_blur ? rad : 0.5; + float y_rad = vert_blur ? rad : 0.5; - if (horiz_blur && vert_blur) { - for (float i=1.0;i<=radius;i++) { - sum += (4.0*gaussian2(i, 0.0, sigma)); - } + float sum = 0.0; - for (float x=1.0;x<=radius;x++) { - for (float y=1.0;y<=radius;y++) { - sum += (4.0*gaussian2(x, y, sigma)); - } + 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); } - - gl_FragColor = texture2D(image, gl_FragCoord.xy/resolution)*(middleWeight/sum); + } - for (float i=1.0;i<=radius;i++) { - float weight = (gaussian2(i, 0.0, sigma)/sum); - gl_FragColor += texture2D(image, (vec2(gl_FragCoord.x-i, gl_FragCoord.y))/resolution)*(weight); - gl_FragColor += texture2D(image, (vec2(gl_FragCoord.x+i, gl_FragCoord.y))/resolution)*(weight); - gl_FragColor += texture2D(image, (vec2(gl_FragCoord.x, gl_FragCoord.y+i))/resolution)*(weight); - gl_FragColor += texture2D(image, (vec2(gl_FragCoord.x, gl_FragCoord.y-i))/resolution)*(weight); + 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); + gl_FragColor += texture2D(image, (vec2(gl_FragCoord.x+x, gl_FragCoord.y+y))/resolution)*(weight); } - - for (float x=1.0;x<=radius;x++) { - for (float y=1.0;y<=radius;y++) { - float weight = (gaussian2(x, y, sigma)/sum); - gl_FragColor += texture2D(image, (vec2(gl_FragCoord.x-x, gl_FragCoord.y-y))/resolution)*(weight); - gl_FragColor += texture2D(image, (vec2(gl_FragCoord.x+x, gl_FragCoord.y+y))/resolution)*(weight); - gl_FragColor += texture2D(image, (vec2(gl_FragCoord.x-x, gl_FragCoord.y+y))/resolution)*(weight); - gl_FragColor += texture2D(image, (vec2(gl_FragCoord.x+x, gl_FragCoord.y-y))/resolution)*(weight); - } - } - } else { - for (float i=1.0;i<=radius;i++) { - sum += (2.0*gaussian(i, sigma)); - } - - gl_FragColor = texture2D(image, gl_FragCoord.xy/resolution)*(middleWeight/sum); - - for (float i=1.0;i<=radius;i++) { - float weight = gaussian(i, sigma)/sum; - if (horiz_blur) { - gl_FragColor += texture2D(image, (vec2(gl_FragCoord.x-i, gl_FragCoord.y))/resolution)*(weight); - gl_FragColor += texture2D(image, (vec2(gl_FragCoord.x+i, gl_FragCoord.y))/resolution)*(weight); - } else { - gl_FragColor += texture2D(image, (vec2(gl_FragCoord.x, gl_FragCoord.y-i))/resolution)*(weight); - gl_FragColor += texture2D(image, (vec2(gl_FragCoord.x, gl_FragCoord.y+i))/resolution)*(weight); - } - } - } + } } } \ No newline at end of file diff --git a/effects/gaussianblur.xml b/effects/gaussianblur.xml index 0b35d47d2..76a6bc82a 100644 --- a/effects/gaussianblur.xml +++ b/effects/gaussianblur.xml @@ -12,5 +12,8 @@ + \ No newline at end of file diff --git a/effects/radialblur.frag b/effects/radialblur.frag index b857d3258..2101480f2 100644 --- a/effects/radialblur.frag +++ b/effects/radialblur.frag @@ -10,26 +10,24 @@ uniform float center_y; uniform vec2 resolution; void main(void) { - vec2 distance = vec2((gl_FragCoord.x - (resolution.x/2.0) - center_x), (gl_FragCoord.y - (resolution.y/2.0) - center_y)); + if (radius > 0.0) { + vec2 distance = vec2((gl_FragCoord.x - (resolution.x/2.0) - center_x), (gl_FragCoord.y - (resolution.y/2.0) - center_y)); - float angle = atan(distance.y/distance.x); - float sin_angle = sin(angle); - float cos_angle = cos(angle); + float angle = atan(distance.y/distance.x); + float sin_angle = sin(angle); + float cos_angle = cos(angle); - //float multiplier = pow(length((2.0 * (distance / resolution)) - 1.0), 2.0); - float multiplier = length(distance/resolution); + float multiplier = length(distance/resolution); - float limit = radius * multiplier; + float limit = ceil(radius * multiplier); - float divider = 1.0 / ((floor(limit)*2.0)+1.0); - - gl_FragColor = texture2D(image, gl_FragCoord.xy/resolution)*(divider); - - for (float i=1.0;i<=limit;i++) { - float y = sin_angle * i; - float x = cos_angle * i; - - gl_FragColor += texture2D(image, vec2(gl_FragCoord.x-x, gl_FragCoord.y-y)/resolution)*(divider); - gl_FragColor += texture2D(image, vec2(gl_FragCoord.x+x, gl_FragCoord.y+y)/resolution)*(divider); + float divider = 1.0 / limit; + for (float i=-limit+0.5;i<=limit;i+=2.0) { + float y = sin_angle * i; + float x = cos_angle * i; + gl_FragColor += texture2D(image, vec2(gl_FragCoord.x+x, gl_FragCoord.y+y)/resolution)*(divider); + } + } else { + gl_FragColor = texture2D(image, gl_FragCoord.xy/resolution); } } \ No newline at end of file diff --git a/effects/vignette.frag b/effects/vignette.frag new file mode 100644 index 000000000..df200f234 --- /dev/null +++ b/effects/vignette.frag @@ -0,0 +1,27 @@ +#version 110 + +uniform sampler2D sceneTex; // 0 +uniform float lensRadiusX; +uniform float lensRadiusY; +uniform bool circular; +uniform vec2 resolution; +// uniform vec2 lensRadius; // 0.45, 0.38 + +varying vec2 vTexCoord; + +void main(void) { + if (lensRadiusX == 0.0) { + discard; + } + vec4 c = texture2D(sceneTex, vTexCoord); + vec2 vignetteCoord = vTexCoord; + if (circular) { + float ar = (resolution.x/resolution.y); + vignetteCoord.x *= ar; + vignetteCoord.x -= (1.0-(1.0/ar)); + } + float dist = distance(vignetteCoord, vec2(0.5,0.5)); + float size = (lensRadiusX*0.01); + c *= smoothstep(size, size*0.99*(1.0-lensRadiusY*0.01), dist); + gl_FragColor = c; +} \ No newline at end of file diff --git a/effects/vignette.xml b/effects/vignette.xml new file mode 100644 index 000000000..5156a84c4 --- /dev/null +++ b/effects/vignette.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/mainwindow.cpp b/mainwindow.cpp index e18a459f5..1547cf736 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -59,7 +59,7 @@ void MainWindow::setup_layout(bool reset) { bool load_default = true; - if (!reset) { + /*if (!reset) { QFile panel_config(get_data_path() + "/layout"); if (panel_config.exists() && panel_config.open(QFile::ReadOnly)) { if (restoreState(panel_config.readAll(), 0)) { @@ -67,7 +67,7 @@ void MainWindow::setup_layout(bool reset) { } panel_config.close(); } - } + }*/ if (load_default) { addDockWidget(Qt::TopDockWidgetArea, panel_project);