Search Unity

Why doesn't my blur shader work with linear rendering?

Discussion in 'Shaders' started by Orion_Reed, Feb 9, 2016.

  1. Orion_Reed

    Orion_Reed

    Joined:
    Aug 14, 2012
    Posts:
    39
    I've been working on a shader for UI and other aspects of a project i'm working on, I was wondering if anyone knows how I can make it work with linear rendering, as I really want to use linear rendering for this project. The buttons are using the blur shader. Screenshots and shader code:

    Gamma:


    Linear:


    Code (CSharp):
    1. Shader "Blur/Blur1xPass" {
    2.     Properties {
    3.         _TintColor ("Tint Color", Color) = (1, 1, 1, 0.2)
    4.         _Size ("Spacing", Range(0, 20)) = 5.0
    5.         _Vibrancy ("Vibrancy", Range(0, 2)) = 0.2
    6.      
    7.         [HideInInspector]
    8.         _StencilComp ("Stencil Comparison", Float) = 8
    9.         [HideInInspector]
    10.         _Stencil ("Stencil ID", Float) = 0
    11.         [HideInInspector]
    12.         _StencilOp ("Stencil Operation", Float) = 0
    13.         [HideInInspector]
    14.         _StencilWriteMask ("Stencil Write Mask", Float) = 255
    15.         [HideInInspector]
    16.         _StencilReadMask ("Stencil Read Mask", Float) = 255
    17.  
    18.          _ColorMask ("Color Mask", Float) = 15
    19.  
    20.     }
    21.     Category {
    22.  
    23.             Tags {
    24.             "Queue"="Transparent"
    25.             "IgnoreProjector"="True"
    26.             "RenderType"="Opaque"
    27.         }
    28.  
    29.         Stencil {
    30.             Ref [_Stencil]
    31.             Comp [_StencilComp]
    32.             Pass [_StencilOp]
    33.             ReadMask [_StencilReadMask]
    34.             WriteMask [_StencilWriteMask]
    35.         }
    36.  
    37.         SubShader {
    38.  
    39.             GrabPass {                      
    40.                 Tags { "LightMode" = "Always" }
    41.             }
    42.  
    43.             Stencil
    44.              {
    45.              Ref [_Stencil]
    46.              Comp [_StencilComp]
    47.              Pass [_StencilOp]
    48.              ReadMask [_StencilReadMask]
    49.              WriteMask [_StencilWriteMask]
    50.          }
    51.           ColorMask [_ColorMask]
    52.  
    53.             // Vertical blur
    54.             Pass {
    55.                 Name "VERTICAL"
    56.                 Tags { "LightMode" = "Always" }
    57.              
    58.                 CGPROGRAM
    59.                 #pragma vertex vert
    60.                 #pragma fragment frag
    61.                 #pragma fragmentoption ARB_precision_hint_fastest
    62.                 #include "UnityCG.cginc"
    63.              
    64.                 struct appdata_t {
    65.                     float4 vertex : POSITION;
    66.                     float2 texcoord: TEXCOORD0;
    67.                 };
    68.              
    69.                 struct v2f {
    70.                     float4 vertex : POSITION;
    71.                     float4 uvgrab : TEXCOORD0;
    72.                 };
    73.              
    74.                 v2f vert (appdata_t v) {
    75.                     v2f o;
    76.                     o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
    77.                     #if UNITY_UV_STARTS_AT_TOP
    78.                     float scale = -1.0;
    79.                     #else
    80.                     float scale = 1.0;
    81.                     #endif
    82.                     o.uvgrab.xy = (float2(o.vertex.x, o.vertex.y*scale) + o.vertex.w) * 0.5;
    83.                     o.uvgrab.zw = o.vertex.zw;
    84.                     return o;
    85.                 }
    86.              
    87.                 sampler2D _GrabTexture;
    88.                 float4 _GrabTexture_TexelSize;
    89.                 float _Size;
    90.              
    91.                 half4 frag( v2f i ) : COLOR {
    92.                     half4 sum = half4(0,0,0,0);
    93.                     #define GRABPIXEL(weight,kernely) tex2Dproj( _GrabTexture, UNITY_PROJ_COORD(float4(i.uvgrab.x, i.uvgrab.y + _GrabTexture_TexelSize.y * kernely * _Size, i.uvgrab.z, i.uvgrab.w))) * weight
    94.                     sum += GRABPIXEL(0.05, -4.0);
    95.                     sum += GRABPIXEL(0.09, -3.0);
    96.                     sum += GRABPIXEL(0.12, -2.0);
    97.                     sum += GRABPIXEL(0.15, -1.0);
    98.                     sum += GRABPIXEL(0.18,  0.0);
    99.                     sum += GRABPIXEL(0.15, +1.0);
    100.                     sum += GRABPIXEL(0.12, +2.0);
    101.                     sum += GRABPIXEL(0.09, +3.0);
    102.                     sum += GRABPIXEL(0.05, +4.0);
    103.                  
    104.                     return sum;
    105.                 }
    106.              
    107.                 ENDCG
    108.             }
    109.  
    110.             GrabPass {                      
    111.                 Tags { "LightMode" = "Always" }
    112.             }
    113.  
    114.             Pass {
    115.                 Name "HORIZONTAL"
    116.          
    117.                 Tags { "LightMode" = "Always" }
    118.              
    119.                 CGPROGRAM
    120.                 #pragma vertex vert
    121.                 #pragma fragment frag
    122.                 #pragma fragmentoption ARB_precision_hint_fastest
    123.                 #include "UnityCG.cginc"
    124.              
    125.                 struct appdata_t {
    126.                     float4 vertex : POSITION;
    127.                     float2 texcoord: TEXCOORD0;
    128.                 };
    129.              
    130.                 struct v2f {
    131.                     float4 vertex : POSITION;
    132.                     float4 uvgrab : TEXCOORD0;
    133.                 };
    134.              
    135.                 v2f vert (appdata_t v) {
    136.                     v2f o;
    137.                     o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
    138.                     #if UNITY_UV_STARTS_AT_TOP
    139.                     float scale = -1.0;
    140.                     #else
    141.                     float scale = 1.0;
    142.                     #endif
    143.                     o.uvgrab.xy = (float2(o.vertex.x, o.vertex.y*scale) + o.vertex.w) * 0.5;
    144.                     o.uvgrab.zw = o.vertex.zw;
    145.                     return o;
    146.                 }
    147.              
    148.                 sampler2D _GrabTexture;
    149.                 float4 _GrabTexture_TexelSize;
    150.                 float _Size;
    151.              
    152.                 half4 frag( v2f i ) : COLOR {
    153.                     half4 sum = half4(0,0,0,0);
    154.                     #define GRABPIXEL(weight,kernelx) tex2Dproj(_GrabTexture, UNITY_PROJ_COORD(float4(i.uvgrab.x + _GrabTexture_TexelSize.x * kernelx * _Size, i.uvgrab.y, i.uvgrab.z, i.uvgrab.w))) * weight
    155.                     sum += GRABPIXEL(0.05, -4.0);
    156.                     sum += GRABPIXEL(0.09, -3.0);
    157.                     sum += GRABPIXEL(0.12, -2.0);
    158.                     sum += GRABPIXEL(0.15, -1.0);
    159.                     sum += GRABPIXEL(0.18,  0.0);
    160.                     sum += GRABPIXEL(0.15, +1.0);
    161.                     sum += GRABPIXEL(0.12, +2.0);
    162.                     sum += GRABPIXEL(0.09, +3.0);
    163.                     sum += GRABPIXEL(0.05, +4.0);
    164.                  
    165.                     return sum;
    166.                 }
    167.              
    168.                 ENDCG
    169.             }
    170.  
    171.             GrabPass {                      
    172.                 Tags { "LightMode" = "Always" }
    173.             }
    174.          
    175.             Pass {
    176.                 Tags { "LightMode" = "Always" }
    177.              
    178.                 CGPROGRAM
    179.                 #pragma vertex vert
    180.                 #pragma fragment frag
    181.                 #pragma fragmentoption ARB_precision_hint_fastest
    182.                 #include "UnityCG.cginc"
    183.              
    184.                 struct appdata_t {
    185.                     float4 vertex : POSITION;
    186.                     float2 texcoord: TEXCOORD0;
    187.                 };
    188.              
    189.                 struct v2f {
    190.                     float4 vertex : POSITION;
    191.                     float4 uvgrab : TEXCOORD0;
    192.                 };
    193.              
    194.                 v2f vert (appdata_t v) {
    195.                     v2f o;
    196.                     o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
    197.                     #if UNITY_UV_STARTS_AT_TOP
    198.                     float scale = -1.0;
    199.                     #else
    200.                     float scale = 1.0;
    201.                     #endif
    202.                     o.uvgrab.xy = (float2(o.vertex.x, o.vertex.y*scale) + o.vertex.w) * 0.5;
    203.                     o.uvgrab.zw = o.vertex.zw;
    204.                  
    205.                     return o;
    206.                 }
    207.              
    208.                 half4 _TintColor;
    209.                 float _Vibrancy;
    210.                 sampler2D _GrabTexture;
    211.                 float4 _GrabTexture_TexelSize;
    212.              
    213.                 half4 frag( v2f i ) : COLOR {
    214.                     half4 col = tex2Dproj( _GrabTexture, UNITY_PROJ_COORD(i.uvgrab));
    215.                     col.xyz *= 1 + _Vibrancy;
    216.                        col = lerp (col, _TintColor, _TintColor.w);
    217.                     return col;
    218.                 }
    219.                 ENDCG
    220.             }
    221.         }
    222.     }
    223. }