/ English Language

Tick Loop: Set parameters of an instanced material

I got few car models. The models already have materials and texture. There is even a special material for the headlights and brake with the right texture mask. The only thing I have to do is to set the parameter of the instanciated material and switch to true or false a boolean. Before start, I wanted to know how works material with Unreal Engine, I already knew how to set parameters from blueprints but not from the C++. Now I am able to change on the fly the parameters of a material in the update loop.

car/content/images/2017/07/Screenshot_Car_back.PNG

Material

Material

Constructor

AMyPlaneActor::AMyPlaneActor() {
	PrimaryActorTick.bCanEverTick = true;

	static ConstructorHelpers::FObjectFinder<UStaticMesh> mesh(TEXT("StaticMesh'/Game/MyPlaneStaticMesh.MyPlaneStaticMesh'"));
	if (mesh.Succeeded() == false) {
		return;
	}
	static ConstructorHelpers::FObjectFinder<UMaterial> tmpMaterial(TEXT("Material'/Game/M_Test.M_Test'"));
	if (tmpMaterial.Succeeded() == false) {
		return;
	}
	material = tmpMaterial.Object;
	GetStaticMeshComponent()->SetStaticMesh(mesh.Object);
	materialInstance = UMaterialInstanceDynamic::Create(material, nullptr);
	GetStaticMeshComponent()->SetMaterial(0, materialInstance);
}

Tick

void AMyPlaneActor::Tick(float dt) {
	Super::Tick(dt);
	lifetimeActor += dt;
	materialInstance->SetScalarParameterValue(TEXT("InnerRadius"), fInnerParam);
	materialInstance->SetScalarParameterValue(TEXT("OuterRadius"), fOuterParam);
}

Header private attributes

public:
	float									fInnerParam;
	float									fOuterParam;

private:
	float									lifetimeActor;
	UMaterial*								material;
	UMaterialInstanceDynamic*				materialInstance;