Change Landscape component material at game run-time?
I can Create Dynamic Material Instance, use Set Material function (Target is Primitive Component, the node says so) for StaticMeshComponent.
However, for LandscapeComponent, neither Set Material function nor Set Override Material (just an exposed property, although BlueprintReadWrite) works, nothing happens using them.
I even can write a function, SetOverrideMaterial(UMaterialInterface* Material), in ULandscapeComponent:
The component's material is changed, but is shown as the default material (checker), although in Blueprint if I get that Overrided Material, Get Base Material, it is the material I set.
ULandscapeComponent::GetLandscapeMaterial() in Landscape.cpp is inclosed in "#if WITH_EDITOR".
All references to ULandscapeComponent::OverrideMaterial are inclosed in "#if WITH_EDITOR".
Changing the entire material in UpdateMaterialInstances is really heavyweight and isn't suitable for use outside the editor.
But we should be able to make a change so the landscape components have a MaterialInstanceDynamic in the front of them, allowing the user to adjust parameter values for nodes that were created in the original Landscape Material.
I've just added a feature request for that under UE-24979
At the moment each component has a MaterialInstanceConstant which points to another MIC (which is shared between components with the same layer usage) which finally points to the LandscapeMaterial.
The final MIC is stored in the MaterialInstance property of the LandscapeComponent. You could change that property to be a MaterialInstanceDynamic and change the code in UpdateMaterialInstances to create a M.I.D instead, under the comment "// Create the instance for this component, that will use the layer combination instance.".
After making this change and then modifying the LandscapeMaterial, each component should be allocated its own MaterialInstanceDynamic and you should be able to change parameters on it at will.
If you do end up getting this to work, please consider submitting a pull request.
I think the target for the dynamic material instance should be the mesh, not the Primitive Component, unless its the mesh object itself, but if that's not it, there is a different way of doing it which I prefer, because it allows gradual changes, basically it involves changing material properties at runtime, its the one way I know how to do this so far:
https://www.youtube.com/watch?v=IqqzKvBc9hc (Dynamic Snow tutorial by Liaret)
answered Jan 05 '16 at 12:47 AM
Follow this question
Once you sign in you will be able to subscribe for any updates here