Visual Computing Library  devel
Loading...
Searching...
No Matches
material_uniforms.h
1/*****************************************************************************
2 * VCLib *
3 * Visual Computing Library *
4 * *
5 * Copyright(C) 2021-2026 *
6 * Visual Computing Lab *
7 * ISTI - Italian National Research Council *
8 * *
9 * All rights reserved. *
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the Mozilla Public License Version 2.0 as published *
13 * by the Mozilla Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * Mozilla Public License Version 2.0 *
20 * (https://www.mozilla.org/en-US/MPL/2.0/) for more details. *
21 ****************************************************************************/
22
23#ifndef VCL_BGFX_DRAWABLE_UNIFORMS_MATERIAL_UNIFORMS_H
24#define VCL_BGFX_DRAWABLE_UNIFORMS_MATERIAL_UNIFORMS_H
25
26#include <vclib/bgfx/drawable/mesh/pbr_macros.h>
27#include <vclib/bgfx/uniform.h>
28#include <vclib/render/settings/draw_object_settings.h>
29
30#include <vclib/mesh.h>
31
32namespace vcl {
33
42{
43 static const uint N_TEXTURES = toUnderlying(Material::TextureType::COUNT);
44
45 static inline std::array<float, 4> sBaseColor = {1.0, 1.0, 1.0, 1.0};
46
47 // metallic, roughness and occlusion are stored in the B, G and R channels
48 // respectively for consistency with textures
49 static inline std::array<float, 4> sFactorsPack = {
50 1.0, // occlusion strength
51 1.0, // roughness
52 1.0, // metallic
53 1.0 // normal scale
54 };
55
56 // emissive color factor stored in RGB channels, alpha channel is unused so
57 // it can be used to store the alpha cutoff when needed
58 static inline std::array<float, 4> sEmissiveAlphaCutoffPack =
59 {0.0, 0.0, 0.0, 0.5};
60
61 // settings packed in a vec4
62 // .x : pbr settings
63 // .y : texture settings
64 static inline std::array<float, 4> sSettings = {0.0, 0.0, 0.0, 0.0};
65
66 static inline Uniform sBaseColorUniform;
67 static inline Uniform sFactorsPackUniform;
68 static inline Uniform sEmissiveAlphaCutoffPackUniform;
69 static inline Uniform sSettingsUniform;
70
71public:
72 MaterialUniforms() = delete;
73
74 static void set(
75 const Material& m,
77 const std::array<bool, N_TEXTURES>& textureAvailable,
79 bool imageBasedLighting)
80 {
81 uint pbrSettingBits = 0;
82
83 if (vertexColorAvailable) // per-vertex color available
84 pbrSettingBits |= 1 << VCL_PBR_VERTEX_COLOR;
85
86 if (vertexTangentAvailable) // per-vertex tangent available
87 pbrSettingBits |= 1 << VCL_PBR_VERTEX_TANGENT;
88
89 if (m.alphaMode() ==
90 Material::AlphaMode::ALPHA_MASK) { // alpha mode is MASK
91 pbrSettingBits |= 1 << VCL_PBR_IS_ALPHA_MODE_MASK;
92 sEmissiveAlphaCutoffPack[3] = m.alphaCutoff();
93 }
94
95 if (imageBasedLighting) {
96 pbrSettingBits |= 1 << VCL_PBR_IMAGE_BASED_LIGHTING;
97 }
98
99 sSettings[0] = std::bit_cast<float>(pbrSettingBits);
100
101 uint textureSettings = 0;
102
103 for (int i = 0; i < N_TEXTURES; ++i) {
104 if (textureAvailable[i]) {
105 // texture available, uses settings from 0 to N_TEXTURES
106 textureSettings |= 1 << (VCL_PBR_TEXTURE_BASE_COLOR + i);
107 }
108 }
109
110 sSettings[1] = std::bit_cast<float>(textureSettings);
111
112 sBaseColor[0] = m.baseColor().redF();
113 sBaseColor[1] = m.baseColor().greenF();
114 sBaseColor[2] = m.baseColor().blueF();
115 sBaseColor[3] = m.baseColor().alphaF();
116
117 // metallic, roughness and occlusion are stored in the B, G and R
118 // channels respectively for consistency with textures
119 sFactorsPack[0] = m.occlusionStrength();
120 sFactorsPack[1] = m.roughness();
121 sFactorsPack[2] = m.metallic();
122 sFactorsPack[3] = m.normalScale();
123
124 sEmissiveAlphaCutoffPack[0] = m.emissiveColor().redF();
125 sEmissiveAlphaCutoffPack[1] = m.emissiveColor().greenF();
126 sEmissiveAlphaCutoffPack[2] = m.emissiveColor().blueF();
127 }
128
129 static void bind()
130 {
131 // lazy initialization
132 // to avoid creating uniforms before bgfx is initialized
133 if (!sBaseColorUniform.isValid())
134 sBaseColorUniform =
135 Uniform("u_baseColorFactor", bgfx::UniformType::Vec4);
136 if (!sFactorsPackUniform.isValid())
137 sFactorsPackUniform =
138 Uniform("u_FactorsPack", bgfx::UniformType::Vec4);
139 if (!sEmissiveAlphaCutoffPackUniform.isValid())
140 sEmissiveAlphaCutoffPackUniform =
141 Uniform("u_emissiveAlphaCutoffPack", bgfx::UniformType::Vec4);
142 if (!sSettingsUniform.isValid())
143 sSettingsUniform = Uniform("u_settings", bgfx::UniformType::Vec4);
144
145 sBaseColorUniform.bind(&sBaseColor);
146 sFactorsPackUniform.bind(&sFactorsPack);
147 sEmissiveAlphaCutoffPackUniform.bind(&sEmissiveAlphaCutoffPack);
148 sSettingsUniform.bind(&sSettings);
149 }
150};
151
152} // namespace vcl
153
154#endif // VCL_BGFX_DRAWABLE_UNIFORMS_MATERIAL_UNIFORMS_H
The MaterialUniforms class is responsible for managing the shader uniforms related to a material.
Definition material_uniforms.h:42
Represents a Physically-Based Rendering (PBR) material.
Definition material.h:45
@ COUNT
Utility value to get the number of texture types.
A class representing a line segment in n-dimensional space. The class is parameterized by a PointConc...
Definition segment.h:41
The Uniform class wraps a bgfx::UniformHandle and provides a simple interface to set the uniform data...
Definition uniform.h:43
void bind(const void *data) const
Sets the uniform data for the current shader program.
Definition uniform.h:165
bool isValid() const
Checks if the Uniform is valid (i.e., if it has a valid bgfx::UniformHandle).
Definition uniform.h:129