-
[SWF(width = 500, height=500, backgroundColor = 0x000000)]
-
x = stage.stageWidth / 2;
-
y = stage.stageHeight / 2;
-
-
// standard Vectors for using drawTriangles and projectVectors
-
var verts:Vector.<Number> = new Vector.<Number>();
-
var pVerts:Vector.<Number>;
-
var uvts:Vector.<Number>;
-
var indices:Vector.<int>;
-
// needed for z-sorting
-
var sortedIndices:Vector.<int>;
-
var faces:Array = [];
-
-
// we'll use this for transforming points
-
// and as the transformation matrix for our render
-
var m:Matrix3D = new Matrix3D();
-
-
// plot a poly
-
var poly:Vector.<Number>;
-
poly = Vector.<Number>([ 0, 0, 0,
-
10, 0, 0,
-
0,10, 0]);
-
-
verts = verts.concat(poly);
-
faces.push(new Vector3D());
-
-
// add two more polygons by transorming the poly Vector
-
// and concatenating it to the verts Vector
-
// Vector3D instances are added to the faces
-
// Array for z-sorting
-
-
// temp vect for any transformed polygons
-
var transPoly:Vector.<Number> = new Vector.<Number>();
-
-
m.appendRotation(10, Vector3D.Z_AXIS);
-
m.appendTranslation(0,-2,5);
-
m.transformVectors(poly, transPoly);
-
-
verts = verts.concat(transPoly);
-
faces.push(new Vector3D());
-
-
m.appendRotation(10, Vector3D.Z_AXIS);
-
m.appendTranslation(0,-2,5);
-
m.transformVectors(poly, transPoly);
-
verts = verts.concat(transPoly);
-
faces.push(new Vector3D());
-
-
// hard coded indices
-
indices = Vector.<int>([0, 1, 2, 3, 4, 5, 6, 7, 8]);
-
sortedIndices = new Vector.<int>(indices.length, true);
-
-
// create texture
-
var tex:BitmapData = new BitmapData(100,100,false, 0x000000);
-
tex.fillRect(new Rectangle(0,0,50,50), 0xFF0000);
-
tex.fillRect(new Rectangle(50,0,50,50), 0x0000FF);
-
tex.fillRect(new Rectangle(0,50,50,50), 0x00FF00);
-
-
// hard coded uvts
-
uvts = Vector.<Number>([0,0,0, .5,0,0, 0,.5,0,
-
.53, 0, 0, 1, 0, 0, .53, .5, 0,
-
0,.6,.5, 0,.6,0, 0,1, 0]);
-
-
// fix all vector lengths
-
verts.fixed = true, uvts.fixed = true, indices.fixed = true
-
pVerts = new Vector.<Number>(verts.length/3 * 2, true);
-
-
// we need these if we want perspective
-
var persp:PerspectiveProjection = new PerspectiveProjection();
-
// projection matrix
-
var proj:Matrix3D = persp.toMatrix3D();
-
-
addEventListener(Event.ENTER_FRAME, onLoop);
-
function onLoop(evt:Event):void {
-
m.identity();
-
m.appendRotation(mouseY * 2, Vector3D.X_AXIS);
-
m.appendRotation(mouseX * 2, Vector3D.Y_AXIS);
-
// push everything back so its not too close
-
m.appendTranslation(0,0,40);
-
// append the projection matrix at the end
-
m.append(proj);
-
-
Utils3D.projectVectors(m, verts, pVerts, uvts);
-
-
var face:Vector3D;
-
var inc:int = 0;
-
for (var i:int = 0; i<indices.length; i+=3){
-
face = faces[inc];
-
face.x = indices[i];
-
// it may look odd, but casting to an int
-
// when doing operations inside array sytnax
-
// adds a big speed boost
-
face.y = indices[int(i + 1)];
-
face.z = indices[int(i + 2)];
-
var i3:int = i * 3;
-
// get the average z position (t value) and store it in the Vector3D w property
-
// depending on your model, you may not need to do an average of all 3 values
-
// (t is the distance from the eye to the texture)
-
face.w = (uvts[int(i3 + 2)] + uvts[int(i3 + 5)] + uvts[int(i3 + 8)]) * 0.333333;
-
inc++;
-
}
-
-
// sort on w, so far this beats all other sorting methods for speed,
-
// faster than Vector.sort(), faster than any custom sort method I could find
-
faces.sortOn("w", Array.NUMERIC);
-
-
// re-order indices so that faces are drawn in the correct order (back to front);
-
inc = 0;
-
for each (face in faces){
-
sortedIndices[inc++] = face.x;
-
sortedIndices[inc++] = face.y;
-
sortedIndices[inc++] = face.z;
-
}
-
-
graphics.clear();
-
graphics.beginBitmapFill(tex, null, false, false);
-
graphics.drawTriangles(pVerts, sortedIndices, uvts, TriangleCulling.NONE);
-
}
So you want to be able to do some z-sorting with drawTriangles()? Well, its extremely unintuitive. Here is what you need to know:
1) Vector.sort() is slow
2) Array.sortOn() is quite fast
3) I couldn't get any custom sorting method to be faster than Array.sortOn
4) You can use the t value from uvts to do z-sorting (like in this demo)
5) If your going for speed, don't forget to use fixed Vectors (I often forget this step)
6) When working with Vectors cast any operations inside brackets to int() (my tests showed this is a huge speed boost up to 3x faster - franky I think it's horrible that you have to do this).
The swf is not very interesting because I wanted to keep it super simple, but here it is anyway...
Questions
I was told and have read that drawTriangles is slower than using drawGraphicsData and a GraphicsTrianglePath instance. I tested this extensively and found that drawGraphicsData was slower. I'm still not sure which is faster but am up for checking out any simple benchmarks that people have.
drawTriangles is supposed to be faster without adding t values to the uvts Vector. In my benchmark tests there was no real difference in speed. The t value is used to render more accurate textures - which would mean you could potentially use less triangles in your models. One would assume that not using a t value would be faster... but I couldn't confirm this. Again if someone has a good benchmark for this, please let me know.
Help
I had a great deal of help from a few people on Twitter, Katopz (Away3D contributor) of sleepydesign.com made me aware of the fact that Array.sortOn is the way to go... You can see some great demos over at Katopz's blog. While I didn't end up using a custom sorting method, Inspirit
pointed me to his article about custom Vector sorting methods which proved to be very enlightening. I was able to get some really great results for doing different types of sorting - just sorting 1D Vectors with FlashSort can be very fast (faster than Array.sortOn - but not helpful for sorting a 1D Vector of indices).
I also learned a good deal by looking at this code post by Psyark... I actually optimized that code to run about 2x faster (will post that on wonderfl.net at some point, it just needs to be cleaned up).
kevinSuttle sent me a link to The video