Skip to content

ShaderMaterial

Material rendered using user-defined GLSL shaders.

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.

cpp
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.

cpp
ShaderMaterial::ShaderMaterial(Parameters params);
ParameterDescription
paramsInitialization parameters defining the shader sources and initial uniform values.

Factories preferred


ShaderMaterial::Create() std::shared_ptr<ShaderMaterial>

Creates a shared instance of ShaderMaterial.

cpp
static std::shared_ptr<ShaderMaterial> ShaderMaterial::Create(const Parameters& params);
ParameterDescription
paramsInitialization parameters defining the shader sources and initial uniform values.

Types

ShaderMaterial::Parameters struct

Parameters for constructing a ShaderMaterial object.

ParameterDescription
vertex_shader std::stringVertex shader code.
fragment_shader std::stringFragment shader code.
uniforms UniformListInitial 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.

ParameterDescription
name std::stringName 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.

cpp
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.

cpp
using vglx::ShaderMaterial::UniformValue = std::variant<bool, int, float, Color, Matrix3, Matrix4, Vector2, Vector3, Vector4>

Functions

GetType() Type virtual

Identifies this material as Material::Type::ShaderMaterial.

cpp
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.

cpp
void ShaderMaterial::SetTexture(std::string_view name, std::shared_ptr<Texture> texture);
ParameterDescription
nameName of the sampler variable as declared in the shader.
textureThe 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.

cpp
void ShaderMaterial::SetUniform(std::string_view name, UniformValue value);
ParameterDescription
nameName of the uniform variable as declared in the shader.
valueValue to assign to the uniform.

Released under the MIT License.