This article's documentation is for anything that uses the Source engine. Click here for more information.

$envmap

From Valve Developer Community
(Redirected from $envmaptint)
Jump to navigation Jump to search
English (en)Русский (ru)中文 (zh)Translate (Translate)
For the set of material parameters used to mask specular reflections via textures, see $envmapmask.

$envmap is a material shader parameter available in all Source Source games.

Specular reflections at full intensity.

It most commonly used to approximate the specular reflections seen on smooth surfaces, such as metals, glass and plastics. This is achieved by mapping the vertex normals of a model to positions on a cubemap - specified by $envmap - and adding the sampled color from the cubemap to the model's surface. Typically, the cubemap is a simple, static "environment map" which has been pre-rendered (although a custom cubemap can be animated).

When $envmap is set to env_cubemap and buildcubemaps has been run on the map, the engine will automatically select the environment map contained in the BSP which is nearest to the model as it moves through the world, providing a very rough approximation of dynamic reflections.

The other form of specularity supported by Source is the Phong type, which provides simpler reflections on both static and dynamic models.

Availability

$envmap is available in all Source engine based games.

Note.pngNote:Most Half-Life: Source textures use static reflective images (located in environment maps folder) instead of env_cubemap by default. So if you have already built cubemaps, most of these textures will still use static reflective images, unless it's .vmt files were edited.

Syntax

$envmap env_cubemap

"env_cubemap" is typically used, which tells VBSP to swap in the name of the nearest env_cubemap when the map compiles. However, it is also possible to use a static cubemap image that has been manually created.

Note.pngNote:If $envmap is used without specifying $basetexture, the specular reflections will be force drawn regardless of anything.
Note.pngNote:Cubemap transparency is affected by the $basetexture alpha/transparency map even when using $envmapmask. This causes conflicting problems.
Icon-Important.pngImportant:Decals which use env_cubemap will use the closest cubemap to the player, not the closest cubemap to the decal itself!
Confirm:Overlays might get fixed by VBSP, like occurs for brushes and displacements.

Creating a custom static cubemap texture

Creating a custom cubemap texture might be required for some circumstances and is used occasionally in some Valve models.

Icon-Bug.pngBug:Cubemaps flagged as sRGB will only properly load as sRGB when HDR is disabled. Either include an HDR .hdr.vtf equivalent for an sRGB-encoded SDR .vtf cubemap, or encode the SDR cubemap in regular RGB.  [todo tested in ?]
Tip.pngTip:Due to non-RGBA16F cubemaps having issues in HDR mode (often glowing white), it is preferable to include .hdr.vtf equivalents for all .vtf cubemaps. cubemap2hdr will correctly convert an SDR cubemap (such as one made in VTFEdit) to HDR.

In VTFEdit

For this to work you will need to have the 6 sides of the cubemap saved as individual textures in the correct orientation for it to appear correctly in the Source Engine. So some faces might need to be rotated either clockwise or counter clockwise. Each face will need to be mirrored horizontally before orientation.

Note.pngNote:Pre-Alien Swarm Alien Swarm games require 7 faces for the cubemap to work. The seventh in this case is a round texture, used as a sphere map for the (broken) $envmapsphere parameter. VTFEdit can generate this face automatically, via the "Advanced" tab when importing.

To the right hand side of this page you will find two guide images to aid in properly rotating each side of your environment render. Save one or both of those guides on your computer and then import your environment render onto it or recreate it in a smaller scale, then rotate your sides as instructed by the images. Save every side as a separate image file, their names should be the number found on that specific square. This will ensure that the environment texture will be built correctly, as the numbers also represent the import order in VTFEdit.

Orientation guide for T-assembled environments. Click to enlarge
Orientation guide for Blender render results. Click to enlarge

Once you have all the image sides ready you can just import them all into VTFEdit and select "Environment Map". Texture format is up to you, generally DXT1 will do.

Cubemap tut1 cra0.png

Enable the "Clamp S" and "Clamp T" texture flags.

Then just save it all as a .vtf file and use it in your VMT like so:

"$Envmap" "environment maps/my_cubemap"

"environment maps" is the usual folder for environments/cubemaps You may put yours anywhere you like.

In VTEX

