

layout(location = 0) out vec4 color0;
layout(location = 1) out vec4 color1;
layout(location = 2) out vec4 color2;
layout(location = 3) out vec4 color3;

vec2 voxy_OctWrap(vec2 v) {
    return (1.0 - abs(v.yx)) * (step(vec2(0.0), v.xy) * 2.0 - 1.0);
}
vec2 voxy_normalEncode(vec3 n) {
    n /= (abs(n.x) + abs(n.y) + abs(n.z));
    n.xy = n.z >= 0.0 ? n.xy : voxy_OctWrap(n.xy);
    n.xy = n.xy * 0.5 + 0.5;
    return n.xy;
}

void voxy_emitFragment(VoxyFragmentParameters parameters) {
    vec3 normal = vec3(
        uint((parameters.face>>1)==2),
        uint((parameters.face>>1)==0),
        uint((parameters.face>>1)==1)
    ) * (float(int(parameters.face)&1)*2-1);

    vec3 albedo = parameters.sampledColour.rgb * parameters.tinting.rgb;
    vec2 lm = clamp(parameters.lightMap, 0.5/16.0, 15.5/16.0);

    color0 = vec4(albedo, 1.0);
    color1 = vec4(lm, 8888.0 / 10000.0, 1.0);
    color2 = vec4(voxy_normalEncode(normal), voxy_normalEncode(normal));
    color3 = vec4(0.0, 0.0, 0.0, 1.0);
}