diff --git a/src/client/content_cao.cpp b/src/client/content_cao.cpp index 5bf3402ba..e11debc94 100644 --- a/src/client/content_cao.cpp +++ b/src/client/content_cao.cpp @@ -220,14 +220,9 @@ static scene::SMesh *generateNodeMesh(Client *client, MapNode n, // Set up material auto &mat = buf->Material; - mat.setTexture(0, p.layer.texture); u32 shader_id = shdsrc->getShader("object_shader", p.layer.material_type, NDT_NORMAL); mat.MaterialType = shdsrc->getShaderInfo(shader_id).material; - if (layer == 1) { - mat.PolygonOffsetSlopeScale = -1; - mat.PolygonOffsetDepthBias = -1; - } - p.layer.applyMaterialOptionsWithShaders(mat); + p.layer.applyMaterialOptions(mat, layer); mesh->addMeshBuffer(buf.get()); } diff --git a/src/client/mapblock_mesh.cpp b/src/client/mapblock_mesh.cpp index 81c698363..35ac65de4 100644 --- a/src/client/mapblock_mesh.cpp +++ b/src/client/mapblock_mesh.cpp @@ -687,28 +687,16 @@ MapBlockMesh::MapBlockMesh(Client *client, MeshMakeData *data): // Create material video::SMaterial material; - material.BackfaceCulling = true; material.FogEnable = true; - material.setTexture(0, p.layer.texture); material.forEachTexture([] (auto &tex) { tex.MinFilter = video::ETMINF_NEAREST_MIPMAP_NEAREST; tex.MagFilter = video::ETMAGF_NEAREST; }); - /* - * The second layer is for overlays, but uses the same vertex positions - * as the first, which quickly leads to z-fighting. - * To fix this we can offset the polygons in the direction of the camera. - * This only affects the depth buffer and leads to no visual gaps in geometry. - */ - if (layer == 1) { - material.PolygonOffsetSlopeScale = -1; - material.PolygonOffsetDepthBias = -1; - } { material.MaterialType = m_shdrsrc->getShaderInfo( p.layer.shader_id).material; - p.layer.applyMaterialOptionsWithShaders(material); + p.layer.applyMaterialOptions(material, layer); } scene::SMeshBuffer *buf = new scene::SMeshBuffer(); diff --git a/src/client/tile.cpp b/src/client/tile.cpp index ad5811d64..b13d39056 100644 --- a/src/client/tile.cpp +++ b/src/client/tile.cpp @@ -4,41 +4,10 @@ #include "tile.h" -// Sets everything else except the texture in the material -void TileLayer::applyMaterialOptions(video::SMaterial &material) const +void TileLayer::applyMaterialOptions(video::SMaterial &material, int layer) const { - switch (material_type) { - case TILE_MATERIAL_OPAQUE: - case TILE_MATERIAL_LIQUID_OPAQUE: - case TILE_MATERIAL_WAVING_LIQUID_OPAQUE: - material.MaterialType = video::EMT_SOLID; - break; - case TILE_MATERIAL_BASIC: - case TILE_MATERIAL_WAVING_LEAVES: - case TILE_MATERIAL_WAVING_PLANTS: - case TILE_MATERIAL_WAVING_LIQUID_BASIC: - material.MaterialTypeParam = 0.5; - material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF; - break; - case TILE_MATERIAL_ALPHA: - case TILE_MATERIAL_LIQUID_TRANSPARENT: - case TILE_MATERIAL_WAVING_LIQUID_TRANSPARENT: - material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL; - break; - default: - break; - } - material.BackfaceCulling = (material_flags & MATERIAL_FLAG_BACKFACE_CULLING) != 0; - if (!(material_flags & MATERIAL_FLAG_TILEABLE_HORIZONTAL)) { - material.TextureLayers[0].TextureWrapU = video::ETC_CLAMP_TO_EDGE; - } - if (!(material_flags & MATERIAL_FLAG_TILEABLE_VERTICAL)) { - material.TextureLayers[0].TextureWrapV = video::ETC_CLAMP_TO_EDGE; - } -} + material.setTexture(0, texture); -void TileLayer::applyMaterialOptionsWithShaders(video::SMaterial &material) const -{ material.BackfaceCulling = (material_flags & MATERIAL_FLAG_BACKFACE_CULLING) != 0; if (!(material_flags & MATERIAL_FLAG_TILEABLE_HORIZONTAL)) { material.TextureLayers[0].TextureWrapU = video::ETC_CLAMP_TO_EDGE; @@ -48,4 +17,15 @@ void TileLayer::applyMaterialOptionsWithShaders(video::SMaterial &material) cons material.TextureLayers[0].TextureWrapV = video::ETC_CLAMP_TO_EDGE; material.TextureLayers[1].TextureWrapV = video::ETC_CLAMP_TO_EDGE; } + + /* + * The second layer is for overlays, but uses the same vertex positions + * as the first, which easily leads to Z-fighting. + * To fix this we can offset the polygons in the direction of the camera. + * This only affects the depth buffer and leads to no visual gaps in geometry. + */ + if (layer == 1) { + material.PolygonOffsetSlopeScale = -1; + material.PolygonOffsetDepthBias = -1; + } } diff --git a/src/client/tile.h b/src/client/tile.h index a337f1381..8336d1d85 100644 --- a/src/client/tile.h +++ b/src/client/tile.h @@ -84,9 +84,13 @@ struct TileLayer return !(*this == other); } - void applyMaterialOptions(video::SMaterial &material) const; - - void applyMaterialOptionsWithShaders(video::SMaterial &material) const; + /** + * Set some material parameters accordingly. + * @note does not set `MaterialType` + * @param material material to mody + * @param layer index of this layer in the `TileSpec` + */ + void applyMaterialOptions(video::SMaterial &material, int layer) const; /// @return is this layer semi-transparent? bool isTransparent() const diff --git a/src/client/wieldmesh.cpp b/src/client/wieldmesh.cpp index b5851264e..7b87f7bdf 100644 --- a/src/client/wieldmesh.cpp +++ b/src/client/wieldmesh.cpp @@ -326,13 +326,8 @@ static scene::SMesh *createGenericNodeMesh(Client *client, MapNode n, buf->append(&p.vertices[0], p.vertices.size(), &p.indices[0], p.indices.size()); - // Set up material - buf->Material.setTexture(0, p.layer.texture); - if (layer == 1) { - buf->Material.PolygonOffsetSlopeScale = -1; - buf->Material.PolygonOffsetDepthBias = -1; - } - p.layer.applyMaterialOptions(buf->Material); + // note: material type is left unset, overriden later + p.layer.applyMaterialOptions(buf->Material, layer); mesh->addMeshBuffer(buf.get()); colors->emplace_back(p.layer.has_color, p.layer.color); @@ -432,7 +427,7 @@ void WieldMeshSceneNode::setItem(const ItemStack &item, Client *client, bool che u32 material_count = m_meshnode->getMaterialCount(); for (u32 i = 0; i < material_count; ++i) { video::SMaterial &material = m_meshnode->getMaterial(i); - // FIXME: overriding this breaks different alpha modes the mesh may have + // FIXME: we should take different alpha modes of the mesh into account here material.MaterialType = m_material_type; material.MaterialTypeParam = 0.5f; material.forEachTexture([this] (auto &tex) {