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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

addshadow and screenpos

Discussion in 'Shaders' started by Kahel_, Sep 29, 2020.

  1. Kahel_

    Kahel_

    Joined:
    Nov 9, 2014
    Posts:
    8
    Hello, I've been struggling for a while with a bug on my surface shader and succeed to isolate a strange behavior I can't understand. Sometimes shadows are rendered on part of my mesh that are transparent.

    For exemple with this simple code
    Code (CSharp):
    1. Shader "Custom/CutoutShadowSurface"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex ("Albedo (RGB)", 2D) = "white" {}
    6.         _Cutout("Cutout", Range(0, 1)) = 0.5
    7.     }
    8.     SubShader
    9.     {
    10.         Tags { "RenderType"="Opaque" }
    11.         LOD 200
    12.  
    13.         CGPROGRAM
    14.         // Physically based Standard lighting model, and enable shadows on all light types
    15.         #pragma surface surf SimpleLambert alphatest:_Cutout addshadow
    16.  
    17.         // Use shader model 3.0 target, to get nicer looking lighting
    18.         #pragma target 3.0
    19.  
    20.         sampler2D _MainTex;
    21.  
    22.         struct Input
    23.         {
    24.             float2 uv_MainTex;
    25.             float4 screenPos;
    26.         };
    27.  
    28.         half _Glossiness;
    29.         half _Metallic;
    30.         fixed4 _Color;
    31.  
    32.         // Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.
    33.         // See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
    34.         // #pragma instancing_options assumeuniformscaling
    35.         UNITY_INSTANCING_BUFFER_START(Props)
    36.             // put more per-instance properties here
    37.         UNITY_INSTANCING_BUFFER_END(Props)
    38.  
    39.         half4 LightingSimpleLambert(SurfaceOutput s, half3 lightDir, half atten) {
    40.             half NdotL = dot(s.Normal, lightDir);
    41.             half4 c;
    42.             c.rgb = s.Albedo /** _LightColor0.rgb * (NdotL * atten)*/;
    43.             c.a = s.Alpha;
    44.             return c;
    45.         }
    46.  
    47.         void surf (Input IN, inout SurfaceOutput o)
    48.         {
    49.             float3 screenPos = IN.screenPos.xyz / IN.screenPos.w;
    50.             fixed4 c = tex2D (_MainTex, screenPos);
    51.             o.Albedo = float3(1, 1, 1);
    52.             o.Alpha = c.r;
    53.         }
    54.         ENDCG
    55.     }
    56.     FallBack "Diffuse"
    57. }
    58.  
    when I use some "dither noise" texture I found on internet I get this result:
    ditherTexture.PNG
    but when I use the default unity knob texture I got no shadows at all:
    knobTexture.PNG
    When I use the UV texture coordinates instead of screenPos I don't have this problem.
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,242
    Kahel_ likes this.
  3. Kahel_

    Kahel_

    Joined:
    Nov 9, 2014
    Posts:
    8