Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Join us on Dec 8, 2022, between 7 am & 7 pm EST, in the DOTS Dev Blitz Day 2022 - Q&A forum, Discord, and Unity3D Subreddit to learn more about DOTS directly from the Unity Developers.
    Dismiss Notice
  3. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice

True Volumetric Lights (Now Open Source)

Discussion in 'Shaders' started by Michal_, Mar 10, 2016.

  1. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    11,838
    Good to know! I missed that line on the Adam page.
     
  2. Soul-Challenger

    Soul-Challenger

    Joined:
    Dec 30, 2010
    Posts:
    152
    Yes, basically dim volumetric lights by global fog, + gradually drown the landscape in darkness, thus also avoiding a sharp transition at the far clip plane. This is how it looked with my old, fake particle-based fog + global fog:


    And here is your volumetric fog + global fog:


    Again, your volumetric fog but with added distance-based density, no global fog + directional light volume fog:
     
  3. Drommedhar

    Drommedhar

    Joined:
    Sep 24, 2013
    Posts:
    78
    @Michal_ :
    I put together a fairly simple project, which reproduces the problem.
    Link
    This is what it looks like after starting the scene "DevTest_2":


    Just a short info about the graphics hardware being used on my end (because maybe it's hardware dependent):
    AMD R9 290X
     
  4. JacobK

    JacobK

    Joined:
    Jun 18, 2013
    Posts:
    20
    Tried setting this up, but I just got a completely white screen, even in the demo scene, not sure why.

    Latest unity beta / NVIDIA GTX 970
     
  5. Michal_

    Michal_

    Joined:
    Jan 14, 2015
    Posts:
    365
    Yeah, it doesn't work with 5.4 beta. And I guess there's no point in fixing it now since Unity is likely to ship their own volumetric lights with 5.4. It should be better then this too. It is apparently the same tech as the one used in Assassin's Creed 4 (presentation). It should be faster and it should support transparent geometry.

    I also briefly looked at NVidia's volumetric fog somebody posted here and I like it. It is pretty smart.
     
  6. Drommedhar

    Drommedhar

    Joined:
    Sep 24, 2013
    Posts:
    78
    What was announced and what get's released, just saying ^^
    I would still love to have a version working with Unity 5.4, even if they release their own solution. It's a matter of choice. And to be honest, nobody wants to switch such an system all the time, so people who use your's currently, would maybe rather stay with it then switching to the Unity one (if it sees the light of day). Can't say that's how it will be, but just as a note ;D
    Hope you keep on working on this, as it has great potential, even if Unity does it's own stuff (Open Source etc).
     
  7. elbows

    elbows

    Joined:
    Nov 28, 2009
    Posts:
    2,499
    Another vote for 5.4 fixes here, though I obviously won't hold it against you if you don't want to invest time in that.

    Mostly because I intend to spend a lot more time with the 5.4 beta in the months ahead, months where Unity's own solution isn't going to be available. And when it does arrive, I don't think its yet set in stone as to how much of it will be included with Unity as opposed to being presented in bits of demo project like certain effects were in the Blacksmith demo last time around (such as atmospheric scattering if memory serves me correctly?) I know the possibility of the area lights part being included directly in 5.4 was mentioned by someone intimately involved, but it didn't sound like a decision had been finalised at the time and that doesn't necessarily cover the fog part of the system. Nor do we know how heavy the effect is compared to yours, or various other strengths and weaknesses that will only become apparent after more use of both systems in a wide range of scenarios.
     
  8. Michal_

    Michal_

    Joined:
    Jan 14, 2015
    Posts:
    365
    I tried it on two different computers (both GeForce) but it doesn't happen to me. And I don't think I know anybody with AMD gfx. I'm not sure how to help you now. Maybe try different drivers? I'll think about what you could change in the shader to narrow it down.

    As for the future development and possible 5.4 port. I still want to do some experiments with volumetric lights so I won't abandon it just yet. But you have to be realistic about what you can expect from me. I won't have much time to play with it in the coming weeks
     
  9. elbows

    elbows

    Joined:
    Nov 28, 2009
    Posts:
    2,499
    I've got an AMD 7970. It's not in my computer right now but it will be back in a machine by the weekend.

    I'm not naturally skilled or knowledgeable enough to make this sort of stuff from scratch, but I do get lucky punching above my weight when troubleshooting a fair amount of the time. So if things prove stubborn or time & resources are unavailable, I'm happy to take a stab at both AMD and 5.4 beta issues. Mostly only at weekends though, and I'm certainly not the best man for the job. Want to help where I can though.
     
  10. P1st3ll1

    P1st3ll1

    Joined:
    Dec 13, 2012
    Posts:
    69
    Hey @Michal_ !
    I just tested the source on my scene and I'm getting a full white screen. Do you know how to fix that?
     
  11. Drommedhar

    Drommedhar

    Joined:
    Sep 24, 2013
    Posts:
    78
    I feared it is depending on the hardware. I'm currently running latest stable drivers, so would future customers.
    As a workaround we will stick to point lights for the moment, as the problem does only affect spot lights.

    But thanks for your help. If you have any idea what I should test, just get in touch :-D
     
  12. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,314
    Any chance you tested it using Unity 5.4 beta? If so, that's probably the issue. JakobK reported a white-screen issue in 5.4 already, see post #55 for Michal's answer.
     
  13. Zomby138

    Zomby138

    Joined:
    Nov 3, 2009
    Posts:
    657
    Hello. I tried this out last night and I really like the effect. I've got the black squares and @Beinscheibe 's spot light artifacts.

    But I've also found a problem that I haven't heard anyone else mention. For me, the effect is not working in a release build, it only works in the unity editor :(
     
  14. Michal_

    Michal_

    Joined:
    Jan 14, 2015
    Posts:
    365
    Maybe the shaders were stripped from the build? Do you have all 3 shaders (BilateralBlur, BlitAdd, VolumetricLight) in "Always includeded shaders". Also do you have by any chance AMD gfx?
     
  15. Drommedhar

    Drommedhar

    Joined:
    Sep 24, 2013
    Posts:
    78
    @Zomby138
    As Michal_ said, you need to add the shaders to be "Always included". Was my fault the first time too.
    See here
     
    Last edited: Mar 31, 2016
  16. Zomby138

    Zomby138

    Joined:
    Nov 3, 2009
    Posts:
    657
    You guys were right, adding them as always included shaders makes it work in the build.

    And yes I have an AMD Radeon 7900.
     
  17. Drommedhar

    Drommedhar

    Joined:
    Sep 24, 2013
    Posts:
    78
    So it looks like an AMD related problem with the bug we have...
     
  18. Michal_

    Michal_

    Joined:
    Jan 14, 2015
    Posts:
    365
    It happens only with spotlights? Does it happen when camera is inside spot lights volume? It looks like it is either problem with raymarching or ray/cone intersection test (my bet).
    First find RayMarch function in VolumetricLight shader and make it return a constant. Something like "return float4(0.5, 0.5, 0.5, 1). Does that help?
    And second, you can try to use ray/sphere intersection even for spot light. Look in VolumetricLight shader and replace contents of fragSpotOutside function in pass 3 (line 506) with contents of fragPointOutside function from pass 2 (line 441).
     
  19. Soul-Challenger

    Soul-Challenger

    Joined:
    Dec 30, 2010
    Posts:
    152
    @Michal_ I might be able to avoid using GlobalFog completely and use VolumetricLight on a directional light instead. The only problem is that the generated "fog" is far too bright: the higher the density, the brighter the fog. Would it be possible to increase density without affecting color brightness?
     
  20. Drommedhar

    Drommedhar

    Joined:
    Sep 24, 2013
    Posts:
    78
    The first one (returning a constant value) fixed it for me.
    The second one did nothing.

    So it looks like something is wrong with the RayMarch on AMD.
     
  21. Michal_

    Michal_

    Joined:
    Jan 14, 2015
    Posts:
    365
    Ok, you can modify RayMarch function in several way.
    1. disable dithering
    Code (CSharp):
    1. // replace
    2. float3 currentPosition = rayStart + step * offset;
    3. // with
    4. float3 currentPosition = rayStart;
    2. clamp every ray marching sample
    Code (CSharp):
    1. vlight += max(0, atten * stepSize * _LightColor * _VolumetricLight.x * MieScattering(cosAngle));
    3. Take it apart step by step
    Code (CSharp):
    1. vlight += atten * stepSize * _LightColor * _VolumetricLight.x * MieScattering(cosAngle);
    Delete atten, delete MieScattering, replace stepSize with constant.

    Yeah, the dir light is kind of broken now. It was more proof of concept than actual implementation. It is first on my list though. I need it too. I'd like to have something within a week or so. I'll keep you updated.
     
    elbows likes this.
  22. Soul-Challenger

    Soul-Challenger

    Joined:
    Dec 30, 2010
    Posts:
    152
    OK, thanks Michal :) Sorry to keep feeding you with new issues: found out yesterday, that the system does not support multiple cameras (I've got a second cam for the rear-view mirror). Would there be an easy fix for that?
     
  23. Drommedhar

    Drommedhar

    Joined:
    Sep 24, 2013
    Posts:
    78
    Will check this out this evening and will keep you updated after I tried it out.

    EDIT:
    @Michal_
    I did some testing and changing the shader values did nothing for the better. But I found out an interesting fact: The artefacts are NOT visible on the last light which was activated. So in the demo scene I have 3 lights. If I disable all of them and activate one, it works perfectly. If I activate a second one, the first one get's the artifacts. If I now enable the third one, both other lights have the artifacts.
    I did a small video to show you what I mean:
    Video link
    Maybe you know what might happen here?
     
    Last edited: Apr 1, 2016
  24. Zomby138

    Zomby138

    Joined:
    Nov 3, 2009
    Posts:
    657
    Well that's both weird and interesting!
     
  25. Michal_

    Michal_

    Joined:
    Jan 14, 2015
    Posts:
    365
    That makes little sense to me. Different lights are not connected in any way. If it works when you return a constant and it doesn't work when you leave RayMarch the way it is, then one would say the problem is somewhere between that constant and full implementation. Very strange. I don't know. I'm starting to feel that this form of remote debugging isn't going to cut it. I'll try if it happens with Intel gpus as well
     
  26. Michal_

    Michal_

    Joined:
    Jan 14, 2015
    Posts:
    365
    Little update. I plan to change how volumetric lights work in the near future. Only in-scattering (light reflected towards the camera) is currently simulated (very roughly). That means it only adds light to the scene. That is a reasonable approximation for smaller local lights not too far from the camera (the demo scene it was designed for). But it doesn't work very well for directional lights or lights far from the camera. I'm plan to add out-scattering (light reflected away from the camera) and absorption (light's energy transformed into another form of energy upon collision). I haven't decided how much accurate I want it to be yet but it will be more physically based after that.
    It should be relatively simple change too if command buffers don't surprise me again. I'd like to have something in a week or two. Depending on the amount of bugs QA can find at work ;)
     
  27. Drommedhar

    Drommedhar

    Joined:
    Sep 24, 2013
    Posts:
    78
    Sorry for being not such a big help ;D
    Hope you get hold of this bug sometime ^^
     
  28. RyFridge

    RyFridge

    Joined:
    Apr 7, 2014
    Posts:
    51
    Hey!
    Your example scene looks amazing! I imported all of its content into my project and had this bug come up:
    The volumetric fog disables when there is not collider in the way of the light.

    It must be something about my project, as it does not happen in your example project. The console spams this:

    Dimensions of color surface does not match dimensions of depth surface
    UnityEditor.DockArea:OnGUI()

    colliderBug.gif

    (I put a terrain below, but this is not the problem. And don't wonder about the white block, it's because of the ambient light in the scene.)
     
    Last edited: Apr 4, 2016
  29. Michal_

    Michal_

    Joined:
    Jan 14, 2015
    Posts:
    365
    That is a known limitation in Unity. I forgot to mention it on GitHub. Unity disables shadows if there is no shadow caster inside light's volume and there is no way how to find out if shadows are rendered or not. Volumetric effect is rendered after shadow map gets updated but that never happens if there is no shadow caster. It affects shadow casting lights only.
    You can add tiny shadow casting object (invisible?) under your light as a workaround. It will force the shadow map update every frame.
     
  30. shawnblais

    shawnblais

    Joined:
    Oct 11, 2012
    Posts:
    323
    I don't see anything about volumetric lights in the latest roadmap... I wonder if it's been pushed back. It would be really appreciated if you could get this working for 5.4. We're releasing a title for the HTC Vive in the next few days, and we can't roll back to 5.3, but would love to add some volumetric lights. None of the existing Asset Store versions seem to support VR properly, just sorta hoping yours will. Totally understand if you don't have the time, just thought I'd ask/beg/plead :)
     
  31. Michal_

    Michal_

    Joined:
    Jan 14, 2015
    Posts:
    365
    Fixed rendering with multiple cameras and made it partially work with 5.4 b11 (no noise, no point light w/ shadows...). The non-functional parts are probably bugs in Unity. I'll check some future beta if it is less broken but it is a very low priority for me.

    You're using 5.4 beta and you want to add something new few day before release? That's not for the fainthearted. I come from the world where feature freeze happens months before release :) I would be surprised if it works in VR out of the box but you can try that example scene. I made it partially work in 5.4. And if it works then we can talk about workarounds for remaining 5.4 problems.
    Btw. how is the beta working out for you? I briefly evaluated it for one of our projects and the performance was terrible.
     
    red2blue likes this.
  32. Soul-Challenger

    Soul-Challenger

    Joined:
    Dec 30, 2010
    Posts:
    152
    Cool! Thanks a lot for that :)

    Concerning 5.4 beta, I get horrible frame stuttering. Sticking to 5.3.2f1 for now.
     
  33. DxStd_IgnatPribylov

    DxStd_IgnatPribylov

    Joined:
    Sep 24, 2015
    Posts:
    30
    Cool-Cool-Cool!
    Why 11 beta, rather than 12 or 13. I have a test project to develop a 12-beta (it is most stable on my hardware) at the moment.
    *In a test scene there is nothing.
     
  34. RyFridge

    RyFridge

    Joined:
    Apr 7, 2014
    Posts:
    51
    Thanks for you answer, I enabled shadows for my terrain and now it works :) Looks great! I try around with this a little bit.
     
  35. shawnblais

    shawnblais

    Joined:
    Oct 11, 2012
    Posts:
    323
    Heh, ya, well it's a 2 week project, riff on pong in virtual reality, just to have something up for launch.

    Beta is working pretty well, thing is we upgraded the project in hopes of better VR performance, and then realized that it was basically impossible to roll back to the previous version. I've rolled back versions before without too much trouble, but 5.4 to 5.3, just isn't happening, all the prefabs get broken, and it crashes like crazy, so we're stuck here :(

    Performance seems pretty good, but our game is very simple, so it's hard to say.

    Thanks for getting it running, I'll take another look today and see if we can get it workin in VR.
     
  36. shawnblais

    shawnblais

    Joined:
    Oct 11, 2012
    Posts:
    323
    Hey Michal_, just tested it out, and the volumetrics only render to one of my eyes, so the effect doesn't work in VR. Any chance this is an easy fix?
     
  37. Michal_

    Michal_

    Joined:
    Jan 14, 2015
    Posts:
    365
    No specific reason. I had b11 already installed, so that's why. The example scene doesn't work in b12?

    Sorry, I don't know how VR works in Unity and I don't have the time to experiment with it. Besides I would have to work on it at work since I don't have VR kit myself...
     
  38. elbows

    elbows

    Joined:
    Nov 28, 2009
    Posts:
    2,499
    I'll have a look at the VR issue tonight but no promises.
     
  39. elbows

    elbows

    Joined:
    Nov 28, 2009
    Posts:
    2,499
    Hmm actually I don't fancy having a poke around trying to make stuff work with VR in the 5.4 beta as of right now because of known issues in the Unity release notes for present 5.4 beta version such as:
    • VR: Image effects do not currently work with single pass rendering.
    • VR: Single-Pass-Stereo causes some lighting problems
    • VR: Some built-in shaders do not currently work with single pass rendering resulting in wrong transformations in right eye. Workaround is to manually include the shaders in your project.
    Maybe I will still take a look if you can confirm the problem still exists even with single pass VR rendering disabled. But even then you shouldn't hold out much hope that I can fix this in a timely manner, not least because obviously the volumetric fog code is not my own so my knowledge is lacking in certain areas. And generally the desire for a solid 90fps for VR means that in the broader world of game dev we aren't tending to see too much of 'next gen effects' such as genuine volumetric lighting and fog being talked about in the same content as VR. In many cases it can still happen, but it might not be a top priority for all engines to start with, and I suppose effects that have to do expensive operations twice as a result of the slightly different position of the other eye (e.g. ray marching from camera pos? not sure if this is really an issue though) will be performance-cost prohibitive for longer.
     
  40. elbows

    elbows

    Joined:
    Nov 28, 2009
    Posts:
    2,499
    OK I tried it with a DK2 Oculus Rift using the 1.3.0 Oculus stuff, with Unity 5.4 beta 13 with the unity 1.3.0 plugin from oculus installed into the right place.

    Things I have learnt:

    Yes it only renders in one eye. If I turn on the single pass stereo thing as per the release notes bugs then I get a different problem - its in both eyes but the volumetrics are rendered all wrong (too small or too far away).

    I have found very little/no forum chatter or documentation about command buffers in the context of VR.

    Developers with no VR device can debug certain rendering problems as follows (again I only tried on 5.4 beta 13):
    In project settings->player settings, enable VR.
    In the Virtual Reality SDK's section just below the enable flag, press the plus and add 'Split Stereo Display (non head-mounted)'.
    If there are other things in the SDK section already, eg Oculus, either remove it or drag Split Stereo Display (non head-mounted) to the top of the list.
    Now when you press play you should see a left eye and right eye rendering in your gameview, which is enough to debug certain aspects of VR such as stuff not appearing in one eye at all.

    I came up with a very crude temporary fix which works as long as single pass stereo is disabled:

    Duplicate the camera in the scene. Set one camera to render only to the left eye. Set th other camera to render only to the right eye. The effect then appears in both in VR, but obviously having to use another camera has implications, and I only tested it very briefly with the volumetric light demo scene. It may have other issues.
     
  41. shawnblais

    shawnblais

    Joined:
    Oct 11, 2012
    Posts:
    323
    Ah interesting, ya don't see why that wouldn't work. I'll give it a shot and see. Also, fwiw, I did manage to downgrade to 5.3.4 finally, being back on a stable ground feels good :)
     
  42. Michal_

    Michal_

    Joined:
    Jan 14, 2015
    Posts:
    365
    Little sneak peek of the upcoming update. Directional light in action. Coming soon
     
    VertexSoup, moure, red2blue and 10 others like this.
  43. Zicandar

    Zicandar

    Joined:
    Feb 10, 2014
    Posts:
    388
    Michal_, this is really really amazing! Especially if it's somewhat performant! Any chance you think this could be integrated with a tiled/clustered solution for Unity?
     
  44. Michal_

    Michal_

    Joined:
    Jan 14, 2015
    Posts:
    365
    Thanks! I don't know what you mean by tiled/clustered solution.
     
  45. Zicandar

    Zicandar

    Joined:
    Feb 10, 2014
    Posts:
    388
  46. Michal_

    Michal_

    Joined:
    Jan 14, 2015
    Posts:
    365
    I see. I thought you might be talking about tile-based rendering but I didn't know there is a practical way how to implement one of those in Unity (w/o Unity source code).
    Volumetric light(fog) is more or less an post process effect. It requires depth buffer and list of lights + their shadow maps. As long as you can access these, you can make it work with pretty much any kind of renderer.
     
  47. Dreamaster

    Dreamaster

    Joined:
    Aug 4, 2014
    Posts:
    148
    NICE!! Can't WAIT!
     
  48. Michal_

    Michal_

    Joined:
    Jan 14, 2015
    Posts:
    365
    The update will go live later today. There are few things you should know. First of all, it is a breaking change! You will have to setup your lights again if you decide to update. Some parameters were removed, some new were introduced and some work in different way due to fixed bugs.

    I experimented with several techniques and the more physically correct it is the more difficult it is to setup. Especially for NPR scenes. So I decided to go with simplified technique that gives user more control. I'll update readme on GitHub and I'll add several example scenes to give you some idea how the parameters work.

    Change log:
    • Full support for directional light
    • Different light scattering model (Supports in-scattering, out-scattering and absorption)
    • Various bug fixes
    • Minor optimizations
     
  49. chingwa

    chingwa

    Joined:
    Dec 4, 2009
    Posts:
    3,761
    OMG!
     
    Dreamaster likes this.
  50. Dreamaster

    Dreamaster

    Joined:
    Aug 4, 2014
    Posts:
    148
    LOL... yeah this is going to look KILLER with your water bro. :)