package { import flash.display.BitmapData; import flash.display.Graphics; import flash.geom.Matrix; /** * Creates a mesh for distorting an image in 3D space */ public class ImageMesh3D { /** * determines if lines are drawn illustrating * the mesh used to render the image */ public static var showMesh:Boolean = false; /** * Draws into the target graphics the bitmap provided within the 3D coordinates passed * When used, the oneSided property can be either -1 or 1 where -1 represents one side of the * image and 1 the other where only that side is drawn. 0 indicates that both sides are drawn */ public static function draw(target:Graphics, bitmap:BitmapData, divsX:int, divsY:int, x1:Number, y1:Number, z1:Number, x2:Number, y2:Number, z2:Number, x3:Number, y3:Number, z3:Number, x4:Number, y4:Number, z4:Number, env:Environment3D, oneSided:int = 0):void { if (!bitmap) return; // draw outlines if necessary if (showMesh) { target.lineStyle(1, 0xFF0000); } var x:Number = env.originX; var y:Number = env.originY; var f:Number = env.focalLength; var culling:String = (oneSided == 1) ? "negative" : "none"; var uvt:Vector. = new Vector.; uvt.push(0,0,f/(f+z1), 1,0,f/(f+z2), 1,1,f/(f+z3), 0,1,f/(f+z4)); var vertices:Vector. = new Vector.; vertices.push(x+uvt[2]*x1,y+uvt[2]*y1, x+uvt[5]*x2,y+uvt[5]*y2, x+uvt[8]*x3,y+uvt[8]*y3, x+uvt[11]*x4,y+uvt[11]*y4); var indices:Vector. = new Vector.; indices.push(0,1,3, 1,2,3); // draw triangles target.beginBitmapFill(bitmap, null, false, true); target.drawTriangles(vertices, indices, uvt, culling); target.endFill(); } } }