z-sorting a vector of triangles

Speed always being an issue with flash based 3d (fingers crossed this will change), sorting triangles is something one wants to be able to do as fast as possible.

If your working with vectors you might have noticed that they are slower to sort objects on a property than an array is.

There a numerous articles on the web about different methods to squeeze more speed out but here is what I do (specific to 3d shizznit).

var visibleTriangles:Array = [];
/*
...fill array with visible triangles...
*/
visibleTriangles.sortOn("distanceFromCamera", Array.NUMERIC);
return Vector.<Triangle3D>(visibleTriangles);

and that’s it 🙂
Pretty sure this won’t work for everybody but it’s much faster in my tests that using a vector during the sort process. (couldn’t detect any real overhead for the conversion)

2 Responses to “z-sorting a vector of triangles”

  1. Bart says:

    you’re basically allocating two times the memory: once for the Array, then again for the cast to Vector., so what you win by using Array.sortOn you loose on the conversion. Maybe it;s worth it to keep it Array, converting an Array with 10000 elements every render-frame isn’t funny.

  2. bwhiting says:

    indeed it is a waste of memory, but even with the conversion overhead its still much faster.

    its not really recommended but if speed is of the essence and for whatever reason you are tied to vectors it will work.
    (the code for the above solution is less scary than a radix sorter believe me)

    the “lazy man” solution

    🙂

Leave a Reply