Created
March 23, 2016 15:36
-
-
Save unitycoder/399da67db426f20c04f3 to your computer and use it in GitHub Desktop.
VolumetricImageEffect - Image Effect Script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
[ExecuteInEditMode] | |
public class VolumetricImageEffect : MonoBehaviour | |
{ | |
public float exposure=0.6f; | |
public float decay = 0.95f; | |
public float density = 0.96f; | |
public float weight = 0.4f; | |
public float clamp = 1f; | |
public int samples = 16; | |
private Material material; | |
// Creates a private material used to the effect | |
void Awake() | |
{ | |
material = new Material(Shader.Find("Hidden/VolumetricLightApproximation")); | |
} | |
// Postprocess the image | |
void OnRenderImage(RenderTexture source, RenderTexture destination) | |
{ | |
if (material == null) | |
{ | |
material = new Material(Shader.Find("Hidden/VolumetricLightApproximation")); | |
} | |
material.SetFloat("fExposure", exposure); | |
material.SetFloat("fDecay", density); | |
material.SetFloat("fDensity", density); | |
material.SetFloat("fWeight", weight); | |
material.SetFloat("fClamp", clamp); | |
material.SetInt("fSamples", samples); | |
Graphics.Blit(source, destination, material); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// original source: http://demo.bkcore.com/threejs/webgl_tron_godrays.html | |
// converted to surface shader : http://unitycoder.com/blog/2012/10/02/fake-godrays-shader/ | |
// converted to image effect : | |
Shader "Hidden/VolumetricLightApproximation" | |
{ | |
Properties | |
{ | |
_MainTex ("Texture", 2D) = "white" { | |
} | |
fExposure ("fExposure", Float) = 0.6 | |
fDecay ("fDecay", Float) = 0.93 | |
fDensity ("fDensity", Float) = 0.96 | |
fWeight ("fWeight", Float) = 0.4 | |
fClamp ("fClamp", Float) = 1.0 | |
fSamples ("fSamples", Int) = 32 | |
} | |
SubShader | |
{ | |
// No culling or depth | |
Cull Off ZWrite Off ZTest Always | |
Blend SrcAlpha OneMinusSrcAlpha // Traditional transparency | |
Pass | |
{ | |
CGPROGRAM | |
#pragma vertex vert | |
#pragma fragment frag | |
#include "UnityCG.cginc" | |
struct appdata | |
{ | |
float4 vertex : POSITION; | |
float2 uv : TEXCOORD0; | |
}; | |
struct v2f | |
{ | |
float2 uv : TEXCOORD0; | |
float4 vertex : SV_POSITION; | |
}; | |
v2f vert (appdata v) | |
{ | |
v2f o; | |
o.vertex = mul(UNITY_MATRIX_MVP, v.vertex); | |
o.uv = v.uv; | |
return o; | |
} | |
sampler2D _MainTex; | |
float fX,fY,fExposure,fDecay,fDensity,fWeight,fClamp; | |
int fSamples; | |
fixed4 frag (v2f i) : SV_Target | |
{ | |
float2 vUv = i.uv; | |
float2 deltaTextCoord = float2(vUv - float2(0.5,0.5)); | |
deltaTextCoord *= 1.0 / float(fSamples) * fDensity; | |
float2 coord = vUv; | |
float illuminationDecay = 1.0; | |
float4 FragColor = float4(0,0,0,0); | |
for(int i=0; i < fSamples ; i++) | |
{ | |
coord -= deltaTextCoord; | |
float4 texel = tex2D(_MainTex, coord); | |
texel *= illuminationDecay * fWeight; | |
FragColor += texel; | |
illuminationDecay *= fDecay; | |
} | |
FragColor *= fExposure; | |
FragColor = clamp(FragColor, 0.0, fClamp); | |
float4 c = FragColor; | |
return c; | |
} | |
ENDCG | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment