Skip to content

Commit

Permalink
ContentCAO: Fix threshold of alpha channel textures
Browse files Browse the repository at this point in the history
With disabled shaders, the material EMT_TRANSPARENT_ALPHA_CHANNEL uses the
parameter as an alpha threshold to decide whether to draw the texture.
Thus lowering this limit fixes the issue of vanishing textures below alpha 128.
  • Loading branch information
SmallJoker committed Jan 4, 2024
1 parent 995c192 commit 5c0e610
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
18 changes: 12 additions & 6 deletions src/client/content_cao.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,9 @@ void TestCAO::processMessage(const std::string &data)
#include "clientobject.h"

GenericCAO::GenericCAO(Client *client, ClientEnvironment *env):
ClientActiveObject(0, client, env)
ClientActiveObject(0, client, env),
m_material_type(video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF),
m_material_type_param(0.5f)
{
if (!client) {
ClientActiveObject::registerType(getType(), create);
Expand Down Expand Up @@ -634,8 +636,12 @@ void GenericCAO::addToScene(ITextureSource *tsrc, scene::ISceneManager *smgr)
u32 shader_id = shader_source->getShader("object_shader", material_type, NDT_NORMAL);
m_material_type = shader_source->getShaderInfo(shader_id).material;
} else {
m_material_type = (m_prop.use_texture_alpha) ?
video::EMT_TRANSPARENT_ALPHA_CHANNEL : video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
if (m_prop.use_texture_alpha) {
m_material_type = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
m_material_type_param = 0.01f; // minimal alpha for texture rendering
} else {
m_material_type = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
}
}

auto grabMatrixNode = [this] {
Expand Down Expand Up @@ -1341,7 +1347,7 @@ void GenericCAO::updateTextures(std::string mod)

video::SMaterial &material = m_spritenode->getMaterial(0);
material.MaterialType = m_material_type;
material.MaterialTypeParam = 0.5f;
material.MaterialTypeParam = m_material_type_param;
material.setTexture(0, tsrc->getTextureForMesh(texturestring));

// This allows setting per-material colors. However, until a real lighting
Expand Down Expand Up @@ -1377,7 +1383,7 @@ void GenericCAO::updateTextures(std::string mod)
// Set material flags and texture
video::SMaterial &material = m_animated_meshnode->getMaterial(i);
material.MaterialType = m_material_type;
material.MaterialTypeParam = 0.5f;
material.MaterialTypeParam = m_material_type_param;
material.TextureLayers[0].Texture = texture;
material.Lighting = true;
material.BackfaceCulling = m_prop.backface_culling;
Expand Down Expand Up @@ -1421,7 +1427,7 @@ void GenericCAO::updateTextures(std::string mod)
// Set material flags and texture
video::SMaterial &material = m_meshnode->getMaterial(i);
material.MaterialType = m_material_type;
material.MaterialTypeParam = 0.5f;
material.MaterialTypeParam = m_material_type_param;
material.Lighting = false;
material.setTexture(0, tsrc->getTextureForMesh(texturestring));
material.getTextureMatrix(0).makeIdentity();
Expand Down
1 change: 1 addition & 0 deletions src/client/content_cao.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ class GenericCAO : public ClientActiveObject
bool m_is_visible = false;
// Material
video::E_MATERIAL_TYPE m_material_type;
f32 m_material_type_param;
// Settings
bool m_enable_shaders = false;

Expand Down

0 comments on commit 5c0e610

Please sign in to comment.