Skip to content

Instantly share code, notes, and snippets.

@belzecue
Forked from josephbk117/ColouredFog.shader
Created July 21, 2018 16:27
Show Gist options
  • Save belzecue/e73c1ec0da4cb2fa1661b5bc6dd39591 to your computer and use it in GitHub Desktop.
Save belzecue/e73c1ec0da4cb2fa1661b5bc6dd39591 to your computer and use it in GitHub Desktop.
Screen Space Multi-Coloured Fog - Unity Shader
Shader "BitshiftProductions/ColouredFog"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_ColorLookUp("Fog Colour Look Up", 2D) = "white"{}
_FogSpread("Fog Spread", Float) = 10.0
}
SubShader
{
Cull Off ZWrite Off ZTest Always
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;
float4 scrPos : TEXCOORD1;
};
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.scrPos = ComputeScreenPos(o.vertex);
o.uv = v.uv;
return o;
}
sampler2D _MainTex;
sampler2D _CameraDepthTexture;
sampler1D _ColorLookUp;
half _FogSpread;
#define IF(a, b, c) lerp(b, c, step((fixed) (a), 0));
fixed4 frag (v2f i) : SV_Target
{
fixed3 col = tex2D(_MainTex, i.uv);
float zsample = SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, i.uv);
float depth = Linear01Depth(zsample);
depth = clamp(depth * _FogSpread, 0, 1) * IF(depth < 1.0, 1, 0);
col *= tex1D(_ColorLookUp, depth);
return fixed4(col,1.0);
}
ENDCG
}
}
}
using UnityEngine;
[ExecuteInEditMode, ImageEffectAllowedInSceneView, RequireComponent(typeof(Camera))]
public class ScreenSpaceColouredFog: MonoBehaviour
{
public Material mat;
private void OnRenderImage(RenderTexture src, RenderTexture dest)
{
Graphics.Blit(src, dest, mat);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment