Grids & Deformation
Connected Grids
Section titled “Connected Grids”Use MeshGridGenerator2D.GenerateConnectedGrid() to build a connected triangle grid over a texture.
var texture = Texture2D.LoadFromFile("character.png");
var grid = MeshGridGenerator2D.GenerateConnectedGrid( texture, new MeshGridOptions2D { Spacing = 64 });The generator clamps spacing to a practical range and creates shared vertices along cell boundaries.
Mask-Guided Grids
Section titled “Mask-Guided Grids”Pass an IMeshMask2D when you want diagonal choices to better follow a visible shape while keeping the grid connected.
var mask = MeshMask2D.FromPredicate(point => alphaMask.IsOpaqueAt(point.X, point.Y));
var grid = MeshGridGenerator2D.GenerateConnectedGrid( texture, new MeshGridOptions2D { Spacing = 64 }, mask);You can also pass a Func<Vector2, bool> directly:
var grid = MeshGridGenerator2D.GenerateConnectedGrid( texture, point => alphaMask.IsOpaqueAt(point.X, point.Y), new MeshGridOptions2D { Spacing = 64 });Alpha-Map Masks
Section titled “Alpha-Map Masks”Use MeshMask2D.FromAlphaMap() when your app already decoded image pixels and has one alpha byte per pixel. The alpha data is row-major, and stride defaults to width.
var mask = MeshMask2D.FromAlphaMap( alpha: alphaBytes, width: imageWidth, height: imageHeight, threshold: 8);Pass copy: false when you own the alpha buffer lifetime and want to avoid copying it:
var mask = MeshMask2D.FromAlphaMap(alphaBytes, width, height, threshold: 8, copy: false);The core library still does not decode full image pixels. Decode image data in your UI/game layer, then adapt the alpha channel into IMeshMask2D with this helper.
Masked Contour Grids
Section titled “Masked Contour Grids”Use GenerateMaskedContourGrid() when you want mesh faces only around the masked shape.
var contour = MeshGridGenerator2D.GenerateMaskedContourGrid( texture, mask, new MeshGridOptions2D { Spacing = 48 });This is useful for overlays, picking, previews, or visualizing the visible alpha region of a sprite.
Deformers
Section titled “Deformers”IMeshDeformer2D lets you calculate deformed positions without changing the original mesh unless you explicitly apply the result.
public sealed class WaveDeformer : IMeshDeformer2D{ public void DeformInto(Mesh2D mesh, Span<Vector2> target) { for (var i = 0; i < mesh.Vertices.Count; i++) { var position = mesh.Vertices[i].Position; position.Y += MathF.Sin(position.X * 0.04f) * 8f; target[i] = position; } }}Built-in reference deformers include:
| Type | Use |
|---|---|
VertexOffsetDeformer2D |
Offset selected vertices. |
RadialDragDeformer2D |
Drag vertices with a smooth radial falloff. |
var drag = new RadialDragDeformer2D { Radius = 180f };drag.SetDrag(origin, offset);
grid.ApplyDeformer(drag);ApplyDeformer() commits deformed positions back into the mesh.