What I want to achieve: I have two meshes inside of each other. The one on the inside shall fade out when you’re to close to it.
I’ve already got the shader running partially.
The shader for the inside mesh contains two passes. First one renders an outline by culling front faces and slightly expanding the mesh along its normals. The second one renders just a color.
The problem:
Here’s a screenshot of one part of the mesh which works as expected:
Now here’s another part of the mesh where the shader does not work correctly any more (it’s transparent but somehow not blending correctly):
Also another thing I couldn’t figure out: The “Outline” pass does not work when I remove the tags or change them to be transparent.
My shader setup looks like this:
SubShader
{
Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
ZWrite On
Blend SrcAlpha OneMinusSrcAlpha
LOD 100
Pass
{
Name "Outline"
Tags {
"RenderType"="Opaque"
"RenderPipeline"="UniversalPipeline"
"LightMode"="UniversalForward"
//... rest of code
}
Cull Front
Pass
{
Name "Toon"
//... rest of code
}
Fallback "VertexLix"
In both passes I use this code inside the vertex shader to determine the vertex world position:
o.worldPosition = mul(unity_ObjectToWorld, v.vertex);
Then I use this code in both passes for the fragment shader alpha color:
float offset = (_camDistOffset / 1000.);
float distToCam = distance(i.worldPosition, _WorldSpaceCameraPos) * offset;
float alpha = saturate(1. - 1. / distToCam);
return float4(color.r, color.g, color.b, alpha);