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

Bug Shadows not rendering like its suppose to

Discussion in 'Shaders' started by ShavSkelet, Oct 6, 2023.

  1. ShavSkelet

    ShavSkelet

    Joined:
    Nov 29, 2017
    Posts:
    29
    Im porting an old project made in Unity 2019 and for some reason a shader is not rendering well. The shadows are not projected directly on the object. They are projected like "behind"? I'll show you some images to understand what I mean:

    https://imgur.com/cGdur5T
    https://imgur.com/eGsCbI2

    Also if I put a plane under this object the shadow is projected on this plane but you can see the shadow on the object.

    https://imgur.com/v1ED9P5

    In case you are wandering what shader am I using is this one:


    Code (CSharp):
    1. Shader "VacuumShaders/Terrain To Mesh/Legacy Shaders/Diffuse/8 Textures"
    2. {
    3.     Properties
    4.     {
    5.         _Color("Color", color) = (1, 1, 1, 1)
    6.         [NoScaleOffset]_V_T2M_Control ("Control Map (RGBA)", 2D) = "black" {}
    7.  
    8.         //TTM
    9.         [V_T2M_Layer] _V_T2M_Splat1 ("Layer 1 (R)", 2D) = "white" {}
    10.         [HideInInspector] _V_T2M_Splat1_uvScale("", float) = 1
    11.  
    12.         [V_T2M_Layer] _V_T2M_Splat2 ("Layer 2 (G)", 2D) = "white" {}
    13.         [HideInInspector] _V_T2M_Splat2_uvScale("", float) = 1
    14.  
    15.         [V_T2M_Layer] _V_T2M_Splat3 ("Layer 3 (B)", 2D) = "white" {}
    16.         [HideInInspector] _V_T2M_Splat3_uvScale("", float) = 1
    17.  
    18.         [V_T2M_Layer] _V_T2M_Splat4 ("Layer 4 (A)", 2D) = "white" {}
    19.         [HideInInspector] _V_T2M_Splat4_uvScale("", float) = 1
    20.  
    21.  
    22.      
    23.         [NoScaleOffset]_V_T2M_Control2 ("Control Map 2 (RGBA)", 2D) = "black" {}
    24.  
    25.         //TTM            
    26.         [V_T2M_Layer] _V_T2M_Splat5 ("Layer 1 (R)", 2D) = "white" {}
    27.         [HideInInspector] _V_T2M_Splat5_uvScale("", float) = 1
    28.  
    29.         [V_T2M_Layer] _V_T2M_Splat6 ("Layer 2 (G)", 2D) = "white" {}
    30.         [HideInInspector] _V_T2M_Splat6_uvScale("", float) = 1
    31.  
    32.         [V_T2M_Layer] _V_T2M_Splat7 ("Layer 3 (B)", 2D) = "white" {}
    33.         [HideInInspector] _V_T2M_Splat7_uvScale("", float) = 1
    34.  
    35.         [V_T2M_Layer] _V_T2M_Splat8 ("Layer 4 (A)", 2D) = "white" {}
    36.         [HideInInspector] _V_T2M_Splat8_uvScale("", float) = 1
    37.  
    38.  
    39.  
    40.         //Fallback use only
    41.         [NoScaleOffset]_MainTex ("BaseMap (Fallback use only!)", 2D) = "white" {}
    42.     }
    43.  
    44.     SubShader
    45.     {
    46.         Tags { "RenderType"="Opaque" }
    47.         LOD 200
    48.    
    49.         CGPROGRAM
    50.         #pragma surface surf Lambert vertex:vert
    51.         #pragma target 2.0
    52.         //#pragma exclude_renderers gles
    53.      
    54.  
    55.         #define V_T2M_2_CONTROL_MAPS
    56.         #define V_T2M_3_TEX
    57.         #define V_T2M_4_TEX
    58.         #define V_T2M_5_TEX
    59.         #define V_T2M_6_TEX
    60.         #define V_T2M_7_TEX
    61.         #define V_T2M_8_TEX
    62.  
    63.         #include "../cginc/T2M_Deferred.cginc"    
    64.  
    65.         ENDCG
    66.     }
    67.  
    68.     FallBack "Hidden/VacuumShaders/Fallback/VertexLit"
    69. }

    It includes the T2M_Deferred.cging:


    Code (CSharp):
    1. #ifndef VACUUM_SHADERS_T2M_DEFERRED_CGINC
    2. #define VACUUM_SHADERS_T2M_DEFERRED_CGINC
    3.  
    4. #include "../cginc/T2M_Variables.cginc"
    5. #include "../cginc/CurvedWorld.cginc"
    6.  
    7.  
    8. struct Input
    9. {
    10.     float2 uv_V_T2M_Control;
    11.  
    12.     #ifdef V_T2M_2_CONTROL_MAPS
    13.         float2 uv_V_T2M_Control2;
    14.     #endif
    15. };
    16.  
    17.  
    18. void vert (inout appdata_full v, out Input o)
    19. {
    20.     UNITY_INITIALIZE_OUTPUT(Input,o);
    21.  
    22.     //CurvedWorld vertex transform
    23.     CURVED_WORLD_TRANSFORM_POINT_AND_NORMAL(v.vertex, v.normal, v.tangent);
    24. }
    25.  
    26.  
    27. #ifdef V_T2M_STANDARD
    28. void surf (Input IN, inout SurfaceOutputStandard o)
    29. #else
    30. void surf (Input IN, inout SurfaceOutput o)
    31. #endif
    32. {
    33.     half4 splat_control = tex2D (_V_T2M_Control, IN.uv_V_T2M_Control);
    34.  
    35.     fixed4 mainTex  = splat_control.r * tex2D (_V_T2M_Splat1, IN.uv_V_T2M_Control * _V_T2M_Splat1_uvScale);
    36.            mainTex += splat_control.g * tex2D (_V_T2M_Splat2, IN.uv_V_T2M_Control * _V_T2M_Splat2_uvScale);
    37.  
    38.     #ifdef V_T2M_3_TEX
    39.         mainTex += splat_control.b * tex2D (_V_T2M_Splat3, IN.uv_V_T2M_Control * _V_T2M_Splat3_uvScale);
    40.     #endif
    41.     #ifdef V_T2M_4_TEX
    42.         mainTex += splat_control.a * tex2D (_V_T2M_Splat4, IN.uv_V_T2M_Control * _V_T2M_Splat4_uvScale);
    43.     #endif
    44.  
    45.  
    46.     #ifdef V_T2M_2_CONTROL_MAPS
    47.          half4 splat_control2 = tex2D (_V_T2M_Control2, IN.uv_V_T2M_Control2);
    48.  
    49.          mainTex.rgb += tex2D (_V_T2M_Splat5, IN.uv_V_T2M_Control2 * _V_T2M_Splat5_uvScale) * splat_control2.r;
    50.  
    51.          #ifdef V_T2M_6_TEX
    52.             mainTex.rgb += tex2D (_V_T2M_Splat6, IN.uv_V_T2M_Control2 * _V_T2M_Splat6_uvScale) * splat_control2.g;
    53.          #endif
    54.  
    55.          #ifdef V_T2M_7_TEX
    56.             mainTex.rgb += tex2D (_V_T2M_Splat7, IN.uv_V_T2M_Control2 * _V_T2M_Splat7_uvScale) * splat_control2.b;
    57.          #endif
    58.  
    59.          #ifdef V_T2M_8_TEX
    60.             mainTex.rgb += tex2D (_V_T2M_Splat8, IN.uv_V_T2M_Control2 * _V_T2M_Splat8_uvScale) * splat_control2.a;
    61.          #endif
    62.     #endif
    63.  
    64.  
    65.  
    66.     mainTex.rgb *= _Color.rgb;
    67.  
    68.  
    69.     #ifdef V_T2M_BUMP
    70.         fixed4 nrm = 0.0f;
    71.         nrm += splat_control.r * tex2D(_V_T2M_Splat1_bumpMap, IN.uv_V_T2M_Control * _V_T2M_Splat1_uvScale);
    72.         nrm += splat_control.g * tex2D(_V_T2M_Splat2_bumpMap, IN.uv_V_T2M_Control * _V_T2M_Splat2_uvScale);
    73.  
    74.         #ifdef V_T2M_3_TEX
    75.             nrm += splat_control.b * tex2D (_V_T2M_Splat3_bumpMap, IN.uv_V_T2M_Control * _V_T2M_Splat3_uvScale);
    76.         #endif
    77.  
    78.         #ifdef V_T2M_4_TEX
    79.             nrm += splat_control.a * tex2D (_V_T2M_Splat4_bumpMap, IN.uv_V_T2M_Control * _V_T2M_Splat4_uvScale);
    80.         #endif
    81.      
    82.      
    83.         o.Normal = UnpackNormal(nrm);
    84.     #endif
    85.  
    86.  
    87.  
    88.  
    89.     #ifdef V_T2M_STANDARD
    90.         half metallic = 0;
    91.         metallic += splat_control.r * _V_T2M_Splat1_Metallic;
    92.         metallic += splat_control.g * _V_T2M_Splat2_Metallic;
    93.         #ifdef V_T2M_3_TEX
    94.             metallic += splat_control.b * _V_T2M_Splat3_Metallic;
    95.         #endif
    96.         #ifdef V_T2M_4_TEX
    97.             metallic += splat_control.a * _V_T2M_Splat4_Metallic;
    98.         #endif
    99.         #ifdef V_T2M_2_CONTROL_MAPS
    100.             #ifdef V_T2M_5_TEX
    101.                 metallic += splat_control2.r * _V_T2M_Splat5_Metallic;
    102.             #endif
    103.             #ifdef V_T2M_6_TEX
    104.                 metallic += splat_control2.g * _V_T2M_Splat6_Metallic;
    105.             #endif
    106.             #ifdef V_T2M_7_TEX
    107.                 metallic += splat_control2.b * _V_T2M_Splat7_Metallic;
    108.             #endif
    109.             #ifdef V_T2M_8_TEX
    110.                 metallic += splat_control2.a * _V_T2M_Splat8_Metallic;
    111.             #endif
    112.         #endif
    113.  
    114.  
    115.         half glossiness = 0;
    116.         glossiness += splat_control.r * _V_T2M_Splat1_Glossiness;
    117.         glossiness += splat_control.g * _V_T2M_Splat2_Glossiness;
    118.         #ifdef V_T2M_3_TEX
    119.             glossiness += splat_control.b * _V_T2M_Splat3_Glossiness;
    120.         #endif
    121.         #ifdef V_T2M_4_TEX
    122.             glossiness += splat_control.a * _V_T2M_Splat4_Glossiness;
    123.         #endif
    124.         #ifdef V_T2M_2_CONTROL_MAPS
    125.             #ifdef V_T2M_5_TEX
    126.                 glossiness += splat_control2.r * _V_T2M_Splat5_Glossiness;
    127.             #endif
    128.             #ifdef V_T2M_6_TEX
    129.                 glossiness += splat_control2.g * _V_T2M_Splat6_Glossiness;
    130.             #endif
    131.             #ifdef V_T2M_7_TEX
    132.                 glossiness += splat_control2.b * _V_T2M_Splat7_Glossiness;
    133.             #endif
    134.             #ifdef V_T2M_8_TEX
    135.                 glossiness += splat_control2.a * _V_T2M_Splat8_Glossiness;
    136.             #endif
    137.         #endif
    138.  
    139.         o.Metallic = metallic;
    140.         o.Smoothness = glossiness;
    141.     #else
    142.         #ifdef V_T2M_SPECULAR
    143.             o.Gloss = mainTex.a;
    144.  
    145.             half shininess = 0;
    146.             shininess += splat_control.r * _V_T2M_Splat1_Shininess;
    147.             shininess += splat_control.g * _V_T2M_Splat2_Shininess;
    148.             #ifdef V_T2M_3_TEX
    149.                 shininess += splat_control.b * _V_T2M_Splat3_Shininess;
    150.             #endif
    151.             #ifdef V_T2M_4_TEX
    152.                 shininess += splat_control.a * _V_T2M_Splat4_Shininess;
    153.             #endif
    154.  
    155.             o.Specular = shininess;
    156.         #endif
    157.     #endif
    158.  
    159.     o.Albedo = mainTex.rgb;
    160.     o.Alpha = 1.0;
    161. }
    162.  
    163. #endif

    And the variable Control map is this:


    Code (CSharp):
    1. Shader "Custom/ControlMapShader"
    2. {
    3.     Properties{
    4.         _ControlMap1("Control Map 1", 2D) = "white" {}
    5.         _Layer1R1("Layer 1 Red", 2D) = "white" {}
    6.         _Layer2G1("Layer 2 Green", 2D) = "white" {}
    7.         _Layer3B1("Layer 3 Blue", 2D) = "white" {}
    8.         _Layer4A1("Layer 4 Alpha", 2D) = "white" {}
    9.  
    10.         _ControlMap2("Control Map 2", 2D) = "white" {}
    11.         _Layer1R2("Layer 1 Red 2", 2D) = "white" {}
    12.         _Layer2G2("Layer 2 Green 2", 2D) = "white" {}
    13.         _Layer3B2("Layer 3 Blue 2", 2D) = "white" {}
    14.         _Layer4A2("Layer 4 Alpha 2", 2D) = "white" {}
    15.     }
    16.  
    17.         SubShader{
    18.             Tags { "RenderType" = "Opaque" }
    19.             LOD 100
    20.  
    21.             Pass {
    22.                 CGPROGRAM
    23.                 #pragma vertex vert
    24.                 #pragma fragment frag
    25.                 #pragma multi_compile_fwdbase
    26.                 #include "UnityCG.cginc"
    27.  
    28.                 struct appdata_t {
    29.                     float4 vertex : POSITION;
    30.                     float2 uv1 : TEXCOORD0;
    31.                     float2 uv2 : TEXCOORD1;
    32.                 };
    33.  
    34.                 struct v2f {
    35.                     float2 uv1 : TEXCOORD0;
    36.                     float2 uv2 : TEXCOORD1;
    37.                     float4 vertex : SV_POSITION;
    38.                     float4 shadowCoord : TEXCOORD2;
    39.                 };
    40.  
    41.                 sampler2D _ControlMap1;
    42.                 sampler2D _Layer1R1;
    43.                 sampler2D _Layer2G1;
    44.                 sampler2D _Layer3B1;
    45.                 sampler2D _Layer4A1;
    46.  
    47.                 sampler2D _ControlMap2;
    48.                 sampler2D _Layer1R2;
    49.                 sampler2D _Layer2G2;
    50.                 sampler2D _Layer3B2;
    51.                 sampler2D _Layer4A2;
    52.  
    53.                 v2f vert(appdata_t v) {
    54.                     v2f o;
    55.                     o.vertex = UnityObjectToClipPos(v.vertex);
    56.                     o.uv1 = v.uv1;
    57.                     o.uv2 = v.uv2;
    58.                     o.shadowCoord = ComputeScreenPos(o.vertex);
    59.                     return o;
    60.                 }
    61.  
    62.                 fixed4 frag(v2f i) : SV_Target {
    63.                     // Sample control maps
    64.                     fixed4 controlMap1 = tex2D(_ControlMap1, i.uv1);
    65.                     fixed4 controlMap2 = tex2D(_ControlMap2, i.uv2);
    66.  
    67.                     // Sample layer textures
    68.                     fixed4 layer1 = tex2D(_Layer1R1, i.uv1) * controlMap1.r + tex2D(_Layer1R2, i.uv2) * controlMap2.r;
    69.                     fixed4 layer2 = tex2D(_Layer2G1, i.uv1) * controlMap1.g + tex2D(_Layer2G2, i.uv2) * controlMap2.g;
    70.                     fixed4 layer3 = tex2D(_Layer3B1, i.uv1) * controlMap1.b + tex2D(_Layer3B2, i.uv2) * controlMap2.b;
    71.                     fixed4 layer4 = tex2D(_Layer4A1, i.uv1) * controlMap1.a + tex2D(_Layer4A2, i.uv2) * controlMap2.a;
    72.  
    73.                     // Combine the layers
    74.                     fixed4 finalColor = layer1 + layer2 + layer3 + layer4;
    75.  
    76.                     // Apply shadows (Unity's built-in lighting)
    77.                     #ifdef UNITY_PASS_SHADOWCASTER
    78.                     SHADOW_CASTER_FRAGMENT(i)
    79.                     #endif
    80.  
    81.                     return finalColor;
    82.                 }
    83.                 ENDCG
    84.             }
    85.     }
    86. }
    87.  
    Any help with this? :(
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,238
    I'm somewhat concerned with that line. Using a curved world modification to the main shader, but not having a custom shadow caster pass that also does it is usually going to cause a bad time.
     
  3. ShavSkelet

    ShavSkelet

    Joined:
    Nov 29, 2017
    Posts:
    29
    Yeah sorry there were more includes that I didnt post. The T2M_Deferred has 2 more includes:

    T2M_Variables.cginc:

    Code (CSharp):
    1. #ifndef VACUUM_SHADERS_T2M_VARIABLES_CGINC
    2.  
    3. #define VACUUM_SHADERS_T2M_VARIABLES_CGINC
    4.  
    5. fixed4 _Color;
    6.  
    7. sampler2D _V_T2M_Control;
    8. sampler2D _V_T2M_Splat1; half _V_T2M_Splat1_uvScale;
    9. sampler2D _V_T2M_Splat2; half _V_T2M_Splat2_uvScale;
    10.  
    11. #ifdef V_T2M_BUMP
    12.     sampler2D _V_T2M_Splat1_bumpMap;
    13.     sampler2D _V_T2M_Splat2_bumpMap;
    14. #endif
    15.  
    16. #ifdef V_T2M_3_TEX
    17. sampler2D _V_T2M_Splat3; half _V_T2M_Splat3_uvScale;
    18.  
    19.     #ifdef V_T2M_BUMP
    20.         sampler2D _V_T2M_Splat3_bumpMap;
    21.     #endif
    22. #endif
    23.  
    24. #ifdef V_T2M_4_TEX
    25. sampler2D _V_T2M_Splat4; half _V_T2M_Splat4_uvScale;
    26.  
    27.     #ifdef V_T2M_BUMP
    28.         sampler2D _V_T2M_Splat4_bumpMap;
    29.     #endif
    30. #endif
    31.  
    32. #ifdef V_T2M_2_CONTROL_MAPS
    33.     sampler2D _V_T2M_Control2;
    34.  
    35.     sampler2D _V_T2M_Splat5; half _V_T2M_Splat5_uvScale;
    36.     #ifdef V_T2M_BUMP
    37.         sampler2D _V_T2M_Splat5_bumpMap;
    38.     #endif
    39.  
    40.     #ifdef V_T2M_6_TEX
    41.         sampler2D _V_T2M_Splat6; half _V_T2M_Splat6_uvScale;
    42.         #ifdef V_T2M_BUMP
    43.             sampler2D _V_T2M_Splat6_bumpMap;
    44.         #endif
    45.     #endif
    46.  
    47.     #ifdef V_T2M_7_TEX
    48.         sampler2D _V_T2M_Splat7; half _V_T2M_Splat7_uvScale;
    49.     #endif
    50.  
    51.     #ifdef V_T2M_8_TEX
    52.         sampler2D _V_T2M_Splat8; half _V_T2M_Splat8_uvScale;
    53.     #endif
    54. #endif
    55.  
    56. #ifdef V_T2M_SPECULAR
    57.     half _V_T2M_Splat1_Shininess;
    58.     half _V_T2M_Splat2_Shininess;
    59.  
    60.     #ifdef V_T2M_3_TEX
    61.         half _V_T2M_Splat3_Shininess;
    62.     #endif
    63.     #ifdef V_T2M_4_TEX
    64.         half _V_T2M_Splat4_Shininess;
    65.     #endif
    66. #endif
    67.  
    68. #ifdef V_T2M_STANDARD
    69.     half _V_T2M_Splat1_Glossiness;
    70.     half _V_T2M_Splat1_Metallic;
    71.  
    72.     half _V_T2M_Splat2_Glossiness;
    73.     half _V_T2M_Splat2_Metallic;
    74.  
    75.     #ifdef V_T2M_3_TEX
    76.         half _V_T2M_Splat3_Glossiness;
    77.         half _V_T2M_Splat3_Metallic;
    78.     #endif
    79.  
    80.     #ifdef V_T2M_4_TEX
    81.         half _V_T2M_Splat4_Glossiness;
    82.         half _V_T2M_Splat4_Metallic;
    83.     #endif
    84.  
    85.     #ifdef V_T2M_5_TEX
    86.         half _V_T2M_Splat5_Glossiness;
    87.         half _V_T2M_Splat5_Metallic;
    88.     #endif
    89.  
    90.     #ifdef V_T2M_6_TEX
    91.         half _V_T2M_Splat6_Glossiness;
    92.         half _V_T2M_Splat6_Metallic;
    93.     #endif
    94.  
    95.     #ifdef V_T2M_7_TEX
    96.         half _V_T2M_Splat7_Glossiness;
    97.         half _V_T2M_Splat7_Metallic;
    98.     #endif
    99.  
    100.     #ifdef V_T2M_8_TEX
    101.         half _V_T2M_Splat8_Glossiness;
    102.         half _V_T2M_Splat8_Metallic;
    103.     #endif
    104. #endif
    105.  
    106. #endif
    107.  
    And CuveWorld.cginc:


    Code (CSharp):
    1.  
    2. //Uncomment line below and set it to be the correct path to the 'CurvedWorld_Base.cginc' file (part of the Curved World package).
    3. //#include "Assets/VacuumShaders/Curved World/Shaders/cginc/CurvedWorld_Base.cginc"
    4.  
    5.  
    6. #ifndef CURVED_WORLD_TRANSFORM_POINT_AND_NORMAL
    7.     #define CURVED_WORLD_TRANSFORM_POINT_AND_NORMAL(vertex,normal,tangent)
    8. #endif
    9.  
    10. #ifndef CURVED_WORLD_TRANSFORM_POINT
    11.     #define CURVED_WORLD_TRANSFORM_POINT(vertex)
    12. #endif

    Also this shader was on a plugin (Terrain to mesh) and is responsible to bake the terrain textures into one using the RGBA channels and painting each texture in each channel.