https://gist.github.com/JohnBlackburne/f99e4494449c9f487abc9805f5079807
This class generates meshes which have soft, i.e. antialiased, edges. It does this using a technique discussed before, but which I got round to including in a class. It adds vertices around the edge so the edge becomes a gradient from 1 to 0 alpha.
It is meant to be subclassed, or at least that’s how I use it, creating a class for any shape I need. Here e.g. is a quad using it:
public final class AAQuad extends AAMesh {
private static var poly:Polygon = new Polygon();
private var _w :Number;
private var _h :Number;
function AAQuad(w:Number, h:Number, e:Number, tint:uint, alpha:Number = 1.0) {
_w = w;
_h = h;
setupPoly();
super(poly, e, tint, alpha);
}
private function setupPoly():void {
poly.numVertices = 4;
poly.setVertex(0, -_w / 2, -_h / 2);
poly.setVertex(1, _w / 2, -_h / 2);
poly.setVertex(2, _w / 2, _h / 2);
poly.setVertex(3, -_w / 2, _h / 2);
}
public function setDimensions(w:Number, h:Number, e:Number):void {
_w = w;
_h = h;
setupPoly();
updateShape(poly, e);
}
}
It shows the main features, how to create a mesh from a polygon, and how to update its dimensions. The latter is done via a function updateShape which only updates the vertex positions, for optimal performance. This assumes the shape does not change, requiring retriangulation which is rather more work.
The class does not support textures. There’s nothing to stop someone supplying texture uv-coords and a texture, but there’s not an obvious way to do it, in part as this sort of edge antialiasing is often done in texture, so I have not added anything to the class for it. It works fine with the mesh gradient fill routine I wrote.
One concern is doubling the number of vertices might have a performance impact but I find it doesn’t. Especially as if you soften an edge you can often get away with fewer edges and vertices in e.g. a curve. In my tests adding this to a curved shape can mean you need less vertices to draw it.