Transparency and Drawing Order

Translucent mesh rendering is problematic in TGE.  Several methods are outlined below for correcting problems with translucent meshes in TGE.  Unfortunately, it is very difficult to solve "rendering order" problems for complex translucent meshes.  Overlapping or intersecting polygons should be avoided.

TGEA Note: Most of the techniques below do not apply to TGEA, which has more sophisticated handling of translucent meshes.  See the TGEA section at the end of this page for more information.

The Mesh "Sort" parameter - not supported by the Blender DTS Exporter

The DTS file format has a "cluster mesh" BSP mesh sorting system which is not supported by the Blender exporter, but in the words of Tom Spilman (in regards to the forest pack):

     "We don't ever sort meshes. Once you start sorting meshes performance goes down the crapper. Alpha test is your friend. ;)"

Joe Maruschak also writes about the bsp mesh sorting here:

     "The SORT:: parameter was designed to allow low polygon transparent meshes to self sort correctly.
     It was meant to be used on really low polygon shapes (like boxes and such) and was never intended to
     be used on high polycount objects.
     The SORT:: parameter is not a panacea for all sorting issues.
     ... If it is a higher polycount object, the sorting algorithm (which actually splits
     the polygons of the shape) can easily create a VERY large number of polygons."

It is unlikely that the Blender DTS exporter will ever support this feature due to the large amount of work that would be involved in implementing it and the drawbacks of using it.

Using hierarchical linking to control drawing order for translucent meshes

The drawing order of translucent meshes can be controlled manually as described in this TDN article: tdn.garagegames.com/wiki/DTS/3dsmax/Z_Sorting_Explained (in the "Hierarchical Linking" section).

The drawing order of translucent meshes can be controlled in Blender through the use of hierarchical linking.  This is accomplished by defining a parent/child relationship between meshes.  In TGE, meshes are rendered in the order in which they are written out to a dts file.  When the Blender DTS exporter examines your scene, meshes that are at the root of the hierarchy (meshes that have no parent object) are written out first; followed by their children, on down the chain.  Objects that are on the same level of the hierarchy are sorted in alphabetical order before export.  See Blender Survival Guide - parenting one object to another, for instructions on establishing a parent/child relationship between mesh objects.

It is sometimes difficult to understand how hierarchical linking should be used to correct translucent mesh rendering problems.  I will attempt to explain how and why this is done using the following scenarios.

Scenario 1 - Two double sided planes and two observers

Two double sided planes with translucent textures are placed a small distance apart from each other, and are each textured with a translucent dts material.  Because polygons are drawn on the screen one-at-a-time or in per-mesh (or per-material) batches in OpenGL and DirectX, one of these planes will draw first, and the other second.  This gives us two cases to consider, with two different results.

 

Case 1 - Plane 1 draws first, Plane 2 draws second

Case 2 - Plane 2 draws first, Plane 1 draws second

What observer 1 sees

Incorrect, plane 2 which is further away draws on top of plane 1

Correct, plane 1 which is closer draws on top of plane 2

What observer 2 sees

Correct, plane 2 which is closer draws on top of plane 1

 

Incorrect, plane 1 which is further away draws on top of plane 2

No matter which order the two double-sided translucent planes are rendered in, the drawing order will always be incorrect when viewed from one side and correct when viewed from the other.  Unless these two planes can only be viewed from one side in the game engine, this presents a problem.  Read on for a solution. 

Scenario 2 - Two single sided planes and two observers

A note on double sided meshes/polygons: Single sided polygons are, by definition, visible from one side only.  On the graphics hardware level, there is no such thing as a double sided polygon; all polygons are inherently single sided.  The rendering of double sided polygons on the screen is accomplished by rendering a copy of each "front facing" polygon in the same location, but with its face normal reversed.  In effect, each polygon is rendered twice; once in the normal way (front face), and then again facing in the opposite direction (back face).  See Double Sided Meshes for more information.

In the following scenario, two single sided planes are placed a short distance apart.  Two cases are presented.

Case 1

In this case, both of the single sided planes are facing towards observer 1.  The drawing order of the planes is controlled through hierarchical linking so that plane 2 draws first, followed by plane 1.  This is correct when viewed by observer 1, but the two planes are completely invisible when viewed by observer 2.

Case 2

In this case, both of the single sided planes are facing towards observer 2.  The drawing order of the planes is controlled through hierarchical linking so that plane 1 draws first, followed by plane 2.  This is correct when viewed by observer 2, but the two planes are completely invisible when viewed by observer 1 (the opposite of case 1).

Solution - combining the two cases

The solution is to combine the two cases.  By using four single sided planes, we can create an object that is correctly rendered from both sides.  Two of the four single-sided planes are facing towards observer 1's side, and are set up to draw in the correct order when viewed from that side.  The other two planes are facing towards observer 2's side and are set up to draw in the correct order when viewed from observer 2's position.  Since each of the two cases presented is invisible when viewed from the opposite side, they do not interfere with each other like the double sided planes in scenario 1.

 

 

Case 1 and Case 2 combined

What Observer 1 sees

Correct, Observer 1 sees single-sided Planes 1a and 2a, single-sided planes 1b and 2b are invisible since they are facing in the opposite direction.

What Observer 2 sees

Correct, Observer 2 sees single-sided Planes 1b and 2b, single-sided planes 1a and 2a are invisible since they are facing in the opposite direction.

Download the "two planes" translucent rendering example model: TransPlanesExample.zip


Another way of looking at things - Convex hulls and partial convex hulls

Convex meshes have some special properties that allow translucent mesh rendering to be simplified.  For an explanation of what makes a mesh convex, see: What is the difference between Concave and Convex? (rustycode.com) by Matt Fairfax.

A convex single-sided mesh can never overlap itself when rendering.  This means that a polygon on a single-sided convex mesh will never render on top of another polygon in that same mesh.

The following shapes are examples of convex meshes:

(note: there are many more convex shapes than the ones pictured above.  Any shape without "dents" or "caves" in it can be considered convex)

Correctly Rendering Translucent Convex meshes

As in the planes example above, correct rendering of translucent convex meshes is accomplished by using two single-sided versions of the same mesh: an "inside facing" version and an "outside facing" version.  The outside facing version is parented to the inside facing version of the mesh (see: Blender Survival Guide - Parenting one object to another), so that this outside mesh becomes a child of the inside mesh.  This causes the "inside facing" version of the mesh to render first, followed by the "outside facing version":

(left - inside facing version of the sphere, right - outside facing version)

The same technique can be applied to partial "chunks" of a convex mesh, or any mesh that describes a "piece" of a convex shape.  The "two planes" example presented above could also be thought of as a box with all but two sides removed.

Download the "Sphere" translucent rendering example model: TransSphereExample.zip

Translucent mesh rendering in TGEA

The techniques presented above do not apply to the Torque Game Engine Advanced, except in a few limited circumstances.  TGEA has a "render batching" system that allows for improved frame rates by "batching up" all polygons that use the same material and rendering them simultaneously.  TGEA ignores the internal ordering of meshes within a dts file, making the above techniques ineffective.  Instead, there is a "translucentZWrite" material option that should be used with translucent materials in TGEA which will cause them to sort correctly automatically.  An example of such a material definition is presented below:

  new Material(WhiteFence2)
  {
    // Rendering Stage 0
    baseTex[0] = "./WhiteFence2";
    translucent[0] = true;
    translucentZWrite = true;
  };

Note: translucentZWrite should only be enabled in TGEA if a shape is suffering from sorting problems, as it may affect performance.

See Also: