NoJobEngine 2.0.7
Loading...
Searching...
No Matches
MaterialSerializer.cpp
Go to the documentation of this file.
6#include <fstream>
7#include <iomanip>
8#include <algorithm>
9namespace NoJob {
10bool MaterialSerializer::Save(const Material&x,const std::filesystem::path&p,const AssetRegistry&){std::ofstream o(p);if(!o)return false;auto&m=const_cast<Material&>(x);auto c=x.GetColor(); auto e=m.EmissiveColor();o<<"NOJOB_MATERIAL 3\nCOLOR "<<c.r<<' '<<c.g<<' '<<c.b<<' '<<c.a<<"\nPBR "<<x.Metallic()<<' '<<x.Roughness()<<' '<<x.AmbientOcclusion()<<' '<<m.NormalStrength()<<"\nEMISSIVE "<<e.r<<' '<<e.g<<' '<<e.b<<' '<<m.EmissiveStrength()<<"\nSURFACE "<<static_cast<int>(x.SurfaceMode())<<' '<<x.AlphaCutoff()<<"\nTEXTURES "<<std::quoted(AssetManager::GetTexturePath(x.GetTexture()).generic_string())<<' '<<std::quoted(AssetManager::GetTexturePath(x.GetNormalTexture()).generic_string())<<' '<<std::quoted(AssetManager::GetTexturePath(x.GetMetallicTexture()).generic_string())<<' '<<std::quoted(AssetManager::GetTexturePath(x.GetRoughnessTexture()).generic_string())<<' '<<std::quoted(AssetManager::GetTexturePath(x.GetAOTexture()).generic_string())<<' '<<std::quoted(AssetManager::GetTexturePath(x.GetEmissiveTexture()).generic_string())<<"\n";return true;}
11std::shared_ptr<Material> MaterialSerializer::Load(const std::filesystem::path&p,const std::shared_ptr<Shader>&s,AssetRegistry&){std::ifstream i(p);if(!i)return{};std::string l;std::getline(i,l);if(l.rfind("NOJOB_MATERIAL",0)!=0)return{};auto m=std::make_shared<Material>(s);m->UseTexture()=false;std::string k;while(i>>k){if(k=="COLOR")i>>m->GetColor().r>>m->GetColor().g>>m->GetColor().b>>m->GetColor().a;else if(k=="PBR")i>>m->Metallic()>>m->Roughness()>>m->AmbientOcclusion()>>m->NormalStrength();else if(k=="EMISSIVE")i>>m->EmissiveColor().r>>m->EmissiveColor().g>>m->EmissiveColor().b>>m->EmissiveStrength();else if(k=="SURFACE"){int mode=0;i>>mode>>m->AlphaCutoff();m->SurfaceMode()=static_cast<MaterialSurfaceMode>(std::clamp(mode,0,2));}else if(k=="TEXTURES"){std::string a,n,me,r,ao,e;i>>std::quoted(a)>>std::quoted(n)>>std::quoted(me)>>std::quoted(r)>>std::quoted(ao)>>std::quoted(e);auto L=[](auto&q){return q.empty()?std::shared_ptr<Texture2D>{}:AssetManager::LoadTexture(q);};if(!a.empty()){m->SetTexture(L(a));m->UseTexture()=true;}m->SetNormalTexture(L(n));m->SetMetallicTexture(L(me));m->SetRoughnessTexture(L(r));m->SetAOTexture(L(ao));m->SetEmissiveTexture(L(e));}}return m;}
12}
AssetManager declarations for the Asset subsystem.
AssetRegistry declarations for the Asset Management subsystem.
Material declarations for the Rendering subsystem.
MaterialSerializer declarations for the Asset Management subsystem.
Texture declarations for the Rendering subsystem.
static std::filesystem::path GetTexturePath(const std::shared_ptr< Texture2D > &texture)
static std::shared_ptr< Texture2D > LoadTexture(const std::filesystem::path &path)
float & Metallic()
Definition Material.h:49
const std::shared_ptr< Texture2D > & GetAOTexture() const
Definition Material.h:67
const std::shared_ptr< Texture2D > & GetNormalTexture() const
Definition Material.h:64
float & AmbientOcclusion()
Definition Material.h:51
const std::shared_ptr< Texture2D > & GetMetallicTexture() const
Definition Material.h:65
float & Roughness()
Definition Material.h:50
const std::shared_ptr< Texture2D > & GetEmissiveTexture() const
Definition Material.h:68
const std::shared_ptr< Texture2D > & GetTexture() const
Definition Material.h:37
const std::shared_ptr< Texture2D > & GetRoughnessTexture() const
Definition Material.h:66
glm::vec4 & GetColor()
Definition Material.h:29
float & AlphaCutoff()
Definition Material.h:72
MaterialSurfaceMode & SurfaceMode()
Definition Material.h:70
static std::shared_ptr< Material > Load(const std::filesystem::path &, const std::shared_ptr< Shader > &, AssetRegistry &)
static bool Save(const Material &, const std::filesystem::path &, const AssetRegistry &)
MaterialSurfaceMode
Definition Material.h:16