Creating a Material: Difference between revisions

From Valve Developer Community
Jump to navigation Jump to search
(redid texture conversion - nobody should use vtex, ever!)
(→‎Syntax: hyperlinks good)
 
(68 intermediate revisions by 31 users not shown)
Line 1: Line 1:
{{LanguageBar}}
{{toc-right}}
This tutorial is a step-by-step guide to creating a brand new [[material]], including the creation of the material's [[texture]].
This tutorial is a step-by-step guide to creating a brand new [[material]], including the creation of the material's [[texture]].


== Creating a texture ==
== Creating a Texture ==
Any image file can be used as a texture, so long as both of its dimensions (height and width) are a power of two (1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096). To decide which to use, examine existing textures that fill the same role as yours. Higher resolutions lower performance but make an image sharper when viewed up close.


Any image file can be used as a texture, so long as both of its dimensions are even: 2, 4, 6, 8, 256, 1024, and so on. Valve's world textures are ''generally'' 512x512, but you are at liberty to use whatever resolution you see fit. Higher resolutions lower performance but make an image sharper when viewed up close. If you go ''too'' high quality may actually degrade because of the amount of resizing required.  
Different classes of object have different standard resolutions (e.g. character models' are very high). Check Valve's choices in {{code|hl2_textures_dir.vpk}} (from {{hl2|1}}) with [[GCFScape]] or [[VPKEdit]] if you are ever confused.


Different types of object have textures of different resolutions: check Valve's choices in <code>source materials.gcf</code> with [[GCFScape]] if you are ever confused.
{{note|Texture source files should be saved in a lossless format like [[Wikipedia:Truevision TGA|TGA]] pre-compile, to prevent unnecessary loss in quality. For textures without an alpha channel, PNG is also acceptable.}}
{{note|The file size limit is 33,554,432 bytes (32 MB). Compiling a .vtf over this amount will cause errors.}}


{{note|Textures should be saved in a lossless format like [[Wikipedia:Truevision TGA|TGA]] or [[Wikipedia:DirectDraw Surface|DDS]], since they will be stored in an almost lossless format when converted for Source anyway!}}
=== Converting the Texture ===


== Converting a texture ==
Textures must be converted to the [[Valve Texture Format]] before Source can use them. The tool Valve provides for this is [[Vtex (Source 1)|VTEX]]. It's a [[command line]] tool with quite a few limitations, so the third-party tool {{Vtfedit|4}} (or {{Vtfeditrld|4}}) is preferable: it provides a graphical user interface, allows you to change a texture's properties without re-compiling it from scratch, accepts a wide range of image formats, and doesn't require the creation of script files for every texture. There are a handful of situations where you'll need to fall back on VTEX… but only a handful. Also, if you have textures that use transparency, it is recommended that you save them in TIFF format before importing to VTFedit as PNG transparency tends to have problems.


''A material's texture(s) must be [[Valve Texture Format]] files. The tool Valve provide for converting them to this format is [[Vtex|VTEX]], but its command-line design makes it a nightmare to try and use, even when you '''do''' know what you're doing.''
(There are VTF plug-ins that let you save to the format directly from [[Photoshop_VTF_Plugin|Photoshop]], [http://code.google.com/p/gimp-vtf/ GIMP], and [http://nemesis.thewavelength.net/index.php?p=50 Paint.NET], but this tutorial will not cover them as not everyone uses those tools.)


''Instead, you should use the third-party tool [[VTFEdit]]. It provides a graphical interface, allows you to change a VTF file's properties without re-compiling, accepts a far wider array of input files, and is generally better in every regard. The author of VTFEdit has also created VTF plug-ins that let you save to the format directly from [http://nemesis.thewavelength.net/index.php?p=39 Photoshop] and [http://nemesis.thewavelength.net/index.php?p=50 Paint.NET], but this tutorial will not cover them as not everyone uses those tools.''
'''To import your texture with VTFEdit''', use <code>File > Import</code> or press {{key|Ctrl+I}}. Select your image and you will be presented with the import screen:


To import your texture with VTFEdit, use <code>File > Import</code> or press Ctrl+I. Select your image and you will be presented with the import options screen:
[[File:Vtfedit importoptions.png|center|VTFEdit's import screen]]


[[Image:Vtfedit importoptions.png|center|VTFEdit's import options screen]]
==== General settings ====
 
=== General settings ===


These settings are the most important. There are three:
These settings are the most important. There are three:


;Normal format
; Normal format
:The sub-format the texture should be stored in if it is opaque.
: The color/compression format should be used if the texture is opaque.
;Alpha format
; Alpha format
:The sub-format the texture should be stored in if it has transparency.
: The color/compression format should be used if the texture has an alpha channel.
;Texture type
; Texture type
:How this texture will be used; normally as an Animated Texture, even if it doesn't actually have animation.
: Importing more than one image determines what they will be used as (animation frames/cubemap faces/depth slices).
 
If you're making a plain and simple world texture you won't need to change any of these. If you're making a texture with fine gradient detail however, [[Valve Texture Format#Choosing_an_image_format|you'll need to choose more suitable formats]].
 
=== Mipmap settings ===
 
These settings define how the texture is scaled when viewed from far away, or when the game is running with medium or low texture detail. The only time you will realistically need to worry about it is when creating interface textures, for which you should disable the check box. This ensures that your menu images are always sharp and readable, even if the very lowest texture detail setting is being used.
 
=== Normal map settings ===


These settings allow you to auto-generate a [[normal map]] from your input image. You'll want to do this properly, with an image editor, for the material you ship, but the automated option is there regardless if you want quick results.
If you're making a plain and simple world texture you won't need to change any of these. If you're making a texture with fine gradient detail however, you'll need to [[Valve Texture Format#Choosing_an_image_format|choose a non-lossy compression mode]].


== Configuring the texture ==
==== Mipmap settings ====


TODO
[[MIP Mapping|Mipmaps]] are low-resolution versions of a texture that swap in when a surface is far away from the camera. They greatly reduce the amount of texture resizing needed, improving both performance and image quality. Mips are required for texture LOD to work.


==Creating the material file==
{{tip|Textures that will appear in the 3D world should have mipmaps. To conserve memory, textures that will only be used in a 2D interface should not.}}


A [[VMT]] file is necessary for the material to appear in the game and in the [[Hammer]] editor. Use Notepad to create the VMT, and save it in the same directory as your VTF. To prevent Notepad from adding .txt to your file name, choose All Files in the dropdown box for file type.
The following images demonstrate the various mipmap filters available in VTFEdit:


This is the content of the VMT:
<gallery perrow=5 widths=150 heights=180>
File:Vtf sharpen filters.jpg|Sharpen filters
File:Vtf mipmap filters1.jpg|Mip filters #1
File:Vtf mipmap filters2.jpg|Mip filters #2
File:Vtf mipmap filters3.jpg|Mip filters #3
File:Vtf mipmap filters4.jpg|Mip filters #4
</gallery>


<pre>"<shader type>"
==== Normal map settings ====
{
    "$basetexture" "<texture name>"
    "$surfaceprop" "<surface type>"
}</pre>


Replace <code>&lt;shader type&gt;</code> with one of the following:
These (mislabelled{{how}}) settings allow you to automatically generate a [[bump map]] from your input image. You'll want to do this properly with an image editor for the material you ship, but the automated option is there if you want quick results or a base to work from.


;LightmappedGeneric
=== Configuring and Saving ===
:This is the most common and is used for surfaces you want to be lit normally.


;UnlitGeneric
When you hit OK the input file is converted (there may be quite a lengthy pause for large images) and the output VTF appears. Now all that is left is configuring the texture with the options in the checkbox list on the left-hand side of the screen. See [[Valve Texture Format#Image_flags]] for descriptions of each.
:This is used for surfaces that are always fully lit like a computer screen or lit lightbulb.


;VertexLitGeneric
Finally, save the file somewhere under your game or mod's <code>\materials</code> folder.
:This is used for models.


After "$basetexture", replace <code>&lt;texture name&gt;</code> with the name of your texture (VTF) along with its path from the materials folder, in quotes. Do not include the VTF extension.
==Creating a Material==
Source doesn't access textures directly. Everything goes through a '''[[material]]'''.


After "$surfaceprop", replace <code>&lt;surface type&gt;</code> with the the surface type of the material, in quotes. (This affects the sound of impacts and look of bullet holes.) Choose one of these surfaces: metal, paper, wood, concrete, glass, brick, tile. For more choices, see [[Material properties]].
Materials are script files that can be created in any text editor, but it's recommended that you use {{npp|4}} in conjunction with the community-made [[Notepad++ VDF languages|syntax highlighting rules]]. For your material to be detected, '''you must save it under your game or mod's <code>\materials</code> folder with the extension <tt>.vmt</tt>'''.
{{note|If you are creating materials and textures exclusively for a map for an existing mod, consider using [[BSPZIP]]\{{pbsp|3.1}}\{{pakrat|3.1}}\{{Compilepal|3.1}} to package the material and texture files within the map file itself. This will prevent them from ever becoming lost.}}
{{warning|Although your computer will let you save a file name that contains the <tt>%</tt> character, and [[VTF]]s work just fine with the character in the name, Hammer '''cannot''' handle [[VMT]]s whose names contain the <tt>%</tt> character.}}


Here's what the VMT for the example brick wall texture would look like:
===Syntax===
A material file looks like this:


<pre><nowiki>"LightmappedGeneric"
<[[shader]]>
{
{
    "$basetexture" "walls/brickwall"
<parameter> <value>
    "$surfaceprop" "brick"
...
}</nowiki></pre>
}


==Some common additional options==
LightmappedGeneric
{
$basetexture coast\shingle_01
$surfaceprop gravel
}


;"$model" 1
There are many [[shader]]s to choose from, but most materials will use either {{ent|LightmappedGeneric}} ([[brush]]es) or {{ent|VertexLitGeneric}} ([[model]]s). The third most common shader is {{ent|UnlitGeneric}}, which is used for [[VGUI2|UI]] materials and the occasional [[tool texture]]. A full list of shaders can be found [[:Category:Shaders|here]].
:This line is necessary for model skins. Also note that the TGA should also be included in the same directory with the VTF and VMT for the modeler to skin with. Try to keep the skin for each model entirely in one texture—using multiple textures on a model is a resource hog. Remember that the shader type for models is always <code>"VertexLitGeneric"</code>.


;"$decal" 1
===Parameters===
:This is necessary for decals.
:''For a list of all documented shader parameters, see [[:Category:List of Shader Parameters]].''


;"$selfillum" 1
With a shader chosen, you're onto parameters (also called commands). There are hundreds of options for what to put in a material so this article will only cover the most common, which are accepted by more or less all shaders. They are:
:Adding this line will cause the texture to self-illuminate according to the alpha channel of your base texture. An example would be a Combine soldier’s helmet which is lit normally except for the eyes, which glow even in the dark.
*{{ent|$basetexture}}
*{{ent|$surfaceprop}}
*{{ent|$envmap}}
*{{ent|$bumpmap}}
*{{ent|$detail}}
*{{ent|$selfillum}}
*{{ent|$model (VMT)|alt=$model}}
{{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, but this is wholly unnecessary.}}
{{tip|To refresh a material in-game without restarting it, use the following console command: <tt>mat_reloadmaterial "vmt_name without .vmt and quotation marks"</tt>. Even though the <code>mat_reloadallmaterials</code> command exists, it's not recommended to use it, since various artifacts may appear. <tt>mat_reloadtextures</tt> is presented, too, but it seems to be very buggy.
For instance:
  mat_reloadmaterial combine_barricade_short01a
will reload every single material in the filesystem named <tt>combine_barricade_short01a</tt>, including all the custom content. For example: both <tt>materials/models/props_combine/combine_barricade_short01a.vmt</tt> and <tt>materials/models/props_metal_barricade/combine_barricade_short01a.vmt</tt> will be reloaded. This may appear very useful when writing proxies.}}


;"$nocull" 1
== Utilities ==
:Add this line if you want your texture to be visible from both sides. This is only applicable for flat objects like glass or wire fences.
{{Xblahmt|4}}
 
;"$alpha" 0.5
:Adding this line will make the texture translucent. The number can be anything between zero and one.
 
;"$color" "[1 1 1]"
:This will scale the color of the red, green, and blue components of your texture. You might use it if you wanted to use the same texture for multiple materials, such as various colors of the same looking glass.
 
{{note|All entries added to a VMT should be in quotes, except for numbers that appear alone.}}


==See also==
==See also==
* [[Material]]
* [[Material]]
* [[Shader Types and Parameters]]
* [[Material Editor]]
* [[Normal Map Creation]]
* [[Creating Reflective Materials]]
* [[Reflective Materials]]
* [[Creating PBR materials]]
* [[Parallax mapping]]
* [[Adapting PBR Textures to Source]]
* [[:Category:List of Shader Parameters]]


[[Category:Material System]]
[[Category:Material System]]
[[Category:Tutorials]]
[[Category:Tutorials]]

Latest revision as of 03:23, 27 January 2025

English (en)Español (es)Français (fr)日本語 (ja)한국어 (ko)Português do Brasil (pt-br)Русский (ru)中文 (zh)Translate (Translate)

This tutorial is a step-by-step guide to creating a brand new material, including the creation of the material's texture.

Creating a Texture

Any image file can be used as a texture, so long as both of its dimensions (height and width) are a power of two (1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096). To decide which to use, examine existing textures that fill the same role as yours. Higher resolutions lower performance but make an image sharper when viewed up close.

Different classes of object have different standard resolutions (e.g. character models' are very high). Check Valve's choices in hl2_textures_dir.vpk (from Half-Life 2) with GCFScape or VPKEdit if you are ever confused.

Note.pngNote:Texture source files should be saved in a lossless format like TGA pre-compile, to prevent unnecessary loss in quality. For textures without an alpha channel, PNG is also acceptable.
Note.pngNote:The file size limit is 33,554,432 bytes (32 MB). Compiling a .vtf over this amount will cause errors.

Converting the Texture

Textures must be converted to the Valve Texture Format before Source can use them. The tool Valve provides for this is VTEX. It's a command line tool with quite a few limitations, so the third-party tool VTFEdit VTFEdit (or VTFEdit Reloaded VTFEdit Reloaded) is preferable: it provides a graphical user interface, allows you to change a texture's properties without re-compiling it from scratch, accepts a wide range of image formats, and doesn't require the creation of script files for every texture. There are a handful of situations where you'll need to fall back on VTEX… but only a handful. Also, if you have textures that use transparency, it is recommended that you save them in TIFF format before importing to VTFedit as PNG transparency tends to have problems.

(There are VTF plug-ins that let you save to the format directly from Photoshop, GIMP, and Paint.NET, but this tutorial will not cover them as not everyone uses those tools.)

To import your texture with VTFEdit, use File > Import or press Ctrl+I. Select your image and you will be presented with the import screen:

VTFEdit's import screen

General settings

These settings are the most important. There are three:

Normal format
The color/compression format should be used if the texture is opaque.
Alpha format
The color/compression format should be used if the texture has an alpha channel.
Texture type
Importing more than one image determines what they will be used as (animation frames/cubemap faces/depth slices).

If you're making a plain and simple world texture you won't need to change any of these. If you're making a texture with fine gradient detail however, you'll need to choose a non-lossy compression mode.

Mipmap settings

Mipmaps are low-resolution versions of a texture that swap in when a surface is far away from the camera. They greatly reduce the amount of texture resizing needed, improving both performance and image quality. Mips are required for texture LOD to work.

Tip.pngTip:Textures that will appear in the 3D world should have mipmaps. To conserve memory, textures that will only be used in a 2D interface should not.

The following images demonstrate the various mipmap filters available in VTFEdit:

Normal map settings

These (mislabelled[How?]) settings allow you to automatically generate a bump map from your input image. You'll want to do this properly with an image editor for the material you ship, but the automated option is there if you want quick results or a base to work from.

Configuring and Saving

When you hit OK the input file is converted (there may be quite a lengthy pause for large images) and the output VTF appears. Now all that is left is configuring the texture with the options in the checkbox list on the left-hand side of the screen. See Valve Texture Format#Image_flags for descriptions of each.

Finally, save the file somewhere under your game or mod's \materials folder.

Creating a Material

Source doesn't access textures directly. Everything goes through a material.

Materials are script files that can be created in any text editor, but it's recommended that you use Notepad++ Notepad++ in conjunction with the community-made syntax highlighting rules. For your material to be detected, you must save it under your game or mod's \materials folder with the extension .vmt.

Note.pngNote:If you are creating materials and textures exclusively for a map for an existing mod, consider using BSPZIP\Packbsp\Pakrat\Compile Pal to package the material and texture files within the map file itself. This will prevent them from ever becoming lost.
Warning.pngWarning:Although your computer will let you save a file name that contains the % character, and VTFs work just fine with the character in the name, Hammer cannot handle VMTs whose names contain the % character.

Syntax

A material file looks like this:

<shader>
{
	<parameter> <value>
	...
}
LightmappedGeneric
{
	$basetexture coast\shingle_01
	$surfaceprop gravel
}

There are many shaders to choose from, but most materials will use either LightmappedGeneric (brushes) or VertexLitGeneric (models). The third most common shader is UnlitGeneric, which is used for UI materials and the occasional tool texture. A full list of shaders can be found here.

Parameters

For a list of all documented shader parameters, see Category:List of Shader Parameters.

With a shader chosen, you're onto parameters (also called commands). There are hundreds of options for what to put in a material so this article will only cover the most common, which are accepted by more or less all shaders. They are:

Tip.pngTip: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, but this is wholly unnecessary.
Tip.pngTip:To refresh a material in-game without restarting it, use the following console command: mat_reloadmaterial "vmt_name without .vmt and quotation marks". Even though the mat_reloadallmaterials command exists, it's not recommended to use it, since various artifacts may appear. mat_reloadtextures is presented, too, but it seems to be very buggy.

For instance:

 mat_reloadmaterial combine_barricade_short01a
will reload every single material in the filesystem named combine_barricade_short01a, including all the custom content. For example: both materials/models/props_combine/combine_barricade_short01a.vmt and materials/models/props_metal_barricade/combine_barricade_short01a.vmt will be reloaded. This may appear very useful when writing proxies.

Utilities

XBLAH's Modding Tool XBLAH's Modding Tool

See also