Search Unity

The Community Ocean Shader (Open Source) Unity 5

Discussion in 'Shaders' started by laurent-clave, Nov 30, 2015.

  1. elias_t

    elias_t

    Joined:
    Sep 17, 2010
    Posts:
    1,204
    Hi.

    If you want to use the system on other surfaces, you could have it just calculate the ocean simulation without updating its tiled meshes.

    You would have to dig in the code though.

    To do this you should use only the calcComplex and the fft functions and avoid the calcPhase3 and calcPhase4 functions.

    Then to update your mesh, you should use the getHeightChoppyBatch functions to get the height and x position of the vertices of your mesh, calculate normals and tangents.

    I plan to add a projected grid method and by that time I will make the code more modulable, so it would be easier to add mesh rendering/updating methods. But not any time now.

    I upload to github the whole unity project, thats why you see meta files.
    You can export a unity package from there with all its dependencies to add into another project.
     
  2. amimox

    amimox

    Joined:
    Feb 11, 2016
    Posts:
    19
    Oh thank you so much, I did not think to that !
    But by spreading, I want that water flows on a surface freely, like splashing particles. Give a look at Fluvio in the Asset Store. This is what I want.
     
  3. elias_t

    elias_t

    Joined:
    Sep 17, 2010
    Posts:
    1,204
    Ok. But then this system is not suited for what you want.

    Maybe just modify the shader and use it on a mesh with cheaper wave calculations ...
     
  4. amimox

    amimox

    Joined:
    Feb 11, 2016
    Posts:
    19
    Hey elias_t I just packaged the whole thing and damn, this ocean is awesome and so realistic so far ! Never see such a thing ! Great job !
    I have a question : Can we use this to create small water area, like just a lake size ? Because I tried to scale down this ocean transform, but it was unsuccesful. Did it exists a such scaling area setting ?
     
  5. elias_t

    elias_t

    Joined:
    Sep 17, 2010
    Posts:
    1,204
    Good to know it is some use for you.

    Although for a small water area it would be an overkill to run the ocean simulation.
    You can make it lighter however by lowering the width x height resolution to 32x32.

    There is no such thing as scale for the system. You lower the size by:

    - using less tiles (you can even use one tile)
    - lowering the tile size

    As I said before, when the system becomes more generic, it will allow it to work on generic meshes.
     
  6. langpei

    langpei

    Joined:
    Mar 18, 2015
    Posts:
    1
    Thanks a lot for sharing your great work!
    I'm now going to use it in my project (a marine simulator) and I wonder if I can add the wake of a boat based on the current ocean shader.

    Do you have any suggestions or ideas?
     
  7. elias_t

    elias_t

    Joined:
    Sep 17, 2010
    Posts:
    1,204
    I would extrude a mesh or meshes behind the boat and update its vertex positions according to wave height.
    This mesh should use its own shader to alpha blend with the water.
     
    VKirillov and langpei like this.
  8. RavenMikal

    RavenMikal

    Joined:
    Oct 18, 2014
    Posts:
    144
    :eek:!
    it looks fantastic!
     
  9. amimox

    amimox

    Joined:
    Feb 11, 2016
    Posts:
    19
    Hi again !
    I need to know something about the lighting of the Ocean.
    Basically, you can load a Sun (light) into the script.
    But if I reduce the insenty of that light, the Ocean seems to not react and be lighted the same way.

    My goal is to make a realistic night Ocean, with a slight lighting to make the ocean dark but not too much.
    It is possible without altering Base colors ?
     
  10. elias_t

    elias_t

    Joined:
    Sep 17, 2010
    Posts:
    1,204
    Hi.

    The darkness of the ocean depends on the sun's color values.

    Modify this to allow intensity to alter the darkness of the ocean:

    Code (CSharp):
    1.                 if(sunLight.color != oldSunColor) {
    2.                     if(useShaderLods) { for(int i=0; i<numberLods; i++) { if(mat[i] != null) mat[i].SetColor("_SunColor", sunLight.color); } } else { if(material != null) material.SetColor("_SunColor", sunLight.color); }
    3.                     oldSunColor = sunLight.color;
    4.                 }
     
  11. amimox

    amimox

    Joined:
    Feb 11, 2016
    Posts:
    19
    Ok, thank you for the code.
    But sorry if I look dumb, but where I put this code ?
    On Ocean.cs (in what method)?
    So this code will basically alter the darness of the ocean depending on the sun color (in fact, the light color).
    But, is the light intensity matters too ?
    What I want is that the darkness of the ocean change depending on the Sun intensity.
    Example, at night, sunlight intensity will be ~ 0.1 but the sunlight color is always white.

    Thanks for your help.
     
  12. amimox

    amimox

    Joined:
    Feb 11, 2016
    Posts:
    19
    Nevermind I found myself :D
    Sorry for being annoying. ^^
     
  13. elias_t

    elias_t

    Joined:
    Sep 17, 2010
    Posts:
    1,204
    This code is already in the Oceran.cs file. Search for it and you can modify it.

    Create a float oldSunIntensity and compare it with the current sun intensity.
    If the intensity changes, modify the above code likes this:

    Code (CSharp):
    1.     if(sunLight.color != oldSunColor || sunLight.intensity != oldSunIntensity) {
    2.                         if(useShaderLods) { for(int i=0; i<numberLods; i++) { if(mat != null) mat.SetColor("_SunColor", sunLight.color * sunLight.intensity); } } else { if(material != null) material.SetColor("_SunColor", sunLight.color * sunLight.intensity); }
    3.                         oldSunColor = sunLight.color; oldSunIntensity = sunLight.intensity;
    4.  
    5.                  }
     
    amimox, Gherid_lacksGPS and hopeful like this.
  14. amimox

    amimox

    Joined:
    Feb 11, 2016
    Posts:
    19
    It works perfectly ! Thank you !
    My error was to not backup and oldSunIntensity, so the code will not update the sun intensity, but now it's fine !
    Very extensible engine !
     
    Gherid_lacksGPS and elias_t like this.
  15. spuller

    spuller

    Joined:
    Jun 12, 2015
    Posts:
    1
    Hi,
    I have to repeat what most people have said, and say this ocean is amazing. I've attached it to a project I'm working on and it has increased its quality tremendously. However, I'm now looking to make it work in a network. I'll be looking through the ocean.cs plugin to see how I can sync it between players. But if anyone could help guide me toward a possible direction, that would be wonderful. I'm thinking having a stationary object in the middle of the map that hold the ocean and everyone gets the information from that object. However, I've noticed that the wave LOD's expand past the visual waves, perhaps I can make it so different targets of the ocean will load tile from different areas of the loaded ocean. I'll post updates if I make any progress. Again if someone knows already how to do this, it would be great if you could share your all knowing knowledge :)
     
  16. elias_t

    elias_t

    Joined:
    Sep 17, 2010
    Posts:
    1,204
    Hi.

    I haven't done any networking sample till now but I could advice:

    The most important variable of the ocean sim is this:
    Code (CSharp):
    1. float time = Time.time;
    Sync this and of course all the other variables that you might change (they are exposed in the inspector)
    and you could have something to start.
     
    hopeful and Gherid_lacksGPS like this.
  17. Emre_U

    Emre_U

    Joined:
    Jan 27, 2015
    Posts:
    49
    Elias_t I have come in to say this is awesome awesome stuff. As it is a community project I think a thank you post to you guys is the least I can do.
     
    kB11, FiveFingers and elias_t like this.
  18. nozprod

    nozprod

    Joined:
    Feb 20, 2014
    Posts:
    5
    Hi,

    I'm back, this time to talk about some troubles I'm facing when exporting for the web. When running on web browsers, performances drop a lot, even if it's working perfectly through Unity… Ok you might say it's normal, due to network limitations or other specifications.

    We tested it on two macbook retina 2013 :
    - First with a NVIDIA GeForce GT 650M 1024 Mo
    - Then with Intel Iris Pro 1536 Mo

    These two configurations should be able to run the build without any troubles, it works perfectly through Unity, but not when navigating the build on Chrome. Actually, while the framerate stay stable, we've got a lot of frame drops, I can't figure out why… I tried to remove assets one by one to see if there's something significant impacting performances, but always the same problem…

    Do you know if there's a way to improve the script, or if there's some options I can disable to make it run properly on every configs when exporting in webgl, either on the ocean script or in Unity ?

    Thanks a lot for your help :)

    Cheers
     
  19. elias_t

    elias_t

    Joined:
    Sep 17, 2010
    Posts:
    1,204
    Hi.

    You are facing lower performance on WebGl because ... well ... WebGL does not run so good in any other project.
    For the other platforms, when NATIVE preprocessor is enabled, highly optimized native plugins are used.


    But...

    - Try to disable the NATIVE preprocessor directive in the 'other settings' of Build Player Settings.

    (There is an emscripten compiled plugin for WebGL that should give better performance. But it
    could cause trouble in your case.)

    - Lower the tile resolution to (32x32).

    - use a more simple shader (like default shader 3 in the inspector settings)
     
  20. EDarkness

    EDarkness

    Joined:
    Feb 1, 2013
    Posts:
    420
    I started adding this to my scene and the biggest problem is that the water refuses to stay in the right place. I need the water to stay in a certain point, but when it's generated it always ends up in some strange place on the map. It also doesn't always end up in the same place. It's like it's generation is completely random. Is there a way to force it to be in one place at a set height?
     
  21. elias_t

    elias_t

    Joined:
    Sep 17, 2010
    Posts:
    1,204
    Hi. The system, as it is right now, is built to be at 0,0,0 and to extend infinitely around the player camera.

    In a future update I might add the ability to use offsets to allow placing it at different xyz values.
     
  22. EDarkness

    EDarkness

    Joined:
    Feb 1, 2013
    Posts:
    420
    I figured it out through trial and error. Got it to where I want it. The next problem I have is that the seams are broken. There are gaps between them. Is there any way to fix this? Otherwise it looks great and I think I can use this in my game.

    Skullforge_Dev_38.jpg Skullforge_Dev_39.jpg Skullforge_Dev_38.jpg Skullforge_Dev_39.jpg
     
  23. elias_t

    elias_t

    Joined:
    Sep 17, 2010
    Posts:
    1,204
    Hi. Good to see that you solved your issue.

    The seams are intended to be "broken" this way.
    That comes from the Far Lod-Y offset to place the more far away lods to a lower position so that gaps between lods are minimized when having larger waves.

    This should not look like this if your view camera is assigned correctly at your player position.
     
    hopeful likes this.
  24. EDarkness

    EDarkness

    Joined:
    Feb 1, 2013
    Posts:
    420
    Well, there is no camera at the player position. It's just a game object that sits in space that's used as a target so that I can keep the water at a very specific place. Since the zone does not have the player in it when it's created, there's no way to assign the player (unless I do it via code and I don't want to do that since each zone should be stand-alone). Water just needs to sit there and look pretty. Is there a way to turn this off so that the water doesn't do this?

    Also, how can I keep the water level from going too high even with waves? The water keeps getting higher and I only want it to go so high, but I do want it to look like waves are moving.
     
  25. elias_t

    elias_t

    Joined:
    Sep 17, 2010
    Posts:
    1,204
    Then set Far Lod-Y offset to 0.

    sea.png

    But in your case you could do this:

    Set the target of the ocean to the transform of the object that you want the ocean to around it.
    You could also uncheck 'follow' if you want the ocean not to follow that obect.

    Set your Tiles count to 3 (since you don't want a large amount of sea area)

    For the water going too high (in The wave setting):

    Lower the scale and raise the choppy scale.
     
  26. EDarkness

    EDarkness

    Joined:
    Feb 1, 2013
    Posts:
    420
    Thanks for your help. Looks like the water works great now. Looks cool even from the top down angle I'm using. I'm still not sure about the buoyancy stuff, but that's a problem for another day (once I get some actual boats to float on the water). Thumbs up, man. :D
     
    hopeful and elias_t like this.
  27. elias_t

    elias_t

    Joined:
    Sep 17, 2010
    Posts:
    1,204
    Good to hear.

    The buoyancy will work correctly as long as you offset the ocean at xz axis as a multiple of the Tiles XZ size.
     
    Last edited: Mar 24, 2016
    EDarkness likes this.
  28. Brichta

    Brichta

    Joined:
    Jun 15, 2015
    Posts:
    1
    Hi, at first, thanks for this great shader... can you please help me with this small issue (see screenshot). Shader is perfectly working on all iOS devices, only on the new iPhone6 is this problem. Unfortunately, I have only iPad device to debug, so it is hard to solve this issue... do you have any idea ?
     

    Attached Files:

  29. elias_t

    elias_t

    Joined:
    Sep 17, 2010
    Posts:
    1,204
    Hi. This has to do with the rendering path you are using and the cameraDepthTexture produced by unity.
    I wanted in the next release to produce the depth texture in a render texture to avoid such issues.
    (But as I stated, I am swarmed now by commercial contracts and have no time.)

    For now you can:

    - Disable the shore line foam.
    - Find the right combination of ocean rendering queue and rendering path that provides a camera depth texture that does not look like this ...
     
  30. Hormic

    Hormic

    Joined:
    Aug 12, 2014
    Posts:
    250
    Hi Elias, Just discovered this thread and i'm astounished about this project and all your effort floating into it.

    Hopefully get my Vive soon, and then i'm curious if it will also work within VR.

    Thank you very much for this amazing ocean water project!
     
  31. elias_t

    elias_t

    Joined:
    Sep 17, 2010
    Posts:
    1,204
    Hi Hormic.

    I am glad you like it.

    I will continue this project later on by adding shadow support and other rendering methods.

    It is designed with high performance in mind so it should run on VR.

    Provide feedback if you have some results.
     
    Hormic likes this.
  32. iivo_k

    iivo_k

    Joined:
    Jan 28, 2013
    Posts:
    312
    Great job with the optimizations! You should add the Library folder to .gitignore and leave it out of the repository though.
     
  33. elias_t

    elias_t

    Joined:
    Sep 17, 2010
    Posts:
    1,204
    Hi.

    I had it like this in the beginning. But the scene would not load with some assigned objects on the inspector.
    So I put it back in...
     
  34. Scorpile

    Scorpile

    Joined:
    Apr 8, 2016
    Posts:
    5
    Hi, when will you update the code to allow shadows? Really cool work by the way!
     
  35. elias_t

    elias_t

    Joined:
    Sep 17, 2010
    Posts:
    1,204
    Thanks.

    It will take a month or so I guess.
     
    Gherid_lacksGPS likes this.
  36. iivo_k

    iivo_k

    Joined:
    Jan 28, 2013
    Posts:
    312
    I'm having trouble with values not being saved after I change them in the inspector. For example if I toggle "Spead along frames", press Ctrl+S and reload the scene, the value change is not saved. I assume this could be because the editor script doesn't use SerializedObject / SerializedProperties.

    Also, is there any way to simulate forward a couple updates or something on the first frame, so the boat wouldn't drop in the beginning or start underwater when there are large waves?

    Edit: Forgot one, when I pause my game for a while (setting timeScale to 0) and then resume, the boat makes a huge jump. I've been looking around but didn't find the cause yet.
     
  37. elias_t

    elias_t

    Joined:
    Sep 17, 2010
    Posts:
    1,204
    For the 1st issue go to Ocean.cs and add [SerializeField] before public bool spreadAlongFrames = true;

    The other 2 issues: they should actually not happen because I have a function that places the objects on the current sea height. But I guess it is buggy.

    At line 458 of Ocean.cs the script precalculates actually the 1st frame of the simulation and on line 477 places the player boat on the correct sea level.

    Your issue might be with the buoyancy script:

    Check if you have visibility checks enabled and try to modify the Buoynacy script at line 150.
     
  38. iivo_k

    iivo_k

    Joined:
    Jan 28, 2013
    Posts:
    312
    Nope, that's not it. It's a public bool already, so it's serialized. The problem is with OceanGeneratorInspector.cs, which should use SerializedObject and SerializedProperty objects instead of the Ocean object.

    Example:

    Code (CSharp):
    1.     private SerializedObject serObj;
    2.     private SerializedProperty spreadAlongFrames;
    3.  
    4.     void OnEnable()
    5.     {
    6.         serObj = new SerializedObject(target);
    7.     }
    8.  
    9.     public override void OnInspectorGUI()
    10.     {
    11.         serObj.Update();
    12.  
    13.         EditorGUILayout.PropertyField(spreadAlongFrames, new GUIContent("Spread along frames"));
    14.  
    15.         serObj.ApplyModifiedProperties();
    16.     }
    This works properly for the spreadAlongFrames flag and also allows undo to work. I'll see how long it would take to update OceanGeneratorInspector and if I do it I'll post it here.

    I made a youtube video of the behaviour using the Ocean_with_boat scene. Changed the camera position and the waves a bit I think. First I pause and run the scene, going frame by frame to show what's going on with the ocean and the boat. The ocean is flat for the first 2 frames, then changes a lot on the third frame and then settles.

    Then I change the time scale to 0 and back to 1 to show how the boat jumps. Not sure what to change in the buoyancy script to fix that, but I'm not sure how visibility would be related.

     
    Last edited: Apr 12, 2016
  39. elias_t

    elias_t

    Joined:
    Sep 17, 2010
    Posts:
    1,204
    Hi.

    Thanks for pointing those out.

    For the jump issue and the time scale, I'll have to make more tests and get back to you.
     
  40. elias_t

    elias_t

    Joined:
    Sep 17, 2010
    Posts:
    1,204
    Ok. For the jumping issue on the boat the solution is this:

    On the buoyancy inspector of the boat check the use FixedUpate to enabled.
     
  41. iivo_k

    iivo_k

    Joined:
    Jan 28, 2013
    Posts:
    312
    Thanks, fixedUpdate for buoyancy works.

    Here's my version of OceanGeneratorInspector.cs. I also edited Ocean.cs a bit to add OceanMode and GridMode enums.

    The editor now uses SerializedObject and SerializedProperties, so it should save properly and allows undo. There could be bugs and errors of course, but it worked for the stuff I tried. I also changed the info buttons to tooltips on the labels, since I felt it's simpler, although it would be best to add tooltips to all of them for consistency.
     

    Attached Files:

    Gherid_lacksGPS likes this.
  42. elias_t

    elias_t

    Joined:
    Sep 17, 2010
    Posts:
    1,204
    Thanks!

    I will add the serialized stuff in a next update.
     
    Gherid_lacksGPS likes this.
  43. iivo_k

    iivo_k

    Joined:
    Jan 28, 2013
    Posts:
    312
    BTW the buoyancy editor script has the same problem, the changes aren't saved properly since it doesn't use SerializedObject & SerializedProperties.
     
  44. elias_t

    elias_t

    Joined:
    Sep 17, 2010
    Posts:
    1,204
    Thx for the info. Will be fixed too.
     
    hopeful likes this.
  45. creat327

    creat327

    Joined:
    Mar 19, 2009
    Posts:
    1,194
    Wow I haven't checked this project for a long long time. I just gave it a spin on my android. I see no speed improvement over the version a few years ago (it seems to even run slower :?)

    Is the mobile version supposed to get a speed boost from the older community version? This is a quad core chip, it runs at 40 fps with only the water and the boat, default mobile scene (no shore lines, etc). It's pretty slow considering there is nothing else on screen so it would be no use on a real mobile game.
     
  46. elias_t

    elias_t

    Joined:
    Sep 17, 2010
    Posts:
    1,204
    Hi.

    Yes it is considered to run much faster.
    Have you enabled the native preprocessor directives?

    Even with just the c# optimizations I did in the beginning of the revamp it was significantly faster.
    On my 4 year old Android phone I saw a 2x speed increase.

    You might have these issues because you might not comparing same settings.

    Try 32x32 mesh resolution.
    There are shader variations which go from cheap to expensive performance wise.
     
  47. creat327

    creat327

    Joined:
    Mar 19, 2009
    Posts:
    1,194
    It is already at 32x32 by default...
    I've leave the settings on default from the mobile scene with the only exception is activating the shore lines since it was there too on the old version
     
  48. elias_t

    elias_t

    Joined:
    Sep 17, 2010
    Posts:
    1,204
    If you have the default unity5 procedural skybox enabled, don't use this on mobile. Its an fps killer.

    Also you could have a fillrate problem with your mobile gpu. Reduce the number of Tiles in the Tiles slider.
    This version comes by default with many more tiles enabled then the original version.

    What gpu does your mobile device have?
     
  49. creat327

    creat327

    Joined:
    Mar 19, 2009
    Posts:
    1,194
    that was it, the procedural skybox!
    Anyway, it looks great on mobile but the problem as always is the shore lines... There must be a way to add them on mobile since it doesn't have depth buffer it must be by using meshes.
    I'm trying to merge the water with the generated mesh from this:
    http://forum.unity3d.com/threads/auto-foam-generator-request.29578/

    but so far no luck, the shore line is shown too much through it. Trying to modify the shore shader and alpha but still not getting it right. There must be a way to get that shore line generator and place it inside the package, mixing it with the water color so that it looks as one
     
  50. elias_t

    elias_t

    Joined:
    Sep 17, 2010
    Posts:
    1,204
    I intend to change the depth buffer usage by producing it through a render texture.

    But I have no time right now ...
     
unityunity