Skip to content
Tutorials Center
My learning

Topics

Lesson 26 of 38 ยท Materials and Lighting

Material Instances and Parameters

Create flexible Unreal Engine materials with scalar and vector parameters, Material Instances, parameter collections and dynamic material instances changed at run time in C++.

Compiling a material is expensive: every change to the node graph recompiles shaders, which can take seconds to minutes. If you need fifty variations of the same look, say fifty colors of the same car paint, you do not want fifty graphs. The solution is parameters and Material Instances.

The editor cannot run on this site, so the editor steps are described in words, the C++ is read-only and the Python stand-ins model the parameter system.

Parent materials and parameters

A parent material is a normal material in which some values are exposed as parameters. To expose one, right-click a Constant or Constant3Vector node and choose Convert to Parameter (or create a ScalarParameter, VectorParameter or TextureSampleParameter2D node directly). Give it a clear name such as BaseTint, RoughnessScale or DirtAmount, plus a default value and optionally a Group and Sort Priority to organize the instance panel.

Material Instances

In the Content Browser, right-click the parent material and choose Create Material Instance. The result, usually named MI_..., shows every exposed parameter with a checkbox. Tick the box to override it; unticked parameters inherit from the parent.

Key facts:

  • Changing an instance's parameters does not recompile shaders, so changes are instant and cheap.
  • An instance can have another instance as its parent, forming a chain (parent, then variants, then sub-variants). Keep chains short.
  • Static Switch Parameters and Static Component Mask Parameters do trigger shader permutations, since they change the compiled code. Use them sparingly and only when a branch should be removed entirely.
  • Instances are small assets, so thousands can ship without bloating packages.
Python Stand-in: parameter inheritance in instances

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Dynamic material instances at run time

A Material Instance asset is fixed at edit time. To change a value while the game runs (a flashing hit effect, a health bar fill, a team color), create a Material Instance Dynamic (MID) from C++ or Blueprint:

C++
void AEnemy::BeginPlay()
{
    Super::BeginPlay();

    // Create from the material currently in slot 0 and assign it back
    DynamicMat = Mesh->CreateAndSetMaterialInstanceDynamic(0);
}

void AEnemy::OnHit()
{
    if (DynamicMat)
    {
        DynamicMat->SetScalarParameterValue(TEXT("HitFlash"), 1.0f);
        DynamicMat->SetVectorParameterValue(TEXT("TeamColor"), FLinearColor::Red);
    }
}

DynamicMat should be a UPROPERTY() TObjectPtr<UMaterialInstanceDynamic> so the garbage collector keeps it alive (see Memory and garbage collection). Parameter names are matched as strings, so a typo silently does nothing. Define the names once as constants.

Each MID is a separate object: creating one per Actor is fine for dozens of Actors, but creating thousands per frame is wasteful. For values shared by many materials, use a Material Parameter Collection.

Material Parameter Collections

A Material Parameter Collection (MPC) is an asset holding named scalar and vector parameters that any material can read with a CollectionParameter node. Change it once from C++ or Blueprint and every material that uses it updates, which is ideal for global values such as wind strength, a day/night factor or a global wetness amount.

C++
UMaterialParameterCollectionInstance* Inst =
    GetWorld()->GetParameterCollectionInstance(WeatherMPC);
Inst->SetScalarParameterValue(TEXT("Wetness"), 0.8f);
Need Tool
Variants set up in the editor Material Instance (constant)
Per-Actor values changed in play Material Instance Dynamic
One value shared by many materials Material Parameter Collection
A branch removed from compiled code Static Switch Parameter (in an instance)
Python Stand-in: shared parameter collection

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Good practice

  • One master material per surface family (opaque props, foliage, glass, decals) with many instances beats hundreds of unique graphs.
  • Name parameters consistently across masters so they can be reused by code and tools.
  • Group parameters in the parent so the instance panel is readable.
  • Check the Material Instance's Stats and use Shader Complexity view (Alt+8 in the viewport) to find expensive masters.
  • Avoid MIDs on every Actor for static scenery. Instances in the asset are enough.

Common mistakes

  • Using Material Instance Constant at run time. You cannot change a constant instance in play; make a dynamic one.
  • Typos in parameter names. The call is silently ignored.
  • Too many static switches. Each combination compiles a separate shader permutation, increasing load time and package size.
  • Not storing the MID. If it is not referenced by a UPROPERTY, it can be collected.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why use Material Instances instead of copying a material?

    Instances override exposed parameters on a shared master, so changes are instant and the shader is reused.
  2. 2. Which asset do you create to change a material parameter while the game runs?

    A dynamic instance is created at run time, for example with CreateAndSetMaterialInstanceDynamic.
  3. 3. What is a Material Parameter Collection best suited for?

    An MPC is read through CollectionParameter nodes by any material, so one change updates them all.
  4. 4. What happens if you misspell a parameter name in SetScalarParameterValue?

    Names are matched as strings at run time; an unknown name does nothing.

Summary

  • Expose values as parameters in a parent material, then create Material Instances to vary them.
  • Instance changes do not recompile shaders; static switches do.
  • Use CreateAndSetMaterialInstanceDynamic for run-time changes, and keep a UPROPERTY reference.
  • Use a Material Parameter Collection for globally shared values.

Next: Lighting: Lights, Lumen and Mobility