Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Wanted: Ocean shader

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

  1. bigkahuna

    bigkahuna

    Joined:
    Apr 30, 2006
    Posts:
    5,435
    If I recall correctly, I believe Nikko posted an iPhone version of the shader here.
     
  2. VIC20

    VIC20

    Joined:
    Jan 19, 2008
    Posts:
    2,445
    http://forum.unity3d.com/viewtopic.php?t=16378
     
  3. tbarbe

    tbarbe

    Joined:
    Dec 22, 2008
    Posts:
    154
    very cool...

    Thanks to you... and Thanks to Nikko too!

    :)
     
  4. tbarbe

    tbarbe

    Joined:
    Dec 22, 2008
    Posts:
    154
    hi

    I read thru every page of this thread... all 24 pages!

    It looks like Nikko started to work on an iPhone version... and I followed that link provided in previous comment reply ( thanks ) theres lots of info and a few nice demos that people show videos of... but sadly no example projects for the iphone.

    ;) secret sauce?
     
  5. bigkahuna

    bigkahuna

    Joined:
    Apr 30, 2006
    Posts:
    5,435
    I don't know if anyone else here has tried this yet, but Unity 3.0 beta breaks at least one of the shaders in this project that were working in 2.6.1. I've contacted Unity Tech who I'm hoping will help identify the fix as I haven't a clue how to fix it. :cry:
     
  6. Novodantis

    Novodantis

    Joined:
    Nov 12, 2009
    Posts:
    93
    Great demo Eki. Already one of the more satisfying renditions of the sea I've seen done in a virtual environment; despite being work in progress with a few glitches occasionally.

    I love that little shark sub too...
     
  7. Vimalakirti

    Vimalakirti

    Joined:
    Oct 12, 2009
    Posts:
    755
    Hey everyone,

    Looks like some great, great work happening here!

    I've been paging back through this thread for over an hour now and I'm not able to find a version that is working on Unity Free here. There's a lot of old broken links, and there's a version giving me that "ImageEffects does not exist in the current context." error that I don't know if can be fixed with free version.

    I am less interested in the shaders for my project. The parts I am looking for are the script that creates and deforms the mesh of large, swelling waves, and the ability to have objects 'float' on that surface.

    Any help in finding a link or advice on how to use certain parts of the scripts would be welcome.

    Thank you!
     
  8. mojojojo

    mojojojo

    Joined:
    Dec 16, 2009
    Posts:
    138
  9. mojojojo

    mojojojo

    Joined:
    Dec 16, 2009
    Posts:
    138
    Can anyone explain exactly what an "ocean" type is? I've tried attaching the script to an empty component and it doesn't work. The only way I can get it to work is to open the original file and strip away everything else I don't want. I'm hoping to learn how to modify it and create a prefab but I can't seem to get it to work as a seperate component.
     
  10. Vimalakirti

    Vimalakirti

    Joined:
    Oct 12, 2009
    Posts:
    755
    Ungoliath:

    Thanks for that link! It's got me moving in the right direction. However, I can't get my boyant object to work right. It's bobbing up and down a little and tilting some, but not moving enough to follow the surface of the water. There was this post back on page 16 I think:

    But that didn't do anything. I'm probably not using the right boyancy script, because my script doesn't call that function at all. Here's the one I'm using:

    Code (csharp):
    1.  
    2. var ocean : Ocean;
    3. // Water plane at y = 0
    4. private var mag = 1.0;
    5.  
    6. private var ypos = 0.0;
    7. private var blobs : Array;
    8. private var ax = 2.0;
    9. private var ay = 2.0;
    10.  
    11. private var dampCoeff = .2;
    12.  
    13. private var engine = false;
    14.  
    15. function Start () {
    16.    
    17. //  mag = rigidbody.mass / (ax * ay) * 10;
    18.     rigidbody.centerOfMass = Vector3 (0.0, -0.5, 0.0);
    19.    
    20.     var bounds = GetComponent("MeshCollider").mesh.bounds.size;
    21.     var length = bounds.z;
    22.     var width = bounds.x;
    23.  
    24.     blobs = new Array();
    25.     blobs.length = ax * ay;
    26.     var i = 0;
    27.     xstep = 1.0 / (ax-1);
    28.     ystep = 1.0 / (ay-1);
    29.    
    30.     var point;
    31.     var velocity;
    32.     for (x=0;x<ax;x++){
    33.         for (y=0;y<ay;y++){    
    34.             blobs[i] = Vector3 ((-0.5+x*xstep)*width, 0.0, (-0.5+y*ystep)*length) + Vector3.up*ypos;
    35.             i++;
    36.         }      
    37.     }
    38. }
    39.  
    40. function Update (){
    41. //  if (Input.GetButton ("Fire1")){
    42. //      engine = !engine;
    43.         //blobs[0] = null;
    44. //  }
    45. }
    46.  
    47. function FixedUpdate () {
    48.     for (i=0; i<blobs.length;i++) {
    49.         var blob = blobs[i];
    50.         if (blob != null) {
    51.         wpos = transform.TransformPoint (blob);
    52.         damp = rigidbody.GetPointVelocity(wpos).y;
    53.         if (ocean)
    54.             rigidbody.AddForceAtPosition (-Vector3.up * (mag * (wpos.y - ocean.GetWaterHeightAtLocation (wpos.x, wpos.z)) + dampCoeff*damp) , wpos);       
    55.         else
    56.             rigidbody.AddForceAtPosition (-Vector3.up * (mag * (wpos.y ) + dampCoeff*damp) , wpos);    
    57.        
    58.     }
    59.     if (engine)
    60.         rigidbody.AddForceAtPosition (transform.forward*40.0, transform.TransformPoint (Vector3 (0.0, -1.0, -7.5)));       
    61.    
    62. }
    63.  
    64. }
    Does anyone have a correct boyancy script they can post?

    Thanks!
     
  11. Vimalakirti

    Vimalakirti

    Joined:
    Oct 12, 2009
    Posts:
    755
    Concerning boyancy, I found this at the bottom of page 11:


    I found that not having pro DOES matter so I'm still using the Unity Package from Ungoliath's link, but here is Coalguru's Boyancy Modified.js script:

    Code (csharp):
    1.  
    2. var ocean : Ocean;
    3. // Water plane at y = 0
    4. private var mag;
    5.  
    6. private var ypos = 0.0;
    7. private var blobs : Array;
    8. private var ax = 4.0;
    9. private var ay = 4.0;
    10.  
    11. var dampCoeff = 0.2;
    12.  
    13. var engine = false;
    14.  
    15. function Start () {
    16.    
    17.     mag = rigidbody.mass / (ax * ay) * 100;
    18.     rigidbody.centerOfMass = Vector3 (0.0, -0.5, 0.0);
    19.    
    20.     var bounds = GetComponent("MeshCollider").mesh.bounds.size;
    21.     var length = bounds.z;
    22.     var width = bounds.x;
    23.  
    24.     blobs = new Array();
    25.     blobs.length = ax * ay;
    26.  
    27.     var i = 0;
    28.     xstep = 1.0 / (ax - 1);
    29.     ystep = 1.0 / (ay - 1);
    30.    
    31.     for (x = 0; x < ax; x++){
    32.         for (y = 0; y < ay; y++){      
    33.             blobs[i] = Vector3 ((-0.5 + x * xstep) * width, 0.0,
    34.                 (-0.5 + y * ystep) * length) + Vector3.up * ypos;
    35.             i++;
    36.         }      
    37.     }
    38. }
    39.  
    40. function Update (){
    41.  
    42. }
    43.  
    44. function FixedUpdate () {
    45.     for (i = 0; i < blobs.length; i++) {
    46.         var blob = blobs[i];
    47.         if (blob != null) {
    48.             wpos = transform.TransformPoint(blob);
    49.             damp = rigidbody.GetPointVelocity(wpos).y;
    50.            
    51.         var waterHeight = ocean.GetWaterHeightAtLocation (wpos.x, wpos.z) * 10 * ocean.scale;
    52.        
    53.         if (ocean )
    54.             rigidbody.AddForceAtPosition (-Vector3.up * (mag * (wpos.y - waterHeight) + dampCoeff * damp) , wpos);     
    55.         else
    56.             rigidbody.AddForceAtPosition (-Vector3.up * (mag * (wpos.y ) + dampCoeff*damp) , wpos);    
    57.  
    58.     }
    59.     if (engine)
    60.         rigidbody.AddForceAtPosition (transform.forward*40.0, transform.TransformPoint (Vector3 (0.0, -1.0, -7.5)));       
    61.    
    62. }
    63.  
    64. }
    It's still lagging behind the wave height so that it looks almost opposite of the waves. I'm trying to tweak the different variables to make it work better with not much progress, but still headed in the right direction. Thank you Coalguru.

    Notice that he has an option for a motor to be put on that! This could be useful!

    So, we now have the links on this one page to the working project and boyancy for Unity free.

    Thanks again to all the hard working souls here especially Big Kahuna! Aloha!
     
  12. BenAllen

    BenAllen

    Joined:
    Sep 11, 2010
    Posts:
    13
    This is a great looking project. Well done guys.

    However, for some reason, when I import the prefab the ocean is always offset in relation to my terrain.
    I can never get the two to meet. Is there something in the ocean script that I can alter. I can see the ocean far away from the edge of my map. Is anyone able to help please?

    I've attached a screen shot.
     

    Attached Files:

  13. Vimalakirti

    Vimalakirti

    Joined:
    Oct 12, 2009
    Posts:
    755
    Ben,

    This might not be the answer you are looking for, but wouldn't it look sharper to lower the edge of your terrain down so that the land continues down under the ocean like a real beach?

    Otherwise it will be difficult to match up the positions of the ocean and terrain exactly, no matter what. But you are adjusting the Y value of the position of the Ocean object to change its height?
     
  14. BenAllen

    BenAllen

    Joined:
    Sep 11, 2010
    Posts:
    13
    What I mean is. I want to set the ocean at the same level as my water. No matter if I align the prefab to my terrain, it always offsets somewhere else.
     
  15. hessrid

    hessrid

    Joined:
    Sep 20, 2010
    Posts:
    2
    Hello, everyone

    I am currently working on a little project that has to do with rendering an underwater scene and everything shown in this thread is fantastic and totally up my alley. It is going to save me a little of time and thank you all for making your hard work available to everyone else.

    However, I have a little problem. The thing that is critical to my application is displaying the "semi-submerged" state as best as possible. The user ToreTank seems to have done an amazing shader just for that (judging by the example by Yann). Things is - the files are no longer available and I can't seem to find them anywhere. If that mod for the Ocean project is still freely available, could someone that still has it repost it here, or on the wiki, or PM me about it. I would be extremely greatful.

    Also, the scene I'm preparing will need to have as many other effects as possible, so I'm thinking about trying to implement godrays and possibly some particle effects. Naturally, if what I do is good enough (or even if it isn't) I'd be glad to share it with everyone.

    Hope I'm not demanding too much as a first time poster and thank you all in advance.

    Cheers
    Alex
     
  16. bigkahuna

    bigkahuna

    Joined:
    Apr 30, 2006
    Posts:
    5,435
    @hessrid - Do you have 3.0 yet? I've started working on converting the version I have to 3.0 and could use some assistance. BTW, 3.0 has its own god-rays although I haven't tried them with this yet.
     
  17. immFX

    immFX

    Joined:
    Mar 20, 2010
    Posts:
    109
    Kudos to you sir!
     
  18. hessrid

    hessrid

    Joined:
    Sep 20, 2010
    Posts:
    2
    Hi,

    No, still 2.6. I did see that 3.0 is going to have its own godrays though, which is really good news but I'll try and implement a solution for 2.6 anyway (good way to learn, if anything else).

    I still want to try a some other things (mainly, some simple "plankton" like particle-effects) and see if I can take it further with a few more complicated ideas later on.

    My original question stands, btw (sorry for being a nag) - does anyone have ToreTank's "semi-submerged" code. Maybe ToreTank himself(I'm not too sure if he's been active recently)?

    Thanks a lot for the quick response and as immFX already said - kudos to you for the 3.0 version.
     
  19. bigkahuna

    bigkahuna

    Joined:
    Apr 30, 2006
    Posts:
    5,435
    Don't thank me yet, it's not working and I'm going to need help to get it to work.
     
  20. jeanRicK

    jeanRicK

    Joined:
    Sep 23, 2010
    Posts:
    4
    Hey. I have a quick question: When using the ocean object is there a way to set the position it spawns at? Or is it necessary to reposition everything else in the scene around it?
     
  21. ratamorph

    ratamorph

    Joined:
    Sep 2, 2007
    Posts:
    458
    I was doing some testing to get an object to follow the height of the waves (I'm not talking about the buoyancy script). Just testing the values returned by GetWaterHeightAtLocation() and noticed this function doesn't actually return the height of the wave at position x,y. It rather returns a height that's at an index in the range of (0-width and 0- height). So basically it only transforms the x and y values so they relate to an index that's in the bounds of the array but not the actual index you are standing on top of or close to.

    This may look ok for big objects if you just want to simulate basic buoyant behavior, but not as well for small objects that are expected to match the waves like a small boat or a jetski.

    I'm working on something very similar to waverace and could use a way to get the actual height of the wave under my jetski so I can get the waves integrated into the gameplay.

    If anyone wants to test this, the easiest way I found was to make a script that moves an object around x,z and constantly returns me on a GUI on the screen the index of the position on the data array and the height stored at this position. Deactivating the ocean script after the game is running will pause the waves making it easier to do this kind of tests.

    Was this a known fact about the ocean script? How can we make it so it returns the actual height at x,y?

    Sorry if this was already stated at some other page, but this thread has become too large to easily browse.
     
    Last edited: Sep 27, 2010
  22. ratamorph

    ratamorph

    Joined:
    Sep 2, 2007
    Posts:
    458
    Did you manage to port the script to unity 3 already?

    I already did, the main issue I found was that the new implementation of the javascript compiler doesn't support negative indexes on arrays.
    The ocean script code has a lot of those.

    Basically if you have this

    array[-1]

    means 1 element from the end of my array backwards.

    I just made a function to do this index readjustment and used it where I saw errors, not sure if it's the best or most optimized way to do this but it works.

    If you want me to post my script let me know, I'm just not sure if I have the latest and greatest version of the original script.
     
    Last edited: Sep 27, 2010
  23. bigkahuna

    bigkahuna

    Joined:
    Apr 30, 2006
    Posts:
    5,435
    No, I haven't had any time to work on the ocean shader for 3.0, in fact, I was beginning to think it might not be possible so I'd very much like to see what you've done. What I have is an assembly of "pieces" from various ocean projects that sort of works with a few issues (like if antialiasing is turned on the screen is flipped upside down).
     
  24. ratamorph

    ratamorph

    Joined:
    Sep 2, 2007
    Posts:
    458
    Here's my unity 3.0 script.

    Like I said I don't think I got the latest version, for instance I don't have ToreTank's underwater effects. If you manage to merge it with all the other stuff please post that back.

    If you or anyone happen to have Toretank's underwater effects please share it, all links seem to be dead and I could really use that.
     

    Attached Files:

  25. bigkahuna

    bigkahuna

    Joined:
    Apr 30, 2006
    Posts:
    5,435
    PM sent.
     
  26. ratamorph

    ratamorph

    Joined:
    Sep 2, 2007
    Posts:
    458
    Thanks for the files bigK, I got it working.

    Seems like the issues I runned into when first porting the script have disappeared, probably due to the beta nature of Unity3 when I did my port.

    I found some issues however when building webplayer builds of the ocean, the shaders seem to be broken for webplayer builds on 3.0 so I sent a bug report.

    Also the water post effect used for the underwater effect is causing problems on 3.0, I'll try to make sense of what's happening there.
     
    Last edited: Sep 28, 2010
  27. bigkahuna

    bigkahuna

    Joined:
    Apr 30, 2006
    Posts:
    5,435
    Good job! Could you post your updated files? I'd like to give it a go. Thanks!
     
  28. ratamorph

    ratamorph

    Joined:
    Sep 2, 2007
    Posts:
    458
    I'm getting some very strange artifacts on the reflection when my camera gets close to the surface of the ocean. Anyone that's good with shaders have any clue what's wrong here?

    I'm posting a couple of images showing the artifacts and the shader I'm using for the ocean.
     

    Attached Files:

  29. Nikolay116

    Nikolay116

    Joined:
    Mar 21, 2010
    Posts:
    421
    Could you please update the first post with some info what is done and not done for the project? And maybe add download links?
     
  30. bigkahuna

    bigkahuna

    Joined:
    Apr 30, 2006
    Posts:
    5,435
    @ratamorph - I've gone back to my original shader for the reasons we discussed earlier today. I'm not seeing the issues you've posted on Windows, but I do get the "upside-down" issue when using AA and some weirdness as the camera transitions from above to below the water. The improvements you made look super, just wish I could get it to work in a Windows stand alone.

    @Nikolay - I haven't really done much more than collect the bits that have been posted and assembled them. What I really need to do is to find someone to help fix some of the remaining issues (perhaps Unity Tech will assist?). In its current state it's only barely usable.
     
  31. Saker

    Saker

    Joined:
    Sep 28, 2010
    Posts:
    2
    Can you please put the link here ?! :)
     
  32. bigkahuna

    bigkahuna

    Joined:
    Apr 30, 2006
    Posts:
    5,435
    Don't the links in the wiki work anymore? That's the version I have.
     
  33. Saker

    Saker

    Joined:
    Sep 28, 2010
    Posts:
    2
    Which wiki ?! Unify ?! .... I was talking about Toretank's underwater effects.

    Thanks.
     
  34. bigkahuna

    bigkahuna

    Joined:
    Apr 30, 2006
    Posts:
    5,435
  35. ykk

    ykk

    Joined:
    Dec 8, 2009
    Posts:
    51
    Ocean shader can be support in Unity 3? I had this error " IndexOutOfRangeException: Array index is out of range.
    Ocean.Update () (at Assets/Ocean_v3/Ocean.js:647) "

    Anyone Help..
     
  36. ratamorph

    ratamorph

    Joined:
    Sep 2, 2007
    Posts:
    458
    The version on the wiki works.

    Make sure you are not using the Unity3 beta, I got those errors on the beta.
     
  37. creat327

    creat327

    Joined:
    Mar 19, 2009
    Posts:
    1,224
    any chance we can get this to work on iphone/android? at least a very simplistic version... it would be great
     
  38. ykk

    ykk

    Joined:
    Dec 8, 2009
    Posts:
    51
    How can i make buoyancy object on this Ocean?
     
  39. Nikolay116

    Nikolay116

    Joined:
    Mar 21, 2010
    Posts:
    421
    Could anyone tell me why there are these seems? What's the purpose? is it some bug just I have or not? I would call them displacements, because each LOD layer is slightly off from the previous one. Looks like it was done on purpose. If so, why? It is not visible from the camera unless you are high enough. I am making a flight sim.

     
    Last edited: Oct 2, 2010
  40. bigkahuna

    bigkahuna

    Joined:
    Apr 30, 2006
    Posts:
    5,435
    AFAIK, what you're seeing is a side effect of the LOD system. That's one of the limitations of the current ocean shader. If you keep the camera low the seams aren't really noticeable. I think unless you can fix that you're going to have a hard time using it for a flight sim.

    Incidentally, I noticed you're running this under Windows. Are you seeing the "upside down" issue when AA is turned on? I am and so far am not able to find a fix for it.
     
    Last edited: Oct 2, 2010
  41. Nikolay116

    Nikolay116

    Joined:
    Mar 21, 2010
    Posts:
    421
    Thanks for the support! :0

    I am not sure what is up side down issue. I think I did not use AA so far. I will tell you if I see other bugs.

    Again, thanks! In any case it is a nice effort!
     
  42. Prodigalpops

    Prodigalpops

    Joined:
    May 5, 2010
    Posts:
    86
    Hi all. I've been following this thread for a while and have benefitted greatly from the comments etc. Consequently I felt it was time to put back a bit of assistance into the community...

    I've attached a copy of the Ocean.cs file I currently use. It is considerably tweaked from the original - not the least of which is that I converted it to c#.

    It "stitches" the ocean surface at the LOD joins so there is no "step down" at each LOD change.
    The GetWaterHeightAtLocation code has been changed as well to help with buoyancy (which I do with springs).


    I don't guarantee it will work for you as it's obviously coded to work for my project but should give you a start. To give you an idea have a look at a very basic web player build (based on code about 3 weeks ago via U3 Pro beta7) at http://www.simulator-trading.com/WebPlayer/WebPlayer.html

    BTW - This also merges the skydome package from pixelstudio. Try pressing Tab and Backspace....

    I hope it helps...
     

    Attached Files:

  43. Nikolay116

    Nikolay116

    Joined:
    Mar 21, 2010
    Posts:
    421
    Could you please help noobs like me and insert the script into some example project. Like what already exists here as oceam shader 3.4
     
  44. Prodigalpops

    Prodigalpops

    Joined:
    May 5, 2010
    Posts:
    86
    To be honest Nikolay116, I'm of the opinon that noobs learn better through trying and experimentation than being given a working scene (which you obviously already have from the screen shots above). You should attempt to compare the code I've provided with the current ocean code to see where things are different and try and understand what it's doing. Then ask questions about what it's doing if it makes no sense. Only then will you learn and understand.
    HOWEVER - as I don't want to be seen as a grumpy old git I'll spend some of MY development time putting one together and will post it when done.
     
  45. Prodigalpops

    Prodigalpops

    Joined:
    May 5, 2010
    Posts:
    86
    Ok - tried to upload a full package but it wouldn't accept it so ...

    Create a new project with standard assets and import the current ocean package.

    Save the Ocean.cs and Environment.cs files in the Ocean folder.

    Unzip the scene file and open it in your project.

    You should have in your scene...
    Environment Game Object with associated Environment script.
    Main Camera
    Ocean Game Object with associated Ocean script.
    Sun directional light
     

    Attached Files:

  46. bigkahuna

    bigkahuna

    Joined:
    Apr 30, 2006
    Posts:
    5,435
    @Prodigalpops - So your version just uses the 2.6.1 Standard Assets and not the Pro Standard Assets? So no refraction or image effects then. I gave it a quick try in a project I have which uses the ocean and it threw up all sorts of errors, which make me suspect you and I are using different ocean packages.
     
  47. Prodigalpops

    Prodigalpops

    Joined:
    May 5, 2010
    Posts:
    86
    The point of posting the code was to address the issue of the gaps in the LOD. I am no doubt using a different version but it was converted from 2.6 through various V3 betas. I've had to modify the shader due to the arithmetic limit and there are various other changes I've made hence the limited project I provided. Expecting it to work with the current package is likley to produce the errors you see.
    For reference my code currently does this....
    http://www.simulator-trading.com/TestWeb2/WebPlayer/WebPlayer.html
     
  48. bigkahuna

    bigkahuna

    Joined:
    Apr 30, 2006
    Posts:
    5,435
    I'll give it another try starting with a new project this time. It looks quite good but I'm seeing some artifacts at the seams when the camera is at certain angles (see attachment). I've only looked at it on my Mac so far. I'll give it another go later this afternoon. I'm real curious to see if your version has the "upside down" issue like the one I have.
     

    Attached Files:

  49. Prodigalpops

    Prodigalpops

    Joined:
    May 5, 2010
    Posts:
    86
    Hmm - curious artifacts there. I don't see them on my Windows machine, either in Unity, via a standalone or the web player.
    I also tried my MacBook Pro (2010 machine) and my sons Windows 7 machine. All different graphics cards and all worked.

    I've also downloaded the oceanV3.2 from the wiki, removed the glow and contrast stretch and modified it to use my ocean.cs file with no (major) failures.

    BTW - just been playing with AA from none to 16x and saw no difference in the ocean (but the edges improved as expected quite nicely). This was on Windows (32 bit Vista twin Geforce Go 7950 GTX via SLI).
    I'll have a check on my MacBook tomorrow - now it's time for the pub.
     
    Last edited: Oct 2, 2010
  50. Tinus

    Tinus

    Joined:
    Apr 6, 2009
    Posts:
    435
    @bigkahuna Know anything more about the AA upside-down issue? I'm on windows, and I'm having this problem with a lot of shaders. It started with Multivac's motion blur shaders (thread), but now I'm experiencing it with Unity3's DoF shader as well.

    Just thought I'd throw that information in so we can get a more all-around picture of this wacky bug.
     
unityunity