]> git.lizzy.rs Git - dragonfireclient.git/blobdiff - src/client/mapblock_mesh.cpp
Merge branch 'master' of https://github.com/minetest/minetest
[dragonfireclient.git] / src / client / mapblock_mesh.cpp
index c165438a097b622a3a324f52bec823e3f1c4459f..f0d43ec7e2f12dac890382faa75ba1d2a61ac035 100644 (file)
@@ -30,16 +30,15 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "client/meshgen/collector.h"
 #include "client/renderingengine.h"
 #include <array>
+#include <algorithm>
 
 /*
        MeshMakeData
 */
 
-MeshMakeData::MeshMakeData(Client *client, bool use_shaders,
-               bool use_tangent_vertices):
+MeshMakeData::MeshMakeData(Client *client, bool use_shaders):
        m_client(client),
-       m_use_shaders(use_shaders),
-       m_use_tangent_vertices(use_tangent_vertices)
+       m_use_shaders(use_shaders)
 {}
 
 void MeshMakeData::fillBlockDataBegin(const v3s16 &blockpos)
@@ -81,33 +80,6 @@ void MeshMakeData::fill(MapBlock *block)
        }
 }
 
-void MeshMakeData::fillSingleNode(MapNode *node)
-{
-       m_blockpos = v3s16(0,0,0);
-
-       v3s16 blockpos_nodes = v3s16(0,0,0);
-       VoxelArea area(blockpos_nodes-v3s16(1,1,1)*MAP_BLOCKSIZE,
-                       blockpos_nodes+v3s16(1,1,1)*MAP_BLOCKSIZE*2-v3s16(1,1,1));
-       s32 volume = area.getVolume();
-       s32 our_node_index = area.index(1,1,1);
-
-       // Allocate this block + neighbors
-       m_vmanip.clear();
-       m_vmanip.addArea(area);
-
-       // Fill in data
-       MapNode *data = new MapNode[volume];
-       for(s32 i = 0; i < volume; i++)
-       {
-               if (i == our_node_index)
-                       data[i] = *node;
-               else
-                       data[i] = MapNode(CONTENT_AIR, LIGHT_MAX, 0);
-       }
-       m_vmanip.copyFrom(data, area, area.MinEdge, area.MinEdge, area.getExtent());
-       delete[] data;
-}
-
 void MeshMakeData::setCrack(int crack_level, v3s16 crack_pos)
 {
        if (crack_level >= 0)
@@ -116,7 +88,7 @@ void MeshMakeData::setCrack(int crack_level, v3s16 crack_pos)
 
 void MeshMakeData::setSmoothLighting(bool smooth_lighting)
 {
-       m_smooth_lighting = smooth_lighting;
+       m_smooth_lighting = smooth_lighting && ! g_settings->getBool("fullbright");
 }
 
 /*
@@ -228,7 +200,7 @@ static u16 getSmoothLightCombined(const v3s16 &p,
                return f.light_propagates;
        };
 
-       std::array<bool, 4> obstructed = {{ 1, 1, 1, 1 }};
+       bool obstructed[4] = { true, true, true, true };
        add_node(0);
        bool opaque1 = !add_node(1);
        bool opaque2 = !add_node(2);
@@ -375,6 +347,32 @@ void final_color_blend(video::SColor *result,
        Mesh generation helpers
 */
 
+// This table is moved outside getNodeVertexDirs to avoid the compiler using
+// a mutex to initialize this table at runtime right in the hot path.
+// For details search the internet for "cxa_guard_acquire".
+static const v3s16 vertex_dirs_table[] = {
+       // ( 1, 0, 0)
+       v3s16( 1,-1, 1), v3s16( 1,-1,-1),
+       v3s16( 1, 1,-1), v3s16( 1, 1, 1),
+       // ( 0, 1, 0)
+       v3s16( 1, 1,-1), v3s16(-1, 1,-1),
+       v3s16(-1, 1, 1), v3s16( 1, 1, 1),
+       // ( 0, 0, 1)
+       v3s16(-1,-1, 1), v3s16( 1,-1, 1),
+       v3s16( 1, 1, 1), v3s16(-1, 1, 1),
+       // invalid
+       v3s16(), v3s16(), v3s16(), v3s16(),
+       // ( 0, 0,-1)
+       v3s16( 1,-1,-1), v3s16(-1,-1,-1),
+       v3s16(-1, 1,-1), v3s16( 1, 1,-1),
+       // ( 0,-1, 0)
+       v3s16( 1,-1, 1), v3s16(-1,-1, 1),
+       v3s16(-1,-1,-1), v3s16( 1,-1,-1),
+       // (-1, 0, 0)
+       v3s16(-1,-1,-1), v3s16(-1,-1, 1),
+       v3s16(-1, 1, 1), v3s16(-1, 1,-1)
+};
+
 /*
        vertex_dirs: v3s16[4]
 */
@@ -387,68 +385,49 @@ static void getNodeVertexDirs(const v3s16 &dir, v3s16 *vertex_dirs)
                2: top-left
                3: top-right
        */
-       if (dir == v3s16(0, 0, 1)) {
-               // If looking towards z+, this is the face that is behind
-               // the center point, facing towards z+.
-               vertex_dirs[0] = v3s16(-1,-1, 1);
-               vertex_dirs[1] = v3s16( 1,-1, 1);
-               vertex_dirs[2] = v3s16( 1, 1, 1);
-               vertex_dirs[3] = v3s16(-1, 1, 1);
-       } else if (dir == v3s16(0, 0, -1)) {
-               // faces towards Z-
-               vertex_dirs[0] = v3s16( 1,-1,-1);
-               vertex_dirs[1] = v3s16(-1,-1,-1);
-               vertex_dirs[2] = v3s16(-1, 1,-1);
-               vertex_dirs[3] = v3s16( 1, 1,-1);
-       } else if (dir == v3s16(1, 0, 0)) {
-               // faces towards X+
-               vertex_dirs[0] = v3s16( 1,-1, 1);
-               vertex_dirs[1] = v3s16( 1,-1,-1);
-               vertex_dirs[2] = v3s16( 1, 1,-1);
-               vertex_dirs[3] = v3s16( 1, 1, 1);
-       } else if (dir == v3s16(-1, 0, 0)) {
-               // faces towards X-
-               vertex_dirs[0] = v3s16(-1,-1,-1);
-               vertex_dirs[1] = v3s16(-1,-1, 1);
-               vertex_dirs[2] = v3s16(-1, 1, 1);
-               vertex_dirs[3] = v3s16(-1, 1,-1);
-       } else if (dir == v3s16(0, 1, 0)) {
-               // faces towards Y+ (assume Z- as "down" in texture)
-               vertex_dirs[0] = v3s16( 1, 1,-1);
-               vertex_dirs[1] = v3s16(-1, 1,-1);
-               vertex_dirs[2] = v3s16(-1, 1, 1);
-               vertex_dirs[3] = v3s16( 1, 1, 1);
-       } else if (dir == v3s16(0, -1, 0)) {
-               // faces towards Y- (assume Z+ as "down" in texture)
-               vertex_dirs[0] = v3s16( 1,-1, 1);
-               vertex_dirs[1] = v3s16(-1,-1, 1);
-               vertex_dirs[2] = v3s16(-1,-1,-1);
-               vertex_dirs[3] = v3s16( 1,-1,-1);
-       }
+
+       // 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)
+       if (dir.X > 0 || dir.Y != 0 || dir.Z < 0)
                base -= scale;
        if (dir == v3s16(0,0,1)) {
-               *u = -base.X - 1;
-               *v = -base.Y - 1;
+               *u = -base.X;
+               *v = -base.Y;
        } else if (dir == v3s16(0,0,-1)) {
                *u = base.X + 1;
-               *v = -base.Y - 2;
+               *v = -base.Y - 1;
        } else if (dir == v3s16(1,0,0)) {
                *u = base.Z + 1;
-               *v = -base.Y - 2;
-       } 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 - 2;
+               *v = -base.Z - 1;
        } else if (dir == v3s16(0,-1,0)) {
-               *u = base.X;
-               *v = base.Z;
+               *u = base.X + 1;
+               *v = base.Z + 1;
        }
 }
 
