- Home /
How do I make a water shader that has depth.
In shader code and materials for water in unity I have seen some assets on the asset store that show brilliant photos of the water becoming more opaque or darker in colour the deeper the object underneath it is. It sort of makes a volumetric appearance. Here are some examples of what I am talking about:
Notice how the deeper the water is the darker or less translucent it is, I was wondering how I could code this in a shader and make a material with these properties. Thanks for the help
Answer by JonPQ · Dec 18, 2017 at 08:17 PM
on PC you can read the Z buffer as input to a shader, so you draw the ground first, then the water on top (this is usual anyway, and transparencies are normally rendered after opaque geometry) then use the Z depth in the pixel shader to control blending to some other thing, other than the usual pixel you are about to render... Such as blend to a white color, or to a foam texture, or both a foam and a white color at the edge. subtract screen Z from the Z you are rendering... multiply by a scalar, and clamp to 0-1 range. Then you have something you can use to lerp with. https://forum.unity.com/threads/reading-depth-buffer-in-vertex-shader.431670/
On mobile you pretty much don't have access to the screen z depth in shader.... but you can do this another way, by rendering a top-town view of your map into a texture, to get the shoreline contour, and then paint in extra information in photoshop.... so you can paint your own depth into say the red channel... then paint other info into g,b,a channels also... whatever you want. Then add this as an extra texture input channel to your shader, using same UV input as the main texture (assuming mapped with parallel projection from above.
Your answer
Follow this Question
Related Questions
Adding object to scene causes problem with other objects material/shader 0 Answers
Writing a shader that creates an infinite number of water ripples 1 Answer
See backside of a transparent emissive shader? 0 Answers
Grabpass 2D water reflection, UV issue 0 Answers
Turning standard materials from opaque to transparent and back in runtime 1 Answer