2013-11-30 16:33:06 -05:00
|
|
|
uniform sampler2D ntex;
|
2021-05-21 11:13:55 -04:00
|
|
|
#if defined(GL_ES) && defined(GL_FRAGMENT_PRECISION_HIGH)
|
|
|
|
uniform highp sampler2D dtex;
|
|
|
|
#else
|
2013-11-30 16:33:06 -05:00
|
|
|
uniform sampler2D dtex;
|
2021-05-21 11:13:55 -04:00
|
|
|
#endif
|
2015-01-01 14:06:34 -05:00
|
|
|
uniform sampler2DArrayShadow shadowtex;
|
2014-10-27 16:19:03 -04:00
|
|
|
|
|
|
|
uniform float split0;
|
|
|
|
uniform float split1;
|
|
|
|
uniform float split2;
|
|
|
|
uniform float splitmax;
|
2015-01-10 18:44:06 -05:00
|
|
|
uniform float shadow_res;
|
2013-11-30 16:33:06 -05:00
|
|
|
|
2017-12-25 01:00:10 -05:00
|
|
|
uniform vec3 sundirection;
|
|
|
|
uniform vec3 sun_color;
|
|
|
|
|
2014-02-09 11:47:11 -05:00
|
|
|
in vec2 uv;
|
2016-09-12 15:39:52 -04:00
|
|
|
#ifdef GL_ES
|
|
|
|
layout (location = 0) out vec4 Diff;
|
|
|
|
layout (location = 1) out vec4 Spec;
|
|
|
|
#else
|
2014-02-09 11:47:11 -05:00
|
|
|
out vec4 Diff;
|
|
|
|
out vec4 Spec;
|
2016-09-12 15:39:52 -04:00
|
|
|
#endif
|
2014-02-09 11:47:11 -05:00
|
|
|
|
2016-06-27 07:11:27 -04:00
|
|
|
#stk_include "utils/decodeNormal.frag"
|
|
|
|
#stk_include "utils/SpecularBRDF.frag"
|
|
|
|
#stk_include "utils/DiffuseBRDF.frag"
|
|
|
|
#stk_include "utils/getPosFromUVDepth.frag"
|
|
|
|
#stk_include "utils/SunMRP.frag"
|
2014-11-20 20:44:27 -05:00
|
|
|
|
2014-12-14 15:39:18 -05:00
|
|
|
float getShadowFactor(vec3 pos, int index)
|
2014-02-10 08:20:09 -05:00
|
|
|
{
|
2017-12-25 01:00:10 -05:00
|
|
|
vec4 shadowcoord = (u_shadow_projection_view_matrices[index] * u_inverse_view_matrix * vec4(pos, 1.0));
|
2014-12-14 15:39:18 -05:00
|
|
|
shadowcoord.xy /= shadowcoord.w;
|
|
|
|
vec2 shadowtexcoord = shadowcoord.xy * 0.5 + 0.5;
|
2017-10-29 15:18:08 -04:00
|
|
|
//float d = .5 * shadowcoord.z + .5;
|
|
|
|
float d = .5 * shadowcoord.z + .5 - 1. / (shadow_res * 5.);
|
2014-10-27 16:19:03 -04:00
|
|
|
|
2015-01-01 14:06:34 -05:00
|
|
|
float result = 0.;
|
|
|
|
|
|
|
|
for (float i = -1.; i <= 1.; i += 1.)
|
|
|
|
{
|
|
|
|
for (float j = -1.; j <= 1.; j += 1.)
|
2017-02-07 15:48:50 -05:00
|
|
|
{
|
2017-10-04 16:18:32 -04:00
|
|
|
result += texture(shadowtex, vec4(shadowtexcoord + vec2(i,j) / shadow_res, float(index), d));
|
2017-02-07 15:48:50 -05:00
|
|
|
}
|
2015-01-01 14:06:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return result / 9.;
|
2014-02-10 08:20:09 -05:00
|
|
|
}
|
|
|
|
|
2013-11-30 16:33:06 -05:00
|
|
|
void main() {
|
2017-12-25 01:00:10 -05:00
|
|
|
vec2 uv = gl_FragCoord.xy / u_screen;
|
2014-12-14 15:39:18 -05:00
|
|
|
float z = texture(dtex, uv).x;
|
2017-12-25 01:00:10 -05:00
|
|
|
vec4 xpos = getPosFromUVDepth(vec3(uv, z), u_inverse_projection_matrix);
|
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-21 15:05:41 -05:00
|
|
|
vec3 Lightdir = SunMRP(norm, eyedir);
|
|
|
|
float NdotL = clamp(dot(norm, Lightdir), 0., 1.);
|
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.;
|
|
|
|
|
2017-12-25 01:00:10 -05:00
|
|
|
Diff = vec4(factor * NdotL * Diffuse * sun_color, 1.);
|
|
|
|
Spec = vec4(factor * NdotL * Specular * sun_color, 1.);
|
2013-11-30 16:33:06 -05:00
|
|
|
}
|