Search Unity

Question Shadow Caster Pass using the same Texture for all Materials

Discussion in 'Shaders' started by MsticFox, Feb 13, 2023.

  1. MsticFox

    MsticFox

    Joined:
    Dec 7, 2022
    Posts:
    2
    I am trying to write a shader that uses the alpha of the Main Texture in the Shadow Caster pass in Unity 2022.2.6. Currently the shader writes to the ShadowCaster Pass but it only uses one Texture for all Materials. I have looked into the Frame Debugger and there the Main Texture is always the same.

    upload_2023-2-13_16-43-53.png

    Am I missing something or is it some kind of Bug with the SRP Batcher?

    The Renderer Data Settings:
    upload_2023-2-13_16-50-18.png

    The Render Pipeline Settings:

    upload_2023-2-13_16-50-50.png

    This is the Shader I am working on:
    Code (HLSL):
    1. Shader "Question/Lit"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex ("Albedo Texture", 2D) = "white" {}
    6.     }
    7.     CustomEditor "Ulisses.Hexxen1733.Shader.Editor.CelShaderEditor"
    8.     SubShader
    9.     {
    10.         Tags { "RenderType"="Opaque" "RenderPipeline"="UniversalRenderPipeline"}
    11.      
    12.         // this pass renders the shadows
    13.         Pass{
    14.             Name "ShadowCaster"
    15.             Tags { "LightMode"="ShadowCaster"}
    16.             ZWrite On
    17.             ZTest LEqual
    18.             ColorMask 0
    19.  
    20.             CGPROGRAM
    21.             #pragma vertex vert
    22.             #pragma fragment frag
    23.             #pragma multi_compile_instancing
    24.             #pragma multi_compile_shadowcaster
    25.             #pragma multi_compile _ DOTS_INSTANCING_ON
    26.             #include "UnityCG.cginc"
    27.  
    28.             CBUFFER_START(UnityPerMaterial)
    29.                 sampler2D _MainTex;
    30.                 float4 _MainTex_ST;
    31.             CBUFFER_END
    32.  
    33.             struct v2f
    34.             {
    35.                 V2F_SHADOW_CASTER;
    36.                 float2 uv : TEXCOORD1;
    37.             };
    38.  
    39.             v2f vert(appdata_full v )
    40.             {
    41.                 v2f o;
    42.                 o.uv = v.texcoord;
    43.                 TRANSFER_SHADOW_CASTER(o)
    44.  
    45.             return o;
    46.             }
    47.  
    48.             void frag(v2f i)
    49.             {
    50.                 float alpha = tex2D (_MainTex, i.uv).a;
    51.                 clip(alpha -0.5f);
    52.             }
    53.             ENDCG
    54.         }
    55.      
    56.         // This Pass Renders the Depth of to the Depth Texture
    57.         Pass{
    58.             Name "Depth"
    59.             Tags { "LightMode"="DepthOnly" }
    60.             ZWrite On
    61.             ZTest LEqual
    62.             Cull BACK
    63.  
    64.             CGPROGRAM
    65.  
    66.             #pragma vertex vert
    67.             #pragma fragment frag
    68.             #pragma multi_compile_shadowcaster
    69.             #include "UnityCG.cginc"
    70.  
    71.             CBUFFER_START(UnityPerMaterial)
    72.                 sampler2D _MainTex;
    73.                 float4 _MainTex_ST;
    74.             CBUFFER_END
    75.  
    76.             struct appdata{
    77.                 float2 uv           : TEXCOORD0;
    78.             };
    79.  
    80.             struct v2f{
    81.                 V2F_SHADOW_CASTER;
    82.                 float2 uv : TEXCOORD1;
    83.             };
    84.  
    85.  
    86.             v2f vert(appdata_full v ){
    87.                 v2f o;
    88.                 o.uv = v.texcoord;
    89.                 TRANSFER_SHADOW_CASTER(o)
    90.                 return o;
    91.             }
    92.  
    93.             float4 frag(v2f i) : COLOR{
    94.                 float alpha = tex2D (_MainTex, i.uv).a;
    95.                 clip(alpha -0.5f);
    96.                 SHADOW_CASTER_FRAGMENT(i)
    97.             }
    98.             ENDCG
    99.         }
    100.  
    101.         // This Pass Renders the final color
    102.         Pass
    103.         {
    104.             Tags { "LightMode"="UniversalForward" }
    105.          
    106.             Blend One Zero
    107.             AlphaToMask On
    108.          
    109.             HLSLPROGRAM
    110.             #pragma vertex vert
    111.             #pragma fragment frag
    112.             #pragma multi_compile _ _MAIN_LIGHT_SHADOWS
    113.             #pragma multi_compile _ _MAIN_LIGHT_SHADOWS_CASCADE
    114.             #pragma multi_compile _ _SHADOWS_SOFT
    115.  
    116.             #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
    117.             #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
    118.             #include "Utility/HeXXen1733_Utility.hlsl"
    119.  
    120.             CBUFFER_START(UnityPerMaterial)
    121.                 sampler2D _MainTex;
    122.                 float4 _MainTex_ST;
    123.             CBUFFER_END
    124.                    
    125.             struct appdata
    126.             {
    127.                 float4 positionOS   : POSITION;
    128.                 float2 uv           : TEXCOORD0;
    129.                 float3 normal       : NORMAL;
    130.             };
    131.  
    132.             struct v2f
    133.             {
    134.                 float2 uv           : TEXCOORD0;
    135.                 float3 vertexWS     : TEXCOORD1;
    136.                 float4 vertex       : SV_POSITION;
    137.                 float4 screenSpace  : TEXCOORD2;
    138.                 float3 normal       : NORMAL;
    139.             };
    140.          
    141.              
    142.  
    143.             sampler2D _CameraDepthTexture;
    144.  
    145.             v2f vert(appdata v)
    146.             {
    147.                 v2f o;
    148.                 VertexPositionInputs positionInputs = GetVertexPositionInputs(v.positionOS.xyz);
    149.                 o.vertex = positionInputs.positionCS;
    150.                 o.vertexWS = positionInputs.positionWS;
    151.                 o.screenSpace = ComputeScreenPos(o.vertex);
    152.                 o.normal = v.normal;
    153.                 o.uv = TRANSFORM_TEX(v.uv, _MainTex);
    154.                 return o;
    155.             }
    156.  
    157.             half4 frag(v2f i) : SV_Target
    158.             {
    159.                 float4 shadowCoord = TransformWorldToShadowCoord(i.vertexWS);
    160.                 Light mainLight = GetMainLight(shadowCoord);
    161.  
    162.                 float2 screenSpaceUV = i.screenSpace.xy / i.screenSpace.w;
    163.  
    164.                 //float depth = tex2D(_CameraDepthTexture, screenSpaceUV).r;
    165.  
    166.                 Surface surf;
    167.                 surf.normal = i.normal;
    168.                 float4 texCol = tex2D(_MainTex, i.uv);
    169.                 float3 color = surfaceAtten(mainLight, surf, 0) * texCol.rgb;
    170.                 float alpha = smoothstep(0.5f,0.5f,texCol.a);
    171.                 return float4(color, alpha);
    172.             }
    173.             ENDHLSL
    174.         }
    175.     }
    176. }
     
  2. MsticFox

    MsticFox

    Joined:
    Dec 7, 2022
    Posts:
    2
    I just found out that if I remove the Tag "RenderPipeline"="UniversalRenderPipeline" the textures will be displayed correctly. In Unity 2022.1 the Tag had no issues.