# Super Dynamic Lighting Shader

This is an advanced version of the Dynamic Lighting Shader.

{% embed url="<https://youtu.be/mdfkya7F8ZQ>" %}

Additional features:

* Supported Texture channels and their transformations: diffuse, transparency, specular, reflection, displacement, and normal map
* Support for shadow maps from infinite and spot affected lights
* Selection of Lambert or Oren-Nayar shading for diffuse
* Options for specular reflectivity: Phong, Anisotropic and Cook Torrance
* Rim lighting
* Vertex displacement
* Reflection spherical texturing
* Support for a second UV set with assigned LightMap textures per model
* MatCap diffuse shading
* Support for global ambient and fog settings

{% hint style="info" %}
To get access to a wider list of texture channels, you have to put checkbox "Advanced Mode" in the material layout
{% endhint %}

The shader is taking in a count texture transformation as well for channels: diffuse, transparency, displacement, specular, reflect and normal map.

<figure><img src="https://21658545-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FYKfPCkQKpHW2adXsZwun%2Fuploads%2FmvDh2nAX9E2861YCYS9N%2Fimage.png?alt=media&#x26;token=4ecde8cb-bf24-447f-bc31-08c1dc4c5e0f" alt=""><figcaption><p>Texture transformation in a texture layout</p></figcaption></figure>

### Shader Navigator Layout

<figure><img src="https://21658545-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FYKfPCkQKpHW2adXsZwun%2Fuploads%2FhMgb56A3GfCKJpDyYuCx%2Fimage.png?alt=media&#x26;token=61841d9b-7c30-43ae-a8f5-29739f9bb138" alt=""><figcaption><p>navigator layout view of super dynamic lighting shader</p></figcaption></figure>

### Shader Properties

<figure><img src="https://21658545-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FYKfPCkQKpHW2adXsZwun%2Fuploads%2F2zepGVrBMaJnyMyOPnXu%2Fimage.png?alt=media&#x26;token=5a554c9d-08db-4ce2-a1ff-1ac1549d69c1" alt=""><figcaption><p>properties view of super dynamic lighting shader</p></figcaption></figure>

| Property              | Value                    | Description                                                                                         |
| --------------------- | ------------------------ | --------------------------------------------------------------------------------------------------- |
| Reload Shaders        | Action                   | a development button, to trigger an internal glsl shaders reload                                    |
| Use Scene Lights      | boolean                  | switch between global list of scene lights and local list of affected lights in the shader          |
| AffectingLights       | List of lights           | defines which lights should be used for the shader. That the option required for the shadow mapping |
| Transparency          | Enum alpha mode          | defines a more of transparency                                                                      |
| TransparencyFactor    | Number                   | multiplier of transparency                                                                          |
| Shadows               | boolean                  | a toggle to use shadow maps                                                                         |
| ShadowMapSize         | Number                   | defines a map of shadow map. It's prefered to have it power of two (1024, 2048, 4096)               |
| ShadowCasters         | List of models           | list of models to be used as shadow casters. That is a requirement for the shadow mapping.          |
| Offset Factor         | Number                   | for shadow map rendering, multiplies the max depth slope of the polygon                             |
| Offset Units          | Number                   | fr shadow map rendering, a fixed const offset in depth units for all polygons                       |
| Switch Albedo to sRGB | Boolean                  | switch between sRGB and linear color space, a gamma correction for used material textures           |
| Force Update Textures | Boolean                  | update on gpu for the used material textures every frame                                            |
| Use Rim Lighting      | Boolean                  | emulation of back lighting                                                                          |
| Rim Power             | Number                   | back lighting power                                                                                 |
| Rim Color             | Color                    | back lighting color                                                                                 |
| Use MatCap            | Boolean                  | use a Material Capture texture, see a correspondent section in the documentation                    |
| MatCap                | texture object reference | a reference to a texture object                                                                     |

### Shading

{% hint style="info" %}
The selection is defined by a material custom property (see a custom properties section)
{% endhint %}

Supported diffuse shading:

* Lambert
* Oren-Nayar (more diffused light destribution, better fit for a character skin surface)

Supported specular reflections:

* Phong (material like plastic)
* Anisotropic (material like brushed metal)

  <figure><img src="https://21658545-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FYKfPCkQKpHW2adXsZwun%2Fuploads%2FRzV0Nw4GPP21b98jnnoU%2Fimage.png?alt=media&#x26;token=8becf54c-88a2-4a2e-9d21-9c09a57a4b07" alt=""><figcaption><p>anisotropic specular reflection</p></figcaption></figure>
