C

Custom Material Reference

This page explains how to write custom materials.

Overview

The material specification allows writing custom materials and connect those to the studio lighting system. A fragment shader must be written for the material, which must implement all the functions studio uses to calculate the shaded color. The material system also offers ready-made functions to help implementing the material. These functions can be accessed by the shader using #include directive with the name of the function.

The material system supports dielectric and transparent materials, point lights, area lights, ambient occlusion, shadowing, two-sided polygons, index-of-refraction and fragment cutoff(masking).

It is also possible to write custom material without using the boilerplate code, in which case the main function must be implemented in the shader.

For more information on the XML format see Custom Materials and Effects and Effect Reference.

Note: Some characters used in shader code such as '<' break the XML parsing. It is advisable to surround any shader code with <![CDATA[]]> to enable use of such characters. Example usage:

<FragmentShader><![CDATA[ void main() { } ]]></FragmentShader>

Required Functions

These are the functions each fragment shader must implement.

bool evalTwoSided()

This function controls two-sided lighting. Return true to enable two-sided lighting and false to disable it. When two-sided lighting is disabled, only the front-facing material functions are called.

float computeIOR()

This function is called to compute the index of refraction for the material. Return material index of refraction.

float evalCutout()

This function is called when evaluating fragment cutoff(masking) value. The fragment will be discarded if the value returned by this function is less or equal to zero.

vec3 computeNormal()

This function is used to calculate the normal for the fragment. Return the normal of the fragment.

void computeTemporaries()

This function is called to allow the material to calculate any temporary values it needs. It gets called before any other function.

void initializeLayerVariables()

This function is called to allow the material to initialize layer parameters. User should initialize variables to store the lighting values to be computed in the computeFrontLayerColor, computeFrontAreaColor, computeFrontLayerEnvironment, computeBackLayerColor, computeBackAreaColor and computeBackLayerEnvironment.

void initializeLayerVariablesWithLightmap()

This function is called to allow the material to initialize layer parameters.

Note: This function is optional and gets called only if the material uses lightmaps.

vec3 computeFrontMaterialEmissive()

This function is called when the material calculates the Emissive component of the material for the front-facing polygon. Return vec3 RGB emissive value.

vec3 computeBackMaterialEmissive()

This function is called when the material calculates the Emissive component of the material for the back-facing polygon. Return vec3 RGB emissive value.

void computeFrontLayerColor( in vec3 normal, in vec3 lightDir, in vec3 viewDir,
                             in vec3 lightDiffuse, in vec3 lightSpecular,
                             in float materialIOR, in float aoFactor )

This function gets called for every light(excluding area lights) for the front-facing polygon. The material can write their own lighting model or use the provided functions. The functions available for use are microfacetBSDF, physGlossyBSDF and simpleGlossyBSDF. The normal is the fragment normal. The lightDir is the normalized vector from fragment to light in world space.

void computeFrontAreaColor( in int lightIdx, in vec4 lightDiffuse, in vec4 lightSpecular )

This function gets called for every area light for the front-facing polygon.

void computeFrontLayerEnvironment( in vec3 normal, in vec3 viewDir, in float aoFactor )

This function gets called once to calculate the environmental light for the front-facing polygon.

void computeBackLayerColor( in vec3 normal, in vec3 lightDir, in vec3 viewDir,
                            in vec3 lightDiffuse, in vec3 lightSpecular,
                            in float materialIOR, in float aoFactor )

This function gets called for every light(excluding area lights) for the back-facing polygon. The material can write their own lighting model or use the provided functions. The functions available for use are microfacetBSDF, physGlossyBSDF and simpleGlossyBSDF. The normal is the fragment normal. The lightDir is the normalized vector from fragment to light in world space.

void computeBackAreaColor( in int lightIdx, in vec4 lightDiffuse, in vec4 lightSpecular )

This function gets called for every area light for the back-facing polygon.

void computeBackLayerEnvironment( in vec3 normal, in vec3 viewDir, in float aoFactor )

This function gets called once to calculate the environmental light for the back-facing polygon.

vec4 computeLayerWeights( in float alpha )

This function gets called after all lighting have been processed to calculate the final lighting value for the fragment.

