Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow nodes to have their post_effect_color affected by lighting #13637

Merged
merged 6 commits into from
Aug 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion doc/lua_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -8702,7 +8702,11 @@ Used by `minetest.register_node`.
-- Only when `paramtype2` supports palettes.

post_effect_color = "#00000000",
-- Screen tint if player is inside node, see "ColorSpec"
-- Screen tint if a player is inside this node, see `ColorSpec`.
-- Color is alpha-blended over the screen.

post_effect_color_shaded = false,
-- Determines whether `post_effect_color` is affected by lighting.

paramtype = "none", -- See "Nodes"

Expand Down
4 changes: 4 additions & 0 deletions games/devtest/mods/basenodes/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ minetest.register_node("basenodes:water_source", {
liquid_alternative_source = "basenodes:water_source",
liquid_viscosity = WATER_VISC,
post_effect_color = {a = 64, r = 100, g = 100, b = 200},
post_effect_color_shaded = true,
groups = {water = 3, liquid = 3},
})

Expand Down Expand Up @@ -177,6 +178,7 @@ minetest.register_node("basenodes:water_flowing", {
liquid_alternative_source = "basenodes:water_source",
liquid_viscosity = WATER_VISC,
post_effect_color = {a = 64, r = 100, g = 100, b = 200},
post_effect_color_shaded = true,
groups = {water = 3, liquid = 3},
})

Expand Down Expand Up @@ -206,6 +208,7 @@ minetest.register_node("basenodes:river_water_source", {
liquid_renewable = false,
liquid_range = 2,
post_effect_color = {a = 103, r = 30, g = 76, b = 90},
post_effect_color_shaded = true,
groups = {water = 3, liquid = 3, },
})

Expand Down Expand Up @@ -238,6 +241,7 @@ minetest.register_node("basenodes:river_water_flowing", {
liquid_renewable = false,
liquid_range = 2,
post_effect_color = {a = 103, r = 30, g = 76, b = 90},
post_effect_color_shaded = true,
groups = {water = 3, liquid = 3, },
})

Expand Down
33 changes: 33 additions & 0 deletions games/devtest/mods/testnodes/properties.lua
Original file line number Diff line number Diff line change
Expand Up @@ -588,3 +588,36 @@ minetest.register_node("testnodes:drowning_1", {
groups = {dig_immediate=3},
})

-- post_effect_color_shaded

minetest.register_node("testnodes:post_effect_color_shaded_false", {
description = S("\"post_effect_color_shaded = false\" Node"),

drawtype = "allfaces",
tiles = {"testnodes_post_effect_color_shaded_false.png"},
grorp marked this conversation as resolved.
Show resolved Hide resolved
use_texture_alpha = "blend",
paramtype = "light",
sunlight_propagates = true,
post_effect_color = {a = 128, r = 255, g = 255, b = 255},
post_effect_color_shaded = false,

walkable = false,
is_ground_content = false,
groups = {dig_immediate=3},
})

minetest.register_node("testnodes:post_effect_color_shaded_true", {
description = S("\"post_effect_color_shaded = true\" Node"),

drawtype = "allfaces",
tiles = {"testnodes_post_effect_color_shaded_true.png"},
use_texture_alpha = "blend",
paramtype = "light",
sunlight_propagates = true,
post_effect_color = {a = 128, r = 255, g = 255, b = 255},
post_effect_color_shaded = true,

walkable = false,
is_ground_content = false,
groups = {dig_immediate=3},
})
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 16 additions & 6 deletions src/client/clientmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ ClientMap::~ClientMap()
g_settings->deregisterChangedCallback("enable_raytraced_culling", on_settings_changed, this);
}

