#extension GL_OES_standard_derivatives : enable

uniform vec2 u_texsize;
uniform float u_fade;

uniform sampler2D u_image_0;
uniform sampler2D u_image_1;
uniform sampler2D u_image_2;
uniform sampler2D u_image_3;
uniform sampler2D u_image_4;
uniform sampler2D u_image_5;
uniform float u_height_factor;
uniform vec4 u_roofcolor;

varying vec2 v_pos_a;
varying vec2 v_pos_b;
varying vec2 v_uv;
varying vec4 v_lighting;
varying float v_isRoof;
varying vec4 v_pos;
varying vec4 v_color;
varying float v_lightup;

#pragma mapbox: define lowp float lightup
#pragma mapbox: define lowp float base
#pragma mapbox: define lowp float height
#pragma mapbox: define lowp vec4 pattern_from
#pragma mapbox: define lowp vec4 pattern_to
#pragma mapbox: define lowp float pixel_ratio_from
#pragma mapbox: define lowp float pixel_ratio_to
#pragma mapbox: define highp vec4 color

void main() {
    #pragma mapbox: initialize lowp float lightup
    #pragma mapbox: initialize lowp float base
    #pragma mapbox: initialize lowp float height
    #pragma mapbox: initialize mediump vec4 pattern_from
    #pragma mapbox: initialize mediump vec4 pattern_to
    #pragma mapbox: initialize lowp float pixel_ratio_from
    #pragma mapbox: initialize lowp float pixel_ratio_to
    #pragma mapbox: initialize highp vec4 color

    float u = fract(v_uv.x);
    vec2 uv = vec2(u, v_uv.y);

    vec4 out_color;
    vec4 side_color;
    bool is_auto_roofcolor = u_roofcolor.b == 0.996078431372549;
    vec4 auto_roofcolor;

    if (height > 151.5) {
        auto_roofcolor = texture2D(u_image_5, vec2(0.3, 0.3));
        side_color = texture2D(u_image_5, vec2(uv.x * 0.1, uv.y));
    } else if (height > 105.5) {
        auto_roofcolor = texture2D(u_image_4, vec2(0.3, 0.3));
        side_color = texture2D(u_image_4, vec2(uv.x * 0.1, uv.y));
    } else if (height > 101.1) {
        auto_roofcolor = texture2D(u_image_3, vec2(0.3, 0.3));
        side_color = texture2D(u_image_3, vec2(uv.x * 0.1, uv.y));
    } else if (height > 71.1) {
        auto_roofcolor = texture2D(u_image_2, vec2(0.3, 0.3));
        side_color = texture2D(u_image_2, uv);
    } else if (height > 51.2) {
        auto_roofcolor = texture2D(u_image_1, vec2(0.3, 0.3));
        side_color = texture2D(u_image_1, vec2(uv.x * 0.5, uv.y));
    } else {
        auto_roofcolor = texture2D(u_image_0, vec2(0.3, 0.3));
        side_color = texture2D(u_image_0, uv);
    }
    out_color = v_isRoof >= 0.5 ? (is_auto_roofcolor ? auto_roofcolor : u_roofcolor) : side_color;
    out_color = out_color * v_lighting;
    
    if (v_lightup >= 0.5) {
        // 高亮着色
    } else {
        
    }
#ifdef FOG
    out_color = fog_dither(fog_apply_premultiplied(out_color, v_fog_pos));
#endif

    gl_FragColor = out_color;

#ifdef OVERDRAW_INSPECTOR
    gl_FragColor = vec4(1.0);
#endif
}
