Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
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
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

89 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Gu::ConvexMesh::loadConvexHull: convex hull init failed error what does this all mean and how do I fix this problem? 0 Answers

Creating mesh from random points 1 Answer

How to fix Unity 5 Mesh Collider Convex Problems? 1 Answer

how would i convert a drawing into a sequence of points then use Delaunay triangulation to make a crafting system and a character creation system? 0 Answers

Convex/Concave mesh collider 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges