Unravel Engine C++ Reference
Loading...
Searching...
No Matches
standard_material.cpp
Go to the documentation of this file.
2#include "material.hpp"
4
7
10
12
13namespace unravel
14{
15
17{
18 entt::meta_factory<pbr_material>{}
19 .type("pbr_material"_hs)
21 entt::attribute{"name", "pbr_material"},
22 entt::attribute{"pretty_name", "PBR Material"},
23 })
24 .base<material>()
25 .func<&pbr_material::get_meta_type>("get_meta_type"_hs)
26 .func<&pbr_material::get_static_meta_type>("get_static_meta_type"_hs)
27 .func<&pbr_material::as_derived>("as_derived"_hs)
28 .data<&pbr_material::set_base_color, &pbr_material::get_base_color>("base_color"_hs)
30 entt::attribute{"name", "base_color"},
31 entt::attribute{"pretty_name", "Base Color"}
32 })
33 .data<&pbr_material::set_subsurface_color, &pbr_material::get_subsurface_color>("subsurface_color"_hs)
35 entt::attribute{"name", "subsurface_color"},
36 entt::attribute{"pretty_name", "Subsurface Color"}
37 })
38 .data<&pbr_material::set_emissive_color, &pbr_material::get_emissive_color>("emissive_color"_hs)
40 entt::attribute{"name", "emissive_color"},
41 entt::attribute{"pretty_name", "Emissive Color"}
42 })
43 .data<&pbr_material::set_emissive_intensity, &pbr_material::get_emissive_intensity>("emissive_intensity"_hs)
45 entt::attribute{"name", "emissive_intensity"},
46 entt::attribute{"pretty_name", "Emissive Intensity"},
47 entt::attribute{"min", 0.0f},
48 entt::attribute{"max", 100.0f},
49 })
50 .data<&pbr_material::set_roughness, &pbr_material::get_roughness>("roughness"_hs)
52 entt::attribute{"name", "roughness"},
53 entt::attribute{"pretty_name", "Roughness"},
54 entt::attribute{"min", 0.0f},
55 entt::attribute{"max", 1.0f},
56 })
57 .data<&pbr_material::set_metalness, &pbr_material::get_metalness>("metalness"_hs)
59 entt::attribute{"name", "metalness"},
60 entt::attribute{"pretty_name", "Metalness"},
61 entt::attribute{"min", 0.0f},
62 entt::attribute{"max", 1.0f},
63 })
64 .data<&pbr_material::set_bumpiness, &pbr_material::get_bumpiness>("bumpiness"_hs)
66 entt::attribute{"name", "bumpiness"},
67 entt::attribute{"pretty_name", "Bumpiness"},
68 entt::attribute{"min", 0.0f},
69 entt::attribute{"max", 10.0f},
70 })
71 .data<&pbr_material::set_alpha_mode, &pbr_material::get_alpha_mode>("alpha_mode"_hs)
73 entt::attribute{"name", "alpha_mode"},
74 entt::attribute{"pretty_name", "Alpha Mode"},
75 })
76 .data<&pbr_material::set_alpha_cutoff, &pbr_material::get_alpha_cutoff>("alpha_cutoff"_hs)
78 entt::attribute{"name", "alpha_cutoff"},
79 entt::attribute{"pretty_name", "Alpha Cutoff"},
80 entt::attribute{"min", 0.0f},
81 entt::attribute{"max", 1.0f},
82 })
83 .data<&pbr_material::set_tiling, &pbr_material::get_tiling>("tiling"_hs)
85 entt::attribute{"name", "tiling"},
86 entt::attribute{"pretty_name", "Tiling"}
87 })
88 .data<&pbr_material::set_dither_threshold, &pbr_material::get_dither_threshold>("dither_threshold"_hs)
90 entt::attribute{"name", "dither_threshold"},
91 entt::attribute{"pretty_name", "Dither Threshold"}
92 })
93 .data<&pbr_material::set_color_map, &pbr_material::get_color_map>("color_map"_hs)
95 entt::attribute{"name", "color_map"},
96 entt::attribute{"pretty_name", "Color Map"}
97 })
98 .data<&pbr_material::set_normal_map, &pbr_material::get_normal_map>("normal_map"_hs)
100 entt::attribute{"name", "normal_map"},
101 entt::attribute{"pretty_name", "Normal Map"}
102 })
103 .data<&pbr_material::set_roughness_map, &pbr_material::get_roughness_map>("roughness_map"_hs)
105 entt::attribute{"name", "roughness_map"},
106 entt::attribute{"pretty_name", "Roughness Map"},
107 entt::attribute{"tooltip", "Red Channel (R): Contains the roughness values.\nWhen Metalness and Roughness maps are the same.\nAs per glTF 2.0 specification:\nGreen Channel (G): Contains the roughness values.\nBlue Channel (B): Contains the metalness values."},
108 })
109 .data<&pbr_material::set_metalness_map, &pbr_material::get_metalness_map>("metalness_map"_hs)
111 entt::attribute{"name", "metalness_map"},
112 entt::attribute{"pretty_name", "Metalness Map"},
113 entt::attribute{"tooltip", "Red Channel (R): Contains the metalness values.\nWhen Metalness and Roughness maps are the same.\nAs per glTF 2.0 specification:\nGreen Channel (G): Contains the roughness values.\nBlue Channel (B): Contains the metalness values."},
114 })
115 .data<&pbr_material::set_emissive_map, &pbr_material::get_emissive_map>("emissive_map"_hs)
117 entt::attribute{"name", "emissive_map"},
118 entt::attribute{"pretty_name", "Emissive Map"},
119 entt::attribute{"tooltip", "emissive color map."}
120 })
121 .data<&pbr_material::set_ao_map, &pbr_material::get_ao_map>("ao_map"_hs)
123 entt::attribute{"name", "ao_map"},
124 entt::attribute{"pretty_name", "AO Map"},
125 entt::attribute{"tooltip", "black/white texture."}
126 });
127}
128
130{
131 try_save(ar, ser20::make_nvp("base_type", ser20::base_class<material>(&obj)));
132 try_save(ar, ser20::make_nvp("base_color", obj.base_color_));
133 try_save(ar, ser20::make_nvp("subsurface_color", obj.subsurface_color_));
134 try_save(ar, ser20::make_nvp("emissive_color", obj.emissive_color_));
135 try_save(ar, ser20::make_nvp("emissive_intensity", obj.emissive_intensity_));
136
137 float roughness = obj.get_roughness();
138 float metalness = obj.get_metalness();
139 float bumpiness = obj.get_bumpiness();
140 try_save(ar, ser20::make_nvp("roughness", roughness));
141 try_save(ar, ser20::make_nvp("metalness", metalness));
142 try_save(ar, ser20::make_nvp("bumpiness", bumpiness));
143
144 try_save(ar, ser20::make_nvp("alpha_mode", obj.alpha_mode_));
145 try_save(ar, ser20::make_nvp("alpha_cutoff", obj.alpha_cutoff_));
146
147 try_save(ar, ser20::make_nvp("tiling", obj.tiling_));
148 try_save(ar, ser20::make_nvp("dither_threshold", obj.dither_threshold_));
149
150 try_save(ar, ser20::make_nvp("color_map", obj.color_map_));
151 try_save(ar, ser20::make_nvp("normal_map", obj.normal_map_));
152 try_save(ar, ser20::make_nvp("roughness_map", obj.roughness_map_));
153 try_save(ar, ser20::make_nvp("metalness_map", obj.metalness_map_));
154 try_save(ar, ser20::make_nvp("emissive_map", obj.emissive_map_));
155 try_save(ar, ser20::make_nvp("ao_map", obj.ao_map_));
156
157 // Changes here should be reflected in ex::get_format_version<material>() in asset_extensions.h
158
159}
162
164{
165 try_load(ar, ser20::make_nvp("base_type", ser20::base_class<material>(&obj)));
166 try_load(ar, ser20::make_nvp("base_color", obj.base_color_));
167 try_load(ar, ser20::make_nvp("subsurface_color", obj.subsurface_color_));
168 try_load(ar, ser20::make_nvp("emissive_color", obj.emissive_color_));
169 try_load(ar, ser20::make_nvp("emissive_intensity", obj.emissive_intensity_));
170
171 if constexpr(is_binary_archive<Archive>())
172 {
173 float roughness = obj.get_roughness();
174 float metalness = obj.get_metalness();
175 float bumpiness = obj.get_bumpiness();
176 try_load(ar, ser20::make_nvp("roughness", roughness));
177 try_load(ar, ser20::make_nvp("metalness", metalness));
178 try_load(ar, ser20::make_nvp("bumpiness", bumpiness));
179 obj.set_roughness(roughness);
180 obj.set_metalness(metalness);
181 obj.set_bumpiness(bumpiness);
182
183 try_load(ar, ser20::make_nvp("alpha_mode", obj.alpha_mode_));
184
185 float alpha_cutoff = obj.alpha_cutoff_;
186 try_load(ar, ser20::make_nvp("alpha_cutoff", alpha_cutoff));
187 obj.set_alpha_cutoff(alpha_cutoff);
188 }
189 else
190 {
191 const bool has_alpha_mode = try_load(ar, ser20::make_nvp("alpha_mode", obj.alpha_mode_));
192
193 float alpha_cutoff = obj.alpha_cutoff_;
194 const bool has_alpha_cutoff = try_load(ar, ser20::make_nvp("alpha_cutoff", alpha_cutoff));
195
196 math::vec4 surface_data{};
197 if(try_load(ar, ser20::make_nvp("surface_data", surface_data)))
198 {
199 obj.surface_data_ = surface_data;
200 if(!has_alpha_cutoff)
201 {
202 alpha_cutoff = surface_data.w;
203 }
204 }
205 else
206 {
207 float roughness = obj.get_roughness();
208 if(try_load(ar, ser20::make_nvp("roughness", roughness)))
209 {
210 obj.set_roughness(roughness);
211 }
212
213 float metalness = obj.get_metalness();
214 if(try_load(ar, ser20::make_nvp("metalness", metalness)))
215 {
216 obj.set_metalness(metalness);
217 }
218
219 float bumpiness = obj.get_bumpiness();
220 if(try_load(ar, ser20::make_nvp("bumpiness", bumpiness)))
221 {
222 obj.set_bumpiness(bumpiness);
223 }
224 }
225
226 if(!has_alpha_cutoff)
227 {
228 float legacy_alpha_test = alpha_cutoff;
229 if(try_load(ar, ser20::make_nvp("alpha_test_value", legacy_alpha_test)))
230 {
231 alpha_cutoff = legacy_alpha_test;
232 }
233 }
234
235 obj.alpha_cutoff_ = alpha_cutoff;
236
237 if(!has_alpha_mode)
238 {
239 obj.infer_alpha_mode_from_legacy_cutoff();
240 }
241 else
242 {
243 obj.set_alpha_cutoff(alpha_cutoff);
244 }
245 }
246
247 try_load(ar, ser20::make_nvp("tiling", obj.tiling_));
248 try_load(ar, ser20::make_nvp("dither_threshold", obj.dither_threshold_));
249
250 try_load(ar, ser20::make_nvp("color_map", obj.color_map_));
251 try_load(ar, ser20::make_nvp("normal_map", obj.normal_map_));
252 try_load(ar, ser20::make_nvp("roughness_map", obj.roughness_map_));
253 try_load(ar, ser20::make_nvp("metalness_map", obj.metalness_map_));
254 try_load(ar, ser20::make_nvp("emissive_map", obj.emissive_map_));
255 try_load(ar, ser20::make_nvp("ao_map", obj.ao_map_));
256
257 // Changes here should be reflected in ex::get_format_version<material>() in asset_extensions.h
258}
261} // namespace unravel
Class for physically-based rendering (PBR) materials.
Definition material.h:112
attributes::value_type attribute
Definition reflection.h:19
std::map< std::string, meta_any > attributes
Definition reflection.h:18
BinaryInputArchive iarchive_binary_t
simd::JSONOutputArchive oarchive_associative_t
BinaryOutputArchive oarchive_binary_t
simd::JSONInputArchive iarchive_associative_t
#define REFLECT(cls)
Definition reflection.h:149
#define SAVE_INSTANTIATE(cls, Archive)
#define LOAD(cls)
auto try_save(Archive &ar, ser20::NameValuePair< T > &&t, const hpp::source_location &loc=hpp::source_location::current()) -> bool
#define LOAD_INSTANTIATE(cls, Archive)
constexpr auto is_binary_archive() -> bool
#define SAVE(cls)
auto try_load(Archive &ar, ser20::NameValuePair< T > &&t, const hpp::source_location &loc=hpp::source_location::current()) -> bool
virtual auto as_derived() -> entt::meta_any
virtual auto get_meta_type() const -> entt::meta_type