[WIP]Scripts for preparing PBR texture

svictor

Registered
In this post I will be sharing scripts that will hopefully help with preparing PBR texture, especially for updating none PBR mod or conversion which often lack of certain required material textures, and often have to create new material texture using diffuse map texture.

The main tool used for scripting is ImageMagick: https://imagemagick.org/

Important Notice:
None of the scripts are meant to be one-button solution for creating PBR material (it's impossible). They are used to avoid some of the repetitive process and save time for preparing certain PBR materials.

It is almost absolutely required to do further manual editing on individual processed textures to achieve realistic results (depending on different materials).

Lastly, official PBR texture guide is still the best way for understanding and doing PBR properly.

Download Script:
Batching script is now provided for easy access, see in attachment.
Script can be opened by any text editor for further editing.
Here is how it looks like:
upload_2022-4-1_13-37-36.png


How to use:
The easiest way is to download batch script file as above mentioned. Extract and place script in a folder. Then put all original images into the same folder with script, then run the script & follow the instruction.

After processing finished, new textures will appear in the same folder, with corresponding material suffix, which can be opened in GIMP for further editing. You can easily sort those files by extension.

It is best to move those newly generated TGA files to another folder before further editing in GIMP or other software, so they won't accidentally be overridden if later runs batch script again for similar task.

Once all done, use official MapConverter tool to convert all TGA files to DDS, and ready to be assigned to a material and tested in game.

You can also run commands (which is provided in the following sections) manually. Here is step to do it manually:
1. Open console Command Prompt (DOS shell) from the folder that contains image (unfortunately there is not easy way to do it under WIN10).
Note: If the image is not PNG, you will have to change the png letters in command to the same format of your image. For example, if original image is DDS format, then change all *.png to *.dds
2. Copy the command line into console, hit Enter, and wait for finish.


Preparing Albedo map

Albedo map
Code:
FOR %a IN (*.png) DO magick convert "%~a" -channel RGB ( -clone 0 -set colorspace Gray -separate -average -negate ) ( -clone 0 ) -compose multiply -composite "%~dpna_Alb.tga"

Explain:
The above command creates base Albedo map using old diffuse map (the main texture of a material). The command follows the light-removal concept in official PBR texture guide. It first make a layer copy of original image and convert it to grayscale and invert color. Then apply layer multiply effect to remove lighting. And last, save processed file with _Alb.tga suffix in the same folder. (You may need to change file suffix to _Alba.tga if the texture has alpha channel)

You will still need to adjust Luminosity as official PBR texture guide has suggested.
upload_2022-3-31_23-9-58.png


Note about grayscale:
ImageMagick has many ways for converting grayscale, and affects the result of lighting removal. You can test and see which one of them works best:
-set colorspace Gray -separate -average
-grayscale Rec709Luma
-grayscale Rec709Luminance
-colorspace Gray
-modulate 100,0,100
See "Reference Link" at the end of post for more info about grayscale. Metal & Rough map commands also use those grayscale code.

Note about luminosity:
It is possible to add additional command after -composite to batch manipulate luminosity/brightness, such as one of the three follow command, by adjusting value highlighted in red (may require tests):
-gamma 1.2
-modulate 120,100,100
+level 0%,150%


Preparing Metal & Rough map

Metal & Rough map
Code:
FOR %a IN (*.png) DO magick convert "%~a" -channel RGB -set colorspace Gray -separate -average -negate -fill black -colorize 0,0,100% "%~dpna_Mr.tga"

Rough map only:
Code:
FOR %a IN (*.png) DO magick convert "%~a" -channel RGB -set colorspace Gray -separate -average -negate -fill black -colorize 100%,0,100% "%~dpna_Mr.tga"

Metal map only (this option is probably useless):
Code:
FOR %a IN (*.png) DO magick convert "%~a" -channel RGB -set colorspace Gray -separate -average -negate -fill black -colorize 0,0,100% -fill white -colorize 0,100%,0 "%~dpna_Mr.tga

Explain:
The above commands create base Metal & Rough map using old diffuse map (the main texture of a material). It will first convert image to grayscale and invert all color. Then remove color from blue (and also red if it's rough map only) channel by filling with black color, and save processed file with _Mr.tga suffix in the same folder. (Green channel is used for Rough map; Red channel is used for Metal map.)
upload_2022-3-31_23-25-37.png


General tip for further adjusting rough map (similar applies to metal map):
1. In GIMP, open _Mr.tga file that you have just generated from script.
2. Switch to Channels tab, click once on red & blue channel so that they are deselected. The idea is to only edit one channel at a time, and left other channel untouched.
3. From main menu Colors open Levels, adjust input & output levels as desired (darker = more smoothness; brighter = more roughness). As below:
upload_2022-3-31_10-41-53.png

Note: For some textures, inverted color may not be desired. You may want to re-invert the color or channel, or adjust certain area separately.

For example, the above image has a glass window, which should be smoother than the wall, but is darker in the original photo and has less smoothness after processing, so it needs manual editing and adjusting, same with other elements.


Reference Link:
https://docs.studio-397.com/developers-guide/general-reference/pbr-an-introduction-authoring-guide
https://docs.studio-397.com/developers-guide/track-development/general-tracks/naming-conventions
https://legacy.imagemagick.org/Usage/windows/#dos
https://imagemagick.org/script/command-line-options.php
https://imagemagick.org/script/color-management.php
https://imagemagick.org/script/command-line-processing.php#grayscale
https://stackoverflow.com/questions/13317753/convert-rgb-to-grayscale-in-imagemagick-command-line


Script Changelog:
v2022-04-01 hotfix1
- Fixed "Metal map only" option which had set wrong channel color.

v2022-04-01
- Now user can type in file extension in console before processing.
- Add new option to generate Albedo Map with Alpha Channel (only affect suffix name, which is required by MapConverter).
- Now displays processing text.

v2022-03-31
- Initial version
 

Attachments

Last edited:
First Post is now updated with script to generate base Albedo Map, also updated scripts for generating Metal & Rough map. Note: now all commands are switched to use Command Prompt (DOS shell) in order to achieve complex operation (main reason for not using powershell is that I don't know how to properly write powershell specific code).

A simple all-in-one script is also provided for easy use, can be downloaded from first post.

Thanks.
 
Last edited:
Edit:
v2022-04-01 hotfix1
- Fixed "Metal map only" option which had set wrong channel color (green channel should be filled with white color to remove any smoothness).

Batch script is updated in the first post with new feature:
v2022-04-01
- Now user can type in file extension in console before processing.
- Add new option to generate Albedo Map with Alpha Channel (only affect suffix name, which is required by MapConverter).
- Now displays processing text.

index.php
 
Last edited:
s.victor,
I have a problem with the script.
I create a dir add a couple of texture files, run the script, and i receive an error of:

'magick' is not recognized as an internal or external command,
operable program or batch file.

ImageMagick-6.9.12-Q16-HDRI
Win10, 16gb, Nvid RTX 3060 12gb, SSD drives
i cannot fine another file to replace ' magick' with ?

ImageMagick is installed to default location.
any suggestions to what i am missing?
 
hi, please uninstall 6.X version of imagemagick(it's outdated), and download & install the new 7.X version from official site.

also if there is any option mentioned environment PATH while installing, make sure have it checked( I forgot whether installer has the option or not, I will need to check later when back to computer), as environment PATH is vital and required for command to be recognized.

lemme know if it works or not, thanks.

Update: I just check latest version installer, the second option will add required environment PATH, make sure it is checked:
upload_2022-4-1_18-53-39.png
 
Last edited:
svictor,
thanks you it needed the latest version, which i thought i had.Environment PATH is mentioned on install and i had chosen it.
Works ok now.
many thanks for all you do.
 
Mauro,
i worked with motorfx on many tracks for a number of years. Charade was one of these, i continued work on it with further updates and textures for rf1,gtl,gtr2.
It is a mix of Charade and Clermont-Ferrand for all of the above.Plus the many updates for RF2.
It is coming along, but it is a big track with lots of work.
 
Thanks for providing such a good info & tool!
Is there any method to make AO map easily also?
The tool provided by team doesn't work.
 
Thanks for providing such a good info & tool!
Is there any method to make AO map easily also?
The tool provided by team doesn't work.
To properly create AO map, you will need to do this in 3D software, such as blender/3dsmax, etc.

This requires a few key knowledge:
1. Create AO UV mapping in secondary channels(channel 2,3,4) of a material.
2. "Bake" new AO on to one of the secondary channel you choose.
3. After baking done, save this ao texture, then do some extra manual editing to convert it into AO Specular map for RF2 to use.

For example, here is the pit building I baked in blender for Dundrod track:
1. Create AO UV
upload_2023-3-21_15-21-5.png


2. Bake AO map in blender
upload_2023-3-21_15-23-38.png


3. Save AO texture, and remember to also save & export this new building model(as it contains UV mapping info).
upload_2023-3-21_15-27-7.png


4. Open this new AO texture in GIMP. Fill blue channel with black, then either fill red channel with white, or manually adjust this red channel for specific specular strength, etc. Then export image as ***_Aos.tga , and it is ready to be converted using mapconverter.
upload_2023-3-21_15-30-12.png


The 3D part is probably the biggest obstacle, but there are a lot tutorials video you can find online about how to map UV & baking AO map.
 
To properly create AO map, you will need to do this in 3D software, such as blender/3dsmax, etc.

This requires a few key knowledge:
1. Create AO UV mapping in secondary channels(channel 2,3,4) of a material.
2. "Bake" new AO on to one of the secondary channel you choose.
3. After baking done, save this ao texture, then do some extra manual editing to convert it into AO Specular map for RF2 to use.

For example, here is the pit building I baked in blender for Dundrod track:
1. Create AO UV
View attachment 51527

2. Bake AO map in blender
View attachment 51528

3. Save AO texture, and remember to also save & export this new building model(as it contains UV mapping info).
View attachment 51530

4. Open this new AO texture in GIMP. Fill blue channel with black, then either fill red channel with white, or manually adjust this red channel for specific specular strength, etc. Then export image as ***_Aos.tga , and it is ready to be converted using mapconverter.
View attachment 51531

The 3D part is probably the biggest obstacle, but there are a lot tutorials video you can find online about how to map UV & baking AO map.

Thanks for kind explanation!
 
Back
Top