
// ------- UNIFORMS (SHADER CONSTS) -------
mat4		hu_mMMatrix;
mat4		hu_mPVMatrix;
vec3		hu_vCameraPos;

vec4		u_vFloatingHighDistanceFunction;

// ------- ATTRIBUTES (VERTEX BUFFER DATA) -------
struct VIn
{
	vec4	position: POSITION;
	vec2	uv: TEXCOORD;	// [building ID, terrain height]
	vec3	uv1: TEXCOORD1;	// building's top center
	vec4	color: COLOR;
};

// ------- INTERPOLATORS (SHADER OUTPUTS) -------
struct PIn
{
    vec4	position: SV_POSITION;
	vec4	color: COLOR;
	float	distToCamera: TEXCOORD;
};

#include "functions.sh"

PIn main(VIn input)
{
	PIn output;

	vec4 vWorldPos = mul(hu_mMMatrix, input.position);
	vec4 vCenterPos = mul(hu_mMMatrix, vec4(input.uv1, 1.0));
	vec3 vDiff = vCenterPos.xyz - hu_vCameraPos;
	float fDistToCamera = length(vDiff);
	float fMaxHeight = GetGrowingValue(u_vFloatingHighDistanceFunction, fDistToCamera);

	float fTerrainHeight = input.uv.y;
	float fHeightNoTerrain = vWorldPos.y - fTerrainHeight;
	if (fHeightNoTerrain > fMaxHeight)
	{
		vWorldPos.y -= fHeightNoTerrain - fMaxHeight;
	}

	output.color = input.color;
	output.distToCamera = fDistToCamera;

	output.position = mul(hu_mPVMatrix, vWorldPos);

	return output;
}
