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));
}
Tags
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

2D Vertical Pixel Dissolving Wave

2D Pixel Perfect – Adapted to rotation and scaling

Color Replacer + Pixel Perfect Damage Shader

guest

2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
caelohm
5 months ago

When I try to use this in the editor, it works as intended. But then when I try to tween the shader in game, the pixels change position every frame instead of already invisible pixels staying invisible. solution?

caelohm
5 months ago
Reply to  caelohm

I’m using tween_method to a function that is using ShaderMaterial.set_shader_parameter to set the sensitivity