Materials for models

From Valve Developer Community
Jump to: navigation, search
English (en)
Edit

Introduction

Models in Source games - whether props, characters, viewmodels, etc - need at least 3 basic components: your.MDL, your.VMT and your.VTF.

Unfortunately it's not accurate enough to say the MDL is the "mesh", the VTF is the "skin" and the VMT is the "physics". At each stage, your model will consist of at least two separate but always cross-referenced files. Each stage of compiling requires data from some other files. Therefore the procedure must follow a strict sequence, and file names and locations are also quite strict.

The process of generating these files falls into two broad stages:

  1. The first stage is creating a base texture and mapping it to your mesh, resulting in a pair of 'temporary' files: your.SMD and your.TGA.
  2. The second stage is using your.SMD and your.TGA to compile your.MDL, your.VMT and your.VTF in a particular Source game_directory.

Mapping Textures to your .SMD model using XSI

  • To apply a texture to your model in XSI:
  1. select your modelled mesh Object.
  2. select XSI's Render mode (top of left sidebar).
  3. from Render>Get-Material select Blinn (close the Material window)
  4. from Render>Get-Texture select Image to open the Image-Texture window:
  5. in Image-Texture tab>Image press "New" to locate & import your new TGA texture file.
  6. in Image-Texture tab>Texture Projection press "New" and select Unique UVs (polymesh) (close the Image window)
  7. Press "Alt+7" to open the XSI Texture Editor window.
  • If you mess up your texture mapping and want to start over:
1. Press "8" to open the Scene Explorer, then navigate to the Texture Projection like this:
-> Cube (or your base object)
--> Polygon Mesh
---> Clusters
----> Texture_Coordinates_Auto
-----> Texture Projection (Explicit UVWs) 
2. Now left-click "Texture Projection (Explicit UVWs)" to select it, then press your "Delete" key to delete it.
3. Check the 3D view (in 'textured' mode) to make sure you got the right one. If not, press "Ctrl+Z" to undo and take another shot at it.

Proper Previewing

  • Making judgments about the progress of your textured character should be done directly in the Source engine, before getting too far with your material, export a vtf, create a vmt, build you mdl and look at the model in the engine. Working the fine points of texturing and shader parameters can only really happen after you’ve seen your model in the Engine.

Checkerboard Template Texture

  • Using a checkerboard texture is a good way to start laying out your UV coordinates; this lets you see very quickly if you have any distortions, streaks, or seams that may need to be corrected. Also, using a checkerboard texture gives you a very good way to measure how much texel density you have on the various parts of your model, as you define the UV’s, some areas like faces may need a bit of extra density, but overall you should strive for even and consistent texel density across your model. The default noicon.pic file in XSI is a good choice or you may use this file, Checkerboard Starter Texture (right-click save as...) included in the SDK.

What size should my texture be?

  • Export a .VTF as early as possible and load preview your model in the engine, using the mat_showmiplevels command at the console, check what mip level you’re seeing at some of the most common encounters your character is going to have during gameplay. While it may seem that bigger is better, this isn’t always the case, mipmapping can force your large texture down in size until blurriness becomes a problem.

Multi-layered Textures

  • You should try to only use one basetexture, unless material demands require otherwise. Multiple textures on a model can drastically affect rendering time. If you do need to create multiple textures on one model, you must first select a group of polygons, make a cluster of them, then apply the material and image to your cluster. Make sure that you have your preferences set to "reload externally modified images on focus". This reloads textures when you alt-tab back to XSI from another program like Photoshop, a good time saver.

Compiling your.VTF : Valve Texture Format

Any Texture that has been mapped to a model (before exporting your.SMD from XSI) must be converted from a TGA to a VTF file before it can be used in the Source Engine. The VTF filename must remain the same as the TGA used to compiling your.SMD (eg: your.TGA -> your.VTF) and the VTF placed in the game_directory/materials/models folder.

  • SourceSDK/bin includes Vtex.exe, the application used to compile to .VTFs. The simplest way to use Vtex is to create a shortcut to vtex.exe on your desktop, and drag .TGA files onto the shortcut. This will create a .VTF file with the same name as your .TGA file in the materials directory. When you drag open a tga file with Vtex, a .TXT file is also created in the same directory as your .TGA, in this file you can issue commands to Vtex to alter your compression.
