Dirt Cleaning Shader

This is an extremely simple shader I created for my idle fishing game Lured In. It’s designed for applying a mask onto a noise texture, and emulating a dirt cleaning effect. All that is required is to set the noise and mask textures, and you’re good to go. You can add it to a ColorRect to overlay dirt or grime ontop of things.

Keep in mind that the mask multiplies the noise texture, so any part that is BLACK will make the noise texture go invisible.

Below is a basic script showing off how you can achieve the cleaning effect by creating a mask and applying it to the shader in a script attached to the ColorRect with the material:

extends ColorRect

var mask : Image
var eraser_image : Image
 
func _ready() -> void:
	mask = Image.create(size.x, size.y, false, Image.FORMAT_RGBA8)
	mask.fill(Color(1.0, 1.0, 1.0, 0.0))
	eraser_image = preload("res://eraser_sprite.png").get_image()

func _process(delta: float) -> void:
	var mask_position = get_global_mouse_position()
	mask.blend_rect(eraser_image, Rect2i(Vector2i.ZERO, eraser_image.get_size()), mask_position - (eraser_image.get_size()/2.0))
	material.set_shader_parameter("mask", ImageTexture.create_from_image(mask))
Shader code
shader_type canvas_item;

uniform sampler2D noise : filter_nearest;
uniform sampler2D mask : filter_nearest;

void fragment() {
	COLOR = texture(noise, UV);
	COLOR.a *= texture(mask, UV).r;
}
Live Preview
Tags
2d, canvas, cleaning, drawing, erasing, masking, overlay
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 Big Boy Games

Related shaders

guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments