Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions builtin/game/misc_s.lua
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ core.protocol_versions = {
["5.10.0"] = 46,
["5.11.0"] = 47,
["5.12.0"] = 48,
["5.13.0"] = 49,
}

setmetatable(core.protocol_versions, {__newindex = function()
Expand Down
9 changes: 9 additions & 0 deletions client/shaders/second_stage/opengl_fragment.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ centroid varying vec2 varTexCoord;
varying float exposure; // linear exposure factor, see vertex shader
#endif

uniform mat3 colorTransformMatrix;

#ifdef ENABLE_BLOOM

vec4 applyBloom(vec4 color, vec2 uv)
Expand Down Expand Up @@ -103,6 +105,12 @@ vec3 screen_space_dither(highp vec2 frag_coord) {
}
#endif

vec4 applyColorVision(vec4 color)
{
vec3 transformedColor = colorTransformMatrix * color.rgb;
return vec4(transformedColor, color.a);
}

void main(void)
{
vec2 uv = varTexCoord.st;
Expand Down Expand Up @@ -133,6 +141,7 @@ void main(void)
color = applyBloom(color, uv);
#endif

color = applyColorVision(color);

color.rgb = clamp(color.rgb, vec3(0.), vec3(1.));

Expand Down
17 changes: 17 additions & 0 deletions doc/lua_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -9064,6 +9064,23 @@ child will follow movement and rotation of that bone.
* Currently, bloom `intensity` and `strength_factor` affect volumetric
lighting `strength` and vice versa. This behavior is to be changed
in the future, do not rely on it.
* `color_transform_matrix`: is a matrix with default value (identity matrix):
```lua
{ {1.0, 0.0, 0.0}, -- r
{0.0, 1.0, 0.0}, -- g
{0.0, 0.0, 1.0}} -- b
```

* Work as `transformed_color_RGB = color_transform_matrix * color_RGB`
* Can be used for creation color blind effect, base for night vision effect etc.
* Request client with protocol version 49 or higger.

```lua
-- example of night vision like transform
{ {0.0, 0.0, 0.0},
{1.0, 9.0, 1.0},
{0.0, 0.0, 0.0}}
```

* `get_lighting()`: returns the current state of lighting for the player.
* Result is a table with the same fields as `light_definition` in `set_lighting`.
Expand Down
5 changes: 5 additions & 0 deletions src/client/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,9 @@ class GameGlobalShaderUniformSetter : public IShaderUniformSetter
CachedPixelShaderSetting<float>
m_volumetric_light_strength_pixel{"volumetricLightStrength"};

CachedPixelShaderSetting<float, 9>
m_color_transform_matrix{"colorTransformMatrix"};

static constexpr std::array<const char*, 1> SETTING_CALLBACKS = {
"exposure_compensation",
};
Expand Down Expand Up @@ -321,6 +324,8 @@ class GameGlobalShaderUniformSetter : public IShaderUniformSetter
m_bloom_radius_pixel.set(&radius, services);
}

m_color_transform_matrix.set(lighting.vision_effects.color_transform_matrix.data(), services);

float saturation = lighting.saturation;
m_saturation_pixel.set(&saturation, services);

Expand Down
13 changes: 13 additions & 0 deletions src/lighting.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,18 @@ struct AutoExposure
{}
};

struct VisionEffects
{
std::array<float, 9> color_transform_matrix;

constexpr VisionEffects()
: color_transform_matrix{
1.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 1.0f}
{}
};

/** Describes ambient light settings for a player
*/
struct Lighting
Expand All @@ -52,4 +64,5 @@ struct Lighting
float bloom_intensity {0.05f};
float bloom_strength_factor {1.0f};
float bloom_radius {1.0f};
VisionEffects vision_effects;
};
5 changes: 5 additions & 0 deletions src/network/clientpackethandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1794,4 +1794,9 @@ void Client::handleCommand_SetLighting(NetworkPacket *pkt)
>> lighting.bloom_strength_factor
>> lighting.bloom_radius;
}
if (pkt->getRemainingBytes() >= 36) {
for (int i = 0; i < 9; ++i) {
*pkt >> lighting.vision_effects.color_transform_matrix[i];
}
}
}
5 changes: 4 additions & 1 deletion src/network/networkprotocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,13 @@
PROTOCOL VERSION 48
Add compression to some existing packets
[scheduled bump for 5.12.0]
PROTOCOL VERSION 49
Add "vision_effects" to TOCLIENT_SET_LIGHTING packet
[scheduled bump for 5.13.0]
*/

// Note: Also update core.protocol_versions in builtin when bumping
const u16 LATEST_PROTOCOL_VERSION = 48;
const u16 LATEST_PROTOCOL_VERSION = 49;

// See also formspec [Version History] in doc/lua_api.md
const u16 FORMSPEC_API_VERSION = 9;
38 changes: 37 additions & 1 deletion src/script/lua_api/l_object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2696,7 +2696,32 @@ int ObjectRef::l_set_lighting(lua_State *L)
lighting.bloom_radius = getfloatfield_default(L, -1, "radius", lighting.bloom_radius);
}
lua_pop(L, 1); // bloom
}

lua_getfield(L, 2, "color_transform_matrix");

// if none or nil, keep color transform matrix unchanged
if (!lua_isnoneornil(L, -1)) {
if (!lua_istable(L, -1))
throw LuaError("vision_effects.color_transform_matrix is not a table");

for (int row = 1; row <= 3; ++row) {
lua_rawgeti(L, -1, row);
if (!lua_istable(L, -1))
throw LuaError("color_transform_matrix should be in format {{a, b, c},{d, e, f},{g, a, h}}");

for (int col = 1; col <= 3; ++col) {
lua_rawgeti(L, -1, col);
if (!lua_isnumber(L, -1))
throw LuaError("color_transform_matrix should be in format {{a, b, c},{d, e, f},{g, a, h}}");

lighting.vision_effects.color_transform_matrix[(row - 1) * 3 + (col - 1)] = (float)lua_tonumber(L, -1);
lua_pop(L, 1); // Pop the value at [row][col]
}
lua_pop(L, 1); // Pop the row table
}
}
lua_pop(L, 1); // color_transform_matrix
}

getServer(L)->setLighting(player, lighting);
return 0;
Expand Down Expand Up @@ -2748,6 +2773,17 @@ int ObjectRef::l_get_lighting(lua_State *L)
lua_pushnumber(L, lighting.bloom_radius);
lua_setfield(L, -2, "radius");
lua_setfield(L, -2, "bloom");
lua_newtable(L); // "color_transform_matrix"
// Create the nested table structure {{a, b, c}, {d, e, f}, {g, h, i}}
for (int row = 0; row < 3; row++) {
lua_newtable(L); // Create inner row table
for (int col = 0; col < 3; col++) {
lua_pushnumber(L, lighting.vision_effects.color_transform_matrix[row * 3 + col]); // Push the value
lua_rawseti(L, -2, col + 1); // Set value in inner table with 1-based indexing
}
lua_rawseti(L, -2, row + 1); // Set inner table in the outer matrix table
}
lua_setfield(L, -2, "color_transform_matrix");
return 1;
}

Expand Down
4 changes: 4 additions & 0 deletions src/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1952,6 +1952,10 @@ void Server::SendSetLighting(session_t peer_id, const Lighting &lighting)
pkt << lighting.bloom_intensity << lighting.bloom_strength_factor <<
lighting.bloom_radius;

for (int i = 0; i < 9; ++i) {
pkt << lighting.vision_effects.color_transform_matrix[i];
}

Send(&pkt);
}

Expand Down
Loading