SunShaftsComposite.shader 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. // Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
  2. Shader "Hidden/SunShaftsComposite" {
  3. Properties {
  4. _MainTex ("Base", 2D) = "" {}
  5. _ColorBuffer ("Color", 2D) = "" {}
  6. _Skybox ("Skybox", 2D) = "" {}
  7. }
  8. CGINCLUDE
  9. #include "UnityCG.cginc"
  10. struct v2f {
  11. float4 pos : SV_POSITION;
  12. float2 uv : TEXCOORD0;
  13. #if UNITY_UV_STARTS_AT_TOP
  14. float2 uv1 : TEXCOORD1;
  15. #endif
  16. };
  17. struct v2f_radial {
  18. float4 pos : SV_POSITION;
  19. float2 uv : TEXCOORD0;
  20. float2 blurVector : TEXCOORD1;
  21. };
  22. sampler2D _MainTex;
  23. sampler2D _ColorBuffer;
  24. sampler2D _Skybox;
  25. sampler2D_float _CameraDepthTexture;
  26. uniform half4 _SunThreshold;
  27. uniform half4 _SunColor;
  28. uniform half4 _BlurRadius4;
  29. uniform half4 _SunPosition;
  30. uniform half4 _MainTex_TexelSize;
  31. #define SAMPLES_FLOAT 6.0f
  32. #define SAMPLES_INT 6
  33. v2f vert( appdata_img v ) {
  34. v2f o;
  35. o.pos = UnityObjectToClipPos(v.vertex);
  36. o.uv = v.texcoord.xy;
  37. #if UNITY_UV_STARTS_AT_TOP
  38. o.uv1 = v.texcoord.xy;
  39. if (_MainTex_TexelSize.y < 0)
  40. o.uv1.y = 1-o.uv1.y;
  41. #endif
  42. return o;
  43. }
  44. half4 fragScreen(v2f i) : SV_Target {
  45. half4 colorA = tex2D (_MainTex, i.uv.xy);
  46. #if UNITY_UV_STARTS_AT_TOP
  47. half4 colorB = tex2D (_ColorBuffer, i.uv1.xy);
  48. #else
  49. half4 colorB = tex2D (_ColorBuffer, i.uv.xy);
  50. #endif
  51. half4 depthMask = saturate (colorB * _SunColor);
  52. return 1.0f - (1.0f-colorA) * (1.0f-depthMask);
  53. }
  54. half4 fragAdd(v2f i) : SV_Target {
  55. half4 colorA = tex2D (_MainTex, i.uv.xy);
  56. #if UNITY_UV_STARTS_AT_TOP
  57. half4 colorB = tex2D (_ColorBuffer, i.uv1.xy);
  58. #else
  59. half4 colorB = tex2D (_ColorBuffer, i.uv.xy);
  60. #endif
  61. half4 depthMask = saturate (colorB * _SunColor);
  62. return colorA + depthMask;
  63. }
  64. v2f_radial vert_radial( appdata_img v ) {
  65. v2f_radial o;
  66. o.pos = UnityObjectToClipPos(v.vertex);
  67. o.uv.xy = v.texcoord.xy;
  68. o.blurVector = (_SunPosition.xy - v.texcoord.xy) * _BlurRadius4.xy;
  69. return o;
  70. }
  71. half4 frag_radial(v2f_radial i) : SV_Target
  72. {
  73. half4 color = half4(0,0,0,0);
  74. for(int j = 0; j < SAMPLES_INT; j++)
  75. {
  76. half4 tmpColor = tex2D(_MainTex, i.uv.xy);
  77. color += tmpColor;
  78. i.uv.xy += i.blurVector;
  79. }
  80. return color / SAMPLES_FLOAT;
  81. }
  82. half TransformColor (half4 skyboxValue) {
  83. return dot(max(skyboxValue.rgb - _SunThreshold.rgb, half3(0,0,0)), half3(1,1,1)); // threshold and convert to greyscale
  84. }
  85. half4 frag_depth (v2f i) : SV_Target {
  86. #if UNITY_UV_STARTS_AT_TOP
  87. float depthSample = SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, i.uv1.xy);
  88. #else
  89. float depthSample = SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, i.uv.xy);
  90. #endif
  91. half4 tex = tex2D (_MainTex, i.uv.xy);
  92. depthSample = Linear01Depth (depthSample);
  93. // consider maximum radius
  94. #if UNITY_UV_STARTS_AT_TOP
  95. half2 vec = _SunPosition.xy - i.uv1.xy;
  96. #else
  97. half2 vec = _SunPosition.xy - i.uv.xy;
  98. #endif
  99. half dist = saturate (_SunPosition.w - length (vec.xy));
  100. half4 outColor = 0;
  101. // consider shafts blockers
  102. if (depthSample > 0.99)
  103. outColor = TransformColor (tex) * dist;
  104. return outColor;
  105. }
  106. half4 frag_nodepth (v2f i) : SV_Target {
  107. #if UNITY_UV_STARTS_AT_TOP
  108. float4 sky = (tex2D (_Skybox, i.uv1.xy));
  109. #else
  110. float4 sky = (tex2D (_Skybox, i.uv.xy));
  111. #endif
  112. float4 tex = (tex2D (_MainTex, i.uv.xy));
  113. // consider maximum radius
  114. #if UNITY_UV_STARTS_AT_TOP
  115. half2 vec = _SunPosition.xy - i.uv1.xy;
  116. #else
  117. half2 vec = _SunPosition.xy - i.uv.xy;
  118. #endif
  119. half dist = saturate (_SunPosition.w - length (vec));
  120. half4 outColor = 0;
  121. // find unoccluded sky pixels
  122. // consider pixel values that differ significantly between framebuffer and sky-only buffer as occluded
  123. if (Luminance ( abs(sky.rgb - tex.rgb)) < 0.2)
  124. outColor = TransformColor (sky) * dist;
  125. return outColor;
  126. }
  127. ENDCG
  128. Subshader {
  129. Pass {
  130. ZTest Always Cull Off ZWrite Off
  131. CGPROGRAM
  132. #pragma vertex vert
  133. #pragma fragment fragScreen
  134. ENDCG
  135. }
  136. Pass {
  137. ZTest Always Cull Off ZWrite Off
  138. CGPROGRAM
  139. #pragma vertex vert_radial
  140. #pragma fragment frag_radial
  141. ENDCG
  142. }
  143. Pass {
  144. ZTest Always Cull Off ZWrite Off
  145. CGPROGRAM
  146. #pragma vertex vert
  147. #pragma fragment frag_depth
  148. ENDCG
  149. }
  150. Pass {
  151. ZTest Always Cull Off ZWrite Off
  152. CGPROGRAM
  153. #pragma vertex vert
  154. #pragma fragment frag_nodepth
  155. ENDCG
  156. }
  157. Pass {
  158. ZTest Always Cull Off ZWrite Off
  159. CGPROGRAM
  160. #pragma vertex vert
  161. #pragma fragment fragAdd
  162. ENDCG
  163. }
  164. }
  165. Fallback off
  166. } // shader