improved bloom system

This commit is contained in:
2018-09-03 19:31:16 -05:00
parent d6b39c8d73
commit 8fd8308d47
7 changed files with 96 additions and 53 deletions

View File

@@ -17,6 +17,6 @@ void main() {
if (brightness > 0.7) {
gl_FragColor = color;
} else {
gl_FragColor = vec4(0.0);
gl_FragColor = vec4(0.0, 0.0, 0.0, 0.0);
}
}

View File

@@ -1,34 +1,47 @@
#version 330 core
#ifdef GL_ES
#define LOWP lowp
precision mediump float;
#else
#define LOWP
#endif
varying LOWP vec4 vColor;
varying vec4 vColor;
varying vec2 vTexCoord;
uniform int horizontal;
uniform sampler2D u_texture;
uniform float weight[5] = float[] (0.227027, 0.1945946, 0.1216216, 0.054054, 0.016216);
uniform vec2 resolution;
uniform float radius;
uniform int pass;
void main() {
vec2 tex_offset = 1.0 / textureSize(u_texture, 0);
vec4 color = texture(u_texture, vTexCoord);
vec3 result = color.rgb * weight[0];
//this will be our RGBA sum
vec4 sum = vec4(0.0);
//our original texcoord for this fragment
vec2 tc = vTexCoord;
//the amount to blur, i.e. how far off center to sample from
//1.0 -> blur by one pixel
//2.0 -> blur by two pixels, etc.
float blur = radius/resolution.y;
//the direction of our blur
//(1.0, 0.0) -> x-axis blur
//(0.0, 1.0) -> y-axis blur
float hstep = 1.0;
float vstep = 0.0;
if (pass == 1) {
hstep = 0.0;
vstep = 1.0;
}
sum += texture2D(u_texture, vec2(tc.x - 5.0*blur*hstep, tc.y - 5.0*blur*vstep)) * 0.014374;
sum += texture2D(u_texture, vec2(tc.x - 4.0*blur*hstep, tc.y - 4.0*blur*vstep)) * 0.035855;
sum += texture2D(u_texture, vec2(tc.x - 3.0*blur*hstep, tc.y - 3.0*blur*vstep)) * 0.072994;
sum += texture2D(u_texture, vec2(tc.x - 2.0*blur*hstep, tc.y - 2.0*blur*vstep)) * 0.121281;
sum += texture2D(u_texture, vec2(tc.x - 1.0*blur*hstep, tc.y - 1.0*blur*vstep)) * 0.164472;
sum += texture2D(u_texture, vec2(tc.x, tc.y)) * 0.182049;
sum += texture2D(u_texture, vec2(tc.x + 1.0*blur*hstep, tc.y + 1.0*blur*vstep)) * 0.164472;
sum += texture2D(u_texture, vec2(tc.x + 2.0*blur*hstep, tc.y + 2.0*blur*vstep)) * 0.121281;
sum += texture2D(u_texture, vec2(tc.x + 3.0*blur*hstep, tc.y + 3.0*blur*vstep)) * 0.072994;
sum += texture2D(u_texture, vec2(tc.x + 4.0*blur*hstep, tc.y + 4.0*blur*vstep)) * 0.035855;
sum += texture2D(u_texture, vec2(tc.x + 5.0*blur*hstep, tc.y + 5.0*blur*vstep)) * 0.014374;
if (horizontal == 1) {
for (int i = 1; i < 5; ++i) {
result += texture(u_texture, vTexCoord + vec2(tex_offset.x * i, 0.0)).rgb * weight[i];
result += texture(u_texture, vTexCoord - vec2(tex_offset.x * i, 0.0)).rgb * weight[i];
}
} else {
for (int i = 1; i < 5; ++i) {
result += texture(u_texture, vTexCoord + vec2(0.0, tex_offset.y * i)).rgb * weight[i];
result += texture(u_texture, vTexCoord - vec2(0.0, tex_offset.y * i)).rgb * weight[i];
}
}
gl_FragColor = vec4(result, color.a);
gl_FragColor = vColor * vec4(sum.rgb, 1.0);
}

View File

@@ -0,0 +1,15 @@
attribute vec4 a_position;
attribute vec4 a_color;
attribute vec2 a_texCoord0;
uniform mat4 u_projTrans;
varying vec4 vColor;
varying vec2 vTexCoord;
void main() {
vColor = a_color;
vTexCoord = a_texCoord0;
gl_Position = u_projTrans * a_position;
}