You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

330 lines
7.0 KiB

5 years ago
  1. // Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
  2. Shader "Hidden/FogVolumeBloom" {
  3. Properties {
  4. _MainTex ("Base (RGB)", 2D) = "white" {}
  5. _Bloom ("Bloom (RGB)", 2D) = "black" {}
  6. }
  7. CGINCLUDE
  8. #include "UnityCG.cginc"
  9. #pragma shader_feature BLOOM
  10. sampler2D _MainTex;
  11. sampler2D _Bloom;
  12. sampler2D RT_FogVolumeConvolution, _source;
  13. uniform half4 _MainTex_TexelSize;
  14. half4 _MainTex_ST;
  15. uniform half _Saturation;
  16. uniform half4 _Parameter;
  17. uniform half4 _OffsetsA;
  18. uniform half4 _OffsetsB;
  19. half _Falloff;
  20. #define ONE_MINUS_THRESHHOLD_TIMES_INTENSITY _Parameter.w
  21. #define THRESHHOLD _Parameter.z
  22. struct v2f_simple
  23. {
  24. float4 pos : SV_POSITION;
  25. half2 uv : TEXCOORD0;
  26. #if UNITY_UV_STARTS_AT_TOP
  27. half2 uv2 : TEXCOORD1;
  28. #endif
  29. };
  30. v2f_simple vertBloom ( appdata_img v )
  31. {
  32. v2f_simple o;
  33. o.pos = UnityObjectToClipPos (v.vertex);
  34. o.uv = UnityStereoScreenSpaceUVAdjust(v.texcoord, _MainTex_ST);
  35. #if UNITY_UV_STARTS_AT_TOP
  36. o.uv2 = o.uv;
  37. if (_MainTex_TexelSize.y < 0.0)
  38. o.uv.y = 1.0 - o.uv.y;
  39. #endif
  40. return o;
  41. }
  42. struct v2f_tap
  43. {
  44. float4 pos : SV_POSITION;
  45. half2 uv20 : TEXCOORD0;
  46. half2 uv21 : TEXCOORD1;
  47. half2 uv22 : TEXCOORD2;
  48. half2 uv23 : TEXCOORD3;
  49. };
  50. v2f_tap vert4Tap ( appdata_img v )
  51. {
  52. v2f_tap o;
  53. o.pos = UnityObjectToClipPos (v.vertex);
  54. o.uv20 = UnityStereoScreenSpaceUVAdjust(v.texcoord + _MainTex_TexelSize.xy, _MainTex_ST);
  55. o.uv21 = UnityStereoScreenSpaceUVAdjust(v.texcoord + _MainTex_TexelSize.xy * half2(-0.5h,-0.5h), _MainTex_ST);
  56. o.uv22 = UnityStereoScreenSpaceUVAdjust(v.texcoord + _MainTex_TexelSize.xy * half2(0.5h,-0.5h), _MainTex_ST);
  57. o.uv23 = UnityStereoScreenSpaceUVAdjust(v.texcoord + _MainTex_TexelSize.xy * half2(-0.5h,0.5h), _MainTex_ST);
  58. return o;
  59. }
  60. fixed4 fragBloom ( v2f_simple i ) : SV_Target
  61. {
  62. float2 uv = i.uv;
  63. #if UNITY_UV_STARTS_AT_TOP
  64. uv = i.uv2;
  65. #endif
  66. fixed4 BlurredFV = tex2D(_MainTex, uv);
  67. fixed Density = min(1, tex2D(RT_FogVolumeConvolution, uv).a * _Falloff);
  68. fixed4 Scene = tex2D(_source, uv);
  69. fixed4 Blended = lerp(Scene, BlurredFV, Density);
  70. #ifdef BLOOM
  71. fixed4 Bloom = tex2D(_Bloom, uv);
  72. Blended += Bloom*Density;
  73. #endif
  74. return Blended;
  75. }
  76. fixed4 fragBlend(v2f_simple i) : SV_Target
  77. {
  78. return 1;
  79. }
  80. fixed4 fragDownsample ( v2f_tap i ) : SV_Target
  81. {
  82. fixed4 color = tex2D (_MainTex, i.uv20);
  83. color += tex2D (_MainTex, i.uv21);
  84. color += tex2D (_MainTex, i.uv22);
  85. color += tex2D (_MainTex, i.uv23);
  86. return max(color/4 - THRESHHOLD, 0) * ONE_MINUS_THRESHHOLD_TIMES_INTENSITY;
  87. }
  88. // weight curves
  89. static const half curve[7] = { 0.0205, 0.0855, 0.232, 0.324, 0.232, 0.0855, 0.0205 }; // gauss'ish blur weights
  90. static const half4 curve4[7] = { half4(0.0205,0.0205,0.0205,0), half4(0.0855,0.0855,0.0855,0), half4(0.232,0.232,0.232,0),
  91. half4(0.324,0.324,0.324,1), half4(0.232,0.232,0.232,0), half4(0.0855,0.0855,0.0855,0), half4(0.0205,0.0205,0.0205,0) };
  92. struct v2f_withBlurCoords8
  93. {
  94. float4 pos : SV_POSITION;
  95. half4 uv : TEXCOORD0;
  96. half2 offs : TEXCOORD1;
  97. };
  98. struct v2f_withBlurCoordsSGX
  99. {
  100. float4 pos : SV_POSITION;
  101. half2 uv : TEXCOORD0;
  102. half4 offs[3] : TEXCOORD1;
  103. };
  104. v2f_withBlurCoords8 vertBlurHorizontal (appdata_img v)
  105. {
  106. v2f_withBlurCoords8 o;
  107. o.pos = UnityObjectToClipPos (v.vertex);
  108. o.uv = half4(v.texcoord.xy,1,1);
  109. o.offs = _MainTex_TexelSize.xy * half2(1.0, 0.0) * _Parameter.x;
  110. return o;
  111. }
  112. v2f_withBlurCoords8 vertBlurVertical (appdata_img v)
  113. {
  114. v2f_withBlurCoords8 o;
  115. o.pos = UnityObjectToClipPos (v.vertex);
  116. o.uv = half4(v.texcoord.xy,1,1);
  117. o.offs = _MainTex_TexelSize.xy * half2(0.0, 1.0) * _Parameter.x;
  118. return o;
  119. }
  120. half FOV_compensation;
  121. half4 fragBlur8 ( v2f_withBlurCoords8 i ) : SV_Target
  122. {
  123. half2 uv = i.uv.xy;
  124. half2 netFilterWidth = i.offs*FOV_compensation;
  125. half2 coords = uv - netFilterWidth * 3.0;
  126. half4 color = 0;
  127. for( int l = 0; l < 7; l++ )
  128. {
  129. half4 tap = tex2D(_MainTex, UnityStereoScreenSpaceUVAdjust(coords, _MainTex_ST));
  130. // added casting for ps4
  131. tap.rgb = lerp((half)Luminance(tap.rgb), (half3)tap.rgb, (half3)_Saturation);
  132. color += tap * curve4[l];
  133. coords += netFilterWidth;
  134. }
  135. return color;
  136. }
  137. v2f_withBlurCoordsSGX vertBlurHorizontalSGX (appdata_img v)
  138. {
  139. v2f_withBlurCoordsSGX o;
  140. o.pos = UnityObjectToClipPos (v.vertex);
  141. o.uv = v.texcoord.xy;
  142. half offsetMagnitude = _MainTex_TexelSize.x * _Parameter.x;
  143. o.offs[0] = v.texcoord.xyxy + offsetMagnitude * half4(-3.0h, 0.0h, 3.0h, 0.0h);
  144. o.offs[1] = v.texcoord.xyxy + offsetMagnitude * half4(-2.0h, 0.0h, 2.0h, 0.0h);
  145. o.offs[2] = v.texcoord.xyxy + offsetMagnitude * half4(-1.0h, 0.0h, 1.0h, 0.0h);
  146. return o;
  147. }
  148. v2f_withBlurCoordsSGX vertBlurVerticalSGX (appdata_img v)
  149. {
  150. v2f_withBlurCoordsSGX o;
  151. o.pos = UnityObjectToClipPos (v.vertex);
  152. o.uv = half4(v.texcoord.xy,1,1);
  153. half offsetMagnitude = _MainTex_TexelSize.y * _Parameter.x;
  154. o.offs[0] = v.texcoord.xyxy + offsetMagnitude * half4(0.0h, -3.0h, 0.0h, 3.0h);
  155. o.offs[1] = v.texcoord.xyxy + offsetMagnitude * half4(0.0h, -2.0h, 0.0h, 2.0h);
  156. o.offs[2] = v.texcoord.xyxy + offsetMagnitude * half4(0.0h, -1.0h, 0.0h, 1.0h);
  157. return o;
  158. }
  159. half4 fragBlurSGX ( v2f_withBlurCoordsSGX i ) : SV_Target
  160. {
  161. half2 uv = i.uv.xy;
  162. half4 color = tex2D(_MainTex, UnityStereoScreenSpaceUVAdjust(i.uv, _MainTex_ST)) * curve4[3];
  163. for( int l = 0; l < 3; l++ )
  164. {
  165. half4 tapA = tex2D(_MainTex, UnityStereoScreenSpaceUVAdjust(i.offs[l].xy, _MainTex_ST));
  166. half4 tapB = tex2D(_MainTex, UnityStereoScreenSpaceUVAdjust(i.offs[l].zw, _MainTex_ST));
  167. color += (tapA + tapB) * curve4[l];
  168. }
  169. return color;
  170. }
  171. ENDCG
  172. SubShader {
  173. /* Stencil
  174. {
  175. Ref 2
  176. Comp Equal
  177. }*/
  178. ZTest Off Cull Off ZWrite Off Blend Off
  179. // 0
  180. Pass {
  181. CGPROGRAM
  182. #pragma vertex vertBloom
  183. #pragma fragment fragBloom
  184. ENDCG
  185. }
  186. // 1
  187. Pass {
  188. CGPROGRAM
  189. #pragma vertex vert4Tap
  190. #pragma fragment fragDownsample
  191. ENDCG
  192. }
  193. // 2
  194. Pass {
  195. ZTest Always
  196. Cull Off
  197. CGPROGRAM
  198. #pragma vertex vertBlurVertical
  199. #pragma fragment fragBlur8
  200. ENDCG
  201. }
  202. // 3
  203. Pass {
  204. ZTest Always
  205. Cull Off
  206. CGPROGRAM
  207. #pragma vertex vertBlurHorizontal
  208. #pragma fragment fragBlur8
  209. ENDCG
  210. }
  211. // alternate blur
  212. // 4
  213. Pass {
  214. ZTest Always
  215. Cull Off
  216. CGPROGRAM
  217. #pragma vertex vertBlurVerticalSGX
  218. #pragma fragment fragBlurSGX
  219. ENDCG
  220. }
  221. // 5
  222. Pass {
  223. ZTest Always
  224. Cull Off
  225. CGPROGRAM
  226. #pragma vertex vertBlurHorizontalSGX
  227. #pragma fragment fragBlurSGX
  228. ENDCG
  229. }
  230. // 6
  231. Pass
  232. {
  233. ZTest Always
  234. Cull Off
  235. CGPROGRAM
  236. #pragma vertex vertBloom
  237. #pragma fragment fragBlend
  238. ENDCG
  239. }
  240. }
  241. FallBack Off
  242. }