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

$distancealpha: Difference between revisions

From Valve Developer Community
Jump to navigation Jump to search
mNo edit summary
No edit summary
 
(29 intermediate revisions by 17 users not shown)
Line 1: Line 1:
{{stub}}
{{LanguageBar|title={{mono|$distancealpha}}}}
{{toc-right}}


Another new OB feature: distance alpha. [http://www.valvesoftware.com/publications/2007/SIGGRAPH2007_AlphaTestedMagnification.pdf See this PDF] for a high-level explanation of what all this stuff does. The article doesn't mention it's most useful application, oddly: foliage.
{{This is a|name=$distancealpha|shader parameter|since=Source 2007}} It is a [[cheap]] edge filtering technique for raster images. It takes advantage of 3D hardware's well-established texture filtering methods by defining a series of [[alpha channel]] thresholds above, below, or between which effects can be applied. This feature was added to the {{source|2}} engine with the release of {{src07|4}}. It's great for UI elements, foliage, chain link fences, grates, and more.
{{bug|hidetested=1|This shader reads the image in [[sRGB]] space rather than linear space by default, which throws off the calculations and leads to wavy-like edges and wrong opacity. To fix this, always use <code>$gammacolorread 1</code> with this technique (only available on [[UnlitGeneric]]).}}
{{bug|hidetested=1|In {{src13}}{{tf2}} On {{ent|VertexLitGeneric}} <code>$distancealpha</code> does not work with any of : {{ent|$envmap}}, {{ent|$bumpmap}}, {{ent|$selfillum}}, {{ent|$selfillum_envmapmask_alpha}}, {{ent|$seamless_detail}}, {{ent|$blendtintbybasealpha}}}}
{{warning|{{csgo|only}} <code>$distancealpha</code> and parameters that come with it, don't appear to exist for {{ent|VertexLitGeneric}} and {{ent|LightmappedGeneric}} Shaders.}}


;Advantages
;Advantages
:Takes advantage of 3D texture filtering technology to be as fast (or almost as fast as) vector graphics, without the need for Valve to create an entirely new vector rendering path prone to bugs
:Vector-like edges on raster images even during extreme zoom.
:Can be used on raster images - faster than adaptive AA.
:Far faster than adaptive AA.
;Disadvantages
;Disadvantages:Effects look extremely pixelated from grazing angles in some situations (might be related to [[brush]] application?).
:Distance alpha effects look extremely pixellated from grazing angles in some situations (might be related to [[brush]] application).


{{bug|Distance alpha materials are not rendered correctly in Hammer's 3D view.}}
[https://steamcdn-a.akamaihd.net/apps/valve/2007/SIGGRAPH2007_AlphaTestedMagnification.pdf For (lots) more detail, see this SIGGRAPH paper.]


== Texture creation ==
{{bug|hidetested=1|Distance alpha materials are not rendered correctly in Hammer's 3D view.}}


Distance alpha source textures are created at a very high resolution. Valve use 4096x4096 images, but the SDK's build of [[Vtex|VTEX]] will crash if you pass it anything over 2048x2048. The image should otherwise be completely normal, except that the alpha channel should avoid gradients (as they will be created by the shader and during the texture's compile).
== Texture Creation ==
Distance alpha source textures are created at a very high resolution. Valve use 4096x4096 images, but the SDK's build of [[Vtex (Source 1)|VTEX]] will crash if you pass it anything over 2048x2048.
{{tip|{{tf2|2}}: Patched VTEX can be [https://cdn.discordapp.com/attachments/596042830259027990/1136772785021140992/vtex_dll.dll downloaded here] that supports 4096x4096 TGA textures.
{{todo|Host elsewhere; Discord download links require pasting into a new discord post to generate a new access token.}} }}


To compile the texture you must use [[Vtex|VTEX]]; third party tools don't support distance alpha yet. Your .txt file should look something like this:
The image should otherwise be completely normal, except that the alpha channel should avoid gradients (you'll be able to re-construct them with the shader).
 
To compile the texture, you must use [[Vtex (Source 1)|VTEX]]; third-party tools (including {{vtfedit|1}} and {{vtfeditreloaded|1}}) don't support distance alpha yet.
 
Your <tt>.txt</tt> file should look something like this:


  ''// Enable distance alpha compiling''
  ''// Enable distance alpha compiling''
Line 32: Line 42:
  nocompress 1
  nocompress 1


== Material creation (class_spy_outline) ==
== Material Creation (<tt>class_spy_outline</tt>) ==
 
[[File:Class spy outline.png|frame|Alpha channel of {{ent|$basetexture}} (100% zoom).]]
[[Image:Class spy outline.png|frame|Alpha channel of [[$basetexture]] (100% zoom).]]
[[File:Class spy outline ingame.jpg|thumb|256px|The material in-game (50% zoom).]]
[[Image:Class spy outline ingame.jpg|thumb|256px|The material in-game (50% zoom).]]
 
This material is displayed by [[Team Fortress 2]] when a spy player gains or loses a disguise. Despite the fact that it fills the entire screen at any resolution, its texture (right) is only 256 pixels square.


It can scale to high resolutions thanks to the Valve's new method of alpha testing, wherein threshold alpha values are used to construct smooth lines on the video card regardless of resolution.
This material is displayed in {{tf2|2}} when a Spy player gains or loses a disguise. Despite the fact that it fills the entire screen at any resolution, its texture (seen right) is only 256 pixels square.


  [[UnlitGeneric]]
  [[UnlitGeneric]]
Line 66: Line 73:
  }
  }


{{todo|$outline}}
The most interesting commands here are the <tt>start</tt>s and <tt>end</tt>s—their values determine the range of alpha values across which each effect is applied.
 
*<code>$softedges</code> is applied from 50% to 46% opacity
*<code>$glow</code> is applied from 10% to 50% opacity
 
By setting <code>$glowcolor</code> to black and <code>$glowalpha</code> to 40%, the glow effect is made to look like a shadow.
 
== Effects ==
===<tt>$softedges</tt>===
Fades out all color channels from <code>$edgesoftnessstart</code> (opaque edge) to <code>$edgesoftnessend</code> (transparent edge).
{{bug|hidetested=1|In {{src13}}{{tf2}} On {{ent|LightmappedGeneric}} and {{ent|WorldVertexTransition}} <code>$softedges</code> does not work with any of : {{ent|$seamless_scale}}, {{ent|$selfillum}}, {{ent|$lightwarptexture}}, {{ent|$bumpmap}}, {{ent|$bumpmap2}}, {{ent|$basetexture2}}, {{ent|$blendmodulatetexture}}}}
 
;<code>$scaleedgesoftnessbasedonscreenres <[[bool]]></code>
:{{todo|Describe}}
 
===<tt>$glow</tt>===
Creates a monotone gradient from <code>$glowstart</code> (opaque edge) to <code>$glowend</code> (transparent edge).
 
; <code>$glowcolor <RGB></code>
: Color of glow.
; <code>$glowalpha <[[normal]]></code>
: Opacity of glow.
; <code>$glowx <normal></code>
; <code>$glowy <normal></code>
: Offsets glow; good for shadow effects.
; <code>$glowstart <normal></code>
; <code>$glowend <normal></code>
: Alpha channel thresholds. <code>$glowstart</code> is full intensity, <code>$glowend</code> is where it becomes invisible.
 
===<tt>$outline</tt>===
The same as glow, but with the added option of defining how sharp each edge appears.
{{bug|hidetested=1|In {{src13}}{{tf2}} On {{ent|LightmappedGeneric}} and {{ent|WorldVertexTransition}} <code>$outline</code> does not work with any of : {{ent|$seamless_scale}}, {{ent|$selfillum}}, {{ent|$lightwarptexture}}, {{ent|$bumpmap}}, {{ent|$bumpmap2}}, {{ent|$basetexture2}}, {{ent|$blendmodulatetexture}}}}


Note in the above material how <code>$edgesoftnessstart</code> has the same value as <code>$glowend</code>. The glow effect is used for parts of the material above 50% opacity, while soft edges are created for parts below.
; <code>$outlinecolor <RGB></code>
: Color of outline.
; <code>$outlinealpha <[[normal]]></code>
: Opacity of outline.
; <code>$outlinestart0 <normal></code>
; <code>$outlinestart1 <normal></code>
: Defines the outer (i.e., low alpha) edge of the outline. Any gap between the two values will become a gradient.
: 0 is "outer outer," while 1 is "inner outer."
; <code>$outlineend0 <normal></code>
; <code>$outlineend1 <normal></code>
: Defines the inner (i.e., high alpha) edge of the outline.
: 0 is the "outer inner," while 1 is the "inner inner." {{note|The two values must differ by at least 0.02 or the entire outline will disappear!}}
; <code>$scaleoutlinesoftnessbasedonscreenres 1</code>
: Scales the outline's softness based on the user's screen resolution.


[[Category:List of Shader Parameters|D]]
[[Category:Shader parameters|distancealpha]]

Latest revision as of 17:36, 15 July 2025

English (en)Español (es)Translate (Translate)

$distancealpha is a material shader parameter available in all Source Source games since Source 2007 Source 2007. It is a cheap edge filtering technique for raster images. It takes advantage of 3D hardware's well-established texture filtering methods by defining a series of alpha channel thresholds above, below, or between which effects can be applied. This feature was added to the Source Source engine with the release of Source 2007 Source 2007. It's great for UI elements, foliage, chain link fences, grates, and more.

Icon-Bug.pngBug:This shader reads the image in sRGB space rather than linear space by default, which throws off the calculations and leads to wavy-like edges and wrong opacity. To fix this, always use $gammacolorread 1 with this technique (only available on UnlitGeneric).
Warning.pngWarning:(only in Counter-Strike: Global Offensive) $distancealpha and parameters that come with it, don't appear to exist for VertexLitGeneric and LightmappedGeneric Shaders.
Advantages
Vector-like edges on raster images even during extreme zoom.
Far faster than adaptive AA.
Disadvantages
Effects look extremely pixelated from grazing angles in some situations (might be related to brush application?).

For (lots) more detail, see this SIGGRAPH paper.

Icon-Bug.pngBug:Distance alpha materials are not rendered correctly in Hammer's 3D view.

Texture Creation

Distance alpha source textures are created at a very high resolution. Valve use 4096x4096 images, but the SDK's build of VTEX will crash if you pass it anything over 2048x2048.

Tip.pngTip:Team Fortress 2 Team Fortress 2: Patched VTEX can be downloaded here that supports 4096x4096 TGA textures.
Todo: Host elsewhere; Discord download links require pasting into a new discord post to generate a new access token.

The image should otherwise be completely normal, except that the alpha channel should avoid gradients (you'll be able to re-construct them with the shader).

To compile the texture, you must use VTEX; third-party tools (including VTFEdit and VTFEdit Reloaded) don't support distance alpha yet.

Your .txt file should look something like this:

// Enable distance alpha compiling
alphatodistance 1

// Scale the image down to 1/nth its original size. '8' takes 2048 down to 256
reduce 8

// Size of 'spread' zone between alpha values of 0 and 1. This is the area the shader works with.
// Bear in mind that higher numbers exponentially increase texture compile time!
distancespread 4

// To prevent artefacting:
nomip 1
nolod 1
nocompress 1

Material Creation (class_spy_outline)

Alpha channel of $basetexture (100% zoom).
The material in-game (50% zoom).

This material is displayed in Team Fortress 2 Team Fortress 2 when a Spy player gains or loses a disguise. Despite the fact that it fills the entire screen at any resolution, its texture (seen right) is only 256 pixels square.

UnlitGeneric
{
	$basetexture	hud\class_spy_outline
	$translucent	1
	$distancealpha	1
	
	$softedges		1
	$edgesoftnessstart	.5
	$edgesoftnessend	.46
	$scaleedgesoftnessbasedonscreenres 1
	
	$glow		1
	$glowcolor	"[0 0 0]"
	$glowalpha	.4
	$glowstart	.1
	$glowend	.5
	//$glowx	.98
	//$glowy	.98

	$vertexcolor	1
	$vertexalpha	1
	$no_fullbright	1
	$ignorez	1
}

The most interesting commands here are the starts and ends—their values determine the range of alpha values across which each effect is applied.

  • $softedges is applied from 50% to 46% opacity
  • $glow is applied from 10% to 50% opacity

By setting $glowcolor to black and $glowalpha to 40%, the glow effect is made to look like a shadow.

Effects

$softedges

Fades out all color channels from $edgesoftnessstart (opaque edge) to $edgesoftnessend (transparent edge).

$scaleedgesoftnessbasedonscreenres <bool>
Todo: Describe

$glow

Creates a monotone gradient from $glowstart (opaque edge) to $glowend (transparent edge).

$glowcolor <RGB>
Color of glow.
$glowalpha <normal>
Opacity of glow.
$glowx <normal>
$glowy <normal>
Offsets glow; good for shadow effects.
$glowstart <normal>
$glowend <normal>
Alpha channel thresholds. $glowstart is full intensity, $glowend is where it becomes invisible.

$outline

The same as glow, but with the added option of defining how sharp each edge appears.

$outlinecolor <RGB>
Color of outline.
$outlinealpha <normal>
Opacity of outline.
$outlinestart0 <normal>
$outlinestart1 <normal>
Defines the outer (i.e., low alpha) edge of the outline. Any gap between the two values will become a gradient.
0 is "outer outer," while 1 is "inner outer."
$outlineend0 <normal>
$outlineend1 <normal>
Defines the inner (i.e., high alpha) edge of the outline.
0 is the "outer inner," while 1 is the "inner inner."
Note.pngNote:The two values must differ by at least 0.02 or the entire outline will disappear!
$scaleoutlinesoftnessbasedonscreenres 1
Scales the outline's softness based on the user's screen resolution.