void Start() { createMesh = GetComponent <MeshCreator> (); //add and clear a new meshFilter to the object MeshFilter filter = gameObject.AddComponent <MeshFilter>(); mesh = filter.mesh; mesh.Clear(); //add the four vertices in the position 1 and the four vertices in the position 2 AddStartingPositions(positions[1].position); AddEndingPositions(positions[0].position); //createMesh will return the organized array of vertices and triangles for the first cube, which is stored in vertList and triangleList vertList.AddRange(createMesh.ArrangeVerticesForCube(newVertices)); trianglesList.AddRange(createMesh.ArrangeTrianglesForCube(trianglesList.Count)); //now add to the ending positions of newVertices the four vertices of position 2 AddEndingPositions(positions[2].position); //now vertList and triangleList will receive the organized array of vertices and triangles between positions 1 and 2 vertList.AddRange(createMesh.ArrangeVerticesForCube(newVertices)); trianglesList.AddRange(createMesh.ArrangeTrianglesForCube(trianglesList.Count)); //now add to the beggining positions of newVertices the four vertices of position 3 AddStartingPositions(positions[3].position); //now vertList and triangleList will receive the organized array of vertices and triangles between positions 2 and 3 vertList.AddRange(createMesh.ArrangeVerticesForCube(newVertices)); trianglesList.AddRange(createMesh.ArrangeTrianglesForCube(trianglesList.Count)); //now add to the mesh the list of vertices and triangles, which will result in the extruded mesh between all 4 points mesh.vertices = vertList.ToArray();; mesh.triangles = trianglesList.ToArray(); mesh.RecalculateBounds(); ; }
//This method will create the new mesh and attach it to the original mesh void CreateNewMesh() { List <Vector3> endPosVerts = new List <Vector3> (); //add the original vertices found on the mesh to the endPosVerts list endPosVerts.AddRange(vertPos); //each cube, which are the vertices, will be moved to the desired extrude position and here I get the positions of each of them foreach (GameObject cube in vertCubes) { endPosVerts.Add(cube.transform.position); Destroy(cube); } //finalVertArray will receive the ordered array of new vertices to be added to the mesh Vector3[] finalVertArray = meshCreate.ArrangeVerticesForCube(endPosVerts.ToArray()); //change all new vertices to local space which fixes the positioning for (int i = 0; i < finalVertArray.Length; i++) { finalVertArray[i] = hitTransform.InverseTransformPoint(finalVertArray[i]); } //add the mesh verts and the new vertices to a list, then add it to the mesh endPosVerts.Clear(); endPosVerts.AddRange(mesh.vertices); endPosVerts.AddRange(finalVertArray); mesh.vertices = endPosVerts.ToArray(); //calculate the new triangles and add them to the mesh triangles array List <int> triList = new List <int> (); int[] newTriangles = meshCreate.ArrangeTrianglesForCube(mesh.triangles.Length); triList.AddRange(mesh.triangles); triList.AddRange(newTriangles); mesh.triangles = triList.ToArray(); //reset and destroy some variables vertCubes.Clear(); Destroy(extrudingCube); Destroy(endPos); //remove and add the MeshCollider so the collider is updated with the new mesh formation Destroy(hitTransform.gameObject.GetComponent <MeshCollider> ()); hitTransform.gameObject.AddComponent <MeshCollider>(); }