Skip to content
On this page

Billboarding

A billboard is geometry that refuses to be seen from the side. Leaves, fire, pickups, health bars above heads: all of them should turn to face the camera no matter where you walk. Billboarding does that in the vertex stage, in a few lines, without the object’s transform ever knowing.

// Rebuild the quad in the view frame, keep only x and y, done:
float4 viewPos = TransformWorldToView(centerWS);
viewPos.xyz   += IN.positionOS.xyz;   // local offset, now camera aligned
OUT.positionCS = TransformViewToHClip(viewPos);

Why not just rotate the object

The obvious answer is to make the object look at the camera every frame, a rotation computed on the CPU. It works, and it is wrong for the moment you have a thousand of them, because each one is a transform update, a CPU cost per object per frame, and a CPU to GPU round trip for data the GPU already has. The shader knows where the camera is, in a constant it already uses every frame, so the rotation can happen where the data lives.

The construction the book by Pêcheux walks through, and the one this chapter uses, is elegant because it never builds a rotation matrix. Take the object’s center, convert it to view space, throw away the x and y of that view space position and keep the z depth, then add the vertex’s local x and y offset back in. In view space, the camera’s forward is the z axis and the screen axes are x and y, so a quad built from local x and y at that spot is, by definition, facing the camera. Convert to clip space and you are done:

// Center of the object, carried along in view space.
float4 centerVS = TransformWorldToView(TransformObjectToWorld(float3(0, 0, 0)));

// The vertex: same spot, but its local x and y now live in the screen plane.
float4 posVS = centerVS;
posVS.x += IN.positionOS.x;
posVS.y += IN.positionOS.y;
// posVS.z is left alone, so the quad sits at the object's depth

OUT.positionCS = TransformViewToHClip(posVS);

The quad’s local z is ignored, because the quad is flat and a full billboard has no depth to speak of. Every vertex ends up on the screen plane at the object’s distance, which is exactly “a piece of 2D content anchored at a 3D point”.

Shader Story: Textures - Billboarding

The variations you will actually meet

A full billboard is the version above, the quad turns on every axis. A cylindrical billboard locks one axis, usually world up: the quad swings to face the camera around the vertical axis only, so it never tips, which is the right choice for signs, trees, and anything that should respect the horizon. The cylindrical version keeps the local y and rotates x and z with the horizontal view angle instead of rebuilding in the view frame. And a screen aligned plane is a billboard taken to its conclusion: the quad is rebuilt to sit at a fixed screen size no matter the distance, the classic sprite that stays readable from any range, which is a health bar’s whole job.

The trade to remember: a billboard is a plane, and a plane from the side is nothing. The technique buys you guaranteed readability by giving up silhouette, so it belongs on things that are flat to look at anyway, leaves, smoke, icons. Put it on a character and you have made a poster, which is sometimes the point, usually not.

For the UV side of a billboard, see Sprite-Space UVs: the geometry that always faces the camera is exactly the geometry whose texture should fill it corner to corner.


What the shader does

The shader billboards the mesh to face the camera, with a cylindrical variant that swings but never tips. The full and cylindrical variants on a toggle show the visible difference the lock makes.

Shader Story: Textures: Billboarding - schematic

Illustrative schematic: Full billboards face the camera; cylindrical swings without tipping.


The view frame rebuild with a toggle for full, cylindrical, and off, so the difference is visible on the same quad.

Shader code
Shader "DecompiledArt/Textures/Projections/Billboarding/Billboarding"
{
    Properties
    {
        _MainTex("Texture", 2D) = "white" {}
        [MainColor] _BaseColor("Base Color", Color) = (1, 1, 1, 1)
        [Enum(Off,0,Full,1,Cylindrical,2)] _Mode("Mode", Float) = 1
    }

    SubShader
    {
        Tags { "RenderType" = "Transparent" "RenderPipeline" = "UniversalPipeline" "Queue" = "Transparent" }
        Cull Off

        Pass
        {
            Name "ForwardOnly"
            Tags { "LightMode" = "UniversalForward" }
            Blend One OneMinusSrcAlpha
            ZWrite Off

            HLSLPROGRAM
            #pragma vertex Vert
            #pragma fragment Frag

            #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"

            TEXTURE2D(_MainTex);   SAMPLER(sampler_MainTex);

            struct Attributes
            {
                float4 positionOS : POSITION;
                float2 uv         : TEXCOORD0;
            };

            struct Varyings
            {
                float4 positionCS : SV_POSITION;
                float2 uv         : TEXCOORD0;
            };

            CBUFFER_START(UnityPerMaterial)
                half4 _BaseColor;
                half  _Mode;
            CBUFFER_END

            Varyings Vert(Attributes IN)
            {
                Varyings OUT;
                OUT.uv = IN.uv;

                float3 centerWS = TransformObjectToWorld(float3(0, 0, 0));

                if (_Mode < 0.5h)
                {
                    // Off: the plain transform.
                    OUT.positionCS = TransformObjectToHClip(IN.positionOS.xyz);
                }
                else if (_Mode < 1.5h)
                {
                    // Full billboard: rebuild in the view frame.
                    float4 posVS = TransformWorldToView(centerWS);
                    posVS.x += IN.positionOS.x;
                    posVS.y += IN.positionOS.y;
                    OUT.positionCS = TransformViewToHClip(posVS);
                }
                else
                {
                    // Cylindrical: swing around world up only.
                    // Rotate the local offset by the angle from object to camera.
                    float3 toCamWS = _WorldSpaceCameraPos - centerWS;
                    float  angle   = atan2(toCamWS.x, toCamWS.z);
                    float  c       = cos(angle);
                    float  s       = sin(angle);
                    float3 rotated = float3(IN.positionOS.x * c - IN.positionOS.z * s,
                                             IN.positionOS.y,
                                             IN.positionOS.x * s + IN.positionOS.z * c);
                    OUT.positionCS = TransformWorldToHClip(centerWS + rotated);
                }
                return OUT;
            }

            half4 Frag(Varyings IN) : SV_Target
            {
                half4 tex = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, IN.uv);
                return half4(_BaseColor.rgb * tex.rgb, _BaseColor.a * tex.a);
            }

            ENDHLSL
        }
    }

    FallBack Off
}

The cylindrical branch rotates the vertex’s local offset around the world up axis by the angle from the object to the camera, which is the shortest path to “face you, but stay upright”.


Sprite-Space UVs • Coordinate Spaces • Parallax Mapping

Related functions: Sine/Cosine


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