2026-03-26 20:43:17 +08:00
|
|
|
#include "Components/MeshRendererComponent.h"
|
|
|
|
|
|
|
|
|
|
#include "Core/Asset/ResourceManager.h"
|
|
|
|
|
|
|
|
|
|
#include <sstream>
|
|
|
|
|
|
|
|
|
|
namespace XCEngine {
|
|
|
|
|
namespace Components {
|
|
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
|
|
std::string ToStdString(const Containers::String& value) {
|
|
|
|
|
return std::string(value.CStr());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::vector<std::string> SplitMaterialPaths(const std::string& value) {
|
|
|
|
|
std::vector<std::string> paths;
|
2026-03-27 15:32:37 +08:00
|
|
|
if (value.empty()) {
|
|
|
|
|
return paths;
|
2026-03-26 20:43:17 +08:00
|
|
|
}
|
|
|
|
|
|
2026-03-27 15:32:37 +08:00
|
|
|
size_t start = 0;
|
|
|
|
|
while (true) {
|
|
|
|
|
const size_t separator = value.find('|', start);
|
|
|
|
|
if (separator == std::string::npos) {
|
|
|
|
|
paths.push_back(value.substr(start));
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
paths.push_back(value.substr(start, separator - start));
|
|
|
|
|
start = separator + 1;
|
2026-03-26 20:43:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return paths;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
|
|
Resources::Material* MeshRendererComponent::GetMaterial(size_t index) const {
|
|
|
|
|
return index < m_materials.size() ? m_materials[index].Get() : nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const Resources::ResourceHandle<Resources::Material>& MeshRendererComponent::GetMaterialHandle(size_t index) const {
|
|
|
|
|
static const Resources::ResourceHandle<Resources::Material> kNullHandle;
|
|
|
|
|
return index < m_materials.size() ? m_materials[index] : kNullHandle;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-27 14:52:00 +08:00
|
|
|
const std::string& MeshRendererComponent::GetMaterialPath(size_t index) const {
|
|
|
|
|
static const std::string kEmptyPath;
|
|
|
|
|
return index < m_materialPaths.size() ? m_materialPaths[index] : kEmptyPath;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MeshRendererComponent::SetMaterialPath(size_t index, const std::string& materialPath) {
|
|
|
|
|
EnsureMaterialSlot(index);
|
|
|
|
|
m_materialPaths[index] = materialPath;
|
|
|
|
|
if (materialPath.empty()) {
|
|
|
|
|
m_materials[index].Reset();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_materials[index] = Resources::ResourceManager::Get().Load<Resources::Material>(materialPath.c_str());
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-26 20:43:17 +08:00
|
|
|
void MeshRendererComponent::SetMaterial(size_t index, const Resources::ResourceHandle<Resources::Material>& material) {
|
|
|
|
|
EnsureMaterialSlot(index);
|
|
|
|
|
m_materials[index] = material;
|
|
|
|
|
m_materialPaths[index] = MaterialPathFromHandle(material);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MeshRendererComponent::SetMaterial(size_t index, Resources::Material* material) {
|
|
|
|
|
SetMaterial(index, Resources::ResourceHandle<Resources::Material>(material));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MeshRendererComponent::SetMaterials(const std::vector<Resources::ResourceHandle<Resources::Material>>& materials) {
|
|
|
|
|
m_materials = materials;
|
|
|
|
|
m_materialPaths.resize(materials.size());
|
|
|
|
|
for (size_t i = 0; i < materials.size(); ++i) {
|
|
|
|
|
m_materialPaths[i] = MaterialPathFromHandle(materials[i]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MeshRendererComponent::ClearMaterials() {
|
|
|
|
|
m_materials.clear();
|
|
|
|
|
m_materialPaths.clear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MeshRendererComponent::Serialize(std::ostream& os) const {
|
|
|
|
|
os << "materials=";
|
|
|
|
|
for (size_t i = 0; i < m_materialPaths.size(); ++i) {
|
|
|
|
|
if (i > 0) {
|
|
|
|
|
os << "|";
|
|
|
|
|
}
|
|
|
|
|
os << m_materialPaths[i];
|
|
|
|
|
}
|
|
|
|
|
os << ";";
|
|
|
|
|
os << "castShadows=" << (m_castShadows ? 1 : 0) << ";";
|
|
|
|
|
os << "receiveShadows=" << (m_receiveShadows ? 1 : 0) << ";";
|
|
|
|
|
os << "renderLayer=" << m_renderLayer << ";";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MeshRendererComponent::Deserialize(std::istream& is) {
|
|
|
|
|
ClearMaterials();
|
|
|
|
|
m_castShadows = true;
|
|
|
|
|
m_receiveShadows = true;
|
|
|
|
|
m_renderLayer = 0;
|
|
|
|
|
|
|
|
|
|
std::string token;
|
|
|
|
|
while (std::getline(is, token, ';')) {
|
|
|
|
|
if (token.empty()) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const size_t eqPos = token.find('=');
|
|
|
|
|
if (eqPos == std::string::npos) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const std::string key = token.substr(0, eqPos);
|
|
|
|
|
const std::string value = token.substr(eqPos + 1);
|
|
|
|
|
|
|
|
|
|
if (key == "materials") {
|
|
|
|
|
m_materialPaths = SplitMaterialPaths(value);
|
|
|
|
|
m_materials.resize(m_materialPaths.size());
|
|
|
|
|
for (size_t i = 0; i < m_materialPaths.size(); ++i) {
|
2026-03-27 14:52:00 +08:00
|
|
|
SetMaterialPath(i, m_materialPaths[i]);
|
2026-03-26 20:43:17 +08:00
|
|
|
}
|
|
|
|
|
} else if (key == "castShadows") {
|
|
|
|
|
m_castShadows = (std::stoi(value) != 0);
|
|
|
|
|
} else if (key == "receiveShadows") {
|
|
|
|
|
m_receiveShadows = (std::stoi(value) != 0);
|
|
|
|
|
} else if (key == "renderLayer") {
|
|
|
|
|
m_renderLayer = static_cast<uint32_t>(std::stoul(value));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MeshRendererComponent::EnsureMaterialSlot(size_t index) {
|
|
|
|
|
if (index >= m_materials.size()) {
|
|
|
|
|
m_materials.resize(index + 1);
|
|
|
|
|
m_materialPaths.resize(index + 1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string MeshRendererComponent::MaterialPathFromHandle(const Resources::ResourceHandle<Resources::Material>& material) {
|
|
|
|
|
return material.Get() != nullptr ? ToStdString(material->GetPath()) : std::string();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace Components
|
|
|
|
|
} // namespace XCEngine
|