andywiecko / BurstTriangulator

2d Delaunay triangulation with mesh refinement for Unity with Burst compiler
https://andywiecko.github.io/BurstTriangulator/
MIT License
166 stars 14 forks source link

Concave generation #148

Open riccardo-runci opened 4 days ago

riccardo-runci commented 4 days ago

Hi, i'm using this awesome library to generate mesh at runtime. Basically i have a list of vector3 (points) that i use to generate the mesh, it work perfectly but when i generate a complex mesh i get a convex mesh, what i need is a mesh with some concave parts.

This is a screenshot of what i have:

https://imgur.com/a/xtv2Mu5 the red box are my points in the world and the "white" mesh are what i get with Burst Triangulator,

this is what i'm trying to achieve https://imgur.com/a/zTGwuZq

same screenshot but i need to remove the "yellow" parts

and this is the source code:

Thanks for this awesome library


//Vector3[] points => points from the world (red boxes)
var go = new GameObject("Surface");
var renderer = go.AddComponent<MeshRenderer>();
go.AddComponent<MeshFilter>();
renderer.material = new Material(Shader.Find("Universal Render Pipeline/Lit"));
var positions = new NativeArray<float2>(points.Length, Allocator.Persistent);
for (int i = 0; i < points.Length; i++)
{
    positions[i] = new float2(points[i].x, points[i].z); // Usa x e z per 2D
}

using (var triangulator = new Triangulator(Allocator.Persistent){
    Settings = {
        RestoreBoundary = true,
    }
})
{

    triangulator.Input.Positions = positions;
    triangulator.Run();

    var triangles = triangulator.Output.Triangles;

    // Crea una nuova mesh
    Mesh mesh = new Mesh();
    Vector3[] meshVertices = new Vector3[points.Length];
    int[] meshTriangles = new int[triangles.Length];

    // Aggiungi i vertici alla mesh
    for (int i = 0; i < points.Length; i++)
    {
        meshVertices[i] = points[i];
    }

    // Aggiungi i triangoli alla mesh
    for (int i = 0; i < triangles.Length; i++)
    {
        meshTriangles[i] = triangles[i];
    }

    mesh.vertices = meshVertices;
    mesh.triangles = meshTriangles;
    mesh.RecalculateNormals();

    go.GetComponent<MeshFilter>().mesh = mesh;
}
positions.Dispose();
andywiecko commented 4 days ago

Hi @riccardo-runci

To enable RestoringBoundary properly, you need to provide ConstraintEdges:

using var constraintEdges = new NativeArray<int>(..., Allocator.Persistent);
using var positions = new NativeArray<float2>(..., Allocator.Persistent);
using var triangulator = new Triangulator(Allocator.Persistent)
{
  Input = { 
    Positions = positions,
    ConstraintEdges = constraintEdges,
  },
  Settings = {
    RestoreBoundary = true,
  }
};

triangulator.Run();

var triangles = triangulator.Output.Triangles;

I guess in your case constraints will have the form $[0, 1, 1, 2, 2, 3, ...]$.

See manual for more details.

Many thanks for the contribution! I guess I have to add additional Exception to better inform users about usage (reported on project board).

Best, Andrzej