On this page
Normal Map Sampling
A normal map is a color texture that is pretending to be a vector. Its texels are packed between zero and one like any other image, but each one secretly means a direction. Sampling a normal map is the texture side of the technique, and it is a short chapter, because the math side already lives in Foundations: Normal Maps.
half3 nrmTS = UnpackNormal(SAMPLE_TEXTURE2D(_NormalMap, sampler_NormalMap, uv));
What a normal map texel actually stores¶
Open a normal map and you see a mottled blue image, and that blue is not a mood, it is data. Each texel stores a unit direction in three channels. The z channel is almost always near its maximum, because the surface mostly points out of itself, which is why the blue channel dominates and the map reads as blue. Small red and green shifts are the direction leaning along the surface, and those shifts are the entire detail the map adds.
The packing problem is a range problem. A stored channel runs from 0 to 1. A direction runs from minus 1 to 1, because it can lean either way along an axis. So the stored value is a remap of the real one, and the sample has to undo it:
// The unpack, spelled out: double it, subtract one.
half3 packed = SAMPLE_TEXTURE2D(_NormalMap, sampler_NormalMap, uv).rgb;
half3 nrm = packed * 2.0 - 1.0; // back to -1..1
nrm = normalize(nrm);
In practice you do not write that. You call UnpackNormal, which does the remap and also handles the platform’s choice of which two channels are stored and which is recovered from the unit length constraint. The reason to know the arithmetic underneath is that it tells you what can go wrong, and what goes wrong is almost always the import flag, not the math.
The three sampling rules¶
It is data, so it stays linear. A normal map must not be sRGB decoded, because the gamma curve would bend the directions and the surface would look subtly wrong at grazing angles, a soft, plasticky sheen that is very hard to diagnose. If a normal map ever looks off for no reason you can name, this is the first thing to check.
Sample it at the same UV as the base map. The map encodes detail relative to the base texture, so it must land on the exact same texel of the same surface. If your base is tiled and the normal map is not, or the two use different UV sets, the detail slides off the pattern and the surface looks smears off.
Do not tint it. The multiply by base color that is the heart of Sampling & Tinting makes sense for color, where the artist wants the hue. A normal map has no hue. Tinting it scales the directions and breaks the unit length, so the rule is simply: color textures get tinted, data textures get unpacked.
One line of the pipeline does all three at once: sample, unpack, then hand the tangent space direction to the TBN matrix that the foundations chapter covers, and the bump is where the artist painted it.
What the shader does¶
The shader samples and unpacks a normal map the standard way. A toggle switches the import interpretation between linear and sRGB on the same map, and the sRGB version visibly over-bends the detail at glancing angles, which is the failure this chapter is about.
Illustrative schematic: Linear vs sRGB on the same normal map.
Sample, unpack, and display. One toggle compares the two import interpretations on the same map.
Shader "DecompiledArt/Textures/Basics/NormalMapSampling/NormalMapSampling"
{
Properties
{
_NormalMap("Normal Map", 2D) = "bump" {}
[MainColor] _BaseColor("Base Color", Color) = (0.5, 0.5, 0.6, 1)
[Enum(Packed,0,Unpacked,1,LinearVsSrgb,2)] _View("View", Float) = 1
}
SubShader
{
Tags { "RenderType" = "Opaque" "RenderPipeline" = "UniversalPipeline" "Queue" = "Geometry" }
Pass
{
Name "ForwardOnly"
Tags { "LightMode" = "UniversalForward" }
HLSLPROGRAM
#pragma vertex Vert
#pragma fragment Frag
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
TEXTURE2D(_NormalMap); SAMPLER(sampler_NormalMap);
struct Attributes
{
float4 positionOS : POSITION;
float2 uv : TEXCOORD0;
};
struct Varyings
{
float4 positionCS : SV_POSITION;
float2 uv : TEXCOORD0;
};
CBUFFER_START(UnityPerMaterial)
half4 _BaseColor;
half _View;
CBUFFER_END
Varyings Vert(Attributes IN)
{
Varyings OUT;
OUT.positionCS = TransformObjectToHClip(IN.positionOS.xyz);
OUT.uv = IN.uv;
return OUT;
}
half4 Frag(Varyings IN) : SV_Target
{
half3 packed = SAMPLE_TEXTURE2D(_NormalMap, sampler_NormalMap, IN.uv).rgb;
half3 out = packed; // packed 0..1
if (_View > 0.5h && _View < 1.5h)
out = UnpackNormal(half4(packed, 1.0h)).xyz * 0.5h + 0.5h;
else if (_View >= 1.5h)
out = pow(packed, half3(2.2, 2.2, 2.2)) * 0.5h + 0.5h; // sRGB decoded
return half4(out * _BaseColor.rgb, _BaseColor.a);
}
ENDHLSL
}
}
FallBack Off
}
The second view shows the unpacked direction remapped back to 0 to 1 for display, so the blue is neutral and the leans are what you are reading.
🔗 Related Chapters¶
Foundations: Normal Maps • Color Data & Sampling • Parallax Mapping
Related functions: Normalize • Remap
❤️ 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.