VTEX will automatically mirror and orient your faces, you need only to provide correctly named or built textures.
You may either supply six separate faces, or one "T-Assembly" image with the faces arranged in a specific layout. Some games, like Left 4 Dead 2 Left 4 Dead 2 require this T-Assembly shape when building HDR Cubemaps.
If you want to have HDR envmaps, you must export the texture(s) as a BMP format and run those through HDRShop to create PFM files.

If you would like a video guide with the prefabs to create LDR & HDR cubemaps (as well as skyboxes) via a mostly automated Python script, visit this link. Following this guide will circumvent everything shown below.

Separate faces

Each one of your faces must have the correct suffix BK, FT, LF, RT, UP, DN for VTEX to use them.
You have two ways of making such textures from within the game. Either using the Envmap ConCommand, or the Garry's mod addon Cubemap Camera. Both have their Pro's and Con's.
You may also source these images from anywhere you'd like. They don't need to be made from within a game.

Note.pngNote:The naming convention of the faces makes little sense, as the names do not actually align with the cardinal directions of hammer.
BK is north, RT is east, FT is south, LF is west, relative to Hammer's 2D "Top" view.
Using Envmap ConCommand

Using the Envmap ConCommand is possible in every game, but the image quality is not the best, as the gamma is slighty different, and the images appear to be less saturated.

Note.pngNote:The command runs on the same system as the buildcubemaps command, except it renders everything. Including dynamic props, npc's, players, etc.

First you may want to set the ConVars mat_envmaptgasize and mat_envmapsize to set the scale of the faces you will get.
In some games, such as Left 4 Dead 2 Left 4 Dead 2 these commands are hidden and cannot be changed, unless you wrap the commands in the following string:

script Convars.SetValue( "mat_envmaptgasize" ,256)
script Convars.SetValue( "mat_envmapsize" ,256)

Resolutions larger than 256x256 are possible, but require a specific screen resolutiuon. More info here.
After setting the scale you may run the Envmap ConCommand. The game will generate cubemap faces from your current location and deposit the PFM files in the folder cubemap_screenshots.

Now that you have these PFM files, you can either run these through VTEX immediately, or turn them into a T-Assembly.

Using Garry's Mod Addon

The aforementioned Cubemap Camera addon generates PNG files as an output, but the orientation is incorrect for our uses, so they need to be changed later.

First, keep in mind that this addon's output is oriented along your current view. So the Front is wherever you look, even if it's down at the ground.
You may want to use the command Setang 0 270 0 to make the player look towards the "front" (which is actually south), so it aligns in the same way as the Envmap ConCommand would.

On this image below you have the result of this addon as png on the top, and the outputs of the Envmap ConCommand on the bottom (after turning them to a TGA).
Note how the Down.png needs to be rotated 90° clockwise, Up.png needs to be rotated 90° counter clockwise, Left.png and Right.png need to be swapped.
Furthermore, the filenames must be renamed to use the BK, FT, LF, RT, UP, DN suffixes that VTEX requires.
Envmap-concommand-vs-gmod-addon.jpg

For the lazy individual, you may use the FFMPEG script in the expandbox below as a .bat file to do the rotation and renaming for you.


ffmpeg	-i down.png	-vf "transpose=1" -y tmp_dn.png
ffmpeg	-i up.png	-vf "transpose=2" -y tmp_up.png
del 	down.png
del 	up.png
move	/Y	tmp_dn.png		envdn.png
move	/Y	tmp_up.png		envup.png
move		left.png		tmp_lf.png
move	/Y	right.png		env_lf.png
move	tmp_lf.png			envrt.png
move	front.png			envft.png
move	back.png			envbk.png

Now that you have these PNG files, you can turn them into a T-Assembly layout to run through HDRShop or VTEX. Neither of those tools accept PNG, but the next FFMPEG scripts on this page will sort that out for you.

Preparing T-Assembly

To create the T-Assembly image you can use any image editing software of your choice to align the faces as seen in the image below.

If you prefer to automate this, you can use the FFMPEG script in the expand box below as a .bat file to do this for you.
The resulting image is what you see below. Ignore that the UP face was copied over multiple times, as it has no effect and it was easier to do in the script.


@echo off

:: If you want to run this through HDRShop, set the z_output at the end to BMP. As HDRShop cannot interpret FFMPEG's TGA files
:: If you do not want HDR PFM's, use TGA for the Z_output instead.

