Search Unity

LWRP Unlit Shader that receives shadows

Discussion in 'Shaders' started by Wojzax, Aug 26, 2019.

  1. Wojzax

    Wojzax

    Joined:
    Jul 22, 2014
    Posts:
    34
    Is that even possible in shader graph?
    Im trying to achieve shader that wont affect texture (so totally unlit) or affect it only with something like self-shading (to do this i've connected all the nodes in Specular PBR to Emission, leaving Albedo black).
    It did gives me desired self-shading (shown on picture), but it doesnt receives any shadows from other objects.
    I've put all nodes to Albedo again, and try to make it look as close to unlit as possible.
    All my attempts however failed, it lights up certain areas, gives too much shadows on others, or (when contrast seems right) desaturate colours. This really affect texture, i want it to be displayed just like it was drawned.



    My second attempt was using this code below, it works (unlit receiving shadows) but i also need double-sided transparency and I cant really figure out how to change it :(

    Code (CSharp):
    1.  
    2. Shader "Unlit With Shadows" {
    3.     Properties {
    4.         _Color ("Main Color", Color) = (1,1,1,1)
    5.         _MainTex ("Base (RGB)", 2D) = "white" {}
    6.     }
    7.     SubShader {
    8.         Tags {"Queue" = "Geometry" "RenderType" = "Opaque"}
    9.         Pass {
    10.             Tags {"LightMode" = "ForwardBase"}
    11.             CGPROGRAM
    12.                 #pragma vertex vert
    13.                 #pragma fragment frag
    14.                 #pragma multi_compile_fwdbase
    15.                 #pragma fragmentoption ARB_fog_exp2
    16.                 #pragma fragmentoption ARB_precision_hint_fastest
    17.                
    18.                 #include "UnityCG.cginc"
    19.                 #include "AutoLight.cginc"
    20.                
    21.                 struct v2f
    22.                 {
    23.                     float4    pos            : SV_POSITION;
    24.                     float2    uv            : TEXCOORD0;
    25.                     LIGHTING_COORDS(1,2)
    26.                 };
    27.                 float4 _MainTex_ST;
    28.                 v2f vert (appdata_tan v)
    29.                 {
    30.                     v2f o;
    31.                    
    32.                     o.pos = mul( UNITY_MATRIX_MVP, v.vertex);
    33.                     o.uv = TRANSFORM_TEX (v.texcoord, _MainTex).xy;
    34.                     TRANSFER_VERTEX_TO_FRAGMENT(o);
    35.                     return o;
    36.                 }
    37.                 sampler2D _MainTex;
    38.                 fixed4 frag(v2f i) : COLOR
    39.                 {
    40.                     fixed atten = LIGHT_ATTENUATION(i);    // Light attenuation + shadows.
    41.                     //fixed atten = SHADOW_ATTENUATION(i); // Shadows ONLY.
    42.                     return tex2D(_MainTex, i.uv) * atten;
    43.                 }
    44.             ENDCG
    45.         }
    46.         Pass {
    47.             Tags {"LightMode" = "ForwardAdd"}
    48.             Blend One One
    49.             CGPROGRAM
    50.                 #pragma vertex vert
    51.                 #pragma fragment frag
    52.                 #pragma multi_compile_fwdadd_fullshadows
    53.                 #pragma fragmentoption ARB_fog_exp2
    54.                 #pragma fragmentoption ARB_precision_hint_fastest
    55.                
    56.                 #include "UnityCG.cginc"
    57.                 #include "AutoLight.cginc"
    58.                
    59.                 struct v2f
    60.                 {
    61.                     float4    pos            : SV_POSITION;
    62.                     float2    uv            : TEXCOORD0;
    63.                     LIGHTING_COORDS(1,2)
    64.                 };
    65.                 float4 _MainTex_ST;
    66.                 v2f vert (appdata_tan v)
    67.                 {
    68.                     v2f o;
    69.                    
    70.                     o.pos = mul( UNITY_MATRIX_MVP, v.vertex);
    71.                     o.uv = TRANSFORM_TEX (v.texcoord, _MainTex).xy;
    72.                     TRANSFER_VERTEX_TO_FRAGMENT(o);
    73.                     return o;
    74.                 }
    75.                 sampler2D _MainTex;
    76.                 fixed4 frag(v2f i) : COLOR
    77.                 {
    78.                     fixed atten = LIGHT_ATTENUATION(i);    // Light attenuation + shadows.
    79.                     //fixed atten = SHADOW_ATTENUATION(i); // Shadows ONLY.
    80.                     return tex2D(_MainTex, i.uv) * atten;
    81.                 }
    82.             ENDCG
    83.         }
    84.     }
    85.     FallBack "VertexLit"
    86. }
    87.