GameObject ChooseEdgeToMergeWith() { //choose from adjacent cells List <GameObject> adjacentCells = GetComponent <AdjacentCells>().adjacentCells; // Debug.Log("adjacents = " + adjacentCells.Count); List <GameObject> possibleTargets = new List <GameObject>(); //check if any possible targets have already been merged, as we merge them we drop an disabled merge cells component on them as a bookmark for (int i = 0; i < adjacentCells.Count; i++) { //if no merge cell on cell it means we havent tried to merge it at all // if (adjacentCells[i].GetComponent<MergeCell>() == null) ////////***************possibly need this possibleTargets.Add(adjacentCells[i]); } if (possibleTargets.Count == 0) { Debug.Log("Can't merge"); return(null); } GameObject targetCell = possibleTargets[Random.Range(0, possibleTargets.Count)]; //if allocated in inspector (for tests) if (target != null) { targetCell = target; } targetCell.name = "Target"; targetCell.GetComponent <MeshRenderer>().sharedMaterial = Resources.Load("White") as Material; //add a merge cell component to book mark targetCell.AddComponent <MergeCell>().enabled = false; //find which edge we share with target cell int[] edge = GetComponent <AdjacentCells>().FindSharedEdgeFromTargetCell(targetCell); if (edge == null) { //no suitable edges found, cancel merge Debug.Log("no good edge, stopping merge"); return(null); } //first edge entry is target edge number, second is this - not exactly trivial List <List <int> > edges = GetComponent <AdjacentCells>().edges; List <List <int> > otherEdges = targetCell.GetComponent <AdjacentCells>().edges; List <int> thisEdge = edges[edge[0]]; List <int> otherEdge = otherEdges[edge[1]]; Vector3[] targetVertices = targetCell.GetComponent <MeshFilter>().mesh.vertices; Vector3[] vertices = GetComponent <MeshFilter>().mesh.vertices; Debug.DrawLine(vertices[thisEdge[0]], vertices[thisEdge[1]], Color.cyan); Debug.DrawLine(targetVertices[otherEdge[0]] + Vector3.up, targetVertices[otherEdge[1]] + Vector3.up, Color.white); //organise points from the two cells by angle - probably an eaiser way todo this but I was having problems doing it //centre point is average point between Vector3 centrePoint = Vector3.Lerp(vertices[0], targetVertices[0], 0.5f); List <Vector3[]> cellPoints = new List <Vector3[]>(); cellPoints.Add(vertices); cellPoints.Add(targetVertices); // List<Vector3> ring = OrganiseRingPoints(cellPoints); //List<Vector3> ring = OrganiseRingEdges(); Mesh combinedAndWeldedMesh = WeldedMesh(new List <GameObject>() { gameObject, targetCell }); List <Vector3> ring = OutsidePath(combinedAndWeldedMesh); //add central point to this ring ring.Insert(0, centrePoint); //now add this to mesh, we don't need to bother with triangles- we will create a triangulate street/pavement for it anyway //GetComponent<MeshFilter>().mesh.vertices = ring.ToArray(); List <int> triangles = new List <int>(); //now triangulate cell for (int j = 0; j < ring.Count; j++) { if (j == 0) { continue;//skip centre } // GameObject c = GameObject.CreatePrimitive(PrimitiveType.Cube); // c.transform.position = ring.po if (j < ring.Count - 1) { triangles.Add(j); triangles.Add(j + 1); triangles.Add(0); } else { //finish loop by attaching to the first index triangles.Add(j); triangles.Add(1); triangles.Add(0); } } GameObject mergedCell = new GameObject(); mergedCell.name = "Merged Cell x " + mergedWith.ToString();; //place a disabled merge cell as a marker (we will not merge two merged cells) mergedCell.AddComponent <MergeCell>().enabled = false; mergedCell.transform.parent = transform.parent; MeshFilter mf = mergedCell.AddComponent <MeshFilter>(); mergedCell.AddComponent <MeshRenderer>().sharedMaterial = Resources.Load("Ground") as Material; Mesh mesh = new Mesh(); mesh.vertices = ring.ToArray(); mesh.triangles = triangles.ToArray(); mesh.RecalculateNormals(); mf.mesh = mesh; //remove from main cells list MeshGenerator mg = GameObject.FindGameObjectWithTag("Code").GetComponent <MeshGenerator>(); mg.cells.Remove(gameObject); mg.cells.Remove(targetCell); mg.cells.Add(mergedCell); //add adjacent cells script to new cell// mergedCell.AddComponent <AdjacentCells>().Edges(); /*//doing in build loop for all cells each merge * //redo edges for all adjacents * for (int i = 0; i < adjacentCells.Count; i++) * { * AdjacentCells.CalculateAdjacents(mg.cells, adjacentCells[i], mg.minEdgeSize); * } * * List<GameObject> targetAdjacents = targetCell.GetComponent<AdjacentCells>().adjacentCells; * //and for each on targets * for (int i = 0; i < targetAdjacents.Count; i++) * { * AdjacentCells.CalculateAdjacents(mg.cells, targetAdjacents[i], mg.minEdgeSize); * } * //now we can add adjacents to our new merged cell * AdjacentCells.CalculateAdjacents(mg.cells, mergedCell, mg.minEdgeSize); * */ //if using build list for mergin, remove this - will do in build loop meshGenerator.CalculateAdjacents(); //option for debuggin to keep track of what heppened bool destroyOldCells = false; if (destroyOldCells) { Destroy(gameObject); Destroy(targetCell); } else { //turn renderer off and save to a list which will be passed on to next merged cell we can view in inspector gameObject.GetComponent <MeshRenderer>().enabled = false; targetCell.GetComponent <MeshRenderer>().enabled = false; //gameObject.transform.parent = mergedCell.transform; //targetCell.transform.parent = mergedCell.transform; //positional issues with this //add cell to list if we haven't merged it. Keeping track of all original cells if (gameObject.GetComponent <MergeCell>().mergedWith == 0) { previousCells.Add(gameObject); } if (targetCell.GetComponent <MergeCell>().mergedWith == 0) { previousCells.Add(targetCell); } } // if (canMerge) // mergedCell.AddComponent<MergeCell>(); return(mergedCell); }