Skip to content

Commit

Permalink
Support ambient light only for the shaders
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrey2470T committed Mar 8, 2024
1 parent 399c8c7 commit 1aea0c9
Show file tree
Hide file tree
Showing 9 changed files with 25 additions and 52 deletions.
1 change: 1 addition & 0 deletions doc/lua_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -8266,6 +8266,7 @@ child will follow movement and rotation of that bone.
* `light_definition` is a table with the following optional fields:
* `ambient_light` is a ColorSpec controlling lightness & color of ambient light; alpha must be 255
(global lighting; default: `{a = 255, r = 0, g = 0, b = 0}` / last set value).
* It works only if 'enable_shaders' setting is set to true.
* `saturation` sets the saturation (vividness; default: `1.0`).
* values > 1 increase the saturation
* values in [0,1] decrease the saturation
Expand Down
13 changes: 9 additions & 4 deletions src/client/clientenvironment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -251,10 +251,10 @@ void ClientEnvironment::step(float dtime)
// Update the ambient light
auto new_ambient_light_clr = getLocalPlayer()->getLighting().ambient_light;

if (new_ambient_light_clr != m_ambient_light) {
getClientMap().forceUpdateLightColor();
bool enable_shaders = g_settings->getBool("enable_shaders");

if (enable_shaders && (new_ambient_light_clr != m_ambient_light))
m_ambient_light = new_ambient_light_clr;
}

// Update lighting on local player (used for wield item)
u32 day_night_ratio = getDayNightRatio();
Expand All @@ -270,7 +270,12 @@ void ClientEnvironment::step(float dtime)

u16 light = getInteriorLight(node_at_lplayer, 0, m_client->ndef());
lplayer->light_color = encode_light(light, 0); // this transfers light.alpha
final_color_blend(&lplayer->light_color, light, day_night_ratio, m_ambient_light);

video::SColor ambient_light(255, 0, 0, 0);

if (enable_shaders)
ambient_light = m_ambient_light;
final_color_blend(&lplayer->light_color, light, day_night_ratio, ambient_light);
}

