Skip to content
On this page

Rotating UVs

Rotating a texture is rotating the UVs, not the texture. You take the point, move it to a pivot, spin it with a two by two rotation, move it back, and sample. Three lines, and a texture that spins without ever leaving its grid.

float2 d = uv - pivot;
float2 ru = half2(
    dot(half2( cosA, sinA), d),
    dot(half2(-sinA, cosA), d)
);
float2 uvOut = pivot + ru;

Rotate around a pivot, not around zero

A raw rotation matrix turns points around the origin, which for a 0 to 1 UV is the corner of the texture. You usually want the texture to spin around its center, so the rotation happens in three moves. Subtract the pivot to move the center to the origin, apply the rotation, add the pivot back. That is the entire technique, and it is the same translate, rotate, translate pattern you use for any 2D transform.

float2 pivot = half2(0.5, 0.5);
float2 d     = uv - pivot;
half  c      = cos(angle);
half  s      = sin(angle);
float2 out   = pivot + float2(dot(half2(c, s), d), dot(half2(-s, c), d));

The dot products are doing the matrix multiply, one row at a time. Writing it this way keeps the code readable and is exactly as fast as the matrix form, because the compiler turns either one into the same two multiplies.

One consequence to plan for

Rotate a square texture inside a square UV space, and the corners of the texture wander outside the square while it turns. What you see at the edges depends on the wrap mode. With Repeat, the texture spins seamlessly forever, which is the classic look of a rotating dial or a spinning rune. With Clamp, the corners smear against the border and the texture looks like it is dragging. So the wrap mode is part of the effect, and you choose it to decide whether rotation reads as a wheel or as a sliding card.

Shader Story: Textures - Rotating UVs

Driving the angle

The angle is just a number, so anything that makes a number drives the rotation. A slider for a static turn, time for a continuous spin, a noise sample for a wobbling turn. The chapter on UVs & Time covers the time part, and the wrap matters there too: a long running session should feed the sine a small angle, not a huge one, which is what Time Overflow is about.


What the shader does

The shader rotates a circular texture on a quad through a rotation property, with a toggle for Repeat versus Clamp wrap. Repeat turns the texture into a seamless wheel; Clamp smears the corners at the border.

Shader Story: Textures: Rotating UVs - schematic

Illustrative schematic: Repeat wraps seamlessly; clamp smears the corners.


One texture, one pivot, one angle. The angle comes from a slider by default, and the wrap mode on the texture import decides the edge behavior.

Shader code
Shader "DecompiledArt/Textures/Basics/UVRotation/UVRotation"
{
    Properties
    {
        _MainTex("Texture", 2D) = "white" {}
        [MainColor] _BaseColor("Base Color", Color) = (1, 1, 1, 1)
        _Angle("Angle (radians)", Range(0, 6.2832)) = 0
        [Enum(Manual,0,Spin,1)] _Mode("Mode", Float) = 0
        _Speed("Spin 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;
                float2 uv         : TEXCOORD0;
            };

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

            CBUFFER_START(UnityPerMaterial)
                half4 _BaseColor;
                half  _Angle;
                half  _Mode;
                half  _Speed;
            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
            {
                half angle = (_Mode < 0.5h) ? _Angle : frac(_Time.y * _Speed) * 6.2832h;

                float2 pivot = half2(0.5, 0.5);
                float2 d     = IN.uv - pivot;
                half  c      = cos(angle);
                half  s      = sin(angle);
                float2 out   = pivot + float2(dot(half2(c, s), d), dot(half2(-s, c), d));

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

            ENDHLSL
        }
    }

    FallBack Off
}

Tiling & Offset • Bulge UVs • UVs & Time

Related functions: Sine/Cosine • Frac


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