#include "common.sh"

struct VsInput
{
  float4 pos       : POSITION;
};

struct VsOutput
{
  float4 pos       : POSITION;
  float3 dir       : TEXCOORD0;
};


VsOutput vsh(VsInput I)
{
  VsOutput O;

  O.pos=mul(globtm, I.pos);
  O.dir=I.pos.xyz-view_pos.xyz;

  return O;
}


float4 psh(VsOutput I) : COLOR
{
  float3 res;

  float3 vdir=normalize(I.dir);

  float mu=(vdir.y-horizon_cos)/(1-horizon_cos);

  float vs=dot(vdir, sun_dir.xyz);

  float vsun=vs;
  float vsd=min(vsun+(1-sun_dir.w), 1);

  float hf=smoothstep(-0.005, 0, mu);

  res=calc_sky_sun_color(mu, vs, vsd, hf)*lerp(planet_k, 1, hf);

  res=lerp(res, sunD, smoothstep(1-(1-sun_dir.w)*1.02, sun_dir.w, vsun)*hf);

  return pack_color(res, 0);
}
