UE4 微笔记 之 调整网格材质参数

2021-12-06 11:21:43 浏览数 (1)

简单记录一下调整网格(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之材质参数的使用

0 人点赞