Dynamic Material

Dynamic material allows us to change material behavior on the Runtime.  This happens through the parameters that can be numeral values or even a Textures. Depending what we want to do, we need to use different parameter type. Vector parameter allows us to change the vector value, Texture parameters allows us to change texture and Scalar parameter allows us to change a single value. Using Dynamic material is always a good idea when compared to creating a multiple materials or material elements for your actor. Using more than one element in material will always increase the Draw Calls for that specific actor and that means, it will take more time to process. By using Dynamic materials, you are only using one Draw Call.


Example

For this example, we need to create a class based on Actor. So we go and do that! In Actor header file, lets create a Root and a Staticmesh Component. They are visible to anyone, but they can be edited inside their own class.

UPROPERTY(VisibleAnywhere, BlueprintReadOnly, meta = (AllowPrivateAccess = "true"))
USceneComponent* RootScene;
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, meta = (AllowPrivateAccess = "true"))
UStaticMeshComponent* MainMesh;;

Next. Let’s create our materials pointers. They can be private. MaterialInterface is like normal material, but it can be used to create Dynamic material! So we need to use that. MaterialInstanceDynamic is the final material we apply into our mesh.

Private:
UPROPERTY()
UMaterialInterface* MaterialToGet;
UPROPERTY()
UMaterialInstanceDynamic* DynamicMaterialToUse;

Inside our .CPP file, lets create our Root and Mesh in constructor.

RootScene = CreateDefaultSubobject(TEXT("Root"));
RootComponent = RootScene;
MainMesh = CreateDefaultSubobject(TEXT("MainMesh"));
MainMesh->SetupAttachment(RootScene);

We leave C++ editor for now. Next, We create our material. Create a Folder called Materials and in there create a material named DynColor.

a1

Open material, right click in Graph and find VectorParameter. Give it a name ”Color”. Plug it in Base Color.

a1

Now, go ahead and create a Blueprint based on our C++ class. Set our mesh to be a Cube or something other you like.

a1

Lets go back to our code! in .CPP we get reference to our MaterialInterface using ConstructorHelpers. Remember to change the path properly, if you have material in different folder. Code goes to class constructor.

static ConstructorHelpers::FObjectFinder DynamicMaterial(TEXT("/Game/Materials/DynColor.DynColor"));
if (DynamicMaterial.Succeeded())
{
MaterialToGet = Cast(DynamicMaterial.Object);
}

In our BeginPlay(), we can finally create Dynamic material, change the color and set it as our new mesh material.

Super::BeginPlay();
if (MaterialToGet)
{
UE_LOG(LogTemp, Warning, TEXT("Material Successfully GET!"));
// Create Color Struct and give it value. Green in this case.
FLinearColor Color;
Color.R = 0;
Color.B = 0;
Color.G = 1;
// Here we actually create the Dynamic Material from the base material!
DynamicMaterialToUse = UMaterialInstanceDynamic::Create(MaterialToGet, this);
// We set new value to our VectorParameter we created in material. Value for vector comes from FLinearColor struct we created.
DynamicMaterialToUse->SetVectorParameterValue("Color", Color);
// Important! We need to change the actual material for the mesh!
MainMesh->SetMaterial(0, DynamicMaterialToUse);
}

Thats it! You can now change materials directly from the code!

Vastaa

Sähköpostiosoitettasi ei julkaista. Pakolliset kentät on merkitty *