texture to vertices with spritesheet

Topics: Developer Forum
Dec 25, 2008 at 12:41 AM
Is there a way to convert part of a texture (as on a spritesheet) to vertices?  Going by the example code, I'd say there isn't.  But I'm wondering if there is a way to create a Texture2D object from part of a sprite sheet, so that I might feed that into the texture-to-vertices algorithm.
Dec 25, 2008 at 1:03 AM
Err.. I just figured it out, in case anyone else is trying to do something similar.

The reason I wanted to do this in the first place was so that I could take advantage of using spritesheets for drawing my sprites in various states (healthy, damaged, destroyed, etc).  All of the states should use the same geometry, but with different pixel data.  But if you use the entire spritesheet to generate the geometry, obviously you'd run into issues.  So what I did was create a spritesheet with the various states arranged top to bottom.  I then used the following code to generate my geometry:

int numImages = 2; // in my case I tested with a spritesheet that was two images tall

texture = game.Content.Load<Texture2D>("Textures/" + textureName);
uint[] data = new uint[texture.Width * texture.Height];
texture.GetData(data);

int newDataSize = texture.Width * texture.Height / numImages;
uint[] newData = new uint[newDataSize];

for (int i = 0; i < newDataSize; i++) {
    newData[i] = data[i];
}

Vertices verts = Vertices.CreatePolygon(newData, texture.Width, texture.Height / numImages);