Skip to content
On this page

Screen-Space UVs

Every UV so far has been attached to the surface: it moves with the mesh. A screen space UV is attached to the screen. It is the pixel’s own address, derived from the clip space position, and it makes the texture behave like a layer of the image, not a coat on the object.

// Clip position, divided by the viewport, is a 0..1 screen point.
float2 screenUV = IN.positionCS.xy / _ScreenParams.xy;

Deriving the coordinate

The clip space position is the vertex’s address in the final image, and after the rasterizer it exists per pixel. Divide the x and y by the viewport size and you have a point in 0 to 1 that says where this pixel sits on screen. In URP the viewport size arrives as a built in called _ScreenParams, so the whole projection is a division:

float2 screenUV = IN.positionCS.xy / _ScreenParams.xy;
half4 tex = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, screenUV);

Two conventions to keep straight. Clip space y points up while screen images usually read from the top, so you will often flip with 1.0 - screenUV.y depending on how the texture is authored. And the point can sit outside 0 to 1 for pixels that a large primitive covers but the viewport does not show, so the wrap mode matters here too, though for a screen sized texture you usually keep it in bounds and let the edges clamp.

Shader Story: Textures - Screen Space UVs

Why you would want a surface to read the screen

The first use is screen aligned pattern: a texture that is the same size as the window, like a vignette, a scanline field, or a subtle grain, sampled from a real texture instead of pure math. The pattern does not rotate or stretch with the object, because it is not on the object. Turn the object and the pattern holds its place on screen, which is exactly the behavior you want for a HUD like overlay painted onto a 3D object, and the opposite of what you want for a decal, which is a useful distinction to have.

The second use is screen space distortion: read the screen position, perturb it, and sample a texture at the perturbed spot to make ripples, heat haze, and liquid effects on a mesh. The perturbation is the interesting part, and any 2D warp from this category, bulge, swirl, parallax, works in screen space just as well as in UV space, with the difference that screen space warps are resolution stable by construction, they are defined in pixels, not in whatever scale the UVs happened to have.

The trade to keep in mind: a screen space UV ignores the mesh’s own geometry, so a curved surface samples the texture through its flat projection. Details that should wrap around a corner do not, because the coordinate never knew the corner was there. Screen space is for effects that belong to the view, not to the object, and the moment you want the pattern to live on the surface, you are back in surface UVs.


What the shader does

The shader samples a window-sized texture in screen space, so the pattern stays put while the object turns. A toggle adds a screen-space ripple that wobbles the sampled point over time.

Shader Story: Textures: Screen-Space UVs - schematic

Illustrative schematic: The pattern stays put while the object turns.


Clip position to screen UV, one division, and a ripple that bends the sample point.

Shader code
Shader "DecompiledArt/Textures/Projections/ScreenSpaceUVs/ScreenSpaceUVs"
{
    Properties
    {
        _MainTex("Screen Texture", 2D) = "white" {}
        [MainColor] _BaseColor("Base Color", Color) = (1, 1, 1, 1)
        _RippleStrength("Ripple Strength", Range(0, 0.05)) = 0
        _RippleSpeed("Ripple Speed", Range(0, 4)) = 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(_MainTex);   SAMPLER(sampler_MainTex);

            struct Attributes
            {
                float4 positionOS : POSITION;
            };

            struct Varyings
            {
                float4 positionCS : SV_POSITION;
            };

            CBUFFER_START(UnityPerMaterial)
                half4 _BaseColor;
                half  _RippleStrength;
                half  _RippleSpeed;
            CBUFFER_END

            Varyings Vert(Attributes IN)
            {
                Varyings OUT;
                OUT.positionCS = TransformObjectToHClip(IN.positionOS.xyz);
                return OUT;
            }

            half4 Frag(Varyings IN) : SV_Target
            {
                float2 uv = IN.positionCS.xy / _ScreenParams.xy;
                uv.y = 1.0 - uv.y;   // images read top down, clip y points up

                // A gentle screen space ripple, centered on the screen.
                float2 c = uv - 0.5;
                float d = length(c);
                uv += c * _RippleStrength * sin(d * 40.0 - _Time.y * _RippleSpeed * 4.0);

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

            ENDHLSL
        }
    }

    FallBack Off
}

Tri-Planar UVs • Parallax Mapping

Related functions: Remap • Length


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