Search Unity

True Volumetric Lights (Now Open Source)

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

  1. Michal_

    Michal_

    Joined:
    Jan 14, 2015
    Posts:
    365
    Yeah, transparent object aren't supported. It requires depth buffer and transparent object don't write into depth buffer. There is no easy way around it. I can explain in more detail but it is not a trivial change.
    The easiest way is to buy a volumetric lights plugin from the asset store. It support transparent objects. There's a link in one of the post above.
     
    Martin_H likes this.
  2. Assembler-Maze

    Assembler-Maze

    Joined:
    Jan 6, 2016
    Posts:
    630
    I see. I think there's a quick workaround, drawing the object with multiple passes, one that is alpha tested and writes to the depth buffer only.

    But I also encounter some weird light leaks through geometry. Any ideas on those?
     
    Alverik likes this.
  3. Lexie

    Lexie

    Joined:
    Dec 7, 2012
    Posts:
    646
    The only way to get transparency support is to render out a 3D grid of the volumetric data, and then have the transparent objects sample that to estimate how much light is behind/infront. It's not a simple task.
     
    shkar-noori likes this.
  4. Michal_

    Michal_

    Joined:
    Jan 14, 2015
    Posts:
    365
    Sorry, I don't know what you're talking about. Could you make a repro project?
     
  5. Assembler-Maze

    Assembler-Maze

    Joined:
    Jan 6, 2016
    Posts:
    630
    Sure, I'll let you know if it happens again, but with screens/demo project this time.
     
  6. thirdcoke

    thirdcoke

    Joined:
    Nov 2, 2015
    Posts:
    8
    Hello ! Thanks for the package but the directional light doesn't work on my Mac(OpenGL 4.1) and the same on Windows Unity Editor with any of the OpenGL API levels( but spotlight works actually). Does anyone get the same issue like mine?
     
  7. ArchVisual

    ArchVisual

    Joined:
    Jun 24, 2013
    Posts:
    8
    This looks fantastic in deferred, awesome job!

    However, Ive tried this with unity 5.4.1 and my camera is flipped when using forward rendering.

    Any ideas why this is happening?
     
    Last edited: Oct 28, 2016
  8. LazyAnt

    LazyAnt

    Joined:
    Jul 14, 2015
    Posts:
    7
    The example scenes no longer work in 5.4.1. There is no volumetric light and the screen is pink.
     
  9. Powelus

    Powelus

    Joined:
    Aug 3, 2012
    Posts:
    2
    I'm loving this, it really add tons of aesthetics to my work
    final.jpg
     
    ftejada, Kridian and Michal_ like this.
  10. Michal_

    Michal_

    Joined:
    Jan 14, 2015
    Posts:
    365
    Ok, I don't have a Mac and Unity with Opengl on Windows is very unstable for me. I tried to debug it for a moment and SV_VertexId semantic isn't working properly. At least not on my pc. Directional light depends on it. It is probably driver or Unity bug. I would guess it is a Unity bug, if the same thing happens on Mac. It is be possible to store vertex Id in vertex buffer as a workaround but I don't plan to do it myself.

    Haven't seen that. What platform? I'll take a look if you tell me how to reproduce it or send me repro project.

    Well, it is working for me. What platform are you using? There must be some errors in console or Unity log if the screen is pink.
     
  11. LazyAnt

    LazyAnt

    Joined:
    Jul 14, 2015
    Posts:
    7
    I'm using Windows 10. I have Unity 5.4.1. I open up the project, update the project to 5.4.1 when prompted and then I open the 'example' scene (the error occurs on all other scenes too). Below I have images of what happens.

    The console:


    The editor:


    In the Editor log I basically get these two errors shown above:

    D3D shader create error for vertex shader [0x80070057]
    Shader warning in 'Hidden/BilateralBlur': use of potentially uninitialized variable (BilateralUpsample) at line 157 (on d3d11)

    ---

    Compiling Fragment program
    Platform defines: UNITY_ENABLE_REFLECTION_BUFFERS UNITY_PBS_USE_BRDF1 UNITY_SPECCUBE_BOX_PROJECTION UNITY_SPECCUBE_BLENDING
    (Filename: BilateralBlur Line: 157)
     
    Last edited: Nov 3, 2016
  12. Michal_

    Michal_

    Joined:
    Jan 14, 2015
    Posts:
    365
    It looks like Unity decided to hide the error from us. Anyway, I see you're using DX10 class GPU. That's likely the problem. One of the shaders is using Texture.Gather() function and that apparently requires shader model 4.1. I guess you GPU doesn't support it. I think I could try to add fallback pass but I have no way to test it. I'll think about it...
     
  13. LazyAnt

    LazyAnt

    Joined:
    Jul 14, 2015
    Posts:
    7
    Yeah, my computer is pretty old.

    If you want to go ahead with this, I could test it for you if it's possible.
     
  14. Michal_

    Michal_

    Joined:
    Jan 14, 2015
    Posts:
    365
    Can you try using this commit? It should be DX10 compatible. It requires deferred renderer and there are one or two bugs that are fixed in latest version but it should work otherwise. I'll add the fallback if you're still interested in it after trying it.
     
    Alverik likes this.
  15. LazyAnt

    LazyAnt

    Joined:
    Jul 14, 2015
    Posts:
    7
    Sorry for taking so long to getting around to this.

    I'm afraid it still doesn't seem to be working. I get the pink overlay, and the lights don't have volume. Rendering path is 'Deferred' and the camera is HDR. I'm using the 'example' scene on the Example Project you provided, and I've substituted the VolumetricLight script with the one here.

    By the way, you don't have to try and make this work. I have an older computer than most. I am not even using Unity for anything right now. I just found this cool tool you made and decided to check it out. :)

    If you want to fix this, I will help test it out, but most probably it won't affect others. Thanks for taking time for this, appreciated.
     
  16. Michal_

    Michal_

    Joined:
    Jan 14, 2015
    Posts:
    365
    Sorry, that's not what I meant. Please take all files from the '2e9e2a6' revision and not just the VolumetricLight script. Either use git to go back to that commit or simply download it here. I'm curious myself if it will work.
     
    Alverik and LazyAnt like this.
  17. LazyAnt

    LazyAnt

    Joined:
    Jul 14, 2015
    Posts:
    7
    Ah, I see. I didn't think this through, I just jumped in, apologies.

    It works now. Thanks.

    As a young programmer, I'm really impressed by your work here. I hope one day I will be able to do cool stuff like that. I know this sounds really sappy and weird, but I thought I should let you know. Keep up the great work, you are awesome. :)
     
    Michal_, Martin_H and Alverik like this.
  18. Michal_

    Michal_

    Joined:
    Jan 14, 2015
    Posts:
    365
    Well, thank you. You know, in a way, that's how I got where I am today. I was impressed by somebody else's work (I still am) and I wanted to do those great things myself. It was always part of my motivation. So I'm really happy if I can do the same thing for others...

    Alright, does anybody want me to bring DX10 compatibility back? Or are we good?
     
    Alverik and LazyAnt like this.
  19. Alverik

    Alverik

    Joined:
    Apr 15, 2016
    Posts:
    417
    well, I'd love to have it too :) Is there a way to have both options in the same package or would it need to be two independent packages?

    Anyway, I also think like LazyAnts, your work is very impressive and inspiring, specially for an amateur like me, heh.
     
  20. Michal_

    Michal_

    Joined:
    Jan 14, 2015
    Posts:
    365
    I should be able to add it as a fallback. I'll see what I can do.
     
    Alverik likes this.
  21. LazyAnt

    LazyAnt

    Joined:
    Jul 14, 2015
    Posts:
    7
    I have a little suggestion.

    In the VolumetricLight script, the variable ScatteringCoef is initialized at 0.1f. I have found that this is too low and the volume of the light is not immediately noticeable. I think it should be cranked up to something in the range of 0.7-0.9 so users won't have to fiddle around with values to get to see the script in effect.
     
    Alverik likes this.
  22. Alverik

    Alverik

    Joined:
    Apr 15, 2016
    Posts:
    417
    By the way, I've been testing the volumetric lights in one of my projects (and it looks awesome by the way :) ) but I've noticed that the fog continues to show up even if the light component in the object it's attached to is off. Is that intended or have I done something wrong? or do I always need to disable the volumetric light component at the same time as the light component so that everything goes into total darkness? even though the fog/mist in the semi darkness looks uber cool, I was just wondering if that's how it's supposed to work?

    Anyway, one way or another the volumetric lights look great. So thanks!
     
  23. Michal_

    Michal_

    Joined:
    Jan 14, 2015
    Posts:
    365
    I added DX10 fallback pass. I didn't find a way how to test it so let me know. I also found a workaround for the OpenGL and directional light problem (@thirdcoke in case you're still interested).

    I see your point. The problem is that the coefficient is amount of scattering per unit length. In other words in depends on light's "size". 0.1 is ok for large lights like directional light and it is too little for small local lights. It will always be a bad default value for some users. Maybe local lights are more common and I should change it? I don't know

    Not intended I guess. I didn't think about it to be honest. I used it for my little tech demo and I didn't really think about other use-cases. I think you're right. I'll fix it
     
    thirdcoke, Alverik and Martin_H like this.
  24. LazyAnt

    LazyAnt

    Joined:
    Jul 14, 2015
    Posts:
    7
    Ah, I see then. It is up to you. Maybe increase it just a bit, for a compromise of sorts?
     
    Alverik likes this.
  25. ManuelCosta

    ManuelCosta

    Joined:
    Dec 24, 2015
    Posts:
    9
    Im trying it out on unity 5.5,and when using directional light setup the array "_frustumCorners" gives me an exception (out of range)
     
  26. Michal_

    Michal_

    Joined:
    Jan 14, 2015
    Posts:
    365
    Fixed. It looks like spot light shadows aren't working in 5.5 though. I'll take a look when I can. I'm too busy lately.
     
    Alverik likes this.
  27. Lexie

    Lexie

    Joined:
    Dec 7, 2012
    Posts:
    646
    Just so you know they flipped the depth buffer in 5.5...That's why they don't work.
     
    Michal_ likes this.
  28. Michal_

    Michal_

    Joined:
    Jan 14, 2015
    Posts:
    365
    Thank you Lexie. That's very helpful. Reversed Z buffer is probably the last thing I would suspect. Is there a list of affected platforms somewhere? I assume it doesn't apply to platforms that have nothing to gain.

    I'll make a quick fix tomorrow if it is this simple.
     
    Alverik and Martin_H like this.
  29. Michal_

    Michal_

    Joined:
    Jan 14, 2015
    Posts:
    365
    Ok, I didn't find any documentation but I found this in built-in shaders.
    Code (CSharp):
    1. #if defined(SHADER_API_D3D11) || defined(SHADER_API_PSSL) || defined(SHADER_API_XBOXONE) || defined(SHADER_API_METAL) || defined(SHADER_API_VULKAN)
    2. #define UNITY_REVERSED_Z 1
    3. #endif
    It is interesting they decided to leave OpengGL 4.5 behind. That would benefit as well. Looks like OpenGL is indeed dead and Vulkan/Metal will replace it.

    Anyway, 5.5 is now fully supported as far as I can tell. Also, @Alverik, Volumetric fog is now turned off when you disable light component.
     
    Alverik, Martin_H and Lex4art like this.
  30. Martin_H

    Martin_H

    Joined:
    Jul 11, 2015
    Posts:
    4,436
    I just wanted to quickly drop in and mention that I think I found the optimal use for the effect in my specific project. I have an RTS style perspective onto the battlefield and originally I thought I'll just give the sun volumetric lighting to have everything cast volumetric shadows to look big and epic. But the shadow effect was rather subtle from that perspective and the light made the entire scene look foggy in a way that I could approximate cheaper with a global fog image effect. But then I had the idea to try out volumetric light effects on explosions instead of the sun and it looks super cool to blow something up and then have volumetric light shine through the flying debris! As a bonus this looks perfectly fine with pretty much the lowest quality settings. Compared between the two usecases that I tried I feel I'm getting a more impressive effect for a lower performance cost in my usecase scenario. Thanks for still maintaining this awesome project!
     
    Michal_, Alverik and Arganth like this.
  31. Alverik

    Alverik

    Joined:
    Apr 15, 2016
    Posts:
    417
    Super, thanks!
     
  32. OsmiousH

    OsmiousH

    Joined:
    Aug 16, 2016
    Posts:
    6
  33. Michal_

    Michal_

    Joined:
    Jan 14, 2015
    Posts:
    365
    Martin_H likes this.
  34. OsmiousH

    OsmiousH

    Joined:
    Aug 16, 2016
    Posts:
    6
    Ok I'll use 5.5 I guess:D:D:D Sorry for the trouble ;)
     
    Michal_ likes this.
  35. Michal_

    Michal_

    Joined:
    Jan 14, 2015
    Posts:
    365
    Thanks :) There is new beta version every week or two and making sure they're all compatible gets old quickly...
     
  36. Alverik

    Alverik

    Joined:
    Apr 15, 2016
    Posts:
    417
    Yeah,true. But I can understand OsmiousH. I also can't wait for Vulkan support. The wait for Unity 5.6 is going to feel eternal this time, lol.
     
  37. Michal_

    Michal_

    Joined:
    Jan 14, 2015
    Posts:
    365
    I guess I could make it compatible with one specific beta version for the impatient :). But not this year
     
    Alverik and jason-fisher like this.
  38. FeeeshMeister

    FeeeshMeister

    Joined:
    Jul 5, 2016
    Posts:
    1
    @Michal_ Thank you for this wonderful asset. I don't know if you still read this thread, but is commercial use allowed for this?
     
  39. Martin_H

    Martin_H

    Joined:
    Jul 11, 2015
    Posts:
    4,436
  40. guillaumepauli

    guillaumepauli

    Joined:
    Jan 3, 2017
    Posts:
    6
    Hello there... after searching for weeks i got 2 errors when I import all the package into my project.

    First error is - Invalid pass number (4) for Graphics.Blit. Material=Sandbox/VolumetricLight, npasses=1
    Second error is
    Trying to access pass 4, but material 'Sandbox/VolumetricLight' subshader (0) has only 1 valid passes.
    UnityEngine.Material:SetPass(Int32)
    VolumetricLight:SetupDirectionalLight(VolumetricLightRenderer, Matrix4x4) (at Assets/Volumetric Lights/Scripts/VolumetricLight.cs:382)
    VolumetricLight:VolumetricLightRenderer_PreRenderEvent(VolumetricLightRenderer, Matrix4x4) (at Assets/Volumetric Lights/Scripts/VolumetricLight.cs:190)
    VolumetricLightRenderer:OnPreRender() (at Assets/Volumetric Lights/Scripts/VolumetricLightRenderer.cs:310)

    thank you for your answer :)
     
  41. Baldinoboy

    Baldinoboy

    Joined:
    Apr 14, 2012
    Posts:
    1,526
    What version of Unity are you using?
     
  42. Michal_

    Michal_

    Joined:
    Jan 14, 2015
    Posts:
    365
    Some of the shaders didn't compile. I guess your platform, Unity version or HW isn't supported. What platform/unity/hw are you using?
     
  43. Martin_H

    Martin_H

    Joined:
    Jul 11, 2015
    Posts:
    4,436
    @Michal_ I just updated to the newest version from the repository, but I'm still occasionally getting this error. I can't reproduce or pin down what's causing it either (not very helpful, I know :-/ ). Do you have a recommendation on where I could start looking to fix this? Since I'm using the volumetric lights for explosions I'm creating and destroying many of them. Does that maybe have something to do with it? I can't make it happen more consistently through instantiating more explosions though... Any ideas?
     
  44. Michal_

    Michal_

    Joined:
    Jan 14, 2015
    Posts:
    365
    Strange. It looks like you destroy a light but it is still registered to VolumetricLightRenderer.PreRenderEvent. Light should deregister itself in VolumetricLight.OnDisable. And I believe OnDisable is called before object is destroyed. You can try two things.
    1. Try to deregister also in OnDestroy. Add
    Code (CSharp):
    1. void OnDestroy()
    2. {
    3.      VolumetricLightRenderer.PreRenderEvent -= VolumetricLightRenderer_PreRenderEvent;
    4.  
    5.      Destroy(_material);
    6. }
    2. Check if light isn't null and optionally deregister light if it is
    Code (CSharp):
    1. private void VolumetricLightRenderer_PreRenderEvent(VolumetricLightRenderer renderer, Matrix4x4 viewProj)
    2. {
    3.      if(_light == null || _light.gameObject == null)
    4.      {
    5.             VolumetricLightRenderer.PreRenderEvent -= VolumetricLightRenderer_PreRenderEvent;
    6.             return;
    7.      }
    8.  
    9.      ...
    10. }
     
    Alverik and Martin_H like this.
  45. Martin_H

    Martin_H

    Joined:
    Jul 11, 2015
    Posts:
    4,436
    Thank you so much for the amazingly quick fix! I tried both things you suggested, the first one didn't seem to fix it and I got he error again fairly quickly. I then also added the second piece of code and I tried for a few minutes and haven't been able to reproduce the error. I'm hopefull this has solved it. Thanks a lot for all your great work!
     
    Michal_ and Alverik like this.
  46. G3R1

    G3R1

    Joined:
    Sep 3, 2016
    Posts:
    33
    @Michal_
    Thank you for the volumetric light that you have developed so far.
    I have tested your volumetric lights in my game scene before and never had a problem or an error, but when i tried to use volumetric lights at one of my scenes that i have been working on for 1 month there seems to occur an error and the scene stops and doesn't run, neither do volumetric lights work. In my scene I have 8 spotlights and wanted to make only 2 of them volumetric. When tried to do so the following errors occured :

    1- "RenderingCommandBuffer: invalid pass index 3 in DrawMesh"
    2- "RenderingCommandBuffer: invalid pass index 1 in DrawMesh"
    3- "Invalid pass number (1) for Graphics.Blit (Material "Hidden/BilateralBlur" with 1 passes)
    UnityEngine.Graphics:Blit(Texture, RenderTexture, Material, Int32)
    VolumetricLightRenderer:OnRenderImage(RenderTexture, RenderTexture) (at Assets/Volumetric Lights Complete/Scripts/VolumetricLightRenderer.cs:354)"

    Also, when i go to the inspector of the shader "BilateralBlur " and "VolumetricLight" a warning sign is shown saying :
    "Shader is not supported on this GPU".

    Everything works fine if create a new scene and add a spolight to test the volumetric light. The problem is only with the current scene that i am working with and I don't want to start over my scene again. Hope you fix these errors.
    Thank you Michal.
     
  47. G3R1

    G3R1

    Joined:
    Sep 3, 2016
    Posts:
    33
    @Michal_
    I think I found why it gives me this error when i try to run the game.
    It happens every time I switch the build settings from PC, etc to Ios or Android. When I switch back to Pc build settings the error dissapears.
    Can you please make it work for Android and Ios too ?
    You would do help all of us alot.
    Thank's anyway for this cool thing you made.
    If you have a fix for this please let us now here. ;)
     
  48. Michal_

    Michal_

    Joined:
    Jan 14, 2015
    Posts:
    365
    I'm sorry, but I'm not going to spend my free time trying to make it run on mobile devices. I seriously doubt it would be practical anyway. It is an expensive effect. Best suited for consoles and PCs.
    You can ask @Lexie if his volumetric lights support mobile devices but I doubt it.
     
    Martin_H likes this.
  49. G3R1

    G3R1

    Joined:
    Sep 3, 2016
    Posts:
    33
    To me it doesn't look that much expensive effect to run on mobile devices though, always based on the profiler and testing it on the game scene I'm making. Anyway, its a cool effect in fact that's why I asked you to try and develop something similar for mobile devices.

    Thank's for your reply.
     
  50. ZoneOfTanks

    ZoneOfTanks

    Joined:
    Aug 10, 2014
    Posts:
    128
    Hi! Great asset!
    But I have this warning in Unity 5.3.7:

    Shader warning in 'Hidden/BilateralBlur': use of potentially uninitialized variable (BilateralUpsample) at line 171 (on d3d11)

    Compiling Fragment program
    Platform defines: UNITY_ENABLE_REFLECTION_BUFFERS UNITY_PBS_USE_BRDF1 UNITY_SPECCUBE_BOX_PROJECTION UNITY_SPECCUBE_BLENDING

    Please fix it ;)