Set bool parameter in a constant material instance

How to set a bool (static switch) parameter in a constant material instance (UMaterialInstanceConstant)?

Set*ParameterValue functions with EditorOnly suffix work for other parameter types but not for bool.

thanks,
Emanuel

It looks like you’ll have to do something like this:


UMaterialInstanceConstant* MIC = ...;

if (MIC)
{
	FStaticParameterSet OutStaticParameters;
	MIC->GetStaticParameterValues(OutStaticParameters);

	// ... update OutStaticParameters.StaticSwitchParameters as needed ...
	// ... update OutStaticParameters.StaticComponentMaskParameters as needed ...
	// ... update OutStaticParameters.StaticSwitchParameters as needed ...

	MIC->UpdateStaticPermutation(OutStaticParameters);
}

I think this will force the game to recompile the shader which is not good. You change the material class of that object to MaterialInstanceDynamic instead and operate on it, I did this for landscape ( I needed per landscape component texture ) and it’s pretty easy to do.

As Vebski mentionned already, UpdateStaticPermutation() will recompile the material which will nuke your performances. I’m not even sure that ShaderCompilerWorker is shipped in Shipping (at least it wasn’t shipped in UE3) so it might not even work due to that (which would leave you with the default material instead).

A good trick that works fine for static switches (especially if they optimize out a large amount of instructions) is: if you want to change this value during gameplay and still avoid having the cost of making the parameter dynamic is to have 2 version of the material.
One with the boolean to true and another to false, then you can assign the material depending on the value you want to be rendered.
However, while this is interesting when it changes from time to time, if you plan to switch the material every frame it might still be better to have a dynamic boolean param (once again, it depends on how the static param would optimize out instructions).

Static bools are just that, Static. This also won’t work in a packaged game anyway.

Use a Lerp node and a float to switch between the two possibilities.

If you had read Emanuel’s other thread https://forums.unrealengine.com/showthread.php?126172-Create-a-material-instance you would have probably suggested the same with the knowledge this will be used in an editor environment :stuck_out_tongue:

Unless my assumption was wrong and this was meant to be used ingame in which case you’re right :slight_smile:

While this works for just a single boolean static switch parameter, it will quickly add up many permutations if you add a few more.

this works thanks!

I am loading a 3d model in the editor with custom materials as you know :slight_smile:
This is what I am doing for each material loaded, can you tell me if this logic is correct or anything can be done to optimize?

  • create a constant material instance using a parent material as a template
  • set the material parameters (other then static)
  • get the material static parameter values (in a FStaticParameterSet )
  • update the static parameters (using the FStaticParameterSet reference)
  • To finalize the material update (in the following order):
    NewMaterialInstance->MarkPackageDirty();
    NewMaterialInstance->PostEditChange();
    NewMaterialInstance->UpdateStaticPermutation(FStaticParameterSet &);