• News

• Industries

• Learning & Support

• Community

• Marketplace

# Perfecting Water Shader

So I made a Gerstner Water Shader, from this video: https://www.youtube.com/watch?v=_y7Z0MbGOMw, and made some modifications to it.
The thing is, near the shore the water waves are visible and makes it look unnatural:

Another Picture:

So this is what I’ve tried in the material blueprint, but it doesn’t seem to help that much. How do I get this shoreline to look natural?

Water should have specular of 0.255. Roughness should be lot closer to zero. Metalness should be 0. You should add refraction or simulate it by yourself with scene color. Using scene color is better because you can make colored transmission, chromatic aberration and you can take account distorted pixel depth.
Opacity should be calculated from fresnell using water specular.(multiply specular with 0.08 for normalizing specular to physical based range). Opacity is also modified by distance from water surface to bottom. Simple formula for transmission is exp(-(scatteringRGB + absorption )* distance).

For base color I calculate inscattering thickness but this is not very important for start. But deeper the water more light is scattered.

So I played around with it for a while but couldn’t get the opacity and refraction to work right. Could you please send a picture of how to do it or something?

This is how to setup refraction. Using Refraction | Unreal Engine Documentation
You should set BaseReflectionFraction to 0.08*specular. This same fresnel can used to drive opacity.

Ok, so I went through it and got this:

The DepthFade depth is set to 250.
The thing is that in the world, it gives a sort of weird reflection thing going on in the background:

Check this out. Unreal Engine 4.13 Released! - Unreal Engine “NEW: IMPROVED REFRACTION SHADERS”
This should fix that bug. Also depth fade should be used only for small scale effect to hide triangle intersections. Try value like 10cm.
You also need depth based transmission. Longer path that ray goes underwater more light is outscattered. Beer–Lambert law - Wikipedia

Ok, so refraction works great with the Pixel Normal Offset, but the opacity thing is a little tricky. So I didn’t change the depth fade, since if I do the waves are visible on the shore. And at 250 it looks alright , not too bad but if more could be done, it’d be nice. Also, the opacity for all of the water not near a shore, or in the distance, is too low, I don’t want to be able to see through the water so far away. How do I fix that?

Opacity is inverse of transmission. So you calculate how much light can pass through and then calculate opacity = 1 - transmission. So you first calculate how much you can see under surface with fresnel. Then you calculate how much you can see bottom of sea.(exp(-thickness * distance). You multiply those and you get transmission.

Sorry, I’m not too advanced in blueprints. Can you please tell me how to do this step-by-step? Thanks

Take Worldposition node and WorldPositionBehindTrancluency node. Calculate distance between those. Multiply that with -density.(use value like 0.005). Plug that to Pow node Exponent. Plug constant 2.71828182846 to Base. This is value how much you can see underwater.

Ok, then where do I plug the node that comes out of the Power?

Multiply that with opacity from fresnell node and then invert it with 1-x node and output to opacity.

Hello, everyone!
[MENTION=37019]Jenny Gore[/MENTION]: mind if i ask, how would one approach foam at the wave crests? I’ve been struggling with this for awhile, here’s how my water looks like now:

I have Gerstner’s Waves in WPO, displacement from heightmap, opacity as per your earlier advice and i use fresnel to lerp between 2 colors for Base Color. There is actually some foam i managed to get but they look ugly and unrealistic:

Also, is there something i can do with reflection artifacts (or what is that?) like here:

Could you recommend something?

For reflection I would use planar reflections.

[MENTION=37019]Jenny Gore[/MENTION]: Oh, sorry, i forgot to mention this already uses planar reflections. Still, quality at glancing angles seems lacking to me. Like how the individual waves sort of blend with each other and barely distinguishable.

@Zarkopafilis: You’ve linked the whole book, i assume you ment Gerstenr’s Waves which i already said i use? There is nothing i can find about foam though.

the reason your waves are barely distinguishable is because you are not recomputing your normals. so while your gerstner waves are offsetting the vertices, the normals are all still pointing upwards
there’s a few threads that mention this subject but it’s not easy to implement

@Chosker: Well actually i am recomputing them. I’m not sure i do it right, but they look more-or-less correct to me. Here’s a screenshot of just Gerstner’s Waves:

And this is how i calculate them along with normals:

And a Pastebin of that function if someone wants:

For wave i am using equations from the book linked earlier, although i had to struggle quite a bit to make normals look at least somehow believeable.

Try to calculate normals with DDX DDY trick. This is good reference point for smooth normal calculations.

[MENTION=37019]Jenny Gore[/MENTION]: do you mean what’s discussed here: Ocean shader normal issues! - Rendering - Unreal Engine Forums! ? I’ve tried that and came to the same conclusion as RyanB in the last post, that manually calculating normals should look better. If i do that trick i get either faceted mesh (if using WorldPos with Offsets) or an almost flat surface (without offsets). Maybe you mean something different?