Smooth Overlap with Semi-Transparent Geometry

I’m having difficulty with drawing overlapping semi-transparent cubes. Basically, I’d like to have two semi-transparent blue cubes which overlap seamlessly. If I use the built-in Alpha-Diffuse shader, then there is a dark spot where the cubes overlap (see attached picture).

Most of the solutions to this that I’ve seen say to enable ZWrite on the shader, but that doesn’t work in this case for some reason. Below is a copy of my attempt at enabling ZWrite, it didn’t get rid of the dark overlapping area. I’m not very famiilar with shaders, so any help would be appreciated.

Shader "Transparent/DiffuseZWriteOff" {
Properties {
    _Color ("Main Color", Color) = (1,1,1,1)
    _MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
}

SubShader {
    Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
    LOD 200

    Pass
    {
        ZWrite On
        ColorMask 0
    }

CGPROGRAM
#pragma surface surf Lambert alpha

sampler2D _MainTex;
fixed4 _Color;

struct Input {
    float2 uv_MainTex;
};

void surf (Input IN, inout SurfaceOutput o) {
    fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
    o.Albedo = c.rgb;
    o.Alpha = c.a;
}
ENDCG
}

Fallback "Transparent/Diffuse"
}

Hi,

I had the same problem, and I found this shader code which solves it:

Cheers,
Nicolas

Thanks for the link, I tried that shader and I believe it doesn’t work if the overlapping happens between different meshes. My guess is that what I’m trying to do is actually impossible, because the rendering pipeline doesn’t allow it. Unfortunately, I know enough about that pipeline to verify that.

Hi,

did you try the stencil buffer?
Usually you can use the stencil buffer for this.
Here is a example for sprites but this should work for meshes too: http://wompipomp.blogspot.de/2015/10/use-stencil-buffer-to-prevent.html

Put this before CGPROGRAM
Stencil
{
Ref 1
Comp Greater
Pass IncrSat
}

both meshes must use the shader with the stencil buffer.

Cheers
Mark

The Stencil worked perfectly, thanks :smile:

Final Shader is

Shader "Test/Alpha-Diffuse-Test" {
Properties {
    _Color ("Main Color", Color) = (1,1,1,1)
    _MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
}

SubShader {
    Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
    LOD 200

    Stencil
{
Ref 1
Comp Greater
Pass IncrSat
}

CGPROGRAM
#pragma surface surf Lambert alpha

sampler2D _MainTex;
fixed4 _Color;

struct Input {
    float2 uv_MainTex;
};

void surf (Input IN, inout SurfaceOutput o) {
    fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
    o.Albedo = c.rgb;
    o.Alpha = c.a;
}
ENDCG
}

Fallback "Transparent/VertexLit"
}