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

$translucent: Difference between revisions

From Valve Developer Community
Jump to navigation Jump to search
 
(49 intermediate revisions by 24 users not shown)
Line 1: Line 1:
{{toc-right}}
{{LanguageBar}}
<!-- {{split|[[$translucent]] and [[$additive]]}} -->
{{this is a|shader parameter|name=$translucent}}{{toc-right}}
It specifies that the material should be partially see-through. The [[alpha channel]] of the {{matparam|$basetexture}} is used to decide translucency per-pixel.


The <code>[[$translucent]]</code> [[VMT]] command specifies that the material should be partially see-through. The [[alpha channel]] of <code>[[$basetexture]]</code> is used to decide translucency per-pixel.
Any object that has a {{matparam|$translucent}} material does not affect [[VIS]], and can be seen through by NPCs from any angle. Visleaves '''do''' affect alpha sorting of translucent materials, however.


Any object that has a <code>$translucent</code> material does not affect [[VIS]], and can be seen through by NPCs from any angle.
{{Note|Enabling translucency will completely disable shadows made by [[env_projectedtexture|projected textures]] for the texture. Either separate the opaque and solid parts of the texture, or use {{matparam|$alphatest}} instead.}}


==VMT syntax example==
== VMT Syntax Example ==
 
  $translucent <[[Boolean|bool]]>
  $translucent <[[bool]]>


  [[LightmappedGeneric]]
  [[LightmappedGeneric]]
Line 14: Line 16:
  '''$translucent 1'''
  '''$translucent 1'''
  }
  }
==Additional parameters==
; <code>$additive <bool></code>
: Add the material's colour values to the existing image, instead of performing a multiplication. This means, among other things, that the material will always brighten the world. This is useful for effects like volumetric dust, light sprites, etc...


== Caveats ==
== Caveats ==
=== Flickering and Reversed Depth ===
Translucency can sometimes cause a material to flicker, or cause sorting issues with nearby surfaces. In both cases consider using {{matparam|$alphatest}} instead of {{matparam|$translucent}} when this happens. It drastically lowers quality, but will usually resolve the issue and is much faster to draw. It will also cast flashlight shadows, unlike translucents.


=== Flickering and reversed depth ===


Translucency can sometimes cause a material to flicker, or cause sorting issues with nearby surfaces. In both cases consider using '''<code>$alphatest</code>''' instead of <code>$translucent</code> when this happens. It drastically lowers quality, but will usually resolve the issue and is much faster to draw.  
[[File:Translucent2p.png]]




[[File:Translucent2p.png]]
Unlike {{matparam|$translucent}}, which allows for varying degrees of opacity, alpha testing does not—portions of your texture are either 'on' or 'off'. {{matparam|$alphatestreference}}, a normal parameter, controls the threshold of the transparency masking (Using lower values like .01 will result in a more blurred edge while a value of .99 will be extremely sharp.) Since alpha-testing is cheaper than {{matparam|$translucent}}, this can be used to achieve a similar effect to materials using the <code>$translucent</code> parameter at reduced cost. Using {{matparam|$allowalphatocoverage}} will enable antialiasing of alpha-tested textures, giving them much softer edges. When {{matparam|$allowAlphaToCoverage}} is enabled, {{matparam|$alphatestreference}} is ignored and will not affect the texture's appearance, though it will still affect projected texture stenciling (e.g. the flashlight).


{{note|The effect of {{matparam|$allowAlphaToCoverage}} will only be visible if the user has MSAA enabled.}}


Unlike <code>$translucent</code> which allows for varying degrees of opacity, alpha testing does not - portions of your texture are either 'on' or 'off'. <code>'''$alphatestreference'''</code>, a normal parameter, controls the 'fuzziness' of the transparency masking (Using lower values like .01 will result in a more blurred and fuzzy edge while a value of .99 will be extremely sharp.) Since alpha testing is cheaper than <code>$translucent</code>, this can be used to achieve a similar effect to materials using the <code>$translucent</code> parameter at reduced cost.
{{tip|Brush textures on non-detail worldspawn brushes use the [[BSP tree]] to improve alpha sorting, reducing the likelihood of this sort of error occurring. Avoid tying simple translucent brushes to {{ent|func_detail}} unless necessary; they will not affect VIS unless they are showing the opposite side of an [[areaportal]].}}
{{codenote|If programming your own shaders, apply alphatesting when doing alpha blending in order to prevent completely transparent texels from rendering in front of other surfaces and causing them to be obscured.}}


=== Compatibility with other effects ===
===Compatibility With Other Effects===
Certain shader parameters (such as <code>[[$envmap]]</code>) can disable translucency. If you are having trouble getting it to work, try commenting out other parts of the material.
Certain shader parameters{{inline note|name=Which ones?}} disable translucency. If you are having trouble getting it to work, try commenting out other parts of the material one by one to see which one causes the conflict.


=== Filtering borders ===
Known conflicts include:
* {{matparam|$selfillum}}, as it requires the alpha mask to determine the glow effect, and takes it "away" from transparency. Even using {{matparam|$selfillummask}} as a separate texture does not fix it.
* {{matparam|$blendtintbybasealpha}}, on models. It uses the alpha mask to determine the amount of tinting, again taking it "away".
* {{matparam|$alphatest}} and {{matparam|$alphatestreference}}, while technically possible to combine with {{matparam|$translucent}}, are incompatible on stock shaders.


[[Wikipedia:Texture filtering|Texture filtering]] will blend nearby pixels together even if some of of them are entirely transparent. This can create unwanted outlining effects if sudden drop-offs in alpha coincide with drop-offs in colour, which may well happen when the [[alpha channel]] reaches zero and you stop being able to see the pixels.
===Filtering Borders===
{{Wikipedia|Texture filtering|Texture filtering}} will blend nearby pixels together even if some of of them are entirely transparent. This can create unwanted outlining effects if sudden drop-offs in alpha coincide with drop-offs in colour, which may well happen when the [[alpha channel]] reaches zero and you stop being able to see the pixels.


[[Image:Decal anisotropic.jpg|center|An unwanted filtering border.]]
[[File:Decal anisotropic.jpg|center|An unwanted filtering border.]]


To resolve this issue, simply blend the colours of your image slightly beyond where the alpha channel drops off. This can be easily achieved by duplicating the translucent layer and smudging it into the relevant areas.
To resolve this issue, simply blend the colours of your image slightly beyond where the alpha channel drops off. This can be easily achieved by duplicating the translucent layer and smudging it into the relevant areas.


[[Image:Filterborder fixed.jpg|center|Fixing filtering borders.]]
[[File:Filterborder fixed.jpg|center|Fixing filtering borders.]]
 