vec4 computeGlass( in vec3 normal, in float materialIOR, in float alpha, in vec4 color )

This function gets called only if the material is transparent and non-transmissive after computeLayerWeights has been called.

Note: This function is optional and gets called only if the material is transparent and non-transmissive.

vec4 computeOpacity( in vec4 color )

This function gets called only if the material is transmissive after computeLayerWeights has been called.

Note: This function is optional and gets called only if the material is transmissive.

Mandatory Includes

#include "vertexFragmentBase.glsllib"
#include "SSAOCustomMaterial.glsllib"
#include "sampleLight.glsllib"
#include "sampleProbe.glsllib"
#include "sampleArea.glsllib"

These are required includes for all materials.

Global Variables

These variables are available to the material, but should not be written to.

vec3 normal;
vec3 surfNormal;
vec3 texCoord0;
vec3 tangent;
vec3 binormal;
vec3 viewDir;

These are the fragment shader input variables and can only be read.

vec3 varTexCoord0;
vec3 varTexCoord1;
vec3 varNormal;
vec3 varTangent;
vec3 varBinormal;
vec3 varObjTangent;
vec3 varObjBinormal;
vec3 varWorldPos;
vec3 varObjPos;

Configuration Flags

These are configuration flags, which can be used to enable certain features for the material.

#define QT3DS_ENABLE_UV0 1/0
#define QT3DS_ENABLE_WORLD_POSITION 1/0
#define QT3DS_ENABLE_TEXTAN 1/0
#define QT3DS_ENABLE_BINORMAL 1/0
  • QT3DS_ENABLE_UV0 flag enables texture coordinate 0 variable.
  • QT3DS_ENABLE_WORLD_POSITION flag enables world position variable.
  • QT3DS_ENABLE_TEXTAN flag enables tangent variable.
  • QT3DS_ENABLE_BINORMAL flag enables binormal variable.

Configured Features

These flags are conditionally enabled by the material system when the material is being compiled. The custom shader can use them to enable different code paths for compilation.

#define QT3DS_ENABLE_CG_LIGHTING
#define QT3DS_ENABLE_LIGHT_PROBE
#define QT3DS_ENABLE_SSAO
#define QT3DS_ENABLE_SSDO
#define QT3DS_ENABLE_SSM
#define QT3DS_ENABLE_RNM
  • QT3DS_ENABLE_CG_LIGHTING flag is enabled when lighting is enabled.
  • QT3DS_ENABLE_LIGHT_PROBE flag is enabled when light probe is enabled.
  • QT3DS_ENABLE_SSAO flag is enabled when screen space ambient occlusion is enabled.
  • QT3DS_ENABLE_SSDO flag is enabled when screen space direct occlusion is enabled.
  • QT3DS_ENABLE_SSM flag is enabled when shadow mapping is enabled.
  • QT3DS_ENABLE_RNM flag is enabled when normal-mapped radiosity is enabled.

    Note: Normal-mapped radiosity is not currently supported.

Using Includes to Add Functionality from Shader Library

The material can import functions from the shader library using the #include directive. Some functionality requires the user to define the constant and structures of the functionality. For example, to use the blendColorLayers function the user must specify the mono_xxx constants and the texture_return and color_layer structure (at least once) before including them in their material.

#define mono_alpha 0
#define mono_average 1
#define mono_luminance 2
#define mono_maximum 3

struct texture_return
{
    vec3 tint;
    float mono;
};

struct color_layer
{
    vec3 layer_color;
    float weight;
    int mode;
};

#include "blendColorLayers.glsllib"

texture_return blendColorLayers( in color_layer colorLayer[1], in vec3 base, in int monoSource );

Some includes require additional includes to work correctly. For example fileBumpTexture requires these additional includes and defines:

#include "luminance.glsllib"
#include "monoChannel.glsllib"
#define wrap_clamp 0
#define wrap_repeat 1
#define wrap_mirrored_repeat 2
#include "rotationTranslationScale.glsllib"
#include "transformCoordinate.glsllib"

Includable Functions

microfacetBSDF

#define scatter_reflect 0
#define scatter_transmit 1
#define scatter_reflect_transmit 2

