Search Unity

Albedo Cutoff not functioning on Standard DoubleSided Shader.

Discussion in 'Shaders' started by Ryn-Shar, May 25, 2019.

  1. Ryn-Shar

    Ryn-Shar

    Joined:
    Nov 12, 2012
    Posts:
    2
    So I've got this shader here, which is working close to perfectly for my goal, which is a 2.5d game where the sprites have bump maps and other things you'd expect on 3d models. The only issue is that the _Cutoff doesn't seem to work at all, meaning that it always renders as opaque.

    Code (csharp):
    1.  
    2. Shader "DoubleSided"
    3. {
    4.     Properties
    5.     {
    6.         _Color("Color", Color) = (1,1,1,1)
    7.         _MainTex("Albedo and Alpha", 2D) = "white" {}
    8.         _Cutoff("Alpha Cutoff", Range(0.0, 1.0)) = 0.5
    9.         _Glossiness("Smoothness", Range(0.0, 1.0)) = 0.5
    10.         _GlossMapScale("Smoothness Scale", Range(0.0, 1.0)) = 1.0
    11.         [Enum(Metallic Alpha,0,Albedo Alpha,1)]
    12.         _SmoothnessTextureChannel("Smoothness texture channel", Float) = 0
    13.         [Gamma] _Metallic("Metallic", Range(0.0, 1.0)) = 0.0
    14.         _MetallicGlossMap("Metallic", 2D) = "white" {}
    15.         [ToggleOff] _SpecularHighlights("Specular Highlights", Float) = 1.0
    16.         [ToggleOff] _GlossyReflections("Glossy Reflections", Float) = 1.0
    17.         _BumpScale("Scale", Float) = 1.0
    18.         _BumpMap("Normal Map", 2D) = "bump" {}
    19.         _Parallax("Height Scale", Range(0.005, 0.08)) = 0.02
    20.         _ParallaxMap("Height Map", 2D) = "black" {}
    21.         _OcclusionStrength("Strength", Range(0.0, 1.0)) = 1.0
    22.         _OcclusionMap("Occlusion", 2D) = "white" {}
    23.         _EmissionColor("Color", Color) = (0,0,0)
    24.         _EmissionMap("Emission", 2D) = "white" {}
    25.         _DetailMask("Detail Mask", 2D) = "white" {}
    26.         _DetailAlbedoMap("Detail Albedo x2", 2D) = "grey" {}
    27.         _DetailNormalMapScale("Scale", Float) = 1.0
    28.         _DetailNormalMap("Normal Map", 2D) = "bump" {}
    29.         [Enum(UV0,0,UV1,1)] _UVSec("UV Set for secondary textures", Float) = 0
    30.             // Blending state
    31.             [HideInInspector] _Mode("__mode", Float) = 0.0
    32.             [HideInInspector] _SrcBlend("__src", Float) = 1.0
    33.             [HideInInspector] _DstBlend("__dst", Float) = 0.0
    34.             [HideInInspector] _ZWrite("__zw", Float) = 1.0
    35.     }
    36.         CGINCLUDE
    37. #define UNITY_SETUP_BRDF_INPUT MetallicSetup
    38.             ENDCG
    39.             SubShader
    40.         {
    41.             Tags {"Queue" = "Transparent" "RenderType" = "Transparent" "PerformanceChecks" = "False" }
    42.             LOD 300
    43.             // ------------------------------------------------------------------
    44.             //  Base forward pass (directional light, emission, lightmaps, ...)
    45.             Pass
    46.             {
    47.                 Name "FORWARD"
    48.                 Tags { "LightMode" = "ForwardBase" "Queue" = "Transparent" "RenderType" = "Transparent"}
    49.                 Blend[_SrcBlend][_DstBlend]
    50.                 ZWrite[_ZWrite]
    51.                 Cull Off
    52.                 CGPROGRAM
    53.                 #pragma target 3.0
    54.             // -------------------------------------
    55.             #pragma shader_feature _NORMALMAP
    56.             #pragma shader_feature _ALPHATEST_ON
    57.             //#pragma shader_feature _ _ALPHATEST_ON _ALPHABLEND_ON _ALPHAPREMULTIPLY_ON
    58.             //#pragma surface surf Lambert alphatest:_Cutoff
    59.             #pragma shader_feature _EMISSION
    60.             #pragma shader_feature _METALLICGLOSSMAP
    61.             #pragma shader_feature ___ _DETAIL_MULX2
    62.             #pragma shader_feature _ _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A
    63.             #pragma shader_feature _ _SPECULARHIGHLIGHTS_OFF
    64.             #pragma shader_feature _ _GLOSSYREFLECTIONS_OFF
    65.             #pragma shader_feature _PARALLAXMAP
    66.             #pragma multi_compile_fwdbase
    67.             #pragma multi_compile_fog
    68.             #pragma multi_compile_instancing
    69.             #pragma vertex vertBase
    70.             #pragma fragment fragBase
    71.             #include "UnityStandardCoreForward.cginc"
    72.             ENDCG
    73.         }
    74.             // ------------------------------------------------------------------
    75.             //  Additive forward pass (one light per pass)
    76.             Pass
    77.             {
    78.                 Name "FORWARD_DELTA"
    79.                 Tags { "LightMode" = "ForwardAdd" "Queue" = "Transparent" "RenderType" = "Transparent"}
    80.                 Blend[_SrcBlend] One
    81.                 Fog { Color(0,0,0,0) } // in additive pass fog should be black
    82.                 ZWrite Off
    83.                 ZTest LEqual
    84.                 Cull Off
    85.                 CGPROGRAM
    86.                 #pragma target 3.0
    87.             // -------------------------------------
    88.             #pragma shader_feature _NORMALMAP
    89.             #pragma shader_feature _ALPHATEST_ON
    90.             //#pragma shader_feature _ _ALPHATEST_ON _ALPHABLEND_ON _ALPHAPREMULTIPLY_ON
    91.             #pragma shader_feature _METALLICGLOSSMAP
    92.             #pragma shader_feature _ _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A
    93.             //#pragma shader_feature _Alphacutoff
    94.             #pragma shader_feature _ _SPECULARHIGHLIGHTS_OFF
    95.             #pragma shader_feature ___ _DETAIL_MULX2
    96.             #pragma shader_feature _PARALLAXMAP
    97.                            
    98.             #pragma multi_compile_fwdadd_fullshadows
    99.             #pragma multi_compile_fog
    100.             #pragma vertex vertAdd
    101.             #pragma fragment fragAdd
    102.             #include "UnityStandardCoreForward.cginc"
    103.             ENDCG
    104.         }
    105.             // ------------------------------------------------------------------
    106.             //  Shadow rendering pass
    107.             Pass {
    108.                 Name "ShadowCaster"
    109.                 Tags { "LightMode" = "ShadowCaster""Queue" = "Transparent" "RenderType" = "Transparent" }
    110.                 ZWrite On ZTest LEqual
    111.                 CGPROGRAM
    112.                 #pragma target 3.0
    113.             // -------------------------------------
    114.             #pragma shader_feature _ALPHATEST_ON
    115.             //#pragma shader_feature _ _ALPHATEST_ON _ALPHABLEND_ON _ALPHAPREMULTIPLY_ON
    116.             #pragma shader_feature _METALLICGLOSSMAP
    117.             #pragma shader_feature _PARALLAXMAP
    118.             #pragma multi_compile_shadowcaster
    119.             #pragma multi_compile_instancing
    120.             #pragma vertex vertShadowCaster
    121.             #pragma fragment fragShadowCaster
    122.             #include "UnityStandardShadow.cginc"
    123.             ENDCG
    124.         }
    125.             // ------------------------------------------------------------------
    126.             //  Deferred pass
    127.             Pass
    128.             {
    129.                 Name "DEFERRED"
    130.                 Tags { "LightMode" = "Deferred" "Queue" = "Transparent" "RenderType" = "Transparent"}
    131.                 CGPROGRAM
    132.                 #pragma target 3.0
    133.                 #pragma exclude_renderers nomrt
    134.             // -------------------------------------
    135.             #pragma shader_feature _NORMALMAP
    136.             #pragma shader_feature _ _ALPHATEST_ON _ALPHABLEND_ON _ALPHAPREMULTIPLY_ON
    137.             #pragma shader_feature _EMISSION
    138.             #pragma shader_feature _METALLICGLOSSMAP
    139.             #pragma shader_feature _ _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A
    140.             #pragma shader_feature _ _SPECULARHIGHLIGHTS_OFF
    141.             #pragma shader_feature ___ _DETAIL_MULX2
    142.             #pragma shader_feature _PARALLAXMAP
    143.             #pragma multi_compile_prepassfinal
    144.             #pragma multi_compile_instancing
    145.             #pragma vertex vertDeferred
    146.             #pragma fragment fragDeferred
    147.             #include "UnityStandardCore.cginc"
    148.             ENDCG
    149.         }
    150.             // ------------------------------------------------------------------
    151.             // Extracts information for lightmapping, GI (emission, albedo, ...)
    152.             // This pass it not used during regular rendering.
    153.             Pass
    154.             {
    155.                 Name "META"
    156.                 Tags { "LightMode" = "Meta""Queue" = "Transparent" "RenderType" = "Transparent" }
    157.                 Cull Off
    158.                 CGPROGRAM
    159.                 #pragma vertex vert_meta
    160.                 #pragma fragment frag_meta
    161.                 #pragma shader_feature _EMISSION
    162.                 #pragma shader_feature _METALLICGLOSSMAP
    163.                 #pragma shader_feature _ _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A
    164.                 #pragma shader_feature ___ _DETAIL_MULX2
    165.                 #pragma shader_feature EDITOR_VISUALIZATION
    166.                 #include "UnityStandardMeta.cginc"
    167.                 ENDCG
    168.             }
    169.         }
    170.             SubShader
    171.         {
    172.             Tags { "Queue" = "Transparent" "RenderType" = "Transparent" "PerformanceChecks" = "False" }
    173.             LOD 150
    174.             // ------------------------------------------------------------------
    175.             //  Base forward pass (directional light, emission, lightmaps, ...)
    176.             Pass
    177.             {
    178.                 Name "FORWARD"
    179.                 Tags { "LightMode" = "ForwardBase" "Queue" = "Transparent" "RenderType" = "Transparent"}
    180.                 Blend[_SrcBlend][_DstBlend]
    181.                 ZWrite[_ZWrite]
    182.                 Cull Off
    183.                 CGPROGRAM
    184.                 #pragma target 2.0
    185.                 #pragma shader_feature _NORMALMAP
    186.                 #pragma shader_feature _ _ALPHATEST_ON _ALPHABLEND_ON _ALPHAPREMULTIPLY_ON
    187.                 #pragma shader_feature _EMISSION
    188.                 #pragma shader_feature _METALLICGLOSSMAP
    189.                 #pragma shader_feature _ _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A
    190.                 #pragma shader_feature _ _SPECULARHIGHLIGHTS_OFF
    191.                 #pragma shader_feature _ _GLOSSYREFLECTIONS_OFF
    192.             // SM2.0: NOT SUPPORTED shader_feature ___ _DETAIL_MULX2
    193.             // SM2.0: NOT SUPPORTED shader_feature _PARALLAXMAP
    194.             #pragma skip_variants SHADOWS_SOFT DIRLIGHTMAP_COMBINED
    195.             #pragma multi_compile_fwdbase
    196.             #pragma multi_compile_fog
    197.             #pragma vertex vertBase
    198.             #pragma fragment fragBase
    199.             #include "UnityStandardCoreForward.cginc"
    200.             ENDCG
    201.         }
    202.             // ------------------------------------------------------------------
    203.             //  Additive forward pass (one light per pass)
    204.             Pass
    205.             {
    206.                 Name "FORWARD_DELTA"
    207.                 Tags { "LightMode" = "ForwardAdd" "Queue" = "Transparent" "RenderType" = "Transparent"}
    208.                 Blend[_SrcBlend] One
    209.                 Fog { Color(0,0,0,0) } // in additive pass fog should be black
    210.                 ZWrite Off
    211.                 ZTest LEqual
    212.                 Cull Off
    213.                 CGPROGRAM
    214.                 #pragma target 2.0
    215.                 #pragma shader_feature _NORMALMAP
    216.                 #pragma shader_feature _ _ALPHATEST_ON _ALPHABLEND_ON _ALPHAPREMULTIPLY_ON
    217.                 #pragma shader_feature _METALLICGLOSSMAP
    218.                 #pragma shader_feature _ _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A
    219.                 #pragma shader_feature _ _SPECULARHIGHLIGHTS_OFF
    220.                 #pragma shader_feature ___ _DETAIL_MULX2
    221.             // SM2.0: NOT SUPPORTED shader_feature _PARALLAXMAP
    222.             #pragma skip_variants SHADOWS_SOFT
    223.             #pragma multi_compile_fwdadd_fullshadows
    224.             #pragma multi_compile_fog
    225.             #pragma vertex vertAdd
    226.             #pragma fragment fragAdd
    227.             #include "UnityStandardCoreForward.cginc"
    228.             ENDCG
    229.         }
    230.             // ------------------------------------------------------------------
    231.             //  Shadow rendering pass
    232.             Pass {
    233.                 Name "ShadowCaster"
    234.                 Tags { "LightMode" = "ShadowCaster""Queue" = "Transparent" "RenderType" = "Transparent" }
    235.                 ZWrite On ZTest LEqual
    236.                 CGPROGRAM
    237.                 #pragma target 2.0
    238.                 #pragma shader_feature _ _ALPHATEST_ON _ALPHABLEND_ON _ALPHAPREMULTIPLY_ON
    239.                 #pragma shader_feature _METALLICGLOSSMAP
    240.                 #pragma skip_variants SHADOWS_SOFT
    241.                 #pragma multi_compile_shadowcaster
    242.                 #pragma vertex vertShadowCaster
    243.                 #pragma fragment fragShadowCaster
    244.                 #include "UnityStandardShadow.cginc"
    245.                 ENDCG
    246.             }
    247.             // ------------------------------------------------------------------
    248.             // Extracts information for lightmapping, GI (emission, albedo, ...)
    249.             // This pass it not used during regular rendering.
    250.             Pass
    251.             {
    252.                 Name "META"
    253.                 Tags { "LightMode" = "Meta" "Queue" = "Transparent" "RenderType" = "Transparent"}
    254.                 Cull Off
    255.                 CGPROGRAM
    256.                 #pragma vertex vert_meta
    257.                 #pragma fragment frag_meta
    258.                 #pragma shader_feature _EMISSION
    259.                 #pragma shader_feature _METALLICGLOSSMAP
    260.                 #pragma shader_feature _ _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A
    261.                 #pragma shader_feature ___ _DETAIL_MULX2
    262.                 #pragma shader_feature EDITOR_VISUALIZATION
    263.                 #include "UnityStandardMeta.cginc"
    264.                 ENDCG
    265.             }
    266.         }
    267.             FallBack "VertexLit"
    268.             CustomEditor "StandardShaderGUI"
    269. }
    270.  
    Basically, the setup is this shader on an object with a sprite renderer, which switches between spritesheets through the Animator.

    The issue is that the sprite never goes into Cutout mode, all modes, when pulling from the Albedo Alpha, render as opaque. If I switch to Metallic Alpha, it will work until the game starts running, and then also display as opaque. Note, I am not using a metallic texture at this point. Either way, the slider for Alpha Cutoff has no effect at all.

    you can see my settings here: https://imgur.com/a/HE2uhpJ

    I've tried changing the _ALPHATEST pragma lines, dropping the other alpha settings, or even completely commenting out the instances of that line, but it seems to have no effect one way or the other. I am not very familiar with Shader Features, I've mostly used "SURF" shaders in the past. I tried inputting the surf void, but the other Shader_Features are incompatible with it.

    Is this an issue that anyone has come across? Can I force it to use the Albedo alpha to cutout? Am I just queuing/tagging it wrong? Is it an issue with combining the Standard Shader with a sprite renderer? Should I try cutting out the shadow/meta passes entirely? I've been bashing against this for a couple days with no progress, and I could use any suggestions anyone's got. Thanks!
     
  2. Ryn-Shar

    Ryn-Shar

    Joined:
    Nov 12, 2012
    Posts:
    2
    I have resolved the issue with help from IRC! There were two problems - one, in a separate area of code, I was using GetComponent<Renderer>().material.shaderKeywords = new string[1] { "_NORMALMAP" }; instead of MonsterHolder.GetComponent<Renderer>().material.EnableKeyword( "_NORMALMAP"),
    and the second issue was with the #pragma shader_feature _ _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A line. With those lines commented out and that line switched, it is now working!