Sprite Cut-Out/Cut-In Mask

A simple shader for creating cut-outs or cut-ins on sprites. Useful for if you want a simple ring/radial bar sprite or a shape outline.

Sources on functions for scaling and rotating UVs.

Shader code
shader_type canvas_item;
render_mode unshaded;

uniform float fillAmount : hint_range(0.0, 1.0) = 0.5;
uniform float maskRotation : hint_range(-360.0, 360.0, 0.1) = 0;
uniform vec2 maskOffset = vec2(0, 0);
uniform vec2 maskSize = vec2(1, 1);
uniform vec2 maskRotationPivot = vec2(0.5, 0.5);
uniform vec2 maskScalePivot = vec2(0.5, 0.5);

uniform bool invertMask = false;
uniform bool useDifferentTexture = false;
uniform sampler2D maskTexture : filter_nearest;

vec2 rotate_degrees(vec2 uv, vec2 pivot, float deg){
	float rad = radians(deg + 90.0);
	mat2 rotation = mat2(vec2(sin(rad), -cos(rad)),vec2(cos(rad), sin(rad)));
	
	uv -= pivot;
	uv = uv * rotation;
	uv += pivot;
	return uv;
}

vec2 scale(vec2 uv, vec2 pivot, vec2 scale){
	uv -= pivot;
	uv = uv / scale;
	uv += pivot;
	return uv;
}

void fragment(){
	vec2 uv_rotated = rotate_degrees(UV, maskRotationPivot, maskRotation);
	vec2 uv_position = scale(uv_rotated, maskScalePivot, maskSize * (1.0 - fillAmount)) + maskOffset;
	
	float alpha_total = texture(TEXTURE, uv_position).a;
	
	if (useDifferentTexture)
		alpha_total = texture(maskTexture, uv_position).a;
	
	bool isWithinBounds = uv_position.x >= 0.0 && uv_position.y >= 0.0 && uv_position.x <= 1.0 && uv_position.y <= 1.0;
	
	if (!invertMask){
		if (isWithinBounds)
			COLOR.a -= alpha_total;
	}
	else{
		COLOR.a -= 1.0 - alpha_total;
		
		if (!isWithinBounds)
			COLOR.a = 0.0;
	}
}
Tags
Cut-In, Cut-Out, mask, sprites
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

Fog of war with alpha cut off as white color

Cut Out shader

Simple UI Noise Mask

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments