Swirl/Sink

This simple shader allows swirling a sprite on itself. In its current state, it only works on stand-alone sprites. Don’t try to use an atlas with the rect selection.

  • ratio : from 1 (unmodified) to 0 (totally folded)
  • power : Speed of retraction. Determine at which pace the sprite will be disappearing to the center.
  • min_speed : swirling speed at the border.
  • max_speed : swirling speed near the center.
Shader code
shader_type canvas_item;
 
uniform float ratio : hint_range(0.0, 1.0) = 0.1;
 
uniform float power = 3.0;
 
uniform float min_speed = 10.0;
 
uniform float max_speed = 90.0;
 
void fragment() {
    vec2 uv = UV;
    
    uv *= 2.0;
    uv -= vec2(1.0);
    
    float len = length(uv);
 
    float rspeed = mix(max_speed, min_speed, len);
    
    float sinx = sin((1. - ratio) * rspeed);
    float cosx = cos((1. - ratio) * rspeed);
 
    vec2 trs = uv * mat2(vec2(cosx, sinx), vec2(-sinx, cosx));
    trs /= pow(ratio, power);
    
    trs += vec2(1.0);
    trs /= 2.;
    if(trs.x > 1. || trs.x < 0. || trs.y > 1. || trs.y < 0.) {
        // Prevent sprite leaking.
        COLOR = vec4(0.);
    } else {
        vec4 col = texture(TEXTURE, trs);   
    	COLOR = col;
    }
}
Tags
Swirl, vortex
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 CasualGarageCoder

(Almost) Invisible Character

Shield with impact waves

2D Fountain

Related shaders

Swirl Shader

guest

0 Comments
Inline Feedbacks
View all comments