Foamy Liquid Shader

A variation of my animated water shader supporting foam around obstacles. Unfortunately I can’t prepare a video, but the foam is fully dynamic, it appears around places with small flow and is subject to flow animation. Attached is the archive with the shader, all necessary textures and a demo scene in Unity 3.5.x. Enjoy!

Alternately, blue channel in the flow map could be used for controlling where the foam appears, it’s very straightforward to change in the source code, so you shouldn’t have any trouble.

DOWNLOAD: foamy_fluid.zip

UPDATE: Thanks to courtesy of Janssens you can watch a video of his game using an improved version of my animated water shader with vertex displacement:

and also download the modified source code: flowmap_vertexdisplacement_shader.zip

12 Comments

  • Could you maybe explain on how to do it with the blue channel ?
    And also where there is blue there are waves formed (displacement on the Y axis ) so that blue spot has a scrolling transparent foam texture and there are waves only there .

    Also i saw you marked out the emmission , when there is a cubemap applied the foam texture is white only like glow

    Thanks !

    • Yeah well… there’s this line:

      float c = length(flowDir.rg);

      If you replace it with:

      float c = flowDir.b;

      it should do the blue trick. However with geometry displacement, it’s not that simple because unity surface shaders don’t support any kind of geometry manipulation.

      With the emission part I can’t remember why I commented it out. It either looked ugly or didn’t compile for my integrated Intel graphics card. In any case you can uncomment it and see what happens.

      PS. Just checked and maybe actually there’s some way to hook up vertex shaders with surface shaders. But you have to look into that yourself, sorry 😉

      • Okay many thanks for the blue part , the problem is i’m more a general programmer and i’m not so into shader programming so this is basically uncharted territory for me.

        Yeah i removed the comment and the foam just looked really ugly, like there is a constant glow on it (you can’t see the texture anymore)

        For the displacement i think i will look into the unity bootcamp water grabpass shader there is a sinus displacement wave contanstly rolling trough , maybe if i find some way to apply that to this shader i’m all done.

        The only sad thing is the reflections are failed. Without the cubemap reflection the water looks a bit weird , and in nature water mostly always have a refection.

        PS: i have the free version of unity.

        And at last may i ask where you learned to do CG shaders for unity ?

        Many thanks !

        • When i’m done with it i will make a video of it and credit you in the video and in the game where i will be using this shader.
          It’s in an outdoor setting with rough train , kinda like red dead redemption 😀

        • Actually I’ve learned it exclusively using Unity documentation and forums. It’s quite easy really, just a little program executed for each rendered pixel – some things come in (texture coordinates, light positions/colors/intensities, etc.), then there’s a bunch of things you can do with them (maths, texture lookups) and finally pixel color comes out. That’s all there is to it.

          With regards to this displacement mapping, now that I think of it – one could really try to do this purely on the pixel/surface shader level. It would have to be a bit similar to relief mapping though, involving loops to track which part of the displaced terrain the camera actually sees.


Leave a Reply

Your email address will not be published. Required fields are marked *