+
+void Sky::setSunriseTexture(const std::string &sunglow_texture,
+ ITextureSource* tsrc)
+{
+ // Ignore matching textures (with modifiers) entirely.
+ if (m_sun_params.sunrise == sunglow_texture)
+ return;
+ m_sun_params.sunrise = sunglow_texture;
+ m_materials[2].setTexture(0, tsrc->getTextureForMesh(
+ sunglow_texture.empty() ? "sunrisebg.png" : sunglow_texture)
+ );
+}
+
+void Sky::setMoonTexture(const std::string &moon_texture,
+ const std::string &moon_tonemap, ITextureSource *tsrc)
+{
+ // Ignore matching textures (with modifiers) entirely,
+ // but lets at least update the tonemap before hand.
+ m_moon_params.tonemap = moon_tonemap;
+ m_moon_tonemap = tsrc->isKnownSourceImage(m_moon_params.tonemap) ?
+ tsrc->getTexture(m_moon_params.tonemap) : nullptr;
+ m_materials[4].Lighting = !!m_moon_tonemap;
+
+ if (m_moon_params.texture == moon_texture)
+ return;
+ m_moon_params.texture = moon_texture;
+
+ if (moon_texture != "") {
+ // We want to ensure the texture exists first.
+ m_moon_texture = tsrc->getTextureForMesh(m_moon_params.texture);
+
+ if (m_moon_texture) {
+ m_materials[4] = baseMaterial();
+ m_materials[4].setTexture(0, m_moon_texture);
+ m_materials[4].MaterialType = video::
+ EMT_TRANSPARENT_ALPHA_CHANNEL;
+ // Disables texture filtering
+ m_materials[4].setFlag(
+ video::E_MATERIAL_FLAG::EMF_BILINEAR_FILTER, false);
+ m_materials[4].setFlag(
+ video::E_MATERIAL_FLAG::EMF_TRILINEAR_FILTER, false);
+ m_materials[4].setFlag(
+ video::E_MATERIAL_FLAG::EMF_ANISOTROPIC_FILTER, false);
+ }
+ } else {
+ m_moon_texture = nullptr;
+ }
+}
+
+void Sky::setStarCount(u16 star_count, bool force_update)
+{
+ // Allow force updating star count at game init.
+ if (m_star_params.count != star_count || force_update) {
+ m_star_params.count = star_count;
+ updateStars();
+ }
+}
+
+void Sky::updateStars()
+{
+ m_stars.reset(new scene::SMeshBuffer());
+ // Stupid IrrLicht doesn’t allow non-indexed rendering, and indexed quad
+ // rendering is slow due to lack of hardware support. So as indices are
+ // 16-bit and there are 4 vertices per star... the limit is 2^16/4 = 0x4000.
+ // That should be well enough actually.
+ if (m_star_params.count > 0x4000) {
+ warningstream << "Requested " << m_star_params.count << " stars but " << 0x4000 << " is the max\n";
+ m_star_params.count = 0x4000;
+ }
+ m_stars->Vertices.reallocate(4 * m_star_params.count);
+ m_stars->Indices.reallocate(6 * m_star_params.count);
+
+ video::SColor fallback_color = m_star_params.starcolor; // used on GLES 2 “without shaders”
+ PcgRandom rgen(m_seed);
+ float d = (0.006 / 2) * m_star_params.scale;
+ for (u16 i = 0; i < m_star_params.count; i++) {
+ v3f r = v3f(
+ rgen.range(-10000, 10000),
+ rgen.range(-10000, 10000),
+ rgen.range(-10000, 10000)
+ );
+ core::CMatrix4<f32> a;
+ a.buildRotateFromTo(v3f(0, 1, 0), r);
+ v3f p = v3f(-d, 1, -d);
+ v3f p1 = v3f(d, 1, -d);
+ v3f p2 = v3f(d, 1, d);
+ v3f p3 = v3f(-d, 1, d);
+ a.rotateVect(p);
+ a.rotateVect(p1);
+ a.rotateVect(p2);
+ a.rotateVect(p3);
+ m_stars->Vertices.push_back(video::S3DVertex(p, {}, fallback_color, {}));
+ m_stars->Vertices.push_back(video::S3DVertex(p1, {}, fallback_color, {}));
+ m_stars->Vertices.push_back(video::S3DVertex(p2, {}, fallback_color, {}));
+ m_stars->Vertices.push_back(video::S3DVertex(p3, {}, fallback_color, {}));
+ }
+ for (u16 i = 0; i < m_star_params.count; i++) {
+ m_stars->Indices.push_back(i * 4 + 0);
+ m_stars->Indices.push_back(i * 4 + 1);
+ m_stars->Indices.push_back(i * 4 + 2);
+ m_stars->Indices.push_back(i * 4 + 2);
+ m_stars->Indices.push_back(i * 4 + 3);
+ m_stars->Indices.push_back(i * 4 + 0);
+ }
+ m_stars->setHardwareMappingHint(scene::EHM_STATIC);
+}
+
+void Sky::setSkyColors(const SkyColor &sky_color)
+{
+ m_sky_params.sky_color = sky_color;
+}
+
+void Sky::setHorizonTint(video::SColor sun_tint, video::SColor moon_tint,
+ const std::string &use_sun_tint)
+{
+ // Change sun and moon tinting:
+ m_sky_params.fog_sun_tint = sun_tint;
+ m_sky_params.fog_moon_tint = moon_tint;
+ // Faster than comparing strings every rendering frame
+ if (use_sun_tint == "default")
+ m_default_tint = true;
+ else if (use_sun_tint == "custom")
+ m_default_tint = false;
+ else
+ m_default_tint = true;
+}
+
+void Sky::addTextureToSkybox(const std::string &texture, int material_id,
+ ITextureSource *tsrc)
+{
+ // Sanity check for more than six textures.
+ if (material_id + 5 >= SKY_MATERIAL_COUNT)
+ return;
+ // Keep a list of texture names handy.
+ m_sky_params.textures.emplace_back(texture);
+ video::ITexture *result = tsrc->getTextureForMesh(texture);
+ m_materials[material_id+5] = baseMaterial();
+ m_materials[material_id+5].setTexture(0, result);
+ m_materials[material_id+5].MaterialType = video::EMT_SOLID;
+}
+
+// To be called once at game init to setup default values.
+void Sky::setSkyDefaults()
+{
+ SkyboxDefaults sky_defaults;
+ m_sky_params.sky_color = sky_defaults.getSkyColorDefaults();
+ m_sun_params = sky_defaults.getSunDefaults();
+ m_moon_params = sky_defaults.getMoonDefaults();
+ m_star_params = sky_defaults.getStarDefaults();
+}
+
+float getWickedTimeOfDay(float time_of_day)
+{
+ float nightlength = 0.415f;
+ float wn = nightlength / 2;
+ float wicked_time_of_day = 0;
+ if (time_of_day > wn && time_of_day < 1.0f - wn)
+ wicked_time_of_day = (time_of_day - wn) / (1.0f - wn * 2) * 0.5f + 0.25f;
+ else if (time_of_day < 0.5f)
+ wicked_time_of_day = time_of_day / wn * 0.25f;
+ else
+ wicked_time_of_day = 1.0f - ((1.0f - time_of_day) / wn * 0.25f);
+ return wicked_time_of_day;
+}