aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorx2048 <codeforsmile@gmail.com>2022-05-04 23:44:55 +0200
committerGitHub <noreply@github.com>2022-05-04 23:44:55 +0200
commitcc56ebd90db3858313a9e597a89c5db8fec3b617 (patch)
tree24ae58d10b0272a805b84763e180264bbacb7419 /src
parent89c82035d88532bb63c9173e0c4edf54b7ea89f8 (diff)
Avoid rendering invisible faces of simple nodeboxes (#12262)
* Skip rendering faces adjacent to opaque nodes * Cancel out opposite faces of adjacent nodebox nodes of the same type Fixes #6409
Diffstat (limited to 'src')
-rw-r--r--src/client/content_mapblock.cpp90
-rw-r--r--src/client/content_mapblock.h5
2 files changed, 82 insertions, 13 deletions
diff --git a/src/client/content_mapblock.cpp b/src/client/content_mapblock.cpp
index b13ae86f4..8675275aa 100644
--- a/src/client/content_mapblock.cpp
+++ b/src/client/content_mapblock.cpp
@@ -150,8 +150,10 @@ void MapblockMeshGenerator::drawQuad(v3f *coords, const v3s16 &normal,
// should be (2+2)*6=24 values in the list. The order of
// the faces in the list is up-down-right-left-back-front
// (compatible with ContentFeatures).
+// mask - a bit mask that suppresses drawing of tiles.
+// tile i will not be drawn if mask & (1 << i) is 1
void MapblockMeshGenerator::drawCuboid(const aabb3f &box,
- TileSpec *tiles, int tilecount, const LightInfo *lights, const f32 *txc)
+ TileSpec *tiles, int tilecount, const LightInfo *lights, const f32 *txc, u8 mask)
{
assert(tilecount >= 1 && tilecount <= 6); // pre-condition
@@ -274,6 +276,8 @@ void MapblockMeshGenerator::drawCuboid(const aabb3f &box,
// Add to mesh collector
for (int k = 0; k < 6; ++k) {
+ if (mask & (1 << k))
+ continue;
int tileindex = MYMIN(k, tilecount - 1);
collector->append(tiles[tileindex], vertices + 4 * k, 4, quad_indices, 6);
}
@@ -363,7 +367,7 @@ void MapblockMeshGenerator::generateCuboidTextureCoords(const aabb3f &box, f32 *
}
void MapblockMeshGenerator::drawAutoLightedCuboid(aabb3f box, const f32 *txc,
- TileSpec *tiles, int tile_count)
+ TileSpec *tiles, int tile_count, u8 mask)
{
bool scale = std::fabs(f->visual_scale - 1.0f) > 1e-3f;
f32 texture_coord_buf[24];
@@ -403,12 +407,49 @@ void MapblockMeshGenerator::drawAutoLightedCuboid(aabb3f box, const f32 *txc,
d.Z = (j & 1) ? dz2 : dz1;
lights[j] = blendLight(d);
}
- drawCuboid(box, tiles, tile_count, lights, txc);
+ drawCuboid(box, tiles, tile_count, lights, txc, mask);
} else {
- drawCuboid(box, tiles, tile_count, nullptr, txc);
+ drawCuboid(box, tiles, tile_count, nullptr, txc, mask);
+ }
+}
+
+u8 MapblockMeshGenerator::getNodeBoxMask(aabb3f box, u8 solid_neighbors, u8 sametype_neighbors) const
+{
+ const f32 NODE_BOUNDARY = 0.5 * BS;
+
+ // For an oversized nodebox, return immediately
+ if (box.MaxEdge.X > NODE_BOUNDARY ||
+ box.MinEdge.X < -NODE_BOUNDARY ||
+ box.MaxEdge.Y > NODE_BOUNDARY ||
+ box.MinEdge.Y < -NODE_BOUNDARY ||
+ box.MaxEdge.Z > NODE_BOUNDARY ||
+ box.MinEdge.Z < -NODE_BOUNDARY)
+ return 0;
+
+ // We can skip faces at node boundary if the matching neighbor is solid
+ u8 solid_mask =
+ (box.MaxEdge.Y == NODE_BOUNDARY ? 1 : 0) |
+ (box.MinEdge.Y == -NODE_BOUNDARY ? 2 : 0) |
+ (box.MaxEdge.X == NODE_BOUNDARY ? 4 : 0) |
+ (box.MinEdge.X == -NODE_BOUNDARY ? 8 : 0) |
+ (box.MaxEdge.Z == NODE_BOUNDARY ? 16 : 0) |
+ (box.MinEdge.Z == -NODE_BOUNDARY ? 32 : 0);
+
+ u8 sametype_mask = 0;
+ if (f->alpha == AlphaMode::ALPHAMODE_OPAQUE) {
+ // In opaque nodeboxes, faces on opposite sides can cancel
+ // each other out if there is a matching neighbor of the same type
+ sametype_mask =
+ ((solid_mask & 3) == 3 ? 3 : 0) |
+ ((solid_mask & 12) == 12 ? 12 : 0) |
+ ((solid_mask & 48) == 48 ? 48 : 0);
}
+
+ // Combine masks with actual neighbors to get the faces to be skipped
+ return (solid_mask & solid_neighbors) | (sametype_mask & sametype_neighbors);
}
+
void MapblockMeshGenerator::prepareLiquidNodeDrawing()
{
getSpecialTile(0, &tile_liquid_top);
@@ -1366,13 +1407,38 @@ void MapblockMeshGenerator::drawNodeboxNode()
getTile(nodebox_tile_dirs[face], &tiles[face]);
}
+ bool param2_is_rotation =
+ f->param_type_2 == CPT2_COLORED_FACEDIR ||
+ f->param_type_2 == CPT2_COLORED_WALLMOUNTED ||
+ f->param_type_2 == CPT2_FACEDIR ||
+ f->param_type_2 == CPT2_WALLMOUNTED;
+
+ bool param2_is_level =
+ f->param_type_2 == CPT2_LEVELED;
+
// locate possible neighboring nodes to connect to
u8 neighbors_set = 0;
- if (f->node_box.type == NODEBOX_CONNECTED) {
- for (int dir = 0; dir != 6; dir++) {
- u8 flag = 1 << dir;
- v3s16 p2 = blockpos_nodes + p + nodebox_connection_dirs[dir];
- MapNode n2 = data->m_vmanip.getNodeNoEx(p2);
+ u8 solid_neighbors = 0;
+ u8 sametype_neighbors = 0;
+ for (int dir = 0; dir != 6; dir++) {
+ u8 flag = 1 << dir;
+ v3s16 p2 = blockpos_nodes + p + nodebox_tile_dirs[dir];
+ MapNode n2 = data->m_vmanip.getNodeNoEx(p2);
+
+ // mark neighbors that are the same node type
+ // and have the same rotation or higher level stored as param2
+ if (n2.param0 == n.param0 &&
+ (!param2_is_rotation || n.param2 == n2.param2) &&
+ (!param2_is_level || n.param2 <= n2.param2))
+ sametype_neighbors |= flag;
+
+ // mark neighbors that are simple solid blocks
+ if (nodedef->get(n2).drawtype == NDT_NORMAL)
+ solid_neighbors |= flag;
+
+ if (f->node_box.type == NODEBOX_CONNECTED) {
+ p2 = blockpos_nodes + p + nodebox_connection_dirs[dir];
+ n2 = data->m_vmanip.getNodeNoEx(p2);
if (nodedef->nodeboxConnects(n, n2, flag))
neighbors_set |= flag;
}
@@ -1433,8 +1499,10 @@ void MapblockMeshGenerator::drawNodeboxNode()
}
}
- for (auto &box : boxes)
- drawAutoLightedCuboid(box, nullptr, tiles, 6);
+ for (auto &box : boxes) {
+ u8 mask = getNodeBoxMask(box, solid_neighbors, sametype_neighbors);
+ drawAutoLightedCuboid(box, nullptr, tiles, 6, mask);
+ }
}
void MapblockMeshGenerator::drawMeshNode()
diff --git a/src/client/content_mapblock.h b/src/client/content_mapblock.h
index 7344f05ee..b13748cbc 100644
--- a/src/client/content_mapblock.h
+++ b/src/client/content_mapblock.h
@@ -100,10 +100,11 @@ public:
// cuboid drawing!
void drawCuboid(const aabb3f &box, TileSpec *tiles, int tilecount,
- const LightInfo *lights , const f32 *txc);
+ const LightInfo *lights , const f32 *txc, u8 mask = 0);
void generateCuboidTextureCoords(aabb3f const &box, f32 *coords);
void drawAutoLightedCuboid(aabb3f box, const f32 *txc = NULL,
- TileSpec *tiles = NULL, int tile_count = 0);
+ TileSpec *tiles = NULL, int tile_count = 0, u8 mask = 0);
+ u8 getNodeBoxMask(aabb3f box, u8 solid_neighbors, u8 sametype_neighbors) const;
// liquid-specific
bool top_is_same_liquid;
429'>429 430 431 432
/*
Minetest
Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#include <cctype>
#include <fstream>
#include "mods.h"
#include "filesys.h"
#include "log.h"
#include "subgame.h"
#include "settings.h"
#include "convert_json.h"

static bool parseDependsLine(std::istream &is,
		std::string &dep, std::set<char> &symbols)
{
	std::getline(is, dep);
	dep = trim(dep);
	symbols.clear();
	size_t pos = dep.size();
	while(pos > 0 && !string_allowed(dep.substr(pos-1, 1), MODNAME_ALLOWED_CHARS)){
		// last character is a symbol, not part of the modname
		symbols.insert(dep[pos-1]);
		--pos;
	}
	dep = trim(dep.substr(0, pos));
	return dep != "";
}

void parseModContents(ModSpec &spec)
{
	// NOTE: this function works in mutual recursion with getModsInPath
	Settings info;
	info.readConfigFile((spec.path+DIR_DELIM+"mod.conf").c_str());

	if (info.exists("name"))
		spec.name = info.get("name");

	spec.depends.clear();
	spec.optdepends.clear();
	spec.is_modpack = false;
	spec.modpack_content.clear();

	// Handle modpacks (defined by containing modpack.txt)
	std::ifstream modpack_is((spec.path+DIR_DELIM+"modpack.txt").c_str());
	if(modpack_is.good()){ //a modpack, recursively get the mods in it
		modpack_is.close(); // We don't actually need the file
		spec.is_modpack = true;
		spec.modpack_content = getModsInPath(spec.path, true);

		// modpacks have no dependencies; they are defined and
		// tracked separately for each mod in the modpack
	}
	else{ // not a modpack, parse the dependencies
		std::ifstream is((spec.path+DIR_DELIM+"depends.txt").c_str());
		while(is.good()){
			std::string dep;
			std::set<char> symbols;
			if(parseDependsLine(is, dep, symbols)){
				if(symbols.count('?') != 0){
					spec.optdepends.insert(dep);
				}
				else{
					spec.depends.insert(dep);
				}
			}
		}
	}
}

std::map<std::string, ModSpec> getModsInPath(std::string path, bool part_of_modpack)
{
	// NOTE: this function works in mutual recursion with parseModContents

	std::map<std::string, ModSpec> result;
	std::vector<fs::DirListNode> dirlist = fs::GetDirListing(path);
	for(u32 j=0; j<dirlist.size(); j++){
		if(!dirlist[j].dir)
			continue;
		std::string modname = dirlist[j].name;
		// Ignore all directories beginning with a ".", especially
		// VCS directories like ".git" or ".svn"
		if(modname[0] == '.')
			continue;
		std::string modpath = path + DIR_DELIM + modname;

		ModSpec spec(modname, modpath);
		spec.part_of_modpack = part_of_modpack;
		parseModContents(spec);
		result.insert(std::make_pair(modname, spec));
	}
	return result;
}

std::map<std::string, ModSpec> flattenModTree(std::map<std::string, ModSpec> mods)
{
	std::map<std::string, ModSpec> result;
	for(std::map<std::string,ModSpec>::iterator it = mods.begin();
		it != mods.end(); ++it)
	{
		ModSpec mod = (*it).second;
		if(mod.is_modpack)
		{
			std::map<std::string, ModSpec> content =
				flattenModTree(mod.modpack_content);
			result.insert(content.begin(),content.end());
			result.insert(std::make_pair(mod.name,mod));
		}
		else //not a modpack
		{
			result.insert(std::make_pair(mod.name,mod));
		}
	}
	return result;
}

std::vector<ModSpec> flattenMods(std::map<std::string, ModSpec> mods)
{
	std::vector<ModSpec> result;
	for(std::map<std::string,ModSpec>::iterator it = mods.begin();
		it != mods.end(); ++it)
	{
		ModSpec mod = (*it).second;
		if(mod.is_modpack)
		{
			std::vector<ModSpec> content = flattenMods(mod.modpack_content);
			result.reserve(result.size() + content.size());
			result.insert(result.end(),content.begin(),content.end());

		}
		else //not a modpack
		{
			result.push_back(mod);
		}
	}
	return result;
}

ModConfiguration::ModConfiguration(std::string worldpath)
{
	SubgameSpec gamespec = findWorldSubgame(worldpath);

	// Add all game mods and all world mods
	addModsInPath(gamespec.gamemods_path);
	addModsInPath(worldpath + DIR_DELIM + "worldmods");

	// check world.mt file for mods explicitely declared to be
	// loaded or not by a load_mod_<modname> = ... line.
	std::string worldmt = worldpath+DIR_DELIM+"world.mt";
	Settings worldmt_settings;
	worldmt_settings.readConfigFile(worldmt.c_str());
	std::vector<std::string> names = worldmt_settings.getNames();
	std::set<std::string> include_mod_names;
	for(std::vector<std::string>::iterator it = names.begin();
		it != names.end(); ++it)
	{
		std::string name = *it;
		// for backwards compatibility: exclude only mods which are
		// explicitely excluded. if mod is not mentioned at all, it is
		// enabled. So by default, all installed mods are enabled.
		if (name.compare(0,9,"load_mod_") == 0 &&
			worldmt_settings.getBool(name))
		{
			include_mod_names.insert(name.substr(9));
		}
	}

	// Collect all mods that are also in include_mod_names
	std::vector<ModSpec> addon_mods;
	for(std::set<std::string>::const_iterator it_path = gamespec.addon_mods_paths.begin();
			it_path != gamespec.addon_mods_paths.end(); ++it_path)
	{
		std::vector<ModSpec> addon_mods_in_path = flattenMods(getModsInPath(*it_path));
		for(std::vector<ModSpec>::iterator it = addon_mods_in_path.begin();
			it != addon_mods_in_path.end(); ++it)
		{
			ModSpec& mod = *it;
			if(include_mod_names.count(mod.name) != 0)
				addon_mods.push_back(mod);
			else
				worldmt_settings.setBool("load_mod_" + mod.name, false);
		}
	}
	worldmt_settings.updateConfigFile(worldmt.c_str());

	addMods(addon_mods);

	// report on name conflicts
	if(!m_name_conflicts.empty()){
		std::string s = "Unresolved name conflicts for mods ";
		for(std::set<std::string>::const_iterator it = m_name_conflicts.begin();
				it != m_name_conflicts.end(); ++it)
		{
			if(it != m_name_conflicts.begin()) s += ", ";
			s += std::string("\"") + (*it) + "\"";
		}
		s += ".";
		throw ModError(s);
	}

	// get the mods in order
	resolveDependencies();
}

void ModConfiguration::addModsInPath(std::string path)
{