Use mjResourceProvider instead of custom LoadAsset callbacks.

PiperOrigin-RevId: 853172824
Change-Id: I60ef1207611c4da119cc3f0a7819088cffca6e4f
This commit is contained in:
Haroon Qureshi
2026-01-07 03:02:06 -08:00
committed by Copybara-Service
parent 4830f5a79e
commit 9743c2d385
12 changed files with 142 additions and 167 deletions
+5 -6
View File
@@ -111,13 +111,12 @@ static constexpr std::array<const char*, 31> kPercentRealTime = {
};
// clang-format on
App::App(int width, int height, std::string ini_path,
const platform::LoadAssetFn& load_asset_fn)
: ini_path_(std::move(ini_path)), load_asset_fn_(load_asset_fn) {
App::App(int width, int height, std::string ini_path)
: ini_path_(std::move(ini_path)) {
window_ = std::make_unique<platform::Window>("MuJoCo Studio", width, height,
kWindowConfig, load_asset_fn);
renderer_ = std::make_unique<platform::Renderer>(
window_->GetNativeWindowHandle(), load_asset_fn);
kWindowConfig);
renderer_ =
std::make_unique<platform::Renderer>(window_->GetNativeWindowHandle());
ImPlot::CreateContext();
mjv_defaultPerturb(&perturb_);
+1 -3
View File
@@ -36,8 +36,7 @@ namespace mujoco::studio {
// Owns, updates, and renders a MuJoCo simulation.
class App {
public:
App(int width, int height, std::string ini_path,
const platform::LoadAssetFn& load_asset_fn);
App(int width, int height, std::string ini_path);
enum ContentType {
kFilepath, // Path to a model file.
@@ -168,7 +167,6 @@ class App {
std::unique_ptr<platform::Window> window_;
std::unique_ptr<platform::Renderer> renderer_;
platform::LoadAssetFn load_asset_fn_;
platform::StepControl step_control_;
platform::SimProfiler profiler_;
platform::SimHistory history_;
+60 -15
View File
@@ -15,43 +15,88 @@
// Main entry point for the Filament-based MuJoCo renderer.
#include <cstddef>
#include <cstdint>
#include <cstdlib>
#include <fstream>
#include <ios>
#include <iosfwd>
#include <optional>
#include <string>
#include <string_view>
#include <vector>
#include <absl/flags/flag.h>
#include <mujoco/mujoco.h>
#include "experimental/studio/app.h"
ABSL_FLAG(int, window_width, 1400, "Window width");
ABSL_FLAG(int, window_height, 720, "Window height");
ABSL_FLAG(std::string, model_file, "", "MuJoCo model file.");
static std::vector<std::byte> LoadAsset(std::string_view path) {
std::string fullpath = std::string("assets/") + std::string(path);
std::ifstream file(fullpath, std::ios::binary | std::ios::ate);
if (!file.is_open()) {
return {};
}
std::streampos file_size = file.tellg();
file.seekg(0, std::ios::beg);
std::vector<std::byte> buffer(file_size);
if (!file.read(reinterpret_cast<char*>(buffer.data()), file_size)) {
return {};
}
return buffer;
std::string Resolve(std::string_view path) {
return std::string("assets/") + std::string(path);
}
class FileResource {
public:
explicit FileResource(const std::string& path)
: file_(path, std::ios::binary | std::ios::ate) {
if (!file_.is_open()) {
mju_warning("Cannot open file %s", path.c_str());
return;
}
size_ = file_.tellg();
file_.seekg(0, std::ios::beg);
}
int Read(const void** buffer) {
buffer_.resize(size_);
if (!file_.read(reinterpret_cast<char*>(buffer_.data()), size_)) {
return 0;
}
*buffer = buffer_.data();
return size_;
}
int Size() const { return size_; }
FileResource(const FileResource&) = delete;
FileResource& operator=(const FileResource&) = delete;
private:
std::ifstream file_;
std::vector<char> buffer_;
int size_ = 0;
};
int main(int argc, char** argv, char** envp) {
const char* home = getenv("HOME");
const std::string ini_path = std::string(home ? home : ".") + "/.mujoco.ini";
mjpResourceProvider resource_provider;
resource_provider.open = [](mjResource* resource) {
const std::string resolved_path = Resolve(resource->name);
FileResource* f = new FileResource(resolved_path);
resource->data = f;
return f->Size();
};
resource_provider.read = [](mjResource* resource, const void** buffer) {
FileResource* f = static_cast<FileResource*>(resource->data);
return f->Read(buffer);
};
resource_provider.close = [](mjResource* resource) {
delete static_cast<FileResource*>(resource->data);
};
resource_provider.prefix = "font";
mjp_registerResourceProvider(&resource_provider);
resource_provider.prefix = "filament";
mjp_registerResourceProvider(&resource_provider);
const int width = absl::GetFlag(FLAGS_window_width);
const int height = absl::GetFlag(FLAGS_window_height);
mujoco::studio::App app(width, height, ini_path, LoadAsset);
mujoco::studio::App app(width, height, ini_path);
// If the model file is not specified, try to load it from the first argument
std::string model_file = absl::GetFlag(FLAGS_model_file);
+34 -21
View File
@@ -14,17 +14,17 @@
// Main entry point for the Filament-based MuJoCo web app.
#include <cstddef>
#include <string>
#include <string_view>
#include <unordered_map>
#include <vector>
#include <utility>
#include <emscripten.h>
#include <emscripten/bind.h>
#include <emscripten/val.h>
#include <filesystem>
#include <string>
#include <string_view>
#include <unordered_map>
#include <utility>
#include <mujoco/mujoco.h>
#include "experimental/studio/app.h"
// Global app instance. Lifetime is controlled by Init/Deinit calls which are
@@ -43,31 +43,25 @@ class AssetRegistry {
// Registers asset contents with the given filename.
void RegisterAsset(std::string filename, std::string contents) {
std::filesystem::path(filename).filename().string();
assets_[filename] = std::move(contents);
}
// Returns the contents of the given asset by name.
std::vector<std::byte> LoadAsset(std::string_view filename) {
if (auto it = assets_.find(std::string(filename)); it != assets_.end()) {
const std::byte* begin = reinterpret_cast<const std::byte*>(it->second.data());
const std::byte* end = begin + it->second.size();
return std::vector<std::byte>(begin, end);
}
return {};
const std::string& Get(std::string_view filename) const {
filename = filename.substr(filename.find_first_of(':') + 1);
static std::string empty;
auto it = assets_.find(std::string(filename));
return it != assets_.end() ? it->second : empty;
}
private:
std::unordered_map<std::string, std::string> assets_;
};
static std::vector<std::byte> LoadAsset(std::string_view filename) {
return AssetRegistry::Instance().LoadAsset(filename);
}
// Javascript-facing function to register an asset.
void RegisterAsset(std::string filename, std::string contents) {
AssetRegistry::Instance().RegisterAsset(std::move(filename),
std::move(contents));
std::move(contents));
}
// Javascript-facing function to initialize the app.
@@ -76,7 +70,26 @@ void Init() {
const int width = 100;
const int height = 100;
const std::string ini_path = "";
g_app = new mujoco::studio::App(width, height, ini_path, LoadAsset);
mjpResourceProvider resource_provider;
resource_provider.open = [](mjResource* resource) {
AssetRegistry& r = AssetRegistry::Instance();
return static_cast<int>(r.Get(resource->name).size());
};
resource_provider.read = [](mjResource* resource, const void** buffer) {
AssetRegistry& r = AssetRegistry::Instance();
const std::string& contents = r.Get(resource->name);
*buffer = contents.data();
return static_cast<int>(contents.size());
};
resource_provider.close = [](mjResource* resource) {};
resource_provider.prefix = "font";
mjp_registerResourceProvider(&resource_provider);
resource_provider.prefix = "filament";
mjp_registerResourceProvider(&resource_provider);
g_app = new mujoco::studio::App(width, height, ini_path);
g_app->LoadModel("", mujoco::studio::App::ContentType::kModelXml);
}