Help me use Triangulator (from wiki) with javascript

So I’m trying to build a mesh with code and generate triangles. I’ve successfully made tris manually, but I’m hoping to utilize this Triangulator script to generate tris for more complex concave/convex shapes.

http://www.unifycommunity.com/wiki/index.php?title=Triangulator Here’s the triangulator code in C# – I’m very unfamiliar with C# so this is pretty confusing to me.

Here’s a thread that has helped me so far http://forum.unity3d.com/threads/27346-JavaScript.Array-gt-C-.List-and-constructor?highlight=constructor+triangulator

Closest I’ve gotten is to have this declared at the top (have the script set in inspector):
var triScript : Triangulator;

And this when I start building the mesh

temp = new Triangulator(vertices2D.ToBuiltin(Vector2)); //problem line
temp.Triangulate();

But I get the warning “The class defined in teh script is not derived from MonoBehavior or ScriptableObject!”" and I don’t get any tris.

I barely understand what that line is doing anyway – it seems like it’s sending the Triangulator script the vertices2D array (converted to system array), but where does the Triangulator receive/store it and how would I then receive the result in the js script after doing temp.Triangulate()?

Any help is greatly appreciated :slight_smile:

That page includes a complete example of usage too, and the conversion from C# to UnityScript is as simple as changing the variable declarations (int i → var i : int) and array initialization, and the need to convert the UnityScript array into a built-in array (which is what you’re doing with the ToBuiltin() call you’re already using).

I’m not sure why you’re declaring that variable, since you don’t seem to be using it; and no idea what you mean ‘have the script set in inspector’. Neither is needed.

You don’t ‘get any tris’ because the triangle data is what’s returned from temp.Triangulate(), and you’re ignoring that return value. Notice in the example usage script that that’s assigned to a variable which is then passed into the new mesh later on?

What script is giving that warning? Are you trying to drag the Triangulator script (which is not, and should not be, a MonoBehaviour and can’t be used like that)?

Yes, that line is sending the vertices into the triangulator. It doesn’t matter at all how the triangulator stores it; you get the result by saving the return value of Triangulate() into a variable, just as in the example.

I haven’t tested this at all, it’s a direct translation into UnityScript of the example code from the wiki, but it should at least get you closer:

function Start () {
    // Create Vector2 vertices
    //Vector2[] vertices2D : = new Vector2[] {
    var vertices2Darray : Array = Array(
        new Vector2(0,0),
        new Vector2(0,50),
        new Vector2(50,50),
        new Vector2(50,100),
        new Vector2(0,100),
        new Vector2(0,150),
        new Vector2(150,150),
        new Vector2(150,100),
        new Vector2(100,100),
        new Vector2(100,50),
        new Vector2(150,50),
        new Vector2(150,0)
    );
    var vertices2D : Vector2[] = vertices2Darray.ToBuiltin(Vector3);

    // Use the triangulator to get indices for creating triangles
    //Triangulator tr = new Triangulator(vertices2D);
    //int[] indices = tr.Triangulate();
    var tr : Triangulator = new Triangulator(vertices2D);
    var indices : int[] = tr.Triangulate();

    // Create the Vector3 vertices
    //Vector3[] vertices = new Vector3[vertices2D.Length];
    var vertices : Vector3[] = new Vector3[vertices2D.Length];
    for (var i : int = 0; i<vertices.Length; i++) {
        vertices[i] = new Vector3(vertices2D[i].x, vertices2D[i].y, 0);
    }
   
    // Create the mesh
    var msh : Mesh = new Mesh();
    msh.vertices = vertices;
    msh.triangles = indices;
    msh.RecalculateNormals();
    msh.RecalculateBounds();
   
    // Set up game object with mesh;
    gameObject.AddComponent(typeof(MeshRenderer));
    var filter : MeshFilter = gameObject.AddComponent(typeof(MeshFilter)) as MeshFilter;
    filter.mesh = msh;
}

Got it working – typos sent me on a wild goose chase. Only issue now is that the normals are flipped. Wiki page mentions “you can [fix] that by constructing your mesh with the vertex indices in reverse order.” but when I attempt to do that, using .Reverse() before setting the mesh verts, it breaks the triangulation

edit: unifycommunity.com this worked.

Thanks for the help

You didn’t describe how, exactly, the triangulation got broken, but I’d have to guess you reversed the wrong array; you need to reverse ‘indices’, not ‘vertices’. At least you figured out a way to get it working though.