[as3][3d] triangle to triangle matrix transform function (for 3d texture flash games)
Hi.
I was recently debugging a 3d texture triangle actionscript thing, and needed to make a better triangle to triangle transform function, one that worked as a general case for right angle triangles.
I found several tutorials, like senoculars excellent one, but what I wanted was the function code without bothering to have to work it out. I couldnt find it anywhere, so I had to work it out.
Im posting it here so that anyone looking for this sort of triangle to triangle code using matrix transformations and bitmapbeginfill to make 3d texture games in flash actionscript 3, will find this code from google and save themselves lots of pain.
Code:
function TriangleToTriangleMap( bm:BitmapData, sAx:int, sAy:int, sBx:int, sBy:int, sCx:int, sCy:int, g:Graphics, dAx:int, dAy:int, dBx:int, dBy:int, dCx:int, dCy:int ){
var m:Matrix = new Matrix();
// make matrix for triangle
m.a = (dBx - dAx) / (sBx - sAx); // scale X
m.d = (dCy - dAy) / (sCy - sAy); // scale Y
m.b = (dBy - dAy) / (sBx - sAx); // skew X
m.c = (dCx - dAx) / (sCy - sAy); // skew Y
m.tx = dAx - sAx * m.a - sAy * m.c; // translate X
m.ty = dAy - sAy * m.d - sAx * m.b; // translate Y
// render
g.beginBitmapFill( bm, m, false );
g.moveTo(dAx, dAy);
g.lineTo(dBx, dBy);
g.lineTo(dCx, dCy);
g.endFill();
}
I have made a little demo tutorial thing, attached.