Material Parameter Collections and Code?

Hello,

I have been having issues of working with a material parameter collection in C++. So far I haven’t found a working solution to accessing a parameter collection made in editor and changing the values in code.
I have been trying to get it work since yesterday and found little info on the subject on the forums or answerhub. Either I’m not quite understanding how they work with code or I’m missing something.
Overall I’m stumped on how to make this work.

Any help is appreciated.

Thanks

1 Like

Take a look at KismetMaterialLibrary.cpp (the SetScalarParameterValue and SetVectorParameterValue functions). They basically do this:

UMaterialParameterCollectionInstance* Instance = World->GetParameterCollectionInstance(Collection);
Instance->SetScalarParameterValue(ParameterName, ParameterValue);
1 Like

Thank you, that did the trick :)!

Added a reference to the Collection Parameter and used that for the GetParameterCollectionInstance.
Found I could reference it as a UProperty and access from anywhere in my class found on https://docs.unrealengine.com/latest/INT/Programming/Assets/ReferencingAssets/

So far though I’m wondering if this is the best method to do this or if there is another more efficient way? Since they list 4 of them and I’m still learning I’m not sure which of these is best for my case.

Adding a property as a reference to the collection is probably the best way. You could just load the asset by name, but that won’t work properly for cooked content (you’ll have to force that asset to be cooked). If you have something that references it and the class you add the property to gets cooked (like PlayerController or Pawn), then the referenced asset (the collection) will also get cooked.

Hi. Just in case someone wonder how directly load de collection. But keep in mind the above warning about cooked content.




// Class variable
UMaterialParameterCollection* collection;
...
// Inside constructor:
...
ConstructorHelpers::FObjectFinder<UMaterialParameterCollection> objectType(TEXT("/Game/Project/Materials/MPC_Params"));
if (objectType.Succeeded())
{
collection = objectType.Object;
}
...
// when needed
UMaterialParameterCollectionInstance * pci = GetWorld()->GetParameterCollectionInstance(collection);
if (pci)
{
bool found = pci->SetVectorParameterValue(FName("ParamName"), FLinearColor(400, 400, 0, 0));
...
}
}

3 Likes

I’m looking for how to add an index. Basically make the parameter collection dynamic. I have to send a 512 sized float4 array. Looked around in the class and could not find anything about it. theres only functions about setting values and stuff. I guess i have to add all 512 indexes one by one. How frustrating…

I could declare a float4 in unity like usual, float4 vec[512]; and then send the whole array from c sharp into shader with 1 line of code. Any one knows how to so it directly?