On this page
Normal Maps
A normal map lets a low-poly surface look like it has bumps, grooves, and pores, without adding a single triangle. It is the single most important surface-detail tool in real-time graphics, and it rests on one small idea: lighting reads a normal, so we can feed it a better normal.
// The whole trick, in one line:
// light the map's normal, not the flat geometric one.
half3 shadeNormal = TBN * UnpackNormal(SAMPLE_TEXTURE2D(_NormalMap, sampler, uv));
The problem: flat normals light flat¶
Lighting is a conversation between a light and a normal. Every model in this library asks, in some form, how directly the light hits the surface, and that question is answered by the normal. A low-poly mesh has simple, flat normals, so it lights flat. A rock, a brick wall, or a leather surface all look wrong for the same reason: the geometry is too smooth to catch light the way the real thing does.
The obvious fix is to add geometry, thousands of tiny triangles, so the surface has real bumps to light. That is what it would cost, and it is what you cannot afford, especially on a phone or a scene with a million objects. You need the look of detail without the cost of geometry.
The idea: change the normal, not the shape¶
A normal map stores a per-pixel normal in a texture. The geometry stays exactly as flat as it was. But in the fragment stage, instead of lighting the flat geometric normal, you light the normal read from the map. The surface still has the same smooth silhouette, the same outline, the same memory cost. Only the normal that lighting uses has changed, and that is enough to make a smooth quad read as a bumpy, detailed surface. You are not adding geometry. You are lying, gently, to the lighting.
The catch: a normal needs a frame¶
Here is the part that trips people up. A normal is a direction, and a direction is meaningless unless you say relative to what. The flat geometric normal points out of the surface, so it is unambiguous. But the small, tilted normals in a normal map need a reference frame to be interpreted, and that frame is built right on the surface from three vectors at each point.
The tangent runs along the U direction of the UV. The bitangent runs along the V direction. The normal points out of the surface. Together the three form a small local coordinate system, and the matrix that holds them is called the TBN matrix. The normal map stores its normals in exactly this frame, called tangent space, because that frame moves with the surface and with the texture. The same normal map can be reused on differently oriented parts of a mesh, and the detail stays glued to the UVs instead of floating in the world. That is why tangent space is the default, and why a tangent is exactly the attribute you need the moment you add a normal map.
Putting it together¶
The normal map pipeline is five steps, and once you have done it a few times it becomes second nature.
- In the vertex stage, take the mesh’s normal and tangent, transform them to world space, and pass them, together with the UV, to the fragment stage.
- In the fragment stage, build the TBN matrix from those three world-space vectors.
- Sample the normal map and unpack it from the stored color range to a real vector.
- Multiply that tangent-space normal by the TBN to rotate it into world space.
- Light the surface with that perturbed normal instead of the flat geometric one.
There is one small detail worth knowing at step three. A normal map is a texture, so each of its channels is stored between zero and one. A real normal, though, ranges from minus one to one, because it can point either way along an axis. So you remap the stored color, double it, subtract one, and you are back in the range a normal needs. On many platforms the map is also packed to save memory, storing two of the three channels and recovering the third from the fact that a unit vector must satisfy x squared plus y squared plus z squared equals one. The UnpackNormal helper does all of that, including choosing the right layout for the platform, which is why you reach for it instead of doing the arithmetic by hand.
What the shader does¶
The shader blends a base map with a normal map and lights it. The Normal Scale property stretches how far the map’s normals tilt, and the toggle switches the normal map on or off, so the same flat surface goes from smooth to detailed. The geometry never moves; only the normals the lighting pass reads change.
Illustrative schematic: Same geometry, smooth vs normal-mapped.
A base color, an optional base map, and a normal map. The vertex stage builds the world-space frame, the fragment stage unpacks and transforms the map’s normals, and a toggle lets you compare the flat geometric normal against the perturbed one on the same surface.
Shader "DecompiledArt/Foundations/NormalMaps/NormalMaps"
{
Properties
{
[MainColor] _BaseColor("Base Color", Color) = (0.5, 0.6, 0.8, 1.0)
_BaseMap("Base Map", 2D) = "white" {}
_NormalMap("Normal Map", 2D) = "bump" {}
_NormalScale("Normal Scale", Range(0.0, 4.0)) = 1.0
[Toggle(_USE_NORMALMAP)] _UseNormalMap("Use Normal Map", Float) = 1.0
_Ambient("Ambient", Range(0.0, 1.0)) = 0.25
}
SubShader
{
Tags { "RenderType" = "Opaque" "RenderPipeline" = "UniversalPipeline" "Queue" = "Geometry" }
Pass
{
Name "ForwardOnly"
Tags { "LightMode" = "UniversalForward" }
HLSLPROGRAM
#pragma vertex Vert
#pragma fragment Frag
#pragma shader_feature_local _USE_NORMALMAP
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
TEXTURE2D(_BaseMap); SAMPLER(sampler_BaseMap);
TEXTURE2D(_NormalMap); SAMPLER(sampler_NormalMap);
struct Attributes
{
float4 positionOS : POSITION;
float3 normalOS : NORMAL;
float4 tangentOS : TANGENT;
float2 uv : TEXCOORD0;
};
struct Varyings
{
float4 positionCS : SV_POSITION;
float2 uv : TEXCOORD0;
half3 normalWS : TEXCOORD1;
half3 tangentWS : TEXCOORD2;
half3 bitangentWS : TEXCOORD3;
};
CBUFFER_START(UnityPerMaterial)
half4 _BaseColor;
float _NormalScale;
half _Ambient;
CBUFFER_END
Varyings Vert(Attributes IN)
{
Varyings OUT;
VertexPositionInputs p = GetVertexPositionInputs(IN.positionOS);
OUT.positionCS = p.positionCS;
OUT.uv = IN.uv;
// Build the world-space frame from the mesh's normal and tangent.
VertexNormalInputs n = GetVertexNormalInputs(IN.normalOS, IN.tangentOS);
OUT.normalWS = n.normalWS;
OUT.tangentWS = n.tangentWS;
OUT.bitangentWS = n.bitangentWS;
return OUT;
}
half4 Frag(Varyings IN) : SV_Target
{
half3 normalWS = normalize(IN.normalWS);
// Assemble the TBN matrix from the three world-space frame vectors.
half3x3 TBN = half3x3(IN.tangentWS, IN.bitangentWS, IN.normalWS);
// Unpack the map to a tangent-space vector, then rotate it to world space.
half3 nrmWS = normalWS;
#ifdef _USE_NORMALMAP
half3 nrmTS = UnpackNormal(SAMPLE_TEXTURE2D(_NormalMap, sampler_NormalMap, IN.uv));
nrmTS *= _NormalScale;
nrmWS = normalize(TBN * nrmTS);
#endif
half3 albedo = _BaseColor.rgb * SAMPLE_TEXTURE2D(_BaseMap, sampler_BaseMap, IN.uv).rgb;
Light mainLight = GetMainLight();
half NdotL = saturate(dot(normalize(nrmWS), mainLight.direction));
half3 col = albedo * (_Ambient + mainLight.color * NdotL * mainLight.shadowAttenuation);
return half4(col, _BaseColor.a);
}
ENDHLSL
}
}
FallBack Off
}
🔗 Related Chapters¶
Mesh Data • Normals Reconstruction • Blinn-Phong
Related functions: Cross • Normalize • Dot
❤️ Support Shader Story¶
If this article helped you, consider supporting the project on Patreon: you’ll get access to the related source files and reference cheat-sheets.
Your support helps keep this library open and free for everyone.