How: mask a sprite using an image so that areas outside that image are invisible?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By bitbutter

Hi all. I’d like to mask a sprite in such a way that areas falling outside the image i’m using as a mask are invisible.

Here’s an image showing what I’ve got so far, and what I’d like to get.

Is there a way to do this (without using a bigger mask image)

enter image description here

:bust_in_silhouette: Reply From: rakkarage
shader_type canvas_item;

uniform vec4 color: hint_color;
uniform sampler2D mask: hint_albedo;
uniform float scale = 2.0;
const float pivot = 0.5;

void fragment() {
	vec2 ps = SCREEN_PIXEL_SIZE;
	vec2 ratio = (ps.x > ps.y) ? vec2(ps.y / ps.x, 1) : vec2(1, ps.x / ps.y);
	float a = 1.0 - texture(mask, (UV - pivot) * scale * ratio + pivot).a;
	COLOR.a = (a < 0.5) ? 0.0 : color.a;
	COLOR.rgb = color.rgb;
}

Here is how i did it. you can see it in action here when you go down stairs after door. i am sure it could be improved but… gotm.io | gotm.io