LibTessDotNet

repository·master·Indexed 18 days ago

https://github.com/speps/libtessdotnet

A .NET Standard 2.0 port of the GLU Tesselator (Tess) used for polygon tessellation. It decomposes complex polygons, including self-intersecting ones, into simpler primitives like triangles. The library supports various winding rules (EvenOdd, NonZero, Positive, Negative) and provides a combining callback to interpolate custom vertex data such as colors or UV coordinates.

Tokens
1.4K
Snippets
2
Records
5
Agent score
13%

What's inside LibTessDotNet

  1. Overview of LibTess.NET

    master
    LibTess.NET is a C# port of the well-known GLU Tessellator. It is used for tessellating polygons (breaking complex polygons into simpler primitives like triangles) which is a common requirement in computer graphics for rendering complex shapes.
  2. Handle custom vertex attributes with a combining callback

    master

    When tessellating, new vertices are often created at intersection points. To preserve or interpolate custom data (like UV coordinates or colors) associated with the original vertices, provide a combining callback to the Tessellate method.

    Signature pattern: object VertexCombine(Vec3 position, object[] data, float[] weights)

    • position: The position of the new vertex.
    • data: An array of the Data objects from the original vertices that contributed to this intersection.
    • weights: An array of weights used to interpolate the data.

    Return the interpolated object (e.g., a new Color or a new UV struct) to be assigned to the new vertex's Data property.

    private static object VertexCombine(LibTessDotNet.Vec3 position, object[] data, float[] weights)
    {
        // Example: Interpolating colors
        var colors = new Color[] { (Color)data[0], (Color)data[1], (Color)data[2], (Color)data[3] };
        var rgba = new float[] {
            (float)colors[0].R * weights[0] + (float)colors[1].R * weights[1] + (float)colors[2].R * weights[2] + (float)colors[3].R * weights[3],
            // ... (repeat for G, B, A)
        };
        return Color.FromArgb((int)rgba[3], (int)rgba[0], (int)rgba[1], (int)rgba[2]);
    }
  3. Tessellate polygons with LibTessDotNet

    master

    LibTessDotNet is a fast and robust tessellator for .NET that converts complex polygons into simpler polygons or triangles. It supports self-intersecting polygons, coincident vertices, and various winding rules (even/odd, non-zero, etc.).

    To use the library, follow these steps:

    1. Create an instance of LibTessDotNet.Tess.
    2. Define your vertices using ContourVertex arrays. Each vertex requires a Position (using Vec3). You can optionally attach custom data to the Data property.
    3. Add contours to the tessellator using AddContour. You can specify the orientation (e.g., Clockwise, CounterClockwise, or Original).
    4. Call Tessellate specifying the WindingRule, the ElementType (e.g., Polygons), the number of vertices per element, and an optional vertex combining callback.
    5. Access the resulting geometry via tess.Vertices and tess.Elements.

    Note: If you use ElementType.BoundaryContours, tess.Elements will contain ranges in the format [startVertexIndex, vertexCount] instead of direct indices.

    using LibTessDotNet;
    using System;
    using System.Drawing;
    
    // ... (VertexCombine implementation) ...
    
    static void Main(string[] args)
    {
        var inputData = new float[] { 0.0f, 3.0f, -1.0f, 0.0f, 1.6f, 1.9f, -1.6f, 1.9f, 1.0f, 0.0f };
        var tess = new LibTessDotNet.Tess();
    
        int numPoints = inputData.Length / 2;
        var contour = new LibTessDotNet.ContourVertex[numPoints];
        for (int i = 0; i < numPoints; i++)
        {
            contour[i].Position = new LibTessDotNet.Vec3(inputData[i * 2], inputData[i * 2 + 1], 0);
            contour[i].Data = Color.Azure;
        }
    
        tess.AddContour(contour, LibTessDotNet.ContourOrientation.Clockwise);
    
        // Tessellate into triangles (3 vertices per polygon)
        tess.Tessellate(LibTessDotNet.WindingRule.EvenOdd, LibTessDotNet.ElementType.Polygons, 3, VertexCombine);
    
        // Access results
        for (int i = 0; i < tess.ElementCount; i++)
        {
            var v0 = tess.Vertices[tess.Elements[i * 3]].Position;
            // ...
        }
    }
  4. Configure Tessellate output types and winding rules

    master

    The Tessellate method allows you to control how polygons are interpreted and how the output is structured.

    Winding Rules

    Determines how different contours are combined. Supported values include:

    • EvenOdd
    • NonZero
    • Positive
    • Negative
    • |winding| >= 2

    Element Types

    • Polygons: Generates polygons with N vertices (where N is the third parameter of Tessellate). Use 3 to generate triangles.
    • BoundaryContours: Generates only the boundary. When using this, tess.Elements returns ranges of [startVertexIndex, vertexCount] rather than individual indices.