126 lines
4.0 KiB
C++
126 lines
4.0 KiB
C++
|
|
#include <gtest/gtest.h>
|
||
|
|
|
||
|
|
#include <XCEngine/Components/GameObject.h>
|
||
|
|
#include <XCEngine/Components/MeshFilterComponent.h>
|
||
|
|
#include <XCEngine/Components/MeshRendererComponent.h>
|
||
|
|
#include <XCEngine/Core/Asset/IResource.h>
|
||
|
|
#include <XCEngine/Resources/Material/Material.h>
|
||
|
|
#include <XCEngine/Resources/Mesh/Mesh.h>
|
||
|
|
|
||
|
|
#include <sstream>
|
||
|
|
|
||
|
|
using namespace XCEngine::Components;
|
||
|
|
using namespace XCEngine::Resources;
|
||
|
|
|
||
|
|
namespace {
|
||
|
|
|
||
|
|
Mesh* CreateTestMesh(const char* name, const char* path) {
|
||
|
|
auto* mesh = new Mesh();
|
||
|
|
IResource::ConstructParams params = {};
|
||
|
|
params.name = name;
|
||
|
|
params.path = path;
|
||
|
|
params.guid = ResourceGUID::Generate(path);
|
||
|
|
mesh->Initialize(params);
|
||
|
|
return mesh;
|
||
|
|
}
|
||
|
|
|
||
|
|
Material* CreateTestMaterial(const char* name, const char* path) {
|
||
|
|
auto* material = new Material();
|
||
|
|
IResource::ConstructParams params = {};
|
||
|
|
params.name = name;
|
||
|
|
params.path = path;
|
||
|
|
params.guid = ResourceGUID::Generate(path);
|
||
|
|
material->Initialize(params);
|
||
|
|
return material;
|
||
|
|
}
|
||
|
|
|
||
|
|
TEST(MeshFilterComponent_Test, SetMeshCachesResourceAndPath) {
|
||
|
|
GameObject gameObject("MeshHolder");
|
||
|
|
auto* component = gameObject.AddComponent<MeshFilterComponent>();
|
||
|
|
Mesh* mesh = CreateTestMesh("Quad", "Meshes/quad.mesh");
|
||
|
|
|
||
|
|
component->SetMesh(mesh);
|
||
|
|
|
||
|
|
EXPECT_EQ(component->GetMesh(), mesh);
|
||
|
|
EXPECT_EQ(component->GetMeshPath(), "Meshes/quad.mesh");
|
||
|
|
|
||
|
|
component->ClearMesh();
|
||
|
|
delete mesh;
|
||
|
|
}
|
||
|
|
|
||
|
|
TEST(MeshFilterComponent_Test, SerializeAndDeserializePreservesPath) {
|
||
|
|
MeshFilterComponent source;
|
||
|
|
Mesh* mesh = CreateTestMesh("Quad", "Meshes/serialized.mesh");
|
||
|
|
source.SetMesh(mesh);
|
||
|
|
|
||
|
|
std::stringstream stream;
|
||
|
|
source.Serialize(stream);
|
||
|
|
|
||
|
|
MeshFilterComponent target;
|
||
|
|
target.Deserialize(stream);
|
||
|
|
|
||
|
|
EXPECT_EQ(target.GetMeshPath(), "Meshes/serialized.mesh");
|
||
|
|
EXPECT_EQ(target.GetMesh(), nullptr);
|
||
|
|
|
||
|
|
source.ClearMesh();
|
||
|
|
delete mesh;
|
||
|
|
}
|
||
|
|
|
||
|
|
TEST(MeshRendererComponent_Test, SetMaterialsKeepsSlotsAndFlags) {
|
||
|
|
GameObject gameObject("RendererHolder");
|
||
|
|
auto* component = gameObject.AddComponent<MeshRendererComponent>();
|
||
|
|
Material* material0 = CreateTestMaterial("M0", "Materials/m0.mat");
|
||
|
|
Material* material1 = CreateTestMaterial("M1", "Materials/m1.mat");
|
||
|
|
|
||
|
|
component->SetMaterial(0, material0);
|
||
|
|
component->SetMaterial(1, material1);
|
||
|
|
component->SetCastShadows(false);
|
||
|
|
component->SetReceiveShadows(false);
|
||
|
|
component->SetRenderLayer(7);
|
||
|
|
|
||
|
|
ASSERT_EQ(component->GetMaterialCount(), 2u);
|
||
|
|
EXPECT_EQ(component->GetMaterial(0), material0);
|
||
|
|
EXPECT_EQ(component->GetMaterial(1), material1);
|
||
|
|
EXPECT_EQ(component->GetMaterialPaths()[0], "Materials/m0.mat");
|
||
|
|
EXPECT_EQ(component->GetMaterialPaths()[1], "Materials/m1.mat");
|
||
|
|
EXPECT_FALSE(component->GetCastShadows());
|
||
|
|
EXPECT_FALSE(component->GetReceiveShadows());
|
||
|
|
EXPECT_EQ(component->GetRenderLayer(), 7u);
|
||
|
|
|
||
|
|
component->ClearMaterials();
|
||
|
|
delete material0;
|
||
|
|
delete material1;
|
||
|
|
}
|
||
|
|
|
||
|
|
TEST(MeshRendererComponent_Test, SerializeAndDeserializePreservesMaterialPathsAndSettings) {
|
||
|
|
MeshRendererComponent source;
|
||
|
|
Material* material0 = CreateTestMaterial("M0", "Materials/serialized0.mat");
|
||
|
|
Material* material1 = CreateTestMaterial("M1", "Materials/serialized1.mat");
|
||
|
|
source.SetMaterial(0, material0);
|
||
|
|
source.SetMaterial(1, material1);
|
||
|
|
source.SetCastShadows(false);
|
||
|
|
source.SetReceiveShadows(true);
|
||
|
|
source.SetRenderLayer(3);
|
||
|
|
|
||
|
|
std::stringstream stream;
|
||
|
|
source.Serialize(stream);
|
||
|
|
|
||
|
|
MeshRendererComponent target;
|
||
|
|
target.Deserialize(stream);
|
||
|
|
|
||
|
|
ASSERT_EQ(target.GetMaterialCount(), 2u);
|
||
|
|
EXPECT_EQ(target.GetMaterial(0), nullptr);
|
||
|
|
EXPECT_EQ(target.GetMaterial(1), nullptr);
|
||
|
|
EXPECT_EQ(target.GetMaterialPaths()[0], "Materials/serialized0.mat");
|
||
|
|
EXPECT_EQ(target.GetMaterialPaths()[1], "Materials/serialized1.mat");
|
||
|
|
EXPECT_FALSE(target.GetCastShadows());
|
||
|
|
EXPECT_TRUE(target.GetReceiveShadows());
|
||
|
|
EXPECT_EQ(target.GetRenderLayer(), 3u);
|
||
|
|
|
||
|
|
source.ClearMaterials();
|
||
|
|
delete material0;
|
||
|
|
delete material1;
|
||
|
|
}
|
||
|
|
|
||
|
|
} // namespace
|