terrain-gen/shaders/commonlighting.glsl

25 lines
815 B
GLSL

float shadow(vec4 worldSpacePosition, vec3 cameraSpaceNormal) {
vec4 shadowPosition = u_depthVP * worldSpacePosition;
float bias = 0.005 * tan(acos(dot(cameraSpaceNormal, u_lightPos.xyz)));;
bias = clamp(bias, 0.0, 0.01);
float shadow = 0;
for (int y = -2; y < 2; y++) {
for (int x = -2; x < 2; x++) {
if (texture(shadowMapTexture, shadowPosition.xy + vec2(x, y) / 3072).r < shadowPosition.z - bias) {
shadow += 1;
}
}
}
return shadow / 25;
}
float visibility(float shadow) {
return mix(1.0, 1 - clamp(shadow * 2, 0.0, 1.0), u_dayNightFactor);
}
float diffuse(vec3 cameraSpaceNormal) {
return mix(max(dot(cameraSpaceNormal, -u_lightPos.xyz), 0.0), max(dot(cameraSpaceNormal, u_lightPos.xyz), 0.0), u_dayNightFactor);
}