Esempio n. 1
0
    IEnumerator WaitForRequest(WWW www)
    {
        yield return(www);


        //Debug.Log(www.text);


        // check for errors
        if (www.error == null)
        {
            Debug.Log("WWW Ok!: " + www.text);
            graph = MyWrapper.CreateFromJSON(www.text);


            //Debug.Log(graph.nodes.Count);

            nodes = new Dictionary <string, int>();



            for (int i = 0; i < graph.nodes.Count; i++)
            {
                ProcessNode(graph.nodes[i].id, nodes.Count, false);
            }


            for (int i = 0; i < graph.edges.Count; i++)
            {
                if (graph.edges[i].style == "graphmeta")
                {
                    graph.edges.RemoveAt(i);
                    i--;
                }
                else

                {
                    ProcessNode(graph.edges[i].from, nodes.Count, true);
                    ProcessNode(graph.edges[i].to, nodes.Count, true);

                    if (nodes.ContainsKey(graph.edges[i].from) && nodes.ContainsKey(graph.edges[i].to))
                    {
                        MyNode source = graph.nodes[nodes[graph.edges[i].from]];
                        MyNode target = graph.nodes[nodes[graph.edges[i].to]];
                        source.edgeIndicesOut.Add(i);
                        target.edgeIndicesIn.Add(i);

                        source.connectedNodes.Add(nodes[graph.edges[i].to]);
                        target.connectedNodes.Add(nodes[graph.edges[i].from]);
                    }
                }
            }


            identifySubgraphs();
            SolveUsingForces(30, 40f, true);

            List <int> rootIndices    = new List <int>();
            bool[]     visited        = new bool[graph.nodes.Count];
            float      maxConnections = 0;
            for (int i = 0; i < graph.nodes.Count; i++)
            {
                /*
                 * string to = "";
                 * for(int j=0; j< graph.nodes[i].edgeIndicesIn.Count; j++)
                 * {
                 *  to += graph.nodes[nodes[graph.edges[graph.nodes[i].edgeIndicesIn[j]].from]].label+", ";
                 * }
                 * Debug.Log(graph.nodes[i].label + "<-" + to);
                 *
                 * string from = "";
                 * for (int j = 0; j < graph.nodes[i].edgeIndicesOut.Count; j++)
                 * {
                 *  from += graph.nodes[nodes[graph.edges[graph.nodes[i].edgeIndicesOut[j]].to]].label + ", ";
                 * }
                 * Debug.Log(graph.nodes[i].label + "->" + from);
                 *
                 * //*/
                if (graph.nodes[i].edgeIndicesIn.Count == 0)
                {
                    rootIndices.Add(i);
                }

                maxConnections = Mathf.Max(graph.nodes[i].connectedNodes.Count, maxConnections);
            }

            Debug.Log("#root nodes: " + rootIndices.Count);

            float maxHeight = 0;


            for (int i = 0; i < rootIndices.Count; i++)
            {
                for (int j = 0; j < graph.nodes.Count; j++)
                {
                    visited[j] = false;
                }
                int         curHeight      = 1;
                Stack <int> nodeIndexStack = new Stack <int>();
                nodeIndexStack.Push(rootIndices[i]);
                Stack <int> heightStack = new Stack <int>();
                heightStack.Push(curHeight);
                visited[rootIndices[i]] = true;

                while (nodeIndexStack.Count > 0)
                {
                    int curIndex = nodeIndexStack.Pop();
                    curHeight = heightStack.Pop();
                    maxHeight = Mathf.Max(curHeight, maxHeight);
                    MyNode curNode = graph.nodes[curIndex];

                    for (int j = 0; j < curNode.edgeIndicesOut.Count; j++)
                    {
                        int childNodeIndex = nodes[graph.edges[curNode.edgeIndicesOut[j]].to];
                        graph.nodes[childNodeIndex].height = Mathf.Max(curHeight, graph.nodes[childNodeIndex].height);
                        if (!visited[childNodeIndex])
                        {
                            visited[childNodeIndex] = true;
                            nodeIndexStack.Push(childNodeIndex);
                            heightStack.Push(curHeight + 1);
                        }
                    }
                }
            }
            sliceWidth = vol / maxHeight;
            Debug.Log("maxHeight: " + maxHeight);
            ;

            for (int i = 0; i < graph.nodes.Count; i++)
            {
                var node = graph.nodes[i];

                var y = node.height * sliceWidth;

                /*  float x =(maxConnections/10- node.connectedNodes.Count)*20 ;
                 * if (x < -vol) x = 0;
                 *
                 * if (i % 2 == 0) x *= -1;*/

                Vector3 pos = new Vector3(node.pos.x, y, node.pos.z);

                node.pos = pos;
                node.nodeObject.transform.position = pos;
            }



            Vector3[] vertices  = new Vector3[4 * graph.edges.Count];
            int[]     triangles = new int[6 * graph.edges.Count];
            line = new GameObject();
            MeshRenderer mr   = line.AddComponent <MeshRenderer>();
            MeshFilter   mf   = line.AddComponent <MeshFilter>();
            Mesh         mesh = new Mesh();

            for (int i = 0; i < graph.edges.Count; i++)
            {
                if (nodes.ContainsKey(graph.edges[i].from) && nodes.ContainsKey(graph.edges[i].to))
                {
                    MyNode source = graph.nodes[nodes[graph.edges[i].from]];
                    MyNode target = graph.nodes[nodes[graph.edges[i].to]];

                    Vector3 dir = target.pos - source.pos;

                    Vector3 offset = Vector3.Cross(dir, Vector3.up).normalized * 0.5f;

                    vertices[0 + i * 4] = source.pos - offset;
                    vertices[2 + i * 4] = source.pos + offset;
                    vertices[1 + i * 4] = target.pos - offset;
                    vertices[3 + i * 4] = target.pos + offset;

                    triangles[0 + i * 6] = 0 + i * 4;
                    triangles[1 + i * 6] = 1 + i * 4;
                    triangles[2 + i * 6] = 2 + i * 4;
                    triangles[3 + i * 6] = 2 + i * 4;
                    triangles[4 + i * 6] = 3 + i * 4;
                    triangles[5 + i * 6] = 1 + i * 4;
                }
            }

            mesh.vertices  = vertices;
            mesh.triangles = triangles;

            mr.material = mat;
            mf.mesh     = mesh;

            line.transform.parent = this.transform;

            init = true;
        }
        else
        {
            Debug.Log("WWW Error: " + www.error);
        }
    }