Move GetMaterialType into material module.
PiperOrigin-RevId: 915895534 Change-Id: Ic7e9d52374131aa93445f90cf2208f6c93d123f4
This commit is contained in:
committed by
Copybara-Service
parent
072125c49c
commit
85bf2b9660
@@ -21,12 +21,76 @@
|
|||||||
#include <filament/TextureSampler.h>
|
#include <filament/TextureSampler.h>
|
||||||
#include <mujoco/mujoco.h>
|
#include <mujoco/mujoco.h>
|
||||||
#include "experimental/filament/filament_util.h"
|
#include "experimental/filament/filament_util.h"
|
||||||
|
#include "experimental/filament/filament/mesh.h"
|
||||||
#include "experimental/filament/filament/object_manager.h"
|
#include "experimental/filament/filament/object_manager.h"
|
||||||
#include "experimental/filament/filament/texture.h"
|
#include "experimental/filament/filament/texture.h"
|
||||||
#include "experimental/filament/render_context_filament.h"
|
#include "experimental/filament/render_context_filament.h"
|
||||||
|
|
||||||
namespace mujoco {
|
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,
|
void UpdateMaterialInstance(filament::MaterialInstance* instance,
|
||||||
const mjrMaterial& material,
|
const mjrMaterial& material,
|
||||||
ObjectManager* object_mgr) {
|
ObjectManager* object_mgr) {
|
||||||
|
|||||||
@@ -17,14 +17,20 @@
|
|||||||
|
|
||||||
#include <filament/Engine.h>
|
#include <filament/Engine.h>
|
||||||
#include <filament/MaterialInstance.h>
|
#include <filament/MaterialInstance.h>
|
||||||
|
#include "experimental/filament/filament/mesh.h"
|
||||||
#include "experimental/filament/filament/object_manager.h"
|
#include "experimental/filament/filament/object_manager.h"
|
||||||
#include "experimental/filament/render_context_filament.h"
|
#include "experimental/filament/render_context_filament.h"
|
||||||
|
|
||||||
namespace mujoco {
|
namespace mujoco {
|
||||||
|
|
||||||
// Updates the material instance using the given parameters and texture data. In
|
// Returns a MaterialType that best matches the given material data and mesh.
|
||||||
// some cases where a material needs a texture, but a specific texture is not
|
ObjectManager::MaterialType GetMaterialType(
|
||||||
// provided, a default texture from the ObjectManager will be used instead.
|
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,
|
void UpdateMaterialInstance(filament::MaterialInstance* instance,
|
||||||
const mjrMaterial& material,
|
const mjrMaterial& material,
|
||||||
ObjectManager* object_mgr);
|
ObjectManager* object_mgr);
|
||||||
|
|||||||
@@ -14,13 +14,13 @@
|
|||||||
|
|
||||||
#include "experimental/filament/filament/mesh.h"
|
#include "experimental/filament/filament/mesh.h"
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
#include <cfloat>
|
#include <cfloat>
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <span>
|
|
||||||
#include <utility>
|
#include <utility>
|
||||||
|
|
||||||
#include <filament/Box.h>
|
#include <filament/Box.h>
|
||||||
@@ -353,8 +353,10 @@ filament::RenderableManager::PrimitiveType Mesh::GetPrimitiveType() const {
|
|||||||
return type_;
|
return type_;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::span<const filament::VertexAttribute> Mesh::GetVertexAttributes() const {
|
bool Mesh::HasVertexAttribute(mjrVertexAttributeUsage attrib) const {
|
||||||
return {attributes_.data(), attributes_.data() + num_attributes_};
|
auto fattrib = GetUsage(mjrVertexAttribute{.usage = attrib});
|
||||||
|
auto it = std::find(attributes_.begin(), attributes_.end(), fattrib);
|
||||||
|
return it != attributes_.end();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Mesh::HasBounds() const {
|
bool Mesh::HasBounds() const {
|
||||||
|
|||||||
@@ -20,7 +20,6 @@
|
|||||||
#include <memory>
|
#include <memory>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <optional>
|
#include <optional>
|
||||||
#include <span>
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include <filament/Box.h>
|
#include <filament/Box.h>
|
||||||
@@ -52,8 +51,8 @@ class Mesh : public mjrMesh {
|
|||||||
// Returns the primitive type of the mesh.
|
// Returns the primitive type of the mesh.
|
||||||
filament::RenderableManager::PrimitiveType GetPrimitiveType() const;
|
filament::RenderableManager::PrimitiveType GetPrimitiveType() const;
|
||||||
|
|
||||||
// Returns the vertex attribute usages for the mesh.
|
// Returns true if the mesh has the given attribute.
|
||||||
std::span<const filament::VertexAttribute> GetVertexAttributes() const;
|
bool HasVertexAttribute(mjrVertexAttributeUsage attrib) const;
|
||||||
|
|
||||||
// Returns whether the mesh has bounds.
|
// Returns whether the mesh has bounds.
|
||||||
bool HasBounds() const;
|
bool HasBounds() const;
|
||||||
|
|||||||
@@ -14,7 +14,6 @@
|
|||||||
|
|
||||||
#include "experimental/filament/filament/renderable.h"
|
#include "experimental/filament/filament/renderable.h"
|
||||||
|
|
||||||
#include <algorithm>
|
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <numbers>
|
#include <numbers>
|
||||||
|
|
||||||
@@ -34,7 +33,6 @@
|
|||||||
#include "experimental/filament/filament/material.h"
|
#include "experimental/filament/filament/material.h"
|
||||||
#include "experimental/filament/filament/mesh.h"
|
#include "experimental/filament/filament/mesh.h"
|
||||||
#include "experimental/filament/filament/object_manager.h"
|
#include "experimental/filament/filament/object_manager.h"
|
||||||
#include "experimental/filament/filament/texture.h"
|
|
||||||
#include "experimental/filament/render_context_filament.h"
|
#include "experimental/filament/render_context_filament.h"
|
||||||
|
|
||||||
namespace mujoco {
|
namespace mujoco {
|
||||||
@@ -210,7 +208,9 @@ void Renderable::RemoveFromScene(filament::Scene* scene) {
|
|||||||
void Renderable::UpdateMaterial(const mjrMaterial& material) {
|
void Renderable::UpdateMaterial(const mjrMaterial& material) {
|
||||||
material_ = 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) {
|
if (!material_.decor_ux) {
|
||||||
AssignMaterial(mjDRAW_MODE_DEPTH, ObjectManager::kUnlitDepth);
|
AssignMaterial(mjDRAW_MODE_DEPTH, ObjectManager::kUnlitDepth);
|
||||||
AssignMaterial(mjDRAW_MODE_SEGMENTATION, ObjectManager::kUnlitSegmentation);
|
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) {
|
void Renderable::SetGeomMesh(mjtGeom type, int nstack, int nslice, int nquad) {
|
||||||
Builtins* builtins = object_mgr_->GetBuiltins(nstack, nslice, nquad);
|
Builtins* builtins = object_mgr_->GetBuiltins(nstack, nslice, nquad);
|
||||||
geom_type_ = type;
|
geom_type_ = type;
|
||||||
|
|||||||
@@ -121,7 +121,6 @@ class Renderable : public mjrRenderable {
|
|||||||
void AppendMesh(const Mesh* mesh);
|
void AppendMesh(const Mesh* mesh);
|
||||||
void InitPartEntity(Part& part);
|
void InitPartEntity(Part& part);
|
||||||
|
|
||||||
ObjectManager::MaterialType GetColorMaterialType() const;
|
|
||||||
void AssignMaterial(mjrDrawMode mode,
|
void AssignMaterial(mjrDrawMode mode,
|
||||||
ObjectManager::MaterialType material_type);
|
ObjectManager::MaterialType material_type);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user