From a66e6d4dff0b1dad065297442981718d7f7aec55 Mon Sep 17 00:00:00 2001 From: sfan5 Date: Fri, 6 May 2022 09:54:04 +0200 Subject: [PATCH] Consolidate some data structures in MapBlockMesh --- src/client/mapblock_mesh.cpp | 69 ++++++++++++++++++------------------ src/client/mapblock_mesh.h | 13 ++++--- 2 files changed, 44 insertions(+), 38 deletions(-) diff --git a/src/client/mapblock_mesh.cpp b/src/client/mapblock_mesh.cpp index 2c5500fca..868573bf0 100644 --- a/src/client/mapblock_mesh.cpp +++ b/src/client/mapblock_mesh.cpp @@ -1252,6 +1252,9 @@ MapBlockMesh::MapBlockMesh(MeshMakeData *data, v3s16 camera_offset): 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++) { @@ -1281,18 +1284,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(layer, i)] = p.layer; - m_animation_frames[std::pair(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(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(layer, i)] = 0; + info.frame_offset = 0; } // Replace tile texture with the first animation frame p.layer.texture = (*p.layer.frames)[0].texture; @@ -1303,19 +1306,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 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(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 @@ -1384,7 +1391,7 @@ MapBlockMesh::MapBlockMesh(MeshMakeData *data, v3s16 camera_offset): m_has_animation = !m_crack_materials.empty() || !m_daynight_diffs.empty() || - !m_animation_tiles.empty(); + !m_animation_info.empty(); } MapBlockMesh::~MapBlockMesh() @@ -1418,25 +1425,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; } } @@ -1444,28 +1448,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); } } diff --git a/src/client/mapblock_mesh.h b/src/client/mapblock_mesh.h index cfc39ade2..72d128038 100644 --- a/src/client/mapblock_mesh.h +++ b/src/client/mapblock_mesh.h @@ -219,7 +219,14 @@ class MapBlockMesh { return this->m_transparent_buffers; } + private: + struct AnimationInfo { + int frame; // last animation frame + int frame_offset; + TileLayer tile; + }; + scene::IMesh *m_mesh[MAX_TILE_LAYERS]; MinimapMapblock *m_minimap_mapblock; ITextureSource *m_tsrc; @@ -238,12 +245,10 @@ class MapBlockMesh // Maps mesh and mesh buffer (i.e. material) indices to base texture names std::map, std::string> m_crack_materials; - // Animation info: texture animationi + // Animation info: texture animation // Maps mesh and mesh buffer indices to TileSpecs // Keys are pairs of (mesh index, buffer index in the mesh) - std::map, TileLayer> m_animation_tiles; - std::map, int> m_animation_frames; // last animation frame - std::map, int> m_animation_frame_offsets; + std::map, AnimationInfo> m_animation_info; // Animation info: day/night transitions // Last daynight_ratio value passed to animate() -- 2.44.0