Using Arrays in Materials

Unfortunately I didnt find a way to send custom structs to the shader.
I used MaterialParameterCollection to send specific data via code to my Material.

To do this:

  1. Create a MaterialParameterCollection Asset in the Unreal Editor

  2. Add a specific number of elements to the Array (like 10, 20, 128, 256…)

  3. Get this Asset in code by using ConstructorHelpers:
    static ConstructorHelpers::FObjectFinder MaterialParamCollection(TEXT(“MaterialParameterCollection’/Game/Materials/EchoCollection.EchoCollection’”));

  4. Next is to fill the MaterialParameterCollection:

    FCollectionVectorParameter VectorParam;
    VectorParam.DefaultValue = FLinearColor(1, 0, 0);

    //Generate any Name to find this Parameter in you Collection
    FString Name = FString(“Index_”) + FString::FromInt(index) + FString(“_”);
    VectorParam.ParameterName = *Name;

    // This line and the line below should not be different. I dont know anymore why I chose to do this twice :P. Maybe there is a difference
    ParameterCollection->VectorParameters[index++] = VectorParam;

    UKismetMaterialLibrary::SetVectorParameterValue(GetWorld(), ParameterCollection, VectorParam.ParameterName, VectorParam.DefaultValue);

    //The same procedure goes for FCollectionScalarParameter

To actually iterate through this array you have to write custom expression code in your materials

!!!Warning!!! You have to use one Vector or Scalar Parameter from the Collection as an Input. Otherwise the shader doesnt know your collection

for(int i = 0; i < NumberOfParams; ++i)
{
    float4 VectorParam = MaterialCollection0.Vectors[i];
    //Do something...
}

This should be it :slight_smile:

3 Likes