+1 vote

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

in Engine by (21 points)

1 Answer

+1 vote
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;
}

enter image description here

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... https://gotm.io/henry-software/pixelleveltest/

by (1,879 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.