void ClientMap::updateCamera(v3f pos, v3f dir, f32 fov, v3s16 offset)
void ClientMap::updateCamera(v3f pos, v3f dir, f32 fov, v3s16 offset, video::SColor light_color)
{
v3s16 previous_node = floatToInt(m_camera_position, BS) + m_camera_offset;
v3s16 previous_block = getContainerPos(previous_node, MAP_BLOCKSIZE);
Expand All @@ -133,6 +133,7 @@ void ClientMap::updateCamera(v3f pos, v3f dir, f32 fov, v3s16 offset)
m_camera_direction = dir;
m_camera_fov = fov;
m_camera_offset = offset;
m_camera_light_color = light_color;

v3s16 current_node = floatToInt(m_camera_position, BS) + m_camera_offset;
v3s16 current_block = getContainerPos(current_node, MAP_BLOCKSIZE);
Expand Down Expand Up @@ -1059,21 +1060,30 @@ void ClientMap::renderPostFx(CameraMode cam_mode)
MapNode n = getNode(floatToInt(m_camera_position, BS));

const ContentFeatures& features = m_nodedef->get(n);
video::SColor post_effect_color = features.post_effect_color;
video::SColor post_color = features.post_effect_color;

if (features.post_effect_color_shaded) {
auto apply_light = [] (u32 color, u32 light) {
return core::clamp(core::round32(color * light / 255.0f), 0, 255);
};
post_color.setRed(apply_light(post_color.getRed(), m_camera_light_color.getRed()));
post_color.setGreen(apply_light(post_color.getGreen(), m_camera_light_color.getGreen()));
post_color.setBlue(apply_light(post_color.getBlue(), m_camera_light_color.getBlue()));
}

// If the camera is in a solid node, make everything black.
// (first person mode only)
if (features.solidness == 2 && cam_mode == CAMERA_MODE_FIRST &&
!m_control.allow_noclip) {
post_effect_color = video::SColor(255, 0, 0, 0);
!m_control.allow_noclip) {
post_color = video::SColor(255, 0, 0, 0);
}

if (post_effect_color.getAlpha() != 0) {
if (post_color.getAlpha() != 0) {
// Draw a full-screen rectangle
video::IVideoDriver* driver = SceneManager->getVideoDriver();
v2u32 ss = driver->getScreenSize();
core::rect<s32> rect(0,0, ss.X, ss.Y);
driver->draw2DRectangle(post_effect_color, rect);
driver->draw2DRectangle(post_color, rect);
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/client/clientmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class ClientMap : public Map, public scene::ISceneNode
ISceneNode::drop(); // calls destructor
}

void updateCamera(v3f pos, v3f dir, f32 fov, v3s16 offset);
void updateCamera(v3f pos, v3f dir, f32 fov, v3s16 offset, video::SColor light_color);

/*
Forcefully get a sector from somewhere
Expand Down Expand Up @@ -201,6 +201,7 @@ class ClientMap : public Map, public scene::ISceneNode
v3f m_camera_direction = v3f(0,0,1);
f32 m_camera_fov = M_PI;
v3s16 m_camera_offset;
video::SColor m_camera_light_color = video::SColor(0xFFFFFFFF);
bool m_needs_update_transparent_meshes = true;

std::map<v3s16, MapBlock*, MapBlockComparer> m_drawlist;
Expand Down
2 changes: 1 addition & 1 deletion src/client/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3143,7 +3143,7 @@ void Game::updateCamera(f32 dtime)
v3f camera_direction = camera->getDirection();

client->getEnv().getClientMap().updateCamera(camera_position,
camera_direction, camera_fov, camera_offset);
camera_direction, camera_fov, camera_offset, player->light_color);

if (m_camera_offset_changed) {
client->updateCameraOffset(camera_offset);
Expand Down
7 changes: 7 additions & 0 deletions src/nodedef.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,7 @@ void ContentFeatures::reset()
node_dig_prediction = "air";
move_resistance = 0;
liquid_move_physics = false;
post_effect_color_shaded = false;
}

void ContentFeatures::setAlphaFromLegacy(u8 legacy_alpha)
Expand Down Expand Up @@ -543,6 +544,7 @@ void ContentFeatures::serialize(std::ostream &os, u16 protocol_version) const
writeU8(os, alpha);
writeU8(os, move_resistance);
writeU8(os, liquid_move_physics);
writeU8(os, post_effect_color_shaded);
}

void ContentFeatures::deSerialize(std::istream &is, u16 protocol_version)
Expand Down Expand Up @@ -656,6 +658,11 @@ void ContentFeatures::deSerialize(std::istream &is, u16 protocol_version)
if (is.eof())
throw SerializationError("");
liquid_move_physics = tmp;

tmp = readU8(is);
if (is.eof())
throw SerializationError("");
post_effect_color_shaded = tmp;
} catch(SerializationError &e) {};
}

Expand Down
1 change: 1 addition & 0 deletions src/nodedef.h
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,7 @@ struct ContentFeatures
std::vector<content_t> connects_to_ids;
// Post effect color, drawn when the camera is inside the node.
video::SColor post_effect_color;
bool post_effect_color_shaded;
// Flowing liquid or leveled nodebox, value = default level
u8 leveled;
// Maximum value for leveled nodes
Expand Down
4 changes: 4 additions & 0 deletions src/script/common/c_content.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -685,6 +685,8 @@ void read_content_features(lua_State *L, ContentFeatures &f, int index)
read_color(L, -1, &f.post_effect_color);
lua_pop(L, 1);

getboolfield(L, index, "post_effect_color_shaded", f.post_effect_color_shaded);

f.param_type = (ContentParamType)getenumfield(L, index, "paramtype",
ScriptApiNode::es_ContentParamType, CPT_NONE);
f.param_type_2 = (ContentParamType2)getenumfield(L, index, "paramtype2",
Expand Down Expand Up @@ -916,6 +918,8 @@ void push_content_features(lua_State *L, const ContentFeatures &c)

push_ARGB8(L, c.post_effect_color);
lua_setfield(L, -2, "post_effect_color");
lua_pushboolean(L, c.post_effect_color_shaded);
lua_setfield(L, -2, "post_effect_color_shaded");
lua_pushnumber(L, c.leveled);
lua_setfield(L, -2, "leveled");
lua_pushnumber(L, c.leveled_max);
Expand Down