Godot Shader - SCREEN_TEXTURE and Z Index issue

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


I’m trying to create a water reflection shader and apply it to a Node2D.
I used the SCREEN_TEXTURE to do this. Here is my shader code.

shader_type canvas_item;

render_mode unshaded;

uniform float base_y;

void fragment()
	vec2 uv = SCREEN_UV;
	float y = base_y - uv.y;

	vec4 tx = texture(SCREEN_TEXTURE, vec2(uv.x, y));

	COLOR.r = COLOR.r + (tx.r * 0.3);
	COLOR.g = COLOR.g + (tx.g * 0.3);
	COLOR.b = COLOR.b + (tx.b * 0.3);

Actually, i did it. The image below show the water reflecting what is above it.

Well… not everything is being reflected!
I noticed that only the nodes that are below the water node (lower z-index) are being displayed on the reflection.

The image above show the reflection of a platform where the player is on top of.
But the player itself is not being displayed.

The player has a higher z-index, compared with the water node.
If i change this z-index to be lower than the water node one, the player is displayed on the reflection.

Here is a quick test.

On this first image, the player is not being displayed on the reflection.

On this second image, the player get displayed. What i did was just reordered the nodes on the scene. That basicaly changed the z-index of the nodes.

Am i doing something wrong?
I’d like to show on the reflection everything that is on the screen.
And not only the nodes that are below (lower z-index) the water node.

:bust_in_silhouette: Reply From: MysteryGM

Am i doing something wrong?

No, you are correct. What you need to understand is that it can’t render something that isn’t rendered yet.
See the z-index tells the renderer in what order to render sprites.

A image with a low z-index renders first. Then a image with a higher z-index is render over the other image. This is how z-sorting is done in 2D.
So if your water has a low z-index, it gets rendered before the other objects is rendered. So it can only reflect objects that have been rendered before it.

Give your water the highest z-index, so it always renders last.


Thanks for the info on that.
I think i’ll have to limit myself on how to use this water node on the project.

But glad to know i was not doing anything wrong on the shader (still learning this)

bottoni | 2018-12-05 15:03