#define SPEC_LAB 0
#define SPEC_OLD 1
#define SPECULAR_FORMAT SPEC_LAB // [SPEC_LAB SPEC_OLD]

vec4 FormatSpecularData(vec4 materialInfo) {
    // Grabs specular data from the correct specular map channel, based on the chosen specular format.

    float roughness, reflectance, emission;

    #if SPECULAR_FORMAT == SPEC_LAB

        // Lab PBR
        roughness = materialInfo.x;
        reflectance = materialInfo.y;
        emission = materialInfo.a;

    #elif SPECULAR_FORMAT == SPEC_OLD

        // Old PBR
        roughness = materialInfo.x;
        reflectance = materialInfo.y;
        emission = materialInfo.z;

    #endif

    return vec4(roughness, reflectance, emission, 0.0);
}

void GetSpecularData(vec4 materialInfo, int blockID, out float roughness, out float reflectance, out float emission) {
    if (dot(materialInfo.xyz, materialInfo.xyz) <= 0.0) {
        roughness = 0.99;
        reflectance = 0.02;
        emission = float(isEmissive(blockID));
        return;
    }

    #if SPECULAR_FORMAT == SPEC_LAB

        // Lab PBR
        roughness = clamp(1.0 - materialInfo.x, 1e-4, 1.0);
        reflectance = clamp(materialInfo.y, 0.01, 1.0);
        emission = materialInfo.z <= 254.0 / 255.0 ? materialInfo.z * (255.0 / 254.0) : 0.0;

    #elif SPECULAR_FORMAT == SPEC_OLD

        // Old PBR
        roughness = 1.0 - materialInfo.x;
        reflectance = clamp(materialInfo.y, 0.02, 1.0);
        emission = materialInfo.z;

    #endif
}

void GetMetalIOR(inout float reflectance, out mat2x3 metalIOR) {
	int metalID = int(reflectance * 255.0);

	#if SPECULAR_FORMAT == SPEC_LAB
		
		if(metalID < 230) return; // Skip metal reflectance if the surface isn't a metal.

		if(metalID == 230) metalIOR = mat2x3(2.9114, 2.9497, 2.5845, 3.0893, 2.9318, 2.7670); // Iron
		if(metalID == 231) metalIOR = mat2x3(0.18299, 0.42108, 1.3734, 3.4242, 2.3459, 1.7704); // Gold
		if(metalID == 232) metalIOR = mat2x3(1.3456, 0.96521, 0.61722, 7.4746, 6.3995, 5.3031); // Aluminium
		if(metalID == 233) metalIOR = mat2x3(3.1071, 3.1812, 2.3230, 3.3314, 3.3291, 3.1350); // Chrome
		if(metalID == 234) metalIOR = mat2x3(0.27105, 0.67693, 1.3164, 3.6092, 2.6248, 2.2921); // Copper
		if(metalID == 235) metalIOR = mat2x3(1.9100, 1.8300, 1.4400, 3.5100, 3.4000, 3.1800); // Lead
		if(metalID == 236) metalIOR = mat2x3(2.3757, 2.0847, 1.8453, 4.2655, 3.7153, 3.1365); // Platinum
		if(metalID == 237) metalIOR = mat2x3(0.15943, 0.14512, 0.13547, 3.9291, 3.1900, 2.3808); // Silver

		reflectance = metalIOR[0].x > 0.0 ? 0.91 : reflectance;

	#elif SPECULAR_FORMAT == SPEC_OLD

	#endif
}
