Clean up TileLayer::applyMaterialOptions

This commit is contained in:
sfan5 2025-02-15 14:25:04 +01:00
parent 7d3f0628c4
commit 83fd837d75
5 changed files with 25 additions and 63 deletions

View file

@ -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());
}

View file

@ -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();

View file

@ -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;
}
}

View file

@ -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

View file

@ -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) {