:: Uncomment these lines below if you want to turn your PFM to PNG, and then turn it into a T-Assembly immediately.
::	for %%S in (up dn bk ft lf rt) do (
::		for %%F in (*%%S.pfm) do (
::			ffmpeg -i "%%F" -y -vf "scale=in_range=full:out_range=full,eq=gamma=2.2" -pix_fmt bgr24 "env%%S.png"
::		)
::	)

ffmpeg ^
-i envup.png -i envup.png -i envup.png -i envup.png ^
-i envft.png -i envlf.png -i envbk.png -i envrt.png ^
-i envup.png -i envup.png -i envup.png -i envdn.png ^
-filter_complex "xstack=inputs=12:layout=0_0|w0_0|w0+w4_0|w0+w4+w8_0|0_h0|w0_h0|w0+w4_h0|w0+w4+w8_h0|0_h0+h1|w0_h0+h1|w0+w4_h0+h1|w0+w4+w8_h0+h1" -y Z_output.tga

Cubemap axis reference 1.jpg

With this image done, you can either run the resulting TGA through VTEX as-is, or turn it into a BMP to import into HDRShop, to alter the gamma and create a PFM file.

Creating Envmaps

Once you have one T-Assembly TGA/PFM texture — or six TGA/PFM faces named as such: EnvBK.tga, EnvFT.tga, EnvLF.tga, EnvRT.tga, EnvUP.tga, EnvDN.tga. — You're ready for VTEX.

VTEX however needs a text file to receive inputs such as flags or settings. You may create this file manually, or use yet another script.

Automated

Use the script in the expand box below to create this text file, generate a finished cubemap, and then delete the text file. Which removes some manual labor.
Simply create a bat file with the following contents, edit the paths in the yellow lines, and then drag-drop your tga file onto the script.


@echo off
setlocal enabledelayedexpansion

:: Path to your game
SET Exe_Path=D:\Programme\Steam\steamapps\common\Left 4 Dead 2

:: Create the txt next to the bat
set txtfile=%~n1.txt

:: Write content into the txt
(
echo // pfm 1				// For HDR only. read .pfm file instead of .tga
echo // pfmscale 1			// For HDR only. higher values will result in a brighter sky
echo // nocompress 1		// Outputs a BGR888 instead of DXT1/
echo stripalphachannel 1	// Skyboxes and cubemaps do not need an alpha channel. removing them saves disk space.
echo nonice 1				// prevent seams appearing at low texture detail.
echo clamps 1				// Clamping sides to prevent wrapping.
echo clampt 1				// Clamping top/bottom to prevent wrapping.
echo nolod 1				// flags vtf with "No Level of Detail"
echo cubemap 1				// Vtex may need this, else it prints errors.
) > "%txtfile%"

::	Create cubemap. change subfolder  below HERE from "left4dead2" to wherever your gameinfo.txt is.
"%Exe_Path%\bin\vtex.exe" -game "%Exe_Path%\left4dead2" -outdir "%~dp0\" -nopause %~n1.txt

del "%~dp0\%~n1.txt"
endlocal

HDR envmaps will be named like Env.hdr.vtf, and only need to exist to work. You might need to add the .hdr.vtf suffix yourself. You may also get pwl.vtf files which in most cases have no use and can be deleted.

Manually

Create a text file the same name as your textures, but without the suffix. For this example it would be Env.txt.
If building for HDR write in the following, otherwise leave it blank:

pfm 1        // read .pfm file instead of .tga
pfmscale 1   // higher values will result in a brighter sky
nocompress 1 // disable compression (HDR compression is only supported by skyboxes)
// cubemap 1 // Vtex may need this, else it prints errors.

Put all your textures and the text file into materialsrc and then just drag-n-drop the .txt file into vtex.exe and you should end up with a working cubemap!
HDR envmaps will be named like Env.hdr.vtf, and only need to exist to work. You might need to add the .hdr.vtf suffix yourself. You may also get pwl.vtf files which in most cases have no use and can be deleted.

Parameters and Effects

