Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

UI blur with 2DRenderer with LWRP does not work

Discussion in '2D' started by penguin21, Jan 23, 2020.

  1. penguin21

    penguin21

    Joined:
    Aug 14, 2014
    Posts:
    11
    Hi, I know that grabpass no longer works in lwrp, but there is a solution or alternative to this that is for my 2D project please.
    And the shader took it from the internet because I have no idea of making shaders.
    Screenshots:
    https://imgur.com/a/aQH9vpk
    Shader Code:
    Code (CSharp):
    1. // Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
    2.  
    3. Shader "Custom/BlurUI" {
    4.     Properties{
    5.         _Color("Main Color", Color) = (1,1,1,1)
    6.         _BumpAmt("Distortion", Range(0,128)) = 10
    7.         _MainTex("Tint Color (RGB)", 2D) = "white" {}
    8.         _BumpMap("Normalmap", 2D) = "bump" {}
    9.         _Size("Size", Range(0, 20)) = 1
    10.     }
    11.  
    12.         Category{
    13.  
    14.             Tags { "Queue" = "Transparent" "IgnoreProjector" = "True" "RenderType" = "Opaque" }
    15.  
    16.  
    17.             SubShader {
    18.  
    19.                 GrabPass {
    20.                     Tags { "LightMode" = "Always" }
    21.                 }
    22.                 Pass {
    23.                     Tags { "LightMode" = "Always" }
    24.  
    25.                     CGPROGRAM
    26.                     #pragma vertex vert
    27.                     #pragma fragment frag
    28.                     #pragma fragmentoption ARB_precision_hint_fastest
    29.                     #include "UnityCG.cginc"
    30.  
    31.                     struct appdata_t {
    32.                         float4 vertex : POSITION;
    33.                         float2 texcoord: TEXCOORD0;
    34.                     };
    35.  
    36.                     struct v2f {
    37.                         float4 vertex : POSITION;
    38.                         float4 uvgrab : TEXCOORD0;
    39.                     };
    40.  
    41.                     v2f vert(appdata_t v) {
    42.                         v2f o;
    43.                         o.vertex = UnityObjectToClipPos(v.vertex);
    44.                         #if UNITY_UV_STARTS_AT_TOP
    45.                         float scale = -1.0;
    46.                         #else
    47.                         float scale = 1.0;
    48.                         #endif
    49.                         o.uvgrab.xy = (float2(o.vertex.x, o.vertex.y*scale) + o.vertex.w) * 0.5;
    50.                         o.uvgrab.zw = o.vertex.zw;
    51.                         return o;
    52.                     }
    53.  
    54.                     sampler2D _GrabTexture;
    55.                     float4 _GrabTexture_TexelSize;
    56.                     float _Size;
    57.  
    58.                     half4 frag(v2f i) : COLOR {
    59.  
    60.                         half4 sum = half4(0,0,0,0);
    61.                         #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
    62.                         sum += GRABPIXEL(0.05, -4.0);
    63.                         sum += GRABPIXEL(0.09, -3.0);
    64.                         sum += GRABPIXEL(0.12, -2.0);
    65.                         sum += GRABPIXEL(0.15, -1.0);
    66.                         sum += GRABPIXEL(0.18,  0.0);
    67.                         sum += GRABPIXEL(0.15, +1.0);
    68.                         sum += GRABPIXEL(0.12, +2.0);
    69.                         sum += GRABPIXEL(0.09, +3.0);
    70.                         sum += GRABPIXEL(0.05, +4.0);
    71.  
    72.                         return sum;
    73.                     }
    74.                     ENDCG
    75.                 }
    76.  
    77.                 GrabPass {
    78.                     Tags { "LightMode" = "Always" }
    79.                 }
    80.                 Pass {
    81.                     Tags { "LightMode" = "Always" }
    82.  
    83.                     CGPROGRAM
    84.                     #pragma vertex vert
    85.                     #pragma fragment frag
    86.                     #pragma fragmentoption ARB_precision_hint_fastest
    87.                     #include "UnityCG.cginc"
    88.  
    89.                     struct appdata_t {
    90.                         float4 vertex : POSITION;
    91.                         float2 texcoord: TEXCOORD0;
    92.                     };
    93.  
    94.                     struct v2f {
    95.                         float4 vertex : POSITION;
    96.                         float4 uvgrab : TEXCOORD0;
    97.                     };
    98.  
    99.                     v2f vert(appdata_t v) {
    100.                         v2f o;
    101.                         o.vertex = UnityObjectToClipPos(v.vertex);
    102.                         #if UNITY_UV_STARTS_AT_TOP
    103.                         float scale = -1.0;
    104.                         #else
    105.                         float scale = 1.0;
    106.                         #endif
    107.                         o.uvgrab.xy = (float2(o.vertex.x, o.vertex.y*scale) + o.vertex.w) * 0.5;
    108.                         o.uvgrab.zw = o.vertex.zw;
    109.                         return o;
    110.                     }
    111.  
    112.                     sampler2D _GrabTexture;
    113.                     float4 _GrabTexture_TexelSize;
    114.                     float _Size;
    115.  
    116.                     half4 frag(v2f i) : COLOR {
    117.  
    118.                         half4 sum = half4(0,0,0,0);
    119.                         #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
    120.  
    121.                         sum += GRABPIXEL(0.05, -4.0);
    122.                         sum += GRABPIXEL(0.09, -3.0);
    123.                         sum += GRABPIXEL(0.12, -2.0);
    124.                         sum += GRABPIXEL(0.15, -1.0);
    125.                         sum += GRABPIXEL(0.18,  0.0);
    126.                         sum += GRABPIXEL(0.15, +1.0);
    127.                         sum += GRABPIXEL(0.12, +2.0);
    128.                         sum += GRABPIXEL(0.09, +3.0);
    129.                         sum += GRABPIXEL(0.05, +4.0);
    130.  
    131.                         return sum;
    132.                     }
    133.                     ENDCG
    134.                 }
    135.  
    136.                 GrabPass {
    137.                     Tags { "LightMode" = "Always" }
    138.                 }
    139.                 Pass {
    140.                     Tags { "LightMode" = "Always" }
    141.  
    142.                     CGPROGRAM
    143.                     #pragma vertex vert
    144.                     #pragma fragment frag
    145.                     #pragma fragmentoption ARB_precision_hint_fastest
    146.                     #include "UnityCG.cginc"
    147.  
    148.                     struct appdata_t {
    149.                         float4 vertex : POSITION;
    150.                         float2 texcoord: TEXCOORD0;
    151.                     };
    152.  
    153.                     struct v2f {
    154.                         float4 vertex : POSITION;
    155.                         float4 uvgrab : TEXCOORD0;
    156.                         float2 uvbump : TEXCOORD1;
    157.                         float2 uvmain : TEXCOORD2;
    158.                     };
    159.  
    160.                     float _BumpAmt;
    161.                     float4 _BumpMap_ST;
    162.                     float4 _MainTex_ST;
    163.  
    164.                     v2f vert(appdata_t v) {
    165.                         v2f o;
    166.                         o.vertex = UnityObjectToClipPos(v.vertex);
    167.                         #if UNITY_UV_STARTS_AT_TOP
    168.                         float scale = -1.0;
    169.                         #else
    170.                         float scale = 1.0;
    171.                         #endif
    172.                         o.uvgrab.xy = (float2(o.vertex.x, o.vertex.y*scale) + o.vertex.w) * 0.5;
    173.                         o.uvgrab.zw = o.vertex.zw;
    174.                         o.uvbump = TRANSFORM_TEX(v.texcoord, _BumpMap);
    175.                         o.uvmain = TRANSFORM_TEX(v.texcoord, _MainTex);
    176.                         return o;
    177.                     }
    178.  
    179.                     fixed4 _Color;
    180.                     sampler2D _GrabTexture;
    181.                     float4 _GrabTexture_TexelSize;
    182.                     sampler2D _BumpMap;
    183.                     sampler2D _MainTex;
    184.  
    185.                     half4 frag(v2f i) : COLOR {
    186.  
    187.                         half2 bump = UnpackNormal(tex2D(_BumpMap, i.uvbump)).rg;
    188.                         float2 offset = bump * _BumpAmt * _GrabTexture_TexelSize.xy;
    189.                         i.uvgrab.xy = offset * i.uvgrab.z + i.uvgrab.xy;
    190.  
    191.                         half4 col = tex2Dproj(_GrabTexture, UNITY_PROJ_COORD(i.uvgrab));
    192.                         half4 tint = tex2D(_MainTex, i.uvmain) * _Color;
    193.  
    194.                         return col * tint;
    195.                     }
    196.                     ENDCG
    197.                 }
    198.             }
    199.         }
    200. }
    Any solution or alternative please do not leave it unanswered.