UV Simple Rotation

This is a ready-made algorithm for simple UV rotation

Shader code
// https://github.com/ArcticWolves
// UV Simple Rotation Shader
// https://arcticwolves.games/uv-simple-rotation-godot-engine/

shader_type spatial;
render_mode blend_mix, depth_draw_opaque, cull_back, diffuse_lambert, specular_schlick_ggx;

// Параметры управления вращением
uniform bool is_rotation_reverse = false;
uniform bool enable_rotation = false;
uniform float rotation_axis_offset = 0.0;
uniform float rotation_speed = 0.1;

// Текстура
uniform sampler2D albedo_texture : filter_linear, repeat_enable;

void fragment() {
	// Текущее время сцены
	float time = TIME;

	// Ось вращения зависит от enable_rotation
	float axis = mix(rotation_axis_offset, time, float(enable_rotation));

	// Реверсирование вращения
	float axis_reversed = 1.0 - axis;
	float rotation_value = mix(axis, axis_reversed, float(is_rotation_reverse));

	// Рассчитываем финальный угол вращения
	float angle = rotation_value * rotation_speed;

	// Косинус и синус угла
	float cos_angle = cos(angle);
	float sin_angle = sin(angle);

	// Центрируем UV координаты
	vec2 uv = UV;
	vec2 uv_centered = uv - vec2(0.5, 0.5);

	// Поворачиваем координаты вокруг центра
	float rotated_x = cos_angle * uv_centered.x + sin_angle * uv_centered.y;
	float rotated_y = cos_angle * uv_centered.y - sin_angle * uv_centered.x;
	vec2 rotated_uv = vec2(rotated_x, rotated_y);

	// Семплируем текстуру по повернутым координатам
	vec4 albedo_color = texture(albedo_texture, rotated_uv);

	// Устанавливаем цвет альбедо
	ALBEDO = albedo_color.rgb;
}
Tags
MatrixRotation, rotation, TextureRotation, uv, UV1, UV2, UVRotation
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.

More from Riko

Related shaders

guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments