2D Shader accessing UV offscreen

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Kanor

I’ve been working with shaders to make a reflective-water shader. The problem is when it tries to reflect an area that is not on-screen:

shader_type canvas_item;

void fragment() {
	vec2 reflection = vec2(SCREEN_UV.x, SCREEN_UV.y + UV.y*(SCREEN_PIXEL_SIZE.y/TEXTURE_PIXEL_SIZE.y)*2.0);

	COLOR = texture(SCREEN_TEXTURE, reflection);
}

The problem is when reflection.y ends up being more than 1. Rather than correctly getting the look of a block from above the camera view, it gets the pixel color of the tip-top pixel on-screen. It CANNOT get values offscreen.

Is this intentional? Is there a way around it? I can live without it, but I’d like to have it if at all possible

:bust_in_silhouette: Reply From: kbal

This is expected behavior for fragment shaders.

I don’t know how to solve this exactly in Godot, but I did this in pure OpenGL and Unreal: Render a larger area to a proportionally larger texture and then render the desired cutout from the texture to screen in a second pass.

Beware that this can increase your performance requirements a lot. If you render x2 the length in u- and v-direction to the texture, your game has to calculate 4x the amount of pixels.

Aw, shucks. Maybe there’s some way to force Godot to render offscreen scenery? Thanks for letting me know!

Kanor | 2022-08-01 18:21