Unravel Engine C++ Reference
Loading...
Searching...
No Matches
material.cpp
Go to the documentation of this file.
1#include "material.h"
2
3#include "batch_key.h"
4
5#include <graphics/texture.h>
6#include <graphics/uniform.h>
7
8#include <algorithm>
9#include <cmath>
10
11namespace unravel
12{
13
14namespace
15{
16constexpr float k_deferred_dither_alpha = 0.25f;
17constexpr float k_legacy_opaque_cutoff_sentinel = 0.25f;
18} // namespace
19
21{
22 auto mat = std::make_shared<material>(*this);
23 return mat;
24}
25
27{
28 static asset_handle<gfx::texture> texture;
29 return texture;
30}
31
33{
34 static asset_handle<gfx::texture> texture;
35 return texture;
36}
37
38auto material::submit(gpu_program* program) const -> bool
39{
40 return false;
41}
42
44{
45 return cull_type_;
46}
47
49{
50 cull_type_ = val;
51}
52
53auto material::get_render_states(bool apply_cull, bool depth_write, bool depth_test) const -> uint64_t
54{
55 // Set render states.
56 uint64_t states = 0 | BGFX_STATE_WRITE_RGB | BGFX_STATE_WRITE_A | BGFX_STATE_MSAA;
57
58 if(depth_write)
59 {
60 states |= BGFX_STATE_WRITE_Z;
61 }
62
63 if(depth_test)
64 {
65 states |= BGFX_STATE_DEPTH_TEST_LESS;
66 }
67
68 if(apply_cull)
69 {
70 auto cull_type = get_cull_type();
72 {
73 states |= BGFX_STATE_CULL_CCW;
74 }
76 {
77 states |= BGFX_STATE_CULL_CW;
78 }
79 }
80
81 return states;
82}
83
84
85auto pbr_material::clone() const -> material::sptr
86{
87 auto mat = std::make_shared<pbr_material>(*this);
88 return mat;
89}
90
92{
93 alpha_mode_ = mode;
94 sync_surface_alpha_channel();
95}
96
98{
99 alpha_cutoff_ = std::clamp(cutoff, 0.0f, 1.0f);
100 if(alpha_mode_ == alpha_mode::mask && alpha_cutoff_ <= 0.0f)
101 {
102 alpha_cutoff_ = 0.5f;
103 }
104 sync_surface_alpha_channel();
105}
106
107void pbr_material::sync_surface_alpha_channel()
108{
109 surface_data_.w = alpha_mode_ == alpha_mode::mask ? alpha_cutoff_ : k_deferred_dither_alpha;
110}
111
113{
114 return alpha_mode_ == alpha_mode::mask;
115}
116
117auto pbr_material::casts_shadow() const -> bool
118{
119 return alpha_mode_ != alpha_mode::blend;
120}
121
123{
125 const auto& color_map = get_color_map();
126 const auto& albedo = color_map ? color_map : default_color_map();
127 state.color_map = albedo.get();
128 state.alpha_test_value = alpha_cutoff_;
129 state.base_color = get_base_color();
130 state.tiling = get_tiling();
131 return state;
132}
133
135{
136 if(std::abs(alpha_cutoff_ - k_legacy_opaque_cutoff_sentinel) > 0.001f
137 || std::abs(surface_data_.w - k_legacy_opaque_cutoff_sentinel) > 0.001f)
138 {
139 if(std::abs(alpha_cutoff_ - k_legacy_opaque_cutoff_sentinel) <= 0.001f)
140 {
141 alpha_cutoff_ = surface_data_.w;
142 }
144 return;
145 }
146
148}
149
150} // 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::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
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
auto make_shadow_cutout_state() const -> shadow_cutout_state
Snapshot of shader state needed by the shadow cutout pass.
Definition material.cpp:122
auto get_color_map() const -> const asset_handle< gfx::texture > &
Gets the color map of the material.
Definition material.h:345
void infer_alpha_mode_from_legacy_cutoff()
Infer alpha_mode when loading legacy assets that only stored a cutoff value.
Definition material.cpp:134
auto casts_shadow() const -> bool
True when this material should contribute to shadow maps.
Definition material.cpp:117
auto uses_alpha_cutout() const -> bool
True when alpha_mode is alpha_mode::mask (hard alpha clip + cutout shadows).
Definition material.cpp:112
auto clone() const -> std::shared_ptr< material > override
Definition material.cpp:85
auto get_tiling() const -> const math::vec2 &
Gets the tiling factor of the material.
Definition material.h:309
void set_alpha_mode(alpha_mode mode)
Sets the alpha mode.
Definition material.cpp:91
void set_alpha_cutoff(float cutoff)
Sets the alpha cutoff for alpha_mode::mask materials.
Definition material.cpp:97
auto get_base_color() const -> const math::color &
Gets the base color of the material.
Definition material.h:122
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.
Thread-safe handle to an asset.
Per-draw state for alpha-cutout shadow batches (texture + clip threshold).
Definition batch_key.h:105
std::shared_ptr< gfx::texture > color_map
Definition batch_key.h:106