Tutorials

Sections

j3d.org

Java 3D Volume Rendering

By David Yazel of the Cosm MMPROG project.

One area that is constantly the subject of questions and also criticsm is the Java3D capability to handle transparency (also known as alpha blending). Some of this is due to the lack of good documentation and some is attributable to the specification itself. Until the latest 1.3 beta release, properly rendering multiple overlapping transparent objects was close to impossible to do efficiently. In this short tutorial we introduce you to the basics of adding transparency to your objects.

 

Types of transparency controls for objects

There are 3 seperate and combinable controls for transparency:

1. At the Shape3D level you can attach transparency attributes. This lets you control the blending method for the pixels. You can use all the combinations of a source blend function and destination blend function to get different results. You can also set up a threshhold to force the blend into a boolean operation (on or off) which is very helpful for distant imposters.

2. At the triangle level you can set the alpha of every single vertex. This is modified by transparency attributes as an override, so it is possible to have an opaque alpha on a vertex turned into a 50 percent alpha by setting the transparency attributes to 50 percent. The vertex alphas are interpolated between vertices just like all the other colors, so you can easily have nice fades and blends between vertices.

3. Textures can also contain alphas. When modulated with vertex colors you can effectivly merge the texture alpha with the vertex alpha to create a new alpha for every pixel. The blend functions then determine how to blend the pixel with the screen.

 

Calculating Transparency

As you have seen, there are a variety of ways that transparency information can be provided to your objects. In order to work out the final result you can look at the calculations being performed by Java3D in a set of stages.

Stage 1

Calculate the pixel color. This is done using the vertex colors blended with the texture (if using the TextureAttributes.setTextureMode(MODULATE)). The resultant value can have an alpha value. This is the standard method that combines interpolated texels with interpolated vertex colors, materials and lights to produce a final pixel color.

Stage 2

Adjust the alpha component of the pixel. This is done through the TransparencyAttributes.setTransparency() method. This value is modulated against the alpha. This is multiplicative, so if the pixel alpha is already at 50 percent and the transparency value is at 50 percent then the final alpha will be 25 percent. For fine control you generally you set the transparency to fully opaque and use the vertex/texture alphas to control the amount of transparency. If you want to adjust the transparency at a much less granular basis then set the transparency value in the TransparencyAttributes and it will be applied to every pixel rendered by shapes sharing that appearance.

Stage 3

Check to see if the pixel should be written. RenderingAttributes controls this through the use of the setAlphaTestFunction() and setAlphaTestValue() methods. Generally for blended you have the function set to ALWAYS since you always want to write the pixel. If you want to only write the pixel when the alpha meets certain requirements, then set the threshhold using setAlphaTestValue() and then the comparison using setAlphaTestFunction().

Stage 4

Blend the pixel into the scene. This uses the transparency attributes to control the method for merging the pixel with whatever pixel is already in the framebuffer. There are many different effects one can do with the bending functions. For common scene blending use (src=BLEND_img_ALPHA, dst=BLEND_ONE_MINUS_img_ALPHA). For particle systems where you want an brighter effect for overlayed pixels use something like (src=BLEND_img_ALPHA, dst=BLEND_ONE).

 

Rendering order

Many blending techniques are order dependant, requiring that the order of the triangles be rendered back to front. There are several mechanisms for controlling this in Java3d. You can use OrderedGroup for some sorts of things. We do this to draw land and then the ocean, since the ocean is translucent. If you have a series of shapes and geometries which are rendered translucent and want them sorted by distance to the view, then you can use a feature in Java3d to have that done automatically. If you have a single shape with many triangles all intersecting then it is likely you would have to use a GeometryUpdater and use your own sorting policies to handle that.