Skip to content

Material System

Eddie S edited this page Oct 3, 2020 · 11 revisions

Material System

Material system allows you to create a variety of unique materials.

Material

Material determines how object looks like. Each unique material has its own material shader described by index.

Properties

  • Color

    Defines the color of the Material, takes glm::vec4 value.

  • Emissive Color

    Defines the color that your Material will appear to glow, takes glm::vec4 value.

  • Diffuse

    Defines the color using texture.

  • Specular

    Defines the ability to reflect light using texture.

  • Normal

    Defines the normal map is used to provide surface details.

  • Displacement

    Defines a texture is used to manipulate vertices of a mesh in world space.This is useful for making objects move, change shape, rotate and a variety of other effects.

  • Emissive mask

    Defines which parts of Material will appear to glow. Zero values from texture means this fragment should be shaded as usual, non zero values means emitting light color. If Emissive color presents, it is multiplies. Takes a texture.

  • Blend mask

    Defines which parts of Material will be visible. Takes a texture r value. Values more than 0.5 is visible, less than 0.5 - discarded. Used with blending modes.

  • Shininess

    Defines shininess reflection value of material. Default is 16.0.

  • Metallic

    Defines 'metal-like' surface. Nonmetals have Metallic values of 0 and metals have Metallic values of 1.

  • Roughness

    The Roughness input controls how rough surface is.

  • Metallic texture

    Metallic property based on texture.

  • Roughness texture

    Roughness property based on texture.

BlendingTransparency

Blend Mode describes how the current Material will be blended with others in the background.

  • Opaque

    Opaque material will draw on top.

  • Translucent

    Alpha blending. Final color = Material color * alpha + Dest color * (1 - alpha)

  • Additive

    Final color = Material color + Dest color

  • Modulate

    Final color = Material color * Dest color

Shading

  • Unlit

    Does not respond to light.

  • Lit

    Does respond to light.


[[nodiscard]] UniformValue<glm::vec4>& getColor() const;

[[nodiscard]] UniformValue<glm::vec4>& getEmissiveColor() const;

[[nodiscard]] UniformValue<float>& getShininess() const;

[[nodiscard]] UniformValue<float>& getMetallic() const;

[[nodiscard]] UniformValue<float>& getRoughness() const;

[[nodiscard]] UniformSampler& getMetallicTexture() const;

[[nodiscard]] UniformSampler& getRoughnessTexture() const;

[[nodiscard]] UniformSampler& getDiffuse() const;

[[nodiscard]] UniformSampler& getSpecular() const;

[[nodiscard]] UniformSampler& getNormal() const;

[[nodiscard]] UniformSampler& getDisplacement() const;

[[nodiscard]] UniformSampler& getEmissiveMask() const;

[[nodiscard]] UniformSampler& getBlendMask() const;

> Gets specified property, returns corresponding UniformValue/UniformSampler if exists, otherwise throws an exception.

[[nodiscard]] uint64_t getShaderIndex() const noexcept

> Gets material's shader index.

Blending& getBlending() noexcept

> Gets blending to change.

const std::string& getName() const noexcept

> Gets material's name.

[[nodiscard]] virtual Material* clone() const noexcept

> Makes a copy of the material.

CustomMaterial

CustomMaterial allows you to write your own shader code in material to get any material you like.

  • Vertex shader code

    You can access any of present material's property in vertex shader to make you calculations:

    There are const variables contained by uniform buffer object:

    vec4 material_color

    vec4 material_emissive_color

    sampler2D material_diffuse

    sampler2D material_specular

    sampler2D material_normal

    sampler2D material_displacement

    sampler2D material_emissive_mask

    sampler2D material_blend_mask

    sampler2D material_metallic_texture

    sampler2D material_roughness_texture

    float material_shininess

    float material_metallic

    float material_roughness

    There are non-const additional model variables:

    vec4 vertex_position

    Defines current vertex position before any space transformations.

    mat4 model_matrix

    Defines model space matrix.

  • Fragment shader code

    There are non const prequaried accessable variables:

    vec4 mat_color

    vec3 mat_emissive_color

    vec4 mat_diffuse

    float mat_specular

    vec3 mat_normal

    vec3 mat_displacement

    vec3 mat_emissive_mask

    float mat_blend_mask

    float mat_shininess

    float mat_metallic

    float mat_roughness

  • Shared variables in uniform buffer

    Also there are current scene data const variables that can be used too: (Maybe one day it is will be non const)

    mat4 projection

    Projection space matrix.

    mat4 view

    View space matrix.

    mat4 VP

    Projection View space matrix.

    vec4 camera_position

    Camera current position.

    vec4 ambient_color

    Ambient scene color.

  • Uniforms

    Uniforms allow you to add your own custom properties to material shader. In case you want to change color in shader based on time, you need to add time uniform in your custom material and update it.

Builders

Builders is used to construct materials.

  • MaterialBuilder

    MaterialBuilder& add(PropertyType type, std::shared_ptr<Texture> texture);

    MaterialBuilder& add(PropertyType type, const glm::vec4& value);

    MaterialBuilder& add(PropertyType type, float value);

    MaterialBuilder& setBlending(Blending blending) noexcept;

    MaterialBuilder& setShading(Shading shading) noexcept;

    Sets specified parameters.

       enum class MaterialShader {
        Default,
        Deferred,
        ForwardPlus,
        DirectionalShadow,
        PointShadow,
        ColorPicker
       };
    
       enum class ModelShader {
        Model,
        Skeletal,
        Instanced,
        SkeletalInstanced,
       };
    
       using RequiredMaterialShaders = std::vector<MaterialShader>;
       using RequiredModelShaders = std::vector<ModelShader>;
    
    

std::shared_ptr<Material> build(const std::string& name, const RequiredModelShaders& model_shaders = RequiredModelShaders{ ModelShader::Model }, const RequiredMaterialShaders& material_shaders = RequiredMaterialShaders{ MaterialShader::Default });

Builds required shaders for specified model&material types, returns material.

  • CustomMaterialBuilder

    void addUniform(std::string name, Uniform* uniform);

    Adds uniform to construction material.

    void setVertexCode(const std::string& vs_code) noexcept;

    void setFragmentCode(const std::string& fs_code) noexcept;

    Sets specified code in shaders.

MaterialInstance

  • BaseMaterial

  • Layers