AlphaBlend Shader w/ Mask

Hi all! First time in the ShaderLab forum, so bear with me please, and thank you in advance for any help!

Goal: I need to make a shader that supports 2 textures with an alpha cutoff to fade the texture over time. The shader should use the Mask texture’s alpha value to determine which pixels should be culled based on a cutoff threshold, BUT I do not want the alpha value from the mask applied to the Main Texture, which so far has eliminated a simple “AlphaTest” check for me.

Main Texture: RGBA (supporting alpha blending)
Mask/Cutout Texture: Alpha channel for cutoff mask
Cutoff Threshold: 0 - 1 for alpha testing/culling.

I want to be able to fade parts of the texture based on the Mask over time, based on the cutoff threshold, but otherwise use the main texture with all of it’s anti-aliased transparent goodness.

So, now that the goal is out of the way. I’ve got this working on my PC based desktop, but I can’t get it to work on my Android tablet. It shows me a solid pink texture. (the shader is referenced directly by the material, I’m not using Shader.Find()). I cannot see any compile time warnings or errors in my Editor log (unless I’m missing it)… so my thought is that I’m using something that is not supported by OpenGL on the Android device.

I’ve tried several shaders found on the internet, but none of them seem to be doing what I’m looking for.

So, I have 2 questions:

  1. Is there a more trivial way to accomplish what I’m looking for and I’m over complicating this because I’m a Shader Programming noob?
  2. What feature in the shader below am I using that would make it incompatible with Android/iOS mobile devices?

Regards,
Josh

Here’s the shader (free for anyone else to use):

Shader "MyTransparentCutoutShader" {
Properties {
	_MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
	_CutTex ("Cutout (A)", 2D) = "white" {}
	_Cutoff ("Alpha cutoff", Range(0,1)) = 0.5
}
 
SubShader {
	Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
	LOD 200
 
 	Lighting Off
 	ZWrite Off
    Blend SrcAlpha OneMinusSrcAlpha
    
	 Pass {
		CGPROGRAM
		#pragma vertex vert
		#pragma fragment frag
		
		#include "UnityCG.cginc"
		
		struct appdata_t {
			float4 vertex : POSITION;
			float2 texcoord : TEXCOORD0;
		};
				
		struct v2f {
			float4 vertex : SV_POSITION;
			half2 texcoord : TEXCOORD0;
		};
		
		sampler2D _MainTex;
		sampler2D _CutTex;
		float _Cutoff;
		float4 _MainTex_ST;
		 
		float4 frag(float2 uv_MainTex : TEXCOORD) : COLOR
		{
			fixed4 c = tex2D(_MainTex, uv_MainTex);
			float ca = tex2D(_CutTex, uv_MainTex).a;
			
			if (ca < _Cutoff)
			  discard;
		  
			return c;
		}
					
		v2f vert (appdata_t v)
		{
			v2f o;
			o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
			o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
			return o;
		}
		
		ENDCG
		}
	}
}

Based on some very preliminary evidence, the change that seems to have fixed this on the Android running OpenGL is changing the semantic name attached to the fragment shader parameter from TEXCOORD to TEXCOORD0. No more pink textures, with the desired affect!

Will post more updates if any are necessary. Thanks for hanging out with me. :smiley: