19constexpr float kDome = 800.0f;
21constexpr float kLowest = 80.0f;
22constexpr float kHighest = 130.0f;
24constexpr float kShade = 0.45f;
27constexpr const char* kDomeVertexShader = R
"(#version 330
28in vec3 vertexPosition;
33 direction = vertexPosition;
34 gl_Position = mvp * vec4(vertexPosition, 1.0);
38constexpr const char* kDomeFragmentShader = R
"(#version 330
50 return fract(sin(dot(p, vec2(127.1, 311.7))) * 43758.5453);
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);
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);
83constexpr const char* kCloudVertexShader = R
"(#version 330
84in vec3 vertexPosition;
90 world = (matModel * vec4(vertexPosition, 1.0)).xyz;
91 gl_Position = mvp * vec4(vertexPosition, 1.0);
95constexpr const char* kCloudFragmentShader = R
"(#version 330
101uniform float fadeNear;
102uniform float fadeFar;
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);
121 explicit Random(uint32_t seed) : state_(seed) {}
123 float Between(
float low,
float high) {
124 state_ += 0x9E3779B9u;
126 z = (z ^ (z >> 16)) * 0x85EBCA6Bu;
127 z = (z ^ (z >> 13)) * 0xC2B2AE35u;
129 return low + (high - low) *
static_cast<float>(z >> 8) / 16777216.0f;
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);
141void SetFloat(Shader shader,
const char* name,
float value) {
142 SetShaderValue(shader, GetShaderLocation(shader, name), &value,
143 SHADER_UNIFORM_FLOAT);
149 : dome_shader_(
LoadGlsl(kDomeVertexShader, kDomeFragmentShader)),
150 cloud_shader_(
LoadGlsl(kCloudVertexShader, kCloudFragmentShader)),
151 dome_(LoadModelFromMesh(GenMeshSphere(1.0f, 24, 32))),
153 puff_(LoadModelFromMesh(GenMeshSphere(1.0f, 5, 7))) {
154 dome_.materials[0].shader = dome_shader_;
155 puff_.materials[0].shader = cloud_shader_;
161 UnloadShader(dome_shader_);
162 UnloadShader(cloud_shader_);
166 Random random(seed * 7919u + 17u);
168 for (
int i = 0; i <
kClouds; ++i) {
172 cloud.height = random.Between(kLowest, kHighest);
173 cloud.rank = random.Between(0.0f, 1.0f);
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}});
190 clouds_.push_back(std::move(cloud));
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) {
200 return {camera.position.x + wrap(at.x - camera.position.x),
201 camera.position.z + wrap(at.y - camera.position.z)};
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);
215 rlDisableBackfaceCulling();
216 rlDisableDepthMask();
217 DrawModelEx(dome_, camera.position, {0.0f, 1.0f, 0.0f}, 0.0f,
218 {kDome, kDome, kDome}, WHITE);
220 rlEnableBackfaceCulling();
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);
229 SetFloat(cloud_shader_,
"fadeNear", 0.28f *
kSpread);
230 SetFloat(cloud_shader_,
"fadeFar", 0.48f *
kSpread);
231 for (
const Cloud& cloud : clouds_) {
235 const Vector2 at = Blown(cloud, camera, seconds);
236 for (
const Puff& puff : cloud.puffs) {
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);
246 const Atmosphere& atmosphere,
float seconds)
const {
247 std::vector<Vector4> balls;
248 for (
const Cloud& cloud : clouds_) {
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});
259 const auto away = [&](
const Vector4& ball) {
260 return Vector2Distance({ball.x, ball.y},
261 {camera.target.x, camera.target.z});
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),
267 SetShaderValueV(shader, GetShaderLocation(shader,
"clouds"), shadows.data(),
269 SetFloat(shader,
"cloudShade", kShade);
void Shade(Shader shader, const Camera3D &camera, const Atmosphere &atmosphere, float seconds) const
static constexpr Vector2 kWind
In metres a second, which way the wind blows the clouds.
static constexpr int kClouds
The most clouds a land has, all of them on a cloudy day.
static constexpr float kSpread
void Gather(uint32_t seed)
static constexpr int kShadows
void Draw(const Camera3D &camera, const Atmosphere &atmosphere, float seconds) const
Shader LoadGlsl(const char *vertex, const char *fragment)
The sky and the light, which the shaders read each frame.
std::array< float, 3 > zenith
std::array< float, 3 > sky
The background, which the fog fades into.
std::array< float, 3 > disc
std::array< float, 3 > clouds
std::array< float, 3 > sun
Towards the sun.