Difference between revisions of "Material"

From Valve Developer Community
Jump to: navigation, search
 
m (Convert {{otherlang2}} to {{lang}}.)
 
(85 intermediate revisions by 38 users not shown)
Line 1: Line 1:
[[Category:Material System]]
+
{{lang|Material}}
<i>Materials</i> are what the Source engine uses to define which <i>textures</i> (in .VTF file format) and <i>shaders</i> (functions which define how materials are rendered to the screen) are used on <i>surfaces</i> (models, world surfaces, sprites, etc).
 
  
=Material files=
+
A '''material''' is a <code>.vmt</code> ("Valve Material Type") text file that defines a two-dimensional surface. It contains all of the information needed for Source to simulate the surface visually, aurally, and physically.
  
Materials are defined inside of a .VMT (Valve Material) file. This is a high-level script that details how a material is to be rendered.
+
The contents of a material will fall into some or all of these categories:
  
Let’s start by looking at an example of a .VMT file:
+
# [[Texture]] names
 +
# [[$surfaceprop|Physical surface types]]
 +
# [[Shader]] [[:Category:List of Shader Parameters|parameters]]
 +
# [[Material optimization|Fallbacks]]
 +
# [[Material Proxy|Proxies]]
  
<pre>
+
== A simple example ==
"LightmappedGeneric"
 
{           
 
    // String values are quoted
 
    "$basetexture" "shadertest/LightmappedTexture"
 
    "$envmap" "shadertest/LightmappedTexture_envmap"
 
  
    // Vector values are quoted
+
LightmappedGeneric
    "$color" "[1 0 0]"
+
{
    // Float and integer values are *not* quoted
+
$basetexture coast\shingle_01
 +
$surfaceprop gravel
 +
}
  
    "$alpha" 0.5
+
This is a very basic [[Wikipedia:Shingle beach|shingle beach]] material.
    "$nocull" 1
 
}
 
</pre>
 
  
The first line of the .VMT file is the name of the shader to be used. The material variables for the shader are defined inside the curly braces. Note that you should not have '=' between the material variable name and its value. Also note that comment lines use '//'. Any text after the '//' on the same line will be ignored when the material is loaded.
+
#The <code>[[LightmappedGeneric]]</code> shader is used, which means that the material is for use on surfaces with [[lightmap]]s (i.e. [[brush]]es).
 +
#''The '''{''' character opens a set of parameters''
 +
#The <code>[[$basetexture]]</code> parameter is given with <code>coast\shingle_01</code>, which is the location of a texture. This is what will be drawn on the screen.
 +
#<code>[[$surfaceprop]]</code> gives the material the physical properties of gravel.
 +
#''The '''}''' character closes a set of parameters''
  
If the shader needs to <i>fallback</i> to a simpler shader because it's running on a lower-end system, you can optionally specify an additional block to override material variable values specified in the original block.
+
It's important to remember that this material can only be used on brushes. If it needed to be used on [[model]]s, for instance, another version would need to be created using the <code>[[VertexLitGeneric]]</code> shader.
  
Here's an example:
+
Most of the time switching materials from one shader to another is as simple as changing their first line, since a great number of parameters are shared between them. Some params only work with certain shaders, like [[Phong]] effects, which are only available with <code>VertexLitGeneric</code>, but unfortunately you won't encounter any critical errors if a param isn't understood by the shader. It just won't have any effect.
  
<pre>
+
{{tip|If you ever need to use a space or tab character in a parameter value, you must wrap the whole value with "quote marks". You'll often see absolutely everything wrapped like this - save yourself some typing, as that's unnecessary.}}
"LightmappedGeneric"
 
{              
 
    "$basetexture" "shadertest/LightmappedTexture"
 
 
    "$envmap" "shadertest/LightmappedTexture_envmap"
 
  
    // If the shader falls back to shader "LightmappedGeneric_DX7",
+
== Finding materials ==
    // then the environment map defined in this block will be used instead of the
 
    // one defined above. Since $basetexture isn't defined in this block,
 
    // the original one will be used.
 
    "LightmappedGeneric_DX7"
 
    {
 
        "$envmap" "shadertest/OverrideEnvMap"
 
    }
 
 
    // If the shader falls back to shader "LightmappedGeneric_DX6",
 
    // then the base texture defined in this block will be used instead of the
 
    // one defined above. Since $envmap isn't defined in this block, the original
 
    // one will be used.
 
    "LightmappedGeneric_DX6"
 
    {
 
        "$basetexture" "shadertest/OverrideTexture"
 
    }
 
}
 
</pre>
 
  
See Material choices and rendering performance in Controlling Geometry Visibility and Compile Times for more information on shader fallbacks.
+
=== SteamPipe ===
  
