Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Issues with Transparency on Transparency

Discussion in 'General Graphics' started by ATLAS-INTERACTIVE, Jun 9, 2015.

  1. ATLAS-INTERACTIVE

    ATLAS-INTERACTIVE

    Joined:
    Mar 3, 2014
    Posts:
    1,421
    I am designing a greenhouse scene for my portfolio (I stripped it down a few days ago), and have hit a pretty large issue graphically.
    when I use any kind of object with a transparent material over another object with a transparent material, it appears the order gets mixed up and one is rendered over the other (in the wrong order).

    Please ignore the fact that the screenshot is largely untextured, I have been trying to focus on this issue for a few hours now.

    In this case, the Ivy is physically over the glass, infront of it, but is being rendered as if it were behind the glass, this is evident by looking at the solid frame versus the glass there the Ivy intersects the two.

    Does anyone know how to fix this, it appears in scene and game view and is really limiting design?
     

    Attached Files:

  2. OskarSwierad

    OskarSwierad

    Joined:
    Dec 17, 2014
    Posts:
    25
    Sorting order problems are a normal thing, unfortunately.
    Use Cutout type of transparency for the leaves. It will be little difference visually. Cutout renders almost like a regular solid mesh, though, so no usual transparency problems.
     
    theANMATOR2b likes this.
  3. ATLAS-INTERACTIVE

    ATLAS-INTERACTIVE

    Joined:
    Mar 3, 2014
    Posts:
    1,421
    This worked well, thanks.
    Is this the only way to achieve this? What if I am placing objects that need transparency specifically? Is there any way to correct this?
     
  4. OskarSwierad

    OskarSwierad

    Joined:
    Dec 17, 2014
    Posts:
    25
    I don't know if there's any way to influence sorting in Unity Editor.

    In your own shader, you could use "Queue"="Transparent+AMOUNT".
    Then use this shader for objects that need to be always in front of other transparents.

    Normal code:
    Code (csharp):
    1.  
    2. SubShader {
    3.      Tags {
    4.        "RenderType" = "Transparent"
    5.        "Queue" = "Transparent"
    6.      }
    7.      Fog { Mode Off }
    8.  
    Your modified sorting priority:
    Code (csharp):
    1.  
    2. SubShader {
    3.      Tags {
    4.        "RenderType" = "Transparent"
    5.        "Queue" = "Transparent+10"
    6.      }
    7.      Fog { Mode Off }
    8.  
     
  5. OskarSwierad

    OskarSwierad

    Joined:
    Dec 17, 2014
    Posts:
    25
    Here's a replacement for a Standard PBR shader with Fade transparency.
    Change line 39 for your own sorting priority (e.g. Transparent+10).

    Code (csharp):
    1.  
    2.  
    3. // Author: Oskar Swierad
    4. // 11 June 2015
    5.  
    6. Shader "CustomStandard/Basic/Fade"
    7. {
    8.    Properties
    9.    {
    10.      _Color("Color", Color) = (1,1,1,1)
    11.      _MainTex("Albedo", 2D) = "white" {}
    12.  
    13.      // _Cutoff("Alpha Cutoff", Range(0.0, 1.0)) = 0.5   // for Cutout
    14.  
    15.      _MetallicGlossMap("Metallic", 2D) = "white" {}
    16.  
    17.      _BumpScale("Normal Strength", Float) = 1.0
    18.      _BumpMap("Normal Map", 2D) = "bump" {}
    19.  
    20.      // _ParallaxMap ("Height Map", 2D) = "black" {}
    21.      // _Parallax ("Height Scale", Range (0.005, 0.08)) = 0.02
    22.  
    23.      _OcclusionMap("Occlusion", 2D) = "white" {}
    24.      _OcclusionStrength("Occ Strength", Range(0.0, 1.0)) = 1.0
    25.  
    26.      // Blending state
    27.      // _Mode (0, 1, 2, 3) maps to (Opaque, Cutout, Fade and Transparent)
    28.      [HideInInspector] _Mode ("__mode", Float) = 3.0
    29.      [HideInInspector] _SrcBlend ("__src", Float) = 1.0
    30.      [HideInInspector] _DstBlend ("__dst", Float) = 0.0
    31.      [HideInInspector] _ZWrite ("__zw", Float) = 1.0
    32.    }
    33.  
    34.  
    35.    SubShader
    36.    {
    37.      Tags {
    38.        "RenderType" = "Transparent" // http://docs.unity3d.com/Manual/SL-ShaderReplacement.html
    39.        "Queue" = "Transparent+0"   // http://docs.unity3d.com/Manual/SL-SubShaderTags.html
    40.      }
    41.      LOD 300
    42.  
    43.      CGPROGRAM
    44.      #pragma target 3.0
    45.      #include "UnityPBSLighting.cginc"
    46.      #include "UnityStandardUtils.cginc"
    47.      #define _GLOSSYENV 1
    48.      // #define _ALPHATEST_ON   // for Cutout
    49.      #pragma surface surf Standard addshadow alpha:fade // or 'alphatest:_Cutoff' or 'alpha:premul' for Transparent
    50.  
    51.      uniform half4 _Color;
    52.      uniform sampler2D _MainTex;
    53.      // uniform half _Cutoff;   // for Cutout
    54.  
    55.      uniform sampler2D _MetallicGlossMap;
    56.      uniform float _BumpScale;
    57.      uniform sampler2D _BumpMap;
    58.      uniform fixed _OcclusionStrength;
    59.      uniform sampler2D _OcclusionMap;
    60.      //uniform float _Parallax;
    61.      //uniform sampler2D _ParallaxMap;
    62.  
    63.  
    64.  
    65.      struct Input {
    66.        float2 uv_MainTex;
    67.        // float4 color : COLOR;     // enable to use vertex color
    68.        // float3 viewDir;     // required when using parallax (height map)
    69.      };
    70.  
    71.  
    72.  
    73.      void surf (Input IN, inout SurfaceOutputStandard o )
    74.      {
    75.        // const fixed3 vertexColor = IN.color.rgb;
    76.  
    77.        half2 uv = IN.uv_MainTex;
    78.        // half2 parallax = ParallaxOffset1Step( tex2D( _ParallaxTex, uv ), _ParallaxStrength, IN.viewDir );
    79.        // uv += parallax;
    80.  
    81.        const half2 metalGloss = tex2D( _MetallicGlossMap, uv ).ra; // metalness is in R channel, smoothness is A
    82.  
    83.        // OUTPUTS
    84.        o.Albedo = _Color.rgb * tex2D( _MainTex, uv ).rgb; // * vertexColor;
    85.        o.Alpha = _Color.a * tex2D( _MainTex, uv ).a;
    86.        o.Normal = UnpackNormal( _BumpScale * tex2D( _BumpMap, uv ) );
    87.        o.Metallic = metalGloss.x;
    88.        o.Smoothness = metalGloss.y;
    89.        o.Occlusion = LerpOneTo( tex2D( _OcclusionMap, uv ).g, _OcclusionStrength ); // occlusion is in G channel
    90.      }
    91.  
    92.  
    93.      ENDCG
    94.    }
    95.  
    96.    FallBack "Standard"
    97. }
    98.  
    99.  
     
    Last edited: Jun 15, 2015
    theANMATOR2b likes this.
  6. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,199
    You can change the sorting order (render queue) on a per-material basis. You can either do it through scripting, or by looking at the material with the inspector in debug mode, and changing the "custom render queue" value.

    You get to debug mode by selecting the little drop-down in the inspector's upper right corner, next to the lock symbol.

    The render queue of objects influence how they're rendered - objects on a queue with ID <= 2500 are rendered front-to-back, while objects with a higher queue are rendered back-to-front. AlphaTest is at 2450, while Transparent is 3000, so you'll get different results between those two.

    See more here.
     
  7. OskarSwierad

    OskarSwierad

    Joined:
    Dec 17, 2014
    Posts:
    25
    Nice trick with the Debug inspector mode
     
  8. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    That approach solves it for one side, but if you then go to the other side it will not anymore be correct.
     
  9. SeriousBusinessFace

    SeriousBusinessFace

    Joined:
    May 10, 2014
    Posts:
    127
    The problem is probably that, while Unity sorts transparent objects back to front, that doesn't solve overlapping transparent objects.

    What I'd suggest is splitting your plant into two meshes, and placing one mesh in front of the glass and one mesh behind, so it still looks the same, but unity can sort the two meshes for in front/behind.
     
    theANMATOR2b likes this.
  10. OskarSwierad

    OskarSwierad

    Joined:
    Dec 17, 2014
    Posts:
    25
    Dantus: I see. What is the proper approach, or at least more correct?
     
  11. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    Split it up us much as needed, such that the sorting of Unity can handle it.

    I expect that the glass in the picture is one mesh. If it is split up into e.g. three parts, one on the left, one on the right and one below the plant, it becomes a lot easier to handle it. The plant itself should be split up as well depending on the positions from which it can be seen.

    It is very annoying to do that, that's out of question. But if a general solution is needed, this is the only way to handle it.
     
    OskarSwierad likes this.
  12. ChunkyToads

    ChunkyToads

    Joined:
    Jun 21, 2017
    Posts:
    8

    You are a lifesaver @Baste ...THANK YOU!