Simple Unlit Transparent shader with 2 UV sets

I have very little experience with shader programming and I could use some help with getting the following shader to work.

What I’m trying to get is the following:

  • Unlit transparent shader using 2 textures, each for one UV set.
  • The second UV set is has a texture which determines the transparency (like an alpha texture).
  • The reason I’m not using the alpha channel of the first texture is that the alpha texture should use the 2nd UV set instead of the first.

I managed to get the 2 textures loaded, each using a different UV set. The problem, however, is that I cannot figure out how to combine them so that the black color of the second texture becomes transparent.

Texture input:

First texture (UVset 1)

Second texture (UVset 2)

First texture + second texture (everything in BLACK should be transparent)…

Here’s the slightly modified shader code:

Shader "Kitten/Unlit/UnlitKitten"
{
    Properties
    {
        _MainTex("Texture", 2D) = "white" {}
    _AlphaTex("Alpha", 2D) = "white" {}
    }
        SubShader
    {
        Tags{ "RenderType" = "Transparent" }
        LOD 100

        Pass
    {
        CGPROGRAM
#pragma vertex vert
#pragma fragment frag
        // make fog work
#pragma multi_compile_fog

#include "UnityCG.cginc"

    struct appdata
    {
        float4 vertex : POSITION;
        float2 uv : TEXCOORD0;
        float2 uv2 : TEXCOORD1;
    };

    struct v2f
    {
        float2 uv : TEXCOORD0;
        float2 uv2 : TEXCOORD1;
        UNITY_FOG_COORDS(1)
            float4 vertex : SV_POSITION;
    };

    sampler2D _MainTex;
    sampler2D _AlphaTex;
    float4 _MainTex_ST;
    float4 _AlphaTex_ST;
  

    v2f vert(appdata v)
    {
        v2f o;
        o.vertex = UnityObjectToClipPos(v.vertex);
        o.uv = TRANSFORM_TEX(v.uv, _MainTex);
        o.uv2 = TRANSFORM_TEX(v.uv2, _AlphaTex);
        UNITY_TRANSFER_FOG(o,o.vertex);
        return o;
    }

    fixed4 frag(v2f i) : SV_Target
    {
    // sample the texture
    fixed4 col = tex2D(_MainTex, i.uv);
    fixed4 alpha = tex2D(_AlphaTex, i.uv2);
    fixed4 combined = col * alpha;
    col.rgb = combined;

    return col;


    }
        ENDCG
    }
    }
}

That’s only part of what’s needed to make a shader render as transparent, in some ways that’s actually the least important part. You also need to set the Blend, ZWrite, and the Queue. Also your textures are both RGB textures with no alpha, and by default textures have 100% full alpha (opaque), and you never assign an alpha value. You in fact throw away the alpha channel of your alpha texture if it had alpha by using col.rgb = combined;

Around where you have Tags you’ll want this:
Tags { “Queue”=“Transparent” “RenderType”=“Transparent” }
Blend SrcAlpha OneMinusSrcAlpha
ZWrite Off

And then in the shader code itself you want this:
fixed4 col = tex2D(_MainTex, i.uv);
fixed4 alpha = tex2D(_AlphaTex, i.uv2);
col.a *= alpha.g;
return col;

That uses the green channel of your “alpha” texture as the alpha or transparency output by the shader.

However once you do all of this you’ll find your cat looks kind of funny if this shader is used on the entire body of the cat, and that’s because of ZWrite Off. If you remove that line or set it to ZWrite On you’ll also get a different funny result. I’ve written about this a few times here on the forum if you want to know why.

1 Like

Thanks for the help! That did exactly what I wanted. I assigned a non-transparent shader to the body, to counter the sorting issue. Works great.

Again, thanks for helping me out here :slight_smile: