nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
daylight.cc
Go to the documentation of this file.
1#include "client/daylight.h"
2
3#include <algorithm>
4#include <array>
5#include <cmath>
6#include <cstddef>
7
8#include "terrain/lands.h"
9
10namespace nanommo {
11
12namespace {
13
14using Rgb = std::array<float, 3>;
15
16// A land's looks through the day, beside its own, which is its day.
17struct Times {
18 Atmosphere dawn;
19 Atmosphere dusk;
20 Atmosphere night;
21};
22
23constexpr Times kMeadowsTimes = {
24 .dawn = {.sky = {0.93f, 0.68f, 0.55f},
25 .fog_near = 40.0f,
26 .fog_far = 140.0f,
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},
31 .disc_size = 4.0f,
32 .clouds = {1.0f, 0.8f, 0.7f},
33 .cloudiness = 0.5f},
34 .dusk = {.sky = {0.96f, 0.55f, 0.34f},
35 .fog_near = 40.0f,
36 .fog_far = 140.0f,
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},
41 .disc_size = 4.5f,
42 .clouds = {1.0f, 0.64f, 0.5f},
43 .cloudiness = 0.5f},
44 // A moon in place of the sun, and the stars.
45 .night = {.sky = {0.06f, 0.08f, 0.16f},
46 .fog_near = 25.0f,
47 .fog_far = 115.0f,
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},
52 .disc_size = 2.2f,
53 .clouds = {0.17f, 0.19f, 0.27f},
54 .cloudiness = 0.45f,
55 .stars = 1.0f},
56};
57
58constexpr Times kAshlandsTimes = {
59 .dawn = {.sky = {0.56f, 0.28f, 0.16f},
60 .fog_near = 35.0f,
61 .fog_far = 130.0f,
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},
66 .disc_size = 6.0f,
67 .clouds = {0.5f, 0.33f, 0.28f},
68 .cloudiness = 0.8f},
69 .dusk = {.sky = {0.5f, 0.18f, 0.1f},
70 .fog_near = 35.0f,
71 .fog_far = 130.0f,
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},
76 .disc_size = 6.0f,
77 .clouds = {0.45f, 0.22f, 0.16f},
78 .cloudiness = 0.8f},
79 // Through the smoke, a dim red moon, and a few stars. The lava lights
80 // the rest.
81 .night = {.sky = {0.13f, 0.045f, 0.035f},
82 .fog_near = 25.0f,
83 .fog_far = 115.0f,
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},
88 .disc_size = 2.8f,
89 .clouds = {0.22f, 0.1f, 0.08f},
90 .cloudiness = 0.8f,
91 .stars = 0.35f},
92};
93
94// The Crypt has no days: its stars are out all the time, when the mist lets
95// them.
96constexpr float kCryptStars = 0.6f;
97
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};
101}
102
103Rgb Scale(const Rgb& a, float s) { return {a[0] * s, a[1] * s, a[2] * s}; }
104
105float Mix(float a, float b, float t) { return a + (b - a) * t; }
106
107float Luminance(const Rgb& c) {
108 return 0.3f * c[0] + 0.59f * c[1] + 0.11f * c[2];
109}
110
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);
114}
115
116Atmosphere Mix(const Atmosphere& a, const Atmosphere& b, float t) {
117 Atmosphere m = a;
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);
129 return m;
130}
131
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};
135}
136
137// The look at that time of day: night until dawn, dawn into day, the day
138// itself, day into dusk, and dusk into night.
139Atmosphere Through(const Atmosphere& day, const Times& times, float d) {
140 if (d < 0.18f || d >= 0.82f) {
141 return times.night;
142 }
143 if (d < 0.25f) {
144 return Mix(times.night, times.dawn, Smooth(0.18f, 0.25f, d));
145 }
146 if (d < 0.33f) {
147 return Mix(times.dawn, day, Smooth(0.25f, 0.33f, d));
148 }
149 if (d < 0.67f) {
150 return day;
151 }
152 if (d < 0.75f) {
153 return Mix(day, times.dusk, Smooth(0.67f, 0.75f, d));
154 }
155 return Mix(times.dusk, times.night, Smooth(0.75f, 0.82f, d));
156}
157
158// Under the weather, as much of it as there is.
159void Overcast(Atmosphere& a, protocol::Weather weather, float w) {
160 const float bright = Luminance(a.sky);
161 switch (weather) {
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);
166 break;
167 case protocol::WEATHER_RAIN:
168 case protocol::WEATHER_STORM: {
169 // Grey, and darker in a storm, at the brightness the sky had.
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);
182 a.stars *= 1.0f - w;
183 break;
184 }
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);
193 a.stars *= 1.0f - w;
194 break;
195 }
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);
205 a.stars *= 1.0f - w;
206 break;
207 }
208 case protocol::WEATHER_FIRE: {
209 // The fire lights the smoke from below, whatever the time.
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);
217 a.stars *= 1.0f - w;
218 break;
219 }
220 default:
221 break;
222 }
223}
224
225} // namespace
226
227float Darkness(float day) {
228 return day < 0.5f ? 1.0f - Smooth(0.18f, 0.3f, day)
229 : Smooth(0.7f, 0.82f, day);
230}
231
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});
235}
236
237Atmosphere AtmosphereUnder(int land, const protocol::Sky& sky,
238 const Showing& showing) {
239 const std::size_t at = static_cast<std::size_t>(land) % kLooks.size();
240 Atmosphere a = kLooks[at].atmosphere;
241 const bool turns = !kLands[at].dungeon;
242 if (!turns) {
243 a.stars = kCryptStars;
244 } else if (showing.day_night) {
245 const Times& times = at == 1 ? kAshlandsTimes : kMeadowsTimes;
246 a = Through(kLooks[at].atmosphere, times, sky.day());
247 // The sun while it's up, and the moon, across from it, while it's down.
248 // Its light fades out as it reaches the horizon, so that the one giving
249 // way to the other is never seen.
250 const Rgb sun = SunAt(sky.day());
251 const Rgb light =
252 sun[1] >= 0.0f ? sun : Normalized({-sun[0], -sun[1], sun[2]});
253 a.sun = light;
254 const float up = Smooth(0.0f, 0.08f, light[1]);
255 a.sunlight = Scale(a.sunlight, up);
256 a.disc = Scale(a.disc, Smooth(0.0f, 0.04f, light[1]));
257 }
258 if (showing.weather) {
259 Overcast(a, sky.weather(), std::clamp(sky.strength(), 0.0f, 1.0f));
260 }
261 return a;
262}
263
264} // namespace nanommo
Definition chat.cc:9
float Darkness(float day)
Definition daylight.cc:227
constexpr std::array< Look, 3 > kLooks
In the order of the lands.
Definition atmosphere.h:164
Atmosphere AtmosphereUnder(int land, const protocol::Sky &sky, const Showing &showing)
Definition daylight.cc:237
constexpr std::array< Land, 3 > kLands
Definition lands.h:31
std::array< float, 3 > SunAt(float day)
Definition daylight.cc:232
void Mix(Samples &mix, const Samples &sound, float at, float gain)
Definition synth.cc:69
The sky and the light, which the shaders read each frame.
Definition atmosphere.h:13
std::array< float, 3 > sunlight
What the sun adds to a face that's turned to it.
Definition atmosphere.h:23
std::array< float, 3 > sky
The background, which the fog fades into.
Definition atmosphere.h:15
std::array< float, 3 > disc
Definition atmosphere.h:31
std::array< float, 3 > sun
Towards the sun.
Definition atmosphere.h:21
What turns on and off: the time of day, and the weather.
Definition daylight.h:18