Search Unity

  1. Unity 2019.1 is now released.
    Dismiss Notice

Wanted: Ocean shader

Discussion in 'Works In Progress' started by bigkahuna, Jan 8, 2009.

  1. Zerk

    Zerk

    Joined:
    Jul 26, 2011
    Posts:
    4
    I have been playing around with this source and find it to be pretty neat. Reading some of the posts, and having experienced it myself, the accuracy of the GetWaterHeightAtLocation is inaccurate. One post said to change the GetModulus() to .Re. Did that and it's still inaccurate. I found that since the water can move, the transform of the ocean needs to be subtracted from the desired location in order to get the correct data. Here is my function that works. I converted it all to C# BTW.

    Code (csharp):
    1.  
    2.     public float GetWaterHeightAtLocation(float x, float y)
    3.     {
    4.         x -= transform.position.x;
    5.         y -= transform.position.z;
    6.         x = x / size.x;
    7.         x = (x-Mathf.Floor(x)) * width;
    8.         y = y / size.z;
    9.         y = (y-Mathf.Floor(y)) * height;
    10.        
    11.         int index = (int)width * Mathf.FloorToInt(y) + Mathf.FloorToInt(x);
    12.         float wheight = waterData.data[index].Re * scale / (width * height);
    13.         return wheight;
    14.     }
    15.  
     
  2. bigkahuna

    bigkahuna

    Joined:
    Apr 30, 2006
    Posts:
    5,435
    Just a short note to say that I spent a bit of time on this and got a "version" of the shader working on Windows 7 with Unity 3.5.6 Pro. If there is interest, and when I have time, I'll re-bundle this working version and post it here and on the wiki.
     
  3. antenna-tree

    antenna-tree

    Joined:
    Oct 30, 2005
    Posts:
    5,325
    Bumping to bring it back into the fold after going dormant in the Collaboration section..
     
  4. Reanimate_L

    Reanimate_L

    Joined:
    Oct 10, 2009
    Posts:
    2,350
    Any news for the latest version of the Ocean Project?
     
  5. bigkahuna

    bigkahuna

    Joined:
    Apr 30, 2006
    Posts:
    5,435
    It's working on my machine, just haven't had time to sort out the assets and build a proper / functioning package yet. But thanks for reminding me, it's on my to do list. ;)
     
  6. DragonRider

    DragonRider

    Joined:
    Jul 19, 2012
    Posts:
    9
    I managed to get CommunityWaterExperiment3 mostly functioning in 3.5 Indie, but due to shader restrictions with RenderTextures, I only get skybox reflections and wave-crest foam. It still looks fantastic ( if somewhat glassy ) but since the water color shader relies so heavily on procedurally-generated RenderTextures, you can't change the color of the water. Needless to say, the underwater effect is disabled in Indie as well.

    That said, I imagine it wouldn't take an undue amount of effort to create a simpler color material that looked fairly decent.

    Initially I had some issues with waterCompositionMaterial being initialized as null and remaining that way up through the realtime color computations which resulted in a lot of NullReference Exceptions. It looked like the material was only assigned a shader at startup via Shader.Find(), which, for some reason, wasn't working, so I exposed the waterCompositionMaterial variable in the editor, created a new Material asset that uses the WaterComposition shader, and assigned it to waterCompositionMaterial by hand. Haven't gotten any NullReference Exceptions since, but I don't know if this approach will transition well over to Pro because Indie can't render the effects that waterCompositionMaterial is used for anyway.

    Due to the skybox having a black base, the water reflection sometimes has black spots that look out of place. This could be fixed by inverting the top half of the skybox and stitching that onto the bottom to replace the blackness with mirrored sky. In most cases that would make the horizon look weird from high elevations, but for our purposes the water is only going to be reflecting sky anyway. Just assign the new skybox to the reflection only and hey presto, no black spots and the original horizon remains intact.

    Short version: Waves are great, water has no color, foam works, no underwater effect, and water reflects skybox only - looks glassy but most definitely wet.

    Here's my quick-and-dirty patched version of CommunityWaterExperiment3 for Indie ( project files only; full build is too big to upload ).

    Note: As the source files were fairly old, this version may not be as up-to-date as others. It should work, but you may find yourself replacing it with newer versions from other contributors.

    View attachment $CommunityWaterExperiment3-mod.zip
     
  7. bigkahuna

    bigkahuna

    Joined:
    Apr 30, 2006
    Posts:
    5,435
    Pretty amazing first post! :D

    FYI - Your version works fine with Unity Pro 3.5.6 running under Windows 7. All the reflections and refractions appear the same as in the version of the ocean project I have. Thanks very much for your contribution!
     
  8. BrUnO-XaVIeR

    BrUnO-XaVIeR

    Joined:
    Dec 6, 2010
    Posts:
    1,687
    Renamed all 'LOD' to 'L0D' and compiled into Unity4; rolled back the 'ultra hack from hell' (LOL) for OsX because it wasn't working too.
    Overall it works pretty nice, very cool!
     
    Last edited: Nov 14, 2012
  9. HeadHunter2009

    HeadHunter2009

    Joined:
    Jul 3, 2012
    Posts:
    49
    I made a big optimisation for ocean, and now it work on mobile, even on armv6.
    This is game we working on now:






    Game called DarkWaters and will be relised on Android in a few months)
     
  10. ChadM

    ChadM

    Joined:
    Jul 3, 2012
    Posts:
    73
    Nice work! Care to share source for the changes you made to optimize the shader/ocean?
     
  11. creat327

    creat327

    Joined:
    Mar 19, 2009
    Posts:
    1,002
    yeah, that was the purpose of this thread, since the original was open source... it would be nice to share it too :)
     
  12. bigkahuna

    bigkahuna

    Joined:
    Apr 30, 2006
    Posts:
    5,435
    That's not necessarily true. My original post was to solicite paid help to develop an ocean shader system for Unity. That was a long time ago and I've lost count of all the water and ocean shaders that have become available since, including some very generous contributions posted to this thread. It's up to each author to decide whether their contribution is to be shared or not, but obviously if your work is based on someone else's and you didn't pay to use it, then I think "the rule of good karma" should dictate what you do. ;)
     
  13. creat327

    creat327

    Joined:
    Mar 19, 2009
    Posts:
    1,002
    true then, anyhow I thought it was based on the one on here and we were asked to share modifications (I thought so :)
     
  14. ChadM

    ChadM

    Joined:
    Jul 3, 2012
    Posts:
    73
    Maybe someone can point me in the right direction..

    I'm looking for a water/ocean shader that has realistic waves that would look good like Head@Hunter's images. It will be for a ship type game and would need to look infinite.

    Is there an example of that in this thread or online? BTW, I have Unity pro.
     
  15. HeadHunter2009

    HeadHunter2009

    Joined:
    Jul 3, 2012
    Posts:
    49
    I will make package when i have free time ;)

    Ok here zipped project folder:
    http://www.mediafire.com/?jd16s3hf8pc661d

    And here unitypackage file:
    http://www.mediafire.com/?kqg6p0vtnm6pcf0

    Open project or import package, and then open "Main" scene file in "Ocean" folder, click play to see ocean in action, all configured for prettie fast working on Mobile.
    You need Unity Pro to use this package, but you always can try to disable reflection and refraction to use it in Unity Indie.

    Have fun :)
     
    Last edited: Dec 18, 2012
  16. norby

    norby

    Joined:
    Jul 10, 2006
    Posts:
    257
    Hi Head@Hunter

    Thank you very much

    Norby
     
  17. tatelax

    tatelax

    Joined:
    Feb 4, 2010
    Posts:
    1,146
    Thanks Head@Hunter!

    Any plans to make this work with Unity 4?
     
  18. Reanimate_L

    Reanimate_L

    Joined:
    Oct 10, 2009
    Posts:
    2,350
    Just renamed the LOD with L0D just like BrUnO XaVIeR explained
     
  19. creat327

    creat327

    Joined:
    Mar 19, 2009
    Posts:
    1,002
    head@hunter I would give you karma if it was possible :)
     
  20. creat327

    creat327

    Joined:
    Mar 19, 2009
    Posts:
    1,002
    head@hunter
    I got your version of the code and ported it to c# and unity4. Even after some optimizations it runs poorly on a nexus one (arm7 device) and on a arm6 is simply a no-no (5 fps when tested on 3.5).

    I went over all of it on the migration, improved sqrt and normalized calls and things like that, but the improvement I doubt it's even better than 10% on speed. Not exactly impressive after all the work I did with the profiler :(

    So, the only way I see this to be fast is either not to use FFT and switch to something else or to get the FFT code, use pointers and unsafe code, compile it with visual studio and include it as a dll into the project. That should make it 50% to 70% faster if done right.
    The problem is that modifying that FFT code is a nightmare for me. Is there anyone who got a good FFT, hyper fast, that can be compiled to a .net dll? or someone with the guts to do it from the current FFT implementation, or better yet, got a better/faster algorithm to replace the FFT chunk?
     
  21. creat327

    creat327

    Joined:
    Mar 19, 2009
    Posts:
    1,002
    Actually guys, maybe it's a silly question but I just plugged the Water4 from unity prefabs, and even the pro advanced (more detailed) version runs at twice the speed the ocean shader in here on my android device. So it seems water4 works great on mobile too.
    What's the advantage of using Ocean Shader vs Water4 included in unity at this point?
     
  22. creat327

    creat327

    Joined:
    Mar 19, 2009
    Posts:
    1,002
    The thread seems to be a bit dead, but anyway I'm uploading here the changes I made to the mobile version. It runs at 30fps on my samsung galaxy 2.
    I ported it all to c#, made some optimizations and also added a new FFT library I found on the internet. It's not used but it's apparently faster than the current one. I just won't have time to look into it until new years, so if anyone wants to give it a try...

    btw, I found a whole bunch of UV code removed from the code. I tried to put it back but it doesn't work, so if someone finds it useful (it looks useful to be able to move the UV so it looks animated) can try to put it back.

    I added options on screen to play around, I saw no difference on changing the normals on screen.
     

    Attached Files:

  23. HeadHunter2009

    HeadHunter2009

    Joined:
    Jul 3, 2012
    Posts:
    49
    We working on game for Tegra3 and others top devices, on galaxy S3 we have around 30+ fps with ocean and all ships.
    And thanks for sharing improvements)

    UPD. I tryed your c# code, for some reason it work with the same speed, only thing is ocean tiles with the same value is too small, and waves now dont look good, before ocean have big scrolling waves and now it dont look as good as before. Can you please tell what what heppence with waves?
     
    Last edited: Jan 10, 2013
  24. GeneBox

    GeneBox

    Joined:
    Nov 15, 2012
    Posts:
    480
    Holy Guacamole! This looks fantastic!
     
  25. SevenBits

    SevenBits

    Joined:
    Dec 26, 2011
    Posts:
    1,953
    Great! Looks fantastic.
     
  26. HeadHunter2009

    HeadHunter2009

    Joined:
    Jul 3, 2012
    Posts:
    49
    My awasome partner just made wery cool boat wake, the idea was to generate some tangents on player position, and then shader read them like foam.
    Video showing how it looks in game:

    to add this cool future you need to search for parts where tangents get generated, should be around 595 lines.
    And replace this part with this code:
    Code (csharp):
    1.  
    2. public float wakeDistance = 5f; //Should be class variable
    3.  
    4. if (reflectionRefractionEnabled) {
    5.             for (int y = 0; y < g_height; y++) {
    6.                 for (int x = 0; x < g_width; x++) {
    7.                     int item=x + g_width * y;
    8.                     if (x + 1 >= g_width) {
    9.                         tangents [item].w = tangents [g_width * y].w;
    10.                    
    11.                         continue;
    12.                     }
    13.                     if (y + 1 >= g_height) {
    14.                         tangents [item].w = tangents [x].w;
    15.                        
    16.                         continue;
    17.                     }
    18.                     float right = vertices[(x + 1) + g_width * y].x - vertices[item].x;
    19.                     float foam = right/(size.x / g_width);
    20.                     if (foam < 0.0f)
    21.                         tangents [item].w = 1f;
    22.                     else if (foam < 0.5f)
    23.                         tangents [item].w += 3.0f * Time.deltaTime;
    24.                     else
    25.                         tangents [item].w -= 0.4f * Time.deltaTime;
    26.  
    27.                     if (player != null )
    28.                     {
    29.                         Vector3 player2Vertex = (player.position - vertices[item] - transform.position);
    30.                         // foam around boat
    31.                         if (player2Vertex.x >= size.x)
    32.                             player2Vertex.x -= size.x;
    33.                        
    34.                         if (player2Vertex.x<= -size.x)
    35.                             player2Vertex.x += size.x;
    36.                        
    37.                         if (player2Vertex.z >= size.z)
    38.                             player2Vertex.z -= size.z;
    39.                        
    40.                         if (player2Vertex.z<= -size.z)
    41.                             player2Vertex.z += size.z;
    42.                         player2Vertex.y = 0;
    43.                        
    44.                         if (player2Vertex.sqrMagnitude < wakeDistance * wakeDistance)
    45.                             tangents[item].w += 3.0f * Time.deltaTime;
    46.                     }
    47.                     tangents [item].w = Mathf.Clamp (tangents[item].w, 0.0f, 2.0f);
    48.                 }
    49.             }
    50.         }
     
    Last edited: Mar 3, 2013
  27. bigkahuna

    bigkahuna

    Joined:
    Apr 30, 2006
    Posts:
    5,435
    Pretty nice, I'll have to try it. I did something similar using projectors but I think your solution looks even better. ;)
     
  28. HeadHunter2009

    HeadHunter2009

    Joined:
    Jul 3, 2012
    Posts:
    49
    Can you please share your projector solution? I spend a lot of time figuring out how to make this using projector but witout success)
     
  29. bigkahuna

    bigkahuna

    Joined:
    Apr 30, 2006
    Posts:
    5,435
  30. HolBol

    HolBol

    Joined:
    Feb 9, 2010
    Posts:
    2,886
    Head@Hunter, this isn't a thread to show off your game. Create your own thread for that; this is only for the Ocean Shader project.
     
  31. creat327

    creat327

    Joined:
    Mar 19, 2009
    Posts:
    1,002
    head@hunter the waves and all look the same, you just need to change the settings on the screen until you get the storm effect you want
     
  32. FargleBargle

    FargleBargle

    Joined:
    Oct 15, 2011
    Posts:
    649
    <rant>This thread, as well as ocean shader development in general, was almost dead until Head@Hunter showed up. Besides pictures, he's also been posting updated code, optimized for mobile devices. His is the first new development on this project in ages. Looking back through the thread, I don't see any recent contributions from you. If Head@Hunter wants to post relevant examples, demonstrating what his version of the shader looks like, so that others can decide if they want to download and try his mods, I say let him. Attacking people who are contributing is no way to encourage them to share their work with the rest of the community.</rant>
     
  33. HeadHunter2009

    HeadHunter2009

    Joined:
    Jul 3, 2012
    Posts:
    49
    I found a way how to optimize ocean a little better for mobile, OnWillRenderObject function WERY expencive on mobile. I also made cool looking editor script for Ocean inspector. In few days i will post updated version.
     
  34. bigkahuna

    bigkahuna

    Joined:
    Apr 30, 2006
    Posts:
    5,435
    Cool, looking forward to it. :D
     
  35. bigkahuna

    bigkahuna

    Joined:
    Apr 30, 2006
    Posts:
    5,435
    Have a look at what Nvidia is working on for later this year:

     
    RendCycle likes this.
  36. Pauls

    Pauls

    Joined:
    Nov 12, 2012
    Posts:
    46
    Hi,
    I was wondering if someone could provide a small explanation of the code, for example from the folder : OceanMobileJoaquin.zip above, to understand roughly how it works? For example, the relation between the bumpmap and the ocean, what does the Fourier script (roughly) and few words about the shader (where they are used in the code?). I am interested in understanding this great project, and with a small help at the beginning, I think it would be easier ;-) Thanks
     
  37. skeptika

    skeptika

    Joined:
    Dec 31, 2011
    Posts:
    103
    Thank you headhunter, really appreciate you work and interest. If you get a chance, I'd love to see your most recent optimized version? :) I'm loving playing with this shader, it's so much fun!
     
  38. SAFAD

    SAFAD

    Joined:
    Mar 1, 2013
    Posts:
    1
    Delete Fluvio, This community version is ultra awesome,
    any Ideas on how to shape the water ?
     
  39. ZJP

    ZJP

    Joined:
    Jan 22, 2010
    Posts:
    2,646
    +1. The results you showed on your last video is awesome.
     
  40. HeadHunter2009

    HeadHunter2009

    Joined:
    Jul 3, 2012
    Posts:
    49
    Sorry guys i wery busy at this moment, but when i will get a few free hours i will post updated version of the code ;)
     
  41. peyay

    peyay

    Joined:
    Oct 8, 2012
    Posts:
    6
    Is there a way to get anti-aliasing to work underwater? Everything is very pixelated.
    I also get this weird white flickering boarder around the camera.

    Any solutions to this would be great. thanks.
     
  42. joukev

    joukev

    Joined:
    Apr 22, 2013
    Posts:
    10
    Because of the Oculus Rift, I am currently just starting with Unity and really appreciate this development on ocean shaders: my objective is to create a full blown sail simulator for solo ships (laser, Finn) and will include a motion platform etc etc.
    This is a work in progress, e.g. have a look at http://www.youtube.com/watch?v=0PAdxzdZgVk and http://www.youtube.com/watch?v=lU9uJcNhNE4
    I am able to run and tune the mobile shader, the wake effect does not match up well (tiling behavior) etc etc but I first need to learn the basics on unity so nothing to show you yet.
    ANy advice, links or comments are appreciated...
     
    Last edited: May 6, 2013
  43. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,671
    Joukev: Is this running in Unity yet? I'd love to see any Unity WiP's!
     
  44. joukev

    joukev

    Joined:
    Apr 22, 2013
    Posts:
    10
    No it isn't yet done. It might be ported to Unity3D, but not before our Rift arrives in the late summer....
    Kind regards,
    PS your sales representatives are not replying on my mails regarding educational licenses etc.
     
  45. creat327

    creat327

    Joined:
    Mar 19, 2009
    Posts:
    1,002
    @headhunter any updates? I'm very curious to see what you managed to do with the mobile version I gave out :)
    I also uploaded it to the script section but nobody replied there, so it seems only you and joykev were able to pick it up and improve upon it so far. Hopefully it gets much better. I think someone with a serious shader experience should be able to cut the per pixel area of the shader and make it twice as fast.
     
  46. joukev

    joukev

    Joined:
    Apr 22, 2013
    Posts:
    10
    Just a brief update: now unity3D has a FREE iOS license I tried the (slightly modified) mobile ocean shader on my 4S where it runs @30FPS consistently... YAY. Now I want the wake effect in the right place (guess tedious because of the tiling effect in the mesh).
    I also tried the indie ocean shader (frp, http://scrawkblog.com/2013/02/24/ocean-renderer-for-unity/ but that doesn't produce statisfying effects (just lightblue surface @6FPS). Please correct me if I'm wrong, still a novice Unity3D user...

    EDIT I MADE A MISTAKE: headhunters code runs at the same FPS as creat327s code (with FPS counter now).
     
    Last edited: May 21, 2013
  47. creat327

    creat327

    Joined:
    Mar 19, 2009
    Posts:
    1,002
    i'm curious to see what @headhunter was able to achieve
     
  48. heavyh2o

    heavyh2o

    Joined:
    Nov 4, 2012
    Posts:
    317
  49. creat327

    creat327

    Joined:
    Mar 19, 2009
    Posts:
    1,002
    @heavy yeah we know them. The video is based on code that it's almost 10 years old. Triton is a pretty popular asset on the store but it's expensive. the ocean shader was born out to be free and community based open source code.
     
  50. heavyh2o

    heavyh2o

    Joined:
    Nov 4, 2012
    Posts:
    317
    Ok, thanks for the info.