stk-code_catmod/data/shaders/sunlight.frag
Vincent Lejeune 4a8afd2d40 Implement sun as an disk area light.
Solid angle is defaulted to sun's one.
2014-11-21 01:54:30 +01:00

66 lines
1.6 KiB
GLSL

uniform sampler2D ntex;
uniform sampler2D dtex;
//uniform sampler2D cloudtex;
uniform vec3 direction;
uniform vec3 col;
uniform float sunangle = .54;
//uniform int hasclouds;
//uniform vec2 wind;
out vec4 Diff;
out vec4 Spec;
vec3 DecodeNormal(vec2 n);
vec3 getSpecular(vec3 normal, vec3 eyedir, vec3 lightdir, vec3 color, float roughness);
vec4 getPosFromUVDepth(vec3 uvDepth, mat4 InverseProjectionMatrix);
void main() {
vec2 uv = gl_FragCoord.xy / screen;
float z = texture(dtex, uv).x;
vec4 xpos = getPosFromUVDepth(vec3(uv, z), InverseProjectionMatrix);
if (z < 0.03)
{
// Skyboxes are fully lit
Diff = vec4(1.0);
Spec = vec4(1.0);
return;
}
vec3 norm = normalize(DecodeNormal(2. * texture(ntex, uv).xy - 1.));
float roughness = texture(ntex, uv).z;
vec3 eyedir = -normalize(xpos.xyz);
// Normalized on the cpu
vec3 L = direction;
float NdotL = max(0., dot(norm, L));
vec3 D = direction;
vec3 R = reflect(eyedir, norm);
float angle = 3.14 * sunangle / 180;
float d = cos(angle);
float r = sin(angle);
float DdotR = dot (D, R);
vec3 S = R - DdotR * D;
vec3 Lightdir = DdotR < d ? normalize (d * D + normalize (S) * r) : R;
vec3 Specular = getSpecular(norm, eyedir, L, col, roughness) * dot(Lightdir, norm);
vec3 outcol = NdotL * col;
/* 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);
outcol *= cloud;
}*/
Diff = vec4(NdotL * col, 1.);
Spec = vec4(Specular, 1.);
}