#include "common.sh"


sampler   normTex : register(s1);


struct VsInput
{
  float2 xz        : POSITION0;
  float  y         : POSITION1;
  float  foam      : TEXCOORD1;
};

struct VsOutput
{
  float4 pos       : POSITION;
  float4 fog       : COLOR0;
  float3 tc        : TEXCOORD0; // foam in Z
};


float4 tcOfs : register(c16);


VsOutput vsh(VsInput I)
{
  VsOutput O;

  float4 pos=float4(I.xz.x, I.y, I.xz.y, 1);

  O.pos=mul(globtm, pos);

  O.tc.xy=pos.xz*tcOfs.xy+tcOfs.w;
  O.tc.z=1-fastexp(I.foam*0.9);

  O.fog=vs_calc_fog_color(pos.xyz);

  return O;
}


float4 psh(VsOutput I) : COLOR
{
  float4 norm=tex2D(normTex, I.tc.xy);
  norm.xyz=norm.xyz*2-1;
  norm.xyz=normalize(norm.xyz);
  float ao=norm.w;

  float b=I.tc.z;

  // light
  float3 light=calc_simple_light(norm.xyz, ao);

  float t=b;
  float3 res=lerp(
    light*0.03,
    float3(t+0.5, t*t*0.5, 0),
    smoothstep(0.05, 0.50, t));

  // fog
  res=lerp(res, I.fog.rgb, I.fog.a);

  return pack_color(res, 1);
}
