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