#include "calculateRoughness.glsllib"
#include "microfacetBSDF.glsllib"

vec4 microfacetBSDF( in mat3 tanFrame, in vec3 L, in vec3 V, in vec3 lightSpecular, in float ior,
                     in float roughnessU, in float roughnessV, in int mode )

This function calculates light value for rough surface using microfacet BSDF lighting model. The tanFrame parameter is the tangent-space matrix of the fragment, L and V are the light vector and view vector, lightSpecular is the light specular value, ior specifies the index-of-refraction of the surface, the roughnessU and roughnessV are the roughness factors relative to the texture U and V coordinates and the mode parameter specifies how the scattering is calculated. The return value is 4-component rgba vector.

vec4 microfacetSampledBSDF( in mat3 tanFrame, in vec3 viewDir, in float roughnessU,
                            in float roughnessV, in int mode )

This function calculates light value for rough surface using microfacet BSDF lighting model based on environment map. The environment map is specified with the uEnvironmentMap property. The tanFrame parameter is the tangent-space matrix of the fragment, viewDir is the view vector, roughnessU and roughnessV are the roughness factors relative to the texture U and V coordinates and the mode parameter specifies how the scattering is calculated. The return value is 4-component rgba vector.

physGlossyBSDF

#define scatter_reflect 0
#define scatter_transmit 1
#define scatter_reflect_transmit 2

#include "physGlossyBSDF.glsllib"

vec4 kggxGlossyBSDF( in mat3 tanFrame, in vec3 L, in vec3 V, in vec3 lightSpecular, in float ior,
                     in float roughnessU, in float roughnessV, in int mode )

This function calculates light value for glossy surface using the GGX BSDF. The tanFrame parameter is the tangent-space matrix of the fragment, L and V are the light vector and view vector, lightSpecular is the light specular value, ior specifies the index-of-refraction of the surface, the roughnessU and roughnessV are the roughness factors relative to the texture U and V coordinates and the mode parameter specifies how the scattering is calculated. The return value is 4-component rgba vector.

vec4 wardGlossyBSDF( in mat3 tanFrame, in vec3 L, in vec3 V, in vec3 lightSpecular, in float ior,
                     in float roughnessU, in float roughnessV, in int mode )

This function calculates light value for glossy surface using the Ward BSDF. The tanFrame parameter is the tangent-space matrix of the fragment, L and V are the light vector and view vector, lightSpecular is the light specular value, ior specifies the index-of-refraction of the surface, the roughnessU and roughnessV are the roughness factors relative to the texture U and V coordinates and the mode parameter specifies how the scattering is calculated. The return value is 4-component rgba vector.

simpleGlossyBSDF

#define scatter_reflect 0
#define scatter_transmit 1
#define scatter_reflect_transmit 2

#include "calculateRoughness.glsllib"
#include "simpleGlossyBSDF.glsllib"

vec4 simpleGlossyBSDF( in mat3 tanFrame, in vec3 L, vec3 V, in vec3 lightSpecular, in float ior,
                       in float roughnessU, in float roughnessV, in int mode )

This function calculates light value for glossy surface using the simple BSDF. The tanFrame parameter is the tangent-space matrix of the fragment, L and V are the light vector and view vector, lightSpecular is the light specular value, ior specifies the index-of-refraction of the surface, the roughnessU and roughnessV are the roughness factors relative to the texture U and V coordinates and the mode parameter specifies how the scattering is calculated. The return value is 4-component rgba vector.

vec4 simpleGlossyBSDFEnvironment( in mat3 tanFrame, in vec3 viewDir, in float roughnessU,
                                  in float roughnessV, in int mode )

This function calculates light value for glossy surface using simple BSDF lighting model based on environment map. The environment map is specified with the uEnvironmentMap property. The tanFrame parameter is the tangent-space matrix of the fragment, viewDir is the view vector, roughnessU and roughnessV are the roughness factors relative to the texture U and V coordinates and the mode parameter specifies how the scattering is calculated. The return value is 4-component rgba vector.

sampleProbe

#include "sampleProbe.glsllib"

vec4 sampleGlossyAniso( mat3 tanFrame, vec3 viewDir, float roughnessU, float roughnessV )

