- Home /
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 :)
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?
Ok, how to disable the lighting then? CGPROGRA$$anonymous$$ 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 CGPROGRA$$anonymous$$ with light calculation presets(Lambert). Isn't the simple one faster than using surface shader?
Bascially everything outside the CGPROGRA$$anonymous$$ block is part of shaderlab. I'm no shader pro, but can't you simply throw a 'Lighting Off' above the CGPROGRA$$anonymous$$ block in the original shader (inside the subshader)?
I though about that:) It seems it cannot be done. Even so, its still doing the lambert calculations in CGPROGRA$$anonymous$$.
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.
Answer by ScroodgeM · Aug 20, 2012 at 08:19 PM
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" }
Your answer
