Translucent shader distance

I’d like to preface this question by stating that I’m new to shader development in general. It’s entirely possible I’m asking a really dumb question, or a really simple one that I don’t have the vocabulary to Google yet.

Alright, so here’s what I want to do. I want to make a semi-passable water shader with a depth-based opacity. In the PBR model, my understanding that the engine should handle opacity and reflection via the refraction property. However, it doesn’t work properly in UE4, as is documented here (I’m seeing the same weird cutoff at shallow angles).

So, I decided I’d just try and do it myself using the method posted by Roel the previous link. The basic premise of the shader is that the opacity decreases as the depth of the water decreases. In doing some research on this, I came to understand that “depth” is probably a bad term to use. The provided Depth functions only measure distance to the camera, so the opacity looks really bad in deep water if the camera is close to the surface. The texture will look completely transparent despite the land underneath being very far away.

What I want (and here I may use some bad terminology) is distance between the world position of the texture pixel and the world position of the pixel that it is blending with. In short, if I have a translucent texture on object A, I want access to the position of object B:

 camera------>A------>B

Is this possible to get access to in the shader? Through the SceneColor function, I can get the color of the pixel in object B (albeit in screen space, not worldspace), so I’m hoping that the position is available in the rendering pipeline at some point. Again, I’m new so I might be going about this all wrong. If so, please let me know of a better way!

[Download this material][1] I threw together quickly. Give it a try!

The result looks like this:

-The opacity based on depth is in there, look at the staircase.

-It has texture planar mapped in world space, so it is consistent throughout your game.

-Custom refraction like in my other post.

-Custom panner so there is control in material instances over the x and y speed.

-It has a little edge fade to make the border not as hard.

Ah, I see now! The DepthFade function is more or less doing what I was going to try to do manually. Thanks for sharing this!

Hey Roel. Again thanks for sharing! Just wanted to mention to anyone else who finds this, if you add a Sphere Reflection Capture and a Skylight actor to the world (Found under the Visual tab and Lights tab respectively) it makes everything look fantastic!

Can you upload this map so we can see the whole thing or are you working on a game

This map is from the free Shooter Game example at the bottom of the learning tab in the epic games launcher.