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

@@ -104,6 +104,18 @@ TEST(Material, SetGetTexture) {
EXPECT_EQ(material.GetTexture("uDiffuse").Get(), texture);
}
TEST(Material, SetTextureReplacesExistingBinding) {
Material material;
Texture* firstTexture = new Texture();
Texture* secondTexture = new Texture();
material.SetTexture("uDiffuse", ResourceHandle<Texture>(firstTexture));
material.SetTexture("uDiffuse", ResourceHandle<Texture>(secondTexture));
EXPECT_EQ(material.GetTextureBindingCount(), 1u);
EXPECT_EQ(material.GetTexture("uDiffuse").Get(), secondTexture);
}
TEST(Material, HasProperty) {
Material material;

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

View File

@@ -28,5 +28,9 @@ target_include_directories(texture_tests PRIVATE
${CMAKE_SOURCE_DIR}/tests/fixtures
)
target_compile_definitions(texture_tests PRIVATE
XCENGINE_TEST_FIXTURES_DIR="${CMAKE_SOURCE_DIR}/tests/fixtures"
)
include(GoogleTest)
gtest_discover_tests(texture_tests)

View File

@@ -2,12 +2,17 @@
#include <XCEngine/Resources/Texture/TextureLoader.h>
#include <XCEngine/Core/Asset/ResourceTypes.h>
#include <XCEngine/Core/Containers/Array.h>
#include <filesystem>
using namespace XCEngine::Resources;
using namespace XCEngine::Containers;
namespace {
std::string GetTextureFixturePath(const char* fileName) {
return (std::filesystem::path(XCENGINE_TEST_FIXTURES_DIR) / "Resources" / "Texture" / fileName).string();
}
TEST(TextureLoader, GetResourceType) {
TextureLoader loader;
EXPECT_EQ(loader.GetResourceType(), ResourceType::Texture);
@@ -34,4 +39,22 @@ TEST(TextureLoader, LoadInvalidPath) {
EXPECT_FALSE(result);
}
TEST(TextureLoader, LoadValidBmpTexture) {
TextureLoader loader;
const std::string path = GetTextureFixturePath("checker.bmp");
LoadResult result = loader.Load(path.c_str());
ASSERT_TRUE(result);
ASSERT_NE(result.resource, nullptr);
auto* texture = static_cast<Texture*>(result.resource);
EXPECT_EQ(texture->GetWidth(), 2u);
EXPECT_EQ(texture->GetHeight(), 2u);
EXPECT_EQ(texture->GetTextureType(), TextureType::Texture2D);
EXPECT_EQ(texture->GetFormat(), TextureFormat::RGBA8_UNORM);
EXPECT_EQ(texture->GetPixelDataSize(), 16u);
delete texture;
}
} // namespace

Binary file not shown.

After

Width:  |  Height:  |  Size: 69 B

View File

@@ -0,0 +1,7 @@
newmtl TestMaterial
Ka 1.000000 1.000000 1.000000
Kd 1.000000 1.000000 1.000000
Ks 0.000000 0.000000 0.000000
d 1.0
illum 2
map_Kd checker.bmp

View File

@@ -0,0 +1,11 @@
mtllib textured_triangle.mtl
o TexturedTriangle
v 0.0 0.0 1.0
v 1.0 0.0 1.0
v 0.0 1.0 1.0
vt 0.0 0.0
vt 1.0 0.0
vt 0.0 1.0
vn 0.0 0.0 1.0
usemtl TestMaterial
f 1/1/1 2/2/1 3/3/1

Binary file not shown.

After

Width:  |  Height:  |  Size: 69 B