Search Unity

Smooth Overlap with Semi-Transparent Geometry

Discussion in 'Shaders' started by zebrose, Oct 22, 2015.

  1. zebrose

    zebrose

    Joined:
    Oct 19, 2015
    Posts:
    5
    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.

    Code (csharp):
    1.  
    2. Shader "Transparent/DiffuseZWriteOff" {
    3. Properties {
    4.     _Color ("Main Color", Color) = (1,1,1,1)
    5.     _MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
    6. }
    7.  
    8. SubShader {
    9.     Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
    10.     LOD 200
    11.  
    12.     Pass
    13.     {
    14.         ZWrite On
    15.         ColorMask 0
    16.     }
    17.  
    18. CGPROGRAM
    19. #pragma surface surf Lambert alpha
    20.  
    21. sampler2D _MainTex;
    22. fixed4 _Color;
    23.  
    24. struct Input {
    25.     float2 uv_MainTex;
    26. };
    27.  
    28. void surf (Input IN, inout SurfaceOutput o) {
    29.     fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
    30.     o.Albedo = c.rgb;
    31.     o.Alpha = c.a;
    32. }
    33. ENDCG
    34. }
    35.  
    36. Fallback "Transparent/Diffuse"
    37. }
    38.  
    39.  
     

    Attached Files:

  2. Nicolas-Liatti

    Nicolas-Liatti

    Joined:
    Jun 19, 2013
    Posts:
    89
  3. zebrose

    zebrose

    Joined:
    Oct 19, 2015
    Posts:
    5
    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.
     
  4. Wompipomp

    Wompipomp

    Joined:
    Aug 31, 2014
    Posts:
    24
    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
     
  5. zebrose

    zebrose

    Joined:
    Oct 19, 2015
    Posts:
    5
    The Stencil worked perfectly, thanks :D

    Final Shader is
    Code (csharp):
    1.  
    2. Shader "Test/Alpha-Diffuse-Test" {
    3. Properties {
    4.     _Color ("Main Color", Color) = (1,1,1,1)
    5.     _MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
    6. }
    7.  
    8. SubShader {
    9.     Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
    10.     LOD 200
    11.  
    12.     Stencil
    13. {
    14. Ref 1
    15. Comp Greater
    16. Pass IncrSat
    17. }
    18.  
    19. CGPROGRAM
    20. #pragma surface surf Lambert alpha
    21.  
    22. sampler2D _MainTex;
    23. fixed4 _Color;
    24.  
    25. struct Input {
    26.     float2 uv_MainTex;
    27. };
    28.  
    29. void surf (Input IN, inout SurfaceOutput o) {
    30.     fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
    31.     o.Albedo = c.rgb;
    32.     o.Alpha = c.a;
    33. }
    34. ENDCG
    35. }
    36.  
    37. Fallback "Transparent/VertexLit"
    38. }
    39.  
    40.