1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-08-06 17:41:04 +00:00

Refactor the way you set material properties

Instead of using SMaterial::setFlag, you now set them directly on SMaterial or SMaterialLayer.
This commit is contained in:
Gregor Parzefall 2023-06-23 11:33:23 +02:00 committed by sfan5
parent 128d22e6ee
commit 307e380f30
15 changed files with 202 additions and 171 deletions

View file

@ -252,11 +252,11 @@ void TestCAO::addToScene(ITextureSource *tsrc, scene::ISceneManager *smgr)
u16 indices[] = {0,1,2,2,3,0};
buf->append(vertices, 4, indices, 6);
// Set material
buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
buf->getMaterial().setFlag(video::EMF_BACK_FACE_CULLING, false);
buf->getMaterial().Lighting = false;
buf->getMaterial().BackfaceCulling = false;
buf->getMaterial().setTexture(0, tsrc->getTextureForMesh("rat.png"));
buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
buf->getMaterial().TextureLayer[0].BilinearFilter = false;
buf->getMaterial().FogEnable = true;
buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
// Add to mesh
mesh->addMeshBuffer(buf);
@ -643,16 +643,21 @@ void GenericCAO::addToScene(ITextureSource *tsrc, scene::ISceneManager *smgr)
m_matrixnode->grab();
};
auto setSceneNodeMaterial = [this] (scene::ISceneNode *node) {
node->setMaterialFlag(video::EMF_LIGHTING, false);
node->setMaterialFlag(video::EMF_BILINEAR_FILTER, false);
node->setMaterialFlag(video::EMF_FOG_ENABLE, true);
node->setMaterialType(m_material_type);
auto setMaterial = [this] (video::SMaterial &mat) {
mat.MaterialType = m_material_type;
mat.Lighting = false;
mat.FogEnable = true;
if (m_enable_shaders) {
node->setMaterialFlag(video::EMF_GOURAUD_SHADING, false);
node->setMaterialFlag(video::EMF_NORMALIZE_NORMALS, true);
mat.GouraudShading = false;
mat.NormalizeNormals = true;
}
mat.forEachTexture([] (video::SMaterialLayer &tex) {
tex.BilinearFilter = false;
});
};
auto setSceneNodeMaterials = [setMaterial] (scene::ISceneNode *node) {
node->forEachMaterial(setMaterial);
};
if (m_prop.visual == "sprite") {
@ -660,10 +665,12 @@ void GenericCAO::addToScene(ITextureSource *tsrc, scene::ISceneManager *smgr)
m_spritenode = m_smgr->addBillboardSceneNode(
m_matrixnode, v2f(1, 1), v3f(0,0,0), -1);
m_spritenode->grab();
m_spritenode->setMaterialTexture(0,
tsrc->getTextureForMesh("no_texture.png"));
video::ITexture *tex = tsrc->getTextureForMesh("no_texture.png");
m_spritenode->forEachMaterial([tex] (video::SMaterial &mat) {
mat.setTexture(0, tex);
});
setSceneNodeMaterial(m_spritenode);
setSceneNodeMaterials(m_spritenode);
m_spritenode->setSize(v2f(m_prop.visual_size.X,
m_prop.visual_size.Y) * BS);
@ -695,16 +702,11 @@ void GenericCAO::addToScene(ITextureSource *tsrc, scene::ISceneManager *smgr)
}
u16 indices[] = {0,1,2,2,3,0};
buf->append(vertices, 4, indices, 6);
// Set material
buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
buf->getMaterial().MaterialType = m_material_type;
// Set material
setMaterial(buf->getMaterial());
if (m_enable_shaders) {
buf->getMaterial().EmissiveColor = c;
buf->getMaterial().setFlag(video::EMF_GOURAUD_SHADING, false);
buf->getMaterial().setFlag(video::EMF_NORMALIZE_NORMALS, true);
}
// Add to mesh
@ -726,16 +728,11 @@ void GenericCAO::addToScene(ITextureSource *tsrc, scene::ISceneManager *smgr)
}
u16 indices[] = {0,1,2,2,3,0};
buf->append(vertices, 4, indices, 6);
// Set material
buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
buf->getMaterial().MaterialType = m_material_type;
// Set material
setMaterial(buf->getMaterial());
if (m_enable_shaders) {
buf->getMaterial().EmissiveColor = c;
buf->getMaterial().setFlag(video::EMF_GOURAUD_SHADING, false);
buf->getMaterial().setFlag(video::EMF_NORMALIZE_NORMALS, true);
}
// Add to mesh
@ -753,10 +750,12 @@ void GenericCAO::addToScene(ITextureSource *tsrc, scene::ISceneManager *smgr)
mesh->drop();
m_meshnode->setScale(m_prop.visual_size);
m_meshnode->setMaterialFlag(video::EMF_BACK_FACE_CULLING,
m_prop.backface_culling);
setSceneNodeMaterial(m_meshnode);
setSceneNodeMaterials(m_meshnode);
m_meshnode->forEachMaterial([this] (video::SMaterial &mat) {
mat.BackfaceCulling = m_prop.backface_culling;
});
} else if (m_prop.visual == "mesh") {
grabMatrixNode();
scene::IAnimatedMesh *mesh = m_client->getMesh(m_prop.mesh, true);
@ -779,10 +778,11 @@ void GenericCAO::addToScene(ITextureSource *tsrc, scene::ISceneManager *smgr)
setAnimatedMeshColor(m_animated_meshnode, video::SColor(0xFFFFFFFF));
setSceneNodeMaterial(m_animated_meshnode);
setSceneNodeMaterials(m_animated_meshnode);
m_animated_meshnode->setMaterialFlag(video::EMF_BACK_FACE_CULLING,
m_prop.backface_culling);
m_animated_meshnode->forEachMaterial([this] (video::SMaterial &mat) {
mat.BackfaceCulling = m_prop.backface_culling;
});
} else
errorstream<<"GenericCAO::addToScene(): Could not load mesh "<<m_prop.mesh<<std::endl;
} else if (m_prop.visual == "wielditem" || m_prop.visual == "item") {
@ -1337,23 +1337,26 @@ void GenericCAO::updateTextures(std::string mod)
if (!m_prop.textures.empty())
texturestring = m_prop.textures[0];
texturestring += mod;
m_spritenode->getMaterial(0).MaterialType = m_material_type;
m_spritenode->getMaterial(0).MaterialTypeParam = 0.5f;
m_spritenode->setMaterialTexture(0,
tsrc->getTextureForMesh(texturestring));
video::SMaterial &material = m_spritenode->getMaterial(0);
material.MaterialType = m_material_type;
material.MaterialTypeParam = 0.5f;
material.setTexture(0, tsrc->getTextureForMesh(texturestring));
// This allows setting per-material colors. However, until a real lighting
// system is added, the code below will have no effect. Once MineTest
// has directional lighting, it should work automatically.
if (!m_prop.colors.empty()) {
m_spritenode->getMaterial(0).AmbientColor = m_prop.colors[0];
m_spritenode->getMaterial(0).DiffuseColor = m_prop.colors[0];
m_spritenode->getMaterial(0).SpecularColor = m_prop.colors[0];
material.AmbientColor = m_prop.colors[0];
material.DiffuseColor = m_prop.colors[0];
material.SpecularColor = m_prop.colors[0];
}
m_spritenode->getMaterial(0).setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
m_spritenode->getMaterial(0).setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
m_spritenode->getMaterial(0).setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
material.forEachTexture([=] (video::SMaterialLayer &tex) {
tex.TrilinearFilter = use_trilinear_filter;
tex.BilinearFilter = use_bilinear_filter;
tex.AnisotropicFilter = use_anisotropic_filter ? 0xFF : 0;
});
}
}
@ -1365,20 +1368,19 @@ void GenericCAO::updateTextures(std::string mod)
if (texturestring.empty())
continue; // Empty texture string means don't modify that material
texturestring += mod;
video::ITexture* texture = tsrc->getTextureForMesh(texturestring);
video::ITexture *texture = tsrc->getTextureForMesh(texturestring);
if (!texture) {
errorstream<<"GenericCAO::updateTextures(): Could not load texture "<<texturestring<<std::endl;
continue;
}
// Set material flags and texture
video::SMaterial& material = m_animated_meshnode->getMaterial(i);
video::SMaterial &material = m_animated_meshnode->getMaterial(i);
material.MaterialType = m_material_type;
material.MaterialTypeParam = 0.5f;
material.TextureLayer[0].Texture = texture;
material.setFlag(video::EMF_LIGHTING, true);
material.setFlag(video::EMF_BILINEAR_FILTER, false);
material.setFlag(video::EMF_BACK_FACE_CULLING, m_prop.backface_culling);
material.Lighting = true;
material.BackfaceCulling = m_prop.backface_culling;
// don't filter low-res textures, makes them look blurry
// player models have a res of 64
@ -1387,22 +1389,22 @@ void GenericCAO::updateTextures(std::string mod)
use_trilinear_filter &= res > 64;
use_bilinear_filter &= res > 64;
m_animated_meshnode->getMaterial(i)
.setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
m_animated_meshnode->getMaterial(i)
.setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
m_animated_meshnode->getMaterial(i)
.setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
material.forEachTexture([=] (video::SMaterialLayer &tex) {
tex.TrilinearFilter = use_trilinear_filter;
tex.BilinearFilter = use_bilinear_filter;
tex.AnisotropicFilter = use_anisotropic_filter ? 0xFF : 0;
});
}
for (u32 i = 0; i < m_prop.colors.size() &&
i < m_animated_meshnode->getMaterialCount(); ++i)
i < m_animated_meshnode->getMaterialCount(); ++i)
{
video::SMaterial &material = m_animated_meshnode->getMaterial(i);
// This allows setting per-material colors. However, until a real lighting
// system is added, the code below will have no effect. Once MineTest
// has directional lighting, it should work automatically.
m_animated_meshnode->getMaterial(i).AmbientColor = m_prop.colors[i];
m_animated_meshnode->getMaterial(i).DiffuseColor = m_prop.colors[i];
m_animated_meshnode->getMaterial(i).SpecularColor = m_prop.colors[i];
material.AmbientColor = m_prop.colors[i];
material.DiffuseColor = m_prop.colors[i];
material.SpecularColor = m_prop.colors[i];
}
}
}
@ -1417,15 +1419,12 @@ void GenericCAO::updateTextures(std::string mod)
texturestring = m_prop.textures[i];
texturestring += mod;
// Set material flags and texture
video::SMaterial& material = m_meshnode->getMaterial(i);
video::SMaterial &material = m_meshnode->getMaterial(i);
material.MaterialType = m_material_type;
material.MaterialTypeParam = 0.5f;
material.setFlag(video::EMF_LIGHTING, false);
material.setFlag(video::EMF_BILINEAR_FILTER, false);
material.setTexture(0,
tsrc->getTextureForMesh(texturestring));
material.Lighting = false;
material.setTexture(0, tsrc->getTextureForMesh(texturestring));
material.getTextureMatrix(0).makeIdentity();
// This allows setting per-material colors. However, until a real lighting
@ -1433,14 +1432,16 @@ void GenericCAO::updateTextures(std::string mod)
// has directional lighting, it should work automatically.
if(m_prop.colors.size() > i)
{
m_meshnode->getMaterial(i).AmbientColor = m_prop.colors[i];
m_meshnode->getMaterial(i).DiffuseColor = m_prop.colors[i];
m_meshnode->getMaterial(i).SpecularColor = m_prop.colors[i];
material.AmbientColor = m_prop.colors[i];
material.DiffuseColor = m_prop.colors[i];
material.SpecularColor = m_prop.colors[i];
}
m_meshnode->getMaterial(i).setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
m_meshnode->getMaterial(i).setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
m_meshnode->getMaterial(i).setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
material.forEachTexture([=] (video::SMaterialLayer &tex) {
tex.TrilinearFilter = use_trilinear_filter;
tex.BilinearFilter = use_bilinear_filter;
tex.AnisotropicFilter = use_anisotropic_filter ? 0xFF : 0;
});
}
} else if (m_prop.visual == "upright_sprite") {
scene::IMesh *mesh = m_meshnode->getMesh();
@ -1449,9 +1450,9 @@ void GenericCAO::updateTextures(std::string mod)
if (!m_prop.textures.empty())
tname = m_prop.textures[0];
tname += mod;
auto& material = m_meshnode->getMaterial(0);
material.setTexture(0,
tsrc->getTextureForMesh(tname));
auto &material = m_meshnode->getMaterial(0);
material.setTexture(0, tsrc->getTextureForMesh(tname));
// This allows setting per-material colors. However, until a real lighting
// system is added, the code below will have no effect. Once MineTest
@ -1462,9 +1463,11 @@ void GenericCAO::updateTextures(std::string mod)
material.SpecularColor = m_prop.colors[0];
}
material.setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
material.setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
material.setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
material.forEachTexture([=] (video::SMaterialLayer &tex) {
tex.TrilinearFilter = use_trilinear_filter;
tex.BilinearFilter = use_bilinear_filter;
tex.AnisotropicFilter = use_anisotropic_filter ? 0xFF : 0;
});
}
{
std::string tname = "no_texture.png";
@ -1473,9 +1476,9 @@ void GenericCAO::updateTextures(std::string mod)
else if (!m_prop.textures.empty())
tname = m_prop.textures[0];
tname += mod;
auto& material = m_meshnode->getMaterial(1);
material.setTexture(0,
tsrc->getTextureForMesh(tname));
auto &material = m_meshnode->getMaterial(1);
material.setTexture(0, tsrc->getTextureForMesh(tname));
// This allows setting per-material colors. However, until a real lighting
// system is added, the code below will have no effect. Once MineTest
@ -1490,9 +1493,11 @@ void GenericCAO::updateTextures(std::string mod)
material.SpecularColor = m_prop.colors[0];
}
material.setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
material.setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
material.setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
material.forEachTexture([=] (video::SMaterialLayer &tex) {
tex.TrilinearFilter = use_trilinear_filter;
tex.BilinearFilter = use_bilinear_filter;
tex.AnisotropicFilter = use_anisotropic_filter ? 0xFF : 0;
});
}
// Set mesh color (only if lighting is disabled)
if (!m_prop.colors.empty() && m_prop.glow < 0)
@ -1975,7 +1980,9 @@ void GenericCAO::updateMeshCulling()
if (m_prop.visual == "upright_sprite") {
// upright sprite has no backface culling
node->setMaterialFlag(video::EMF_FRONT_FACE_CULLING, hidden);
node->forEachMaterial([=] (video::SMaterial &mat) {
mat.FrontfaceCulling = hidden;
});
return;
}
@ -1983,16 +1990,16 @@ void GenericCAO::updateMeshCulling()
// Hide the mesh by culling both front and
// back faces. Serious hackyness but it works for our
// purposes. This also preserves the skeletal armature.
node->setMaterialFlag(video::EMF_BACK_FACE_CULLING,
true);
node->setMaterialFlag(video::EMF_FRONT_FACE_CULLING,
true);
node->forEachMaterial([] (video::SMaterial &mat) {
mat.BackfaceCulling = true;
mat.FrontfaceCulling = true;
});
} else {
// Restore mesh visibility.
node->setMaterialFlag(video::EMF_BACK_FACE_CULLING,
m_prop.backface_culling);
node->setMaterialFlag(video::EMF_FRONT_FACE_CULLING,
false);
node->forEachMaterial([this] (video::SMaterial &mat) {
mat.BackfaceCulling = m_prop.backface_culling;
mat.FrontfaceCulling = false;
});
}
}