23
2016
Volumetric Image Effect Shader

Converted this old godrays shader ( http://unitycoder.com/blog/2012/10/02/fake-godrays-shader/ ) into image effect – interesting results! (see video below)
Source codes below (Attach script to main camera)
This file contains hidden or 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 hidden or 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 | |
| } | |
| } | |
| } |
Scene has just few spotlights, particles, and 3d objects..

—
References:
Image effects tutorial – http://www.alanzucconi.com/2015/07/08/screen-shaders-and-postprocessing-effects-in-unity3d/
Related Posts
Leave a comment
Recent posts
- Favorites in PackageManager
- LudumDare59 : Signal
- Unity Editor: Tree Generator
- Leaf/Foliage Generator Tools (Runs in Browser)
- Testing Unity AI Beta
- Ways to Support UnityCoder Development
- Using UI Slider to Create 5-Star Rating Element
- Game Music Library For Unity (editor plugin)
- Fontastic : Easily Test Fonts in Unity Editor!
- GeoTiff Importer & Terrain Generator for Unity
- Create Baked DropShadow for UI images
- .JP2 Ortho Image Converter to PNG/JPG/TIFF
Recent Comments
- on Mesh Exploder (sources)
- on Sprite Sheet Flip Book Shader
- on Sprite Sheet Flip Book Shader
- on [Asset Store] PolygonCollider2D Optimizer
- on Trajectory Test Scene 2.0
- on Vector3 maths for dummies!
- on UnityHub 3.6.0: Remove Version Control & Cloud Dashboard columns
- on Using RenderDoc with Unity (graphics debugger)
Coin:
CUgDSbRqFcAumDSAcdKDvuXsw26VdkJe8C8WGUQHBAGS
An article by











