Page 1 of 1

Tutorial: The Material System in the New Shaders

Posted: Mon May 04, 2020 6:50 am
by blue_max
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:


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


Re: The Material System in the New Shaders

Posted: Mon May 04, 2020 6:59 am
by blue_max
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:


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:


I'm glossing over some technical details, but this is the gist.

Re: The Material System in the New Shaders

Posted: Mon May 04, 2020 7:13 am
by blue_max
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:


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:


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


The size of the SpecularVal sheen is also controlled by the Glossiness setting.

Re: The Material System in the New Shaders

Posted: Mon May 04, 2020 8:15 am
by blue_max
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

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.

Re: The Material System in the New Shaders

Posted: Mon May 04, 2020 8:34 pm
by blue_max
A few more details. Material files have the format "Materials\<OPTName>.mat". Inside, there can be several sections:

Code: Select all

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

Metallic   = 0.5
Intensity  = 0.8
Glossiness = 0.1

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!