Remove fast faces (#13216)

Co-authored-by: Lars <larsh@apache.org>
This commit is contained in:
Vitaliy 2023-04-08 21:17:15 +03:00 committed by GitHub
parent c2a9ac24ac
commit 35929d27e3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 208 additions and 695 deletions

@ -44,6 +44,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
// Corresponding offsets are listed in g_27dirs
#define FRAMED_NEIGHBOR_COUNT 18
// Maps light index to corner direction
static const v3s16 light_dirs[8] = {
v3s16(-1, -1, -1),
v3s16(-1, -1, 1),
@ -55,8 +56,20 @@ static const v3s16 light_dirs[8] = {
v3s16( 1, 1, 1),
};
// Maps cuboid face and vertex indices to the corresponding light index
static const u8 light_indices[6][4] = {
{3, 7, 6, 2},
{0, 4, 5, 1},
{6, 7, 5, 4},
{3, 2, 0, 1},
{7, 3, 1, 5},
{2, 6, 4, 0},
};
// Standard index set to make a quad on 4 vertices
static constexpr u16 quad_indices[] = {0, 1, 2, 2, 3, 0};
static constexpr u16 quad_indices_02[] = {0, 1, 2, 2, 3, 0};
static constexpr u16 quad_indices_13[] = {0, 1, 3, 3, 1, 2};
static const auto &quad_indices = quad_indices_02;
const std::string MapblockMeshGenerator::raillike_groupname = "connect_to_raillike";
@ -140,82 +153,42 @@ void MapblockMeshGenerator::drawQuad(v3f *coords, const v3s16 &normal,
collector->append(tile, vertices, 4, quad_indices, 6);
}
// Create a cuboid.
// tiles - the tiles (materials) to use (for all 6 faces)
// tilecount - number of entries in tiles, 1<=tilecount<=6
// lights - vertex light levels. The order is the same as in light_dirs.
// NULL may be passed if smooth lighting is disabled.
// txc - texture coordinates - this is a list of texture coordinates
// for the opposite corners of each face - therefore, there
// 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, u8 mask)
{
assert(tilecount >= 1 && tilecount <= 6); // pre-condition
static std::array<video::S3DVertex, 24> setupCuboidVertices(const aabb3f &box, const f32 *txc, TileSpec *tiles, int tilecount) {
v3f min = box.MinEdge;
v3f max = box.MaxEdge;
video::SColor colors[6];
if (!data->m_smooth_lighting) {
for (int face = 0; face != 6; ++face) {
colors[face] = encode_light(light, f->light_source);
}
if (!f->light_source) {
applyFacesShading(colors[0], v3f(0, 1, 0));
applyFacesShading(colors[1], v3f(0, -1, 0));
applyFacesShading(colors[2], v3f(1, 0, 0));
applyFacesShading(colors[3], v3f(-1, 0, 0));
applyFacesShading(colors[4], v3f(0, 0, 1));
applyFacesShading(colors[5], v3f(0, 0, -1));
}
}
video::S3DVertex vertices[24] = {
std::array<video::S3DVertex, 24> vertices = {{
// top
video::S3DVertex(min.X, max.Y, max.Z, 0, 1, 0, colors[0], txc[0], txc[1]),
video::S3DVertex(max.X, max.Y, max.Z, 0, 1, 0, colors[0], txc[2], txc[1]),
video::S3DVertex(max.X, max.Y, min.Z, 0, 1, 0, colors[0], txc[2], txc[3]),
video::S3DVertex(min.X, max.Y, min.Z, 0, 1, 0, colors[0], txc[0], txc[3]),
video::S3DVertex(min.X, max.Y, max.Z, 0, 1, 0, {}, txc[0], txc[1]),
video::S3DVertex(max.X, max.Y, max.Z, 0, 1, 0, {}, txc[2], txc[1]),
video::S3DVertex(max.X, max.Y, min.Z, 0, 1, 0, {}, txc[2], txc[3]),
video::S3DVertex(min.X, max.Y, min.Z, 0, 1, 0, {}, txc[0], txc[3]),
// bottom
video::S3DVertex(min.X, min.Y, min.Z, 0, -1, 0, colors[1], txc[4], txc[5]),
video::S3DVertex(max.X, min.Y, min.Z, 0, -1, 0, colors[1], txc[6], txc[5]),
video::S3DVertex(max.X, min.Y, max.Z, 0, -1, 0, colors[1], txc[6], txc[7]),
video::S3DVertex(min.X, min.Y, max.Z, 0, -1, 0, colors[1], txc[4], txc[7]),
video::S3DVertex(min.X, min.Y, min.Z, 0, -1, 0, {}, txc[4], txc[5]),
video::S3DVertex(max.X, min.Y, min.Z, 0, -1, 0, {}, txc[6], txc[5]),
video::S3DVertex(max.X, min.Y, max.Z, 0, -1, 0, {}, txc[6], txc[7]),
video::S3DVertex(min.X, min.Y, max.Z, 0, -1, 0, {}, txc[4], txc[7]),
// right
video::S3DVertex(max.X, max.Y, min.Z, 1, 0, 0, colors[2], txc[ 8], txc[9]),
video::S3DVertex(max.X, max.Y, max.Z, 1, 0, 0, colors[2], txc[10], txc[9]),
video::S3DVertex(max.X, min.Y, max.Z, 1, 0, 0, colors[2], txc[10], txc[11]),
video::S3DVertex(max.X, min.Y, min.Z, 1, 0, 0, colors[2], txc[ 8], txc[11]),
video::S3DVertex(max.X, max.Y, min.Z, 1, 0, 0, {}, txc[ 8], txc[9]),
video::S3DVertex(max.X, max.Y, max.Z, 1, 0, 0, {}, txc[10], txc[9]),
video::S3DVertex(max.X, min.Y, max.Z, 1, 0, 0, {}, txc[10], txc[11]),
video::S3DVertex(max.X, min.Y, min.Z, 1, 0, 0, {}, txc[ 8], txc[11]),
// left
video::S3DVertex(min.X, max.Y, max.Z, -1, 0, 0, colors[3], txc[12], txc[13]),
video::S3DVertex(min.X, max.Y, min.Z, -1, 0, 0, colors[3], txc[14], txc[13]),
video::S3DVertex(min.X, min.Y, min.Z, -1, 0, 0, colors[3], txc[14], txc[15]),
video::S3DVertex(min.X, min.Y, max.Z, -1, 0, 0, colors[3], txc[12], txc[15]),
video::S3DVertex(min.X, max.Y, max.Z, -1, 0, 0, {}, txc[12], txc[13]),
video::S3DVertex(min.X, max.Y, min.Z, -1, 0, 0, {}, txc[14], txc[13]),
video::S3DVertex(min.X, min.Y, min.Z, -1, 0, 0, {}, txc[14], txc[15]),
video::S3DVertex(min.X, min.Y, max.Z, -1, 0, 0, {}, txc[12], txc[15]),
// back
video::S3DVertex(max.X, max.Y, max.Z, 0, 0, 1, colors[4], txc[16], txc[17]),
video::S3DVertex(min.X, max.Y, max.Z, 0, 0, 1, colors[4], txc[18], txc[17]),
video::S3DVertex(min.X, min.Y, max.Z, 0, 0, 1, colors[4], txc[18], txc[19]),
video::S3DVertex(max.X, min.Y, max.Z, 0, 0, 1, colors[4], txc[16], txc[19]),
video::S3DVertex(max.X, max.Y, max.Z, 0, 0, 1, {}, txc[16], txc[17]),
video::S3DVertex(min.X, max.Y, max.Z, 0, 0, 1, {}, txc[18], txc[17]),
video::S3DVertex(min.X, min.Y, max.Z, 0, 0, 1, {}, txc[18], txc[19]),
video::S3DVertex(max.X, min.Y, max.Z, 0, 0, 1, {}, txc[16], txc[19]),
// front
video::S3DVertex(min.X, max.Y, min.Z, 0, 0, -1, colors[5], txc[20], txc[21]),
video::S3DVertex(max.X, max.Y, min.Z, 0, 0, -1, colors[5], txc[22], txc[21]),
video::S3DVertex(max.X, min.Y, min.Z, 0, 0, -1, colors[5], txc[22], txc[23]),
video::S3DVertex(min.X, min.Y, min.Z, 0, 0, -1, colors[5], txc[20], txc[23]),
};
static const u8 light_indices[24] = {
3, 7, 6, 2,
0, 4, 5, 1,
6, 7, 5, 4,
3, 2, 0, 1,
7, 3, 1, 5,
2, 6, 4, 0
};
video::S3DVertex(min.X, max.Y, min.Z, 0, 0, -1, {}, txc[20], txc[21]),
video::S3DVertex(max.X, max.Y, min.Z, 0, 0, -1, {}, txc[22], txc[21]),
video::S3DVertex(max.X, min.Y, min.Z, 0, 0, -1, {}, txc[22], txc[23]),
video::S3DVertex(min.X, min.Y, min.Z, 0, 0, -1, {}, txc[20], txc[23]),
}};
for (int face = 0; face < 6; face++) {
int tileindex = MYMIN(face, tilecount - 1);
@ -263,23 +236,42 @@ void MapblockMeshGenerator::drawCuboid(const aabb3f &box,
}
}
if (data->m_smooth_lighting) {
for (int j = 0; j < 24; ++j) {
video::S3DVertex &vertex = vertices[j];
vertex.Color = encode_light(
lights[light_indices[j]].getPair(MYMAX(0.0f, vertex.Normal.Y)),
f->light_source);
if (!f->light_source)
applyFacesShading(vertex.Color, vertex.Normal);
}
return vertices;
}
// Add to mesh collector
enum class QuadDiagonal {
Diag02,
Diag13,
};
// Create a cuboid with custom lighting.
// tiles - the tiles (materials) to use (for all 6 faces)
// tilecount - number of entries in tiles, 1<=tilecount<=6
// txc - texture coordinates - this is a list of texture coordinates
// for the opposite corners of each face - therefore, there
// 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
// face_lighter(int face, video::S3DVertex vertices[4]) -> QuadDiagonal -
// a callback that will be called for each face drawn to setup vertex colors,
// and to choose diagonal to split the quad at.
template <typename Fn>
void MapblockMeshGenerator::drawCuboid(const aabb3f &box,
TileSpec *tiles, int tilecount, const f32 *txc, u8 mask, Fn &&face_lighter)
{
assert(tilecount >= 1 && tilecount <= 6); // pre-condition
auto vertices = setupCuboidVertices(box, txc, tiles, tilecount);
for (int k = 0; k < 6; ++k) {
if (mask & (1 << k))
continue;
QuadDiagonal diagonal = face_lighter(k, &vertices[4 * k]);
const u16 *indices = diagonal == QuadDiagonal::Diag13 ? quad_indices_13 : quad_indices_02;
int tileindex = MYMIN(k, tilecount - 1);
collector->append(tiles[tileindex], vertices + 4 * k, 4, quad_indices, 6);
collector->append(tiles[tileindex], &vertices[4 * k], 4, indices, 6);
}
}
@ -366,6 +358,11 @@ void MapblockMeshGenerator::generateCuboidTextureCoords(const aabb3f &box, f32 *
coords[i] = txc[i];
}
static inline int lightDiff(LightPair a, LightPair b)
{
return abs(a.lightDay - b.lightDay) + abs(a.lightNight - b.lightNight);
}
void MapblockMeshGenerator::drawAutoLightedCuboid(aabb3f box, const f32 *txc,
TileSpec *tiles, int tile_count, u8 mask)
{
@ -404,9 +401,124 @@ 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, mask);
drawCuboid(box, tiles, tile_count, txc, mask, [&] (int face, video::S3DVertex vertices[4]) {
LightPair final_lights[4];
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], f->light_source);
if (!f->light_source)
applyFacesShading(vertex.Color, vertex.Normal);
}
if (lightDiff(final_lights[1], final_lights[3]) < lightDiff(final_lights[0], final_lights[2]))
return QuadDiagonal::Diag13;
return QuadDiagonal::Diag02;
});
} else {
drawCuboid(box, tiles, tile_count, nullptr, txc, mask);
drawCuboid(box, tiles, tile_count, txc, mask, [&] (int face, video::S3DVertex vertices[4]) {
video::SColor color = encode_light(light, f->light_source);
if (!f->light_source)
applyFacesShading(color, vertices[0].Normal);
for (int j = 0; j < 4; j++) {
video::S3DVertex &vertex = vertices[j];
vertex.Color = color;
}
return QuadDiagonal::Diag02;
});
}
}
void MapblockMeshGenerator::drawSolidNode()
{
u8 faces = 0; // k-th bit will be set if k-th face is to be drawn.
static const v3s16 tile_dirs[6] = {
v3s16(0, 1, 0),
v3s16(0, -1, 0),
v3s16(1, 0, 0),
v3s16(-1, 0, 0),
v3s16(0, 0, 1),
v3s16(0, 0, -1)
};
TileSpec tiles[6];
u16 lights[6];
content_t n1 = n.getContent();
for (int face = 0; face < 6; face++) {
v3s16 p2 = blockpos_nodes + p + tile_dirs[face];
MapNode neighbor = data->m_vmanip.getNodeNoEx(p2);
content_t n2 = neighbor.getContent();
bool backface_culling = f->drawtype == NDT_NORMAL;
if (n2 == n1)
continue;
if (n2 == CONTENT_IGNORE)
continue;
if (n2 != CONTENT_AIR) {
const ContentFeatures &f2 = nodedef->get(n2);
if (f2.solidness == 2)
continue;
if (f->drawtype == NDT_LIQUID) {
if (n2 == nodedef->getId(f->liquid_alternative_flowing))
continue;
if (n2 == nodedef->getId(f->liquid_alternative_source))
continue;
backface_culling = f2.solidness >= 1;
}
}
faces |= 1 << face;
getTile(tile_dirs[face], &tiles[face]);
for (auto &layer : tiles[face].layers) {
if (backface_culling)
layer.material_flags |= MATERIAL_FLAG_BACKFACE_CULLING;
else
layer.material_flags &= ~MATERIAL_FLAG_BACKFACE_CULLING;
layer.material_flags |= MATERIAL_FLAG_TILEABLE_HORIZONTAL;
layer.material_flags |= MATERIAL_FLAG_TILEABLE_VERTICAL;
}
if (!data->m_smooth_lighting) {
lights[face] = getFaceLight(n, neighbor, nodedef);
}
}
if (!faces)
return;
u8 mask = faces ^ 0b0011'1111; // k-th bit is set if k-th face is to be *omitted*, as expected by cuboid drawing functions.
origin = intToFloat(p, BS);
auto box = aabb3f(v3f(-0.5 * BS), v3f(0.5 * BS));
f32 texture_coord_buf[24];
box.MinEdge += origin;
box.MaxEdge += origin;
generateCuboidTextureCoords(box, texture_coord_buf);
if (data->m_smooth_lighting) {
LightPair lights[6][4];
for (int face = 0; face < 6; ++face) {
for (int k = 0; k < 4; k++) {
v3s16 corner = light_dirs[light_indices[face][k]];
lights[face][k] = LightPair(getSmoothLightSolid(blockpos_nodes + p, tile_dirs[face], corner, data));
}
}
drawCuboid(box, tiles, 6, texture_coord_buf, mask, [&] (int face, video::S3DVertex vertices[4]) {
auto final_lights = lights[face];
for (int j = 0; j < 4; j++) {
video::S3DVertex &vertex = vertices[j];
vertex.Color = encode_light(final_lights[j], f->light_source);
if (!f->light_source)
applyFacesShading(vertex.Color, vertex.Normal);
}
if (lightDiff(final_lights[1], final_lights[3]) < lightDiff(final_lights[0], final_lights[2]))
return QuadDiagonal::Diag13;
return QuadDiagonal::Diag02;
});
} else {
drawCuboid(box, tiles, 6, texture_coord_buf, mask, [&] (int face, video::S3DVertex vertices[4]) {
video::SColor color = encode_light(lights[face], f->light_source);
if (!f->light_source)
applyFacesShading(color, vertices[0].Normal);
for (int j = 0; j < 4; j++) {
video::S3DVertex &vertex = vertices[j];
vertex.Color = color;
}
return QuadDiagonal::Diag02;
});
}
}
@ -1124,6 +1236,7 @@ void MapblockMeshGenerator::drawPlantlikeNode()
void MapblockMeshGenerator::drawPlantlikeRootedNode()
{
drawSolidNode();
useTile(0, MATERIAL_FLAG_CRACK_OVERLAY, 0, true);
origin += v3f(0.0, BS, 0.0);
p.Y++;
@ -1581,11 +1694,12 @@ void MapblockMeshGenerator::errorUnknownDrawtype()
void MapblockMeshGenerator::drawNode()
{
// skip some drawtypes early
switch (f->drawtype) {
case NDT_NORMAL: // Drawn by MapBlockMesh
case NDT_AIRLIKE: // Not drawn at all
case NDT_LIQUID: // Drawn by MapBlockMesh
return;
case NDT_LIQUID:
case NDT_NORMAL: // solid nodes dont need the usual setup
drawSolidNode();
return;
default:
break;

@ -99,11 +99,10 @@ public:
float vertical_tiling = 1.0);
// cuboid drawing!
void drawCuboid(const aabb3f &box, TileSpec *tiles, int tilecount,
const LightInfo *lights , const f32 *txc, u8 mask = 0);
template <typename Fn>
void drawCuboid(const aabb3f &box, TileSpec *tiles, int tilecount, const f32 *txc, u8 mask, Fn &&face_lighter);
void generateCuboidTextureCoords(aabb3f const &box, f32 *coords);
void drawAutoLightedCuboid(aabb3f box, const f32 *txc = NULL,
TileSpec *tiles = NULL, int tile_count = 0, u8 mask = 0);
void drawAutoLightedCuboid(aabb3f box, f32 const *txc = nullptr, TileSpec *tiles = nullptr, int tile_count = 0, u8 mask = 0);
u8 getNodeBoxMask(aabb3f box, u8 solid_neighbors, u8 sametype_neighbors) const;
// liquid-specific
@ -154,6 +153,7 @@ public:
float offset_h, float offset_v = 0.0);
// drawtypes
void drawSolidNode();
void drawLiquidNode();
void drawGlasslikeNode();
void drawGlasslikeFramedNode();

@ -106,8 +106,7 @@ u16 getInteriorLight(MapNode n, s32 increment, const NodeDefManager *ndef)
Calculate non-smooth lighting at face of node.
Single light bank.
*/
static u8 getFaceLight(enum LightBank bank, MapNode n, MapNode n2,
v3s16 face_dir, const NodeDefManager *ndef)
static u8 getFaceLight(enum LightBank bank, MapNode n, MapNode n2, const NodeDefManager *ndef)
{
ContentLightingFlags f1 = ndef->getLightingFlags(n);
ContentLightingFlags f2 = ndef->getLightingFlags(n2);
@ -132,11 +131,10 @@ static u8 getFaceLight(enum LightBank bank, MapNode n, MapNode n2,
Calculate non-smooth lighting at face of node.
Both light banks.
*/
u16 getFaceLight(MapNode n, MapNode n2, const v3s16 &face_dir,
const NodeDefManager *ndef)
u16 getFaceLight(MapNode n, MapNode n2, const NodeDefManager *ndef)
{
u16 day = getFaceLight(LIGHTBANK_DAY, n, n2, face_dir, ndef);
u16 night = getFaceLight(LIGHTBANK_NIGHT, n, n2, face_dir, ndef);
u16 day = getFaceLight(LIGHTBANK_DAY, n, n2, ndef);
u16 night = getFaceLight(LIGHTBANK_NIGHT, n, n2, ndef);
return day | (night << 8);
}
@ -355,316 +353,6 @@ static const v3s16 vertex_dirs_table[] = {
v3s16(-1, 1, 1), v3s16(-1, 1,-1)
};
/*
vertex_dirs: v3s16[4]
*/
static void getNodeVertexDirs(const v3s16 &dir, v3s16 *vertex_dirs)
{
/*
If looked from outside the node towards the face, the corners are:
0: bottom-right
1: bottom-left
2: top-left
3: top-right
*/
// Direction must be (1,0,0), (-1,0,0), (0,1,0), (0,-1,0),
// (0,0,1), (0,0,-1)
assert(dir.X * dir.X + dir.Y * dir.Y + dir.Z * dir.Z == 1);
// Convert direction to single integer for table lookup
u8 idx = (dir.X + 2 * dir.Y + 3 * dir.Z) & 7;
idx = (idx - 1) * 4;
#if defined(__GNUC__) && !defined(__clang__)
#pragma GCC diagnostic push
#if __GNUC__ > 7
#pragma GCC diagnostic ignored "-Wclass-memaccess"
#endif
#endif
memcpy(vertex_dirs, &vertex_dirs_table[idx], 4 * sizeof(v3s16));
#if defined(__GNUC__) && !defined(__clang__)
#pragma GCC diagnostic pop
#endif
}
static void getNodeTextureCoords(v3f base, const v3f &scale, const v3s16 &dir, float *u, float *v)
{
if (dir.X > 0 || dir.Y != 0 || dir.Z < 0)
base -= scale;
if (dir == v3s16(0,0,1)) {
*u = -base.X;
*v = -base.Y;
} else if (dir == v3s16(0,0,-1)) {
*u = base.X + 1;
*v = -base.Y - 1;
} else if (dir == v3s16(1,0,0)) {
*u = base.Z + 1;
*v = -base.Y - 1;
} else if (dir == v3s16(-1,0,0)) {
*u = -base.Z;
*v = -base.Y;
} else if (dir == v3s16(0,1,0)) {
*u = base.X + 1;
*v = -base.Z - 1;
} else if (dir == v3s16(0,-1,0)) {
*u = base.X + 1;
*v = base.Z + 1;
}
}
struct FastFace
{
TileSpec tile;
video::S3DVertex vertices[4]; // Precalculated vertices
/*!
* The face is divided into two triangles. If this is true,
* vertices 0 and 2 are connected, othervise vertices 1 and 3
* are connected.
*/
bool vertex_0_2_connected;
};
static void makeFastFace(const TileSpec &tile, u16 li0, u16 li1, u16 li2, u16 li3,
const v3f &tp, const v3f &p, const v3s16 &dir, const v3f &scale, std::vector<FastFace> &dest)
{
// Position is at the center of the cube.
v3f pos = p * BS;
float x0 = 0.0f;
float y0 = 0.0f;
float w = 1.0f;
float h = 1.0f;
v3f vertex_pos[4];
v3s16 vertex_dirs[4];
getNodeVertexDirs(dir, vertex_dirs);
if (tile.world_aligned)
getNodeTextureCoords(tp, scale, dir, &x0, &y0);
v3s16 t;
u16 t1;
switch (tile.rotation) {
case 0:
break;
case 1: //R90
t = vertex_dirs[0];
vertex_dirs[0] = vertex_dirs[3];
vertex_dirs[3] = vertex_dirs[2];
vertex_dirs[2] = vertex_dirs[1];
vertex_dirs[1] = t;
t1 = li0;
li0 = li3;
li3 = li2;
li2 = li1;
li1 = t1;
break;
case 2: //R180
t = vertex_dirs[0];
vertex_dirs[0] = vertex_dirs[2];
vertex_dirs[2] = t;
t = vertex_dirs[1];
vertex_dirs[1] = vertex_dirs[3];
vertex_dirs[3] = t;
t1 = li0;
li0 = li2;
li2 = t1;
t1 = li1;
li1 = li3;
li3 = t1;
break;
case 3: //R270
t = vertex_dirs[0];
vertex_dirs[0] = vertex_dirs[1];
vertex_dirs[1] = vertex_dirs[2];
vertex_dirs[2] = vertex_dirs[3];
vertex_dirs[3] = t;
t1 = li0;
li0 = li1;
li1 = li2;
li2 = li3;
li3 = t1;
break;
case 4: //FXR90
t = vertex_dirs[0];
vertex_dirs[0] = vertex_dirs[3];
vertex_dirs[3] = vertex_dirs[2];
vertex_dirs[2] = vertex_dirs[1];
vertex_dirs[1] = t;
t1 = li0;
li0 = li3;
li3 = li2;
li2 = li1;
li1 = t1;
y0 += h;
h *= -1;
break;
case 5: //FXR270
t = vertex_dirs[0];
vertex_dirs[0] = vertex_dirs[1];
vertex_dirs[1] = vertex_dirs[2];
vertex_dirs[2] = vertex_dirs[3];
vertex_dirs[3] = t;
t1 = li0;
li0 = li1;
li1 = li2;
li2 = li3;
li3 = t1;
y0 += h;
h *= -1;
break;
case 6: //FYR90
t = vertex_dirs[0];
vertex_dirs[0] = vertex_dirs[3];
vertex_dirs[3] = vertex_dirs[2];
vertex_dirs[2] = vertex_dirs[1];
vertex_dirs[1] = t;
t1 = li0;
li0 = li3;
li3 = li2;
li2 = li1;
li1 = t1;
x0 += w;
w *= -1;
break;
case 7: //FYR270
t = vertex_dirs[0];
vertex_dirs[0] = vertex_dirs[1];
vertex_dirs[1] = vertex_dirs[2];
vertex_dirs[2] = vertex_dirs[3];
vertex_dirs[3] = t;
t1 = li0;
li0 = li1;
li1 = li2;
li2 = li3;
li3 = t1;
x0 += w;
w *= -1;
break;
case 8: //FX
y0 += h;
h *= -1;
break;
case 9: //FY
x0 += w;
w *= -1;
break;
default:
break;
}
for (u16 i = 0; i < 4; i++) {
vertex_pos[i] = v3f(
BS / 2 * vertex_dirs[i].X,
BS / 2 * vertex_dirs[i].Y,
BS / 2 * vertex_dirs[i].Z
);
}
for (v3f &vpos : vertex_pos) {
vpos.X *= scale.X;
vpos.Y *= scale.Y;
vpos.Z *= scale.Z;
vpos += pos;
}
f32 abs_scale = 1.0f;
if (scale.X < 0.999f || scale.X > 1.001f) abs_scale = scale.X;
else if (scale.Y < 0.999f || scale.Y > 1.001f) abs_scale = scale.Y;
else if (scale.Z < 0.999f || scale.Z > 1.001f) abs_scale = scale.Z;
v3f normal(dir.X, dir.Y, dir.Z);
u16 li[4] = { li0, li1, li2, li3 };
u16 day[4];
u16 night[4];
for (u8 i = 0; i < 4; i++) {
day[i] = li[i] >> 8;
night[i] = li[i] & 0xFF;
}
bool vertex_0_2_connected = abs(day[0] - day[2]) + abs(night[0] - night[2])
< abs(day[1] - day[3]) + abs(night[1] - night[3]);
v2f32 f[4] = {
core::vector2d<f32>(x0 + w * abs_scale, y0 + h),
core::vector2d<f32>(x0, y0 + h),
core::vector2d<f32>(x0, y0),
core::vector2d<f32>(x0 + w * abs_scale, y0) };
// equivalent to dest.push_back(FastFace()) but faster
dest.emplace_back();
FastFace& face = *dest.rbegin();
for (u8 i = 0; i < 4; i++) {
video::SColor c = encode_light(li[i], tile.emissive_light);
if (!tile.emissive_light)
applyFacesShading(c, normal);
face.vertices[i] = video::S3DVertex(vertex_pos[i], normal, c, f[i]);
}
/*
Revert triangles for nicer looking gradient if the
brightness of vertices 1 and 3 differ less than
the brightness of vertices 0 and 2.
*/
face.vertex_0_2_connected = vertex_0_2_connected;
face.tile = tile;
}
/*
Nodes make a face if contents differ and solidness differs.
Return value:
0: No face
1: Face uses m1's content
2: Face uses m2's content
equivalent: Whether the blocks share the same face (eg. water and glass)
TODO: Add 3: Both faces drawn with backface culling, remove equivalent
*/
static u8 face_contents(content_t m1, content_t m2, bool *equivalent,
const NodeDefManager *ndef)
{
*equivalent = false;
if (m1 == m2 || m1 == CONTENT_IGNORE || m2 == CONTENT_IGNORE)
return 0;
const ContentFeatures &f1 = ndef->get(m1);
const ContentFeatures &f2 = ndef->get(m2);
// Contents don't differ for different forms of same liquid
if (f1.sameLiquidRender(f2))
return 0;
u8 c1 = f1.solidness;
u8 c2 = f2.solidness;
if (c1 == c2)
return 0;
if (c1 == 0)
c1 = f1.visual_solidness;
else if (c2 == 0)
c2 = f2.visual_solidness;
if (c1 == c2) {
*equivalent = true;
// If same solidness, liquid takes precense
if (f1.isLiquidRender())
return 1;
if (f2.isLiquidRender())
return 2;
}
if (c1 > c2)
return 1;
return 2;
}
/*
Gets nth node tile (0 <= n <= 5).
*/
@ -749,232 +437,6 @@ void getNodeTile(MapNode mn, const v3s16 &p, const v3s16 &dir, MeshMakeData *dat
tile.rotation = tile.world_aligned ? 0 : dir_to_tile[tile_index + 1];
}
static void getTileInfo(
// Input:
MeshMakeData *data,
const v3s16 &p,
const v3s16 &face_dir,
// Output:
bool &makes_face,
v3s16 &p_corrected,
v3s16 &face_dir_corrected,
u16 *lights,
u8 &waving,
TileSpec &tile
)
{
VoxelManipulator &vmanip = data->m_vmanip;
const NodeDefManager *ndef = data->m_client->ndef();
v3s16 blockpos_nodes = data->m_blockpos * MAP_BLOCKSIZE;
const MapNode &n0 = vmanip.getNodeRefUnsafe(blockpos_nodes + p);
// Don't even try to get n1 if n0 is already CONTENT_IGNORE
if (n0.getContent() == CONTENT_IGNORE) {
makes_face = false;
return;
}
const MapNode &n1 = vmanip.getNodeRefUnsafeCheckFlags(blockpos_nodes + p + face_dir);
if (n1.getContent() == CONTENT_IGNORE) {
makes_face = false;
return;
}
// This is hackish
bool equivalent = false;
u8 mf = face_contents(n0.getContent(), n1.getContent(),
&equivalent, ndef);
if (mf == 0) {
makes_face = false;
return;
}
makes_face = true;
MapNode n = n0;
if (mf == 1) {
p_corrected = p;
face_dir_corrected = face_dir;
} else {
n = n1;
p_corrected = p + face_dir;
face_dir_corrected = -face_dir;
}
getNodeTile(n, p_corrected, face_dir_corrected, data, tile);
const ContentFeatures &f = ndef->get(n);
waving = f.waving;
tile.emissive_light = f.light_source;
// eg. water and glass
if (equivalent) {
for (TileLayer &layer : tile.layers)
layer.material_flags |= MATERIAL_FLAG_BACKFACE_CULLING;
}
if (!data->m_smooth_lighting) {
lights[0] = lights[1] = lights[2] = lights[3] =
getFaceLight(n0, n1, face_dir, ndef);
} else {
v3s16 vertex_dirs[4];
getNodeVertexDirs(face_dir_corrected, vertex_dirs);
v3s16 light_p = blockpos_nodes + p_corrected;
for (u16 i = 0; i < 4; i++)
lights[i] = getSmoothLightSolid(light_p, face_dir_corrected, vertex_dirs[i], data);
}
}
/*
startpos:
translate_dir: unit vector with only one of x, y or z
face_dir: unit vector with only one of x, y or z
*/
static void updateFastFaceRow(
MeshMakeData *data,
const v3s16 &&startpos,
v3s16 translate_dir,
const v3f &&translate_dir_f,
const v3s16 &&face_dir,
std::vector<FastFace> &dest)
{
static thread_local const bool waving_liquids =
g_settings->getBool("enable_shaders") &&
g_settings->getBool("enable_waving_water");
static thread_local const bool force_not_tiling =
g_settings->getBool("enable_dynamic_shadows");
v3s16 p = startpos;
u16 continuous_tiles_count = 1;
bool makes_face = false;
v3s16 p_corrected;
v3s16 face_dir_corrected;
u16 lights[4] = {0, 0, 0, 0};
u8 waving = 0;
TileSpec tile;
// Get info of first tile
getTileInfo(data, p, face_dir,
makes_face, p_corrected, face_dir_corrected,
lights, waving, tile);
// Unroll this variable which has a significant build cost
TileSpec next_tile;
for (u16 j = 0; j < data->side_length; j++) {
// If tiling can be done, this is set to false in the next step
bool next_is_different = true;
bool next_makes_face = false;
v3s16 next_p_corrected;
v3s16 next_face_dir_corrected;
u16 next_lights[4] = {0, 0, 0, 0};
// If at last position, there is nothing to compare to and
// the face must be drawn anyway
if (j != data->side_length - 1) {
p += translate_dir;
getTileInfo(data, p, face_dir,
next_makes_face, next_p_corrected,
next_face_dir_corrected, next_lights,
waving,
next_tile);
if (!force_not_tiling
&& next_makes_face == makes_face
&& next_p_corrected == p_corrected + translate_dir
&& next_face_dir_corrected == face_dir_corrected
&& memcmp(next_lights, lights, sizeof(lights)) == 0
// Don't apply fast faces to waving water.
&& (waving != 3 || !waving_liquids)
&& next_tile.isTileable(tile)) {
next_is_different = false;
continuous_tiles_count++;
}
}
if (next_is_different) {
/*
Create a face if there should be one
*/
if (makes_face) {
// Floating point conversion of the position vector
v3f pf(p_corrected.X, p_corrected.Y, p_corrected.Z);
// Center point of face (kind of)
v3f sp = pf - ((f32)continuous_tiles_count * 0.5f - 0.5f)
* translate_dir_f;
v3f scale(1, 1, 1);
if (translate_dir.X != 0)
scale.X = continuous_tiles_count;
if (translate_dir.Y != 0)
scale.Y = continuous_tiles_count;
if (translate_dir.Z != 0)
scale.Z = continuous_tiles_count;
makeFastFace(tile, lights[0], lights[1], lights[2], lights[3],
pf, sp, face_dir_corrected, scale, dest);
g_profiler->avg("Meshgen: Tiles per face [#]", continuous_tiles_count);
}
continuous_tiles_count = 1;
}
makes_face = next_makes_face;
p_corrected = next_p_corrected;
face_dir_corrected = next_face_dir_corrected;
memcpy(lights, next_lights, sizeof(lights));
if (next_is_different)
tile = std::move(next_tile); // faster than copy
}
}
static void updateAllFastFaceRows(MeshMakeData *data,
std::vector<FastFace> &dest)
{
/*
Go through every y,z and get top(y+) faces in rows of x+
*/
for (s16 y = 0; y < data->side_length; y++)
for (s16 z = 0; z < data->side_length; z++)
updateFastFaceRow(data,
v3s16(0, y, z),
v3s16(1, 0, 0), //dir
v3f (1, 0, 0),
v3s16(0, 1, 0), //face dir
dest);
/*
Go through every x,y and get right(x+) faces in rows of z+
*/
for (s16 x = 0; x < data->side_length; x++)
for (s16 y = 0; y < data->side_length; y++)
updateFastFaceRow(data,
v3s16(x, y, 0),
v3s16(0, 0, 1), //dir
v3f (0, 0, 1),
v3s16(1, 0, 0), //face dir
dest);
/*
Go through every y,z and get back(z+) faces in rows of x+
*/
for (s16 z = 0; z < data->side_length; z++)
for (s16 y = 0; y < data->side_length; y++)
updateFastFaceRow(data,
v3s16(0, y, z),
v3s16(1, 0, 0), //dir
v3f (1, 0, 0),
v3s16(0, 0, 1), //face dir
dest);
}
static void applyTileColor(PreMeshBuffer &pmb)
{
video::SColor tc = pmb.layer.color;
@ -1198,48 +660,8 @@ MapBlockMesh::MapBlockMesh(MeshMakeData *data, v3s16 camera_offset):
}
}
// 4-21ms for MAP_BLOCKSIZE=16 (NOTE: probably outdated)
// 24-155ms for MAP_BLOCKSIZE=32 (NOTE: probably outdated)
//TimeTaker timer1("MapBlockMesh()");
std::vector<FastFace> fastfaces_new;
fastfaces_new.reserve(512);
/*
We are including the faces of the trailing edges of the block.
This means that when something changes, the caller must
also update the meshes of the blocks at the leading edges.
NOTE: This is the slowest part of this method.
*/
{
// 4-23ms for MAP_BLOCKSIZE=16 (NOTE: probably outdated)
//TimeTaker timer2("updateAllFastFaceRows()");
updateAllFastFaceRows(data, fastfaces_new);
}
// End of slow part
/*
Convert FastFaces to MeshCollector
*/
v3f offset = intToFloat((data->m_blockpos - data->m_mesh_grid.getMeshPos(data->m_blockpos)) * MAP_BLOCKSIZE, BS);
MeshCollector collector(m_bounding_sphere_center, offset);
{
// avg 0ms (100ms spikes when loading textures the first time)
// (NOTE: probably outdated)
//TimeTaker timer2("MeshCollector building");
for (const FastFace &f : fastfaces_new) {
static const u16 indices[] = {0, 1, 2, 2, 3, 0};
static const u16 indices_alternate[] = {0, 1, 3, 2, 3, 1};
const u16 *indices_p =
f.vertex_0_2_connected ? indices : indices_alternate;
collector.append(f.tile, f.vertices, 4, indices_p, 6);
}
}
/*
Add special graphics:
- torches

@ -299,8 +299,7 @@ video::SColor encode_light(u16 light, u8 emissive_light);
// Compute light at node
u16 getInteriorLight(MapNode n, s32 increment, const NodeDefManager *ndef);
u16 getFaceLight(MapNode n, MapNode n2, const v3s16 &face_dir,
const NodeDefManager *ndef);
u16 getFaceLight(MapNode n, MapNode n2, const NodeDefManager *ndef);
u16 getSmoothLightSolid(const v3s16 &p, const v3s16 &face_dir, const v3s16 &corner, MeshMakeData *data);
u16 getSmoothLightTransparent(const v3s16 &p, const v3s16 &corner, MeshMakeData *data);

@ -254,12 +254,6 @@ struct TileLayer
}
}
bool isTileable() const
{
return (material_flags & MATERIAL_FLAG_TILEABLE_HORIZONTAL)
&& (material_flags & MATERIAL_FLAG_TILEABLE_VERTICAL);
}
bool isTransparent() const
{
switch (material_type) {
@ -312,22 +306,6 @@ struct TileSpec
{
TileSpec() = default;
/*!
* Returns true if this tile can be merged with the other tile.
*/
bool isTileable(const TileSpec &other) const {
for (int layer = 0; layer < MAX_TILE_LAYERS; layer++) {
if (layers[layer] != other.layers[layer])
return false;
// Only non-transparent tiles can be merged into fast faces
if (layers[layer].isTransparent() || !layers[layer].isTileable())
return false;
}
return rotation == 0
&& rotation == other.rotation
&& emissive_light == other.emissive_light;
}
//! If true, the tile rotation is ignored.
bool world_aligned = false;
//! Tile rotation.