layout( std140 ) uniform view {
	mat4 V;
	mat4 P;
	vec3 camera_pos;
};

layout( std140 ) uniform sun {
	vec3 sun_dir;
	float sun_angle;
};

layout( std140 ) uniform clipmap_skirt {
	float scale;
};

struct VSOut {
	vec4 view_position;
};

uniform sampler2D blue_noise;

#ifdef VERTEX_SHADER

in vec3 position;
out VSOut v2f;

void main() {
	vec2 xy = position.xy + camera_pos.xy;
	vec2 snapped_xy = floor( xy / scale ) * scale;
	v2f.view_position = V * vec4( snapped_xy, 0.0, 1.0 );
	gl_Position = P * v2f.view_position;
}

#else

in VSOut v2f;
out vec4 screen_colour;

void main() {
	vec3 normal = vec3( 0.0, 0.0, 1.0 );
	vec3 ground = vec3( 0.0, 0.25, 1.0 );

	float sunlight_lambert = max( 0, dot( normal, sun_dir ) );
	vec3 sunlight = sunlight_lambert * vec3( 0.9, 0.9, 0.5 );
	vec3 ambient = vec3( 0.05, 0.05, 0.15 );

	vec3 c = ( sunlight + ambient ) * ground;

	screen_colour = vec4( linear_to_srgb( apply_fog( c + get_dither_noise( blue_noise ), length( v2f.view_position ) ) ), 1.0 );
}

#endif
