Ejemplo n.º 1
0
    void OnDrawGizmos()
    {
        if (m_mat == null)
        {
            m_mat = new Material(Shader.Find("Unlit/Color"));
        }

        List <List <IntPoint> > walkablePolygons = new List <List <IntPoint> >()
        {
            new List <IntPoint>()
            {
                new IntPoint(10, 10),
                new IntPoint(600, 10),
                new IntPoint(600, 400),
                new IntPoint(10, 400),
            }
        };

        List <List <IntPoint> > blockedPolygons = new List <List <IntPoint> >()
        {
            new List <IntPoint>()
            {
                new IntPoint(100, 160),
                new IntPoint(150, 160),
                new IntPoint(200, 200),
                new IntPoint(130, 300),
            },
            new List <IntPoint>()
            {
                new IntPoint(220, 50),
                new IntPoint(260, 50),
                new IntPoint(260, 100),
                new IntPoint(220, 100),
            },
            new List <IntPoint>()
            {
                new IntPoint(300, 150),
                new IntPoint(350, 150),
                new IntPoint(350, 200),
                new IntPoint(300, 200),
            },
            new List <IntPoint>()
            {
                new IntPoint(400, 250),
                new IntPoint(450, 250),
                new IntPoint(450, 300),
                new IntPoint(400, 300),
            },
        };

        List <DelaunayTriangle> triangles;
        var nodes = NavmeshGenerator.Generate(walkablePolygons, blockedPolygons, out triangles);

        //设置起点和终点
        DelaunayTriangle start = null, end = null;
        GraphNode        startNode = null, endNode = null;
        float            minX = float.MaxValue, maxX = 0;

        for (int i = 0; i < triangles.Count; i++)
        {
            var t        = triangles[i];
            var centroid = t.Centroid();
            if (centroid.Xf < minX)
            {
                minX      = centroid.Xf;
                start     = t;
                startNode = nodes[i];
            }
            if (centroid.Xf > maxX)
            {
                maxX    = centroid.Xf;
                end     = t;
                endNode = nodes[i];
            }

            //鼠标所在三角形为终点
            if (IsTriangleContains(t, Input.mousePosition))
            {
                maxX    = float.MaxValue;
                end     = t;
                endNode = nodes[i];
            }
        }

        GraphAStar algo = new GraphAStar(startNode, endNode);

        algo.Process();

        m_mat.SetColor("_Color", Color.black);
        for (int i = 0; i < blockedPolygons.Count; i++)
        {
            DrawPolygon(blockedPolygons[i], true, true);
        }

        m_mat.SetColor("_Color", Color.white);
        for (int i = 0; i < triangles.Count; i++)
        {
            DrawTriangle(triangles[i], false);
        }

        m_mat.SetColor("_Color", Color.yellow);
        DrawTriangle(start, false);
        DrawTriangle(end, false);

        List <Vector2> path = new List <Vector2>();
        var            node = endNode;

        while (node != null)
        {
            path.Add(node.Center);
            node = node.Parent;
        }

        m_mat.SetColor("_Color", Color.red);
        GraphicsTool.DrawPolygon(path, m_mat, false);
    }
Ejemplo n.º 2
0
    void OnDrawGizmos()
    {
        if (m_mat == null)
        {
            m_mat = new Material(Shader.Find("Unlit/Color"));
        }

        m_mat.SetColor("_Color", Color.black);
        List <Polygon> obstacles = new List <Polygon>()
        {
            new Polygon(new Vector2[]
            {
                new Vector2(100, 50),
                new Vector2(100, 100),
                new Vector2(180, 100),
                new Vector2(180, 50),
            }),
            new Polygon(new Vector2[]
            {
                new Vector2(250, 120),
                new Vector2(230, 140),
                new Vector2(250, 160),
                new Vector2(290, 150),
            }),
            new Polygon(new Vector2[]
            {
                new Vector2(90, 130),
                new Vector2(70, 150),
                new Vector2(110, 150),
            }),
            new Polygon(new Vector2[]
            {
                new Vector2(150, 200),
                new Vector2(150, 250),
                new Vector2(280, 250),
                new Vector2(280, 200),
            }),
        };

        for (int i = 0; i < obstacles.Count; i++)
        {
            GraphicsTool.DrawPolygon(obstacles[i].Points, m_mat, true, true);
        }

        m_mat.SetColor("_Color", Color.yellow);
        List <GraphNode> nodes = VisibilityGraphGenerator.Generate(m_start, m_end, obstacles);

        for (int a = 0; a < nodes.Count; a++)
        {
            var n = nodes[a];
            GraphicsTool.DrawPoint(nodes[a].Center, 3, m_mat);
            for (int i = 0; i < n.Neighbors.Count; i++)
            {
                GraphicsTool.DrawLine(n.Center, n.Neighbors[i].Center, m_mat);
            }
        }

        m_mat.SetColor("_Color", Color.red);
        GraphicsTool.DrawPoint(m_start, 4, m_mat);
        GraphicsTool.DrawPoint(m_end, 4, m_mat);
        GraphNode  startNode = nodes[nodes.Count - 2];
        GraphNode  endNode   = nodes[nodes.Count - 1];
        GraphAStar astar     = new GraphAStar(startNode, endNode);

        astar.Process();
        List <Vector2> path = new List <Vector2>();

        while (endNode != null)
        {
            path.Add(endNode.Center);
            endNode = endNode.Parent;
        }
        GraphicsTool.DrawPolygon(path, m_mat, false);
    }
Ejemplo n.º 3
0
 void Start()
 {
     instance = this;
 }