gboisse / gfx

A minimalist and easy to use graphics API.
MIT License
498 stars 35 forks source link

Requesting Non-PBR material support #8

Closed sayan1an closed 2 years ago

sayan1an commented 2 years ago

Is it possible to extend materials to have kd, ks and shininess loaded from .mtl files? Tinyobjloader already loads these properties, just need support from gfx.

Alternatively, an approximate conversion from non-pbr to pbr would also help. e.g. metallicity = ks / (ks + kd) and roughness = sqrt(sqrt(2 / (2 + shininess))).

gboisse commented 2 years ago

This is something that should be possible indeed and sounds like a good idea, but I most likely won't have the time to look into it anytime soon. PRs are welcome though 🙂

sayan1an commented 2 years ago

I added the following lines in the function importObj() right after material textures are loaded. It works for me, and does so without interfering with the current functionality. It's not well tested, so, unsure whether I should generate a PR! :)

float diffuse_magnitude = sqrt(obj_material.diffuse[0] * obj_material.diffuse[0] + obj_material.diffuse[1] * obj_material.diffuse[1] + obj_material.diffuse[2] * obj_material.diffuse[2]);
            float specular_magnitude = sqrt(obj_material.specular[0] * obj_material.specular[0] + obj_material.specular[1] * obj_material.specular[1] + obj_material.specular[2] * obj_material.specular[2]);
            if(specular_magnitude > 1e-4f && material_ref->metallicity == 0.0f) material_ref->metallicity = specular_magnitude / (specular_magnitude + diffuse_magnitude);
            if(obj_material.shininess > 0 && material_ref->roughness == 0.0f) material_ref->roughness = sqrt(2 / (2 + obj_material.shininess));
gboisse commented 2 years ago

Happy you found a solution that works for you. For a PR, I think it'd be good to also process the texture data on load using that same conversion but at the texel level. Closing for now.