NcEngine
Material.h
Go to the documentation of this file.
1
5#pragma once
6
9#include "ncmath/Vector.h"
10
11#include <limits>
12#include <utility>
13
14namespace nc
15{
16namespace graphics
17{
18class MaterialRegistry;
19} // namespace graphics
20
22using MaterialInstanceHandle = uint32_t;
23
25constexpr auto NullMaterialInstanceHandle = std::numeric_limits<MaterialInstanceHandle>::max();
26
29{
30 using type = uint64_t;
31
32 static constexpr auto UniShadow = type{1 << 0};
33 static constexpr auto PointShadow = type{1 << 1};
34 static constexpr auto Depth = type{1 << 2};
35 static constexpr auto Toon = type{1 << 3};
36 static constexpr auto ToonStencilWrite = type{1 << 4};
37 static constexpr auto StencilOutline = type{1 << 5};
38 static constexpr auto Normals = type{1 << 6};
39};
40
42using MaterialPassFlags = MaterialPassFlag::type;
43
45constexpr auto ShadowedToonMaterial = MaterialPassFlag::UniShadow | MaterialPassFlag::PointShadow | MaterialPassFlag::Depth | MaterialPassFlag::Toon | MaterialPassFlag::ToonStencilWrite | MaterialPassFlag::StencilOutline | MaterialPassFlag::Normals;
46
49{
50 Vector4 gradientStart = Vector4::One();
51 Vector4 gradientEnd = Vector4::One();
52 Vector4 primaryColor = Vector4::One();
53 Vector4 secondaryColor = Vector4::One();
54
55 Vector4 tertiaryColor = Vector4::One();
56 Vector4 stencilOutlineColor = Vector4::One();
57
58 float normalIntensity = 1.0f;
59 float hatchTiling = 1.0f;
60 float gradientAmount = 0.1f;
61 float reflectivity = 0.0f;
62
63 float stencilOutlineWidth = 0.0f;
67
68 bool useTextureNormals = true;
69 bool useFlatShading = true;
70 bool useColorOverride = false;
71 bool useHatchTexture = false;
72
73 bool useStencilOutline = false;
74};
75
78{
79 std::string name = "DefaultMaterial";
82};
83
86{
87 public:
88 explicit MaterialInstance(const MaterialDesc& desc = MaterialDesc{});
89 MaterialInstance(MaterialInstance&& other) noexcept;
90 MaterialInstance& operator=(MaterialInstance&& other) noexcept;
91 ~MaterialInstance() noexcept;
92
93 MaterialInstance(const MaterialInstance&) = delete;
94 MaterialInstance& operator=(const MaterialInstance&) = delete;
95
97 auto Clone() const -> MaterialInstance;
98
100 auto GetName() const -> std::string_view;
101 void SetName(std::string_view name);
102
104 auto GetPasses() const -> MaterialPassFlags;
105
107 auto GetProperties() const -> const MaterialProperties&;
108 void SetProperties(const MaterialProperties& properties);
109
112 {
113 return m_handle;
114 }
115
118
119 private:
121 static inline graphics::MaterialRegistry* s_registry = nullptr;
122
123 MaterialInstanceHandle m_handle;
124
125 void Release() noexcept;
126};
127
128inline MaterialInstance::MaterialInstance(MaterialInstance&& other) noexcept
129 : m_handle{std::exchange(other.m_handle, NullMaterialInstanceHandle)}
130{
131}
132
133inline MaterialInstance& MaterialInstance::operator=(MaterialInstance&& other) noexcept
134{
135 if (this != &other)
136 {
137 Release();
138 m_handle = std::exchange(other.m_handle, NullMaterialInstanceHandle);
139 }
140
141 return *this;
142}
143
144inline MaterialInstance::~MaterialInstance() noexcept
145{
146 Release();
147}
148} // namespace nc
MaterialPassFlag::type MaterialPassFlags
Set of flags indicating a MaterialInstance's enabled passes.
Definition: Material.h:42
constexpr auto NullMaterialInstanceHandle
Null identifier for a MaterialInstance.
Definition: Material.h:25
uint32_t MaterialInstanceHandle
Identifier for a MaterialInstance.
Definition: Material.h:22
constexpr auto ShadowedToonMaterial
Default passes for a toon material.
Definition: Material.h:45
Owning wrapper around a material in GPU memory.
Definition: Material.h:86
auto GetHandle() const -> MaterialInstanceHandle
Get the instance's handle.
Definition: Material.h:111
auto GetProperties() const -> const MaterialProperties &
Get the instance's handle.
void SetProperties(const MaterialProperties &properties)
Get the instance's handle.
static auto OnPassesChanged() -> nc::Signal< MaterialInstanceHandle, MaterialPassFlags > &
Raised when passes change.
auto Clone() const -> MaterialInstance
Create a new MaterialInstance from this instance's properties.
friend class graphics::MaterialRegistry
Get the instance's handle.
Definition: Material.h:120
An event source supporting multiple Connections.
Definition: Signal.h:65
Properties for constructing a MaterialInstance.
Definition: Material.h:78
Material pass flags.
Definition: Material.h:29
Properties of a MaterialInstance passed to shaders.
Definition: Material.h:49
A four component vector.
Definition: Vector.h:48
Definition: AssetViews.h:66