precision highp float;

// #typescript options?.clipPlaneUniforms()

uniform sampler2D diffuseSampler;
uniform sampler2D propertiesSampler;
uniform vec3 size;
uniform float packSize;
uniform vec3 highlightColors[3];

// #include DitherUniforms.glsl

// #include HSVMapping.glsl

in float vColorPixelIndex;
#ifdef requiresWorldFragment
    in vec3 vPositionW;
#endif

void main(void) {

    ivec2 textureSize = textureSize(diffuseSampler, 0);
    // #include ColorPickingIndex.glsl
    // #include PropertiesPixel.glsl

    vec4 diffuseColor = texelFetch(diffuseSampler, ivec2(pixelIndex), 0);

    if (diffuseColor.a == 0.0) {
        discard;
    }

    #ifdef requiresWorldFragment
        vec3 worldSpaceVertex = vPositionW;
        // #typescript options?.clipPlaneDiscarding()
    #endif

    float opaqueness = diffuseColor.a;

    vec3 finalDiffuse = diffuseColor.rgb;
    
    #ifdef requiresWorldFragment
        // #typescript options?.clipPlaneColorBlending()
    #endif

    // #include HSVColorChanger.glsl
    
    if (!visible) {
        discard;
    }

    if (highlighted) {
        finalDiffuse = highlightColors[highlightIndex - 1];
    }

    
    #ifdef dithering
        float distanceImpact = 0.0;
        // #include DitherDiscard.glsl
        const float DITHERING_COLOR_COEFFICIENT = 2.0;
        // Also make the color a bit brigther since there are fewer lit fragments
        gl_FragColor = vec4(finalDiffuse * DITHERING_COLOR_COEFFICIENT, 1.0); // Alpha does not matter here as blending is disabled when dithering is enabled
    #else
        gl_FragColor = vec4(finalDiffuse, opaqueness);
    #endif
}