Giter VIP home page Giter VIP logo

volumetriclights's Introduction

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

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.

volumetriclights's People

Contributors

arrqh avatar kwnetzwelt avatar nxrighthere avatar slightlymad avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

volumetriclights's Issues

Nothing with HD Render Pipeline

Hi. I have used your asset in the development of my game since Unity 5.4. Now, I am moving to HD Render Pipeline, and I can't get your asset working.
What should I do?
Thanks.

Some shaders unaffected by volumetric lights

Materials using certain shaders, transparent shaders in particular, aren't affected by volumetric lights the way others are. In the attached example, you can see that the decals on the wall remain dark, as does a portion of the oil lantern (front left). These using Legacy Shaders/Transparent/Diffuse and Standard Fade, respectively.

Edit: it seems to be related to render queues. Decreasing the render queue for the affected materials seems to fix the issue. I'm wondering if the render queue for the VolumetricLight shader is set too low, though? The issue happens with quite a few default shaders, out-of-the-box. (Unity 5.60f3, FYI)

shot

Rendering issue from certain angles.

@SlightlyMad
If i have shadows enabled for the spotlight, and it doesn't hit any objects, the light stops rendering / turns invisible if viewed from certain angles.

This can be recreated in the spotlight scene, if you rotate the light upwards, then move the camera and the light upwards, at one point it will stop rendering the light, seems like range has something to do with it as well.

Is this a known issue or something new? The rendering's values seems to stay the same, so I couldn't debug it any further.

Multiple cameras

Hi,

I have been using this Unity extention and it's great! But I came across one problem. To avoid gun clipping I have added second camera that draws only player and gun.

So my main camera has depth -1, and my gun camera has depth 1. With that I cannot see cone or any lightning effects. I alo can't add renderrer script to gun camera since it does not render walls.

Is there a way to bypass this problem or should I try different way to fix gun clipping?

Things sometimes look dark (2017.3.1f1)

Objects turn dark when they are at the opposite side of the light source.

Here's everything looking fine (the light source is pointing at the camera):
1

The building is lit perfectly, but the terrain in the background is darkened (the camera is turned 180 degrees):
2

If we get farther from the building, we see it turning black:
3

This is how everything should be lit. No dark areas or shadows.
4

2 errors in Unity 2017.1.1.f1

Hi! I'm getting these 2 errors in Unity 2017.1.1.f1:

  • Assertion failed: Assertion failed on expression: 'IsNormalized(dir)'
  • Assertion failed: Assertion failed on expression: 'IsNormalized(ray.GetDirection())'

And I don't know if this is related to me, using a newer engine version, or just a bug...

Thank you! :)

Does not work on WebGL

[Error] Invalid pass number (2) for Graphics.Blit (Material "Hidden/BilateralBlur" with 1 passes)
VolumetricLightRenderer.OnRenderImage()    Assets/Plugins/VolumetricLightsSlightltyMad/VolumetricLightRenderer.cs:345
344:   // horizontal bilateral blur at half res
-->345:   Graphics.Blit(_halfVolumeLightTexture, temp, _bilateralBlurMaterial, 2);
347:   // vertical bilateral blur at half res

Volumetric lights disabled in built package

It's quite confusing that the asset package has no problem, it runs well both in unity editor and
built application.
But when I import the asset to my own project, everything seems ok in unity editor,
then I built a PC package.
All volumetric light effects disappeared.
I think maybe there is something wrong with my settings in unity.
I'm using 2018.3.9

Won't work on unity 2018.1.0b12 with Lightweight render pipeline

As the title says no error no warning but doesn't work anymore
Also using Lightweight render pipeline. Might be the issue.

Using the lightweight render pipeline 1.1.2 installed from the package manager is confirmed the issue.
I don't know what's wrong there is no warning or error but the lights are for sure non volumetric now.

Resizing Issue

When you resize the window in editor while playing the fog intensity goes up exponentially until the screen is white. Also happens when you hit pause.

Would like to implement no-fog zones

Hi,

Is there a way to add no fog zones in the shader code?

We currently are running some tests, using the height fog as some kind of mist in our scene, and we want one of the character skills to remove some of the mist. The problem is that the skill is done away from the player, and he can move close or away from that zone.

We know it is possible with Ray Marching to add primitive(s) to the calculation and wondered if you would know where in the code we should be looking in order to achieve what we want?

We don't know if that will look realistic but we want to try it ;p

Cheers!

Noise Texture should be 8 channel

Save a bunch of GPU memory, set the 3D texture format to A8. When sampling the texture, be sure to do a .a, i.e.

tex3Dlod(noiseTexture, float4(x,y,z,-999.0)).a

Build a package

Can you build the package so i can put it in my project please?

VR Compatibility

The lights are great but in VR (with an HTC Vive), they are somewhat shifted from the original position in both the right and the left eye but on different positions depending on the eye.

Effect blinks on half a second after camera spawns

I'm having an issue where the effect kind of "blinks" on shortly after my player camera spawns. Is there a way to force the effect on as soon as the camera is instantiated? Or is this a know issue / something is wrong with my setup? Thanks.

Wont work in specific scene

For some reason, In one specific scene, the light just goes like "yeah, i'm not gonna be volumetric" and just fails, leaving a shitstain of disappointment on my entire project, Thinking it my be a problem with my version of unity, i opened an example script and it worked fine. i did literally the exact same things as in the example, and it still doesn't want to work.

edit: oops i fixed it. the range of the light was too goddamn bigg.

Doesn't show up on build

