Volumetric Light Scattering as a Post-Process

Hey folks, I’m lost and could use some direction with something I’m working on. I’m trying to implement Chapter 13 of GPU Gems 3 which as the thread title suggests is about godrays.

I’m new to writing shaders, I’m still going over the docs but I’m definitely lost. Here’s the shader and accompanying script.

Shader "VolumetricLightScattering"
{
	Properties
	{
		frameSampler ("Base (RGB)", RECT) = "white" {}
		ScreenLightPos("Light Position", Vector) = (0,0,0,0)
		Density("Density", Float) = 0
		
		Weight("Weight", Float) = 0
		illuminationDecay("Illumination Decay", Float) = 0
		Decay("Decay", Float) = 0
		Exposure("Exposure", Float) = 0
	}
	
	SubShader
	{
	
		Pass
		{

			//Tags{"Queue" = "Transparent"}
			Cull front
			//Color (0,0,0,1)
			//Blend One One
			
			CGPROGRAM
			#pragma multi_compile_builtin
			#pragma fragment frag
			#include "UnityCG.cginc"

			
			uniform sampler2D frameSampler;
			float4 ScreenLightPos;
			float Density;
			
			float Weight;
			float illuminationDecay;
			float Decay;
			float Exposure;

			float4 frag(float2 texCoord : TEXCOORD0) : COLOR0
			{
			
				float NUM_SAMPLES = 5;
				half2 deltaTexCoord = (texCoord - ScreenLightPos.xy);
				deltaTexCoord *= 1.0f / NUM_SAMPLES * Density;
			
				half3 color = tex2D(frameSampler, texCoord);
				half illuminationDecay = 1.0f;
				//for(int i = 0; i < NUM_SAMPLES; i++)
				for(int i = 0; i < 5; i++)
				{
					texCoord -= deltaTexCoord;	
					half3 sample = tex2D(frameSampler, texCoord);
					sample *= illuminationDecay * Weight;
					color += sample;
					illuminationDecay *= Decay;
				}
				
				return float4(color * Exposure, 1);
			}
			ENDCG
		}
		Pass
		{
		 Blend One One
		}
	}
	FallBack off
}

Script:

using UnityEngine;
using System.Collections;


[RequireComponent(typeof(Camera))]
public class VolumetricLightScattering : ImageEffectBase {

    public Vector3 ScreenLightPos;
    
    public float Density = 1;
    public float Weight = 1;
    public float illuminationDecay = 0.5f;
    public float Decay = 1;
    public float Exposure = 1;

    public Transform Sun;
    private RenderTexture accumTexture;

	

    void OnRenderImage(RenderTexture source, RenderTexture destination)
    {
        this.ScreenLightPos = camera.WorldToScreenPoint(this.Sun.position);
        material.SetVector("Light Position", this.ScreenLightPos);
        material.SetFloat("Density", this.Density);
        material.SetFloat("Weight", this.Weight);
        material.SetFloat("Decay", this.Decay);
        material.SetFloat("Exposure", this.Exposure);
        material.SetTexture("frameSampler", source);
        
        Graphics.Blit(source, destination, material);
    }
}

In the chapter it says you should render all objects as black as a prepass then combine the processed image with a regularly rendered image. I’ve tried using multiple passes but I can’t seem to get anything to work. I was hoping this would be a nice simple shader to start with but either I was wrong or I am very bad at shaders lol

The effect that’s coming out right now is below.

Thanks for any and all help.

Cheers,
Ryan

There’s a godray implementation in this thread that you might find useful for reference.

Thanks for the reply :slight_smile:

I’d seen that but it didn’t look like it was tailing the same approach. I know it said that it was using the same reference but the method (aside from the IP algorithm) looked really different. I’ll take another look at the code though, after some experimenting I think I’ll b better able to understand it.

While I’m here I’ll update my pogress.

I think the there is a space mismatch, one of the camera and shader go from [0,1] and the other has a domain of [-1,1]. Can’t remember offhand which one is which, I think the shader has the latter.

After reading the article more carefully I saw I need to render the background with the light then render the occluding geometry black over that. Next I need to do the godray IP then blend normal render with the processed image.

Put another way, lots to do. I’ll post updates.