precision highp float;
varying highp vec2 textureCoordinate;
uniform sampler2D inputImageTexture;
uniform sampler2D inputImageTexture2;
uniform sampler2D inputImageTexture3;
uniform int type;
uniform float adjustAlpha;

void useLut() {
    vec4 textureColor = texture2D(inputImageTexture, textureCoordinate);
    float blueColor = textureColor.b * 63.0;
    vec2 quad1; quad1.y = floor(floor(blueColor) / 8.0);
    quad1.x = floor(blueColor) - (quad1.y * 8.0);
    vec2 quad2;
    quad2.y = floor(ceil(blueColor) / 8.0);
    quad2.x = ceil(blueColor) - (quad2.y * 8.0);
    vec2 texPos1;
    texPos1.x = (quad1.x * 0.125) + 0.5/512.0 + ((0.125 - 1.0/512.0) * textureColor.r);
    texPos1.y = (quad1.y * 0.125) + 0.5/512.0 + ((0.125 - 1.0/512.0) * textureColor.g);
    vec2 texPos2;
    texPos2.x = (quad2.x * 0.125) + 0.5/512.0 + ((0.125 - 1.0/512.0) * textureColor.r);
    texPos2.y = (quad2.y * 0.125) + 0.5/512.0 + ((0.125 - 1.0/512.0) * textureColor.g);
    vec4 newColor1 = texture2D(inputImageTexture2, texPos1);
    vec4 newColor2 = texture2D(inputImageTexture2, texPos2);
    vec4 newColor = mix(newColor1, newColor2, fract(blueColor));
    float alpha = textureColor.a * adjustAlpha;
    newColor = mix(textureColor, newColor, alpha);
    gl_FragColor = vec4(newColor.rgb, 1.0);
    //gl_FragColor = vec4(textureColor.rgb, 1.0);
}

void main() {
    if(type == 0) {
        vec4 textureColor = texture2D(inputImageTexture, textureCoordinate);
        gl_FragColor = vec4(textureColor.r, textureColor.g, textureColor.b, 1.0);
    } else if(type == 1) {
        useLut();
    } else if(type == 2) {
        vec4 textureColor = texture2D(inputImageTexture, textureCoordinate);
        float alpha = textureColor.a;
        vec4 textureColor2 = texture2D(inputImageTexture3, textureCoordinate);
        gl_FragColor = textureColor2;
    }
}