Hey I love your asset it looks great. Unfortunately it doesn't show up when I do a build of the project.
I moved all the shaders and the text asset that contains the dds file to the "Resource" folder so it can find it when it builds.
(shaders that are included via Shader.Find() don't get automatically added to the build)
But although there are no longer any error messages the fog still doesn't show up on the build, however it works fine when i play in editor.

Transparency issues - are they just renderque or more fundamental?

Playing with this volumetric lighting in a project of ours - looks absolutely brilliant, except of course for transparent geometry which looks like it's being rendered later. Is it just a matter of twiddling the shaders' render queues (which I can't seem to do in unity with these compiled shaders), or is there something more fundamental going on?

You can see in this image the water (black bits) seems to be being rendered after the volumetric effect, which is a shame.

volumetric

Error in Unity 2017.1.1p4

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/VolumetricLights/Scripts/VolumetricLightRenderer.cs:354)

Returning black screen in release builds.

I've done some investigation.
Shaders have been explicitly included in the build.
The problem exists in Unity 5.5 as well as newer version. Tested multiple Unity versions upto 2018, starting at 5.5
The problem only occurs in standalone builds and not in the editor.
The problem seems to be the RayMarch function in VolumetricLight.shader returns black with alpha set to 1 for the entire screen.

Not rendering at all, can't explain exactly why

Not rendering at all, can't explain, sorry.

Here are the errors that shows:

Invalid pass number (10) for Graphics.Blit (Material "(Unknown material)" with 1 passes)

Invalid pass number (2) for Graphics.Blit (Material "Hidden/BilateralBlur" with 1 passes) UnityEngine.Graphics:Blit(Texture, RenderTexture, Material, Int32) VolumetricLightRenderer:OnRenderImage(RenderTexture, RenderTexture) (at Assets/Volumetric Lights/Assets/Scripts/VolumetricLightRenderer.cs:337)

Slight problem with RenderTexture

It seems that RenderTextures render the textures a bit darker than they should, as you can see in the screenshots. Is there something I might be doing wrong or is this a bug?

Scene with the VolumetricLightRenderer enabled: (you can see where the RenderTexture is, even though both sides of that visible line should be completely white)
screenshot 72

Scene with VolumetricLightRenderer disabled: (you don't see where the normal object stops and the RenderTexture continues because both are completely white)
screenshot 74

Effect disabled after using light for baking

Hi,

first of all thanks very much for this great stuff!

Here´s my issue:
I use a directional light (Realtime mode) as a sun for my outdoor scene.
I setup the VoluemticLighting component on this light to get some nice godrays using realtime shadows.
I see everything is fine in runtime.

I switch this light to Baked mode for baking some lightmaps.
I switch this light back to realtime mode but the godrays are no longer present in runtime.

I clear baked data and godrays are present again.

Any ideas?

Kind regards,
Carsten

Changing shadow strength on a light affects volumetric shadows on another light

So lets say I have a point light or spot light somewhere with a volumetric light.
This light will have a shadow strength of something less than 1, let's say 0.7;
Now I add a second light with a volumetric light. This one will have a strength of 1.
The second light's volumetric shadow now behaves like it has the shadow strength of the first one.

Also changing the strength of the first light now affects both volumetric lights.
So basically the volumetric lights take the lowest shadow strength of all volumetric lights that are available and uses it for all volumetric shadows in the players view.
It happens with both spot lights and point lights.

0 7

1 0

Effect disabling entirely when no shadows present near camera

Hello!

I'm having an issue that to be quite frank i've been having virtually in all assets of volumetric lights, atmospheric scattering, height fog, global fog, volumetric fog etc...

The effect works great, however whenever there are no shadows being casted near the camera, the effect disables itself entirely.

GIF of problem

Just wanted to know if this is intended behaviour (and by extension intended behaviour of all volumetric effect assets), or if i'm just doing something terribly wrong or anything I might be missing for the proper scene setup, or maybe even if there's something wrong with my GPU drivers.

Is it not work in iOS?

I tried but received this message:

Invalid pass number (2) for Graphics.Blit (Material "Hidden/BilateralBlur" with 1 passes)
UnityEngine.Graphics:Blit(Texture, RenderTexture, Material, Int32)
VolumetricLightRenderer:OnRenderImage(RenderTexture, RenderTexture) (at Assets/Scripts/VolumetricLightRenderer.cs:337)

Compiling Problem

Hi,SlightlyMad.
I've been testing this project on a PS4 console.However,there were tons of problems showed up.I managed to handle some of them by converting
float4 centerColor = _MainTex.Sample(sampler_MainTex,uv);
into
float4 centerColor = UNITY_SAMPLE_TEX2D(_MainTex,uv);.
Solved Problems(Image Below):
image
But there are still some issues that drive me mad.(Image Below):
image
I can sense that the problems are caused by PS4 sampler state,sort of.But I can't get any answer or information on the web.
Would you please give me some hints?
Much appreciated.
BTW,sorry for my poor English.(^_^)

Camera Flip

For some reason when i hit play camera flips 180 and mirrors the image horizontal
1
2
3

This also happens on example scenes that VL provides

I get an error when playing

this is it :
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 Light/Scripts/VolumetricLightRenderer.cs:363)
I'm using Unity5.6.7
any help ?

Error when used with XBOX ONE as UWP App

Maybe the shader wasn't meant to run on the XBOX One. But I just wanted to inform that it gives this error ["Invalid pass number (1) for Graphics.Blit (Material "Hidden/BilateralBlur" with 1 passes"] when run on the xbox one ...

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.