Note.pngNote:For quick material creation, try adding "-shader VertexLitGeneric" to your target field in the Vtex shortcut properties. this works with any shadertype. Dragging a texture on to this modified shortcut will then create a .VTF file as usual, and create a .VMT file with the shader and basetexture defined for you.
  • Cannonfodder's StudioCompiler provides a more user-friendly GUI to Vtex (and Studiomdl). It is able to compile/decompile VTF, VMT and MDL files from/to TGA and SMD files.

Compiling your.VMT : Valve Material Type

Each Material for the Source engine is defined by a .VMT file. It specifies the lighting model, the basetexture (VTF), and any other additional channels you may have like: specularity, self-illumination, normalmap, transparency, translucency, and proxy shaders.

For models, your.VMT must be written by hand (using notepad, etc) and placed in the game_directory/materials/models folder so that your.MDL can find it. The $basetexture (your.VTF) referenced in your.VMT must also be in the same game_directory/materials/models folder so that your.VMT can find it.

Tip.pngTip:The QC command $cdmaterials can be used to specify further subfolders.

If you're creating your model in Blender, you can use Blender VertexLitGeneric to help with the VMT: graphical interface, sliders and real time changes make it easy to see what you're doing. Then, you can automatically compile your VMT

Warning.pngWarning:Most games only support the VertexLitGeneric shader for models. Model Materials may not work properly with LightmappedGeneric or UnlitGeneric.

Here’s an example VMT file from HL2:

"VertexLitGeneric"
{
     "$basetexture" "Models/Combine_soldier/Combine_elite"
     "$bumpmap" "models/combine_soldier/combine_elite_normal"
     "$envmap" "env_cubemap"
     "$normalmapalphaenvmapmask" 1
     "$envmapcontrast" 1
     "$model" 1
     "$selfillum" 1
}

This example is a material used on a polygonal model, using the VertexLitGeneric shader. The $basetexture or diffusemap is specified as "Models/Combine_soldier/Combine_elite" The $bumpmap or normalmap is imported from "models/combine_soldier/combine_elite_normal". The $envmap or environmentmap is set to "env_cubemap", which refers to cubemap entities placed in the level; the specular reflections of this character will always reflect the world around it - assuming you have cubemap entities placed appropriately in you level. You can use a custom cubemap instead, if you choose. The next line, "$normalmapalphaenvmapmask" 1 tells the renderer that this material has a mask for the specularity, so only some areas of the model will reflect the env_cubemap information, as defined in this case by the alpha channel of the normal map, the specular mask can be here, in the alpha of the basetexture "$basealphaenvmapmask 1" or in a separate mask file "$envmapmask" "models/modelname/mymask". The "$envmapcontrast" 1 command means the brightness of the envmap reflections is multiplied by itself one time; to create brighter highlights and deeper shadows. All models need to contain the command, "$model" 1 which tells the renderer that this material is being used on a polygonal model compiled by Studiomdl.exe. The last line "$selfillum" 1 tells the renderer that the alpha channel of the $basetexture contains self illuminated areas, these areas of the alpha channel show up as glowing areas of the model.

Alpha-channel

To get materials with alpha-channel to work, you have to insert the command "$translucent" 1 to your VMT file. Sometimes this leads to rendering errors in the model (e.g. theres convex 1 in the back of convex 2, but convex 2 gets hidden by convex 1 => Rendering error). If this happens, using the command "$alphatest" 1 instead of "$translucent" 1 should fix it.

Proxy shaders

A powerful component of the source engine material system is the Proxy shader system. Examples of what proxy shaders are capable of are shown in the shaderzoo map contained in the sdk, and further documentation on their creation can be found in the materials documents.