Search Unity

Shader does not work under LWRP but does not throw any errors

Discussion in 'Universal Render Pipeline' started by QFSW, Nov 27, 2019.

  1. QFSW

    QFSW

    Joined:
    Mar 24, 2015
    Posts:
    2,906
    Hi,

    I have a vert/frag shader that does not use any surf calls, so should work in LWRP but is not. It also doesn't throw any errors. How would one recommend investigating it?

    upload_2019-11-27_9-16-16.png
    upload_2019-11-27_9-16-52.png

    Code (CSharp):
    1. // Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
    2.  
    3. Shader "QFSW/Blur"
    4. {
    5.     Properties
    6.     {
    7.         _Color("Main Color", Color) = (1,1,1,1)
    8.         _MainTex("Tint Color (RGB)", 2D) = "white" {}
    9.         _Radius("Radius", Range(0, 20)) = 1
    10.     }
    11.  
    12.     Category
    13.     {
    14.  
    15.     Tags{ "Queue" = "Transparent" "IgnoreProjector" = "True" "RenderType" = "Opaque" }
    16.     SubShader{
    17.  
    18.     GrabPass{
    19.     Tags{ "LightMode" = "Always" }
    20.     }
    21.     Pass{
    22.     Tags{ "LightMode" = "Always" }
    23.  
    24.     CGPROGRAM
    25. #pragma vertex vert
    26. #pragma fragment frag
    27. #pragma fragmentoption ARB_precision_hint_fastest
    28. #include "UnityCG.cginc"
    29.  
    30.     struct appdata_t
    31.     {
    32.         float4 vertex : POSITION;
    33.         float2 texcoord: TEXCOORD0;
    34.         fixed4 color : COLOR;
    35.     };
    36.  
    37.     struct v2f
    38.     {
    39.         float4 vertex : POSITION;
    40.         float4 uvgrab : TEXCOORD0;
    41.         fixed4 color : COLOR;
    42.     };
    43.  
    44.     v2f vert(appdata_t v)
    45.     {
    46.         v2f o;
    47.         o.vertex = UnityObjectToClipPos(v.vertex);
    48. #if UNITY_UV_STARTS_AT_TOP
    49.         float scale = -1.0;
    50. #else
    51.         float scale = 1.0;
    52. #endif
    53.         o.uvgrab.xy = (float2(o.vertex.x, o.vertex.y*scale) + o.vertex.w) * 0.5;
    54.         o.uvgrab.zw = o.vertex.zw;
    55.         o.color = v.color;
    56.         return o;
    57.     }
    58.  
    59.     sampler2D _GrabTexture;
    60.     float4 _GrabTexture_TexelSize;
    61.     uniform float _Radius;
    62.     uniform float _BlurMultiplier = 1;
    63.  
    64.     half4 frag(v2f i) : COLOR
    65.     {
    66.         _Radius *= _BlurMultiplier;
    67.         _Radius *= i.color.a;
    68.         half4 sum = half4(0,0,0,0);
    69. #define GRABPIXEL(weight, kernelx) tex2Dproj(_GrabTexture, UNITY_PROJ_COORD(float4(i.uvgrab.x + _GrabTexture_TexelSize.x * kernelx * _Radius, i.uvgrab.y, i.uvgrab.z, i.uvgrab.w))) * weight
    70.         sum += GRABPIXEL(0.05, -4.0);
    71.         sum += GRABPIXEL(0.09, -3.0);
    72.         sum += GRABPIXEL(0.12, -2.0);
    73.         sum += GRABPIXEL(0.15, -1.0);
    74.         sum += GRABPIXEL(0.18,  0.0);
    75.         sum += GRABPIXEL(0.15, +1.0);
    76.         sum += GRABPIXEL(0.12, +2.0);
    77.         sum += GRABPIXEL(0.09, +3.0);
    78.         sum += GRABPIXEL(0.05, +4.0);
    79.  
    80.         return sum;
    81.     }
    82.         ENDCG
    83.     }
    84.  
    85.     GrabPass
    86.     {
    87.         Tags{ "LightMode" = "Always" }
    88.     }
    89.     Pass{
    90.         Tags{ "LightMode" = "Always" }
    91.  
    92.         CGPROGRAM
    93. #pragma vertex vert
    94. #pragma fragment frag
    95. #pragma fragmentoption ARB_precision_hint_fastest
    96. #include "UnityCG.cginc"
    97.  
    98.         struct appdata_t
    99.         {
    100.             float4 vertex : POSITION;
    101.             float2 texcoord: TEXCOORD0;
    102.             fixed4 color : COLOR;
    103.         };
    104.  
    105.     struct v2f {
    106.         float4 vertex : POSITION;
    107.         float4 uvgrab : TEXCOORD0;
    108.         fixed4 color : COLOR;
    109.     };
    110.  
    111.     v2f vert(appdata_t v) {
    112.         v2f o;
    113.         o.vertex = UnityObjectToClipPos(v.vertex);
    114. #if UNITY_UV_STARTS_AT_TOP
    115.         float scale = -1.0;
    116. #else
    117.         float scale = 1.0;
    118. #endif
    119.         o.uvgrab.xy = (float2(o.vertex.x, o.vertex.y*scale) + o.vertex.w) * 0.5;
    120.         o.uvgrab.zw = o.vertex.zw;
    121.         o.color = v.color;
    122.         return o;
    123.     }
    124.  
    125.     sampler2D _GrabTexture;
    126.     float4 _GrabTexture_TexelSize;
    127.     uniform float _Radius;
    128.     uniform float _BlurMultiplier = 1;
    129.  
    130.     half4 frag(v2f i) : COLOR{
    131.         _Radius *= _BlurMultiplier;
    132.         _Radius *= i.color.a;
    133.         half4 sum = half4(0,0,0,0);
    134. #define GRABPIXEL(weight,kernely) tex2Dproj( _GrabTexture, UNITY_PROJ_COORD(float4(i.uvgrab.x, i.uvgrab.y + _GrabTexture_TexelSize.y * kernely*_Radius, i.uvgrab.z, i.uvgrab.w))) * weight
    135.  
    136.         sum += GRABPIXEL(0.05, -4.0);
    137.         sum += GRABPIXEL(0.09, -3.0);
    138.         sum += GRABPIXEL(0.12, -2.0);
    139.         sum += GRABPIXEL(0.15, -1.0);
    140.         sum += GRABPIXEL(0.18,  0.0);
    141.         sum += GRABPIXEL(0.15, +1.0);
    142.         sum += GRABPIXEL(0.12, +2.0);
    143.         sum += GRABPIXEL(0.09, +3.0);
    144.         sum += GRABPIXEL(0.05, +4.0);
    145.  
    146.         return sum;
    147.     }
    148.         ENDCG
    149.     }
    150.  
    151.         GrabPass{
    152.         Tags{ "LightMode" = "Always" }
    153.     }
    154.         Pass{
    155.         Tags{ "LightMode" = "Always" }
    156.  
    157.         CGPROGRAM
    158. #pragma vertex vert
    159. #pragma fragment frag
    160. #pragma fragmentoption ARB_precision_hint_fastest
    161. #include "UnityCG.cginc"
    162.  
    163.         struct appdata_t {
    164.         float4 vertex : POSITION;
    165.         float2 texcoord: TEXCOORD0;
    166.     };
    167.  
    168.     struct v2f
    169.     {
    170.         float4 vertex : POSITION;
    171.         float4 uvgrab : TEXCOORD0;
    172.         float2 uvmain : TEXCOORD1;
    173.     };
    174.  
    175.     float4 _MainTex_ST;
    176.  
    177.     v2f vert(appdata_t v) {
    178.         v2f o;
    179.         o.vertex = UnityObjectToClipPos(v.vertex);
    180. #if UNITY_UV_STARTS_AT_TOP
    181.         float scale = -1.0;
    182. #else
    183.         float scale = 1.0;
    184. #endif
    185.         o.uvgrab.xy = (float2(o.vertex.x, o.vertex.y*scale) + o.vertex.w) * 0.5;
    186.         o.uvgrab.zw = o.vertex.zw;
    187.         o.uvmain = TRANSFORM_TEX(v.texcoord, _MainTex);
    188.         return o;
    189.     }
    190.  
    191.     fixed4 _Color;
    192.     sampler2D _GrabTexture;
    193.     float4 _GrabTexture_TexelSize;
    194.     sampler2D _MainTex;
    195.  
    196.     half4 frag(v2f i) : COLOR
    197.     {
    198.         half4 col = tex2Dproj(_GrabTexture, UNITY_PROJ_COORD(i.uvgrab));
    199.         half4 tint = tex2D(_MainTex, i.uvmain) * _Color;
    200.  
    201.         return col * tint;
    202.     }
    203.         ENDCG
    204.     }
    205.     }
    206.     }
    207. }
     
  2. Andre_Mcgrail

    Andre_Mcgrail

    Unity Technologies

    Joined:
    Dec 9, 2016
    Posts:
    244
    Hey,

    So this shader does indeed look like it is rendering as expected, there are two things I see here, first is the material preview being pink is probably down to a caching issue and reimporting the material should re-generate a correct thumbnail.
    The second issue is that you are using GrabPass throughout the shader which is not supported in LWRP(Universal RP) this has been superseded by the CameraOpaqueTexture for efficiency sakes.

    Here I assume you are wanting to create a UI that blurs the rendering behind it? if so @Andy-Touch created a nice example in our renderer examples here, it is not quite finished up but works and shows the idea.

    Unity_ePPPf3bT31.png
     
    QFSW likes this.
  3. QFSW

    QFSW

    Joined:
    Mar 24, 2015
    Posts:
    2,906
    Does
    CameraOpaqueTexture
    work in standard and earlier versions of Unity? Or will I need two versions of the shader?
     
  4. QFSW

    QFSW

    Joined:
    Mar 24, 2015
    Posts:
    2,906
    And yes this is what I am doing, here is how it looks without an SRP in use
    upload_2019-11-27_12-17-16.png