From 5f5910487754514020efc8a193bff32be12f4c14 Mon Sep 17 00:00:00 2001 From: Haroon Qureshi Date: Thu, 4 Dec 2025 07:56:20 -0800 Subject: [PATCH] Properly handle PBR materials where no color texture is provided. In this case, we'll use the mujoco material color which is then multiplied with a white fallback texture. PiperOrigin-RevId: 840244521 Change-Id: I2134f45dcbaed7db268e5278ca8ac5429488943d --- .../filament/filament/drawable.cc | 70 ++++++++++--------- .../filament/filament/object_manager.cc | 4 +- 2 files changed, 40 insertions(+), 34 deletions(-) diff --git a/src/experimental/filament/filament/drawable.cc b/src/experimental/filament/filament/drawable.cc index 3fc52200..a8a6b317 100644 --- a/src/experimental/filament/filament/drawable.cc +++ b/src/experimental/filament/filament/drawable.cc @@ -342,7 +342,9 @@ void Drawable::UpdateMaterial(const mjvGeom& geom) { if (geom.type == mjGEOM_LINE || geom.type == mjGEOM_LINEBOX) { material_.SetNormalMaterialType(ObjectManager::kUnlitLine); } else { + bool material_assigned = false; if (geom.matid >= 0) { + material_assigned = true; if (textures.orm) { material_.SetNormalMaterialType(ObjectManager::kPbrPacked); } else if (textures.metallic) { @@ -353,43 +355,47 @@ void Drawable::UpdateMaterial(const mjvGeom& geom) { material_.SetNormalMaterialType(ObjectManager::kPbr); } else if (model->mat_roughness[geom.matid] >= 0) { material_.SetNormalMaterialType(ObjectManager::kPbr); + } else { + material_assigned = false; } } - // Check to see if we're dealing with a mesh with texture coordinates. - // `data_id` is the id of the mesh in model (i.e. the geom has mesh - // geometry) and `mesh_texcoordadr` stores the address of the mesh uvs if it - // has them. - bool has_texcoords = false; - if ((geom.type == mjGEOM_MESH || geom.type == mjGEOM_SDF) && - geom.dataid >= 0 && model->mesh_texcoordadr[geom.dataid / 2] >= 0) { - has_texcoords = true; - } + if (!material_assigned) { + // Check to see if we're dealing with a mesh with texture coordinates. + // `data_id` is the id of the mesh in model (i.e. the geom has mesh + // geometry) and `mesh_texcoordadr` stores the address of the mesh uvs if + // it has them. + bool has_texcoords = false; + if ((geom.type == mjGEOM_MESH || geom.type == mjGEOM_SDF) && + geom.dataid >= 0 && model->mesh_texcoordadr[geom.dataid / 2] >= 0) { + has_texcoords = true; + } - if (textures.color == nullptr) { - if (geom.rgba[3] < 1.0f) { - material_.SetNormalMaterialType(ObjectManager::kPhongColorFade); + if (textures.color == nullptr) { + if (geom.rgba[3] < 1.0f) { + material_.SetNormalMaterialType(ObjectManager::kPhongColorFade); + } else { + material_.SetNormalMaterialType(ObjectManager::kPhongColor); + } + } else if (textures.color->getTarget() == + filament::Texture::Sampler::SAMPLER_CUBEMAP) { + if (geom.rgba[3] < 1.0f) { + material_.SetNormalMaterialType(ObjectManager::kPhongCubeFade); + } else { + material_.SetNormalMaterialType(ObjectManager::kPhongCube); + } + } else if (has_texcoords) { + if (geom.rgba[3] < 1.0f) { + material_.SetNormalMaterialType(ObjectManager::kPhong2dUvFade); + } else { + material_.SetNormalMaterialType(ObjectManager::kPhong2dUv); + } } else { - material_.SetNormalMaterialType(ObjectManager::kPhongColor); - } - } else if (textures.color->getTarget() == - filament::Texture::Sampler::SAMPLER_CUBEMAP) { - if (geom.rgba[3] < 1.0f) { - material_.SetNormalMaterialType(ObjectManager::kPhongCubeFade); - } else { - material_.SetNormalMaterialType(ObjectManager::kPhongCube); - } - } else if (has_texcoords) { - if (geom.rgba[3] < 1.0f) { - material_.SetNormalMaterialType(ObjectManager::kPhong2dUvFade); - } else { - material_.SetNormalMaterialType(ObjectManager::kPhong2dUv); - } - } else { - if (geom.rgba[3] < 1.0f) { - material_.SetNormalMaterialType(ObjectManager::kPhong2dFade); - } else { - material_.SetNormalMaterialType(ObjectManager::kPhong2d); + if (geom.rgba[3] < 1.0f) { + material_.SetNormalMaterialType(ObjectManager::kPhong2dFade); + } else { + material_.SetNormalMaterialType(ObjectManager::kPhong2d); + } } } } diff --git a/src/experimental/filament/filament/object_manager.cc b/src/experimental/filament/filament/object_manager.cc index 5e204b78..cbaff5e9 100644 --- a/src/experimental/filament/filament/object_manager.cc +++ b/src/experimental/filament/filament/object_manager.cc @@ -121,8 +121,8 @@ ObjectManager::ObjectManager(const mjModel* model, filament::Engine* engine, fallback_orm_ = Create2dTexture(engine_, 1, 1, 3, orm_data, false); fallback_textures_[mjTEXROLE_USER] = fallback_black_; - fallback_textures_[mjTEXROLE_RGB] = fallback_black_; - fallback_textures_[mjTEXROLE_OCCLUSION] = fallback_black_; + fallback_textures_[mjTEXROLE_RGB] = fallback_white_; + fallback_textures_[mjTEXROLE_OCCLUSION] = fallback_white_; fallback_textures_[mjTEXROLE_ROUGHNESS] = fallback_white_; fallback_textures_[mjTEXROLE_METALLIC] = fallback_black_; fallback_textures_[mjTEXROLE_NORMAL] = fallback_normal_;