#ifdef FRAGMENT_SHADER
    varying vec2 TexCoords;
    varying vec3 Normal;
    varying vec4 Color;

    varying vec2 LightmapCoords;

    uniform sampler2D lightmap;
    uniform sampler2D texture;

    uniform int heldItemId;

    uniform float near;
    uniform float far;

    #ifdef DISTANT_HORIZONS
        uniform float dhFarPlane;
    #else
        float dhFarPlane = far;
    #endif

    uniform int viewWidth;
    uniform int viewHeight;

    uniform vec3 cameraPosition;

    #include "/lib/globalDefines.glsl"

    #include "/lib/includes2.glsl"
    #include "/lib/optimizationFunctions.glsl"

    mediump float AdjustLightmapTorch(in float torch) {
        const mediump float K = 2.0f;
        const mediump float P = 5.06f;
        return K * pow2(torch, P);
    }

    mediump float AdjustLightmapSky(in float sky){
        mediump float sky_2 = sky * sky;
        return sky_2 * sky_2;
    }

    vec2 AdjustLightmap(in vec2 Lightmap){
        vec2 NewLightMap;
        NewLightMap.x = AdjustLightmapTorch(Lightmap.x);
        NewLightMap.y = AdjustLightmapSky(Lightmap.y);
        return NewLightMap;
    }

    vec4 vanillaLight(in vec2 Lightmap) {
        const vec3 TorchColor = vec3(1.0f, 1.0f, 1.0f);
        vec4 lightColor = vec4(TorchColor * Lightmap.x,1.0);
        return lightColor;
    }

    /* RENDERTARGETS:0,1,2,6,5,13 */

    void main() {
        vec4 albedo = texture2D(texture, TexCoords) * Color;

        mediump float distanceFromCamera = distance(vec3(0), viewSpaceFragPosition);
        
        gl_FragData[0] = albedo;
        gl_FragData[1] = vec4((mat3(gbufferModelViewInverse) * Normal) * 0.5 + 0.5f, albedo.a);
        #if LIGHTING_MODE == 1
            vec4 vanilla = vanillaLight(AdjustLightmap(LightmapCoords));
            vec4 lighting = mix2( pow2(vanilla * 0.5f,vec4(0.25f)),vec4(vec3(0.0),1.0),clamp(length(max(vec3(1.0) - vanilla.xyz,vec3(0.0))),0,1));
            gl_FragData[2] = vec4(lighting.xyz, albedo.a);
        #else
            gl_FragData[2] = vec4(LightmapCoords, 0.0f, albedo.a);
        #endif
        #ifdef DISTANT_HORIZONS
            gl_FragData[3] = vec4(0.0,encodeDist(distanceFromCamera, dhFarPlane),0.0,albedo.a);
        #else
            gl_FragData[3] = vec4(0.0,encodeDist(distanceFromCamera, far),0.0,albedo.a);
        #endif
        gl_FragData[4] = vec4(0.0,1.0,1.0,albedo.a);
        gl_FragData[5] = vec4(LightmapCoords, 0.0, albedo.a);
    }
#endif

#ifdef VERTEX_SHADER
    varying vec2 TexCoords;
    varying vec3 Normal;
    varying vec4 Color;

    varying vec2 LightmapCoords;

    out vec3 viewSpaceFragPosition;

    void main() {
        gl_Position = ftransform();

        LightmapCoords = mat2(gl_TextureMatrix[1]) * gl_MultiTexCoord1.st;

        viewSpaceFragPosition = (gl_ModelViewMatrix * gl_Vertex).xyz;

        TexCoords = gl_MultiTexCoord0.st;
        Normal = gl_NormalMatrix * gl_Normal;
        Color = gl_Color;
    }
#endif