#version 320 es
precision highp float;
precision highp int;
precision highp sampler2D;
precision highp sampler2DArray;

#include "gamma.sdr"

layout (binding = 0) in vec4 fragTexCoord;
layout (binding = 1) in vec4 fragColor;

layout (location = 0) out vec4 fragOut0;

layout (std140) uniform genericData {
    mat4  modelMatrix;
    vec4  color;
    vec4  clipEquation;
    int   baseMapIndex;
    int   alphaTexture;
    int   noTexturing;
    int   srgb;
    float intensity;
    float alphaThreshold;
    uint  clipEnabled;
};

in float vClipDist;

layout(binding = 2) uniform sampler2DArray baseMap;

void main()
{
	if (clipEnabled != uint(0) && vClipDist < 0.0) {
		discard;
	}

	vec4 baseColor = texture(baseMap, vec3(fragTexCoord.xy, float(baseMapIndex)));
	if(alphaThreshold > baseColor.a) discard;
	baseColor.rgb = (srgb == 1) ? srgb_to_linear(baseColor.rgb) : baseColor.rgb;
	vec4 blendColor = (srgb == 1) ? vec4(srgb_to_linear(fragColor.rgb), fragColor.a) : fragColor;
	fragOut0 = mix(mix(baseColor * blendColor, vec4(blendColor.rgb, baseColor.r * blendColor.a), float(alphaTexture)), blendColor, float(noTexturing)) * intensity;
}
