- Home /
How to add transparency to a shader!?
I want to make it so that this shader can read the alpha properties from a texture and apply them. I cant seem to get it to work. :( The shader is below. Please help!
Shader "Planet" {
Properties {
_MainTex ("Diffuse(RGB) Spec(A)", 2D) = "white" {}
_BumpMap ("Bumpmap", 2D) = "bump" {}
_RimColor ("Rim Color", Color) = (0.26,0.19,0.16,0.0)
_RimPower ("Rim Power", Range(0.5,8.0)) = 3.0
_SpecColor ("Specular Color", Color) = (0.5,0.5,0.5,1)
_Shininess ("Shininess", Range (0.01, 1)) = 0.078125
}
SubShader {
Tags { "RenderType" = "Opaque" }
CGPROGRAM
#pragma surface surf SimpleSpecular
float _Shininess;
half4 LightingSimpleSpecular (SurfaceOutput s, half3 lightDir, half3 viewDir, half atten) {
half3 h = normalize (lightDir + viewDir);
half diff = max (0, dot (s.Normal, lightDir));
float nh = max (0, dot (s.Normal, h));
float spec = pow (nh, 48.0);
half4 c;
c.rgb = (s.Albedo * _LightColor0.rgb * diff + _LightColor0.rgb * spec * s.Alpha * _Shininess * _SpecColor) * (atten * 2);
c.a = s.Alpha;
return c;
}
struct Input {
float2 uv_MainTex;
float2 uv_BumpMap;
float3 viewDir;
};
sampler2D _MainTex;
sampler2D _BumpMap;
float4 _RimColor;
float _RimPower;
void surf (Input IN, inout SurfaceOutput o) {
o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb;
o.Normal = UnpackNormal (tex2D (_BumpMap, IN.uv_BumpMap));
half rim = 1.0 - saturate(dot (normalize(IN.viewDir), o.Normal));
o.Emission = _RimColor.rgb * pow (rim, _RimPower);
o.Alpha = tex2D (_MainTex, IN.uv_MainTex).a;
}
ENDCG
}
Fallback "Diffuse"
}
Answer by Tim-Michels · Nov 28, 2012 at 07:30 AM
I took a look at your shader and added alpha to it. I also optimized the fact that you sample your texture twice, once for rgb and once for your alpha. It's better to store the tex2D in a fixed4, so you only need to sample your texture once.
the 'alpha' keyword was added and the RenderType was set to 'Transparent'
Here's your new shader:
Shader "Planet" {
Properties {
_MainTex ("Diffuse(RGB) Spec(A)", 2D) = "white" {}
_BumpMap ("Bumpmap", 2D) = "bump" {}
_RimColor ("Rim Color", Color) = (0.26,0.19,0.16,0.0)
_RimPower ("Rim Power", Range(0.5,8.0)) = 3.0
_SpecColor ("Specular Color", Color) = (0.5,0.5,0.5,1)
_Shininess ("Shininess", Range (0.01, 1)) = 0.078125
}
SubShader {
Tags { "RenderType" = "Transparent" }
CGPROGRAM
#pragma surface surf SimpleSpecular alpha
float _Shininess;
half4 LightingSimpleSpecular (SurfaceOutput s, half3 lightDir, half3 viewDir, half atten) {
half3 h = normalize (lightDir + viewDir);
half diff = max (0, dot (s.Normal, lightDir));
float nh = max (0, dot (s.Normal, h));
float spec = pow (nh, 48.0);
half4 c;
c.rgb = (s.Albedo * _LightColor0.rgb * diff + _LightColor0.rgb * spec * s.Alpha * _Shininess * _SpecColor) * (atten * 2);
c.a = s.Alpha;
return c;
}
struct Input {
float2 uv_MainTex;
float2 uv_BumpMap;
float3 viewDir;
};
sampler2D _MainTex;
sampler2D _BumpMap;
float4 _RimColor;
float _RimPower;
void surf (Input IN, inout SurfaceOutput o) {
fixed4 c = tex2D (_MainTex, IN.uv_MainTex);
o.Albedo = c.rgb;
o.Normal = UnpackNormal (tex2D (_BumpMap, IN.uv_BumpMap));
half rim = 1.0 - saturate(dot (normalize(IN.viewDir), o.Normal));
o.Emission = _RimColor.rgb * pow (rim, _RimPower);
o.Alpha = c.a;
}
ENDCG
}
Fallback "Diffuse"
}
Feel free to mark as answered if this is what you need.
Cheers ;)
NICE! Its almost perfect. The one thing that I was trying to avoid was the fact that when a texture with transparency is attached, the rim color only creates that rim glow where the texture is visible... When applied to a sphere, only the visible parts are glowing around the edge, but I wanted that whole sphere to be have that glow.. so if would be a circle.. not just the visible parts. I hope this makes sense. If so, is it possible?
Change the line o.Alpha = c.a; to o.Alpha = c.a + rim; I hope that's the result you're going for...
O$$anonymous$$G YESSSSS! YOU ARE A GODSEND! Thank you so much!!!!! <3 $$anonymous$$y birthday is in 1 $$anonymous$$ute.. I'll take this as an early b-day present! ;) Thanks again!!!!
So, I ran into a few problems. In my scene, I have two objects. The earth and the atmosphere. The atmosphere is bigger than the earth object. I'm applying this shader to the atmosphere (which needs opacity for the clouds) but for some reason it doesn't show. I also have a moon object in the scene so I tried applying the shader to that as well (just to test) and for some reason when the moon travels over an object with a regular diffuse shader, it disappears. Any reason as to what this happens? Is there a fix for it?
Try changing the line
Tags { "RenderType" = "Transparent" }
to
Tags { "RenderType" = "Transparent" "Queue" = "Transparent" }
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Get a UV texture of a models surface 2 Answers
Blending one material/texture/shader to another 1 Answer
What's the syntax for creating a skybox with a script? 2 Answers
CRT/LCD screen shader (for materials) 4 Answers