Missing fog in alpha testing material

In my current project I am using two materials: an opaque one with alpha testing for already loaded parts (chunks) of my world and a transparent one for recently loaded parts. The transparent one renders with fog but the opaque one with alpha testing doesn’t! Here’s an image to illustrate the problem:

Note: Although the transparent material would be transparent in general, I set its alpha value to 1, so actually in the image above the world parts are NOT transparent! The brighter colors are due to the fog only.

The interesting thing here is that, if I change “Queue” = “AlphaTest” in the alpha testing shader to “Queue” = “Transparent” I will get fog (but also loose shadows). So why does this happen? Is this a bug? Can this be solved somehow? I really would like to have some fog.

These are the shaders I am using: The transparent shader that has fog:

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

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

  LOD 200

  CGPROGRAM
  #pragma surface surf Lambert alpha

  sampler2D _MainTex;
  fixed4 _Color;

  struct Input
  {
  float2 uv_MainTex;
  float4 color : COLOR;
  float3 worldNormal;
  float3 worldPos;
  };

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

  o.Albedo = c.rgb;
  o.Alpha  = _Color.a;
  }

  ENDCG
  }

  Fallback "Transparent/VertexLit"
}

…and the opaque material with alpha testing (the shader that doesn’t generate fog):

Shader "OpaqueAlphaTestingMaterial"
{
  Properties
  {
  _Color ("Main Color", Color)  = (0.5,0.5,0.5,1)
  _MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
  _Grid ("Base (RGA) Trans (A)", 2D)  = "white" {}
  _Cutoff ("Alpha cutoff", Range(0,1))  = 0.5
  }

  SubShader
  {
  Tags
  {
  "Queue"  = "AlphaTest"
  "IgnoreProjector" = "True"
  "RenderType"  = "TransparentCutout"
  }

  LOD 200

  CGPROGRAM
  #pragma surface surf Lambert alphatest:_Cutoff noforwardadd novertexlights nolightmap exclude_path:forward exclude_path:prepass nometa nodirlightmap nodynlightmap

  sampler2D _MainTex;
  sampler2D _Grid;
  fixed4 _Color;

  bool _RenderGrid;
  float _GridAmount;

  struct Input
  {
  float2 uv_MainTex;
  float4 color : COLOR;
  float3 worldNormal;
  float3 worldPos;
  };

  float getGrid(Input IN)
  {
  return (_RenderGrid  &&
  (IN.color.a  < 0.5)  &&
  (IN.worldNormal.y > 0.01)  &&
  !(IN.uv_MainTex.x < 0.0625 && IN.uv_MainTex.y < 0.03125) &&
  !(IN.uv_MainTex.y > 0.03125))  *
  _GridAmount * 0.2;
  }

  float2 getGridUV(float3 worldPos)
  {
  float u = worldPos.x / 3.0;
  float v = worldPos.z / 3.0;

  return float2(u, v);
  }

  void surf(Input IN, inout SurfaceOutput o)
  {
  float gridFactor = getGrid(IN);

  fixed4 c  = tex2D(_MainTex, IN.uv_MainTex) * _Color;
  float gridAlpha  = tex2D(_Grid, getGridUV(IN.worldPos)).a * gridFactor;
  fixed3 gridColor = fixed3(1,1,1);

  float darkeningFactor = 0.0;

  o.Albedo  = (c.rgb * (1.0 - gridAlpha) + gridColor.rgb * gridAlpha) * (1.0 - darkeningFactor);
  o.Alpha  = c.a;
  }

  ENDCG
  }

  Fallback "Transparent/Cutout/VertexLit"
}

bump

No one?

Fog no longer works in deferred that’s why you get fog only on transparent objects that are rendered in forward.
You will need to use a post-effect or code it in your shader.

Thank you Phantomx. It doesn’t solve my problem but at least I know the reasoning behind it now. I guess, I will have to investigate the global fog image effect, then.

Again, thank you.

I also made a pretty cool post-effect for fog. have a look here: Unity Asset Store - The Best Assets for Game Making