Skip to content
On this page

Parallax Mapping

A flat texture looks flat, and the eye knows it. Parallax mapping is the trick that makes a flat surface read as deep: it uses a height map to shift the UVs along the view direction before sampling, so the details appear to lean toward and away from the camera as you move; the plane stays one quad and the surface gains the look of bumps and recesses.

// The whole effect in three lines:
float  h   = SAMPLE_TEXTURE2D(_HeightMap, s, uv).r;
float2 off = h * _Parallax * viewDirT.xy / max(viewDirT.z, 0.1);
float2 uvOut = uv + off;

The optical effect, used in reverse

Parallax is a real effect: when you move, near things move fast and far things move slow. Look through a window and the frame slides while the distant view barely does. Parallax mapping runs the illusion backwards, from the image to the depth. The shader takes the view direction into the surface’s tangent space, so it knows which way “toward the viewer” is across the texture, and it shifts the sampling UV in the opposite direction, scaled by a height map. High places shift one way, low places the other, and to the eye the texture has volume.

The tangent space detail is load bearing, and it is the reason this technique lives in the same neighborhood as Normal Maps. The view direction has to be expressed in the frame where the texture lives, the frame built from the mesh’s tangent, bitangent, and normal, or the shift points the wrong way and the surface appears to sink instead of rise.

// View direction, carried into the texture's own frame:
half3  viewDirT = TBN * normalize(GetWorldSpaceViewDir(positionWS));

Shader Story: Textures - Parallax Mapping

The single step, and its honest limit

The basic version, a single step, does one shift and one sample: read the height at the UV, push the UV by that height times the view direction, sample the color at the pushed spot. It is cheap, one extra texture read, and it reads as depth at moderate angles. Its limit is the steep ones. At a grazing view the true ray into the surface travels a long way across the texture, and a single step does not follow that travel, so the effect under plays and can slide the sample off the edge of the geometry, the classic artifact of the cheap version.

The upgraded version is parallax occlusion mapping, a small loop: march the height in steps along the view direction, each step asking “is the height here still above my ray?”, until it finds where the ray crosses the surface, then sample there. Eight steps is a common budget, and the result holds up at angles the single step gives up on. The loop is the cost, and the loop is the point: parallax quality is bought in samples, exactly like manual filtering buys blur in samples, and the same discipline applies, use the cheap version where the angles are kind and the loop where they are not.

The height map is just a grayscale texture, linear, no sRGB, same data texture habit as Normal Map Sampling. And because the UV has moved, everything that should stay glued to the surface, the color, the normal map, gets the moved UV, which is the small detail that makes the depth read as real: the bump and its shading move together.


What the shader does

The shader applies parallax mapping to a bumpy texture on a flat plane, with a single-step mode and a stepped mode on a toggle. The head-on view is almost identical between the two, which is the point of the cheap one; at grazing angles the single step smears and clips while the stepped version keeps its volume.

Shader Story: Textures: Parallax Mapping - schematic

Illustrative schematic: One step vs eight steps; the cheap one smears at grazing angles.


Single step by default, with a stepped mode that runs a short march, and a height map that drives everything.

Shader code
Shader "DecompiledArt/Textures/Projections/ParallaxMapping/ParallaxMapping"
{
    Properties
    {
        _MainTex("Color Texture", 2D) = "white" {}
        _HeightMap("Height Map (linear)", 2D) = "bump" {}
        [MainColor] _BaseColor("Base Color", Color) = (1, 1, 1, 1)
        _Parallax("Parallax Height", Range(0, 0.3)) = 0.1
        [Enum(SingleStep,0,Stepped,1)] _Mode("Mode", Float) = 0
        _Steps("Steps (Stepped mode)", Range(1, 16)) = 8
    }

    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(_MainTex);     SAMPLER(sampler_MainTex);
            TEXTURE2D(_HeightMap);   SAMPLER(sampler_HeightMap);

            struct Attributes
            {
                float4 positionOS : POSITION;
                float3 normalOS   : NORMAL;
                float4 tangentOS  : TANGENT;
                float2 uv         : TEXCOORD0;
            };

            struct Varyings
            {
                float4 positionCS  : SV_POSITION;
                float3 positionWS  : TEXCOORD0;
                half3  normalWS    : TEXCOORD1;
                half3  tangentWS   : TEXCOORD2;
                half3  bitangentWS : TEXCOORD3;
                float2 uv          : TEXCOORD4;
            };

            CBUFFER_START(UnityPerMaterial)
                half4 _BaseColor;
                float _Parallax;
                half  _Mode;
                half  _Steps;
            CBUFFER_END

            Varyings Vert(Attributes IN)
            {
                Varyings OUT;
                VertexPositionInputs p = GetVertexPositionInputs(IN.positionOS);
                OUT.positionCS  = p.positionCS;
                OUT.positionWS  = p.positionWS;
                OUT.uv          = IN.uv;

                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
            {
                half3x3 TBN = half3x3(IN.tangentWS, IN.bitangentWS, IN.normalWS);
                half3  viewDirT = TBN * normalize(GetWorldSpaceViewDir(IN.positionWS));

                float2 uv = IN.uv;
                if (_Mode < 0.5h)
                {
                    // Single step: one height read, one shift.
                    float h = SAMPLE_TEXTURE2D(_HeightMap, sampler_HeightMap, uv).r;
                    uv += (viewDirT.xy / max(viewDirT.z, 0.1)) * h * _Parallax;
                }
                else
                {
                    // Stepped: march along the view until the ray crosses the height.
                    float stepSize = _Parallax / _Steps;
                    float h = SAMPLE_TEXTURE2D(_HeightMap, sampler_HeightMap, uv).r;
                    for (float i = 1.0; i <= _Steps; i++)
                    {
                        float2 candidate = uv - (viewDirT.xy / max(viewDirT.z, 0.1)) * stepSize * i;
                        float  hNext = SAMPLE_TEXTURE2D(_HeightMap, sampler_HeightMap, candidate).r;
                        if (hNext < h * (1.0 - i / _Steps) * 0.5)
                            uv = candidate;
                    }
                }

                uv = clamp(uv, 0.0, 1.0);   // never let the march leave the surface
                half3 color = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, uv).rgb;
                return half4(_BaseColor.rgb * color, _BaseColor.a);
            }

            ENDHLSL
        }
    }

    FallBack Off
}

The clamp at the end is the guard against the one failure the eye catches first, the sample sliding off the edge of the texture at a hard angle.


Foundations: Normal Maps • Normal Map Sampling • Interior Mapping

Related functions: Clamp/Saturate • Normalize


❤️ 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.

DecompiledArt on Patreon DecompiledArt on Buy Me a Coffee

Your support helps keep this library open and free for everyone.