Materials & Material Instances
A material defines how a surface looks: base colour, metallic, roughness, normal, emissive, opacity. You build it as a node graph in the Material Editor, and Unreal compiles it to shader code for every platform and rendering path. Programmers rarely author the graphs, but they constantly drive them from code: flash white on hit, dissolve on death, team colours, a charging glow.
The material hierarchy
| Asset | Compiles shaders? | Changed at runtime? | Use for |
|---|---|---|---|
Material (UMaterial) |
yes, every change recompiles | no | the graph: M_Character, M_Surface_Master |
Material Instance Constant (UMaterialInstanceConstant, "MI") |
no (unless it changes static switches) | no | variations made in the editor: MI_Enemy_Red, MI_Enemy_Blue |
Material Instance Dynamic (UMaterialInstanceDynamic, "MID") |
no | yes | per-object runtime changes from code or Blueprint |
A material exposes parameters: Scalar (float), Vector (colour), Texture, and Static Switch parameters. Instances override parameter values without recompiling, which is why artists build a few master materials with many parameters and make everything else as instances. Static switches are the exception: each combination compiles a new shader permutation, so use them sparingly.
Driving a material from C++
// ArenaEnemy.h
UPROPERTY()
TObjectPtr<UMaterialInstanceDynamic> BodyMID;
// ArenaEnemy.cpp
#include "Materials/MaterialInstanceDynamic.h"
void AArenaEnemy::BeginPlay()
{
Super::BeginPlay();
// Creates a MID from the material in slot 0 and assigns it to the mesh. Do this once, not per hit.
BodyMID = GetMesh()->CreateDynamicMaterialInstance(0);
}
void AArenaEnemy::FlashOnHit()
{
if (!BodyMID) return;
BodyMID->SetScalarParameterValue(TEXT("HitFlash"), 1.f);
BodyMID->SetVectorParameterValue(TEXT("TeamColor"), FLinearColor(0.9f, 0.1f, 0.1f));
GetWorldTimerManager().SetTimer(FlashTimer, FTimerDelegate::CreateWeakLambda(this, [this]()
{
BodyMID->SetScalarParameterValue(TEXT("HitFlash"), 0.f);
}), 0.1f, false);
}Parameter names are FNames that must match the graph exactly; a typo silently does nothing. Keep the names in one header as constants, and agree on them with the technical artist.
Cheaper alternatives to a MID per object
A MID per enemy is fine for dozens of enemies. For thousands of instances:
- Custom Primitive Data: per-component floats read in the material through the Custom Primitive Data node, set with
Mesh->SetCustomPrimitiveDataFloat(Index, Value)(orSetCustomPrimitiveDataVector4). The material instance is shared, so batching (and Nanite instancing) still works. - Material Parameter Collections (
UMaterialParameterCollection): global parameters every material can read, such as the player's position for grass bending, or a global "danger" tint. Set withUKismetMaterialLibrary::SetScalarParameterValue(this, Collection, Name, Value). - Per-instance custom data on Instanced Static Mesh components for crowds and foliage.
A model: how a parameter value is resolved
When the renderer needs TeamColor for a mesh, it looks it up from the most specific source outward: the MID's override, then each parent instance's override, then the material's default. This model resolves values through that chain.
#include <iostream>
#include <map>
#include <string>
struct MaterialInterface {
std::string Name;
const MaterialInterface* Parent = nullptr; // null for the base Material
std::map<std::string, std::string> Overrides; // the base material holds the defaults here
std::string Get(const std::string& param, std::string& from) const {
if (auto it = Overrides.find(param); it != Overrides.end()) { from = Name; return it->second; }
if (Parent) return Parent->Get(param, from);
from = "-";
return "(no such parameter: silently ignored)";
}
};
int main() {
MaterialInterface master{"M_Character", nullptr, {{"TeamColor", "white"}, {"Roughness", "0.6"}, {"HitFlash", "0"}}};
MaterialInterface red{"MI_Enemy_Red", &master, {{"TeamColor", "red"}}};
MaterialInterface mid{"MID (Grunt_7)", &red, {}};
mid.Overrides["HitFlash"] = "1"; // SetScalarParameterValue at runtime
for (std::string p : {"TeamColor", "Roughness", "HitFlash", "Hitflash"}) {
std::string from;
std::string v = mid.Get(p, from);
std::cout << p << " = " << v << " [from " << from << "]\n";
}
}TeamColor = red [from MI_Enemy_Red]
Roughness = 0.6 [from M_Character]
HitFlash = 1 [from MID (Grunt_7)]
Hitflash = (no such parameter: silently ignored) [from -]
The last line is the typo case: Hitflash instead of HitFlash. The engine doesn't warn about it either.
Practical notes
- Shader compile times dominate iteration for artists. Every change to a master material recompiles every permutation. Keep master materials few and shared.
r.ShaderDevelopmentMode 1gives more detailed shader compile errors.- Material quality and platform: use Quality Switch and Feature Level Switch nodes to give mobile or low settings cheaper paths.
- Translucency is expensive and doesn't sort perfectly. Prefer Masked materials for cut-outs (leaves, fences) and dithered opacity for fades.
- Material functions are reusable sub-graphs, the material equivalent of a helper function.
Test yourself
Which asset type do you create at runtime to change a material parameter on one mesh?
- UMaterial
- UMaterialInstanceConstant
- UMaterialInstanceDynamic
- UMaterialParameterCollection
Why are Static Switch parameters used sparingly?
- They can't be edited
- Each combination compiles a separate shader permutation
- They only work on mobile
- They disable Nanite
What happens if you call SetScalarParameterValue with a misspelled parameter name?
- A compile error
- A crash
- Nothing visible: the value is ignored
- The material resets