* Cook Torrance (with a given specular map where R channel describes the roughness, it's a good fit for a skin surface specular reflections)&#x20;

  <figure><img src="https://21658545-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FYKfPCkQKpHW2adXsZwun%2Fuploads%2FG2pqKdzcRWykNkITcfad%2Fmetahuman_face_test2.jpg?alt=media&#x26;token=4cc9dd77-8b38-43c7-afc4-f9fa70f64b27" alt=""><figcaption></figcaption></figure>

### Shadows

Shadows are implemented as projected textured shadow maps. Shadows will be rendered only from infinite and spot lights which are defined in Affected Lights properties of the shader.

Shadows will be rendered only for models defined in the Casters property list of a shader.

{% hint style="info" %}
Defined list of affected lights and list of caster models is a requirement for shadows
{% endhint %}

Super dynamic lighting has support for unlimited number of lights (depends on your hardware capabilities, I would recommend to avoid having more than 32). The same for shadow mapping, it goes as a texture array and will try to allocate and render as many shadows as it was required.

<figure><img src="https://21658545-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FYKfPCkQKpHW2adXsZwun%2Fuploads%2FteIeMoOAKPM4n55hp4bm%2FshadowMapping.jpg?alt=media&#x26;token=53476916-85d2-412e-ac4e-374298722f32" alt=""><figcaption></figcaption></figure>

On a shader side we have parameters

* **Shadows** - to toggle shadow mapping for the shader instance
* **Shadow Size** - by default 2k, the size of shadow textures (better to use a size of a power of two like 1024, 2048, 4096)
* **Offset Factor** - offset to a polygon in a current view based on its slope
* **Offset Units** - global const offset in depth units

Each light has "**cast shadows**" flag to toggle shadow casting individually.

Each model has flags for **Casts Shadows** and **Receive Shadows** which are connected to a correspondent logic inside the shader.

### Infinite light shadow map

Infinite light shadow uses the whole scene bounding box as a default frustum area to render into a shadow map. With a big complex scene that could lead to a shadow quality degradation.&#x20;

There is a way to define a custom bounding box for a shadow rendering. To do so you have to define an object or set of objects which bounding box you would like to use.

<figure><img src="https://21658545-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FYKfPCkQKpHW2adXsZwun%2Fuploads%2F8KixBCIObOuQrj4i8zIy%2Fimage.png?alt=media&#x26;token=96dca4cf-4f72-4a91-bd32-cc082b5eccc9" alt=""><figcaption><p>example of infinite light shadow bounding box</p></figcaption></figure>

Then attach these objects into a "Shadow Bounding Box" custom light property

<figure><img src="https://21658545-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FYKfPCkQKpHW2adXsZwun%2Fuploads%2Fq0ywW7n1HihnEUVE5h6c%2Fimage.png?alt=media&#x26;token=8d02be5f-8365-4ee2-87aa-fde4a2cb43cb" alt=""><figcaption><p>custom object property and defined model to be used as bounding box area for shadow</p></figcaption></figure>

### Custom Properties

#### Scene material properties

* **Diffuse Type** where 0 - Lambert, and 1 - Oren-Nayar
* **Specular Type** where 0 - Phong, 1 - Anisotropic and 2 - Cook Torrance
* **RoughnessX** and **RoughnessY** - properties or specular distribution for the anisotropic specular

<figure><img src="https://21658545-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FYKfPCkQKpHW2adXsZwun%2Fuploads%2F8nNGMWb7U37wiUR7C3jN%2Fimage.png?alt=media&#x26;token=0481717f-61a9-48b6-968f-4b0d57dac51b" alt=""><figcaption></figcaption></figure>

#### Scene light properties

* **Magnitude** - Number, defines the light influence radius
* **Cast Specular On Object** - bool, defines either light have to contribute to a specular term
* Shadow Bounding Box - list of objects, to define a custom bounding box area for an infinite frustum when we do render objects into a shadow map

<figure><img src="https://21658545-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FYKfPCkQKpHW2adXsZwun%2Fuploads%2FwXgZqiVkG7Uw9LrrD3yM%2Fimage.png?alt=media&#x26;token=c0d07f00-b7a6-4d8d-84f1-93c941ac3f97" alt=""><figcaption></figcaption></figure>

#### Script to automate properties creation

There is a script ScriptCreateLightAndMaterialProperties.py in OpenMobu/PythonScripts/Actions/SuperDynamicLightingShader

The script will help to create needed custom properties for selected light or material models.

### Transparency

When shader transparency is defined, then blend mode based on alpha channel source is enabled in opengl

In additional shader could do a fragment discard on alpha less than 0.5. That could be useful for a depth information masking for ambient occlusion.

### Global Ambient and Fog

Material ambient color is used in combination with a defined global ambient color. In case you have black global ambient color, there will be no effect from the material ambient

<figure><img src="https://21658545-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FYKfPCkQKpHW2adXsZwun%2Fuploads%2FEuSu94RIF0zmmx3JLsbA%2Fimage.png?alt=media&#x26;token=68697173-e08b-40c7-bab8-09b7609ff37e" alt=""><figcaption></figcaption></figure>

Fog in global light settings also has an influence on the shading, so you can use it with Super Dynamic Lighting shader.

## How to use second UV set and baked shadows

[Video on Youtube](https://youtu.be/ILbo5VWHVTE?si=0_cpQJcIU0AALap6)

## What is MatCap

**MatCap** (short for **"Material Capture"**) is a shading technique commonly used in 3D rendering to create fast and visually appealing materials by mapping lighting and material properties directly from a texture.

In order to use MatCap shading you have to import such matcap image and assign it as input for MatCap texture in the shader properties. Plus you have to put "Use MatCap" property to true.

<figure><img src="https://21658545-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FYKfPCkQKpHW2adXsZwun%2Fuploads%2FLREN5RNvStgY4cg7LUEu%2Fimage.png?alt=media&#x26;token=5a220f21-bf0b-40dd-aff9-e4f1b1bff83a" alt=""><figcaption></figcaption></figure>

<figure><img src="https://21658545-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FYKfPCkQKpHW2adXsZwun%2Fuploads%2FCfajg4DxpBVzcCIJoMmp%2Fimage.png?alt=media&#x26;token=81816b63-4e48-4b70-a905-9e4e5ee68821" alt=""><figcaption><p>example on how matcap texture looks like</p></figcaption></figure>

<figure><img src="https://21658545-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FYKfPCkQKpHW2adXsZwun%2Fuploads%2Fl4h531mT2y7FyiPRVZyR%2Fimage.png?alt=media&#x26;token=da2f205e-ad75-4f41-98ad-4dcbdb9f5503" alt=""><figcaption></figcaption></figure>

## Where to get matcap images ?

[Here is a huge collection of matcaps on github repository](https://github.com/nidorx/matcaps)
