From 4bb18ada6181ca868399fdc724bd0a494b5939f7 Mon Sep 17 00:00:00 2001 From: Almaas Rizvi Date: Fri, 10 Jan 2025 22:23:46 +0530 Subject: [PATCH 1/2] added correct usage of mra map textures RMA maps store Roughness as Red channel, Metalness as Green, and Ambient occlusion as Blue. Added functionality to use these maps for accurate pbr materials. --- examples/shaders/shaders_basic_pbr.c | 34 +++++++++++++++++++++------- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/examples/shaders/shaders_basic_pbr.c b/examples/shaders/shaders_basic_pbr.c index d02980afd9b8..895867b3d3e5 100644 --- a/examples/shaders/shaders_basic_pbr.c +++ b/examples/shaders/shaders_basic_pbr.c @@ -22,7 +22,7 @@ #if defined(PLATFORM_DESKTOP) #define GLSL_VERSION 330 #else // PLATFORM_ANDROID, PLATFORM_WEB - #define GLSL_VERSION 100 + #define GLSL_VERSION 120 #endif #include // Required for: NULL @@ -84,7 +84,7 @@ int main() const int screenHeight = 450; SetConfigFlags(FLAG_MSAA_4X_HINT); - InitWindow(screenWidth, screenHeight, "raylib [shaders] example - basic pbr"); + InitWindow(screenWidth, screenHeight, "basic pbr"); // Define the camera to look into our 3d world Camera camera = { 0 }; @@ -143,10 +143,17 @@ int main() car.materials[0].maps[MATERIAL_MAP_ROUGHNESS].value = 0.0f; car.materials[0].maps[MATERIAL_MAP_OCCLUSION].value = 1.0f; car.materials[0].maps[MATERIAL_MAP_EMISSION].color = (Color){ 255, 162, 0, 255 }; - // Setup materials[0].maps default textures car.materials[0].maps[MATERIAL_MAP_ALBEDO].texture = LoadTexture("resources/old_car_d.png"); - car.materials[0].maps[MATERIAL_MAP_METALNESS].texture = LoadTexture("resources/old_car_mra.png"); + + //Load RMA texture and convert it to an image to extract channels + Texture2D carTexture=LoadTexture("resources/old_car_mra.png"); + Image carImage =LoadImageFromTexture(carTexture); + //RMA textures -> R corresponds to Roughness, G to Metalness, and B to Occlusion + //Using different channels of RMA map for different parameters + car.materials[0].maps[MATERIAL_MAP_METALNESS].texture = LoadTextureFromImage(ImageFromChannel(carImage,1)); + car.materials[0].maps[MATERIAL_MAP_ROUGHNESS].texture = LoadTextureFromImage(ImageFromChannel(carImage,0)); + car.materials[0].maps[MATERIAL_MAP_OCCLUSION].texture = LoadTextureFromImage(ImageFromChannel(carImage,2)); car.materials[0].maps[MATERIAL_MAP_NORMAL].texture = LoadTexture("resources/old_car_n.png"); car.materials[0].maps[MATERIAL_MAP_EMISSION].texture = LoadTexture("resources/old_car_e.png"); @@ -165,9 +172,14 @@ int main() floor.materials[0].maps[MATERIAL_MAP_ROUGHNESS].value = 0.0f; floor.materials[0].maps[MATERIAL_MAP_OCCLUSION].value = 1.0f; floor.materials[0].maps[MATERIAL_MAP_EMISSION].color = BLACK; - floor.materials[0].maps[MATERIAL_MAP_ALBEDO].texture = LoadTexture("resources/road_a.png"); - floor.materials[0].maps[MATERIAL_MAP_METALNESS].texture = LoadTexture("resources/road_mra.png"); + + Texture2D floorTexture=LoadTexture("resources/road_mra.png"); + Image floorImage=LoadImageFromTexture(floorTexture); + //Using different channels of RMA map for different parameters + floor.materials[0].maps[MATERIAL_MAP_METALNESS].texture = LoadTextureFromImage(ImageFromChannel(floorImage,1)); + floor.materials[0].maps[MATERIAL_MAP_ROUGHNESS].texture = LoadTextureFromImage(ImageFromChannel(floorImage,0)); + floor.materials[0].maps[MATERIAL_MAP_OCCLUSION].texture = LoadTextureFromImage(ImageFromChannel(floorImage,2)); floor.materials[0].maps[MATERIAL_MAP_NORMAL].texture = LoadTexture("resources/road_n.png"); // Models texture tiling parameter can be stored in the Material struct if required (CURRENTLY NOT USED) @@ -199,7 +211,7 @@ int main() // Update //---------------------------------------------------------------------------------- UpdateCamera(&camera, CAMERA_ORBITAL); - + //if (IsKeyPressedRepeat(KEY_RIGHT)){UpdateCamera(&camera,CAMERA_ORBITAL);}; // Update the shader with the camera view vector (points towards { 0.0f, 0.0f, 0.0f }) float cameraPos[3] = {camera.position.x, camera.position.y, camera.position.z}; SetShaderValue(shader, shader.locs[SHADER_LOC_VECTOR_VIEW], cameraPos, SHADER_UNIFORM_VEC3); @@ -251,7 +263,7 @@ int main() DrawText("Toggle lights: [1][2][3][4]", 10, 40, 20, LIGHTGRAY); - DrawText("(c) Old Rusty Car model by Renafox (https://skfb.ly/LxRy)", screenWidth - 320, screenHeight - 20, 10, LIGHTGRAY); + //DrawText("(c) Old Rusty Car model by Renafox (https://skfb.ly/LxRy)", screenWidth - 320, screenHeight - 20, 10, LIGHTGRAY); DrawFPS(10, 10); @@ -261,8 +273,14 @@ int main() // De-Initialization //-------------------------------------------------------------------------------------- + UnloadImage(floorImage); + UnloadTexture(floorTexture); + UnloadImage(carImage); + UnloadTexture(carTexture); // Unbind (disconnect) shader from car.material[0] // to avoid UnloadMaterial() trying to unload it automatically + + car.materials[0].shader = (Shader){ 0 }; UnloadMaterial(car.materials[0]); car.materials[0].maps = NULL; From 475b4c0858b2dd9902be92679b3334bdab823f81 Mon Sep 17 00:00:00 2001 From: Almaas Rizvi Date: Fri, 10 Jan 2025 22:33:13 +0530 Subject: [PATCH 2/2] Changed things to implement correct usage of RMA (or MRA) textures RMA (MRA) textures provide Red channel for Roughness, Green channel for Metalness, and Blue channel for Ambient Occlusion. Made changes to this example to implement this texture correctly --- examples/shaders/shaders_basic_pbr.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/shaders/shaders_basic_pbr.c b/examples/shaders/shaders_basic_pbr.c index 895867b3d3e5..b7fdfc2f687d 100644 --- a/examples/shaders/shaders_basic_pbr.c +++ b/examples/shaders/shaders_basic_pbr.c @@ -84,7 +84,7 @@ int main() const int screenHeight = 450; SetConfigFlags(FLAG_MSAA_4X_HINT); - InitWindow(screenWidth, screenHeight, "basic pbr"); + InitWindow(screenWidth, screenHeight, "raylib [shaders] example - basic pbr"); // Define the camera to look into our 3d world Camera camera = { 0 }; @@ -263,7 +263,7 @@ int main() DrawText("Toggle lights: [1][2][3][4]", 10, 40, 20, LIGHTGRAY); - //DrawText("(c) Old Rusty Car model by Renafox (https://skfb.ly/LxRy)", screenWidth - 320, screenHeight - 20, 10, LIGHTGRAY); + DrawText("(c) Old Rusty Car model by Renafox (https://skfb.ly/LxRy)", screenWidth - 320, screenHeight - 20, 10, LIGHTGRAY); DrawFPS(10, 10);