The Alpha Channel
Translucency and other effects such as specular make use of the alpha channel of the textures in the Material. In order to use this channel you must use .png or some other supported 32 bit file format for the texture.
Activation
To turn on Material translucency, set the translucency parameter to be true:
new Material( TransFoo ) { baseTex[0] = "foo"; translucent = true; };
Blend Operations
There are several possible blend operations that can be used with translucency. The default is "LerpAlpha". Here's a brief summary of blend modes:
- Add - adds the color of the material to the frame buffer with full alpha for each pixel
- AddAlpha - same as add, but the color is modulated by the alpha channel before being added to the frame buffer
- Mul - modulates the Material with the frame buffer contents
- LerpAlpha - Linearly interpolates between Material color and frame buffer color based on alpha
Use "translucentBlendOp to set the blend operation. Example:
new Material( TransFoo ) { baseTex[0] = "foo"; translucent = true; BlendOp = AddAlpha; };
To add more blend modes and see how they are implemented with the GFX interface, take a look at Material::setBlendState() in material.cpp.
Using cubemaps with translucency
Cubemaps are tricky to use with translucency because the intensity of the cubemap is modulated by the alpha channel of the baseTex. This means that as a Material gets more translucent, the cubemap reflection will be more faded. If this is not the desired effect, it is recommended to switch to a CustomMaterial and write a shader to support the custom translucent / cubemap interaction.
Alpha test
Alpha test is on by default in rendering translucent Materials. It can be toggled with the "alphaTest" parameter. Alpha testing can improve performance by discarding pixels that are under a certain intensity threshold. By discarding the pixels, the fillrate is improved because the GPU doesn't need to perform an expensive blend operation. The intensity threshold can be set via the "alphaRef" parameter. If the alpha value coming out of the shader is below this value, the pixel will not be rendered. AlphaRef is set to 1 by default, so only pixels that have an alpha value of 0 (and therefore will be invisible anyway) will be discarded. For more information on alpha testing, read DirectX SDK or OpenGL docs.
Example:
new Material( AlphaFoo ) { baseTex[0] = "foo"; translucent = true; translucentBlendOp = LerpAlpha; alphaTest = true; // default value alphaRef = 128; // alpha less than 128 in brightness (255 is max) will not be rendered };
Translucent Z writes
There are circumstances such as rendering fences, grating, or foliage, in which writing to the zbuffer is desired for a translucent Material. Usually this is done in combination with alphaTest and a LerpAlpha blendOp. The parts of the Material that look opaque have a high alpha value (255) and will write to the zbuffer, and the parts that are not opaque are usually invisible with an alpha of 0 and do not write to the zbuffer. To turn on z buffer writes for translucent Materials, set the parameter "translucentZWrite" to be true.
example:
new Material( FoliageFoo ) { baseTex[0] = "leaves"; translucent = true; translucentBlendOp = LerpAlpha; translucentZWrite = true; alphaRef = 20; // anything below this number is not visible and is not written to zbuffer };
(TODO: does this still Work this way?)