Volumetric Lights for Unity 5
Open source (BSD) extension for built-in Unity lights. It uses ray marching in light's volume to compute volumetric fog. This technique is similar to the one used in Killzone (GPU Pro 5: Volumetric Light Effects in Killzone Shadow Fall by Nathan Vos)
Corresponding thread in Unity Forum can be found here.
I developed this technology for my hobby project. It was never meant for real use and it is therefore little rough around the edges.
You can see the demo on Youtube. Or you can try it for yourself: Binary download
- Volumetric fog effect for multiple lights. Point, spot and directional lights are fully supported.
- Volumetric shadows
- Volumetric light cookies
- Volumetric noise implemented as animated 3D texture.
- Add VolumetricLightRenderer script to your camera and set default cookie texture for spot light.
- Add VolumetricLight script to every light that you want to have volumetric fog.
Volumetric lights will respect standard light's parameters like color, intensity, range, shadows and cookie. There are also additional parameters specific for volumetric lights. For example:
- Sample count - number of raymarching samples (trade quality for performance)
- Scattering Coef - scattering coefficient controls amount of light that is reflected towards camera. Makes the effect stronger.
- Extinction Coef - controls amount of light absorbed or out-scattered with distance. Makes the effect weaker with increasing distance. It also attenuates existing scene color when used with directional lights.
- Skybox Extinction Coef - Only affects directional light. It controls how much skybox is affected by Extinction coefficient. This technique ignores small air particles and decreasing particle density with altitude. That often makes skybox too "foggy". Skybox extinction coefficient can help with it.
- MieG - controls mie scattering (controls how light is reflected with respect to light's direction)
- Height Fog - toggle exponential height fog
- Height Fog Scale - scale height fog
- Noise - enable volumetric noise
- Noise Scale - noise scale
- Noise Speed - noise animation speed
Several sample scenes are part of this project.
Building a standalone player
IMPORTANT - Standalone player will work only if all shaders are included in build! All shaders must be added into "Always Included Shaders" in ProjectSettings/Graphics.
Example scene with different parameters
- Low/High Scattering and Extinction parameters
- Low/High Mie G parameter
- High/Low Shadow Strength. This technique uses single scattering model. That makes areas in shadow unnaturally dark. Use Shadow Strength to "simulate" multiple scattering.
- Height fog with different scale
- High/Low Skybox Extinction parameter
Volumetric fog can be rendered in smaller resolution as an optimization. Set rendering resolution in VolumetricLightRenderer script.
- Full resolution - best quality, poor performance. Serves as a "ground truth".
- Half resolution - best quality/performance ratio.
- Quarter resolution - experimental. Worse quality, best performance.
- Unity 5 (tested on 5.3.4, 5.4 and 5.5)
- DirectX 10/11 or OpenGL 4.1 and above
- Tested on Windows and Mac but it should work on other platforms as well
- VR isn't supported
- Mobile devices aren't supported
Currently requires HDR camera and deferred renderer Currently requires DirectX 11
- Doesn't handle transparent geometry correctly (cutout is ok)
- 3d noise texture is hard coded. VolumetricLightRenderer has custom dds file loader that loads one specific 3d texture (Unity doesn't support 3d textures loaded from file). File "NoiseVolume.bytes" has to be in Resources folder.
- Shadow fading is not implemented
- Parameters and thresholds for bilateral blur and upscale are hard-coded in shaders. Different projects may need different constants. Look into BilateralBlur.shader. There are several constants at the beginning that control downsampling, blur and upsampling.
- Create render target for volumetric light (volume light buffer)
- Use CameraEvent.BeforeLighting to clear volume light buffer
- For every volumetric light render light's volume geometry (sphere, cone, full-screen quad)
- Use LightEvent.AfterShadowMap for shadow casting lights
- Use CameraEvent.BeforeLighting for non-shadow casting lights
- Perform raymarching in light's volume
- Dithering is used to offset ray origin for neighbouring pixels
- Use CameraEvent.AfterLighting to perform depth aware gaussian blur on volume light buffer
- Add volume light buffer to Unity's built-in light buffer
- Light's volume geometry is unnecessarily high poly
- Ray marching is performed in view space and every sample is then transformed to shadow space. It would be better to perform ray marching in both spaces simultaneously.
- Add temporal filter to improve image quality and stability in motion. Change sampling pattern every frame to get more ray marching steps at no additional cost (works only with temporal filter)
- Bilateral blur and upscale can be improved
- I didn't try to optimize the shaders, there is likely room for improvement.
I've been asked about donation button several times now. I don't feel very comfortable about it. I don't need the money. I have a well paid job. It could also give the impression that I would use the money for further development. But that is certainly not the case.
But if you really like it and you want to brighten my day then you can always buy me a little gift. Send me Amazon or Steam gift card and I'll buy myself something shiny. You can find my email in my profile.