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 m_fallback_bg_color(255, 255, 255, 255),
22 m_cloud_brightness(0.5),
23 m_bgcolor_bright_f(1, 1, 1, 1),
24 m_skycolor_bright_f(1, 1, 1, 1),
25 m_cloudcolor_bright_f(1, 1, 1, 1)
27 setAutomaticCulling(scene::EAC_OFF);
28 m_box.MaxEdge.set(0, 0, 0);
29 m_box.MinEdge.set(0, 0, 0);
35 mat.ZBuffer = video::ECFN_NEVER;
36 mat.ZWriteEnable = false;
38 mat.TextureLayer[0].TextureWrapU = video::ETC_CLAMP_TO_EDGE;
39 mat.TextureLayer[0].TextureWrapV = video::ETC_CLAMP_TO_EDGE;
40 mat.BackfaceCulling = false;
45 //m_materials[1].MaterialType = video::EMT_TRANSPARENT_VERTEX_ALPHA;
46 m_materials[1].MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
49 m_materials[2].setTexture(0, tsrc->getTextureForMesh("sunrisebg.png"));
50 m_materials[2].MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
51 //m_materials[2].MaterialType = video::EMT_TRANSPARENT_ADD_COLOR;
53 m_sun_texture = tsrc->isKnownSourceImage("sun.png") ?
54 tsrc->getTextureForMesh("sun.png") : NULL;
55 m_moon_texture = tsrc->isKnownSourceImage("moon.png") ?
56 tsrc->getTextureForMesh("moon.png") : NULL;
57 m_sun_tonemap = tsrc->isKnownSourceImage("sun_tonemap.png") ?
58 tsrc->getTexture("sun_tonemap.png") : NULL;
59 m_moon_tonemap = tsrc->isKnownSourceImage("moon_tonemap.png") ?
60 tsrc->getTexture("moon_tonemap.png") : NULL;
64 m_materials[3].setTexture(0, m_sun_texture);
65 m_materials[3].MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
67 m_materials[3].Lighting = true;
72 m_materials[4].setTexture(0, m_moon_texture);
73 m_materials[4].MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
75 m_materials[4].Lighting = true;
78 for (u32 i = 0; i < SKY_STAR_COUNT; i++) {
80 myrand_range(-10000, 10000),
81 myrand_range(-10000, 10000),
82 myrand_range(-10000, 10000)
84 m_stars[i].normalize();
87 m_directional_colored_fog = g_settings->getBool("directional_colored_fog");
89 m_clouds_enabled = true;
93 void Sky::OnRegisterSceneNode()
96 SceneManager->registerNodeForRendering(this, scene::ESNRP_SKY_BOX);
98 scene::ISceneNode::OnRegisterSceneNode();
107 video::IVideoDriver* driver = SceneManager->getVideoDriver();
108 scene::ICameraSceneNode* camera = SceneManager->getActiveCamera();
110 if (!camera || !driver)
113 ScopeProfiler sp(g_profiler, "Sky::render()", SPT_AVG);
115 // Draw perspective skybox
117 core::matrix4 translate(AbsoluteTransformation);
118 translate.setTranslation(camera->getAbsolutePosition());
120 // Draw the sky box between the near and far clip plane
121 const f32 viewDistance = (camera->getNearValue() + camera->getFarValue()) * 0.5f;
123 scale.setScale(core::vector3df(viewDistance, viewDistance, viewDistance));
125 driver->setTransform(video::ETS_WORLD, translate * scale);
127 if (m_sunlight_seen) {
128 float sunsize = 0.07;
129 video::SColorf suncolor_f(1, 1, 0, 1);
131 suncolor_f.g = MYMAX(0.3, MYMIN(1.0, 0.7 + m_time_brightness * 0.5));
132 suncolor_f.b = MYMAX(0.0, m_brightness * 0.95);
133 video::SColorf suncolor2_f(1, 1, 1, 1);
135 suncolor_f.g = MYMAX(0.3, MYMIN(1.0, 0.85 + m_time_brightness * 0.5));
136 suncolor_f.b = MYMAX(0.0, m_brightness);
138 float moonsize = 0.04;
139 video::SColorf mooncolor_f(0.50, 0.57, 0.65, 1);
140 video::SColorf mooncolor2_f(0.85, 0.875, 0.9, 1);
142 float nightlength = 0.415;
143 float wn = nightlength / 2;
144 float wicked_time_of_day = 0;
145 if (m_time_of_day > wn && m_time_of_day < 1.0 - wn)
146 wicked_time_of_day = (m_time_of_day - wn) / (1.0 - wn * 2) * 0.5 + 0.25;
147 else if (m_time_of_day < 0.5)
148 wicked_time_of_day = m_time_of_day / wn * 0.25;
150 wicked_time_of_day = 1.0 - ((1.0 - m_time_of_day) / wn * 0.25);
151 /*std::cerr<<"time_of_day="<<m_time_of_day<<" -> "
152 <<"wicked_time_of_day="<<wicked_time_of_day<<std::endl;*/
154 video::SColor suncolor = suncolor_f.toSColor();
155 video::SColor suncolor2 = suncolor2_f.toSColor();
156 video::SColor mooncolor = mooncolor_f.toSColor();
157 video::SColor mooncolor2 = mooncolor2_f.toSColor();
159 // Calculate offset normalized to the X dimension of a 512x1 px tonemap
160 float offset = (1.0 - fabs(sin((m_time_of_day - 0.5) * irr::core::PI))) * 511;
163 u8 * texels = (u8 *)m_sun_tonemap->lock();
164 video::SColor* texel = (video::SColor *)(texels + (u32)offset * 4);
165 video::SColor texel_color (255, texel->getRed(),
166 texel->getGreen(), texel->getBlue());
167 m_sun_tonemap->unlock();
168 m_materials[3].EmissiveColor = texel_color;
171 if (m_moon_tonemap) {
172 u8 * texels = (u8 *)m_moon_tonemap->lock();
173 video::SColor* texel = (video::SColor *)(texels + (u32)offset * 4);
174 video::SColor texel_color (255, texel->getRed(),
175 texel->getGreen(), texel->getBlue());
176 m_moon_tonemap->unlock();
177 m_materials[4].EmissiveColor = texel_color;
182 static const u16 indices[4] = {0, 1, 2, 3};
183 video::S3DVertex vertices[4];
185 driver->setMaterial(m_materials[1]);
187 video::SColor cloudyfogcolor = m_bgcolor;
189 // Draw far cloudy fog thing blended with skycolor
190 for (u32 j = 0; j < 4; j++) {
191 video::SColor c = cloudyfogcolor.getInterpolated(m_skycolor, 0.45);
192 vertices[0] = video::S3DVertex(-1, 0.08, -1, 0, 0, 1, c, t, t);
193 vertices[1] = video::S3DVertex( 1, 0.08, -1, 0, 0, 1, c, o, t);
194 vertices[2] = video::S3DVertex( 1, 0.12, -1, 0, 0, 1, c, o, o);
195 vertices[3] = video::S3DVertex(-1, 0.12, -1, 0, 0, 1, c, t, o);
196 for (u32 i = 0; i < 4; i++) {
201 // Switch from -Z (south) to +X (east)
202 vertices[i].Pos.rotateXZBy(90);
204 // Switch from -Z (south) to -X (west)
205 vertices[i].Pos.rotateXZBy(-90);
207 // Switch from -Z (south) to +Z (north)
208 vertices[i].Pos.rotateXZBy(-180);
210 driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
213 // Draw far cloudy fog thing
214 for (u32 j = 0; j < 4; j++) {
215 video::SColor c = cloudyfogcolor;
216 vertices[0] = video::S3DVertex(-1, -1.0, -1, 0, 0, 1, c, t, t);
217 vertices[1] = video::S3DVertex( 1, -1.0, -1, 0, 0, 1, c, o, t);
218 vertices[2] = video::S3DVertex( 1, 0.08, -1, 0, 0, 1, c, o, o);
219 vertices[3] = video::S3DVertex(-1, 0.08, -1, 0, 0, 1, c, t, o);
220 for (u32 i = 0; i < 4; i++) {
225 // Switch from -Z (south) to +X (east)
226 vertices[i].Pos.rotateXZBy(90);
228 // Switch from -Z (south) to -X (west)
229 vertices[i].Pos.rotateXZBy(-90);
231 // Switch from -Z (south) to +Z (north)
232 vertices[i].Pos.rotateXZBy(-180);
234 driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
237 // Draw bottom far cloudy fog thing
238 video::SColor c = cloudyfogcolor;
239 vertices[0] = video::S3DVertex(-1, -1.0, -1, 0, 1, 0, c, t, t);
240 vertices[1] = video::S3DVertex( 1, -1.0, -1, 0, 1, 0, c, o, t);
241 vertices[2] = video::S3DVertex( 1, -1.0, 1, 0, 1, 0, c, o, o);
242 vertices[3] = video::S3DVertex(-1, -1.0, 1, 0, 1, 0, c, t, o);
243 driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
245 driver->setMaterial(m_materials[2]);
247 // Draw sunrise/sunset horizon glow texture (textures/base/pack/sunrisebg.png)
250 float mid = wicked_time_of_day < 0.5 ? mid1 : (1.0 - mid1);
251 float a_ = 1.0 - fabs(wicked_time_of_day - mid) * 35.0;
252 float a = easeCurve(MYMAX(0, MYMIN(1, a_)));
253 //std::cerr<<"a_="<<a_<<" a="<<a<<std::endl;
254 video::SColor c(255, 255, 255, 255);
255 float y = -(1.0 - a) * 0.22;
256 vertices[0] = video::S3DVertex(-1, -0.05 + y, -1, 0, 0, 1, c, t, t);
257 vertices[1] = video::S3DVertex( 1, -0.05 + y, -1, 0, 0, 1, c, o, t);
258 vertices[2] = video::S3DVertex( 1, 0.2 + y, -1, 0, 0, 1, c, o, o);
259 vertices[3] = video::S3DVertex(-1, 0.2 + y, -1, 0, 0, 1, c, t, o);
260 for (u32 i = 0; i < 4; i++) {
261 if (wicked_time_of_day < 0.5)
262 // Switch from -Z (south) to +X (east)
263 vertices[i].Pos.rotateXZBy(90);
265 // Switch from -Z (south) to -X (west)
266 vertices[i].Pos.rotateXZBy(-90);
268 driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
272 if (wicked_time_of_day > 0.15 && wicked_time_of_day < 0.85) {
273 if (!m_sun_texture) {
274 driver->setMaterial(m_materials[1]);
275 float d = sunsize * 1.7;
276 video::SColor c = suncolor;
277 c.setAlpha(0.05 * 255);
278 vertices[0] = video::S3DVertex(-d, -d, -1, 0, 0, 1, c, t, t);
279 vertices[1] = video::S3DVertex( d, -d, -1, 0, 0, 1, c, o, t);
280 vertices[2] = video::S3DVertex( d, d, -1, 0, 0, 1, c, o, o);
281 vertices[3] = video::S3DVertex(-d, d, -1, 0, 0, 1, c, t, o);
282 for (u32 i = 0; i < 4; i++) {
283 // Switch from -Z (south) to +X (east)
284 vertices[i].Pos.rotateXZBy(90);
285 vertices[i].Pos.rotateXYBy(wicked_time_of_day * 360 - 90);
287 driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
291 c.setAlpha(0.15 * 255);
292 vertices[0] = video::S3DVertex(-d, -d, -1, 0, 0, 1, c, t, t);
293 vertices[1] = video::S3DVertex( d, -d, -1, 0, 0, 1, c, o, t);
294 vertices[2] = video::S3DVertex( d, d, -1, 0, 0, 1, c, o, o);
295 vertices[3] = video::S3DVertex(-d, d, -1, 0, 0, 1, c, t, o);
296 for (u32 i = 0; i < 4; i++) {
297 // Switch from -Z (south) to +X (east)
298 vertices[i].Pos.rotateXZBy(90);
299 vertices[i].Pos.rotateXYBy(wicked_time_of_day * 360 - 90);
301 driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
304 vertices[0] = video::S3DVertex(-d, -d, -1, 0, 0, 1, suncolor, t, t);
305 vertices[1] = video::S3DVertex( d, -d, -1, 0, 0, 1, suncolor, o, t);
306 vertices[2] = video::S3DVertex( d, d, -1, 0, 0, 1, suncolor, o, o);
307 vertices[3] = video::S3DVertex(-d, d, -1, 0, 0, 1, suncolor, t, o);
308 for (u32 i = 0; i < 4; i++) {
309 // Switch from -Z (south) to +X (east)
310 vertices[i].Pos.rotateXZBy(90);
311 vertices[i].Pos.rotateXYBy(wicked_time_of_day * 360 - 90);
313 driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
316 vertices[0] = video::S3DVertex(-d, -d, -1, 0, 0, 1, suncolor2, t, t);
317 vertices[1] = video::S3DVertex( d, -d, -1, 0, 0, 1, suncolor2, o, t);
318 vertices[2] = video::S3DVertex( d, d, -1, 0, 0, 1, suncolor2, o, o);
319 vertices[3] = video::S3DVertex(-d, d, -1, 0, 0, 1, suncolor2, t, o);
320 for (u32 i = 0; i < 4; i++) {
321 // Switch from -Z (south) to +X (east)
322 vertices[i].Pos.rotateXZBy(90);
323 vertices[i].Pos.rotateXYBy(wicked_time_of_day * 360 - 90);
325 driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
327 driver->setMaterial(m_materials[3]);
328 float d = sunsize * 1.7;
331 c = video::SColor (0, 0, 0, 0);
333 c = video::SColor (255, 255, 255, 255);
334 vertices[0] = video::S3DVertex(-d, -d, -1, 0, 0, 1, c, t, t);
335 vertices[1] = video::S3DVertex( d, -d, -1, 0, 0, 1, c, o, t);
336 vertices[2] = video::S3DVertex( d, d, -1, 0, 0, 1, c, o, o);
337 vertices[3] = video::S3DVertex(-d, d, -1, 0, 0, 1, c, t, o);
338 for(u32 i = 0; i < 4; i++) {
339 // Switch from -Z (south) to +X (east)
340 vertices[i].Pos.rotateXZBy(90);
341 vertices[i].Pos.rotateXYBy(wicked_time_of_day * 360 - 90);
343 driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
348 if (wicked_time_of_day < 0.3 || wicked_time_of_day > 0.7) {
349 if (!m_moon_texture) {
350 driver->setMaterial(m_materials[1]);
351 float d = moonsize * 1.9;
352 video::SColor c = mooncolor;
353 c.setAlpha(0.05 * 255);
354 vertices[0] = video::S3DVertex(-d, -d, -1, 0, 0, 1, c, t, t);
355 vertices[1] = video::S3DVertex( d, -d, -1, 0, 0, 1, c, o, t);
356 vertices[2] = video::S3DVertex( d, d, -1, 0, 0, 1, c, o, o);
357 vertices[3] = video::S3DVertex(-d, d, -1, 0, 0, 1, c, t, o);
358 for (u32 i = 0; i < 4; i++) {
359 // Switch from -Z (south) to -X (west)
360 vertices[i].Pos.rotateXZBy(-90);
361 vertices[i].Pos.rotateXYBy(wicked_time_of_day * 360 - 90);
363 driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
367 c.setAlpha(0.15 * 255);
368 vertices[0] = video::S3DVertex(-d, -d, -1, 0, 0, 1, c, t, t);
369 vertices[1] = video::S3DVertex( d, -d, -1, 0, 0, 1, c, o, t);
370 vertices[2] = video::S3DVertex( d, d, -1, 0, 0, 1, c, o, o);
371 vertices[3] = video::S3DVertex(-d, d, -1, 0, 0, 1, c, t, o);
372 for (u32 i = 0; i < 4; i++) {
373 // Switch from -Z (south) to -X (west)
374 vertices[i].Pos.rotateXZBy(-90);
375 vertices[i].Pos.rotateXYBy(wicked_time_of_day * 360 - 90);
377 driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
380 vertices[0] = video::S3DVertex(-d, -d, -1, 0, 0, 1, mooncolor, t, t);
381 vertices[1] = video::S3DVertex( d, -d, -1, 0, 0, 1, mooncolor, o, t);
382 vertices[2] = video::S3DVertex( d, d, -1, 0, 0, 1, mooncolor, o, o);
383 vertices[3] = video::S3DVertex(-d, d, -1, 0, 0, 1, mooncolor, t, o);
384 for (u32 i = 0; i < 4; i++) {
385 // Switch from -Z (south) to -X (west)
386 vertices[i].Pos.rotateXZBy(-90);
387 vertices[i].Pos.rotateXYBy(wicked_time_of_day * 360 - 90);
389 driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
391 float d2 = moonsize * 0.6;
392 vertices[0] = video::S3DVertex(-d, -d, -1, 0, 0, 1, mooncolor2, t, t);
393 vertices[1] = video::S3DVertex( d2,-d, -1, 0, 0, 1, mooncolor2, o, t);
394 vertices[2] = video::S3DVertex( d2, d2, -1, 0, 0, 1, mooncolor2, o, o);
395 vertices[3] = video::S3DVertex(-d, d2, -1, 0, 0, 1, mooncolor2, t, o);
396 for (u32 i = 0; i < 4; i++) {
397 // Switch from -Z (south) to -X (west)
398 vertices[i].Pos.rotateXZBy(-90);
399 vertices[i].Pos.rotateXYBy(wicked_time_of_day * 360 - 90);
401 driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
403 driver->setMaterial(m_materials[4]);
404 float d = moonsize * 1.9;
407 c = video::SColor (0, 0, 0, 0);
409 c = video::SColor (255, 255, 255, 255);
410 vertices[0] = video::S3DVertex(-d, -d, -1, 0, 0, 1, c, t, t);
411 vertices[1] = video::S3DVertex( d, -d, -1, 0, 0, 1, c, o, t);
412 vertices[2] = video::S3DVertex( d, d, -1, 0, 0, 1, c, o, o);
413 vertices[3] = video::S3DVertex(-d, d, -1, 0, 0, 1, c, t, o);
414 for (u32 i = 0; i < 4; i++) {
415 // Switch from -Z (south) to -X (west)
416 vertices[i].Pos.rotateXZBy(-90);
417 vertices[i].Pos.rotateXYBy(wicked_time_of_day * 360 - 90);
419 driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
424 driver->setMaterial(m_materials[1]);
426 float starbrightness = MYMAX(0, MYMIN(1,
427 (0.285 - fabs(wicked_time_of_day < 0.5 ?
428 wicked_time_of_day : (1.0 - wicked_time_of_day))) * 10));
429 float f = starbrightness;
431 video::SColor starcolor(255, f * 90, f * 90, f * 90);
432 if (starcolor.getBlue() < m_skycolor.getBlue())
434 u16 indices[SKY_STAR_COUNT * 4];
435 video::S3DVertex vertices[SKY_STAR_COUNT * 4];
436 for (u32 i = 0; i < SKY_STAR_COUNT; i++) {
437 indices[i * 4 + 0] = i * 4 + 0;
438 indices[i * 4 + 1] = i * 4 + 1;
439 indices[i * 4 + 2] = i * 4 + 2;
440 indices[i * 4 + 3] = i * 4 + 3;
442 core::CMatrix4<f32> a;
443 a.buildRotateFromTo(v3f(0, 1, 0), v3f(d, 1 + d / 2, 0));
446 a.buildRotateFromTo(v3f(0, 1, 0), v3f(d, 1, d));
449 a.buildRotateFromTo(v3f(0, 1, 0), v3f(0, 1 - d / 2, d));
452 p.rotateXYBy(wicked_time_of_day * 360 - 90);
453 p1.rotateXYBy(wicked_time_of_day * 360 - 90);
454 p2.rotateXYBy(wicked_time_of_day * 360 - 90);
455 p3.rotateXYBy(wicked_time_of_day * 360 - 90);
456 vertices[i * 4 + 0].Pos = p;
457 vertices[i * 4 + 0].Color = starcolor;
458 vertices[i * 4 + 1].Pos = p1;
459 vertices[i * 4 + 1].Color = starcolor;
460 vertices[i * 4 + 2].Pos = p2;
461 vertices[i * 4 + 2].Color = starcolor;
462 vertices[i * 4 + 3].Pos = p3;
463 vertices[i * 4 + 3].Color = starcolor;
465 driver->drawVertexPrimitiveList(vertices, SKY_STAR_COUNT * 4,
466 indices, SKY_STAR_COUNT, video::EVT_STANDARD,
467 scene::EPT_QUADS, video::EIT_16BIT);
470 // Draw far cloudy fog thing below east and west horizons
471 for (u32 j = 0; j < 2; j++) {
472 video::SColor c = cloudyfogcolor;
473 vertices[0] = video::S3DVertex(-1, -1.0, -1, 0, 0, 1, c, t, t);
474 vertices[1] = video::S3DVertex( 1, -1.0, -1, 0, 0, 1, c, o, t);
475 vertices[2] = video::S3DVertex( 1, -0.02, -1, 0, 0, 1, c, o, o);
476 vertices[3] = video::S3DVertex(-1, -0.02, -1, 0, 0, 1, c, t, o);
477 for (u32 i = 0; i < 4; i++) {
478 //if (wicked_time_of_day < 0.5)
480 // Switch from -Z (south) to +X (east)
481 vertices[i].Pos.rotateXZBy(90);
483 // Switch from -Z (south) to -X (west)
484 vertices[i].Pos.rotateXZBy(-90);
486 driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
492 void Sky::update(float time_of_day, float time_brightness,
493 float direct_brightness, bool sunlight_seen,
494 CameraMode cam_mode, float yaw, float pitch)
496 // Stabilize initial brightness and color values by flooding updates
497 if (m_first_update) {
498 /*dstream<<"First update with time_of_day="<<time_of_day
499 <<" time_brightness="<<time_brightness
500 <<" direct_brightness="<<direct_brightness
501 <<" sunlight_seen="<<sunlight_seen<<std::endl;*/
502 m_first_update = false;
503 for (u32 i = 0; i < 100; i++) {
504 update(time_of_day, time_brightness, direct_brightness,
505 sunlight_seen, cam_mode, yaw, pitch);
510 m_time_of_day = time_of_day;
511 m_time_brightness = time_brightness;
512 m_sunlight_seen = sunlight_seen;
514 bool is_dawn = (time_brightness >= 0.20 && time_brightness < 0.35);
519 video::SColorf bgcolor_bright_normal_f(170. / 255, 200. / 255, 230. / 255, 1.0);
520 video::SColorf bgcolor_bright_dawn_f(0.666, 200. / 255 * 0.7, 230. / 255 * 0.5, 1.0);
521 video::SColorf bgcolor_bright_dawn_f(0.666, 0.549, 0.220, 1.0);
522 video::SColorf bgcolor_bright_dawn_f(0.666 * 1.2, 0.549 * 1.0, 0.220 * 1.0, 1.0);
523 video::SColorf bgcolor_bright_dawn_f(0.666 * 1.2, 0.549 * 1.0, 0.220 * 1.2, 1.0);
525 video::SColorf cloudcolor_bright_dawn_f(1.0, 0.591, 0.4);
526 video::SColorf cloudcolor_bright_dawn_f(1.0, 0.65, 0.44);
527 video::SColorf cloudcolor_bright_dawn_f(1.0, 0.7, 0.5);
530 video::SColorf bgcolor_bright_normal_f = video::SColor(255, 155, 193, 240);
531 video::SColorf bgcolor_bright_indoor_f = video::SColor(255, 100, 100, 100);
532 video::SColorf bgcolor_bright_dawn_f = video::SColor(255, 186, 193, 240);
533 video::SColorf bgcolor_bright_night_f = video::SColor(255, 64, 144, 255);
535 video::SColorf skycolor_bright_normal_f = video::SColor(255, 140, 186, 250);
536 video::SColorf skycolor_bright_dawn_f = video::SColor(255, 180, 186, 250);
537 video::SColorf skycolor_bright_night_f = video::SColor(255, 0, 107, 255);
539 // pure white: becomes "diffuse light component" for clouds
540 video::SColorf cloudcolor_bright_normal_f = video::SColor(255, 255, 255, 255);
541 // dawn-factoring version of pure white (note: R is above 1.0)
542 video::SColorf cloudcolor_bright_dawn_f(255.0f/240.0f, 223.0f/240.0f, 191.0f/255.0f);
544 float cloud_color_change_fraction = 0.95;
546 if (fabs(time_brightness - m_brightness) < 0.2) {
547 m_brightness = m_brightness * 0.95 + time_brightness * 0.05;
549 m_brightness = m_brightness * 0.80 + time_brightness * 0.20;
550 cloud_color_change_fraction = 0.0;
553 if (direct_brightness < m_brightness)
554 m_brightness = m_brightness * 0.95 + direct_brightness * 0.05;
556 m_brightness = m_brightness * 0.98 + direct_brightness * 0.02;
559 m_clouds_visible = true;
560 float color_change_fraction = 0.98;
562 if (is_dawn) { // Dawn
563 m_bgcolor_bright_f = m_bgcolor_bright_f.getInterpolated(
564 bgcolor_bright_dawn_f, color_change_fraction);
565 m_skycolor_bright_f = m_skycolor_bright_f.getInterpolated(
566 skycolor_bright_dawn_f, color_change_fraction);
567 m_cloudcolor_bright_f = m_cloudcolor_bright_f.getInterpolated(
568 cloudcolor_bright_dawn_f, color_change_fraction);
570 if (time_brightness < 0.07) { // Night
571 m_bgcolor_bright_f = m_bgcolor_bright_f.getInterpolated(
572 bgcolor_bright_night_f, color_change_fraction);
573 m_skycolor_bright_f = m_skycolor_bright_f.getInterpolated(
574 skycolor_bright_night_f, color_change_fraction);
576 m_bgcolor_bright_f = m_bgcolor_bright_f.getInterpolated(
577 bgcolor_bright_normal_f, color_change_fraction);
578 m_skycolor_bright_f = m_skycolor_bright_f.getInterpolated(
579 skycolor_bright_normal_f, color_change_fraction);
582 m_cloudcolor_bright_f = m_cloudcolor_bright_f.getInterpolated(
583 cloudcolor_bright_normal_f, color_change_fraction);
586 m_bgcolor_bright_f = m_bgcolor_bright_f.getInterpolated(
587 bgcolor_bright_indoor_f, color_change_fraction);
588 m_skycolor_bright_f = m_skycolor_bright_f.getInterpolated(
589 bgcolor_bright_indoor_f, color_change_fraction);
590 m_cloudcolor_bright_f = m_cloudcolor_bright_f.getInterpolated(
591 cloudcolor_bright_normal_f, color_change_fraction);
592 m_clouds_visible = false;
595 video::SColor bgcolor_bright = m_bgcolor_bright_f.toSColor();
596 m_bgcolor = video::SColor(
598 bgcolor_bright.getRed() * m_brightness,
599 bgcolor_bright.getGreen() * m_brightness,
600 bgcolor_bright.getBlue() * m_brightness
603 video::SColor skycolor_bright = m_skycolor_bright_f.toSColor();
604 m_skycolor = video::SColor(
606 skycolor_bright.getRed() * m_brightness,
607 skycolor_bright.getGreen() * m_brightness,
608 skycolor_bright.getBlue() * m_brightness
611 // Horizon coloring based on sun and moon direction during sunset and sunrise
612 video::SColor pointcolor = video::SColor(m_bgcolor.getAlpha(), 255, 255, 255);
613 if (m_directional_colored_fog) {
614 if (m_horizon_blend() != 0) {
615 // Calculate hemisphere value from yaw, (inverted in third person front view)
617 if (cam_mode > CAMERA_MODE_THIRD)
619 f32 pointcolor_blend = wrapDegrees_0_360(yaw * dir_factor + 90);
620 if (pointcolor_blend > 180)
621 pointcolor_blend = 360 - pointcolor_blend;
622 pointcolor_blend /= 180;
623 // Bound view angle to determine where transition starts and ends
624 pointcolor_blend = rangelim(1 - pointcolor_blend * 1.375, 0, 1 / 1.375) *
626 // Combine the colors when looking up or down, otherwise turning looks weird
627 pointcolor_blend += (0.5 - pointcolor_blend) *
628 (1 - MYMIN((90 - std::fabs(pitch)) / 90 * 1.5, 1));
629 // Invert direction to match where the sun and moon are rising
630 if (m_time_of_day > 0.5)
631 pointcolor_blend = 1 - pointcolor_blend;
632 // Horizon colors of sun and moon
633 f32 pointcolor_light = rangelim(m_time_brightness * 3, 0.2, 1);
635 video::SColorf pointcolor_sun_f(1, 1, 1, 1);
637 pointcolor_sun_f.r = pointcolor_light *
638 (float)m_materials[3].EmissiveColor.getRed() / 255;
639 pointcolor_sun_f.b = pointcolor_light *
640 (float)m_materials[3].EmissiveColor.getBlue() / 255;
641 pointcolor_sun_f.g = pointcolor_light *
642 (float)m_materials[3].EmissiveColor.getGreen() / 255;
644 pointcolor_sun_f.r = pointcolor_light * 1;
645 pointcolor_sun_f.b = pointcolor_light *
646 (0.25 + (rangelim(m_time_brightness, 0.25, 0.75) - 0.25) * 2 * 0.75);
647 pointcolor_sun_f.g = pointcolor_light * (pointcolor_sun_f.b * 0.375 +
648 (rangelim(m_time_brightness, 0.05, 0.15) - 0.05) * 10 * 0.625);
651 video::SColorf pointcolor_moon_f(0.5 * pointcolor_light,
652 0.6 * pointcolor_light, 0.8 * pointcolor_light, 1);
653 if (m_moon_tonemap) {
654 pointcolor_moon_f.r = pointcolor_light *
655 (float)m_materials[4].EmissiveColor.getRed() / 255;
656 pointcolor_moon_f.b = pointcolor_light *
657 (float)m_materials[4].EmissiveColor.getBlue() / 255;
658 pointcolor_moon_f.g = pointcolor_light *
659 (float)m_materials[4].EmissiveColor.getGreen() / 255;
662 video::SColor pointcolor_sun = pointcolor_sun_f.toSColor();
663 video::SColor pointcolor_moon = pointcolor_moon_f.toSColor();
664 // Calculate the blend color
665 pointcolor = m_mix_scolor(pointcolor_moon, pointcolor_sun, pointcolor_blend);
667 m_bgcolor = m_mix_scolor(m_bgcolor, pointcolor, m_horizon_blend() * 0.5);
668 m_skycolor = m_mix_scolor(m_skycolor, pointcolor, m_horizon_blend() * 0.25);
671 float cloud_direct_brightness = 0;
673 if (!m_directional_colored_fog) {
674 cloud_direct_brightness = time_brightness;
675 if (time_brightness >= 0.2 && time_brightness < 0.7)
676 cloud_direct_brightness *= 1.3;
678 cloud_direct_brightness = MYMIN(m_horizon_blend() * 0.15 +
679 m_time_brightness, 1);
682 cloud_direct_brightness = direct_brightness;
685 m_cloud_brightness = m_cloud_brightness * cloud_color_change_fraction +
686 cloud_direct_brightness * (1.0 - cloud_color_change_fraction);
687 m_cloudcolor_f = video::SColorf(
688 m_cloudcolor_bright_f.r * m_cloud_brightness,
689 m_cloudcolor_bright_f.g * m_cloud_brightness,
690 m_cloudcolor_bright_f.b * m_cloud_brightness,
693 if (m_directional_colored_fog) {
694 m_cloudcolor_f = m_mix_scolorf(m_cloudcolor_f,
695 video::SColorf(pointcolor), m_horizon_blend() * 0.25);