@@ -681,6 +660,7 @@ static u8 face_contents(content_t m1, content_t m2, bool *equivalent,
        u8 c1 = f1.solidness;
        u8 c2 = f2.solidness;
 
+
        if (c1 == c2)
                return 0;
 
@@ -689,6 +669,7 @@ static u8 face_contents(content_t m1, content_t m2, bool *equivalent,
        else if (c2 == 0)
                c2 = f2.visual_solidness;
 
+
        if (c1 == c2) {
                *equivalent = true;
                // If same solidness, liquid takes precense
@@ -788,6 +769,24 @@ void getNodeTile(MapNode mn, const v3s16 &p, const v3s16 &dir, MeshMakeData *dat
        tile.rotation = tile.world_aligned ? 0 : dir_to_tile[tile_index + 1];
 }
 
+std::set<content_t> splitToContentT(std::string str, const NodeDefManager *ndef)
+{
+       str += "\n";
+       std::set<content_t> dat;
+       std::string buf;
+       for (char c : str) {
+               if (c == ',' || c == '\n') {
+                       if (! buf.empty()) {
+                               dat.insert(ndef->getId(buf));
+                       }
+                       buf.clear();
+               } else if (c != ' ') {
+                       buf += c;
+               }
+       }
+       return dat;
+}
+
 static void getTileInfo(
                // Input:
                MeshMakeData *data,
@@ -799,8 +798,10 @@ static void getTileInfo(
                v3s16 &face_dir_corrected,
                u16 *lights,
                u8 &waving,
-               TileSpec &tile
-       )
+               TileSpec &tile,
+               // lol more Input
+               bool xray,
+               std::set<content_t> xraySet)
 {
        VoxelManipulator &vmanip = data->m_vmanip;
        const NodeDefManager *ndef = data->m_client->ndef();
@@ -808,22 +809,29 @@ static void getTileInfo(
 
        const MapNode &n0 = vmanip.getNodeRefUnsafe(blockpos_nodes + p);
 
+       content_t c0 = n0.getContent();
+       if (xray && xraySet.find(c0) != xraySet.end())
+               c0 = CONTENT_AIR;
        // Don't even try to get n1 if n0 is already CONTENT_IGNORE
-       if (n0.getContent() == CONTENT_IGNORE) {
+       if (c0 == CONTENT_IGNORE) {
                makes_face = false;
                return;
        }
 
        const MapNode &n1 = vmanip.getNodeRefUnsafeCheckFlags(blockpos_nodes + p + face_dir);
 
-       if (n1.getContent() == CONTENT_IGNORE) {
+       content_t c1 = n1.getContent();
+       if (xray && xraySet.find(c1) != xraySet.end())
+               c1 = CONTENT_AIR;
+
+       if (c1 == CONTENT_IGNORE) {
                makes_face = false;
                return;
        }
 
        // This is hackish
        bool equivalent = false;
-       u8 mf = face_contents(n0.getContent(), n1.getContent(),
+       u8 mf = face_contents(c0, c1,
                        &equivalent, ndef);
 
        if (mf == 0) {
@@ -879,12 +887,17 @@ static void updateFastFaceRow(
                v3s16 translate_dir,
                const v3f &&translate_dir_f,
                const v3s16 &&face_dir,
-               std::vector<FastFace> &dest)
+               std::vector<FastFace> &dest,
+               bool xray,
+               std::set<content_t> xraySet)
 {
        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;
@@ -893,11 +906,13 @@ static void updateFastFaceRow(
        v3s16 p_corrected;
        v3s16 face_dir_corrected;
        u16 lights[4] = {0, 0, 0, 0};
-       u8 waving;
+       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);
+                       lights, waving, tile, xray, xraySet);
 
        // Unroll this variable which has a significant build cost
        TileSpec next_tile;
@@ -905,8 +920,6 @@ static void updateFastFaceRow(
                // If tiling can be done, this is set to false in the next step
                bool next_is_different = true;
 
-               v3s16 p_next;
-
                bool next_makes_face = false;
                v3s16 next_p_corrected;
                v3s16 next_face_dir_corrected;
@@ -915,18 +928,21 @@ static void updateFastFaceRow(
                // If at last position, there is nothing to compare to and
                // the face must be drawn anyway
                if (j != MAP_BLOCKSIZE - 1) {
-                       p_next = p + translate_dir;
+                       p += translate_dir;
 
-                       getTileInfo(data, p_next, face_dir,
+                       getTileInfo(data, p, face_dir,
                                        next_makes_face, next_p_corrected,
                                        next_face_dir_corrected, next_lights,
                                        waving,
-                                       next_tile);
+                                       next_tile,
+                                       xray,
+                                       xraySet);
 
-                       if (next_makes_face == makes_face
+                       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, ARRLEN(lights) * sizeof(u16)) == 0
+                                       && memcmp(next_lights, lights, sizeof(lights)) == 0
                                        // Don't apply fast faces to waving water.
                                        && (waving != 3 || !waving_liquids)
                                        && next_tile.isTileable(tile)) {
@@ -964,15 +980,14 @@ static void updateFastFaceRow(
                makes_face = next_makes_face;
                p_corrected = next_p_corrected;
                face_dir_corrected = next_face_dir_corrected;
-               std::memcpy(lights, next_lights, ARRLEN(lights) * sizeof(u16));
+               memcpy(lights, next_lights, sizeof(lights));
                if (next_is_different)
-                       tile = next_tile;
-               p = p_next;
+                       tile = std::move(next_tile); // faster than copy
        }
 }
 
 static void updateAllFastFaceRows(MeshMakeData *data,
-               std::vector<FastFace> &dest)
+               std::vector<FastFace> &dest, bool xray, std::set<content_t> xraySet)
 {
        /*
                Go through every y,z and get top(y+) faces in rows of x+
@@ -984,7 +999,9 @@ static void updateAllFastFaceRows(MeshMakeData *data,
                                v3s16(1, 0, 0), //dir
                                v3f  (1, 0, 0),
                                v3s16(0, 1, 0), //face dir
-                               dest);
+                               dest,
+                               xray,
+                               xraySet);
 
        /*
                Go through every x,y and get right(x+) faces in rows of z+
@@ -996,7 +1013,9 @@ static void updateAllFastFaceRows(MeshMakeData *data,
                                v3s16(0, 0, 1), //dir
                                v3f  (0, 0, 1),
                                v3s16(1, 0, 0), //face dir
-                               dest);
+                               dest,
+                               xray,
+                               xraySet);
 
        /*
                Go through every y,z and get back(z+) faces in rows of x+
@@ -1008,7 +1027,9 @@ static void updateAllFastFaceRows(MeshMakeData *data,
                                v3s16(1, 0, 0), //dir
                                v3f  (1, 0, 0),
                                v3s16(0, 0, 1), //face dir
-                               dest);
+                               dest,
+                               xray,
+                               xraySet);
 }
 
 static void applyTileColor(PreMeshBuffer &pmb)
@@ -1025,6 +1046,173 @@ static void applyTileColor(PreMeshBuffer &pmb)
        }
 }
 
+/*
+       MapBlockBspTree
+*/
+
+void MapBlockBspTree::buildTree(const std::vector<MeshTriangle> *triangles)
+{
+       this->triangles = triangles;
+
+       nodes.clear();
+
+       // assert that triangle index can fit into s32
+       assert(triangles->size() <= 0x7FFFFFFFL);
+       std::vector<s32> indexes;
+       indexes.reserve(triangles->size());
+       for (u32 i = 0; i < triangles->size(); i++)
+               indexes.push_back(i);
+
+       root = buildTree(v3f(1, 0, 0), v3f(85, 85, 85), 40, indexes, 0);
+}
+
+/**
+ * @brief Find a candidate plane to split a set of triangles in two
+ * 
+ * The candidate plane is represented by one of the triangles from the set.
+ * 
+ * @param list Vector of indexes of the triangles in the set
+ * @param triangles Vector of all triangles in the BSP tree
+ * @return Address of the triangle that represents the proposed split plane
+ */
+static const MeshTriangle *findSplitCandidate(const std::vector<s32> &list, const std::vector<MeshTriangle> &triangles)
+{
+       // find the center of the cluster.
+       v3f center(0, 0, 0);
+       size_t n = list.size();
+       for (s32 i : list) {
+               center += triangles[i].centroid / n;
+       }
+
+       // find the triangle with the largest area and closest to the center
+       const MeshTriangle *candidate_triangle = &triangles[list[0]];
+       const MeshTriangle *ith_triangle;
+       for (s32 i : list) {
+               ith_triangle = &triangles[i];
+               if (ith_triangle->areaSQ > candidate_triangle->areaSQ ||
+                               (ith_triangle->areaSQ == candidate_triangle->areaSQ &&
+                               ith_triangle->centroid.getDistanceFromSQ(center) < candidate_triangle->centroid.getDistanceFromSQ(center))) {
+                       candidate_triangle = ith_triangle;
+               }
+       }
+       return candidate_triangle;
+}
+
+s32 MapBlockBspTree::buildTree(v3f normal, v3f origin, float delta, const std::vector<s32> &list, u32 depth)
+{
+       // if the list is empty, don't bother
+       if (list.empty())
+               return -1;
+
+       // if there is only one triangle, or the delta is insanely small, this is a leaf node
+       if (list.size() == 1 || delta < 0.01) {
+               nodes.emplace_back(normal, origin, list, -1, -1);
+               return nodes.size() - 1;
+       }
+
+       std::vector<s32> front_list;
+       std::vector<s32> back_list;
+       std::vector<s32> node_list;
+
+       // split the list
+       for (s32 i : list) {
+               const MeshTriangle &triangle = (*triangles)[i];
+               float factor = normal.dotProduct(triangle.centroid - origin);
+               if (factor == 0)
+                       node_list.push_back(i);
+               else if (factor > 0)
+                       front_list.push_back(i);
+               else
+                       back_list.push_back(i);
+       }
+
+       // define the new split-plane
+       v3f candidate_normal(normal.Z, normal.X, normal.Y);
+       float candidate_delta = delta;
+       if (depth % 3 == 2)
+               candidate_delta /= 2;
+
+       s32 front_index = -1;
+       s32 back_index = -1;
+
+       if (!front_list.empty()) {
+               v3f next_normal = candidate_normal;
+               v3f next_origin = origin + delta * normal;
+               float next_delta = candidate_delta;
+               if (next_delta < 10) {
+                       const MeshTriangle *candidate = findSplitCandidate(front_list, *triangles);
+                       next_normal = candidate->getNormal();
+                       next_origin = candidate->centroid;
+               }
+               front_index = buildTree(next_normal, next_origin, next_delta, front_list, depth + 1);
+
+               // if there are no other triangles, don't create a new node
+               if (back_list.empty() && node_list.empty())
+                       return front_index;
+       }
+
+       if (!back_list.empty()) {
+               v3f next_normal = candidate_normal;
+               v3f next_origin = origin - delta * normal;
+               float next_delta = candidate_delta;
+               if (next_delta < 10) {
+                       const MeshTriangle *candidate = findSplitCandidate(back_list, *triangles);
+                       next_normal = candidate->getNormal();
+                       next_origin = candidate->centroid;
+               }
+
+               back_index = buildTree(next_normal, next_origin, next_delta, back_list, depth + 1);
+
+               // if there are no other triangles, don't create a new node
+               if (front_list.empty() && node_list.empty())
+                       return back_index;
+       }
+
+       nodes.emplace_back(normal, origin, node_list, front_index, back_index);
+
+       return nodes.size() - 1;
+}
+
+void MapBlockBspTree::traverse(s32 node, v3f viewpoint, std::vector<s32> &output) const
+{
+       if (node < 0) return; // recursion break;
+
+       const TreeNode &n = nodes[node];
+       float factor = n.normal.dotProduct(viewpoint - n.origin);
+
+       if (factor > 0)
+               traverse(n.back_ref, viewpoint, output);
+       else
+               traverse(n.front_ref, viewpoint, output);
+
+       if (factor != 0)
+               for (s32 i : n.triangle_refs)
+                       output.push_back(i);
+
+       if (factor > 0)
+               traverse(n.front_ref, viewpoint, output);
+       else
+               traverse(n.back_ref, viewpoint, output);
+}
+
+
+
+/*
+       PartialMeshBuffer
+*/
+
+void PartialMeshBuffer::beforeDraw() const
+{
+       // Patch the indexes in the mesh buffer before draw
+
+       m_buffer->Indices.clear();
+       if (!m_vertex_indexes.empty()) {
+               for (auto index : m_vertex_indexes)
+                       m_buffer->Indices.push_back(index);
+       }
+       m_buffer->setDirty(scene::EBT_INDEX);
+}
+
 /*
        MapBlockMesh
 */
@@ -1040,10 +1228,9 @@ MapBlockMesh::MapBlockMesh(MeshMakeData *data, v3s16 camera_offset):
        for (auto &m : m_mesh)
                m = new scene::SMesh();
        m_enable_shaders = data->m_use_shaders;
-       m_use_tangent_vertices = data->m_use_tangent_vertices;
        m_enable_vbo = g_settings->getBool("enable_vbo");
 
-       if (g_settings->getBool("enable_minimap")) {
+       if (data->m_client->getMinimap()) {
                m_minimap_mapblock = new MinimapMapblock;
                m_minimap_mapblock->getMinimapNodes(
                        &data->m_vmanip, data->m_blockpos * MAP_BLOCKSIZE);
@@ -1055,6 +1242,15 @@ MapBlockMesh::MapBlockMesh(MeshMakeData *data, v3s16 camera_offset):
 
        std::vector<FastFace> fastfaces_new;
        fastfaces_new.reserve(512);
+       /*
+               X-Ray
+       */
+       bool xray = g_settings->getBool("xray");
+       std::set<content_t> xraySet, nodeESPSet;
+       if (xray)
+               xraySet = splitToContentT(g_settings->get("xray_nodes"), data->m_client->ndef());
+
+       nodeESPSet = splitToContentT(g_settings->get("node_esp_nodes"), data->m_client->ndef());
 
        /*
                We are including the faces of the trailing edges of the block.
@@ -1066,10 +1262,27 @@ MapBlockMesh::MapBlockMesh(MeshMakeData *data, v3s16 camera_offset):
        {
                // 4-23ms for MAP_BLOCKSIZE=16  (NOTE: probably outdated)
                //TimeTaker timer2("updateAllFastFaceRows()");
-               updateAllFastFaceRows(data, fastfaces_new);
+               updateAllFastFaceRows(data, fastfaces_new, xray, xraySet);
        }
        // End of slow part
 
+       /*
+               NodeESP
+       */
+       {
+               v3s16 blockpos_nodes = data->m_blockpos * MAP_BLOCKSIZE;
+               for (s16 x = 0; x < MAP_BLOCKSIZE; x++) {
+                       for (s16 y = 0; y < MAP_BLOCKSIZE; y++) {
+                               for (s16 z = 0; z < MAP_BLOCKSIZE; z++) {
+                                       v3s16 pos = v3s16(x, y, z) + blockpos_nodes;
+                                       const MapNode &node = data->m_vmanip.getNodeRefUnsafeCheckFlags(pos);
+                                       if (nodeESPSet.find(node.getContent()) != nodeESPSet.end())
+                                               esp_nodes.insert(pos);
+                               }
+                       }
+               }
+       }
+
        /*
                Convert FastFaces to MeshCollector
        */
@@ -1099,14 +1312,17 @@ MapBlockMesh::MapBlockMesh(MeshMakeData *data, v3s16 camera_offset):
        */
 
        {
-               MapblockMeshGenerator generator(data, &collector);
-               generator.generate();
+               MapblockMeshGenerator(data, &collector,
+                       data->m_client->getSceneManager()->getMeshManipulator()).generate();
        }
 
        /*
                Convert MeshCollector to SMesh
        */
 
+       const bool desync_animations = g_settings->getBool(
+               "desynchronize_mapblock_texture_animation");
+
        for (int layer = 0; layer < MAX_TILE_LAYERS; layer++) {
                for(u32 i = 0; i < collector.prebuffers[layer].size(); i++)
                {
@@ -1136,18 +1352,18 @@ MapBlockMesh::MapBlockMesh(MeshMakeData *data, v3s16 camera_offset):
                        // - Texture animation
                        if (p.layer.material_flags & MATERIAL_FLAG_ANIMATION) {
                                // Add to MapBlockMesh in order to animate these tiles
-                               m_animation_tiles[std::pair<u8, u32>(layer, i)] = p.layer;
-                               m_animation_frames[std::pair<u8, u32>(layer, i)] = 0;
-                               if (g_settings->getBool(
-                                               "desynchronize_mapblock_texture_animation")) {
+                               auto &info = m_animation_info[{layer, i}];
+                               info.tile = p.layer;
+                               info.frame = 0;
+                               if (desync_animations) {
                                        // Get starting position from noise
-                                       m_animation_frame_offsets[std::pair<u8, u32>(layer, i)] =
+                                       info.frame_offset =
                                                        100000 * (2.0 + noise3d(
                                                        data->m_blockpos.X, data->m_blockpos.Y,
                                                        data->m_blockpos.Z, 0));
                                } else {
                                        // Play all synchronized
-                                       m_animation_frame_offsets[std::pair<u8, u32>(layer, i)] = 0;
+                                       info.frame_offset = 0;
                                }
                                // Replace tile texture with the first animation frame
                                p.layer.texture = (*p.layer.frames)[0].texture;
@@ -1158,19 +1374,23 @@ MapBlockMesh::MapBlockMesh(MeshMakeData *data, v3s16 camera_offset):
                                // Dummy sunlight to handle non-sunlit areas
                                video::SColorf sunlight;
                                get_sunlight_color(&sunlight, 0);
-                               u32 vertex_count = p.vertices.size();
+
+                               std::map<u32, video::SColor> colors;
+                               const u32 vertex_count = p.vertices.size();
                                for (u32 j = 0; j < vertex_count; j++) {
                                        video::SColor *vc = &p.vertices[j].Color;
                                        video::SColor copy = *vc;
                                        if (vc->getAlpha() == 0) // No sunlight - no need to animate
                                                final_color_blend(vc, copy, sunlight); // Finalize color
                                        else // Record color to animate
-                                               m_daynight_diffs[std::pair<u8, u32>(layer, i)][j] = copy;
+                                               colors[j] = copy;
 
                                        // The sunlight ratio has been stored,
                                        // delete alpha (for the final rendering).
                                        vc->setAlpha(255);
                                }
+                               if (!colors.empty())
+                                       m_daynight_diffs[{layer, i}] = std::move(colors);
                        }
 
                        // Create material
@@ -1194,51 +1414,38 @@ MapBlockMesh::MapBlockMesh(MeshMakeData *data, v3s16 camera_offset):
 
                        scene::SMesh *mesh = (scene::SMesh *)m_mesh[layer];
 
-                       // Create meshbuffer, add to mesh
-                       if (m_use_tangent_vertices) {
-                               scene::SMeshBufferTangents *buf =
-                                               new scene::SMeshBufferTangents();
-                               buf->Material = material;
-                               buf->Vertices.reallocate(p.vertices.size());
-                               buf->Indices.reallocate(p.indices.size());
-                               for (const video::S3DVertex &v: p.vertices)
-                                       buf->Vertices.push_back(video::S3DVertexTangents(v.Pos, v.Color, v.TCoords));
-                               for (u16 i: p.indices)
-                                       buf->Indices.push_back(i);
-                               buf->recalculateBoundingBox();
-                               mesh->addMeshBuffer(buf);
-                               buf->drop();
-                       } else {
-                               scene::SMeshBuffer *buf = new scene::SMeshBuffer();
-                               buf->Material = material;
+                       scene::SMeshBuffer *buf = new scene::SMeshBuffer();
+                       buf->Material = material;
+                       switch (p.layer.material_type) {
+                       // list of transparent materials taken from tile.h
+                       case TILE_MATERIAL_ALPHA:
+                       case TILE_MATERIAL_LIQUID_TRANSPARENT:
+                       case TILE_MATERIAL_WAVING_LIQUID_TRANSPARENT:
+                               {
+                                       buf->append(&p.vertices[0], p.vertices.size(),
+                                               &p.indices[0], 0);
+
+                                       MeshTriangle t;
+                                       t.buffer = buf;
+                                       for (u32 i = 0; i < p.indices.size(); i += 3) {
+                                               t.p1 = p.indices[i];
+                                               t.p2 = p.indices[i + 1];
+                                               t.p3 = p.indices[i + 2];
+                                               t.updateAttributes();
+                                               m_transparent_triangles.push_back(t);
+                                       }
+                               }
+                               break;
+                       default:
                                buf->append(&p.vertices[0], p.vertices.size(),
                                        &p.indices[0], p.indices.size());
-                               mesh->addMeshBuffer(buf);
-                               buf->drop();
+                               break;
                        }
-               }
-
-               /*
-                       Do some stuff to the mesh
-               */
-               m_camera_offset = camera_offset;
-               translateMesh(m_mesh[layer],
-                       intToFloat(data->m_blockpos * MAP_BLOCKSIZE - camera_offset, BS));
-
-               if (m_use_tangent_vertices) {
-                       scene::IMeshManipulator* meshmanip =
-                               RenderingEngine::get_scene_manager()->getMeshManipulator();
-                       meshmanip->recalculateTangents(m_mesh[layer], true, false, false);
+                       mesh->addMeshBuffer(buf);
+                       buf->drop();
                }
 
                if (m_mesh[layer]) {
-#if 0
-                       // Usually 1-700 faces and 1-7 materials
-                       std::cout << "Updated MapBlock has " << fastfaces_new.size()
-                                       << " faces and uses " << m_mesh[layer]->getMeshBufferCount()
-                                       << " materials (meshbuffers)" << std::endl;
-#endif
-
                        // Use VBO for mesh (this just would set this for ever buffer)
                        if (m_enable_vbo)
                                m_mesh[layer]->setHardwareMappingHint(scene::EHM_STATIC);
@@ -1246,24 +1453,27 @@ MapBlockMesh::MapBlockMesh(MeshMakeData *data, v3s16 camera_offset):
        }
 
        //std::cout<<"added "<<fastfaces.getSize()<<" faces."<<std::endl;
+       m_bsp_tree.buildTree(&m_transparent_triangles);
 
        // Check if animation is required for this mesh
        m_has_animation =
                !m_crack_materials.empty() ||
                !m_daynight_diffs.empty() ||
-               !m_animation_tiles.empty();
+               !m_animation_info.empty();
 }
 
 MapBlockMesh::~MapBlockMesh()
 {
        for (scene::IMesh *m : m_mesh) {
-               if (m_enable_vbo && m)
+#if IRRLICHT_VERSION_MT_REVISION < 5
+               if (m_enable_vbo) {
                        for (u32 i = 0; i < m->getMeshBufferCount(); i++) {
                                scene::IMeshBuffer *buf = m->getMeshBuffer(i);
                                RenderingEngine::get_video_driver()->removeHardwareBuffer(buf);
                        }
+               }
+#endif
                m->drop();
-               m = NULL;
        }
        delete m_minimap_mapblock;
 }
@@ -1283,25 +1493,22 @@ bool MapBlockMesh::animate(bool faraway, float time, int crack,
                for (auto &crack_material : m_crack_materials) {
                        scene::IMeshBuffer *buf = m_mesh[crack_material.first.first]->
                                getMeshBuffer(crack_material.first.second);
-                       std::string basename = crack_material.second;
 
                        // Create new texture name from original
-                       std::ostringstream os;
-                       os << basename << crack;
+                       std::string s = crack_material.second + itos(crack);
                        u32 new_texture_id = 0;
                        video::ITexture *new_texture =
-                                       m_tsrc->getTextureForMesh(os.str(), &new_texture_id);
+                                       m_tsrc->getTextureForMesh(s, &new_texture_id);
                        buf->getMaterial().setTexture(0, new_texture);
 
-                       // If the current material is also animated,
-                       // update animation info
-                       auto anim_iter = m_animation_tiles.find(crack_material.first);
-                       if (anim_iter != m_animation_tiles.end()) {
-                               TileLayer &tile = anim_iter->second;
+                       // If the current material is also animated, update animation info
+                       auto anim_it = m_animation_info.find(crack_material.first);
+                       if (anim_it != m_animation_info.end()) {
+                               TileLayer &tile = anim_it->second.tile;
                                tile.texture = new_texture;
                                tile.texture_id = new_texture_id;
                                // force animation update
-                               m_animation_frames[crack_material.first] = -1;
+                               anim_it->second.frame = -1;
                        }
                }
 
@@ -1309,28 +1516,25 @@ bool MapBlockMesh::animate(bool faraway, float time, int crack,
        }
 
        // Texture animation
-       for (auto &animation_tile : m_animation_tiles) {
-               const TileLayer &tile = animation_tile.second;
+       for (auto &it : m_animation_info) {
+               const TileLayer &tile = it.second.tile;
                // Figure out current frame
-               int frameoffset = m_animation_frame_offsets[animation_tile.first];
-               int frame = (int)(time * 1000 / tile.animation_frame_length_ms
-                               + frameoffset) % tile.animation_frame_count;
+               int frameno = (int)(time * 1000 / tile.animation_frame_length_ms
+                               + it.second.frame_offset) % tile.animation_frame_count;
                // If frame doesn't change, skip
-               if (frame == m_animation_frames[animation_tile.first])
+               if (frameno == it.second.frame)
                        continue;
 
-               m_animation_frames[animation_tile.first] = frame;
+               it.second.frame = frameno;
 
-               scene::IMeshBuffer *buf = m_mesh[animation_tile.first.first]->
-                       getMeshBuffer(animation_tile.first.second);
+               scene::IMeshBuffer *buf = m_mesh[it.first.first]->getMeshBuffer(it.first.second);
 
-               const FrameSpec &animation_frame = (*tile.frames)[frame];
-               buf->getMaterial().setTexture(0, animation_frame.texture);
+               const FrameSpec &frame = (*tile.frames)[frameno];
+               buf->getMaterial().setTexture(0, frame.texture);
                if (m_enable_shaders) {
-                       if (animation_frame.normal_texture)
-                               buf->getMaterial().setTexture(1,
-                                       animation_frame.normal_texture);
-                       buf->getMaterial().setTexture(2, animation_frame.flags_texture);
+                       if (frame.normal_texture)
+                               buf->getMaterial().setTexture(1, frame.normal_texture);
+                       buf->getMaterial().setTexture(2, frame.flags_texture);
                }
        }
 
@@ -1357,16 +1561,64 @@ bool MapBlockMesh::animate(bool faraway, float time, int crack,
        return true;
 }
 
-void MapBlockMesh::updateCameraOffset(v3s16 camera_offset)
+void MapBlockMesh::updateTransparentBuffers(v3f camera_pos, v3s16 block_pos)
 {
-       if (camera_offset != m_camera_offset) {
-               for (scene::IMesh *layer : m_mesh) {
-                       translateMesh(layer,
-                               intToFloat(m_camera_offset - camera_offset, BS));
-                       if (m_enable_vbo)
-                               layer->setDirty();
+       // nothing to do if the entire block is opaque
+       if (m_transparent_triangles.empty())
+               return;
+
+       v3f block_posf = intToFloat(block_pos * MAP_BLOCKSIZE, BS);
+       v3f rel_camera_pos = camera_pos - block_posf;
+
+       std::vector<s32> triangle_refs;
+       m_bsp_tree.traverse(rel_camera_pos, triangle_refs);
+
+       // arrange index sequences into partial buffers
+       m_transparent_buffers.clear();
+
+       scene::SMeshBuffer *current_buffer = nullptr;
+       std::vector<u16> current_strain;
+       for (auto i : triangle_refs) {
+               const auto &t = m_transparent_triangles[i];
+               if (current_buffer != t.buffer) {
+                       if (current_buffer) {
+                               m_transparent_buffers.emplace_back(current_buffer, current_strain);
+                               current_strain.clear();
+                       }
+                       current_buffer = t.buffer;
+               }
+               current_strain.push_back(t.p1);
+               current_strain.push_back(t.p2);
+               current_strain.push_back(t.p3);
+       }
+
+       if (!current_strain.empty())
+               m_transparent_buffers.emplace_back(current_buffer, current_strain);
+}
+
+void MapBlockMesh::consolidateTransparentBuffers()
+{
+       m_transparent_buffers.clear();
+
+       scene::SMeshBuffer *current_buffer = nullptr;
+       std::vector<u16> current_strain;
+
+       // use the fact that m_transparent_triangles is already arranged by buffer
+       for (const auto &t : m_transparent_triangles) {
+               if (current_buffer != t.buffer) {
+                       if (current_buffer != nullptr) {
+                               this->m_transparent_buffers.emplace_back(current_buffer, current_strain);
+                               current_strain.clear();
+                       }
+                       current_buffer = t.buffer;
                }
-               m_camera_offset = camera_offset;
+               current_strain.push_back(t.p1);
+               current_strain.push_back(t.p2);
+               current_strain.push_back(t.p3);
+       }
+
+       if (!current_strain.empty()) {
+               this->m_transparent_buffers.emplace_back(current_buffer, current_strain);
        }
 }