8. Clipping, ZEffect, AlphaTest - Tutorial

Knowledge of these techniques will give you ability to create some complex effects based on per-pixel masks which are impossible to implement with sprite drawing only.



Clipping is simplest technique of limiting graphics output. Clipping is possible only for rectangular area of screen without any transformation (means on top level). It is just like viewport. In example we clip dancing drops around mouse cursor.

using (canvas <= CreateClipping()) {
    ... all drawing inside will be clipped

//Creates clipping with center at mouse point or no clipping if clipping is not enabled
private Clipping CreateClipping () {
    return _ClippingEnabled ? new Clipping(Rect.FromPoint(Mouse.X, Mouse.Y, 400, 300)) : Clipping.None;


Z-Effect based on depth-stencil buffer which is inalienable part of any 3D libarary like DirectX or OpenGL. Z-Buffer contains one numerical value per each pixel. We can specify which value to put with drawing or draw only pixels for which z-value is passing some comparison test. In Vortex2D z-value is represented as float in range 0..1. In other words ZEffect encapsulates 4 main states of z-buffer usage: no usage, test only, test and write, write only. All of them are available with static methods: ZEffect.None, ZEffect.PassIf, ZEffect.Write, ZEffect.WriteIf.

First, before writing something we need to clear z-buffer with some value. It can be done with Canvas2D.Clear method:


After z-buffer is clear we need to prepare mask - set some z-value for pixels. Step 1 - we need to disable color writing of mask, otherwise mask geometry will be visible, step 2 - use ZEffect with writing only of some z-value.

//mask pixel should not-be written into color buffer
using (canvas <= Blending.Phantom) {
    //requested to write z-value without any tests
    using (canvas <= ZEffect.Write(DEMO_ZMASK_VALUE)) {
        ... all drawing inside will populate DEMO_ZMASK_VALUE in z-buffer
After mask is prepared we need to enable testing z-values for another geometry which will be per-pixel clipped:

//if z-mask is enabled lets show only pixels with z-value DEMO_ZMASK_VALUE
using (canvas <= (_ZMaskEnabled ? ZEffect.PassIf(DEMO_ZMASK_VALUE, CompareFunction.Equal) : ZEffect.None)) {
    //draw content, per-pixel clipped

Alpha Test

All geometry will write z-value for each rendered pixel including fully transparent pixels. To use only non-transparent pixels for writing z-values we need to enable alpha test, rejecting transparent pixels from processing.

//we need to write only pixels with alpha > 50%
using (canvas <= AlphaTest.GreaterThan(0.5f)) {
    canvas.DrawSprite(Window.Width / 2, Window.Height / 2, _StencilTexture.ToSprite(), ColorU.White);


Clipping is very basic method to limit drawing output. ZEffect in couple with AphaTest are powerful tool to create per-pixel masks for any kind of graphics output.

Last edited Aug 24, 2010 at 2:34 PM by AlexKhomich, version 8


No comments yet.