Fix importing emissive, roughness, metalness and wireframe properties of materials with Assimp

#3418
This commit is contained in:
Wojtek Figat
2025-09-23 15:42:49 +02:00
parent dc05bbbbcd
commit d8f7199c11
6 changed files with 62 additions and 6 deletions

View File

@@ -93,14 +93,15 @@ namespace
};
template<typename T>
void AddInput(MaterialLayer* layer, Meta11 meta, MaterialGraphBoxes box, const Guid& texture, const T& value, const T& defaultValue, const Float2& pos, ShaderGraphNode<>** outTextureNode = nullptr)
void AddInput(MaterialLayer* layer, Meta11 meta, MaterialGraphBoxes box, const Guid& texture, const T& value, const T& defaultValue, const Float2& pos, ShaderGraphNode<>** outTextureNode = nullptr, uint8 channel = MAX_uint8)
{
auto textureNode = AddTextureNode(layer, texture);
auto valueNode = AddValueNode<T>(layer, value, defaultValue);
auto textureNodeBox = channel == MAX_uint8 ? 1 : channel + 2; // Color or specific channel (RGBA)
if (textureNode && valueNode)
{
auto diffuseMultiply = AddMultiplyNode(layer);
CONNECT(diffuseMultiply->Boxes[0], textureNode->Boxes[1]);
CONNECT(diffuseMultiply->Boxes[0], textureNode->Boxes[textureNodeBox]);
CONNECT(diffuseMultiply->Boxes[1], valueNode->Boxes[0]);
CONNECT(layer->Root->Boxes[static_cast<int32>(box)], diffuseMultiply->Boxes[2]);
SET_POS(valueNode, pos + Float2(-467.7404, 91.41332));
@@ -109,7 +110,7 @@ namespace
}
else if (textureNode)
{
CONNECT(layer->Root->Boxes[static_cast<int32>(box)], textureNode->Boxes[1]);
CONNECT(layer->Root->Boxes[static_cast<int32>(box)], textureNode->Boxes[textureNodeBox]);
SET_POS(textureNode, pos + Float2(-293.5272f, -2.926111f));
}
else if (valueNode)
@@ -178,8 +179,9 @@ CreateAssetResult CreateMaterial::Create(CreateAssetContext& context)
// Opacity
AddInput(layer, meta, MaterialGraphBoxes::Opacity, options.Opacity.Texture, options.Opacity.Value, 1.0f, Float2(0, 400));
// Opacity
AddInput(layer, meta, MaterialGraphBoxes::Roughness, options.Roughness.Texture, options.Roughness.Value, 0.5f, Float2(200, 400));
// Roughness + Metalness
AddInput(layer, meta, MaterialGraphBoxes::Roughness, options.Roughness.Texture, options.Roughness.Value, 0.5f, Float2(200, 400), nullptr, options.Roughness.Channel);
AddInput(layer, meta, MaterialGraphBoxes::Metalness, options.Metalness.Texture, options.Metalness.Value, 0.0f, Float2(200, 600), nullptr, options.Metalness.Channel);
// Normal
auto normalMap = AddTextureNode(layer, options.Normals.Texture, true);

View File

@@ -40,9 +40,17 @@ public:
struct
{
float Value = 0.5f;
uint8 Channel = 0;
Guid Texture = Guid::Empty;
} Roughness;
struct
{
float Value = 0.0f;
uint8 Channel = 0;
Guid Texture = Guid::Empty;
} Metalness;
struct
{
Guid Texture = Guid::Empty;

View File

@@ -372,6 +372,8 @@ bool MaterialSlotEntry::UsesProperties() const
Opacity.TextureIndex != -1 ||
Math::NotNearEqual(Roughness.Value, 0.5f) ||
Roughness.TextureIndex != -1 ||
Math::NotNearEqual(Metalness.Value, 0.5f) ||
Metalness.TextureIndex != -1 ||
Normals.TextureIndex != -1;
}

View File

@@ -327,14 +327,23 @@ struct FLAXENGINE_API MaterialSlotEntry
{
float Value = 0.5f;
int32 TextureIndex = -1;
uint8 Channel = 0;
} Roughness;
struct
{
float Value = 0.0f;
int32 TextureIndex = -1;
uint8 Channel = 0;
} Metalness;
struct
{
int32 TextureIndex = -1;
} Normals;
bool TwoSided = false;
bool Wireframe = false;
bool UsesProperties() const;
static float ShininessToRoughness(float shininess);

View File

@@ -528,12 +528,24 @@ bool ImportMaterials(ModelData& result, AssimpImporterData& data, String& errorM
aiColor3D aColor;
if (aMaterial->Get(AI_MATKEY_COLOR_DIFFUSE, aColor) == AI_SUCCESS)
materialSlot.Diffuse.Color = ToColor(aColor);
if (aMaterial->Get(AI_MATKEY_COLOR_EMISSIVE, aColor) == AI_SUCCESS)
materialSlot.Emissive.Color = ToColor(aColor);
if (aMaterial->Get(AI_MATKEY_COLOR_EMISSIVE, aColor) == AI_SUCCESS)
materialSlot.Emissive.Color = ToColor(aColor);
bool aBoolean;
if (aMaterial->Get(AI_MATKEY_TWOSIDED, aBoolean) == AI_SUCCESS)
materialSlot.TwoSided = aBoolean;
bool aFloat;
if (aMaterial->Get(AI_MATKEY_ENABLE_WIREFRAME, aBoolean) == AI_SUCCESS)
materialSlot.Wireframe = aBoolean;
float aFloat;
if (aMaterial->Get(AI_MATKEY_OPACITY, aFloat) == AI_SUCCESS)
materialSlot.Opacity.Value = aFloat;
if (aMaterial->Get(AI_MATKEY_GLOSSINESS_FACTOR, aFloat) == AI_SUCCESS)
materialSlot.Roughness.Value = 1.0f - aFloat;
else if (aMaterial->Get(AI_MATKEY_SHININESS, aFloat) == AI_SUCCESS)
materialSlot.Roughness.Value = MaterialSlotEntry::ShininessToRoughness(aFloat);
if (aMaterial->Get(AI_MATKEY_EMISSIVE_INTENSITY, aFloat) == AI_SUCCESS)
materialSlot.Emissive.Color *= aFloat;
if (EnumHasAnyFlags(data.Options.ImportTypes, ImportDataTypes::Textures))
{
@@ -541,6 +553,15 @@ bool ImportMaterials(ModelData& result, AssimpImporterData& data, String& errorM
ImportMaterialTexture(result, data, aMaterial, aiTextureType_EMISSIVE, materialSlot.Emissive.TextureIndex, TextureEntry::TypeHint::ColorRGB);
ImportMaterialTexture(result, data, aMaterial, aiTextureType_NORMALS, materialSlot.Normals.TextureIndex, TextureEntry::TypeHint::Normals);
ImportMaterialTexture(result, data, aMaterial, aiTextureType_OPACITY, materialSlot.Opacity.TextureIndex, TextureEntry::TypeHint::ColorRGBA);
ImportMaterialTexture(result, data, aMaterial, aiTextureType_METALNESS, materialSlot.Metalness.TextureIndex, TextureEntry::TypeHint::ColorRGB);
ImportMaterialTexture(result, data, aMaterial, aiTextureType_DIFFUSE_ROUGHNESS, materialSlot.Roughness.TextureIndex, TextureEntry::TypeHint::ColorRGB);
if (materialSlot.Roughness.TextureIndex != -1 && (data.Path.EndsWith(TEXT(".gltf")) || data.Path.EndsWith(TEXT(".glb"))))
{
// glTF specification with a single metallicRoughnessTexture (G = roughness, B = metalness)
materialSlot.Roughness.Channel = 1;
materialSlot.Metalness.Channel = 2;
}
if (materialSlot.Diffuse.TextureIndex != -1)
{

View File

@@ -1520,6 +1520,9 @@ bool ModelTool::ImportModel(const String& path, ModelData& data, Options& option
const Char* roughnessNames[] = { TEXT("roughness"), TEXT("rough") };
TrySetupMaterialParameter(materialInstance, ToSpan(roughnessNames, ARRAY_COUNT(roughnessNames)), material.Roughness.Value, MaterialParameterType::Float);
TRY_SETUP_TEXTURE_PARAM(Roughness, roughnessNames, Texture);
const Char* metalnessNames[] = { TEXT("metalness"), TEXT("metallic") };
TrySetupMaterialParameter(materialInstance, ToSpan(metalnessNames, ARRAY_COUNT(metalnessNames)), material.Metalness.Value, MaterialParameterType::Float);
TRY_SETUP_TEXTURE_PARAM(Metalness, metalnessNames, Texture);
#undef TRY_SETUP_TEXTURE_PARAM
materialInstance->Save();
@@ -1545,11 +1548,22 @@ bool ModelTool::ImportModel(const String& path, ModelData& data, Options& option
materialOptions.Opacity.Texture = data.Textures[material.Opacity.TextureIndex].AssetID;
materialOptions.Roughness.Value = material.Roughness.Value;
if (material.Roughness.TextureIndex != -1)
{
materialOptions.Roughness.Texture = data.Textures[material.Roughness.TextureIndex].AssetID;
materialOptions.Roughness.Channel = material.Roughness.Channel;
}
materialOptions.Metalness.Value = material.Metalness.Value;
if (material.Metalness.TextureIndex != -1)
{
materialOptions.Metalness.Texture = data.Textures[material.Metalness.TextureIndex].AssetID;
materialOptions.Metalness.Channel = material.Metalness.Channel;
}
if (material.Normals.TextureIndex != -1)
materialOptions.Normals.Texture = data.Textures[material.Normals.TextureIndex].AssetID;
if (material.TwoSided || material.Diffuse.HasAlphaMask)
materialOptions.Info.CullMode = CullMode::TwoSided;
if (material.Wireframe)
materialOptions.Info.FeaturesFlags |= MaterialFeaturesFlags::Wireframe;
if (!Math::IsOne(material.Opacity.Value) || material.Opacity.TextureIndex != -1)
materialOptions.Info.BlendMode = MaterialBlendMode::Transparent;
AssetsImportingManager::Create(AssetsImportingManager::CreateMaterialTag, assetPath, material.AssetID, &materialOptions);