Godot Version
4.2.1
Question
Hello,
I am creating an isometric 2D game. I have a PointLight2D that illuminates the scene. I want my character to be lit when the feet touch the light. For example:
If the character is below the light source and only the top half of the sprite is in the light, the whole sprite should stay dark.
If the character is above the light and the feet touch the light, the whole character should be lit with the light value that touches the feet (including the part of the sprite that is still in the dark).
How it looks now vs how it should look like:
I am trying to solve this with a shader. My idea is, that I check a few points on the bottom of the sprite if they are black and if at least one of them isn’t, each pixel is lit up.
Is it even possible to achieve this with shaders? If not I will just add area2Ds to the lights and check if the LightOccluder of the character is in the light area to change the light mask or something like that. I’d just have loved to find another solution.
Code:
I guess it doesn’t work because the texture function only get the values of the texture before it is lit and not actually the texture as it was displayed on the screen in the last frame. Also during writing this I realized that even if my code worked, my character would never be able to turn black again. So my attempt is flawed anyway.
void light() {
vec4 point1 = texture(TEXTURE, vec2(0.2,0.3));
vec4 point2 = texture(TEXTURE, vec2(0.2,0.7));
vec4 point3 = texture(TEXTURE, vec2(0.4,0.3));
vec4 point4 = texture(TEXTURE, vec2(0.4,0.7));
vec3 black = vec3(0.0,0.0,0.0);
if(point1.rgb != black || point2.rgb != black || point2.rgb != black || point2.rgb != black)
{
LIGHT = vec4(1.0, 1.0, 1.0, 1.0);
} else {
LIGHT = LIGHT_COLOR*LIGHT_ENERGY;
}
This is my first project in Godot and the first time programming in a few years so I am not familiar with shaders at all. I am thankful for any helpful hint.