Search Unity

  1. Unity 2019.1 beta is now available.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. We're looking for insight from anyone who has experience with game testing to help us better Unity. Take our survey here. If chosen to participate you'll be entered into a sweepstake to win an Amazon gift card.
    Dismiss Notice
  4. On February 28th the Feedback website will shut down and be redirected to the Unity forums. See the full post for more information.
    Dismiss Notice
  5. Want to provide direct feedback to the Unity team? Join the Unity Advisory Panel.
    Dismiss Notice
  6. Unity 2018.3 is now released.
    Dismiss Notice
  7. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice

Post-Process Godrays In Unity

Discussion in 'Made With Unity' started by Texel, Dec 31, 2009.

  1. Texel

    Texel

    Joined:
    Dec 25, 2009
    Posts:
    61



    This does require Unity Pro.

    (Links updated with fixes)
    Webplayer build is hosted at:
    http://dl.dropbox.com/u/82486/GodraysExample.html
    (Project file can be found here)
    Controls are LMB/CTRL to change the angle, ALT/RMB to change the level of detail. Move using Mouse/WASD/ArrowKeys.

    Godrays from GPU Gems 3, implemented for both SM 2.0 and SM 3.0 hardware

    Basically, it takes the alpha-channel of your image, and pulls everything away from the sun. As such, it is not compatible with the default glow effect, aswell as any other effects which make use of the alpha channel. To use, Simply set anything that would be emissive to show on the alpha channel (example shaders for this are in the project file), then set the sun location on the script.

    The traditional implementation breaks down as your view approaches grazing angles, so to compensate for that I've allowed it to falloff and automatically disable when it would be visually irrelevant. One of the settings on the script allows further refinement of the range it fades at, to better configure the effect. At lower levels of detail or increasingly grazing angles, it will appear fragmented, to correct this, either use a higher level of detail or change the "density" parameter in the script. Weight controls the overall visibility of the effect, where Exposure will dim the rest of the screen as you look at the lightsource, giving a pseudo-HDR effect.

    The Controls for the effect are:
    Density: Proximity of samples taken, higher makes the effect more spread out but may cause fragmenting
    Weight: Brightness of the effect, lower makes it less visible, higher may overexpose
    Decay: Falloff for the effect, lower makes it fade more rapidly
    Exposure: Brightness of the scene relative to rays

    It should run successfully on any DX9.0 level card, but requires Shadermodel 3.0 to efficiently take a good number of samples.

    System requirements should be minimum Geforce FX 5200/Radeon 9500 to display anything at all, Geforce 6100/ATI x1300 or HD Series in order to display it in full Pixelshader 3.0 goodness.

    The shader has three levels of detail, aswell as a Pixelshader 2.0 fallback, the main difference between them being the number of samples per pass, and the number of passes run- performance wise, it's really just fillrate.

    I have not tested this on anything except OSX, so I don't know if it will work on Windows.

    Please let me know how it runs, I haven't been able to test it as much as I'd have liked.

    Edit

    Managed to track down the issue to a difference between how OpenGL and DirectX handle blending and texture coordinates. Sped up the PS2.0 fallback slightly, and I've now allowed the density to decrease with the view angle, which limits visible fragmenting of the rays. Demo and project file updated, aswell as attachment.
     

    Attached Files:

  2. MatthewJCollins

    MatthewJCollins

    Joined:
    Dec 3, 2009
    Posts:
    372
    The images look beautiful! Awesome work!

    Unfortunately I can't get the webplayer to work properly. I have an ATI HD4850 card and am running Windows XP Pro.
     
  3. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    31,939
    Cool effect. Runs well on my G5 + Radeon X800, although it looks fairly blocky, which is to be expected given SM 2.0.

    --Eric
     
  4. the_gnoblin

    the_gnoblin

    Joined:
    Jan 10, 2009
    Posts:
    722
    Does not work on Windows at all :(. Tested on 3 different machines.
     
  5. bigkahuna

    bigkahuna

    Joined:
    Apr 30, 2006
    Posts:
    5,435
    Same results here: it worked on my MacBook Pro (SM 2.0) but not on my Dell Vostro notebook. Very cool effect though, would love to see this working on Windows also.
     
  6. KaelisAsur

    KaelisAsur

    Joined:
    Apr 9, 2009
    Posts:
    361
    Whoa, spectacular.

    But yes, does not work on Windows.
     
  7. maxfax2009

    maxfax2009

    Joined:
    Feb 4, 2009
    Posts:
    410
    does not work on Windows Vista :(
     
  8. CoatlGames

    CoatlGames

    Joined:
    Apr 25, 2008
    Posts:
    773
    doesnt work on windows 7 either, i have a geforce 9600 gs
     
  9. Discord

    Discord

    Joined:
    Mar 19, 2009
    Posts:
    1,002
    Worked on my Mac mini at work. Haven't tried on my windows pc at home but I'm assuming it won't work. :p The effect does look a little blocky at one of the settings, but looks good overall. Would be great if it worked on Windows!
     
  10. pgpprogram

    pgpprogram

    Joined:
    Oct 9, 2009
    Posts:
    153
    WOW-Awesome!!!!!
     
  11. Texel

    Texel

    Joined:
    Dec 25, 2009
    Posts:
    61
    Updated demo, project file, and unitypackage, should now work properly on both DirectX and OpenGL. I'm not sure if it will appear as bright on both OpenGL and DirectX, if somebody with the proper setup could compare to make sure it is consistent, I'd be grateful.
     
  12. bigkahuna

    bigkahuna

    Joined:
    Apr 30, 2006
    Posts:
    5,435
    Texel - I think you should get the award for "best first post on a forum"! This is really a great addition to the community!

    I'll test this on my PC later today, but this version runs about the same on my Macbook Pro.

    I have a question, actually two:

    a. Is there a way to get this to "ignore" some colliders and only only use others?

    b. I wonder what methods might be used to animate the God rays besides moving the light?
     
  13. minevr

    minevr

    Joined:
    Mar 4, 2008
    Posts:
    1,015
    Take a look, Fire!!! 8)
     
  14. Red Ocktober

    Red Ocktober

    Joined:
    Oct 28, 2005
    Posts:
    84
    web player works fine on my win vista setup with geforce8600GT...

    nice effects...

    --Mike
     
  15. Neodrop

    Neodrop

    Joined:
    Oct 24, 2008
    Posts:
    1,358
    Thank you for sharing!
     
  16. Thomas-Pasieka

    Thomas-Pasieka

    Moderator

    Joined:
    Sep 19, 2005
    Posts:
    2,088
    Not bad but seems to be rather "expensive/taxing" on the hardware. Surely a good idea though but not sure I'd use it in a game when i can do it the old fashioned way.
     
  17. Texel

    Texel

    Joined:
    Dec 25, 2009
    Posts:
    61
    A. The effect is not based on colliders, but is calculated as a post process from what is visible on your screen. It has some faults because of that, such as rays only being cast by objects that are 'white' on the alpha channel, and being blocked by objects that are 'black'- I had to cheat a bit in order to get trees to cast volume-shadows, but for the most part you can use colormask with shaderlab to set something block it. The tropical Island Demo is setup to work with Glow (where bright things are white on the alpha channel), so you can use that as an example of how to configure your scene.

    B. Moving the light is, for the effect, a free way of doing it. Another easy way would be to vary the alpha value for the material color (most builtin materials will output texture * color)- You could also modulate the alpha value for the sky by sliding another texture across it, such as sliding a cloud layer across the sky, and having the light pass through them. I'd wanted to try having a subway car and sliding the lightray source along the side of the car (You can have multiple sources by adding additional instances of the script, but the fillrate costs add up fast), and sliding them down the length, and it should work with dark particle effects, like a column of smoke from a fire.

    It's taxing in the same way that Screen-Space Ambient Occlusion is, being expensive primarily to the fillrate, and consuming proportionately more fillrate in order to deliver a smoother effect. Newer graphics cards are equipped well enough to achieve most of these effects 'for free', where some of the older cards will have issues keeping up. While it's better to manually place geometry because of the precondition that the source of the rays must be visible on screen, it is challenging to handled animated objects and volumetric shadowing, not to mention saving the time on creating all these meshes. I would like to reduce the fillrate hit more, making it a cheaper effect, but I was having some issues implementing it, and I'll need to rewrite most of it to make it any cheaper. I also had a few issues since Unity wouldn't let me limit the shader level of detail for post-process shaders, so I've had to use a messy alternative- I do plan to make it cheaper though, certainly.
     
  18. Texel

    Texel

    Joined:
    Dec 25, 2009
    Posts:
    61
    Rewrote the effect to now use rendertarget 'buffers' to render the effect and impose it back over the original image



    Instead of running a single multipass shader over the image to generate the godrays, this version will render the effect to a buffer, then that buffer will be added to the 'summation buffer' to be imposed onto the final image. Now, instead of having the number of passes determined by the level of detail setting, you can instead specify the number of passes for it to render into the summation buffer. The big difference with this version, however, is that it is that godrays are being sampled from a reduced buffer and are thus independent of screen resolution, and can be adjusted for speed/performance more easily.

    Another nice bonus is that it now only uses a single PS2.0 shader, so it shouldn't look as chunky on older hardware.

    Because the effect is being rendered and -then- imposed over the background, rounding errors from repeated passes are far less evident, and the only thing that should be affected by rounding errors would be the colors of the rays, rather then the color for the entire scene.

    So, for new settings, we have

    TargetSize: Size for rendertarget buffers, smaller is faster but less accurate
    PassCount: Number of passes to render, more has a smoother effect but leads to inaccuracies in rounding
    QualityFiltering: Uses better filtering on the rendertexture, more expensive, if the texture is a power of two, it should be able to use trilinear (Couldn't confirm this worked, however)
    QualityBuffers: Use ARGBHalf (16-bit floating point per pixel) accuracy for rendering the effect, more expensive but doesn't have the same rounding isssues
    DebugGodrays: View only the godrays, not the scene
    SmoothAddition: SmoothAdd rather then standard addition for blending the rays, making them approach white slower then additive blending
    DimmingFalloff: Falloff for the fading of the godrays on approach to grazing angles.

    If people could confirm that this version works across multiple configurations or can suggest anything else to add, I'd be much obliged.
     

    Attached Files:

  19. Texel

    Texel

    Joined:
    Dec 25, 2009
    Posts:
    61
    Apologies for the repeated postings, I was sufferring connection issues at the time, if a moderator could clean it up I'd be much obligded.
     

    Attached Files:

  20. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    4,488
    hi texel,
    really nice work – but i have some "popping" instead of fade in / fade out of the god rays on rotating. it also hides the gui text when inactive due to viewing angle and turns it on again when active?
    (all settings are the original ones from the script...)

    webplayer: http://dl.dropbox.com/u/2322017/vtl_05.html

    lars
     
  21. Texel

    Texel

    Joined:
    Dec 25, 2009
    Posts:
    61
    larsbertram, it worked perfectly fine on my OSX machine (though the density seemed rather low, the rays seemed to be fine to me, though the source being below the horizon seemed a little off), and on my Windows (3850HD) when the godrays were visible, the scene was upside down, and when combined with glow the godrays would appear with proper alignment, but not the scene itself. Didn't experience your issue with the GUIText appearing/disappearing however... Can I ask which version of the script you are using? (The one on the front, or the one I have here on the second page)
     
  22. Zante

    Zante

    Joined:
    Mar 29, 2008
    Posts:
    347
    Looks great but, in the picture I've included, the rays seem to be coming off very bright objects for some reason too. It's behaving like a glow effect.

    The 'sun' is outside the window, the walls should occlude everything but, for some reason, the rays come off the lights too, as well as through the windows.

    Running on Windows XP with an Nvidia Quadro FX1500m (laptop)

    Edit: Ah, it's controlled by alpha.
     

    Attached Files:

  23. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    4,488
    hi texel, i use the script from the second page of the thread.
     
  24. Texel

    Texel

    Joined:
    Dec 25, 2009
    Posts:
    61
    In order to find out where the light is being shown from, the shader takes the alpha values for your scene as the emission. Try setting the view for scene mode to "Alpha", anything that is black is going to act as a blocker, versus anything white which is going to cast the rays. Try changing the alpha value of the materials for all objects so they are black on the alpha channel- Again, you can see this done in the Island demo, aswell as in the Sewer example.


    Larsbertram, I'm thinking it might have something to do with using the GL class, I couldn't get some of the old blendmodes (like add and addsmooth) to work with Graphics.Blit, so I used ImageEffects.Blit, which makes me think it may have something to do with a GL class inconsistency. If I can figure out how to use the old fixed-function blends with Graphics.Blit, maybe that will fix it. Until then, the script on the first page will probably be the best
     
  25. Zante

    Zante

    Joined:
    Mar 29, 2008
    Posts:
    347
    Texel, thanks for the explanation. Is there any way to get the effect/script to work with layers?

    I need my alpha as my glow threshold effects require high values to work.
     
  26. Texel

    Texel

    Joined:
    Dec 25, 2009
    Posts:
    61
    Zante, I have an alternative glow effect I'd written (that includes Gamma, tone, and vignette) that I find I have been able to use to get most of my glow effects to work without needing to use the alpha channel- The alternative is that you use a shader replacement technique, but that would require you to draw the scene geometry multiple times. Alternatively you could try to 'split' the alpha channel to have the 0-127 range dedicated to godrays and the 128-255 to glow, I can try to write that up for you if the attached Package isn't an adequate replacement for your glow.
     

    Attached Files:

  27. Zante

    Zante

    Joined:
    Mar 29, 2008
    Posts:
    347
    Texel, for me, your glow effect only works when I enable real-time ambient occlusion. Is there something else I'm supposed to do? :]

    The range trick sounds promising, does much have to be edited?
     
  28. Texel

    Texel

    Joined:
    Dec 25, 2009
    Posts:
    61
    Been getting weird issues with using the old imageEffects functions it seems, I wonder if they really did break the GL class with 2.6

    After looking at the current builtin glow shader, it's unclear how I'd apply the change (they adjust for alpha in a fixed function section which also does the downsampling, and with the basic blends not working right I can't easily rewrite that)

    I updated the fake HDR glow to handle other 2.6 changes though, and removed the seemingly problematic GL class
     

    Attached Files:

  29. lazalong

    lazalong

    Joined:
    Oct 13, 2009
    Posts:
    138
    Hey thanks for your work.

    Here is what I have with an X300 (hence shader 2) on winxp. So it's not too bad as it is better in movement.

     
  30. Zante

    Zante

    Joined:
    Mar 29, 2008
    Posts:
    347
    Just messing around with it, again brilliant work.

    If we could take out the requirement of needing the sun position and have the rays emitted in the direction of the face, that'd be awesome. As you can see in the shot, I've had to move the 'sun' into a place where it causes all the rays to aim at the ground.

    Equal distribution based on face orientation would be great, is this possible?

     
  31. SavaB

    SavaB

    Joined:
    Feb 11, 2010
    Posts:
    39
    i can't seem to get this project working in unity... even the example project doesn't seem to do anything but display the inside of a church. The only thing i noticed is that when i run it, the godray script disables itself..
     
  32. cannon

    cannon

    Joined:
    Jun 5, 2009
    Posts:
    751
    It probably means your hardware doesn't support it. Most render texture effect scripts disable themselves once they find they can't initialize the resources they need.
     
  33. SavaB

    SavaB

    Joined:
    Feb 11, 2010
    Posts:
    39
    that's what i was first thinking. But because the browser example did work, i figured it has to be something else.

    I'm running windows vista on a fairly new, very good laptop.
     
  34. Nikolay116

    Nikolay116

    Joined:
    Mar 21, 2010
    Posts:
    421
    can I make god rays work with particles somehow, more specifically mesh particle emitter?
    thanks
     
  35. Nikolay116

    Nikolay116

    Joined:
    Mar 21, 2010
    Posts:
    421
    Hello,

    I am using package266 and unity 2.6.1. I have noticed, that it works ok in the editor, but fails to work in webplayer or standalone.
    Does anybody have this problem too?
     
  36. HectorFPi

    HectorFPi

    Joined:
    Jun 6, 2011
    Posts:
    10
    Hi

    First, thanks for the job
    And now, i have tried to use the glow effect but unity says that some fails.


    Ciao
     
  37. Texel

    Texel

    Joined:
    Dec 25, 2009
    Posts:
    61
    Howdy KFR, I wrote this effect back for Unity 2.6, as of Unity3.0 there is an official Godrays effect you can find under Assets -> Import Package -> Image Effects (Pro Only) that is using the same technique as mine. After that one had come out, I stopped maintaining this. If you still want to play with it, the particular change there is from TEXCORD to texcoord, aswell removing the pixel scaling code which was outdated by Unity's switching of RECT to 2D.
     
  38. HectorFPi

    HectorFPi

    Joined:
    Jun 6, 2011
    Posts:
    10
    Hi

    Thanks for the fast reply ^^ about the effect that comes with unity, is the first time that i read about it and i'm going to look for it ;)

    Edit: Would be sunshafts and if it is combined with bloom and flares, the result is very good
     
    Last edited: Jun 15, 2011
  39. HectorFPi

    HectorFPi

    Joined:
    Jun 6, 2011
    Posts:
    10
    I have done a test and the result is quite good, here is the video of it.

    But i don't know really how to make the texture or i dont understand something about the godrays effect.
    Does the material have to be unlit-transparent/cutout, no? and the effect is achieved by using the alpha channel of the texture...no? xD
     
  40. rumblemonkey

    rumblemonkey

    Joined:
    Mar 25, 2011
    Posts:
    280
    Ran on ATi HD4850. Needs more gaussian fuzz on the final pass, imo.
     
  41. dreamerflyer

    dreamerflyer

    Joined:
    Jun 11, 2011
    Posts:
    887
  42. moinchdog

    moinchdog

    Joined:
    Apr 4, 2012
    Posts:
    15
    i couldnt get the script to work it had to modify the Shader.Find section and move it to void Start ()

    you might want to fix that

    the inbuilt Sun Shafts image effect with unit is much easier to use.
     
  43. Brian-Sinasac

    Brian-Sinasac

    Joined:
    Sep 8, 2012
    Posts:
    95
    Looks really nice! Great work.