# Texture Conversion in Gem RTS

## **Introduction**

Texture conversion is necessary for transforming texture sets created for Phong materials into PBR[^1] material components. In the Gem RTS engine, the conversion process can be performed automatically using material schemes created in the Scheme Editor.

### **Differences between texture components for Phong and PBR materials**

**Phong material components**

* The `lightmap` is baked into the `diffuse` texture map, located in a separate upper layer of the original `.tif` file and overlaid on the color map using the Multiply Blend mode.
* `Specular` texture map is used to define the shininess and reflectivity of surfaces..

<figure><img src="/files/bwbBC8vSZzWZleRM5Jot" alt=""><figcaption><p><strong>Diffuse Texture (with baked lightmap) for Phong Material</strong></p></figcaption></figure>

**PBR material components:**

* The `diffuse` texture retains only the material color, while the `lightmap` is transferred to a separate `ambient occlusion` texture.
* `Metallic` and `roughness` texture maps are used to simulate how light interacts with surfaces in a realistic way

<figure><img src="/files/dzFG5QILWuUjG5aM7IBy" alt=""><figcaption><p><strong>Diffuse Texture for PBR Material</strong></p></figcaption></figure>

<figure><img src="/files/ZA34E5zhBRBj1frY5a2f" alt=""><figcaption><p><strong>Ambient Occlusion Texture for PBR Material</strong></p></figcaption></figure>

## **Texture conversion using the Scheme Editor**

Using conversion blocks in the Scheme Editor, you can create material schemes that automatically convert Phong texture sets into PBR materials during rendering.

The conversion works by transforming Phong texture sets (diffuse and specular) into PBR components diffuse, metallic, roughness, and ambient occlusion. The generation of PBR materials from Phong texture sets is based on the following assumptions:

<table><thead><tr><th width="235">Component</th><th>Conversion assumption</th></tr></thead><tbody><tr><td><strong>Metallic</strong></td><td>A high <code>specular</code> value corresponds to the physical properties of metal. <br>The lower threshold of <code>specular</code> brightness, beyond which metals begin, is taken into account.</td></tr><tr><td><strong>Roughness</strong></td><td>The higher the specular value, the smoother and more reflective the material. <br>The minimum specular brightness value is taken into account to avoid very smooth surfaces. The original scheme's <code>specular_power</code> parameter indicates the glossiness of the surface.</td></tr><tr><td><strong>Ambient Occlusion</strong></td><td><code>Ambient occlusion</code> is baked into the diffuse texture. <br>Lower brightness values of the diffuse texture are taken as a sign of <code>ambient occlusion</code>.</td></tr></tbody></table>

### **Conversion Blocks**

| Conversion Block Name                                                                                | Input Pin                   | Output Pins                                               |
| ---------------------------------------------------------------------------------------------------- | --------------------------- | --------------------------------------------------------- |
| <p><strong>Specular</strong> <br><strong>to</strong> <br><strong>metallic and roughness</strong></p> | specular                    | <p></p><ul><li>metallic, </li><li>roughness</li></ul>     |
| <p><strong>Diffuse</strong> <br><strong>to</strong> <br><strong>ambient occlusion</strong></p>       | diffuse with baked lightmap | <ul><li>clean diffuse</li><li>ambient occlusion</li></ul> |

<figure><img src="/files/5PWAg4MqG4r16opESxSY" alt=""><figcaption><p>'S<strong>pecular to metallic and roughness' Conversion Block</strong></p></figcaption></figure>

<figure><img src="/files/AyT4efHC2mKGHC2sBxTZ" alt=""><figcaption><p>'<strong>Diffuse to ambient occlusion' Conversion Block</strong></p></figcaption></figure>

### **Creating a Scheme for automatic conversion of Phong texture sets to PBR material**

1. Launch the Scheme Editor.
2. Open the Phong material scheme.
3. Add Conversion blocks to the scheme.
4. Connect the Phong `diffuse` and `specular` textures to the input pins of the Conversion blocks.
5. Combine all data from the output pins of the Conversion blocks into the Material block with the chosen lighting method.
6. Save the scheme to the `/gem2` folder.

<figure><img src="/files/JRJtIz3f7SdoswTxxlJ7" alt="" width="555"><figcaption><p><strong>Example of combining data into the 'Material: metallic + roughness' block</strong></p></figcaption></figure>

**Example of using conversion blocks in the bump material scheme from the gem2 folder**

<figure><img src="/files/33GqcD6caCr1eeAKNBTN" alt=""><figcaption><p><strong>Bump material scheme from the gem2 folder</strong></p></figcaption></figure>

{% hint style="info" %}
New PBR material schemes can be created using the Scheme Editor.
{% endhint %}

## **Methods of Converting Phong Textures to PBR**

### **Automatic Conversion**

* Using conversion blocks in the material scheme, Phong texture sets (diffuse, specular) are transformed into PBR material components (diffuse, ambient occlusion, metallic, roughness) automatically during rendering.

### **Partially-Automatic Conversion**

* Extract the lightmap from the diffuse texture and save it as a separate ambient occlusion texture using a graphic editor.
* The specular map is converted into metallic and roughness components using a conversion block in the material scheme.

### **Conversion using a graphic editor**

**Steps:**

1. Transfer the `lightmap` from the Phong `diffuse` texture to a separate `ambient occlusion` texture.
2. Convert the Phong `specular` texture to a `roughness` texture map. Usually, inverting the `specular` image and adjusting it with levels is sufficient.
3. Create a `metallic` map based on the Phong `specular` texture, leaving the brightest areas white (metallic = 1).
4. Assemble the PBR material scheme in the Scheme Editor using the PBR material texture maps.

## **Assigning the PBR material scheme to a model**

{% hint style="info" %}
When creating new models, it is recommended to assign materials from the `pbr/` folder.
{% endhint %}

{% hint style="info" %}
Material schemes from the `gem2/` folder support the automatic conversion of Phong texture sets to PBR material during rendering.
{% endhint %}

### **Re-exporting the model**

1. In 3D Max, assign the PBR material scheme to the model.
2. Export the model to the game using the [3D Max Plugin](/modeling/3ds-max-plugin.md). \
   During export, the .material file in the model folder is automatically updated to include the material scheme and the paths to the textures for the material components.

### **Without Re-exporting the model**

1. Open the `.material` file from the model folder using a text editor.
2. Replace the material scheme name and paths to the corresponding PBR textures (textures must be in `.dds` format).

[^1]: [Physically Based Rendering](/textures-and-materials/physically-based-rendering.md)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.bestway.com.ua/textures-and-materials/physically-based-rendering/texture-conversion-in-gem-rts.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
