Skip to content

Commit

Permalink
model importer - refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
nem0 committed Dec 15, 2024
1 parent 9d957a9 commit 90f6b5e
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 49 deletions.
1 change: 0 additions & 1 deletion src/editor/file_system_watcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ struct LUMIX_EDITOR_API FileSystemWatcher
virtual ~FileSystemWatcher() {}

static UniquePtr<FileSystemWatcher> create(const char* path, struct IAllocator& allocator);
// TODO on windows, this always returns lower case path
virtual Delegate<void (const char*)>& getCallback() = 0;
};

Expand Down
28 changes: 1 addition & 27 deletions src/renderer/editor/fbx_importer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,7 @@ struct FBXImporter : ModelImporter {
}
});

postprocessCommon(meta);
postprocessCommon(meta, path);
}

void insertHierarchy(const ofbx::Object* node) {
Expand Down Expand Up @@ -1235,10 +1235,7 @@ struct FBXImporter : ModelImporter {
mat.name = name;
}

// TODO move this to modelimporter?
// gather textures
// we don't support dds, but try it as last option, so user can get error message with filepath
const char* exts[] = { "png", "jpg", "jpeg", "tga", "bmp", "dds" };
FileSystem& filesystem = m_app.getEngine().getFileSystem();
for (u32 i = 0, num_mats = (u32)m_materials.size(); i < num_mats; ++i) {
ImportMaterial& mat = m_materials[i];
Expand All @@ -1250,29 +1247,6 @@ struct FBXImporter : ModelImporter {
ofbx::DataView filename = texture->getRelativeFileName();
if (filename == "") filename = texture->getFileName();
tex.path = toStringView(filename);
tex.src = tex.path;
tex.import = filesystem.fileExists(tex.src);

StringView tex_ext = Path::getExtension(tex.path);
if (!tex.import && (equalStrings(tex_ext, "dds") || !findTexture(src_dir, tex_ext, tex))) {
for (const char*& ext : exts) {
if (findTexture(src_dir, ext, tex)) {
// we assume all texture have the same extension,
// so we move it to the beginning, so it's checked first
swap(ext, exts[0]);
break;
}
}
}

Path::normalize(tex.src.data);

if (!tex.import) {
logInfo(fbx_filename, ": texture ", tex.src, " not found");
tex.src = "";
}

tex.import = true;
};

gatherTexture(ofbx::Texture::DIFFUSE);
Expand Down
65 changes: 48 additions & 17 deletions src/renderer/editor/model_importer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,40 @@ static i32 getAttributeOffset(const ModelImporter::ImportGeometry& mesh, Attribu
return -1;
}

void ModelImporter::postprocessCommon(const ModelMeta& meta) {
void ModelImporter::postprocessCommon(const ModelMeta& meta, StringView src_filepath) {
StringView src_dir = Path::getDir(src_filepath);
FileSystem& filesystem = m_app.getEngine().getFileSystem();

for (ImportMaterial& mat : m_materials) {
// we don't support dds, but try it as last option, so user can get error message with filepath
const char* exts[] = { "png", "jpg", "jpeg", "tga", "bmp", "dds" };
for (ImportTexture& tex : mat.textures) {
if (tex.path.empty()) continue;
tex.src = tex.path;

const bool exists = filesystem.fileExists(tex.src);
StringView tex_ext = Path::getExtension(tex.path);

if (!exists && (equalStrings(tex_ext, "dds") || !findTexture(src_dir, tex_ext, tex))) {
for (const char*& ext : exts) {
if (findTexture(src_dir, ext, tex)) {
// we assume all texture have the same extension,
// so we move it to the beginning, so it's checked first
swap(ext, exts[0]);
break;
}
}
}

if (tex.src.empty()) {
logInfo(src_filepath, ": texture ", tex.path, " not found");
continue;
}

Path::normalize(tex.src.data);
}
}

jobs::forEach(m_meshes.size(), 1, [&](i32 mesh_idx, i32){
// TODO this can process the same geom multiple times

Expand Down Expand Up @@ -588,10 +621,10 @@ bool ModelImporter::writeMaterials(const Path& src, const ModelMeta& meta, bool
blob.clear();

blob << "shader \"/shaders/standard.hlsl\"\n";
if (material.textures[2].import) blob << "uniform \"Metallic\", 1.000000\n";
if (!material.textures[2].src.empty()) blob << "uniform \"Metallic\", 1.000000\n";

auto writeTexture = [&](const ImportTexture& texture, u32 idx) {
if (texture.import && idx < 2) {
if (!texture.src.empty() && idx < 2) {
const Path meta_path(texture.src, ".meta");
if (!filesystem.fileExists(meta_path)) {
os::OutputFile file;
Expand All @@ -601,7 +634,7 @@ bool ModelImporter::writeMaterials(const Path& src, const ModelMeta& meta, bool
}
}
}
if (texture.import) {
if (!texture.src.empty()) {
blob << "texture \"/"
<< texture.src
<< "\"\n";
Expand All @@ -626,7 +659,7 @@ bool ModelImporter::writeMaterials(const Path& src, const ModelMeta& meta, bool
blob << "texture \"\"\n";
}

if (!material.textures[0].import && !meta.ignore_material_colors) {
if (material.textures[0].src.empty() && !meta.ignore_material_colors) {
const Vec3 color = material.diffuse_color;
blob << "uniform \"Material color\", {" << color.x
<< "," << color.y
Expand All @@ -648,20 +681,18 @@ bool ModelImporter::findTexture(StringView src_dir, StringView ext, ModelImporte
PathInfo file_info(tex.path);
tex.src = src_dir;
tex.src.append(file_info.basename, ".", ext);
tex.import = filesystem.fileExists(tex.src);
if (filesystem.fileExists(tex.src)) return true;

if (!tex.import) {
tex.src = src_dir;
tex.src.append(file_info.dir, "/", file_info.basename, ".", ext);
tex.import = filesystem.fileExists(tex.src);
tex.src = src_dir;
tex.src.append(file_info.dir, "/", file_info.basename, ".", ext);
if (filesystem.fileExists(tex.src)) return true;

if (!tex.import) {
tex.src = src_dir;
tex.src.append("textures/", file_info.basename, ".", ext);
tex.import = filesystem.fileExists(tex.src);
}
}
return tex.import;
tex.src = src_dir;
tex.src.append("textures/", file_info.basename, ".", ext);
if (filesystem.fileExists(tex.src)) return true;

tex.src = "";
return false;
}

void ModelImporter::writeImpostorVertices(float center_y, Vec2 bounding_cylinder) {
Expand Down
7 changes: 3 additions & 4 deletions src/renderer/editor/model_importer.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,8 @@ struct ModelImporter {
COUNT
};

bool import = true;
StringView path;
StaticString<MAX_PATH> src;
StringView path; // path as saved in source asset
StaticString<MAX_PATH> src; // path to actual file on disk
};

struct ImportAnimation {
Expand Down Expand Up @@ -160,7 +159,7 @@ struct ModelImporter {

// compute AO, auto LODs, etc.
// call this from parse when appropriate
void postprocessCommon(const ModelMeta& meta);
void postprocessCommon(const ModelMeta& meta, StringView src_dir);

StudioApp& m_app;
IAllocator& m_allocator;
Expand Down

0 comments on commit 90f6b5e

Please sign in to comment.