Skip to content

Commit

Permalink
Changed blur filter to a multi-pass filter with Bokeh.
Browse files Browse the repository at this point in the history
  • Loading branch information
GeReV committed Feb 5, 2020
1 parent 4710980 commit d2e4469
Show file tree
Hide file tree
Showing 8 changed files with 573 additions and 154 deletions.
117 changes: 64 additions & 53 deletions assets/shaders/background.frag
Original file line number Diff line number Diff line change
@@ -1,53 +1,64 @@
#version 330 core

uniform sampler2D Texture;
uniform vec2 Resolution;

in VS_OUTPUT {
vec3 Position;
vec4 Color;
vec2 Uv;
} IN;

out vec4 Color;

float normpdf(in float x, in float sigma)
{
return 0.39894 * exp(-0.5 * x * x / (sigma * sigma)) / sigma;
}

void main()
{
//declare stuff
const int mSize = 13;
const int kSize = (mSize-1) / 2;

float kernel[mSize];
vec3 final_color = vec3(0.0);

//create the 1-D kernel
float sigma = 7.0;
float Z = 0.0;

for (int j = 0; j <= kSize; ++j)
{
kernel[kSize+j] = kernel[kSize-j] = normpdf(float(j), sigma);
}

//get the normalization factor (as the gaussian has been clamped)
for (int j = 0; j < mSize; ++j)
{
Z += kernel[j];
}

//read out the texels
for (int i=-kSize; i <= kSize; ++i)
{
for (int j=-kSize; j <= kSize; ++j)
{
final_color += kernel[kSize+j] * kernel[kSize+i] * texture(Texture, IN.Uv + vec2(float(i), float(j)) / Resolution).rgb;
}
}

Color = vec4(final_color / (Z*Z), 1.0);
}
#version 330 core

uniform sampler2D TextureR;
uniform sampler2D TextureG;
uniform sampler2D TextureB;
uniform vec2 Resolution;
uniform vec4[17] Kernel0;
uniform vec4[17] Kernel1;
uniform float FilterRadius;

in VS_OUTPUT {
vec3 Position;
vec4 Color;
vec2 Uv;
} IN;

out vec4 Color;

const int KERNEL_RADIUS = 8;

const vec2 Kernel0Weights_RealX_ImY = vec2(0.411259, -0.548794);
const vec2 Kernel1Weights_RealX_ImY = vec2(0.513282, 4.561110);

//(Pr+Pi)*(Qr+Qi) = (Pr*Qr+Pr*Qi+Pi*Qr-Pi*Qi)
vec2 multComplex(vec2 p, vec2 q)
{
return vec2(p.x*q.x-p.y*q.y, p.x*q.y+p.y*q.x);
}

void main()
{
vec2 stepVal = 1.0 / Resolution;

vec4 valR = vec4(0, 0, 0, 0);
vec4 valG = vec4(0, 0, 0, 0);
vec4 valB = vec4(0, 0, 0, 0);

for (int i=-KERNEL_RADIUS; i <=KERNEL_RADIUS; ++i)
{
vec2 coords = IN.Uv + stepVal * vec2(0.0, float(i)) * FilterRadius;
vec4 imageTexelR = texture(TextureR, coords);
vec4 imageTexelG = texture(TextureG, coords);
vec4 imageTexelB = texture(TextureB, coords);

int pixel = int(i + KERNEL_RADIUS);

vec4 c0_c1 = vec4(Kernel0[pixel].xy, Kernel1[pixel].xy);

valR.xy += multComplex(imageTexelR.xy, c0_c1.xy);
valR.zw += multComplex(imageTexelR.zw, c0_c1.zw);

valG.xy += multComplex(imageTexelG.xy, c0_c1.xy);
valG.zw += multComplex(imageTexelG.zw, c0_c1.zw);

valB.xy += multComplex(imageTexelB.xy, c0_c1.xy);
valB.zw += multComplex(imageTexelB.zw, c0_c1.zw);
}

float redChannel = dot(valR.xy, Kernel0Weights_RealX_ImY)+dot(valR.zw, Kernel1Weights_RealX_ImY);
float greenChannel = dot(valG.xy, Kernel0Weights_RealX_ImY)+dot(valG.zw, Kernel1Weights_RealX_ImY);
float blueChannel = dot(valB.xy, Kernel0Weights_RealX_ImY)+dot(valB.zw, Kernel1Weights_RealX_ImY);

Color = vec4(vec3(redChannel, greenChannel, blueChannel), 1.0);
}
40 changes: 40 additions & 0 deletions assets/shaders/background_pass_blue.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#version 330 core