{{note|This process is called '''dilation''', and also helps with proper generation of mipmaps.}}
('''Unfortunately for users of [[The GIMP]],''' the program will '''automatically remove colour information for pixels with an alpha of zero''' whenever layers are combined. The developers are aware of the the problems such behaviour creates [http://bugzilla.gnome.org/show_bug.cgi?id=411599#c4 but do not intend to change it].)
 
==Trouble Achieving Transparency In Materials==
 
Making a from-scratch material transparent seems to be tempermental. Some tips that may help:


* When making a Targa in Photoshop, compress as 32-bit, but without RLE selected.
('''For users of older versions of [[GIMP]],''' the program will '''automatically remove colour information for pixels with an alpha of zero''' whenever layers are combined. As a work-around for this, you could give those pixels an alpha value of one instead.
* Don't create layer style effects in Photoshop then save to Targa, even if you rastarize the layer. These may bring low-level pixel artefacts that interfere with transparency. Instead create your imagery by hand, or possibly exporting to/saving as a .png (from Illustrator or Photoshop) then reimporting to Photoshop and saving the .png as a fresh Targa.
* You may need to ensure that opaque or semi-transparent pixels are touching one or more of the actual edges of your material. If no opaque or semi-transparent pixels touches any edges, VTF Edit (or something in the guts of the system) may decide to give your material an opaque border.


== See also ==
As of GIMP 2.8.14, pixels with an alpha value of 0 retain their color information when saved as a tga.
{{tip|For ports from {{gldsrc|4}}:
[[xwad]] will automatically generate proper transparent pixels for any converted [[WAD]] textures that contain the {{key|{}} prefix, any converted [[SPR]] sprites flagged as {{mono|AlphaTest}}, and any converted loose [[BMP]] textures that were converted using the {{code|-transparent}} flag.}}


*<code>[[$alpha]]</code>, for texture-wide translucency
==See also==
*<code>[[$distancealpha]]</code>, for vector-like alpha edges
* {{matparam|$additive}}, for translucency via additive blending
*<code>[[$vertexalpha]]</code>
* {{matparam|$alpha}}, for texture-wide translucency
* {{matparam|$distancealpha}}, for vector-like alpha edges
* {{matparam|$vertexalpha}}
* {{matparam|$alphatest}}, for binary opacity.


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

Latest revision as of 08:36, 24 August 2025

English (en)Español (es)Français (fr)中文 (zh)Translate (Translate)

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

It specifies that the material should be partially see-through. The alpha channel of the $basetexture is used to decide translucency per-pixel.

Any object that has a $translucent material does not affect VIS, and can be seen through by NPCs from any angle. Visleaves do affect alpha sorting of translucent materials, however.

Note.pngNote:Enabling translucency will completely disable shadows made by projected textures for the texture. Either separate the opaque and solid parts of the texture, or use $alphatest instead.

VMT Syntax Example

$translucent <bool>
LightmappedGeneric
{
	$basetexture glass\window001a
	$translucent 1
}

Caveats

Flickering and Reversed Depth

Translucency can sometimes cause a material to flicker, or cause sorting issues with nearby surfaces. In both cases consider using $alphatest instead of $translucent when this happens. It drastically lowers quality, but will usually resolve the issue and is much faster to draw. It will also cast flashlight shadows, unlike translucents.


Translucent2p.png


Unlike $translucent, which allows for varying degrees of opacity, alpha testing does not—portions of your texture are either 'on' or 'off'. $alphatestreference, a normal parameter, controls the threshold of the transparency masking (Using lower values like .01 will result in a more blurred edge while a value of .99 will be extremely sharp.) Since alpha-testing is cheaper than $translucent, this can be used to achieve a similar effect to materials using the $translucent parameter at reduced cost. Using $allowalphatocoverage will enable antialiasing of alpha-tested textures, giving them much softer edges. When $allowAlphaToCoverage is enabled, $alphatestreference is ignored and will not affect the texture's appearance, though it will still affect projected texture stenciling (e.g. the flashlight).

Note.pngNote:The effect of $allowAlphaToCoverage will only be visible if the user has MSAA enabled.
Tip.pngTip:Brush textures on non-detail worldspawn brushes use the BSP tree to improve alpha sorting, reducing the likelihood of this sort of error occurring. Avoid tying simple translucent brushes to func_detail unless necessary; they will not affect VIS unless they are showing the opposite side of an areaportal.
Cpp.pngCode:If programming your own shaders, apply alphatesting when doing alpha blending in order to prevent completely transparent texels from rendering in front of other surfaces and causing them to be obscured.

Compatibility With Other Effects

Certain shader parameters[Which ones?] disable translucency. If you are having trouble getting it to work, try commenting out other parts of the material one by one to see which one causes the conflict.

Known conflicts include:

Filtering Borders

Wikipedia icon Texture filtering will blend nearby pixels together even if some of of them are entirely transparent. This can create unwanted outlining effects if sudden drop-offs in alpha coincide with drop-offs in colour, which may well happen when the alpha channel reaches zero and you stop being able to see the pixels.

An unwanted filtering border.

To resolve this issue, simply blend the colours of your image slightly beyond where the alpha channel drops off. This can be easily achieved by duplicating the translucent layer and smudging it into the relevant areas.

Fixing filtering borders.
Note.pngNote:This process is called dilation, and also helps with proper generation of mipmaps.

(For users of older versions of GIMP, the program will automatically remove colour information for pixels with an alpha of zero whenever layers are combined. As a work-around for this, you could give those pixels an alpha value of one instead.

As of GIMP 2.8.14, pixels with an alpha value of 0 retain their color information when saved as a tga.

Tip.pngTip:For ports from GoldSrc GoldSrc: xwad will automatically generate proper transparent pixels for any converted WAD textures that contain the { prefix, any converted SPR sprites flagged as AlphaTest, and any converted loose BMP textures that were converted using the -transparent flag.

See also