TExcelTAD
Home › Shaders & Graphics Programming › Unreal: Materials and Custom HLSL

Unreal: Materials and Custom HLSL

Unreal Engine 5 almost never asks you to write a whole shader. Instead you build a material in the Material Editor, a node graph whose outputs are the inputs of Unreal's own lighting model: Base Color, Metallic, Roughness, Normal, Emissive Color, Opacity and so on. Unreal compiles the graph to HLSL, combines it with its deferred renderer, Lumen, Nanite and shadows, and produces every shader permutation the engine needs. When the nodes run out, the Custom node lets you type HLSL directly.

If you know the lessons on PBR and Rim Light, the Material Editor is just those ideas with boxes and wires.

The material's settings decide the shader

Select the main material node (the big result node) and look at the Details panel. Three settings change what the material can even be:

Setting Common values What it changes
Material Domain Surface, Deferred Decal, Light Function, Post Process, User Interface, Volume where the material is used: on meshes, as a decal, as a full-screen post-process pass, in UMG widgets
Blend Mode Opaque, Masked, Translucent, Additive, Modulate how the result is written: solid, cut out by Opacity Mask, or blended with what is behind
Shading Model Default Lit, Unlit, Subsurface, Clear Coat, Two Sided Foliage, Hair, Cloth, Eye, Thin Translucent which lighting model the GBuffer stores; which output pins are enabled

Output pins that the current settings do not use are grayed out. For example, Opacity Mask only lights up with Blend Mode Masked, and Emissive Color is the only "color" pin that matters for Unlit.

Building a graph, step by step

This graph makes a tinted, scrolling, rim-lit material: a textured surface with a glowing edge, like a force field or a hologram on a solid object.

  1. In the Content Browser, right-click > Material, name it M_RimScroll, and double-click to open it.
  2. Leave Material Domain = Surface, Blend Mode = Opaque, Shading Model = Default Lit.
  3. Add a Texture Coordinate node and a Panner node. Wire the coordinate into the Panner's Coordinate input and set its Speed X to 0.1. The Panner adds Time × Speed to the UVs, so the texture scrolls.
  4. Add a Texture Sample node, pick a texture, and wire the Panner output to its UVs input.
  5. Add a Vector Parameter named Tint. Multiply the Texture Sample's RGB by Tint and wire the result to Base Color.
  6. Add two Scalar Parameter nodes, Roughness (0.5) and Metallic (0), and wire them to the matching pins.
  7. Add a Fresnel node. Its output is about 0 where the surface faces the camera and 1 at grazing angles: the rim term from the Rim Light lesson. Set its Exponent to around 4.
  8. Add a Vector Parameter RimColor and a Scalar Parameter RimStrength. Multiply Fresnel × RimColor × RimStrength and wire the result to Emissive Color. With bloom on, values above 1 glow.
  9. Optional pulse: add a Time node, feed it through a Sine node, remap from −1..1 to 0..1 (Multiply by 0.5, Add 0.5), and multiply it into the rim.
  10. Click Apply (or Save) to compile. The Stats panel (Window > Stats) shows the instruction count for the base pass, a quick way to see the cost of the graph.

To blend between two looks, use Lerp (Unreal's name is LinearInterpolate): inputs A, B and Alpha, exactly like GLSL's mix(a, b, t).

Parameters and Material Instances

Parameters (Scalar, Vector, Texture Sample Parameter 2D, Static Switch) turn a material into a template. Right-click the material in the Content Browser and choose Create Material Instance. The instance has no graph; it only overrides parameter values. That matters:

  • Instances do not compile shaders, so tweaking 50 color variants costs nothing at load time.
  • Changing a parameter at runtime needs a Material Instance Dynamic (in C++, UMaterialInstanceDynamic::Create(...) and then SetScalarParameterValue / SetVectorParameterValue; in Blueprint, Create Dynamic Material Instance).
  • Static Switch parameters are different: each combination compiles a separate shader permutation. Use them for real feature toggles, not for per-object variation.
  • A Material Parameter Collection holds global values (wind direction, wetness, time of day) that many materials read, like a shared uniform buffer.

The Custom node: HLSL inside the graph

The Custom node (search for "Custom" in the palette) holds HLSL. Its Details panel has:

  • Code: an expression (Input.bgr) or a function body ending in return.
  • Output Type: the type of the main output pin, CMOT Float 1 to CMOT Float 4 (or Material Attributes).
  • Description: the title shown on the node.
  • Inputs: a list of named input pins. Each Input Name becomes a variable you use in the code.
  • Additional Outputs, Additional Defines and Include File Paths for larger snippets.

This Custom node computes a rim term with a soft, banded look that the built-in Fresnel node cannot make. Inputs: N (wire PixelNormalWS), V (wire CameraVector), Power, Bands. Output Type: CMOT Float 1.

hlsl
float facing = saturate(dot(normalize(N), normalize(V)));
float rim = pow(1.0 - facing, Power);
// Quantize into a few bands for a toon-style rim
rim = floor(rim * Bands + 0.5) / max(Bands, 1.0);
return rim;

Unreal wraps your code in a generated function whose parameters are the inputs, which explains its rules:

  • The code must return a value of the output type.
  • You cannot declare functions directly in the box. Put helper functions in a .ush file under a plugin or project shader directory and add it with Include File Paths, or define a struct with member functions inside the code.
  • The node blocks constant folding, so it can cost more than the same math built from normal nodes. Use it when nodes cannot express the idea (loops, early-outs, a raymarch) or when the graph would become unreadable.
  • Write explicit types (float3, float4x4) for portability between shader backends.

Tip: CameraVector in Unreal points from the surface toward the camera, and PixelNormalWS is the final world-space normal after the Normal input. Both are already in world space, so you can dot them directly.

Seeing the HLSL

The Material Editor can show the HLSL it generated for the graph (in UE 5 the entry is under Window > Shader Code > HLSL Code; check the Window menu of your version). It is long, but searching for your Custom node's description or for CalcPixelMaterialInputs shows how your nodes became code. For deeper GPU work, Unreal integrates RenderDoc through a built-in plugin, covered in Debugging Shaders.

Writing global shaders

Engine programmers sometimes write global shaders: .usf files with a C++ class derived from FGlobalShader, used for custom post-processing or compute passes through the Render Dependency Graph (RDG). That path needs a C++ module with a shader directory mapping and is far more involved than materials; start with materials and Custom nodes, and see the Unreal course and Game Engine Architecture for the C++ side.

Test yourself

Which Blend Mode enables the Opacity Mask pin for cut-out materials such as foliage or dissolve?

  1. Opaque
  2. Masked
  3. Additive
  4. Modulate

Why do Material Instances load faster than separate materials with different constant values?

  1. They use lower texture resolution
  2. They don't compile new shaders; they only override parameter values
  3. They skip lighting
  4. They are baked into the level

What must the Code of a multi-line Custom node end with?

  1. A semicolon-free expression
  2. A return statement of the output type
  3. #pragma once
  4. Nothing special