gilzoide / unity-lyon-tessellation

Tessellation of vector paths in Unity, powered by lyon

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Lyon Tesselation

(WIP) Tessellation of vector paths in Unity, powered by lyon.

Image showing a smiley face in the Unity editor built using this package

Image showing a smiley face in the Unity editor built using this package

Features

  • Supports both Fill and Stroke tessellation, with several configurations provided by lyon
  • Easy to use PathBuilder API for drawing paths that will be passed to the Tessellator
  • Run multithreaded tessellation with the Job System and Burst by leveraging the Create*Job methods from Tessellator
  • Both PathBuilder and Tessellator support reusing memory, just call Clear() instead of disposing of them and allocating new instances
  • Prebuilt for the following platforms: Windows (x86_64), Linux (x86_64), macOS (x86_64, arm64), iOS (arm64), Android (arm32, arm64, x86, x86_64)

Samples

This package include the following sample scenes:

  • Graphic: a smiley path being tessellated into a Unity UI Graphic.

Usage example

using Gilzoide.LyonTesselation;
using UnityEngine;
using UnityEngine.Jobs;
using UnityEngine.UI;

// 1. Create a path
var path = new PathBuilder();
path.AddCircle(Vector2.zero, 100)
    .AddCircle(new Vector2(20, 20), 20)
    .AddCircle(new Vector2(20, 20), 20)
    .BeginAt(new Vector2(30, 5))
        .CubicBezierTo(
            new Vector2(30, 45),
            new Vector2(30, 45),
            new Vector2(30, 5)
        )
    .Close();

// 2. Create a Tessellator
// The type parameters represent the Vertex and Index types:
// - Vertex type must contain at least 2 floats, for the position
// - Index type should be an integer of at least 16 bits (ushort, int...)
// In this example, we'll use UIVertex for building meshes for Unity UI.
var tessellator = new Tessellator<UIVertex, int>();

// 3. Tessellate! This will fill the tessellator's internal buffers
// 3.a) AppendPathFill will triangulate the path's fill
tessellator.AppendPathFill(path);
// Alternatively, schedule the path fill using Unity's Job System
JobHandle fillJob = tessellator.CreatePathFillJob(path).Schedule();
// 3.b) AppendPathFill will triangulate the path's stroke
tessellator.AppendPathStroke();
// Alternatively, schedule the path stroke using Unity's Job System
JobHandle strokeJob = tessellator.CreatePathStrokeJob(path).Schedule();

// 4. Fetch the vertex buffer and index buffer for whatever
// If you used jobs for fill/stroke, make sure to complete them first.
fillJob.Complete();
strokeJob.Complete();
NativeArray<UIVertex> vertices = tessellator.Vertices;
NativeArray<int> indices = tessellator.Indices;

// 5. If you are using UIVertex, there is a helper job that fills
// vertices color and UVs. You can use job dependencies to run it
// right after tessellation ends.
JobHandle dependency = JobHandle.CombineDependencies(fillJob, strokeJob);
JobHandle uiVertexJob = tessellator.CreateUIVertexJob(color, rect).Schedule(dependency);
// at some point, e.g. inside Graphic.OnPopulateMesh
uiVertexJob.Complete()
NativeArray<UIVertex> coloredVertices = tessellator.Vertices;
NativeArray<int> indices = tessellator.Indices;
// Check out the "Graphic" sample for an example of usage

// 6. Dispose of paths and tessellators when not needed anymore.
tessellator.Dispose();
path.Dispose();

About

Tessellation of vector paths in Unity, powered by lyon

License:The Unlicense


Languages

Language:C# 59.2%Language:Rust 16.1%Language:C++ 13.8%Language:Makefile 5.8%Language:C 3.4%Language:ShaderLab 1.6%