Texture Meshes - absoluteAquarian/GraphicsLib GitHub Wiki
While XNA's SpriteBatch
class is very useful, it's very limiting when trying to draw sprites that aren't just rotated, flipped or squashed/stretched in the cardinal axes.
That's where GraphicsLib's Mesh
class comes into play. Mesh
allows full control over how a texture is drawn to the screen, although its usage is significantly more complex than SpriteBatch
.
Fields
Mesh
doesn't require many fields to work. The few that exist are listed below:
Name | Type | Description |
---|---|---|
texture |
Texture2D |
The texture used by the Mesh instance. |
mesh |
VertexPositionColorTexture[] |
The array of vertex data used to draw the texture. |
shader |
Effect |
The shader used when drawing the texture, or null if a shader should not be used. |
ID |
int |
The number assigned to the Mesh instance. Each Mesh instance has a unique ID. |
Usage
Constructors
Mesh
provides a few constructors to create a Mesh
instance, listed below:
Mesh(Texture2D texture, Vector2[] positions, Vector2[] textureCoords, Color color, Effect shader = null)
Creates a mesh where each vertex uses the same color.
Texture coordinates provided in textureCoords
must be >= 0
and <= 1
in order to be considered valid.
Mesh(Texture2D texture, Vector2[] positions, Vector2[] textureCoords, Color[] colors, Effect shader = null)
Creates a mesh where each vertex is assigned its own color.
Texture coordinates provided in textureCoords
must be >= 0
and <= 1
in order to be considered valid.
Mesh(Texture2D texture, VertexPositionColorTexture[] vertices, Effect shader = null)
Creates a mesh using pre-defined vertex data.
Texture coordinates provided in vertices[X].TextureCoordinate
must be >= 0
and <= 1
in order to be considered valid.
Mesh(Texture2D texture, VertexPositionColorTexture[] vertices, int[] indices, Effect shader = null)
Creates a mesh using pre-defined vertex data and index data.
The length of vertices
does not have to match the length of indices
.
Texture coordinates provided in vertices[X].TextureCoordinate
must be >= 0
and <= 1
in order to be considered valid.
Mesh(Texture2D texture, Vector2[] positions, Vector2[] textureCoords, Color color, int[] indices, Effect shader = null)
Creates a mesh where each vertex uses the same color.
The mesh will have pre-defined index data.
Texture coordinates provided in textureCoords
must be >= 0
and <= 1
in order to be considered valid.
Mesh(Texture2D texture, Vector2[] positions, Vector2[] textureCoords, Color[] colors, int[] indices, Effect shader = null)
Creates a mesh where each vertex is assigned its own color.
The mesh will have pre-defined index data.
Texture coordinates provided in textureCoords
must be >= 0
and <= 1
in order to be considered valid.
Drawing
Drawing a mesh is as simple as calling Draw()
on the Mesh
instance. Meshes can be reused infinitely, so it's advised that you cache the Mesh
instances you create, since GraphicsLib won't do it for you.
Manipulation
Translation: Meshes can be moved via mesh.ApplyTranslation(Vector2 offset)
.
This method moves all vertices by the offset
provided.
Rotation: Meshes can be rotated around an ABSOLUTE coordinate via mesh.ApplyRotation(Vector2 rotationOrigin, float radians)
.
This method revolves all vertices around rotationOrigin
by radians
radians.
Scaling: Meshes can be stretched/squished via mesh.ApplyScale(float scale, Vector2 scaleCenter, float? scaleAxesDirection = null)
or mesh.ApplyScale(Vector2 scale, Vector2 scaleCenter, float? scaleAxesDirection = null)
.
These methods stretch or squish the mesh, using scaleCenter
as the anchor point, by the scale
factor.
scaleAxesDirection
corresponds to the "angle" the X-axis of scaling will use. It is recommended to pass in how much the mesh has been rotated by into scaleAxesDirection
, though it defaults to 0
(the standard axes).