Remove dependency between SceneBridge and ObjectManager.
PiperOrigin-RevId: 911824153 Change-Id: Ie87d6d9e77b0b3f68a41a0c7d13a36fb2029e4c2
This commit is contained in:
committed by
Copybara-Service
parent
8e214e5133
commit
607970a900
@@ -16,6 +16,7 @@
|
||||
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <utility>
|
||||
|
||||
@@ -28,8 +29,6 @@
|
||||
#include <mujoco/mujoco.h>
|
||||
#include "experimental/filament/compat/model_objects.h"
|
||||
#include "experimental/filament/compat/scene_geom_util.h"
|
||||
#include "experimental/filament/filament/filament_context.h"
|
||||
#include "experimental/filament/filament/object_manager.h"
|
||||
#include "experimental/filament/filament/math_util.h"
|
||||
#include "experimental/filament/filament/model_util.h"
|
||||
#include "experimental/filament/render_context_filament.h"
|
||||
@@ -44,9 +43,17 @@ using filament::math::mat4;
|
||||
|
||||
static UniquePtr<mjrTexture> CreateFallbackIndirectLightTexture(
|
||||
mjrfContext* ctx) {
|
||||
std::unique_ptr<ObjectManager::Asset> asset =
|
||||
FilamentContext::downcast(ctx)->GetObjectManager()->LoadAsset(
|
||||
ObjectManager::kDefaultEnvironmentLight);
|
||||
const std::string filename = ResolveFilamentAssetPath("ibl.ktx");
|
||||
mjResource* resource =
|
||||
mju_openResource("", filename.c_str(), nullptr, nullptr, 0);
|
||||
if (!resource) {
|
||||
mju_error("Failed to open resource: %s", filename.c_str());
|
||||
}
|
||||
const void* bytes = nullptr;
|
||||
const int nbytes = mju_readResource(resource, &bytes);
|
||||
if (bytes == nullptr || nbytes <= 0) {
|
||||
mju_error("Failed to read resource: %s", filename.c_str());
|
||||
}
|
||||
|
||||
mjrTextureConfig config;
|
||||
mjr_defaultTextureConfig(&config);
|
||||
@@ -60,12 +67,12 @@ static UniquePtr<mjrTexture> CreateFallbackIndirectLightTexture(
|
||||
|
||||
mjrTextureData payload;
|
||||
mjr_defaultTextureData(&payload);
|
||||
payload.bytes = asset->GetBytes().data();
|
||||
payload.nbytes = asset->GetBytes().size();
|
||||
payload.bytes = bytes;
|
||||
payload.nbytes = nbytes;
|
||||
payload.release_callback = +[](void* user_data) {
|
||||
delete static_cast<ObjectManager::Asset*>(user_data);
|
||||
mju_closeResource((mjResource*)user_data);
|
||||
};
|
||||
payload.user_data = asset.release();
|
||||
payload.user_data = resource;
|
||||
|
||||
mjrf_setTextureData(texture.get(), &payload);
|
||||
return texture;
|
||||
|
||||
@@ -33,55 +33,45 @@
|
||||
|
||||
namespace mujoco {
|
||||
|
||||
static std::string GetAssetPath(std::string_view filename) {
|
||||
std::string path = "filament:" + std::string(filename);
|
||||
std::string ResolveFilamentAssetPath(const std::string& filename) {
|
||||
std::string path = "filament:" + filename;
|
||||
return path;
|
||||
}
|
||||
|
||||
ObjectManager::Asset::Asset(std::string_view filename) {
|
||||
std::string path = GetAssetPath(filename);
|
||||
resource = mju_openResource("", path.c_str(), nullptr, nullptr, 0);
|
||||
size = mju_readResource(resource, const_cast<const void**>(&payload));
|
||||
}
|
||||
|
||||
ObjectManager::Asset::~Asset() {
|
||||
if (resource) {
|
||||
mju_closeResource(resource);
|
||||
}
|
||||
}
|
||||
|
||||
std::span<const std::byte> ObjectManager::Asset::GetBytes() const {
|
||||
return {reinterpret_cast<const std::byte*>(payload), size};
|
||||
}
|
||||
static filament::Material* LoadMaterial(filament::Engine* engine,
|
||||
std::string_view filename) {
|
||||
const std::string path = ResolveFilamentAssetPath(std::string(filename));
|
||||
mjResource* resource = mju_openResource("", path.c_str(), nullptr, nullptr, 0);
|
||||
void* payload = nullptr;
|
||||
int size = mju_readResource(resource, const_cast<const void**>(&payload));
|
||||
filament::Material::Builder material_builder;
|
||||
material_builder.package(payload, size);
|
||||
filament::Material* material = material_builder.build(*engine);
|
||||
mju_closeResource(resource);
|
||||
return material;
|
||||
};
|
||||
|
||||
ObjectManager::ObjectManager(filament::Engine* engine)
|
||||
: engine_(engine) {
|
||||
auto LoadMaterial = [this](std::string_view filename) {
|
||||
Asset asset(filename);
|
||||
filament::Material::Builder material_builder;
|
||||
material_builder.package(asset.payload, asset.size);
|
||||
return material_builder.build(*this->engine_);
|
||||
};
|
||||
|
||||
materials_[kPbr] = LoadMaterial("pbr.filamat");
|
||||
materials_[kPbrPacked] = LoadMaterial("pbr_packed.filamat");
|
||||
materials_[kPhong2d] = LoadMaterial("phong_2d.filamat");
|
||||
materials_[kPhong2dFade] = LoadMaterial("phong_2d_fade.filamat");
|
||||
materials_[kPhong2dReflect] = LoadMaterial("phong_2d_reflect.filamat");
|
||||
materials_[kPhong2dUv] = LoadMaterial("phong_2d_uv.filamat");
|
||||
materials_[kPhong2dUvFade] = LoadMaterial("phong_2d_uv_fade.filamat");
|
||||
materials_[kPhong2dUvReflect] = LoadMaterial("phong_2d_uv_reflect.filamat");
|
||||
materials_[kPhongColor] = LoadMaterial("phong_color.filamat");
|
||||
materials_[kPhongColorFade] = LoadMaterial("phong_color_fade.filamat");
|
||||
materials_[kPhongColorReflect] = LoadMaterial("phong_color_reflect.filamat");
|
||||
materials_[kPhongCube] = LoadMaterial("phong_cube.filamat");
|
||||
materials_[kPhongCubeFade] = LoadMaterial("phong_cube_fade.filamat");
|
||||
materials_[kPhongCubeReflect] = LoadMaterial("phong_cube_reflect.filamat");
|
||||
materials_[kUnlitSegmentation] = LoadMaterial("unlit_segmentation.filamat");
|
||||
materials_[kUnlitLine] = LoadMaterial("unlit_line.filamat");
|
||||
materials_[kUnlitDecor] = LoadMaterial("unlit_decor.filamat");
|
||||
materials_[kUnlitDepth] = LoadMaterial("unlit_depth.filamat");
|
||||
materials_[kUnlitUi] = LoadMaterial("unlit_ui.filamat");
|
||||
materials_[kPbr] = LoadMaterial(engine, "pbr.filamat");
|
||||
materials_[kPbrPacked] = LoadMaterial(engine, "pbr_packed.filamat");
|
||||
materials_[kPhong2d] = LoadMaterial(engine, "phong_2d.filamat");
|
||||
materials_[kPhong2dFade] = LoadMaterial(engine, "phong_2d_fade.filamat");
|
||||
materials_[kPhong2dReflect] = LoadMaterial(engine, "phong_2d_reflect.filamat");
|
||||
materials_[kPhong2dUv] = LoadMaterial(engine, "phong_2d_uv.filamat");
|
||||
materials_[kPhong2dUvFade] = LoadMaterial(engine, "phong_2d_uv_fade.filamat");
|
||||
materials_[kPhong2dUvReflect] = LoadMaterial(engine, "phong_2d_uv_reflect.filamat");
|
||||
materials_[kPhongColor] = LoadMaterial(engine, "phong_color.filamat");
|
||||
materials_[kPhongColorFade] = LoadMaterial(engine, "phong_color_fade.filamat");
|
||||
materials_[kPhongColorReflect] = LoadMaterial(engine, "phong_color_reflect.filamat");
|
||||
materials_[kPhongCube] = LoadMaterial(engine, "phong_cube.filamat");
|
||||
materials_[kPhongCubeFade] = LoadMaterial(engine, "phong_cube_fade.filamat");
|
||||
materials_[kPhongCubeReflect] = LoadMaterial(engine, "phong_cube_reflect.filamat");
|
||||
materials_[kUnlitSegmentation] = LoadMaterial(engine, "unlit_segmentation.filamat");
|
||||
materials_[kUnlitLine] = LoadMaterial(engine, "unlit_line.filamat");
|
||||
materials_[kUnlitDecor] = LoadMaterial(engine, "unlit_decor.filamat");
|
||||
materials_[kUnlitDepth] = LoadMaterial(engine, "unlit_depth.filamat");
|
||||
materials_[kUnlitUi] = LoadMaterial(engine, "unlit_ui.filamat");
|
||||
|
||||
static uint8_t black_rgb[3] = {0, 0, 0};
|
||||
static uint8_t white_rgb[3] = {255, 255, 255};
|
||||
@@ -156,9 +146,4 @@ const filament::Texture* ObjectManager::GetFallbackTexture(
|
||||
}
|
||||
return fallback_textures_[role];
|
||||
}
|
||||
|
||||
std::unique_ptr<ObjectManager::Asset> ObjectManager::LoadAsset(
|
||||
std::string_view filename) {
|
||||
return std::unique_ptr<Asset>(new Asset(filename));
|
||||
}
|
||||
} // namespace mujoco
|
||||
|
||||
@@ -35,23 +35,6 @@ namespace mujoco {
|
||||
// Creates and owns various filament objects based on the data in a mjrContext.
|
||||
class ObjectManager {
|
||||
public:
|
||||
class Asset {
|
||||
public:
|
||||
~Asset();
|
||||
|
||||
std::span<const std::byte> GetBytes() const;
|
||||
|
||||
Asset(const Asset&) = delete;
|
||||
Asset& operator=(const Asset&) = delete;
|
||||
private:
|
||||
friend class ObjectManager;
|
||||
explicit Asset(std::string_view filename);
|
||||
|
||||
std::size_t size = 0;
|
||||
void* payload = nullptr;
|
||||
mjResource* resource = nullptr;
|
||||
};
|
||||
|
||||
ObjectManager(filament::Engine* engine);
|
||||
~ObjectManager();
|
||||
|
||||
@@ -90,9 +73,6 @@ class ObjectManager {
|
||||
// Returns the built-in mesh collection with the given parameters.
|
||||
Builtins* GetBuiltins(int nstack, int nslice, int nquad);
|
||||
|
||||
// Loads the given asset from the filament resource directory.
|
||||
std::unique_ptr<Asset> LoadAsset(std::string_view filename);
|
||||
|
||||
// The default environment light to use if no environment light is specified.
|
||||
static constexpr const char* kDefaultEnvironmentLight = "ibl.ktx";
|
||||
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
#define MUJOCO_SRC_EXPERIMENTAL_FILAMENT_RENDER_CONTEXT_FILAMENT_CPP_H_
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
#include "experimental/filament/render_context_filament.h"
|
||||
|
||||
@@ -66,6 +67,8 @@ inline UniquePtr<mjrRenderTarget> CreateRenderTarget(
|
||||
return UniquePtr<mjrRenderTarget>(render_target, mjrf_destroyRenderTarget);
|
||||
}
|
||||
|
||||
std::string ResolveFilamentAssetPath(const std::string& filename);
|
||||
|
||||
} // namespace mujoco
|
||||
|
||||
#endif // MUJOCO_SRC_EXPERIMENTAL_FILAMENT_RENDER_CONTEXT_FILAMENT_CPP_H_
|
||||
|
||||
Reference in New Issue
Block a user