载入中...
搜索中...
未找到
DayNight.cpp
浏览该文件的文档.
1#include "daynight/DayNight.h"
3
4#include "graphics/Graphics.h"
6#include "graphics/Light.h"
8#include "graphics/Texture.h"
9
10#include <cmath>
11#include <cstdint>
12#include <cstring>
13#include <string>
14#include <vector>
15
16#include <simplesquirrel/simplesquirrel.hpp>
17
18namespace eve::daynight {
19
20namespace {
21
22// Solar orbit constants.
23constexpr float kPi = 3.14159265f;
24constexpr float kMaxElevationDeg = 70.f; // solar elevation at local noon
25constexpr float kSkyCubeSize = 128; // per-face resolution of the procedural sky
26constexpr int kMaxFireflies = 8;
27
28// Night light names (script-facing) — index maps to the Impl flags array.
29
30inline float deg2rad(float d) { return d * kPi / 180.f; }
31inline float smoothstep(float e0, float e1, float x) {
32 if (std::fabs(e1 - e0) < 1e-7f) return x < e0 ? 0.f : 1.f;
33 const float t = std::clamp((x - e0) / (e1 - e0), 0.f, 1.f);
34 return t * t * (3.f - 2.f * t);
35}
36
37struct Vec3 {
38 float x, y, z;
39};
40
41inline Vec3 add(Vec3 a, Vec3 b) { return {a.x + b.x, a.y + b.y, a.z + b.z}; }
42inline Vec3 mul(Vec3 a, Vec3 b) { return {a.x * b.x, a.y * b.y, a.z * b.z}; }
43inline Vec3 scale(Vec3 a, float s) { return {a.x * s, a.y * s, a.z * s}; }
44
45// Compact single-scattering atmosphere approximation. The wavelength-dependent
46// coefficients preserve the important physical relationships (blue Rayleigh sky,
47// neutral Mie haze, warm attenuated low sun) while remaining cheap enough to build
48// the IBL cubemap on the CPU. It is deliberately isolated so a future GPU LUT
49// implementation can consume the same public atmosphere parameters.
50Vec3 atmosphereRadiance(Vec3 view, Vec3 sun, float turbidity, float mieStrength) {
51 const float mu = std::clamp(view.x * sun.x + view.y * sun.y + view.z * sun.z, -1.f, 1.f);
52 const float horizonMass = 1.f / std::max(0.08f, view.y + 0.14f);
53 const float sunMass = 1.f / std::max(0.06f, sun.y + 0.12f);
54 const Vec3 betaR{0.055f, 0.130f, 0.285f};
55 const Vec3 betaM = scale(Vec3{0.18f, 0.17f, 0.15f}, mieStrength * (0.35f + turbidity * 0.09f));
56 const Vec3 extinction = add(betaR, betaM);
57 const Vec3 viewT{std::exp(-extinction.x * horizonMass),
58 std::exp(-extinction.y * horizonMass),
59 std::exp(-extinction.z * horizonMass)};
60 const Vec3 sunT{std::exp(-extinction.x * sunMass),
61 std::exp(-extinction.y * sunMass),
62 std::exp(-extinction.z * sunMass)};
63 const float rayleighPhase = 3.f / (16.f * kPi) * (1.f + mu * mu);
64 const float g = std::clamp(0.72f + turbidity * 0.008f, 0.72f, 0.82f);
65 const float gg = g * g;
66 const float miePhase = (1.f - gg) /
67 (4.f * kPi * std::pow(std::max(0.015f, 1.f + gg - 2.f * g * mu), 1.5f));
68 const Vec3 scatter = add(scale(betaR, rayleighPhase * 13.f),
69 scale(betaM, miePhase * 2.2f));
70 Vec3 sky = mul(mul(scatter, sunT), Vec3{1.f - viewT.x, 1.f - viewT.y, 1.f - viewT.z});
71 // Multiple-scattering floor prevents a black antisolar horizon and approximates
72 // light returned by the ground/atmosphere without an expensive integral.
73 sky = add(sky, scale(mul(sunT, Vec3{0.18f, 0.22f, 0.30f}),
74 0.12f + 0.18f * (1.f - std::max(view.y, 0.f))));
75 return sky;
76}
77
78Vec3 toneMapSky(Vec3 c, float exposure) {
79 c = scale(c, std::max(exposure, 0.01f));
80 // ACES fitted curve, followed by display gamma. Keeps a smooth solar halo in
81 // the current RGBA8 backend rather than clipping radiance before conversion.
82 auto channel = [](float x) {
83 x = std::clamp((x * (2.51f * x + 0.03f)) /
84 (x * (2.43f * x + 0.59f) + 0.14f), 0.f, 1.f);
85 return std::pow(x, 1.f / 2.2f);
86 };
87 return {channel(c.x), channel(c.y), channel(c.z)};
88}
89
90Vec3 attenuatedSunColor(float sunElevation, float turbidity, float mieStrength) {
91 const float mass = 1.f / std::max(0.06f, sunElevation + 0.12f);
92 const Vec3 extinction = add(Vec3{0.055f, 0.130f, 0.285f},
93 scale(Vec3{0.18f, 0.17f, 0.15f}, mieStrength * (0.35f + turbidity * 0.09f)));
94 Vec3 c{std::exp(-extinction.x * mass), std::exp(-extinction.y * mass),
95 std::exp(-extinction.z * mass)};
96 const float maxChannel = std::max({c.x, c.y, c.z, 1e-5f});
97 return scale(c, 1.f / maxChannel);
98}
99
100Vec3 correlatedColorTemperature(float kelvin) {
101 const float t = std::clamp(kelvin, 1000.f, 40000.f) / 100.f;
102 const float r = t <= 66.f ? 1.f : 1.2929362f * std::pow(t - 60.f, -0.13320476f);
103 const float g = t <= 66.f ? 0.39008158f * std::log(t) - 0.63184144f
104 : 1.1298909f * std::pow(t - 60.f, -0.07551485f);
105 const float b = t >= 66.f ? 1.f
106 : t <= 19.f ? 0.f
107 : 0.5432068f * std::log(t - 10.f) - 1.1962541f;
108 return {std::clamp(r, 0.f, 1.f), std::clamp(g, 0.f, 1.f),
109 std::clamp(b, 0.f, 1.f)};
110}
111
112Vec3 manualSunColor(const PcgManualSunState &sun) {
113 const Vec3 temperature = correlatedColorTemperature(sun.kelvin);
114 return {sun.red * temperature.x, sun.green * temperature.y,
115 sun.blue * temperature.z};
116}
117
118// Convert an elevation/azimuth to a unit direction pointing at the sun.
119// azimuth measured clockwise from +Z, elevation above the horizon.
120inline void sunDirection(float elevDeg, float azimDeg, float &dx, float &dy, float &dz) {
121 const float el = deg2rad(elevDeg);
122 const float az = deg2rad(azimDeg);
123 const float he = std::cos(el);
124 dx = he * std::sin(az);
125 dy = std::sin(el);
126 dz = he * std::cos(az);
127}
128
129// Tiny deterministic hash for stars (no <random> dependency).
130inline uint32_t hash13(uint32_t x) {
131 x ^= x >> 16;
132 x *= 0x7feb352du;
133 x ^= x >> 15;
134 x *= 0x846ca68bu;
135 x ^= x >> 16;
136 return x;
137}
138inline float hashUnit(uint32_t x) { return float(hash13(x) % 10000u) / 9999.f; }
139
140// Fill one cubemap face's RGBA. `face` in {0..5} order +X,-X,+Y,-Y,+Z,-Z.
141// dirAt(x,y) writes the world direction (unnormalized ok) for a pixel.
142void fillSkyFace(std::vector<uint8_t> &px, int size, int face,
143 const float sunDir[3], const Vec3 &directSunColor, float sunEnergy,
144 float nightAmount,
145 float turbidity, float mieStrength, float exposure, float cloudiness,
146 float rotationDegrees, const Vec3 &tint,
147 void (*dirAt)(int face, int size, int x, int y, float out[3])) {
148 const int n = size;
149 for (int y = 0; y < n; ++y) {
150 for (int x = 0; x < n; ++x) {
151 float d[3];
152 dirAt(face, n, x, y, d);
153 const float len = std::sqrt(d[0] * d[0] + d[1] * d[1] + d[2] * d[2]);
154 if (len < 1e-6f) { d[0] = 0.f; d[1] = 1.f; d[2] = 0.f; }
155 else { d[0] /= len; d[1] /= len; d[2] /= len; }
156 if (rotationDegrees != 0.f) {
157 const float angle = deg2rad(rotationDegrees);
158 const float x = d[0] * std::cos(angle) + d[2] * std::sin(angle);
159 d[2] = -d[0] * std::sin(angle) + d[2] * std::cos(angle);
160 d[0] = x;
161 }
162
163 const float up = d[1];
164 const Vec3 view{d[0], std::max(d[1], 0.002f), d[2]};
165 const Vec3 sun{sunDir[0], sunDir[1], sunDir[2]};
166 Vec3 day = atmosphereRadiance(view, sun, turbidity, mieStrength);
167 Vec3 night{0.006f + 0.012f * std::max(up, 0.f),
168 0.010f + 0.020f * std::max(up, 0.f),
169 0.035f + 0.070f * std::max(up, 0.f)};
170 Vec3 color = add(scale(day, (1.f - nightAmount) * sunEnergy),
171 scale(night, nightAmount));
172
173 // Sun disc: a tight highlight around the sun direction.
174 const float dot = d[0] * sunDir[0] + d[1] * sunDir[1] + d[2] * sunDir[2];
175 // Physical solar angular radius is about 0.27 degrees. Slightly enlarge it
176 // to remain stable in a 128px cubemap; the Mie term supplies the broad halo.
177 const float disc = smoothstep(std::cos(deg2rad(0.65f)),
178 std::cos(deg2rad(0.35f)), dot) * sunEnergy;
179 const float sunMass = 1.f / std::max(0.06f, sun.y + 0.12f);
180 const Vec3 atmosphericSun{std::exp(-0.16f * sunMass),
181 std::exp(-0.28f * sunMass),
182 std::exp(-0.58f * sunMass)};
183 const Vec3 sunColor{atmosphericSun.x * directSunColor.x,
184 atmosphericSun.y * directSunColor.y,
185 atmosphericSun.z * directSunColor.z};
186 color = add(color, scale(sunColor, disc * 12.f));
187
188 // Stars (only at night, only in the sky hemisphere, avoid the sun).
189 float star = 0.f;
190 if (nightAmount > 0.5f && up > 0.05f && dot < 0.98f) {
191 uint32_t h = hash13(uint32_t((x * 73856093) ^ (y * 19349663) ^ (face * 83492791)));
192 if (h % 40u == 0u) {
193 const float tw = 0.6f + 0.4f * hashUnit(h + 1u);
194 star = nightAmount * tw;
195 }
196 }
197 color = add(color, scale(Vec3{0.9f, 0.95f, 1.f}, star * (1.f - cloudiness)));
198 const Vec3 overcast{0.075f + 0.035f * std::max(up, 0.f),
199 0.090f + 0.045f * std::max(up, 0.f),
200 0.125f + 0.060f * std::max(up, 0.f)};
201 color = add(scale(color, 1.f - cloudiness * 0.82f),
202 scale(overcast, cloudiness));
203 color = {color.x * tint.x, color.y * tint.y, color.z * tint.z};
204 color = toneMapSky(color, exposure);
205
206 const int i = (y * n + x) * 4;
207 px[i + 0] = uint8_t(std::clamp(color.x * 255.f, 0.f, 255.f));
208 px[i + 1] = uint8_t(std::clamp(color.y * 255.f, 0.f, 255.f));
209 px[i + 2] = uint8_t(std::clamp(color.z * 255.f, 0.f, 255.f));
210 px[i + 3] = 255;
211 }
212 }
213}
214
215// Standard cubemap direction mapping for each face (u,v in [0,size]).
216void cubeDir(int face, int size, int x, int y, float out[3]) {
217 const float u = (2.f * (float(x) + 0.5f) / float(size)) - 1.f; // -1..1
218 const float v = (2.f * (float(y) + 0.5f) / float(size)) - 1.f; // -1..1
219 switch (face) {
220 case 0: out[0] = 1.f; out[1] = -v; out[2] = -u; break; // +X
221 case 1: out[0] = -1.f; out[1] = -v; out[2] = u; break; // -X
222 case 2: out[0] = u; out[1] = 1.f; out[2] = v; break; // +Y
223 case 3: out[0] = u; out[1] = -1.f; out[2] = -v; break; // -Y
224 case 4: out[0] = u; out[1] = -v; out[2] = 1.f; break; // +Z
225 default: out[0] = -u; out[1] = -v; out[2] = -1.f; break; // -Z
226 }
227}
228
229} // namespace
230
233 bool built = false;
234
235 // clock
236 float timeOfDay = 9.f; // hours 0..24
237 float speed = 0.5f; // simulated hours per real second
238 bool paused = false;
239
240 // derived sun
241 float elevDeg = 0.f;
242 float azimDeg = 0.f;
243 float sunDir[3] = {0.f, 1.f, 0.f};
244 float sunEnergy = 1.f;
245 float turbidity = 2.5f;
246 float skyExposure = 1.f;
247 float mieStrength = 1.f;
248 float weatherCloudiness = 0.f;
249 float weatherFlash = 0.f;
255 bool pcgFogWasActive = false;
256 std::string pcgFogBaseMode = "screenspace";
257 std::string pcgFogBaseQuality = "medium";
258 float pcgFogBaseDensity = 0.85f;
259 float pcgFogBaseStart = 2.f;
260 float pcgFogBaseEnd = 40.f;
261
262 // sky cache (regenerate only when the sun bucket changes)
263 bool skyboxEnabled = true;
265 std::array<graphics::Texture *, 6> skyFaces{};
266 std::array<float, 6> skyFaceCenterLuminance{};
268
269 // night lights
270 bool nightLight[4] = {true, true, false, true}; // moonlight, starlight, fire, fireflies
271 float fireX = 0.f, fireY = 0.5f, fireZ = 0.f;
272
273 struct Fly {
274 float x, y, z; // base anchor
275 float seed; // animation phase
276 };
277 std::vector<Fly> flies;
280 std::vector<graphics::Light3D *> flyLights;
281
282 void refreshSun() {
283 const float fraction = (timeOfDay - 6.f) / 12.f;
284 elevDeg = kMaxElevationDeg * std::sin(kPi * fraction);
285 azimDeg = (timeOfDay / 24.f) * 360.f;
286 if (manualSun.enabled) {
287 const float pitch = deg2rad(manualSun.pitchDegrees);
288 const float rotation = deg2rad(manualSun.rotationDegrees);
289 sunDir[0] = -std::cos(pitch) * std::sin(rotation);
290 sunDir[1] = std::sin(pitch);
291 sunDir[2] = -std::cos(pitch) * std::cos(rotation);
292 elevDeg = std::asin(std::clamp(sunDir[1], -1.f, 1.f)) * 180.f / kPi;
293 azimDeg = std::atan2(sunDir[0], sunDir[2]) * 180.f / kPi;
294 if (azimDeg < 0.f) azimDeg += 360.f;
295 } else {
296 sunDirection(elevDeg, azimDeg, sunDir[0], sunDir[1], sunDir[2]);
297 }
298 sunEnergy = manualSun.enabled ? manualSun.intensity : std::clamp((elevDeg + 6.f) / 14.f, 0.f, 1.f);
299 }
300};
301
302const char *const DayNight::kNamedLights[] = {"moonlight", "starlight", "fire", "fireflies"};
303const int DayNight::kNamedLightCount = 4;
304
305DayNight::DayNight() : impl_(new Impl()) { impl_->refreshSun(); }
306DayNight::~DayNight() { delete impl_; }
307
308// ---------------------------------------------------------------------------
309// Clock / derived state
310// ---------------------------------------------------------------------------
311
312void DayNight::setTimeOfDay(float hours) {
313 float h = std::fmod(hours, 24.f);
314 if (h < 0.f) h += 24.f;
315 impl_->timeOfDay = h;
316 impl_->refreshSun();
317}
318float DayNight::getTimeOfDay() const { return impl_->timeOfDay; }
319
320void DayNight::setSpeed(float hprs) { impl_->speed = hprs < 0.f ? 0.f : hprs; }
321float DayNight::getSpeed() const { return impl_->speed; }
322void DayNight::setPaused(bool p) { impl_->paused = p; }
323bool DayNight::isPaused() const { return impl_->paused; }
324
325bool DayNight::isNight() const { return impl_->elevDeg < 0.f; }
326
327float DayNight::getSunElevation() const { return impl_->elevDeg; }
328float DayNight::getSunAzimuth() const { return impl_->azimDeg; }
329float DayNight::getSunDirX() const { return impl_->sunDir[0]; }
330float DayNight::getSunDirY() const { return impl_->sunDir[1]; }
331float DayNight::getSunDirZ() const { return impl_->sunDir[2]; }
332float DayNight::getSunIntensity() const { return impl_->sunEnergy; }
333float DayNight::getSunR() const {
334 const Vec3 color = impl_->manualSun.enabled
335 ? manualSunColor(impl_->manualSun)
336 : attenuatedSunColor(impl_->sunDir[1], impl_->turbidity,
337 impl_->mieStrength);
338 const float multiplier = impl_->pcgAmbient.active
340 : 1.f;
341 return color.x * impl_->sunEnergy * (1.f - 0.82f * impl_->weatherCloudiness) * multiplier;
342}
343float DayNight::getSunG() const {
344 const Vec3 color = impl_->manualSun.enabled
345 ? manualSunColor(impl_->manualSun)
346 : attenuatedSunColor(impl_->sunDir[1], impl_->turbidity,
347 impl_->mieStrength);
348 const float multiplier = impl_->pcgAmbient.active
350 : 1.f;
351 return color.y * impl_->sunEnergy * (1.f - 0.82f * impl_->weatherCloudiness) * multiplier;
352}
353float DayNight::getSunB() const {
354 const Vec3 color = impl_->manualSun.enabled
355 ? manualSunColor(impl_->manualSun)
356 : attenuatedSunColor(impl_->sunDir[1], impl_->turbidity,
357 impl_->mieStrength);
358 const float multiplier = impl_->pcgAmbient.active
360 : 1.f;
361 return color.z * impl_->sunEnergy * (1.f - 0.82f * impl_->weatherCloudiness) * multiplier;
362}
363
365 const bool finite = std::isfinite(state.pitchDegrees) &&
366 std::isfinite(state.rotationDegrees) &&
367 std::isfinite(state.intensity) && std::isfinite(state.red) &&
368 std::isfinite(state.green) && std::isfinite(state.blue) &&
369 std::isfinite(state.kelvin);
370 if (!finite || state.pitchDegrees < 0.f || state.pitchDegrees > 360.f ||
371 state.rotationDegrees < 0.f || state.rotationDegrees > 360.f ||
372 state.intensity < 0.f || state.intensity > 8.f || state.red < 0.f ||
373 state.green < 0.f || state.blue < 0.f || state.kelvin < 1500.f ||
374 state.kelvin > 20000.f) {
376 DiagnosticCode::InvalidArgument, "invalid Pcg manual-sun state", {}, {},
377 "daynight.pcgManualSun"));
378 }
379 impl_->manualSun = state;
380 impl_->refreshSun();
381 impl_->lastSkyBucket = -1;
382 return Result<void>::success();
383}
384
385PcgManualSunState DayNight::getPcgManualSun() const noexcept { return impl_->manualSun; }
386
388 const bool finite = std::isfinite(state.rotationDegrees) &&
389 std::isfinite(state.exposure) && std::isfinite(state.tintRed) &&
390 std::isfinite(state.tintGreen) && std::isfinite(state.tintBlue);
391 if (!finite || state.rotationDegrees < 0.f || state.rotationDegrees > 360.f ||
392 state.exposure < 0.f || state.exposure > 30.f || state.tintRed < 0.f ||
393 state.tintGreen < 0.f || state.tintBlue < 0.f) {
395 DiagnosticCode::InvalidArgument, "invalid Pcg skybox state", {}, {},
396 "daynight.pcgSkybox"));
397 }
398 impl_->pcgSkybox = state;
399 impl_->lastSkyBucket = -1;
400 return Result<void>::success();
401}
402
403PcgSkyboxState DayNight::getPcgSkybox() const noexcept { return impl_->pcgSkybox; }
404
406 const float values[] = {state.additionalLinearDistance, state.additionalExponentialDensity,
407 state.red, state.green, state.blue, state.density,
408 state.startDistance, state.endDistance,
409 state.globalDensityMultiplier, state.densityAlbedoRed,
410 state.densityAlbedoGreen, state.densityAlbedoBlue,
411 state.densityVolumeDistance};
412 for (float value : values)
413 if (!std::isfinite(value))
415 DiagnosticCode::InvalidArgument, "Pcg fog values must be finite", {}, {},
416 "daynight.pcgFog"));
417 const bool invalid = state.additionalLinearDistance < -5000.f ||
418 state.additionalLinearDistance > 5000.f ||
419 state.additionalExponentialDensity < 0.f ||
420 state.additionalExponentialDensity > 0.05f || state.mode < 0 ||
421 state.mode > 2 || state.red < 0.f || state.green < 0.f ||
422 state.blue < 0.f || state.density < 0.f || state.density > 0.05f ||
423 state.startDistance < 0.f || state.startDistance > 5000.f ||
424 state.endDistance < 0.f || state.endDistance > 5000.f ||
425 state.globalDensityMultiplier < 0.f ||
426 state.globalDensityMultiplier > 5.f ||
427 state.densityAlbedoRed < 0.f || state.densityAlbedoGreen < 0.f ||
428 state.densityAlbedoBlue < 0.f || state.densityVolumeDistance < 0.01f ||
429 state.densityVolumeDistance > 1500.f ||
430 state.densityVolumeEffect < 0 || state.densityVolumeEffect > 4 ||
431 state.densityVolumeTiling < 0 || state.densityVolumeTiling > 5;
432 if (invalid)
434 DiagnosticCode::InvalidArgument, "invalid Pcg fog state", {}, {},
435 "daynight.pcgFog"));
436 impl_->pcgFog = state;
437 return Result<void>::success();
438}
439
440PcgFogState DayNight::getPcgFog() const noexcept { return impl_->pcgFog; }
441
443 const float values[] = {state.intensity, state.skyRed, state.skyGreen, state.skyBlue,
444 state.equatorRed, state.equatorGreen, state.equatorBlue,
445 state.groundRed, state.groundGreen, state.groundBlue,
446 state.globalLightMultiplier};
447 for (float value : values) {
448 if (!std::isfinite(value) || value < 0.f)
451 "Pcg ambient-light values must be finite and non-negative", {}, {},
452 "daynight.pcgAmbientLight"));
453 }
454 if (state.intensity > 10.f || state.globalLightMultiplier > 5.f)
456 DiagnosticCode::InvalidArgument, "invalid Pcg ambient-light range", {}, {},
457 "daynight.pcgAmbientLight"));
458 impl_->pcgAmbient = state;
459 return Result<void>::success();
460}
461
463 return impl_->pcgAmbient;
464}
466 impl_->turbidity = std::clamp(v, 1.5f, 10.f);
467 impl_->lastSkyBucket = -1;
468}
469float DayNight::getTurbidity() const { return impl_->turbidity; }
471 impl_->skyExposure = std::clamp(v, 0.05f, 8.f);
472 impl_->lastSkyBucket = -1;
473}
474float DayNight::getSkyExposure() const { return impl_->skyExposure; }
476 impl_->mieStrength = std::clamp(v, 0.f, 4.f);
477 impl_->lastSkyBucket = -1;
478}
479float DayNight::getMieStrength() const { return impl_->mieStrength; }
480
481// Sky / ambient colors are functions of the sun energy and night amount.
482float DayNight::getSkyR() const {
483 const float angle = deg2rad(impl_->pcgSkybox.enabled ? impl_->pcgSkybox.rotationDegrees : 0.f);
484 const Vec3 c = toneMapSky(atmosphereRadiance({std::sin(angle), 0.04f, std::cos(angle)},
485 {impl_->sunDir[0], impl_->sunDir[1], impl_->sunDir[2]}, impl_->turbidity,
486 impl_->mieStrength), impl_->pcgSkybox.enabled ? impl_->pcgSkybox.exposure : impl_->skyExposure);
487 const float clear = c.x * impl_->sunEnergy + 0.012f * (1.f - impl_->sunEnergy);
488 const float result = clear * (1.f - impl_->weatherCloudiness * 0.72f) +
489 impl_->weatherCloudiness * 0.075f + impl_->weatherFlash * 0.38f;
490 return result * (impl_->pcgSkybox.enabled ? impl_->pcgSkybox.tintRed : 1.f);
491}
492float DayNight::getSkyG() const {
493 const float angle = deg2rad(impl_->pcgSkybox.enabled ? impl_->pcgSkybox.rotationDegrees : 0.f);
494 const Vec3 c = toneMapSky(atmosphereRadiance({std::sin(angle), 0.04f, std::cos(angle)},
495 {impl_->sunDir[0], impl_->sunDir[1], impl_->sunDir[2]}, impl_->turbidity,
496 impl_->mieStrength), impl_->pcgSkybox.enabled ? impl_->pcgSkybox.exposure : impl_->skyExposure);
497 const float clear = c.y * impl_->sunEnergy + 0.020f * (1.f - impl_->sunEnergy);
498 const float result = clear * (1.f - impl_->weatherCloudiness * 0.72f) +
499 impl_->weatherCloudiness * 0.090f + impl_->weatherFlash * 0.48f;
500 return result * (impl_->pcgSkybox.enabled ? impl_->pcgSkybox.tintGreen : 1.f);
501}
502float DayNight::getSkyB() const {
503 const float angle = deg2rad(impl_->pcgSkybox.enabled ? impl_->pcgSkybox.rotationDegrees : 0.f);
504 const Vec3 c = toneMapSky(atmosphereRadiance({std::sin(angle), 0.04f, std::cos(angle)},
505 {impl_->sunDir[0], impl_->sunDir[1], impl_->sunDir[2]}, impl_->turbidity,
506 impl_->mieStrength), impl_->pcgSkybox.enabled ? impl_->pcgSkybox.exposure : impl_->skyExposure);
507 const float clear = c.z * impl_->sunEnergy + 0.060f * (1.f - impl_->sunEnergy);
508 const float result = clear * (1.f - impl_->weatherCloudiness * 0.62f) +
509 impl_->weatherCloudiness * 0.125f + impl_->weatherFlash * 0.68f;
510 return result * (impl_->pcgSkybox.enabled ? impl_->pcgSkybox.tintBlue : 1.f);
511}
513 const float night = impl_->nightLight[1] ? 1.0f : 0.6f; // starlight boost
514 return 0.05f * night + impl_->sunEnergy * 0.5f *
515 (1.f - impl_->weatherCloudiness * 0.68f) + impl_->weatherFlash * 0.45f;
516}
517
518void DayNight::setWeatherInfluence(float cloudiness, float lightningFlash) {
519 const float nextCloudiness = std::clamp(cloudiness, 0.f, 1.f);
520 if (std::fabs(nextCloudiness - impl_->weatherCloudiness) >= 0.04f)
521 impl_->lastSkyBucket = -1;
522 impl_->weatherCloudiness = nextCloudiness;
523 impl_->weatherFlash = std::clamp(lightningFlash, 0.f, 1.f);
524}
525float DayNight::getWeatherCloudiness() const { return impl_->weatherCloudiness; }
526float DayNight::getWeatherFlash() const { return impl_->weatherFlash; }
528 if (impl_->pcgAmbient.active) {
529 const auto &a = impl_->pcgAmbient;
530 return (a.skyRed * 0.5f + a.equatorRed * 0.35f + a.groundRed * 0.15f) *
531 a.intensity;
532 }
533 const float ab = getAmbientBrightness();
534 return ab * 0.95f;
535}
537 if (impl_->pcgAmbient.active) {
538 const auto &a = impl_->pcgAmbient;
539 return (a.skyGreen * 0.5f + a.equatorGreen * 0.35f + a.groundGreen * 0.15f) *
540 a.intensity;
541 }
542 const float ab = getAmbientBrightness();
543 return ab * (0.95f + 0.05f * impl_->sunEnergy); // greener in daylight
544}
546 if (impl_->pcgAmbient.active) {
547 const auto &a = impl_->pcgAmbient;
548 return (a.skyBlue * 0.5f + a.equatorBlue * 0.35f + a.groundBlue * 0.15f) *
549 a.intensity;
550 }
551 const float ab = getAmbientBrightness();
552 return ab * (0.95f + 0.15f * impl_->sunEnergy); // bluer in daylight
553}
554
556 if (!fog) return;
558 fog->setFogColor(getSkyR(), getSkyG(), getSkyB());
559 fog->setCloudLightColor(getSunR() + impl_->weatherFlash * 0.5f,
560 getSunG() + impl_->weatherFlash * 0.65f,
561 getSunB() + impl_->weatherFlash * 0.9f);
562 fog->setIntensity(std::max(0.08f, impl_->sunEnergy + impl_->weatherFlash));
563 fog->setTime(impl_->timeOfDay * 18.f);
564 const PcgFogState &pcg = impl_->pcgFog;
565 const bool hasWeatherOffset = pcg.additionalLinearDistance != 0.f ||
567 const bool pcgFogActive = pcg.overrideDensityVolume || pcg.overrideFog ||
568 hasWeatherOffset;
569 if (impl_->pcgFogTarget != fog) {
570 impl_->pcgFogTarget = fog;
571 impl_->pcgFogWasActive = false;
572 }
573 if (pcgFogActive && !impl_->pcgFogWasActive) {
574 impl_->pcgFogBaseMode = fog->getMode();
575 impl_->pcgFogBaseQuality = fog->getQuality();
576 impl_->pcgFogBaseDensity = fog->getFloat("density");
577 impl_->pcgFogBaseStart = fog->getFloat("fogStart");
578 impl_->pcgFogBaseEnd = fog->getFloat("fogEnd");
579 } else if (!pcgFogActive && impl_->pcgFogWasActive) {
580 fog->setMode(impl_->pcgFogBaseMode);
581 fog->setQuality(impl_->pcgFogBaseQuality);
582 fog->setDensity(impl_->pcgFogBaseDensity);
583 fog->setFogStart(impl_->pcgFogBaseStart);
584 fog->setFogEnd(impl_->pcgFogBaseEnd);
585 }
586 impl_->pcgFogWasActive = pcgFogActive;
587 if (pcg.overrideDensityVolume) {
588 static constexpr float hazeDensity[] = {0.0025f, 0.005f, 0.01f, 0.02f, 0.04f};
589 fog->setMode("fog");
590 fog->setQuality(pcg.densityVolumeTiling < 2 ? "low" :
591 pcg.densityVolumeTiling < 4 ? "medium" : "high");
594 fog->setDensity(hazeDensity[pcg.densityVolumeEffect] *
596 fog->setFogStart(0.f);
598 } else if (pcg.overrideFog || hasWeatherOffset) {
599 fog->setMode("fog");
600 fog->setFogColor(pcg.red, pcg.green, pcg.blue);
601 const float modeScale = pcg.mode == 2 ? 1.5f : 1.f;
603 pcg.globalDensityMultiplier * modeScale);
604 const float start = std::max(0.f, pcg.startDistance + pcg.additionalLinearDistance);
605 fog->setFogStart(start);
606 fog->setFogEnd(std::max(start + 1.f,
608 }
609}
610
612 if (!probe) return;
613 static constexpr Vec3 directions[6] = {
614 {1.f, 0.f, 0.f}, {-1.f, 0.f, 0.f}, {0.f, 1.f, 0.f},
615 {0.f, -1.f, 0.f}, {0.f, 0.f, 1.f}, {0.f, 0.f, -1.f},
616 };
617 const Vec3 sun{impl_->sunDir[0], impl_->sunDir[1], impl_->sunDir[2]};
618 const float nightAmount = std::clamp((-impl_->elevDeg) / 12.f, 0.f, 1.f);
619 const float skyRotation = deg2rad(
620 impl_->pcgSkybox.enabled ? impl_->pcgSkybox.rotationDegrees : 0.f);
621 const Vec3 skyTint = impl_->pcgSkybox.enabled
622 ? Vec3{impl_->pcgSkybox.tintRed, impl_->pcgSkybox.tintGreen,
623 impl_->pcgSkybox.tintBlue}
624 : Vec3{1.f, 1.f, 1.f};
625 for (int face = 0; face < 6; ++face) {
626 Vec3 direction = directions[face];
627 const float rotatedX = direction.x * std::cos(skyRotation) +
628 direction.z * std::sin(skyRotation);
629 direction.z = -direction.x * std::sin(skyRotation) +
630 direction.z * std::cos(skyRotation);
631 direction.x = rotatedX;
632 const float up = direction.y;
633 const Vec3 view{direction.x, std::max(direction.y, 0.002f), direction.z};
634 const Vec3 day = atmosphereRadiance(view, sun, impl_->turbidity, impl_->mieStrength);
635 const Vec3 night{0.006f + 0.012f * std::max(up, 0.f),
636 0.010f + 0.020f * std::max(up, 0.f),
637 0.035f + 0.070f * std::max(up, 0.f)};
638 Vec3 color = add(scale(day, (1.f - nightAmount) * impl_->sunEnergy),
639 scale(night, nightAmount));
640 const Vec3 overcast{0.075f + 0.035f * std::max(up, 0.f),
641 0.090f + 0.045f * std::max(up, 0.f),
642 0.125f + 0.060f * std::max(up, 0.f)};
643 color = add(scale(color, 1.f - impl_->weatherCloudiness * 0.82f),
644 scale(overcast, impl_->weatherCloudiness));
645 color = add(color, scale(Vec3{0.75f, 0.90f, 1.20f}, impl_->weatherFlash));
647 : impl_->skyExposure);
648 color = {color.x * skyTint.x, color.y * skyTint.y, color.z * skyTint.z};
649 probe->setSkyFaceColor(face, color.x, color.y, color.z);
650 probe->setSkyFaceTexture(face, impl_->skyFaces[static_cast<size_t>(face)]);
651 const float linearLuminance =
652 color.x * 0.2126f + color.y * 0.7152f + color.z * 0.0722f;
653 const float encodedLuminance =
654 impl_->skyFaceCenterLuminance[static_cast<size_t>(face)];
656 face, encodedLuminance > 1e-4f ? linearLuminance / encodedLuminance : 1.f);
657 }
658 probe->setEnvironmentLighting(impl_->skyCube, 0.5f + 0.5f * impl_->sunEnergy);
659}
660
661// ---------------------------------------------------------------------------
662// Skybox
663// ---------------------------------------------------------------------------
664
666 impl_->skyboxEnabled = enabled;
667 impl_->lastSkyBucket = -1; // force regenerate if re-enabled
668}
669bool DayNight::isSkyboxEnabled() const { return impl_->skyboxEnabled; }
670
671// ---------------------------------------------------------------------------
672// Night lights
673// ---------------------------------------------------------------------------
674
675void DayNight::setNightLight(const std::string &name, bool enabled) {
676 for (int i = 0; i < kNamedLightCount; ++i) {
677 if (name == kNamedLights[i]) {
678 impl_->nightLight[i] = enabled;
679 return;
680 }
681 }
682}
683bool DayNight::isNightLight(const std::string &name) const {
684 for (int i = 0; i < kNamedLightCount; ++i) {
685 if (name == kNamedLights[i]) return impl_->nightLight[i];
686 }
687 return false;
688}
689
690void DayNight::setFirePosition(float x, float y, float z) {
691 impl_->fireX = x; impl_->fireY = y; impl_->fireZ = z;
692 if (impl_->fireLight) {
693 impl_->fireLight->setPosition(x, y, z);
694 impl_->fireLight->setColor(1.0f, 0.55f, 0.2f, 1.2f);
695 impl_->fireLight->setRadius(6.f);
696 }
697}
698
699void DayNight::addFirefly(float x, float y, float z) {
700 if (int(impl_->flies.size()) >= kMaxFireflies) return;
701 Impl::Fly f;
702 f.x = x; f.y = y; f.z = z;
703 f.seed = float(impl_->flies.size()) * 1.7f;
704 impl_->flies.push_back(f);
705 if (impl_->built) {
707 l->setColor(0.6f, 0.9f, 0.3f, 0.9f);
708 l->setRadius(2.5f);
709 l->setEnabled(false);
710 l->setPosition(x, y, z);
711 impl_->flyLights.push_back(l);
712 }
713}
715 impl_->flies.clear();
716 impl_->flyLights.clear();
717}
718int DayNight::getFireflyCount() const { return int(impl_->flies.size()); }
719
720// ---------------------------------------------------------------------------
721// init / update
722// ---------------------------------------------------------------------------
723
725 if (impl_->built) return;
726 impl_->built = true;
727 impl_->gfx = gfx;
728 if (!gfx) return;
729
730 // Moon: a cool directional light, driven at night.
732 impl_->moonLight->setColor(0.55f, 0.65f, 0.9f, 0.35f);
733 impl_->moonLight->setDirection(0.2f, -0.8f, 0.4f);
734 impl_->moonLight->setEnabled(false);
735
736 // Fire: a warm point light (position set by setFirePosition).
738 impl_->fireLight->setColor(1.0f, 0.55f, 0.2f, 1.2f);
739 impl_->fireLight->setRadius(6.f);
740 impl_->fireLight->setPosition(impl_->fireX, impl_->fireY, impl_->fireZ);
741 impl_->fireLight->setEnabled(false);
742
743 // Fireflies.
744 for (const auto &f : impl_->flies) {
746 l->setColor(0.6f, 0.9f, 0.3f, 0.9f);
747 l->setRadius(2.5f);
748 l->setEnabled(false);
749 l->setPosition(f.x, f.y, f.z);
750 impl_->flyLights.push_back(l);
751 }
752}
753
755 if (!impl_->built) init(gfx);
756 if (!impl_->gfx) return;
757
758 if (!impl_->paused) {
759 impl_->timeOfDay += dt * impl_->speed;
760 impl_->timeOfDay = std::fmod(impl_->timeOfDay, 24.f);
761 if (impl_->timeOfDay < 0.f) impl_->timeOfDay += 24.f;
762 }
763 impl_->refreshSun();
764 const float elevDeg = impl_->elevDeg;
765 const float azimDeg = impl_->azimDeg;
766 const float nightAmount = std::clamp((-elevDeg) / 12.f, 0.f, 1.f);
767
768 // Push the directional sun (replaces the legacy directional when no other
769 // dir Light3D is active; we keep moon as a Light3D instead so it can have
770 // different color/intensity than the sun slot).
771 const Vec3 directSun = impl_->manualSun.enabled
772 ? manualSunColor(impl_->manualSun)
773 : attenuatedSunColor(impl_->sunDir[1], impl_->turbidity,
774 impl_->mieStrength);
775 const float weatherSun = 1.f - 0.82f * impl_->weatherCloudiness;
776 const Vec3 flashLight{impl_->weatherFlash * 0.75f, impl_->weatherFlash * 0.90f,
777 impl_->weatherFlash * 1.20f};
778 const float globalLight = impl_->pcgAmbient.active
780 : 1.f;
781 gfx->setDirectionalLight(impl_->sunDir[0], impl_->sunDir[1], impl_->sunDir[2],
782 directSun.x * impl_->sunEnergy * weatherSun * globalLight + flashLight.x,
783 directSun.y * impl_->sunEnergy * weatherSun * globalLight + flashLight.y,
784 directSun.z * impl_->sunEnergy * weatherSun * globalLight + flashLight.z);
785
786 // Background matches the sky at the horizon for the clear color.
787 const float skyR = getSkyR(), skyG = getSkyG(), skyB = getSkyB();
788 gfx->setBackgroundColorRGBA(skyR, skyG, skyB, 1.f);
789
790 // --- procedural skybox (IBL env), regenerated per sun bucket ---
791 if (impl_->skyboxEnabled) {
792 const int bucket = int(elevDeg) + int(azimDeg / 4.f) * 1000 +
793 int(impl_->weatherCloudiness * 10.f) * 100000;
794 if (bucket != impl_->lastSkyBucket) {
795 impl_->lastSkyBucket = bucket;
796 std::vector<uint8_t> faces(
797 size_t(kSkyCubeSize) * size_t(kSkyCubeSize) * 4 * 6);
798 for (int f = 0; f < 6; ++f) {
799 std::vector<uint8_t> face(
800 size_t(kSkyCubeSize) * size_t(kSkyCubeSize) * 4);
801 const Vec3 tint = impl_->pcgSkybox.enabled
802 ? Vec3{impl_->pcgSkybox.tintRed, impl_->pcgSkybox.tintGreen,
803 impl_->pcgSkybox.tintBlue}
804 : Vec3{1.f, 1.f, 1.f};
805 fillSkyFace(face, int(kSkyCubeSize), f, impl_->sunDir, directSun,
806 impl_->sunEnergy, nightAmount, impl_->turbidity,
807 impl_->mieStrength,
808 impl_->pcgSkybox.enabled ? impl_->pcgSkybox.exposure : impl_->skyExposure,
809 impl_->weatherCloudiness,
810 impl_->pcgSkybox.enabled ? impl_->pcgSkybox.rotationDegrees : 0.f,
811 tint, cubeDir);
812 impl_->skyFaces[static_cast<size_t>(f)] =
813 gfx->newTexture(int(kSkyCubeSize), int(kSkyCubeSize), face.data());
814 const size_t center =
815 (size_t(kSkyCubeSize / 2) * size_t(kSkyCubeSize) + size_t(kSkyCubeSize / 2)) *
816 4u;
817 impl_->skyFaceCenterLuminance[static_cast<size_t>(f)] =
818 (float(face[center]) * 0.2126f + float(face[center + 1]) * 0.7152f +
819 float(face[center + 2]) * 0.0722f) /
820 255.f;
821 std::memcpy(faces.data() + size_t(f) * face.size(), face.data(), face.size());
822 }
823 // Replace the previous env cube; Graphics owns old textures.
824 impl_->skyCube = gfx->newCubemap(int(kSkyCubeSize), faces.data(),
826 gfx->setMesh3DEnv(impl_->skyCube, 0.5f + 0.5f * impl_->sunEnergy);
827 }
828 }
829
830 // --- night light systems (only meaningful below the horizon) ---
831 const bool night = elevDeg < 0.f;
832
833 // Moonlight: a directional light at the opposite-ish angle of the sun.
834 if (impl_->moonLight) {
835 const bool on = night && impl_->nightLight[0];
836 impl_->moonLight->setEnabled(on);
837 if (on) {
838 impl_->moonLight->setDirection(-impl_->sunDir[0], -impl_->sunDir[1],
839 -impl_->sunDir[2]);
840 }
841 }
842
843 // Fire.
844 if (impl_->fireLight) {
845 impl_->fireLight->setEnabled(night && impl_->nightLight[2]);
846 }
847
848 // Fireflies: gentle sinusoidal drift.
849 if (impl_->flyLights.size() == impl_->flies.size()) {
850 for (size_t i = 0; i < impl_->flies.size(); ++i) {
851 graphics::Light3D *l = impl_->flyLights[i];
852 const Impl::Fly &f = impl_->flies[i];
853 const bool on = night && impl_->nightLight[3];
854 l->setEnabled(on);
855 if (on) {
856 const float t = impl_->timeOfDay + f.seed;
857 l->setPosition(f.x + std::sin(t * 0.9f) * 0.6f,
858 f.y + std::sin(t * 1.3f + 1.7f) * 0.4f,
859 f.z + std::cos(t * 0.8f) * 0.6f);
860 }
861 }
862 }
863}
864
865// ---------------------------------------------------------------------------
866// Script binding
867// ---------------------------------------------------------------------------
868
869void DayNight::expose(ssq::Table &table) {
870 auto cls = table.addClass(name, DayNight::create, false);
871 expose(cls);
873}
874
875void DayNight::expose(ssq::Class &cls) {
876 cls.addFunc("getName", &DayNight::getName);
877 cls.addFunc("init", &DayNight::init);
878 cls.addFunc("update", &DayNight::update);
879 cls.addFunc("setTimeOfDay", &DayNight::setTimeOfDay);
880 cls.addFunc("getTimeOfDay", &DayNight::getTimeOfDay);
881 cls.addFunc("setSpeed", &DayNight::setSpeed);
882 cls.addFunc("getSpeed", &DayNight::getSpeed);
883 cls.addFunc("setPaused", &DayNight::setPaused);
884 cls.addFunc("isPaused", &DayNight::isPaused);
885 cls.addFunc("isNight", &DayNight::isNight);
886 cls.addFunc("getSunElevation", &DayNight::getSunElevation);
887 cls.addFunc("getSunAzimuth", &DayNight::getSunAzimuth);
888 cls.addFunc("getSunDirX", &DayNight::getSunDirX);
889 cls.addFunc("getSunDirY", &DayNight::getSunDirY);
890 cls.addFunc("getSunDirZ", &DayNight::getSunDirZ);
891 cls.addFunc("getSunIntensity", &DayNight::getSunIntensity);
892 cls.addFunc("getSunR", &DayNight::getSunR);
893 cls.addFunc("getSunG", &DayNight::getSunG);
894 cls.addFunc("getSunB", &DayNight::getSunB);
895 cls.addFunc("setTurbidity", &DayNight::setTurbidity);
896 cls.addFunc("getTurbidity", &DayNight::getTurbidity);
897 cls.addFunc("setSkyExposure", &DayNight::setSkyExposure);
898 cls.addFunc("getSkyExposure", &DayNight::getSkyExposure);
899 cls.addFunc("setMieStrength", &DayNight::setMieStrength);
900 cls.addFunc("getMieStrength", &DayNight::getMieStrength);
901 cls.addFunc("getSkyR", &DayNight::getSkyR);
902 cls.addFunc("getSkyG", &DayNight::getSkyG);
903 cls.addFunc("getSkyB", &DayNight::getSkyB);
904 cls.addFunc("getAmbientR", &DayNight::getAmbientR);
905 cls.addFunc("getAmbientG", &DayNight::getAmbientG);
906 cls.addFunc("getAmbientB", &DayNight::getAmbientB);
907 cls.addFunc("getAmbientBrightness", &DayNight::getAmbientBrightness);
908 cls.addFunc("applyAtmosphere", &DayNight::applyAtmosphere);
909 cls.addFunc("applyReflectionProbeSky", &DayNight::applyReflectionProbeSky);
910 cls.addFunc("setWeatherInfluence", &DayNight::setWeatherInfluence);
911 cls.addFunc("getWeatherCloudiness", &DayNight::getWeatherCloudiness);
912 cls.addFunc("getWeatherFlash", &DayNight::getWeatherFlash);
913 cls.addFunc("setSkyboxEnabled", &DayNight::setSkyboxEnabled);
914 cls.addFunc("isSkyboxEnabled", &DayNight::isSkyboxEnabled);
915 cls.addFunc("setNightLight", &DayNight::setNightLight);
916 cls.addFunc("isNightLight", &DayNight::isNightLight);
917 cls.addFunc("setFirePosition", &DayNight::setFirePosition);
918 cls.addFunc("addFirefly", &DayNight::addFirefly);
919 cls.addFunc("clearFireflies", &DayNight::clearFireflies);
920 cls.addFunc("getFireflyCount", &DayNight::getFireflyCount);
921}
922
924
925} // namespace eve::daynight
double z
double y
double x
Duration start
const std::string & s
float rotationDegrees
glm::vec4 p[6]
HSQOBJECT cls
Definition ECS.cpp:39
std::string name
Definition ECS.cpp:154
tensor::Graph g
Definition GpuGraph.cpp:7
glm::vec4 tint
float u
Definition Grass.cpp:233
glm::vec3 n
Definition Grass.cpp:63
std::uint32_t ab
double r
float v
HexVec3 up
std::int32_t c
float temperature
int h
std::vector< Colorf > px
std::array< float, 4 > rotation
std::array< float, 3 > scale
Poco::JSON::Object::Ptr result
MeleePoint3 b
Definition MeleeHit.cpp:41
MeleePoint3 a
Definition MeleeHit.cpp:40
#define Module_IMPL(ModuleName, newExpr)
Definition Module.h:27
glm::dvec2 center
glm::dvec3 dx
glm::dvec3 dz
double dt
OceanSprayView view
size_t directions
bool finite
float f
std::set< std::string > values
Object::Ptr value
float d
float t
RoadLaneDirection direction
ScriptStructure out
float dy
Definition Shadow.cpp:151
Vec extinction
float size
Definition TreeMesh.cpp:158
float fraction
static Diagnostic error(DiagnosticCode code, std::string message, std::string path={})
Construct an error diagnostic with the standard error severity.
virtual std::string getName() const =0
Returns the name.
Move-only operation result carrying either a value or Status.
Definition Result.h:176
static Result success(T value)
Construct a successful result owning value.
Definition Result.h:185
static Result failure(Status status)
Construct a failed result from a structured status.
Definition Result.h:196
DayNight module — a time-of-day cycle that drives the sun, sky and light.
Definition DayNight.h:94
float getSpeed() const
Returns the speed.
Definition DayNight.cpp:321
float getWeatherFlash() const
Returns the weather flash.
Definition DayNight.cpp:526
float getSunR() const
Atmosphere-attenuated direct sunlight red channel, including intensity.
Definition DayNight.cpp:333
bool isPaused() const
True when paused.
Definition DayNight.cpp:323
int getFireflyCount() const
Returns the firefly count.
Definition DayNight.cpp:718
float getSunG() const
Atmosphere-attenuated direct sunlight green channel, including intensity.
Definition DayNight.cpp:343
PcgManualSunState getPcgManualSun() const noexcept
Return the authoritative manual-sun settings by value.
Definition DayNight.cpp:385
void applyReflectionProbeSky(graphics::ReflectionProbeCapture *probe) const
Synchronize linear directional sky radiance and sky IBL to a reflection probe.
Definition DayNight.cpp:611
float getSunDirY() const
Returns the sun dir y.
Definition DayNight.cpp:330
float getSkyR() const
Returns the sky r.
Definition DayNight.cpp:482
void applyAtmosphere(graphics::Volumetric *fog) const
Synchronize sun direction and atmosphere-derived fog lighting.
Definition DayNight.cpp:555
float getSkyG() const
Returns the sky g.
Definition DayNight.cpp:492
float getSkyB() const
Returns the sky b.
Definition DayNight.cpp:502
bool isNight() const
True when the sun is below the horizon (night).
Definition DayNight.cpp:325
float getSunDirZ() const
Returns the sun dir z.
Definition DayNight.cpp:331
void setPaused(bool paused)
Sets the paused.
Definition DayNight.cpp:322
void setSkyboxEnabled(bool enabled)
Sets the skybox enabled.
Definition DayNight.cpp:665
void clearFireflies()
Clears fireflies.
Definition DayNight.cpp:714
Result< void > setPcgManualSun(const PcgManualSunState &state)
Atomically replace the persistent Pcg manual-sun state.
Definition DayNight.cpp:364
void init(graphics::Graphics *gfx)
Idempotent; builds lights / sky cubemap on first call.
Definition DayNight.cpp:724
static const int kNamedLightCount
Definition DayNight.h:251
float getSunIntensity() const
0..1 sun energy; ramps to 0 below the horizon.
Definition DayNight.cpp:332
float getTimeOfDay() const
Returns the time of day.
Definition DayNight.cpp:318
float getAmbientR() const
Returns the ambient r.
Definition DayNight.cpp:527
bool isNightLight(const std::string &name) const
True when night light.
Definition DayNight.cpp:683
PcgFogState getPcgFog() const noexcept
Return the authoritative Pcg fog state by value.
Definition DayNight.cpp:440
float getAmbientB() const
Returns the ambient b.
Definition DayNight.cpp:545
Result< void > setPcgSkybox(const PcgSkyboxState &state)
Atomically replace Pcg's persistent skybox override.
Definition DayNight.cpp:387
float getSunAzimuth() const
Solar azimuth in degrees, measured clockwise from +Z.
Definition DayNight.cpp:328
PcgSkyboxState getPcgSkybox() const noexcept
Return the authoritative Pcg skybox override by value.
Definition DayNight.cpp:403
float getSkyExposure() const
Returns the sky exposure.
Definition DayNight.cpp:474
Result< void > setPcgAmbientLight(const PcgAmbientLightState &state)
Atomically replace Pcg ambient colors, intensity and direct-light multiplier.
Definition DayNight.cpp:442
Result< void > setPcgFog(const PcgFogState &state)
Atomically replace all Pcg fog and density-volume controls.
Definition DayNight.cpp:405
bool isSkyboxEnabled() const
True when skybox enabled.
Definition DayNight.cpp:669
void setFirePosition(float x, float y, float z)
Position of the campfire point light (fire system).
Definition DayNight.cpp:690
void addFirefly(float x, float y, float z)
Add one firefly anchor (world space); up to kMaxFireflies.
Definition DayNight.cpp:699
float getMieStrength() const
Returns the mie strength.
Definition DayNight.cpp:479
float getSunB() const
Atmosphere-attenuated direct sunlight blue channel, including intensity.
Definition DayNight.cpp:353
void setTurbidity(float turbidity)
Set aerosol turbidity. 1.5 is very clear; 10 is hazy.
Definition DayNight.cpp:465
PcgAmbientLightState getPcgAmbientLight() const noexcept
Return the authoritative Pcg ambient-light state by value.
Definition DayNight.cpp:462
float getAmbientG() const
Returns the ambient g.
Definition DayNight.cpp:536
~DayNight() override
Day night.
Definition DayNight.cpp:306
void setWeatherInfluence(float cloudiness, float lightningFlash)
Apply cloud cover and lightning exposure to the unified sky/lighting model.
Definition DayNight.cpp:518
static const char *const kNamedLights[]
Definition DayNight.h:250
float getTurbidity() const
Returns the turbidity.
Definition DayNight.cpp:469
void setSkyExposure(float exposure)
Exposure used when mapping physical sky radiance to the RGBA8 sky cubemap.
Definition DayNight.cpp:470
void setSpeed(float hoursPerRealHour)
Sets the speed.
Definition DayNight.cpp:320
float getSunElevation() const
Solar elevation in degrees (max at local noon, ~70°).
Definition DayNight.cpp:327
void setNightLight(const std::string &name, bool enabled)
Enable a named light system: "moonlight"|"starlight"|"fire"|"fireflies".
Definition DayNight.cpp:675
void setMieStrength(float strength)
Relative Mie aerosol density; controls horizon haze and solar halo.
Definition DayNight.cpp:475
float getAmbientBrightness() const
Returns the ambient brightness.
Definition DayNight.cpp:512
float getSunDirX() const
World-space direction pointing AT the sun (normalized).
Definition DayNight.cpp:329
float getWeatherCloudiness() const
Returns the weather cloudiness.
Definition DayNight.cpp:525
void setTimeOfDay(float hours)
Sets the time of day.
Definition DayNight.cpp:312
void update(float dt, graphics::Graphics *gfx)
Advance the clock and push sun/sky/light state; before gfx.render3D().
Definition DayNight.cpp:754
virtual Texture * newCubemap(int faceSize, const uint8_t *rgbaFaces)=0
Create an RGBA8 cubemap from 6 faces packed as +X,-X,+Y,-Y,+Z,-Z (each faceSize×faceSize,...
virtual void setDirectionalLight(float dx, float dy, float dz, float r=1.f, float g=1.f, float b=1.f)
Sets the directional light.
Definition Graphics.cpp:273
virtual void setBackgroundColorRGBA(float r, float g, float b, float a=1.f)
Sets the background color rgba.
virtual void setMesh3DEnv(Texture *cube, float intensity)=0
Specular IBL environment for subsequent default mesh draws. cube must be from newCubemap (or nullptr ...
virtual Texture * newTexture(int width, int height, const uint8_t *rgba, bool repeatU=false, bool repeatV=false)=0
Creates a texture. @ownership Caller deletes unless documented otherwise.
Declarative 3D light. Collected by RenderSystem3D (max 8 per frame). type: "point" | "dir" | "spot" (...
Definition Light.h:190
void setEnabled(bool enabled)
Definition Light.cpp:165
void setDirection(float dx, float dy, float dz)
Definition Light.cpp:133
void setPosition(float x, float y, float z)
Definition Light.cpp:122
static Light3D * createLight(const std::string &type="point")
Definition Light.cpp:91
void setRadius(float radius)
Definition Light.cpp:152
void setColor(float r, float g, float b, float intensity=1.f)
Definition Light.cpp:144
Incremental six-face HDR scene capture for a runtime reflection probe.
void setEnvironmentLighting(Texture *environment, float intensity=1.f)
Set non-recursive global environment lighting for captured geometry.
void setSkyFaceTextureScale(int face, float scale)
Set the linear HDR radiance multiplier for one sky face texture.
void setSkyFaceTexture(int face, Texture *texture)
Set a directional per-pixel sky texture for one capture face.
void setSkyFaceColor(int face, float r, float g, float b)
Override one directional backup sky face in +X,-X,+Y,-Y,+Z,-Z order.
GPU texture created via Graphics::newTexture. Owns GPU resources through an opaque backend handle.
Definition Texture.h:15
Volumetric light + fog.
Definition Volumetric.h:38
float getFloat(const std::string &name) const
Returns the float.
void setMode(const std::string &mode)
"screenspace" | "raymarch" | "fog" | "froxel" | "cloud".
void setCloudLightColor(float r, float g, float b)
Direct sunlight color used by cloud single scattering.
void setFogEnd(float endDistance)
Sets the fog end.
std::string getMode() const
Returns the mode.
Definition Volumetric.h:56
void setIntensity(float intensity)
Sets the intensity.
std::string getQuality() const
Returns the quality.
Definition Volumetric.h:51
void setLightDirection(float dx, float dy, float dz)
World-space direction toward the lit surface (ray march / phase).
void setTime(float seconds)
Sets the time.
void setQuality(const std::string &quality)
"low" | "medium" | "high" (unknown → medium).
void setDensity(float density)
Sets the density.
void setFogStart(float startDistance)
View-distance ramp where fog appears (world units along the ray).
void setFogColor(float r, float g, float b)
Sets the fog color.
void exposeSkyScriptBindings(ssq::Table &table, ssq::Class &daynightClass)
Register independent sky bindings on the VM/graphics owner thread.
ForceVector add(ForceVector a, ForceVector b)
Definition ForceMath.h:13
double dot(ForceVector a, ForceVector b)
Definition ForceMath.h:14
Vec3 mul(Vec3 a, float scale)
float smoothstep(float edge0, float edge1, float value)
bool enabled
PcgManualSunState manualSun
Definition DayNight.cpp:250
std::vector< graphics::Light3D * > flyLights
Definition DayNight.cpp:280
std::array< float, 6 > skyFaceCenterLuminance
Definition DayNight.cpp:266
graphics::Volumetric * pcgFogTarget
Definition DayNight.cpp:254
std::vector< Fly > flies
Definition DayNight.cpp:277
graphics::Light3D * moonLight
Definition DayNight.cpp:278
graphics::Light3D * fireLight
Definition DayNight.cpp:279
graphics::Graphics * gfx
Definition DayNight.cpp:232
graphics::Texture * skyCube
Definition DayNight.cpp:264
std::array< graphics::Texture *, 6 > skyFaces
Definition DayNight.cpp:265
PcgAmbientLightState pcgAmbient
Definition DayNight.cpp:253
Pcg ambient-gradient colors and global direct-light multiplier.
Definition DayNight.h:61
Joint Pcg Photo Mode fog and density-volume state.
Definition DayNight.h:43
Persistent manual sun settings projected from Pcg Photo Mode.
Definition DayNight.h:21
Persistent Pcg skybox override settings.
Definition DayNight.h:33
static TextureCreateInfo withMipmaps(bool aniso=true, float maxAniso=16.f)
With mipmaps.
std::mutex mu
Definition Graphics.cpp:198
uint32_t bucket
glm::vec4 color