Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question _Color property and shadow caster problem.

Discussion in 'Shaders' started by R4s3tsu, Sep 9, 2020.

  1. R4s3tsu

    R4s3tsu

    Joined:
    Mar 21, 2020
    Posts:
    12
    Hello,

    I need help with configuring the shader to receive the shadows from other objects that don't have _Color property.

    I was having the strange problems with this shader, some objects was not casting the shadows on it, so I was looking for what is causing it and found some old thread here that gave me the tip, so I have found the main problem - the shader cannot receive the shadows from the objects with the shaders without _Color property.
    I cannot add _Color to those existing shaders, cos it will need to change a lot of them, instead, I need to configure the shadow caster on this particular shader so it can receive the shadows from it. How it can be done?

    Code (CSharp):
    1. Shader "Hidden/LightingPass"
    2. {
    3.     Properties
    4.     {
    5.         _Color ("Main Color", Color) = (1,1,1,1)
    6.         _MainTex ("Base (RGB)", 2D) = "white" {}
    7.         _Cutoff("Mask Clip Value", Float) = 0.5
    8.         [HideInInspector] __dirty("", Int) = 1
    9.  
    10.     }
    11.  
    12.     //SSS pass
    13.     SubShader
    14.     {
    15.         //Tags { "RenderType" = "SSS" }
    16.         Tags { "Queue" = "AlphaTest-50" "RenderType" = "Opaque" }
    17.         LOD 100
    18.  
    19.         CGPROGRAM
    20.         //SSS_LightingPass
    21.         #pragma surface surf SSS_LightingPass vertex:vert fullforwardshadows nometa nodynlightmap nodirlightmap nofog noshadowmask
    22.         #pragma target 3.0
    23.         #include "../Resources/SSS_Common.hlsl"
    24.         //#pragma multi_compile _ TRANSMISSION
    25.         //#pragma multi_compile _ SUBSURFACE_ALBEDO
    26.         #pragma multi_compile _ ENABLE_ALPHA_TEST
    27.  
    28.  
    29.         half _Cutoff;
    30.  
    31.     struct Input {
    32.         float2 uv_MainTex;
    33.         float3 worldNormal;
    34.         float4 screenPos;
    35.  
    36.         INTERNAL_DATA
    37.     };
    38.  
    39.     struct DataStructure
    40.     {
    41.         fixed3 Albedo;  // diffuse color
    42.         fixed3 Normal;  // tangent space normal, if written
    43.         fixed3 Emission;
    44.         fixed Alpha;
    45.         fixed3 Occlusion;
    46.         fixed Glossiness;
    47.         fixed3 Transmission;
    48.         fixed2 screenUV;
    49.     };
    50.  
    51.     void vert(inout appdata_full v, out Input o)
    52.     {
    53.     UNITY_INITIALIZE_OUTPUT(Input, o);
    54.     //experimental
    55.     //v.vertex.xyz += v.normal * .0001;
    56.     }
    57.     half4 LightingSSS_LightingPass(DataStructure s, half3 lightDir, half3 viewDir, half atten)
    58.     {
    59.     //#if defined (__INTELLISENSE__)
    60.     //#define TRANSMISSION
    61.     //#endif
    62.  
    63.     half NdotL = max(0.0, dot(lightDir, s.Normal));
    64.     half3 Lighting = atten * _LightColor0.rgb;
    65.  
    66.     half3 Diffuse = Lighting * NdotL * s.Albedo;
    67.      
    68.     //return float4(Lighting, atten);
    69.  
    70.     //#ifdef TRANSMISSION
    71.     Diffuse += ADDITIVE_PASS_TRANSMISSION
    72.     //#endif
    73.     //return saturate (dot(normalize(viewDir), normalize(-lightDir)));
    74.     return float4(Diffuse, 1);
    75.          
    76.     }
    77.  
    78.     // Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.
    79.         // See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
    80.         // #pragma instancing_options assumeuniformscaling
    81.         UNITY_INSTANCING_BUFFER_START(Props)
    82.             // put more per-instance properties here
    83.         UNITY_INSTANCING_BUFFER_END(Props)
    84.  
    85.     void surf (Input IN, inout DataStructure o)
    86.     {
    87.     //if (SSS_shader != 1)discard;
    88.     //#if defined (__INTELLISENSE__)
    89.     //#define TRANSMISSION
    90.     //#endif
    91.  
    92.     half2 uv = IN.uv_MainTex;
    93.  
    94.     SSS_OCCLUSION
    95.  
    96.     //#ifdef SUBSURFACE_ALBEDO
    97.     //To be correct (PB) this should be multiplied by 1 - specular
    98.     _Color.rgb *= lerp(1.0, tex2D(_SubsurfaceAlbedo, uv).rgb, _SubsurfaceAlbedoOpacity);
    99.     //#endif
    100.     float3 MainTex = 0, Final = 0;
    101.     if (SSS_shader != 1)
    102.     //Final = tex2D(_MainTex, IN.uv_MainTex).xyz;
    103.     //Final = 0.5;
    104.     Final = 0;
    105.     else
    106.     Final = _Color.rgb * OcclusionColored.rgb;
    107.  
    108.     #ifdef ENABLE_ALPHA_TEST
    109.     clip(tex2D(_MainTex, IN.uv_MainTex).a - _Cutoff);
    110.     #endif
    111.  
    112.     o.Albedo = Final;
    113.     o.Alpha = 1;
    114.     o.Normal = BumpMap(uv);
    115.     float3 Emission = 0;
    116.  
    117.     //#ifdef TRANSMISSION
    118.     BASE_TRANSMISSION
    119.     //#endif
    120.     o.Emission = Emission;
    121.     float4 coords = UNITY_PROJ_COORD(IN.screenPos);
    122.     coords.w += 1e-9f;
    123.     float2 screenUV = coords.xy / coords.w;
    124.     o.screenUV = screenUV;
    125.     }
    126.     ENDCG
    127.     }
    128.  
    129.     Opaque pass
    130.     SubShader
    131.     {
    132.         Tags { "Queue" = "AlphaTest-50" "RenderType" = "Opaque" }
    133.         Pass {
    134.         Name "ShadowCaster"
    135.         Tags { "LightMode" = "ShadowCaster" }
    136.  
    137.         Fog {Mode Off}
    138.  
    139.         Offset 1, 1
    140.  
    141.         CGPROGRAM
    142.         #pragma vertex vert
    143.         #pragma fragment frag
    144.         #pragma multi_compile_shadowcaster
    145.         #include "UnityCG.cginc"
    146.  
    147.         struct v2f {
    148.             V2F_SHADOW_CASTER;
    149.         };
    150.  
    151.         v2f vert(appdata_base v)
    152.         {
    153.             v2f o;
    154.             TRANSFER_SHADOW_CASTER(o)
    155.             TRANSFER_SHADOW_CASTER_NORMALOFFSET(o)
    156.  
    157.             return o;
    158.         }
    159.  
    160.         float4 frag(v2f i) : SV_Target
    161.         {
    162.             SHADOW_CASTER_FRAGMENT(i)
    163.         }
    164.         ENDCG
    165.  
    166.         }
    167.     }
    168.     //Cutout pass
    169.     SubShader
    170.     {
    171.         Tags{ "RenderType" = "TransparentCutout" "LightMode" = "ShadowCaster"}
    172.  
    173.         CGPROGRAM
    174.         #pragma target 3.0
    175.         #pragma surface surf Unlit  addshadow fullforwardshadows noshadow noambient novertexlights nolightmap  nodynlightmap nodirlightmap nofog nometa noforwardadd
    176.  
    177.  
    178.         sampler2D _MainTex;
    179.         fixed4 _Color;
    180.  
    181.         struct Input {
    182.             float2 uv_MainTex;
    183. };
    184.         uniform float _Cutoff = 0.94;
    185.         inline half4 LightingUnlit(SurfaceOutput s, half3 lightDir, half atten)
    186.         {
    187.             return half4 (0, 0, 0, s.Alpha);
    188.         }
    189.         void surf(Input i , inout SurfaceOutput o)
    190.         {
    191.             fixed4 c = tex2D(_MainTex, i.uv_MainTex) * _Color;
    192.             o.Albedo = 0;
    193.             o.Alpha = c.a;
    194.             clip(c.a - _Cutoff);
    195.         }
    196.  
    197.         ENDCG
    198.      
    199.         }
    200.      
    201.  
    202. Fallback "Legacy Shaders/VertexLit"
    203.  
    204.  
    205. }

    My experience with vertex frag shaders are very poor. I've tried to set Fallback "Diffuse" but no luck. so I'm looking forward to your help.
     
    Last edited: Sep 10, 2020
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,238
    That shader is ... curious. And makes me think it's a red herring. The one line in that shader that is using
    _Color
    is never going to ever get used because it's in the third
    SubShader
    , and apart from very specific and rare cases, only the first
    SubShader
    of any shader is actually used. The rest are straight up ignored.

    Even if modifying that shader does fix it, it will just break any shader that does use
    _Color
    to fade out. Adding
    _Color
    to the existing shaders is way easier. The problem is any shader that doesn't have a
    _Color
    property will mean it'll be seen as having an unintialized
    _Color
    value, so it'll use a default ... which is (0,0,0,0) ... in other words it'll be set to have an alpha of zero and the above replacement shader will cause it to be clipped.

    The trick is to add this line to all of the shaders that have the problem of not casting shadows:
    [HideInInspector] _Color ("", Color) = (1,1,1,1)


    You don't need to modify any materials, it should auto-fix itself... unless for some reason those materials have their
    _Color
    set to black already for some reason.
     
  3. R4s3tsu

    R4s3tsu

    Joined:
    Mar 21, 2020
    Posts:
    12
    Hello,
    This shader is used only for screen space effect and used as an underlay diffuse pass and applied in screen space to main shader which is standard shader and has the rest values like specularity and all math, but it's diffuse pass and shadows is replaced by this LightingPass shader.

    Ok, first of I cannot modify existing shaders without _color cos they are 3rd party and compiled. And replacing all of them is not the case for me. I cannot do this cos I don't have a source code. Cos I make a game modification actually.
    They are basically the standard surface shaders, but they have _BaseColor and _MainTex property instead of _Color I guess. So I need to make it receive all types of shadows.

    Second, if I remove or modify Opaqe Pass subshader then I got shadow acne, and such things, and the shader stop receiving the light bias.
    If I remove the CutoutPass subshader it loses all shadows, and not receiving it.

    https://imgbox.com/4rSh1jZF
     
  4. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,238
    Well, like I said, there’s only one line that uses
    _Color
    in the above shader. Remove that?
     
  5. R4s3tsu

    R4s3tsu

    Joined:
    Mar 21, 2020
    Posts:
    12
    Well, I've been testing it recently, I've tried to change some numbers here:


    void surf(Input i , inout SurfaceOutput o)
    {
    fixed4 c = tex2D(_MainTex, i.uv_MainTex) * _Color;
    o.Albedo = 0;
    o.Alpha = c.a;
    clip(c.a - _Cutoff);
    }


    Actually tried to remove _Color from this, use 1 instead, use 1 in o.Alpha and other stuff. And it's like it's almost not reacting to it at all.If I changed the name to Color1 for example, its not receiving shadows from other objects at all.

    so probably the thing is not there, maybe I need to change something there, but what exactly getting this weird issue?


    SubShader
    {
    Tags { "Queue" = "Geometry" "RenderType" = "Opaque" }
    Pass {
    Name "ShadowCaster"
    Tags { "LightMode" = "ShadowCaster" }

    Fog {Mode Off}

    Offset 1, 1

    CGPROGRAM
    #pragma vertex vert
    #pragma fragment frag
    #pragma multi_compile_shadowcaster
    #include "UnityCG.cginc"

    struct v2f {
    V2F_SHADOW_CASTER;
    };

    v2f vert(appdata_base v)
    {
    v2f o;
    TRANSFER_SHADOW_CASTER(o)
    TRANSFER_SHADOW_CASTER_NORMALOFFSET(o)

    return o;
    }

    float4 frag(v2f i) : SV_Target
    {
    SHADOW_CASTER_FRAGMENT(i)
    }
    ENDCG

    }
    }


    This probably a shadow caster problem and the thing is that other shaders receiving shadows from shaders without _Color correctly.
     
    Last edited: Sep 13, 2020