terrain-gen/shaders/skybox.frag

66 lines
2.1 KiB
GLSL

#version 420 core
#extension GL_NV_shadow_samplers_cube : enable
out vec4 out_color;
in vec3 position;
uniform samplerCube skyTexture;
uniform vec4 u_lightPos;
uniform float u_dayNightFactor;
#define M_PI 3.1415926535897932384626433832795
const vec3 nightHorizonColor = 0.3 * vec3(0.015, 0.592, 0.976);
const vec3 nightSkyColor = 0.4 * vec3(0.098, 0.129, 0.219);
const vec3 dayHorizonColor = vec3(0.21, 0.52, 0.76);
const vec3 daySkyColor = vec3(0.023, 0.14, 0.45);
const vec3 sunColor = vec3(1, 1, 0.94);
const vec3 sunriseCenterColor = vec3(1.00,0.61,0.00);
float max3 (vec3 v) {
return max (max (v.x, v.y), v.z);
}
float fade(float t) {
return 3 * t * t - 2 * t * t * t;
}
vec3 colorGradient(float t, float startPos, vec3 startColor, float endPos, vec3 endColor) {
return startColor + fade(clamp((t - startPos) / (endPos - startPos), 0, 1)) * (endColor - startColor);
}
vec3 night(vec3 position) {
vec3 skyTexture = textureCube(skyTexture, position / 2).rgb;
float ambiantFactor = 1 - max3(skyTexture);
return skyTexture + ambiantFactor * colorGradient(position.y, -0.2, nightHorizonColor, 1, nightSkyColor);
}
vec3 sun(vec3 position) {
float sunIntensity = pow(max(dot(normalize(u_lightPos.xyz), position), 0), 50);
return sunColor * sunIntensity;
}
vec3 day(vec3 position, float t) {
float sunCos = dot(position, u_lightPos.xyz);
float darkFactor = (1 - t) * (0.3 + (sunCos * 0.5 + 0.5) * 0.7) + t;
float sunRiseColorFactor = pow(max(sunCos, 0), 2) * clamp((0.5 - abs(u_lightPos.y)) / 0.2, 0, 1);
vec3 dayAmbiant = colorGradient(position.y, -0.25, dayHorizonColor, 1, daySkyColor);
return sunRiseColorFactor * sunriseCenterColor + darkFactor * (1 - sunRiseColorFactor) * dayAmbiant * t;
}
vec3 sky(vec3 position) {
vec3 fullDayAmbiant = day(position, u_dayNightFactor);
float nightFactor = (1 - max3(fullDayAmbiant)) * (1 - u_dayNightFactor);
return fullDayAmbiant + nightFactor * night(position) + sun(position);
}
void main(void)
{
vec3 normalizedPos = normalize(position);
out_color = vec4(sky(normalizedPos), 1.0);
}