Godot Version
Godot 4.6.1.stable.official
Question
I want to create a dynamic lighting system like this for my 2d rpg. Does anyone have any ideas? I’ve tried squashing, stretching, and manually moving the shadow sprite, but it doesn’t look as good and is relatively costly performance-wise.
So I put this together, and it works pretty well! I started by creating a new scene called “shadowed sprite 2D” that will replace all of my sprite 2Ds in the world. The node tree looks like this:
When instanced, drag your sprite into the shadowed sprite 2D per usual, and the code should take care of pretty much everything else. The attached script will take the texture and assign it to both $Sprite2D and $Shadow/Sprite2D. Then it will track a point in space (right now the mouse) and rotate the shadow sprite such that it is facing away from that point. in the final version, this will be a invisable point that circles the camera slowly. Additionally, the script tracks the distance the point is from itself and stretches the shadow accordingly. This code does allow for animation, but right now you need to either make $Sprite2D and $Shadow/Sprite2D update every frame based on the frame of ShadowedSprite2D or manually update the frame of $Sprite2D and $Shadow/Sprite2D from the node you want to animate.
The shadow shader, attached to $Shadow/Sprite2D, squashes the sprite down a little and turns it transparent. I then took this shader code, changed the pixel size to 1, and merged it with my own to ensure pixel snapping.
finally, the only not automated part at the moment is shadow positioning based on sprite size. in my script, i include specific coordinates for different sprites and automatically map them to those coordinates at runtime. if anyone has any ideas how to automate this process so the bottom center of the sprite is at 0,0 please let me know!
the code in effect: