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. VIC20

    VIC20

    Joined:
    Jan 19, 2008
    Posts:
    2,687
    Usually a mac mini is all you need for development.

    So far I always wrote It possibly comes to android later. But this depends on how things turn out… something important changed some days ago that could turn out to have an extreme influence on my strategy. Maybe it won't be clever to support other mobile platforms than iOS then.

    The whole android platform is too fragmented anyway, much harder to optimize the shaders for the different GPUs and all those different devices and screen resolutions are a nightmare.

    enough thread hijacking… my work isn't based on TwinFox's approach.
     
  2. skeptika

    skeptika

    Joined:
    Dec 31, 2011
    Posts:
    108
    Did anyone ever make a decent transparent version of this shader or involving some basic distortion/caustics?
     
  3. lazygunn

    lazygunn

    Joined:
    Jul 24, 2011
    Posts:
    2,749
    I think the optimised for mobile version has transparency and refraction, you change the contribution of reflection and refraction by its colour, reflection is obviously surface reflection colour and refraction the 'murkiness'

    Its hard to keep track of all of the releases but yes i think that was the last one that i used that i found most effective. Ill check when im next at my desktop anyways
     
  4. skeptika

    skeptika

    Joined:
    Dec 31, 2011
    Posts:
    108
    I'm using the one from git atm, and I don't see any transparency, but perhaps I'm blind :)
     
  5. lazygunn

    lazygunn

    Joined:
    Jul 24, 2011
    Posts:
    2,749
    As far as i reember there are at least 2 on git, i think?

    Looking through the thread should give several versions
     
  6. VIC20

    VIC20

    Joined:
    Jan 19, 2008
    Posts:
    2,687
    OK, just a last one in HD quality

     
  7. digifanter

    digifanter

    Joined:
    Oct 26, 2011
    Posts:
    7
    hello, how i can seperate the disortion effect from the rest of the ocean shader. i only need a script that makes that nice underwater disortion. can anybody help me?
     
  8. alexzzzz

    alexzzzz

    Joined:
    Nov 20, 2010
    Posts:
    1,447
    What is the latest "official" version of the community ocean script? The last time I used it was two years ago. I want to verify a performance improvement idea but don't know what version to take.
     
  9. creat327

    creat327

    Joined:
    Mar 19, 2009
    Posts:
    1,756
    here: And the public git for anyone willing to download and contribute is at https://github.com/joaquingrech/mobileocean
     
  10. Reanimate_L

    Reanimate_L

    Joined:
    Oct 10, 2009
    Posts:
    2,788
    Holy crap i completely forgot this thread...o_O
    *just bumping :p
     
  11. lazygunn

    lazygunn

    Joined:
    Jul 24, 2011
    Posts:
    2,749
    I'm quite sure this is the one with transparency, or, transparency via a relative 'murkiness' of the body of the water controlled by its reflectivity and refraction. The darker the reflective colour (and possibly the alpha) the less reflections allowing you to see into the water, the depth of which is controlled with the refraction colour. This is how the one I used worked anyways and im sure its the mobile effort, ive a video of a fun thing i did with a mate for visuals at a ska gig running it



    It's in the 4:3 aspect of the projector we used originally for the full experience. You can see as you get closer that the community shader iteration there is getting very solid
     
  12. nerf zone

    nerf zone

    Joined:
    Jun 6, 2013
    Posts:
    6
  13. ZJP

    ZJP

    Joined:
    Jan 22, 2010
    Posts:
    2,649
    Bump.

    Has anybody has improved the tool?
     
  14. Reanimate_L

    Reanimate_L

    Joined:
    Oct 10, 2009
    Posts:
    2,788
    I doubt that :/
     
  15. DIFTOW

    DIFTOW

    Joined:
    Aug 22, 2012
    Posts:
    53
    Is a community shader still going?
     
  16. CaptainMurphy

    CaptainMurphy

    Joined:
    Jul 15, 2014
    Posts:
    746
    We have started to use this in our project and are starting with one of the last mobile versions we could find. Does anyone have a later edition than a couple years back? The Nikko versions can't seem to be found anywhere and we were hoping to make a detailed version for our game.
     
  17. CaptainMurphy

    CaptainMurphy

    Joined:
    Jul 15, 2014
    Posts:
    746
    We have forged ahead with one of the optimized mobile versions and have melded it with the water from the Pro assets to get a very nice looking bit of water. We are also adding some changes to the foam to give it some more randomness by using two noise images and two separate layers of foam that keeps the foam from looking repetitive too much.


    Reflections are looking good with the distortion. This is a Water4 effect that was integrated.




    Lower angle shot to show the fresnel a bit


    Shoreline and object foam using depth texture


    Showing some of the specular effect. It matches the color of the sun light source.


    With sea foam added back in.
     
    FargleBargle and John-G like this.
  18. creat327

    creat327

    Joined:
    Mar 19, 2009
    Posts:
    1,756
    Mind sharing the changes? :)
    Also a YouTube would be nice
     
  19. CaptainMurphy

    CaptainMurphy

    Joined:
    Jul 15, 2014
    Posts:
    746
    The code needs a major cleanup. Once all of the effects are ironed out we will release the package, but as it is right now the code is a mess and the lighting isn't nearly complete. It will end up being a Pro only version, though, because of the integration of the pro effects and code.
     
    John-G likes this.
  20. John-G

    John-G

    Joined:
    Mar 21, 2013
    Posts:
    1,122
    @Member, can you show a screenshot if rough weather. Looks like a nice ocean solution.
     
  21. CaptainMurphy

    CaptainMurphy

    Joined:
    Jul 15, 2014
    Posts:
    746
    Not sure if that was supposed to be towards me, but the ocean actions are the same as one of the earlier mobile builds. It needs a lot of work for third person view still so any input is appreciated.






     
    FargleBargle and John-G like this.
  22. John-G

    John-G

    Joined:
    Mar 21, 2013
    Posts:
    1,122
    Looking very nice, some more foam on the wave crests and nave some spray would really help convey some rough water.
     
  23. CaptainMurphy

    CaptainMurphy

    Joined:
    Jul 15, 2014
    Posts:
    746
    I have been trying to get the peaks of the normals to also show foam (based on some external integer like a wind speed) but am at a loss on doing it in the shader. I tried making some spray a while back but just couldn't get the particles to draw where I wanted them randomly enough to look good. Again, it will have to be worked out in the next few weeks. Our plan is to make a Beaufort scale type system that the script uses to interpolate several points of and can then adjust the seas to predefined ranges from the scale.

    Edit: I figured out part of the foam peaking system. I was able to get the height of the normals and use a 'step' to only get the height above a certain point, then use the combination of both normals (the water4 shader has two that are moving in different direction) to only get the peaks when both of them are intersecting. That gave a pseudo random look to the foam when applied with the current foam texture along with it. Since my normals are turned sideways to the motion of the main waves, that gives a nice streaking look. The value is settable through the material, so it can be set according to the Beaufort scale later. So you can adjust the intensity and the height to cut off the peaks.



     
    Last edited: Sep 26, 2014
    FargleBargle and John-G like this.
  24. CaptainMurphy

    CaptainMurphy

    Joined:
    Jul 15, 2014
    Posts:
    746
    https://bitbucket.org/HeartsOfOak/unity-community-ocean-system-pro/

    I got a working copy up on BitBucket. It isn't pretty, but it is working for the most part. It took me a while to strip out the stuff that was coded in for our game. Hopefully we can make it more modular for other games to use easier. I will try and go through the code and document what I know about it. A lot of my work so far has just been trying to understand it in order to make it do what I need.
     
    John-G likes this.
  25. John-G

    John-G

    Joined:
    Mar 21, 2013
    Posts:
    1,122
    Hi Captain Murphy, would it be possible to include relevant textures for the Ocean Materials. Finding it near impossible to get the ocean working with included material - no matter what I use in the materials properties.



    Would it be possible to include a sample Scene not necessarily using your project textures but default ones that show how to set up the shaders and materials for use.
    Am I correct in guessing that you are using the CommunityOceanPro Material and TessellatedWater (OceanReflectionReflaction) Shader.
     
  26. CaptainMurphy

    CaptainMurphy

    Joined:
    Jul 15, 2014
    Posts:
    746
    I'll get a prefab and a scene in this week to give a better example. I think I have some unnecessary files still in there (like the tessellated shader). I kept some in because they are examples of what I want to include in the project.
     
    John-G likes this.
  27. Foxxis

    Foxxis

    Joined:
    Jun 27, 2006
    Posts:
    1,108
    We have been working on a modified version of the mobile ocean project, and it is working fine apart from a weird problem in the shader. We use the mobile shader which seems to be identical to Murphy's project. The problem is that when we rotate the water surface (a dirty way to change wind direction) the fresnel calculation will introduce shading errors (different LOD tiles get different fresnel values, and it is visible).
    Has anyone else seen this, or do you use a different approach to wind direction changes?
     
  28. CaptainMurphy

    CaptainMurphy

    Joined:
    Jul 15, 2014
    Posts:
    746
    How did you get past the tiles going crazy when the ocean is turned? When I tried to turn the ocean to induce a different ocean direction it will cause the tiles to render out of sync with the underlying system and the tiles 'jump' every time they move.
     
  29. Foxxis

    Foxxis

    Joined:
    Jun 27, 2006
    Posts:
    1,108
    I took a peek at your ocean.cs and it seems to use a different tile system (radial?) than the version of the mobile ocean we based our code on, so I am not sure how much help I will be able to provide.
    In short, the version we use have tiles that copy their wave data from a "base tile". This data is not dependent of rotation (it is just vector data from the FFT) so the wave data stays intact when rotating.
    The only thing I did to make sure rotations worked was to adapt the getheight and position offset calculation to take rotation into account.
    However, as I mentioned, when it does rotate the fresnel calculation for wave tiles at LOD > 0 gets messed up. I am still trying to figure out why, but i suspect it is due to the tangent space viewdir calculation not compensating for rotations.
     
  30. Foxxis

    Foxxis

    Joined:
    Jun 27, 2006
    Posts:
    1,108
    ...and I know why the mobileocean shader will not work as is for rotated planes:
    The shader uses tangent.w to pass foam into the shader. However, tangent.w is used to flip the binormal (used to calculate the rotation matrix used to prepare viewdir for fresnel calculations use). When the plane is not rotated I guess the binormal does not have to be flipped. However, when rotating the plane some tiles will need to flip the binormal depending on rotation and camera angle. So the .w is needed.
    I will see if I can work around that, if so I will post the solution here.
     
  31. CaptainMurphy

    CaptainMurphy

    Joined:
    Jul 15, 2014
    Posts:
    746
    Actually you are seeing a radial grid that is under the actual tiles. It still uses the tiles for the ocean rendering, the radial is used to give a false horizon and extension of the ocean itself by simply using the same material. We also did the change for the position offset of the getheight (just swapping the global for local position), but when we change the rotation between a 90 degree increment then our ocean tiles don't work right when we shift them when moving. I am just wondering if you have modified this portion as well:

    Code (csharp):
    1.  
    2. if (followMainCamera) {
    3.  
    4.             Vector3 centerOffset;
    5.            
    6.             //centerOffset.x = Mathf.Floor (player.position.x / size.x) * size.x;
    7.             //centerOffset.z = Mathf.Floor (player.position.z / size.z) * size.z;
    8.             centerOffset.y = transform.position.y;
    9.             //centerOffset.x =  (((int)player.position.x + 64) & ~127);
    10.             //centerOffset.z =  (((int)player.position.z + 64) & ~127);
    11.            
    12.             //Optimized ocean tiles movement
    13.             centerOffset.x = (Mathf.Floor( (player.position.x + size.x * 0.5f)  * sizeInv.x )   *  size.x);
    14.             centerOffset.z = (Mathf.Floor( (player.position.z + size.z * 0.5f)  * sizeInv.y )   *  size.z);
    15.            if(transform.position != centerOffset)
    16.                transform.position = centerOffset;
    17.            
    18.         }
    19.  
     
  32. Foxxis

    Foxxis

    Joined:
    Jun 27, 2006
    Posts:
    1,108
    Yes, you have to modify that to take rotations into account. This is the quick&dirty code that does work, although it needs to be optimised and tweaked to make the transition happen when it's optimal.
    Code (CSharp):
    1. if (followMainCamera) {
    2.             Vector3 modvec = Vector3.zero;
    3.             Vector3 locdiffvec = transform.InverseTransformPoint(player.transform.position);
    4.             if(Mathf.Abs (locdiffvec.x) > size.x*0.5f) modvec += size.x*Mathf.Sign (locdiffvec.x)*transform.right;
    5.             if(Mathf.Abs (locdiffvec.z) > size.z*0.5f) modvec += size.z*Mathf.Sign (locdiffvec.z)*transform.forward;
    6.             willMove = modvec;
    7.             if(modvec!= Vector3.zero) willChange = true;
    8.         }
    Ignore the willMove etc. and just set transform.position += modvec. As mentioned, this code is ugly so please do rewrite it to only act when it's needed.
     
    CaptainMurphy likes this.
  33. CaptainMurphy

    CaptainMurphy

    Joined:
    Jul 15, 2014
    Posts:
    746
    That helped immensely, thank you! Here is what I am at right now and it seems to work very well.

    Code (csharp):
    1.  
    2.         if (followMainCamera) {
    3.             Vector3 modvec = Vector3.zero;
    4.             Vector3 locdiffvec = transform.InverseTransformPoint(Camera.main.transform.position);
    5.             if(Mathf.Abs (locdiffvec.x) > size.x*0.5f) modvec += size.x*Mathf.Sign (locdiffvec.x)*transform.right;
    6.             if(Mathf.Abs (locdiffvec.z) > size.z*0.5f) modvec += size.z*Mathf.Sign (locdiffvec.z)*transform.forward;
    7.  
    8.             if (modvec != Vector3.zero) {
    9.                 transform.position += modvec;
    10.             };
    11.         }
    12.  
     
    John-G likes this.
  34. Foxxis

    Foxxis

    Joined:
    Jun 27, 2006
    Posts:
    1,108
    Yep, that's it. In case someone else wants a brief explanation on what it does: It just checks if the playerposition is outside of the main tile and then moves the grid along the rotated x/z axis instead, which means that the tiling effect works even though the entire grid is rotated or offset by any amount.
     
    CaptainMurphy and John-G like this.
  35. CaptainMurphy

    CaptainMurphy

    Joined:
    Jul 15, 2014
    Posts:
    746
    It works great, thanks again for that. I was not thinking about it correctly and it kept befuddling my caffeine starved brain.

    I updated the project with the latest code (commented out the radial grid for now) and generally did some code cleanup. I updated the test scene and am putting together a night version that shows some differences in the colors to try and replicate night conditions. Hopefully we can get the foam to dim down some at night since it is still very bright no matter what you do.

    There has to be a better way to make the grid look like it goes on to the horizon but so far I am still not able to get anything looking really nice that can do it.

    One thing I have been trying to get the angle that the camera is seeing the material at (inside the shader) so that we can change the transparency and the reflection power based on it. That way the higher the lower the angle you view the water, the less transparent and more reflective it becomes. Anyone good with shaders that can help with that? I have a distance based version working for now, but it doesn't do exactly what I want.
     
  36. Foxxis

    Foxxis

    Joined:
    Jun 27, 2006
    Posts:
    1,108
    The ocean.shader you have already does that (lerps between refraction/reflection based on a fresnel lookup). It needs some tweaking but it does what you want. ;-)
     
  37. CaptainMurphy

    CaptainMurphy

    Joined:
    Jul 15, 2014
    Posts:
    746
    Latest version is committed.
    • whitecaps are distance oriented in the shader. They fade out at around 300 units.
    • Modified the fresnel to change transparency from the refl2Refr variable. Still not what I want, but it is getting closer
    • Added a static plane system to build dynamic simple planes all the way to the view clip. The tiles selection only determines how far 'active' tiles will be visible. From that point to the camera clip a static tile should render.
    • Made change to the shader to change the color of the rgb channel to reduce red coloring based on depth texture. Will later make this a second color selection to signify coming up on shoal water.
    • Increased OceanInspector to allow up to 20 tiles selection. Not necessary with new static tiles system.
    Still need to make the distance for face out a variable. Same for fresnel distance and color change for depth texture. It is getting closer to what I was wanting for our project. Soon it will be time to build the Beaufort system to manage it all.
     
    FargleBargle and John-G like this.
  38. CaptainMurphy

    CaptainMurphy

    Joined:
    Jul 15, 2014
    Posts:
    746
    Committed a couple of changes over the past few hours.

    I have done a bit of code cleanup and renamed all of the classes to have a 'COP_' prefix so they are easier to find in a project. Removed unnecessary files. Added a few buoyancy objects to test the ocean setup. Made a change to the ocean height get script to return a -100 when it is outside of the index. This way it is easier to catch and doesn't throw an error anymore. The buoyancy script just skips that frame for moving the object when it sees the -100. Will work on why it is requesting an object outside of the index before too long.

    I modified the way the static plane system works so that instead of making small tiles, it will make large tiles (the size of the active zone). I also added in some small 'wall' planes that bank away from the static tiles to keep the transition between active and static appearing seamless.

    I did remove some of the alpha changes from the previous build, however, since it was causing problems with the distortion working in regards to wave shape. Right now I am sitting at around 200-300fps with 3k draw distance in fantastic setting.
     
    John-G likes this.
  39. elias_t

    elias_t

    Joined:
    Sep 17, 2010
    Posts:
    1,367
    Hi CaptainMurphy.

    I tried your implementation, but the ocean is always pink.
    I am familiar with the ocean shader I have used it in the past and have made my own optimizations.

    I can't get your shader to a workable state.
     
  40. CaptainMurphy

    CaptainMurphy

    Joined:
    Jul 15, 2014
    Posts:
    746
    Sounds like the shader itself is either not connected correctly or using the wrong one. How are you bringing the ocean into the scene?
     
  41. elias_t

    elias_t

    Joined:
    Sep 17, 2010
    Posts:
    1,367
    I just made a new project and extracted your assets there. Is there another step needed?
     
  42. John-G

    John-G

    Joined:
    Mar 21, 2013
    Posts:
    1,122
    I was getting the same untill I noticed the communityoceanpro material was blank.
    Dragged the foam and normals textures into material to get it working.
    I think the last build CaptainMurphy added has these on the main script so you can assign them there.
     
  43. CaptainMurphy

    CaptainMurphy

    Joined:
    Jul 15, 2014
    Posts:
    746
    The latest build I pushed this morning has an updated unitypackage in the root path of the project. You can try using that instead to see if it leaves the attachments in place.

    I just did a new project, imported no assets into it to start, pulled in the "CommunityOceanPro.package" file and in the new scene I dropped the Ocean prefab from the Assets/OceanPro/Prefabs folder. It worked correctly in that order. Let me know if that works for you, if not we can set up a time to work through it and maybe find the issue.
     
    John-G likes this.
  44. elias_t

    elias_t

    Joined:
    Sep 17, 2010
    Posts:
    1,367
    I have imported the package in a new project. Imported the prefab into the scene and still get the pink shader.

    The same goes for the day/night scenes.

    edit: the shader throws erros. Here the screenshot.

    oos.jpg
     
    Last edited: Oct 9, 2014
  45. CaptainMurphy

    CaptainMurphy

    Joined:
    Jul 15, 2014
    Posts:
    746
    Possibly a GPU problem? What graphics card are you using? I know a lot of the shader is optimized for dx11 so it may be a factor.
     
  46. elias_t

    elias_t

    Joined:
    Sep 17, 2010
    Posts:
    1,367
    Ok. I am using an ati 5870 on winxp dx9.
    Is the shader supposed to run on dx11 only? If so sorry for the hassle.
     
  47. John-G

    John-G

    Joined:
    Mar 21, 2013
    Posts:
    1,122
    Latest version is looking and running fantastic.



    Would it be possible to darken (shadow) or separate colour the front of the waves to give them more definition.
    Would really help to make the waves stand out. Something like this:



    As is its still fantastic and the best solution I've seen to date. :D
     
  48. CaptainMurphy

    CaptainMurphy

    Joined:
    Jul 15, 2014
    Posts:
    746
    No hassle, it is interesting to know it doesn't work right on dx9 though. I don't think I even have a machine that is limited to it and have yet to figure out how to bring the engine or my gpu back that level to test. I will bet that it has to do with the depth rendering texture. Maybe we can put some kind of switch in the shader to turn that off for non-dx11 cards.


    That is what I was looking for with the angle of the viewer to the water poly. The fresnel doesn't cover that exactly so I think in the shader it will have to be worked in there somehow. I want to make the water turn slightly more transparent and be able to change the coloring based on the angle that the viewer is seeing the surface. Now that it is running pretty smooth I may get a chance to dig into that part of it. Shaders are far from my expertise, so it is like fumbling in the dark to get a working solution for me.
     
  49. John-G

    John-G

    Joined:
    Mar 21, 2013
    Posts:
    1,122
    @CaptainMurphy is the bouyancy script in package the same as what you are using on your vessels.
    Can't seem to get it to behave correctly in heavy seas. Model seems to drift from box collider :confused:.
     
  50. CaptainMurphy

    CaptainMurphy

    Joined:
    Jul 15, 2014
    Posts:
    746
    Our buoyancy script is not even close to that one. Ours handles the CG, mass, etc. but is heavily integrated into our ship script since it affects a lot of the ships properties dynamically. I can try to find the original script ours is based on and see about including that in the next build of COP. The one included is a simple 'put me at Y elevation and try to mimic the angle of the wave'.

    At this point I am about to start over on the ocean system. The grid of tiles isn't working like I want and I can't make the wave length change enough to suit my vessel hull speed calculations. I am starting to look at a system like Scrawk made for his Proland build where he only renders what is in the camera frustum instead of rendering all of the tiles at once. Then using a calculation to determine the wave height at X,Z instead of always using a noise map. Hopefully offloading a bit more work to the GPU as well.

    Edit: found the base script we started with, though ours doesn't really resemble that one anymore.
    http://forum.unity3d.com/threads/buoyancy-script.72974/
     
    Last edited: Oct 24, 2014
    John-G likes this.