Skip to content

Commit

Permalink
FBX importer: override file callbacks to correctly handle UWP
Browse files Browse the repository at this point in the history
  • Loading branch information
turanszkij committed May 2, 2024
1 parent fc592a2 commit 81adeaa
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 13 deletions.
12 changes: 6 additions & 6 deletions Editor/ContentBrowserWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand Down
2 changes: 1 addition & 1 deletion Editor/Editor.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ class EditorComponent : public wi::RenderPath2D
wi::vector<std::string> recentFilenames;
size_t maxRecentFilenames = 10;
wi::vector<std::string> recentFolders;
size_t maxRecentFolders = 5;
size_t maxRecentFolders = 8;
void RegisterRecentlyUsed(const std::string& filename);

void Open(std::string filename);
Expand Down
51 changes: 45 additions & 6 deletions Editor/ModelImporter_FBX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint8_t> 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<uint8_t> 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 = {};
Expand All @@ -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)
Expand Down Expand Up @@ -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();
}

Expand Down

0 comments on commit 81adeaa

Please sign in to comment.