2013-11-30 16:33:06 -05:00
|
|
|
uniform sampler2D ntex;
|
|
|
|
uniform sampler2D dtex;
|
2014-11-01 17:38:38 -04:00
|
|
|
uniform sampler2DArray shadowtex;
|
2014-10-27 16:19:03 -04:00
|
|
|
|
|
|
|
uniform float split0;
|
|
|
|
uniform float split1;
|
|
|
|
uniform float split2;
|
|
|
|
uniform float splitmax;
|
2013-11-30 16:33:06 -05:00
|
|
|
|
2014-02-09 11:47:11 -05:00
|
|
|
in vec2 uv;
|
|
|
|
out vec4 Diff;
|
|
|
|
out vec4 Spec;
|
|
|
|
|
2014-03-21 13:17:21 -04:00
|
|
|
vec3 DecodeNormal(vec2 n);
|
2014-12-04 18:20:16 -05:00
|
|
|
vec3 SpecularBRDF(vec3 normal, vec3 eyedir, vec3 lightdir, vec3 color, float roughness);
|
2014-12-14 15:39:18 -05:00
|
|
|
vec3 DiffuseBRDF(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-11-20 20:44:27 -05:00
|
|
|
vec3 getMostRepresentativePoint(vec3 direction, vec3 R, float angularRadius)
|
|
|
|
{
|
|
|
|
vec3 D = direction;
|
|
|
|
float d = cos(angularRadius);
|
|
|
|
float r = sin(angularRadius);
|
|
|
|
float DdotR = dot(D, R);
|
|
|
|
vec3 S = R - DdotR * D;
|
|
|
|
return (DdotR < d) ? normalize(d * D + normalize (S) * r) : R;
|
|
|
|
}
|
|
|
|
|
2014-12-14 15:39:18 -05:00
|
|
|
float getShadowFactor(vec3 pos, int index)
|
2014-02-10 08:20:09 -05:00
|
|
|
{
|
2014-12-14 15:39:18 -05:00
|
|
|
vec4 shadowcoord = (ShadowViewProjMatrixes[index] * InverseViewMatrix * vec4(pos, 1.0));
|
|
|
|
shadowcoord.xy /= shadowcoord.w;
|
|
|
|
vec2 shadowtexcoord = shadowcoord.xy * 0.5 + 0.5;
|
2014-10-27 16:19:03 -04:00
|
|
|
|
2014-12-14 15:39:18 -05:00
|
|
|
float z = texture(shadowtex, vec3(shadowtexcoord, float(index))).x;
|
|
|
|
float d = shadowcoord.z;
|
|
|
|
return min(pow(exp(-32. * d) * z, 8.), 1.);
|
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-12-14 15:39:18 -05:00
|
|
|
float z = texture(dtex, uv).x;
|
|
|
|
vec4 xpos = getPosFromUVDepth(vec3(uv, z), InverseProjectionMatrix);
|
2013-11-30 16:33:06 -05:00
|
|
|
|
2014-12-14 15:39:18 -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
|
|
|
|
2014-12-15 17:09:19 -05:00
|
|
|
vec3 L = normalize((transpose(InverseViewMatrix) * vec4(sun_direction, 0.)).xyz);
|
2014-12-05 11:48:56 -05:00
|
|
|
float NdotL = clamp(dot(norm, L), 0., 1.);
|
2014-04-06 15:23:09 -04:00
|
|
|
|
2014-12-15 17:09:19 -05:00
|
|
|
float angle = 3.14 * sun_angle / 180.;
|
2014-11-20 20:44:27 -05:00
|
|
|
vec3 R = reflect(-eyedir, norm);
|
2014-12-15 17:09:19 -05:00
|
|
|
vec3 Lightdir = getMostRepresentativePoint(L, R, angle);
|
2014-11-20 19:54:30 -05:00
|
|
|
|
2014-12-14 15:39:18 -05:00
|
|
|
vec3 Specular = SpecularBRDF(norm, eyedir, Lightdir, vec3(1.), roughness);
|
|
|
|
vec3 Diffuse = DiffuseBRDF(norm, eyedir, Lightdir, vec3(1.), roughness);
|
|
|
|
|
|
|
|
// Shadows
|
|
|
|
float factor;
|
|
|
|
if (xpos.z < split0)
|
|
|
|
factor = getShadowFactor(xpos.xyz, 0);
|
|
|
|
else if (xpos.z < split1)
|
|
|
|
factor = getShadowFactor(xpos.xyz, 1);
|
|
|
|
else if (xpos.z < split2)
|
|
|
|
factor = getShadowFactor(xpos.xyz, 2);
|
|
|
|
else if (xpos.z < splitmax)
|
|
|
|
factor = getShadowFactor(xpos.xyz, 3);
|
|
|
|
else
|
|
|
|
factor = 1.;
|
|
|
|
|
2014-12-15 17:09:19 -05:00
|
|
|
Diff = vec4(factor * NdotL * Diffuse * sun_col, 1.);
|
|
|
|
Spec = vec4(factor * NdotL * Specular * sun_col, 1.);
|
2013-11-30 16:33:06 -05:00
|
|
|
}
|