Update opengl_fragment.glsl : Adding new features !

Adding blur function "average". Works diagonal. Also some edgefinders. And focus.
ADDED FOCUS BLUR AND EDGE DARKENING.
TESTERS REQUIRED !
master
Lars Müller 2018-01-03 15:07:14 +01:00 committed by GitHub
parent 96fdbe2957
commit 80c4d22aa4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 102 additions and 1 deletions

View File

@ -1,4 +1,78 @@
uniform sampler2D baseTexture;
uniform sampler2D depthmap; /*Required, but currently just BLACK. C++ code needs to have a change.*/
uniform float resolution;
/*
Usage :
radius - Blur radius in px
step - Blur stepsize in px
*/
vec4 average(float radius, float step)
{
vec2 texcoords_pass = gl_TexCoord[0].st; /*I miss the texcoords*/
vec2 dirs[]=vec2[8] (vec2(1.0f/resolution,0.0f),vec2(-1.0f/resolution,0.0f),vec2(0.0f,1.0f/resolution),vec2(0.0f,-1.0f/resolution),vec2(1.0f/resolution,1.0f/resolution),vec2(-1.0f/resolution,-1.0f/resolution),vec2(-1.0f/resolution,1.0f/resolution),vec2(1.0f/resolution,-1.0f/resolution));
vec4 result=texture(baseTexture,texcoords_pass);
float sum=1.0f;
for (float i=0; i < radius; i+= step) {
for (int j=0; j < 8; j++) {
result+=texture(baseTexture,texcoords_pass+i*dirs[j]);
}
sum+=8;
}
return result/sum;
}
/*
Just for fun :
Edgefinder, depthmap based
Usage :
radius - Blur radius in px
step - Blur stepsize in px
tc - starting uv coordinates
*/
float edge(float radius, float step, vec2 tc)
{
float sd=texture(depthmap,tc).x;
if (sd==1.0f) {
return 0.0f;
}
float edge=1.0f;
for (float i=0; i < radius; i+=step) {
for (int j=0; j < 8; j++) {
float w=abs(1-abs(texture(depthmap, tc+i*dirs[j]).x-sd));
edge*=w;
}
}
return edge;
}
/*
Just for fun :
Edgefinder, texture based
Usage :
radius - Blur radius in px
step - Blur stepsize in px
tc - starting uv coordinates
*/
float edge_texture(float radius, float step, vec2 tc)
{
vec3 sd=texture(baseTexture,tc).xyz;
if (sd.x+sd.y+sd.z==0.0f) {
return 0.0f;
}
float edge=1.0f;
for (float i=0;i < radius; i+= step) {
for (int j=0; j < 8; j++) {
vec3 rgb=texture(baseTexture, tc+i*dirs[j]).xyz;
vec3 diff=round(vec3(1,1,1)-abs(rgb-sd)*vec3(4,4,4))/2.5f;
float w=diff.x*diff.y*diff.z;
edge*=w*w;
}
}
return sqrt(edge);
}
#if ENABLE_TONE_MAPPING
@ -30,6 +104,17 @@ vec4 applyToneMapping(vec4 color) {
}
#endif
#if ENABLE_FOCUS
const float focus_radius=3.0f;
const float focus_step=1.0f;
const float focus_start=0.2f;
#endif
#if ENABLE_TOON
const float toon_radius=3.0f;
const float toon_step=1.0f;
#endif
void main(void)
{
vec2 uv = gl_TexCoord[0].st;
@ -37,5 +122,21 @@ void main(void)
#if ENABLE_TONE_MAPPING
color = applyToneMapping(color);
#endif
gl_FragColor = color;
#if ENABLE_FOCUS
/*I assume the depth values are all linearized !*/
float depth_at_center=texture(depthmap,vec2(0.5f,0.5f)).x; /*Depth value at the center of the screen, the distance where the player is focusing at*/
float depth_here=texture(depthmap,uv).x; /*Depth value here*/
float difference=abs(depth_at_center-depth_here);
if (focus_start > 0.2f) {
color=vec4(average(difference*focus_radius,focus_step), color.w);
}
#endif
#if ENABLE_TOON
/*I assume the depth values are all linearized !*/
edge_tex=edge_texture(toon_radius, toon_step, uv);
edge_depth=edge(toon_radius, toon_step, uv);
edge=max(edge_tex, edge_depth);
color=vec4(color.x*(1-edge), color.y*(1-edge), color.z*(1-edge), color.w);
#endif
gl_FragColor = clamp(color, 0, 1); /*Safety : Clamping*/
}