源战役客户端
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.

287 lines
6.4 KiB

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