From 81adeaae53909cad9a7e38d3d02809fb6f743ec0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tur=C3=A1nszki=20J=C3=A1nos?= Date: Thu, 2 May 2024 07:43:00 +0200 Subject: [PATCH] FBX importer: override file callbacks to correctly handle UWP --- Editor/ContentBrowserWindow.cpp | 12 ++++---- Editor/Editor.h | 2 +- Editor/ModelImporter_FBX.cpp | 51 +++++++++++++++++++++++++++++---- 3 files changed, 52 insertions(+), 13 deletions(-) diff --git a/Editor/ContentBrowserWindow.cpp b/Editor/ContentBrowserWindow.cpp index 01ccbdd232..02224d4c1e 100644 --- a/Editor/ContentBrowserWindow.cpp +++ b/Editor/ContentBrowserWindow.cpp @@ -333,12 +333,12 @@ void ContentBrowserWindow::SetSelection(SELECTION selection) size_t i = selection - SELECTION_RECENTFOLDER_BEGIN; i = std::min(i, editor->recentFolders.size() - 1); const std::string& folder = editor->recentFolders[editor->recentFolders.size() - 1 - i]; - AddItems(folder + "/", "wiscene", ICON_OBJECT); - AddItems(folder + "/", "vrm", ICON_HUMANOID); - AddItems(folder + "/", "gltf", ICON_OBJECT); - AddItems(folder + "/", "glb", ICON_OBJECT); - AddItems(folder + "/", "fbx", ICON_OBJECT); - AddItems(folder + "/", "obj", ICON_OBJECT); + AddItems(folder, "wiscene", ICON_OBJECT); + AddItems(folder, "vrm", ICON_HUMANOID); + AddItems(folder, "gltf", ICON_OBJECT); + AddItems(folder, "glb", ICON_OBJECT); + AddItems(folder, "fbx", ICON_OBJECT); + AddItems(folder, "obj", ICON_OBJECT); openFolderButton.OnClick([this, folder](wi::gui::EventArgs args) { wi::helper::OpenUrl(folder); }); diff --git a/Editor/Editor.h b/Editor/Editor.h index e3f2b9571e..72a5a6bb87 100644 --- a/Editor/Editor.h +++ b/Editor/Editor.h @@ -142,7 +142,7 @@ class EditorComponent : public wi::RenderPath2D wi::vector recentFilenames; size_t maxRecentFilenames = 10; wi::vector recentFolders; - size_t maxRecentFolders = 5; + size_t maxRecentFolders = 8; void RegisterRecentlyUsed(const std::string& filename); void Open(std::string filename); diff --git a/Editor/ModelImporter_FBX.cpp b/Editor/ModelImporter_FBX.cpp index 1911737629..2f20d686b7 100644 --- a/Editor/ModelImporter_FBX.cpp +++ b/Editor/ModelImporter_FBX.cpp @@ -13,6 +13,49 @@ using namespace wi::ecs; void Import_Mixamo_Bone(Scene& scene, Entity armatureEntity, Entity boneEntity); +// File callbacks are implemented for platforms that don't support default c++ filesystem correctly (like UWP) +struct FileDataStream +{ + wi::vector data; + size_t offset = 0; +}; +size_t read_cb(void* user, void* data, size_t size) +{ + FileDataStream* datastream = (FileDataStream*)user; + uint8_t* src = datastream->data.data() + datastream->offset; + size = std::min(size, datastream->data.size() - datastream->offset); + std::memcpy(data, src, size); + datastream->offset += size; + return size; +} +bool skip_cb(void* user, size_t size) +{ + FileDataStream* datastream = (FileDataStream*)user; + size = std::min(size, datastream->data.size() - datastream->offset); + datastream->offset += size; + return true; +} +void close_cb(void* user) +{ + FileDataStream* datastream = (FileDataStream*)user; + delete datastream; +} +bool open_file_cb(void* user, ufbx_stream* stream, const char* path, size_t path_len, const ufbx_open_file_info* info) +{ + wi::vector filedata; + if (wi::helper::FileRead(path, filedata)) + { + FileDataStream* datastream = new FileDataStream; + datastream->data = std::move(filedata); + stream->user = datastream; + stream->read_fn = read_cb; + stream->skip_fn = skip_cb; + stream->close_fn = close_cb; + return true; + } + return false; +} + void ImportModel_FBX(const std::string& filename, wi::scene::Scene& scene) { ufbx_load_opts opts = {}; @@ -23,6 +66,8 @@ void ImportModel_FBX(const std::string& filename, wi::scene::Scene& scene) opts.space_conversion = UFBX_SPACE_CONVERSION_TRANSFORM_ROOT; //opts.space_conversion = UFBX_SPACE_CONVERSION_ADJUST_TRANSFORMS; //opts.space_conversion = UFBX_SPACE_CONVERSION_MODIFY_GEOMETRY; + opts.generate_missing_normals = true; + opts.open_file_cb.fn = open_file_cb; ufbx_error error = {}; ufbx_scene* fbxscene = ufbx_load_file(filename.c_str(), &opts, &error); if (fbxscene == nullptr) @@ -383,12 +428,6 @@ void ImportModel_FBX(const std::string& filename, wi::scene::Scene& scene) } } - if (meshcomponent.vertex_normals.empty()) - { - meshcomponent.vertex_normals.resize(meshcomponent.vertex_positions.size()); - meshcomponent.ComputeNormals(MeshComponent::COMPUTE_NORMALS_SMOOTH_FAST); - } - meshcomponent.CreateRenderData(); }