Class Mesh
Triangles with an optional color, texture coordinate or value at each vertex, for DrawMesh(Mesh, Paint?, BlendMode, ReadOnlySpan<GradientStop>): Gouraud-shaded triangles, textured triangles (a paint mapped through the texture coordinates) and PDF's free-form and lattice-form shadings (one value per vertex, mapped through gradient stops). Immutable: the constructor copies what it is given.
public sealed class Mesh
- Inheritance
-
Mesh
- Inherited Members
Examples
using (RasterCanvas<Rgba8> canvas = raster.CreateCanvas())
{
// Gouraud shading: a color at each corner, blended across each triangle without cracks between them.
Vector2[] quad = [new(10, 10), new(140, 20), new(130, 140), new(20, 120)];
RgbaF32[] colors = [new(1, 0.2f, 0.2f, 1), new(1, 0.9f, 0.2f, 1), new(0.2f, 0.6f, 1, 1), new(0.6f, 0.2f, 0.9f, 1)];
canvas.DrawMesh(new Mesh(MeshMode.Fan, quad, colors), Rgba8.White);
// A textured mesh: each vertex says which point of the paint lands on it, here bending a photo into a trapezoid.
Vector2[] trapezoid = [new(170, 20), new(280, 30), new(290, 140), new(160, 130)];
Vector2[] corners = [new(0, 0), new(64, 0), new(64, 64), new(0, 64)];
canvas.DrawMesh(new Mesh(MeshMode.Fan, trapezoid, texCoords: corners), new ImagePaint(photo));
}
Constructors
Mesh(MeshMode, ReadOnlySpan<Vector2>, ReadOnlySpan<RgbaF32>, ReadOnlySpan<Vector2>, ReadOnlySpan<float>, ReadOnlySpan<int>)
Initializes a new instance of the Mesh class.
public Mesh(MeshMode mode, ReadOnlySpan<Vector2> positions, ReadOnlySpan<RgbaF32> colors = default, ReadOnlySpan<Vector2> texCoords = default, ReadOnlySpan<float> values = default, ReadOnlySpan<int> indices = default)
Parameters
modeMeshModeHow the vertices make triangles.
positionsReadOnlySpan<Vector2>The vertices, in the canvas's local coordinates.
colorsReadOnlySpan<RgbaF32>One straight-alpha color per vertex, or empty for none.
texCoordsReadOnlySpan<Vector2>One point in the paint's coordinates per vertex, or empty for none: the paint is mapped onto each triangle through them.
valuesReadOnlySpan<float>One value per vertex, or empty for none: interpolated, then mapped to a color through the stops given to DrawMesh(Mesh, Paint?, BlendMode, ReadOnlySpan<GradientStop>). A mesh has colors or values, not both.
indicesReadOnlySpan<int>Indices into the vertices, read by
modein place of the vertices themselves, or empty to read the vertices in order.
Exceptions
- ArgumentOutOfRangeException
modeis not defined, or an index is outside the vertices.- ArgumentException
A per-vertex list's length differs from the positions', both colors and values are given, or a number is not finite.
Properties
Bounds
Gets the bounds of the vertices.
public RectF Bounds { get; }
Property Value
Colors
Gets the vertex colors, or empty when there are none: for a mesh with inks, their appearances in sRGB.
public ReadOnlyMemory<RgbaF32> Colors { get; }
Property Value
Indices
Gets the indices, or empty when the vertices are read in order.
public ReadOnlyMemory<int> Indices { get; }
Property Value
Inks
Gets the vertex colours as inks, or empty when the mesh has none (see WithInks(ReadOnlySpan<InkColor>)).
public ReadOnlyMemory<InkColor> Inks { get; }
Property Value
Mode
Gets how the vertices make triangles.
public MeshMode Mode { get; }
Property Value
Positions
Gets the vertices.
public ReadOnlyMemory<Vector2> Positions { get; }
Property Value
TexCoords
Gets the texture coordinates, or empty when there are none.
public ReadOnlyMemory<Vector2> TexCoords { get; }
Property Value
TriangleCount
Gets the number of triangles the mode makes, degenerate ones included.
public int TriangleCount { get; }
Property Value
Values
Gets the vertex values, or empty when there are none.
public ReadOnlyMemory<float> Values { get; }
Property Value
Methods
WithInks(ReadOnlySpan<InkColor>)
Returns this mesh with a colour given as inks for each vertex: an ink canvas blends their tints across each triangle (an ink a vertex does not name counts as 0 there); other canvases use the appearances in sRGB, which become Colors.
public Mesh WithInks(ReadOnlySpan<InkColor> inks)
Parameters
inksReadOnlySpan<InkColor>One colour per vertex.
Returns
- Mesh
The new mesh.
Exceptions
- ArgumentException
The count differs from the vertices', or the mesh has values.
- ArgumentNullException
A colour is null.