Import material textures with mesh assets

This commit is contained in:
2026-03-26 16:22:24 +08:00
parent c479595bf5
commit e174862b8a
18 changed files with 622 additions and 21 deletions

View File

@@ -1,5 +1,6 @@
#include <gtest/gtest.h>
#include <XCEngine/Resources/Mesh/Mesh.h>
#include <XCEngine/Resources/Material/Material.h>
#include <XCEngine/Core/Asset/ResourceTypes.h>
#include <XCEngine/Core/Asset/ResourceManager.h>
@@ -31,6 +32,11 @@ TEST(Mesh, GetSections) {
EXPECT_EQ(mesh.GetSections().Size(), 0u);
}
TEST(Mesh, GetMaterials) {
Mesh mesh;
EXPECT_EQ(mesh.GetMaterials().Size(), 0u);
}
TEST(Mesh, SetVertexAndIndexData) {
Mesh mesh;
@@ -71,6 +77,17 @@ TEST(Mesh, AddSection) {
EXPECT_EQ(mesh.GetSections()[0].materialID, 2u);
}
TEST(Mesh, AddMaterial) {
Mesh mesh;
auto* material = new Material();
mesh.AddMaterial(material);
ASSERT_EQ(mesh.GetMaterials().Size(), 1u);
EXPECT_EQ(mesh.GetMaterial(0), material);
EXPECT_GT(mesh.GetMemorySize(), 0u);
}
TEST(Mesh, SetBounds) {
Mesh mesh;

View File

@@ -10,7 +10,7 @@ namespace {
TEST(MeshImportSettings, DefaultConstructor) {
MeshImportSettings settings;
EXPECT_EQ(settings.GetImportFlags(), MeshImportFlags::None);
EXPECT_EQ(settings.GetImportFlags(), MeshImportFlags::ImportMaterials);
EXPECT_FLOAT_EQ(settings.GetScale(), 1.0f);
EXPECT_EQ(settings.GetOffset(), Vector3::Zero());
EXPECT_TRUE(settings.GetAxisConversion());

View File

@@ -1,6 +1,8 @@
#include <gtest/gtest.h>
#include <XCEngine/Resources/Mesh/MeshLoader.h>
#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 <filesystem>
@@ -102,4 +104,32 @@ TEST(MeshLoader, GeneratesNormalsAndTangentsWhenRequested) {
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;
}
} // namespace