$envmapmask  <texture>
See $envmapmask. A texture file that determines per-texel reflection intensity.
$envmaptint <RGB matrix>
Controls the intensity of the reflection's red, green and blue color channels. Any positive number can be used. Default is "[1 1 1]", which means 100% intensity.
Icon-Important.pngImportant: On VertexLitGeneric this parameter is being converted from gamma to linear. The input will turn into X^2.2f. For example, 0.25 is not 25% reflectivity, it's actually ~5%!
Note.pngNote:You must use quotemarks, as there are space characters within the value.
Note.pngNote:You can also define the value as a single float, in which case it should equally apply to all three channels (e. g. $envmaptint 0.5 will equate to $envmaptint "[0.5 0.5 0.5]"). In this case you won't need the quotemarks. However, this method appears to not be 100% reliable.[confirm]
Tip.pngTip:This command is often used to dim the brightness of a specular reflection without the overhead of an $envmapmask.
$envmapcontrast <normal>
Controls the contrast of the reflection. 0 is natural contrast, while 1 is the full squaring of the color (i.e. color*color).
Note.pngNote:Will not work when Phong is enabled.
Tip.pngTip:Use higher contrasts to diminish relatively darker areas and increase "hot spots".
$envmapsaturation <RGB matrix>
Controls the color saturation of the reflection. 0 is greyscale, while 1 is natural saturation. R, G and B can be de/oversaturated respectively using "[r g b]"
Note.pngNote:In the Shadercode for Source 2013 Source 2013 and Alien Swarm Alien Swarm the parameter is defined as a float parameter. However it will be used as a vec3 and the Shader expects and uses it as such.
Note.pngNote:Will not work when Phong is enabled on models.
Icon-Bug.pngBug:Source 2013 MultiplayerTeam Fortress 2 branch Works only on models.
Confirm:Is this the case for other branches as well?
  (tested in: Counter-Strike: SourceDay of Defeat: Source)
$envmapframe <integer>
The frame to start an animated cubemap on.
$envmapmode <integer> Obsolete
Depreciated MATERIAL_VAR_ENVMAPMODE = (1 << 25), // OBSOLETE
$basetexturenoenvmap and $basetexture2noenvmap <boolean> (DX9 SM2) (removed since Counter-Strike: Global Offensive)
Shader(s): WorldVertexTransition
Used for materials with two albedos, to make one or the other matte. See also $basetexture and $basetexture2.
Note.pngNote:Superseded in Counter-Strike: Global Offensive by $envmapmask2.
$envmapoptional <choices> (removed since Left 4 Dead)
Sets the oldest DirectX version that should draw the reflection. Choose from:
  • 80 (DirectX 8)
  • 81 (DirectX 8.1)
  • 90 (DirectX 9)
  • 95 (DirectX 9 with Shader Model 3)
