#if !defined INCLUDE_MATERIAL
#define INCLUDE_MATERIAL

struct Material {
	vec3  albedo;       // Diffuse albedo.
	float roughness;    // Linear GGX roughness.
	float porosity;     // Currently not used.
	vec3  reflectance;  // Fresnel reflectance at normal incidence.
	// TODO: Add K to support LAB's predefined metals.
	vec3  emission;     // Light emitted at surface. Currently not used.
	vec3  translucency; // Currently not used.
};

Material DecodeMaterial(vec3 baseTex, vec4 specTex) {
	baseTex = SrgbToLinear(baseTex);

	Material material;

	bool isMetal = specTex.g > (229.5 / 255.0);
	bool isPorous = specTex.b < (64.5 / 255.0);

	material.albedo       = isMetal ? vec3(0.0) : baseTex;
	material.roughness    = 1.0 - specTex.r; material.roughness *= material.roughness;
	material.porosity     = !isMetal && isPorous ? specTex.b * (255.0 / 64.0) : 0.0;
	material.reflectance  = isMetal ? baseTex : vec3(specTex.g * specTex.g);
	material.emission     = vec3(specTex.a < (254.5 / 255.0) ? specTex.a * (255.0 / 254.0) : 0.0);
	material.translucency = vec3(!isMetal && !isPorous ? specTex.b * (255.0 / 190.0) - (65.0 / 190.0) : 0.0);

	// TODO: Add support for LAB's predefined metals.

	return material;
}

#endif
