MTR files define a material which can be referenced from within models (MDL files) that are linked to a node on a model. They essentially can define any custom shaders and shader parameters to use, any additional texture references such as normal or specular textures, and even replace the base texture (the diffuse one) by referencing another texture file.
Loading MTR Files
There are two ways to reference an mtr file. The first method is to use the new materialname field in the MDL file, usually with a file name such as m_name.mtr, since this this removes any ambiguity and for the case of PLT textures makes it clear what MTR file is in use, due to the bugs below.
materialname myMaterialFile
The second way is to reference it in a bitmap or texture0 field:
bitmap myMaterialFile
The latter enables using materials with existing textures and models without model edits. If an mtr file is present it takes precedence over image textures - be it DDS, PLT or TGA.
PLT and MTR Usage Note
Please note that MTR files are PLT files need to be done in a very specific way. The texture0 line in the MTR file must be omitted entirely. It cannot be null, or even the original PLT texture name, since this does some odd things around what PLT colour are chosen and can lead to colour bleeding between models.
The best bug free method is to have the bitmap (or texture0) to be the PLT texture name - mainly as a point of reference (it is actually ignored) - and materialfile line to be the material name, eg:
bitmap pmh0_belt003 materialname M_pmh0_belt003
Then the file m_pmh0_belt003.mtr contains just this line (since it is just a normal file):
texture1 pmh0_belt003_N
Note the omission of the texture0 line.
MTR File Format
MTR files are made up of a number of simple text settings one per line. They are outlined below. Comments can be added with two slashes (eg: "// comment" ).
A sample completed MTR file:
// Specify it is normal and spec mapped renderhint NormalAndSpecMapped // Define textures texture0 plc_tree_d // Diffuse texture1 plc_tree_n // Normal texture2 plc_tree_s // Specular texture3 plc_tree_r // Roughness texture4 plc_tree_h // Height texture5 plc_tree_i // Illumination
MTR Parameters List
Note you can use //
to have comments in a MTR file.
Parameter Name | Parameter Value(s) (case insensitive) | Usage Example | Description |
---|---|---|---|
bitmap | Diffuse texture but you don't tend to use this and use texture0 instead. | ||
texture0 | null (default) or Texture Resref | Diffuse texture (if both this and bitmap are used, this overrides it, it is recommended to not use bitmap and use this instead) Note: for MTR files attached to a PLT file do not specify this line at all leave it out of the MTR file entirely else it bugs out. | |
texture1 | null (default) or Texture Resref | Normal texture | |
texture2 | null (default) or Texture Resref | Specular texture | |
texture3 | null (default) or Texture Resref | Roughness texture | |
texture4 | null (default) or Texture Resref | Height texture | |
texture5 | null (default) or Texture Resref | Illumination texture | |
texture6 | null (default) or Texture Resref | Unused by default shaders can be used in custom shaders | |
texture7 | null (default) or Texture Resref | Unused by default shaders can be used in custom shaders | |
texture8 | null (default) or Texture Resref | Unused by default shaders can be used in custom shaders | |
texture9 | null (default) or Texture Resref | Unused by default shaders can be used in custom shaders | |
texture10 | null (default) or Texture Resref | Unused by default shaders can be used in custom shaders | |
renderhint | none NormalAndSpecMapped NormalTangents | renderhint NormalAndSpecMapped | See further below. |
customShaderVS | Shader resref | customShaderVS vsMyVertexShd | Custom Vertex Shader |
customShaderFS | Shader resref | customShaderFS fsMyFragmentShd | Custom Fragment Shader |
customShaderGS | Shader resref | customShaderGS gsMyGeometryShd | Custom Geometry Shader |
parameter | type name value Type can be: Vector char float int short bool Quaternion unsigned int | parameter float Specularity 0.02 parameter float Roughness 0.1 | Shader parameter. See Shaders for using them there. EG: one MTR file you want to be really shiny, one you want to be really rough, you can change direct values the shader then uses even if the texture files are otherwise identical. The only non-obvious ones are vector and quaternion - need to test them (likely vector vectorName 0.1 0.1 0.1 (x/y/z) and quaternion quaternionName 0.1 0.1 0.1 0.1 (w/x/y/z)) the rest are standard types. |
transparency | 0 (default) or 1 | transparency 1 | If 1 always render after opaque models, guarantees transparency. |
twosided | 0 (default) or 1 | twosided 1 | If 1 renders both back- and front-facing vertices. |
sample_framebuffer | 0 (default), 1 or 2 | sample_framebuffer 1 | If 1 or 2 renders near the end of the frame (1 before shadows, 2 after shadows). Allows rendering refractions and similar. |
volumetric | 0 (default) or 1 | volumetric 1 | If 1 renders back-facing vertices to depth and renders with updated depth buffer sample texture before rendering front-facing. |
Shaders
Defining shaders is optional. If omitted, a default shader will be picked depending on content type. See here for more about shaders. Remember filenames can only be 16 characters long.
Renderhint
Specifying a renderhint in the material file will instruct the game to handle the associated content in a certain way. Currently, only two renderhint tokens are supported, which have identical function and can be used interchangeably : "NormalAndSpecMapped" and "NormalTangents". Specifying either of these will make the game generate vertex tangent and handedness information (provided that the associated model is not already compiled), as well as picking a shader variant with normal mapping if no custom shader is specified.
The renderhint can also be added directly to a MDL file.
Note that NormalAndSpecMapped and NormalTangents do seem to have different effects if you're directly altering a flat amount of specular or roughness set in the MTR file itself. See Shaders for an example.
Textures
Texture definitions are optional. Any texture defined in the mtr file takes precedence over the one in the model/ mdl file. Up to 11 textures are supported: texture0 - texture10 (numbering doesn't need to be continuous). Specifying a texture makes it available in the shaders in the corresponding uniform sampler (texUnit*).
As noted above texture0 should be omitted for PLT mtr files due to bugs.
Default textures are as per below if using Standard material inputs:
texture0 - Diffuse texture1 - Normal texture2 - Specular texture3 - Roughness texture4 - Height texture5 - Illumination
To have a set of texture0, texture1...texture10 lines but miss some out, use the keyword null, eg to miss off the specular and roughness map but have the height map:
renderhint NormalAndSpecMapped texture0 object_tex texture1 object_tex_n texture2 null texture3 null texture4 object_tex_h texture5 object_tex_i
Additional texture slots are available from 11 to 15 which previously had similar functionality, however they are currently unavailable for use in the MTR file using the default shaders. Each slot is now internally used for other purposes as listed below.
Texture Slot | Current Use |
---|---|
texture11 | Depth Buffer [used for post processing] |
texture12 | Screen Buffer Color [used for reflections and post processing] |
texture13 | Cube Environment Map |
texture14 | Flat Environment Map |
texture15 | Noise Map [used for water] |
Parameters
Optional definitions of parameters, which will be passed to the shader.
See an example of passing parameters on the Standard material inputs page.
You can also set parameters on objects in NWN (so not individual tiles, but say a creature, item or placeable) and for a individual player (so their entire shader view) for shaders with nwscript, such as with the function SetMaterialShaderUniformVec4, see Shaders for some more info on this.
Examples
Overwriting Textures
The following example overrides the standard armoire placeable (plc_a01.mdl). The new placeable is a simple square and references a mtr file with three textures. The files have to be placed into the override folder:
The MDL file has these fields:
node trimesh a01_planeA parent plc_a01 ... bitmap ttr01_grass02 # Old style texture reference, overwritten in mtr texture1 null # texture will be overwritten in mtr texture2 null # texture will be overwritten in mtr renderhint NormalAndSpecMapped # Enables normal and specular mapping materialname testmat # Reference to mtr file ... endnode
For information on PBR texture references in .mtr files, see here.
// textures0 overwrites bitmap or texture0 parameter in mdl texture0 testtx // Used by normal and spec mapping shaders texture1 testtx_n texture2 testtx_s
Roughness, Heightmaps and Illumination are supported as well, the fuller usually used list are as follows:
renderhint NormalAndSpecMapped texture0 * (Diffuse) texture1 *_n (Normal) texture2 *_s (Specular) texture3 *_r (Roughness) texture4 *_h (Height) texture5 *_i (Illumination)
Remember: if the model uses PLT textures make sure you do not include the texture0 line at all, even if it says "texture0 null", since it can bug out the PLT loading.
Accessing Textures Within Shader Code
Each texture 0-10 comes with two uniforms accessible within shader code.
The first is texture<X>Bound where <X> is the index of the texture. If texture1Bound is not equal to 0, then a texture is available in that slot to the shader. If not, then reading that slot may instead print waste data from the object's last purpose. Errors include printing a ghost spec map from another material in the scene if a spec map is not explicitly given in the MTR file or when texture2 is set to null.
The second uniform is tex<X<Unit, again where <X> is the index of the texture. This uniform holds the Sampler2D object. Using the texture2D function, you can ask for a texel form the sampler2D object by specifying uv coordinates and an optional bias. See external link for more details.
Textures 11-15 use other names as listed below.
Texture Slot | Availability Flag | Shader Uniform or Method |
---|---|---|
texture11 | N/A | uniform sampler2D texFBDepth |
texture12 | N/A | uniform sampler2D texFBColor |
texture13 | texEnvCubeBound | uniform samplerCubeMap texUnitEnvCube and function SampleEnvironmentMapCube() |
texture14 | texEnvBound | uniform sampler2D texUnitEnv and function SampleEnvironmentMap() |
texture15 | texNoiseBound | uniform sampler2D texUnitNoise |