Transparent Shader + Fog Problem (Fixed)

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?

Which transparent shader are you using for the decals?

A custom written surface shader:

This shader code was working prior to the upgrade.

Shader "Bullet Hole Decal" {
    Properties {
        _Color ("Tint", Color) = (1, 1, 1, 1)
        _MainTex ("Base (RGB)", 2D) = "white" {}
        _HeatGlow ("Heat Glow", Range(0, 1)) = 1
    }

    SubShader {
        Tags {"Queue"="Transparent-1" "IgnoreProjector"="True" "RenderType"="Transparent"}
        Fog {Mode Off}
        ZTest LEqual
        ZWrite Off
        AlphaTest Greater 0
        Offset -1, -1

        CGPROGRAM
        #pragma surface surf Lambert alpha

        uniform sampler2D _MainTex;
        uniform fixed4 _Color;
        uniform float _HeatGlow;

        struct Input {
            float2 uv_MainTex;
        };

        void surf (Input IN, inout SurfaceOutput o) {
            fixed4 tex = tex2D(_MainTex, IN.uv_MainTex) * _Color;

            half3 intensity = half3(0, 0, 0);
            if(_HeatGlow > 0) {
                half2 center = half2(0.52, 0.49) - IN.uv_MainTex.xy;
                half dist = max(0, (0.05 + (_HeatGlow * 0.04)) - (sqrt(center.x * center.x + center.y * center.y) * 0.8));
                intensity = half3(1.0 + _HeatGlow * 0.5, 0.25 + _HeatGlow * 0.1, 0) * dist * 2.5;
            }

            o.Albedo = tex.rgb + intensity;
            o.Emission = intensity * 10 * _HeatGlow;
            o.Alpha = tex.a;
        }
        ENDCG
    }
}

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.

1 Like

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!

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:

Shader "Bullet Hole Decal" {
    Properties {
        _Color ("Tint", Color) = (1, 1, 1, 1)
        _MainTex ("Base (RGB)", 2D) = "white" {}
        _HeatGlow ("Heat Glow", Range(0, 1)) = 1
    }

    SubShader {
        Tags {"Queue"="Transparent-1" "IgnoreProjector"="True" "RenderType"="Transparent"}
        Lighting Off
        ZTest LEqual
        ZWrite Off
        Blend SrcAlpha OneMinusSrcAlpha
        AlphaTest Greater 0.01
        Offset -1, -1

        CGPROGRAM
        #pragma surface surf Lambert alpha:blend

        uniform sampler2D _MainTex;
        uniform fixed4 _Color;
        uniform float _HeatGlow;

        struct Input {
            float2 uv_MainTex;
        };

        void surf (Input IN, inout SurfaceOutput o) {
            fixed4 tex = tex2D(_MainTex, IN.uv_MainTex) * _Color;

            half3 intensity = half3(0, 0, 0);
            if(_HeatGlow > 0) {
                half2 center = half2(0.52, 0.49) - IN.uv_MainTex.xy;
                half dist = max(0, (0.05 + (_HeatGlow * 0.04)) - (sqrt(center.x * center.x + center.y * center.y) * 0.8));
                intensity = half3(1.0 + _HeatGlow * 0.5, 0.25 + _HeatGlow * 0.1, 0) * dist * 2.5;
            }

            o.Albedo = tex.rgb + intensity;
            o.Emission = intensity * 10 * _HeatGlow;
            o.Alpha = tex.a;
        }
        ENDCG
    }
}

Once again, thanks for your assistance and your quick response, attenna tree! I hope you have a good day/evening/night.

Glad you got it working :slight_smile: