Visual Computing Library  devel
Loading...
Searching...
No Matches
material_uniforms.h
1/*****************************************************************************
2 * VCLib *
3 * Visual Computing Library *
4 * *
5 * Copyright(C) 2021-2025 *
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
29#include <vclib/mesh.h>
30
31namespace vcl {
32
34{
35 static const uint N_TEXTURES = toUnderlying(Material::TextureType::COUNT);
36
37 std::array<float, 4> mBaseColor = {1.0, 1.0, 1.0, 1.0};
38
39 // metallic, roughness and occlusion are stored in the B, G and R channels
40 // respectively for consistency with textures
41 std::array<float, 4> mFactorsPack = {
42 1.0, // occlusion strength
43 1.0, // roughness
44 1.0, // metallic
45 1.0 // normal scale
46 };
47
48 // emissive color factor stored in RGB channels, alpha channel is unused so
49 // it can be used to store the alpha cutoff when needed
50 std::array<float, 4> mEmissiveAlphaCutoffPack = {0.0, 0.0, 0.0, 0.5};
51
52 // settings packed in a vec4
53 // .x : pbr settings
54 // .y : texture settings
55 std::array<float, 4> mSettings = {0.0, 0.0, 0.0, 0.0};
56
57 Uniform mBaseColorUniform =
58 Uniform("u_baseColorFactor", bgfx::UniformType::Vec4);
59
60 Uniform mFactorsPackUniform =
61 Uniform("u_FactorsPack", bgfx::UniformType::Vec4);
62
63 Uniform mEmissiveAlphaCutoffPackUniform =
64 Uniform("u_emissiveAlphaCutoffPack", bgfx::UniformType::Vec4);
65
66 Uniform mSettingsUniform = Uniform("u_settings", bgfx::UniformType::Vec4);
67
68public:
69 MaterialUniforms() = default;
70
71 const std::array<float, 4>& currentBaseColor() const { return mBaseColor; }
72
73 const std::array<float, 4>& currentFactorsPack() const
74 {
75 return mFactorsPack;
76 }
77
78 const std::array<float, 4>& currentEmissiveAlphaCutoffPack() const
79 {
80 return mEmissiveAlphaCutoffPack;
81 }
82
83 const std::array<float, 4>& currentSettings() const { return mSettings; }
84
85 void update(
86 const Material& m,
88 const std::array<bool, N_TEXTURES>& textureAvailable,
90 {
91 uint pbrSettings = 0;
92
93 if (vertexColorAvailable) // per-vertex color available
94 pbrSettings |= 1 << VCL_PBR_VERTEX_COLOR;
95
96 if (vertexTangentAvailable) // per-vertex tangent available
97 pbrSettings |= 1 << VCL_PBR_VERTEX_TANGENT;
98
99 if (m.alphaMode() ==
100 Material::AlphaMode::ALPHA_MASK) { // alpha mode is MASK
101 pbrSettings |= 1 << VCL_PBR_IS_ALPHA_MODE_MASK;
102 mEmissiveAlphaCutoffPack[3] = m.alphaCutoff();
103 }
104
106
107 uint textureSettings = 0;
108
109 for (int i = 0; i < N_TEXTURES; ++i) {
110 if (textureAvailable[i]) {
111 // texture available, uses settings from 0 to N_TEXTURES
112 textureSettings |= 1 << (VCL_PBR_TEXTURE_BASE_COLOR + i);
113 }
114 }
115
117
118 mBaseColor[0] = m.baseColor().redF();
119 mBaseColor[1] = m.baseColor().greenF();
120 mBaseColor[2] = m.baseColor().blueF();
121 mBaseColor[3] = m.baseColor().alphaF();
122
123 // metallic, roughness and occlusion are stored in the B, G and R
124 // channels respectively for consistency with textures
125 mFactorsPack[0] = m.occlusionStrength();
126 mFactorsPack[1] = m.roughness();
127 mFactorsPack[2] = m.metallic();
128 mFactorsPack[3] = m.normalScale();
129
130 mEmissiveAlphaCutoffPack[0] = m.emissiveColor().redF();
131 mEmissiveAlphaCutoffPack[1] = m.emissiveColor().greenF();
132 mEmissiveAlphaCutoffPack[2] = m.emissiveColor().blueF();
133 }
134
135 void bind() const
136 {
137 mBaseColorUniform.bind(&mBaseColor);
138 mFactorsPackUniform.bind(&mFactorsPack);
139 mEmissiveAlphaCutoffPackUniform.bind(&mEmissiveAlphaCutoffPack);
140 mSettingsUniform.bind(&mSettings);
141 }
142};
143
144} // namespace vcl
145
146#endif // VCL_BGFX_DRAWABLE_UNIFORMS_MATERIAL_UNIFORMS_H
A class representing a box in N-dimensional space.
Definition box.h:46
Definition material_uniforms.h:34
Represents a Physically-Based Rendering (PBR) material.
Definition material.h:45
@ COUNT
Utility value to get the number of texture types.
The Uniform class wraps a bgfx::UniformHandle and provides a simple interface to set the uniform data...
Definition uniform.h:43
static float uintBitsToFloat(uint bits)
Utility function to reinterpret the bits of an unsigned integer as a float.
Definition uniform.h:155
void bind(const void *data) const
Sets the uniform data for the current shader program.
Definition uniform.h:144