One other thing you'll see in the .VMT file format is the occasional variable starting with a '%'.
+
When Valve updated some games to [[SteamPipe]], the materials were moved from [[GCF]] into [[VPK]] files. VPK Files work with [[GCFScape]].
  
For example:
+
More info on SteamPipe [https://support.steampowered.com/kb_article.php?ref=7388-QPFN-2491 here]
 +
=== Non-SteamPipe Games ===
  
<pre>
+
in non [[SteamPipe]] source games, Materials are stored in the <code>materials\</code> folder of your game or mod. The best way to browse them is from [[Hammer]]'s texture selection screen.
"UnlitGeneric"
 
{
 
    $envmap" "shadertest/shadertest_envmap"
 
    %tooltexture" "shadertest/shadertest_envmap"
 
}
 
</pre>
 
  
This simply means that the variable is used by tools only and won't be loaded by the engine. The only variables that need '%' are <code>'%tooltexture"</code>, <code>"%keywords"</code>, <code>"%detailtype"</code>, and all of the compile variables like <code>"%compileWater"</code> or <code>"%compileHint"</code>.
+
If you want to edit or view the code of Valve's material files you will first need to extract them from their [[GCF]] package with [[GCFScape]]. They tend to be stored in GCFs with 'materials' in their name.
  
=Compiling Source Textures=
+
== See also ==
  
The Source engine uses .VTF (Valve Texture) files to store its texture data. These files contain not only the basic source data, but also mip-levels used for rendering the texture over varying distances. .VTF files are created from .TGA files (16, 24, or 32-bits in depth). All .TGA files must have a resolution equal to a power of 2, although the height and width can be different (i.e. 16x16, 32x32, 64x128, 128x128, 128x256, 512x512, etc.).
+
* [[Creating a Material]]
 +
* [[Notepad++ VDF languages|Notepad++ syntax highlighting for materials]]
 +
* [[Valve Texture Format]]
 +
* [[:Category:List of Shader Parameters|List of Shader Parameters]]
 +
* [[Material Editor]]
 +
* [https://github.com/Xyphos/VMTGen VMTGen]
 +
* [http://www.therazzerapp.de/tutorials/vmt_erstellen.html German Tutorial by TheRaZZeRApp]
  
The tool Vtex (located in <code>sourcesdk/bin</code>) is used to compile .TGA files into the .VTF format. Vtex takes a .TGA file and an optional text file that describes extra parameters for compiling the texture. Using these two files then it creates a .VTF file. The .TXT file containing the parameters must share the same base name and directory as the .TGA file being compiled. For example: the test.tga file must be in the same directory as the test.txt file for the tool to link the files properly.
+
[[Category:Material System]]
 
+
[[Category:Glossary]]
If a .TXT file does not exist, Vtex will automatically create an empty file with the correct name and in the correct location. The following section describes the compile parameters that can be inserted in this .TXT file.
 
 
 
To compile a texture, simply drag either the .TXT or .TGA file onto the <code>sourcesdk/bin/vtex.exe</code> icon, or supply the filename of either file as a command-line parameter to Vtex. The tool will report that it created the file and a .VTF will be created in the parallel materials sub-directory to where the source texture resided. '''Note:''' For this to work properly, you must select the correct game directory for your MOD. If the destination directory in <code>/materials</code> does not exist, you must create it in both locations before continuing.
 
 
 
For example, if you’re compiling a TGA file named <code>"sample.tga"</code> that resides in <code>"sourcesdk_content/hl2/materialsrc/test/MyTexture.tga"</code>, the resulting .VTF file will be placed in <code>"/half-life 2/hl2/materials/test/MyTexture.vtf"</code>.
 
 
 
=Vtex .TXT file compile parameters=
 
 
 
The parameters Vtex can parse and understand from the .TXT file are:
 
 
 
{|
 
| <code>$nolod</code> || Do not use lower quality versions of this texture in lower DirectX versions. Used for non-world graphics such as HUD art.
 
|-
 
| <code>$nomip</code> || Do not make mip-levels for this texture. Used for materials like skyboxes and menu backgrounds.
 
|-
 
| <code>$clamps</code><br><code>$clampt</code> || Do not allow the texture to wrap in the S or T coordinate space, respectively. This is most often used for sprites that are not tiled.
 
|-
 
| <code>$skybox</code> || Used for compiling skyboxes. This assures the edges match between each facet.
 
|-
 
| <code>$startframe (integer)</code><br><code>sendframe (integer)</code> || Used for animated textures. Textures must be named as texture000, texture001, texture002, etc. The $startframe defines the beginning frame and the <i>$endframe</i> defines the ending frame.
 
|-
 
| <code>$nocompress</code> || Do not use compression on this texture. Useful for textures with fine gradation (like light halos).
 
|-
 
| <code>$nonice</code> || Do not use NICE filtering on this texture’s lower mip-levels.
 
|- <code>$dxt5</code> || Use DXT5 compression instead of full compression.
 
|}
 
 
 
=Creating a .VMT file=
 
 
 
Once the .VTF file has been created, a .VMT file must be created to actually use the texture inside of the engine.
 
 
 
The .VMT will define how the .VTF file is rendered to the screen. There are many shaders that can be used to render a texture. To begin with, we'll use a simple shader: LightmappedGeneric. This shader is used for world surfaces that should receive lightmaps.
 
 
 
The simplest definition of this shader is:
 
 
 
<pre>
 
"LightmappedGeneric"
 
{
 
    "$basetexture" "test/MyTexture"
 
}
 
</pre>
 
 
 
 
 
 
One of the easiest methods of creating new .VMT files is to open an existing .VMT that has similar properties to the material you are creating. Edit the contents of the file and save it with a different name to create a new material.
 
 
 
=Using Vtex on the command-line=
 
 
 
For advanced users, <code>vtex.exe</code> can also be executed and scripted from a Windows command prompt. See Using Vtex on the command-line for more information.
 
 
 
=Summary of creating materials=
 
 
 
Here is a brief summary of the steps necessary to create a material for the Source engine:
 
 
 
1. Create the source texture in the .TGA format, in 16-, 24- or 32-bit format, with a resolution equal to a power of 2. The .TGA can also contain an alpha channel to be used with effects such as transparency or specularity.
 
2. Save your .TGA to a directory in <code>/materialsrc</code> that corresponds to the directory in <code>/materials</code> where you want the material to appear.
 
 
 
For example:
 
 
 
To have a material compile to <code>Half-Life 2/hl2/materials/metal</code> place the source .TGA into the <code>sourcesdk_content/hl2/materialsrc/metal</code> directory.
 
 
 
If the destination directory in <code>/materials</code> does not exist, you must create it in both locations before continuing.
 
 
 
3. Create a .TXT with any compile parameters in the same location, and name it the same as the source .TGA, but with the .TXT extension. If you don't need to add any compile parameters, skip this step, and the .TXT file will automatically be created for you in the next
 
 
 
4. Compile the .VTF file by dragging the .TGA file onto the vtex.exe icon in the <code>/sourcesdk/bin directory</code>. The .VTF file will be compiled to the target directory in <code>/materials</code> inside the current game directory. For advanced users, Vtex can also be run from a command prompt.
 
 
 
5. Create a .VMT file with a reference to the .VTF you've created.
 
 
 
6. Launch the Hammer editor and check that the new material works properly.
 

Latest revision as of 15:40, 2 September 2021

English Deutsch Español Français Русский 한국어

A material is a .vmt ("Valve Material Type") text file that defines a two-dimensional surface. It contains all of the information needed for Source to simulate the surface visually, aurally, and physically.

The contents of a material will fall into some or all of these categories:

  1. Texture names
  2. Physical surface types
  3. Shader parameters
  4. Fallbacks
  5. Proxies

A simple example

LightmappedGeneric
{
	$basetexture coast\shingle_01
	$surfaceprop gravel
}

This is a very basic shingle beach material.

  1. The LightmappedGeneric shader is used, which means that the material is for use on surfaces with lightmaps (i.e. brushes).
  2. The { character opens a set of parameters
  3. The $basetexture parameter is given with coast\shingle_01, which is the location of a texture. This is what will be drawn on the screen.
  4. $surfaceprop gives the material the physical properties of gravel.
  5. The } character closes a set of parameters

It's important to remember that this material can only be used on brushes. If it needed to be used on models, for instance, another version would need to be created using the VertexLitGeneric shader.

Most of the time switching materials from one shader to another is as simple as changing their first line, since a great number of parameters are shared between them. Some params only work with certain shaders, like Phong effects, which are only available with VertexLitGeneric, but unfortunately you won't encounter any critical errors if a param isn't understood by the shader. It just won't have any effect.

Tip.png Tip: If you ever need to use a space or tab character in a parameter value, you must wrap the whole value with "quote marks". You'll often see absolutely everything wrapped like this - save yourself some typing, as that's unnecessary.

Finding materials

SteamPipe

When Valve updated some games to SteamPipe, the materials were moved from GCF into VPK files. VPK Files work with GCFScape.

More info on SteamPipe here

Non-SteamPipe Games

in non SteamPipe source games, Materials are stored in the materials\ folder of your game or mod. The best way to browse them is from Hammer's texture selection screen.

If you want to edit or view the code of Valve's material files you will first need to extract them from their GCF package with GCFScape. They tend to be stored in GCFs with 'materials' in their name.

See also