コード例 #1
0
ファイル: RoadGraphBuilder.cs プロジェクト: kabirules/Kenny
        public void CreateCircle(Vector3 center, float radius, float thickness)
        {
            float circumference = 2 * Mathf.PI * radius;

            if (settings.interNodeDistance <= 0)
            {
                settings.interNodeDistance = 10;
            }
            int numSegments = Mathf.RoundToInt(circumference / settings.interNodeDistance);

            RoadGraphBuildNode firstNode    = null;
            RoadGraphBuildNode previousNode = null;

            for (int i = 0; i < numSegments; i++)
            {
                float angle  = i / (float)numSegments * (2 * Mathf.PI);
                var   offset = Vector3.zero;
                offset.x = Mathf.Cos(angle) * radius;
                offset.z = Mathf.Sin(angle) * radius;
                var position = center + offset;
                var node     = CreateNode(position);
                if (i == 0)
                {
                    firstNode = node;
                }
                else
                {
                    ConnectNodes(previousNode, node, thickness);
                }
                previousNode = node;
            }

            // Connect the last node with the first node
            ConnectNodes(previousNode, firstNode, thickness);
        }
コード例 #2
0
ファイル: RoadGraphBuilder.cs プロジェクト: kabirules/Kenny
        public RoadGraphBuildNode CreateNode(Vector3 position)
        {
            int nodeId = ++_nodeIdCounter;
            var node   = new RoadGraphBuildNode(nodeId, position);

            nodes.Add(node.nodeId, node);
            return(node);
        }
コード例 #3
0
ファイル: RoadGraphBuilder.cs プロジェクト: kabirules/Kenny
        public void ConnectNodes(RoadGraphBuildNode a, RoadGraphBuildNode b, float thickness)
        {
            if (!a.connectedNodes.Contains(b))
            {
                a.connectedNodes.Add(b);
                a.edgeThickness.Add(thickness);
            }

            if (!b.connectedNodes.Contains(a))
            {
                b.connectedNodes.Add(a);
                b.edgeThickness.Add(thickness);
            }
        }
コード例 #4
0
ファイル: RoadGraphBuilder.cs プロジェクト: kabirules/Kenny
        RoadGraphBuildNode FindNearestNode(Vector3 position, float searchRadius)
        {
            // TODO: Optimize me with a spatial grid
            RoadGraphBuildNode bestMatch = null;
            float bestDistanceSq         = float.MaxValue;

            foreach (var node in nodes.Values)
            {
                var distanceSq = (node.position - position).sqrMagnitude;
                if (distanceSq < bestDistanceSq)
                {
                    bestMatch      = node;
                    bestDistanceSq = distanceSq;
                }
            }

            return(bestMatch);
        }
コード例 #5
0
ファイル: RoadGraphBuilder.cs プロジェクト: kabirules/Kenny
        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);
                }
            }
        }
コード例 #6
0
        public RoadGraph BakeLayoutGraph()
        {
            var vertices = new List <Vector3>();
            var uvs      = new List <Vector2>();

            var graphBuilder = new RoadGraphBuilder();
            var edgeVisited  = new HashSet <int>();

            foreach (var node in graphNodes.Values)
            {
                foreach (var edge in node.adjacentEdges)
                {
                    if (!edgeVisited.Contains(edge.edgeId))
                    {
                        var blockEdges = TraverseEdgeBlock(edge, edgeVisited);
                        var boundary   = GenerateBlockBoundary(blockEdges);

                        if (RoadBlockLayoutBuilt != null)
                        {
                            RoadBlockLayoutBuilt(ref boundary);
                        }

                        GenerateBoundaryMesh(blockEdges, boundary, vertices, uvs);

                        var boundaryBuildNodes = new List <RoadGraphBuildNode>();
                        foreach (var point in boundary)
                        {
                            var buildNode = graphBuilder.CreateNode(point);
                            boundaryBuildNodes.Add(buildNode);
                        }

                        // Connect the build nodes
                        for (int i = 0; i < boundaryBuildNodes.Count; i++)
                        {
                            RoadGraphBuildNode start = boundaryBuildNodes[i];
                            RoadGraphBuildNode end   = boundaryBuildNodes[(i + 1) % boundaryBuildNodes.Count];
                            graphBuilder.ConnectNodes(start, end, 0);
                        }
                    }
                }
            }

            //var vertexMap = new Dictionary<Vector3, int>();
            //var vertexBuffer = new List<Vector3>();
            //var uvBuffer = new List<Vector2>();
            var indexBuffer = new List <int>();

            // Populate the vertex buffer
            for (int i = 0; i < vertices.Count; i++)
            {
                indexBuffer.Add(i);
            }

            var mesh = meshFilter.sharedMesh;

            mesh.vertices  = vertices.ToArray();
            mesh.uv        = uvs.ToArray();
            mesh.triangles = indexBuffer.ToArray();
            mesh.RecalculateNormals();
            mesh.RecalculateBounds();

            return(graphBuilder.BakeRoadGraph());
        }