uniform sampler2D Texture;
uniform vec2 Resolution;
uniform vec4[17] Kernel0;
uniform vec4[17] Kernel1;
uniform float FilterRadius;

in VS_OUTPUT {
vec3 Position;
vec4 Color;
vec2 Uv;
} IN;

out vec4 Color;

const int KERNEL_RADIUS = 8;

void main()
{
vec2 stepVal = 1.0 / Resolution;

vec4 val = vec4(0, 0, 0, 0);

for (int i=-KERNEL_RADIUS; i <=KERNEL_RADIUS; ++i)
{
vec2 coords = IN.Uv + stepVal * vec2(float(i), 0.0) * FilterRadius;

float imageTexelB = texture(Texture, coords).b;

int pixel = int(i + KERNEL_RADIUS);

vec4 c0_c1 = vec4(Kernel0[pixel].xy, Kernel1[pixel].xy);

val.xy += imageTexelB * c0_c1.xy;
val.zw += imageTexelB * c0_c1.zw;
}

Color = val;
}
40 changes: 40 additions & 0 deletions assets/shaders/background_pass_green.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#version 330 core

uniform sampler2D Texture;
uniform vec2 Resolution;
uniform vec4[17] Kernel0;
uniform vec4[17] Kernel1;
uniform float FilterRadius;

in VS_OUTPUT {
vec3 Position;
vec4 Color;
vec2 Uv;
} IN;

out vec4 Color;

const int KERNEL_RADIUS = 8;

void main()
{
vec2 stepVal = 1.0 / Resolution;

vec4 val = vec4(0, 0, 0, 0);

for (int i=-KERNEL_RADIUS; i <=KERNEL_RADIUS; ++i)
{
vec2 coords = IN.Uv + stepVal * vec2(float(i), 0.0) * FilterRadius;

float imageTexelG = texture(Texture, coords).g;

int pixel = int(i + KERNEL_RADIUS);

vec4 c0_c1 = vec4(Kernel0[pixel].xy, Kernel1[pixel].xy);

val.xy += imageTexelG * c0_c1.xy;
val.zw += imageTexelG * c0_c1.zw;
}

Color = val;
}
40 changes: 40 additions & 0 deletions assets/shaders/background_pass_red.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#version 330 core

uniform sampler2D Texture;
uniform vec2 Resolution;
uniform vec4[17] Kernel0;
uniform vec4[17] Kernel1;
uniform float FilterRadius;

in VS_OUTPUT {
vec3 Position;
vec4 Color;
vec2 Uv;
} IN;

out vec4 Color;

const int KERNEL_RADIUS = 8;

void main()
{
vec2 stepVal = 1.0 / Resolution;

vec4 val = vec4(0, 0, 0, 0);

for (int i=-KERNEL_RADIUS; i <=KERNEL_RADIUS; ++i)
{
vec2 coords = IN.Uv + stepVal * vec2(float(i), 0.0) * FilterRadius;

float imageTexelR = texture(Texture, coords).r;

int pixel = int(i + KERNEL_RADIUS);

vec4 c0_c1 = vec4(Kernel0[pixel].xy, Kernel1[pixel].xy);

val.xy += imageTexelR * c0_c1.xy;
val.zw += imageTexelR * c0_c1.zw;
}

Color = val;
}
Loading

0 comments on commit d2e4469

Please sign in to comment.