+1 vote

Hi.

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.
https://ibb.co/kXXsgmV

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.
https://ibb.co/vYx15Kb

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.
https://ibb.co/rHW1C4p

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.

in Engine by (13 points)

1 Answer

+2 votes

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.

by (1,488 points)

:(

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)

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.