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. John-G

    John-G

    Joined:
    Mar 21, 2013
    Posts:
    1,122
    Best of luck with it, scrawks ocean sure looked good. Hopefully you pull it off, Unity sure needs a good ocean solution.
    Cheers for link, have seen the script before but didn't realise it worked with this ocean.
    Thanks will try it out.
     
  2. CaptainMurphy

    CaptainMurphy

    Joined:
    Jul 15, 2014
    Posts:
    746
    The only issue I had with scrawks ocean was that it couldn't be made larger. The waves stopped at around 1-2m tall and that was as big as you could make them. Other than that, it is a fantastic solution, plus with the later whitecap version I was getting something like 1200fps gpu render. Best I can get from my current iteration of this ocean is around 450-500.
     
  3. John-G

    John-G

    Joined:
    Mar 21, 2013
    Posts:
    1,122
    Now that is good performance, but for proper weather 2m waves would be low indeed.
    Hopefully it's possible to get them larger.
    Was there any issue with bouyancy and the whitecap version?
     
    Last edited: Oct 27, 2014
  4. CaptainMurphy

    CaptainMurphy

    Joined:
    Jul 15, 2014
    Posts:
    746
    Not that I know of, though I never really messed with that part of it. The whitecaps weren't exactly good looking though, they were closer to just a peak whitening and didn't actually stay as foam that persisted. It simple grabbed the height of the vertex and applied foam to it as far as I could tell.
     
  5. odival

    odival

    Joined:
    Jun 11, 2014
    Posts:
    57
    I'm having a hard time getting Trond's github version to work with my network project. I fixed the LOD references to L0D and that made the Ocean script work in Unity 4.5. However I have 2 problems:

    a) My project is a online multiplayer open-world game, using PUN. That means the player must be instantiated into the scene. To make some of his components work (mostly pertaining a skydome), the player has a 2 camera system, and the only way I could think off adding the ocean to him was like this:

    While this kind of works, the ocean is always placed a few hundred units below the player, no matter where I positioned the OceanGO or Ocean game objects. I wish the ocean stayed at y=160 (global pos). I tried using both
    Code (CSharp):
    1. myPlayerGO.transform.FindChild("OceanGO").transform.position = new Vector3(0, 160, 0);
    2.  
    3. //and
    4.  
    5. myPlayerGO.transform.FindChild("OceanGO").transform.localPosition = new Vector3(0, 160, 0);
    and that moved the OceanGo game object, but the Ocean itself remains below the ground, although the underwater effect worked properly (as in, it activates out of thin air when I'm under y=160). I select some of the ocean tiles and noticed that they were positioned at about y= -160, the very exactly oposite of what I set. Strangely enough it all works if I manually change the OceanGo or Ocean y position in the Editor during runtime...
    Any sugestions?

    b) For some reason, when I test the ocean in its demonstration scene (imported inside my project) it works flawlessly. But when I instantiate the ocean into my scene, some of its tiles do not allign properly, like in the image below:

    I only noticed this when using the ocean in my scene. Is it a known bug or am I doing something wrong? Any sugestions on how to fix it? Both my player cameras have the tag "Main Camera" (but they're positioned at the same spot), and the ocean is suposed to follow the camera with this tag. Could that be the problem?
     
    Last edited: Jan 9, 2015
  6. odival

    odival

    Joined:
    Jun 11, 2014
    Posts:
    57
    Help? Especially with first question.. =(
     
  7. skylem

    skylem

    Joined:
    Jul 17, 2013
    Posts:
    11
    The Ocean is always placed at the Zero point in your world i believe, make sure ur player starts out there too and you shouldn't have any problems. im not sure if theres any alternatives im just sharing what i've learned, EDIT-- Also note the follow player options on the Ocean, you could probably set this to something else if u need your player to spawn anywhere other then world zero.
     
    Last edited: Jan 12, 2015
  8. HeadHunter2009

    HeadHunter2009

    Joined:
    Jul 3, 2012
    Posts:
    50
    A bit progress with our mobile game Dark Waters, it work with 40 fps on my LG Optimus G. First level still in development.

    001.jpg 012_0001.jpg 013_0001.jpg load-01.jpg UITiF20a0GM.jpg
     
    John-G and CaptainMurphy like this.
  9. HeadHunter2009

    HeadHunter2009

    Joined:
    Jul 3, 2012
    Posts:
    50
    Screenshots with new low class ship at night.
    2.jpg 3.jpg 4.jpg 5.jpg
     
    judah4 and CaptainMurphy like this.
  10. CaptainMurphy

    CaptainMurphy

    Joined:
    Jul 15, 2014
    Posts:
    746
  11. odival

    odival

    Joined:
    Jun 11, 2014
    Posts:
    57
    @CaptainMurphy Hi, could you answer me a question about your version of the Community Ocean? The old versions used to have an "OceanReflectionRefraction" shader. In your version, there is no such shader. How are reflection/refraction handle in your version of COP_Ocean.cs?
     
  12. CaptainMurphy

    CaptainMurphy

    Joined:
    Jul 15, 2014
    Posts:
    746
    @odival My version uses a modified Water4 shader that utilizes an ocean reflection camera to create the reflection view.
     
  13. HeadHunter2009

    HeadHunter2009

    Joined:
    Jul 3, 2012
    Posts:
    50
    When we finish firs playable alpha version, i will post there as well.
     
  14. odival

    odival

    Joined:
    Jun 11, 2014
    Posts:
    57
    @CaptainMurphy thaks!
    @ToreTank I'm using your ocean script (the github one), and it seems that the ocean level is fixed at y=0. Where is this defined in the script? How could I change this so that we can define the sea level at wherever we want? I would really apreciate some insight into this. =(
     
  15. Alabatross

    Alabatross

    Joined:
    Dec 15, 2012
    Posts:
    223
    Would anyone happen to know why the ocean shader in this does not work correctly in linear lighting?
     
  16. creat327

    creat327

    Joined:
    Mar 19, 2009
    Posts:
    1,756
    Because the shader was never done for linear lighting. You can write it and submit to it if you want.
     
  17. HeadHunter2009

    HeadHunter2009

    Joined:
    Jul 3, 2012
    Posts:
    50
    Render of first small willage (not actual game)
    015.jpg 020.jpg 021.jpg 022.jpg
     
  18. Alabatross

    Alabatross

    Joined:
    Dec 15, 2012
    Posts:
    223
    I don't understand how you write a shader 'for' linear lighting. As far as I've known linear/gamma only effects color outputs. When I switch to linear it seems the lightDir or some other direction is completely different

    Fixed it, it had nothing to do with the shader it seems. The textures it relied on needed to bypass SRGB
     
    Last edited: Jan 23, 2015
  19. odival

    odival

    Joined:
    Jun 11, 2014
    Posts:
    57
    @CaptainMurphy sorry to bother you yet again, but I'm trying to wrap my head around your system to extend static tiles to the visible distance. Would you mind explaining to me (and showing the code bits) where the script defines how many static tiles are going to be created?
     
  20. CaptainMurphy

    CaptainMurphy

    Joined:
    Jul 15, 2014
    Posts:
    746
    in the COP_Ocean file from line 251 to 283.

    Code (csharp):
    1.  
    2. //make empty tiles that use a simplified mesh
    3. Mesh emptyMesh = new Mesh ();
    4. emptyMesh = CreateMesh(0.5f);
    5. //build static tiles only
    6. //take the current position, move to halfway between the end of the active tiles and the farclip
    7. //float _maxWidth = Camera.main.farClipPlane * 2;
    8. float _distanceToOffsetX = size.x * tiles;
    9. float _distanceToOffsetZ = size.z * tiles;
    10. float _sizeX = size.x * tiles;
    11. float _sizeZ = size.z * tiles;
    12. //build the tiles a certain number of spaces away
    13. int _staticTilesX = (int)((Camera.main.farClipPlane + (tiles * size.x)) / (tiles * size.x));
    14. int _staticTilesZ = (int)((Camera.main.farClipPlane + (tiles * size.z)) / (tiles * size.z));
    15. GameObject _staticTiles = new GameObject ("StaticTiles");
    16. _staticTiles.transform.parent = parentTile.transform;
    17. for (int sZ = -_staticTilesZ; sZ < _staticTilesZ; sZ++) {
    18. for (int sX = -_staticTilesX; sX < _staticTilesX; sX++) {
    19. if (!(sX == 0 && sZ == 0)) {
    20. BuildStaticTile (new Vector3 (_distanceToOffsetX * sX, 0, _distanceToOffsetZ * sZ), new Vector3 (_sizeX, 0, _sizeZ),Quaternion.Euler(0,0,0), emptyMesh, _staticTiles, material);
    21. }
    22. }
    23. }
    24. //build walls along the edge that will keep the user from seeing a gap in the ocean tiles
    25. float wH = Mathf.Sqrt (Mathf.Pow (20,2) / 2) / 2;
    26. float wX = Mathf.Floor (tiles * 0.5f) * size.x;
    27. float wZ = Mathf.Floor (tiles * 0.5f) * size.z;
    28. BuildStaticTile (new Vector3 (0,-wH + 0.001f, wZ - wH), new Vector3 (_sizeX, 0, 20),Quaternion.AngleAxis(-45, Vector3.right), emptyMesh, _staticTiles, material);
    29. BuildStaticTile (new Vector3 (0,-wH + 0.001f, -wZ + wH), new Vector3 (_sizeX, 0, 20),Quaternion.AngleAxis(45, Vector3.right), emptyMesh, _staticTiles, material);
    30. BuildStaticTile (new Vector3 (wX - wH,-wH + 0.001f, 0), new Vector3 (20, 0, _sizeZ),Quaternion.AngleAxis(45, Vector3.forward), emptyMesh, _staticTiles, material);
    31. BuildStaticTile (new Vector3 (-wX + wH,-wH + 0.001f, 0), new Vector3 (20, 0, _sizeZ),Quaternion.AngleAxis(-45, Vector3.forward), emptyMesh, _staticTiles, material);
    32.  
    edit: this is actually an old version, I have a newer one that I need to commit that cuts the number of tiles down substantially.
     
  21. odival

    odival

    Joined:
    Jun 11, 2014
    Posts:
    57
    Thanks. But can you point out to me what exactly defines the number os tiles to be made? I'm not a coder, but I'm trying to understand. For some reason in my test scene (not the project you provided) it always build only 3 static tiles, intead of the dozens we can see in your demo. So I'm trying to pinpoint what defines the numer of tiles, but I am at loss =(
     
  22. CaptainMurphy

    CaptainMurphy

    Joined:
    Jul 15, 2014
    Posts:
    746
    I was mistaken, that is the latest variation for that tile creation system.

    In short what it does is look at the main camera for the level (the one tagged MainCamera) and get the farClipPlane from it, then takes that distance and creates a count for x and y tiles by dividing the current tile size by the x/y of the active tiles.
    1. int _staticTilesX = (int)((Camera.main.farClipPlane + (tiles * size.x)) / (tiles * size.x));
    2. int _staticTilesZ = (int)((Camera.main.farClipPlane + (tiles * size.z)) / (tiles * size.z));
    From there what it does it count from 0 to the number of tiles in X and Y axis and create a static tile at that size up to the distance of the clipping plane by moving the tile center to the next space and continuing until it reaches the _staticTilesX/Y count.

    In short, the further your camera clip is, the more tiles it makes to reach it.
     
  23. odival

    odival

    Joined:
    Jun 11, 2014
    Posts:
    57
    Thanks for your time Captain! It's amazing how simple it seems after that explanation! =)
     
  24. odival

    odival

    Joined:
    Jun 11, 2014
    Posts:
    57
    So, before I start I just want to say that I'm not a programmer, nor do I understand shaders, so I'm sorry if the following script is a mess. =X

    I want to share with you guys my iteration of this community ocean script. Since I'm not a programmer, I couldn't create anything new, so I just compared several versions of the Ocean script (I believe I have almost all of them now :D) and tried to merge a few things from one another. Using Joaquin Grech's c# ocean as a stating point, I tried to reimplement ToreTank's undewater effects and Captain Murphy's static tiles system,sun position and getheights method. I'm also using the waves sfx from TwinFox's v3 demo - I guess its free to use. Its kind of a mess right now but it works. I can't give you realiabe numbers regarding fps because my GPU broke, thus I'm using integrated gpu wich sucks (I get about 20-30 fps). I made the terrain from scratch and it's free for using and modifying at will.

    There are still a few problems tough:
    - shader does not work with Dx11;
    - gaps between LODs are visible;
    - for some reason, if you "dive" to the depths of the terrain, the game starts to hang for a few secs, seems random (I actually believe the underwatertexture update hangs, not the game itself);
    - static tiles retain reflection from the ocean material, and it looks weird;
    - underwater refraction is not working exactly like in the original ToreTank's version, for some reason;
    - I'm not sure the fresnel thing is working at all;

    Since I can't code, I would really apreciate if you guys helped with those glitches. And I'll try to keep improving on it since its crucial to some of my projects.

    Here are the links for download, and below some screenies:
    unity package
    project folder


    * There's 2 bouyancy scripts, the Alexzzz version, and the one in the Hearts of Oak package. They both work so I chose the keep them both in this package.
    ** This script is not optmized for mobile, nor will it work properly with Unity Indie.
     
    Last edited: Jan 27, 2015
    CaptainMurphy likes this.
  25. HeadHunter2009

    HeadHunter2009

    Joined:
    Jul 3, 2012
    Posts:
    50
    Hi guys, need your help a little. I have a weird problem with reflection, for some reason closed objects render behind other objects in reflection, have anyone know why this happence? Becouse of this ship and islands reflections looks like messy.
    5.jpg 6.jpg
     
  26. HeadHunter2009

    HeadHunter2009

    Joined:
    Jul 3, 2012
    Posts:
    50
    I rewrited ocean reflection part, now it render layers in right order, also it render with fog and i even can render sky now, so you can see clouds reflection.
    7.jpg 8.jpg 9.jpg 10.jpg
     
    odival, CaptainMurphy and John-G like this.
  27. odival

    odival

    Joined:
    Jun 11, 2014
    Posts:
    57
    Whoa, thats very good @Head@Hunter. You just had to tweak the script or the shader? Could you share the update?
     
  28. HeadHunter2009

    HeadHunter2009

    Joined:
    Jul 3, 2012
    Posts:
    50
    I will share update in few days, with greatly improved editor look with dinamic ocean waves change and with nice water mist.
     
    odival likes this.
  29. HeadHunter2009

    HeadHunter2009

    Joined:
    Jul 3, 2012
    Posts:
    50
    Before uploading update decided to finish my editor extension for ocean project.
    OceanEditor.jpg
     
    Last edited: Jan 30, 2015
  30. odival

    odival

    Joined:
    Jun 11, 2014
    Posts:
    57
  31. John-G

    John-G

    Joined:
    Mar 21, 2013
    Posts:
    1,122
    Agreed very nice additions Head Hunter.
     
  32. HeadHunter2009

    HeadHunter2009

    Joined:
    Jul 3, 2012
    Posts:
    50
    Finished ocean generator extension, now it can be scaled.
    1.jpg
     
    CaptainMurphy, John-G and odival like this.
  33. HeadHunter2009

    HeadHunter2009

    Joined:
    Jul 3, 2012
    Posts:
    50
    Last edited: Feb 2, 2015
    RendCycle, KingLlama, Divinux and 9 others like this.
  34. elias_t

    elias_t

    Joined:
    Sep 17, 2010
    Posts:
    1,367
  35. DizzyTornado

    DizzyTornado

    Joined:
    Nov 17, 2013
    Posts:
    134
    Wow, I can't beleive ive only found this thread now!
     
  36. Axel59

    Axel59

    Joined:
    Feb 28, 2013
    Posts:
    37
    Great. Thank a lot.
     
  37. norby

    norby

    Joined:
    Jul 10, 2006
    Posts:
    277
    wow thank you very much
     
  38. ZJP

    ZJP

    Joined:
    Jan 22, 2010
    Posts:
    2,649
    @Head@Hunter nice move. :cool:
     
  39. schipman

    schipman

    Joined:
    Jan 22, 2010
    Posts:
    45
    That's an awesome way to give back @Head@Hunter - thanks so much
     
  40. Nsomnia

    Nsomnia

    Joined:
    Sep 26, 2014
    Posts:
    27
    @Head@Hunter Your giving your work away on what could be a $20+ asset? Wow thanks this will help my project immensely. Thank you very much. Will do the same any decent updates I come up with will be shared.
     
    HeadHunter2009 likes this.
  41. firestorm713q

    firestorm713q

    Joined:
    Jan 6, 2013
    Posts:
    16
    So I finally decided to make my own version of the shader, but I can't seem to get it working.

    For some reason, the waves seem to be updating too quickly? I can't really tell. I can't seem to slow down the changes in any way, shape, or form. I can exaggerate them, but they still move too fast to diagnose what's going wrong.

    The github project is here: https://github.com/firestorm713/ShaderExperiments/
    There's a lot there, I wanted to load the whole thing into a height map so you could arbitrarily define plane sizes and texture sizes, I also wanted to play around with setting the transparency and color based on the height and camera angle. I also calculate the Normal map manually. All of these functions work just fine.

    Somewhere in the wave functions something is going wrong, and I am at a total loss as to what. Can anyone take a look?
     
  42. HeadHunter2009

    HeadHunter2009

    Joined:
    Jul 3, 2012
    Posts:
    50
    Dark waters project started when i first saw this thread and was able to run this ocean on mobile. So i wanted to share my progress with you guys. Thank's.
     
    ZJP likes this.
  43. odival

    odival

    Joined:
    Jun 11, 2014
    Posts:
    57
    @Head@Hunter that's amazing! Thanks for sharing with us! :D
    I have one question tough: For some reason the ocean never seems to center with the main camera. Even in the scene provided in the package (in an empty project), the watertiles always seems to position thenselves in such a way, as to place the camera in the northeast corner of the central tile, no matter how many tiles I set. Like the picture below:


    Is this normal? Isn't there a way to tweak this, so that the camera is placed at the center of the central watertile?
     
  44. HeadHunter2009

    HeadHunter2009

    Joined:
    Jul 3, 2012
    Posts:
    50
    OceanEditor.jpg
    When you select ocean Object in scene you see Target/Player field, in my scene i assigned there floating Barrel, so ocean follow this barrel. If you want ocean follow your camera just assign it there and make sure Follow toggle is on.
     
  45. odival

    odival

    Joined:
    Jun 11, 2014
    Posts:
    57
    Thanks @Head@Hunter. I tried this but the followed object stays at the northeast corner of the central tile. To be honest the script still follows the main camera regardless of the game object selected in Target/Player. The code block regarding this seems to search the camera with the main.camera tag and follows it. It can be easily changed, but somehow there's always a little offset as in the camera in not properly centered around the ocean, because of this the player can easily see the gaps between tiles.
    My first guess (based on previous experiences with the ealier iterations of this ocean) was that this is due to the values of tiles and tiles LOD, but in this version of the ocean, tweaking doesn't fix the centralization. =|

    Below another image showing the offset better:
     
    Last edited: Feb 10, 2015
  46. HeadHunter2009

    HeadHunter2009

    Joined:
    Jul 3, 2012
    Posts:
    50
    There was several Ocean follow implementation, open Ocean.cs and search for line 490, and replace with this code:
    Code (CSharp):
    1. if (followMainCamera && player != null) {
    2.  
    3.             Vector3 centerOffset;
    4.             centerOffset.x = Mathf.Floor(player.position.x * sizeInv.x) *  size.x;
    5.             centerOffset.z = Mathf.Floor(player.position.z * sizeInv.y) *  size.z;
    6.             centerOffset.y = transform.position.y;
    7.  
    8.             if(transform.position != centerOffset)
    9.                 transform.position = centerOffset;
    10.  
    11.         }
    I updated OceanMobile package, it should now be centered for all settings.
    http://www.mediafire.com/download/1gfs8kzl6ydpevp/OceanMobile_1.1.unitypackage
     
    Last edited: Feb 10, 2015
    odival likes this.
  47. Nsomnia

    Nsomnia

    Joined:
    Sep 26, 2014
    Posts:
    27
    I like it I was playing around with it today. Now to figure out how to get seamless underwater views as soon as a wave passes the camera whoo boy that'll be fun.
     
  48. odival

    odival

    Joined:
    Jun 11, 2014
    Posts:
    57
    Last edited: Feb 11, 2015
  49. Nsomnia

    Nsomnia

    Joined:
    Sep 26, 2014
    Posts:
    27
    I didnt even see that. I can program so I'll have a look at his stuff and see what I can come up with.
     
    odival likes this.
  50. Dheen Doha

    Dheen Doha

    Joined:
    Jan 1, 2015
    Posts:
    10
    Great!! Thanks for sharing such a useful asset for free!!...... 1 Million *s for u!! :)