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->SetUniform("u_Time", timer.GetElapsedSeconds());
my_scene->Add(vglx::Mesh::Create(geometry, material));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(Parameters params);| Parameter | Description |
|---|---|
| params | Initialization parameters defining the shader sources and initial uniform values. |
Factories preferred
ShaderMaterial::Create() std::shared_ptr<ShaderMaterial>
Creates a shared instance of ShaderMaterial.
static std::shared_ptr<ShaderMaterial> 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 UniformList | Initial uniform values. |
| textures std::vector<TextureBinding> |
ShaderMaterial::TextureBinding struct
Represents a texture bound to a shader sampler.
Maps a GLSL sampler name to a specific texture instance. Texture bindings are used to pass image data to the shader for sampling.
| Parameter | Description |
|---|---|
| name std::string | Name of the sampler uniform in the shader. |
| texture std::shared_ptr<Texture> | Shared pointer to the texture resource. |
UniformList typedef
List of named uniform initializers.
Defines a collection of uniform name–value pairs used to initialize a ShaderMaterial at creation time. Each entry maps a GLSL uniform variable name to its initial value.
using vglx::ShaderMaterial::UniformList = std::initializer_list<std::pair<std::string, UniformValue>>UniformValue typedef
Represents a supported uniform value type.
A uniform can be a boolean, 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<bool, int, float, Color, Matrix3, Matrix4, Vector2, Vector3, Vector4>Functions
Identifies this material as Material::Type::ShaderMaterial.
Type ShaderMaterial::GetType() const override;SetTexture() void
Assigns a texture to a shader sampler by name.
Binds a texture resource to a specific sampler uniform. If a texture binding with the specified name already exists, it is replaced. Otherwise, a new binding is added to the material.
Textures are bound to the appropriate texture units the next time the material is used for rendering. The name must match the sampler2D (or other sampler type) declared in the GLSL shader source.
void ShaderMaterial::SetTexture(std::string_view name, std::shared_ptr<Texture> texture);| Parameter | Description |
|---|---|
| name | Name of the sampler variable as declared in the shader. |
| texture | The texture instance to bind. |
SetUniform() void
Sets or updates a uniform value by name.
Associates a uniform variable in the shader program with the given value. If a uniform with the specified name exists its value is updated. Otherwise, a new uniform entry is created.
Uniform values are uploaded to the GPU the next time the material is bound and rendered. The uniform name must exactly match the name declared in the GLSL shader source.
void ShaderMaterial::SetUniform(std::string_view name, UniformValue value);| Parameter | Description |
|---|---|
| name | Name of the uniform variable as declared in the shader. |
| value | Value to assign to the uniform. |