/*
Expand Down
8 changes: 2 additions & 6 deletions src/client/clientmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -719,7 +719,6 @@ void ClientMap::renderMap(video::IVideoDriver* driver, s32 pass)
const float animation_time = m_client->getAnimationTime();
const int crack = m_client->getCrackLevel();
const u32 daynight_ratio = m_client->getEnv().getDayNightRatio();
const auto ambient_light = m_client->getEnv().getAmbientLight();

const v3f camera_position = m_camera_position;

Expand Down Expand Up @@ -778,11 +777,10 @@ void ClientMap::renderMap(video::IVideoDriver* driver, s32 pass)
// Pretty random but this should work somewhat nicely
bool faraway = d >= BS * 50;
if (block_mesh->isAnimationForced() || !faraway ||
mesh_animate_count < (m_control.range_all ? 200 : 50) ||
m_force_update_light_color) {
mesh_animate_count < (m_control.range_all ? 200 : 50)) {

bool animated = block_mesh->animate(faraway, animation_time,
crack, daynight_ratio, ambient_light);
crack, daynight_ratio);
if (animated)
mesh_animate_count++;
} else {
Expand Down Expand Up @@ -826,8 +824,6 @@ void ClientMap::renderMap(video::IVideoDriver* driver, s32 pass)
}
}

m_force_update_light_color = false;

// Capture draw order for all solid meshes
for (auto &map : grouped_buffers.maps) {
for (auto &list : map) {
Expand Down
4 changes: 0 additions & 4 deletions src/client/clientmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,6 @@ class ClientMap : public Map, public scene::ISceneNode

void onSettingChanged(const std::string &name);

void forceUpdateLightColor() { m_force_update_light_color = true; }

protected:
void reportMetrics(u64 save_time_us, u32 saved_blocks, u32 all_blocks) override;
private:
Expand Down Expand Up @@ -201,6 +199,4 @@ class ClientMap : public Map, public scene::ISceneNode

bool m_loops_occlusion_culler;
bool m_enable_raytraced_culling;

bool m_force_update_light_color = false;
};
3 changes: 1 addition & 2 deletions src/client/content_cao.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -909,8 +909,7 @@ void GenericCAO::updateLight(u32 day_night_ratio)
if (m_enable_shaders)
light = encode_light(light_at_pos, m_prop.glow);
else
final_color_blend(&light, light_at_pos, day_night_ratio,
m_client->getEnv().getAmbientLight());
final_color_blend(&light, light_at_pos, day_night_ratio);

if (light != m_last_light) {
m_last_light = light;
Expand Down
11 changes: 2 additions & 9 deletions src/client/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ class GameGlobalShaderConstantSetter : public IShaderConstantSetter
CachedPixelShaderSetting<float>
m_animation_timer_delta_pixel{"animationTimerDelta"};
CachedPixelShaderSetting<float, 3> m_day_light{"dayLight"};
CachedPixelShaderSetting<float, 1> m_ambient_light{"ambientLight"};
CachedPixelShaderSetting<float, 3> m_ambient_light{"ambientLight"};
CachedPixelShaderSetting<float, 3> m_eye_position_pixel{"eyePosition"};
CachedVertexShaderSetting<float, 3> m_eye_position_vertex{"eyePosition"};
CachedPixelShaderSetting<float, 3> m_minimap_yaw{"yawVec"};
Expand Down Expand Up @@ -473,14 +473,7 @@ class GameGlobalShaderConstantSetter : public IShaderConstantSetter
get_sunlight_color(&sunlight, daynight_ratio);
m_day_light.set(sunlight, services);

auto ambient_light = m_client->getEnv().getAmbientLight();

float ambient_light_f[3] = {
ambient_light.getRed() / 255.f,
ambient_light.getGreen() / 255.f,
ambient_light.getBlue() / 255.f
};

video::SColorf ambient_light_f(m_client->getEnv().getAmbientLight());
m_ambient_light.set(ambient_light_f, services);

u32 animation_timer = m_client->getEnv().getFrameTime() % 1000000;
Expand Down
29 changes: 6 additions & 23 deletions src/client/mapblock_mesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -751,15 +751,13 @@ MapBlockMesh::MapBlockMesh(Client *client, MeshMakeData *data, v3s16 camera_offs
video::SColorf sunlight;
get_sunlight_color(&sunlight, 0);

auto ambientlight = client->getEnv().getAmbientLight();

std::map<u32, video::SColor> colors;
const u32 vertex_count = p.vertices.size();
for (u32 j = 0; j < vertex_count; j++) {
video::SColor *vc = &p.vertices[j].Color;
video::SColor copy = *vc;
if (vc->getAlpha() == 0) // No sunlight - no need to animate
final_color_blend(vc, copy, sunlight, ambientlight); // Finalize color
final_color_blend(vc, copy, sunlight); // Finalize color
else // Record color to animate
colors[j] = copy;

Expand Down Expand Up @@ -852,8 +850,7 @@ MapBlockMesh::~MapBlockMesh()
delete block;
}

bool MapBlockMesh::animate(bool faraway, float time, int crack,
u32 daynight_ratio, video::SColor ambient_light)
bool MapBlockMesh::animate(bool faraway, float time, int crack, u32 daynight_ratio)
{
if (!m_has_animation) {
m_animation_force_timer = 100000;
Expand Down Expand Up @@ -912,37 +909,23 @@ bool MapBlockMesh::animate(bool faraway, float time, int crack,
}
}

video::SColorf day_color;
get_sunlight_color(&day_color, daynight_ratio);

// Day-night transition
if (!m_enable_shaders && (daynight_ratio != m_last_daynight_ratio)) {
video::SColorf day_color;
get_sunlight_color(&day_color, daynight_ratio);

for (auto &daynight_diff : m_daynight_diffs) {
auto *mesh = m_mesh[daynight_diff.first.first];
mesh->setDirty(scene::EBT_VERTEX); // force reload to VBO
scene::IMeshBuffer *buf = mesh->
getMeshBuffer(daynight_diff.first.second);
video::S3DVertex *vertices = (video::S3DVertex *)buf->getVertices();
for (const auto &j : daynight_diff.second)
final_color_blend(&(vertices[j.first].Color), j.second,
day_color, video::SColor(255, 0, 0, 0));
final_color_blend(&(vertices[j.first].Color), j.second, day_color);
}
m_last_daynight_ratio = daynight_ratio;
}

// Ambient light
if (!m_enable_shaders) {
for (u32 i = 0; i < MAX_TILE_LAYERS; i++)
for (u32 j = 0; j < m_mesh[i]->getMeshBufferCount(); j++) {
scene::IMeshBuffer *buf = m_mesh[i]->getMeshBuffer(j);
video::S3DVertex *vertices = (video::S3DVertex *)buf->getVertices();

for (u32 k = 0; k < buf->getVertexCount(); k++)
final_color_blend(&(vertices[k].Color), vertices[k].Color,
day_color, ambient_light);
}
}

return true;
}

Expand Down
6 changes: 3 additions & 3 deletions src/client/mapblock_mesh.h
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ class MapBlockMesh
// daynight_ratio: 0 .. 1000
// crack: -1 .. CRACK_ANIMATION_LENGTH-1 (-1 for off)
// Returns true if anything has been changed.
bool animate(bool faraway, float time, int crack, u32 daynight_ratio, video::SColor ambient_light);
bool animate(bool faraway, float time, int crack, u32 daynight_ratio);

scene::IMesh *getMesh()
{
Expand Down Expand Up @@ -318,7 +318,7 @@ void get_sunlight_color(video::SColorf *sunlight, u32 daynight_ratio);
* night light
*/
void final_color_blend(video::SColor *result,
u16 light, u32 daynight_ratio, video::SColor ambientLight);
u16 light, u32 daynight_ratio, video::SColor ambientLight=video::SColor(255,0,0,0));

/*!
* Gives the final SColor shown on screen.
Expand All @@ -329,7 +329,7 @@ void final_color_blend(video::SColor *result,
*/
void final_color_blend(video::SColor *result,
const video::SColor &data, const video::SColorf &dayLight,
const video::SColor &ambientLight);
const video::SColor &ambientLight=video::SColor(255,0,0,0));

// Retrieves the TileSpec of a face of a node
// Adds MATERIAL_FLAG_CRACK if the node is cracked
Expand Down
2 changes: 1 addition & 1 deletion src/lighting.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ struct Lighting
{
AutoExposure exposure;
/// @brief Ambient light color & intensity for nodes & entities. Alpha is ignored.
video::SColor ambient_light = {255, 0, 0, 0};
video::SColor ambient_light {255, 0, 0, 0};
float shadow_intensity {0.0f};
float saturation {1.0f};
float volumetric_light_strength {0.0f};
Expand Down

0 comments on commit 1aea0c9

Please sign in to comment.