vkmEngine 1.0.15
A C++ game engine · vkmengine.com
Loading...
Searching...
No Matches
material_asset.h
1#pragma once
2
3#include <array>
4#include <cstddef>
5#include <cstdint>
6
7#include <glm/glm.hpp>
8
9#include "resource/resource.h"
10#include "resource/resource_handle.h"
11
12#include "resource/asset/texture_asset.h"
13
14#include "core/reflect.h"
15
16namespace Vkm::Engine {
17
25#define VKM_MATERIAL_MAPS(X) \
26 X(albedo, albedoTexture, ALBEDO, Base color (RGBA; A feeds AlphaMask / Transparent)) \
27 X(normal, normalTexture, NORMAL, Tangent-space normal map (RGB)) \
28 X(metallicRoughness, metallicRoughnessTexture, METALLIC_ROUGHNESS, Combined roughness (G) + metallic (B) in the glTF layout) \
29 X(metallic, metallicTexture, METALLIC, Separate metalness (R channel)) \
30 X(roughness, roughnessTexture, ROUGHNESS, Separate roughness (R channel)) \
31 X(ao, aoTexture, AO, Ambient occlusion (R channel)) \
32 X(aoMetallicRoughness, aoMetallicRoughnessTexture, AO_METALLIC_ROUGHNESS, Combined AO (R) + roughness (G) + metallic (B)) \
33 X(emission, emissionTexture, EMISSION, Emission (RGB)) \
34 X(height, heightTexture, HEIGHT, Height field for parallax (R channel, white is high)) \
35 X(clearcoat, clearcoatTexture, CLEARCOAT, Clearcoat strength mask (R channel)) \
36 X(transmission, transmissionTexture, TRANSMISSION, Transmission mask (R channel))
37
43#define VKM_MATERIAL_MAP_COUNT(key, member, slot, doc) +1
44inline constexpr std::size_t MATERIAL_MAP_COUNT = 0 VKM_MATERIAL_MAPS(VKM_MATERIAL_MAP_COUNT);
45#undef VKM_MATERIAL_MAP_COUNT
46
54enum class MaterialType : uint8_t {
55 Opaque,
56 Transparent,
57 Unlit,
58 AlphaMask,
59 Count
60};
61
62struct MaterialAsset;
63using MaterialHandle = Handle<MaterialAsset>;
64
71struct MaterialAsset : public Resource {
73 static constexpr uint32_t MAX_LAYERS = 4;
74
75 MaterialType type = MaterialType::Opaque;
77 float alphaCutoff = 0.5f;
79 bool doubleSided = false;
80
82 glm::vec4 albedo = {1,1,1,1};
83 float metallic = 0.0f;
85 float roughness = 0.5f;
86 float ior = 1.5f;
89 float ao = 1.0f;
91 float normalScale = 1.0f;
92
93 glm::vec3 emission = {0,0,0};
94 float emissiveStrength = 1.0f;
95
97 float clearcoat = 0.0f;
98 float clearcoatRoughness = 0.0f;
99 float anisotropy = 0.0f;
100 glm::vec3 anisotropyDirection = {1,0,0};
101
102 glm::vec3 sheenColor = {0,0,0};
103 float sheenRoughness = 0.3f;
104
105 float subsurface = 0.0f;
106 glm::vec3 subsurfaceColor = {1,1,1};
107
109 float transmission = 0.0f;
110 // KHR_materials_volume: transmittance = pow(attenuationColor, path / attenuationDistance),
111 // the path from thicknessFactor.
113 float thicknessFactor = 0.0f;
117 glm::vec3 attenuationColor = {1,1,1};
118
120 float heightScale = 0.0f;
121
125 glm::vec2 uvScale = {1.0f, 1.0f};
126 glm::vec2 uvOffset = {0.0f, 0.0f};
127
134 float boxMapping = 0.0f;
138 bool waves = false;
139
140#define VKM_MATERIAL_MAP_MEMBER(key, member, slot, doc) TextureHandle member;
141 VKM_MATERIAL_MAPS(VKM_MATERIAL_MAP_MEMBER)
142#undef VKM_MATERIAL_MAP_MEMBER
143
152 std::array<MaterialHandle, MAX_LAYERS> layers{};
153 TextureHandle layerWeights;
156 std::array<glm::vec2, MAX_LAYERS> layerTiling{};
157
159 bool layered() const {
160 for (const MaterialHandle& layer : layers) {
161 if (layer) return true;
162 }
163 return false;
164 }
165};
166
167} // namespace Vkm::Engine
168
169VKM_ENUM_NAMES(::Vkm::Engine::MaterialType, "Opaque", "Transparent", "Unlit", "AlphaMask")
170
171// Non-texture fields; texture handles serialize separately as name refs.
172VKM_REFLECT_BEGIN(::Vkm::Engine::MaterialAsset)
173 VKM_F(type)
174 VKM_F(alphaCutoff)
175 VKM_F(doubleSided)
176 VKM_F(albedo)
177 VKM_F(metallic)
178 VKM_F(roughness)
179 VKM_F(ior)
180 VKM_F(ao)
181 VKM_F(normalScale)
182 VKM_F(emission)
183 VKM_F(emissiveStrength)
184 VKM_F(clearcoat)
185 VKM_F(clearcoatRoughness)
186 VKM_F(anisotropy)
187 VKM_F(anisotropyDirection)
188 VKM_F(sheenColor)
189 VKM_F(sheenRoughness)
190 VKM_F(subsurface)
191 VKM_F(subsurfaceColor)
192 VKM_F(transmission)
193 VKM_F(thicknessFactor)
194 VKM_F(attenuationDistance)
195 VKM_F(attenuationColor)
196 VKM_F(heightScale)
197 VKM_F(uvScale)
198 VKM_F(uvOffset)
199 VKM_F(boxMapping)
200 VKM_F(waves)
201VKM_REFLECT_END()
Opaque, type-safe handle for resources, distinguished by ResourceType.
Definition resource_handle.h:16
A complete PBR material.
Definition material_asset.h:71
float metallic
Definition material_asset.h:83
glm::vec3 attenuationColor
Transmittance after one attenuationDistance (white = no tint)
Definition material_asset.h:117
float heightScale
Parallax-occlusion depth scale (0: off; 0.02-0.1 typical)
Definition material_asset.h:120
bool doubleSided
Both faces drawn, a back face lit as its own (glTF doubleSided): cloth, leaves, cards.
Definition material_asset.h:79
glm::vec3 anisotropyDirection
Anisotropy direction (tangent space)
Definition material_asset.h:100
float normalScale
Normal map intensity (0: flat, 1: as authored, >1: exaggerated)
Definition material_asset.h:91
std::array< MaterialHandle, MAX_LAYERS > layers
Materials blended in its place, by layerWeights: what a terrain draws with.
Definition material_asset.h:152
glm::vec3 subsurfaceColor
Subsurface color tint.
Definition material_asset.h:106
float clearcoatRoughness
Clearcoat lobe roughness (0: smooth, 1: rough)
Definition material_asset.h:98
std::array< glm::vec2, MAX_LAYERS > layerTiling
Definition material_asset.h:156
glm::vec4 albedo
Base color (RGB) + opacity (A; Transparent type blends on it)
Definition material_asset.h:82
bool layered() const
Whether it blends layers, as a terrain's does.
Definition material_asset.h:159
static constexpr uint32_t MAX_LAYERS
The most materials a layered one blends (layers).
Definition material_asset.h:73
glm::vec3 sheenColor
Sheen tint, Charlie lobe (0 = disabled); cloth / velvet.
Definition material_asset.h:102
float ior
Definition material_asset.h:86
bool waves
Definition material_asset.h:138
glm::vec3 emission
Emissive color (RGB), linear.
Definition material_asset.h:93
float ao
Definition material_asset.h:89
float boxMapping
Definition material_asset.h:134
float thicknessFactor
Volume thickness in metres (0: thin-walled, no absorption)
Definition material_asset.h:113
float clearcoat
Clearcoat layer strength (0: none, 1: full); attenuates the base layer.
Definition material_asset.h:97
glm::vec2 uvScale
Definition material_asset.h:125
float transmission
Fraction of light refracted instead of diffused (0: opaque, 1: glass)
Definition material_asset.h:109
float roughness
Surface roughness (0: smooth, 1: rough); GGX alpha = roughness^2.
Definition material_asset.h:85
float sheenRoughness
Sheen lobe roughness.
Definition material_asset.h:103
float anisotropy
Anisotropy strength (0: isotropic, 1: fully anisotropic)
Definition material_asset.h:99
float emissiveStrength
HDR multiplier on emission.
Definition material_asset.h:94
float attenuationDistance
Path length at which radiance reaches attenuationColor (m)
Definition material_asset.h:115
TextureHandle layerWeights
Definition material_asset.h:153
float alphaCutoff
AlphaMask: discard below this albedo alpha (glTF default 0.5)
Definition material_asset.h:77
float subsurface
Subsurface scattering strength; skin / wax / leaves.
Definition material_asset.h:105