Tutorial: The Material System in the New Shaders

This forum is dedicated to sharing knowledge via guides, tutorials and any other form of how-to's. It may in time succeed the modding wiki.
Post Reply

Tutorial: The Material System in the New Shaders

User avatar
blue_max
XWAU Member
Posts: 2291
Joined: Wed Mar 20, 2019 5:12 am

Post by blue_max » Mon May 04, 2020 6:50 am

A couple of releases ago (?) I introduced the concept of materials in XWA through ddraw, but I haven't really sat down to explain how it works. Please, bear in mind that it would've been impossible to write material definition files for all OPTs out there. Instead, I had to make a few compromises to come up with something that looks "generally OK" for most crafts. The material system is inspired by Blender's Principled Shader and it's also based on my own experience when using the Blender Cycles system.

Anyway, the material system is based on three main settings: Metallicity, Intensity (of the specular reflection), and Glossiness. When writing a new material definition file, the first thing you've got to decide is the metallicity of the material. This value goes from 0 (not metallic at all, like plastic or wood) to 1 (completely metallic). In practical terms, metallicity affects the tint of the specular reflection and how the surface reacts to ambient illumination.

Metallicity 0: The specular reflection is gray/white. The lightness of the texture from the OPT is used to compute how bright is the specular reflection. Ambient illumination directly increases the illumination of the surface
Metallicity 1: The specular reflection is tinted by the color of the surface (the texture from the OPT). Ambient illumination is ignored.

For example, look how the specular reflection is tinted red on the left; but is white on the right in the following image:

Metallic-00-05.png

For the ambient illumination part, look at the following image:

Metallic-Levels.png
You do not have the required permissions to view the files attached to this post.

User avatar
blue_max
XWAU Member
Posts: 2291
Joined: Wed Mar 20, 2019 5:12 am

Post by blue_max » Mon May 04, 2020 6:59 am

The next setting is the Intensity of the specular reflection. This value goes from 0 (no specular reflection at all) to 1 (super bright specular reflection). This is easy to understand with another example:

Intensity-Levels.jpg

Glossiness controls the size of the specular reflection. I'm using the Phong Illumination model, so this guy (roughly) corresponds to the exponent of the specular component. Eh... It means it's non-linear. Glossiness 0 is a very wide specular reflection. Glossiness 1 is a very focused and small specular reflection, like glass:

Glossiness-Levels.jpg

I'm glossing over some technical details, but this is the gist.
You do not have the required permissions to view the files attached to this post.

User avatar
blue_max
XWAU Member
Posts: 2291
Joined: Wed Mar 20, 2019 5:12 am

Post by blue_max » Mon May 04, 2020 7:13 am

The next setting is the NMIntensity or Normal Mapping Intensity. This is still fake normal mapping and it's based on whatever is rendered on the screen... If you've ever done an embossing filter on an image, it's about the same thing. It's not perfect and I know it causes some artifacts here and there, so I still need to work on that. Another concept that is easier to understand with images:

NMIntensity.jpg

I mentioned earlier that the specular reflection is based on the color of the surface, for both metallic and non-metallic textures. An easy rule is "a white surface will reflect more light than a dark surface". But this is a problem for surfaces that are (nearly) black, like the panels of a TIE-Fighter. In this case, the Intensity setting above won't work at all:

T-F-Intensity.jpg

To fix this problem, I introduced another setting called "SpecularVal" which adds a white specular component to surfaces that won't otherwise display any:

SpeVal-05.jpg

The size of the SpecularVal sheen is also controlled by the Glossiness setting.
You do not have the required permissions to view the files attached to this post.

User avatar
blue_max
XWAU Member
Posts: 2291
Joined: Wed Mar 20, 2019 5:12 am

Post by blue_max » Mon May 04, 2020 8:15 am

Thanks for your comments guys; but there's more!

Glass: Early on in the process I decided that if a surface was (semi-)transparent, it had to be glass of some sort. So every surface with transparency above, er, 90% or so (need to double-check that) is automatically rendered with maximum glossiness and a white specular value. However, this is not true for all transparent surfaces. For instance, some hangars have tubes hanging from the ceiling and they will look quite weird. So, to remove the specular sheen on surfaces with transparency you need set the surface as "Shadeless" and use following settings:

Code: Select all

[TEX000NN]
Shadeless   = 1
Intensity   = 0.0
Glossiness  = 0.1
NMIntensity = 0
Laser lights and Sun Colors: Lasers now emit light; but the color and intensity of the light is read from a material file. The format is:

Code: Select all

Light = Red, Green, Blue
Each component should be in the range 0..1. Each Laser OPT comes with multiple colors, so there's multiple entries in the .mat files for the lasers too (just take a look at any Laser*.mat file and you'll see what I'm talking about). Starting on release 1.1.3, Sun colors are also configured using the "Light" setting.

The Default Global Material: In release 1.1.3 I added a new special file (DefaultGlobalMaterial.mat) that contains the default settings for all OPTs that don't have a materials definition file. This file can be used to reduce or eliminate the normal mapping for all OPTs, for instance.

User avatar
blue_max
XWAU Member
Posts: 2291
Joined: Wed Mar 20, 2019 5:12 am

Post by blue_max » Mon May 04, 2020 8:34 pm

A few more details. Material files have the format "Materials\<OPTName>.mat". Inside, there can be several sections:

Code: Select all

[Default]
Metallic   = 0.25
Intensity  = 0.5
Glossiness = 0.1
The [Default] section applies the settings to all the surfaces in this OPT, overriding the settings from DefaultGlobalMaterial.mat.

Individual surfaces can be grouped together, like this (don't put more than 10 texture names per row, though):

Code: Select all

[TEX00000,TEX00001,TEX00002,TEX00003,TEX00004,TEX00005,TEX00006,TEX00007]
Metallic   = 0.5
Intensity  = 0.8
Glossiness = 0.1

[TEX00027,TEX00028,TEX00029,TEX00030]
Metallic   = 0.5
Intensity  = 0.8
Glossiness = 0.1
The texture names are case sensitive. So "TexNNNNN" is different from "TEXNNNNN". Yeah, I know, I should fix that later... :P

The DefaultGlobalMaterial.mat file doesn't have a [Default] section... it's already default!

Post Reply