Rotate Texture

Rotate or spin a texture around its center. For best results, use textures that don’t have pixels on the edges.

Shader code
shader_type canvas_item;

uniform sampler2D spin_texture;
uniform bool clockwise = true;
uniform float speed : hint_range(0.0, 2.0) = 1.0;

vec2 rotate(vec2 uv, vec2 pivot, float angle)
{
	mat2 rotation = mat2(vec2(sin(angle), -cos(angle)),
			vec2(cos(angle), sin(angle)));
    
    uv -= pivot;
    uv = uv * rotation;
    uv += pivot;
    return uv;
}

void fragment()
{
	float direction = clockwise ? 1.0 : -1.0;
	vec2 rotated_uv = rotate(UV, vec2(0.5), TIME * speed * direction);
	COLOR = texture(spin_texture, rotated_uv);
}
Live Preview
Tags
rotate, rotation, spin
The shader code and all code snippets in this post are under CC0 license and can be used freely without the author's permission. Images and videos, and assets depicted in those, do not fall under this license. For more info, see our License terms.

Related shaders

guest

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
corpsinhere
7 months ago

Ty 😀 – this was just the code snippet I needed to understand rotation!!