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 /
avatar image
0
Question by small502 · Nov 16, 2017 at 03:37 AM · meshverticestrianglesmeshfiltermodeling

Extruding faces of a mold. Need to properly assign vertices to triangles

So I am creating a sort of modelling app using Unity. The user is able to select some vertices (using these vertexHandle GameObjects) and from there, extrude the face surrounded by these vertices.

My process for this currently is:

1.Copy the selected vertices, and clone them a unit above. Add them to the array of vertices that the mesh pulls from

2.Check to see if there are any triangles that every vertex was selected. These triangles would be inside the mesh, and so they are deleted (Though for some reason, this is not working).

3.Iterate through the selected and new vertices, connecting them with triangles (I'm not sure how to properly attempt this at all, though I tried)

Here is my code:

 public class ExtrudeScript : MonoBehaviour {
 public Material defaultMat;

 // Use this for initialization
 void Start () {
     
 }
 
 // Update is called once per frame
 void Update () {
     
 }

 public void ExtrudeFaces() 
 {
     //Find all vertex handles
     GameObject[] vertexHandles = GameObject.FindGameObjectsWithTag("VertexHandle");

     //List of Vectors to store the current vertices
     List<GameObject> vertList = new List<GameObject>();

     //Sort through all of them and gets selected vertices
     for (int i = 0; i < vertexHandles.Length; i++)                                      
     {
         //Gets the ObjectVertex script for each and checks if is selected
         ObjectVertex ov = vertexHandles[i].GetComponent<ObjectVertex>();                
         if (ov.isSelected)                                                            
         {
             vertList.Add(vertexHandles[i]);
         }
     }


     //Creates a list of vectors from the vertices
     List<Vector3> vectorList = new List<Vector3>();
     GameObject mesh = GameObject.Find("Object");
     Mesh meshData = mesh.GetComponent<MeshFilter>().mesh;       //Gets the object's mesh filter
     Vector3[] vertEdit = meshData.vertices;                        //Grabs the list of the object's vertices
     List<Vector3> meshVertList = vertEdit.ToList();             //Creates list of all vertices
     List<int> triList = meshData.triangles.ToList();            //Creates list of all triangles
     List<int> connectList = new List<int>();                    //List to store nums of old and new vert array positions


     int newVertCount = meshVertList.Count;
     for (int i = 0; i < vertList.Count; i++) 
     {
         //Creates list of new vertice vectors, vertexNum is the vertex's assigned num in the mesh's array of vertices
         vectorList.Add(vertEdit[vertList[i].GetComponent<ObjectVertex>().vertexNum]);
         vectorList[i] += (Vector3.up);

         //Add new vertices to mesh
         meshVertList.Add(vectorList[i]);

         //Adds the new and connected verts to an array
         newVertCount++;
         connectList.Add(vertList[i].GetComponent<ObjectVertex>().vertexNum);
         connectList.Add(newVertCount);
     }

     bool vertA = false;
     bool vertB = false;
     bool vertC = false;

     //Delete covered triangles
     //Cycles through triangles
     for (int i = 2; i < triList.Count; i += 3) 
     {
         //Cycles through selection, checking if the vertice is in it
         for (int j = 0; j < vertList.Count; j++) 
         {
             if (i == vertList[j].GetComponent<ObjectVertex>().vertexNum)
             {
                 vertA = true;
             }

             if ((i - 1) == vertList[j].GetComponent<ObjectVertex>().vertexNum) 
             {
                 vertB = true;
             }

             if ((i - 2) == vertList[j].GetComponent<ObjectVertex>().vertexNum) 
             {
                 vertC = true;
             }
         }

         //If all vertices were in the selection, delete the triangle
         if ((vertA) && (vertB) && (vertC)) 
         {
             triList.Remove(i);
             triList.Remove(i-1);
             triList.Remove(i-2);

             i -= 3;
         }

         vertA = false;
         vertB = false;
         vertC = false;
     }

     //Attach new faces with triangles
     for (int i = 0; i < vertList.Count; i++) 
     {
         triList.Add(connectList[i]);
         triList.Add(connectList[i + 1]);
         triList.Add(connectList[i + vertList.Count - 1]);
     }

     //Refresh Mesh
     meshData.vertices = meshVertList.ToArray();                                     //Assigns the vertices
     meshData.triangles = triList.ToArray();                                         //Assigns the triangles
     meshData.RecalculateBounds();                                                   //Recalculates the object size
     meshData.RecalculateNormals();                                                  //Recalculates the object normals

     mesh.GetComponent<Renderer>().material = defaultMat;                            //Sets mat to the default mat
 }
 }

I know that number 2 is on the cusp of working, and there's probably something small that I'm missing that's preventing the triangles from being removed. But I can't figure out part three at all. Does anyone know what to do?

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

79 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

Related Questions

Generated Mesh Triangles not Being Made/Visible? 1 Answer

Save only visible part of mesh 1 Answer

Connecting flatshaded vertices 0 Answers

Does mesh.triangles also return a copy of the triangle array? 1 Answer

Setting triangles failing 0 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