14using Rgb = std::array<float, 3>;
23constexpr Times kMeadowsTimes = {
24 .dawn = {.sky = {0.93f, 0.68f, 0.55f},
27 .sunlight = {0.95f, 0.66f, 0.46f},
28 .ambient = {0.36f, 0.35f, 0.44f},
29 .zenith = {0.38f, 0.45f, 0.7f},
30 .disc = {4.0f, 2.7f, 1.7f},
32 .clouds = {1.0f, 0.8f, 0.7f},
34 .dusk = {.sky = {0.96f, 0.55f, 0.34f},
37 .sunlight = {1.0f, 0.55f, 0.3f},
38 .ambient = {0.34f, 0.29f, 0.42f},
39 .zenith = {0.27f, 0.25f, 0.52f},
40 .disc = {4.0f, 2.0f, 0.85f},
42 .clouds = {1.0f, 0.64f, 0.5f},
45 .night = {.sky = {0.06f, 0.08f, 0.16f},
48 .sunlight = {0.24f, 0.29f, 0.44f},
49 .ambient = {0.11f, 0.13f, 0.22f},
50 .zenith = {0.01f, 0.016f, 0.05f},
51 .disc = {1.8f, 1.9f, 2.3f},
53 .clouds = {0.17f, 0.19f, 0.27f},
58constexpr Times kAshlandsTimes = {
59 .dawn = {.sky = {0.56f, 0.28f, 0.16f},
62 .sunlight = {1.0f, 0.5f, 0.26f},
63 .ambient = {0.32f, 0.24f, 0.24f},
64 .zenith = {0.24f, 0.11f, 0.09f},
65 .disc = {4.0f, 1.2f, 0.4f},
67 .clouds = {0.5f, 0.33f, 0.28f},
69 .dusk = {.sky = {0.5f, 0.18f, 0.1f},
72 .sunlight = {1.0f, 0.42f, 0.2f},
73 .ambient = {0.3f, 0.2f, 0.2f},
74 .zenith = {0.18f, 0.06f, 0.06f},
75 .disc = {4.0f, 1.0f, 0.3f},
77 .clouds = {0.45f, 0.22f, 0.16f},
81 .night = {.sky = {0.13f, 0.045f, 0.035f},
84 .sunlight = {0.32f, 0.15f, 0.1f},
85 .ambient = {0.15f, 0.09f, 0.08f},
86 .zenith = {0.03f, 0.01f, 0.01f},
87 .disc = {1.7f, 1.0f, 0.7f},
89 .clouds = {0.22f, 0.1f, 0.08f},
96constexpr float kCryptStars = 0.6f;
98Rgb
Mix(
const Rgb& a,
const Rgb& b,
float t) {
99 return {a[0] + (b[0] - a[0]) * t, a[1] + (b[1] - a[1]) * t,
100 a[2] + (b[2] - a[2]) * t};
103Rgb Scale(
const Rgb& a,
float s) {
return {a[0] * s, a[1] * s, a[2] * s}; }
105float Mix(
float a,
float b,
float t) {
return a + (b - a) * t; }
107float Luminance(
const Rgb& c) {
108 return 0.3f * c[0] + 0.59f * c[1] + 0.11f * c[2];
111float Smooth(
float edge0,
float edge1,
float x) {
112 const float t = std::clamp((x - edge0) / (edge1 - edge0), 0.0f, 1.0f);
113 return t * t * (3.0f - 2.0f * t);
118 m.
sky =
Mix(a.sky, b.sky, t);
119 m.fog_near =
Mix(a.fog_near, b.fog_near, t);
120 m.fog_far =
Mix(a.fog_far, b.fog_far, t);
121 m.sunlight =
Mix(a.sunlight, b.sunlight, t);
122 m.ambient =
Mix(a.ambient, b.ambient, t);
123 m.zenith =
Mix(a.zenith, b.zenith, t);
124 m.disc =
Mix(a.disc, b.disc, t);
125 m.disc_size =
Mix(a.disc_size, b.disc_size, t);
126 m.clouds =
Mix(a.clouds, b.clouds, t);
127 m.cloudiness =
Mix(a.cloudiness, b.cloudiness, t);
128 m.stars =
Mix(a.stars, b.stars, t);
132Rgb Normalized(
const Rgb& v) {
133 const float length = std::sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);
134 return {v[0] / length, v[1] / length, v[2] / length};
140 if (d < 0.18f || d >= 0.82f) {
144 return Mix(times.night, times.dawn, Smooth(0.18f, 0.25f, d));
147 return Mix(times.dawn, day, Smooth(0.25f, 0.33f, d));
153 return Mix(day, times.dusk, Smooth(0.67f, 0.75f, d));
155 return Mix(times.dusk, times.night, Smooth(0.75f, 0.82f, d));
159void Overcast(
Atmosphere& a, protocol::Weather weather,
float w) {
160 const float bright = Luminance(a.sky);
162 case protocol::WEATHER_CLOUDY:
163 a.cloudiness = std::max(a.cloudiness,
Mix(a.cloudiness, 1.0f, w));
164 a.sunlight = Scale(a.sunlight, 1.0f - 0.3f * w);
165 a.disc = Scale(a.disc, 1.0f - 0.5f * w);
167 case protocol::WEATHER_RAIN:
168 case protocol::WEATHER_STORM: {
170 const float storm = weather == protocol::WEATHER_STORM ? 1.0f : 0.0f;
171 const float dim = 0.85f - 0.3f * storm;
172 const Rgb grey = Scale({0.92f, 0.97f, 1.05f}, bright * dim);
173 a.sky =
Mix(a.sky, grey, 0.8f * w);
174 a.zenith =
Mix(a.zenith, Scale(grey, 0.75f), 0.85f * w);
175 a.sunlight = Scale(a.sunlight, 1.0f - (0.65f + 0.15f * storm) * w);
176 a.ambient = Scale(a.ambient, 1.0f - (0.12f + 0.06f * storm) * w);
177 a.fog_near =
Mix(a.fog_near, a.fog_near * 0.45f, w);
178 a.fog_far =
Mix(a.fog_far, a.fog_far * (0.62f - 0.12f * storm), w);
179 a.disc = Scale(a.disc, 1.0f - 0.92f * w);
180 a.clouds =
Mix(a.clouds, Scale(a.clouds, 0.55f - 0.15f * storm), w);
181 a.cloudiness =
Mix(a.cloudiness, 1.0f, w);
185 case protocol::WEATHER_MIST: {
186 const Rgb pale = Scale({1.0f, 1.0f, 1.02f}, bright * 0.95f + 0.04f);
187 a.sky =
Mix(a.sky, pale, 0.65f * w);
188 a.zenith =
Mix(a.zenith, pale, 0.5f * w);
189 a.fog_near =
Mix(a.fog_near, 2.0f, w);
190 a.fog_far =
Mix(a.fog_far, 34.0f, w);
191 a.sunlight = Scale(a.sunlight, 1.0f - 0.4f * w);
192 a.disc = Scale(a.disc, 1.0f - 0.7f * w);
196 case protocol::WEATHER_ASH: {
197 const Rgb ash = Scale({1.12f, 1.0f, 0.95f}, bright * 0.9f);
198 a.sky =
Mix(a.sky, ash, 0.6f * w);
199 a.zenith =
Mix(a.zenith, Scale(ash, 0.6f), 0.6f * w);
200 a.fog_far =
Mix(a.fog_far, a.fog_far * 0.6f, w);
201 a.sunlight = Scale(a.sunlight, 1.0f - 0.4f * w);
202 a.clouds =
Mix(a.clouds, Scale({0.3f, 0.29f, 0.28f}, bright * 2.0f), w);
203 a.cloudiness =
Mix(a.cloudiness, 1.0f, w);
204 a.disc = Scale(a.disc, 1.0f - 0.6f * w);
208 case protocol::WEATHER_FIRE: {
210 a.sky =
Mix(a.sky, {0.55f, 0.17f, 0.07f}, 0.55f * w);
211 a.zenith =
Mix(a.zenith, {0.2f, 0.05f, 0.03f}, 0.6f * w);
212 a.sunlight =
Mix(a.sunlight, {0.9f, 0.4f, 0.2f}, 0.4f * w);
213 a.ambient =
Mix(a.ambient, {0.36f, 0.18f, 0.12f}, 0.5f * w);
214 a.clouds =
Mix(a.clouds, {0.34f, 0.1f, 0.05f}, w);
215 a.cloudiness =
Mix(a.cloudiness, 1.0f, w);
216 a.fog_far =
Mix(a.fog_far, a.fog_far * 0.7f, w);
228 return day < 0.5f ? 1.0f - Smooth(0.18f, 0.3f, day)
229 : Smooth(0.7f, 0.82f, day);
232std::array<float, 3>
SunAt(
float day) {
233 const float turn = 6.2831853f * (day - 0.25f);
234 return Normalized({std::cos(turn), std::sin(turn), 0.35f});
239 const std::size_t at =
static_cast<std::size_t
>(land) %
kLooks.size();
241 const bool turns = !
kLands[at].dungeon;
243 a.
stars = kCryptStars;
245 const Times& times = at == 1 ? kAshlandsTimes : kMeadowsTimes;
246 a = Through(
kLooks[at].atmosphere, times, sky.day());
250 const Rgb sun =
SunAt(sky.day());
252 sun[1] >= 0.0f ? sun : Normalized({-sun[0], -sun[1], sun[2]});
254 const float up = Smooth(0.0f, 0.08f, light[1]);
256 a.
disc = Scale(a.
disc, Smooth(0.0f, 0.04f, light[1]));
259 Overcast(a, sky.weather(), std::clamp(sky.strength(), 0.0f, 1.0f));
float Darkness(float day)
constexpr std::array< Look, 3 > kLooks
In the order of the lands.
Atmosphere AtmosphereUnder(int land, const protocol::Sky &sky, const Showing &showing)
constexpr std::array< Land, 3 > kLands
std::array< float, 3 > SunAt(float day)
void Mix(Samples &mix, const Samples &sound, float at, float gain)
The sky and the light, which the shaders read each frame.
std::array< float, 3 > sunlight
What the sun adds to a face that's turned to it.
std::array< float, 3 > sky
The background, which the fog fades into.
std::array< float, 3 > disc
std::array< float, 3 > sun
Towards the sun.
What turns on and off: the time of day, and the weather.