Skip to content

A stylized water shader with depth colors, refraction, normal displacement and foam effect. Using ShaderGraph in Unity URP.

Notifications You must be signed in to change notification settings

z4gon/water-shader-unity

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

64 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Water Shader

With Shader Graph in Unity 2022.3.3f1

Features

Screenshots

Cliff Lake


Water depth color fade effect

Progress

Water Depth = Scene Depth - Fragment Position.

Water depth effect

Lerping between the two colors.

Water depth effect

Water colors

Sub Graph for the depth fade effect

Sub Graph example by Binary Lunar

Calculates the depth of the water using the Scene Depth node and the Screen Postion node. Then scales it down and clamps it to a 0..1 value to be able to use it for lerping colors later.

Depth fade sub graph

Understanding Fragment Depth vs Scene Depth (Depth Texture)

Depth by Cyanilux

How objects that are occluded by the object with the shader are used by the shader.

Depth fade sub graph

Water color section (lerp)

Shader example by Binary Lunar

Lerp between shallow water color and deep water color, using the Depth Fade sub graph.

Water color section


Refraction effect

Progress

Scene Color generated with transformed Scene Position, used a Normals Map generated with a time looped gradient noise.

Refraction depth effect

Apply refractions to the Scene Depth calculation.

Water depth refracted

Sub Graph for UV Movement

Sub Graph example by Binary Lunar

Tiles and offsets UV coordinates given a speed and a scale.

UV Movement sub graph

Refraction section (normal gradient with scene color)

Shader example by Binary Lunar

Generate a normals map from a time looped gradient noise, then add it to the Screen Position to displace it. Use these UVs from screen position to get a displaced and deformed Scene Color from objects behind.

Water Refraction section


Foam effect

Progress

Looped gradient noise, stepped by the depth fade calculation. Then lerped with the depth fade colors, and finally lerped with the refraction of the scene color.

Foam effect

Foam section (stepped gradient noise)

Shader example by Binary Lunar

Generate a Depth Fade 0..1 value, to then use as Step against a looped gradient noise. Use this result to multiply it with the foam color alpha channel.

Finally use this stepped alpha for the foam, and lerp the water depth colors with the foam color, using this stepped alpha.

Water Foam section


Waves effect

Progress

Normal vectors transformed with a gradient noise. Then added to the position vectors to displace the geometry.

Waves effect

Waves section (displacing the geometry)

Shader example by Brackeys

Time loop a gradient noise to transform the normal vectors, then add these vectors to the position vector.

Water Waves section


Complete Graph

All the sections working together.

Complete Graph


Scene objects setup

Integrating low poly assets from the asset store.

Low-Poly Simple Nature Pack

Scene objects setup


Implementation explained

  1. Project configuration

    1. Use the Universal Render Pipeline.
    2. In the Scriptable Object asset for the Render Pipeline Enable:
      • Depth Texture
      • Opaque Texture
  2. Water depth effect

    1. Make a "Depth Fade" Sub Graph using:

      1. Scene Depth Node with Eye sampling (Depth Texture)

      2. Screen Position Node with Raw mode (Fragment Position)

        Take just the Aplha channel which contains the Fragment Postion.

      3. Substract these two to calculate "Water Depth" = Scene Depth 1. Fragment Position

      4. Divide by a scaling factor to dynamically adjust the fading color for the water depth via property.

      5. Finally clamp the value to (0,1) to use it as lerp between two colors (shallow and deep water)

    2. Shader for the Water

      1. Make the Shader's Surface Type transparent.
    3. Depth color fade

      1. Use the Depth Fade Sub Graph to lerp between two colors (shallow and deep water)
  3. Refraction effect

    1. Make a "UV Movement" Sub Graph using:

      1. Time node to have a looping time value.
      2. A Speed input to make the time go faster or slower.
      3. Tiling and Offset node to transform the UVs.
      4. A Scale input to affect tiling.
      5. Output the Vector2 which will affect the UVs of the refraction later.
    2. Water Refraction

      1. Use the UV Movement Sub Graph to loop over time a gradient noise, using the Water Refraction Speed and Water Refraction Scale inputs.
      2. Generate a normals map from heights using this looping gradient noise.
      3. Multiply the normals by a Refraction Strenght input to be able to adjust dynamically.
      4. Add the normals to the Screen Position (Fragment Position)
      5. Use the transformed Screen Position as UVs to get the Screen Color from objects behind the water.
      6. Lerp this deformed Screen Color with the Depth Color from before, using the colors alpha.
      7. Set the alpha of the shader output to 1, because we are now manually lerping the blend with the scene color.
      8. Refractions for Water Depth
        • Connect the transformed UVs as source for the Scene Depth too, to make the water depth get refractions too.
  4. Foam effect

    1. Use the Depth Fade Sub Graph using the Foam Amount input, to generate a cutoff of where the foam should stop. (Foam should only show where objects are closer to the mesh)
    2. Use the UV Movement Sub Graph to loop over time a gradient noise, using the Foam Speed and Foam Scale inputs.
    3. Use the Step node to cut off the gradient to the calculated cutoff using the depth fade.
    4. Use this cut gradient and multiply it by the alpha channel of the foam color, to make it have the correct transparency.
    5. Finally lerp the two-color depth fade colors with the foam color, using the alpha we just calculated for the cutoff gradient for the foam. This will make a transition between the water colors and the foam.
    6. Now make the final lerp be between the refracted scene color and the combined water color + foam, based on the alpha channel for transparency.
  5. Waves effect

    1. Use the UV Movement Sub Graph to loop over time a gradient noise, using the Waves Speed and Waves Scale inputs.
    2. Multiply the Normal Vector by this gradient noise.
    3. Add these transformed Normal Vectors to the Position Vector to displace the fragment geometry
    4. Conditionally discard these values if "Use Waves" was set to false.
    5. Set the result position as the position in the output of the shader graph.
  6. Apply to object

    1. Create a Material with the Shader and apply it to the Water plane.

References

About

A stylized water shader with depth colors, refraction, normal displacement and foam effect. Using ShaderGraph in Unity URP.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published