Help with Light processor function in CanvasItem shader

Godot Version



Hi there,
I’m trying to write a CanvasItem shader for a sprite in my 2D game to handle some a custom lighting simulation.

I essentially want to try to simulate Y-sorting for PointLight2D nodes as I couldn’t see any built in way to do this. I thought that an easy way to do this would be in a CanvasItem shader in the light processor function for each sprite node that needs to be illuminated.

The shader could calculate the distance between the light source and the sprite on the Y axis to determine if the light source is in front or behind, then illuminate based on that.

My problem is in calculating the distance between the light source and the sprite. I’m not sure how to do this or if it’s even possible in a gdshader. I’ve tried various calculations using the built-ins e.g. LIGHT_POSITION LIGHT_DIRECTION LIGHT_VERTEX SCREEN_UV and UV but haven’t had any luck.

Any help would be gladly appreciated.