In this post I'm going to show you how to create latent Blueprint nodes that…
In this post we’re going to see how we can communicate with the Material Editor and change specific parameters of our Materials during runtime.
Creating a dummy Material
Create a material which consists of two parameters:
- A Vector Parameter, connected to the base color input
- A Scalar Parameter, connected to the metallic value
Here is a screenshot of my material so far:
Note that I’ve named both parameters with a desired name. In order to name a parameter, click on it and then modify the Parameter Name value.
Accessing material parameters
Create a C++ class which inherits the Actor class and add the following component inside the header file:
/*The static mesh of the actor*/
Then, inside the source file, type in the following code:
// Sets default values
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
//Initialize the static mesh component
StaticMesh = CreateDefaultSubobject<UStaticMeshComponent>(FName("StaticMesh"));
// Called when the game starts or when spawned
//Get the first material of the static mesh and turn it into a material instance
UMaterialInstanceDynamic* DynamicMatInstance = StaticMesh->CreateAndSetMaterialInstanceDynamic(0);
//Generate random RGB and Alpha values for our Vector Parameter
RandomColor.R = FMath::RandRange(0, 1);
RandomColor.G = FMath::RandRange(0, 1);
RandomColor.B = FMath::RandRange(0, 1);
RandomColor.A = FMath::RandRange(0, 1);
//If we have a valid dynamic material instance, modify its parameters
DynamicMatInstance->SetScalarParameterValue(FName("MetalParam"), FMath::RandRange(0, 1));
Once you’re done with that, compile your code and create a Blueprint based on your C++ class. Then, choose a static mesh of your desire and assign to the first Material slot the material you have created in the first step of this post:
When you’re done with that, place some actors on your level and play the game.
Check out some results of mine: