float4 ambientColor  : register (c0);
float4 diffuseColor  : register (c1);
float4 worldLight    : register (c2);
float4 specularColor : register (c3); //power in c3.a

struct PS_INPUT
{
   float4 Color  : COLOR0;
#ifdef OMNISCALE
   float4 Omni   : COLOR1;
#endif
   float  Fog    : FOG;

   float2 Tex0    : TEXCOORD0; // base0
   float3 EyeVec  : TEXCOORD1; // world space eye vec
};

struct PS_OUTPUT
{
   float4 Color : COLOR0;
};

sampler2D sTex0: register (s0);
 
PS_OUTPUT SRPL_L25_VEGETATION01_T0 (PS_INPUT Input)
{
  PS_OUTPUT Out;
  
  float4 tex = tex2D (sTex0, Input.Tex0.xy);        // sample base tex

  if (tex.a < 0.5)
  {
    discard;
  }

  float alpha1 = tex2D (sTex0, Input.Tex0.xy + float2(-0.025, -0.025)).a;
  float alpha2 = tex2D (sTex0, Input.Tex0.xy + float2( 0.025, -0.025)).a;
  float alpha3 = tex2D (sTex0, Input.Tex0.xy + float2(-0.025,  0.025)).a;
  float alpha4 = tex2D (sTex0, Input.Tex0.xy + float2( 0.025,  0.025)).a;

  float3 normEyeVec = normalize (Input.EyeVec.xyz);
  
  float3 lightVec = normalize(worldLight); 

  float dotLightEye = dot(lightVec.xz, normEyeVec.xz);

  float lightThrough = pow(max(0.0, dotLightEye), 3.0);
  float lightInFront = 0.68 - dot(lightVec.xz, normEyeVec.xz) * 0.32;

//  lightThrough *= Input.Params.x;
  
  lightThrough *= min(0.4, (5.0 - tex.a - alpha1 - alpha2 - alpha3 - alpha4) / 5.0);

  float day = diffuseColor.r;
  lightThrough *= day;


  // compute diffuse color
//  half4 diffuse = diffuseColor + ambientColor;
  half4 diffuse = diffuseColor * 1.2 + ambientColor * 0.8;

//#ifdef OMNISCALE
//  diffuse = diffuse + Input.Omni * OMNISCALE;
//#endif
//
//  Out.Color = (tex * diffuse) * Input.Color;

  tex.rgb = lerp(tex.rgb * diffuse.rgb * lightInFront, float3(1.5, 1.8, 0.5) * tex.rgb, lightThrough);  //subsurface scattering

  Out.Color.rgba = tex.rgba;

  return (Out);
}
