Skip to content

Instantly share code, notes, and snippets.

@GeorgiyRyaposov
Created November 21, 2017 01:16
Show Gist options
  • Save GeorgiyRyaposov/75ea4e9821cb04e72bb9ed158b944c96 to your computer and use it in GitHub Desktop.
Save GeorgiyRyaposov/75ea4e9821cb04e72bb9ed158b944c96 to your computer and use it in GitHub Desktop.
// Upgrade NOTE: replaced '_Object2World' with 'unity_ObjectToWorld'
Shader "Custom/FogShader"
{
Properties
{
_MainTex("Fog texture", 2D) = "white" {}
_MainTex2("Fog texture 2", 2D) = "white" {}
_FogColorFactor("Fog color factor", Float) = 1
_FogScroll("Fog scroll speed", vector) = (0,0,0,0)
_FogMap("Fog map", 2D) = "white" {}
_NoiseTex("Noise texture", 2D) = "white" {}
_NoiseTex2("Noise texture 2", 2D) = "white" {}
_NoiseScroll("Noise offset speed", vector) = (0,0,0,0)
_NoiseFactor("Noise factor", Float) = 0.005
//_BlurAmount("Blur Amount", Range(0,02)) = 0.0005
}
SubShader
{
Tags{ "Queue" = "Transparent" "IgnoreProjector" = "True" "RenderType" = "Transparent" }
//LOD 100
ZWrite Off
Blend SrcAlpha OneMinusSrcAlpha
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float4 uv : TEXCOORD0;
float4 screenpos : TEXCOORD2;
float4 vertex : SV_POSITION;
float4 noiseUv : TEXCOORD3;
};
sampler2D _MainTex;
float4 _MainTex_ST;
sampler2D _MainTex2;
float4 _MainTex2_ST;
sampler2D _FogMap;
float _NoiseFactor;
sampler2D _NoiseTex;
float4 _NoiseTex_ST;
sampler2D _NoiseTex2;
float4 _NoiseTex2_ST;
float4 _NoiseScroll;
float4 _FogScroll;
float _FogColorFactor;
//float _BlurAmount;
v2f vert(appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv.xy = TRANSFORM_TEX(v.uv, _MainTex);
o.uv.zw = TRANSFORM_TEX(v.uv, _MainTex2);
o.uv -= _Time.yyyy * _FogScroll;
o.screenpos = ComputeScreenPos(o.vertex);
float2 xz = mul(unity_ObjectToWorld, v.vertex).xz;
float2 noiseUv = TRANSFORM_TEX(xz, _NoiseTex);
float2 noiseUv2 = TRANSFORM_TEX(xz, _NoiseTex2);
o.noiseUv = float4(noiseUv, noiseUv2);
o.noiseUv -= _Time.yyyy * _NoiseScroll;
return o;
}
fixed4 frag(v2f i) : SV_Target
{
fixed4 col = tex2D(_MainTex, i.uv.xy) * tex2D(_MainTex2, i.uv.zw) * _FogColorFactor;
float2 maskUv = i.screenpos.xy / i.screenpos.w;
half2 noise = tex2D(_NoiseTex, i.noiseUv.xy).rg;
half2 noise2 = tex2D(_NoiseTex2, i.noiseUv.zw).rg;
half2 noiseMix = noise * noise2;
maskUv += (noiseMix * 2 - 1) * _NoiseFactor;
//no blur
col.a *= (1 - tex2D(_FogMap, maskUv).r);
//blur
//half fogcol = half(0);
//float remaining = 1.0f;
//float coef = 1.0;
//float fI = 0;
//for (int j = 0; j < 3; j++) {
// fI++;
// coef *= 0.32;
// fogcol += (1 - tex2D(_FogMap, float2(maskUv.x, maskUv.y - fI * _BlurAmount))) * coef;
// fogcol += (1 - tex2D(_FogMap, float2(maskUv.x - fI * _BlurAmount, maskUv.y))) * coef;
// fogcol += (1 - tex2D(_FogMap, float2(maskUv.x + fI * _BlurAmount, maskUv.y))) * coef;
// fogcol += (1 - tex2D(_FogMap, float2(maskUv.x, maskUv.y + fI * _BlurAmount))) * coef;
// remaining -= 4 * coef;
//}
//col.a *= fogcol + (1 - tex2D(_FogMap, float2(maskUv.x, maskUv.y))) * remaining;
return col;
}
ENDCG
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment