ShaderMaterial
Shader material provides full control over the shading pipeline by allowing custom vertex and fragment shader code. It is intended for advanced rendering effects, experimental lighting models, and post-processing passes that go beyond built-in material types.
Uniforms can be defined dynamically at creation time and later updated by name. Supported uniform types include scalar, vector, matrix, and color values.
auto material = vglx::ShaderMaterial::Create({
.vertex_shader = vert_source,
.fragment_shader = frag_source,
.uniforms = {
{"u_Time", 0.0f},
{"u_Resolution", Vector2::Zero()}
}
});
// Update a uniform each frame
material->uniforms["u_Time"] = timer.GetElapsedSeconds();
auto mesh = vglx::Mesh::Create(geometry, material);
scene->Add(mesh);INFO
Derives from Material and inherits all public properties and methods.
Construction
Constructors
ShaderMaterial()
Constructs a shader material from custom GLSL source strings.
ShaderMaterial::ShaderMaterial(const Parameters& params);| Parameter | Description |
|---|---|
| params | Initialization parameters defining the shader sources and initial uniform values. |
Factories preferred
ShaderMaterial::Create() auto
Creates a shared instance of ShaderMaterial.
static auto ShaderMaterial::Create(const Parameters& params);| Parameter | Description |
|---|---|
| params | Initialization parameters defining the shader sources and initial uniform values. |
Types
ShaderMaterial::Parameters struct
Parameters for constructing a ShaderMaterial object.
| Parameter | Description |
|---|---|
| vertex_shader std::string | Vertex shader code. |
| fragment_shader std::string | Fragment shader code. |
| uniforms std::unordered_map<std::string, UniformValue> | Initial uniform values. |
UniformValue typedef
Represents a supported uniform value type.
A uniform can be an integer, float, color, or a vector/matrix type. Values are matched by name when uploaded to the shader program.
using vglx::ShaderMaterial::UniformValue = std::variant<int, float, Color, Matrix3, Matrix4, Vector2, Vector3, Vector4>Properties
uniforms std::unordered_map<std::string, UniformValue>
Map of uniform names to their current values. To update a uniform, modify this map directly using the uniform name. Updates take effect on the next frame when the material is bound.
std::unordered_map<std::string, UniformValue> uniforms {};Functions
Identifies this material as Material::Type::ShaderMaterial.
Type ShaderMaterial::GetType() const override;