nobuyukinyuu / Godot-EasyBlend

Custom blending mode shader for Godot
MIT License
35 stars 5 forks source link

Possible bug with the shader #2

Open NedAlex94 opened 3 years ago

NedAlex94 commented 3 years ago

Hi Nobuyuki,

First I want to thank you for your shader, it has been really useful.

The way we are using the shader is to blend in shadows "realistically" in our game. For example, when the shadow (which have low opacity) from two objects overlap, they should not display on top of each other, like in the image below:

https://i.imgur.com/CDrpPOv.png

However, when using your shader with the "add" blend mode, the textures blend in correctly and display as one, which is super cool.

https://i.imgur.com/ODx9xMZ.png

This effect however breaks if the camera or player is parallel to the textures, like here. But if it's above or below, it seems to work fine.

https://i.imgur.com/A10IYRC.png

Is this effect intended, or is it a bug? Do you know of any potential solution? Thanks in advance!

HeroicNate commented 3 years ago

I'm curious to know if your shadows are always on the same z level. Does moving the player change the z of the shadows?

NedAlex94 commented 3 years ago

No, the shadows are always on the -1 z-index.

This is how the structure looks.

Crate scene Shadow container Node2D (where the shader is placed), z-index: -1 Shadow Sprite (which uses parent's material) z-index: 0, relative

However, perhaps the built in y-sorting changes the z-index of the whole Crate scene? Could that be the problem?

I also noticed that if I place the container on the 1 z-index, the problem does not appear. However, this also draws the shadow in front of all the other elements, instead of drawing them on the floor. Which is not ideal for a shadow.

nobuyukinyuu commented 3 years ago

Since this is a screen space shader (as i remember it), the effect should be on the same pass as whatever object it's attached to is in the draw order. If a YSort changes that then I wouldn't know exactly how to fix it. However, the effect of merging multiple instances of the shader is definitely not intentional. It is probably a side effect of the shader happening in the same screen space on one pass I imagine. If you were to take two instances of objects utilizing the shader and stick a back buffer copy object in between them, you will most likely observe the effect in the first image. As far as I know you can only explicitly call a back buffer copy this way and you can't specifically control when godot decides it needs to make another pass, but again I'm not sure and this is just a guess.

nobuyukinyuu commented 3 years ago

As a workaround, you can try setting the shadow index to one as you mentioned before and then setting all the other objects on a higher index like 2 and see if that helps.

NedAlex94 commented 3 years ago

As a workaround, you can try setting the shadow index to one as you mentioned before and then setting all the other objects on a higher index like 2 and see if that helps.

I really appreciate your help!

That seems to be working from the tests i have made.

However, do you know why it does not work between different scenes, like in here? The settings are all the same. As you can see, it works between the crates and between the fences, but not between the fences and crates.

https://imgur.com/a/E60ztgw

nobuyukinyuu commented 3 years ago

If I had to guess, it would be the way godot's renderer batches draw calls. You can try testing it on a different version of Godot and see if that makes a difference, or if these different scenes are in different viewports for any reason that would pretty much guarantee that a back buffer copy was already done.

NedAlex94 commented 3 years ago

If I had to guess, it would be the way godot's renderer batches draw calls. You can try testing it on a different version of Godot and see if that makes a difference, or if these different scenes are in different viewports for any reason that would pretty much guarantee that a back buffer copy was already done.

Thank you nobuyukinyuu, we understand the problem now. We are currently brainstorming some solutions, your input has been very valuable.