106 lines
2.5 KiB
C++
106 lines
2.5 KiB
C++
#pragma once
|
|
|
|
#include <asset.hpp>
|
|
#include <json.hpp>
|
|
#include <utility.hpp>
|
|
#include <filesystem.hpp>
|
|
#include <log.hpp>
|
|
#include <fstream>
|
|
|
|
class Material : public Asset
|
|
{
|
|
public:
|
|
Material(const std::string& path)
|
|
{
|
|
m_filepath = path;
|
|
|
|
LoadMat();
|
|
}
|
|
|
|
void LoadMat()
|
|
{
|
|
int size;
|
|
File handle = Filesystem::GetFileHandle(m_filepath, size);
|
|
if(handle != nullptr)
|
|
{
|
|
nlohmann::json file = nlohmann::json::parse(handle);
|
|
|
|
albedoID = file["albedo_tex"];
|
|
albedoColor = Utility::StringToVec3(file["albedo_color"]);
|
|
normalID = file["normal_tex"];
|
|
metallic = file["metallic"];
|
|
roughness = file["roughness"];
|
|
metallicID = file["metallic_tex"];
|
|
roughnessID = file["roughness_tex"];
|
|
emissionColor = Utility::StringToVec3(file["emission_color"]);
|
|
|
|
Filesystem::FreeFileHandle(handle);
|
|
}
|
|
else
|
|
{
|
|
if(!Filesystem::IsPacked())
|
|
{
|
|
Log::Error("Material file %s not found! Creating new one...", m_filepath.c_str());
|
|
|
|
SaveMat();
|
|
LoadMat();
|
|
}
|
|
}
|
|
}
|
|
|
|
void SaveMat()
|
|
{
|
|
nlohmann::json file;
|
|
|
|
file["albedo_tex"] = albedoID;
|
|
file["albedo_color"] = Utility::Vec3ToString(albedoColor);
|
|
file["normal_tex"] = normalID;
|
|
file["metallic"] = metallic;
|
|
file["roughness"] = roughness;
|
|
file["metallic_tex"] = metallicID;
|
|
file["roughness_tex"] = roughnessID;
|
|
file["emission_color"] = Utility::Vec3ToString(emissionColor);
|
|
|
|
std::ofstream out;
|
|
out.open(m_filepath);
|
|
|
|
out << file.dump(1);
|
|
out.close();
|
|
}
|
|
|
|
std::string GetName() override
|
|
{
|
|
return Utility::GetFilename(m_filepath);
|
|
}
|
|
|
|
std::string GetPath() override
|
|
{
|
|
return m_filepath;
|
|
}
|
|
|
|
std::vector<int> GetDependencies() override
|
|
{
|
|
return { albedoID, normalID, metallicID, roughnessID };
|
|
}
|
|
|
|
std::type_index GetType() override
|
|
{
|
|
return typeid(Material);
|
|
}
|
|
|
|
//used for apis like vulkan where materials are cached, useful for the editor
|
|
bool invalidated = false;
|
|
|
|
int albedoID = 0;
|
|
glm::vec3 albedoColor = glm::vec3(1);
|
|
glm::vec3 emissionColor = glm::vec3(0);
|
|
|
|
int normalID = 0;
|
|
|
|
float metallic = 0.0f, roughness = 0.0f;
|
|
int metallicID = 0;
|
|
int roughnessID = 0;
|
|
|
|
private:
|
|
std::string m_filepath;
|
|
};
|