Pixel perfect dissolving

Dissolve any sprite pixel perfect, regardless of resolution or zoom.

Image by Ravenmore.

Shader code
shader_type canvas_item;

float random(vec2 uv) {
	return fract(sin(dot(uv, vec2(12.9898, 78.233))) * 438.5453);

uniform float sensitivity : hint_range(0.0, 1.0) = .5;

void fragment() {
	// Get size of texture in pixels
	float size_x = float(textureSize(TEXTURE, 0).x);
	float size_y = float(textureSize(TEXTURE, 0).y);
	vec4 pixelColor = texture(TEXTURE, UV);
	// Create a new "UV" which remaps every UV value to a snapped pixel value
	vec2 UVr = vec2(floor(UV.x*size_x)/size_x, floor(UV.y*size_y)/size_y);
	// Determine whether pixel should be visible or not
	float visible = step(sensitivity, random(UVr));
	// Draw the pixel, or not depending on if it is visible or not
	COLOR = vec4(pixelColor.r, pixelColor.g, pixelColor.b, min(visible, pixelColor.a));
dissolving, pixel perfect
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

Pixel Perfect Sprite Stacking Edges in 3D

Sub-Pixel Accurate Pixel-Sprite Filtering

Pixel Explosion

Notify of

Inline Feedbacks
View all comments