Search Unity

Open Source Atmospheric Scattering

Discussion in 'Works In Progress' started by Michal_, Jul 30, 2016.

  1. Michal_

    Michal_

    Joined:
    Jan 14, 2015
    Posts:
    357
    I experimented with atmospheric scattering and volumetric lights in my free time and I decided to make the projects open source. Maybe some of you will find it useful.

    You can find the source code on GitHub
    ! These projects are my hobby/prototyping projects after a little clean up. They're not truly finished or polished and probably never will be. It may be good enough for some as it is and it may be a starting point for others.


    Sister project: Volumetric Lights (GitHub) (Forum)
     
    Last edited: Aug 2, 2016
    Aniow, konsic, Elecman and 10 others like this.
  2. No0B25

    No0B25

    Joined:
    May 2, 2016
    Posts:
    67
    Looks super neat! :) Will try it out and tell you my thoughts on it.
     
  3. jwinn

    jwinn

    Joined:
    Sep 1, 2012
    Posts:
    72
    Looks great. How is this similar or different from The Blacksmith: Atmospheric Scattering package, and how is the performance? I haven't messed around with either yet, but I'd only need something simple and lightweight on my current project.
     
  4. Michal_

    Michal_

    Joined:
    Jan 14, 2015
    Posts:
    357
    You're going to have to try it for yourself. I didn't do any profiling (or optimizing for that matter). But it runs on my passively cooled ultrabook with integrated gfx.. Light shafts are the only truly expensive part. You should be ok if you turn them off.
    I never tried the blacksmith demo but I read the blog post last year. They only calculate atmospheric fog/haze and light shafts. Everything else is static (skybox, light color, ambient color..). So you can't use it for dynamic time of day unless you handle sky and light color by yourself. It also isn't physically based. It was designed to give more artistic control (often preferable for games). Both projects have in fact very little in common. Just try them and see what works better for you.
     
    jwinn likes this.
  5. moure

    moure

    Joined:
    Aug 18, 2013
    Posts:
    184
    Tried it a bit in latest Unity 5.4.0f3 . Its a bit glitchy, you can check the issue on this video. Just the default demo scene.
     
  6. Michal_

    Michal_

    Joined:
    Jan 14, 2015
    Posts:
    357
    That's not a bit glitchy, that's regularly broken. Didn't know 5.4 is out already. And just when 5.3 was starting to look good after almost 30 patches :). Can you verify if it works in 5.3? I would state somewhere clearly that it is for 5.3 only for now.
     
  7. moure

    moure

    Joined:
    Aug 18, 2013
    Posts:
    184
    Yeah , i underplayed it a bit! I don't have 5.3 at the moment, i downloaded 5.4 and went on installing it as i thought it would create a new folder like it did while in beta. I was wrong, it overwrote my 5.3 installation. So if you do try it, remember to check the installation folder :D
     
  8. K-E-I

    K-E-I

    Joined:
    Oct 6, 2012
    Posts:
    17
    Atmospheric scattering is pretty good actually, but light shafts seems wrong to me in states when sun is visible or not.
    Tested it on 5.4.
    I also added a sun disk to your shader and combined it with kode80's cloud rendering system. And it looks really gorgeous together!

     
    DevMember and JamesArndt like this.
  9. Michal_

    Michal_

    Joined:
    Jan 14, 2015
    Posts:
    357
    I just verified that it doesn't work correctly on 5.4. It is mostly broken in fact. I'm surprised you managed to get something out of it. The video I posted is 100% representative of how it looks on 5.3. Including the sun disk.
     
  10. Max231

    Max231

    Joined:
    Dec 23, 2013
    Posts:
    22
    Hi, looks very interesting! I was wondering if there is a way to scale the atmosphere for planets with other sizes than earth
     
  11. Ysgramor

    Ysgramor

    Joined:
    Jan 23, 2014
    Posts:
    69
    It is such a coincidence that Im actually wanna create a new thread about my free realtime atmospheric scattering based on unity blacksmith, since no free one here in community, but saw your thread here, maybe i'll post it later then
     
  12. Crembo

    Crembo

    Joined:
    Jun 2, 2014
    Posts:
    16
    If you're using kode80 clouds, you probably want to attach a script that grabs your directional light color and ambient color and updates the cloud top/bottom color from it so they match what Atmospheric Scattering is doing. Looks much better.
     
  13. Michal_

    Michal_

    Joined:
    Jan 14, 2015
    Posts:
    357
    It is possible to do but not supported out of the box. Planet radius, atmosphere radius, atmosphere density etc. are hard-coded constants. The scattering is mostly precomputed, so only one planet is supported atm.
    It is also worth noting that the optimization strategy I took favors lower altitudes. It doesn't look very good from upper atmosphere or outer space.
    The more the merrier :)
     
    Ysgramor likes this.
  14. Michal_

    Michal_

    Joined:
    Jan 14, 2015
    Posts:
    357
    Should work with 5.4 now
     
    elbows, Ysgramor and moure like this.
  15. nbac

    nbac

    Joined:
    Jul 7, 2015
    Posts:
    239
    nice work will try this asap :)
     
  16. nbac

    nbac

    Joined:
    Jul 7, 2015
    Posts:
    239
    could someone provide such a script!? since its now working and looks super cool! just when sun moves below horizon clouds should go dark.
     
  17. Crembo

    Crembo

    Joined:
    Jun 2, 2014
    Posts:
    16
    If you stick this on the kode80 clouds GameObject, and link your directional light to it from the Inspector, it should do more or less what you want.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class UpdateCloudColor : MonoBehaviour {
    5.     public Light dirLight;
    6.     private kode80.Clouds.kode80Clouds clouds;
    7.  
    8.     // Use this for initialization
    9.     void Start () {
    10.         clouds = GetComponent<kode80.Clouds.kode80Clouds>();
    11.     }
    12.  
    13.     // Update is called once per frame
    14.     void Update () {
    15.         clouds.cloudBaseColor = RenderSettings.ambientLight * RenderSettings.ambientIntensity;
    16.         clouds.cloudTopColor = dirLight.color * dirLight.intensity;
    17.     }
    18. }
     
    Arkade and John-G like this.
  18. nbac

    nbac

    Joined:
    Jul 7, 2015
    Posts:
    239
    thanks crembo this pretty awesome
     
    Last edited: Aug 10, 2016
    DevMember likes this.
  19. nbac

    nbac

    Joined:
    Jul 7, 2015
    Posts:
    239
    crembo could i ask you for a favour. could you provide a script doing the same thing to the crepusuclar rays from kode80´s clouds. my unity scripting knowledge is kind of limited ;D
     
  20. mcbauer

    mcbauer

    Joined:
    Oct 10, 2015
    Posts:
    358
    I just can't get the light shafts to work. Even running the demo.

    I have the light set to cast shadows, DX11, deferred,.....is there some other special trick to get it to work?
     
    Last edited: Aug 18, 2016
  21. mcbauer

    mcbauer

    Joined:
    Oct 10, 2015
    Posts:
    358
    @nbac was there anything additional you had to do in order to get those light shafts to appear?
     
  22. nbac

    nbac

    Joined:
    Jul 7, 2015
    Posts:
    239
    no nothing special those shafts are just shadows dropped onto the volumetric clouds. the whole effect needs shadows to be activated of course. you are using both atmo scattering + volumetric lights right?
     
  23. mcbauer

    mcbauer

    Joined:
    Oct 10, 2015
    Posts:
    358
    @nbac I'm using the demo scene it ships with. The one with the mountains.

    I'm not seeing anything on the light that makes it a volume light.
     
  24. mcbauer

    mcbauer

    Joined:
    Oct 10, 2015
    Posts:
    358
    The volume light project isn't working for me, several compile errors. I guess it might something to do with me being in Unity 5.1.
     
  25. pointcache

    pointcache

    Joined:
    Sep 22, 2012
    Posts:
    525
    Please organize the project so that core files are in separate folder, examples are in separate folder and not just dumped into assets root, this is not the way to share things with people.
     
  26. pointcache

    pointcache

    Joined:
    Sep 22, 2012
    Posts:
    525
    where is the working version?
     
  27. pointcache

    pointcache

    Joined:
    Sep 22, 2012
    Posts:
    525
    tested on unity 5.4f3 everything is black
     
  28. ronjart

    ronjart

    Joined:
    May 16, 2013
    Posts:
    93
    Great Asset, and thank you for making this free (I'd pay for it :)

    I got this working pretty nicely in Unity 5.4f3. Works perfectly in VR (SteamVR and Vive) in the Editor, but once I build all turns to black (I see some faint outlines) I included the Shadersas described in the readme on Github.

    Any idea what the cause could be?
     
  29. PixelMind

    PixelMind

    Joined:
    Aug 16, 2013
    Posts:
    100
    Looks great!
    Thank you for sharing this!
     
  30. Michal_

    Michal_

    Joined:
    Jan 14, 2015
    Posts:
    357
    Please try 5.3.4 or newer if you can. There are serious bugs in previous versions. Both projects are unlikely to run properly on older versions.
    I think I made terrific job if I compare it to the assets store packages I've seen :) Yeah, I guess it is not ideal. I'll keep it on mind next time I publish something. Thanks for the feedback. I never really though about it to be honest. At least I took the effort to clean it up a little. You should see it before the clean up. It is still just my prototyping project that I made publicly available. You have to keep your expectations in check ;)
    Are you sure you've included all the shaders? Can you look at the application log? There are likely some errors.
     
    Martin_H likes this.
  31. rus_pa

    rus_pa

    Joined:
    Mar 30, 2016
    Posts:
    10
    I tried using these scripts and shaders on my planet but as I add and configure things (following the usage steps you describe) I am not even able to complete the config that everything in the editor goes gray and stutters like if I had a 1990 pc with a cirrus logic card. I can't even use unity editor any more, I have tried to lower any given parameter on the camera script but this does not help at all.
    But if I load your plain scenes without anything else it's really wonderful!
    Too bad I tried again with my project to spot any possible difference between your scene config and mine and nvidia driver crashed twice and then pc rebooted.
    That's something :D
     
  32. Michal_

    Michal_

    Joined:
    Jan 14, 2015
    Posts:
    357
    Sorry you're having such problems. I have no idea what could cause them. I assume there aren't any errors in console or unity log? I'm afraid there isn't much I can do if the sample scene is working fine. Not without repro steps or project.
     
  33. Sir-Spunky

    Sir-Spunky

    Joined:
    Apr 7, 2013
    Posts:
    132
    First of all, I just have to say a big thank you for releasing this for free. It's the best atmospheric scattering effect I've seen for Unity! The official Blacksmith scattering looked great as well, but was too static and hard to combine with a dynamic time-of-day. Yours is much better.

    Your scattering looks beautiful, and performs great with the "Optimized" setting. However, I'm having a few strange issues in 5.4.0f3:

    When adding the improved Bloom post-effect from Unity's new Cinematic Image Effects (must also disable Tonemapping) I get some strange highlights in the sky if I rotate the Sun below the horizon (see screenshot below). Even when I disable bloom, I noticed that kode80 clouds look odd in the sky, as if they lose all lighting and just becomes a constant gray color. Again, this only happens after I rotate the Sun below the horizon, otherwise it looks great.

    Another issue I'm having in a project, is when I rotate the Camera above the horizon, so that I only see the sky in the Camera's view, then the sky becomes much darker, until I put the ground back into view. This only happens with Light Shafts enabled. However, I've not been able to reproduce this last issue in your demo scene, so it's really hard to track down, even though I've tried disabling everything in my project without success. There have also been occasions when it's been working fine, so it's very hard to pin-point.

    I'll try to keep a look out and see if I can gather more information on these issues.

    Here's a screenshot from the first issue:
    Bloom.jpg
     
  34. Michal_

    Michal_

    Joined:
    Jan 14, 2015
    Posts:
    357
    I'm not familiar with Unity's bloom. I'll take a look when I have time.
    Again, I don't know anything about kode80 clouds. But my system simulates light scattering from sun only. It doesn't simulate moon or stars so there is no incoming light at night. I assume the clouds are colored based on light's color and that doesn't change at night. You're going to have to add your own moon/star/ambient light for night and make it work with the clouds.
    Yeah, that's a Unity limitation. Unity stops to render shadow maps if there is no visible shadow caster and that breaks the light shafts. Unfortunately, there is no way how to detect that from code. I put a "DummyShadowCaster" object under camera in the sample scene as a workaround. It isn't visible because the back faces will be clipped during rendering and it forces Unity to always render the shadow map.
     
    Sir-Spunky likes this.
  35. BlendCraft

    BlendCraft

    Joined:
    Nov 2, 2014
    Posts:
    274
    I use Time of Day Skydome's atmospheric scattering plugin and it has problems with some shaders. Will this be a better alternative?
     
  36. Michal_

    Michal_

    Joined:
    Jan 14, 2015
    Posts:
    357
    @Sir-Spunky Hey, fixed the bloom issue. Sorry, it took so long.
     
    Crembo, Alverik and Sir-Spunky like this.
  37. Sir-Spunky

    Sir-Spunky

    Joined:
    Apr 7, 2013
    Posts:
    132
    Amazing, now it works perfectly! Thank you so much.
     
  38. igorbasko

    igorbasko

    Joined:
    Oct 24, 2016
    Posts:
    2
    Hi all,
    I tried the example of the latest version on Unity 5.5 on OS X Yosemite (openGL 4.1), and it seems broken.

    That's what I get in the Editor when I run the example.
    upload_2017-1-16_22-24-3.png

    And the following messages in the console:
    upload_2017-1-16_22-24-48.png

    Also the rotation and camera height doesn't work.
    Did somebody else encountered similar behaviour ?
     
  39. Michal_

    Michal_

    Joined:
    Jan 14, 2015
    Posts:
    357
    It requires compute shaders to function so OpenGL 4.1 and OS X with it isn't supported. It could theoretically work when Unity starts to use Metal instead of OpenGL on Mac.
     
  40. igorbasko

    igorbasko

    Joined:
    Oct 24, 2016
    Posts:
    2
    OK, I see

    Thanks
     
  41. thieum

    thieum

    Joined:
    Apr 8, 2011
    Posts:
    50
    Hi there and thank @Michal_ for your amazing and generous work!
    Tested with Unity V.5.5 and V.5.2 works fine. I already made few adjustments as to make it work with my day/night script, which takes control of sunlight direction and cloud coverage... So I'm near to definitely adopting your solution.

    I have two remaining questions :
    - I'd like to take control over the skyboxcolor when Sunlight is below horizon (i.e. my Sun makes a complete revolution around... my world ;°). Dark blue instead of black. Where can I adjust this?

    - Secondly, in Unity 5.2 both the scene and game views turned sometimes black until I hit "play" again, refreshing the skybox. Is there a workaround for that? Like saving the skybox into an image file OnApplicationQuit()? It would be helpful as to stick with the athmosphere when editing...

    atmospherics.jpg
     
  42. googlebac

    googlebac

    Joined:
    Feb 1, 2015
    Posts:
    56
    Hi, I'm trying to use this in my project but I am coming across an issue with the lighting when the camera turns away from the sun. It becomes very dark. I am using UFPS which utilizes two cameras, one for everything (MainCamera) and one for the rendered weapon.

    Placing Atmospheric Scattering on the Parent Camera causes all transparent objects and textures to not render, pictured below (there should be clouds and dust on the distant mountain)




    Placing it on the child weapon camera causes darkness seen in the images below when I turn away from the sun (note that the transparency remains intact.)





    I'm a bit stuck, Do you know of a solution where I can get transparency to render as well as losing this black haze effect?
    The setup on each camera is in accordance with the usage guide on your github.

    I think one of two solutions may exist although I am unsure how to achieve them:
    Either there may be a way to ensure that the transparent objects were rendered after the atmospheric scattering pass.
    Or there was some way to stop the darkness from appearing.

    Thank you very much for your time.
     
  43. Michal_

    Michal_

    Joined:
    Jan 14, 2015
    Posts:
    357
    @thieum @googlebac No time. I'll get back to you when I can. Hopefully next week.
     
  44. googlebac

    googlebac

    Joined:
    Feb 1, 2015
    Posts:
    56
    Thanks mate
     
  45. googlebac

    googlebac

    Joined:
    Feb 1, 2015
    Posts:
    56
    honestly not sure what I did but its working now. might be something to do with the fog though. Thanks for the work its really looking great now and goes very nicely with my other image effects.
     
  46. Michal_

    Michal_

    Joined:
    Jan 14, 2015
    Posts:
    357
    Thank you.
    You can't. Black color is a result of physics calculations (i.e. absence of incoming light in this case). That's the downside of physics based methods. I guess the easiest way how to do it is to manually change skybox color in the night. AtmosphericScatteringSkybox.shader renders skybox. You could interpolate between atmospheric scattering color and your night color based on a parameter controlled from C# script. Do you know how to do that? I'll walk you through it if you aren't familiar with shader programming.
    It wasn't meant to work outside of play mode. It is a strange coincidence if it does sometimes. It is probably possible to make it work in scene view as well. I'll take a look when I have time. Probably not any time soon though

    Thanks. I think your issue was somehow connected to cascade shadows. Try to disable light shafts if you see it again.
     
    thieum likes this.
  47. thieum

    thieum

    Joined:
    Apr 8, 2011
    Posts:
    50
    1.
    I came to the same conclusion.
    For now I'm just fading in-out the opacity of the sky dome shown above, instead. It does the trick nicely.
    I should try to implement all this in your shader eventually: clouds plane and night sky.
    Note that the regular sunshaft posteffect works well with the clouds plane, worth looking into this direction from within the skybox shader as well.

    2.
    Adding [ExecuteInEditMode] to themain script is almost working as is! We can see the scattering skybox in edit mode and adjust its settings. There's a minor warning about OnDestroy(), which makes me think that a workaround could be not to create a new mat on start, but recycling the existing one. Btw, this function is probably called when cycling between applications, when saving the scene or doing other actions in the editor, that's why the skybox turns black then (i.e. material destroyed). Food for thought.
     
  48. nbac

    nbac

    Joined:
    Jul 7, 2015
    Posts:
    239
    5.6.0.b8 gives me
    error CS0101: The namespace `global::' already contains a definition for `AtmosphericScattering'
    error CS0101: The namespace `global::' already contains a definition for `SunController'
    i got volume lights in the project as well
    any idea?
     
  49. Michal_

    Michal_

    Joined:
    Jan 14, 2015
    Posts:
    357
    Sorry, don't know what this is and I don't have time to investigate. I'll take a look when 5.6 is released. You can try to put the scripts in a namespace in the meantime. Or hope for another beta..
     
  50. nbac

    nbac

    Joined:
    Jul 7, 2015
    Posts:
    239
    this was a stupid mistake by myself sorry for that