Add Update function to LightManager.

PiperOrigin-RevId: 931152006
Change-Id: I6fe5d4ab0dbc8032fe84fd12968666d755d1d7d5
This commit is contained in:
Haroon Qureshi
2026-06-12 07:58:00 -07:00
committed by Copybara-Service
parent 01d5be61e3
commit 2c125dd447
2 changed files with 35 additions and 7 deletions
+28 -6
View File
@@ -74,7 +74,7 @@ static UniquePtr<mjrfTexture> CreateFallbackIndirectLightTexture(
LightManager::LightManager(mjrfContext* ctx, mjrfScene* scene,
ModelObjects* model_objects)
: ctx_(ctx), scene_(scene) {
: ctx_(ctx), scene_(scene), model_objects_(model_objects) {
const mjModel* model = model_objects->GetModel();
default_shadow_map_size_ = ReadElement(
model, "filament.shadows.map_size", default_shadow_map_size_);
@@ -89,7 +89,7 @@ LightManager::LightManager(mjrfContext* ctx, mjrfScene* scene,
fallback_environment_light_intensity_ =
ReadElement(model, "filament.fallback.environment_light_intensity",
fallback_environment_light_intensity_);
Prepare(model_objects);
Prepare();
}
LightManager::~LightManager() {
@@ -103,8 +103,8 @@ LightManager::~LightManager() {
fallback_ibl_.reset();
}
void LightManager::Prepare(ModelObjects* model_objects) {
const mjModel* model = model_objects->GetModel();
void LightManager::Prepare() {
const mjModel* model = model_objects_->GetModel();
bool has_image_based_light = false;
float total_light_intensity = 0.0f;
@@ -115,7 +115,7 @@ void LightManager::Prepare(ModelObjects* model_objects) {
mjrfLightParams params;
mjrf_defaultLightParams(&params);
params.type = mjLIGHT_IMAGE;
params.texture = model_objects->GetTexture(model->light_texid[i]);
params.texture = model_objects_->GetTexture(model->light_texid[i]);
params.intensity = model->light_intensity[i];
auto light_obj = CreateLight(ctx_, params);
mjrf_addLightToScene(scene_, light_obj.get());
@@ -201,7 +201,29 @@ void LightManager::Prepare(ModelObjects* model_objects) {
}
}
mjrf_setSceneSkybox(scene_, model_objects->GetSkyboxTexture());
mjrf_setSceneSkybox(scene_, model_objects_->GetSkyboxTexture());
}
void LightManager::Update(const mjData* data) {
const mjModel* model = model_objects_->GetModel();
for (int i = 0; i <= model->nlight; ++i) {
// Light with index nlight is the headlight.
mjrfLight* light = lights_[i].get();
if (i == model->nlight) {
const float3 color = ReadFloat3(model->vis.headlight.diffuse);
mjrf_setLightColor(light, color.v);
mjrf_setLightEnabled(light, model->vis.headlight.active);
} else {
const float3 pos = ReadFloat3(data->light_xpos, i);
const float3 dir = ReadFloat3(data->light_xdir, i);
mjrf_setLightTransform(light, pos.v, dir.v);
const float3 color = ReadFloat3(model->light_diffuse, i);
mjrf_setLightColor(light, color.v);
mjrf_setLightEnabled(light, model->light_active[i]);
}
}
}
mjrfLight* LightManager::GetLight(int index) {
+7 -1
View File
@@ -17,6 +17,7 @@
#include <vector>
#include <mujoco/mujoco.h>
#include "render/filament/mjrfilament.h"
#include "render/filament/mjrfilament_cpp.h"
#include "render/filament/support/model_objects.h"
@@ -29,6 +30,9 @@ class LightManager {
LightManager(mjrfContext* ctx, mjrfScene* scene, ModelObjects* model_objects);
~LightManager();
// Updates the state of the lights in the scene.
void Update(const mjData* data);
// Returns the light with the given index in the mjModel. Note that an extra
// headlight is assigned of the index `nlight`.
mjrfLight* GetLight(int index);
@@ -37,10 +41,12 @@ class LightManager {
LightManager& operator=(const LightManager&) = delete;
private:
void Prepare(ModelObjects* model_objects);
void Prepare();
mjrfContext* ctx_ = nullptr;
mjrfScene* scene_ = nullptr;
ModelObjects* model_objects_ = nullptr;
UniquePtr<mjrfLight> fallback_ibl_{nullptr, nullptr};
UniquePtr<mjrfTexture> fallback_ibl_texture_{nullptr, nullptr};
std::vector<UniquePtr<mjrfLight>> lights_;