From 85bf2b96602da6bb576cb4be1a2f5a394fcbf869 Mon Sep 17 00:00:00 2001 From: Haroon Qureshi Date: Fri, 15 May 2026 02:44:38 -0700 Subject: [PATCH] Move GetMaterialType into material module. PiperOrigin-RevId: 915895534 Change-Id: Ic7e9d52374131aa93445f90cf2208f6c93d123f4 --- .../filament/filament/material.cc | 64 ++++++++++++++++ src/experimental/filament/filament/material.h | 12 ++- src/experimental/filament/filament/mesh.cc | 8 +- src/experimental/filament/filament/mesh.h | 5 +- .../filament/filament/renderable.cc | 73 +------------------ .../filament/filament/renderable.h | 1 - 6 files changed, 83 insertions(+), 80 deletions(-) diff --git a/src/experimental/filament/filament/material.cc b/src/experimental/filament/filament/material.cc index 5e074d68..aded053b 100644 --- a/src/experimental/filament/filament/material.cc +++ b/src/experimental/filament/filament/material.cc @@ -21,12 +21,76 @@ #include #include #include "experimental/filament/filament_util.h" +#include "experimental/filament/filament/mesh.h" #include "experimental/filament/filament/object_manager.h" #include "experimental/filament/filament/texture.h" #include "experimental/filament/render_context_filament.h" namespace mujoco { +ObjectManager::MaterialType GetMaterialType(const mjrMaterial& material, + const Mesh* mesh) { + if (material.decor_ux) { + if (material.color_texture) { + return ObjectManager::kUnlitUi; + } else { + return ObjectManager::kUnlitDecor; + } + } else if (material.orm_texture) { + return ObjectManager::kPbrPacked; + } else if (material.metallic_texture) { + return ObjectManager::kPbr; + } else if (material.roughness_texture) { + return ObjectManager::kPbr; + } else if (material.metallic >= 0) { + return ObjectManager::kPbr; + } else if (material.roughness >= 0) { + return ObjectManager::kPbr; + } + + // 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. + const Texture* color_texture = Texture::downcast(material.color_texture); + const bool has_texcoords = + mesh ? mesh->HasVertexAttribute(mjVERTEX_ATTRIBUTE_USAGE_UV) : false; + + if (color_texture == nullptr) { + if (material.color[3] < 1.0f) { + return ObjectManager::kPhongColorFade; + } else if (material.reflective) { + return ObjectManager::kPhongColorReflect; + } else { + return ObjectManager::kPhongColor; + } + } else if (color_texture->GetSamplerType() == mjTEXTURE_CUBE) { + if (material.color[3] < 1.0f) { + return ObjectManager::kPhongCubeFade; + } else if (material.reflective) { + return ObjectManager::kPhongCubeReflect; + } else { + return ObjectManager::kPhongCube; + } + } else if (has_texcoords) { + if (material.color[3] < 1.0f) { + return ObjectManager::kPhong2dUvFade; + } else if (material.reflective) { + return ObjectManager::kPhong2dUvReflect; + } else { + return ObjectManager::kPhong2dUv; + } + } else { + if (material.color[3] < 1.0f) { + return ObjectManager::kPhong2dFade; + } else if (material.reflective) { + return ObjectManager::kPhong2dReflect; + } else { + return ObjectManager::kPhong2d; + } + } +} + void UpdateMaterialInstance(filament::MaterialInstance* instance, const mjrMaterial& material, ObjectManager* object_mgr) { diff --git a/src/experimental/filament/filament/material.h b/src/experimental/filament/filament/material.h index 25469924..a131633d 100644 --- a/src/experimental/filament/filament/material.h +++ b/src/experimental/filament/filament/material.h @@ -17,14 +17,20 @@ #include #include +#include "experimental/filament/filament/mesh.h" #include "experimental/filament/filament/object_manager.h" #include "experimental/filament/render_context_filament.h" namespace mujoco { -// Updates the material instance using the given parameters and texture data. In -// some cases where a material needs a texture, but a specific texture is not -// provided, a default texture from the ObjectManager will be used instead. +// Returns a MaterialType that best matches the given material data and mesh. +ObjectManager::MaterialType GetMaterialType( + const mjrMaterial& material, const Mesh* mesh); + +// Updates the material instance using the given parameters and texture +// data. In some cases where a material needs a texture, but a specific +// texture is not provided, a default texture from the ObjectManager will be +// used instead. void UpdateMaterialInstance(filament::MaterialInstance* instance, const mjrMaterial& material, ObjectManager* object_mgr); diff --git a/src/experimental/filament/filament/mesh.cc b/src/experimental/filament/filament/mesh.cc index 9d08789e..31c52021 100644 --- a/src/experimental/filament/filament/mesh.cc +++ b/src/experimental/filament/filament/mesh.cc @@ -14,13 +14,13 @@ #include "experimental/filament/filament/mesh.h" +#include #include #include #include #include #include #include -#include #include #include @@ -353,8 +353,10 @@ filament::RenderableManager::PrimitiveType Mesh::GetPrimitiveType() const { return type_; } -std::span Mesh::GetVertexAttributes() const { - return {attributes_.data(), attributes_.data() + num_attributes_}; +bool Mesh::HasVertexAttribute(mjrVertexAttributeUsage attrib) const { + auto fattrib = GetUsage(mjrVertexAttribute{.usage = attrib}); + auto it = std::find(attributes_.begin(), attributes_.end(), fattrib); + return it != attributes_.end(); } bool Mesh::HasBounds() const { diff --git a/src/experimental/filament/filament/mesh.h b/src/experimental/filament/filament/mesh.h index 8e79053b..0e1bb7a0 100644 --- a/src/experimental/filament/filament/mesh.h +++ b/src/experimental/filament/filament/mesh.h @@ -20,7 +20,6 @@ #include #include #include -#include #include #include @@ -52,8 +51,8 @@ class Mesh : public mjrMesh { // Returns the primitive type of the mesh. filament::RenderableManager::PrimitiveType GetPrimitiveType() const; - // Returns the vertex attribute usages for the mesh. - std::span GetVertexAttributes() const; + // Returns true if the mesh has the given attribute. + bool HasVertexAttribute(mjrVertexAttributeUsage attrib) const; // Returns whether the mesh has bounds. bool HasBounds() const; diff --git a/src/experimental/filament/filament/renderable.cc b/src/experimental/filament/filament/renderable.cc index 232cdfb9..0d2def19 100644 --- a/src/experimental/filament/filament/renderable.cc +++ b/src/experimental/filament/filament/renderable.cc @@ -14,7 +14,6 @@ #include "experimental/filament/filament/renderable.h" -#include #include #include @@ -34,7 +33,6 @@ #include "experimental/filament/filament/material.h" #include "experimental/filament/filament/mesh.h" #include "experimental/filament/filament/object_manager.h" -#include "experimental/filament/filament/texture.h" #include "experimental/filament/render_context_filament.h" namespace mujoco { @@ -210,7 +208,9 @@ void Renderable::RemoveFromScene(filament::Scene* scene) { void Renderable::UpdateMaterial(const mjrMaterial& material) { material_ = material; - AssignMaterial(mjDRAW_MODE_COLOR, GetColorMaterialType()); + const Mesh* mesh = !parts_.empty() ? parts_[0].mesh : nullptr; + + AssignMaterial(mjDRAW_MODE_COLOR, GetMaterialType(material_, mesh)); if (!material_.decor_ux) { AssignMaterial(mjDRAW_MODE_DEPTH, ObjectManager::kUnlitDepth); AssignMaterial(mjDRAW_MODE_SEGMENTATION, ObjectManager::kUnlitSegmentation); @@ -348,73 +348,6 @@ void Renderable::SetWireframe(bool wireframe) { } } -ObjectManager::MaterialType Renderable::GetColorMaterialType() const { - if (material_.decor_ux) { - if (material_.color_texture) { - return ObjectManager::kUnlitUi; - } else { - return ObjectManager::kUnlitDecor; - } - } else if (material_.orm_texture) { - return ObjectManager::kPbrPacked; - } else if (material_.metallic_texture) { - return ObjectManager::kPbr; - } else if (material_.roughness_texture) { - return ObjectManager::kPbr; - } else if (material_.metallic >= 0) { - return ObjectManager::kPbr; - } else if (material_.roughness >= 0) { - return ObjectManager::kPbr; - } - - // 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; - const Texture* color_texture = Texture::downcast(material_.color_texture); - if (!parts_.empty()) { - const auto attribs = parts_[0].mesh->GetVertexAttributes(); - auto it = std::find(attribs.begin(), attribs.end(), - filament::VertexAttribute::UV0); - has_texcoords = (it != attribs.end()); - } - - if (color_texture == nullptr) { - if (material_.color[3] < 1.0f) { - return ObjectManager::kPhongColorFade; - } else if (material_.reflective) { - return ObjectManager::kPhongColorReflect; - } else { - return ObjectManager::kPhongColor; - } - } else if (color_texture->GetSamplerType() == mjTEXTURE_CUBE) { - if (material_.color[3] < 1.0f) { - return ObjectManager::kPhongCubeFade; - } else if (material_.reflective) { - return ObjectManager::kPhongCubeReflect; - } else { - return ObjectManager::kPhongCube; - } - } else if (has_texcoords) { - if (material_.color[3] < 1.0f) { - return ObjectManager::kPhong2dUvFade; - } else if (material_.reflective) { - return ObjectManager::kPhong2dUvReflect; - } else { - return ObjectManager::kPhong2dUv; - } - } else { - if (material_.color[3] < 1.0f) { - return ObjectManager::kPhong2dFade; - } else if (material_.reflective) { - return ObjectManager::kPhong2dReflect; - } else { - return ObjectManager::kPhong2d; - } - } -} - void Renderable::SetGeomMesh(mjtGeom type, int nstack, int nslice, int nquad) { Builtins* builtins = object_mgr_->GetBuiltins(nstack, nslice, nquad); geom_type_ = type; diff --git a/src/experimental/filament/filament/renderable.h b/src/experimental/filament/filament/renderable.h index 39a267d5..c41c83d3 100644 --- a/src/experimental/filament/filament/renderable.h +++ b/src/experimental/filament/filament/renderable.h @@ -121,7 +121,6 @@ class Renderable : public mjrRenderable { void AppendMesh(const Mesh* mesh); void InitPartEntity(Part& part); - ObjectManager::MaterialType GetColorMaterialType() const; void AssignMaterial(mjrDrawMode mode, ObjectManager::MaterialType material_type);