#version 150

#define MINECRAFT_LIGHT_POWER   (0.6)
#define MINECRAFT_AMBIENT_LIGHT (0.4)

vec4 minecraft_mix_light(vec3 lightDir0, vec3 lightDir1, vec3 normal, vec4 color) {
    float light0 = max(0.0, dot(lightDir0, normal));
    float light1 = max(0.0, dot(lightDir1, normal));
    float lightAccum = min(1.0, (light0 + light1) * MINECRAFT_LIGHT_POWER + MINECRAFT_AMBIENT_LIGHT);
    return vec4(color.rgb * lightAccum, color.a) * vec4(1,1,1,1);
}

vec4 minecraft_sample_lightmap(sampler2D lightMap, ivec2 uv) {
	float sky_light = uv.y / 16.0;
	float block_light = uv.x / 16.0;
	vec4 clean_sky_light = texture(lightMap, clamp(vec2(0.0, 240.0) / 256.0, vec2(0.5 / 16.0), vec2(15.5 / 16.0)));
	float daytime = 1.0 - (smoothstep(7.0, 15.0, sky_light) * smoothstep(0.164, 0.976, clean_sky_light.r));
	float brightness_modifier = ((block_light * daytime) + 32.0) / 32.0;
	vec3 cold_tint = vec3(-0.3, 0.0, 1.0) * daytime;
	vec3 light_modifier = vec3(brightness_modifier) + (cold_tint * smoothstep(0.0, 7.0, block_light));
	vec4 light_color = texture(lightMap, clamp(uv / 256.0, vec2(0.5 / 16.0), vec2(15.5 / 16.0)));

	if (block_light > 0.0) return light_color * vec4(light_modifier, 1.0);
	else return light_color;
}
