techkaczynski.dev / code

summaryrefslogtreecommitdiff
path: root/res/sdr/sky_old.fs
blob: 039691c1417f136480bf46bb5948f1fb9bd7d5dd (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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#version 330

uniform vec2 resolution;
uniform vec3 camera_forward;
uniform vec3 camera_right;
uniform vec3 camera_up;

uniform vec3 sun_direction;

uniform float aspect_ratio;
uniform float tan_half_fovy;

out vec4 finalColor;

float
angular_halo(float alignment, float outer_cos, float inner_cos) {
	/*
	 * alignment is 1.0 while looking directly at the celestial body.
	 *
	 * outer_cos = cos(outer halo angle)
	 * inner_cos = cos(inner halo angle)
	 */
	return smoothstep(
		outer_cos,
		inner_cos,
		alignment
	);
}

void
main() {
	vec2 screen = gl_FragCoord.xy / resolution;
	vec2 ndc = screen * 2.0 - 1.0;

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

	/*
	 * Controls the vertical gradient.
	 *
	 * -0.2 includes some colour below the horizon so the transition
	 * remains smooth while looking downward.
	 */
	float height = smoothstep(-0.20, 0.80, view_direction.y);

	/*
	 * The world becomes mostly daylight shortly after the sun crosses
	 * the horizon, and mostly night shortly after it sets.
	 */
	float day_amount = smoothstep(-0.10, 0.12, sun_direction.y);

	vec3 night_horizon = vec3(0.055, 0.045, 0.110);
	vec3 night_zenith = vec3(0.005, 0.008, 0.025);

	vec3 day_horizon = vec3(0.620, 0.780, 0.920);
	vec3 day_zenith = vec3(0.080, 0.280, 0.620);

	vec3 night_sky = mix(night_horizon, night_zenith, height);
	vec3 day_sky = mix(day_horizon, day_zenith, height);

	vec3 sky = mix(night_sky, day_sky, day_amount);

	/*
	 * Stylized sunrise/sunset colour.
	 *
	 * This is strongest:
	 * - near the horizon;
	 * - while the sun is near the horizon;
	 * - while looking toward the sun.
	 */
	float horizon =
		1.0 - smoothstep(0.0, 0.35, abs(view_direction.y));

	float twilight =
		1.0 - smoothstep(0.02, 0.28, abs(sun_direction.y));

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

	vec3 sunset_colour = vec3(0.65, 0.16, 0.035);

	sky += sunset_colour *
		horizon *
		twilight *
		sun_facing_4;

	finalColor = vec4(min(sky, vec3(1.0)), 1.0);
}