How to Use It
To get the corrective z-sorting working, you simply need to use a new RenderEngine – QuadrantRenderEngine. It takes one parameter – type – and depending how you set it you will get different results (and different performance).
- QuadrantRenderEngine.CORRECT_Z_FILTER – This filter (QuadrantZFilter) does exactly what it sounds like – it sorts the triangles (when possible) correctly – removing the issues you would normally have with the painters algorithm. Note that this will not correctly sort triangles that pass through each other. For that you will need to use the next value.
- QuadrantRenderEngine.QUAD_SPLIT_FILTER – The QuadrantFilter actually splits faces that intersect each other. Since you can’t correctly z sort triangles that run into each other – if you need the ability to cut faces that intersect, you can use this option. Often though, after using this, you will still need to use the CORRECT_Z_FILTER to sort the newly cut faces properly. Which brings us to:
- QuadrantRenderEngine.ALL_FILTERS – This option uses both the CORRECT_Z_FILTER and QUAD_SPLIT_FILTER – which will give you corrective sorting of triangles no matter if they pass through each other or not. An alternative to using this value is using CORRECT_Z_FILTER | QUAD_SPLIT_FILTER.
Making it run a little better
One feature we did add to the QuadTree is the ability to turn off testing for certain objects. While in alot of cases this won’t be a huge performance gain – it does offload some math thats not needed if you know it won’t be an issue. An example of this would be having a model floating in a courtyard. You know the model will never have z-fighting with the courtyard, so you can turn off the courtyard from the tests. It will still be sorted in the usual pv3d fashion, so you don’t have to worry.
To turn off QuadTree testing for a DisplayObject3D – you can set the do3d.testQuad property. When this boolean is set to false, the faces of the object will not have any QuadTree testing performed on them. This will save you calculations when you don’t need them.
You can also set the maximum depth the QuadTree will subdivided. This can be set with the QuadTree.maxlevel property. Mess with this to find a good balance of node size and performance. Too many nodes can be a waste of infromation and requires testing rediculously small areas – but too large of nodes requires extra processing in many cases, and won’t be as accurate. Play with it per-project to find a good balance.
So When Should I Use This?
Honestly? This is a feature that everyone wants but isn’t the miracle cure they hoped for. While a very cool feature – it can destroy your CPU. For things such as model viewers, where you have no control over certain models, it is a great option. However, for most thing you are MUCH better off using ViewportLayers and correct modeling techniques. These will add no overhead and will give you the results you want. In addition, since the QuadTree operates at runtime, if you have a static scene, you can use BSP, which preprocesses the scene.
I’ve included an extra class (you can see it in action by pressing “V” in the demos). It is the QuadTreeVisualizer which will show you all the subdivisions you are currently using. Doesn’t help much – but is cool to check out and might help when debugging.
Demo One – This demo shows Papervision using the QuadrantRenderEngine.ALL_FILTERS property. Both splitting and corrective z-sorting is performed on the objects. You can toggle the “testQuad” property for the bottom plane as well. Check it out HERE
Demo Two – This demo is only using corrective z-sorting. Toggle it on and off and you can see where triangles are fixed where they were previously messed up. Check it out HERE
Get The Source – Both demos use the same source – just uncomment where required
Enjoy! And of course let us know of any bugs!