- Home /
Custom Transparent shader is not transparent
I am writing a, hopefully, simple shader which will allow me to do 2 things. The first is to layer several textures together using each texture's own alpha as a mask. This part works. The second thing is to allow me to use an entirely separate texture for the geometry's transparency. I have copy/pasted example code into my shader and it does not seem to respond to changes in alpha at all. When I change my base texture to something with an alpha, that alpha is outlined by an orange line in the unity editor. However I am not passing that texture's alpha to the SurfaceOutputStandard object. Regardless, the geometry is never transparent, and changing the blending has no affect except when it's "One One". All I need to wrap up this problem is to be able to use a 4th and separate texture's alpha for the geometry's transparency.
Shader "Custom/multimage"
{
Properties
{
_Color ("Base Color", Color) = (1,1,1,1)
_MainTex ("Base", 2D) = "white" {}
_2ndColor ("2nd Color", Color) = (1,1,1,1)
_Detail ("Detail", 2D) = "white"{}
_Detail2("Detail2", 2D) = "white"{}
_AlphaTex("Alpha Map", 2D) = "white"{}
_Glossiness ("Smoothness", Range(0,1)) = 0.5
_Metallic ("Metallic", Range(0,1)) = 0.0
}
SubShader
{
Tags{ "Queue" = "Transparent" "RenderType" = "Transparent" "IgnoreProjector" = "True" }
ZWrite Off
Blend SrcAlpha OneMinusSrcAlpha
CGPROGRAM
// Physically based Standard lighting model, and enable shadows on all light types
#pragma surface surf Standard fullforwardshadows fragment frag
//#pragma fragment frag
// Use shader model 3.0 target, to get nicer looking lighting
#pragma target 3.0
sampler2D _MainTex;
sampler2D _Detail;
sampler2D _Detail2;
sampler2D _AlphaTex;
struct Input
{
float2 uv_MainTex;
float2 uv_Detail;
float2 uv_Detail2;
float2 uv_alpha;
};
half _Glossiness;
half _Metallic;
fixed4 _Color;
fixed4 _2ndColor;
fixed4 _AlphaMap;
void surf(Input IN, inout SurfaceOutputStandard o)
{
// Albedo comes from a texture tinted by color
fixed4 c0 = tex2D(_MainTex, IN.uv_MainTex) * _Color;
fixed4 c1 = tex2D(_Detail, IN.uv_Detail) * _2ndColor;
fixed4 c2 = tex2D(_Detail2, IN.uv_Detail2) * _2ndColor;
fixed4 a = tex2D(_AlphaTex, IN.uv_alpha);
//Each texture is blended with the base layer in turn
c0.rgb = c1.a * c1.rgb + (1 - c1.a) * c0.rgb;
c0.rgb = c2.a * c2.rgb + (1 - c2.a) * c0.rgb;
//final rgb output is all the layers blended with their respective transparencies
o.Albedo = c0.rgb;
// Metallic and smoothness come from slider variables
o.Metallic = _Metallic;
o.Smoothness = _Glossiness;
//The final alpha OUGHT to be the AlphaMap's alpha
o.Alpha = a.a;
}
ENDCG
}
FallBack "Diffuse"
}
Is the problem that I cannot process more than 3 textures at a time? I still can't get traditional transparency working. Given my specific use I am fine with using clip() to get a transparent cutout, but it never worked. Using one of the first 3 textures does work. It will impact my design if I cannot use 4 texture's however. So if I could use a single channel texture, perhaps, which won't count towards the limit that would work.
Answer by Bunny83 · Apr 11, 2019 at 01:12 AM
Your shader is not a transparent shader at all. All you did is specify the renderqueue and the rendertype tag. Those only control when this shader is actually rendered during the rendering stage. Those do not affect the actual shader behaviour. Please read the Writing Surface Shaders documentation. Your pragma directive needs the "alpha" directive. This will essentially enables alpha blending for this shader.
ps: you may also want to read about sub shader tags to understand their usage. Generally the whole Shader Reference category (on the left) contains all you need to know about Unity's shaderlab syntax.
Your answer
Follow this Question
Related Questions
Change alpha in runtime of gameObject with mobile shader 1 Answer
Cutout Shader getting Opaque when turning alpha off and on again 1 Answer
Transparency and blending issues with custom-made shader 1 Answer
How can i add transparency to this shader? 1 Answer
Transperency not working correctly on imported objects. 1 Answer