Apos.Shapes

Shape rendering library for MonoGame and KNI.

DiscordNuGetNuGet

Description

This library draws crisp anti-aliased shapes on the GPU using SDFs. It also draws text straight from a font’s outlines, SVG drawings straight from theirs, and textures with the SpriteBatch API. Shapes, text, drawings, and textures can be interleaved in any order, and they all go to the GPU together in one upload.

Special thanks to Inigo Quilez for doing a lot of the work on the math functions. The text follows Eric Lengyel’s Slug algorithm, by way of Forme.

A small space scene drawn with Apos.Shapes

Features

  • 11 shapes: Circle, Ellipse, Line, Path, Rectangle, Chamfer, Hexagon, Equilateral Triangle, Triangle, Arc, Ring
  • Fill, Border, and Draw variants for every shape
  • Paths draw a polyline as one continuous shape, with round, miter, or bevel joins and round, butt, or square caps. Open or closed
  • Rounded or chamfered corners, one per corner, plus rotation and adjustable anti-aliasing
  • Dashed outlines and strokes, flat or round-capped, down to dotted lines
  • Gradients: linear, radial, conical, spiral, and more, with repeat styles and Oklab / Oklch / RGB color interpolation. Shapes, text, and SVG drawings all take one
  • Blurred shapes with a Gaussian edge, for drop shadows and glows
  • Measure for every shape, giving the bounds it covers so a camera can cull it. No batch and no view needed
  • Blue noise dithering so slow gradients don’t band on 8-bit displays
  • Text solved from the font’s own outlines in the shader. No atlas, so nothing picks a size up front and a glyph stays exact at any size, rotation, and zoom
  • SVG drawings solved the same way, fills and strokes, in the file’s own colors and gradients or in one color of your own
  • Textures (SpriteBatch API)
  • Clipping to rectangles that can be rounded, rotated, and nested inside each other
  • One batch for everything. Shapes, text, drawings, and textures mix in any order
  • Precompiled shaders embedded in the assembly using ShadowDusk, in a small version for each mix of features so a game only waits on compiling what it draws. No need for Wine to build on Linux or macOS.
  • Works with MonoGame 3.8.2+ and KNI

Documentation

You can also try the library directly in your browser here.

Getting started

Install with:

dotnet add package Apos.Shapes

Set the HiDef profile in your game’s constructor. (With KNI, use FL10_0 instead.) Create a ShapeBatch, then draw between Begin and End:

using Apos.Shapes;

public class Game1 : Game {
    GraphicsDeviceManager _graphics;
    ShapeBatch _sb;

    public Game1() {
        _graphics = new GraphicsDeviceManager(this);
        _graphics.GraphicsProfile = GraphicsProfile.HiDef;
    }

    protected override void LoadContent() {
        _sb = new ShapeBatch(GraphicsDevice);
    }

    protected override void Draw(GameTime gameTime) {
        GraphicsDevice.Clear(new Color(17, 24, 39));

        _sb.Begin();
        _sb.FillCircle(new Vector2(120, 120), 75, new Color(96, 165, 250));
        _sb.DrawRectangle(new Vector2(240, 70), new Vector2(150, 100), new Color(220, 38, 38), Color.White, 4f, 20f);
        _sb.BorderLine(new Vector2(120, 240), new Vector2(390, 240), 15, Color.White, 2f);
        _sb.End();

        base.Draw(gameTime);
    }
}

Read the Getting started guide for the full walkthrough.

Other projects you might like

Edit this page on GitHub