Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Wanted: Ocean shader

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

  1. HarvesteR

    HarvesteR

    Joined:
    May 22, 2009
    Posts:
    531
    I got it!!! :D :D :D :D

    All working now!! buoyancy now will follow the surface regardless of scale


    just had to replace a function call in the return statement... and undo a lot of previous failed attemps :roll:

    anyways, it's all working now!! and it only took an entire night and the whole morning!! 8)

    Here's the modified GetWaterHeightAtLocation function:


    Code (csharp):
    1.  
    2. function GetWaterHeightAtLocation (x : float, y : float) {
    3.     x = x / size.x;
    4.     x = (x-Mathf.Floor (x)) * width;
    5.     y = y / size.z;
    6.     y = (y-Mathf.Floor (y)) * height;
    7.  
    8.     return data[width * Mathf.FloorToInt(y) + Mathf.FloorToInt(x)].Re * scale / (width * height);
    9. }
    10.  
    I haven't tested, but I'm fairly sure you can continue using the original boyancy.js script... it's probably working fine (mine just simplified the contact points)

    anyways, just replace the old water height function for this one and you should be bobbing up and down just fine :wink:

    Cheers everyone, I'm going to put some ice on my brain now

    HarvesteR
     
  2. bigkahuna

    bigkahuna

    Joined:
    Apr 30, 2006
    Posts:
    5,434
    @HarvesteR - Could you post a new asset bundle or project folder? I'm trying to follow your posts but it's not working. Thanks. :)
     
  3. ToreTank

    ToreTank

    Joined:
    Jun 23, 2008
    Posts:
    165
    HarvesterR: Nice work! I'm sure that will come in handy :)

    Yann: Added some sort of underwater refraction, but I'm not sure if it adds to the realism. Not quite the formula you listed, but feel free to replace the one I ended up using with your own! I added a comment in the shader where your magic touch will be appreciated :) Oh, and I am also using the bumpmap from the ocean surface as a displacement map. It should be easy enough to replace it with something else (something lower frequency, maybe?), search for _UnderwaterDistortionTex.

    There are only small updates to two of the files (included HarvesterR's fix, but I have to admit that I haven't tested if it works other than making sure there were no compile errors), uploaded them here in case anyone is interested:

    http://www.terravision.no/~trondabu/unity/ocean_scripts/Ocean.js
    http://www.terravision.no/~trondabu/unity/ocean_scripts/WaterComposition.shader

    It would be cool to get the underwater godrays from this into the ocean stuff (near the bottom): http://www.gamasutra.com/gdce/2001/jensen/jensen_03.htm (yes, it has been referred to a couple of times in this thread already, but it is good :) ) Doesn't seem too complicated, but I suspect the overdraw will be pretty intense. Any takers?

    Edit: Did a quick test with the buoyancy script, and it seems to work fine (a box with a convex mesh collider).
     
  4. Yann

    Yann

    Joined:
    Oct 20, 2007
    Posts:
    432
    @Harvester : excellent, you deserve your ice bag ! :)

    @ToreTank : great, thanks a lot ! I think you did the main part of the work for this underwater refraction, and now it should just be a matter of tweaking. I'll see what I can do.

    Cheers to all :D
     
  5. HarvesteR

    HarvesteR

    Joined:
    May 22, 2009
    Posts:
    531
    Hi again,

    @bigkahuna - I didn't post an asset bundle because I am working with a much older version of the Ocean script here... I'm on Indie, and the most recent versions won't work without Unity Pro...

    In the recent versions, the Ocean script has been so modified that it's nigh unrecognizable... so I just posted the function

    If you guys want, I'll post my buoyancy script too... it's a little easier to understand, and it places gizmos over the water contact points so you can see how it's following the surface (although I'd like to clean it up a bit before... make for something more presentable :wink: )

    Also, I want to make possible to create more than 4 contact points, to support long boats

    One more thing, I noticed today that the new waterHeight function does not take into account the choppiness of the waves... on choppy seas the boat might still leap a little out of the water... so there's still room for further improvement... of course this only happens if you're using a very small boat (smaller than the waves themselves)

    Cheers
     
  6. Yann

    Yann

    Joined:
    Oct 20, 2007
    Posts:
    432
    @HarvesteR : long boats, great ! :D

    @ToreTank : ok, I tweaked the values... here's the visible result, as for now : it's a quick dirty scene (hadn't much time) with this effect applied. This time, it starts underwater... :) You can swim around the three gentlemen in a way that tries to mimic underwater movements (still to improve).

    The scene :

    www.tiviboo.com/unity/underwater_encounter

    A more dramatic (and heavier) version :

    www.tiviboo.com/unity/underwater_encounter2


    Here I quote the edited part of the shader :

    Code (csharp):
    1. half4 frag (v2f i) : COLOR
    2. {
    3.     half3 source = texRECT(_MainTex, i.uv.zw).rgb;
    4.     float depth = tex2D(_DepthTex, i.maskSpacePos.xy).r;
    5.    
    6.     //Yann: You might want to make this match your formula better?
    7.     float2 uvscaled = i.uv.xy*0.03 + _Time.x/10;
    8.     float2 distort = ((tex2D(_UnderwaterDistortionTex, uvscaled).rg * 4.0) -2.2);
    9.    
    10.     half3 underwater = tex2D(_UnderwaterTex, i.uv.xy + distort * 0.03).rgb;
    11.  
    12.  
    13.    
    14.     float delta =  i.maskSpacePos.z - depth;
    15.    
    16.     float underwaterFactor = 0.0;
    17.    
    18.     if (delta >= 0)
    19.         underwaterFactor = 1.0;
    20.    
    21.     //Smooth out the water/air intersection. This can be used to sample from a foam texture
    22.     //for improved effect.
    23.     underwaterFactor += (0.01 - clamp(abs(delta), 0, 0.01)) * 100;
    24.    
    25.     underwaterFactor = clamp(underwaterFactor, 0, 1);
    26.    
    27.     half3 result = lerp(source, underwater * _WaterColor, underwaterFactor);
    28.    
    29.  
    30.     return half4(result, 1.0);
    31. }
    32.  
    33.  
    Note : the effect's quality being directly influenced by the size of the "underwaterTexture", I think it would be good to make it a public variable in the ocean script. A size of 1024 * 1024 seems to be a very minimum.[/url]
     
  7. ToreTank

    ToreTank

    Joined:
    Jun 23, 2008
    Posts:
    165
    Yann: That is looking very cool! Both of your versions run very smooth on my system :)

    I've been playing around with this ocean stuff again today, so I thought I'd report an interesting discovery.

    I was going to figure out what changes needed to be done to get this stuff running on a low-end graphics card (which in my world is a GMA950). Just to see how bad stuff was, I made a build, and ran it on a testcomputer with the minimal hardware. It was no surprise that it ran like a slideshow, but what was a huge surprise, at least to me, is that all of the shaders seem to work fine! I didn't quite believe my eyes, but after having pulled some GMA950 specs off of the Internet (wikipedia, mainly), it turns out it supports pixel shader 2.0!

    So I made some quick editions to the Ocean.js script in order to manually turn on/off passes to figure out which ones took the most time and how many I had to remove to get acceptable framerates (which is hugely individual, I guess, but in my case it means somewhat interactive movement and the illusion of motion). Here's the funny part - the single most demanding pass was the waterdepth pass - when I noticed this, I came to think of the fact that this is a depth texture to get the most precision out of the underwater/above water intersection rendering. This will work using a regular color texture as well, at the cost of less depth resolution (but compared to the loss in framerate, this is completely acceptable - the precision issue can be handled by encoding the depth in all of the color channels, but for my testing purposes I have not done that). Converting this to a regular color-texture made the demo run pretty nice on the testcomputer. Apparently, the GMA950 is not too happy about rendering into floating point textures, or sampling from them - not sure where the bottleneck is.

    To make a long story short: In order to get this running on a low-end graphics hardware, locate this line in Ocean.js:

    Code (csharp):
    1. waterHeightTexture.format = RenderTextureFormat.Depth;
    Convert it to:

    Code (csharp):
    1. //waterHeightTexture.format = RenderTextureFormat.Depth;
    Voila - interactive water on low-end hardware!

    Note: I did this on a Windows computer, not sure if the same trick applies in OpenGL.
     
  8. Yann

    Yann

    Joined:
    Oct 20, 2007
    Posts:
    432
    He he, congrats on this new one, ToreTank ! For OpenGL, I'll make a test tomorrow on a 2006 Mac Mini.
     
  9. Yann

    Yann

    Joined:
    Oct 20, 2007
    Posts:
    432
    oops, forgot to post the results : it seems that the trick doesn't benefit to the Mac.

    Here is the modified version of my latest underwater scene : www.tiviboo.com/unity/undrwater_encounter_gma950
    I noticed no FPS change on a high end iMac and a recent MB Pro, and the same behavior on an old Intel Mac mini.
     
  10. bigkahuna

    bigkahuna

    Joined:
    Apr 30, 2006
    Posts:
    5,434
    AngryAnt asked me to start a page in the Wiki for this project. It can be found here: http://www.unifycommunity.com/wiki/index.php?title=Unity_Projects .

    I would like to encourage all developers who have posted code or modifications to other people's code to add them to this Wiki page. This way it will not only be easier to find but also keep updated. If you need help posting in the Wiki you can ask in the IRC channel. Thanks! :)
     
  11. 401K

    401K

    Joined:
    Jun 18, 2009
    Posts:
    15
    Hi,

    Here is an amazing work, thanks to all developers that help to make this great tool 8)

    I try to use it in a scene under a Unity Indie version but have some problems with the boyants objects.
    I have a long boat with a rigidbody, mesh collider and the boyancy script set with my ocean.

    With the last ocean script (post by ToreTank), console said that WaterPostEffect does not denote a valid type, and Transform position Y of my shape only decrease slowly with the time...

    With an older version, boat seems to up and down a little bit on the waves (Y position variation between 0.5 and 2.5) but stay on the surface :?

    Could you give the right steps to set a realistic boyant object as it possible.
    Is a complete archive is planned to be post on the wiki ?
    Thanks for your help.
     
  12. Pia

    Pia

    Joined:
    Feb 16, 2009
    Posts:
    78
    Marvelous! I think many users (including me) would be really grateful if someone could upload an up-to-date demo scene with all required scripts and shaders to the Unify Wiki.
     
  13. jojimbo

    jojimbo

    Joined:
    Jun 25, 2009
    Posts:
    37
    o wow,just wow,didnt know this thread was here.

    ahh pro needed,just as well im doing a space game eh :)
     
  14. grfxman

    grfxman

    Joined:
    May 28, 2009
    Posts:
    309
    The last demo posted was incredible !!! cant wait to download the next update of this :)
     
  15. norby

    norby

    Joined:
    Jul 10, 2006
    Posts:
    277
    Hi all

    i was just wandering where i can find the latest version of the ocean package. it seems that there are different versions.

    Yann : does the code you are using contain all modifications?

    would be nice to play with it again

    thanks

    Norby :D
     
  16. Yann

    Yann

    Joined:
    Oct 20, 2007
    Posts:
    432
    Norby, I'm afraid not... I used ToreTanks' latest posted version :

    http://www.terravision.no/~trondabu/unity/Ocean_ReflRefrUnderwaterRefr.zip

    then included the mods he posted later for underwater effects :

    http://www.terravision.no/~trondabu/unity/ocean_scripts/Ocean.js
    http://www.terravision.no/~trondabu/unity/ocean_scripts/WaterComposition.shader


    plus some tweaking I made to the shader :


    Code (csharp):
    1. half4 frag (v2f i) : COLOR
    2. {
    3.    half3 source = texRECT(_MainTex, i.uv.zw).rgb;
    4.    float depth = tex2D(_DepthTex, i.maskSpacePos.xy).r;
    5.    
    6.    //Yann: You might want to make this match your formula better?
    7.    float2 uvscaled = i.uv.xy*0.03 + _Time.x/10;
    8.    float2 distort = ((tex2D(_UnderwaterDistortionTex, uvscaled).rg * 4.0) -2.2);
    9.    
    10.    half3 underwater = tex2D(_UnderwaterTex, i.uv.xy + distort * 0.03).rgb;
    11.  
    12.  
    13.    
    14.    float delta =  i.maskSpacePos.z - depth;
    15.    
    16.    float underwaterFactor = 0.0;
    17.    
    18.    if (delta >= 0)
    19.       underwaterFactor = 1.0;
    20.    
    21.    //Smooth out the water/air intersection. This can be used to sample from a foam texture
    22.    //for improved effect.
    23.    underwaterFactor += (0.01 - clamp(abs(delta), 0, 0.01)) * 100;
    24.    
    25.    underwaterFactor = clamp(underwaterFactor, 0, 1);
    26.    
    27.    half3 result = lerp(source, underwater * _WaterColor, underwaterFactor);
    28.    
    29.  
    30.     return half4(result, 1.0);
    31. }
    As for the latest version, I think it was meant to include Nikko's improvements, too - but I don't have it for the moment.
     
  17. norby

    norby

    Joined:
    Jul 10, 2006
    Posts:
    277
    Hi Yann

    It would be great to have this version with nikko's improvements.
    So we can put it on bigkahuna's Wiki entry :D :D

    Thanks for your reply

    Norby
     
  18. Digitalos

    Digitalos

    Joined:
    Jun 1, 2009
    Posts:
    112
    I'm also interested in this, would be good if the main post or the aforementioned wiki could be updated.
     
  19. RHD

    RHD

    Joined:
    Mar 30, 2009
    Posts:
    719
    HarvesteR can you be kind enough to post what you have for those of who also don't have Pro yet? Also your Boyancy script? I've been having exactly the same problem and I'm so happy someone has fixed it as it's beyond me at the moment!

    It would be greatly appreciated.

    Really Amazingly Fantastic work here guys! In years to come this thread will be the stuff of legends...
     
  20. RHD

    RHD

    Joined:
    Mar 30, 2009
    Posts:
    719
    Hi,

    These links are broken, can you repost?

    cheers!
     
  21. nikko

    nikko

    Joined:
    Mar 20, 2009
    Posts:
    436
    I'll very soon, when the v2.6 will be released. All my projects are only working on the beta so none can work with the 2.5.x version of the player.
     
  22. Dakta

    Dakta

    Joined:
    Apr 8, 2008
    Posts:
    492
    Sneaky bastard got himself a hold of the beta... LUCKY!
     
  23. RHD

    RHD

    Joined:
    Mar 30, 2009
    Posts:
    719
    Is this Pro only or will a version work on Indie?
     
  24. RHD

    RHD

    Joined:
    Mar 30, 2009
    Posts:
    719
    This is my first attempt. As I'm on Indie I can't use the more recent versions so am making do with the earliest one but I'm still very pleased with it.

    I've a few things to work out like getting the ocean to follow the camera so you can't see the joins, waves on the coast line and a wake for the boat. Plus getting the boat moving, but I'm really pleased with it even at this stage.

    Huge thanks to everyone who worked on this!

    http://web.mac.com/hayesdavies/Portfolio/oceanbuild1.html
     

    Attached Files:

  25. nikko

    nikko

    Joined:
    Mar 20, 2009
    Posts:
    436
  26. RHD

    RHD

    Joined:
    Mar 30, 2009
    Posts:
    719
    Video doesn't seem to be working. I guess this version only works on Pro?
     
  27. bigkahuna

    bigkahuna

    Joined:
    Apr 30, 2006
    Posts:
    5,434
    Hi Nikko,

    That's looking very nice. Are you able to adjust the underwater "fog"? It looks too dense, I think, to be realistic looking. Also, any thoughts on how to handle "boat wakes"? What I've seen done in other engines is that a string of particles are placed behind the boat that lay just above the water's surface, but I haven't figured out how to keep them there. Any other ideas?

    Keep it going, it's looking good!
     
  28. RHD

    RHD

    Joined:
    Mar 30, 2009
    Posts:
    719
    I've been thinking about boat wakes.

    In the Island demo there's a mesh round the edge of the sea with an animated texture of some sort on it for foam where the waves hit the beach. I was wondering about something like this maybe combined with a particle system for spray.

    The tricky bit would be getting the wake and the spray to adjust according to the speed of the boat and you'd probably need to carve into the ocean mesh a little to look realistic.
     
  29. bigkahuna

    bigkahuna

    Joined:
    Apr 30, 2006
    Posts:
    5,434
    If you look at a real boat wake (see photo), you can probably divide it into two separate projects:


    The first being the bow wake and the second is the stern wake.

    I've seen the bow wake handled a couple ways in other games. One is to put a particle emitter at the bow that throws water spray particles up and out from the bow. This effect would be easy to do and looks fairly decent. Another method that I used was that I created a 3D mesh of a bow wake and made it a child of the boat. As the boat's speed increased, I increased the height of the bow wake mesh. I then use UV animation to simulate water spray. The end result was pretty effective. I haven't tried this in Unity yet, but it shouldn't be too hard to accomplish. A third method might be animate the normal map somehow. Yoggy did this in his "damn nice water demo". And a fourth method might be to modify the ocean mesh somehow. I don't know if this is possible or not, but here's some pseudo code that might help if someone thinks it's possible this code is adapted from a project I'm working on in Carrara. This definitely won't work as is:

    Code (csharp):
    1. var p1 : float;  //  wave angle
    2. var p2 : float;  //  wave head
    3. var p3 : float;  //  wave height
    4.  
    5. y2=(y-0.75)*(y-0.75);
    6. ax=(y2+0.125)/(y2+0.125/16);
    7. x=(x*ax)/(1.2+p1);
    8. x2= (x*x)+(p2/2);
    9. r2=x2+y2;
    10. h=4*(x2-0.0625*y2);
    11. h=(h-r2)*(h-r2);
    12. dz=((1-exp(-8*y2))*(PI/3+atan(-16*(y-0.75)))*exp(-h)/PI)*(1+p3);
    The above code produces the mesh below in Carrara:
     

    Attached Files:

  30. bigkahuna

    bigkahuna

    Joined:
    Apr 30, 2006
    Posts:
    5,434
    The stern wake will be a bit more difficult as the surface it will lay on is dynamic and animated. If the water surface were flat, all that would be needed to do is to use a particle trail renderer streaming from the stern of the boat. If all you're using is the standard Unity water assets, then this will work fine. But on a moving ocean mesh, other methods will be required. Perhaps the ocean foam can be modified for this?
     
  31. RHD

    RHD

    Joined:
    Mar 30, 2009
    Posts:
    719
    Hum, much to ponder!
     
  32. bigkahuna

    bigkahuna

    Joined:
    Apr 30, 2006
    Posts:
    5,434
    Here's an in game photo from one game, "Virtual Sailor". In this game they use 3 separate effects: One is a particle emitter at the bow to simulate spray. Another is a bow wake (two trail renderers) that lay on the ocean surface and project at an angle from the bow of the boat. And the third is the stern wake that is a single trail renderer that is emitted from the stern.


    But by far the best ocean and boat wake simulations are in "Virtual Skipper". They have a number of videos and a free demo that you can view to give you some ideas:
    http://www.virtualskipper-game.com/en/#
     
  33. nikko

    nikko

    Joined:
    Mar 20, 2009
    Posts:
    436
    Yes I can, the video is quite destructive about the detail of the underwater. I use the same kind of code as the one posted on this forum.
    The main problem I had was that my Ocean is made to be very large, and the polygons close to the camera are flashing for some reason.
     
  34. bigkahuna

    bigkahuna

    Joined:
    Apr 30, 2006
    Posts:
    5,434
    Could it be vertex lighting of the terrain / bottom that is causing this?
     
  35. Necron33

    Necron33

    Joined:
    Oct 30, 2009
    Posts:
    33
    Well, the ocean requires 2.x PRO, but well, it won't work on 2.6 freeware edition, and when it does, it's quality is really BAD.
    i've had ten minutes with it so it doesn't require PRO Assets, had a good quality, if you like i will put it under the same license used in the original ocean 3d, with just a second copyright for mine, as mine is modified =].
    (i had to use my own texture, the texture included was BAD.)
    Images:
    on 2.6 free after fixing pro, but not improving quality:

    on 2.6 free after everything fixed:


    Thanks, forgive me if it wasn't that quality, i'm 13 and did that in 10 minutes, heh...
     
  36. bigkahuna

    bigkahuna

    Joined:
    Apr 30, 2006
    Posts:
    5,434
    So you modified the ocean script to work without Pro? Cool, sure I'd love to take a look. :)
     
  37. Necron33

    Necron33

    Joined:
    Oct 30, 2009
    Posts:
    33
    Exactly, but notice i did not modify the shader, just the script.
    take another look of another ten minutes work:
    (OFFTOPIC: I'd love to see vertical rendering of textures on unity3d, as leadwerks 3d game engine, and the slope property so textures appear only on slopes on the terrain.)
    [/quote]
     
  38. RHD

    RHD

    Joined:
    Mar 30, 2009
    Posts:
    719
    Me too. Well done Necron33! As I have Indie I'm on the very early version of the script which has gaps between the tiles of the ocean which I can't figure out how to fix.

    http://web.mac.com/hayesdavies/Portfolio/oceanbuild1.html
     

    Attached Files:

  39. Necron33

    Necron33

    Joined:
    Oct 30, 2009
    Posts:
    33
    Thanks RHD, it's so easy when working with .NET and JS, other than c++ which is a headache, thanks god unity technologies won't provide source codes, as T3D Does.
    I may export a 3 scenes web game providing oceans, just need to know how the GUI system works, hehe.
    Unity3D = Rapid prototyping, and Rapid dev!

    i would like to talk to anyone experienced with unity3d, please contact me on necron18@msn.com
    thank you!
    tommorow i will release the oceans after a little fix to foam and caustics.
     
  40. RHD

    RHD

    Joined:
    Mar 30, 2009
    Posts:
    719
    Might be if you are 13 with a talent for programming. Different story if you are Old and from the Art Side but I totally understand the excitement. I've been waiting for years for something like this.

    Go get 'em kid!

    Look forward to your Indie friendly ocean.
     
  41. Necron33

    Necron33

    Joined:
    Oct 30, 2009
    Posts:
    33
    I don't like 3d art, i didn't ever do it, hehe.
    it's just about getting my freehostia hosting up so i can put up the demos and the downloads so i release it.
    i've been waiting since 2 months for a free unity, well i can't afford 10 cents, can't get them to reach the internet, hehe.

    Enough offtopic!
     
  42. RHD

    RHD

    Joined:
    Mar 30, 2009
    Posts:
    719
    So, nothing happening then.
     
  43. Necron33

    Necron33

    Joined:
    Oct 30, 2009
    Posts:
    33
  44. Necron33

    Necron33

    Joined:
    Oct 30, 2009
    Posts:
    33
    So?....
     
  45. bigkahuna

    bigkahuna

    Joined:
    Apr 30, 2006
    Posts:
    5,434
    I gave your ocean sample a quick test last week, sorry I didn't post any comments before I've just been too busy. As I recall it seemed to work pretty well (MacBook Pro with an ATI X1600 card) but the near edge of the ocean was black (?). I haven't had much time to investigate what might be causing that.
     
  46. RHD

    RHD

    Joined:
    Mar 30, 2009
    Posts:
    719
    I got a whole Console full of errors and haven't had time to work through it.

    Thanks very much for posting it, and all your work. I'm sticking with the very much earlier version on the thread for now but will get back to your version very shortly as I really want the best Ocean I can get.

    Thanks.
     
  47. Mixality_KrankyBoy

    Mixality_KrankyBoy

    Joined:
    Mar 27, 2009
    Posts:
    737
    Anyone interested in doing a waterfall of equal quality? I have looked at the sewer demo (and currently using that technique) but it doesn't look good for a large waterfall.
     
  48. RHD

    RHD

    Joined:
    Mar 30, 2009
    Posts:
    719
    Ooh a waterfall!
     
  49. Mixality_KrankyBoy

    Mixality_KrankyBoy

    Joined:
    Mar 27, 2009
    Posts:
    737
    That's the spirit!
     
  50. RHD

    RHD

    Joined:
    Mar 30, 2009
    Posts:
    719
    Forest Johnson did some nice stuff with ripples but it was Pro only so I couldn't do much with it. I think the thread was called slightly worryingly "Sweet arse water", but it was a great effect.

    Not a waterfall though.