2020-05-05 01:51:13 +02:00
|
|
|
#include "Material.h"
|
2020-06-02 11:46:18 +02:00
|
|
|
#include "Asset/AssetRegistry.h"
|
2020-05-05 01:51:13 +02:00
|
|
|
#include <nlohmann/json.hpp>
|
2020-06-02 11:46:18 +02:00
|
|
|
#include <sstream>
|
2020-05-05 01:51:13 +02:00
|
|
|
|
|
|
|
|
using namespace Seele;
|
|
|
|
|
using json = nlohmann::json;
|
|
|
|
|
|
|
|
|
|
Material::Material()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Material::Material(const std::string& directory, const std::string& name)
|
2020-06-02 11:46:18 +02:00
|
|
|
: MaterialAsset(directory, name)
|
2020-05-05 01:51:13 +02:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Material::Material(const std::string& fullPath)
|
2020-06-02 11:46:18 +02:00
|
|
|
: MaterialAsset(fullPath)
|
|
|
|
|
{
|
2020-05-05 01:51:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Material::~Material()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-02 11:46:18 +02:00
|
|
|
|
2020-05-05 01:51:13 +02:00
|
|
|
void Material::compile()
|
|
|
|
|
{
|
|
|
|
|
auto& stream = getReadStream();
|
|
|
|
|
json j;
|
|
|
|
|
stream >> j;
|
2020-06-02 11:46:18 +02:00
|
|
|
std::stringstream codeStream;
|
|
|
|
|
materialName = j["name"].get<std::string>();
|
|
|
|
|
std::string profile = j["profile"].get<std::string>();
|
|
|
|
|
codeStream << "import LightEnv;" << std::endl;
|
|
|
|
|
codeStream << "import Material;" << std::endl;
|
|
|
|
|
codeStream << "import BRDF;" << std::endl;
|
|
|
|
|
codeStream << "import InputGeometry;" << std::endl;
|
2020-05-05 01:51:13 +02:00
|
|
|
|
2020-06-02 11:46:18 +02:00
|
|
|
codeStream << "struct " << materialName << ": IMaterial {" << std::endl;
|
|
|
|
|
for(auto param : j["params"].items())
|
|
|
|
|
{
|
|
|
|
|
std::string type = param.value()["type"].get<std::string>();
|
|
|
|
|
|
|
|
|
|
auto default = param.value().find("default");
|
|
|
|
|
if(type.compare("float") == 0)
|
|
|
|
|
{
|
|
|
|
|
PFloatParameter p = new FloatParameter();
|
|
|
|
|
if(default != param.value().end())
|
|
|
|
|
{
|
|
|
|
|
p->defaultValue = std::stof(default.value().get<std::string>());
|
|
|
|
|
}
|
|
|
|
|
parameters.add(p);
|
|
|
|
|
}
|
|
|
|
|
else if(type.compare("float3") == 0)
|
|
|
|
|
{
|
|
|
|
|
PVectorParameter p = new VectorParameter();
|
|
|
|
|
if(default != param.value().end())
|
|
|
|
|
{
|
|
|
|
|
p->defaultValue = parseVector(default.value().get<std::string>().c_str());
|
|
|
|
|
}
|
|
|
|
|
parameters.add(p);
|
|
|
|
|
}
|
|
|
|
|
else if(type.compare("Texture2D") == 0)
|
|
|
|
|
{
|
|
|
|
|
PTextureParameter p = new TextureParameter();
|
|
|
|
|
if(default != param.value().end())
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
parameters.add(p);
|
|
|
|
|
}
|
|
|
|
|
else if(type.compare("SamplerState") == 0)
|
|
|
|
|
{
|
|
|
|
|
PSamplerParameter p = new SamplerParameter();
|
|
|
|
|
parameters.add(p);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
std::cout << "Error unsupported parameter type" << std::endl;
|
|
|
|
|
}
|
|
|
|
|
codeStream << type << " " << param.key();
|
|
|
|
|
}
|
|
|
|
|
codeStream << "typedef " << profile << " BRDF;" << std::endl;
|
|
|
|
|
codeStream << profile << " prepare(MaterialPixelParameter geometry){" << std::endl;
|
|
|
|
|
codeStream << profile << " result;" << std::endl;
|
|
|
|
|
for(auto c : j["code"].items())
|
|
|
|
|
{
|
|
|
|
|
codeStream << c.value().get<std::string>() << std::endl;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
codeStream << "}};" << std::endl;
|
2020-05-05 01:51:13 +02:00
|
|
|
}
|