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
This commit is contained in:
Haroon Qureshi
2025-12-04 07:56:20 -08:00
committed by Copybara-Service
parent 680e9a6b79
commit 5f59104877
2 changed files with 40 additions and 34 deletions
+38 -32
View File
@@ -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);
}
}
}
}
@@ -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_;