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

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

Factories preferred


ShaderMaterial::Create() auto

Creates a shared instance of ShaderMaterial.

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

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

cpp
std::unordered_map<std::string, UniformValue> uniforms {};

Functions

GetType() Type virtual

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

cpp
Type ShaderMaterial::GetType() const override;

Released under the MIT License.