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

$alphatest: Difference between revisions

From Valve Developer Community
Jump to navigation Jump to search
(Changed to use Shaderparam and MatParam templates, misc fixes)
No edit summary
 
(51 intermediate revisions by 13 users not shown)
Line 1: Line 1:
{{Shaderparam|$alphatest}} It specifies a mask to use to determine binary opacity. White represents fully opaque, while black represents fully transparent. Any values in-between are rounded to either 0 or 1. This effect is similar to {{ent|$translucent}}, except that it can not be semi-opaque, is much faster to render, and the engine can sort it properly when layered.
{{LanguageBar|title=$alphatest}}


==VMT syntax example==
{{this is a|shader parameter|name=$alphatest}} It specifies a mask to use to determine binary [[opacity]]. White represents fully opaque, while black represents fully transparent. Any values in-between are rounded to either 0 or 1, unless <code>$allowalphatocoverage</code> is also used. This effect is similar to <code>[[$translucent]]</code>, except that it can not be semi-opaque (without {{w|Colour banding|banding}}), is much faster to render, and the engine can always sort it properly when layered (instead of only on non-[[func_detail|detail]] [[worldspawn]]).


$alphatest <[[bool]]>
==Syntax==
{{CodeBlock|$alphatest <[[Boolean|bool]]>}}


[[LightmappedGeneric]]
==VMT example==
{
{{CodeBlock|lines=7|[[LightmappedGeneric]]<nowiki>
[[$basetexture]] glass\window001a
{
'''$alphatest 1'''
</nowiki>[[$basetexture]]<nowiki> "glass\window001a"
        ''' '''
$alphatest 1
        '''$alphatestreference .5'''
$alphatestreference 0.5
        '''$allowalphatocoverage 1'''
$allowalphatocoverage 1
}
}</nowiki>}}


==Additional parameters==
==Additional parameters==
{{MatParamDef|$alphatestreference|nolink=1|float|Specifies the threshold alpha channel value at which the surface should be transparent instead of opaque. A value of ".3" will create a thicker shape while a value of ".7" will create a thinner shape.
{{expand|title=Notes|
{{Important|Unintuitively, this parameter does ''not'' default to "0.5". LightmappedGeneric, UnlitGeneric, and VertexLitGeneric all default to "0.7" instead. To avoid unexpected results, this parameter should always be set.}}
{{bug|Updating <code>$alphatestreference</code> in real time with [[Material_proxies|proxies]] only affects projected shadows (like the flashlight). {{Tip|This limitation can be circumvented by using $alphatestreference 1, and modifying the <code>[[$alpha]]</code> value instead. (requires $alpha to be raised above 1, its normal threshold, this may cause sorting issues)}} {{Tip|For [[VertexLitGeneric|VertexLitGeneric]] on models you can use <code>[[$detail]]</code> with <code>$detailblendmode 3</code> and modify the <code>$detailblendfactor</code> value. Detail texture must be a copy of <code>[[$basetexture]]</code> with a different alpha channel.}}
}}
}} }}


