Unravel Engine C++ Reference
Loading...
Searching...
No Matches
material.h
Go to the documentation of this file.
1#pragma once
2#include <engine/engine_export.h>
3
4#include "gpu_program.h"
6
7#include <graphics/format.h>
8#include <graphics/graphics.h>
9#include <graphics/texture.h>
10#include <math/math.h>
13
14
15namespace unravel
16{
17
18struct shadow_cutout_state;
19
24enum class cull_type : std::uint32_t
25{
26 none,
27 clockwise,
29};
30
32enum class alpha_mode : std::uint32_t
33{
34 opaque,
35 mask,
36 blend,
37};
38
43class material : public crtp_meta_type<material>
44{
45public:
47
48 using sptr = std::shared_ptr<material>;
49 using wptr = std::weak_ptr<material>;
50 using uptr = std::unique_ptr<material>;
51
52 material() = default;
53 virtual ~material() = default;
54
55
60 virtual auto submit(gpu_program* program) const -> bool;
61
66 auto get_cull_type() const -> cull_type;
67
72 void set_cull_type(cull_type val);
73
81 virtual auto get_render_states(bool apply_cull = true, bool depth_write = true, bool depth_test = true) const
82 -> uint64_t;
83
88 static auto default_color_map() -> asset_handle<gfx::texture>&;
89
94 static auto default_normal_map() -> asset_handle<gfx::texture>&;
95
100 virtual auto clone() const -> material::sptr;
101
102protected:
105};
106
112{
113public:
115
116 auto clone() const -> std::shared_ptr<material> override;
117
122 auto get_base_color() const -> const math::color&
123 {
124 return base_color_;
125 }
126
132 {
133 base_color_ = val;
134 }
135
140 auto get_subsurface_color() const -> const math::color&
141 {
142 return subsurface_color_;
143 }
144
150 {
151 subsurface_color_ = val;
152 }
153
158 auto get_emissive_color() const -> const math::color&
159 {
160 return emissive_color_;
161 }
162
168 {
169 emissive_color_ = val;
170 }
171
172 auto get_emissive_intensity() const -> float
173 {
174 return emissive_intensity_;
175 }
176
178 {
179 emissive_intensity_ = val;
180 }
181
186 auto get_roughness() const -> float
187 {
188 return surface_data_.x;
189 }
190
195 void set_roughness(float roughness)
196 {
197 surface_data_.x = roughness;
198 }
199
204 auto get_metalness() const -> float
205 {
206 return surface_data_.y;
207 }
208
213 void set_metalness(float metalness)
214 {
215 surface_data_.y = metalness;
216 }
217
222 auto get_bumpiness() const -> float
223 {
224 return surface_data_.z;
225 }
226
231 void set_bumpiness(float bumpiness)
232 {
233 surface_data_.z = bumpiness;
234 }
235
240 {
241 return alpha_mode_;
242 }
243
247 void set_alpha_mode(alpha_mode mode);
248
252 auto get_alpha_cutoff() const -> float
253 {
254 return alpha_cutoff_;
255 }
256
260 void set_alpha_cutoff(float cutoff);
261
263 [[nodiscard]] auto uses_alpha_cutout() const -> bool;
264
266 [[nodiscard]] auto casts_shadow() const -> bool;
267
269 [[nodiscard]] auto make_shadow_cutout_state() const -> shadow_cutout_state;
270
272 void infer_alpha_mode_from_legacy_cutoff();
273
278 auto get_surface_data() const -> const math::vec4&
279 {
280 return surface_data_;
281 }
282
287 auto get_surface_data2() const -> math::vec4
288 {
289 math::vec4 surface_data2{};
290
291 surface_data2[0] = metalness_roughness_combined() ? 1.0f : 0.0f;
292 const auto& normal_map = normal_map_ ? normal_map_ : default_normal_map();
293 const auto normal_texture = normal_map.get();
294 surface_data2[1] =
295 normal_texture && gfx::normal_map_needs_z_reconstruction(normal_texture->info.format) ? 1.0f : 0.0f;
296
297 return surface_data2;
298 }
299
300 auto metalness_roughness_combined() const -> bool
301 {
302 return metalness_map_ == roughness_map_;
303 }
304
309 auto get_tiling() const -> const math::vec2&
310 {
311 return tiling_;
312 }
313
318 void set_tiling(const math::vec2& tiling)
319 {
320 tiling_ = tiling;
321 }
322
327 auto get_dither_threshold() const -> const math::vec2&
328 {
329 return dither_threshold_;
330 }
331
336 void set_dither_threshold(const math::vec2& threshold)
337 {
338 dither_threshold_ = threshold;
339 }
340
345 auto get_color_map() const -> const asset_handle<gfx::texture>&
346 {
347 return color_map_;
348 }
349
355 {
356 color_map_ = val;
357 }
358
363 auto get_normal_map() const -> const asset_handle<gfx::texture>&
364 {
365 return normal_map_;
366 }
367
373 {
374 normal_map_ = val;
375 }
376
381 auto get_roughness_map() const -> const asset_handle<gfx::texture>&
382 {
383 return roughness_map_;
384 }
385
391 {
392 roughness_map_ = val;
393 }
394
399 auto get_metalness_map() const -> const asset_handle<gfx::texture>&
400 {
401 return metalness_map_;
402 }
403
409 {
410 metalness_map_ = val;
411 }
412
417 auto get_ao_map() const -> const asset_handle<gfx::texture>&
418 {
419 return ao_map_;
420 }
421
427 {
428 ao_map_ = val;
429 }
430
435 auto get_emissive_map() const -> const asset_handle<gfx::texture>&
436 {
437 return emissive_map_;
438 }
439
445 {
446 emissive_map_ = val;
447 }
448
449private:
450 void sync_surface_alpha_channel();
451
453 math::color base_color_{
454 1.0f,
455 1.0f,
456 1.0f,
457 1.0f
458 };
460 math::color subsurface_color_{
461 0.0f,
462 0.0f,
463 0.0f,
464 0.8f
465 };
467 math::color emissive_color_{
468 0.0f,
469 0.0f,
470 0.0f,
471 0.0f
472 };
474 float emissive_intensity_{1.0f};
475 alpha_mode alpha_mode_{alpha_mode::opaque};
476 float alpha_cutoff_{0.5f};
478 math::vec4 surface_data_{
479 0.3f,
480 0.0f,
481 1.0f,
482 0.25f
483 };
485 math::vec2 tiling_{
486 1.0f,
487 1.0f
488 };
490 math::vec2 dither_threshold_{
491 0.5f,
492 0.0f
493 };
494
497 asset_handle<gfx::texture> normal_map_;
498 asset_handle<gfx::texture> roughness_map_;
499 asset_handle<gfx::texture> metalness_map_;
500 asset_handle<gfx::texture> emissive_map_;
502};
503
504} // namespace unravel
Class representing a GPU program.
Definition gpu_program.h:17
Base class for materials used in rendering.
Definition material.h:44
static auto default_normal_map() -> asset_handle< gfx::texture > &
Gets the default normal map.
Definition material.cpp:32
static auto default_color_map() -> asset_handle< gfx::texture > &
Gets the default color map.
Definition material.cpp:26
std::weak_ptr< material > wptr
Definition material.h:49
std::shared_ptr< material > sptr
Definition material.h:48
void set_cull_type(cull_type val)
Sets the culling type of the material.
Definition material.cpp:48
virtual auto submit(gpu_program *program) const -> bool
Submits the material properties to the GPU program.
Definition material.cpp:38
virtual auto get_render_states(bool apply_cull=true, bool depth_write=true, bool depth_test=true) const -> uint64_t
Gets the render states for the material.
Definition material.cpp:53
cull_type cull_type_
< The culling type for this material.
Definition material.h:104
std::unique_ptr< material > uptr
Definition material.h:50
auto get_cull_type() const -> cull_type
Gets the culling type of the material.
Definition material.cpp:43
virtual auto clone() const -> material::sptr
Clones a materiial.
Definition material.cpp:20
Class for physically-based rendering (PBR) materials.
Definition material.h:112
auto get_surface_data2() const -> math::vec4
Gets additional surface data for the material.
Definition material.h:287
auto get_alpha_mode() const -> alpha_mode
Gets the alpha mode (opaque / mask / blend).
Definition material.h:239
auto get_dither_threshold() const -> const math::vec2 &
Gets the dither threshold of the material.
Definition material.h:327
auto get_emissive_map() const -> const asset_handle< gfx::texture > &
Gets the emissive map of the material.
Definition material.h:435
void set_subsurface_color(const math::color &val)
Sets the subsurface color of the material.
Definition material.h:149
void set_roughness_map(const asset_handle< gfx::texture > &val)
Sets the roughness map of the material.
Definition material.h:390
void set_base_color(const math::color &val)
Sets the base color of the material.
Definition material.h:131
auto metalness_roughness_combined() const -> bool
Definition material.h:300
auto get_emissive_intensity() const -> float
Definition material.h:172
auto get_subsurface_color() const -> const math::color &
Gets the subsurface color of the material.
Definition material.h:140
void set_color_map(const asset_handle< gfx::texture > &val)
Sets the color map of the material.
Definition material.h:354
void set_ao_map(const asset_handle< gfx::texture > &val)
Sets the ambient occlusion map of the material.
Definition material.h:426
auto get_color_map() const -> const asset_handle< gfx::texture > &
Gets the color map of the material.
Definition material.h:345
auto get_bumpiness() const -> float
Gets the bumpiness of the material.
Definition material.h:222
void set_emissive_intensity(float val)
Definition material.h:177
auto get_roughness() const -> float
Gets the roughness of the material.
Definition material.h:186
auto get_metalness() const -> float
Gets the metalness of the material.
Definition material.h:204
void set_dither_threshold(const math::vec2 &threshold)
Sets the dither threshold of the material.
Definition material.h:336
auto get_alpha_cutoff() const -> float
Gets the alpha cutoff used when alpha_mode is alpha_mode::mask.
Definition material.h:252
void set_bumpiness(float bumpiness)
Sets the bumpiness of the material.
Definition material.h:231
void set_emissive_map(const asset_handle< gfx::texture > &val)
Sets the emissive map of the material.
Definition material.h:444
void set_normal_map(const asset_handle< gfx::texture > &val)
Sets the normal map of the material.
Definition material.h:372
auto get_roughness_map() const -> const asset_handle< gfx::texture > &
Gets the roughness map of the material.
Definition material.h:381
void set_emissive_color(const math::color &val)
Sets the emissive color of the material.
Definition material.h:167
auto get_metalness_map() const -> const asset_handle< gfx::texture > &
Gets the metalness map of the material.
Definition material.h:399
auto get_tiling() const -> const math::vec2 &
Gets the tiling factor of the material.
Definition material.h:309
auto get_normal_map() const -> const asset_handle< gfx::texture > &
Gets the normal map of the material.
Definition material.h:363
void set_metalness_map(const asset_handle< gfx::texture > &val)
Sets the metalness map of the material.
Definition material.h:408
void set_tiling(const math::vec2 &tiling)
Sets the tiling factor of the material.
Definition material.h:318
void set_roughness(float roughness)
Sets the roughness of the material.
Definition material.h:195
void set_metalness(float metalness)
Sets the metalness of the material.
Definition material.h:213
auto get_base_color() const -> const math::color &
Gets the base color of the material.
Definition material.h:122
auto get_emissive_color() const -> const math::color &
Gets the emissive color of the material.
Definition material.h:158
auto get_ao_map() const -> const asset_handle< gfx::texture > &
Gets the ambient occlusion map of the material.
Definition material.h:417
auto normal_map_needs_z_reconstruction(texture_format fmt) -> bool
Definition format.cpp:399
Definition bbox.cpp:5
Hash specialization for batch_key to enable use in std::unordered_map.
cull_type
Enum representing the type of culling to be used.
Definition material.h:25
@ counter_clockwise
Cull counter-clockwise faces.
@ clockwise
Cull clockwise faces.
alpha_mode
glTF-aligned surface alpha behavior (default: opaque).
Definition material.h:33
@ opaque
No alpha clip; casts solid shadows.
@ blend
Transparent (lit pass); does not cast shadows.
@ mask
Hard alpha cutoff; casts cutout shadows.
std::vector< math::color > color
#define SERIALIZABLE(T)
Thread-safe handle to an asset.
Per-draw state for alpha-cutout shadow batches (texture + clip threshold).
Definition batch_key.h:105