1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-06-27 16:36:03 +00:00

Fog effect when camera is inside cloud

Fixes issue #3576

* Clouds now take camera position as 3D, not 2D

* Cloud grid filling extracted to gridFilled method

* Clouds detect whether camera is inside cloud

* Camera in cloud changes fog by overriding sky colors
  with cloud color

* Sun, moon and stars can be temporarily disabled
  with setBodiesVisible

* Disabling fog also disables all "inside cloud" behaviors
This commit is contained in:
Ben Deutsch 2017-05-07 18:41:47 +02:00 committed by SmallJoker
parent 61a3de42fd
commit 6bedb6de40
7 changed files with 84 additions and 30 deletions

View file

@ -24,6 +24,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "debug.h"
#include "profiler.h"
#include "settings.h"
#include <cmath>
// Menu clouds are created later
@ -31,6 +32,9 @@ class Clouds;
Clouds *g_menuclouds = NULL;
irr::scene::ISceneManager *g_menucloudsmgr = NULL;
// Constant for now
static constexpr const float cloud_size = BS * 64.0f;
static void cloud_3d_setting_changed(const std::string &settingname, void *data)
{
// TODO: only re-read cloud settings, not height or radius
@ -85,8 +89,6 @@ void Clouds::OnRegisterSceneNode()
ISceneNode::OnRegisterSceneNode();
}
#define MYROUND(x) (x > 0.0 ? (int)x : (int)x - 1)
void Clouds::render()
{
@ -112,19 +114,19 @@ void Clouds::render()
Clouds move from Z+ towards Z-
*/
static const float cloud_size = BS * 64.0f;
const float cloud_full_radius = cloud_size * m_cloud_radius_i;
v2f camera_pos_2d(m_camera_pos.X, m_camera_pos.Z);
// Position of cloud noise origin from the camera
v2f cloud_origin_from_camera_f = m_origin - m_camera_pos;
v2f cloud_origin_from_camera_f = m_origin - camera_pos_2d;
// The center point of drawing in the noise
v2f center_of_drawing_in_noise_f = -cloud_origin_from_camera_f;
// The integer center point of drawing in the noise
v2s16 center_of_drawing_in_noise_i(
MYROUND(center_of_drawing_in_noise_f.X / cloud_size),
MYROUND(center_of_drawing_in_noise_f.Y / cloud_size)
std::floor(center_of_drawing_in_noise_f.X / cloud_size),
std::floor(center_of_drawing_in_noise_f.Y / cloud_size)
);
// The world position of the integer center point of drawing in the noise
v2f world_center_of_drawing_in_noise_f = v2f(
center_of_drawing_in_noise_i.X * cloud_size,
@ -172,7 +174,6 @@ void Clouds::render()
bool *grid = new bool[m_cloud_radius_i * 2 * m_cloud_radius_i * 2];
float cloud_size_noise = cloud_size / BS / 200;
for(s16 zi = -m_cloud_radius_i; zi < m_cloud_radius_i; zi++) {
u32 si = (zi + m_cloud_radius_i) * m_cloud_radius_i * 2 + m_cloud_radius_i;
@ -180,19 +181,10 @@ void Clouds::render()
for (s16 xi = -m_cloud_radius_i; xi < m_cloud_radius_i; xi++) {
u32 i = si + xi;
v2s16 p_in_noise_i(
grid[i] = gridFilled(
xi + center_of_drawing_in_noise_i.X,
zi + center_of_drawing_in_noise_i.Y
);
float noise = noise2d_perlin(
(float)p_in_noise_i.X * cloud_size_noise,
(float)p_in_noise_i.Y * cloud_size_noise,
m_seed, 3, 0.5);
// normalize to 0..1 (given 3 octaves)
static const float noise_bound = 1.0f + 0.5f + 0.25f;
float density = noise / noise_bound * 0.5f + 0.5f;
grid[i] = (density < m_params.density);
}
}
@ -350,7 +342,7 @@ void Clouds::step(float dtime)
m_origin = m_origin + dtime * BS * m_params.speed;
}
void Clouds::update(v2f camera_p, video::SColorf color_diffuse)
void Clouds::update(const v3f &camera_p, const video::SColorf &color_diffuse)
{
m_camera_pos = camera_p;
m_color.r = MYMIN(MYMAX(color_diffuse.r * m_params.color_bright.getRed(),
@ -360,6 +352,20 @@ void Clouds::update(v2f camera_p, video::SColorf color_diffuse)
m_color.b = MYMIN(MYMAX(color_diffuse.b * m_params.color_bright.getBlue(),
m_params.color_ambient.getBlue()), 255) / 255.0f;
m_color.a = m_params.color_bright.getAlpha() / 255.0f;
// is the camera inside the cloud mesh?
m_camera_inside_cloud = false; // default
if (m_enable_3d) {
float camera_height = camera_p.Y;
if (camera_height >= m_box.MinEdge.Y &&
camera_height <= m_box.MaxEdge.Y) {
v2f camera_in_noise;
camera_in_noise.X = floor((camera_p.X - m_origin.X) / cloud_size + 0.5);
camera_in_noise.Y = floor((camera_p.Z - m_origin.Y) / cloud_size + 0.5);
bool filled = gridFilled(camera_in_noise.X, camera_in_noise.Y);
m_camera_inside_cloud = filled;
}
}
}
void Clouds::readSettings()
@ -369,3 +375,16 @@ void Clouds::readSettings()
m_cloud_radius_i = g_settings->getU16("cloud_radius");
m_enable_3d = g_settings->getBool("enable_3d_clouds");
}
bool Clouds::gridFilled(int x, int y) const
{
float cloud_size_noise = cloud_size / (BS * 200.f);
float noise = noise2d_perlin(
(float)x * cloud_size_noise,
(float)y * cloud_size_noise,
m_seed, 3, 0.5);
// normalize to 0..1 (given 3 octaves)
static constexpr const float noise_bound = 1.0f + 0.5f + 0.25f;
float density = noise / noise_bound * 0.5f + 0.5f;
return (density < m_params.density);
}