stk-code_catmod/data/shaders/sunlightshadow.frag

97 lines
2.5 KiB
GLSL
Raw Normal View History

uniform sampler2D ntex;
uniform sampler2D dtex;
uniform sampler2DArray shadowtex;
2014-10-27 16:19:03 -04:00
uniform float split0;
uniform float split1;
uniform float split2;
uniform float splitmax;
uniform vec3 direction;
uniform vec3 col;
in vec2 uv;
out vec4 Diff;
out vec4 Spec;
vec3 DecodeNormal(vec2 n);
2014-04-06 17:57:54 -04:00
vec3 getSpecular(vec3 normal, vec3 eyedir, vec3 lightdir, vec3 color, float roughness);
vec4 getPosFromUVDepth(vec3 uvDepth, mat4 InverseProjectionMatrix);
float getShadowFactor(vec3 pos, float bias, int index)
2014-02-10 08:20:09 -05:00
{
2014-10-27 16:19:03 -04:00
2014-03-21 16:51:23 -04:00
vec2 shadowoffset[4] = vec2[](
vec2(-1., -1.),
vec2(-1., 1.),
vec2(1., -1.),
vec2(1., 1.)
2014-02-17 16:36:00 -05:00
);
2014-04-19 18:04:49 -04:00
vec4 shadowcoord = (ShadowViewProjMatrixes[index] * InverseViewMatrix * vec4(pos, 1.0));
2014-11-02 10:00:41 -05:00
shadowcoord.xy /= shadowcoord.w;
vec2 shadowtexcoord = shadowcoord.xy * 0.5 + 0.5;
2014-11-01 13:02:22 -04:00
float z = texture(shadowtex, vec3(shadowtexcoord, float(index))).x;
2014-11-02 10:00:41 -05:00
float d = shadowcoord.z;
2014-11-02 13:07:02 -05:00
return min(pow(exp(-8. * d) * z, 256.), 1.);
2014-02-10 08:20:09 -05:00
}
void main() {
2014-06-03 14:28:42 -04:00
vec2 uv = gl_FragCoord.xy / screen;
float z = texture(dtex, uv).x;
vec4 xpos = getPosFromUVDepth(vec3(uv, z), InverseProjectionMatrix);
vec3 norm = normalize(DecodeNormal(2. * texture(ntex, uv).xy - 1.));
2014-04-06 16:19:10 -04:00
float roughness =texture(ntex, uv).z;
2014-04-06 15:23:09 -04:00
vec3 eyedir = -normalize(xpos.xyz);
// Normalized on the cpu
2014-04-06 15:23:09 -04:00
vec3 L = direction;
2014-04-06 15:23:09 -04:00
float NdotL = max(0., dot(norm, L));
2014-04-06 17:57:54 -04:00
vec3 Specular = getSpecular(norm, eyedir, L, col, roughness) * NdotL;
2014-04-06 16:19:10 -04:00
vec3 outcol = NdotL * col;
// Shadows
2014-04-24 12:33:05 -04:00
float bias = 0.005 * tan(acos(NdotL)); // According to the slope
bias = clamp(bias, 0., 0.01);
float factor;
2014-10-27 16:19:03 -04:00
if (xpos.z < split0)
factor = getShadowFactor(xpos.xyz, bias, 0);
2014-10-27 16:19:03 -04:00
/* else if (xpos.z < 6.)
{
float a = getShadowFactor(xpos.xyz, bias, 0), b = getShadowFactor(xpos.xyz, bias, 1);
2014-04-19 18:21:30 -04:00
factor = mix(a, b, (xpos.z - 5.));
2014-10-27 16:19:03 -04:00
}*/
else if (xpos.z < split1)
factor = getShadowFactor(xpos.xyz, bias, 1);
2014-10-27 16:19:03 -04:00
/* else if (xpos.z < 21.)
{
float a = getShadowFactor(xpos.xyz, bias, 1), b = getShadowFactor(xpos.xyz, bias, 2);
2014-04-19 20:01:29 -04:00
factor = mix(a, b, (xpos.z - 20.));
2014-10-27 16:19:03 -04:00
}*/
else if (xpos.z < split2)
factor = getShadowFactor(xpos.xyz, bias, 2);
2014-10-27 16:19:03 -04:00
/* else if (xpos.z < 55.)
{
float a = getShadowFactor(xpos.xyz, bias, 2), b = getShadowFactor(xpos.xyz, bias, 3);
2014-04-19 20:01:29 -04:00
factor = mix(a, b, (xpos.z - 50.) / 5.);
2014-10-27 16:19:03 -04:00
}*/
else if (xpos.z < splitmax)
factor = getShadowFactor(xpos.xyz, bias, 3);
2014-10-27 16:19:03 -04:00
/* else if (xpos.z < 150.)
2014-04-19 20:01:29 -04:00
{
factor = mix(getShadowFactor(xpos.xyz, bias, 3), 1., (xpos.z - 145.) / 5.);
2014-10-27 16:19:03 -04:00
}*/
2014-04-19 20:01:29 -04:00
else
factor = 1.;
Diff = vec4(factor * NdotL * col, 1.);
Spec = vec4(factor * Specular, 1.);
return;
}