Sponsored By

How To Properly Render an X-Ray Silhouette Effect in Away3D

How to render an X-ray effect for occluded objects in Flash graphics engine Away3D. I show an elegant way of implementing this as an effect method for multi pass materials.

Max Knoblich, Blogger

July 12, 2013

17 Min Read
Game Developer logo in a gray background | Game Developer

Back in February, I published an article which described how the silhouette/X-ray effect works that is used in some games for concealed objects. In the first half of the article, I described the principle behind the effect, in the second part I described how to implement the effect in Away3D.

This is a follow-up to that article, introducing a more elegant, improved way to implement the effect in Away3D.

Rendered Silhouette (XRay) Effect in Away3D

Rendered Silhouette (XRay) Effect in Away3D

The method described in the original article involved complicated geometric transformations and sharing the 3D context between several separate View3D objects.

The version shown in this article only requires a single class containing a few methods and a single View3D.

What Went Wrong In The Original Article

Why did I choose such a unwieldy solution in the original article anyways?

I was assuming two things which touch on central principles necessary for implementing the effect in Away3D. Turns out, that both assumptions were wrong.

  1. It’s necessary to keep the depth buffer from being written to when rendering the X-ray silhouette. From all I knew back then, it seemed like this was not possible with Flash and Stage3D.

    Google searches turned up nothing, my question on the official Adobe forum was never answered and the answer on stackoverflow suggested resetting the backbuffer which wouldn’t have worked.

    Because of this, I devised the complicated workaround which involved flattening the geometry of the object and putting it in front of the original.

    Turns out, all I needed to know about was the setDepthTest method which can be used to set the appropriate flag.

  2. It’s important to render the objects for which the silhouette should be displayed after the potentially occluding objects. This means you probably want to explicitly set the render order of objects.

    The problem is that Away3D gives you basically no explicit way of doing that. The engine uses a lot of internal mechanisms to determine the order in which to draw objects.

    Since you barely have access to these mechanisms by default, I split the scene into several separated views which share a Stage3DProxy. This way, I was able to render the contents of the scenes explicitly.

    In this implementation, we still don’t set the render order explicitly, but rely on the implicit mechanisms implemented in Away3D.

Setup

This article is based on the Away3D 4.1.1 Beta release.

The most work for this example actually is setting up the simple scene we have seen above.

This is the code I used to create that scene in Away3D in the project’s constructor.

Implementing the Silhouette Effect

I implemented the X-ray effect as a shader method. Shader methods store the code for the vertex and/or fragment shader programs and can be used to extend the functionality of materials in Away3D.

In Away3D, you can add method objects extending EffectMethodBase to multi pass materials like ColorMultiPassMaterial. They will be added to that materials effect pass, which is ideal for our purposes.

This is my code for the XRayMethod.

The central piece of the implementation is the activate function containing the call to setDepthTest(false, Context3DCompareMode.GREATER) setting the proper comparision method and preventing any values from being written into the depth buffer.

The other important method is the getFragmentCode function which assembles the fragment shader code. The code in the example above simply outputs the color stored in xrayColor and xrayAlpha. More elaborate effects could be implemented here.

The rest of the class exposes properties that get passed to the shader program.

Applying the Effect

All you have to do now is to instantiate the XRayMethod and set the xrayColor and xrayAlpha properties.

Then, you add the method to a multi pass material.

And that’s it! You should now see the X-ray effect on the objects using the the material.

About the Render Order in Away3D

As described in the original article, it’s important for the effect that any occluding geometry is rendered before the object using the silhouette is drawn.

As of Away3D 4.1.1, the render order of the objects in a scene is not determined by the objects themselves but by the materials they use. To minimize shader program and constant uploads, the objects in the scene are ordered by material.

As far as I can tell after extensively looking at the Away3D source code, the factors for the sort are:

  1. The number of passes on the material. The objects using materials with fewer passes get rendered first.

  2. The point at which the material was instantiated. If two materials have the same number of passes, the objects using the material which was instantiated first get rendered first.

Most standard materials have only one pass. When we add the XRayMethod object to the material, this activates the effect pass, which bumps the number of passes of that material to two. Which, in most situations, is already enough to make sure it’s rendered last, which we need for the effect.

If you have several materials with two passes each, make sure that the ones using the XRayMethod are instantiated last.

If you have materials that should be rendered before the X-ray one which have more than two passes, then… you’re out of luck, at least for now. The function calculating the renderOrderID in Away3D is onPassChange in the MaterialBase class. However, it’s a private event handler, so you can’t override it. You might get somewhere by overriding MaterialBase's addPass function and add another event handler to the added pass that sets the material’s render order ID, but I haven’t tested that yet.

Read more about:

Featured Blogs
Daily news, dev blogs, and stories from Game Developer straight to your inbox

You May Also Like