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;
71 m_materials[4].setTexture(0, m_moon_texture);
72 m_materials[4].MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
74 m_materials[4].Lighting = true;
77 for(u32 i=0; i<SKY_STAR_COUNT; i++){
79 myrand_range(-10000,10000),
80 myrand_range(-10000,10000),
81 myrand_range(-10000,10000)
83 m_stars[i].normalize();
86 m_directional_colored_fog = g_settings->getBool("directional_colored_fog");
89 void Sky::OnRegisterSceneNode()
92 SceneManager->registerNodeForRendering(this, scene::ESNRP_SKY_BOX);
94 scene::ISceneNode::OnRegisterSceneNode();
103 video::IVideoDriver* driver = SceneManager->getVideoDriver();
104 scene::ICameraSceneNode* camera = SceneManager->getActiveCamera();
106 if (!camera || !driver)
109 ScopeProfiler sp(g_profiler, "Sky::render()", SPT_AVG);
111 // draw perspective skybox
113 core::matrix4 translate(AbsoluteTransformation);
114 translate.setTranslation(camera->getAbsolutePosition());
116 // Draw the sky box between the near and far clip plane
117 const f32 viewDistance = (camera->getNearValue() + camera->getFarValue()) * 0.5f;
119 scale.setScale(core::vector3df(viewDistance, viewDistance, viewDistance));
121 driver->setTransform(video::ETS_WORLD, translate * scale);
125 float sunsize = 0.07;
126 video::SColorf suncolor_f(1, 1, 0, 1);
128 suncolor_f.g = MYMAX(0.3, MYMIN(1.0, 0.7+m_time_brightness*(0.5)));
129 suncolor_f.b = MYMAX(0.0, m_brightness*0.95);
130 video::SColorf suncolor2_f(1, 1, 1, 1);
132 suncolor_f.g = MYMAX(0.3, MYMIN(1.0, 0.85+m_time_brightness*(0.5)));
133 suncolor_f.b = MYMAX(0.0, m_brightness);
135 float moonsize = 0.04;
136 video::SColorf mooncolor_f(0.50, 0.57, 0.65, 1);
137 video::SColorf mooncolor2_f(0.85, 0.875, 0.9, 1);
139 float nightlength = 0.415;
140 float wn = nightlength / 2;
141 float wicked_time_of_day = 0;
142 if(m_time_of_day > wn && m_time_of_day < 1.0 - wn)
143 wicked_time_of_day = (m_time_of_day - wn)/(1.0-wn*2)*0.5 + 0.25;
144 else if(m_time_of_day < 0.5)
145 wicked_time_of_day = m_time_of_day / wn * 0.25;
147 wicked_time_of_day = 1.0 - ((1.0-m_time_of_day) / wn * 0.25);
148 /*std::cerr<<"time_of_day="<<m_time_of_day<<" -> "
149 <<"wicked_time_of_day="<<wicked_time_of_day<<std::endl;*/
151 video::SColor suncolor = suncolor_f.toSColor();
152 video::SColor suncolor2 = suncolor2_f.toSColor();
153 video::SColor mooncolor = mooncolor_f.toSColor();
154 video::SColor mooncolor2 = mooncolor2_f.toSColor();
156 // Calculate offset normalized to the X dimension of a 512x1 px tonemap
157 float offset=(1.0-fabs(sin((m_time_of_day - 0.5)*irr::core::PI)))*511;
160 u8 * texels = (u8 *)m_sun_tonemap->lock();
161 video::SColor* texel = (video::SColor *)(texels + (u32)offset * 4);
162 video::SColor texel_color (255,texel->getRed(),texel->getGreen(), texel->getBlue());
163 m_sun_tonemap->unlock();
164 m_materials[3].EmissiveColor = texel_color;
167 u8 * texels = (u8 *)m_moon_tonemap->lock();
168 video::SColor* texel = (video::SColor *)(texels + (u32)offset * 4);
169 video::SColor texel_color (255,texel->getRed(),texel->getGreen(), texel->getBlue());
170 m_moon_tonemap->unlock();
171 m_materials[4].EmissiveColor = texel_color;
176 static const u16 indices[4] = {0,1,2,3};
177 video::S3DVertex vertices[4];
179 driver->setMaterial(m_materials[1]);
181 //video::SColor cloudyfogcolor(255,255,255,255);
182 video::SColor cloudyfogcolor = m_bgcolor;
183 //video::SColor cloudyfogcolor = m_bgcolor.getInterpolated(m_skycolor, 0.5);
185 // Draw far cloudy fog thing
186 for(u32 j=0; j<4; j++)
188 video::SColor c = cloudyfogcolor.getInterpolated(m_skycolor, 0.45);
189 vertices[0] = video::S3DVertex(-1, 0.08,-1, 0,0,1, c, t, t);
190 vertices[1] = video::S3DVertex( 1, 0.08,-1, 0,0,1, c, o, t);
191 vertices[2] = video::S3DVertex( 1, 0.12,-1, 0,0,1, c, o, o);
192 vertices[3] = video::S3DVertex(-1, 0.12,-1, 0,0,1, c, t, o);
193 for(u32 i=0; i<4; i++){
198 // Switch from -Z (south) to +X (east)
199 vertices[i].Pos.rotateXZBy(90);
201 // Switch from -Z (south) to -X (west)
202 vertices[i].Pos.rotateXZBy(-90);
204 // Switch from -Z (south) to -Z (north)
205 vertices[i].Pos.rotateXZBy(-180);
207 driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
209 for(u32 j=0; j<4; j++)
211 video::SColor c = cloudyfogcolor;
212 vertices[0] = video::S3DVertex(-1,-1.0,-1, 0,0,1, c, t, t);
213 vertices[1] = video::S3DVertex( 1,-1.0,-1, 0,0,1, c, o, t);
214 vertices[2] = video::S3DVertex( 1, 0.08,-1, 0,0,1, c, o, o);
215 vertices[3] = video::S3DVertex(-1, 0.08,-1, 0,0,1, c, t, o);
216 for(u32 i=0; i<4; i++){
221 // Switch from -Z (south) to +X (east)
222 vertices[i].Pos.rotateXZBy(90);
224 // Switch from -Z (south) to -X (west)
225 vertices[i].Pos.rotateXZBy(-90);
227 // Switch from -Z (south) to -Z (north)
228 vertices[i].Pos.rotateXZBy(-180);
230 driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
233 driver->setMaterial(m_materials[2]);
237 float mid = (wicked_time_of_day < 0.5 ? mid1 : (1.0 - mid1));
238 float a_ = 1.0 - fabs(wicked_time_of_day - mid) * 35.0;
239 float a = easeCurve(MYMAX(0, MYMIN(1, a_)));
240 //std::cerr<<"a_="<<a_<<" a="<<a<<std::endl;
241 video::SColor c(255,255,255,255);
242 float y = -(1.0 - a) * 0.22;
243 vertices[0] = video::S3DVertex(-1,-0.05+y,-1, 0,0,1, c, t, t);
244 vertices[1] = video::S3DVertex( 1,-0.05+y,-1, 0,0,1, c, o, t);
245 vertices[2] = video::S3DVertex( 1, 0.2+y,-1, 0,0,1, c, o, o);
246 vertices[3] = video::S3DVertex(-1, 0.2+y,-1, 0,0,1, c, t, o);
247 for(u32 i=0; i<4; i++){
248 if(wicked_time_of_day < 0.5)
249 // Switch from -Z (south) to +X (east)
250 vertices[i].Pos.rotateXZBy(90);
252 // Switch from -Z (south) to -X (west)
253 vertices[i].Pos.rotateXZBy(-90);
255 driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
259 if(wicked_time_of_day > 0.15 && wicked_time_of_day < 0.85){
261 driver->setMaterial(m_materials[1]);
262 float d = sunsize * 1.7;
263 video::SColor c = suncolor;
264 c.setAlpha(0.05*255);
265 vertices[0] = video::S3DVertex(-d,-d,-1, 0,0,1, c, t, t);
266 vertices[1] = video::S3DVertex( d,-d,-1, 0,0,1, c, o, t);
267 vertices[2] = video::S3DVertex( d, d,-1, 0,0,1, c, o, o);
268 vertices[3] = video::S3DVertex(-d, d,-1, 0,0,1, c, t, o);
269 for(u32 i=0; i<4; i++){
270 // Switch from -Z (south) to +X (east)
271 vertices[i].Pos.rotateXZBy(90);
272 vertices[i].Pos.rotateXYBy(wicked_time_of_day * 360 - 90);
274 driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
278 c.setAlpha(0.15*255);
279 vertices[0] = video::S3DVertex(-d,-d,-1, 0,0,1, c, t, t);
280 vertices[1] = video::S3DVertex( d,-d,-1, 0,0,1, c, o, t);
281 vertices[2] = video::S3DVertex( d, d,-1, 0,0,1, c, o, o);
282 vertices[3] = video::S3DVertex(-d, d,-1, 0,0,1, c, t, o);
283 for(u32 i=0; i<4; i++){
284 // Switch from -Z (south) to +X (east)
285 vertices[i].Pos.rotateXZBy(90);
286 vertices[i].Pos.rotateXYBy(wicked_time_of_day * 360 - 90);
288 driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
291 vertices[0] = video::S3DVertex(-d,-d,-1, 0,0,1, suncolor, t, t);
292 vertices[1] = video::S3DVertex( d,-d,-1, 0,0,1, suncolor, o, t);
293 vertices[2] = video::S3DVertex( d, d,-1, 0,0,1, suncolor, o, o);
294 vertices[3] = video::S3DVertex(-d, d,-1, 0,0,1, suncolor, t, o);
295 for(u32 i=0; i<4; i++){
296 // Switch from -Z (south) to +X (east)
297 vertices[i].Pos.rotateXZBy(90);
298 vertices[i].Pos.rotateXYBy(wicked_time_of_day * 360 - 90);
300 driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
303 vertices[0] = video::S3DVertex(-d,-d,-1, 0,0,1, suncolor2, t, t);
304 vertices[1] = video::S3DVertex( d,-d,-1, 0,0,1, suncolor2, o, t);
305 vertices[2] = video::S3DVertex( d, d,-1, 0,0,1, suncolor2, o, o);
306 vertices[3] = video::S3DVertex(-d, d,-1, 0,0,1, suncolor2, t, o);
307 for(u32 i=0; i<4; i++){
308 // Switch from -Z (south) to +X (east)
309 vertices[i].Pos.rotateXZBy(90);
310 vertices[i].Pos.rotateXYBy(wicked_time_of_day * 360 - 90);
312 driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
314 driver->setMaterial(m_materials[3]);
315 float d = sunsize * 1.7;
318 c = video::SColor (0,0,0,0);
320 c = video::SColor (255,255,255,255);
321 vertices[0] = video::S3DVertex(-d,-d,-1, 0,0,1, c, t, t);
322 vertices[1] = video::S3DVertex( d,-d,-1, 0,0,1, c, o, t);
323 vertices[2] = video::S3DVertex( d, d,-1, 0,0,1, c, o, o);
324 vertices[3] = video::S3DVertex(-d, d,-1, 0,0,1, c, t, o);
325 for(u32 i=0; i<4; i++){
326 // Switch from -Z (south) to +X (east)
327 vertices[i].Pos.rotateXZBy(90);
328 vertices[i].Pos.rotateXYBy(wicked_time_of_day * 360 - 90);
330 driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
335 if(wicked_time_of_day < 0.3 || wicked_time_of_day > 0.7)
337 if (!m_moon_texture){
338 driver->setMaterial(m_materials[1]);
339 float d = moonsize * 1.9;
340 video::SColor c = mooncolor;
341 c.setAlpha(0.05*255);
342 vertices[0] = video::S3DVertex(-d,-d,-1, 0,0,1, c, t, t);
343 vertices[1] = video::S3DVertex( d,-d,-1, 0,0,1, c, o, t);
344 vertices[2] = video::S3DVertex( d, d,-1, 0,0,1, c, o, o);
345 vertices[3] = video::S3DVertex(-d, d,-1, 0,0,1, c, t, o);
346 for(u32 i=0; i<4; i++){
347 // Switch from -Z (south) to -X (west)
348 vertices[i].Pos.rotateXZBy(-90);
349 vertices[i].Pos.rotateXYBy(wicked_time_of_day * 360 - 90);
351 driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
355 c.setAlpha(0.15*255);
356 vertices[0] = video::S3DVertex(-d,-d,-1, 0,0,1, c, t, t);
357 vertices[1] = video::S3DVertex( d,-d,-1, 0,0,1, c, o, t);
358 vertices[2] = video::S3DVertex( d, d,-1, 0,0,1, c, o, o);
359 vertices[3] = video::S3DVertex(-d, d,-1, 0,0,1, c, t, o);
360 for(u32 i=0; i<4; i++){
361 // Switch from -Z (south) to -X (west)
362 vertices[i].Pos.rotateXZBy(-90);
363 vertices[i].Pos.rotateXYBy(wicked_time_of_day * 360 - 90);
365 driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
368 vertices[0] = video::S3DVertex(-d,-d,-1, 0,0,1, mooncolor, t, t);
369 vertices[1] = video::S3DVertex( d,-d,-1, 0,0,1, mooncolor, o, t);
370 vertices[2] = video::S3DVertex( d, d,-1, 0,0,1, mooncolor, o, o);
371 vertices[3] = video::S3DVertex(-d, d,-1, 0,0,1, mooncolor, 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);
379 float d2 = moonsize * 0.6;
380 vertices[0] = video::S3DVertex(-d,-d,-1, 0,0,1, mooncolor2, t, t);
381 vertices[1] = video::S3DVertex( d2,-d,-1, 0,0,1, mooncolor2, o, t);
382 vertices[2] = video::S3DVertex( d2, d2,-1, 0,0,1, mooncolor2, o, o);
383 vertices[3] = video::S3DVertex(-d, d2,-1, 0,0,1, mooncolor2, 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 driver->setMaterial(m_materials[4]);
392 float d = moonsize * 1.9;
395 c = video::SColor (0,0,0,0);
397 c = video::SColor (255,255,255,255);
398 vertices[0] = video::S3DVertex(-d,-d,-1, 0,0,1, c, t, t);
399 vertices[1] = video::S3DVertex( d,-d,-1, 0,0,1, c, o, t);
400 vertices[2] = video::S3DVertex( d, d,-1, 0,0,1, c, o, o);
401 vertices[3] = video::S3DVertex(-d, d,-1, 0,0,1, c, t, o);
402 for(u32 i=0; i<4; i++){
403 // Switch from -Z (south) to -X (west)
404 vertices[i].Pos.rotateXZBy(-90);
405 vertices[i].Pos.rotateXYBy(wicked_time_of_day * 360 - 90);
407 driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
412 driver->setMaterial(m_materials[1]);
414 float starbrightness = MYMAX(0, MYMIN(1,
415 (0.285 - fabs(wicked_time_of_day < 0.5 ?
416 wicked_time_of_day : (1.0 - wicked_time_of_day))) * 10));
417 float f = starbrightness;
419 video::SColor starcolor(255, f*90,f*90,f*90);
420 if(starcolor.getBlue() < m_skycolor.getBlue())
422 u16 indices[SKY_STAR_COUNT*4];
423 video::S3DVertex vertices[SKY_STAR_COUNT*4];
424 for(u32 i=0; i<SKY_STAR_COUNT; i++){
425 indices[i*4+0] = i*4+0;
426 indices[i*4+1] = i*4+1;
427 indices[i*4+2] = i*4+2;
428 indices[i*4+3] = i*4+3;
430 core::CMatrix4<f32> a;
431 a.buildRotateFromTo(v3f(0,1,0), v3f(d,1+d/2,0));
434 a.buildRotateFromTo(v3f(0,1,0), v3f(d,1,d));
437 a.buildRotateFromTo(v3f(0,1,0), v3f(0,1-d/2,d));
440 p.rotateXYBy(wicked_time_of_day * 360 - 90);
441 p1.rotateXYBy(wicked_time_of_day * 360 - 90);
442 p2.rotateXYBy(wicked_time_of_day * 360 - 90);
443 p3.rotateXYBy(wicked_time_of_day * 360 - 90);
444 vertices[i*4+0].Pos = p;
445 vertices[i*4+0].Color = starcolor;
446 vertices[i*4+1].Pos = p1;
447 vertices[i*4+1].Color = starcolor;
448 vertices[i*4+2].Pos = p2;
449 vertices[i*4+2].Color = starcolor;
450 vertices[i*4+3].Pos = p3;
451 vertices[i*4+3].Color = starcolor;
453 driver->drawVertexPrimitiveList(vertices, SKY_STAR_COUNT*4,
454 indices, SKY_STAR_COUNT, video::EVT_STANDARD,
455 scene::EPT_QUADS, video::EIT_16BIT);
458 for(u32 j=0; j<2; j++)
460 //video::SColor c = m_skycolor;
461 video::SColor c = cloudyfogcolor;
462 vertices[0] = video::S3DVertex(-1,-1.0,-1, 0,0,1, c, t, t);
463 vertices[1] = video::S3DVertex( 1,-1.0,-1, 0,0,1, c, o, t);
464 vertices[2] = video::S3DVertex( 1,-0.02,-1, 0,0,1, c, o, o);
465 vertices[3] = video::S3DVertex(-1,-0.02,-1, 0,0,1, c, t, o);
466 for(u32 i=0; i<4; i++){
467 //if(wicked_time_of_day < 0.5)
469 // Switch from -Z (south) to +X (east)
470 vertices[i].Pos.rotateXZBy(90);
472 // Switch from -Z (south) to -X (west)
473 vertices[i].Pos.rotateXZBy(-90);
475 driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
480 void Sky::update(float time_of_day, float time_brightness,
481 float direct_brightness, bool sunlight_seen,
482 CameraMode cam_mode, float yaw, float pitch)
484 // Stabilize initial brightness and color values by flooding updates
486 /*dstream<<"First update with time_of_day="<<time_of_day
487 <<" time_brightness="<<time_brightness
488 <<" direct_brightness="<<direct_brightness
489 <<" sunlight_seen="<<sunlight_seen<<std::endl;*/
490 m_first_update = false;
491 for(u32 i=0; i<100; i++){
492 update(time_of_day, time_brightness, direct_brightness,
493 sunlight_seen, cam_mode, yaw, pitch);
498 m_time_of_day = time_of_day;
499 m_time_brightness = time_brightness;
500 m_sunlight_seen = sunlight_seen;
502 bool is_dawn = (time_brightness >= 0.20 && time_brightness < 0.35);
504 //video::SColorf bgcolor_bright_normal_f(170./255,200./255,230./255, 1.0);
505 video::SColorf bgcolor_bright_normal_f(155./255,193./255,240./255, 1.0);
506 video::SColorf bgcolor_bright_indoor_f(100./255,100./255,100./255, 1.0);
507 //video::SColorf bgcolor_bright_dawn_f(0.666,200./255*0.7,230./255*0.5,1.0);
508 //video::SColorf bgcolor_bright_dawn_f(0.666,0.549,0.220,1.0);
509 //video::SColorf bgcolor_bright_dawn_f(0.666*1.2,0.549*1.0,0.220*1.0, 1.0);
510 //video::SColorf bgcolor_bright_dawn_f(0.666*1.2,0.549*1.0,0.220*1.2,1.0);
511 video::SColorf bgcolor_bright_dawn_f
512 (155./255*1.2,193./255,240./255, 1.0);
513 video::SColorf bgcolor_bright_night_f
514 (64./255, 144./255, 255./255, 1.0);
516 video::SColorf skycolor_bright_normal_f =
517 video::SColor(255, 140, 186, 250);
518 video::SColorf skycolor_bright_dawn_f =
519 video::SColor(255, 180, 186, 250);
520 video::SColorf skycolor_bright_night_f =
521 video::SColor(255, 0, 107, 255);
523 video::SColorf cloudcolor_bright_normal_f =
524 video::SColor(255, 240,240,255);
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);
528 video::SColorf cloudcolor_bright_dawn_f(1.0, 0.875, 0.75);
530 float cloud_color_change_fraction = 0.95;
532 if(fabs(time_brightness - m_brightness) < 0.2){
533 m_brightness = m_brightness * 0.95 + time_brightness * 0.05;
535 m_brightness = m_brightness * 0.80 + time_brightness * 0.20;
536 cloud_color_change_fraction = 0.0;
540 if(direct_brightness < m_brightness)
541 m_brightness = m_brightness * 0.95 + direct_brightness * 0.05;
543 m_brightness = m_brightness * 0.98 + direct_brightness * 0.02;
546 m_clouds_visible = true;
547 float color_change_fraction = 0.98;
550 m_bgcolor_bright_f = m_bgcolor_bright_f.getInterpolated(
551 bgcolor_bright_dawn_f, color_change_fraction);
552 m_skycolor_bright_f = m_skycolor_bright_f.getInterpolated(
553 skycolor_bright_dawn_f, color_change_fraction);
554 m_cloudcolor_bright_f = m_cloudcolor_bright_f.getInterpolated(
555 cloudcolor_bright_dawn_f, color_change_fraction);
557 if (time_brightness < 0.07) { // Night sky
558 m_bgcolor_bright_f = m_bgcolor_bright_f.getInterpolated(
559 bgcolor_bright_night_f, color_change_fraction);
560 m_skycolor_bright_f = m_skycolor_bright_f.getInterpolated(
561 skycolor_bright_night_f, color_change_fraction);
562 } else { // Daytime sky
563 m_bgcolor_bright_f = m_bgcolor_bright_f.getInterpolated(
564 bgcolor_bright_normal_f, color_change_fraction);
565 m_skycolor_bright_f = m_skycolor_bright_f.getInterpolated(
566 skycolor_bright_normal_f, color_change_fraction);
569 m_cloudcolor_bright_f = m_cloudcolor_bright_f.getInterpolated(
570 cloudcolor_bright_normal_f, color_change_fraction);
573 m_bgcolor_bright_f = m_bgcolor_bright_f.getInterpolated(
574 bgcolor_bright_indoor_f, color_change_fraction);
575 m_skycolor_bright_f = m_skycolor_bright_f.getInterpolated(
576 bgcolor_bright_indoor_f, color_change_fraction);
577 m_cloudcolor_bright_f = m_cloudcolor_bright_f.getInterpolated(
578 cloudcolor_bright_normal_f, color_change_fraction);
579 m_clouds_visible = false;
582 video::SColor bgcolor_bright = m_bgcolor_bright_f.toSColor();
583 m_bgcolor = video::SColor(
585 bgcolor_bright.getRed() * m_brightness,
586 bgcolor_bright.getGreen() * m_brightness,
587 bgcolor_bright.getBlue() * m_brightness);
589 video::SColor skycolor_bright = m_skycolor_bright_f.toSColor();
590 m_skycolor = video::SColor(
592 skycolor_bright.getRed() * m_brightness,
593 skycolor_bright.getGreen() * m_brightness,
594 skycolor_bright.getBlue() * m_brightness);
596 // Horizon coloring based on sun and moon direction during sunset and sunrise
597 video::SColor pointcolor = video::SColor(255, 255, 255, m_bgcolor.getAlpha());
598 if (m_directional_colored_fog) {
599 if (m_horizon_blend() != 0)
601 // calculate hemisphere value from yaw, (inverted in third person front view)
603 if (cam_mode > CAMERA_MODE_THIRD)
605 f32 pointcolor_blend = wrapDegrees_0_360( yaw*dir_factor + 90);
606 if (pointcolor_blend > 180)
607 pointcolor_blend = 360 - pointcolor_blend;
608 pointcolor_blend /= 180;
609 // bound view angle to determine where transition starts and ends
610 pointcolor_blend = rangelim(1 - pointcolor_blend * 1.375, 0, 1 / 1.375) * 1.375;
611 // combine the colors when looking up or down, otherwise turning looks weird
612 pointcolor_blend += (0.5 - pointcolor_blend) * (1 - MYMIN((90 - std::abs(pitch)) / 90 * 1.5, 1));
613 // invert direction to match where the sun and moon are rising
614 if (m_time_of_day > 0.5)
615 pointcolor_blend = 1 - pointcolor_blend;
616 // horizon colors of sun and moon
617 f32 pointcolor_light = rangelim(m_time_brightness * 3, 0.2, 1);
619 video::SColorf pointcolor_sun_f(1, 1, 1, 1);
622 pointcolor_sun_f.r = pointcolor_light * (float)m_materials[3].EmissiveColor.getRed() / 255;
623 pointcolor_sun_f.b = pointcolor_light * (float)m_materials[3].EmissiveColor.getBlue() / 255;
624 pointcolor_sun_f.g = pointcolor_light * (float)m_materials[3].EmissiveColor.getGreen() / 255;
628 pointcolor_sun_f.r = pointcolor_light * 1;
629 pointcolor_sun_f.b = pointcolor_light * (0.25 + (rangelim(m_time_brightness, 0.25, 0.75) - 0.25) * 2 * 0.75);
630 pointcolor_sun_f.g = pointcolor_light * (pointcolor_sun_f.b * 0.375 + (rangelim(m_time_brightness, 0.05, 0.15) - 0.05) * 10 * 0.625);
633 video::SColorf pointcolor_moon_f(0.5 * pointcolor_light, 0.6 * pointcolor_light, 0.8 * pointcolor_light, 1);
636 pointcolor_moon_f.r = pointcolor_light * (float)m_materials[4].EmissiveColor.getRed() / 255;
637 pointcolor_moon_f.b = pointcolor_light * (float)m_materials[4].EmissiveColor.getBlue() / 255;
638 pointcolor_moon_f.g = pointcolor_light * (float)m_materials[4].EmissiveColor.getGreen() / 255;
641 video::SColor pointcolor_sun = pointcolor_sun_f.toSColor();
642 video::SColor pointcolor_moon = pointcolor_moon_f.toSColor();
643 // calculate the blend color
644 pointcolor = m_mix_scolor(pointcolor_moon, pointcolor_sun, pointcolor_blend);
646 m_bgcolor = m_mix_scolor(m_bgcolor, pointcolor, m_horizon_blend() * 0.5);
647 m_skycolor = m_mix_scolor(m_skycolor, pointcolor, m_horizon_blend() * 0.25);
650 float cloud_direct_brightness = 0;
652 if (!m_directional_colored_fog) {
653 cloud_direct_brightness = time_brightness;
654 if(time_brightness >= 0.2 && time_brightness < 0.7)
655 cloud_direct_brightness *= 1.3;
658 cloud_direct_brightness = MYMIN(m_horizon_blend() * 0.15 + m_time_brightness, 1);
661 cloud_direct_brightness = direct_brightness;
663 m_cloud_brightness = m_cloud_brightness * cloud_color_change_fraction +
664 cloud_direct_brightness * (1.0 - cloud_color_change_fraction);
665 m_cloudcolor_f = video::SColorf(
666 m_cloudcolor_bright_f.r * m_cloud_brightness,
667 m_cloudcolor_bright_f.g * m_cloud_brightness,
668 m_cloudcolor_bright_f.b * m_cloud_brightness,
670 if (m_directional_colored_fog) {
671 m_cloudcolor_f = m_mix_scolorf(m_cloudcolor_f, video::SColorf(pointcolor), m_horizon_blend() * 0.25);