Move GetMaterialType into material module.

PiperOrigin-RevId: 915895534
Change-Id: Ic7e9d52374131aa93445f90cf2208f6c93d123f4
This commit is contained in:
Haroon Qureshi
2026-05-15 02:44:38 -07:00
committed by Copybara-Service
parent 072125c49c
commit 85bf2b9660
6 changed files with 83 additions and 80 deletions
@@ -21,12 +21,76 @@
#include <filament/TextureSampler.h>
#include <mujoco/mujoco.h>
#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) {
@@ -17,14 +17,20 @@
#include <filament/Engine.h>
#include <filament/MaterialInstance.h>
#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);
+5 -3
View File
@@ -14,13 +14,13 @@
#include "experimental/filament/filament/mesh.h"
#include <algorithm>
#include <cfloat>
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <memory>
#include <mutex>
#include <span>
#include <utility>
#include <filament/Box.h>
@@ -353,8 +353,10 @@ filament::RenderableManager::PrimitiveType Mesh::GetPrimitiveType() const {
return type_;
}
std::span<const filament::VertexAttribute> 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 {
+2 -3
View File
@@ -20,7 +20,6 @@
#include <memory>
#include <mutex>
#include <optional>
#include <span>
#include <vector>
#include <filament/Box.h>
@@ -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<const filament::VertexAttribute> 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;
@@ -14,7 +14,6 @@
#include "experimental/filament/filament/renderable.h"
#include <algorithm>
#include <cstdint>
#include <numbers>
@@ -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;
@@ -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);