// __multiversion__
// This signals the loading code to prepend either #version 100 or #version 300 es as apropriate.
precision highp float;

#include "vertexVersionCentroidUV.h"

#include "uniformPerFrameConstants.h"
#include "uniformWorldConstants.h"
#include "uniformEntityConstants.h"
#include "uniformWeatherConstants.h"
#ifdef USE_SKINNING
#include "uniformAnimationConstants.h"
#endif

attribute mediump vec4 POSITION;
attribute vec2 TEXCOORD_0;
attribute vec4 NORMAL;
#if defined(USE_SKINNING)
#ifdef MCPE_PLATFORM_NX
attribute uint BONEID_0;
#else
attribute float BONEID_0;
#endif
#endif

varying vec4 light;
varying vec4 fogColor;

const float AMBIENT = 0.45;

const float XFAC = -0.1;
const float ZFAC = 0.1;

float lightIntensity(vec4 position, vec4 normal) {
#ifdef FANCY
#if defined(NETEASE_SKINNING) && defined(USE_SKINNING)
	MAT4 boneMat = GetBoneMatForNetease(int(BONEID_0));
    vec3 N = normalize( boneMat * NORMAL ).xyz;
#else//NETEASE_SKINNING
	vec3 N = normalize( WORLD * normal ).xyz;
#endif
	N.y *= TILE_LIGHT_COLOR.w; //TILE_LIGHT_COLOR.w contains the direction of the light

	//take care of double sided polygons on materials without culling
#ifdef FLIP_BACKFACES
#if defined(NETEASE_SKINNING) && defined(USE_SKINNING)
	vec3 viewDir = normalize( boneMat * POSITION ).xyz;
#else //NETEASE_SKINNING
	vec3 viewDir = normalize((WORLD * position).xyz);
#endif
	if( dot(N, viewDir) > 0.0 )
		N *= -1.0;
#endif

	float yLight = (1.0+N.y) * 0.5;
	return yLight * (1.0-AMBIENT) + N.x*N.x * XFAC + N.z*N.z * ZFAC + AMBIENT;

#else
	return 1.0;
#endif
}

varying vec3 viewVS;
varying vec3 normalVS;
varying vec3 positionOS;

void main()
{
	vec4 entitySpacePosition;
	vec4 entitySpaceNormal;

#ifdef USE_SKINNING
#ifdef NETEASE_SKINNING
		MAT4 boneMat = GetBoneMatForNetease(int(BONEID_0));
		entitySpacePosition = boneMat * POSITION;
		entitySpaceNormal = boneMat * NORMAL;
#else
	#if defined(LARGE_VERTEX_SHADER_UNIFORMS)
		entitySpacePosition = BONES[int(BONEID_0)] * POSITION;
		entitySpaceNormal = BONES[int(BONEID_0)] * NORMAL;
	#else
		entitySpacePosition = BONE * POSITION;
		entitySpaceNormal = BONE * NORMAL;
	#endif
#endif
#else
	entitySpacePosition = POSITION * vec4(1, 1, 1, 1);
	entitySpaceNormal = NORMAL * vec4(1, 1, 1, 0);
#endif

	float L = lightIntensity(entitySpacePosition, entitySpaceNormal);

	light = vec4(vec3(L) * TILE_LIGHT_COLOR.xyz, 1.0);
	uv = TEXCOORD_0;
	vec4 pos = WORLDVIEWPROJ * entitySpacePosition;
	gl_Position = pos;

	positionOS = entitySpacePosition.xyz;
	normalVS = normalize((WORLDVIEW * entitySpaceNormal).xyz);
	viewVS = normalize((WORLDVIEW * entitySpacePosition).xyz);

	//fog
	fogColor.rgb = FOG_COLOR.rgb;
	fogColor.a = clamp(((pos.z / RENDER_DISTANCE) - FOG_CONTROL.x) / (FOG_CONTROL.y - FOG_CONTROL.x), 0.0, 1.0);
}
