x


Combine Alphatest and AlphaBlending in shaderlab

Hi! I want to make a progress bar. The way i choose to control the bar is by shader cutoff value. You can find that solution somewhere in here for the circular progress bar. I also would want to have nice blending going on in that shader, so I have found a solution:

Shader "Custom/cutoutalpha" {
Properties {
    _Color ("Main Color", Color) = (1,1,1,1)
    _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

CGPROGRAM
#pragma surface surf Lambert alpha

sampler2D _MainTex;
sampler2D _CutTex;
fixed4 _Color;
float _Cutoff;

struct Input {
    float2 uv_MainTex;
};

void surf (Input IN, inout SurfaceOutput o) {
    fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
    float ca = tex2D(_CutTex, IN.uv_MainTex).a;
    o.Albedo = c.rgb;

    if (ca > _Cutoff)
      o.Alpha = c.a;
    else
      o.Alpha = 0;
}
ENDCG
}

Fallback "Transparent/VertexLit"
}

(Sorry, I cant find the link to this).

Its a shader that does what i want. Main texture is the progress bar, cutTex is the gradient alpha ramp. Problem is that this shader is a surface shader, and i want my texture to be unlit. So i tried to convert this shader to shaderlab, but cant get it to work. Any ideas?

And is this solution better that say, making 3 textures(left end, right end, middle) and re sizing the middle(with re sizing mainTexture of the middle part texture)? Its for pc.

Anyway i came up with this in shaderlab:

Shader "Custom/cutoutAlphaUnlit" {

    Properties 
    {
    _Color ("Main Color", Color) = (1,1,1,1)
    _MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
    _CutTex ("Cutout (A)", 2D) = "white" {}
    _Cutoff ("Alpha cutoff", Range(0,1)) = 0.5
    }

    Category 
    {
        Lighting Off
        ZWrite Off
        Cull back
        Tags {Queue=Transparent}

        SubShader 
        {
            Pass 
            {
                Blend SrcAlpha OneMinusSrcAlpha
                SetTexture [_MainTex] 
                {
                    ConstantColor [_Color]
                    Combine Texture * constant
                }
            }
            Pass
            {
          AlphaTest Greater [_Cutoff]
                SetTexture[_CutTex]
                {
                combine previous * texture,previous*texture 
                }
            }
           //end of my passes 
        }//end subshader1
    }
}

Which texture should get the alpha test? Both of them? And how can I make the alpha test on the main texture? Can i do it in one pass? I know its not much, but Im not so fluent in writing shaders in shaderlab. Or any other shaders really :)

more ▼

asked Aug 20 '12 at 11:23 AM

Sedesikus gravatar image

Sedesikus
57 11 17 18

Both shaders are using shaderlab. If the first one does what you want, why not use that and disable lighting if that's all you need?

Aug 20 '12 at 12:34 PM Khada

Ok, how to disable the lighting then? CGPROGRAM uses presets in calculating lighting. And people on the forums say that if you are not using light, then don't use surface shaders. And i didn't know that both of them are in shaderLab. I meant that one is in shaderlab(simple) and the other (surface) is with CGPROGRAM with light calculation presets(Lambert). Isn't the simple one faster than using surface shader?

Aug 22 '12 at 12:46 PM Sedesikus

Bascially everything outside the CGPROGRAM block is part of shaderlab. I'm no shader pro, but can't you simply throw a 'Lighting Off' above the CGPROGRAM block in the original shader (inside the subshader)?

Aug 22 '12 at 12:54 PM Khada

I though about that:) It seems it cannot be done. Even so, its still doing the lambert calculations in CGPROGRAM.

Aug 23 '12 at 04:45 AM Sedesikus

Hmm, suck. Don't think I can help. Shaders aren't my strong point (have 'The CG Tutorial' book on the way atm so hoping to change that soon!). Best of luck.

Aug 23 '12 at 05:15 AM Khada
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first
Shader "Custom/cutoutalpha"
{
	Properties
	{
		_Color ("Main Color", Color) = (1,1,1,1)
		_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
		ZWrite Off
		Fog { Mode Off }
		Blend SrcAlpha OneMinusSrcAlpha
		Pass
		{
			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag
			sampler2D _MainTex;
			sampler2D _CutTex;
			fixed4 _Color;
			float _Cutoff;
			struct appdata
			{
				float4 vertex : POSITION;
				float4 texcoord : TEXCOORD0;
			};
			struct v2f
			{
				float4 pos : SV_POSITION;
				float2 uv : TEXCOORD0;
			};
			v2f vert (appdata v)
			{
				v2f o;
				o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
				o.uv = v.texcoord.xy;
				return o;
			}
			half4 frag(v2f i) : COLOR
			{
				fixed4 c = _Color * tex2D(_MainTex, i.uv);
				fixed ca = tex2D(_CutTex, i.uv).a;
				c.a *= ca > _Cutoff ? 1f : 0f;
				return c;
			}
			ENDCG
		}
	}
	Fallback "Transparent/VertexLit"
}
more ▼

answered Aug 20 '12 at 08:19 PM

ScroodgeM gravatar image

ScroodgeM
7.6k 2 6

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1650
x96
x74
x4
x1

asked: Aug 20 '12 at 11:23 AM

Seen: 951 times

Last Updated: Aug 26 '12 at 12:20 AM