Papervision3D: QuadTree Released

If you haven’t seen it yet, Andy Zupko just released QuadTree for Papervision3D! He also released a more indepth look at using the new features on his blog. A big huge thanks to the Away3D team for their code and brilliance!  Andy was able to port it over to work with Papervision3D rather well and we’ve added some features that I think everyone will want to take advantage of.

So, what is QuadTree?  Well, in simple terms, its corrective z sorting for a 3D scene.  Say wha?  In other words, when you have 2 triangles who’s size is bigger than the distance between them, you will sometimes see sorting issues depending on the angle the camera is to the triangles.

It makes this:

Look like this:

How do I use it?

Now, this is very easy to use with Papervision3D.  Instead of creating a BasicRenderEngine, you’d now use the new QuadrantRenderEngine object instead:

var renderer:QuadrantRenderEngine = new QuadrantRenderEngine(QuadrantRenderEngine.CORRECT_Z_FILTER);

You’ll notice that there’s one argument about which type of filter to use.  On the sample I’ve created, I used the CORRECT_Z_FILTER which is the faster filter of the 3 options.  It simply takes care of z sorting of triangles.

Two new features added for speed

There’s also 2 other options worth mentioning.  One is the ability to tell QuadTree that any particular DO3D is not to be tested on each render (DisplayObject3D.testQuad:Boolean=true).  The default setting is true, but you can set it to false.  The second option is the maxLevel setting.  This controls the regions of the QuadTree.  Having less does NOT mean better performance.   You have to find a nice balance between the # of tri’s you have and how many regions gives you the best bang for the buck.

You might be asking yourself “why would I only want certain objects to be tested for z sorting fixes?”.  Well, the example I’ve created shows you just a instance that you *would* choose to be very picky about what QuadTree does and doesn’t test on.  This example is using the 3D object I’d made for my Mom’s site.  When you first load the sample, it is *not* including the big white mass in the middle (under the panels) in the QuadTree testing.  To activate it, hit the “s” key on the keyboard, and you’ll notice a decrease in performance.  In this particular type of demo, not testing the biggest object in the scene pays off as the panels above it are still corrected.  That’s a substantial savings.

Also, if you want to see how maxLevel affects performance, use the Q and A buttons to increase and decrease the maxLevel.  As I said earlier, a lower number does not equal better performance.  You just have to find a nice middle ground.  The default value, btw, is 4 and my demo seemed to run better set to 3.

Try it out here

My point, all along, is proven true😉

Now, this all leads me to a point that I’ve been making since this topic and request for corrective z sorting came up a long time ago.  My point has been:  If you have the 3D model, fix the sorting yourself – its FAR faster performance-wize than QuadTree could ever be.  And my example here proves my point.

In the pictures below, I have the original version of my 3D object, and then a new version with the Panels much closer to the surface of the main structure.  The only difference between the 2 visually, is JUST that:  the panels are simply closer to the main structure.

Original:

The new QuadTree enhanced version:

When you run my sample, just hit “T” to switch between the QuadTree enhanced version and my original model.  You’ll see a huge difference in performance.  Yes, there are still some sorting issues in my model, but I could very easily go in and split the triangles by hand to fix those minor issues.  And I’d still have a very fast presentation.  While I would agree that having the panels closer to the main object looks a bit nicer, it’s not “nice enough” to justify the CPU/performance.

Now don’t go and take this as “John thinks QuadTree is useless” – I don’t think that, but I do think it’s uses are very confined given the speed we all live with in Flash 3D.  It’s awesome to see it working, and I would love to see good samples of it in production for sure, but if speed is what you need, you’ll likely end up fixing your sorting issues by hand in a good editor.  Of course, I haven’t talked about triangles colliding, which is something QuadTree deals with as well.  But for more information on that, head over to Andy’s blog for a full explanation😉

Conclusion

Corrective Z sorting is a very cool feature to have indeed and, btw, the most requested.  But so was shaders before they came out.  Now ask yourself, “how much do I use shaders these days?”  I’d venture a guess that your answer is hardly ever – if ever.  I think we’ll see more need for QuadTree than shaders, but I don’t think that QuadTree is the miracle pill we’ll all be able to leverage very often to fix our sorting issues.  That will most likely STILL fall on the hands of the 3D modeler 95% of the time I would bet.

    • flashm
    • November 6th, 2008

    great article ! i love this kind of articles … deep explanation … keep up good work !

  1. Any chance you might post the source for this flash program? My QuadrantRenderEngine code is running so slowly, I’d love to see how you pull off such great performance…

  2. Thanks for this great article. Any chance you could post the source code too?

    • Steve
    • July 4th, 2009

    First of all thanks for that great article. Quadtrees solved my problems in a very easy way.

    But I have another question to your sample. Are those real shadows what I can see in those two pictures you posted or did you made these very nice looking cubes only with shaders ?

    Couldn’t see it in action because the link seems to be down.
    Maybe you have a working link or sources for me ?

    Greetings
    Steve

  3. First off, this is a good article.

    I definitely agree with ‘sorting’ the z-sorting yourself rather than using QuadTree. Anything to squeeze that last bit of performance from the machine.

    Maybe you could write up a quick tut/article on how you would go about sorting a model yourself to avoid QuadTree as well as perhaps posting the source?

    Keep at it!

  4. So lets see PVD didnt work before. The rendering was buggy. That’s just something that the developer was expected to randomly happen upon without explanation, and then waste time looking for bugs in his code fruitlessly. “What you want PVD to actually work?” Oh well, duh, then you have to search the web to find out that you must set the option

    MakeItRenderCorrectly = true

    oops, no, in this case it’s really

    renderer = QuadrantRenderEngine

    And so along with all the others posters let me say yayayayayayayayayayayayay you guys are so so so great now that you actually made it render correctly!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

    Keep up the superfantastic work!

    • Martijn
    • March 18th, 2010

    Hello,

    The QuadrantRenderEngine solved my Z problem but is there a way to speed it up? I have a DAE model and it is slow.

    Thanks,

    Martijn

    • no, that’s the problem with using Quadtree😉

      The only thing you can do is to do the triangle slicing yourself where you created your 3D model (IE do the tesslation yourself). Otherwise, if you’re using Quadtree, maybe you can lower the poly count on surfaces that don’t need the poly’s.

      then of course, there’s being careful with MovieClip materials that are self animating (you should control that by hand btw)

  1. October 14th, 2008
  2. May 26th, 2010

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s