trying to fade part of texture based on slider

Hi all, I have almost nailed this shader, but came across something just near the finish line.

I believe it is because it thinks my material is white without me declaring it. I can slide my fader along but I get it fade to a greyish black, not just black.

Ideally I just want it to fade the current pixel colour!

// Upgrade NOTE: replaced 'glstate.matrix.mvp' with 'UNITY_MATRIX_MVP'

Shader "Custom/gui/guage_shader" {
 
Properties {
		_MainTex ("Base (RGB)", 2D) = "white" {}
		_Percentage("Shaded % of texture", Range(0,1)) = 0.5
		_ShadeAmount("Shade Level", Range(0,1)) = 0.5
	}
	SubShader 
	{
		Pass 
		{
			Tags { "RenderType"="Transparent" }
			Blend SrcAlpha OneMinusSrcAlpha			

			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag
	
			uniform sampler2D 	_MainTex;
			uniform float4 		_MainTex_ST; 
			float		_Percentage;
			float		_ShadeAmount;
	
			struct vertex_input {
				float4 vertex : POSITION;
				float4 texcoord : TEXCOORD0;
			};
	
			struct vertex_output {
				float4 pos : SV_POSITION;
				float4 tex : TEXCOORD0;
			};
			
			vertex_output vert(vertex_input input)
			{
				vertex_output output;
				output.tex = input.texcoord;
				output.pos = mul(UNITY_MATRIX_MVP, input.vertex);
				return output;
			}
			
			float4 frag(vertex_output input) : COLOR
			{
				float4 col = tex2D(_MainTex, _MainTex_ST.xy * input.tex.xy + _MainTex_ST.zw);
				if (input.tex.x > _Percentage)
					col.a = _ShadeAmount;
				return col;
			}
				
			ENDCG
		} 
	}
 
}

it’s for a guage in a cockpit…for example, at 80% power I want the guage to be 80% complete opacity, the remaining 20% to have x amount of transparency (_ShaderAmount). I have not played much more with the blending equation.

Here is a result of it:
1209295--49033--$Untitled.png

Ideally, I want it to fade to a dark green bar but without the greyness over the texture. Even the full opacity is causing some grey? The texture it comes from has transparency outside of the bars. I am thinking (without testing) I should maybe try an additive blend. But the full opacity mixed with grey makes me think maybe unity is applying some white to it?

Hopefully I have made some sense…any help is appreciated.

Thanks for the help,

The grey is part of the texture (only in unity, it is from a transparent image map) the black beyond that is another texture behind it. Ideally i am looking for the standard transparency shown in the normal transparent shaders in unity.

I applied your fix and now the fade works without fading that greyness, so I get a constant grey on the texture all over, regardless of where the blend is being applied. Better result and one final step to go.

1209516--49073--$Untitled.png

Thoughts?

Did you mean use two textures in one shader and the slider just dictates the size of the mask?
Also, what did you mean by ‘squeeze the mask into one of the channels of your texture, if your requirements allow it.’ ? I am developing for an IOS game.
Going forward once this works like the normal unity transparent textures I will setup this texture a little better.

Got it! it was to do with Z buffer writes being left on. We dont want to write to the z buffer because we don’t overwrite whats in the buffer, rather combine (transparency) with it for our result.

The only remaining problem is that when you dont have a black background, the effect is not as good, so it probably needs a second pass, which im still too noob to figure out…however by tweaking you could use a solid colour instead and it would work quite well…

*** update - this was fixed by multiplying col.a and _ShadeAmount : col.a *= _ShadeAmount;

Here is the shader code now :

Shader "Custom/gui/guage_shader" {
 
Properties {
		_MainTex ("Base (RGB)", 2D) = "white" {}
		_Percentage("Non-shaded % of texture", Range(100,0)) = 0.5
		_ShadeAmount("Shade Level", Range(0,1)) = 0.5
	}
	SubShader 
	{
	   	Tags {"Queue" = "Transparent" }
		Pass 
		{
		   ZWrite Off
		   Blend SrcAlpha OneMinusSrcAlpha
	        
			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag
	
			uniform sampler2D 	_MainTex;
			uniform float4 		_MainTex_ST; 
			float		_Percentage;
			float		_ShadeAmount;
	
			struct vertex_input {
				float4 vertex : POSITION;
				float4 texcoord : TEXCOORD0;
			};
	
			struct vertex_output {
				float4 pos : SV_POSITION;
				float4 tex : TEXCOORD0;
			};
			
			vertex_output vert(vertex_input input)
			{
				vertex_output output;
				output.tex = input.texcoord;
				output.pos = mul(UNITY_MATRIX_MVP, input.vertex);
				return output;
			}
			
			float4 frag(vertex_output input) : COLOR
			{
				float4 col = tex2D(_MainTex, _MainTex_ST.xy * input.tex.xy + _MainTex_ST.zw);
				if (input.tex.x < _Percentage/100)
					col.a *= _ShadeAmount;
				return col;
			}
				
			ENDCG
		} 
	}
 
}