26constexpr const char* kVertexShader = R
"(#version 330
27in vec3 vertexPosition;
28in vec2 vertexTexCoord;
37 glow = vertexTexCoord.x;
38 gl_Position = mvp * vec4(vertexPosition, 1.0);
39 depth = gl_Position.w;
43constexpr const char* kFragmentShader = R
"(#version 330
54 float fog = clamp((depth - fogNear) / (fogFar - fogNear), 0.0, 1.0);
55 vec3 lit = color.rgb * glow;
57 pixel = vec4(lit * (1.0 - fog) * color.a, 1.0);
59 pixel = vec4(mix(lit, fogColor, fog), color.a);
80constexpr Fall kRain = {.count = 3200,
86constexpr Fall
kSnow = {.count = 1800,
92constexpr Fall kEmbers = {.count = 900,
98constexpr Fall
kAsh = {.count = 1700,
106constexpr float kAhead = 12.0f;
109constexpr float kSnowLine = 19.0f;
111constexpr float kSplash = 0.3f;
112constexpr float kSparks = 0.45f;
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};
132constexpr int kMeadowsLand = 0;
133constexpr int kAshlandsLand = 1;
135uint32_t
Hash(uint32_t x) {
144uint32_t
Hash(uint32_t a, uint32_t b) {
145 return Hash(a ^
Hash(b + 0x9E3779B9u));
149float Unit(uint32_t h) {
150 return static_cast<float>(h >> 8) * (1.0f / 16777216.0f);
153float Wrap(
float v,
float size) {
return v - size * std::floor(v / size); }
156float InBox(
float world,
float middle,
float box) {
157 const float low = middle - box / 2.0f;
158 return low + Wrap(world - low, box);
171Spot Falling(
const Fall& fall,
int i,
float clock, Vector3 middle, Vector2 wind,
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) {
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);
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,
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);
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);
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);
234float AwayFromTheEye(Vector3 at, Vector3 eye) {
235 return std::clamp((Vector3Distance(at, eye) - 1.5f) / 2.5f, 0.0f, 1.0f);
238Color
Faded(Color color,
float by) {
239 color.a =
static_cast<unsigned char>(
static_cast<float>(color.a) * by);
246void Glow(Vector3 at,
float radius, Vector3 eye, Vector3 right, Vector3 up,
247 Color color,
float glow) {
249 std::clamp((Vector3Distance(at, eye) - 1.2f) / 1.8f, 0.0f, 1.0f);
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))));
263 Corner(at, color, glow);
264 Corner(rim(i), edge, glow);
265 Corner(rim(i + 1), edge, glow);
270void Facing(Vector3 at,
float half, Vector3 right, Vector3 up, Color color,
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)},
281void Ring(Vector3 at,
float inner,
float outer, Color color,
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)};
291 Quad({point(inner, a), point(outer, a), point(outer, b), point(inner, b)},
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);
300 return {c(rgb[0]), c(rgb[1]), c(rgb[2]), c(alpha)};
303void SetFloat(Shader shader,
const char* name,
float value) {
304 SetShaderValue(shader, GetShaderLocation(shader, name), &value,
305 SHADER_UNIFORM_FLOAT);
320 const Camera3D& camera) {
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;
332 }
else if (soaked > *wetness_) {
333 wetness_ = std::min(soaked, *wetness_ + seconds / 45.0f);
335 wetness_ = std::max(soaked, *wetness_ - seconds / 180.0f);
338 if (!on || !terrain_ || land_ != kMeadowsLand ||
339 sky.weather() != protocol::WEATHER_STORM || sky.strength() < 0.25f) {
342 next_bolt_ -= seconds;
343 if (next_bolt_ > 0.0f) {
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);
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});
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);
376 strike_.near = 1.0f - 0.5f * (away - 110.0f) / 230.0f;
377 bolts_.push_back({.at = {x, ground, z}});
379 next_bolt_ = (5.0f + 11.0f * Unit(
Hash(h, 2))) / (0.5f + sky.strength());
382float Weather::StrikeLight()
const {
383 const float since = clock_ - strike_.at;
384 if (since < 0.0f || since > 0.45f) {
397 return 0.9f * (1.0f - (since - 0.17f) / 0.28f);
405 if (!terrain_ || !on_) {
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);
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);
428 BeginBlendMode(BLEND_ADDITIVE);
429 SetFloat(shader_,
"added", 1.0f);
430 rlBegin(RL_TRIANGLES);
431 DrawGlows(camera, terrain, middle);
434 rlDrawRenderBatchActive();
435 SetFloat(shader_,
"fogNear", 1e5f);
436 SetFloat(shader_,
"fogFar", 2e5f);
437 rlBegin(RL_TRIANGLES);
443 rlEnableBackfaceCulling();
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);
456 std::array<float, 3> lit;
457 for (
int c = 0; c < 3; ++c) {
458 lit[c] = 0.5f * atmosphere.
sky[c] +
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);
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) {
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);
482 if (spot.since < kSplash && spot.landed.y <= kSnowLine) {
483 const float t = spot.since / kSplash;
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)));
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) {
494 Falling(
kSnow, i, clock_, middle, Vector2Scale(wind, 0.6f), terrain);
495 if (spot.lands.y <= kSnowLine) {
498 Facing(spot.at, 0.022f, right, up,
499 Faded(flake, AwayFromTheEye(spot.at, camera.position)), 1.0f);
503 if (land_ == kAshlandsLand) {
504 const float ash = sky_.weather() == protocol::WEATHER_ASH ? strength
505 : sky_.weather() == protocol::WEATHER_FIRE
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) {
512 Falling(
kAsh, i, clock_, middle, Vector2Scale(wind, 0.8f), terrain);
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);
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()};
534 const auto swarm = [&](
const Swarm& s,
float share,
auto&& draw) {
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),
547 if (Unit(h) > s.density * share) {
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));
557 if (land_ == kMeadowsLand) {
560 const bool wet = sky_.weather() == protocol::WEATHER_RAIN ||
561 sky_.weather() == protocol::WEATHER_STORM;
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) {
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)};
577 const float beat = 0.9f + 0.9f * Unit(
Hash(h, 6));
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},
584 if (
kLands[
static_cast<std::size_t
>(land_) %
kLands.size()].dungeon) {
586 swarm(kWisps, 1.0f, [&](uint32_t h,
float x,
float z,
float ground) {
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},
600 if (land_ == kAshlandsLand) {
603 swarm(kSparksOffLava, 0.35f + 0.65f * dark,
604 [&](uint32_t h,
float x,
float z,
float ground) {
605 if (ground > -0.05f) {
608 const float rise = 0.7f + 0.6f * Unit(
Hash(h, 3));
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)},
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);
629 if (spot.since < kSparks) {
630 const float t = spot.since;
631 const uint32_t h =
Hash(
static_cast<uint32_t
>(i), 977u);
633 const float r = 0.15f + 0.9f * t / kSparks;
634 Ring(spot.landed, r * 0.6f, r,
636 static_cast<unsigned char>(200 * (1.0f - t / kSparks))},
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));
653void Weather::DrawStrike(
const Camera3D& camera)
const {
654 const float light = StrikeLight();
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,
static constexpr float kSeaLevel
void Update(float seconds, const protocol::Sky &sky, bool on, const Camera3D &camera)
float flash() const
How bright the sky is from a bolt that's just struck, from 0 to 1.
void set_land(const Terrain &terrain, int land)
void Draw(const Camera3D &camera, const Atmosphere &atmosphere) const
std::vector< Bolt > TakeBolts()
The bolts that struck since the last time, once each.
float Darkness(float day)
uint32_t Hash(uint32_t seed, int32_t x, int32_t z)
Shader LoadGlsl(const char *vertex, const char *fragment)
constexpr std::array< Land, 3 > kLands
ImU32 Faded(ImU32 color, float share)
The same colour, with its alpha times the share.
uint32_t Spot(const Monster &monster, const std::map< uint32_t, Player > &players)
The sky and the light, which the shaders read each frame.
std::array< float, 3 > ambient
What a face gets even when it's turned away from the sun.
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.