Question by 
               DrMowinckel · Nov 28, 2016 at 09:38 PM · 
                convextriangulation  
              
 
              Triangulation only renders one triangle
Hey!
I have run into a problem trying to create a convex hull mesh. My code only renders 1 triangle out of the 3 in the mesh. Anyone have any idea as to why?
 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 public class DataLogger : MonoBehaviour {
 List<GameObject> players;
 Vector2[] playerPositions;
 MeshFilter mf;
 ConvexHull ch;
 // Use this for initialization
 void Start () {
     Debug.Log("DataLogger starting...");
     players = GameObject.Find("Map").GetComponent<GameController>().roles;
     mf = gameObject.GetComponent<MeshFilter>();
     
     //Get all player positions from player array and create Vector2D array from them
     playerPositions = new Vector2[players.Count];                
     playerPositions = GetPlayerPositions(players);
     Debug.Log("Players: " + players.Count);
     foreach (Vector2 playerPos in playerPositions)
     {
         Debug.Log("Player at: " + playerPos.x + "," + playerPos.y);
     }
     
     //Make a new convex hull from the player positions
     ch = new ConvexHull(playerPositions, mf);
     ReverseNormals();
 }
 
 // Update is called once per frame
 void Update () {
     
 }
 Vector2[] GetPlayerPositions(List<GameObject> players)
 {
     Vector2[] playerPositions = new Vector2[players.Count];
     for (int i = 0; i < players.Count; i++)
     {
         playerPositions[i] = new Vector2(players[i].transform.position.x, players[i].transform.position.y);
     }
     return playerPositions;
 }
 void ReverseNormals ()
 {
     MeshFilter filter = GetComponent<MeshFilter>();
     if (filter != null)
     {
         Mesh mesh = filter.mesh;
         Vector3[] normals = mesh.normals;
         for (int i = 0; i < normals.Length; i++)
             normals[i] = -normals[i];
         mesh.normals = normals;
         for (int m = 0; m < mesh.subMeshCount; m++)
         {
             int[] triangles = mesh.GetTriangles(m);
             for (int i = 0; i < triangles.Length; i += 3)
             {
                 int temp = triangles[i + 0];
                 triangles[i + 0] = triangles[i + 1];
                 triangles[i + 1] = temp;
             }
             mesh.SetTriangles(triangles, m);
         }
     }
 }
 public class ConvexHull
 {
     public Vector3[] newVertices;
     public Vector2[] playerPositions;
     public int[] newTriangles;        
     MeshFilter meshFilter;
     public ConvexHull(Vector2[] _playerPositions, MeshFilter _meshFilter)
     {
         //Sets the vertices of the convex hull to the size of the player position array
         newVertices = new Vector3[_playerPositions.Length];
         //Initialise mesh filter in constructor
         meshFilter = _meshFilter;
         //Triangulate between the player positions
         Triangulator tr = new Triangulator(_playerPositions);
         newTriangles = tr.Triangulate();
         //Fill the vertices of the convex hull with Vector3 points made from player positions
         for (int i = 0; i < newVertices.Length; i++)
         {
             newVertices[i] = new Vector3(_playerPositions[i].x, _playerPositions[i].y, -2.0f);
         }
         //Create a new mesh and set its vertices and triangles
         Mesh mesh = new Mesh();
         mesh.vertices = newVertices;
         mesh.triangles = newTriangles;
         mesh.RecalculateNormals();
         mesh.RecalculateBounds();
         mesh.name = "Convex Hull";
         //Sets the meshfilter's mesh to the newly created mesh
         meshFilter.mesh = mesh;
     }
 }
 
               }
               Comment
              
 
               
              Your answer
 
             Follow this Question
Related Questions
Creating mesh from random points 1 Answer
How to fix Unity 5 Mesh Collider Convex Problems? 1 Answer
Convex/Concave mesh collider 2 Answers