Skip to content

Commit

Permalink
Ambient light improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
appgurueu committed Mar 4, 2024
1 parent d0d6775 commit 13b41b1
Show file tree
Hide file tree
Showing 10 changed files with 40 additions and 69 deletions.
5 changes: 2 additions & 3 deletions doc/lua_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -8184,9 +8184,8 @@ child will follow movement and rotation of that bone.
* `set_lighting(light_definition)`: sets lighting for the player
* Passing no arguments resets lighting to its default values.
* `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 = 255, r = 255, g = 255, b = 255}`). Alpha is ignored.
* `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).
* `saturation` sets the saturation (vividness; default: `1.0`).
* values > 1 increase the saturation
* values in [0,1] decrease the saturation
Expand Down
4 changes: 1 addition & 3 deletions src/client/clientenvironment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -298,9 +298,7 @@ void ClientEnvironment::step(float dtime)
m_script->environment_step(dtime);

// Update the ambient light
AmbientLight &ambient_light = getLocalPlayer()->getLighting().ambient_light;

video::SColor new_ambient_light_clr = encodeAmbientLight(ambient_light.luminance, ambient_light.color);
auto new_ambient_light_clr = getLocalPlayer()->getLighting().ambient_light;

if (new_ambient_light_clr != m_ambient_light) {
getClientMap().forceUpdateLightColor();
Expand Down
3 changes: 2 additions & 1 deletion src/client/clientenvironment.h
Original file line number Diff line number Diff line change
Expand Up @@ -163,5 +163,6 @@ 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};
// Note: Alpha should always be 255.
video::SColor m_ambient_light {255, 0, 0, 0};
};
2 changes: 1 addition & 1 deletion src/client/clientmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -719,7 +719,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 auto ambient_light = m_client->getEnv().getAmbientLight();

const v3f camera_position = m_camera_position;

Expand Down
13 changes: 0 additions & 13 deletions src/client/mapblock_mesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1030,19 +1030,6 @@ video::SColor encode_light(u16 light, u8 emissive_light)
return video::SColor(r, b, b, b);
}

video::SColor encodeAmbientLight(u8 light, video::SColor color)
{
video::SColor res_color(0, 0, 0, 0);

float light_f = light / 15.f;

res_color.setRed(core::round32(light_f * color.getRed()));
res_color.setGreen(core::round32(light_f * color.getGreen()));
res_color.setBlue(core::round32(light_f * color.getBlue()));

return res_color;
}

u8 get_solid_sides(MeshMakeData *data)
{
std::unordered_map<v3s16, u8> results;
Expand Down
2 changes: 0 additions & 2 deletions src/client/mapblock_mesh.h
Original file line number Diff line number Diff line change
Expand Up @@ -297,8 +297,6 @@ class MapBlockMesh
*/
video::SColor encode_light(u16 light, u8 emissive_light);

video::SColor encodeAmbientLight(u8 light, video::SColor color);

// Compute light at node
u16 getInteriorLight(MapNode n, s32 increment, const NodeDefManager *ndef);
u16 getFaceLight(MapNode n, MapNode n2, const NodeDefManager *ndef);
Expand Down
19 changes: 2 additions & 17 deletions src/lighting.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,26 +47,11 @@ struct AutoExposure
AutoExposure();
};

/*
* Parameters for adjusting ambient light affecting on colors of map nodes and entities
*/
struct AmbientLight
{
AmbientLight() : luminance(0), color(255, 255, 255, 255)
{}
/// @brief Minimal threshold of luminance of ambience. Can be from 0 - 14.
u8 luminance;

/// @brief Color of ambient light. Default is white color.
video::SColor color;
};

/** Describes ambient light settings for a player
*/
struct Lighting
{
AutoExposure exposure;
AmbientLight ambient_light;
/// @brief Ambient light color & intensity for nodes & entities. Alpha is ignored.
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
43 changes: 25 additions & 18 deletions src/network/clientpackethandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1802,22 +1802,29 @@ void Client::handleCommand_SetLighting(NetworkPacket *pkt)
{
Lighting& lighting = m_env.getLocalPlayer()->getLighting();

if (pkt->getRemainingBytes() >= 4)
*pkt >> lighting.shadow_intensity;
if (pkt->getRemainingBytes() >= 4)
*pkt >> lighting.saturation;
if (pkt->getRemainingBytes() >= 24) {
*pkt >> lighting.exposure.luminance_min
>> lighting.exposure.luminance_max
>> lighting.exposure.exposure_correction
>> lighting.exposure.speed_dark_bright
>> lighting.exposure.speed_bright_dark
>> lighting.exposure.center_weight_power;
}
if (pkt->getRemainingBytes() >= 4)
*pkt >> lighting.volumetric_light_strength;
if (pkt->getRemainingBytes() >= 1) {
*pkt >> lighting.ambient_light.luminance
>> lighting.ambient_light.color;
}
if (pkt->getRemainingBytes() < 4)
return;
*pkt >> lighting.shadow_intensity;

if (pkt->getRemainingBytes() < 4)
return;
*pkt >> lighting.saturation;

if (pkt->getRemainingBytes() < 24)
return;
*pkt >> lighting.exposure.luminance_min
>> lighting.exposure.luminance_max
>> lighting.exposure.exposure_correction
>> lighting.exposure.speed_dark_bright
>> lighting.exposure.speed_bright_dark
>> lighting.exposure.center_weight_power;

if (pkt->getRemainingBytes() < 4)
return;
*pkt >> lighting.volumetric_light_strength;

if (pkt->getRemainingBytes() < 4)
return;
*pkt >> lighting.ambient_light;
lighting.ambient_light.setAlpha(255); // alpha should always be 255
}
15 changes: 6 additions & 9 deletions src/script/lua_api/l_object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,

#include "lua_api/l_object.h"
#include <cmath>
#include "common/c_types.h"
#include "lua_api/l_internal.h"
#include "lua_api/l_inventory.h"
#include "lua_api/l_item.h"
Expand Down Expand Up @@ -2528,14 +2529,10 @@ int ObjectRef::l_set_lighting(lua_State *L)
lua_pop(L, 1); // shadows

lua_getfield(L, 2, "ambient_light");
if(lua_istable(L, -1)) {
getintfield(L, -1, "luminance", lighting.ambient_light.luminance);
lighting.ambient_light.luminance = rangelim(lighting.ambient_light.luminance, 0, LIGHT_SUN);

lua_getfield(L, -1, "color");
if (!lua_isnil(L, -1))
read_color(L, -1, &lighting.ambient_light.color);
lua_pop(L, 1);
if (!lua_isnil(L, -1)) {
read_color(L, -1, &lighting.ambient_light);
if (lighting.ambient_light.getAlpha() != 255)
throw LuaError("ambient light alpha must be 255");
}
lua_pop(L, 1); // ambient light

Expand All @@ -2558,7 +2555,7 @@ int ObjectRef::l_set_lighting(lua_State *L)
lighting.volumetric_light_strength = rangelim(lighting.volumetric_light_strength, 0.0f, 1.0f);
}
lua_pop(L, 1); // volumetric_light
}
}

getServer(L)->setLighting(player, lighting);
return 0;
Expand Down
3 changes: 1 addition & 2 deletions src/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1881,8 +1881,7 @@ void Server::SendSetLighting(session_t peer_id, const Lighting &lighting)

pkt << lighting.volumetric_light_strength;

pkt << lighting.ambient_light.luminance
<< lighting.ambient_light.color;
pkt << lighting.ambient_light;

Send(&pkt);
}
Expand Down

0 comments on commit 13b41b1

Please sign in to comment.