Mask shader that uses texture coordinates from .uv2

Hello everyone.

I’ve got a request for a custom shader, if any of you are willing.

I’m proficient with C#, but shader language is complete greek to me. I’ve done some searching to find the particular functionality that I’m looking for, but I haven’t been able to figure out how to combine what I’ve found into a single shader.

I suppose I COULD go and learn all about shaders and how to write them, but I thought I’d ask here first!

I would like a single material, with two textures. I would like the material to use texture coordinates from two different sets of uvs. In this case, uv and uv2, in the filters mesh instance. Here is a shader I’ve found that can do that:

Shader "Custom/twotex" {
Properties {
    _MainTex ("Base (RGB)", 2D) = "white" {}
    _AoTex ("AO (RGB)", 2D) = "white" {}

}
SubShader {
    Tags { "RenderType"="Opaque" }
    LOD 200

    CGPROGRAM
    #pragma surface surf Lambert alpha

    sampler2D _MainTex;
    sampler2D _AoTex;


    struct Input {
        float2 uv_MainTex : TEXCOORD0;
        float2 uv2_AoTex : TEXCOORD1;
    };

    void surf (Input IN, inout SurfaceOutput o) {
        half4 c = tex2D (_MainTex, IN.uv_MainTex.xy);
        half4 ao = tex2D (_AoTex, IN.uv2_AoTex.xy);
        o.Albedo = c.rgb * ao.rgb;
        o.Alpha = c.a;
    }
    ENDCG
}
FallBack "Diffuse"
}

I realize that uv2 is intended to be used for light maps. This particular game takes place in space, there are no static meshes, and nothing that’s going to be baked into lightmaps, so I’m happy to use their uv coordinates for this reason.

Secondly, I would like the SECOND texture (uv2) to mask the first. Anything transparent in texture 2 should be transparent in texture 1.

Here is a shader I’ve found that allows me to do that:

Shader "MaskedTexture"
{
   Properties
   {
      _MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
      _Mask ("Culling Mask", 2D) = "white" {}
      _Cutoff ("Alpha cutoff", Range (0,1)) = 0.1
   }
   SubShader
   {
      Tags {"Queue"="Transparent" }
      Lighting Off
      ZWrite Off
      Blend SrcAlpha OneMinusSrcAlpha
      AlphaTest GEqual [_Cutoff]
      Pass
      {
         SetTexture [_Mask] {combine texture}
         SetTexture [_MainTex] {combine texture, previous * texture}
      }
   }
}

What would the combined shader look like? Thanks in advance!

EDIT:

Thanks again to @jvo3dc , who helped solve this problem. Working shader:

   Shader "Transparent/UV2Mask" {
 
        Properties {
            _MainTex ("Base (RGB)", 2D) = "white" {}
            _MaskTex ("Mask (RGB)", 2D) = "white" {}

        }
      
        SubShader {
            Tags {"Queue"="Transparent" }
            LOD 200

            CGPROGRAM
            #pragma surface surf Lambert alpha

            sampler2D _MainTex;
            sampler2D _MaskTex;


            struct Input {
                float2 uv_MainTex : TEXCOORD0;
                float2 uv2_MaskTex :   TEXCOORD1;
            };

            void surf (Input IN, inout SurfaceOutput surface) {
                half4 col = tex2D (_MainTex, IN.uv_MainTex.xy);
                half4 mask = tex2D (_MaskTex, IN.uv2_MaskTex.xy);
                surface.Albedo = col.rgb;
                surface.Alpha = mask.a;
            }
            ENDCG
        }
        FallBack "Diffuse"
    }

Bump

Like the first shader, but then:

o.Albedo = c.rgb;
o.Alpha = c.a * ao.a;

instead of:

o.Albedo = c.rgb * ao.rgb;
o.Alpha = c.a;

Thanks @jvo3dc , that comment was all it took to push me in the right direction. I had to do a little bit of fiddling to make it work, but I ultimatly ended up with this shader, which worked as intended!

EDIT: See original post for working shader.

That works, but this multiplication is unneeded:

surface.Albedo = col.rbg * mask.rgb;

Can just be:

surface.Albedo = col.rbg;

Well, I’m using a luma alpha map. It doesnt make sense to me, but if i dont multiply the rbg, i can see black where transparency should be. Maybe something to do with the tags at the top of the shader? I dunno.

That doesn’t really make sense indeed. I did see you’re missing the alpha tag in:

#pragma surface surf Lambert alpha

Yup, that alpha tag is what made the difference. Also, I think I was making changes to the mask image without applying them in the inspector. Thanks again!

Original post updated with working shader.