- Home /
Problem with alpha blending on shader Unity 5
Hi!
I am trying to create a shader to use as fog of war, but when the alpha is changed nothing happens, here is the code to the shader :
Shader "Custom/FoWMask" {
Properties {
_Color ("Main Color", Color) = (1,1,1,1)
_MainTex("Color (RGB)", 2D) = "white"
}
SubShader{
Tags{ "Queue" = "Transparent" "RenderType" = "Transparent" "LightMode" = "ForwardBase" }
Blend SrcAlpha OneMinusSrcAlpha
LOD 200
CGPROGRAM
#pragma surface surf Lambert
fixed4 _Color;
sampler2D _MainTex;
struct Input {
float2 uv_MainTex;
};
void surf(Input IN, inout SurfaceOutput o) {
half4 c = tex2D(_MainTex, IN.uv_MainTex);
o.Albedo = _Color.rgb - c.b;
o.Alpha = _Color.a - c.g;
}
ENDCG
}
FallBack "Diffuse"
}
and here is what it produces: ![alt text][1] [1]: /storage/temp/57656-shader-white.png White parts should be transparent. If I append alpha to the #pragma the output is very strange!! [alt text][2] [2]: /storage/temp/57658-shader-bug.png What am I doing wrong? Thanks!
Answer by helarts · Nov 06, 2015 at 02:35 PM
You need "alpha" in the surface declaration :
#pragma surface surf Lambert alpha
And either multiply your color with the texture or clamp the result:
o.Albedo = _Color.rgb * c.b;
o.Alpha = _Color.a * c.g;
or
o.Albedo = saturate(_Color.rgb - c.b);
o.Alpha = saturate(_Color.a - c.g);
You can also use a specific fog color like so:
o.Albedo = lerp(_FogColor.rgb, _Color.rgb, c.b);
Although I don't know what texture you are using so I'm just guessing. Note that "c.b" is the blue channel of your texture and c.g is the green channel.
Answer by keeperkai2 · Mar 14, 2016 at 05:45 AM
I found a way: To be more specific, you can do it in Unity 5 in two ways: The first way, as @helarts says, you simply use the "alpha" option to do this. And the surface shader compiler would add "Blend SrcAlpha OneMinusSrcAlpha" to the compiled vertex/fragement shader.
The second way, is used when you want to specify your own blending ways. In Unity 4, you didn't have to add any option in the #pragma line of the surface shader(you just Blend SrcAlpha One in front of the surface shader), but in Unity 5, you need to add "keepalpha" and specify your Blending options like: Blend SrcAlpha One It took me all afternoon to figure out... I was even thinking about giving up and sticking to Unity 4(I got 700+ shaders so...)
Your answer
Follow this Question
Related Questions
How can i add a alpha value to this shader? 1 Answer
Custom shader converting to urp need help 1 Answer
Is there a way to create an fading alpha-cutoff effect? 0 Answers
ShaderGraph / Shader URP Cap Alpha Value of Rendered Pixel 0 Answers
Scene Color Node in Shader Graph not working with Unity's 2D Renderer and URP 5 Answers