$envmapsphere <boolean> Obsolete (also in Garry's Mod) (removed since Alien Swarm)
Determines whether the material's envmap should be a spheremap (using matcap shading like GoldSrc "chrome" textures) instead of a cubemap. Set this to 1 to use a spheremap.
Icon-Bug.pngBug:In DirectX 9 mode, this will instead act like a regular cubemap with all 6 faces being identical.  [todo tested in ?]
$noenvmapmip <boolean> (only in Counter-Strike: Global Offensive)
Shader(s): LightmappedGeneric
Only use the top-level mipmap of the cubemap.
$fresnelreflection <float> (in all games since Source 2006)
Shader(s): LightmappedGeneric, Lightmapped_4WayBlend, WorldVertexTransition
Adds a Fresnel effect to the reflection. 0 is none, while 1 applies the full effect, similar to Water. The effect becomes multiplied with values higher than 1.
Icon-Bug.pngBug:Value behaves inverted in Black Mesa, 0 applies the full effect, while 1 is none.  [todo tested in ?]
$envmapfresnel <float> (DX9 SM2) (in all games since Source 2007)
Shader(s): VertexLitGeneric
The same as $fresnelreflection, but for VertexLitGeneric. By default, this will use the fresnel ranges set by $phongfresnelranges.
Note.pngNote:Only works with Phong enabled before Left 4 Dead.
$envmapfresnelminmaxexp <vector> (in all games since Left 4 Dead)
Shader(s): VertexLitGeneric
Sets fresnel ranges for VertexLitGeneric materials using $envmapfresnel without phong. By default "[0 1 2]", making surfaces facing the viewer less reflective than surfaces facing sideways.
Note.pngNote:The fresnel values for this parameter are not the same as those for $phongfresnelranges.Instead, the first value is the minimum amount of fresnel, the second value is the maximum, and the final value is exponent.
Icon-Bug.pngBug:Cannot be used with $bumpmap. $phongfresnelranges must be used instead.  [todo tested in ?]
$envmaplightscale <float> (in all games since Alien Swarm) (also in Garry's Mod)
Shader(s): LightmappedGeneric, Lightmapped_4WayBlend Counter-Strike: Global Offensive, VertexLitGeneric Counter-Strike: Global Offensive, WorldVertexTransition Counter-Strike: Global Offensive
Tint the surface's specular reflection with its lightmap. This matches a surface's reflection with its diffuse lighting and acts as rudimentary reflection occlusion. With a value of 1, a surface will have no reflection if it isn't lit at all. For models in Counter-Strike: Global Offensive, vertex lighting is used for the tinting instead of a lightmap.
Note.pngNote:Values above 1 for this parameter are technically permitted, but start inverting the effect.
Icon-Bug.pngBug:Cannot be used with $envmapmask. $normalmapalphaenvmapmask must be used instead.  (tested in: gmod)
$envmaplightscaleminmax <vector2> (only in Counter-Strike: Global Offensive) (also in Garry's Mod)
Shader(s): LightmappedGeneric, Lightmapped_4WayBlend, VertexLitGeneric, WorldVertexTransition
Thresholds for the lightmap reflection tinting effect. Setting the minimum value higher increases the minimum light amount at which the reflection gets nerfed to nothing.
A vent without and with reflection tinting. The bottom image lists the material parameters used to create the effect.
$envmapanisotropy <boolean> (only in Counter-Strike: Global Offensive)
Shader(s): LightmappedGeneric, Lightmapped_4WayBlend, WorldVertexTransition
Enables anisotropic reflection emulation by warping the reflection downwards. For this it uses the alpha channel of the $bumpmap. It linearly interpolates between a normal reflection vector and an anisotropic one. This means that values of 0 in the alpha will result in normal reflections, and a value of 1 will result in anisotropic reflections.
Icon-Bug.pngBug:Does not work with $normalmapalphaenvmapmask  [todo tested in ?]
Icon-Bug.pngBug:Does not work with $basealphaenvmapmask except Lightmapped_4WayBlend  [todo tested in ?]
Note.pngNote:Requires a $bumpmap to be present.
$envmapanisotropyscale <normal> (only in Counter-Strike: Global Offensive)
Shader(s): LightmappedGeneric, Lightmapped_4WayBlend, WorldVertexTransition
Multiplier for how much the reflection should be warped.
An in-game example of anisotropic reflections, with $envmapanisotropyscale set to 1.

Console commands

Cvar/Command Parameters or default value Descriptor Effect
buildcubemaps Generates cubemaps for use in materials. If this isn't run, objects will reflect the skybox, or using default cubemaps, or have an invalid reflection, such as missing textures, looks extremely bright (with HDR on) or completely black.
Note.pngNote:To properly build cubemaps, see the instructions on Cubemaps - Building cubemaps section.
envmap Generates an environment map at the player's position and dumps it in cubemap_screenshots/, in six separate PFM files (or, if running without HDR, TGA files).
Tip.pngTip:Cubemaps created this way will capture dynamic entities and models in them, which are normally hidden when using buildcubemaps.
mat_envmaptgasize int Sets the resolution for the files created with envmap. Default value: 32
mat_fastspecular bool Quickly disable or enable specular rendering without reloading materials. This does not affect performance, only appearance. mat_specular must be used for proper performance testing.
Icon-Bug.pngBug:Doesn't work in some games.  [todo tested in ?]
Note.pngNote:Despite common misconception, setting this to 0 does not enable a "fancy specular" mode, it simply disables specular rendering entirely.
mat_specular bool Disable or enable specular reflections, unloading or loading the specular materials from memory. Default 1.
Note.pngNote:This console variable is flagged as "launcher"-only in Left 4 Dead and Left 4 Dead 2, so use the following command instead:
Left 4 Dead sm_cvar building_cubemaps <0 or 1> command (with SourceMod installed) or
Left 4 Dead 2 script Convars.SetValue("mat_specular", "<0 or 1>") (using VScript).
r_showenvcubemap bool Debug command to display cubemaps on all dynamic objects at full intensity. It was used to create the image at the start of this article.

See also