float4 main(float2 texCoord: TEXCOORD0,
            float4 viewSpacePosition: TEXCOORD1,
            float4 clipSpacePosition: TEXCOORD2,

            uniform float4 fog,
            uniform sampler2D textureMap: TEXUNIT0,
            uniform sampler2D fogTexture: TEXUNIT1) : COLOR
{
  float3 textureColor = tex2D(textureMap, texCoord).rgb;
  float2 fogUV = float2(1.0/clipSpacePosition.w * ((clipSpacePosition.x + clipSpacePosition.w) * 0.5),
                        1.0/clipSpacePosition.w * ((clipSpacePosition.y + clipSpacePosition.w) * 0.5));
  float3 fogColor = tex2D(fogTexture, fogUV).rgb;
  float depth = -viewSpacePosition.z;
  float fogNear = fog.x;
  float fogFar = fog.y;
  float alpha = clamp((depth - fogNear) / (fogFar - fogNear), 0, 0.75);
  float3 color = lerp(textureColor, fogColor, alpha);
  return float4(color, 1.0);
}
