+1 vote

I am trying to make a drop shadow. I found the example scripts for 2d shaders and used the offset shadow shader as a base. I found that if my assets don't have a transparent margin the shadow would be clipped. Below is an example, both sprites have the same shader applied. The default icon has no margin so the shadow can only be seen in the transparent corners. The character on the right shows the drop shadow correctly. If the shadow was any larger, it would start to clip. The shader code can be found at the bottom of the post.

left clipping right working

shader_type canvas_item;

uniform vec2 offset = vec2(8,8);
uniform vec4 modulate:hint_color;

void fragment(){
    vec2 ps = TEXTURE_PIXEL_SIZE;

    vec4 shadow = vec4(modulate.rgb, texture(TEXTURE, UV - offset*ps).a*modulate.a);
    vec4 col = texture(TEXTURE, UV);

    COLOR = mix(shadow, col, col.a);
in Engine by (13 points)

1 Answer

+1 vote

Normally with a drop shadow you want it to work this way. The alternatives are:

1.) Extend the vertices past the bounds, then re-size the image into the bounds.
This solves the bounds problem, but lowers the quality of the sprite.

2.) Since you are just coloring the sprite Black, then moving it, you can just repeat this using a second sprite.

Duplicate the character sprite. Make it a child, give it a new canvas material, set visibility-> Self Modulate set this to black, move it. Done.

Now you have a second sprite acting as the shadow of the first.

by (1,482 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.