Skip to content

Commit 2feb990

Browse files
authored
Create VerticalCameraDistanceFade.shader
1 parent 4238918 commit 2feb990

File tree

1 file changed

+127
-0
lines changed

1 file changed

+127
-0
lines changed
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
// similar to height fog effect, but starts fading from camera.Y distance (with some offset)
2+
// modified standard shader, with custom lighting pass to make faded part completely black
3+
4+
Shader "UnityLibrary/Standard/Fade/VerticalCameraDistance"
5+
{
6+
Properties
7+
{
8+
_Color("Color", Color) = (1,1,1,1)
9+
_MainTex("Albedo (RGB)", 2D) = "white" {}
10+
//_BumpMap("Normalmap", 2D) = "bump" {}
11+
_Glossiness("Smoothness", Range(0,1)) = 0.5
12+
_Metallic("Metallic", Range(0,1)) = 0.0
13+
_FadeToColor("FadeToColor", Color) = (0,0,0,1)
14+
_FadeStartY("FadeStartFromCameraY", Float) = -2
15+
_FadeEndY("FadeEndFromCameraY", Float) = -4
16+
}
17+
18+
SubShader
19+
{
20+
Tags { "RenderType" = "Opaque" }
21+
LOD 200
22+
23+
CGPROGRAM
24+
25+
// #pragma surface surf Standard fullforwardshadows vertex:vert
26+
#pragma surface surf Custom fullforwardshadows // vertex:vert
27+
#include "UnityPBSLighting.cginc"
28+
29+
struct appdata {
30+
float4 vertex : POSITION;
31+
float4 tangent : TANGENT;
32+
float3 normal : NORMAL;
33+
float2 texcoord : TEXCOORD0;
34+
float2 texcoord1 : TEXCOORD1;
35+
float2 texcoord2 : TEXCOORD2;
36+
};
37+
38+
struct Input {
39+
float2 uv_MainTex;
40+
//float2 uv_BumpMap;
41+
float3 worldPos;
42+
};
43+
44+
// Metallic workflow
45+
// modified from UnityPBSLighting.cginc
46+
struct SurfaceOutputStandardCustom
47+
{
48+
fixed3 Albedo; // base (diffuse or specular) color
49+
float3 Normal; // tangent space normal, if written
50+
half3 Emission;
51+
half Metallic; // 0=non-metal, 1=metal
52+
half Smoothness; // 0=rough, 1=smooth
53+
half Occlusion; // occlusion (default 1)
54+
fixed Alpha; // alpha for transparencies
55+
float Fader; // test
56+
};
57+
58+
sampler2D _MainTex;
59+
//sampler2D _BumpMap;
60+
61+
half _Glossiness;
62+
half _Metallic;
63+
fixed4 _Color;
64+
fixed4 _FadeToColor;
65+
float _FadeStartY;
66+
float _FadeEndY;
67+
68+
float remap(float source, float sourceFrom, float sourceTo, float targetFrom, float targetTo)
69+
{
70+
return targetFrom + (source - sourceFrom)*(targetTo - targetFrom) / (sourceTo - sourceFrom);
71+
}
72+
73+
// from UnityPBSLighting.cginc
74+
inline half4 LightingCustom(SurfaceOutputStandardCustom s, float3 viewDir, UnityGI gi)
75+
{
76+
s.Normal = normalize(s.Normal);
77+
78+
half oneMinusReflectivity;
79+
half3 specColor;
80+
s.Albedo = DiffuseAndSpecularFromMetallic(s.Albedo, s.Metallic, /*out*/ specColor, /*out*/ oneMinusReflectivity);
81+
82+
// shader relies on pre-multiply alpha-blend (_SrcBlend = One, _DstBlend = OneMinusSrcAlpha)
83+
// this is necessary to handle transparency in physically correct way - only diffuse component gets affected by alpha
84+
half outputAlpha;
85+
s.Albedo = PreMultiplyAlpha(s.Albedo, s.Alpha, oneMinusReflectivity, /*out*/ outputAlpha);
86+
87+
half4 c = UNITY_BRDF_PBS(s.Albedo, specColor, oneMinusReflectivity, s.Smoothness, s.Normal, viewDir, gi.light, gi.indirect);
88+
c.a = outputAlpha;
89+
90+
// make it black if outside range
91+
return c*(1 - s.Fader);
92+
}
93+
94+
inline void LightingCustom_GI(SurfaceOutputStandardCustom s,UnityGIInput data,inout UnityGI gi)
95+
{
96+
#if defined(UNITY_PASS_DEFERRED) && UNITY_ENABLE_REFLECTION_BUFFERS
97+
gi = UnityGlobalIllumination(data, s.Occlusion, s.Normal);
98+
#else
99+
Unity_GlossyEnvironmentData g = UnityGlossyEnvironmentSetup(s.Smoothness, data.worldViewDir, s.Normal, lerp(unity_ColorSpaceDielectricSpec.rgb, s.Albedo, s.Metallic));
100+
gi = UnityGlobalIllumination(data, s.Occlusion, s.Normal, g);
101+
#endif
102+
}
103+
104+
void surf(Input IN, inout SurfaceOutputStandardCustom o)
105+
{
106+
fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
107+
108+
// fade
109+
float fadeStartY = _WorldSpaceCameraPos.y + _FadeStartY;
110+
float fadeEndY = _WorldSpaceCameraPos.y + _FadeEndY;
111+
float pixelY = clamp(IN.worldPos.y, fadeEndY, fadeStartY);
112+
float distanceY = remap(pixelY, fadeStartY, fadeEndY, 0, 1);
113+
c.rgb = lerp(c.rgb, _FadeToColor, distanceY);
114+
115+
o.Fader = distanceY;
116+
o.Albedo = c.rgb;
117+
// Metallic and smoothness come from slider variables
118+
o.Metallic = _Metallic;
119+
o.Smoothness = _Glossiness;
120+
//o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
121+
o.Alpha = c.a;
122+
123+
}
124+
ENDCG
125+
}
126+
FallBack "Diffuse"
127+
}

0 commit comments

Comments
 (0)