2 #include "IVideoDriver.h"
3 #include "ISceneManager.h"
4 #include "ICameraSceneNode.h"
6 #include "client/tile.h"
7 #include "noise.h" // easeCurve
9 #include "util/numeric.h"
12 #include "camera.h" // CameraModes
15 Sky::Sky(scene::ISceneNode* parent, scene::ISceneManager* mgr, s32 id,
16 ITextureSource *tsrc):
17 scene::ISceneNode(parent, mgr, id)
19 setAutomaticCulling(scene::EAC_OFF);
20 m_box.MaxEdge.set(0, 0, 0);
21 m_box.MinEdge.set(0, 0, 0);
27 mat.ZBuffer = video::ECFN_NEVER;
28 mat.ZWriteEnable = false;
30 mat.TextureLayer[0].TextureWrapU = video::ETC_CLAMP_TO_EDGE;
31 mat.TextureLayer[0].TextureWrapV = video::ETC_CLAMP_TO_EDGE;
32 mat.BackfaceCulling = false;
37 //m_materials[1].MaterialType = video::EMT_TRANSPARENT_VERTEX_ALPHA;
38 m_materials[1].MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
41 m_materials[2].setTexture(0, tsrc->getTextureForMesh("sunrisebg.png"));
42 m_materials[2].MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
43 //m_materials[2].MaterialType = video::EMT_TRANSPARENT_ADD_COLOR;
45 m_sun_texture = tsrc->isKnownSourceImage("sun.png") ?
46 tsrc->getTextureForMesh("sun.png") : NULL;
47 m_moon_texture = tsrc->isKnownSourceImage("moon.png") ?
48 tsrc->getTextureForMesh("moon.png") : NULL;
49 m_sun_tonemap = tsrc->isKnownSourceImage("sun_tonemap.png") ?
50 tsrc->getTexture("sun_tonemap.png") : NULL;
51 m_moon_tonemap = tsrc->isKnownSourceImage("moon_tonemap.png") ?
52 tsrc->getTexture("moon_tonemap.png") : NULL;
56 m_materials[3].setTexture(0, m_sun_texture);
57 m_materials[3].MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
59 m_materials[3].Lighting = true;
64 m_materials[4].setTexture(0, m_moon_texture);
65 m_materials[4].MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
67 m_materials[4].Lighting = true;
70 for (u32 i = 0; i < SKY_STAR_COUNT; i++) {
72 myrand_range(-10000, 10000),
73 myrand_range(-10000, 10000),
74 myrand_range(-10000, 10000)
76 m_stars[i].normalize();
79 m_directional_colored_fog = g_settings->getBool("directional_colored_fog");
83 void Sky::OnRegisterSceneNode()
86 SceneManager->registerNodeForRendering(this, scene::ESNRP_SKY_BOX);
88 scene::ISceneNode::OnRegisterSceneNode();
97 video::IVideoDriver* driver = SceneManager->getVideoDriver();
98 scene::ICameraSceneNode* camera = SceneManager->getActiveCamera();
100 if (!camera || !driver)
103 ScopeProfiler sp(g_profiler, "Sky::render()", SPT_AVG);
105 // Draw perspective skybox
107 core::matrix4 translate(AbsoluteTransformation);
108 translate.setTranslation(camera->getAbsolutePosition());
110 // Draw the sky box between the near and far clip plane
111 const f32 viewDistance = (camera->getNearValue() + camera->getFarValue()) * 0.5f;
113 scale.setScale(core::vector3df(viewDistance, viewDistance, viewDistance));
115 driver->setTransform(video::ETS_WORLD, translate * scale);
117 if (m_sunlight_seen) {
118 float sunsize = 0.07;
119 video::SColorf suncolor_f(1, 1, 0, 1);
121 suncolor_f.g = MYMAX(0.3, MYMIN(1.0, 0.7 + m_time_brightness * 0.5));
122 suncolor_f.b = MYMAX(0.0, m_brightness * 0.95);
123 video::SColorf suncolor2_f(1, 1, 1, 1);
125 suncolor_f.g = MYMAX(0.3, MYMIN(1.0, 0.85 + m_time_brightness * 0.5));
126 suncolor_f.b = MYMAX(0.0, m_brightness);
128 float moonsize = 0.04;
129 video::SColorf mooncolor_f(0.50, 0.57, 0.65, 1);
130 video::SColorf mooncolor2_f(0.85, 0.875, 0.9, 1);
132 float nightlength = 0.415;
133 float wn = nightlength / 2;
134 float wicked_time_of_day = 0;
135 if (m_time_of_day > wn && m_time_of_day < 1.0 - wn)
136 wicked_time_of_day = (m_time_of_day - wn) / (1.0 - wn * 2) * 0.5 + 0.25;
137 else if (m_time_of_day < 0.5)
138 wicked_time_of_day = m_time_of_day / wn * 0.25;
140 wicked_time_of_day = 1.0 - ((1.0 - m_time_of_day) / wn * 0.25);
141 /*std::cerr<<"time_of_day="<<m_time_of_day<<" -> "
142 <<"wicked_time_of_day="<<wicked_time_of_day<<std::endl;*/
144 video::SColor suncolor = suncolor_f.toSColor();
145 video::SColor suncolor2 = suncolor2_f.toSColor();
146 video::SColor mooncolor = mooncolor_f.toSColor();
147 video::SColor mooncolor2 = mooncolor2_f.toSColor();
149 // Calculate offset normalized to the X dimension of a 512x1 px tonemap
150 float offset = (1.0 - fabs(sin((m_time_of_day - 0.5) * irr::core::PI))) * 511;
153 u8 * texels = (u8 *)m_sun_tonemap->lock();
154 video::SColor* texel = (video::SColor *)(texels + (u32)offset * 4);
155 video::SColor texel_color (255, texel->getRed(),
156 texel->getGreen(), texel->getBlue());
157 m_sun_tonemap->unlock();
158 m_materials[3].EmissiveColor = texel_color;
161 if (m_moon_tonemap) {
162 u8 * texels = (u8 *)m_moon_tonemap->lock();
163 video::SColor* texel = (video::SColor *)(texels + (u32)offset * 4);
164 video::SColor texel_color (255, texel->getRed(),
165 texel->getGreen(), texel->getBlue());
166 m_moon_tonemap->unlock();
167 m_materials[4].EmissiveColor = texel_color;
172 static const u16 indices[4] = {0, 1, 2, 3};
173 video::S3DVertex vertices[4];
175 driver->setMaterial(m_materials[1]);
177 video::SColor cloudyfogcolor = m_bgcolor;
179 // Draw far cloudy fog thing blended with skycolor
180 for (u32 j = 0; j < 4; j++) {
181 video::SColor c = cloudyfogcolor.getInterpolated(m_skycolor, 0.45);
182 vertices[0] = video::S3DVertex(-1, 0.08, -1, 0, 0, 1, c, t, t);
183 vertices[1] = video::S3DVertex( 1, 0.08, -1, 0, 0, 1, c, o, t);
184 vertices[2] = video::S3DVertex( 1, 0.12, -1, 0, 0, 1, c, o, o);
185 vertices[3] = video::S3DVertex(-1, 0.12, -1, 0, 0, 1, c, t, o);
186 for (u32 i = 0; i < 4; i++) {
191 // Switch from -Z (south) to +X (east)
192 vertices[i].Pos.rotateXZBy(90);
194 // Switch from -Z (south) to -X (west)
195 vertices[i].Pos.rotateXZBy(-90);
197 // Switch from -Z (south) to +Z (north)
198 vertices[i].Pos.rotateXZBy(-180);
200 driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
203 // Draw far cloudy fog thing
204 for (u32 j = 0; j < 4; j++) {
205 video::SColor c = cloudyfogcolor;
206 vertices[0] = video::S3DVertex(-1, -1.0, -1, 0, 0, 1, c, t, t);
207 vertices[1] = video::S3DVertex( 1, -1.0, -1, 0, 0, 1, c, o, t);
208 vertices[2] = video::S3DVertex( 1, 0.08, -1, 0, 0, 1, c, o, o);
209 vertices[3] = video::S3DVertex(-1, 0.08, -1, 0, 0, 1, c, t, o);
210 for (u32 i = 0; i < 4; i++) {
215 // Switch from -Z (south) to +X (east)
216 vertices[i].Pos.rotateXZBy(90);
218 // Switch from -Z (south) to -X (west)
219 vertices[i].Pos.rotateXZBy(-90);
221 // Switch from -Z (south) to +Z (north)
222 vertices[i].Pos.rotateXZBy(-180);
224 driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
227 // Draw bottom far cloudy fog thing
228 video::SColor c = cloudyfogcolor;
229 vertices[0] = video::S3DVertex(-1, -1.0, -1, 0, 1, 0, c, t, t);
230 vertices[1] = video::S3DVertex( 1, -1.0, -1, 0, 1, 0, c, o, t);
231 vertices[2] = video::S3DVertex( 1, -1.0, 1, 0, 1, 0, c, o, o);
232 vertices[3] = video::S3DVertex(-1, -1.0, 1, 0, 1, 0, c, t, o);
233 driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
235 driver->setMaterial(m_materials[2]);
237 // Draw sunrise/sunset horizon glow texture (textures/base/pack/sunrisebg.png)
240 float mid = wicked_time_of_day < 0.5 ? mid1 : (1.0 - mid1);
241 float a_ = 1.0 - fabs(wicked_time_of_day - mid) * 35.0;
242 float a = easeCurve(MYMAX(0, MYMIN(1, a_)));
243 //std::cerr<<"a_="<<a_<<" a="<<a<<std::endl;
244 video::SColor c(255, 255, 255, 255);
245 float y = -(1.0 - a) * 0.22;
246 vertices[0] = video::S3DVertex(-1, -0.05 + y, -1, 0, 0, 1, c, t, t);
247 vertices[1] = video::S3DVertex( 1, -0.05 + y, -1, 0, 0, 1, c, o, t);
248 vertices[2] = video::S3DVertex( 1, 0.2 + y, -1, 0, 0, 1, c, o, o);
249 vertices[3] = video::S3DVertex(-1, 0.2 + y, -1, 0, 0, 1, c, t, o);
250 for (u32 i = 0; i < 4; i++) {
251 if (wicked_time_of_day < 0.5)
252 // Switch from -Z (south) to +X (east)
253 vertices[i].Pos.rotateXZBy(90);
255 // Switch from -Z (south) to -X (west)
256 vertices[i].Pos.rotateXZBy(-90);
258 driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
262 if (wicked_time_of_day > 0.15 && wicked_time_of_day < 0.85) {
263 if (!m_sun_texture) {
264 driver->setMaterial(m_materials[1]);
265 float d = sunsize * 1.7;
266 video::SColor c = suncolor;
267 c.setAlpha(0.05 * 255);
268 vertices[0] = video::S3DVertex(-d, -d, -1, 0, 0, 1, c, t, t);
269 vertices[1] = video::S3DVertex( d, -d, -1, 0, 0, 1, c, o, t);
270 vertices[2] = video::S3DVertex( d, d, -1, 0, 0, 1, c, o, o);
271 vertices[3] = video::S3DVertex(-d, d, -1, 0, 0, 1, c, t, o);
272 for (u32 i = 0; i < 4; i++) {
273 // Switch from -Z (south) to +X (east)
274 vertices[i].Pos.rotateXZBy(90);
275 vertices[i].Pos.rotateXYBy(wicked_time_of_day * 360 - 90);
277 driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
281 c.setAlpha(0.15 * 255);
282 vertices[0] = video::S3DVertex(-d, -d, -1, 0, 0, 1, c, t, t);
283 vertices[1] = video::S3DVertex( d, -d, -1, 0, 0, 1, c, o, t);
284 vertices[2] = video::S3DVertex( d, d, -1, 0, 0, 1, c, o, o);
285 vertices[3] = video::S3DVertex(-d, d, -1, 0, 0, 1, c, t, o);
286 for (u32 i = 0; i < 4; i++) {
287 // Switch from -Z (south) to +X (east)
288 vertices[i].Pos.rotateXZBy(90);
289 vertices[i].Pos.rotateXYBy(wicked_time_of_day * 360 - 90);
291 driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
294 vertices[0] = video::S3DVertex(-d, -d, -1, 0, 0, 1, suncolor, t, t);
295 vertices[1] = video::S3DVertex( d, -d, -1, 0, 0, 1, suncolor, o, t);
296 vertices[2] = video::S3DVertex( d, d, -1, 0, 0, 1, suncolor, o, o);
297 vertices[3] = video::S3DVertex(-d, d, -1, 0, 0, 1, suncolor, t, o);
298 for (u32 i = 0; i < 4; i++) {
299 // Switch from -Z (south) to +X (east)
300 vertices[i].Pos.rotateXZBy(90);
301 vertices[i].Pos.rotateXYBy(wicked_time_of_day * 360 - 90);
303 driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
306 vertices[0] = video::S3DVertex(-d, -d, -1, 0, 0, 1, suncolor2, t, t);
307 vertices[1] = video::S3DVertex( d, -d, -1, 0, 0, 1, suncolor2, o, t);
308 vertices[2] = video::S3DVertex( d, d, -1, 0, 0, 1, suncolor2, o, o);
309 vertices[3] = video::S3DVertex(-d, d, -1, 0, 0, 1, suncolor2, t, o);
310 for (u32 i = 0; i < 4; i++) {
311 // Switch from -Z (south) to +X (east)
312 vertices[i].Pos.rotateXZBy(90);
313 vertices[i].Pos.rotateXYBy(wicked_time_of_day * 360 - 90);
315 driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
317 driver->setMaterial(m_materials[3]);
318 float d = sunsize * 1.7;
321 c = video::SColor (0, 0, 0, 0);
323 c = video::SColor (255, 255, 255, 255);
324 vertices[0] = video::S3DVertex(-d, -d, -1, 0, 0, 1, c, t, t);
325 vertices[1] = video::S3DVertex( d, -d, -1, 0, 0, 1, c, o, t);
326 vertices[2] = video::S3DVertex( d, d, -1, 0, 0, 1, c, o, o);
327 vertices[3] = video::S3DVertex(-d, d, -1, 0, 0, 1, c, t, o);
328 for(u32 i = 0; i < 4; i++) {
329 // Switch from -Z (south) to +X (east)
330 vertices[i].Pos.rotateXZBy(90);
331 vertices[i].Pos.rotateXYBy(wicked_time_of_day * 360 - 90);
333 driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
338 if (wicked_time_of_day < 0.3 || wicked_time_of_day > 0.7) {
339 if (!m_moon_texture) {
340 driver->setMaterial(m_materials[1]);
341 float d = moonsize * 1.9;
342 video::SColor c = mooncolor;
343 c.setAlpha(0.05 * 255);
344 vertices[0] = video::S3DVertex(-d, -d, -1, 0, 0, 1, c, t, t);
345 vertices[1] = video::S3DVertex( d, -d, -1, 0, 0, 1, c, o, t);
346 vertices[2] = video::S3DVertex( d, d, -1, 0, 0, 1, c, o, o);
347 vertices[3] = video::S3DVertex(-d, d, -1, 0, 0, 1, c, t, o);
348 for (u32 i = 0; i < 4; i++) {
349 // Switch from -Z (south) to -X (west)
350 vertices[i].Pos.rotateXZBy(-90);
351 vertices[i].Pos.rotateXYBy(wicked_time_of_day * 360 - 90);
353 driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
357 c.setAlpha(0.15 * 255);
358 vertices[0] = video::S3DVertex(-d, -d, -1, 0, 0, 1, c, t, t);
359 vertices[1] = video::S3DVertex( d, -d, -1, 0, 0, 1, c, o, t);
360 vertices[2] = video::S3DVertex( d, d, -1, 0, 0, 1, c, o, o);
361 vertices[3] = video::S3DVertex(-d, d, -1, 0, 0, 1, c, t, o);
362 for (u32 i = 0; i < 4; i++) {
363 // Switch from -Z (south) to -X (west)
364 vertices[i].Pos.rotateXZBy(-90);
365 vertices[i].Pos.rotateXYBy(wicked_time_of_day * 360 - 90);
367 driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
370 vertices[0] = video::S3DVertex(-d, -d, -1, 0, 0, 1, mooncolor, t, t);
371 vertices[1] = video::S3DVertex( d, -d, -1, 0, 0, 1, mooncolor, o, t);
372 vertices[2] = video::S3DVertex( d, d, -1, 0, 0, 1, mooncolor, o, o);
373 vertices[3] = video::S3DVertex(-d, d, -1, 0, 0, 1, mooncolor, t, o);
374 for (u32 i = 0; i < 4; i++) {
375 // Switch from -Z (south) to -X (west)
376 vertices[i].Pos.rotateXZBy(-90);
377 vertices[i].Pos.rotateXYBy(wicked_time_of_day * 360 - 90);
379 driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
381 float d2 = moonsize * 0.6;
382 vertices[0] = video::S3DVertex(-d, -d, -1, 0, 0, 1, mooncolor2, t, t);
383 vertices[1] = video::S3DVertex( d2,-d, -1, 0, 0, 1, mooncolor2, o, t);
384 vertices[2] = video::S3DVertex( d2, d2, -1, 0, 0, 1, mooncolor2, o, o);
385 vertices[3] = video::S3DVertex(-d, d2, -1, 0, 0, 1, mooncolor2, t, o);
386 for (u32 i = 0; i < 4; i++) {
387 // Switch from -Z (south) to -X (west)
388 vertices[i].Pos.rotateXZBy(-90);
389 vertices[i].Pos.rotateXYBy(wicked_time_of_day * 360 - 90);
391 driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
393 driver->setMaterial(m_materials[4]);
394 float d = moonsize * 1.9;
397 c = video::SColor (0, 0, 0, 0);
399 c = video::SColor (255, 255, 255, 255);
400 vertices[0] = video::S3DVertex(-d, -d, -1, 0, 0, 1, c, t, t);
401 vertices[1] = video::S3DVertex( d, -d, -1, 0, 0, 1, c, o, t);
402 vertices[2] = video::S3DVertex( d, d, -1, 0, 0, 1, c, o, o);
403 vertices[3] = video::S3DVertex(-d, d, -1, 0, 0, 1, c, t, o);
404 for (u32 i = 0; i < 4; i++) {
405 // Switch from -Z (south) to -X (west)
406 vertices[i].Pos.rotateXZBy(-90);
407 vertices[i].Pos.rotateXYBy(wicked_time_of_day * 360 - 90);
409 driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
414 driver->setMaterial(m_materials[1]);
416 float starbrightness = MYMAX(0, MYMIN(1,
417 (0.285 - fabs(wicked_time_of_day < 0.5 ?
418 wicked_time_of_day : (1.0 - wicked_time_of_day))) * 10));
419 float f = starbrightness;
421 video::SColor starcolor(255, f * 90, f * 90, f * 90);
422 if (starcolor.getBlue() < m_skycolor.getBlue())
424 u16 indices[SKY_STAR_COUNT * 4];
425 video::S3DVertex vertices[SKY_STAR_COUNT * 4];
426 for (u32 i = 0; i < SKY_STAR_COUNT; i++) {
427 indices[i * 4 + 0] = i * 4 + 0;
428 indices[i * 4 + 1] = i * 4 + 1;
429 indices[i * 4 + 2] = i * 4 + 2;
430 indices[i * 4 + 3] = i * 4 + 3;
432 core::CMatrix4<f32> a;
433 a.buildRotateFromTo(v3f(0, 1, 0), v3f(d, 1 + d / 2, 0));
436 a.buildRotateFromTo(v3f(0, 1, 0), v3f(d, 1, d));
439 a.buildRotateFromTo(v3f(0, 1, 0), v3f(0, 1 - d / 2, d));
442 p.rotateXYBy(wicked_time_of_day * 360 - 90);
443 p1.rotateXYBy(wicked_time_of_day * 360 - 90);
444 p2.rotateXYBy(wicked_time_of_day * 360 - 90);
445 p3.rotateXYBy(wicked_time_of_day * 360 - 90);
446 vertices[i * 4 + 0].Pos = p;
447 vertices[i * 4 + 0].Color = starcolor;
448 vertices[i * 4 + 1].Pos = p1;
449 vertices[i * 4 + 1].Color = starcolor;
450 vertices[i * 4 + 2].Pos = p2;
451 vertices[i * 4 + 2].Color = starcolor;
452 vertices[i * 4 + 3].Pos = p3;
453 vertices[i * 4 + 3].Color = starcolor;
455 driver->drawVertexPrimitiveList(vertices, SKY_STAR_COUNT * 4,
456 indices, SKY_STAR_COUNT, video::EVT_STANDARD,
457 scene::EPT_QUADS, video::EIT_16BIT);
460 // Draw far cloudy fog thing below east and west horizons
461 for (u32 j = 0; j < 2; j++) {
462 video::SColor c = cloudyfogcolor;
463 vertices[0] = video::S3DVertex(-1, -1.0, -1, 0, 0, 1, c, t, t);
464 vertices[1] = video::S3DVertex( 1, -1.0, -1, 0, 0, 1, c, o, t);
465 vertices[2] = video::S3DVertex( 1, -0.02, -1, 0, 0, 1, c, o, o);
466 vertices[3] = video::S3DVertex(-1, -0.02, -1, 0, 0, 1, c, t, o);
467 for (u32 i = 0; i < 4; i++) {
468 //if (wicked_time_of_day < 0.5)
470 // Switch from -Z (south) to +X (east)
471 vertices[i].Pos.rotateXZBy(90);
473 // Switch from -Z (south) to -X (west)
474 vertices[i].Pos.rotateXZBy(-90);
476 driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
482 void Sky::update(float time_of_day, float time_brightness,
483 float direct_brightness, bool sunlight_seen,
484 CameraMode cam_mode, float yaw, float pitch)
486 // Stabilize initial brightness and color values by flooding updates
487 if (m_first_update) {
488 /*dstream<<"First update with time_of_day="<<time_of_day
489 <<" time_brightness="<<time_brightness
490 <<" direct_brightness="<<direct_brightness
491 <<" sunlight_seen="<<sunlight_seen<<std::endl;*/
492 m_first_update = false;
493 for (u32 i = 0; i < 100; i++) {
494 update(time_of_day, time_brightness, direct_brightness,
495 sunlight_seen, cam_mode, yaw, pitch);
500 m_time_of_day = time_of_day;
501 m_time_brightness = time_brightness;
502 m_sunlight_seen = sunlight_seen;
504 bool is_dawn = (time_brightness >= 0.20 && time_brightness < 0.35);
509 video::SColorf bgcolor_bright_normal_f(170. / 255, 200. / 255, 230. / 255, 1.0);
510 video::SColorf bgcolor_bright_dawn_f(0.666, 200. / 255 * 0.7, 230. / 255 * 0.5, 1.0);
511 video::SColorf bgcolor_bright_dawn_f(0.666, 0.549, 0.220, 1.0);
512 video::SColorf bgcolor_bright_dawn_f(0.666 * 1.2, 0.549 * 1.0, 0.220 * 1.0, 1.0);
513 video::SColorf bgcolor_bright_dawn_f(0.666 * 1.2, 0.549 * 1.0, 0.220 * 1.2, 1.0);
515 video::SColorf cloudcolor_bright_dawn_f(1.0, 0.591, 0.4);
516 video::SColorf cloudcolor_bright_dawn_f(1.0, 0.65, 0.44);
517 video::SColorf cloudcolor_bright_dawn_f(1.0, 0.7, 0.5);
520 video::SColorf bgcolor_bright_normal_f = video::SColor(255, 155, 193, 240);
521 video::SColorf bgcolor_bright_indoor_f = video::SColor(255, 100, 100, 100);
522 video::SColorf bgcolor_bright_dawn_f = video::SColor(255, 186, 193, 240);
523 video::SColorf bgcolor_bright_night_f = video::SColor(255, 64, 144, 255);
525 video::SColorf skycolor_bright_normal_f = video::SColor(255, 140, 186, 250);
526 video::SColorf skycolor_bright_dawn_f = video::SColor(255, 180, 186, 250);
527 video::SColorf skycolor_bright_night_f = video::SColor(255, 0, 107, 255);
529 // pure white: becomes "diffuse light component" for clouds
530 video::SColorf cloudcolor_bright_normal_f = video::SColor(255, 255, 255, 255);
531 // dawn-factoring version of pure white (note: R is above 1.0)
532 video::SColorf cloudcolor_bright_dawn_f(255.0f/240.0f, 223.0f/240.0f, 191.0f/255.0f);
534 float cloud_color_change_fraction = 0.95;
536 if (fabs(time_brightness - m_brightness) < 0.2) {
537 m_brightness = m_brightness * 0.95 + time_brightness * 0.05;
539 m_brightness = m_brightness * 0.80 + time_brightness * 0.20;
540 cloud_color_change_fraction = 0.0;
543 if (direct_brightness < m_brightness)
544 m_brightness = m_brightness * 0.95 + direct_brightness * 0.05;
546 m_brightness = m_brightness * 0.98 + direct_brightness * 0.02;
549 m_clouds_visible = true;
550 float color_change_fraction = 0.98;
552 if (is_dawn) { // Dawn
553 m_bgcolor_bright_f = m_bgcolor_bright_f.getInterpolated(
554 bgcolor_bright_dawn_f, color_change_fraction);
555 m_skycolor_bright_f = m_skycolor_bright_f.getInterpolated(
556 skycolor_bright_dawn_f, color_change_fraction);
557 m_cloudcolor_bright_f = m_cloudcolor_bright_f.getInterpolated(
558 cloudcolor_bright_dawn_f, color_change_fraction);
560 if (time_brightness < 0.07) { // Night
561 m_bgcolor_bright_f = m_bgcolor_bright_f.getInterpolated(
562 bgcolor_bright_night_f, color_change_fraction);
563 m_skycolor_bright_f = m_skycolor_bright_f.getInterpolated(
564 skycolor_bright_night_f, color_change_fraction);
566 m_bgcolor_bright_f = m_bgcolor_bright_f.getInterpolated(
567 bgcolor_bright_normal_f, color_change_fraction);
568 m_skycolor_bright_f = m_skycolor_bright_f.getInterpolated(
569 skycolor_bright_normal_f, color_change_fraction);
572 m_cloudcolor_bright_f = m_cloudcolor_bright_f.getInterpolated(
573 cloudcolor_bright_normal_f, color_change_fraction);
576 m_bgcolor_bright_f = m_bgcolor_bright_f.getInterpolated(
577 bgcolor_bright_indoor_f, color_change_fraction);
578 m_skycolor_bright_f = m_skycolor_bright_f.getInterpolated(
579 bgcolor_bright_indoor_f, color_change_fraction);
580 m_cloudcolor_bright_f = m_cloudcolor_bright_f.getInterpolated(
581 cloudcolor_bright_normal_f, color_change_fraction);
582 m_clouds_visible = false;
585 video::SColor bgcolor_bright = m_bgcolor_bright_f.toSColor();
586 m_bgcolor = video::SColor(
588 bgcolor_bright.getRed() * m_brightness,
589 bgcolor_bright.getGreen() * m_brightness,
590 bgcolor_bright.getBlue() * m_brightness
593 video::SColor skycolor_bright = m_skycolor_bright_f.toSColor();
594 m_skycolor = video::SColor(
596 skycolor_bright.getRed() * m_brightness,
597 skycolor_bright.getGreen() * m_brightness,
598 skycolor_bright.getBlue() * m_brightness
601 // Horizon coloring based on sun and moon direction during sunset and sunrise
602 video::SColor pointcolor = video::SColor(m_bgcolor.getAlpha(), 255, 255, 255);
603 if (m_directional_colored_fog) {
604 if (m_horizon_blend() != 0) {
605 // Calculate hemisphere value from yaw, (inverted in third person front view)
607 if (cam_mode > CAMERA_MODE_THIRD)
609 f32 pointcolor_blend = wrapDegrees_0_360(yaw * dir_factor + 90);
610 if (pointcolor_blend > 180)
611 pointcolor_blend = 360 - pointcolor_blend;
612 pointcolor_blend /= 180;
613 // Bound view angle to determine where transition starts and ends
614 pointcolor_blend = rangelim(1 - pointcolor_blend * 1.375, 0, 1 / 1.375) *
616 // Combine the colors when looking up or down, otherwise turning looks weird
617 pointcolor_blend += (0.5 - pointcolor_blend) *
618 (1 - MYMIN((90 - std::fabs(pitch)) / 90 * 1.5, 1));
619 // Invert direction to match where the sun and moon are rising
620 if (m_time_of_day > 0.5)
621 pointcolor_blend = 1 - pointcolor_blend;
622 // Horizon colors of sun and moon
623 f32 pointcolor_light = rangelim(m_time_brightness * 3, 0.2, 1);
625 video::SColorf pointcolor_sun_f(1, 1, 1, 1);
627 pointcolor_sun_f.r = pointcolor_light *
628 (float)m_materials[3].EmissiveColor.getRed() / 255;
629 pointcolor_sun_f.b = pointcolor_light *
630 (float)m_materials[3].EmissiveColor.getBlue() / 255;
631 pointcolor_sun_f.g = pointcolor_light *
632 (float)m_materials[3].EmissiveColor.getGreen() / 255;
634 pointcolor_sun_f.r = pointcolor_light * 1;
635 pointcolor_sun_f.b = pointcolor_light *
636 (0.25 + (rangelim(m_time_brightness, 0.25, 0.75) - 0.25) * 2 * 0.75);
637 pointcolor_sun_f.g = pointcolor_light * (pointcolor_sun_f.b * 0.375 +
638 (rangelim(m_time_brightness, 0.05, 0.15) - 0.05) * 10 * 0.625);
641 video::SColorf pointcolor_moon_f(0.5 * pointcolor_light,
642 0.6 * pointcolor_light, 0.8 * pointcolor_light, 1);
643 if (m_moon_tonemap) {
644 pointcolor_moon_f.r = pointcolor_light *
645 (float)m_materials[4].EmissiveColor.getRed() / 255;
646 pointcolor_moon_f.b = pointcolor_light *
647 (float)m_materials[4].EmissiveColor.getBlue() / 255;
648 pointcolor_moon_f.g = pointcolor_light *
649 (float)m_materials[4].EmissiveColor.getGreen() / 255;
652 video::SColor pointcolor_sun = pointcolor_sun_f.toSColor();
653 video::SColor pointcolor_moon = pointcolor_moon_f.toSColor();
654 // Calculate the blend color
655 pointcolor = m_mix_scolor(pointcolor_moon, pointcolor_sun, pointcolor_blend);
657 m_bgcolor = m_mix_scolor(m_bgcolor, pointcolor, m_horizon_blend() * 0.5);
658 m_skycolor = m_mix_scolor(m_skycolor, pointcolor, m_horizon_blend() * 0.25);
661 float cloud_direct_brightness = 0;
663 if (!m_directional_colored_fog) {
664 cloud_direct_brightness = time_brightness;
665 if (time_brightness >= 0.2 && time_brightness < 0.7)
666 cloud_direct_brightness *= 1.3;
668 cloud_direct_brightness = MYMIN(m_horizon_blend() * 0.15 +
669 m_time_brightness, 1);
672 cloud_direct_brightness = direct_brightness;
675 m_cloud_brightness = m_cloud_brightness * cloud_color_change_fraction +
676 cloud_direct_brightness * (1.0 - cloud_color_change_fraction);
677 m_cloudcolor_f = video::SColorf(
678 m_cloudcolor_bright_f.r * m_cloud_brightness,
679 m_cloudcolor_bright_f.g * m_cloud_brightness,
680 m_cloudcolor_bright_f.b * m_cloud_brightness,
683 if (m_directional_colored_fog) {
684 m_cloudcolor_f = m_mix_scolorf(m_cloudcolor_f,
685 video::SColorf(pointcolor), m_horizon_blend() * 0.25);