2013-11-30 16:33:06 -05:00
|
|
|
uniform sampler2D ntex;
|
|
|
|
uniform sampler2D dtex;
|
2014-02-10 13:28:02 -05:00
|
|
|
uniform sampler2DArrayShadow shadowtex;
|
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 int hasclouds;
|
|
|
|
//uniform vec2 wind;
|
|
|
|
//uniform float shadowoffset;
|
|
|
|
|
|
|
|
in vec2 uv;
|
|
|
|
out vec4 Diff;
|
|
|
|
out vec4 Spec;
|
|
|
|
|
2014-03-21 13:17:21 -04:00
|
|
|
vec3 DecodeNormal(vec2 n);
|
2014-04-06 17:57:54 -04:00
|
|
|
vec3 getSpecular(vec3 normal, vec3 eyedir, vec3 lightdir, vec3 color, float roughness);
|
2014-05-02 12:11:34 -04:00
|
|
|
vec4 getPosFromUVDepth(vec3 uvDepth, mat4 InverseProjectionMatrix);
|
2013-11-30 16:33:06 -05:00
|
|
|
|
2014-02-15 14:45:47 -05:00
|
|
|
float getShadowFactor(vec3 pos, float bias, int index)
|
2014-02-10 08:20:09 -05:00
|
|
|
{
|
2014-02-17 16:36:00 -05:00
|
|
|
//float a[5] = float[](3.4, 4.2, 5.0, 5.2, 1.1);
|
|
|
|
|
2014-03-21 16:51:23 -04:00
|
|
|
vec2 shadowoffset[4] = vec2[](
|
2014-02-15 14:45:47 -05:00
|
|
|
vec2(-1., -1.),
|
|
|
|
vec2(-1., 1.),
|
|
|
|
vec2(1., -1.),
|
|
|
|
vec2(1., 1.)
|
2014-02-17 16:36:00 -05:00
|
|
|
);
|
2014-02-15 14:45:47 -05:00
|
|
|
|
2014-04-19 18:04:49 -04:00
|
|
|
vec4 shadowcoord = (ShadowViewProjMatrixes[index] * InverseViewMatrix * vec4(pos, 1.0));
|
2014-02-15 14:45:47 -05:00
|
|
|
shadowcoord /= shadowcoord.w;
|
|
|
|
vec2 shadowtexcoord = shadowcoord.xy * 0.5 + 0.5;
|
2014-02-10 08:20:09 -05:00
|
|
|
// 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);*/
|
|
|
|
|
2014-02-15 14:45:47 -05:00
|
|
|
//float shadowmapz = 2. * texture(shadowtex, vec3(shadowtexcoord, shadowcoord.z).x - 1.;
|
|
|
|
// bias += smoothstep(0.001, 0.1, moved) * 0.014; // According to the warping
|
|
|
|
float sum = 0.;
|
|
|
|
for (int i = 0; i < 4; i++)
|
2014-02-11 15:49:46 -05:00
|
|
|
{
|
2014-04-24 15:02:24 -04:00
|
|
|
sum += texture(shadowtex, vec4(shadowtexcoord + shadowoffset[i] / 2048., float(index), 0.5 * shadowcoord.z + 0.5));
|
2014-02-10 08:20:09 -05:00
|
|
|
}
|
2014-02-15 14:45:47 -05:00
|
|
|
return sum / 4.;
|
2014-02-10 08:20:09 -05:00
|
|
|
}
|
|
|
|
|
2013-11-30 16:33:06 -05:00
|
|
|
void main() {
|
2014-06-03 14:28:42 -04:00
|
|
|
vec2 uv = gl_FragCoord.xy / screen;
|
2014-02-09 11:47:11 -05:00
|
|
|
float z = texture(dtex, uv).x;
|
2014-05-02 12:11:34 -04:00
|
|
|
vec4 xpos = getPosFromUVDepth(vec3(uv, z), InverseProjectionMatrix);
|
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.));
|
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);
|
2013-11-30 16:33:06 -05:00
|
|
|
|
|
|
|
// Normalized on the cpu
|
2014-04-06 15:23:09 -04:00
|
|
|
vec3 L = direction;
|
2013-11-30 16:33:06 -05:00
|
|
|
|
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
|
|
|
|
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-04-24 12:33:05 -04:00
|
|
|
float bias = 0.005 * tan(acos(NdotL)); // According to the slope
|
|
|
|
bias = clamp(bias, 0., 0.01);
|
2014-02-15 14:45:47 -05:00
|
|
|
float factor;
|
2014-04-19 18:21:30 -04:00
|
|
|
if (xpos.z < 5.)
|
2014-02-15 14:45:47 -05:00
|
|
|
factor = getShadowFactor(xpos.xyz, bias, 0);
|
2014-04-19 18:21:30 -04:00
|
|
|
else if (xpos.z < 6.)
|
2014-02-15 14:45:47 -05:00
|
|
|
{
|
|
|
|
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-02-15 14:45:47 -05:00
|
|
|
}
|
2014-04-19 18:21:30 -04:00
|
|
|
else if (xpos.z < 20.)
|
2014-02-15 14:45:47 -05:00
|
|
|
factor = getShadowFactor(xpos.xyz, bias, 1);
|
2014-04-19 20:01:29 -04:00
|
|
|
else if (xpos.z < 21.)
|
2014-02-15 14:45:47 -05:00
|
|
|
{
|
|
|
|
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-02-15 14:45:47 -05:00
|
|
|
}
|
2014-04-19 18:21:30 -04:00
|
|
|
else if (xpos.z < 50.)
|
2014-02-15 14:45:47 -05:00
|
|
|
factor = getShadowFactor(xpos.xyz, bias, 2);
|
2014-04-19 20:01:29 -04:00
|
|
|
else if (xpos.z < 55.)
|
2014-02-15 14:45:47 -05:00
|
|
|
{
|
|
|
|
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-02-15 14:45:47 -05:00
|
|
|
}
|
2014-04-19 20:01:29 -04:00
|
|
|
else if (xpos.z < 145.)
|
2014-02-15 14:45:47 -05:00
|
|
|
factor = getShadowFactor(xpos.xyz, bias, 3);
|
2014-04-19 20:01:29 -04:00
|
|
|
else if (xpos.z < 150.)
|
|
|
|
{
|
|
|
|
factor = mix(getShadowFactor(xpos.xyz, bias, 3), 1., (xpos.z - 145.) / 5.);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
factor = 1.;
|
2014-04-21 18:44:08 -04:00
|
|
|
Diff = vec4(factor * NdotL * col, 1.);
|
|
|
|
Spec = vec4(factor * Specular, 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
|
|
|
}
|