Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Woj_Gabel_FertileSky · Aug 20, 2012 at 11:23 AM · shadershaderlabcombine

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 :)

Comment
Add comment · Show 6
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Khada · Aug 20, 2012 at 12:34 PM 0
Share

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?

avatar image Woj_Gabel_FertileSky · Aug 22, 2012 at 12:46 PM 0
Share

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?

avatar image Khada · Aug 22, 2012 at 12:54 PM 0
Share

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)?

avatar image Woj_Gabel_FertileSky · Aug 23, 2012 at 04:45 AM 0
Share

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

avatar image Khada · Aug 23, 2012 at 05:15 AM 0
Share

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.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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"
}
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

When doing two passes on different alphas, shader gets overwritten. 0 Answers

Shader: Masked Tint with Transparency 0 Answers

Curve shader with lights 0 Answers

In ShaderLab, how could you get the colour of the texture you're rendering to? 0 Answers

Use lightmap data in shader 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges