1. Help us improve the editor usability and artist workflows. Join our discussion to provide your feedback.
    Dismiss Notice
  2. We're looking for feedback on Unity Starter Kits! Let us know what you’d like.
    Dismiss Notice
  3. Unity 2017.2 beta is now available for download.
    Dismiss Notice
  4. Unity 2017.1 is now released.
    Dismiss Notice
  5. Check out all the fixes for 5.6 on the patch releases page.
    Dismiss Notice

water foam/shoreline shader?

Discussion in 'General Graphics' started by SveinEven, Aug 11, 2015.

  1. SveinEven

    SveinEven

    Joined:
    Sep 25, 2014
    Posts:
    25
    Hey!

    I'm trying to get some foam at the shoreline on my water, but i cant get it to work.
    I've tried water4 advanced, but i dont like the look of it.
    Now I'm using the WaterProDaytime.

    I've never made a shader, so I have no idea where to start.
    Does anyone have tips or something that could help me?

    All I'm looking for is a white foam edge of the water where the water collides with the terrain.

    Thanks in advance! :D
     
  2. INedelcu

    INedelcu

    Unity Technologies

    Joined:
    Jul 14, 2015
    Posts:
    18
    Hey!

    I've modified the WaterProDaytime shader to include the foam you wanted along the shorelines. Check the attached image and the new shader. A sample project using a terrain and the new water shader is also linked. I've added some new parameters to the WaterProDaytime material: a foam texture(found in standard assets), a gradient texture and a parameter to control the foam strength(or length).
    The most important changes are in the vertex shader and pixel shader. You can look after the HAS_FOAM define. It also uses _CameraDepthTexture to get the depth of the geometry behind the water plane.
    The project is made with Unity 5.1.2.

    The project is on Google Drive: https://drive.google.com/file/d/0B4LJksp0Jr1rNnFkdGhlZmZDdVU/view?usp=sharing
     

    Attached Files:

  3. Todd-Wasson

    Todd-Wasson

    Joined:
    Aug 7, 2014
    Posts:
    1,028
  4. SveinEven

    SveinEven

    Joined:
    Sep 25, 2014
    Posts:
    25
    Yea, exactly what i need!! :D
    Thanks!

    One question. If i want the foam texture to be larger, the texture, not the foam, how would i do that? Use a different texture maybe?
     
  5. INedelcu

    INedelcu

    Unity Technologies

    Joined:
    Jul 14, 2015
    Posts:
    18
    Yes, use a bigger texture if you want more detail.

    Right now there's not too much variation and the foam looks like a continuous strip. But if you want more variation you can modify the shader and use a 2D noise texture and modulate it with the final foam intensity. You should use something similar to the attached image that will cover the entire map. If you need help let me know.
     

    Attached Files:

    Last edited: Aug 17, 2015
    ahungrybear likes this.
  6. SveinEven

    SveinEven

    Joined:
    Sep 25, 2014
    Posts:
    25
    I need to teach my self how to create shaders :p
    I know c#, javascript and java. But i have no idea how a shader is put together.

    I would really appreciate a bit of help here :)
     
  7. idurvesh

    idurvesh

    Joined:
    Jun 9, 2014
    Posts:
    466
    @INedelcu Hi thanks much for shader,does it optimized for mobile devices?? Also could you guide me on how to make river water for river racing game like Shine runner and Ripitude GP ?
     
  8. bingheliefeng

    bingheliefeng

    Joined:
    May 8, 2014
    Posts:
    11
    @INedelcu This demo don't support mobile.
     
    idurvesh likes this.
  9. archat01

    archat01

    Joined:
    Oct 18, 2015
    Posts:
    2
    Hello INedelcu & Everyone,
    I'm new to Unity although I've fair amount of experience with C# and WPF. Right now I'm working on a project where foam and water interaction with game objects happen to be the trickiest part. I've made my water and waves tweaking codes from Scrawk blog. But left absolutely clueless about the foam and wave interaction with the game object. I'm uploading a few images where both what I've achieved and what is my target is portrayed (images with Reference at the end are my target). Any help, clue, comment on how to achieve it in Unity or if at all feasible in Unity, would be more than helpful for me. Since these posts are related to foam and water interaction, I thought of posting it here. If I made a mistake, my apology in advance.

    Link on Youtube to the reference Video is :

    Thanks in Advance.
     

    Attached Files:

  10. INedelcu

    INedelcu

    Unity Technologies

    Joined:
    Jul 14, 2015
    Posts:
    18
    Hi archat01

    We don't have that level of detail in Unity. That's clearly a water rendering that uses fluid simulation with all complex mathematical stuff or maybe it's even real :). You could use my project above to generate some foam near geometries but other than this there's no water interaction, just a flat plane.
     
  11. archat01

    archat01

    Joined:
    Oct 18, 2015
    Posts:
    2
    Hello INedelcu,

    I'm really grateful to hear from you. I almost understood that such fluid simulation with complicated foams is near impossible in Unity, still I was looking for a confirmation from someone much experienced. Many thanks once again. I hope if I bother for some suggestions sometimes you won't mind right?
     
  12. Dashin

    Dashin

    Joined:
    Jun 3, 2014
    Posts:
    1
    @INedelcu It works great using unity 5.2.1f1, or 5.2 in general, but not working in 5.3, the foam appears everywhere... anybody knows why? :S checked the code but can't find the problem...

    Thank you!

    PS: What sould I do to to make it mobile friendly? (Is working though, just trying to make it better), should I make it working with rendering path of forward? how?
    Thanks again!
     
    Last edited: Jan 14, 2016
  13. idurvesh

    idurvesh

    Joined:
    Jun 9, 2014
    Posts:
    466
    Check this out,Highly optimized mobile water
    http://forum.unity3d.com/threads/th...pen-source-unity-5.370818/page-3#post-2460148
     
  14. Sir-Spunky

    Sir-Spunky

    Joined:
    Apr 7, 2013
    Posts:
    125
    Hi,

    I tried your shader, and it looks amazing! However, I need to change my camera's clipping plane from the default 0.3-1000 to something like 10-10000. Unfortunately, this makes the foam disappear entirely as it seems dependant on a low clipping plane. Is there an easy way to fix this, or do I need to take another approach?

    Thanks for providing this, I think your shader should be included as the default water shader.
     
  15. sumitb_mdi

    sumitb_mdi

    Joined:
    Feb 20, 2016
    Posts:
    1
    Yes, this shader might not work for the mobile platform, because it uses the CameraDepthTexture. Some platform like mobile have the depth texture mode off by default to reduce the memory footprint.

    So to use this shader on mobile, you need to specifically mention Unity to enable the depth texture mode for your main Camera.

    So in you C# script add :

    mainCamera.depthTextureMode = DepthTextureMode.Depth;

    where mainCamera is the reference to your Main Camera.
     
  16. AlvaroGonzalez

    AlvaroGonzalez

    Joined:
    Dec 8, 2016
    Posts:
    1
    I'm really struggling with water. I've downloaded your project and it looks great. But when I create the same component in another project it just doesn't seem to do the trick. I'm creating a shoreline and I've tried with Basic Water, Water4 and WaterPro but none look anywhere close to realistic for a shoreline.

    Maybe it's totally unrelated but my problem is that even though I'm using your same values, my water just vaguely moves and hence it doesn't create any foam. It should still create foam when it collides with terrain, but it's not meant to work that way. I'm sort of new to Unity so I do not know what else am I missing to have a wave of foam like in your example.
     
  17. INedelcu

    INedelcu

    Unity Technologies

    Joined:
    Jul 14, 2015
    Posts:
    18
    Hi AlvaroGonzalez,

    Can you share a very basic project with your setup and the values you are using. I can do a GPU debug on my side if it's a shader bug.
     
  18. liiijz

    liiijz

    Joined:
    Dec 7, 2014
    Posts:
    3
    Nice Shader, I test it in Unity 5.3.3 it works well, but in Unity 5.5.0, there is no foam effect, no error information with the shader, I don't know why? How to fix it?
     
  19. liiijz

    liiijz

    Joined:
    Dec 7, 2014
    Posts:
    3
    @INedelcu I notice that when update to 5.5, these values changed
    is this the reason this water shader is not working on 5.5? Could you please modify a new version to 5.5?
    Thanks

    Shaders: Z-buffer float inverted
    The Z-buffer (depth buffer) direction has been inverted and this means the Z-buffer will now contain 1.0 at the near plane, 0.0 at the far plane. This, combined with the floating point depth buffer significantly increases the depth buffer precision resulting in less Z-fighting and better shadows, especially when using small near planes and large far planes.

    Graphics API changes:

    • Clip space range is [near, 0] instead of [0, far]
    • _CameraDepthTexture texture range is [1,0] instead of [0,1]
    • Z-bias is negated before being applied
    • 24 bit depth buffers are switched to 32 bit float format
     
  20. INedelcu

    INedelcu

    Unity Technologies

    Joined:
    Jul 14, 2015
    Posts:
    18

    Attached Files:

    paulbuck86 and Flurgle like this.
  21. MU-TH-UR

    MU-TH-UR

    Joined:
    Aug 16, 2016
    Posts:
    3
    Hi, is there any way to get the water up and running in VR with Forward Rendering and Single Pass Stereo? Foam/reflections works Deferred/MultiPass fine, foam goes away in Forward/MultiPass and bad reflections/no foam happen the rest.
     
  22. paulbuck86

    paulbuck86

    Joined:
    Apr 17, 2017
    Posts:
    14
    Uploaded this to the 5.6 project of mine. Everything seems to have imported correctly, except waves and foam isn't showing up. Not sure if you've had time recently to look at this. But it's a very interesting project for sure. I'll be tackling it later to see if I can get it to function correctly. I really like the prodaytime water with foam. The exact look I'm going for with water.