Calculates specular sample for the light probe. The tanFrame parameter is the tangent-space matrix of the fragment, viewDir is the view vector and roughnessU and roughnessV are the roughness factors relative to the texture U and V coordinates.

Note: QT3DS_ENABLE_LIGHT_PROBE must be enabled to use this function.

vec4 sampleDiffuse( mat3 tanFrame )

Calculates diffuse sample for the light probe. The tanFrame parameter is the tangent-space matrix of the fragment.

Note: QT3DS_ENABLE_LIGHT_PROBE must be enabled to use this function.

sampleArea

#include "sampleArea.glsllib"

vec4 sampleAreaGlossy( in mat3 tanFrame, in vec3 pos, in int lightIdx, in vec3 viewDir,
                       in float roughnessU, in float roughnessV )

Computes specular sample for an area light. The tanFrame parameter is the tangent-space matrix of the fragment, pos is the fragmentworld position, lightIdx is the index of the light, viewDir is the view vector and roughnessU and roughnessV are the roughness factors relative to the texture U and V coordinates.

vec4 sampleAreaDiffuse( in mat3 tanFrame, in vec3 pos, in int lightIdx )

Computes diffuse sample for an area light. The tanFrame parameter is the tangent-space matrix of the fragment, pos is the fragmentworld position and lightIdx is the index of the light,

Custom Material with Main Function

It is also possible to write the custom material without the rest of the material system. In this case it is not necessary to write all the functions described above. Each pass only has to contain main function.

out vec4 fragColor;
void main()
{
    fragColor = ...
}

Note: Previous versions stated that the closing parenthesis should not be added to the main function. This is no longer the case and the main function should add the closing parenthesis.

Accessing Textures with Image Transformations

When the custom material is generated, uniforms for the image transformation are also generated, as well as accessor functions to get the transformed coordinate and texture sample.

For texture named "basecolor" the generated uniforms and functions are

uniform vec3 basecolorTransformU;
uniform vec3 basecolorTransformV;

vec3 texcoordTransformed_basecolor(vec3 texcoord);
vec4 sampleTransformed_basecolor(vec3 texcoord);

Simple Custom Material Example

<Material name="simplecustom" version="1.0">
    <MetaData >
        <Property formalName="Red" name="red_weight" type="Float" default="1.0" min="0" max="1.0" category="Material"/>
        <Property formalName="Green" name="green_weight" type="Float" default="1.0" min="0" max="1.0" category="Material"/>
        <Property formalName="Blue" name="blue_weight" type="Float" default="1.0" min="0" max="1.0" category="Material"/>
        <Property formalName="Base Color" name="basecolor" type="Texture" filter="linear" minfilter="linearMipmapLinear" clamp="repeat" category="Material"/>
    </MetaData>
    <Shaders type="GLSL" version="330">
    <Shader>
        <Shared></Shared>
        <VertexShader>
        </VertexShader>
        <FragmentShader><![CDATA[
#define UIC_ENABLE_UV0 1
#define UIC_ENABLE_WORLD_POSITION 1
#define UIC_ENABLE_TEXTAN 0
#define UIC_ENABLE_BINORMAL 0

#include "vertexFragmentBase.glsllib"
#include "SSAOCustomMaterial.glsllib"
#include "sampleLight.glsllib"
#include "sampleProbe.glsllib"
#include "sampleArea.glsllib"

// Set shader output.
out vec4 fragColor;

    void main()
    {
        vec4 c = texture(basecolor, varTexCoord0.xy);
        c.rgb *= vec3(red_weight, green_weight, blue_weight);
        fragColor = c;
    }
        ]]></FragmentShader>
    </Shader>
    </Shaders>
    <Passes >
        <ShaderKey value="7"/>
        <LayerKey count="1"/>
    <Pass >
    </Pass>
</Passes>
</Material>

ShaderKey

ShaderKey-element is a magic number specifying how the material gets compiled. The value is the sum of the properties the material needs to enable. Supported properties are:

ConstantDescription
1Dielectric
2Specular
4Glossy
8Cutout
16Refraction
32Transparent
256Transmissive

LayerKey-element specifies how many layers the material has. This is currently ignored.

Available under certain Qt licenses.
Find out more.