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. Dismiss Notice

Transparent Shader + Fog Problem (Fixed)

Discussion in 'Editor & General Support' started by Kevinsomn1a, Mar 5, 2015.

  1. Kevinsomn1a

    Kevinsomn1a

    Joined:
    Dec 23, 2012
    Posts:
    38
    So, I recently upgraded my project to Unity 5 and messed around with the fancy lighting. However, a visual bug has been bothering me, which is that fog seems to be affecting transparent objects with areas containing alpha of 0 (completely transparent).

    Fog density has been amplified to reveal the problem.
    http://i.gyazo.com/008f7da1e8cb5ed0a477502559a152c0.png

    Has anyone encountered this problem before?
     
  2. antenna-tree

    antenna-tree

    Joined:
    Oct 30, 2005
    Posts:
    5,324
    Which transparent shader are you using for the decals?
     
  3. Kevinsomn1a

    Kevinsomn1a

    Joined:
    Dec 23, 2012
    Posts:
    38
    A custom written surface shader:

    This shader code was working prior to the upgrade.
    Code (CSharp):
    1. Shader "Bullet Hole Decal" {
    2.     Properties {
    3.         _Color ("Tint", Color) = (1, 1, 1, 1)
    4.         _MainTex ("Base (RGB)", 2D) = "white" {}
    5.         _HeatGlow ("Heat Glow", Range(0, 1)) = 1
    6.     }
    7.  
    8.     SubShader {
    9.         Tags {"Queue"="Transparent-1" "IgnoreProjector"="True" "RenderType"="Transparent"}
    10.         Fog {Mode Off}
    11.         ZTest LEqual
    12.         ZWrite Off
    13.         AlphaTest Greater 0
    14.         Offset -1, -1
    15.  
    16.         CGPROGRAM
    17.         #pragma surface surf Lambert alpha
    18.  
    19.         uniform sampler2D _MainTex;
    20.         uniform fixed4 _Color;
    21.         uniform float _HeatGlow;
    22.  
    23.         struct Input {
    24.             float2 uv_MainTex;
    25.         };
    26.  
    27.         void surf (Input IN, inout SurfaceOutput o) {
    28.             fixed4 tex = tex2D(_MainTex, IN.uv_MainTex) * _Color;
    29.  
    30.             half3 intensity = half3(0, 0, 0);
    31.             if(_HeatGlow > 0) {
    32.                 half2 center = half2(0.52, 0.49) - IN.uv_MainTex.xy;
    33.                 half dist = max(0, (0.05 + (_HeatGlow * 0.04)) - (sqrt(center.x * center.x + center.y * center.y) * 0.8));
    34.                 intensity = half3(1.0 + _HeatGlow * 0.5, 0.25 + _HeatGlow * 0.1, 0) * dist * 2.5;
    35.             }
    36.  
    37.             o.Albedo = tex.rgb + intensity;
    38.             o.Emission = intensity * 10 * _HeatGlow;
    39.             o.Alpha = tex.a;
    40.         }
    41.         ENDCG
    42.     }
    43. }
     
  4. antenna-tree

    antenna-tree

    Joined:
    Oct 30, 2005
    Posts:
    5,324
    Ok, well you have fog set properly. There is an edge around your decal that is not receiving fog though... otherwise they'd just be quad outlines. What if you boost your "AlphaTest Greater" by a very small amount?

    Either way, if this looked correct in 4.x then something has indeed changed in the rendering pipeline to make your shader act differently in 5.0. Whether it's a bug or not is unclear though.
     
    Kevinsomn1a likes this.
  5. Kevinsomn1a

    Kevinsomn1a

    Joined:
    Dec 23, 2012
    Posts:
    38
    Thank you very much! Increasing the AlphaTest value seemed to conceal it a bit more, but it is still noticeable (if I increase it too high, it starts to lose detail). I also found it strange that changing the shader code to this will blend perfectly with fog. I'm guessing that it's just Unity's rendering pipeline as you said. I will try using the shader source code as a reference to get the fog blending and see where I go from there. Anyway, thanks for your help once again, I really appreciate it!
     
  6. Kevinsomn1a

    Kevinsomn1a

    Joined:
    Dec 23, 2012
    Posts:
    38
    Well, it's not the rendering pipeline's fault, but it's just that the shader API changed slightly. I downloaded the built-in shaders source code from the Download Archive and took a look at the Transparent Diffuse, and sure enough it shows:

    #pragma surface surf Lambert alpha:blend

    So, I added :blend to the decal shader, and it worked like magic! Here's the fixed shader code for anyone who needs it:
    Code (CSharp):
    1. Shader "Bullet Hole Decal" {
    2.     Properties {
    3.         _Color ("Tint", Color) = (1, 1, 1, 1)
    4.         _MainTex ("Base (RGB)", 2D) = "white" {}
    5.         _HeatGlow ("Heat Glow", Range(0, 1)) = 1
    6.     }
    7.  
    8.     SubShader {
    9.         Tags {"Queue"="Transparent-1" "IgnoreProjector"="True" "RenderType"="Transparent"}
    10.         Lighting Off
    11.         ZTest LEqual
    12.         ZWrite Off
    13.         Blend SrcAlpha OneMinusSrcAlpha
    14.         AlphaTest Greater 0.01
    15.         Offset -1, -1
    16.  
    17.         CGPROGRAM
    18.         #pragma surface surf Lambert alpha:blend
    19.  
    20.         uniform sampler2D _MainTex;
    21.         uniform fixed4 _Color;
    22.         uniform float _HeatGlow;
    23.  
    24.         struct Input {
    25.             float2 uv_MainTex;
    26.         };
    27.  
    28.         void surf (Input IN, inout SurfaceOutput o) {
    29.             fixed4 tex = tex2D(_MainTex, IN.uv_MainTex) * _Color;
    30.  
    31.             half3 intensity = half3(0, 0, 0);
    32.             if(_HeatGlow > 0) {
    33.                 half2 center = half2(0.52, 0.49) - IN.uv_MainTex.xy;
    34.                 half dist = max(0, (0.05 + (_HeatGlow * 0.04)) - (sqrt(center.x * center.x + center.y * center.y) * 0.8));
    35.                 intensity = half3(1.0 + _HeatGlow * 0.5, 0.25 + _HeatGlow * 0.1, 0) * dist * 2.5;
    36.             }
    37.  
    38.             o.Albedo = tex.rgb + intensity;
    39.             o.Emission = intensity * 10 * _HeatGlow;
    40.             o.Alpha = tex.a;
    41.         }
    42.         ENDCG
    43.     }
    44. }
    Once again, thanks for your assistance and your quick response, attenna tree! I hope you have a good day/evening/night.
     
  7. antenna-tree

    antenna-tree

    Joined:
    Oct 30, 2005
    Posts:
    5,324
    Glad you got it working :)