Search code examples
actionscript-3airrotationadobe

Rotating a texture around x, y, z axis and Using it to draw a polygon


I'm trying to draw an arbitrary polygon with a transformed texture with Graphics API .

Here's what I'm trying to do in 3 steps:

First, I have a texture (as a BitmapData)

enter image description here

Second, Transform the texture - Tile it and rotate it around x, y or z axis. (y-axis for now).

enter image description here

Third, Draw a polygon using the transformed texture.

enter image description here

I could rotate it around z-axis with the code below:

var gr:Graphics = sp.graphics;
gr.clear();
var mat:Matrix = new Matrix();
mat.scale( 0.5, 0.5 );
mat.rotate( angle );
gr.beginBitmapFill( bd, mat, true, true );
gr.moveTo( points[0].x, points[0].y );
for ( var lp1:int = 1; lp1 < points.length; lp1++ )
    gr.lineTo( points[lp1].x, points[lp1].y );
gr.lineTo( points[0].x, points[0].y );
gr.endFill();

But I couldn't rotate the texture around x or y axis as it requires some sort of projection I guess.

I thought about drawing a rotated Bitmap object onto a BitmapData and using it as a texture:

var bmp:Bitmap = new Bitmap( bd );
bmp.rotationY = angle;
var transformedBd:BitmapData = new BitmapData( 256, 256, true, 0 );
transformedBd.draw( bmp );
… and call gr.beginBitmapFill() with the transformedBd …

But with this code, the texture won't be tiled.

I also looked at drawTriangles() method but AFIK, it only let me draw a rotated polygon, not a polygon with rotated texture.

If anyone has insights on this issue, please share. Any help will be greatly appreciated!


Solution

  • Perhaps you can:

    1. put your 2D Texture inside a Sprite or other container
    2. 3D transform that container, for example by using

      myContainer.rotationX = 20; myContainer.rotationY = 200;

    3 - then you create a new BitmapData() 4 - and you DRAW the entire myContainer into the bitmapdata.

    myBitmapData.draw(myContainer, myMatrix, myColorTransform, blendMode, myRectangle, smooth);
    

    5 - and finally you delete the original 2D texture and myContainer.

    Voila, you now have a 3d transformed texture inside a single bitmapdata.