2D Rotation deform
This shader allows to deform a sprite by rotating it. It’s very useful to make quick animation, move trees, create springy things, etc..
Learn more about the rotation matrix: https://en.wikipedia.org/wiki/Rotation_matrix
How to use?
- Make sure to have enough empty space in your sprite for the deformation to happen. If you don’t, the sprite will clip
- Add shader to sprite
- Play with the rotation & rotation offset
Parameters
- rotation: amount of rotation to apply, negative to rotate left, positive to rotate right
- rotation_offset: where to apply the rotation on the sprite. For example, making the offset (0.5, 0.9) will apply the rotation near the bottom of the sprite. This is useful is you want the feet of the character to be “anchored” to the ground for example.
Enjoy!
Shader code
shader_type canvas_item;
uniform float rotation = 0.0;
uniform vec2 rotation_offset = vec2(0.5, 0.5);
void fragment() {
// Make the rotation dependant on the UV. Useful to "anchor" the
// bottom of the sprite
float rotation_anchored = rotation * (1.0 - UV.y);
// Create the rotation matrix: https://en.wikipedia.org/wiki/Rotation_matrix
mat2 rotation_mat = mat2(vec2(cos(rotation_anchored), sin(rotation_anchored)),
vec2(-sin(rotation_anchored), cos(rotation_anchored)));
// Offset the UV to apply the rotation
vec2 centered_uv = UV - rotation_offset;
// Apply rotation
vec2 rotated_uv = centered_uv * rotation_mat;
// Offset the UV back to normal
rotated_uv = rotated_uv + rotation_offset;
COLOR = texture(TEXTURE, rotated_uv);
}
Perfect little article, exactly the information I was looking to find.