Using Materials and Shaders | Qt Creator Manual

Qt Creator Manual | Using Materials and Shaders

Qt Creator Manual 4.12.0

Adding Models ◦ Attaching Textures to Materials

# Using Materials and Shaders

Materials and shaders define how object surfaces are rendered in Qt Design Studio and live preview. As you change the properties of materials, new shaders are generated accordingly, and the property values are bound. The complexity of a shader depends on a combination of the properties that are set on it, and the context of the scene itself.

The materials that you used in your imported scenes are imported to Qt Design Studio as Qt Quick 3D components. When you add a View3D component, it contains a DefaultMaterial component. You can use the following predefined Qt Quick 3D components to add materials to models:

- Default material
- Principled material
- Custom material
- Texture

Before a model can be rendered in a scene, it must have at least one material to define how the mesh is shaded. The DefaultMaterial component is the easiest way to define such a material. The PrincipledMaterial component specifies the minimum amount of properties. The CustomMaterial component enables you to access the Qt Quick 3D material library and to implement your own materials.

You can use the Texture component to apply textures to materials. It defines an image and how the image is mapped to meshes in a 3D scene. For more information, see Attaching Textures to Materials.

You can modify material properties in the Properties view, as instructed in the following sections. The availability of the properties depends on the material type.

To enable the material to use vertex colors from the mesh, select the Enable vertex colors check box. These are multiplied by any other colors specified for the material.

You can animate material properties in the Timeline view, as instructed in Creating Animations.

## Blending Colors

To determine how the colors of a model blend with the colors of the models behind it, set the Blend mode property. To make opaque objects occlude the objects behind them, select SourceOver.

For a lighter result, select Screen to blend colors using an inverted multiply or ColorDodge to blend them by inverted division. Color dodge procudes an even lighter result than screen.

For a darker result, select Multiply to blend colors using a multiply or ColorBurn to blend them by inverted division, where the result also is inverted. Color burn produces an even darker result than multiply.

The screen and multiply modes are order-independent, so select them to avoid popping, which can happen when using semi-opaque objects and sorting the back and front faces or models.

For a result with higher contrast, select Overlay, which is a mix of the multiply and screen modes.

## Lighting Materials

To set the lighting method for generating a material, use the Lighting property. Select Fragment lighting to calculate diffuse and specular lighting for each rendered pixel. Some effects, such as Fresnel or a bump map, require fragment lighting.

To skip lighting calculation, select No lighting. This is very fast and quite effective when using image maps that do not need to be shaded by lighting.

To set the base color for the material, use the Diffuse Color property. You can either use the color picker or specify a RBG value. Set the diffuse color to black to create purely-specular materials, such as metals or mirrors. To apply a texture to a material, set it as the value of the Diffuse map property. Using a texture with transparency also applies the alpha channel as an Opacity map. You can set the opacity of the material independently of the model as the value of the Opacity property.

## Self-Illuminating Materials

To set the color and amount of self-illumination for a material, use the Emissive color and Emissive factor properties. In a scene with black ambient lighting, a material with an emissive factor of 0 is black where the light does not shine on it. Setting the emissive factor to 1 shows the material in its diffuse color instead.

To use a Texture for specifying the emissive factor for different parts of the material, set the Emissive map property. Using a grayscale image does not affect the color of the result, while using a color image produces glowing regions with the color affected by the emissive map.

## Using Highlights and Reflections

You can control the highlights and reflections on a material by setting the properties in the Specular group. You can use the color picker or set a RGB value to specify the color used to adjust specular reflections. Use white for no effect

To use a color texture to modulate the amount and the color of specularity across the surface of a material, set the Specular map property. Set the Specular amount property to specify the strength of specularity. This property does not affect the specular reflection map, but it does affect the amount of reflections from a scene's light probe.

Note: Unless your mesh is high-resolution, you may need to use fragment lighting to get good specular highlights from scene lights.

To determine how to calculate specular highlights for lights in the scene, set the Specular model. In addition to the default mode, you can use the GGX or Ward lighting model.

To use a Texture for specular highlighting on a material, set the Reflection map property. When the texture is applied using environmental mapping (not UV mapping), the map appears to be reflecting from the environment as you rotate the model. Specular reflection maps are an easy way to add a high-quality look at a relatively low cost.

To specify an image to use as the specular reflection map, set the Light probe property

Crisp images cause your material to look very glossy. The more you blur your image, the softer your material appears.

To decrease head-on reflections (looking directly at the surface) while maintaining reflections seen at grazing angles, set the Fresnel power property. To select the angles to control, set the Index of refraction property.

To control the size of the specular highlights generated from lights and the clarity of reflections in general, set the Specular roughness property. Larger values increase the roughness, while softening specular highlights and blurring reflections. To control the specular roughness of the material using a Texture, set the Roughness map property.

## Simulating Geometry Displacement

Specify the properties in the Bump/Normal group to simulate fine geometry displacement across the surface of the material. Set the Bump map property to use a grayscale texture for the simulation. Brighter pixels indicate raised regions.

To use a RGB image for simulation, set the Normal map property. The RGB channels indicate XYZ normal deviations.

The amount of displacement is controlled by the Bump amount property.

Bump and normal maps do not affect the silhouette of a model. To affect the silhouette, set the Displacement map property. It specifies a grayscale image used to offset the vertices of geometry across the surface of the material. The Displacement amount property specifies the offset amount.

## Specifying Material Translucency

Set the properties in the Translucency group to control how much light can pass through the material from behind. To use a grayscale texture, specify it as the value of the Translucency map property.

To specify the amount of light wrap for the translucency map, set the Diffuse light wrap property. A value of 0 does not wrap the light at all, while a value of 1 wraps the light all around the object.

To specify the amount of falloff for the translucency based on the angle of the normals of the object to the light source, set the Translucency falloff property.

## Culling Faces

Set the Culling mode property to determine whether the front and back faces of a model are rendered. Culling modes check whether the points in the polygon appear in clockwise or counter-clockwise order when projected onto the screen. If front-facing polygons have a clockwise winding, but the polygon projected on the screen has a counter-clockwise winding, the projected polygon is rotated to face away from the camera and is not rendered. Culling makes rendering objects quicker and more efficient by reducing the number of polygons to draw.

## Creating Custom Materials

The material uses a Shader component to specify shader source and shader stage. These are used with the passes property to create the resulting material. The passes can contain multiple rendering passes and also other commands.

Normally, only the fragment shader needs to be specified as a value for the passes property. The material library generates the vertex shader for the material. The material can also create buffers to store intermediate rendering results.

The shaderInfo property specifies settings for the shader.

To specify that the material state is always dirty, which indicates that the material needs to be refreshed every time it is used, select the alwaysDirty check box.

To specify that the material has refraction, select the hasRefraction check box.

To specify that the material has transparency, select the hasTransparency check box.

## Applying Materials to Models

To apply materials to models:

1. Drag and drop a material component from the Library to a Model component in the Navigator.
2. Select the Model component.
3. In the Properties view, select the material for the model in the Materials list.
4. Select the material component to edit the properties of the material in the Properties view.

Adding Models ◦ Attaching Textures to Materials