Archived
1
Fork 0
This repository has been archived on 2025-04-12. You can view files and clone it, but cannot push or open issues or pull requests.
graphite/engine/assets/include/material.hpp

107 lines
2.5 KiB
C++
Raw Normal View History

2024-01-03 16:05:02 -05:00
#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;
};