-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathMaterialLoader.cpp
More file actions
55 lines (50 loc) · 2.03 KB
/
MaterialLoader.cpp
File metadata and controls
55 lines (50 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include "MaterialLoader.hpp"
#include "RenderContext.hpp"
#include "TextureLoader.hpp"
#include "FileUtility.hpp"
#include "nlohmann/json.hpp"
NLOHMANN_JSON_SERIALIZE_ENUM(ShadingModel,
{
{ShadingModel::Unlit, "Unlit"},
{ShadingModel::DefaultLit, "DefaultLit"},
});
NLOHMANN_JSON_SERIALIZE_ENUM(BlendMode,
{
{BlendMode::Opaque, "Opaque"},
{BlendMode::Masked, "Masked"},
{BlendMode::Transparent, "Transparent"},
});
NLOHMANN_JSON_SERIALIZE_ENUM(CullMode, {
{CullMode::None, "None"},
{CullMode::Front, "Front"},
{CullMode::Back, "Back"},
});
std::shared_ptr<Material> loadMaterial(const std::filesystem::path &p,
TextureCache &textureCache) {
const auto j = nlohmann::json::parse(readText(p));
auto builder = Material::Builder{};
builder.setShadingModel(j["shadingModel"])
.setBlendMode(j["blendMode"])
.setCullMode(j.value("cullMode", CullMode::Back));
if (j.contains("samplers")) {
for (const auto &[_, prop] : j["samplers"].items()) {
const auto texturePath = adjustPath(prop["path"].get<std::string>(), p);
auto textureResource = textureCache.load(texturePath);
builder.addSampler(prop["name"], textureResource);
}
}
std::string vert;
if (j.contains("vertexShader")) {
const auto userCodePath =
adjustPath(j["vertexShader"].get<std::string>(), p);
vert = readText(userCodePath);
}
std::string frag;
if (j.contains("fragmentShader")) {
const auto userCodePath =
adjustPath(j["fragmentShader"].get<std::string>(), p);
frag = readText(userCodePath);
}
builder.setUserCode(vert, frag);
return builder.build();
}