Shaders Introduction
In this tutorial you will learn how shaders work in SkyRenderer. We will describe types of shaders implemented
in SkyRenderer and explain why except PBRShader, we have specialized and utility shaders available.
Agenda:
- Shader
- Physically Based Rendering
- Specialized Shaders
- Utility Shaders
- Parameter Provider
Shader
A procedure responsible for shading process describing how particular surface will physically behave under
the rays. In SkyRenderer we have many types of shaders implemented like:
- PBRShader (Physically Based Rendering Shader)
- SkinShader
- PhongShader
- TranslucentShader
- GlassShader
- many more
Most effects are covered by PBRShader since it is the most universal shader, however for some materials it is
worth to use specialized shaders for a higher quality of the output renders.
Physically Based Rendering
Bidirectional Scattering Distribution Function (BSDF in short), names a model which describes complex
surface-light interactions. BSDF consists of Bidirectional Reflectance Distribution Function (BRDF) and
Bidirectional Transmittance Distribution Function (BTDF) functions.
Physically based rendering's principle is to approximate real material's BSDF function to achieve photorealistic
results and reduce computational complexity. What is more, hyperparameters used in that model are defined
in a more intuitive and handful way. In order to reduce computational complexity additional pre-made maps
(e.g. normal map, specular map, etc.) are used.
In SkyRenderer the default Shader is PBRShader that focuses mainly on BRDF function.
BRDF is described by specular and diffuse component. Based on materials' properties different components
prevail. For fully metallic materials BRDF function consists mainly of specular factor, for non-metallic one
a diffuse factor plays main role. PBRShader implemented in SkyRenderer utilizes also a microfacet BRDF model,
that considers a surface as not smooth on micro-level, providing even more photorealistic results.
For more information check PBRShader case.
Specialized Shaders
Since some physical phenomena are robust and PBRShader generalization may not be enough to provide the highest
quality for particular materials, we have decided to implement specialized shaders, like SkinShader, HairShader,
MetalShader, GlassShader, TranslucentShader and many others. Thanks to that, some materials can be shaded
with Shaders specially designed for them. For some Shaders, optimizations were done, to provide faster rendering.
For the others implementation of robust models, like subsurface scattering, or complex index of refraction were
introduced improving quality of rendered materials.
Utility Shaders
Some Shaders introduced in SkyRenderer have strictly utility purposes, like GeomTestShader, PreviewShader and
others. They enable visualization of some technical properties of rendered meshes - how normal vectors are
oriented, if uv mapping is done properly and others.
For more information check Shader-specific cases.
Parameter Provider
Each shader has their own Parameter Provider class, that passes values of shader-specific parameters to the
shader procedure. To create a Parameter Provider we call shader's .create_parameter_provider() method.
In case we do not specify parameter values, default one are used. Available parameters and their default values
can be checked in shader's documentation.
We will walk through a simple process of creating a shader instance and modify default values of parameters.
We will use custom scene for that.
from skyrenderer.cases.utils import MaterialsSceneComposer
scene_composer = MaterialsSceneComposer(antialiasing_level=256)
scene_composer.setup_scene(render_width=1600, render_height=1000)
scene_composer.visualize(scene_composer.get_render())
2025-02-13 14:59:15,498 | skyrenderer.scene.renderer_context | INFO: Root paths:
- root path: /home/skyengine/anaconda/lib/python3.6/site-packages/skyrenderer
- assets path: /dli/mount/assets
- config path: /home/skyengine/anaconda/lib/python3.6/site-packages/skyrenderer/config
- gpu sources path: /home/skyengine/anaconda/lib/python3.6/site-packages/skyrenderer/optix_sources/sources
- cache path: /dli/mount/cache
- ptx cache path: compiled_ptx/ptx
- ocio path: ocio_configs/aces_1.2/config.ocio
2025-02-13 14:59:17,937 | skyrenderer.utils.time_measurement | INFO: Setup time: 2.41 seconds
2025-02-13 14:59:20,649 | skyrenderer.utils.time_measurement | INFO: Context update time: 2.71 seconds
2025-02-13 14:59:22,784 | skyrenderer.utils.time_measurement | INFO: Key points calculation time: 0 ms
2025-02-13 14:59:22,785 | skyrenderer.utils.time_measurement | INFO: Render time: 2.13 seconds
Let's create a Material Definition using PBRShader and assign this material to the shaderball.
To assign the material we will use RendererContext's .set_material_definition() method.
from skyrenderer.scene.scene_layout.layout_elements_definitions import MaterialDefinition
from skyrenderer.basic_types.procedure.shader.basic_shaders import PBRShader
pbr_shader = PBRShader(scene_composer.renderer_context)
material_definition = MaterialDefinition(shader=pbr_shader)
scene_composer.renderer_context.set_material_definition(
node_name="shaderball_GEO", material_definition=material_definition
)
scene_composer.visualize(scene_composer.get_render())
2025-02-13 14:59:23,504 | skyrenderer.utils.time_measurement | INFO: Setup time: 65 ms
2025-02-13 14:59:26,310 | skyrenderer.utils.time_measurement | INFO: Context update time: 2.81 seconds
2025-02-13 14:59:29,066 | skyrenderer.utils.time_measurement | INFO: Key points calculation time: 0 ms
2025-02-13 14:59:29,066 | skyrenderer.utils.time_measurement | INFO: Render time: 2.75 seconds
Let's change a color for created PBRShader instance to blue:
from skyrenderer.basic_types.procedure import PBRShader
blue_parameter_provider = PBRShader.create_parameter_provider(
scene_composer.renderer_context, base_color=scene_composer.SKY_ACCENT_COLOUR
)
blue_material_definition = MaterialDefinition(shader=pbr_shader, parameter_set=blue_parameter_provider)
scene_composer.renderer_context.set_material_definition(
node_name="shaderball_GEO", material_definition=blue_material_definition
)
scene_composer.visualize(scene_composer.get_render())
2025-02-13 14:59:29,749 | skyrenderer.utils.time_measurement | INFO: Setup time: 59 ms
2025-02-13 14:59:32,601 | skyrenderer.utils.time_measurement | INFO: Context update time: 2.85 seconds
2025-02-13 14:59:35,198 | skyrenderer.utils.time_measurement | INFO: Key points calculation time: 0 ms
2025-02-13 14:59:35,199 | skyrenderer.utils.time_measurement | INFO: Render time: 2.60 seconds
Each shader has a vast range of parameters that are available to manipulate. To check parameters' values can be
changed check shader's documentation.
Summary
In this section you have learnt:
- Shaders are procedures responsible for describing how particular surface will physically behave under the rays.
- Shaders parameters can be changed via Parameter Providers.
- SkyRenderer by default uses Physically Based Rendering Shader (PBRShader).
- SkyRenderer has implemented Specialized improving quality of renders for some materials.
- SkyRenderer has implemented Utility Shaders enabling verification of technical aspects of meshes.
- Parameters of each Shader can be checked in their documentation and their usage is represented in
shader-specific cases. - Shader can be assigned to mesh via Material Definition.