Loic.SlightlyMad.VolumetricLights 1.0.0

dotnet add package Loic.SlightlyMad.VolumetricLights --version 1.0.0
                    
NuGet\Install-Package Loic.SlightlyMad.VolumetricLights -Version 1.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Loic.SlightlyMad.VolumetricLights" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Loic.SlightlyMad.VolumetricLights" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="Loic.SlightlyMad.VolumetricLights" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Loic.SlightlyMad.VolumetricLights --version 1.0.0
                    
#r "nuget: Loic.SlightlyMad.VolumetricLights, 1.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#addin nuget:?package=Loic.SlightlyMad.VolumetricLights&version=1.0.0
                    
Install Loic.SlightlyMad.VolumetricLights as a Cake Addin
#tool nuget:?package=Loic.SlightlyMad.VolumetricLights&version=1.0.0
                    
Install Loic.SlightlyMad.VolumetricLights as a Cake Tool

Volumetric Lights for Unity 5

IMAGE ALT TEXT HERE IMAGE ALT TEXT HERE

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.

Demo Project

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

Features

  • 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.

Usage

  • 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 alternate text is missing from this package README image
  • Low/High Mie G parameter alternate text is missing from this package README image
  • High/Low Shadow Strength. This technique uses single scattering model. That makes areas in shadow unnaturally dark. Use Shadow Strength to "simulate" multiple scattering. alternate text is missing from this package README image
  • Height fog with different scale alternate text is missing from this package README image
  • High/Low Skybox Extinction parameter alternate text is missing from this package README image

Rendering resolution

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.

Requirements

  • 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

Known Limitations

  • 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.

Technique overview

  • 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

Possible improvements

  • 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.

Donations

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.

Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net5.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.0 2 7/31/2021