Problem With Cutout Shader - Unwanted Transparency

Note that I am a total newbie in shader programmin, so I’ve been trying to study it.
My intention on this script was to do, at least for now, a Shader that Accepts Tint Color and accepts an Albedo, for each 0 alpha areas on the Albedo, these would cut out from the rendering, this was supposed to be used on a character’s cloathes. There goes the code:

Shader "Custom/PBR Shader"
{
    Properties
    {
        _TintColor ("Colour Tint", color) = (1,1,1,1)
        _MainTex ("Albedo", 2D) = "white" {}
        _CutOff ("CutOff", Range(0,1)) = 0.5
    }

    SubShader
    {
        Tags {"Queue" = "Transparent" "RenderType" = "Opaque"}
        cull off

        CGPROGRAM

        #pragma surface surf Lambert finalcolor:mycolor alpha

        struct Input
        {
            float2 uv_MainTex;
        };

        fixed4 _TintColor;
        void mycolor (Input IN, SurfaceOutput o,inout fixed4 color)
        {
            color *= _TintColor;
        }

        sampler2D _MainTex;
        float _CutOff;
        void surf (Input IN, inout SurfaceOutput o)
        {
            float ca = tex2D(_MainTex, IN.uv_MainTex).a;
            o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb;

            if (ca > _CutOff)
                o.Alpha = ca;
            else
                o.Alpha = 0;
        }

        ENDCG

    }
    FallBack "Transparent/Diffuse"
}
Shader "Custom/PBR Shader"
{
    Properties
    {
        _TintColor ("Colour Tint", color) = (1,1,1,1)
        _MainTex ("Albedo", 2D) = "white" {}
        _CutOff ("CutOff", Range(0,1)) = 0.5
    }

    SubShader
    {
        Tags {"Queue" = "Transparent" "RenderType" = "Opaque"}
        cull off

        CGPROGRAM

        #pragma surface surf Lambert finalcolor:mycolor alpha

        struct Input
        {
            float2 uv_MainTex;
        };

        fixed4 _TintColor;
        void mycolor (Input IN, SurfaceOutput o,inout fixed4 color)
        {
            color *= _TintColor;
        }

        sampler2D _MainTex;
        float _CutOff;
        void surf (Input IN, inout SurfaceOutput o)
        {
            float ca = tex2D(_MainTex, IN.uv_MainTex).a;
            o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb;

            if (ca > _CutOff)
                o.Alpha = ca;
            else
                o.Alpha = 0;
        }

        ENDCG

    }
    FallBack "Transparent/Diffuse"
}

But the problem is, even fully alpha areas (And I checked the texture plane and those are indeed with no transparency at all) seems a little transparent. And I can’t figure out why. Does anyone know how to help me please? And If you have any optimization or performance advices on the code, please feel free to sugest.

Thanks;
Arthur

It sounds like you want an actual cutout shader then, not an alpha blend shader (which is what you’re using).

Instead of alpha in the #pragma surface line you want:
alphatest:_Cutoff

Then in the surf function just output your texture’s alpha value, no need to have the if there.

Tried it here… It indeed removed the unwanted transparency, although… the cutOut doesn’t work anymore: the transparency of the texture image is not being recognized anymoro.

There’s a terminology issue here. “Cutout” is a hard edge transparency, either on or off. You want alpha blended transparency in addition to hiding areas of full transparency.

You probably want to use a combination of alpha test and alpha blend then, for which you can recreate alpha test in the surf function.

Set the alpha in the #pragma surface line to:
alpha:fade

Then in your surface function use this instead of the if statement you had before:
clip(ca - _CutOff);

If you’re using shadows you’ll also want to add addshadow to the #pragma surface line.

1 Like