Polygon Sprite
A Polygon Sprite is also a Sprite
, that is used to display a 2d image.
However, unlike a normal Sprite
object, which is a rectangle made of just 2
triangles, PolygonSprite
objects are made of a series of triangles.
Why use a Polygon Sprite?
Simple, performance!
There is a lot of technical jargon that we can toss around here about pixel fill rate but the take home lesson is that a PolygonSprite
draws based upon the shape of your Sprite
, not a simple rectangle around the largest width and height. This saves a lot of unnecessary drawing. Just like Sprite
objects, PolygonSprite
objects can be used in spritesheets. Texture Packer is one tool that can handle creating spritesheets with out of PolygonSprite
objects.
Consider this example:
Notice the difference between the left and right versions?
On the left, a typical Sprite
drawn in rectangular fashion by the use of 2
triangles.
On the right, a PolygonSprite
drawn with many smaller triangles.
Whether or not this trade-off is worth it for purely performance reasons depends on a number of factors (sprite shape/detail, size, quantity drawn on screen, etc.), but in general, vertices are cheaper than pixels on modern GPUs.
AutoPolygon
AutoPolygon
is a helper class. It's purpose is to process an image into a 2d
polygon mesh at runtime.
There are functions for each step in the process, from tracing all the points,
to triangulation. The result, can be then passed to a Sprite
objects create
function to create a PolygonSprite
. Example:
// Generate polygon info automatically.
auto pinfo = AutoPolygon::generatePolygon("filename.png");
// Create a sprite with polygon info.
auto sprite = Sprite::create(pinfo);