Move filament::Skybox object management into SceneView.

ModelObjects no longer directly manages any filament objects.

PiperOrigin-RevId: 906873925
Change-Id: Iaa317cbeb9c5236745a5f0d04a740985bec54a5c
This commit is contained in:
Haroon Qureshi
2026-04-28 03:55:28 -07:00
committed by Copybara-Service
parent 6f275b79c6
commit 32aeb377b5
5 changed files with 23 additions and 78 deletions
@@ -26,12 +26,7 @@
#include <vector> #include <vector>
#include <filament/Engine.h> #include <filament/Engine.h>
#include <filament/IndirectLight.h>
#include <filament/Material.h>
#include <filament/Skybox.h>
#include <math/TVecHelpers.h> #include <math/TVecHelpers.h>
#include <math/mat3.h>
#include <math/scalar.h>
#include <math/vec2.h> #include <math/vec2.h>
#include <math/vec3.h> #include <math/vec3.h>
#include <math/vec4.h> #include <math/vec4.h>
@@ -47,7 +42,6 @@ namespace mujoco {
using filament::math::float2; using filament::math::float2;
using filament::math::float3; using filament::math::float3;
using filament::math::float4; using filament::math::float4;
using filament::math::mat3f;
enum class MeshType { enum class MeshType {
kNormal, kNormal,
@@ -534,12 +528,6 @@ ModelObjects::ModelObjects(const mjModel* model, filament::Engine* engine)
} }
ModelObjects::~ModelObjects() { ModelObjects::~ModelObjects() {
for (auto& iter : skyboxes_) {
engine_->destroy(iter);
}
for (auto& iter : indirect_lights_) {
engine_->destroy(iter);
}
meshes_.clear(); meshes_.clear();
textures_.clear(); textures_.clear();
} }
@@ -681,48 +669,13 @@ const Texture* ModelObjects::GetTexture(int mat_id, int role) const {
return GetTexture(tex_id); return GetTexture(tex_id);
} }
filament::IndirectLight* ModelObjects::CreateIndirectLight(int tex_id, const Texture* ModelObjects::GetSkyboxTexture() const {
float intensity) {
filament::Texture* texture = nullptr;
const Texture::SphericalHarmonics* spherical_harmonics = nullptr;
auto texture_iter = textures_.find(tex_id);
if (texture_iter != textures_.end()) {
texture = texture_iter->second->GetFilamentTexture();
spherical_harmonics = texture_iter->second->GetSphericalHarmonics();
}
filament::IndirectLight::Builder builder;
builder.reflections(texture);
if (spherical_harmonics != nullptr) {
builder.irradiance(3, *spherical_harmonics);
}
builder.intensity(intensity);
// Rotate the light to match mujoco's Z-up convention.
builder.rotation(mat3f::rotation(filament::math::f::PI / 2, float3{1, 0, 0}));
filament::IndirectLight* indirect_light = builder.build(*engine_);
indirect_lights_.push_back(indirect_light);
return indirect_light;
}
filament::Skybox* ModelObjects::CreateSkybox() {
filament::Texture* skybox_texture = nullptr;
for (auto& iter : textures_) { for (auto& iter : textures_) {
const int texture_type = model_->tex_type[iter.first]; if (model_->tex_type[iter.first] == mjTEXTURE_SKYBOX) {
if (texture_type == mjTEXTURE_SKYBOX) { return iter.second.get();
skybox_texture = iter.second->GetFilamentTexture();
break;
} }
} }
return nullptr;
if (skybox_texture == nullptr) {
return nullptr;
}
filament::Skybox::Builder builder;
builder.environment(skybox_texture);
filament::Skybox* skybox = builder.build(*engine_);
skyboxes_.push_back(skybox);
return skybox;
} }
} // namespace mujoco } // namespace mujoco
@@ -18,11 +18,8 @@
#include <array> #include <array>
#include <memory> #include <memory>
#include <unordered_map> #include <unordered_map>
#include <vector>
#include <filament/Engine.h> #include <filament/Engine.h>
#include <filament/IndirectLight.h>
#include <filament/Skybox.h>
#include <mujoco/mjmodel.h> #include <mujoco/mjmodel.h>
#include <mujoco/mujoco.h> #include <mujoco/mujoco.h>
#include "experimental/filament/filament/mesh.h" #include "experimental/filament/filament/mesh.h"
@@ -30,7 +27,7 @@
namespace mujoco { namespace mujoco {
// Creates and owns various filament objects based on the data in a mjrContext. // Creates and owns various filament objects based on the mjModel.
class ModelObjects { class ModelObjects {
public: public:
ModelObjects(const mjModel* model, filament::Engine* engine); ModelObjects(const mjModel* model, filament::Engine* engine);
@@ -58,10 +55,6 @@ class ModelObjects {
void CreateSkinFlexMesh(const mjvScene* scene, const mjvGeom& geom); void CreateSkinFlexMesh(const mjvScene* scene, const mjvGeom& geom);
// Returns the filament engine used by the ModelObjects to create filament
// objects.
filament::Engine* GetEngine() const { return engine_; }
// Returns the cached instance of a filament object created from the mjModel. // Returns the cached instance of a filament object created from the mjModel.
const Mesh* GetShapeBuffer(ShapeType shape) const; const Mesh* GetShapeBuffer(ShapeType shape) const;
const Mesh* GetMeshBuffer(int data_id) const; const Mesh* GetMeshBuffer(int data_id) const;
@@ -69,9 +62,7 @@ class ModelObjects {
const Mesh* GetFlexSkinGeomMesh(int geom_id) const; const Mesh* GetFlexSkinGeomMesh(int geom_id) const;
const Texture* GetTexture(int tex_id) const; const Texture* GetTexture(int tex_id) const;
const Texture* GetTexture(int mat_id, int role) const; const Texture* GetTexture(int mat_id, int role) const;
const Texture* GetSkyboxTexture() const;
filament::Skybox* CreateSkybox();
filament::IndirectLight* CreateIndirectLight(int tex_id, float intensity);
float GetSpecularMultiplier() const { return specular_multiplier_; } float GetSpecularMultiplier() const { return specular_multiplier_; }
float GetShininessMultiplier() const { return shininess_multiplier_; } float GetShininessMultiplier() const { return shininess_multiplier_; }
@@ -85,8 +76,6 @@ class ModelObjects {
private: private:
const mjModel* model_ = nullptr; const mjModel* model_ = nullptr;
filament::Engine* engine_ = nullptr; filament::Engine* engine_ = nullptr;
std::vector<filament::Skybox*> skyboxes_;
std::vector<filament::IndirectLight*> indirect_lights_;
std::array<std::unique_ptr<Mesh>, kNumShapes> shapes_; std::array<std::unique_ptr<Mesh>, kNumShapes> shapes_;
std::unordered_map<int, std::unique_ptr<Mesh>> meshes_; std::unordered_map<int, std::unique_ptr<Mesh>> meshes_;
std::unordered_map<int, std::unique_ptr<Mesh>> convex_hulls_; std::unordered_map<int, std::unique_ptr<Mesh>> convex_hulls_;
@@ -322,10 +322,7 @@ void SceneBridge::PrepareLights() {
} }
} }
filament::Skybox* skybox = model_objects_->CreateSkybox(); scene_view_->SetSkybox(model_objects_->GetSkyboxTexture());
if (skybox) {
scene_view_->AddToScene(skybox);
}
} }
filament::math::mat4 CalculateClipFromWorld(const mjrRect& viewport, filament::math::mat4 CalculateClipFromWorld(const mjrRect& viewport,
@@ -158,6 +158,10 @@ SceneView::SceneView(filament::Engine* engine) : engine_(engine) {
} }
SceneView::~SceneView() { SceneView::~SceneView() {
if (skybox_) {
scene_->setSkybox(nullptr);
engine_->destroy(skybox_);
}
for (auto& light : lights_) { for (auto& light : lights_) {
light->RemoveFromScene(scene_); light->RemoveFromScene(scene_);
} }
@@ -210,15 +214,17 @@ void SceneView::RemoveFromScene(Renderable* renderable) {
} }
} }
void SceneView::AddToScene(filament::Skybox* skybox) { void SceneView::SetSkybox(const Texture* skybox_texture) {
skybox_ = skybox; if (skybox_) {
scene_->setSkybox(skybox);
}
void SceneView::RemoveFromScene(filament::Skybox* skybox) {
if (skybox_ == skybox) {
skybox_ = nullptr;
scene_->setSkybox(nullptr); scene_->setSkybox(nullptr);
engine_->destroy(skybox_);
skybox_ = nullptr;
}
if (skybox_texture) {
filament::Skybox::Builder builder;
builder.environment(skybox_texture->GetFilamentTexture());
skybox_ = builder.build(*engine_);
scene_->setSkybox(skybox_);
} }
} }
@@ -31,6 +31,7 @@
#include "experimental/filament/filament/light.h" #include "experimental/filament/filament/light.h"
#include "experimental/filament/filament/renderable.h" #include "experimental/filament/filament/renderable.h"
#include "experimental/filament/filament/render_target.h" #include "experimental/filament/filament/render_target.h"
#include "experimental/filament/filament/texture.h"
namespace mujoco { namespace mujoco {
@@ -49,8 +50,7 @@ class SceneView {
void RemoveFromScene(Light* light); void RemoveFromScene(Light* light);
void AddToScene(Renderable* renderable); void AddToScene(Renderable* renderable);
void RemoveFromScene(Renderable* renderable); void RemoveFromScene(Renderable* renderable);
void AddToScene(filament::Skybox* skybox); void SetSkybox(const Texture* skybox_texture);
void RemoveFromScene(filament::Skybox* skybox);
// Parameters for rendering the scene. // Parameters for rendering the scene.
struct RenderRequest { struct RenderRequest {