LibTessDotNet
repository·master·Indexed 18 days ago
https://github.com/speps/libtessdotnetA .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.
What's inside LibTessDotNet
- 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.
Handle custom vertex attributes with a combining callback
masterWhen 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
Tessellatemethod.Signature pattern:
object VertexCombine(Vec3 position, object[] data, float[] weights)position: The position of the new vertex.data: An array of theDataobjects 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
Coloror a newUVstruct) to be assigned to the new vertex'sDataproperty.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]); }Tessellate polygons with LibTessDotNet
masterLibTessDotNet 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:
- Create an instance of
LibTessDotNet.Tess. - Define your vertices using
ContourVertexarrays. Each vertex requires aPosition(usingVec3). You can optionally attach custom data to theDataproperty. - Add contours to the tessellator using
AddContour. You can specify the orientation (e.g.,Clockwise,CounterClockwise, orOriginal). - Call
Tessellatespecifying theWindingRule, theElementType(e.g.,Polygons), the number of vertices per element, and an optional vertex combining callback. - Access the resulting geometry via
tess.Verticesandtess.Elements.
Note: If you use
ElementType.BoundaryContours,tess.Elementswill 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; // ... } }- Create an instance of
Requirements for LibTessDotNet
masterTo use LibTessDotNet, your project must target:
.NET Standard 2.0
Configure Tessellate output types and winding rules
masterThe
Tessellatemethod 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:
EvenOddNonZeroPositiveNegative|winding| >= 2
Element Types
Polygons: Generates polygons with N vertices (where N is the third parameter ofTessellate). Use3to generate triangles.BoundaryContours: Generates only the boundary. When using this,tess.Elementsreturns ranges of[startVertexIndex, vertexCount]rather than individual indices.