I’ve been writing 2 relatively simple surface shaders whose purpose is to allow for variable alpha in the last few days and I’ve been having problems with the rendering of the actual meshes. One mesh is inside another and at certain angles the nested mesh does not render. Both meshes need have variable transparency.
The image displays the problem. The second image illustrates why I include the first pass. The second pass is to rendor the backface .
The Outer Mesh Shader code is as follows :
Shader "ShaderEnamel" {
Properties{
_MainTex("Base (RGB) Trans (A)", 2D) = "white" {}
_Color("Main Color", Color) = (1,1,1,1)
}
SubShader{
Pass{
ColorMask 0
Offset 1,1
}
Pass{
ZTest Equal
ZWrite Off
Cull Front
Color[_Color]
}
Tags{ "Queue" = "Transparent" "IgnoreProjector" = "True" "RenderType" = "Transparent" }
ZWrite Off
Lighting On
LOD 200
CGPROGRAM
#pragma surface surf Lambert alpha:auto
half4 LightingWrapLambert(SurfaceOutput s, half3 lightDir, half atten) {
half NdotL = dot(s.Normal, lightDir);
half diff = NdotL * 0.5 + .2;
half4 c;
c.rgb = s.Albedo * _LightColor0.rgb * (diff * atten);
c.a = s.Alpha;
return c;
}
struct Input
{
float2 uv_MainTex;
float3 worldPos;
float3 viewDir;
float3 worldNormal;
};
sampler2D _MainTex;
float4 _section;
fixed4 _Color;
void surf(Input IN, inout SurfaceOutput o) {
float toClip = _section.x * 0.1 * IN.worldPos.x +
_section.y * 0.1 * IN.worldPos.y +
_section.z * 0.1 * IN.worldPos.z +
_section.w;
clip(toClip);
float fd = dot(IN.viewDir, IN.worldNormal);
if (fd.x > 0)
{
fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
o.Albedo = c.rgb;
o.Alpha = c.a;
}
}
ENDCG
}
Fallback "Diffuse"
}
The inner mesh shader code :
Shader "ShaderDentin" {
Properties{
_MainTex("Base (RGB) Trans (A)", 2D) = "white" {}
_section("Section plane (x angle, y angle, z angle, w displacement)", vector) = (0,0,0,.15)
_Color("Main Color", Color) = (1,1,1,1)
}
SubShader{
// Render into depth buffer only
// extra pass that renders to depth buffer only
Pass{
ColorMask 0
ZTest LEqual
Offset -1,-1
}
//Rendor the backface of the mesh - If backface culling is disabled in the main shader block then you get wierd z-fighting issues
Pass{
ZWrite Off
Cull Front
Color[_Color]
}
Tags{ "Queue" = "Transparent" "IgnoreProjector" = "True" "RenderType" = "Transparent" }
Offset -1,-1
ZWrite Off
LOD 200
CGPROGRAM
#pragma surface surf SimpleSpecular alpha:auto
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) / 15;
diff = diff / 1.5;
half4 c;
c.rgb = (s.Albedo * _LightColor0.rgb * diff + _LightColor0.rgb * spec) * atten;
c.a = s.Alpha;
return c;
}
struct Input
{
float2 uv_MainTex;
float3 worldPos;
float3 viewDir;
float3 worldNormal;
};
sampler2D _MainTex;
float4 _section;
fixed4 _Color;
void surf(Input IN, inout SurfaceOutput o) {
float toClip = _section.x * 0.1 * IN.worldPos.x +
_section.y * 0.1 * IN.worldPos.y +
_section.z * 0.1 * IN.worldPos.z +
_section.w;
clip(toClip);
float fd = dot(IN.viewDir, IN.worldNormal);
if (fd.x > 0)
{
fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
o.Albedo = c.rgb;
o.Alpha = c.a;
}
}
ENDCG
}
Fallback "Diffuse"
}
I am extremely novice (as I am sure is clear from the shader I am using) but any incite would be greatly appreciated.