Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Concave generation #148

Closed
riccardo-runci opened this issue Jun 24, 2024 · 2 comments
Closed

Concave generation #148

riccardo-runci opened this issue Jun 24, 2024 · 2 comments

Comments

@riccardo-runci
Copy link

riccardo-runci commented Jun 24, 2024

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();
@riccardo-runci riccardo-runci changed the title Convex generation Concave generation Jun 24, 2024
@andywiecko
Copy link
Owner

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

@andywiecko
Copy link
Owner

Hi @riccardo-runci,
I've just published version v3.3.0. There is improved validation, which should log a warning if RestoreBoundary is selected and no constraints are provided. I hope that this will help future users to resolve similar issue in the editor.

Best,
Andrzej

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants