#define EARTH
varying vec3 vViewPosition;
varying vec3 vLightDirection;
varying vec2 vUv;
varying vec3 vEyeNormal;

uniform vec3 diffuse;
uniform vec3 emissive;
uniform vec3 specular;
uniform float shininess;
uniform float opacity;

uniform sampler2D texture0;
uniform sampler2D texture1;
uniform sampler2D specularMap;

#include <common>
#include <packing>
#include <normal_pars_fragment>
#include <normalmap_pars_fragment>

varying vec3 c0;
varying vec3 c1;

void main() {
	vec4 sampledDiffuseColor = texture2D(texture0, vUv);
	vec4 sampledDiffuseColorNight = texture2D(texture1, vUv);

	vec3 N = normalize(vEyeNormal);
	vec3 L = normalize(vLightDirection);
	vec3 V = normalize(vViewPosition);
	vec3 H = normalize(V + L);

	float diffuseAngle = dot(N, L);
	float specularAngle = dot(N, H);

	float cosineAngleSunToNormal = clamp( diffuseAngle * 10.0, -1.0, 1.0);
	float mixAmount = cosineAngleSunToNormal * 0.5 + 0.5;

	vec4 texelSpecular = texture2D(specularMap, vUv);
	float specularStrength = texelSpecular.r;

	vec3 outgoingLight = mix(sampledDiffuseColorNight.xyz, c0 + sampledDiffuseColor.xyz * c1, mixAmount);

  float specularAmount = max(0.0, specularAngle);
	specularAmount = pow(specularAmount, 32.0 * shininess) * specularStrength;

	gl_FragColor = vec4( (outgoingLight + specular * specularAmount), 1.0 );
}
