BlurAndFlares.shader 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. // Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
  2. Shader "Hidden/BlurAndFlares" {
  3. Properties {
  4. _MainTex ("Base (RGB)", 2D) = "" {}
  5. _NonBlurredTex ("Base (RGB)", 2D) = "" {}
  6. }
  7. CGINCLUDE
  8. #include "UnityCG.cginc"
  9. struct v2f {
  10. half4 pos : SV_POSITION;
  11. half2 uv : TEXCOORD0;
  12. };
  13. struct v2f_opts {
  14. half4 pos : SV_POSITION;
  15. half2 uv[7] : TEXCOORD0;
  16. };
  17. struct v2f_blur {
  18. half4 pos : SV_POSITION;
  19. half2 uv : TEXCOORD0;
  20. half4 uv01 : TEXCOORD1;
  21. half4 uv23 : TEXCOORD2;
  22. half4 uv45 : TEXCOORD3;
  23. half4 uv67 : TEXCOORD4;
  24. };
  25. half4 _Offsets;
  26. half4 _TintColor;
  27. half _StretchWidth;
  28. half2 _Threshhold;
  29. half _Saturation;
  30. half4 _MainTex_TexelSize;
  31. sampler2D _MainTex;
  32. sampler2D _NonBlurredTex;
  33. v2f vert (appdata_img v) {
  34. v2f o;
  35. o.pos = UnityObjectToClipPos(v.vertex);
  36. o.uv = v.texcoord.xy;
  37. return o;
  38. }
  39. v2f_blur vertWithMultiCoords2 (appdata_img v) {
  40. v2f_blur o;
  41. o.pos = UnityObjectToClipPos(v.vertex);
  42. o.uv.xy = v.texcoord.xy;
  43. o.uv01 = v.texcoord.xyxy + _Offsets.xyxy * half4(1,1, -1,-1);
  44. o.uv23 = v.texcoord.xyxy + _Offsets.xyxy * half4(1,1, -1,-1) * 2.0;
  45. o.uv45 = v.texcoord.xyxy + _Offsets.xyxy * half4(1,1, -1,-1) * 3.0;
  46. o.uv67 = v.texcoord.xyxy + _Offsets.xyxy * half4(1,1, -1,-1) * 4.0;
  47. o.uv67 = v.texcoord.xyxy + _Offsets.xyxy * half4(1,1, -1,-1) * 5.0;
  48. return o;
  49. }
  50. v2f_opts vertStretch (appdata_img v) {
  51. v2f_opts o;
  52. o.pos = UnityObjectToClipPos(v.vertex);
  53. half b = _StretchWidth;
  54. o.uv[0] = v.texcoord.xy;
  55. o.uv[1] = v.texcoord.xy + b * 2.0 * _Offsets.xy;
  56. o.uv[2] = v.texcoord.xy - b * 2.0 * _Offsets.xy;
  57. o.uv[3] = v.texcoord.xy + b * 4.0 * _Offsets.xy;
  58. o.uv[4] = v.texcoord.xy - b * 4.0 * _Offsets.xy;
  59. o.uv[5] = v.texcoord.xy + b * 6.0 * _Offsets.xy;
  60. o.uv[6] = v.texcoord.xy - b * 6.0 * _Offsets.xy;
  61. return o;
  62. }
  63. v2f_opts vertWithMultiCoords (appdata_img v) {
  64. v2f_opts o;
  65. o.pos = UnityObjectToClipPos(v.vertex);
  66. o.uv[0] = v.texcoord.xy;
  67. o.uv[1] = v.texcoord.xy + 0.5 * _MainTex_TexelSize.xy * _Offsets.xy;
  68. o.uv[2] = v.texcoord.xy - 0.5 * _MainTex_TexelSize.xy * _Offsets.xy;
  69. o.uv[3] = v.texcoord.xy + 1.5 * _MainTex_TexelSize.xy * _Offsets.xy;
  70. o.uv[4] = v.texcoord.xy - 1.5 * _MainTex_TexelSize.xy * _Offsets.xy;
  71. o.uv[5] = v.texcoord.xy + 2.5 * _MainTex_TexelSize.xy * _Offsets.xy;
  72. o.uv[6] = v.texcoord.xy - 2.5 * _MainTex_TexelSize.xy * _Offsets.xy;
  73. return o;
  74. }
  75. half4 fragPostNoBlur (v2f i) : SV_Target {
  76. half4 color = tex2D (_MainTex, i.uv);
  77. return color * 1.0/(1.0 + Luminance(color.rgb) + 0.5); // this also makes it a little noisy
  78. }
  79. half4 fragGaussBlur (v2f_blur i) : SV_Target {
  80. half4 color = half4 (0,0,0,0);
  81. color += 0.225 * tex2D (_MainTex, i.uv);
  82. color += 0.150 * tex2D (_MainTex, i.uv01.xy);
  83. color += 0.150 * tex2D (_MainTex, i.uv01.zw);
  84. color += 0.110 * tex2D (_MainTex, i.uv23.xy);
  85. color += 0.110 * tex2D (_MainTex, i.uv23.zw);
  86. color += 0.075 * tex2D (_MainTex, i.uv45.xy);
  87. color += 0.075 * tex2D (_MainTex, i.uv45.zw);
  88. color += 0.0525 * tex2D (_MainTex, i.uv67.xy);
  89. color += 0.0525 * tex2D (_MainTex, i.uv67.zw);
  90. return color;
  91. }
  92. half4 fragPreAndCut (v2f_opts i) : SV_Target {
  93. half4 color = tex2D (_MainTex, i.uv[0]);
  94. color += tex2D (_MainTex, i.uv[1]);
  95. color += tex2D (_MainTex, i.uv[2]);
  96. color += tex2D (_MainTex, i.uv[3]);
  97. color += tex2D (_MainTex, i.uv[4]);
  98. color += tex2D (_MainTex, i.uv[5]);
  99. color += tex2D (_MainTex, i.uv[6]);
  100. color = max(color / 7.0 - _Threshhold.xxxx, float4(0,0,0,0));
  101. half lum = Luminance(color.rgb);
  102. color.rgb = lerp(half3(lum,lum,lum), color.rgb, _Saturation) * _TintColor.rgb;
  103. return color;
  104. }
  105. half4 fragStretch (v2f_opts i) : SV_Target {
  106. half4 color = tex2D (_MainTex, i.uv[0]);
  107. color = max (color, tex2D (_MainTex, i.uv[1]));
  108. color = max (color, tex2D (_MainTex, i.uv[2]));
  109. color = max (color, tex2D (_MainTex, i.uv[3]));
  110. color = max (color, tex2D (_MainTex, i.uv[4]));
  111. color = max (color, tex2D (_MainTex, i.uv[5]));
  112. color = max (color, tex2D (_MainTex, i.uv[6]));
  113. return color;
  114. }
  115. half4 fragPost (v2f_opts i) : SV_Target {
  116. half4 color = tex2D (_MainTex, i.uv[0]);
  117. color += tex2D (_MainTex, i.uv[1]);
  118. color += tex2D (_MainTex, i.uv[2]);
  119. color += tex2D (_MainTex, i.uv[3]);
  120. color += tex2D (_MainTex, i.uv[4]);
  121. color += tex2D (_MainTex, i.uv[5]);
  122. color += tex2D (_MainTex, i.uv[6]);
  123. return color * 1.0/(7.0 + Luminance(color.rgb) + 0.5); // this also makes it a little noisy
  124. }
  125. ENDCG
  126. Subshader {
  127. ZTest Always Cull Off ZWrite Off
  128. Pass {
  129. CGPROGRAM
  130. #pragma vertex vert
  131. #pragma fragment fragPostNoBlur
  132. ENDCG
  133. }
  134. Pass {
  135. CGPROGRAM
  136. #pragma vertex vertStretch
  137. #pragma fragment fragStretch
  138. ENDCG
  139. }
  140. // 2
  141. Pass {
  142. CGPROGRAM
  143. #pragma vertex vertWithMultiCoords
  144. #pragma fragment fragPreAndCut
  145. ENDCG
  146. }
  147. // 3
  148. Pass {
  149. CGPROGRAM
  150. #pragma vertex vertWithMultiCoords
  151. #pragma fragment fragPost
  152. ENDCG
  153. }
  154. // 4
  155. Pass {
  156. CGPROGRAM
  157. #pragma vertex vertWithMultiCoords2
  158. #pragma fragment fragGaussBlur
  159. ENDCG
  160. }
  161. }
  162. Fallback off
  163. }