PixelBullet  0.0.1
A C++ game engine
Loading...
Searching...
No Matches
material.h
1#pragma once
2
3#include "pixelbullet/filesystem/virtual_path.h"
4#include "pixelbullet/filesystem/virtual_path_serialization.h"
5#include "pixelbullet/serialization/node.h"
6
7#include <glm/glm.hpp>
8
9namespace pixelbullet
10{
11class Filesystem;
12
14{
15public:
16 Material() = default;
17 Material(const Filesystem& filesystem, const VirtualPath& assetPath)
18 {
19 Load(filesystem, assetPath);
20 }
21
22 bool Load(const Filesystem& filesystem, const VirtualPath& assetPath);
23
24 const VirtualPath& GetFilename() const
25 {
26 return filename_;
27 }
28
29 VirtualPath base_color_texture;
30 glm::vec4 base_color_factor = glm::vec4(1.0f);
31
32 friend Node& operator<<(Node& node, const Material& material)
33 {
34 node["baseColorTexture"] << material.base_color_texture;
35
36 Node factorNode;
37 factorNode.SetType(NodeType::Array);
38 auto& factorProperties = factorNode.GetProperties();
39 factorProperties.clear();
40 for (int index = 0; index < 4; ++index)
41 {
42 Node componentNode;
43 componentNode << material.base_color_factor[index];
44 factorProperties.emplace_back("", std::move(componentNode));
45 }
46 node["baseColorFactor"] = std::move(factorNode);
47 return node;
48 }
49
50 friend const Node& operator>>(const Node& node, Material& material)
51 {
52 if (node.HasProperty("baseColorTexture"))
53 {
54 node["baseColorTexture"] >> material.base_color_texture;
55 }
56
57 if (node.HasProperty("baseColorFactor"))
58 {
59 const Node& factorNode = node["baseColorFactor"];
60 ASSERT(factorNode.GetType() == NodeType::Array, "material baseColorFactor must be an array");
61 const auto& factorProperties = factorNode.GetProperties();
62 ASSERT(factorProperties.size() >= 4, "material baseColorFactor requires four components");
63 for (int index = 0; index < 4; ++index)
64 {
65 factorProperties[index].second >> material.base_color_factor[index];
66 }
67 }
68
69 return node;
70 }
71
72private:
73 VirtualPath filename_;
74};
75} // namespace pixelbullet
#define ASSERT(condition,...)
Asserts that a condition is true.
Definition assert.h:163
Definition filesystem.h:16
Definition material.h:14
Represents a hierarchical node capable of storing various data types and supporting YAML serializatio...
Definition node.h:45
Definition virtual_path.h:10