// not currently being used due to crashes private void ClusterSplit() { // --- Split the cluster --- /* Theres an issue with cluster lengths > 50 and crashing * clusters are graphically split, but spatially still the same. */ if (cluster_size > 50) { // calculate amount of clusters needed to split float num = cluster_size / 50; int cluster_rounded = Convert.ToInt32(num); // for each cluster needed create and add node length for (int i = 0; i < cluster_rounded; ++i) { // Create new cluster object GameObject cluster = new GameObject(); // setup meshfilters for child objects MeshFilter[] meshFilters = new MeshFilter[50]; // combine instance mesh array CombineInstance[] combine = new CombineInstance[meshFilters.Length]; // Array for new cluster ArrayList new_nodes = new ArrayList(); for (int k = 50 * i; k < k + 50; ++k) { if (k < nodes.Count) { new_nodes.Add(nodes[k]); } } // set the parent of the cluster to visualization object cluster.transform.parent = this.transform; // set the name of the cluster cluster.transform.name = transform.name + " cluster " + i; // add meshfilter to new cluster cluster.AddComponent <MeshFilter>(); cluster.AddComponent <MeshRenderer>(); cluster.GetComponent <MeshFilter>().mesh = new Mesh(); // combine all the meshes in the mesh array "combine" cluster.GetComponent <MeshFilter>().mesh.CombineMeshes(combine); cluster.GetComponent <MeshRenderer>().material = GetComponent <MeshRenderer>().material; // assign random color to cluster cluster.GetComponent <MeshRenderer>().material.color = UnityEngine.Random.ColorHSV(0, 1f, 0.5f, 1f, 0.5f, 1f); // add clustercode cluster.AddComponent <ClusterBehaviour>(); ClusterBehaviour c = cluster.GetComponent <ClusterBehaviour>(); c.setArrayList(new_nodes); // lastly, add collider cluster.AddComponent <MeshCollider>(); } // delete gameobject } }
//Optimisation Test void CombineMeshes() { // go through each clusterNode parent foreach (VisualNode n in clusterNodes) { // arraylist to be added to cluster late ArrayList nodes = new ArrayList(); // setup meshfilters for child objects MeshFilter[] meshFilters = new MeshFilter[n.getClusterNodes().Count + 1]; // combine instance mesh array CombineInstance[] combine = new CombineInstance[meshFilters.Length]; //stop center from being combined nodes_already_clustered.Add(n); // Create a new cluster gameobject GameObject newCluster = new GameObject(); // Color of cluster Color col = UnityEngine.Random.ColorHSV(0.1f, 0.8f, 0.7f, 1f, 0.5f, 1f); // go through each child node and add to meshFilter array // first cluster node GameObject c1 = n.gameObject; meshFilters[0] = c1.GetComponent <MeshFilter>(); combine[0].mesh = meshFilters[0].sharedMesh; combine[0].transform = meshFilters[0].transform.localToWorldMatrix; meshFilters[0].gameObject.SetActive(false); nodes.Add(n); nodes_already_clustered.Add(n); // add boxcollider for each node //BoxCollider b = newCluster.AddComponent<BoxCollider>(); //b.size = new Vector3(0.05f, 0.05f, 0.05f); //b.center = c1.transform.position; //b.isTrigger = true; ArrayList edge = n.getAllocatedEdges(); if (edge.Count > 0) { GameObject index0 = edge[0] as GameObject; foreach (GameObject ga in edge) { LineRenderer l = ga.GetComponent <LineRenderer>(); l.material.SetColor("_TintColor", new Color(col.r, col.g, col.b, 0.01f)); } } int k = 1; foreach (VisualNode v in n.getClusterNodes()) { if (nodes_already_clustered.Contains(v) == false) { GameObject g = v.gameObject; meshFilters[k] = g.GetComponent <MeshFilter>(); combine[k].mesh = meshFilters[k].sharedMesh; combine[k].transform = meshFilters[k].transform.localToWorldMatrix; meshFilters[k].gameObject.SetActive(false); nodes.Add(v); nodes_already_clustered.Add(v); // add boxcollider for each node //BoxCollider b1 = newCluster.AddComponent<BoxCollider>(); //b1.size = new Vector3(0.05f, 0.05f, 0.05f); //b1.center = g.transform.position; //b1.isTrigger = true; v.transform.parent = n.transform; ArrayList edge1 = v.getAllocatedEdges(); if (edge1.Count > 0) { GameObject index0 = edge1[0] as GameObject; foreach (GameObject ga in edge1) { LineRenderer l = ga.GetComponent <LineRenderer>(); l.material.SetColor("_TintColor", new Color(col.r, col.g, col.b, 0.01f)); } } } else { } k++; } // set the parent of the cluster to visualization object newCluster.transform.parent = this.transform; // set the name of the cluster newCluster.transform.name = n.transform.name + " cluster"; // add meshfilter to new cluster newCluster.AddComponent <MeshFilter>(); newCluster.AddComponent <MeshRenderer>(); newCluster.GetComponent <MeshFilter>().mesh = new Mesh(); // combine all the meshes in the mesh array "combine" newCluster.GetComponent <MeshFilter>().mesh.CombineMeshes(combine); newCluster.GetComponent <MeshRenderer>().material = nodeMat; // assign random color to cluster newCluster.GetComponent <MeshRenderer>().material.color = col; // add clustercode newCluster.AddComponent <ClusterBehaviour>(); ClusterBehaviour c = newCluster.GetComponent <ClusterBehaviour>(); n.transform.parent = c.transform; c.setArrayList(nodes); c.setClusterCentre(n.gameObject); // lastly, add collider (trying box colliders) //newCluster.AddComponent<MeshCollider>(); } EdgeManager edgeManager = Camera.main.GetComponent <EdgeManager> (); ArrayList glEdges = edgeManager.getEdges(); // print (glEdges.Count + "glEdge count"); for (int i = 0; i < glEdges.Count; i++) { Edge e = (Edge)glEdges[i]; e.setColor(e.getTransformRef().parent.parent.GetComponent <MeshRenderer>().material.color); } clusterAllNodes = true; graph_layout_recognized = false; Camera.main.cullingMask = cullingmask; }