How to edit terrain tileset marterial at runtime?

Hello All,
I wonder is there a way to edit world terrain tileset material at runtime ? I follow the Material Editing tutorial ,it works fine. But i want to change the color parameter in my custom material layer at runtime, it doesn’t work.
For this goal,it’s a two-step process:

  1. Get the UMaterialInstanceDynamic of tileset material,i did it by this:
void ASMainActor::onTileLoaded()
{
    TArray<UStaticMeshComponent*>aa; 
    BTerrain->GetComponents(aa);
    if (aa.Num() > 0) {
        int kcount = aa.Num();			
        for (auto& item : aa)
        {
            UMaterialInstanceDynamic* pMaterial =
                Cast<UMaterialInstanceDynamic>(item->GetMaterial(0));
            SetColor(pMaterial);
        }
    }
}	

*BTerrain is the world terrain tileset
SetColor is one function that change the material layer parameter

  1. Set the vectorparameter of BlendParameter,like that:
void ASMainActor::SetColor(UMaterialInstanceDynamic* _pIndynamic)
{
	UMaterialFunctionMaterialLayerBlend* pMF = LoadObject<UMaterialFunctionMaterialLayerBlend>(NULL, TEXT("MaterialFunctionMaterialLayerBlend'/Game/Materials/MLB_AreaTint.MLB_AreaTint'"));

	FMaterialParameterInfo xxy = _pIndynamic->GetParameterInfo(EMaterialParameterAssociation::BlendParameter, FName("BColor"), pMF);

	FMaterialParameterInfo mmy(xxy.Name, xxy.Association, xxy.Index);
	_pIndynamic->SetVectorParameterValueByInfo(mmy, FLinearColor::Blue);
}

It didn’t work.What should I do?
Thanks in Advance.

Jedijin, look into Material Parameter Collections. This creates a modifiable color value that can be plugged in as a variable into your material. Changing the value in a collection at runtime will make your material follow suit

1 Like

@mahalobay hi,thanks for reply! I followed your method ,but it didn’t work. It can change the color at editor but it has no effect at runtime.

Hi jedijin, without seeing how your material is set up it would be hard to debug. I would try setting it up on a sample material first - ie, make a standard material, assign it to a test cube, and set up a blueprint to change the parameters at runtime. Once you figure out how it works you can implement it into your cesium setup. Here’s how the material should be set up:

Once this is set up, you need a function at runtime to modify this value. In this example, I use the F and G keys to extract the color value, increment/decrement the red value, and reinject this into the parameter collection. This will propagate to the material at runtime.

Based on what you said, it’s possible that you just need to do the second part.

1 Like

@mahalobay :smiley: Thanks a lot .It works fine

Good to hear!