- Home /
Alpha Shader not working with obj collision
I've been trying to learn shader code, and am having trouble getting the alpha transparency to work correctly when the mesh is inside another one. In the picture below, the ball is completely inside the box and is being rendered like it is in front of the box when the alpha is off. When the alpha is on both are visible but it still looks like the ball is in front of the square. At some angles when the alpha is off the ball is not visible.
How can I stop this from happening?
][1]
Properties {
_MainColor ("Main Color", Color) = (1, 1, 1, 1)
_RimColor ("Rim Color", Color) = (0, 0, 0, 1)
_RimPower ("Rim Power", Range (-1, 10)) = 3.0
_AlphaRange ("Alpha Range", Range (0, 1)) = 1
_RimRange ("Rim Range", Range (.1, 2.5)) = 0
}
SubShader {
Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" }
LOD 200
CGPROGRAM
#pragma surface surf Lambert alpha
struct Input {
float4 color : Color;
float3 viewDir;
};
float4 _MainColor;
float4 _RimColor;
float _RimPower;
float _AlphaRange;
float _RimRange;
void surf (Input IN, inout SurfaceOutput o) {
IN.color = _MainColor;
half rim = _RimRange - saturate(dot(normalize(IN.viewDir), o.Normal));
o.Emission = _RimColor.rgb * pow(rim, _RimPower);
o.Alpha = _AlphaRange;
o.Albedo = _MainColor;
}
ENDCG
}
FallBack "Diffuse"
}
Answer by RafalDorsz · Sep 11, 2014 at 11:15 AM
Have you tried playing with setting various blending types? I don't know what result exactly you want, but it seems like this is what you should look into.
http://docs.unity3d.com/Manual/SL-Blend.html
Here are some examples:
Thanks for the links, one of them had another link to a page on depth testing.
To be more clear, the problem that I was having was, if obj-1 was inside obj-2. obj-1 was being rendered as if it was in front of obj-2. even though the "Queue"="Transparent" fixed that for objs that are just infront/behind each other.
for anyone else who comes across this problem I inserted this code above the CGPROGRA$$anonymous$$
Pass {
ZTest LEqual
Color$$anonymous$$ask 0
}
A $$anonymous$$or problem is that it doesn't work if the camera is too close on the objs, but that's not something I need.
Edit actually, when the alpha is on, the objs that are inside of it don't show (2nd edit I should clarify they don't show at certain angles but on some angles they do). I'll add an update if I figure out how to fix that.
Your answer
Follow this Question
Related Questions
Shader Help: Adding transparency to a shader. 2 Answers
Luminance to Alpha 0 Answers
Problem with alpha coloured shader 1 Answer