Esempio n. 1
0
        public RoadLayoutBuilder(RoadGraph roadGraph, MeshFilter meshFilter)
        {
            this.meshFilter = meshFilter;
            meshFilter.mesh = new Mesh();

            foreach (var node in roadGraph.nodes)
            {
                graphNodes.Add(node.nodeId, node);
            }
        }
Esempio n. 2
0
        public static RoadGraph BakeRoadGraph(IEnumerable <RoadGraphBuildNode> buildNodes)
        {
            var bakedNodes     = new List <RoadGraphNode>();
            int _edgeIdCounter = 0;

            foreach (var node in buildNodes)
            {
                var bakedNode = new RoadGraphNode();
                bakedNode.nodeId   = node.nodeId;
                bakedNode.position = node.position;

                var edges = new List <RoadGraphEdge>();
                for (int i = 0; i < node.connectedNodes.Count; i++)
                {
                    var connectedNode = node.connectedNodes[i];
                    var edgeThickness = node.edgeThickness[i];
                    var edge          = new RoadGraphEdge();
                    edge.edgeId      = ++_edgeIdCounter;
                    edge.ownerNodeId = node.nodeId;
                    edge.otherNodeId = connectedNode.nodeId;
                    edge.thickness   = edgeThickness;

                    // Find the dot product with the x axis
                    Vector3 direction = (connectedNode.position - node.position).normalized;
                    var     dot       = Vector3.Dot(new Vector3(1, 0, 0), direction);
                    float   angle     = Mathf.Acos(dot);
                    if (direction.z < 0)
                    {
                        angle = 2 * Mathf.PI - angle;
                    }

                    edge.angleToXAxis = angle;

                    edges.Add(edge);
                }

                bakedNode.adjacentEdges = edges.ToArray();
                System.Array.Sort(bakedNode.adjacentEdges, SortEdgesByAngle);
                bakedNodes.Add(bakedNode);
            }

            var graph = new RoadGraph();

            graph.nodes = bakedNodes.ToArray();
            return(graph);
        }
Esempio n. 3
0
        public void Initialize(RoadGraph graph)
        {
            nodes.Clear();

            // Create the node instances
            foreach (var nodeInfo in graph.nodes)
            {
                var node = new RoadGraphBuildNode(nodeInfo);
                nodes.Add(node.nodeId, node);
                _nodeIdCounter = Mathf.Max(_nodeIdCounter, node.nodeId);
            }

            // Generate the node adjacency list
            foreach (var nodeInfo in graph.nodes)
            {
                var node = nodes[nodeInfo.nodeId];
                foreach (var edge in nodeInfo.adjacentEdges)
                {
                    var adjacentNode = nodes[edge.otherNodeId];
                    node.connectedNodes.Add(adjacentNode);
                }
            }
        }