I'm working on implementing a basic occlusion silhouette. The way I'm trying to achieve it is first my character is assembled out of multiple shaded sprites and drawn onto a SubViewport. the ViewportTexture is then routed to two different Sprite2Ds, one ysorted and one with a high z index and the silhouette shader applied. The shader is supposed to see if screen_texture matches sprite_texture and if it does, the character is visible and drawn normally (the silhouette is invisible) but if it doesn't match that means the character was occluded and teh silhouette color is drawn instead. It works in isolation, but the effect is broken if the sprites are affected by lighting (PointLight2Ds) or time of day (CanvasModulate node) effects. It's like the silhouette is not getting the effects applied to it and therefore doesn't match the screen is my best guess.
here is the shader code:
render_mode blend_mix;
uniform sampler2D screen_texture : hint_screen_texture, repeat_disable, filter_nearest;
uniform vec4 silhouette_color : source_color = vec4(0.0, 0.0, 0.0, 0.5);
uniform float color_tolerance : hint_range(0.0, 0.1) = 0.01;
void fragment() {
vec4 sprite_color = texture(TEXTURE, UV);
if (sprite_color.a <= 0.001) {
discard;
}
vec4 screen_color = texture(screen_texture, SCREEN_UV);
bool colors_match = all(lessThan(abs(screen_color - sprite_color), vec4(color_tolerance)));
if (colors_match) {
COLOR = sprite_color;
} else {
COLOR = vec4(silhouette_color.rgb, silhouette_color.a * sprite_color.a);
}
}```