- Home /
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?
Your answer
Follow this Question
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