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 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 | |
} | |
} | |
} |
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
- [GreaseMonkey] Unity Forum Fixer
- UnityHub: Make Hub application background Translucent
- Customize SpriteShapeRenderer quality (but has issues)
- Editor tool: Copy selected gameobject’s names into clipboard as rows (for Excel)
- Editor tool: Replace string in selected gameobject’s names
- UnityHub: Enable built-in Login Dialog (no more browser login/logout issues!)
- Use TikTok-TTS in Unity (with WebRequest)
- Create Scene Thumbnail Image using OnSceneSaved & OnPreviewGUI
- webgl+javascript TTS
- Using Moonsharp (LUA) + Unity Webgl
- Using 3D gameobject prefabs with Unity Tilemap + NavMesh Surface
- Custom Unity Hub Project Template Preview Image/Video (using HTML+CSS in package description)
Recent Comments
- Vector3 maths for dummies! on
- UnityHub: Make Hub application background Translucent on
- UnityHub: Make Hub application background Translucent on
- Install Android SDK+JDK+NDK for Unity (without AndroidStudio or Unity Hub) on
- Install Android SDK+JDK+NDK for Unity (without AndroidStudio or Unity Hub) on
- [Asset Store] Point Cloud Viewer & Tools on
- [Asset Store] Point Cloud Viewer & Tools on
- ffmpeg stream raw video into Unity Texture2D on