Refract

From Valve Developer Community
Jump to: navigation, search
Deutsch Français
A refracting func_brush.

Refract is a material shader available in all Source games. It distorts objects behind it. It is similar to Water, but does not have real-time reflections and so is not restricted to flat surfaces. Can be used to create distorting glass windows, ice, etc.

The refraction effect is achieved by first rendering the view without the refracting surface, and then using the normal map to distort the image by replacing pixel coordinates with new ones from the pre-rendered view by projecting along the normals.

Note:Will not work when applied to world brushes in older engines.[Clarify]
Note:When being used on a model, you must specify $model "1".
Note:Multiple layers of the Refract shader will not render when applied on the same entity.

Keep in mind that refraction is expensive to render when sorted. Multiple layers of refraction can cause immense overdraw. Avoid having large amounts of particle sheets, or multiple surfaces with refract that render above each other.

Syntax

Refract
{
	$normalmap ...
	$dudvmap ...
	$refracttint "[1 1 1]"
	$refractamount .2
}
$normalmap <texture>
$dudvmap <texture>  (DX8)
The pattern of refraction is defined by a normal map (DX9+) or DUDV map (DX8-). May be animated.
$normalmap2 <texture> (in all games since <Source><Source>)  (DX9+)
If a second normal map is specified, it will be blended with the first one.
$bumpframe and $bumpframe2 <integer>
The frame to start an animated bump map on.
$bumptransform and $bumptransform2 <vector3>
Transforms the bump map texture.
The default position is "center .5 .5 scale 1 1 rotate 0 translate 0 0".
  1. center defines the point of rotation. Only useful if rotate is being used.
  2. scale fits the texture into the material the given number of times. '2 1' is a 50% scale in the X axis.
  3. rotate rotates the texture counter-clockwise in degrees. Accepts any number, including negatives.
  4. translate shifts the texture by the given numbers. '.5' will shift it half-way.
Note:All values must be included!
Bug: Scaling the texture may cause odd issues where the Texture Lock tool in Hammer will not actually lock the texture in place.
Bug: Rotating textures applied on brushes will rotate around the map origin (confirm: Orangebox engine only?). A fix for this is to change the center position in the VMT to the brush's origin.
$refracttint <RGB matrix>
$refracttinttexture <texture>
Tints the colour of the refraction either uniformly or per-texel.
$refracttinttextureframe <integer>
Frame to start an animated $refracttinttexture on.
$refractamount <float>
Controls the strength of the refraction by multiplying the normal map intensity.
$bluramount <integer>
Adds a blur effect. Valid values are 0, 1 and 2 (0 and 1 for DX8-).
$forcerefract <boolean>  (DX8)
Forces the shader to be used for cards with poor fill rate.
$envmap <texture>
Adds a cubemap reflection on top of the refraction.
$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.
Note:You must use quotemarks, as there are space characters within the value.
$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).
$envmapsaturation <normal>
Controls the colour saturation of the reflection. 0 is greyscale, while 1 is natural saturation.
$fresnelreflection <float> (in all games since <Source><Source>)
Bug: Broken - Not implemented for this shader despite the parameter existing.
$fadeoutonsilhouette <boolean>
Fades out the refraction effect around the edges.
$vertexcolormodulate  (in all games since <Source><Source>)  (DX9+)
To do: Use the vertex color to affect the refract color. Alpha will adjust refract amount. See $vertexcolor.
$basetexture <texture>  (DX9+)
Use a texture instead of rendering the view for the source of the distorted pixels.
$masked <boolean>
To do: mask using dest alpha
$forcealphawrite <boolean> (in all games since <Source><Source>)  (DX9+)
To do: Force the material to write alpha to the dest buffer
$nowritez <integer>
To do: 0 == write z, 1 = no write z
$noviewportfixup <integer> (in all games since <Alien Swarm>)
Don't adjust UVs for current viewport.
$mirroraboutviewportedges <integer> (in all games since <Alien Swarm>)
Don't sample outside of the viewport.
$magnifyenable <boolean> (in all games since <Alien Swarm>)
Enable magnification of the refracted image around the $magnifycenter screen position by $magnifyscale.
$magnifycenter <vector2> (in all games since <Alien Swarm>)
Magnify the refracted image around this screen position.
$magnifyscale <float> (in all games since <Alien Swarm>)
Magnify the refracted image by this factor.
$localrefract <boolean> (in all games since [Portal 2])
Use the alpha channel of the base texture to create a parallax effect.
$localrefractdepth <float> (in all games since [Portal 2])
Depth of the parallax effect in units.

Caveats

  • Currently it is not possible to define an overall alpha mask to indicate where to fall-off refract. This however only applies to the brush based and model geometry, and not particles. To get alpha on brush or model based geometry you can create a normal map with an alpha channel, and define that as the first $normalmap. Using another normal of your choice you can then define a normal map for $normalmap2. $normalmap2 will inherit the alpha of the first normalmap, and will act accordingly. This is extremely useful for faking effects like scrolling water of a surface. Where the first normal map can provide the alpha, and the second normal map can move along that alpha.
  • If Refract is used on a part of a model, the rest of the model will be rendered invisible when seen through the refractive part due to render sorting. Consider making the refractive part a separate model to remedy the issue.

See also