3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4 Copyright (C) 2020 numzero, Lobachevskiy Vitaliy <numzer0@yandex.ru>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include "IVideoDriver.h"
24 #include "ISceneManager.h"
25 #include "ICameraSceneNode.h"
26 #include "S3DVertex.h"
27 #include "client/tile.h"
28 #include "noise.h" // easeCurve
30 #include "util/numeric.h"
32 #include "client/renderingengine.h"
34 #include "camera.h" // CameraModes
36 using namespace irr::core;
38 static video::SMaterial baseMaterial()
42 #if IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR > 8
43 mat.ZBuffer = video::ECFN_DISABLED;
44 mat.ZWriteEnable = video::EZW_OFF;
46 mat.ZWriteEnable = false;
47 mat.ZBuffer = video::ECFN_NEVER;
50 mat.TextureLayer[0].TextureWrapU = video::ETC_CLAMP_TO_EDGE;
51 mat.TextureLayer[0].TextureWrapV = video::ETC_CLAMP_TO_EDGE;
52 mat.BackfaceCulling = false;
56 Sky::Sky(s32 id, ITextureSource *tsrc, IShaderSource *ssrc) :
57 scene::ISceneNode(RenderingEngine::get_scene_manager()->getRootSceneNode(),
58 RenderingEngine::get_scene_manager(), id)
60 setAutomaticCulling(scene::EAC_OFF);
61 m_box.MaxEdge.set(0, 0, 0);
62 m_box.MinEdge.set(0, 0, 0);
64 m_enable_shaders = g_settings->getBool("enable_shaders");
68 m_materials[0] = baseMaterial();
69 m_materials[0].MaterialType = ssrc->getShaderInfo(ssrc->getShader("stars_shader", TILE_MATERIAL_ALPHA)).material;
70 m_materials[0].Lighting = true;
71 m_materials[0].ColorMaterial = video::ECM_NONE;
73 m_materials[1] = baseMaterial();
74 //m_materials[1].MaterialType = video::EMT_TRANSPARENT_VERTEX_ALPHA;
75 m_materials[1].MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
77 m_materials[2] = baseMaterial();
78 m_materials[2].setTexture(0, tsrc->getTextureForMesh("sunrisebg.png"));
79 m_materials[2].MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
80 //m_materials[2].MaterialType = video::EMT_TRANSPARENT_ADD_COLOR;
82 // Ensures that sun and moon textures and tonemaps are correct.
84 m_sun_texture = tsrc->isKnownSourceImage(m_sun_params.texture) ?
85 tsrc->getTextureForMesh(m_sun_params.texture) : NULL;
86 m_moon_texture = tsrc->isKnownSourceImage(m_moon_params.texture) ?
87 tsrc->getTextureForMesh(m_moon_params.texture) : NULL;
88 m_sun_tonemap = tsrc->isKnownSourceImage(m_sun_params.tonemap) ?
89 tsrc->getTexture(m_sun_params.tonemap) : NULL;
90 m_moon_tonemap = tsrc->isKnownSourceImage(m_moon_params.tonemap) ?
91 tsrc->getTexture(m_moon_params.tonemap) : NULL;
94 m_materials[3] = baseMaterial();
95 m_materials[3].setTexture(0, m_sun_texture);
96 m_materials[3].MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
97 // Disables texture filtering
98 m_materials[3].setFlag(video::E_MATERIAL_FLAG::EMF_BILINEAR_FILTER, false);
99 m_materials[3].setFlag(video::E_MATERIAL_FLAG::EMF_TRILINEAR_FILTER, false);
100 m_materials[3].setFlag(video::E_MATERIAL_FLAG::EMF_ANISOTROPIC_FILTER, false);
101 // Use tonemaps if available
103 m_materials[3].Lighting = true;
105 if (m_moon_texture) {
106 m_materials[4] = baseMaterial();
107 m_materials[4].setTexture(0, m_moon_texture);
108 m_materials[4].MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
109 // Disables texture filtering
110 m_materials[4].setFlag(video::E_MATERIAL_FLAG::EMF_BILINEAR_FILTER, false);
111 m_materials[4].setFlag(video::E_MATERIAL_FLAG::EMF_TRILINEAR_FILTER, false);
112 m_materials[4].setFlag(video::E_MATERIAL_FLAG::EMF_ANISOTROPIC_FILTER, false);
113 // Use tonemaps if available
115 m_materials[4].Lighting = true;
118 for (int i = 5; i < 11; i++) {
119 m_materials[i] = baseMaterial();
120 m_materials[i].Lighting = true;
121 m_materials[i].MaterialType = video::EMT_SOLID;
123 m_directional_colored_fog = g_settings->getBool("directional_colored_fog");
124 setStarCount(1000, true);
127 void Sky::OnRegisterSceneNode()
130 SceneManager->registerNodeForRendering(this, scene::ESNRP_SKY_BOX);
132 scene::ISceneNode::OnRegisterSceneNode();
137 video::IVideoDriver *driver = SceneManager->getVideoDriver();
138 scene::ICameraSceneNode *camera = SceneManager->getActiveCamera();
140 if (!camera || !driver)
143 ScopeProfiler sp(g_profiler, "Sky::render()", SPT_AVG);
145 // Draw perspective skybox
147 core::matrix4 translate(AbsoluteTransformation);
148 translate.setTranslation(camera->getAbsolutePosition());
150 // Draw the sky box between the near and far clip plane
151 const f32 viewDistance = (camera->getNearValue() + camera->getFarValue()) * 0.5f;
153 scale.setScale(core::vector3df(viewDistance, viewDistance, viewDistance));
155 driver->setTransform(video::ETS_WORLD, translate * scale);
157 if (m_sunlight_seen) {
158 float sunsize = 0.07;
159 video::SColorf suncolor_f(1, 1, 0, 1);
161 //suncolor_f.g = MYMAX(0.3, MYMIN(1.0, 0.7 + m_time_brightness * 0.5));
162 //suncolor_f.b = MYMAX(0.0, m_brightness * 0.95);
163 video::SColorf suncolor2_f(1, 1, 1, 1);
164 // The values below were probably meant to be suncolor2_f instead of a
165 // reassignment of suncolor_f. However, the resulting colour was chosen
166 // and is our long-running classic colour. So preserve, but comment-out
167 // the unnecessary first assignments above.
169 suncolor_f.g = MYMAX(0.3, MYMIN(1.0, 0.85 + m_time_brightness * 0.5));
170 suncolor_f.b = MYMAX(0.0, m_brightness);
172 float moonsize = 0.04;
173 video::SColorf mooncolor_f(0.50, 0.57, 0.65, 1);
174 video::SColorf mooncolor2_f(0.85, 0.875, 0.9, 1);
176 float nightlength = 0.415;
177 float wn = nightlength / 2;
178 float wicked_time_of_day = 0;
179 if (m_time_of_day > wn && m_time_of_day < 1.0 - wn)
180 wicked_time_of_day = (m_time_of_day - wn) / (1.0 - wn * 2) * 0.5 + 0.25;
181 else if (m_time_of_day < 0.5)
182 wicked_time_of_day = m_time_of_day / wn * 0.25;
184 wicked_time_of_day = 1.0 - ((1.0 - m_time_of_day) / wn * 0.25);
185 /*std::cerr<<"time_of_day="<<m_time_of_day<<" -> "
186 <<"wicked_time_of_day="<<wicked_time_of_day<<std::endl;*/
188 video::SColor suncolor = suncolor_f.toSColor();
189 video::SColor suncolor2 = suncolor2_f.toSColor();
190 video::SColor mooncolor = mooncolor_f.toSColor();
191 video::SColor mooncolor2 = mooncolor2_f.toSColor();
193 // Calculate offset normalized to the X dimension of a 512x1 px tonemap
194 float offset = (1.0 - fabs(sin((m_time_of_day - 0.5) * irr::core::PI))) * 511;
197 u8 * texels = (u8 *)m_sun_tonemap->lock();
198 video::SColor* texel = (video::SColor *)(texels + (u32)offset * 4);
199 video::SColor texel_color (255, texel->getRed(),
200 texel->getGreen(), texel->getBlue());
201 m_sun_tonemap->unlock();
202 m_materials[3].EmissiveColor = texel_color;
205 if (m_moon_tonemap) {
206 u8 * texels = (u8 *)m_moon_tonemap->lock();
207 video::SColor* texel = (video::SColor *)(texels + (u32)offset * 4);
208 video::SColor texel_color (255, texel->getRed(),
209 texel->getGreen(), texel->getBlue());
210 m_moon_tonemap->unlock();
211 m_materials[4].EmissiveColor = texel_color;
216 static const u16 indices[6] = {0, 1, 2, 0, 2, 3};
217 video::S3DVertex vertices[4];
219 driver->setMaterial(m_materials[1]);
221 video::SColor cloudyfogcolor = m_bgcolor;
223 // Abort rendering if we're in the clouds.
224 // Stops rendering a pure white hole in the bottom of the skybox.
228 // Draw the six sided skybox,
229 if (m_sky_params.textures.size() == 6) {
230 for (u32 j = 5; j < 11; j++) {
231 video::SColor c(255, 255, 255, 255);
232 driver->setMaterial(m_materials[j]);
233 // Use 1.05 rather than 1.0 to avoid colliding with the
234 // sun, moon and stars, as this is a background skybox.
235 vertices[0] = video::S3DVertex(-1.05, -1.05, -1.05, 0, 0, 1, c, t, t);
236 vertices[1] = video::S3DVertex( 1.05, -1.05, -1.05, 0, 0, 1, c, o, t);
237 vertices[2] = video::S3DVertex( 1.05, 1.05, -1.05, 0, 0, 1, c, o, o);
238 vertices[3] = video::S3DVertex(-1.05, 1.05, -1.05, 0, 0, 1, c, t, o);
239 for (video::S3DVertex &vertex : vertices) {
240 if (j == 5) { // Top texture
241 vertex.Pos.rotateYZBy(90);
242 vertex.Pos.rotateXZBy(90);
243 } else if (j == 6) { // Bottom texture
244 vertex.Pos.rotateYZBy(-90);
245 vertex.Pos.rotateXZBy(90);
246 } else if (j == 7) { // Left texture
247 vertex.Pos.rotateXZBy(90);
248 } else if (j == 8) { // Right texture
249 vertex.Pos.rotateXZBy(-90);
250 } else if (j == 9) { // Front texture, do nothing
251 // Irrlicht doesn't like it when vertexes are left
252 // alone and not rotated for some reason.
253 vertex.Pos.rotateXZBy(0);
254 } else {// Back texture
255 vertex.Pos.rotateXZBy(180);
258 driver->drawIndexedTriangleList(&vertices[0], 4, indices, 2);
262 // Draw far cloudy fog thing blended with skycolor
264 driver->setMaterial(m_materials[1]);
265 for (u32 j = 0; j < 4; j++) {
266 vertices[0] = video::S3DVertex(-1, -0.02, -1, 0, 0, 1, m_bgcolor, t, t);
267 vertices[1] = video::S3DVertex( 1, -0.02, -1, 0, 0, 1, m_bgcolor, o, t);
268 vertices[2] = video::S3DVertex( 1, 0.45, -1, 0, 0, 1, m_skycolor, o, o);
269 vertices[3] = video::S3DVertex(-1, 0.45, -1, 0, 0, 1, m_skycolor, t, o);
270 for (video::S3DVertex &vertex : vertices) {
275 // Switch from -Z (south) to +X (east)
276 vertex.Pos.rotateXZBy(90);
278 // Switch from -Z (south) to -X (west)
279 vertex.Pos.rotateXZBy(-90);
281 // Switch from -Z (south) to +Z (north)
282 vertex.Pos.rotateXZBy(-180);
284 driver->drawIndexedTriangleList(&vertices[0], 4, indices, 2);
288 // Draw stars before sun and moon to be behind them
289 if (m_star_params.visible)
290 draw_stars(driver, wicked_time_of_day);
292 // Draw sunrise/sunset horizon glow texture
293 // (textures/base/pack/sunrisebg.png)
294 if (m_sun_params.sunrise_visible) {
295 driver->setMaterial(m_materials[2]);
297 float mid = wicked_time_of_day < 0.5 ? mid1 : (1.0 - mid1);
298 float a_ = 1.0f - std::fabs(wicked_time_of_day - mid) * 35.0f;
299 float a = easeCurve(MYMAX(0, MYMIN(1, a_)));
300 //std::cerr<<"a_="<<a_<<" a="<<a<<std::endl;
301 video::SColor c(255, 255, 255, 255);
302 float y = -(1.0 - a) * 0.22;
303 vertices[0] = video::S3DVertex(-1, -0.05 + y, -1, 0, 0, 1, c, t, t);
304 vertices[1] = video::S3DVertex( 1, -0.05 + y, -1, 0, 0, 1, c, o, t);
305 vertices[2] = video::S3DVertex( 1, 0.2 + y, -1, 0, 0, 1, c, o, o);
306 vertices[3] = video::S3DVertex(-1, 0.2 + y, -1, 0, 0, 1, c, t, o);
307 for (video::S3DVertex &vertex : vertices) {
308 if (wicked_time_of_day < 0.5)
309 // Switch from -Z (south) to +X (east)
310 vertex.Pos.rotateXZBy(90);
312 // Switch from -Z (south) to -X (west)
313 vertex.Pos.rotateXZBy(-90);
315 driver->drawIndexedTriangleList(&vertices[0], 4, indices, 2);
319 if (m_sun_params.visible)
320 draw_sun(driver, sunsize, suncolor, suncolor2, wicked_time_of_day);
323 if (m_moon_params.visible)
324 draw_moon(driver, moonsize, mooncolor, mooncolor2, wicked_time_of_day);
326 // Draw far cloudy fog thing below all horizons in front of sun, moon
329 driver->setMaterial(m_materials[1]);
331 for (u32 j = 0; j < 4; j++) {
332 video::SColor c = cloudyfogcolor;
333 vertices[0] = video::S3DVertex(-1, -1.0, -1, 0, 0, 1, c, t, t);
334 vertices[1] = video::S3DVertex( 1, -1.0, -1, 0, 0, 1, c, o, t);
335 vertices[2] = video::S3DVertex( 1, -0.02, -1, 0, 0, 1, c, o, o);
336 vertices[3] = video::S3DVertex(-1, -0.02, -1, 0, 0, 1, c, t, o);
337 for (video::S3DVertex &vertex : vertices) {
342 // Switch from -Z (south) to +X (east)
343 vertex.Pos.rotateXZBy(90);
345 // Switch from -Z (south) to -X (west)
346 vertex.Pos.rotateXZBy(-90);
348 // Switch from -Z (south) to +Z (north)
349 vertex.Pos.rotateXZBy(-180);
351 driver->drawIndexedTriangleList(&vertices[0], 4, indices, 2);
354 // Draw bottom far cloudy fog thing in front of sun, moon and stars
355 video::SColor c = cloudyfogcolor;
356 vertices[0] = video::S3DVertex(-1, -1.0, -1, 0, 1, 0, c, t, t);
357 vertices[1] = video::S3DVertex( 1, -1.0, -1, 0, 1, 0, c, o, t);
358 vertices[2] = video::S3DVertex( 1, -1.0, 1, 0, 1, 0, c, o, o);
359 vertices[3] = video::S3DVertex(-1, -1.0, 1, 0, 1, 0, c, t, o);
360 driver->drawIndexedTriangleList(&vertices[0], 4, indices, 2);
365 void Sky::update(float time_of_day, float time_brightness,
366 float direct_brightness, bool sunlight_seen,
367 CameraMode cam_mode, float yaw, float pitch)
369 // Stabilize initial brightness and color values by flooding updates
370 if (m_first_update) {
371 /*dstream<<"First update with time_of_day="<<time_of_day
372 <<" time_brightness="<<time_brightness
373 <<" direct_brightness="<<direct_brightness
374 <<" sunlight_seen="<<sunlight_seen<<std::endl;*/
375 m_first_update = false;
376 for (u32 i = 0; i < 100; i++) {
377 update(time_of_day, time_brightness, direct_brightness,
378 sunlight_seen, cam_mode, yaw, pitch);
383 m_time_of_day = time_of_day;
384 m_time_brightness = time_brightness;
385 m_sunlight_seen = sunlight_seen;
388 bool is_dawn = (time_brightness >= 0.20 && time_brightness < 0.35);
393 video::SColorf bgcolor_bright_normal_f(170. / 255, 200. / 255, 230. / 255, 1.0);
394 video::SColorf bgcolor_bright_dawn_f(0.666, 200. / 255 * 0.7, 230. / 255 * 0.5, 1.0);
395 video::SColorf bgcolor_bright_dawn_f(0.666, 0.549, 0.220, 1.0);
396 video::SColorf bgcolor_bright_dawn_f(0.666 * 1.2, 0.549 * 1.0, 0.220 * 1.0, 1.0);
397 video::SColorf bgcolor_bright_dawn_f(0.666 * 1.2, 0.549 * 1.0, 0.220 * 1.2, 1.0);
399 video::SColorf cloudcolor_bright_dawn_f(1.0, 0.591, 0.4);
400 video::SColorf cloudcolor_bright_dawn_f(1.0, 0.65, 0.44);
401 video::SColorf cloudcolor_bright_dawn_f(1.0, 0.7, 0.5);
404 video::SColorf bgcolor_bright_normal_f = m_sky_params.sky_color.day_horizon;
405 video::SColorf bgcolor_bright_indoor_f = m_sky_params.sky_color.indoors;
406 video::SColorf bgcolor_bright_dawn_f = m_sky_params.sky_color.dawn_horizon;
407 video::SColorf bgcolor_bright_night_f = m_sky_params.sky_color.night_horizon;
409 video::SColorf skycolor_bright_normal_f = m_sky_params.sky_color.day_sky;
410 video::SColorf skycolor_bright_dawn_f = m_sky_params.sky_color.dawn_sky;
411 video::SColorf skycolor_bright_night_f = m_sky_params.sky_color.night_sky;
413 video::SColorf cloudcolor_bright_normal_f = m_cloudcolor_day_f;
414 video::SColorf cloudcolor_bright_dawn_f = m_cloudcolor_dawn_f;
416 float cloud_color_change_fraction = 0.95;
418 if (std::fabs(time_brightness - m_brightness) < 0.2f) {
419 m_brightness = m_brightness * 0.95 + time_brightness * 0.05;
421 m_brightness = m_brightness * 0.80 + time_brightness * 0.20;
422 cloud_color_change_fraction = 0.0;
425 if (direct_brightness < m_brightness)
426 m_brightness = m_brightness * 0.95 + direct_brightness * 0.05;
428 m_brightness = m_brightness * 0.98 + direct_brightness * 0.02;
431 m_clouds_visible = true;
432 float color_change_fraction = 0.98f;
434 if (is_dawn) { // Dawn
435 m_bgcolor_bright_f = m_bgcolor_bright_f.getInterpolated(
436 bgcolor_bright_dawn_f, color_change_fraction);
437 m_skycolor_bright_f = m_skycolor_bright_f.getInterpolated(
438 skycolor_bright_dawn_f, color_change_fraction);
439 m_cloudcolor_bright_f = m_cloudcolor_bright_f.getInterpolated(
440 cloudcolor_bright_dawn_f, color_change_fraction);
442 if (time_brightness < 0.13f) { // Night
443 m_bgcolor_bright_f = m_bgcolor_bright_f.getInterpolated(
444 bgcolor_bright_night_f, color_change_fraction);
445 m_skycolor_bright_f = m_skycolor_bright_f.getInterpolated(
446 skycolor_bright_night_f, color_change_fraction);
448 m_bgcolor_bright_f = m_bgcolor_bright_f.getInterpolated(
449 bgcolor_bright_normal_f, color_change_fraction);
450 m_skycolor_bright_f = m_skycolor_bright_f.getInterpolated(
451 skycolor_bright_normal_f, color_change_fraction);
454 m_cloudcolor_bright_f = m_cloudcolor_bright_f.getInterpolated(
455 cloudcolor_bright_normal_f, color_change_fraction);
458 m_bgcolor_bright_f = m_bgcolor_bright_f.getInterpolated(
459 bgcolor_bright_indoor_f, color_change_fraction);
460 m_skycolor_bright_f = m_skycolor_bright_f.getInterpolated(
461 bgcolor_bright_indoor_f, color_change_fraction);
462 m_cloudcolor_bright_f = m_cloudcolor_bright_f.getInterpolated(
463 cloudcolor_bright_normal_f, color_change_fraction);
464 m_clouds_visible = false;
467 video::SColor bgcolor_bright = m_bgcolor_bright_f.toSColor();
468 m_bgcolor = video::SColor(
470 bgcolor_bright.getRed() * m_brightness,
471 bgcolor_bright.getGreen() * m_brightness,
472 bgcolor_bright.getBlue() * m_brightness
475 video::SColor skycolor_bright = m_skycolor_bright_f.toSColor();
476 m_skycolor = video::SColor(
478 skycolor_bright.getRed() * m_brightness,
479 skycolor_bright.getGreen() * m_brightness,
480 skycolor_bright.getBlue() * m_brightness
483 // Horizon coloring based on sun and moon direction during sunset and sunrise
484 video::SColor pointcolor = video::SColor(m_bgcolor.getAlpha(), 255, 255, 255);
485 if (m_directional_colored_fog) {
486 if (m_horizon_blend() != 0) {
487 // Calculate hemisphere value from yaw, (inverted in third person front view)
489 if (cam_mode > CAMERA_MODE_THIRD)
491 f32 pointcolor_blend = wrapDegrees_0_360(yaw * dir_factor + 90);
492 if (pointcolor_blend > 180)
493 pointcolor_blend = 360 - pointcolor_blend;
494 pointcolor_blend /= 180;
495 // Bound view angle to determine where transition starts and ends
496 pointcolor_blend = rangelim(1 - pointcolor_blend * 1.375, 0, 1 / 1.375) *
498 // Combine the colors when looking up or down, otherwise turning looks weird
499 pointcolor_blend += (0.5 - pointcolor_blend) *
500 (1 - MYMIN((90 - std::fabs(pitch)) / 90 * 1.5, 1));
501 // Invert direction to match where the sun and moon are rising
502 if (m_time_of_day > 0.5)
503 pointcolor_blend = 1 - pointcolor_blend;
504 // Horizon colors of sun and moon
505 f32 pointcolor_light = rangelim(m_time_brightness * 3, 0.2, 1);
507 video::SColorf pointcolor_sun_f(1, 1, 1, 1);
508 // Use tonemap only if default sun/moon tinting is used
509 // which keeps previous behaviour.
510 if (m_sun_tonemap && m_default_tint) {
511 pointcolor_sun_f.r = pointcolor_light *
512 (float)m_materials[3].EmissiveColor.getRed() / 255;
513 pointcolor_sun_f.b = pointcolor_light *
514 (float)m_materials[3].EmissiveColor.getBlue() / 255;
515 pointcolor_sun_f.g = pointcolor_light *
516 (float)m_materials[3].EmissiveColor.getGreen() / 255;
517 } else if (!m_default_tint) {
518 pointcolor_sun_f = m_sky_params.fog_sun_tint;
520 pointcolor_sun_f.r = pointcolor_light * 1;
521 pointcolor_sun_f.b = pointcolor_light *
522 (0.25 + (rangelim(m_time_brightness, 0.25, 0.75) - 0.25) * 2 * 0.75);
523 pointcolor_sun_f.g = pointcolor_light * (pointcolor_sun_f.b * 0.375 +
524 (rangelim(m_time_brightness, 0.05, 0.15) - 0.05) * 10 * 0.625);
527 video::SColorf pointcolor_moon_f;
528 if (m_default_tint) {
529 pointcolor_moon_f = video::SColorf(
530 0.5 * pointcolor_light,
531 0.6 * pointcolor_light,
532 0.8 * pointcolor_light,
536 pointcolor_moon_f = video::SColorf(
537 (m_sky_params.fog_moon_tint.getRed() / 255) * pointcolor_light,
538 (m_sky_params.fog_moon_tint.getGreen() / 255) * pointcolor_light,
539 (m_sky_params.fog_moon_tint.getBlue() / 255) * pointcolor_light,
543 if (m_moon_tonemap && m_default_tint) {
544 pointcolor_moon_f.r = pointcolor_light *
545 (float)m_materials[4].EmissiveColor.getRed() / 255;
546 pointcolor_moon_f.b = pointcolor_light *
547 (float)m_materials[4].EmissiveColor.getBlue() / 255;
548 pointcolor_moon_f.g = pointcolor_light *
549 (float)m_materials[4].EmissiveColor.getGreen() / 255;
552 video::SColor pointcolor_sun = pointcolor_sun_f.toSColor();
553 video::SColor pointcolor_moon = pointcolor_moon_f.toSColor();
554 // Calculate the blend color
555 pointcolor = m_mix_scolor(pointcolor_moon, pointcolor_sun, pointcolor_blend);
557 m_bgcolor = m_mix_scolor(m_bgcolor, pointcolor, m_horizon_blend() * 0.5);
558 m_skycolor = m_mix_scolor(m_skycolor, pointcolor, m_horizon_blend() * 0.25);
561 float cloud_direct_brightness = 0.0f;
563 if (!m_directional_colored_fog) {
564 cloud_direct_brightness = time_brightness;
565 // Boost cloud brightness relative to sky, at dawn, dusk and at night
566 if (time_brightness < 0.7f)
567 cloud_direct_brightness *= 1.3f;
569 cloud_direct_brightness = std::fmin(m_horizon_blend() * 0.15f +
570 m_time_brightness, 1.0f);
571 // Set the same minimum cloud brightness at night
572 if (time_brightness < 0.5f)
573 cloud_direct_brightness = std::fmax(cloud_direct_brightness,
574 time_brightness * 1.3f);
577 cloud_direct_brightness = direct_brightness;
580 m_cloud_brightness = m_cloud_brightness * cloud_color_change_fraction +
581 cloud_direct_brightness * (1.0 - cloud_color_change_fraction);
582 m_cloudcolor_f = video::SColorf(
583 m_cloudcolor_bright_f.r * m_cloud_brightness,
584 m_cloudcolor_bright_f.g * m_cloud_brightness,
585 m_cloudcolor_bright_f.b * m_cloud_brightness,
588 if (m_directional_colored_fog) {
589 m_cloudcolor_f = m_mix_scolorf(m_cloudcolor_f,
590 video::SColorf(pointcolor), m_horizon_blend() * 0.25);
594 void Sky::draw_sun(video::IVideoDriver *driver, float sunsize, const video::SColor &suncolor,
595 const video::SColor &suncolor2, float wicked_time_of_day)
596 /* Draw sun in the sky.
597 * driver: Video driver object used to draw
598 * sunsize: the default size of the sun
599 * suncolor: main sun color
600 * suncolor2: second sun color
601 * wicked_time_of_day: current time of day, to know where should be the sun in the sky
604 static const u16 indices[] = {0, 1, 2, 0, 2, 3};
605 std::array<video::S3DVertex, 4> vertices;
606 if (!m_sun_texture) {
607 driver->setMaterial(m_materials[1]);
608 const float sunsizes[4] = {
609 (sunsize * 1.7f) * m_sun_params.scale,
610 (sunsize * 1.2f) * m_sun_params.scale,
611 (sunsize) * m_sun_params.scale,
612 (sunsize * 0.7f) * m_sun_params.scale
614 video::SColor c1 = suncolor;
615 video::SColor c2 = suncolor;
616 c1.setAlpha(0.05 * 255);
617 c2.setAlpha(0.15 * 255);
618 const video::SColor colors[4] = {c1, c2, suncolor, suncolor2};
619 for (int i = 0; i < 4; i++) {
620 draw_sky_body(vertices, -sunsizes[i], sunsizes[i], colors[i]);
621 place_sky_body(vertices, 90, wicked_time_of_day * 360 - 90);
622 driver->drawIndexedTriangleList(&vertices[0], 4, indices, 2);
625 driver->setMaterial(m_materials[3]);
626 float d = (sunsize * 1.7) * m_sun_params.scale;
629 c = video::SColor(0, 0, 0, 0);
631 c = video::SColor(255, 255, 255, 255);
632 draw_sky_body(vertices, -d, d, c);
633 place_sky_body(vertices, 90, wicked_time_of_day * 360 - 90);
634 driver->drawIndexedTriangleList(&vertices[0], 4, indices, 2);
639 void Sky::draw_moon(video::IVideoDriver *driver, float moonsize, const video::SColor &mooncolor,
640 const video::SColor &mooncolor2, float wicked_time_of_day)
642 * Draw moon in the sky.
643 * driver: Video driver object used to draw
644 * moonsize: the default size of the moon
645 * mooncolor: main moon color
646 * mooncolor2: second moon color
647 * wicked_time_of_day: current time of day, to know where should be the moon in
651 static const u16 indices[] = {0, 1, 2, 0, 2, 3};
652 std::array<video::S3DVertex, 4> vertices;
653 if (!m_moon_texture) {
654 driver->setMaterial(m_materials[1]);
655 const float moonsizes_1[4] = {
656 (-moonsize * 1.9f) * m_moon_params.scale,
657 (-moonsize * 1.3f) * m_moon_params.scale,
658 (-moonsize) * m_moon_params.scale,
659 (-moonsize) * m_moon_params.scale
661 const float moonsizes_2[4] = {
662 (moonsize * 1.9f) * m_moon_params.scale,
663 (moonsize * 1.3f) * m_moon_params.scale,
664 (moonsize) *m_moon_params.scale,
665 (moonsize * 0.6f) * m_moon_params.scale
667 video::SColor c1 = mooncolor;
668 video::SColor c2 = mooncolor;
669 c1.setAlpha(0.05 * 255);
670 c2.setAlpha(0.15 * 255);
671 const video::SColor colors[4] = {c1, c2, mooncolor, mooncolor2};
672 for (int i = 0; i < 4; i++) {
673 draw_sky_body(vertices, moonsizes_1[i], moonsizes_2[i], colors[i]);
674 place_sky_body(vertices, -90, wicked_time_of_day * 360 - 90);
675 driver->drawIndexedTriangleList(&vertices[0], 4, indices, 2);
678 driver->setMaterial(m_materials[4]);
679 float d = (moonsize * 1.9) * m_moon_params.scale;
682 c = video::SColor(0, 0, 0, 0);
684 c = video::SColor(255, 255, 255, 255);
685 draw_sky_body(vertices, -d, d, c);
686 place_sky_body(vertices, -90, wicked_time_of_day * 360 - 90);
687 driver->drawIndexedTriangleList(&vertices[0], 4, indices, 2);
691 void Sky::draw_stars(video::IVideoDriver * driver, float wicked_time_of_day)
693 // Tune values so that stars first appear just after the sun
694 // disappears over the horizon, and disappear just before the sun
695 // appears over the horizon.
696 // Also tune so that stars are at full brightness from time 20000
699 float tod = wicked_time_of_day < 0.5f ? wicked_time_of_day : (1.0f - wicked_time_of_day);
700 float starbrightness = (0.25f - fabsf(tod)) * 20.0f;
701 m_star_color = m_star_params.starcolor;
702 m_star_color.a *= clamp(starbrightness, 0.0f, 1.0f);
703 if (m_star_color.a <= 0.0f) // Stars are only drawn when not fully transparent
705 m_materials[0].DiffuseColor = m_materials[0].EmissiveColor = m_star_color.toSColor();
706 auto sky_rotation = core::matrix4().setRotationAxisRadians(2.0f * M_PI * (wicked_time_of_day - 0.25f), v3f(0.0f, 0.0f, 1.0f));
707 auto world_matrix = driver->getTransform(video::ETS_WORLD);
708 driver->setTransform(video::ETS_WORLD, world_matrix * sky_rotation);
709 driver->setMaterial(m_materials[0]);
710 driver->drawMeshBuffer(m_stars.get());
711 driver->setTransform(video::ETS_WORLD, world_matrix);
714 void Sky::draw_sky_body(std::array<video::S3DVertex, 4> &vertices, float pos_1, float pos_2, const video::SColor &c)
717 * Create an array of vertices with the dimensions specified.
718 * pos_1, pos_2: position of the body's vertices
719 * c: color of the body
724 vertices[0] = video::S3DVertex(pos_1, pos_1, -1, 0, 0, 1, c, t, t);
725 vertices[1] = video::S3DVertex(pos_2, pos_1, -1, 0, 0, 1, c, o, t);
726 vertices[2] = video::S3DVertex(pos_2, pos_2, -1, 0, 0, 1, c, o, o);
727 vertices[3] = video::S3DVertex(pos_1, pos_2, -1, 0, 0, 1, c, t, o);
731 void Sky::place_sky_body(
732 std::array<video::S3DVertex, 4> &vertices, float horizon_position, float day_position)
734 * Place body in the sky.
735 * vertices: The body as a rectangle of 4 vertices
736 * horizon_position: turn the body around the Y axis
737 * day_position: turn the body around the Z axis, to place it depending of the time of the day
740 for (video::S3DVertex &vertex : vertices) {
741 // Body is directed to -Z (south) by default
742 vertex.Pos.rotateXZBy(horizon_position);
743 vertex.Pos.rotateXYBy(day_position);
747 void Sky::setSunTexture(std::string sun_texture,
748 std::string sun_tonemap, ITextureSource *tsrc)
750 // Ignore matching textures (with modifiers) entirely,
751 // but lets at least update the tonemap before hand.
752 m_sun_params.tonemap = sun_tonemap;
753 m_sun_tonemap = tsrc->isKnownSourceImage(m_sun_params.tonemap) ?
754 tsrc->getTexture(m_sun_params.tonemap) : NULL;
755 m_materials[3].Lighting = !!m_sun_tonemap;
757 if (m_sun_params.texture == sun_texture)
759 m_sun_params.texture = sun_texture;
761 if (sun_texture != "") {
762 // We want to ensure the texture exists first.
763 m_sun_texture = tsrc->getTextureForMesh(m_sun_params.texture);
766 m_materials[3] = baseMaterial();
767 m_materials[3].setTexture(0, m_sun_texture);
768 m_materials[3].MaterialType = video::
769 EMT_TRANSPARENT_ALPHA_CHANNEL;
770 // Disables texture filtering
771 m_materials[3].setFlag(
772 video::E_MATERIAL_FLAG::EMF_BILINEAR_FILTER, false);
773 m_materials[3].setFlag(
774 video::E_MATERIAL_FLAG::EMF_TRILINEAR_FILTER, false);
775 m_materials[3].setFlag(
776 video::E_MATERIAL_FLAG::EMF_ANISOTROPIC_FILTER, false);
779 m_sun_texture = nullptr;
783 void Sky::setSunriseTexture(std::string sunglow_texture,
784 ITextureSource* tsrc)
786 // Ignore matching textures (with modifiers) entirely.
787 if (m_sun_params.sunrise == sunglow_texture)
789 m_sun_params.sunrise = sunglow_texture;
790 m_materials[2].setTexture(0, tsrc->getTextureForMesh(
791 sunglow_texture.empty() ? "sunrisebg.png" : sunglow_texture)
795 void Sky::setMoonTexture(std::string moon_texture,
796 std::string moon_tonemap, ITextureSource *tsrc)
798 // Ignore matching textures (with modifiers) entirely,
799 // but lets at least update the tonemap before hand.
800 m_moon_params.tonemap = moon_tonemap;
801 m_moon_tonemap = tsrc->isKnownSourceImage(m_moon_params.tonemap) ?
802 tsrc->getTexture(m_moon_params.tonemap) : NULL;
803 m_materials[4].Lighting = !!m_moon_tonemap;
805 if (m_moon_params.texture == moon_texture)
807 m_moon_params.texture = moon_texture;
809 if (moon_texture != "") {
810 // We want to ensure the texture exists first.
811 m_moon_texture = tsrc->getTextureForMesh(m_moon_params.texture);
813 if (m_moon_texture) {
814 m_materials[4] = baseMaterial();
815 m_materials[4].setTexture(0, m_moon_texture);
816 m_materials[4].MaterialType = video::
817 EMT_TRANSPARENT_ALPHA_CHANNEL;
818 // Disables texture filtering
819 m_materials[4].setFlag(
820 video::E_MATERIAL_FLAG::EMF_BILINEAR_FILTER, false);
821 m_materials[4].setFlag(
822 video::E_MATERIAL_FLAG::EMF_TRILINEAR_FILTER, false);
823 m_materials[4].setFlag(
824 video::E_MATERIAL_FLAG::EMF_ANISOTROPIC_FILTER, false);
827 m_moon_texture = nullptr;
831 void Sky::setStarCount(u16 star_count, bool force_update)
833 // Allow force updating star count at game init.
834 if (m_star_params.count != star_count || force_update) {
835 m_star_params.count = star_count;
836 m_seed = (u64)myrand() << 32 | myrand();
841 void Sky::updateStars()
843 m_stars.reset(new scene::SMeshBuffer());
844 // Stupid IrrLicht doesn’t allow non-indexed rendering, and indexed quad
845 // rendering is slow due to lack of hardware support. So as indices are
846 // 16-bit and there are 4 vertices per star... the limit is 2^16/4 = 0x4000.
847 // That should be well enough actually.
848 if (m_star_params.count > 0x4000) {
849 warningstream << "Requested " << m_star_params.count << " stars but " << 0x4000 << " is the max\n";
850 m_star_params.count = 0x4000;
852 m_stars->Vertices.reallocate(4 * m_star_params.count);
853 m_stars->Indices.reallocate(6 * m_star_params.count);
855 video::SColor fallback_color = m_star_params.starcolor; // used on GLES 2 “without shaders”
856 PcgRandom rgen(m_seed);
857 float d = (0.006 / 2) * m_star_params.scale;
858 for (u16 i = 0; i < m_star_params.count; i++) {
860 rgen.range(-10000, 10000),
861 rgen.range(-10000, 10000),
862 rgen.range(-10000, 10000)
864 core::CMatrix4<f32> a;
865 a.buildRotateFromTo(v3f(0, 1, 0), r);
866 v3f p = v3f(-d, 1, -d);
867 v3f p1 = v3f(d, 1, -d);
868 v3f p2 = v3f(d, 1, d);
869 v3f p3 = v3f(-d, 1, d);
874 m_stars->Vertices.push_back(video::S3DVertex(p, {}, fallback_color, {}));
875 m_stars->Vertices.push_back(video::S3DVertex(p1, {}, fallback_color, {}));
876 m_stars->Vertices.push_back(video::S3DVertex(p2, {}, fallback_color, {}));
877 m_stars->Vertices.push_back(video::S3DVertex(p3, {}, fallback_color, {}));
879 for (u16 i = 0; i < m_star_params.count; i++) {
880 m_stars->Indices.push_back(i * 4 + 0);
881 m_stars->Indices.push_back(i * 4 + 1);
882 m_stars->Indices.push_back(i * 4 + 2);
883 m_stars->Indices.push_back(i * 4 + 2);
884 m_stars->Indices.push_back(i * 4 + 3);
885 m_stars->Indices.push_back(i * 4 + 0);
887 m_stars->setHardwareMappingHint(scene::EHM_STATIC);
890 void Sky::setSkyColors(const SkyColor &sky_color)
892 m_sky_params.sky_color = sky_color;
895 void Sky::setHorizonTint(video::SColor sun_tint, video::SColor moon_tint,
896 std::string use_sun_tint)
898 // Change sun and moon tinting:
899 m_sky_params.fog_sun_tint = sun_tint;
900 m_sky_params.fog_moon_tint = moon_tint;
901 // Faster than comparing strings every rendering frame
902 if (use_sun_tint == "default")
903 m_default_tint = true;
904 else if (use_sun_tint == "custom")
905 m_default_tint = false;
907 m_default_tint = true;
910 void Sky::addTextureToSkybox(std::string texture, int material_id,
911 ITextureSource *tsrc)
913 // Sanity check for more than six textures.
914 if (material_id + 5 >= SKY_MATERIAL_COUNT)
916 // Keep a list of texture names handy.
917 m_sky_params.textures.emplace_back(texture);
918 video::ITexture *result = tsrc->getTextureForMesh(texture);
919 m_materials[material_id+5] = baseMaterial();
920 m_materials[material_id+5].setTexture(0, result);
921 m_materials[material_id+5].MaterialType = video::EMT_SOLID;
924 // To be called once at game init to setup default values.
925 void Sky::setSkyDefaults()
927 SkyboxDefaults sky_defaults;
928 m_sky_params.sky_color = sky_defaults.getSkyColorDefaults();
929 m_sun_params = sky_defaults.getSunDefaults();
930 m_moon_params = sky_defaults.getMoonDefaults();
931 m_star_params = sky_defaults.getStarDefaults();