Search Unity

Optically Realistic Water

Discussion in 'Shaders' started by MuckSponge, Oct 19, 2018.

  1. MuckSponge

    MuckSponge

    Joined:
    Jul 11, 2015
    Posts:
    41
    I've been porting Martins Upitis' water shader from Blender to Unity. Got a pretty good reception on Reddit and many people wanted access to the source code so with Martins' permission I've placed it on GitHub.



    There's much more information about it on GitHub and I'll probably edit this post with more images and a video but for now, enjoy :)

    Free to use in your projects but it would be wise to treat this as more of a proof of concept. It requires two shaders - one for the water surface and one for anything which could be submerged in the water (such as terrain, etc.). Because of that second point, it might be difficult to adapt this to an existing project. It is set up this way so that the underwater lighting model can be used, which differs from your standard Lambert lighting model (it essentially has rim lighting and the light colour differs from above water due to scattering and extinction through the water body). With some time and elbow grease I may be able to move this into a deferred replacement shader which toggles between the default lighting model and the underwater model depending on whether the fragment is submerged or not.
     
    ROBYER1 likes this.
  2. ekergraphics

    ekergraphics

    Joined:
    Feb 22, 2017
    Posts:
    257
    This looks very promising. Does it work in single-pass stereo VR?

    I wonder if it can work in combination with something that does vertex displacement, so one can have proper waves...

    Also, I'm guessing it lacks a ripple effect for objects penetrating the surface, as well as foam, but it's a neat start!
     
  3. MuckSponge

    MuckSponge

    Joined:
    Jul 11, 2015
    Posts:
    41
    I haven't tried it with single-pass stereo VR so my guess is it won't work as-is. It's a very direct port of the original Blender implementation so it definitely lacks some features found in other solutions. I deliberately avoided labeling this as an ocean shader because of the lack of vertex displacement and foam/shoreline behaviour. It's a nice prototype to pick apart to see how to approximate realistic looking optics (light scattering, extinction, caustics, etc.) without the overhead of an entire ocean rendering system.

    I'm working on integrating it with a solution compatible with vertex displacement and removing the need for a specific material for underwater objects. I'm looking into a projected grid implementation but I don't have the expertise to create one from scratch and the existing solutions exhibit some bugs.

    I think to remove the dependency on the underwater material I will need to make this deferred-only and render the water geometry into the stencil buffer, and replace the default deferred lighting model to check the stencil buffer and branch to the underwater lighting model (stencil buffer is necessary due to the unpredictable change in water depth at a given fragment thanks to the vertex displacement).
     
  4. ROBYER1

    ROBYER1

    Joined:
    Oct 9, 2015
    Posts:
    282
    Will it ever work in single-pass stereo?