Hi! I'm currently trying to do something pretty simple : modify the alpha value into a fragment shader. This is the code of my shader : Code (CSharp): Shader "UI/GreenBackgroundRemover" { Properties { _MainTex ("Base (RGB)", 2D) = "white" {} } SubShader { Pass { CGPROGRAM #pragma vertex vert_img #pragma fragment frag #include "UnityCG.cginc" uniform sampler2D _MainTex; fixed4 frag(v2f_img i) : SV_Target { fixed4 r = tex2D(_MainTex, i.uv); if(r.g >= 0.7f) { r.a = 0; r.g = r.r = r.b = 0; } return r; } ENDCG } } } I use this shader into a material. Here is my initial texture: And here is the result of my shader: As you can see the pixels are set to black but the alpha value is not modified. Does anyone have an idea about what I'm missing here? Thanks in advance for your help!
You are missing a blending method, I don't know how you can add it directly in the fragment shader but you can do it with shaderlab (cf : http://docs.unity3d.com/Manual/SL-Blend.html). In general you just have to add it ( Blend SrcAlpha OneMinusSrcAlpha) wich is the basic alpha blending. Code (CSharp): Shader "UI/GreenBackgroundRemover" { Properties { _MainTex ("Base (RGB)", 2D) = "white" {} } SubShader { Blend SrcAlpha OneMinusSrcAlpha Pass { CGPROGRAM #pragma vertex vert_img #pragma fragment frag #include "UnityCG.cginc" uniform sampler2D _MainTex; fixed4 frag(v2f_img i) : SV_Target { fixed4 r = tex2D(_MainTex, i.uv); if(r.g >= 0.7f) { r.a = 0; r.g = r.r = r.b = 0; } return r; } ENDCG } } }
@Fuegan Thanks a lot for your answer! Although it seems there is a rendering order problem, instead of seeing the object behing my plane I see the color of the background of my camera. And what is funny is that it is the good result in the Scene Tab but not in the Game Tab. In Scene Tab : In the Game Tab : I'll look further into the link you gave me!
As you don't have any tags set it seems Unity doesn't render your objects in the right order yes. Adding this Code (CSharp): Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"} between the SubShader and Blend lignes should resolve the problem. The main part is "Queue"="Transparent" wich is the order in the queue (Transparent are renderer after opaque objects). The RenderType part is for Unity (it will be treated as a transparent object) I don't know exactly what it does but I suppose it has to do with some optimisation. IgnoreProjector is usually added to transparent shaders as projectors don't work well with transparency. (But it is useless if you don't use projectors). PS : without tags unity sets the objects as "Queue"="Geometry" which means if your background have a standard shader without transparency your plane should have still be rendered last so be sure your objects are not too close (as it can cause flickering).
Thanks for the explanation! I finally had to invert the order of my camera and my objects in z to make it work.
I have a similar problem. I am using a fragment shader and cannot get alpha working. Code (CSharp): Shader "Unlit/SpectrumShader" { Properties { _MainTex ("Base (RGB)", 2D) = "white" {} } SubShader { Tags {"Queue"="Transparent" "IgnoreProjector"="True"} Blend SrcAlpha OneMinusSrcAlpha Pass { CGPROGRAM #pragma vertex vert #pragma fragment frag #include "UnityCG.cginc" struct appdata { float4 vertex : POSITION; float2 uv : TEXCOORD0; }; struct v2f { float2 uv : TEXCOORD0; float4 vertex : SV_POSITION; float4 screenPos : TEXCOORD1; }; float _Offset; float _MainTex; v2f vert (appdata v) { v2f o; UNITY_INITIALIZE_OUTPUT(v2f,o); o.vertex = UnityObjectToClipPos(v.vertex); o.screenPos = ComputeScreenPos(o.vertex); return o; } fixed4 frag (v2f i) : SV_Target { float colourOffset = i.screenPos.y + _Offset; fixed4 col = float4((colourOffset * 10) % 1.0f, colourOffset % 1.0f, (colourOffset / 10) % 1.0f, 1.0f); return col; } ENDCG } } } Can anyone provide me with some guidance to get alpha working? I have my shader on a material, I apply the material to a sprite (say, a donut) and there is no alpha (the donut will look like a solid circle).
Set it to something other than 1.0? That float4 you’re setting, the 4 components are red, green, blue, and alpha. And you’re setting the alpha to 1.0.
Thank you, yes. I do understand if I set that to 0.5 it will be half opacity for the entire material. However, the issue that I am having is I would like to 'knock out' the pieces that are transparent and show the pieces that are opaque. Does that make sense? Sorry, I am a complete shader noob and have been trying to figure out what I should be doing here. I really appreciate any help you can provide. I have attached a screenshot of what I mean. For example the sprite on the left is a donut. And the sprite on the right should be too (it is the same sprite) however it is showing as a solid circle with my shader. How can I make my shader also have the hole in the middle?
There's absolutely nothing in your shader making it a circle. I suspect your "donut" sprite is set to use a tight sprite mesh, which is generating a circular mesh, and that's what you're seeing when you use your shader. If you want your shader to have the same alpha as your sprite, then you need to sample the sprite texture in your shader and use just the alpha. See the default sprite shader, and the cginc file that most of the actual useful shader code is in. https://github.com/TwoTailsGames/Un.../DefaultResourcesExtra/Sprites-Default.shader https://github.com/TwoTailsGames/Unity-Built-in-Shaders/blob/master/CGIncludes/UnitySprites.cginc Copy the parts from the SpriteVert and SpriteFrag functions you need. And maybe go through some beginner tutorials on shaders if you're still having issues. https://www.alanzucconi.com/2015/06/10/a-gentle-introduction-to-shaders-in-unity3d/ (there are 5 parts)