简单记录一下调整网格(Mesh)材质参数的方法
修改网格材质参数一般有以下几个步骤:
以下是一段简单的代码示例:
void SetMeshMaterial(UMeshComponent* MeshComponent, int MaterialIndex)
{
if (MeshComponent)
{
auto Material = MeshComponent->GetMaterial(MaterialIndex);
if (!Material)
{
// create material if needed
Material = LoadObject(nullptr, TEXT("/Game/TemplateMaterial"));
}
if (Material)
{
// check if material is already dynamic
auto MaterialInstance = Cast<UMaterialInstanceDynamic>(Material);
if (!MaterialInstance)
{
// create dynamic material if needed
MaterialInstance = UMaterialInstanceDynamic::Create(Material, nullptr);
// set mesh material
MeshComponent->SetMaterial(MaterialIndex, MaterialInstance);
}
// now we can set material params by SetVectorParameterValue etc.
//MaterialInstance->SetVectorParameterValue(TEXT("BaseColor"), FLinearColor::White);
}
}
}