Search Unity

  1. The 2022.1 beta is now available for testing. To find out what's new, have a look at our 2022.1 beta blog post.
    Dismiss Notice

Converting a Custom Shader to LWRP?

Discussion in 'Shaders' started by Edchina, Oct 7, 2019.

  1. Edchina

    Edchina

    Joined:
    Jun 6, 2017
    Posts:
    4

    Hello everyone, I'm currently stuck in an issue with Lightweight Render Pipeline and I was wondering if someone can help me out. I'm very new to Shaders and I need to have one for a college project: a shader that renders the object invisible (or transparent), but still allows it to receive shadows. I know it's a tricky thing to do and after plenty of research I found this link that does exaclty that: https://github.com/keijiro/ShadowDrawer

    The only problem is, it's not made on LWRP, and since it's such a new feature, there's not much online on how to convert shaders to the LWRP code. Is there any way to make such shader work in LWRP? Any way to convert the code?
    Code (CSharp):
    1. Shader "Custom/ShadowDrawer"
    2. {
    3.     Properties
    4.     {
    5.         _Color ("Shadow Color", Color) = (0, 0, 0, 0.6)
    6.     }
    7.  
    8.     CGINCLUDE
    9.  
    10.     #include "UnityCG.cginc"
    11.     #include "AutoLight.cginc"
    12.  
    13.     struct v2f_shadow {
    14.         float4 pos : SV_POSITION;
    15.         LIGHTING_COORDS(0, 1)
    16.     };
    17.  
    18.     half4 _Color;
    19.  
    20.     v2f_shadow vert_shadow(appdata_full v)
    21.     {
    22.         v2f_shadow o;
    23.         o.pos = UnityObjectToClipPos(v.vertex);
    24.         TRANSFER_VERTEX_TO_FRAGMENT(o);
    25.         return o;
    26.     }
    27.  
    28.     half4 frag_shadow(v2f_shadow IN) : SV_Target
    29.     {
    30.         half atten = LIGHT_ATTENUATION(IN);
    31.         return half4(_Color.rgb, lerp(_Color.a, 0, atten));
    32.     }
    33.  
    34.     ENDCG
    35.  
    36.     SubShader
    37.     {
    38.         Tags { "Queue"="AlphaTest+49" }
    39.  
    40.         // Depth fill pass
    41.         Pass
    42.         {
    43.             ColorMask 0
    44.  
    45.             CGPROGRAM
    46.  
    47.             #pragma vertex vert
    48.             #pragma fragment frag
    49.  
    50.             struct v2f {
    51.                 float4 pos : SV_POSITION;
    52.             };
    53.  
    54.             v2f vert(appdata_full v)
    55.             {
    56.                 v2f o;
    57.                 o.pos = UnityObjectToClipPos (v.vertex);
    58.                 return o;
    59.             }
    60.  
    61.             half4 frag(v2f IN) : SV_Target
    62.             {
    63.                 return (half4)0;
    64.             }
    65.  
    66.             ENDCG
    67.         }
    68.  
    69.         // Forward base pass
    70.         Pass
    71.         {
    72.             Tags { "LightMode" = "ForwardBase" }
    73.             Blend SrcAlpha OneMinusSrcAlpha
    74.             CGPROGRAM
    75.             #pragma vertex vert_shadow
    76.             #pragma fragment frag_shadow
    77.             #pragma multi_compile_fwdbase
    78.             ENDCG
    79.         }
    80.  
    81.         // Forward add pass
    82.         Pass
    83.         {
    84.             Tags { "LightMode" = "ForwardAdd" }
    85.             Blend SrcAlpha OneMinusSrcAlpha
    86.             CGPROGRAM
    87.             #pragma vertex vert_shadow
    88.             #pragma fragment frag_shadow
    89.             #pragma multi_compile_fwdadd_fullshadows
    90.             ENDCG
    91.         }
    92.     }
    93.     FallBack "Mobile/VertexLit"
    94. }
    Even if there isn't a way to convert this code to LWRP, does anyone know a way to make a transparent object that receives shadows that works in LWRP?
     
  2. MadeFromPolygons

    MadeFromPolygons

    Joined:
    Oct 5, 2013
    Posts:
    3,350
    There is definately a way to convert the code to work, but thats the extent of my knowledge because unfortunately I havent kept up with URP written shader API so I dont know what is required.

    For now while waiting for replies from somone more knowledgeable, you could open up the code for the simplelit shader and compare to see what #include and pragma statements are missing / incorrect on yours and in general try to align it to match using that as a template. That should get you close if it doesnt get you all the way! Thats how I converted a shader about 8 months ago and at the time there also was not much information regarding this.
     
  3. Edchina

    Edchina

    Joined:
    Jun 6, 2017
    Posts:
    4
    Thanks for the reply! I barely have any experience with this, but I'll try it out!
     
  4. MadeFromPolygons

    MadeFromPolygons

    Joined:
    Oct 5, 2013
    Posts:
    3,350
unityunity