Motion smear for fast sprites

The smear frames of 2D animation, as a shader: a fast sprite (a dash, a lunge, a thrown object) leaves a short fading copy of itself behind it, and is crisp again at rest.

Put it on a Sprite2D and set velocity from your script each frame, in pixels per second (in the sprite’s own axes, so multiply x by -1 when the sprite is turned with scale.x = -1). Use scale.x = -1 to turn the sprite, not flip_h.

Settings:

  • velocity: how fast the sprite moves; zero draws the sprite unchanged. The default (600, 0) is only there so the preview shows the smear: drive it from your script.
  • length_scale: how many seconds of motion the smear covers (0.06 is a short smear).
  • max_length_px: the longest smear, whatever the speed.
  • margin_px: room drawn round the sprite for the smear (keep it at least max_length_px).
  • strength, smear_tint: how solid the smear is and its colour.
  • steps: copies per pixel; they are dithered, so 16 already looks smooth.

Works with the Compatibility renderer and respects the node modulate.

For real trails behind swords, dashes and bullets (ribbon trails, afterimages, sword slashes), see Trails FX: https://dimensionalmios.itch.io/almios-trails-fx

Shader code
shader_type canvas_item;
// Motion smear for a fast sprite (a dash, a lunge, a thrown object): the picture is drawn
// several times back along its motion, fading out, like the smear frames of 2D animation.
// Set `velocity` from your script each frame (pixels per second, in the sprite's own axes);
// at rest nothing changes. The sprite needs empty margin round it for the smear to show:
// `margin_px` grows the drawn rect by that much on every side. Turn the sprite with
// scale.x = -1, not flip_h (flip_h reverses the UVs this growth relies on).

// the default only makes the smear visible in a preview: set it from your script, zero at rest
uniform vec2 velocity = vec2(600.0, 0.0);
uniform float length_scale : hint_range(0.0, 0.2) = 0.06;   // seconds of motion the smear covers
uniform float max_length_px : hint_range(0.0, 400.0) = 140.0;
uniform float margin_px : hint_range(0.0, 400.0) = 140.0;
uniform float strength : hint_range(0.0, 1.0) = 0.75;
uniform int steps : hint_range(2, 32) = 16;
uniform vec4 smear_tint : source_color = vec4(1.0, 1.0, 1.0, 1.0);

varying vec4 node_tint;
varying vec2 tex_size;

void vertex() {
	node_tint = COLOR;
	tex_size = 1.0 / TEXTURE_PIXEL_SIZE;
	// grow the quad so the smear has room; UV follows so the sprite stays where it was
	vec2 dir = sign(UV - 0.5);   // corners only: works for centered and offset sprites
	VERTEX += dir * margin_px;
	UV += dir * margin_px / tex_size;
}

vec4 sample_in(sampler2D tex, vec2 uv) {
	// outside the texture is empty, not a clamped edge
	vec2 inside = step(vec2(0.0), uv) * step(uv, vec2(1.0));
	return texture(tex, uv) * inside.x * inside.y;
}

void fragment() {
	vec4 base = sample_in(TEXTURE, UV);
	vec2 trail = velocity * length_scale;
	float len = length(trail);
	if (len > max_length_px) {
		trail *= max_length_px / len;
	}
	// the copies sit behind the motion, back along -velocity, older ones fainter; stacked
	// front to back like layers, so a smear stays solid near the sprite
	vec3 col = base.rgb * base.a;
	float cov = base.a;
	// a per-pixel offset of the copies (dither) hides the steps between them
	float jit = fract(dot(FRAGCOORD.xy, vec2(0.06711056, 0.00583715)) * 52.9829189);
	for (int i = 1; i <= steps; i++) {
		float k = (float(i) - jit) / float(steps);
		vec4 s = sample_in(TEXTURE, UV + trail * k / tex_size);
		float a = s.a * (1.0 - k) * strength * smear_tint.a;
		col += s.rgb * smear_tint.rgb * a * (1.0 - cov);
		cov += a * (1.0 - cov);
	}
	vec4 c = vec4(col / max(cov, 0.0001), cov);
	COLOR = c * node_tint;
}
Live Preview
The shader code and all code snippets in this post are under MIT license and can be used freely. Images and videos, and assets depicted in those, do not fall under this license. For more info, see our License terms.

More from DimensionAlmios

Related shaders

Comments 0

No comments yet.