[3.5.1] Reflection shader question
Asked Answered
I

2

0

Hi!
I just had a question of how to implement a specific effect for my game, and I'm not well versed with shaders so I thought I'd ask here on the forum.

I've been working on my game for a while and I'm making great progress. I had made a hacky but simple water shader for one area of my game before that I wanted to reimplement in a new scenario.

The way this shader was implemented was that I just used SCREEN_TEXTURE to get the texture, flipped it, and made it move back and forth a bit to "shimmer". I actually put a blue background underneath for the water's color, but essentially this works fine for this scene because it reflects what I want it to.

Now I essentially wanted to reuse this effect for outside of that cutscene (It's a boxing game), since I wanted this water based enemy to have a wave attack to jump over (He's a mantis shrimp).

However, this implementation seems to be based off of the priority of the nodes themselves, so it only renders the ring itself and the background in the scene, which is basically everything I didn't want to render. I just wanted a reflection of the player and the opponent in the water, which I wanted rendered on a polygon2D (what I had previously) or something else that can be scaled out.

I already tried rethinking the layers of the scene so that this could work without having to find a new solution, but I don't think this will work considering that the layers that are supposed to be in front have to be behind the reflection to render.

Since this seems like an inherent limitation, does anyone have a suggestion for a different way to render a copy of the the combatants on an object? I tried using backbuffer copy, but I was surprised that there's not really any documentation on it at all, shocking since everything else has been written up pretty well. I try to not ask about things I could easily look up, but that doesn't seem to be the case here, so does anyone know if that would be a good solution, and if so how to go about implementing it?

I think something of note would be that the player and opponent are both instantiated from a scene, and it seemed that the backbuffer copy works with nodes that are its children from the one video on it I could find (He said he didn't know much about it either), so if the sprite nodes have to be direct children of the backbuffer that may not work.

Thanks so much

Illassorted answered 21/3, 2023 at 0:48 Comment(0)
O
0

Hi there! It sounds like you're trying to create a reflection effect of the player and opponent on a polygon2D for your mantis shrimp's wave attack in your boxing game, but you're having trouble with the current implementation. I can definitely help you with that!

Instead of using the SCREEN_TEXTURE node, which captures the entire screen, you could use a Viewport node to capture only the parts of the scene that you want reflected, such as the player and opponent. Here's how you can go about it:

Create a new Viewport node in your scene, and set its size to match the size of the polygon2D or other object you want to reflect the player and opponent on.

Add the player and opponent nodes to the Viewport node as children. Note that if any of these nodes have animations, physics, or other active processes, they may need to be paused when added to the viewport to ensure accurate reflections.

Set the Viewport node's "Render Target" property to a new TextureRect node, and set the TextureRect's "Stretch Mode" property to "Viewport". This will render only the contents of the Viewport node onto the TextureRect, allowing you to manipulate it further.

Finally, apply any additional shader effects or transformations to the rendered texture on the TextureRect as desired, such as flipping it vertically for a reflection effect.

By creating a separate Viewport for the reflection effect, you can control exactly what nodes are included in the reflection and avoid rendering unwanted elements like the background or ring. Additionally, this method allows for greater flexibility in manipulating the reflected texture, such as scaling or distorting it to simulate water distortion.

I hope this solution works for you and feel free to let me know if you have any further questions!

Ohm answered 21/3, 2023 at 11:5 Comment(0)
I
0

Ohm Hi! Thanks for the quick reply, I did use viewports before to some success for a part that blended 2D and 3D scenes. I did try the suggested steps, and it's looking mostly good.

It took a little experimenting to figure out some things, like adding another viewport texture so that the player and opponent are visible as well since making them children of the viewport wouldn't make them children of a node2D node. Then I was having an issue with the shallow copy of the texture, so eventually I was able to make two viewports so that the player and opponent could be on separate planes.

Which comes to my issue: The opponent and player move back and forth when they get hit or knocked down. Given the nature of just flipping the texture, when the player moves up, the reflection moves down, and same with the opponent. This seems like an inherent issue without straight up moving the reflection with the opponent (Which I'll experiment with later), but is there any sort of simpler solution than that?

I'll experiment more with it later, when I get around to mapping the reflection to a polygon2d instead of a texturerect I think I'll see if I can set the UV y coordinate of the reflection relative to the sprite of the character.

Anyways, thanks so much for the help!

Illassorted answered 21/3, 2023 at 14:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.