Search Unity

No shadows when looking through a cutout shader

Discussion in 'Shaders' started by 1danielcoelho, Aug 28, 2016.

  1. 1danielcoelho

    1danielcoelho

    Joined:
    Sep 7, 2015
    Posts:
    2
    I'm trying to have a custom cutout shader cast shadows (blue arrow), and allow shadows to be seen through the cutout (red arrow). As you can see, none of that is happening right now.


    (Its a cube with the cutout shader intersecting a default cube below, viewed from the top with an orthographic camera)

    I need a custom shader because the border cutout detail is stored in a separate detail texture. My shader code is below:

    1. Shader"Custom/TerrainShader"{
    2. Properties{
    3. _Color("Color",Color)=(1,1,1,1)
    4. _Cutoff("Alpha cutoff",Range(0,1))=0.5
    5. _MainTex("Base (RGB) Trans (A)",2D)="white"{}
    6. _DetailTex("Detail (RGB)",2D)="white"{}
    7. }
    8. SubShader{
    9. Tags{"Queue"="AlphaTest""RenderType"="TransparentCutout"}
    10. LOD 200
    11. CGPROGRAM
    12. #pragma surface surf Standard fullforwardshadows alphatest:_Cutoff
    13. #pragma target 3.0
    14. sampler2D _MainTex;
    15. sampler2D _DetailTex;
    16. fixed4 _Color;
    17. structInput{
    18. float2 uv_MainTex;
    19. float2 uv2_DetailTex;
    20. };
    21. void surf (Input IN, inout SurfaceOutputStandard o){
    22. fixed4 c = tex2D (_MainTex, IN.uv_MainTex)*_Color* tex2D(_DetailTex, IN.uv2_DetailTex);
    23. o.Albedo= c.rgb;
    24. o.Alpha= c.a;
    25. }
    26. ENDCG
    27. }
    28. FallBack"Transparent/Cutout/Diffuse"
    29. }

    It is based on the old Transparent/Cutout/Diffuse shaders, but also samples the detail texture I described.

    If I change the Queue to Overlay, it draws on top of the shadow casting and I can see shadows through the cutout (red arrow), but obviously it doesn't cast its own shadows then. I think I have to alter the shadow pass to make it discard the alpha pixels in the detail texture, but that wouldn't solve the problem pointed at by the red arrow, I think.

    Any ideas?

    (Cross post from http://answers.unity3d.com/questions/1235851/no-shadows-when-looking-through-a-cutout-shader.html)
     
    GodlikeAurora likes this.
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,348
    You need to add addshadow to your #pragma surface line.

    By default surface shaders do not generate their own shadow caster pass (the shader pass used for casting shadows and receiving the main directional shadows on PC). Most surface shaders get their shadow caster pass from the Fallback shader, and in this case you were going the right way switching the fallback to use the cutout diffuse, but as the cutout diffuse shader doesn't know anything about your _DetailTex, it will only be using the _MainTex for the cutout. By adding the addshadow to your shader it'll tell the surface shader it needs to generate a custom shadow caster pass for this shader, and that one will take into account both the _MainTex and the _DetailTex.
     
    grobonom, GodlikeAurora and tomekkie2 like this.
  3. 1danielcoelho

    1danielcoelho

    Joined:
    Sep 7, 2015
    Posts:
    2
    Wow, that was exactly it! Awesome when problems can be fixed by adding a single word



    Awesome explanation too, thanks!
     
    tomekkie2 likes this.