nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
sky.cc
Go to the documentation of this file.
1#include "client/sky.h"
2
3#include <raymath.h>
4#include <rlgl.h>
5
6#include <algorithm>
7#include <array>
8#include <cmath>
9#include <utility>
10#include <vector>
11
12#include "client/glsl.h"
13
14namespace nanommo {
15namespace {
16
17// The sky is a ball around the camera, behind everything else, and inside
18// what the camera can see: raylib draws nothing past 1000 metres.
19constexpr float kDome = 800.0f;
20// In metres above the sea.
21constexpr float kLowest = 80.0f;
22constexpr float kHighest = 130.0f;
23// How much of the sun's light a cloud's shadow takes away.
24constexpr float kShade = 0.45f;
25
26// Its colour from the horizon up, then the glow around the sun, and the sun.
27constexpr const char* kDomeVertexShader = R"(#version 330
28in vec3 vertexPosition;
29uniform mat4 mvp;
30out vec3 direction;
31
32void main() {
33 direction = vertexPosition;
34 gl_Position = mvp * vec4(vertexPosition, 1.0);
35}
36)";
37
38constexpr const char* kDomeFragmentShader = R"(#version 330
39in vec3 direction;
40uniform vec3 horizon;
41uniform vec3 zenith;
42uniform vec3 sun;
43uniform vec3 disc;
44uniform float edge;
45uniform float stars;
46uniform float seconds;
47out vec4 pixel;
48
49float Hash(vec2 p) {
50 return fract(sin(dot(p, vec2(127.1, 311.7))) * 43758.5453);
51}
52
53void main() {
54 vec3 look = normalize(direction);
55 vec3 toward = normalize(sun);
56 vec3 color = mix(horizon, zenith, smoothstep(0.0, 0.6, max(look.y, 0.0)));
57 // The stars: the sky cut into cells, as seen looking up, with a star in a
58 // few of them, bright enough to bloom, each twinkling in its own time, and
59 // fewer down by the horizon.
60 if (stars > 0.0 && look.y > 0.0) {
61 vec2 at = look.xz / (look.y + 0.4) * 110.0;
62 vec2 cell = floor(at);
63 if (Hash(cell) > 0.982) {
64 vec2 middle = cell + 0.25 + 0.5 * vec2(Hash(cell + 3.1), Hash(cell + 7.7));
65 float near = 1.0 - smoothstep(0.05, 0.16, length(at - middle));
66 float twinkle = 0.75 + 0.25 * sin(seconds * (2.0 + 3.0 * Hash(cell + 1.3))
67 + 40.0 * Hash(cell + 9.1));
68 float bright = 1.2 + 2.2 * Hash(cell + 5.3);
69 color += vec3(0.9, 0.95, 1.0) * stars * near * bright * twinkle
70 * smoothstep(0.02, 0.25, look.y);
71 }
72 }
73 float facing = max(dot(look, toward), 0.0);
74 color += disc * (0.06 * pow(facing, 48.0) + 0.02 * pow(facing, 6.0));
75 // The disc, with its edge smoothed over a few pixels' worth of angle.
76 float inside = smoothstep(edge - 0.00005, edge + 0.00005, dot(look, toward));
77 pixel = vec4(mix(color, disc, inside), 1.0);
78}
79)";
80
81// Lit by the sun face by face, from the faces' own slopes, as everything else
82// in the world is low-poly too, and fading into the horizon far away.
83constexpr const char* kCloudVertexShader = R"(#version 330
84in vec3 vertexPosition;
85uniform mat4 mvp;
86uniform mat4 matModel;
87out vec3 world;
88
89void main() {
90 world = (matModel * vec4(vertexPosition, 1.0)).xyz;
91 gl_Position = mvp * vec4(vertexPosition, 1.0);
92}
93)";
94
95constexpr const char* kCloudFragmentShader = R"(#version 330
96in vec3 world;
97uniform vec3 eye;
98uniform vec3 sun;
99uniform vec3 clouds;
100uniform vec3 horizon;
101uniform float fadeNear;
102uniform float fadeFar;
103out vec4 pixel;
104
105void main() {
106 // A face too small for a pixel has no slope to speak of, and normalizing
107 // nothing gives NaN, which the bloom would spread over the whole picture.
108 vec3 across = cross(dFdx(world), dFdy(world));
109 float size = length(across);
110 vec3 normal = size > 1e-10 ? across / size : vec3(0.0, 1.0, 0.0);
111 float sunlit = max(dot(normal, normalize(sun)), 0.0);
112 vec3 color = clouds * (0.62 + 0.1 * normal.y + 0.42 * sunlit);
113 float far = smoothstep(fadeNear, fadeFar, length(world.xz - eye.xz));
114 pixel = vec4(mix(color, horizon, far), 1.0);
115}
116)";
117
118// The same numbers on every machine, from the seed.
119class Random {
120 public:
121 explicit Random(uint32_t seed) : state_(seed) {}
122
123 float Between(float low, float high) {
124 state_ += 0x9E3779B9u;
125 uint32_t z = state_;
126 z = (z ^ (z >> 16)) * 0x85EBCA6Bu;
127 z = (z ^ (z >> 13)) * 0xC2B2AE35u;
128 z ^= z >> 16;
129 return low + (high - low) * static_cast<float>(z >> 8) / 16777216.0f;
130 }
131
132 private:
133 uint32_t state_;
134};
135
136void SetVec3(Shader shader, const char* name, const std::array<float, 3>& v) {
137 SetShaderValue(shader, GetShaderLocation(shader, name), v.data(),
138 SHADER_UNIFORM_VEC3);
139}
140
141void SetFloat(Shader shader, const char* name, float value) {
142 SetShaderValue(shader, GetShaderLocation(shader, name), &value,
143 SHADER_UNIFORM_FLOAT);
144}
145
146} // namespace
147
149 : dome_shader_(LoadGlsl(kDomeVertexShader, kDomeFragmentShader)),
150 cloud_shader_(LoadGlsl(kCloudVertexShader, kCloudFragmentShader)),
151 dome_(LoadModelFromMesh(GenMeshSphere(1.0f, 24, 32))),
152 // Few rings and slices, so that each ball is made of big flat faces.
153 puff_(LoadModelFromMesh(GenMeshSphere(1.0f, 5, 7))) {
154 dome_.materials[0].shader = dome_shader_;
155 puff_.materials[0].shader = cloud_shader_;
156}
157
159 UnloadModel(dome_);
160 UnloadModel(puff_);
161 UnloadShader(dome_shader_);
162 UnloadShader(cloud_shader_);
163}
164
165void Sky::Gather(uint32_t seed) {
166 Random random(seed * 7919u + 17u);
167 clouds_.clear();
168 for (int i = 0; i < kClouds; ++i) {
169 Cloud cloud;
170 cloud.at = {random.Between(-kSpread / 2, kSpread / 2),
171 random.Between(-kSpread / 2, kSpread / 2)};
172 cloud.height = random.Between(kLowest, kHighest);
173 cloud.rank = random.Between(0.0f, 1.0f);
174 // A row of balls along a line, biggest in the middle, turned any way.
175 const float length = random.Between(40.0f, 95.0f);
176 const int puffs = static_cast<int>(random.Between(4.0f, 8.0f));
177 const float turn = random.Between(0.0f, 2.0f * PI);
178 const Vector2 along = {std::cos(turn), std::sin(turn)};
179 for (int p = 0; p < puffs; ++p) {
180 const float share = (p + 0.5f) / puffs - 0.5f;
181 const float middle = 1.0f - 1.4f * std::abs(share);
182 const float size = length * random.Between(0.22f, 0.3f) * middle;
183 const float x = share * length + random.Between(-0.1f, 0.1f) * length;
184 const float z = random.Between(-0.12f, 0.12f) * length;
185 cloud.puffs.push_back(
186 {.offset = {along.x * x - along.y * z, size * 0.2f,
187 along.y * x + along.x * z},
188 .size = {size, size * random.Between(0.5f, 0.65f), size}});
189 }
190 clouds_.push_back(std::move(cloud));
191 }
192}
193
194Vector2 Sky::Blown(const Cloud& cloud, const Camera3D& camera,
195 float seconds) const {
196 const Vector2 at = Vector2Add(cloud.at, Vector2Scale(kWind, seconds));
197 const auto wrap = [](float d) {
198 return d - kSpread * std::floor((d + kSpread / 2) / kSpread);
199 };
200 return {camera.position.x + wrap(at.x - camera.position.x),
201 camera.position.z + wrap(at.y - camera.position.z)};
202}
203
204void Sky::Draw(const Camera3D& camera, const Atmosphere& atmosphere,
205 float seconds) const {
206 SetVec3(dome_shader_, "horizon", atmosphere.sky);
207 SetVec3(dome_shader_, "zenith", atmosphere.zenith);
208 SetVec3(dome_shader_, "sun", atmosphere.sun);
209 SetVec3(dome_shader_, "disc", atmosphere.disc);
210 SetFloat(dome_shader_, "edge",
211 std::cos(atmosphere.disc_size * 0.5f * DEG2RAD));
212 SetFloat(dome_shader_, "stars", atmosphere.stars);
213 SetFloat(dome_shader_, "seconds", seconds);
214 // From inside, behind everything, so it hides nothing.
215 rlDisableBackfaceCulling();
216 rlDisableDepthMask();
217 DrawModelEx(dome_, camera.position, {0.0f, 1.0f, 0.0f}, 0.0f,
218 {kDome, kDome, kDome}, WHITE);
219 rlEnableDepthMask();
220 rlEnableBackfaceCulling();
221
222 SetVec3(cloud_shader_, "eye",
223 {camera.position.x, camera.position.y, camera.position.z});
224 SetVec3(cloud_shader_, "sun", atmosphere.sun);
225 SetVec3(cloud_shader_, "clouds", atmosphere.clouds);
226 SetVec3(cloud_shader_, "horizon", atmosphere.sky);
227 // Gone into the horizon before they reach the square's edge, where they
228 // jump to the other side.
229 SetFloat(cloud_shader_, "fadeNear", 0.28f * kSpread);
230 SetFloat(cloud_shader_, "fadeFar", 0.48f * kSpread);
231 for (const Cloud& cloud : clouds_) {
232 if (cloud.rank >= atmosphere.cloudiness) {
233 continue;
234 }
235 const Vector2 at = Blown(cloud, camera, seconds);
236 for (const Puff& puff : cloud.puffs) {
237 DrawModelEx(puff_,
238 {at.x + puff.offset.x, cloud.height + puff.offset.y,
239 at.y + puff.offset.z},
240 {0.0f, 1.0f, 0.0f}, 0.0f, puff.size, WHITE);
241 }
242 }
243}
244
245void Sky::Shade(Shader shader, const Camera3D& camera,
246 const Atmosphere& atmosphere, float seconds) const {
247 std::vector<Vector4> balls;
248 for (const Cloud& cloud : clouds_) {
249 if (cloud.rank >= atmosphere.cloudiness) {
250 continue;
251 }
252 const Vector2 at = Blown(cloud, camera, seconds);
253 for (const Puff& puff : cloud.puffs) {
254 balls.push_back({at.x + puff.offset.x, at.y + puff.offset.z,
255 cloud.height + puff.offset.y, puff.size.x});
256 }
257 }
258 // Nearest first, to what the camera's looking at.
259 const auto away = [&](const Vector4& ball) {
260 return Vector2Distance({ball.x, ball.y},
261 {camera.target.x, camera.target.z});
262 };
263 std::ranges::sort(balls, {}, away);
264 std::array<Vector4, kShadows> shadows{};
265 std::copy_n(balls.begin(), std::min<size_t>(balls.size(), kShadows),
266 shadows.begin());
267 SetShaderValueV(shader, GetShaderLocation(shader, "clouds"), shadows.data(),
268 SHADER_UNIFORM_VEC4, kShadows);
269 SetFloat(shader, "cloudShade", kShade);
270}
271
272} // namespace nanommo
void Shade(Shader shader, const Camera3D &camera, const Atmosphere &atmosphere, float seconds) const
Definition sky.cc:245
static constexpr Vector2 kWind
In metres a second, which way the wind blows the clouds.
Definition sky.h:25
static constexpr int kClouds
The most clouds a land has, all of them on a cloudy day.
Definition sky.h:19
static constexpr float kSpread
Definition sky.h:28
void Gather(uint32_t seed)
Definition sky.cc:165
static constexpr int kShadows
Definition sky.h:23
void Draw(const Camera3D &camera, const Atmosphere &atmosphere, float seconds) const
Definition sky.cc:204
Definition chat.cc:9
Shader LoadGlsl(const char *vertex, const char *fragment)
Definition glsl.cc:32
The sky and the light, which the shaders read each frame.
Definition atmosphere.h:13
std::array< float, 3 > zenith
Definition atmosphere.h:28
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 > clouds
Definition atmosphere.h:35
std::array< float, 3 > sun
Towards the sun.
Definition atmosphere.h:21