1
2 Shader "Hidden/PrepareSunShaftsBlur" {
3 Properties {
4 _MainTex ("Base", 2D) = "" {}
5 _Skybox ("Skybox", 2D) = "" {}
6 }
7
8 CGINCLUDE
9
10 #include "UnityCG.cginc"
11
12 struct v2f {
13 float4 pos : SV_POSITION;
14 float2 uv : TEXCOORD0;
15 };
16
17 sampler2D _MainTex;
18 sampler2D _Skybox;
19 sampler2D_float _CameraDepthTexture;
20
21 uniform half _NoSkyBoxMask;
22 uniform half4 _SunPosition;
23
24 v2f vert (appdata_img v) {
25 v2f o;
26 o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
27 o.uv = v.texcoord.xy;
28 return o;
29 }
30
31 half TransformColor (half4 skyboxValue) {
32 return max (skyboxValue.a, _NoSkyBoxMask * dot (skyboxValue.rgb, float3 (0.59,0.3,0.11)));
33 }
34
35 half4 frag (v2f i) : SV_Target {
36 float depthSample = SAMPLE_DEPTH_TEXTURE( _CameraDepthTexture, i.uv.xy);
37 half4 tex = tex2D (_MainTex, i.uv.xy);
38
39 depthSample = Linear01Depth (depthSample);
40
41 // consider maximum radius
42 half2 vec = _SunPosition.xy - i.uv.xy;
43 half dist = saturate (_SunPosition.w - length (vec.xy));
44
45 half4 outColor = 0;
46
47 // consider shafts blockers
48 if (depthSample > 0.99)
49 outColor = TransformColor (tex) * dist;
50
51 return outColor;
52 }
53
54 half4 fragNoDepthNeeded (v2f i) : SV_Target {
55 float4 sky = (tex2D (_Skybox, i.uv.xy));
56 float4 tex = (tex2D (_MainTex, i.uv.xy));
57
58 // consider maximum radius
59 half2 vec = _SunPosition.xy - i.uv.xy;
60 half dist = saturate (_SunPosition.w - length (vec));
61
62 half4 outColor = 0;
63
64 if (Luminance ( abs(sky.rgb - tex.rgb)) < 0.2)
65 outColor = TransformColor (sky) * dist;
66
67 return outColor;
68 }
69
70 ENDCG
71
72 Subshader {
73 Pass {
74 ZTest Always Cull Off ZWrite Off
75
76 CGPROGRAM
77
78 #pragma vertex vert
79 #pragma fragment frag
80
81 ENDCG
82 }
83 Pass {
84 ZTest Always Cull Off ZWrite Off
85
86 CGPROGRAM
87
88 #pragma vertex vert
89 #pragma fragment fragNoDepthNeeded
90
91 ENDCG
92 }
93 }
94
95 Fallback off
96
97 } // shader