Skip to content
On this page

Sprite-Space UVs

A sprite is a small piece of geometry whose whole job is to display a texture, and a sprite’s UVs should describe the sprite, not the mesh it happens to be built from. Sprite space UVs are UVs that run from 0 to 1 across the sprite’s own bounds no matter what the source mesh looks like, which is what lets one quad show exactly the frame of an atlas it is assigned.

// The sprite fills the texture: local position, normalized to the sprite size.
float2 uv = IN.positionOS.xy / _SpriteSize;
half4 tex = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, uv);

The problem with trusting the mesh

Take a quad from a model, or a plane with UVs painted for a ground decal, and put a character sprite texture on it. The texture lands wherever the mesh’s UVs say, which is usually not “the whole sprite, corner to corner”. The sprite gets cropped, stretched, or shifted, because the mesh and the texture disagree about who owns the coordinate.

Sprite space inverts that. The coordinate is derived from the local position of the vertex, divided by the sprite’s size, so the four corners of the geometry are exactly 0,0 and 1,1 and the texture fills it completely. The mesh’s painted UVs are simply ignored for this purpose.

// A 2x1 unit quad maps to the full texture either way it is stretched:
float2 uv = IN.positionOS.xy / _SpriteSize;

Shader Story: Textures - Sprite Space UVs

Atlas frames and the second use

The same idea scales to atlases, which is where sprites live in practice. An atlas is one big texture holding many small frames, and each frame is a sub rectangle of it: a float4 of min and max in texture space. Sprite space UVs then remap the sprite’s 0 to 1 bounds onto that frame rectangle:

// _FrameRect: x = minU, y = minV, z = width, w = height, in 0..1
float2 uv = _FrameRect.xy + IN.positionOS.xy / _SpriteSize * _FrameRect.zw;

The sprite’s geometry owns the 0 to 1, and the material supplies the rectangle, so the same quad asset can show any frame in the atlas by changing one property. This is the exact mechanism every 2D sprite renderer uses under the hood, whether the engine calls it a sprite, a decal, or a quad, and it is why a 2D game can ship thousands of frames in a handful of textures.

The boundary of the technique is the same as everywhere: it is a flat surface trick. The moment the quad bends, the sprite space assumption, that the geometry is a flat window onto the texture, stops being true, and you want a real 3D mapping instead.


What the shader does

The shader has three modes on a toggle: mesh UVs, sprite space (the texture fills the quad exactly), and atlas frame mode, where a frame index picks the sub-rectangle and the same quad becomes a different character of a sprite sheet.

Shader Story: Textures: Sprite-Space UVs - schematic

Illustrative schematic: Mesh UVs, sprite space, atlas frame.


Local position normalized to the sprite size, with an optional frame rectangle for atlas work.

Shader code
Shader "DecompiledArt/Textures/Projections/SpriteSpaceUVs/SpriteSpaceUVs"
{
    Properties
    {
        _MainTex("Texture or Atlas", 2D) = "white" {}
        [MainColor] _BaseColor("Base Color", Color) = (1, 1, 1, 1)
        _SpriteSize("Sprite Size (local units)", Vector) = (2, 1, 0, 0)
        [Enum(MeshUVs,0,SpriteSpace,1,AtlasFrame,2)] _Mode("Mode", Float) = 1
        _FrameRect("Frame Rect (minU, minV, w, h)", Vector) = (0, 0, 0.25, 0.5)
    }

    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;
                float2 uvSprite   : TEXCOORD1;
            };

            CBUFFER_START(UnityPerMaterial)
                half4 _BaseColor;
                float2 _SpriteSize;
                half  _Mode;
                float4 _FrameRect;
            CBUFFER_END

            Varyings Vert(Attributes IN)
            {
                Varyings OUT;
                OUT.positionCS = TransformObjectToHClip(IN.positionOS.xyz);
                OUT.uv        = IN.uv;
                OUT.uvSprite  = IN.positionOS.xy / _SpriteSize;
                return OUT;
            }

            half4 Frag(Varyings IN) : SV_Target
            {
                float2 uv = IN.uv;
                if (_Mode > 0.5h && _Mode < 1.5h)
                    uv = IN.uvSprite;
                else if (_Mode >= 1.5h)
                    uv = _FrameRect.xy + IN.uvSprite * _FrameRect.zw;

                half4 tex = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, uv);
                return half4(_BaseColor.rgb * tex.rgb, _BaseColor.a * tex.a);
            }

            ENDHLSL
        }
    }

    FallBack Off
}

Billboarding • Basic UVs

Related functions: 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.

DecompiledArt on Patreon DecompiledArt on Buy Me a Coffee

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