Skip to content

Instantly share code, notes, and snippets.

@zombiemachines
Forked from baba-s/HlslFog.shader
Created May 27, 2019 02:33
Show Gist options
  • Select an option

  • Save zombiemachines/2efb1417921bf0588e2093575c54b623 to your computer and use it in GitHub Desktop.

Select an option

Save zombiemachines/2efb1417921bf0588e2093575c54b623 to your computer and use it in GitHub Desktop.
Shader "Custom/HlslFog"
{
Properties
{
_MainTex ("Base (RGB)", 2D) = "white" { }
_FogColor ("Fog Color (RGB)", Color) = (0.5, 0.5, 0.5, 1.0)
_FogStart ("Fog Start", Float) = 0.0
_FogEnd ("Fog End", Float) = 10.0
}
SubShader
{
Tags { "RenderType" = "Opaque" }
Fog
{
Mode off
}
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
sampler2D _MainTex;
float4 _FogColor;
float _FogStart;
float _FogEnd;
struct appdata
{
float4 vertex: POSITION;
float4 texcoord: TEXCOORD0;
};
struct v2f
{
float4 pos: SV_POSITION;
float4 uv: TEXCOORD0;
float fog: TEXCOORD1;
};
v2f vert(appdata v)
{
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
o.uv = v.texcoord;
float fogz = mul(UNITY_MATRIX_MV, v.vertex).z;
o.fog = saturate((fogz + _FogStart) / (_FogStart - _FogEnd));
return o;
}
half4 frag(v2f i): COLOR
{
return tex2D(_MainTex, i.uv.xy) + lerp(0, _FogColor, i.fog);
}
ENDCG
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment