nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
weather.cc
Go to the documentation of this file.
1#include "client/weather.h"
2
3#include <raymath.h>
4#include <rlgl.h>
5
6#include <algorithm>
7#include <array>
8#include <cmath>
9#include <cstddef>
10#include <cstdint>
11#include <utility>
12#include <vector>
13
14#include "client/daylight.h"
15#include "client/glsl.h"
16#include "terrain/lands.h"
17
18namespace nanommo {
19
20namespace {
21
22// Each quad's colour is its vertices', and how much brighter than that it
23// glows rides in its first texture coordinate, so that what glows past white
24// blooms. Far away, it fades into the fog, or, added onto what's behind it,
25// to nothing.
26constexpr const char* kVertexShader = R"(#version 330
27in vec3 vertexPosition;
28in vec2 vertexTexCoord;
29in vec4 vertexColor;
30uniform mat4 mvp;
31out vec4 color;
32out float glow;
33out float depth;
34
35void main() {
36 color = vertexColor;
37 glow = vertexTexCoord.x;
38 gl_Position = mvp * vec4(vertexPosition, 1.0);
39 depth = gl_Position.w;
40}
41)";
42
43constexpr const char* kFragmentShader = R"(#version 330
44in vec4 color;
45in float glow;
46in float depth;
47uniform vec3 fogColor;
48uniform float fogNear;
49uniform float fogFar;
50uniform float added;
51out vec4 pixel;
52
53void main() {
54 float fog = clamp((depth - fogNear) / (fogFar - fogNear), 0.0, 1.0);
55 vec3 lit = color.rgb * glow;
56 if (added > 0.5) {
57 pixel = vec4(lit * (1.0 - fog) * color.a, 1.0);
58 } else {
59 pixel = vec4(mix(lit, fogColor, fog), color.a);
60 }
61}
62)";
63
64// What falls, each in a box around the camera that it fills, from its top
65// down to the ground. Each one lands somewhere new each time round, and
66// starts again at the top.
67struct Fall {
68 int count;
69 /// In metres across.
70 float box;
71 /// In metres from the ground up.
72 float height;
73 /// In metres a second.
74 float speed;
75 /// How far a flake sways from side to side as it falls, in metres.
76 float sway;
77 uint32_t salt;
78};
79
80constexpr Fall kRain = {.count = 3200,
81 .box = 38.0f,
82 .height = 20.0f,
83 .speed = 10.0f,
84 .sway = 0.0f,
85 .salt = 11};
86constexpr Fall kSnow = {.count = 1800,
87 .box = 38.0f,
88 .height = 18.0f,
89 .speed = 1.4f,
90 .sway = 0.35f,
91 .salt = 23};
92constexpr Fall kEmbers = {.count = 900,
93 .box = 64.0f,
94 .height = 45.0f,
95 .speed = 8.0f,
96 .sway = 0.0f,
97 .salt = 37};
98constexpr Fall kAsh = {.count = 1700,
99 .box = 40.0f,
100 .height = 20.0f,
101 .speed = 1.1f,
102 .sway = 0.5f,
103 .salt = 41};
104
105// In metres in front of the camera: the middle of what falls and glows.
106constexpr float kAhead = 12.0f;
107
108// Up from here, rain falls as snow, where the ground is white too.
109constexpr float kSnowLine = 19.0f;
110// A drop's splash, or an ember's sparks, last this long, in seconds.
111constexpr float kSplash = 0.3f;
112constexpr float kSparks = 0.45f;
113
114// What glows, a few to each cell of the ground around the camera.
115struct Swarm {
116 /// In metres along a side.
117 float cell;
118 /// How far out from the camera, in metres.
119 float reach;
120 /// Of the cells, the share that have one, at the most.
121 float density;
122 uint32_t salt;
123};
124
125constexpr Swarm kFireflies = {
126 .cell = 2.5f, .reach = 40.0f, .density = 0.6f, .salt = 53};
127constexpr Swarm kWisps = {
128 .cell = 6.0f, .reach = 55.0f, .density = 0.5f, .salt = 59};
129constexpr Swarm kSparksOffLava = {
130 .cell = 3.0f, .reach = 50.0f, .density = 0.3f, .salt = 61};
131
132constexpr int kMeadowsLand = 0;
133constexpr int kAshlandsLand = 1;
134
135uint32_t Hash(uint32_t x) {
136 x ^= x >> 16;
137 x *= 0x7FEB352Du;
138 x ^= x >> 15;
139 x *= 0x846CA68Bu;
140 x ^= x >> 16;
141 return x;
142}
143
144uint32_t Hash(uint32_t a, uint32_t b) {
145 return Hash(a ^ Hash(b + 0x9E3779B9u));
146}
147
148// From 0 to 1.
149float Unit(uint32_t h) {
150 return static_cast<float>(h >> 8) * (1.0f / 16777216.0f);
151}
152
153float Wrap(float v, float size) { return v - size * std::floor(v / size); }
154
155// The world's x or z, moved by whole boxes into the box around the middle.
156float InBox(float world, float middle, float box) {
157 const float low = middle - box / 2.0f;
158 return low + Wrap(world - low, box);
159}
160
161// Where one of what falls is now, and where it landed the last time round,
162// how long ago.
163struct Spot {
164 Vector3 at;
165 Vector3 landed;
166 float since;
167 /// Where it lands this time round.
168 Vector3 lands;
169};
170
171Spot Falling(const Fall& fall, int i, float clock, Vector3 middle, Vector2 wind,
172 const Terrain& terrain) {
173 const uint32_t h = Hash(static_cast<uint32_t>(i), fall.salt);
174 const float period = fall.height / fall.speed;
175 const float round = clock / period + Unit(h);
176 const float k = std::floor(round);
177 const float age = round - k;
178 const auto landing = [&](float cycle) {
179 const uint32_t c =
180 Hash(h, static_cast<uint32_t>(static_cast<int64_t>(cycle)));
181 const float x = InBox(Unit(c) * fall.box, middle.x, fall.box);
182 const float z = InBox(Unit(Hash(c, 7)) * fall.box, middle.z, fall.box);
183 return Vector3{x, std::max(terrain.Height(x, z), Terrain::kSeaLevel), z};
184 };
185 const Vector3 lands = landing(k);
186 const float left = (1.0f - age) * period;
187 const float phase = Unit(Hash(h, 3)) * 40.0f;
188 const float sway = fall.sway * std::sin(clock * 1.3f + phase);
189 const float sway_z = fall.sway * std::cos(clock * 1.1f + phase);
190 return {.at = {lands.x - wind.x * left + sway,
191 lands.y + (1.0f - age) * fall.height,
192 lands.z - wind.y * left + sway_z},
193 .landed = landing(k - 1.0f),
194 .since = age * period,
195 .lands = lands};
196}
197
198// A corner of a triangle, in a colour, glowing as bright as that.
199void Corner(Vector3 at, Color color, float glow) {
200 rlColor4ub(color.r, color.g, color.b, color.a);
201 rlTexCoord2f(glow, 0.0f);
202 rlVertex3f(at.x, at.y, at.z);
203}
204
205// A quad's four corners, in one colour, as two triangles.
206void Quad(const std::array<Vector3, 4>& corners, Color color, float glow) {
207 for (const int i : {0, 1, 2, 0, 2, 3}) {
208 Corner(corners[i], color, glow);
209 }
210}
211
212// From the tail to the head, as wide as that, turned to face the eye.
213void Streak(Vector3 head, Vector3 tail, float width, Vector3 eye,
214 Color head_color, Color tail_color, float glow) {
215 const Vector3 along = Vector3Subtract(head, tail);
216 const Vector3 to_eye = Vector3Subtract(eye, head);
217 const Vector3 side = Vector3Scale(
218 Vector3Normalize(Vector3CrossProduct(along, to_eye)), width / 2.0f);
219 const Vector3 a = Vector3Add(head, side);
220 const Vector3 b = Vector3Subtract(head, side);
221 const Vector3 c = Vector3Subtract(tail, side);
222 const Vector3 d = Vector3Add(tail, side);
223 Corner(a, head_color, glow);
224 Corner(b, head_color, glow);
225 Corner(c, tail_color, glow * 0.6f);
226 Corner(a, head_color, glow);
227 Corner(c, tail_color, glow * 0.6f);
228 Corner(d, tail_color, glow * 0.6f);
229}
230
231// From nothing within a metre and a half of the eye to all of it from four
232// metres on: what falls past the lens fades away, rather than cross the
233// picture as a bar.
234float AwayFromTheEye(Vector3 at, Vector3 eye) {
235 return std::clamp((Vector3Distance(at, eye) - 1.5f) / 2.5f, 0.0f, 1.0f);
236}
237
238Color Faded(Color color, float by) {
239 color.a = static_cast<unsigned char>(static_cast<float>(color.a) * by);
240 return color;
241}
242
243// A round glow facing the eye: full in the middle, and nothing at its edge,
244// as a firefly's light is, rather than a square of it. One that's nearly at
245// the eye fades away, rather than fill the picture.
246void Glow(Vector3 at, float radius, Vector3 eye, Vector3 right, Vector3 up,
247 Color color, float glow) {
248 const float near =
249 std::clamp((Vector3Distance(at, eye) - 1.2f) / 1.8f, 0.0f, 1.0f);
250 if (near <= 0.0f) {
251 return;
252 }
253 glow *= near;
254 constexpr int kSides = 8;
255 const Color edge = {color.r, color.g, color.b, 0};
256 for (int i = 0; i < kSides; ++i) {
257 const auto rim = [&](int k) {
258 const float turn = 2.0f * PI * static_cast<float>(k) / kSides;
259 return Vector3Add(at,
260 Vector3Add(Vector3Scale(right, radius * std::cos(turn)),
261 Vector3Scale(up, radius * std::sin(turn))));
262 };
263 Corner(at, color, glow);
264 Corner(rim(i), edge, glow);
265 Corner(rim(i + 1), edge, glow);
266 }
267}
268
269// A square facing the eye, with its sides along the camera's right and up.
270void Facing(Vector3 at, float half, Vector3 right, Vector3 up, Color color,
271 float glow) {
272 const Vector3 r = Vector3Scale(right, half);
273 const Vector3 u = Vector3Scale(up, half);
274 Quad({Vector3Add(Vector3Add(at, r), u), Vector3Add(Vector3Subtract(at, r), u),
275 Vector3Subtract(Vector3Subtract(at, r), u),
276 Vector3Subtract(Vector3Add(at, r), u)},
277 color, glow);
278}
279
280// A ring lying on the ground or the water, from inner to outer.
281void Ring(Vector3 at, float inner, float outer, Color color,
282 float glow = 1.0f) {
283 constexpr int kSides = 7;
284 for (int i = 0; i < kSides; ++i) {
285 const float a = 2.0f * PI * static_cast<float>(i) / kSides;
286 const float b = 2.0f * PI * static_cast<float>(i + 1) / kSides;
287 const auto point = [&](float r, float turn) {
288 return Vector3{at.x + r * std::cos(turn), at.y + 0.04f,
289 at.z + r * std::sin(turn)};
290 };
291 Quad({point(inner, a), point(outer, a), point(outer, b), point(inner, b)},
292 color, glow);
293 }
294}
295
296Color Shade(const std::array<float, 3>& rgb, float alpha) {
297 const auto c = [](float v) {
298 return static_cast<unsigned char>(std::clamp(v, 0.0f, 1.0f) * 255.0f);
299 };
300 return {c(rgb[0]), c(rgb[1]), c(rgb[2]), c(alpha)};
301}
302
303void SetFloat(Shader shader, const char* name, float value) {
304 SetShaderValue(shader, GetShaderLocation(shader, name), &value,
305 SHADER_UNIFORM_FLOAT);
306}
307
308} // namespace
309
310Weather::Weather() : shader_(LoadGlsl(kVertexShader, kFragmentShader)) {}
311
312Weather::~Weather() { UnloadShader(shader_); }
313
314void Weather::set_land(const Terrain& terrain, int land) {
315 terrain_ = terrain;
316 land_ = land;
317}
318
319void Weather::Update(float seconds, const protocol::Sky& sky, bool on,
320 const Camera3D& camera) {
321 clock_ += seconds;
322 sky_ = sky;
323 on_ = on;
324
325 // Rain soaks the ground over a minute or so, and the sun dries it slower.
326 const bool raining = on && land_ == kMeadowsLand &&
327 (sky.weather() == protocol::WEATHER_RAIN ||
328 sky.weather() == protocol::WEATHER_STORM);
329 const float soaked = raining ? sky.strength() : 0.0f;
330 if (!wetness_) {
331 wetness_ = soaked;
332 } else if (soaked > *wetness_) {
333 wetness_ = std::min(soaked, *wetness_ + seconds / 45.0f);
334 } else {
335 wetness_ = std::max(soaked, *wetness_ - seconds / 180.0f);
336 }
337
338 if (!on || !terrain_ || land_ != kMeadowsLand ||
339 sky.weather() != protocol::WEATHER_STORM || sky.strength() < 0.25f) {
340 return;
341 }
342 next_bolt_ -= seconds;
343 if (next_bolt_ > 0.0f) {
344 return;
345 }
346 // Somewhere off in the storm, most often ahead of us, where we'd see it,
347 // from the clouds to the ground, in a jagged line with a fork off it.
348 const uint32_t h = Hash(seed_, ++bolts_struck_);
349 const Vector3 ahead = Vector3Subtract(camera.target, camera.position);
350 const float wide = Unit(Hash(h, 3)) < 0.7f ? 0.7f : 2.4f;
351 const float turn = std::atan2(ahead.z, ahead.x) + wide * (Unit(h) - 0.5f);
352 const float away = 110.0f + 230.0f * Unit(Hash(h, 1));
353 const float x = camera.position.x + away * std::cos(turn);
354 const float z = camera.position.z + away * std::sin(turn);
355 const float ground = std::max(terrain_->Height(x, z), Terrain::kSeaLevel);
356 constexpr int kSegments = 12;
357 constexpr float kCloud = 115.0f;
358 strike_.points.clear();
359 strike_.branch.clear();
360 for (int i = 0; i <= kSegments; ++i) {
361 const float down = static_cast<float>(i) / kSegments;
362 const float jitter = i == 0 || i == kSegments ? 0.0f : 9.0f;
363 strike_.points.push_back(
364 {x + jitter * (Unit(Hash(h, 10 + i)) - 0.5f) * 2.0f,
365 kCloud + (ground - kCloud) * down,
366 z + jitter * (Unit(Hash(h, 40 + i)) - 0.5f) * 2.0f});
367 }
368 Vector3 fork = strike_.points[4];
369 strike_.branch.push_back(fork);
370 for (int i = 1; i <= 5; ++i) {
371 fork = {fork.x + 7.0f * (Unit(Hash(h, 70 + i)) - 0.3f), fork.y - 9.0f,
372 fork.z + 7.0f * (Unit(Hash(h, 90 + i)) - 0.3f)};
373 strike_.branch.push_back(fork);
374 }
375 strike_.at = clock_;
376 strike_.near = 1.0f - 0.5f * (away - 110.0f) / 230.0f;
377 bolts_.push_back({.at = {x, ground, z}});
378 // The stronger the storm, the more often.
379 next_bolt_ = (5.0f + 11.0f * Unit(Hash(h, 2))) / (0.5f + sky.strength());
380}
381
382float Weather::StrikeLight() const {
383 const float since = clock_ - strike_.at;
384 if (since < 0.0f || since > 0.45f) {
385 return 0.0f;
386 }
387 // It flickers: on, nearly off, on again, then fades.
388 if (since < 0.07f) {
389 return 1.0f;
390 }
391 if (since < 0.11f) {
392 return 0.25f;
393 }
394 if (since < 0.17f) {
395 return 0.9f;
396 }
397 return 0.9f * (1.0f - (since - 0.17f) / 0.28f);
398}
399
400float Weather::flash() const { return 0.85f * strike_.near * StrikeLight(); }
401
402std::vector<Bolt> Weather::TakeBolts() { return std::exchange(bolts_, {}); }
403
404void Weather::Draw(const Camera3D& camera, const Atmosphere& atmosphere) const {
405 if (!terrain_ || !on_) {
406 return;
407 }
408 const Terrain& terrain = *terrain_;
409 SetShaderValue(shader_, GetShaderLocation(shader_, "fogColor"),
410 atmosphere.sky.data(), SHADER_UNIFORM_VEC3);
411 SetFloat(shader_, "fogNear", atmosphere.fog_near);
412 SetFloat(shader_, "fogFar", atmosphere.fog_far);
413 rlDisableBackfaceCulling();
414 rlDisableDepthMask();
415 BeginShaderMode(shader_);
416 BeginBlendMode(BLEND_ALPHA);
417 SetFloat(shader_, "added", 0.0f);
418 // What falls fills a box a little in front of the camera, where we'd see
419 // it, however far off it looks, and what glows is round there too.
420 const Vector3 ahead = Vector3Subtract(camera.target, camera.position);
421 const Vector3 level = Vector3Normalize({ahead.x, 0.0f, ahead.z});
422 const Vector3 middle =
423 Vector3Add(camera.position, Vector3Scale(level, kAhead));
424 rlBegin(RL_TRIANGLES);
425 DrawFalling(camera, atmosphere, terrain, middle);
426 rlEnd();
427 EndBlendMode();
428 BeginBlendMode(BLEND_ADDITIVE);
429 SetFloat(shader_, "added", 1.0f);
430 rlBegin(RL_TRIANGLES);
431 DrawGlows(camera, terrain, middle);
432 rlEnd();
433 // The lightning's seen through the fog and the rain, however far off.
434 rlDrawRenderBatchActive();
435 SetFloat(shader_, "fogNear", 1e5f);
436 SetFloat(shader_, "fogFar", 2e5f);
437 rlBegin(RL_TRIANGLES);
438 DrawStrike(camera);
439 rlEnd();
440 EndBlendMode();
441 EndShaderMode();
442 rlEnableDepthMask();
443 rlEnableBackfaceCulling();
444}
445
446void Weather::DrawFalling(const Camera3D& camera, const Atmosphere& atmosphere,
447 const Terrain& terrain, Vector3 middle) const {
448 const Vector2 wind = {sky_.wind_x(), sky_.wind_z()};
449 const float strength = std::clamp(sky_.strength(), 0.0f, 1.0f);
450 const Vector3 forward =
451 Vector3Normalize(Vector3Subtract(camera.target, camera.position));
452 const Vector3 right =
453 Vector3Normalize(Vector3CrossProduct(forward, camera.up));
454 const Vector3 up = Vector3CrossProduct(right, forward);
455 // Lit as much as the sky and the land around it are.
456 std::array<float, 3> lit;
457 for (int c = 0; c < 3; ++c) {
458 lit[c] = 0.5f * atmosphere.sky[c] +
459 0.45f * (atmosphere.ambient[c] + 0.6f * atmosphere.sunlight[c]);
460 }
461 const auto tinted = [&](std::array<float, 3> tint, float alpha) {
462 return Shade({lit[0] * tint[0], lit[1] * tint[1], lit[2] * tint[2]}, alpha);
463 };
464
465 if (land_ == kMeadowsLand && (sky_.weather() == protocol::WEATHER_RAIN ||
466 sky_.weather() == protocol::WEATHER_STORM)) {
467 const bool storm = sky_.weather() == protocol::WEATHER_STORM;
468 const int drops = static_cast<int>(kRain.count * strength);
469 const Color drop = tinted({1.0f, 1.05f, 1.18f}, storm ? 0.42f : 0.34f);
470 const Vector3 down = Vector3Normalize({wind.x, -kRain.speed, wind.y});
471 for (int i = 0; i < drops; ++i) {
472 const Spot spot = Falling(kRain, i, clock_, middle, wind, terrain);
473 if (spot.lands.y > kSnowLine) {
474 continue;
475 }
476 const Vector3 tail = Vector3Subtract(spot.at, Vector3Scale(down, 0.7f));
477 const Color seen = Faded(drop, AwayFromTheEye(spot.at, camera.position));
478 Streak(spot.at, tail, 0.03f, camera.position, seen,
479 {seen.r, seen.g, seen.b, 0}, 1.0f);
480 // Where it landed a moment ago, a ring that spreads and fades, wider on
481 // the water.
482 if (spot.since < kSplash && spot.landed.y <= kSnowLine) {
483 const float t = spot.since / kSplash;
484 const bool water = spot.landed.y <= Terrain::kSeaLevel + 0.001f;
485 const float r = (water ? 0.08f : 0.04f) + (water ? 0.32f : 0.16f) * t;
486 Ring(spot.landed, r * 0.75f, r,
487 tinted({1.1f, 1.15f, 1.25f}, (water ? 0.5f : 0.32f) * (1.0f - t)));
488 }
489 }
490 const int flakes = static_cast<int>(kSnow.count * strength);
491 const Color flake = tinted({1.5f, 1.5f, 1.6f}, 0.92f);
492 for (int i = 0; i < flakes; ++i) {
493 const Spot spot =
494 Falling(kSnow, i, clock_, middle, Vector2Scale(wind, 0.6f), terrain);
495 if (spot.lands.y <= kSnowLine) {
496 continue;
497 }
498 Facing(spot.at, 0.022f, right, up,
499 Faded(flake, AwayFromTheEye(spot.at, camera.position)), 1.0f);
500 }
501 }
502
503 if (land_ == kAshlandsLand) {
504 const float ash = sky_.weather() == protocol::WEATHER_ASH ? strength
505 : sky_.weather() == protocol::WEATHER_FIRE
506 ? 0.45f * strength
507 : 0.0f;
508 const Color flake = tinted({0.5f, 0.47f, 0.45f}, 0.88f);
509 const int flakes = static_cast<int>(kAsh.count * ash);
510 for (int i = 0; i < flakes; ++i) {
511 const Spot spot =
512 Falling(kAsh, i, clock_, middle, Vector2Scale(wind, 0.8f), terrain);
513 // Tumbling: bigger and smaller as it turns.
514 const float turn = 0.6f + 0.4f * std::sin(clock_ * 2.7f + i * 1.7f);
515 Facing(spot.at, 0.045f * turn, right, up,
516 Faded(flake, AwayFromTheEye(spot.at, camera.position)), 1.0f);
517 }
518 }
519}
520
521void Weather::DrawGlows(const Camera3D& camera, const Terrain& terrain,
522 Vector3 middle) const {
523 const Vector3 forward =
524 Vector3Normalize(Vector3Subtract(camera.target, camera.position));
525 const Vector3 right =
526 Vector3Normalize(Vector3CrossProduct(forward, camera.up));
527 const Vector3 up = Vector3CrossProduct(right, forward);
528 const float strength = std::clamp(sky_.strength(), 0.0f, 1.0f);
529 const float dark = Darkness(sky_.day());
530 const Vector2 wind = {sky_.wind_x(), sky_.wind_z()};
531
532 // Each cell of the ground near the camera that has one, by the share
533 // that's out now.
534 const auto swarm = [&](const Swarm& s, float share, auto&& draw) {
535 if (share <= 0.0f) {
536 return;
537 }
538 const int x0 = static_cast<int>(std::floor((middle.x - s.reach) / s.cell));
539 const int x1 = static_cast<int>(std::floor((middle.x + s.reach) / s.cell));
540 const int z0 = static_cast<int>(std::floor((middle.z - s.reach) / s.cell));
541 const int z1 = static_cast<int>(std::floor((middle.z + s.reach) / s.cell));
542 for (int cx = x0; cx <= x1; ++cx) {
543 for (int cz = z0; cz <= z1; ++cz) {
544 const uint32_t h = Hash(Hash(static_cast<uint32_t>(cx) * 73856093u ^
545 static_cast<uint32_t>(cz) * 19349663u),
546 s.salt);
547 if (Unit(h) > s.density * share) {
548 continue;
549 }
550 const float x = (static_cast<float>(cx) + Unit(Hash(h, 1))) * s.cell;
551 const float z = (static_cast<float>(cz) + Unit(Hash(h, 2))) * s.cell;
552 draw(h, x, z, terrain.Height(x, z));
553 }
554 }
555 };
556
557 if (land_ == kMeadowsLand) {
558 // Fireflies, as the light goes, over the grass: not in the rain, on the
559 // water, or up in the snow.
560 const bool wet = sky_.weather() == protocol::WEATHER_RAIN ||
561 sky_.weather() == protocol::WEATHER_STORM;
562 const float out =
563 dark * (wet ? std::max(0.0f, 1.0f - 2.0f * strength) : 1.0f);
564 swarm(kFireflies, out, [&](uint32_t h, float x, float z, float ground) {
565 if (ground < 0.3f || ground > 14.0f) {
566 return;
567 }
568 const float a = 40.0f * Unit(Hash(h, 3));
569 const float b = 40.0f * Unit(Hash(h, 4));
570 const Vector3 at = {x + 1.1f * std::sin(clock_ * 0.37f + a) +
571 0.4f * std::sin(clock_ * 0.9f + b),
572 ground + 0.7f + 1.3f * Unit(Hash(h, 5)) +
573 0.35f * std::sin(clock_ * 0.6f + b),
574 z + 1.1f * std::sin(clock_ * 0.31f + b) +
575 0.4f * std::sin(clock_ * 0.8f + a)};
576 // Each blinks in its own time: a slow glow up, and down again.
577 const float beat = 0.9f + 0.9f * Unit(Hash(h, 6));
578 const float on =
579 std::pow(std::max(0.0f, std::sin(clock_ * beat + a)), 3.0f);
580 Glow(at, 0.09f, camera.position, right, up, {190, 255, 90, 255},
581 0.5f + 8.0f * on);
582 });
583 }
584 if (kLands[static_cast<std::size_t>(land_) % kLands.size()].dungeon) {
585 // Pale wisps, drifting slowly, all the time.
586 swarm(kWisps, 1.0f, [&](uint32_t h, float x, float z, float ground) {
587 if (ground < Terrain::kSeaLevel) {
588 return;
589 }
590 const float a = 40.0f * Unit(Hash(h, 3));
591 const Vector3 at = {x + 2.0f * std::sin(clock_ * 0.15f + a),
592 ground + 1.0f + 2.2f * Unit(Hash(h, 5)) +
593 0.5f * std::sin(clock_ * 0.4f + a),
594 z + 2.0f * std::cos(clock_ * 0.12f + a)};
595 const float pulse = 0.65f + 0.35f * std::sin(clock_ * 0.8f + a);
596 Glow(at, 0.2f, camera.position, right, up, {140, 190, 255, 255},
597 4.0f * pulse);
598 });
599 }
600 if (land_ == kAshlandsLand) {
601 // Sparks rising off the lava, more of them in the dark, and fire falling
602 // from the sky in a firestorm.
603 swarm(kSparksOffLava, 0.35f + 0.65f * dark,
604 [&](uint32_t h, float x, float z, float ground) {
605 if (ground > -0.05f) {
606 return;
607 }
608 const float rise = 0.7f + 0.6f * Unit(Hash(h, 3));
609 const float up_by =
610 Wrap(clock_ * rise + 9.0f * Unit(Hash(h, 4)), 5.0f);
611 const Vector3 at = {x + wind.x * up_by * 0.3f, 0.2f + up_by,
612 z + wind.y * up_by * 0.3f};
613 const float fade = 1.0f - up_by / 5.0f;
614 Glow(at, 0.05f, camera.position, right, up,
615 {255, 120, 30, static_cast<unsigned char>(255 * fade)},
616 3.5f * fade);
617 });
618 if (sky_.weather() == protocol::WEATHER_FIRE) {
619 const int embers = static_cast<int>(kEmbers.count * strength);
620 const Vector3 down = Vector3Normalize({wind.x, -kEmbers.speed, wind.y});
621 for (int i = 0; i < embers; ++i) {
622 const Spot spot = Falling(kEmbers, i, clock_, middle, wind, terrain);
623 const Vector3 tail = Vector3Subtract(spot.at, Vector3Scale(down, 1.3f));
624 const float away = AwayFromTheEye(spot.at, camera.position);
625 Streak(spot.at, tail, 0.09f, camera.position,
626 Faded({255, 205, 120, 255}, away), {255, 80, 15, 0}, 4.5f);
627 // Where it landed, sparks fly out and fall, and on the lava, a ring
628 // of light spreads.
629 if (spot.since < kSparks) {
630 const float t = spot.since;
631 const uint32_t h = Hash(static_cast<uint32_t>(i), 977u);
632 if (spot.landed.y <= Terrain::kSeaLevel + 0.001f) {
633 const float r = 0.15f + 0.9f * t / kSparks;
634 Ring(spot.landed, r * 0.6f, r,
635 {255, 140, 40,
636 static_cast<unsigned char>(200 * (1.0f - t / kSparks))},
637 2.5f);
638 }
639 for (int s = 0; s < 4; ++s) {
640 const float a = 2.0f * PI * (s + Unit(Hash(h, s))) / 4.0f;
641 const Vector3 at = {spot.landed.x + std::cos(a) * 2.2f * t,
642 spot.landed.y + 2.6f * t - 6.0f * t * t + 0.05f,
643 spot.landed.z + std::sin(a) * 2.2f * t};
644 Glow(at, 0.045f, camera.position, right, up, {255, 170, 60, 255},
645 4.0f * (1.0f - t / kSparks));
646 }
647 }
648 }
649 }
650 }
651}
652
653void Weather::DrawStrike(const Camera3D& camera) const {
654 const float light = StrikeLight();
655 if (light <= 0.0f) {
656 return;
657 }
658 const Color bolt = {215, 228, 255, 255};
659 for (const std::vector<Vector3>* line : {&strike_.points, &strike_.branch}) {
660 const float width = line == &strike_.points ? 1.3f : 0.7f;
661 for (std::size_t i = 1; i < line->size(); ++i) {
662 Streak((*line)[i - 1], (*line)[i], width, camera.position, bolt, bolt,
663 9.0f * light);
664 }
665 }
666}
667
668} // namespace nanommo
static constexpr float kSeaLevel
Definition terrain.h:50
void Update(float seconds, const protocol::Sky &sky, bool on, const Camera3D &camera)
Definition weather.cc:319
float flash() const
How bright the sky is from a bolt that's just struck, from 0 to 1.
Definition weather.cc:400
void set_land(const Terrain &terrain, int land)
Definition weather.cc:314
void Draw(const Camera3D &camera, const Atmosphere &atmosphere) const
Definition weather.cc:404
std::vector< Bolt > TakeBolts()
The bolts that struck since the last time, once each.
Definition weather.cc:402
Definition chat.cc:9
float Darkness(float day)
Definition daylight.cc:227
uint32_t Hash(uint32_t seed, int32_t x, int32_t z)
Definition noise.cc:22
Shader LoadGlsl(const char *vertex, const char *fragment)
Definition glsl.cc:32
constexpr std::array< Land, 3 > kLands
Definition lands.h:31
ImU32 Faded(ImU32 color, float share)
The same colour, with its alpha times the share.
Definition theme.cc:59
uint32_t Spot(const Monster &monster, const std::map< uint32_t, Player > &players)
Definition monsters.cc:194
The sky and the light, which the shaders read each frame.
Definition atmosphere.h:13
std::array< float, 3 > ambient
What a face gets even when it's turned away from the sun.
Definition atmosphere.h:25
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