简单记录一下调整网格(Mesh)材质参数的方法
修改网格材质参数一般有以下几个步骤:
- 首先创建材质参数(这里有较详细的说明)
- 创建动态材质实例(UMaterialInstanceDynamic)
- 设置动态材质参数(通过 SetVectorParameterValue 等方法)
以下是一段简单的代码示例:
代码语言:javascript复制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);
}
}
}
更多资料
- UE4之材质参数的使用