varying vec2 lmcoord;
varying vec2 texcoord;
varying vec3 N;


varying vec4 glcolor;

varying mat3 tbnMatrix;

varying vec3 sunColor, skyColor, lightColor, baseLight;

#include "/lib/uniform.glsl"
#include "/lib/settings.glsl"
#include "/lib/common/utils.glsl"
#include "/lib/camera/colorToolkit.glsl"
#include "/lib/common/normal.glsl"
#include "/lib/lighting/lightmap.glsl"

#ifdef FSH

flat in float entityIdMap;

void main() {
	vec2 parallaxUV = texcoord;

	vec2 texGradX = dFdx(texcoord);
	vec2 texGradY = dFdy(texcoord);

	vec4 texColor = texture(tex, texcoord);

	vec4 color = texColor * glcolor;
	bool lightning = entityIdMap == LIGHTNING_BOLT || entityIdMap == FIREWORK_ROCKET;
	if(entityIdMap == LIGHTNING_BOLT) {
		color = vec4(vec3(0.0), 1.0);
	}
	
	if (color.a <= 0.0005) discard;
	color.rgb = mix(color.rgb, entityColor.rgb, entityColor.a);
	
	
	float parallaxShadow = 1.0;

	vec3 normalTex = normalize(tbnMatrix * (textureGrad(normals, parallaxUV, texGradX, texGradY).rgb * 2.0 - 1.0));
	vec4 specularTex = texture(specular, texcoord);

	vec2 lightmapCoord = lmcoord;
	if(lightning){
		specularTex = vec4(0.0, 0.0, 0.0, 253.0 / 255.0);
		normalTex = normalize(upPosition);
		lightmapCoord = vec2(0.0, 1.0);
	}

	vec4 passD = texelFetch(gaux4, passUV, 0);
	if(passD.r > 0.5){
		vec4 texColor = toLinearR(color);
		vec2 lightmap = AdjustLightmap(lmcoord);
		color.rgb = texColor.rgb * lightmap.x * 0.4 * lightColor;

		vec3 ambLight = (sunColor + skyColor) * saturate(lightmap.y) + baseLight;
		ambLight *= texColor.rgb;
		color.rgb += ambLight;

		color.rgb += nightVision * texColor.rgb * NIGHT_VISION_BRIGHTNESS / PI;
	}

/* RENDERTARGETS: 0,4,5,9,15 */

	gl_FragData[0] = vec4(color.rgb, color.a);
	gl_FragData[1] = vec4(pack2x8To16(1.0, 0.0), pack2x8To16(entityIdMap / ID_SCALE, 0.0), pack4x8To2x16(specularTex));
	gl_FragData[2] = vec4(normalEncode(normalTex), lightmapCoord);
	gl_FragData[3] = vec4(0.0, 0.0, normalEncode(N));
	gl_FragData[4] = vec4(1.0, 0.0, 0.0, 1.0);
}

#endif
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////BY ZYPanDa/////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#ifdef VSH
flat out float entityIdMap;

attribute vec4 mc_Entity;
attribute vec4 mc_midTexCoord;
attribute vec4 at_tangent;
attribute vec3 at_velocity;
attribute vec4 at_midBlock;

#include "/lib/common/materialIdMapper.glsl"
#include "/lib/common/noise.glsl"

void main() {
	entityIdMap = IDMappingEntity();
	gl_Position = ftransform();

	vec2 jitter = Halton_2_3[framemod8];	//-1 to 1
	jitter *= invViewSize;
	gl_Position.xyz /= gl_Position.w;
    gl_Position.xy += jitter * TAA_JITTER_AMOUNT;
    gl_Position.xyz *= gl_Position.w;

	// TBN Mat 参考自 BSL shader
	N = normalize(gl_NormalMatrix * gl_Normal);
	vec3 B = normalize(gl_NormalMatrix * cross(at_tangent.xyz, gl_Normal.xyz) * at_tangent.w);
	vec3 T = normalize(gl_NormalMatrix * at_tangent.xyz);
	tbnMatrix = mat3(T, B, N);
	// T_tbnMatrix = transpose(tbnMatrix);

	lmcoord  = (gl_TextureMatrix[1] * gl_MultiTexCoord1).xy;
	#if defined PATH_TRACING || defined COLORED_LIGHT
		lmcoord.x = ((at_midBlock.a - 1.0)) / 15.0;
	#endif
	texcoord = (gl_TextureMatrix[0] * gl_MultiTexCoord0).xy;
	glcolor = gl_Color;

	sunColor = texelFetch(gaux4, sunColorUV, 0).rgb * 0.4;
	skyColor = texelFetch(gaux4, skyColorUV, 0).rgb;
	#ifdef PATH_TRACING
		baseLight = vec3(DEFERRED10_PT_ALBEDO_SCALE);
	#else
		baseLight = vec3(DEFERRED10_ALBEDO_SCALE);
	#endif

	#ifdef CLOUD_SHADOW
		sunColor *= 1.0 - 0.5 * rainStrength;
		skyColor *= 1.0 - 0.5 * rainStrength;
	#endif

	lightColor = artificial_color;
	#ifdef END
		sunColor = mix(vec3(1.0), endColor, 0.8) * 0.5;
		skyColor = endColor * 0.5;
		lightColor *= 2.5;
		baseLight = vec3(0.125);
	#endif
	#ifdef NETHER
		skyColor = vec3(0.0);
		sunColor = vec3(0.0);
		lightColor = mix(vec3(1.0), netherColor, 0.5) * 0.5;
		baseLight = vec3(0.025);
	#endif
	#ifdef LIGHTNING
		sunColor = vec3(0.0);
		skyColor = vec3(0.0);
		lightColor = vec3(10.0);
	#endif
}

#endif
