water foam/shoreline shader?

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! :smile:

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: WaterFoam.zip - Google Drive


2248590–150201–FXWaterPro.shader (5.33 KB)

14 Likes

Does it solve this problem too? Water shorelines - Sky visible in places - Unity Engine - Unity Discussions

Yea, exactly what i need!! :smile:
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?

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.

2254355--150667--0437-1.jpg

1 Like

I need to teach my self how to create shaders :stuck_out_tongue:
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 :slight_smile:

@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 ?

@INedelcu This demo don’t support mobile.

1 Like

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 :
https://www.youtube.com/watch?v=fv71Pe9kTU0

Thanks in Advance.

2343891--158576--wave_n'_Whale_beginning.png
2343891--158577--wave_n'_Whale_beginning_Reference.png
2343891--158578--wave_n'_Whale.png
2343891--158579--wave_n'_Whale_Reference.png
2343891--158580--wave_n'_Whale_ending.png

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.

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?

@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!

Check this out,Highly optimized mobile water
http://forum.unity3d.com/threads/the-community-ocean-shader-open-source-unity-5.370818/page-3#post-2460148

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.

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.

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.

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.

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?

@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

@liiijz @AlvaroGonzalez I fixed the shader issue in 5.5.2f1. I prepared another improved version. I will upload it soon once I have fixed the 2 remaining bugs: https://drive.google.com/file/d/0B4LJksp0Jr1rVmFXMW9VTmdTRVU/view?usp=sharing

2993605–222956–WaterFoam55.zip (2.92 MB)

4 Likes