Files
XCEngine/tests/Resources/Mesh/test_mesh_loader.cpp

249 lines
9.9 KiB
C++
Raw Normal View History

#include <gtest/gtest.h>
#include <XCEngine/Core/Asset/AssetDatabase.h>
2026-03-28 19:26:08 +08:00
#include <XCEngine/Core/Asset/ResourceManager.h>
#include <XCEngine/Resources/Mesh/MeshLoader.h>
2026-03-26 02:53:34 +08:00
#include <XCEngine/Resources/Mesh/MeshImportSettings.h>
#include <XCEngine/Resources/Material/Material.h>
#include <XCEngine/Resources/Texture/Texture.h>
#include <XCEngine/Core/Asset/ResourceTypes.h>
#include <XCEngine/Core/Containers/Array.h>
#include <chrono>
2026-03-26 02:53:34 +08:00
#include <filesystem>
#include <thread>
using namespace XCEngine::Resources;
using namespace XCEngine::Containers;
namespace {
2026-03-26 02:53:34 +08:00
std::string GetMeshFixturePath(const char* fileName) {
return (std::filesystem::path(XCENGINE_TEST_FIXTURES_DIR) / "Resources" / "Mesh" / fileName).string();
}
TEST(MeshLoader, GetResourceType) {
MeshLoader loader;
EXPECT_EQ(loader.GetResourceType(), ResourceType::Mesh);
}
TEST(MeshLoader, GetSupportedExtensions) {
MeshLoader loader;
auto extensions = loader.GetSupportedExtensions();
EXPECT_GE(extensions.Size(), 1u);
}
TEST(MeshLoader, CanLoad) {
MeshLoader loader;
EXPECT_TRUE(loader.CanLoad("test.obj"));
EXPECT_TRUE(loader.CanLoad("test.fbx"));
EXPECT_TRUE(loader.CanLoad("test.gltf"));
2026-03-26 02:53:34 +08:00
EXPECT_TRUE(loader.CanLoad("test.OBJ"));
EXPECT_FALSE(loader.CanLoad("test.txt"));
EXPECT_FALSE(loader.CanLoad("test.png"));
}
TEST(MeshLoader, LoadInvalidPath) {
MeshLoader loader;
LoadResult result = loader.Load("invalid/path/mesh.obj");
EXPECT_FALSE(result);
}
2026-03-28 19:26:08 +08:00
TEST(MeshLoader, ResourceManagerRegistersMeshAndTextureLoaders) {
ResourceManager& manager = ResourceManager::Get();
manager.Initialize();
EXPECT_NE(manager.GetLoader(ResourceType::Mesh), nullptr);
EXPECT_NE(manager.GetLoader(ResourceType::Texture), nullptr);
manager.Shutdown();
}
2026-03-26 02:53:34 +08:00
TEST(MeshLoader, LoadValidObjMesh) {
MeshLoader loader;
const std::string path = GetMeshFixturePath("triangle.obj");
LoadResult result = loader.Load(path.c_str());
ASSERT_TRUE(result);
ASSERT_NE(result.resource, nullptr);
auto* mesh = static_cast<Mesh*>(result.resource);
EXPECT_EQ(mesh->GetVertexCount(), 3u);
EXPECT_EQ(mesh->GetIndexCount(), 3u);
EXPECT_EQ(mesh->GetVertexStride(), sizeof(StaticMeshVertex));
EXPECT_FALSE(mesh->IsUse32BitIndex());
ASSERT_EQ(mesh->GetSections().Size(), 1u);
EXPECT_EQ(mesh->GetSections()[0].vertexCount, 3u);
EXPECT_EQ(mesh->GetSections()[0].indexCount, 3u);
EXPECT_TRUE(HasVertexAttribute(mesh->GetVertexAttributes(), VertexAttribute::Position));
EXPECT_TRUE(HasVertexAttribute(mesh->GetVertexAttributes(), VertexAttribute::Normal));
EXPECT_TRUE(HasVertexAttribute(mesh->GetVertexAttributes(), VertexAttribute::UV0));
2026-03-26 03:26:44 +08:00
EXPECT_EQ(mesh->GetBounds().GetMin(), XCEngine::Math::Vector3(0.0f, 0.0f, -1.0f));
EXPECT_EQ(mesh->GetBounds().GetMax(), XCEngine::Math::Vector3(1.0f, 1.0f, -1.0f));
EXPECT_EQ(mesh->GetSections()[0].bounds.GetMin(), XCEngine::Math::Vector3(0.0f, 0.0f, -1.0f));
EXPECT_EQ(mesh->GetSections()[0].bounds.GetMax(), XCEngine::Math::Vector3(1.0f, 1.0f, -1.0f));
2026-03-26 02:53:34 +08:00
const auto* vertices = static_cast<const StaticMeshVertex*>(mesh->GetVertexData());
ASSERT_NE(vertices, nullptr);
EXPECT_FLOAT_EQ(vertices[0].position.x, 0.0f);
EXPECT_FLOAT_EQ(vertices[0].position.y, 0.0f);
EXPECT_FLOAT_EQ(vertices[0].position.z, -1.0f);
EXPECT_FLOAT_EQ(vertices[0].normal.z, -1.0f);
EXPECT_FLOAT_EQ(vertices[1].uv0.x, 1.0f);
EXPECT_FLOAT_EQ(vertices[2].uv0.y, 1.0f);
delete mesh;
}
TEST(MeshLoader, GeneratesNormalsAndTangentsWhenRequested) {
MeshLoader loader;
MeshImportSettings settings;
settings.AddImportFlag(MeshImportFlags::GenerateNormals);
settings.AddImportFlag(MeshImportFlags::GenerateTangents);
const std::string path = GetMeshFixturePath("triangle_no_normals.obj");
LoadResult result = loader.Load(path.c_str(), &settings);
ASSERT_TRUE(result);
ASSERT_NE(result.resource, nullptr);
auto* mesh = static_cast<Mesh*>(result.resource);
EXPECT_TRUE(HasVertexAttribute(mesh->GetVertexAttributes(), VertexAttribute::Normal));
EXPECT_TRUE(HasVertexAttribute(mesh->GetVertexAttributes(), VertexAttribute::Tangent));
EXPECT_TRUE(HasVertexAttribute(mesh->GetVertexAttributes(), VertexAttribute::Bitangent));
const auto* vertices = static_cast<const StaticMeshVertex*>(mesh->GetVertexData());
ASSERT_NE(vertices, nullptr);
EXPECT_GT(vertices[0].normal.Magnitude(), 0.0f);
EXPECT_GT(vertices[0].tangent.Magnitude(), 0.0f);
EXPECT_GT(vertices[0].bitangent.Magnitude(), 0.0f);
delete mesh;
}
TEST(MeshLoader, ImportsMaterialTexturesFromObj) {
MeshLoader loader;
const std::string path = GetMeshFixturePath("textured_triangle.obj");
LoadResult result = loader.Load(path.c_str());
ASSERT_TRUE(result);
ASSERT_NE(result.resource, nullptr);
auto* mesh = static_cast<Mesh*>(result.resource);
ASSERT_EQ(mesh->GetSections().Size(), 1u);
ASSERT_GE(mesh->GetMaterials().Size(), 1u);
EXPECT_LT(mesh->GetSections()[0].materialID, mesh->GetMaterials().Size());
Material* material = mesh->GetMaterial(mesh->GetSections()[0].materialID);
ASSERT_NE(material, nullptr);
EXPECT_TRUE(material->HasProperty("baseColorTexture"));
EXPECT_EQ(material->GetTextureBindingCount(), 1u);
ResourceHandle<Texture> diffuseTexture = material->GetTexture("baseColorTexture");
ASSERT_TRUE(diffuseTexture.IsValid());
EXPECT_EQ(diffuseTexture->GetWidth(), 2u);
EXPECT_EQ(diffuseTexture->GetHeight(), 2u);
EXPECT_EQ(diffuseTexture->GetPixelDataSize(), 16u);
EXPECT_EQ(mesh->GetTextures().Size(), 1u);
delete mesh;
}
TEST(MeshLoader, AssetDatabaseCreatesModelArtifactAndReusesItWithoutReimport) {
namespace fs = std::filesystem;
using namespace std::chrono_literals;
const fs::path projectRoot = fs::temp_directory_path() / "xc_mesh_library_cache_test";
const fs::path assetsDir = projectRoot / "Assets";
fs::remove_all(projectRoot);
fs::create_directories(assetsDir);
fs::copy_file(GetMeshFixturePath("textured_triangle.obj"),
assetsDir / "textured_triangle.obj",
fs::copy_options::overwrite_existing);
fs::copy_file(GetMeshFixturePath("textured_triangle.mtl"),
assetsDir / "textured_triangle.mtl",
fs::copy_options::overwrite_existing);
fs::copy_file(GetMeshFixturePath("checker.bmp"),
assetsDir / "checker.bmp",
fs::copy_options::overwrite_existing);
AssetDatabase database;
database.Initialize(projectRoot.string().c_str());
AssetDatabase::ResolvedAsset firstResolve;
ASSERT_TRUE(database.EnsureArtifact("Assets/textured_triangle.obj", ResourceType::Mesh, firstResolve));
ASSERT_TRUE(firstResolve.exists);
ASSERT_TRUE(firstResolve.artifactReady);
EXPECT_TRUE(fs::exists(projectRoot / "Assets" / "textured_triangle.obj.meta"));
EXPECT_TRUE(fs::exists(projectRoot / "Library" / "SourceAssetDB" / "assets.db"));
EXPECT_TRUE(fs::exists(projectRoot / "Library" / "ArtifactDB" / "artifacts.db"));
EXPECT_TRUE(fs::exists(firstResolve.artifactMainPath.CStr()));
EXPECT_TRUE(fs::exists((fs::path(firstResolve.artifactDirectory.CStr()) / "texture_0.xctex")));
AssetRef assetRef;
ASSERT_TRUE(database.TryGetAssetRef("Assets/textured_triangle.obj", ResourceType::Mesh, assetRef));
EXPECT_TRUE(assetRef.IsValid());
const auto originalArtifactWriteTime = fs::last_write_time(firstResolve.artifactMainPath.CStr());
std::this_thread::sleep_for(50ms);
AssetDatabase::ResolvedAsset secondResolve;
ASSERT_TRUE(database.EnsureArtifact("Assets/textured_triangle.obj", ResourceType::Mesh, secondResolve));
EXPECT_EQ(firstResolve.artifactMainPath, secondResolve.artifactMainPath);
EXPECT_EQ(originalArtifactWriteTime, fs::last_write_time(secondResolve.artifactMainPath.CStr()));
database.Shutdown();
fs::remove_all(projectRoot);
}
TEST(MeshLoader, ResourceManagerLoadsModelByAssetRefFromProjectAssets) {
namespace fs = std::filesystem;
ResourceManager& manager = ResourceManager::Get();
manager.Initialize();
const fs::path projectRoot = fs::temp_directory_path() / "xc_mesh_asset_ref_test";
const fs::path assetsDir = projectRoot / "Assets";
fs::remove_all(projectRoot);
fs::create_directories(assetsDir);
fs::copy_file(GetMeshFixturePath("textured_triangle.obj"),
assetsDir / "textured_triangle.obj",
fs::copy_options::overwrite_existing);
fs::copy_file(GetMeshFixturePath("textured_triangle.mtl"),
assetsDir / "textured_triangle.mtl",
fs::copy_options::overwrite_existing);
fs::copy_file(GetMeshFixturePath("checker.bmp"),
assetsDir / "checker.bmp",
fs::copy_options::overwrite_existing);
manager.SetResourceRoot(projectRoot.string().c_str());
const auto firstHandle = manager.Load<Mesh>("Assets/textured_triangle.obj");
ASSERT_TRUE(firstHandle.IsValid());
EXPECT_EQ(firstHandle->GetVertexCount(), 3u);
EXPECT_EQ(firstHandle->GetIndexCount(), 3u);
EXPECT_GE(firstHandle->GetMaterials().Size(), 1u);
EXPECT_EQ(firstHandle->GetTextures().Size(), 1u);
const auto initialMaterialCount = firstHandle->GetMaterials().Size();
AssetRef assetRef;
ASSERT_TRUE(manager.TryGetAssetRef("Assets/textured_triangle.obj", ResourceType::Mesh, assetRef));
EXPECT_TRUE(assetRef.IsValid());
manager.UnloadAll();
const auto secondHandle = manager.Load<Mesh>(assetRef);
ASSERT_TRUE(secondHandle.IsValid());
EXPECT_EQ(secondHandle->GetPath(), "Assets/textured_triangle.obj");
EXPECT_EQ(secondHandle->GetVertexCount(), 3u);
EXPECT_EQ(secondHandle->GetIndexCount(), 3u);
EXPECT_EQ(secondHandle->GetMaterials().Size(), initialMaterialCount);
EXPECT_EQ(secondHandle->GetTextures().Size(), 1u);
manager.SetResourceRoot("");
manager.Shutdown();
fs::remove_all(projectRoot);
}
} // namespace