From bddb5df0d407d1eb50a1e3974ac0d0d1a9c15d37 Mon Sep 17 00:00:00 2001 From: Nikolas Date: Sun, 2 Feb 2025 12:48:18 +0100 Subject: [PATCH] [rmodels] Separate GLTF roughness and metallic channels on load (#4739) --- src/rmodels.c | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/src/rmodels.c b/src/rmodels.c index 9ab2bcca9725..d7bc05f717e8 100644 --- a/src/rmodels.c +++ b/src/rmodels.c @@ -5347,7 +5347,33 @@ static Model LoadGLTF(const char *fileName) Image imMetallicRoughness = LoadImageFromCgltfImage(data->materials[i].pbr_metallic_roughness.metallic_roughness_texture.texture->image, texPath); if (imMetallicRoughness.data != NULL) { - model.materials[j].maps[MATERIAL_MAP_ROUGHNESS].texture = LoadTextureFromImage(imMetallicRoughness); + Image imMetallic, imRoughness; + + imMetallic.data = RL_MALLOC(imMetallicRoughness.width * imMetallicRoughness.height); + imRoughness.data = RL_MALLOC(imMetallicRoughness.width * imMetallicRoughness.height); + + imMetallic.width = imRoughness.width = imMetallicRoughness.width; + imMetallic.height = imRoughness.height = imMetallicRoughness.height; + + imMetallic.format = imRoughness.format = PIXELFORMAT_UNCOMPRESSED_GRAYSCALE; + imMetallic.mipmaps = imRoughness.mipmaps = 1; + + for (int x = 0; x < imRoughness.width; x++) { + for (int y = 0; y < imRoughness.height; y++) { + Color color = GetImageColor(imMetallicRoughness, x, y); + + Color roughness = (Color) {color.g}; + Color metallic = (Color) {color.b}; + + ((unsigned char *)imRoughness.data)[y*imRoughness.width + x] = color.g; + ((unsigned char *)imMetallic.data)[y*imMetallic.width + x] = color.b; + } + } + model.materials[j].maps[MATERIAL_MAP_ROUGHNESS].texture = LoadTextureFromImage(imRoughness); + model.materials[j].maps[MATERIAL_MAP_METALNESS].texture = LoadTextureFromImage(imMetallic); + + UnloadImage(imRoughness); + UnloadImage(imMetallic); UnloadImage(imMetallicRoughness); }