refactor: reorganize Resources module into Core/Asset, Core/IO and Resources subdirectories
- Split core resource architecture into Core/Asset/ (IResource, ResourceManager, ResourceCache, etc.)
- Moved IO layer into Core/IO/ (IResourceLoader, ResourceFileSystem, etc.)
- Reorganized concrete resource types into Resources/{Texture,Mesh,Material,Shader,AudioClip}/
- Updated all include paths from relative to <XCEngine/...> format
- Fixed circular dependency in Material.h (removed unnecessary ResourceManager.h include)
- Fixed malformed include syntax in ResourceManager.h and AsyncLoader.h
- Fixed glad.h path issues in CMakeLists.txt
This commit is contained in:
174
engine/src/Resources/Material/Material.cpp
Normal file
174
engine/src/Resources/Material/Material.cpp
Normal file
@@ -0,0 +1,174 @@
|
||||
#include "Resources/Material.h"
|
||||
#include "Resources/Shader.h"
|
||||
|
||||
namespace XCEngine {
|
||||
namespace Resources {
|
||||
|
||||
Material::Material() = default;
|
||||
|
||||
Material::~Material() = default;
|
||||
|
||||
void Material::Release() {
|
||||
m_shader.Reset();
|
||||
m_properties.Clear();
|
||||
m_textureBindings.Clear();
|
||||
m_constantBufferData.Clear();
|
||||
m_isValid = false;
|
||||
}
|
||||
|
||||
void Material::SetShader(const ResourceHandle<Shader>& shader) {
|
||||
m_shader = shader;
|
||||
}
|
||||
|
||||
void Material::SetFloat(const Containers::String& name, float value) {
|
||||
MaterialProperty prop;
|
||||
prop.name = name;
|
||||
prop.type = MaterialPropertyType::Float;
|
||||
prop.value.floatValue[0] = value;
|
||||
prop.refCount = 1;
|
||||
m_properties.Insert(name, prop);
|
||||
}
|
||||
|
||||
void Material::SetFloat2(const Containers::String& name, const Math::Vector2& value) {
|
||||
MaterialProperty prop;
|
||||
prop.name = name;
|
||||
prop.type = MaterialPropertyType::Float2;
|
||||
prop.value.floatValue[0] = value.x;
|
||||
prop.value.floatValue[1] = value.y;
|
||||
prop.refCount = 1;
|
||||
m_properties.Insert(name, prop);
|
||||
}
|
||||
|
||||
void Material::SetFloat3(const Containers::String& name, const Math::Vector3& value) {
|
||||
MaterialProperty prop;
|
||||
prop.name = name;
|
||||
prop.type = MaterialPropertyType::Float3;
|
||||
prop.value.floatValue[0] = value.x;
|
||||
prop.value.floatValue[1] = value.y;
|
||||
prop.value.floatValue[2] = value.z;
|
||||
prop.refCount = 1;
|
||||
m_properties.Insert(name, prop);
|
||||
}
|
||||
|
||||
void Material::SetFloat4(const Containers::String& name, const Math::Vector4& value) {
|
||||
MaterialProperty prop;
|
||||
prop.name = name;
|
||||
prop.type = MaterialPropertyType::Float4;
|
||||
prop.value.floatValue[0] = value.x;
|
||||
prop.value.floatValue[1] = value.y;
|
||||
prop.value.floatValue[2] = value.z;
|
||||
prop.value.floatValue[3] = value.w;
|
||||
prop.refCount = 1;
|
||||
m_properties.Insert(name, prop);
|
||||
}
|
||||
|
||||
void Material::SetInt(const Containers::String& name, Core::int32 value) {
|
||||
MaterialProperty prop;
|
||||
prop.name = name;
|
||||
prop.type = MaterialPropertyType::Int;
|
||||
prop.value.intValue[0] = value;
|
||||
prop.refCount = 1;
|
||||
m_properties.Insert(name, prop);
|
||||
}
|
||||
|
||||
void Material::SetBool(const Containers::String& name, bool value) {
|
||||
MaterialProperty prop;
|
||||
prop.name = name;
|
||||
prop.type = MaterialPropertyType::Bool;
|
||||
prop.value.boolValue = value;
|
||||
prop.refCount = 1;
|
||||
m_properties.Insert(name, prop);
|
||||
}
|
||||
|
||||
void Material::SetTexture(const Containers::String& name, const ResourceHandle<Texture>& texture) {
|
||||
MaterialProperty prop;
|
||||
prop.name = name;
|
||||
prop.type = MaterialPropertyType::Texture;
|
||||
prop.refCount = 1;
|
||||
m_properties.Insert(name, prop);
|
||||
|
||||
TextureBinding binding;
|
||||
binding.name = name;
|
||||
binding.slot = static_cast<Core::uint32>(m_textureBindings.Size());
|
||||
binding.texture = texture;
|
||||
m_textureBindings.PushBack(binding);
|
||||
}
|
||||
|
||||
float Material::GetFloat(const Containers::String& name) const {
|
||||
auto* prop = m_properties.Find(name);
|
||||
if (prop && prop->type == MaterialPropertyType::Float) {
|
||||
return prop->value.floatValue[0];
|
||||
}
|
||||
return 0.0f;
|
||||
}
|
||||
|
||||
Math::Vector2 Material::GetFloat2(const Containers::String& name) const {
|
||||
auto* prop = m_properties.Find(name);
|
||||
if (prop && prop->type == MaterialPropertyType::Float2) {
|
||||
return Math::Vector2(prop->value.floatValue[0], prop->value.floatValue[1]);
|
||||
}
|
||||
return Math::Vector2::Zero();
|
||||
}
|
||||
|
||||
Math::Vector3 Material::GetFloat3(const Containers::String& name) const {
|
||||
auto* prop = m_properties.Find(name);
|
||||
if (prop && prop->type == MaterialPropertyType::Float3) {
|
||||
return Math::Vector3(prop->value.floatValue[0], prop->value.floatValue[1], prop->value.floatValue[2]);
|
||||
}
|
||||
return Math::Vector3::Zero();
|
||||
}
|
||||
|
||||
Math::Vector4 Material::GetFloat4(const Containers::String& name) const {
|
||||
auto* prop = m_properties.Find(name);
|
||||
if (prop && prop->type == MaterialPropertyType::Float4) {
|
||||
return Math::Vector4(prop->value.floatValue[0], prop->value.floatValue[1],
|
||||
prop->value.floatValue[2], prop->value.floatValue[3]);
|
||||
}
|
||||
return Math::Vector4::Zero();
|
||||
}
|
||||
|
||||
Core::int32 Material::GetInt(const Containers::String& name) const {
|
||||
auto* prop = m_properties.Find(name);
|
||||
if (prop && prop->type == MaterialPropertyType::Int) {
|
||||
return prop->value.intValue[0];
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool Material::GetBool(const Containers::String& name) const {
|
||||
auto* prop = m_properties.Find(name);
|
||||
if (prop && prop->type == MaterialPropertyType::Bool) {
|
||||
return prop->value.boolValue;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
ResourceHandle<Texture> Material::GetTexture(const Containers::String& name) const {
|
||||
for (const auto& binding : m_textureBindings) {
|
||||
if (binding.name == name) {
|
||||
return binding.texture;
|
||||
}
|
||||
}
|
||||
return ResourceHandle<Texture>();
|
||||
}
|
||||
|
||||
void Material::UpdateConstantBuffer() {
|
||||
m_constantBufferData.Clear();
|
||||
}
|
||||
|
||||
bool Material::HasProperty(const Containers::String& name) const {
|
||||
return m_properties.Contains(name);
|
||||
}
|
||||
|
||||
void Material::RemoveProperty(const Containers::String& name) {
|
||||
m_properties.Erase(name);
|
||||
}
|
||||
|
||||
void Material::ClearAllProperties() {
|
||||
m_properties.Clear();
|
||||
m_textureBindings.Clear();
|
||||
m_constantBufferData.Clear();
|
||||
}
|
||||
|
||||
} // namespace Resources
|
||||
} // namespace XCEngine
|
||||
73
engine/src/Resources/Material/MaterialLoader.cpp
Normal file
73
engine/src/Resources/Material/MaterialLoader.cpp
Normal file
@@ -0,0 +1,73 @@
|
||||
#include "Resources/MaterialLoader.h"
|
||||
#include "Resources/ResourceManager.h"
|
||||
#include "Resources/ResourceTypes.h"
|
||||
|
||||
namespace XCEngine {
|
||||
namespace Resources {
|
||||
|
||||
MaterialLoader::MaterialLoader() = default;
|
||||
|
||||
MaterialLoader::~MaterialLoader() = default;
|
||||
|
||||
Containers::Array<Containers::String> MaterialLoader::GetSupportedExtensions() const {
|
||||
Containers::Array<Containers::String> extensions;
|
||||
extensions.PushBack("mat");
|
||||
extensions.PushBack("material");
|
||||
extensions.PushBack("json");
|
||||
return extensions;
|
||||
}
|
||||
|
||||
bool MaterialLoader::CanLoad(const Containers::String& path) const {
|
||||
Containers::String ext = GetExtension(path);
|
||||
return ext == "mat" || ext == "material" || ext == "json";
|
||||
}
|
||||
|
||||
LoadResult MaterialLoader::Load(const Containers::String& path, const ImportSettings* settings) {
|
||||
Containers::Array<Core::uint8> data = ReadFileData(path);
|
||||
if (data.Empty()) {
|
||||
return LoadResult("Failed to read material file: " + path);
|
||||
}
|
||||
|
||||
Material* material = new Material();
|
||||
material->m_path = path;
|
||||
material->m_name = path;
|
||||
material->m_guid = ResourceGUID::Generate(path);
|
||||
|
||||
Containers::String jsonStr;
|
||||
jsonStr.Reserve(data.Size());
|
||||
for (size_t i = 0; i < data.Size(); ++i) {
|
||||
jsonStr += static_cast<char>(data[i]);
|
||||
}
|
||||
|
||||
size_t shaderPos = jsonStr.Find("\"shader\"");
|
||||
if (shaderPos != Containers::String::npos) {
|
||||
size_t colonPos = jsonStr.Find(":", shaderPos);
|
||||
if (colonPos != Containers::String::npos) {
|
||||
size_t quoteStart = jsonStr.Find("\"", colonPos);
|
||||
size_t quoteEnd = jsonStr.Find("\"", quoteStart + 1);
|
||||
if (quoteStart != Containers::String::npos && quoteEnd != Containers::String::npos) {
|
||||
Containers::String shaderPath = jsonStr.Substring(quoteStart + 1, quoteEnd - quoteStart - 1);
|
||||
auto shaderHandle = ResourceManager::Get().Load<Shader>(shaderPath);
|
||||
if (shaderHandle.IsValid()) {
|
||||
material->SetShader(shaderHandle);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
material->m_isValid = true;
|
||||
material->m_memorySize = sizeof(Material) + material->m_name.Length() + material->m_path.Length();
|
||||
|
||||
return LoadResult(material);
|
||||
}
|
||||
|
||||
ImportSettings* MaterialLoader::GetDefaultSettings() const {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
bool MaterialLoader::ParseMaterialData(const Containers::Array<Core::uint8>& data, Material* material) {
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace Resources
|
||||
} // namespace XCEngine
|
||||
Reference in New Issue
Block a user