Luminance2NormalsBlur.shader 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
  2. Shader "Hidden/Luminance2NormalsBlur" {
  3. Properties {
  4. _MainTex ("Base (RGB)", 2D) = "white" {}
  5. _BlurTex ("Base (RGB)", 2D) = "white" {}
  6. }
  7. SubShader {
  8. Pass {
  9. ZTest Always Cull Off ZWrite Off
  10. Fog { Mode off }
  11. CGPROGRAM
  12. #pragma multi_compile SHOW_DEBUG_ON SHOW_DEBUG_OFF
  13. #pragma vertex vert
  14. #pragma fragment frag
  15. #pragma fragmentoption ARB_precision_hint_fastest
  16. #include "UnityCG.cginc"
  17. uniform sampler2D _MainTex;
  18. uniform float4 _MainTex_TexelSize;
  19. uniform float _OffsetScale;
  20. uniform float _BlurRadius;
  21. struct v2f {
  22. float4 pos : POSITION;
  23. float2 uv[8] : TEXCOORD0;
  24. };
  25. v2f vert( appdata_img v )
  26. {
  27. v2f o;
  28. o.pos = UnityObjectToClipPos (v.vertex);
  29. float2 uv = v.texcoord.xy;
  30. #if SHADER_API_D3D9
  31. if (_MainTex_TexelSize.y < 0)
  32. uv.y = 1-uv.y;
  33. #endif
  34. float2 up = float2(0.0, _MainTex_TexelSize.y) * _OffsetScale;
  35. float2 right = float2(_MainTex_TexelSize.x, 0.0) * _OffsetScale;
  36. o.uv[0].xy = uv + up;
  37. o.uv[1].xy = uv - up;
  38. o.uv[2].xy = uv + right;
  39. o.uv[3].xy = uv - right;
  40. o.uv[4].xy = uv - right + up;
  41. o.uv[5].xy = uv - right -up;
  42. o.uv[6].xy = uv + right + up;
  43. o.uv[7].xy = uv + right -up;
  44. return o;
  45. }
  46. half4 frag (v2f i) : COLOR
  47. {
  48. // get luminance values
  49. // maybe: experiment with different luminance calculations
  50. float topL = Luminance( tex2D(_MainTex, i.uv[0]).rgb );
  51. float bottomL = Luminance( tex2D(_MainTex, i.uv[1]).rgb );
  52. float rightL = Luminance( tex2D(_MainTex, i.uv[2]).rgb );
  53. float leftL = Luminance( tex2D(_MainTex, i.uv[3]).rgb );
  54. float leftTopL = Luminance( tex2D(_MainTex, i.uv[4]).rgb );
  55. float leftBottomL = Luminance( tex2D(_MainTex, i.uv[5]).rgb );
  56. float rightBottomL = Luminance( tex2D(_MainTex, i.uv[6]).rgb );
  57. float rightTopL = Luminance( tex2D(_MainTex, i.uv[7]).rgb );
  58. // 2 triangle subtractions
  59. float sum0 = dot(float3(1,1,1), float3(rightTopL,bottomL,leftTopL));
  60. float sum1 = dot(float3(1,1,1), float3(leftBottomL,topL,rightBottomL));
  61. float sum2 = dot(float3(1,1,1), float3(leftTopL,rightL,leftBottomL));
  62. float sum3 = dot(float3(1,1,1), float3(rightBottomL,leftL,rightTopL));
  63. // figure out "normal"
  64. float2 blurDir = half2((sum0-sum1), (sum3-sum2));
  65. blurDir *= _MainTex_TexelSize.xy * _BlurRadius;
  66. // reconstruct normal uv
  67. float2 uv_ = (i.uv[0] + i.uv[1]) * 0.5;
  68. float4 returnColor = tex2D(_MainTex, uv_);
  69. returnColor += tex2D(_MainTex, uv_+ blurDir.xy);
  70. returnColor += tex2D(_MainTex, uv_ - blurDir.xy);
  71. returnColor += tex2D(_MainTex, uv_ + float2(blurDir.x, -blurDir.y));
  72. returnColor += tex2D(_MainTex, uv_ - float2(blurDir.x, -blurDir.y));
  73. #if defined(SHOW_DEBUG_ON)
  74. blurDir = half2((sum0-sum1), (sum3-sum2)) * _BlurRadius;
  75. return half4(normalize( half3(blurDir,1) * 0.5 + 0.5), 1);
  76. #else
  77. return returnColor * 0.2;
  78. #endif
  79. }
  80. ENDCG
  81. }
  82. }
  83. Fallback off
  84. }