{{MatParam|$alphatestreference|float|Specifies the minimum color value of the alpha channel in which the effect is rounded to 255. A value of ".3" will create a thicker shape while a value of ".7" will create a thinner shape.
{{MatParamDef|$allowalphatocoverage|nolink=1|bool|Creates translucent steps based on alpha values. Can be used for softer edges, as well as greatly reducing shimmering when viewed at a distance.<br>While it may not immediately appear to be the case, <code>$alphatestreference</code>is still used when this command is used, for controlling projected shadow masking, for if the client has MSAA disabled, and in Hammer (where $allowalphatocoverage doesn't work).
: {{bug|<code>$alphatestreference</code> cannot be updated in real time with [[Material_proxies|proxies]].}}}}
{{Expand|title=Notes|
{{MatParam|$allowalphatocoverage|bool|Creates transparent steps based on alpha values. Can be used for softer edges. This parameter is incompatible with <code>$alphatestreference</code>.
{{warning|[[Anti-aliasing#MSAA|MSAA]] must be enabled in order for this to have an effect.<br>The granularity of the steps is dependent on chosen amount of MSAA; 2x MSAA will have fewer translucent steps than 8x MSAA. {{note|At 8x MSAA (Tested in {{src13|2}}) there are 7 translucency steps. ( 9 if you include fully Opaque and fully Transparent )<br>
: {{note|MSAA must be enabled in order for this to have effect.}}}}
The levels should roughly be arranged as such:<br>
 
<code>Linear : fully transparent - 0.06 - 0.18 - 0.31 - 0.43 - 0.56 - 0.68 - 0.81 - 0.93 - fully opaque</code><br>
==Comparison with $translucent==
<code>RGB : fully transparent - 15 - 46 - 80 - 110 - 143 - 174 - 207 - 238 - fully opaque</code><br>
( 0-255 values are rounded to the next highest. Formula used is <code>LinearValue * 255)</code>
{{todo|Make a table for this. One that considers other MSAA Levels. There are always 1 fewer translucency steps than the number of MSAA samples (or one additional, if including fully transparent and fully opaque).}} }} }}
{{tip|Alpha to coverage works best with '''sharp, high-resolution alpha channels''', as this mitigates the inherent banding artefacts caused by the stock shaders' lack of sharpening when magnifying alpha to coverage textures.}}
}}
}}


==Comparison==
<div>
{|class=wikitable
{|class=wikitable
! $translucent||$alphatest
! <code>$translucent</code>||<code>$alphatest</code>
|-
|-
| [[File:Alphatest_preview_original.png|thumb|512px|Original image, rendered with $translucent]] || [[File:Alphatest_preview.gif|thumb|512px|Animated preview of the same image, but with $alphatest. The first image shows the effect of $allowalphatocoverage 1. The rest show alphatest with $alphatestreference ranging from 0.1 to 0.9]]
| [[File:Alphatest_preview_original.png|thumb|50%|center|Original image, rendered with $translucent]] || [[File:Alphatest_preview.gif|thumb|50%|center|Animated preview of the same image, but with $alphatest. The first image shows the effect of $allowalphatocoverage 1 (at 4x MSAA). The rest show $alphatest with $alphatestreference ranging from 0.1 to 0.9. Click image if not animating.]]
|}
|}
Notice how the right circle hardly changes.
</div>
 
Notice how the right circle, which has a sharp alpha channel, hardly changes.
== See also ==


*<code>[[$alpha]]</code>, for texture-wide translucency
==See also==
*<code>[[$distancealpha]]</code>, for vector-like alpha edges
*<code>[[$alpha]]</code>
*<code>[[$translucent]]</code>
*<code>[[$distancealpha]]</code>
*<code>[[$vertexalpha]]</code>


[[Category:List of Shader Parameters|T]]
[[Category:Shader parameters|alphatest]]

Latest revision as of 11:40, 23 August 2025

English (en)Español (es)Русский (ru)中文 (zh)Translate (Translate)

$alphatest is a material shader parameter available in all Source Source games. It specifies a mask to use to determine binary opacity. White represents fully opaque, while black represents fully transparent. Any values in-between are rounded to either 0 or 1, unless $allowalphatocoverage is also used. This effect is similar to $translucent, except that it can not be semi-opaque (without Wikipedia icon banding), is much faster to render, and the engine can always sort it properly when layered (instead of only on non-detail worldspawn).

Syntax

$alphatest <bool>

VMT example

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
LightmappedGeneric { $basetexture "glass\window001a" $alphatest 1 $alphatestreference 0.5 $allowalphatocoverage 1 }

Additional parameters

$alphatestreference <float>
Specifies the threshold alpha channel value at which the surface should be transparent instead of opaque. A value of ".3" will create a thicker shape while a value of ".7" will create a thinner shape.
Notes
Icon-Important.pngImportant:Unintuitively, this parameter does not default to "0.5". LightmappedGeneric, UnlitGeneric, and VertexLitGeneric all default to "0.7" instead. To avoid unexpected results, this parameter should always be set.
Icon-Bug.pngBug:Updating $alphatestreference in real time with proxies only affects projected shadows (like the flashlight).
Tip.pngTip:This limitation can be circumvented by using $alphatestreference 1, and modifying the $alpha value instead. (requires $alpha to be raised above 1, its normal threshold, this may cause sorting issues)
Tip.pngTip:For VertexLitGeneric on models you can use $detail with $detailblendmode 3 and modify the $detailblendfactor value. Detail texture must be a copy of $basetexture with a different alpha channel.
  [todo tested in ?]
$allowalphatocoverage <boolean>
Creates translucent steps based on alpha values. Can be used for softer edges, as well as greatly reducing shimmering when viewed at a distance.
While it may not immediately appear to be the case, $alphatestreferenceis still used when this command is used, for controlling projected shadow masking, for if the client has MSAA disabled, and in Hammer (where $allowalphatocoverage doesn't work).
Notes
Warning.pngWarning:MSAA must be enabled in order for this to have an effect.
The granularity of the steps is dependent on chosen amount of MSAA; 2x MSAA will have fewer translucent steps than 8x MSAA.
Note.pngNote:At 8x MSAA (Tested in Source 2013 Source 2013) there are 7 translucency steps. ( 9 if you include fully Opaque and fully Transparent )

The levels should roughly be arranged as such:
Linear : fully transparent - 0.06 - 0.18 - 0.31 - 0.43 - 0.56 - 0.68 - 0.81 - 0.93 - fully opaque
RGB : fully transparent - 15 - 46 - 80 - 110 - 143 - 174 - 207 - 238 - fully opaque
( 0-255 values are rounded to the next highest. Formula used is LinearValue * 255)

Todo: Make a table for this. One that considers other MSAA Levels. There are always 1 fewer translucency steps than the number of MSAA samples (or one additional, if including fully transparent and fully opaque).
Tip.pngTip:Alpha to coverage works best with sharp, high-resolution alpha channels, as this mitigates the inherent banding artefacts caused by the stock shaders' lack of sharpening when magnifying alpha to coverage textures.

Comparison

$translucent $alphatest
Original image, rendered with $translucent
Animated preview of the same image, but with $alphatest. The first image shows the effect of $allowalphatocoverage 1 (at 4x MSAA). The rest show $alphatest with $alphatestreference ranging from 0.1 to 0.9. Click image if not animating.

Notice how the right circle, which has a sharp alpha channel, hardly changes.

See also