Simple mobile shader - RGB + Alpha8

I’m trying to write shader that gets color from RGB texture and Alpha8 texture for alpha.

Shader looks like this:
Shader "SeparateAlphaOpacity" {//
Properties {
	_MainTex ("Diffuse", 2D) = "white" {}
	_AlphaTest("AlphaMap",2D) = "white" {}
}

SubShader {
	Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" }
	Blend SrcAlpha OneMinusSrcAlpha
	Cull Off
	Lighting Off 
	ZWrite Off
	Pass {  
		CGPROGRAM
		#pragma vertex vert
		#pragma fragment frag
		#include "UnityCG.cginc"

		
		sampler2D _MainTex;
		//float4 _MainTex_ST;
		sampler2D _AlphaTest;
		//float4 _ShadowTex_ST;
		
		struct vertexInput {
			half4 vertex : POSITION;
			fixed2 texcoord : TEXCOORD0;
		};

		struct vertexOutput {
			half4 pos : SV_POSITION;
			fixed2 tex : TEXCOORD0;
		};
		
		vertexOutput vert (vertexInput v)
		{
			vertexOutput o;
			o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
			o.tex = v.texcoord;
			return o;
		}
		
		half4 frag (vertexOutput i) : COLOR
		{
			half4 tex = tex2D(_MainTex, i.tex.xy); //,i.tex.xy * _MainTex_ST.xy + _MainTex_ST.zw);
			half4 alphaMap = tex2D(_AlphaTest, i.tex.xy);//,i.tex.xy * _ShadowTex_ST.xy + _ShadowTex_ST.zw);
			return half4(tex[0],tex[1],tex[2],alphaMap[3]);
		}
		ENDCG
		}
	}
}

Shader works on mobile platforms and on OSX, but doesn’t work on Windows.
It works also on windows if I return for example this half4(1,tex[1],tex[2],alphaMap[3]); Ofcourse one color is missing in this case. But whenever I plug all 4 colors into it, stops working.
There is no error, just it renders like whole alpha is value 1. (pure RGB image without transparency)

Any ideas about this strange issue?

Alpha8 only contains alpha, so you need to sample it like this;

half alphaMap = tex2D(_AlphaTest, i.tex.xy).a;

Also you’re better off using .xyz or .rgb rather than index.

return half4(tex.xyz,alphaMap);

Which lets you make things a bit easier on yourself;

fixed3 tex = tex2D(_MainTex, i.tex).rgb;
fixed alphaMap = tex2D(_AlphaTest, i.tex).a;
return fixed4(tex,alphaMap);

I’d also recommend keeping UVs as float2 rather than fixed2 unless you know you’re never going to need large UV values.
I’d also really recommend keeping position as float4.
And there’s no real reason to sample textures into anything other than fixed precision unless you’re using HDR textures…

Thanks for these few tips on better syntax. I’ve been using fixed for most of the stuff already, but i was trying half to see if there is any problem with fixed. This shader is meant for GUI elements which have clamped UVs, so I went with fixed uv coords aswell.

However this didn’t fix shader and it’s still working only on mac/android/ios. PC still rendering as if alpha values were 1.

Maybe try taking off the .a on the end of the alpha texture read… I’m just wondering if an alpha8 texture maps its alpha value to the Red channel?