techkaczynski.dev / code

summaryrefslogtreecommitdiff
path: root/res/sdr/sky.fs
blob: 8fde0f218e4423e42efa75ac1d12f8cd6854ad1f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#version 330

uniform vec2 resolution;

uniform vec3 camera_forward;
uniform vec3 camera_right;
uniform vec3 camera_up;
uniform vec3 sun_direction;

uniform vec3 horizon_colour;
uniform vec3 zenith_colour;

uniform float day_amount;
uniform float twilight_amount;
uniform float tan_half_fovy;

out vec4 finalColor;

void
main() {
	vec2 ndc = gl_FragCoord.xy / resolution * 2.0 - 1.0;
	float aspect_ratio = resolution.x / resolution.y;

	vec3 view_direction = normalize(
		camera_forward +
		camera_right * ndc.x * aspect_ratio * tan_half_fovy +
		camera_up * ndc.y * tan_half_fovy
	);

	float height = smoothstep(-0.20, 0.80, view_direction.y);
	float horizon = 1.0 - smoothstep(0.0, 0.35, abs(view_direction.y));

	vec3 sky = mix(horizon_colour, zenith_colour, height);

	float sun_alignment = dot(view_direction, sun_direction);
	float sun_facing = max(sun_alignment, 0.0);
	float sun_facing_2 = sun_facing * sun_facing;
	float sun_facing_4 = sun_facing_2 * sun_facing_2;

	sky += vec3(0.65, 0.16, 0.035) *
		horizon * twilight_amount * sun_facing_4;

	float sun_wide = smoothstep(0.9510565, 0.9986295, sun_alignment);
	float sun_core = smoothstep(0.9925462, 0.9998477, sun_alignment);

	float moon_alignment = -sun_alignment;
	float moon_wide = smoothstep(0.9702957, 0.9993908, moon_alignment);
	float moon_core = smoothstep(0.9961947, 0.9999619, moon_alignment);

	float sun_visibility = smoothstep(-0.12, 0.04, sun_direction.y);
	float moon_visibility = smoothstep(-0.10, 0.04, -sun_direction.y);

	vec3 sun_glow_colour = mix(
		vec3(1.00, 0.82, 0.38),
		vec3(1.00, 0.25, 0.035),
		twilight_amount
	);

	vec3 moon_glow_colour = vec3(0.32, 0.46, 0.82);

	float sun_glow =
		(sun_wide * 0.13 + sun_core * 0.32) * sun_visibility;

	float moon_glow =
		(moon_wide * 0.08 + moon_core * 0.17) *
		moon_visibility * (1.0 - day_amount);

	sky += (1.0 - sky) * sun_glow_colour * sun_glow;
	sky += (1.0 - sky) * moon_glow_colour * moon_glow;

	finalColor = vec4(clamp(sky, 0.0, 1.0), 1.0);
}