Skip to content

Commit

Permalink
Add ambient light to light color, calculate the color in final_color_…
Browse files Browse the repository at this point in the history
…blend() if shaders are off
  • Loading branch information
Andrey2470T committed Feb 24, 2024
1 parent 908d1dd commit 6daa777
Show file tree
Hide file tree
Showing 12 changed files with 98 additions and 79 deletions.
7 changes: 2 additions & 5 deletions client/shaders/nodes_shader/opengl_fragment.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ uniform vec3 eyePosition;
uniform vec3 cameraOffset;
uniform float animationTimer;

uniform float ambientLight;
uniform vec3 ambientColor;
uniform vec3 ambientLight;
#ifdef ENABLE_DYNAMIC_SHADOWS
// shadow texture
uniform sampler2D ShadowMapSampler;
Expand Down Expand Up @@ -382,10 +381,8 @@ void main(void)
#endif

color = base.rgb;
vec4 col = vec4(color.rgb * varColor.rgb, 1.0);

if (!(ambientColor.r == ambientColor.g && ambientColor.r == ambientColor.b))
col.rgb += ambientLight * ambientColor.rgb;
vec4 col = vec4(color.rgb * (varColor.rgb + ambientLight), 1.0);

#ifdef ENABLE_DYNAMIC_SHADOWS
if (f_shadow_strength > 0.0) {
Expand Down
8 changes: 3 additions & 5 deletions client/shaders/object_shader/opengl_fragment.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ uniform vec3 eyePosition;
uniform vec3 cameraOffset;
uniform float animationTimer;

uniform float ambientLight;
uniform vec3 ambientColor;
uniform vec3 ambientLight;
#ifdef ENABLE_DYNAMIC_SHADOWS
// shadow texture
uniform sampler2D ShadowMapSampler;
Expand Down Expand Up @@ -382,9 +381,8 @@ void main(void)
#endif

color = base.rgb;
vec4 col = vec4(color.rgb * varColor.rgb, 1.0);
if (!(ambientColor.r == ambientColor.g && ambientColor.r == ambientColor.b))
col.rgb += ambientLight * ambientColor.rgb;

vec4 col = vec4(color.rgb * (varColor.rgb + ambientLight), 1.0);
col.rgb *= vIDiff;

#ifdef ENABLE_DYNAMIC_SHADOWS
Expand Down
2 changes: 1 addition & 1 deletion doc/lua_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -8186,7 +8186,7 @@ child will follow movement and rotation of that bone.
* `light_definition` is a table with the following optional fields:
* `ambient_light` is a table controlling amount and color of ambient light (global lighting)
* `luminance` sets the amount of ambient light in range (0... LIGHT_SUN) like `light_source` has (default: `0`)
* `color` sets the color of ambient light (ColorSpec) (default: `{a = 0, r = 0, g = 0, b = 0}`)
* `color` sets the color of ambient light (ColorSpec) (default: `{a = 255, r = 255, g = 255, b = 255}`)
* `saturation` sets the saturation (vividness; default: `1.0`).
* values > 1 increase the saturation
* values in [0,1] decrease the saturation
Expand Down
15 changes: 12 additions & 3 deletions src/client/clientenvironment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,16 @@ void ClientEnvironment::step(float dtime)
if (m_client->modsLoaded())
m_script->environment_step(dtime);

// Update the ambient light
Lighting &lighting = getLocalPlayer()->getLighting();

auto new_ambient_light = encodeAmbientLight(lighting.ambient_light.luminance, lighting.ambient_light.color);

if (new_ambient_light != m_ambient_light) {
m_ambient_light = new_ambient_light;
getClientMap().forceUpdateMapblocksMeshes();
}

// Update lighting on local player (used for wield item)
u32 day_night_ratio = getDayNightRatio();
{
Expand All @@ -310,9 +320,8 @@ void ClientEnvironment::step(float dtime)
node_at_lplayer = m_map->getNode(p);

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

/*
Expand Down
3 changes: 3 additions & 0 deletions src/client/clientenvironment.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ class ClientEnvironment : public Environment
u64 getFrameTime() const { return m_frame_time; }
u64 getFrameTimeDelta() const { return m_frame_dtime; }

const video::SColor& getAmbientLight() const { return m_ambient_light; }

private:
ClientMap *m_map;
LocalPlayer *m_local_player = nullptr;
Expand All @@ -161,4 +163,5 @@ class ClientEnvironment : public Environment
u64 m_frame_time = 0;
u64 m_frame_dtime = 0;
u64 m_frame_time_pause_accumulator = 0;
video::SColor m_ambient_light {0, 0, 0, 0};
};
3 changes: 2 additions & 1 deletion src/client/clientmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -727,6 +727,7 @@ 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 video::SColor &ambient_light = m_client->getEnv().getAmbientLight();

const v3f camera_position = m_camera_position;

Expand Down Expand Up @@ -788,7 +789,7 @@ void ClientMap::renderMap(video::IVideoDriver* driver, s32 pass)
mesh_animate_count < (m_control.range_all ? 200 : 50)) {

bool animated = block_mesh->animate(faraway, animation_time,
crack, daynight_ratio);
crack, daynight_ratio, ambient_light);
if (animated)
mesh_animate_count++;
} else {
Expand Down
6 changes: 3 additions & 3 deletions src/client/content_cao.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -896,10 +896,10 @@ void GenericCAO::updateLight(u32 day_night_ratio)
// Encode light into color, adding a small boost
// based on the entity glow.
if (m_enable_shaders)
light = encode_light(light_at_pos, m_prop.glow,
m_env->getLocalPlayer()->getLighting().ambient_light.luminance);
light = encode_light(light_at_pos, m_prop.glow);
else
final_color_blend(&light, light_at_pos, day_night_ratio);
final_color_blend(&light, light_at_pos, day_night_ratio,
m_client->getEnv().getAmbientLight());

if (light != m_last_light) {
m_last_light = light;
Expand Down
30 changes: 10 additions & 20 deletions src/client/content_mapblock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,7 @@ void MapblockMeshGenerator::useTile(int index, u8 set_flags, u8 reset_flags, boo
else
getTile(index, &cur_node.tile);
if (!data->m_smooth_lighting)
cur_node.color = encode_light(cur_node.light, cur_node.f->light_source,
player->getLighting().ambient_light.luminance);
cur_node.color = encode_light(cur_node.light, cur_node.f->light_source);

for (auto &layer : cur_node.tile.layers) {
layer.material_flags |= set_flags;
Expand Down Expand Up @@ -303,17 +302,15 @@ LightInfo MapblockMeshGenerator::blendLight(const v3f &vertex_pos)
video::SColor MapblockMeshGenerator::blendLightColor(const v3f &vertex_pos)
{
LightInfo light = blendLight(vertex_pos);
return encode_light(light.getPair(), cur_node.f->light_source,
player->getLighting().ambient_light.luminance);
return encode_light(light.getPair(), cur_node.f->light_source);
}

video::SColor MapblockMeshGenerator::blendLightColor(const v3f &vertex_pos,
const v3f &vertex_normal)
{
LightInfo light = blendLight(vertex_pos);
video::SColor color = encode_light(light.getPair(MYMAX(0.0f, vertex_normal.Y)),
cur_node.f->light_source,
player->getLighting().ambient_light.luminance);
cur_node.f->light_source);
if (!cur_node.f->light_source)
applyFacesShading(color, vertex_normal);
return color;
Expand Down Expand Up @@ -388,8 +385,7 @@ void MapblockMeshGenerator::drawAutoLightedCuboid(aabb3f box, const f32 *txc,
for (int j = 0; j < 4; j++) {
video::S3DVertex &vertex = vertices[j];
final_lights[j] = lights[light_indices[face][j]].getPair(MYMAX(0.0f, vertex.Normal.Y));
vertex.Color = encode_light(final_lights[j], cur_node.f->light_source,
player->getLighting().ambient_light.luminance);
vertex.Color = encode_light(final_lights[j], cur_node.f->light_source);
if (!cur_node.f->light_source)
applyFacesShading(vertex.Color, vertex.Normal);
}
Expand All @@ -399,8 +395,7 @@ void MapblockMeshGenerator::drawAutoLightedCuboid(aabb3f box, const f32 *txc,
});
} else {
drawCuboid(box, tiles, tile_count, txc, mask, [&] (int face, video::S3DVertex vertices[4]) {
video::SColor color = encode_light(cur_node.light, cur_node.f->light_source,
player->getLighting().ambient_light.luminance);
video::SColor color = encode_light(cur_node.light, cur_node.f->light_source);
if (!cur_node.f->light_source)
applyFacesShading(color, vertices[0].Normal);
for (int j = 0; j < 4; j++) {
Expand Down Expand Up @@ -480,8 +475,7 @@ void MapblockMeshGenerator::drawSolidNode()
auto final_lights = lights[face];
for (int j = 0; j < 4; j++) {
video::S3DVertex &vertex = vertices[j];
vertex.Color = encode_light(final_lights[j], cur_node.f->light_source,
player->getLighting().ambient_light.luminance);
vertex.Color = encode_light(final_lights[j], cur_node.f->light_source);
if (!cur_node.f->light_source)
applyFacesShading(vertex.Color, vertex.Normal);
}
Expand All @@ -491,8 +485,7 @@ void MapblockMeshGenerator::drawSolidNode()
});
} else {
drawCuboid(box, tiles, 6, texture_coord_buf, mask, [&] (int face, video::S3DVertex vertices[4]) {
video::SColor color = encode_light(lights[face], cur_node.f->light_source,
player->getLighting().ambient_light.luminance);
video::SColor color = encode_light(lights[face], cur_node.f->light_source);
if (!cur_node.f->light_source)
applyFacesShading(color, vertices[0].Normal);
for (int j = 0; j < 4; j++) {
Expand Down Expand Up @@ -574,10 +567,8 @@ void MapblockMeshGenerator::prepareLiquidNodeDrawing()
cur_node.light = LightPair(getInteriorLight(ntop, 0, nodedef));
}

cur_liquid.color_top = encode_light(cur_node.light, cur_node.f->light_source,
player->getLighting().ambient_light.luminance);
cur_node.color = encode_light(cur_node.light, cur_node.f->light_source,
player->getLighting().ambient_light.luminance);
cur_liquid.color_top = encode_light(cur_node.light, cur_node.f->light_source);
cur_node.color = encode_light(cur_node.light, cur_node.f->light_source);
}

void MapblockMeshGenerator::getLiquidNeighborhood()
Expand Down Expand Up @@ -869,8 +860,7 @@ void MapblockMeshGenerator::drawGlasslikeFramedNode()
getTile(g_6dirs[face], &tiles[face]);

if (!data->m_smooth_lighting)
cur_node.color = encode_light(cur_node.light, cur_node.f->light_source,
player->getLighting().ambient_light.luminance);
cur_node.color = encode_light(cur_node.light, cur_node.f->light_source);

TileSpec glass_tiles[6];
for (auto &glass_tile : glass_tiles)
Expand Down
26 changes: 11 additions & 15 deletions src/client/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -387,8 +387,7 @@ class GameGlobalShaderConstantSetter : public IShaderConstantSetter
m_animation_timer_delta_pixel{"animationTimerDelta"};
CachedPixelShaderSetting<float, 3> m_day_light{"dayLight"};
CachedPixelShaderSetting<float, 4> m_star_color{"starColor"};
CachedPixelShaderSetting<float, 1> m_ambient_light{"ambientLight"};
CachedPixelShaderSetting<float, 3> m_ambient_color{"ambientColor"};
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 @@ -506,20 +505,15 @@ class GameGlobalShaderConstantSetter : public IShaderConstantSetter
float clr[4] = {star_color.r, star_color.g, star_color.b, star_color.a};
m_star_color.set(clr, services);

auto lighting = m_client->getEnv().getLocalPlayer()->getLighting();

float ambient_light = lighting.ambient_light.luminance / 16.f;

m_ambient_light.set(&ambient_light, services);

video::SColor ambient_color = lighting.ambient_light.color;
auto ambient_light = m_client->getEnv().getAmbientLight();

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

m_ambient_light.set(ambient_light_f, services);

u32 animation_timer = m_client->getEnv().getFrameTime() % 1000000;
float animation_timer_f = (float)animation_timer / 100000.f;
Expand Down Expand Up @@ -562,6 +556,8 @@ class GameGlobalShaderConstantSetter : public IShaderConstantSetter
m_texel_size0_vertex.set(m_texel_size0_values.data(), services);
m_texel_size0_pixel.set(m_texel_size0_values.data(), services);

auto lighting = m_client->getEnv().getLocalPlayer()->getLighting();

const AutoExposure &exposure_params = lighting.exposure;
std::array<float, 7> exposure_buffer = {
std::pow(2.0f, exposure_params.luminance_min),
Expand Down Expand Up @@ -3520,7 +3516,7 @@ PointedThing Game::updatePointedThing(

u32 daynight_ratio = client->getEnv().getDayNightRatio();
video::SColor c;
final_color_blend(&c, light_level, daynight_ratio);
final_color_blend(&c, light_level, daynight_ratio, client->getEnv().getAmbientLight());

// Modify final color a bit with time
u32 timer = client->getEnv().getFrameTime() % 5000;
Expand Down

0 comments on commit 6daa777

Please sign in to comment.