2014-01-04 19:08:00 -05:00
|
|
|
#version 130
|
2013-11-30 16:33:06 -05:00
|
|
|
uniform sampler2D ntex;
|
|
|
|
uniform sampler2D dtex;
|
2014-02-10 08:20:09 -05:00
|
|
|
uniform sampler2DShadow shadowtex0;
|
|
|
|
uniform sampler2DShadow shadowtex1;
|
|
|
|
uniform sampler2DShadow shadowtex2;
|
2014-02-09 11:47:11 -05:00
|
|
|
//uniform sampler2D warpx;
|
|
|
|
///uniform sampler2D warpy;
|
2013-11-30 16:33:06 -05:00
|
|
|
|
2014-02-09 11:47:11 -05:00
|
|
|
uniform vec3 direction;
|
2013-11-30 16:33:06 -05:00
|
|
|
uniform vec3 col;
|
2014-02-09 11:47:11 -05:00
|
|
|
uniform mat4 invproj;
|
2014-02-10 08:20:09 -05:00
|
|
|
uniform mat4 shadowmat0;
|
|
|
|
uniform mat4 shadowmat1;
|
|
|
|
uniform mat4 shadowmat2;
|
2014-02-09 11:47:11 -05:00
|
|
|
//uniform int hasclouds;
|
|
|
|
//uniform vec2 wind;
|
|
|
|
//uniform float shadowoffset;
|
|
|
|
|
|
|
|
in vec2 uv;
|
|
|
|
out vec4 Diff;
|
|
|
|
out vec4 Spec;
|
|
|
|
|
|
|
|
vec3 DecodeNormal(vec2 n)
|
|
|
|
{
|
|
|
|
float z = dot(n, n) * 2. - 1.;
|
|
|
|
vec2 xy = normalize(n) * sqrt(1. - z * z);
|
|
|
|
return vec3(xy,z);
|
2013-11-30 16:33:06 -05:00
|
|
|
}
|
|
|
|
|
2014-02-10 08:20:09 -05:00
|
|
|
float getShadowFactor(vec3 pos, float bias)
|
|
|
|
{
|
|
|
|
if (pos.z < 10.)
|
|
|
|
{
|
|
|
|
vec4 shadowcoord = (shadowmat0 * vec4(pos, 1.0));
|
|
|
|
shadowcoord /= shadowcoord.w;
|
|
|
|
vec2 shadowtexcoord = shadowcoord.xy * 0.5 + 0.5;
|
|
|
|
// shadowcoord = (shadowcoord * 0.5) + vec3(0.5);
|
|
|
|
|
|
|
|
// float movex = decdepth(texture(warpx, shadowcoord.xy));
|
|
|
|
// float movey = decdepth(texture(warpy, shadowcoord.xy));
|
|
|
|
// float dx = movex * 2.0 - 1.0;
|
|
|
|
// float dy = movey * 2.0 - 1.0;
|
|
|
|
// shadowcoord.xy += vec2(dx, dy);*/
|
|
|
|
|
|
|
|
//float shadowmapz = 2. * texture(shadowtex, vec3(shadowtexcoord, shadowcoord.z).x - 1.;
|
|
|
|
// bias += smoothstep(0.001, 0.1, moved) * 0.014; // According to the warping
|
2014-02-10 09:58:09 -05:00
|
|
|
return texture(shadowtex0, vec3(shadowtexcoord, 0.5 * (shadowcoord.z + bias * 0.001) + 0.5));
|
2014-02-10 08:20:09 -05:00
|
|
|
}
|
|
|
|
else if (pos.z < 60)
|
|
|
|
{
|
|
|
|
vec4 shadowcoord = (shadowmat1 * vec4(pos, 1.0));
|
|
|
|
shadowcoord /= shadowcoord.w;
|
|
|
|
vec2 shadowtexcoord = shadowcoord.xy * 0.5 + 0.5;
|
2014-02-10 09:58:09 -05:00
|
|
|
return texture(shadowtex1, vec3(shadowtexcoord, 0.5 * (shadowcoord.z + bias * 0.001) + 0.5));
|
2014-02-10 08:20:09 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
vec4 shadowcoord = (shadowmat2 * vec4(pos, 1.0));
|
|
|
|
shadowcoord /= shadowcoord.w;
|
|
|
|
vec2 shadowtexcoord = shadowcoord.xy * 0.5 + 0.5;
|
|
|
|
return texture(shadowtex2, vec3(shadowtexcoord, 0.5 * (shadowcoord.z + bias) + 0.5));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-30 16:33:06 -05:00
|
|
|
void main() {
|
2014-02-09 11:47:11 -05:00
|
|
|
float z = texture(dtex, uv).x;
|
|
|
|
vec4 xpos = 2.0 * vec4(uv, z, 1.0) - 1.0;
|
|
|
|
xpos = invproj * xpos;
|
|
|
|
xpos.xyz /= xpos.w;
|
2013-11-30 16:33:06 -05:00
|
|
|
|
2014-02-09 11:47:11 -05:00
|
|
|
vec3 norm = normalize(DecodeNormal(2. * texture(ntex, uv).xy - 1.));
|
2013-11-30 16:33:06 -05:00
|
|
|
|
|
|
|
// Normalized on the cpu
|
2014-02-09 11:47:11 -05:00
|
|
|
vec3 L = direction;
|
2013-11-30 16:33:06 -05:00
|
|
|
|
|
|
|
float NdotL = max(0.0, dot(norm, L));
|
2014-02-09 11:47:11 -05:00
|
|
|
vec3 R = reflect(L, norm);
|
|
|
|
float RdotE = max(0.0, dot(R, normalize(xpos.xyz)));
|
|
|
|
float Specular = pow(RdotE, 200);
|
2013-11-30 16:33:06 -05:00
|
|
|
|
|
|
|
vec3 outcol = NdotL * col;
|
|
|
|
|
2014-02-09 11:47:11 -05:00
|
|
|
// if (hasclouds == 1)
|
|
|
|
// {
|
|
|
|
// vec2 cloudcoord = (xpos.xz * 0.00833333) + wind;
|
|
|
|
// float cloud = texture(cloudtex, cloudcoord).x;
|
|
|
|
// //float cloud = step(0.5, cloudcoord.x) * step(0.5, cloudcoord.y);
|
2013-11-30 16:33:06 -05:00
|
|
|
|
2014-02-09 11:47:11 -05:00
|
|
|
// outcol *= cloud;
|
|
|
|
// }
|
2013-11-30 16:33:06 -05:00
|
|
|
|
2014-02-09 11:47:11 -05:00
|
|
|
// Shadows
|
2014-02-09 13:42:22 -05:00
|
|
|
float bias = 0.002 * tan(acos(NdotL)); // According to the slope
|
|
|
|
bias = clamp(bias, 0.001, 0.014);
|
2014-02-10 08:20:09 -05:00
|
|
|
float factor = getShadowFactor(xpos.xyz, bias);
|
2014-02-09 14:03:54 -05:00
|
|
|
Diff = vec4(factor * NdotL * col, 1.);
|
|
|
|
Spec = vec4(factor * Specular * col, 1.);
|
2014-02-09 11:47:11 -05:00
|
|
|
return;
|
2013-11-30 16:33:06 -05:00
|
|
|
|
2014-02-09 11:47:11 -05:00
|
|
|
// float moved = (abs(dx) + abs(dy)) * 0.5;
|
2013-11-30 16:33:06 -05:00
|
|
|
|
2014-02-09 11:47:11 -05:00
|
|
|
// float avi = 0.002;
|
|
|
|
// float abi = 0.0025;
|
2013-12-01 12:47:30 -05:00
|
|
|
|
2014-02-09 11:47:11 -05:00
|
|
|
/* float avi = 0.0018;
|
2013-12-01 12:47:30 -05:00
|
|
|
float abi = 0.002;
|
|
|
|
|
|
|
|
float bias = avi * tan(acos(NdotL)); // According to the slope
|
|
|
|
bias += smoothstep(0.001, 0.1, moved) * abi; // According to the warping
|
|
|
|
bias = clamp(bias, 0.001, abi);
|
2013-11-30 16:33:06 -05:00
|
|
|
|
|
|
|
// This ID, and four IDs around this must match for a shadow pixel
|
2014-01-19 12:53:35 -05:00
|
|
|
float right = texture(shadowtex, shadowcoord.xy + vec2(shadowoffset, 0.0)).a;
|
|
|
|
float left = texture(shadowtex, shadowcoord.xy + vec2(-shadowoffset, 0.0)).a;
|
|
|
|
float up = texture(shadowtex, shadowcoord.xy + vec2(0.0, shadowoffset)).a;
|
|
|
|
float down = texture(shadowtex, shadowcoord.xy + vec2(0.0, -shadowoffset)).a;
|
2013-11-30 16:33:06 -05:00
|
|
|
|
|
|
|
float matching = ((right + left + up + down) * 0.25) - shadowread.a;
|
|
|
|
matching = abs(matching) * 400.0;
|
|
|
|
|
|
|
|
// If the ID is different, we're likely in shadow - cut the bias to cut peter panning
|
|
|
|
float off = 7.0 - step(abs(shadowread.a - depthread.a) - matching, 0.004) * 6.0;
|
|
|
|
bias /= off;
|
|
|
|
|
|
|
|
const float softness = 8.0; // How soft is the light?
|
|
|
|
float shadowed = step(shadowmapz + bias, shadowcoord.z);
|
|
|
|
float dist = (shadowcoord.z / shadowmapz) - 1.0;
|
|
|
|
float penumbra = dist * softness / gl_FragCoord.z;
|
2014-02-09 11:47:11 -05:00
|
|
|
penumbra *= shadowed;*/
|
2013-11-30 16:33:06 -05:00
|
|
|
|
|
|
|
/* outcol.r = (shadowcoord.z - shadowmapz) * 50.0;
|
|
|
|
outcol.g = moved;*/
|
|
|
|
|
2014-02-09 11:47:11 -05:00
|
|
|
// FragColor = vec4(outcol, 0.05);
|
|
|
|
// OtherOutput = vec4(shadowed, penumbra, shadowed, shadowed);
|
2013-11-30 16:33:06 -05:00
|
|
|
}
|