示例#1
0
        void mohogonyGrid()         // this is the optimized version. Other graphs cannot be optimised this way because they have irregular structures unfortunately. Still, we shouldn't miss on the advantages of this specific graph type, so I included it.
        {
            nodes = new List <Node>();
            nodes.Add(node as Node);
            graph = AstarPath.active.graphs[node.graphIndex] as GridGraph;


            GraphUpdateShape gus = new GraphUpdateShape();

            if (useRealCollider.Value)
            {
                if (go.collider.GetType() == typeof(BoxCollider))                // take render bounds, then turn them into world coordinates
                {
                    Debug.Log("It's a box collider");
                    bounds.center = (go.collider as BoxCollider).center;
                    bounds.size   = (go.collider as BoxCollider).size;

                    calculateBox(gus);
                }
                else if (go.collider.GetType() == typeof(MeshCollider))
                {
                    gus.points = (go.collider as MeshCollider).sharedMesh.vertices;
                    for (var i = 0; i < gus.points.Count(); i++)
                    {
                        gus.points[i] = go.transform.TransformPoint((go.collider as MeshCollider).sharedMesh.vertices[i]);
                    }
                    Debug.Log("It's a mesh collider!");
                }
                else                 // any other collider
                {
                    calculateBox(gus);
                    Debug.Log("This type of collider is not specifically supported. Using bounds instead...");
                }
            }

            else             // get the points of the render bounds
            {
                bounds = (go.renderer.GetComponent(typeof(MeshFilter)) as MeshFilter).sharedMesh.bounds;
                calculateBox(gus);
            }

            gus.convex = true;
            nodes      = graph.GetNodesInArea(gus);


            foreach (Node o in nodes)
            {
                o.walkable = walkability.Value;
            }

            Debug.Log("i" + nodes.Count);


            return;
        }
示例#2
0
 void calculateBox(GraphUpdateShape gus)
 {
     gus.points    = new Vector3[8];
     gus.points[0] = go.transform.TransformPoint(new Vector3(bounds.min.x, bounds.min.y, bounds.max.z));
     gus.points[1] = go.transform.TransformPoint(new Vector3(bounds.max.x, bounds.min.y, bounds.max.z));
     gus.points[2] = go.transform.TransformPoint(new Vector3(bounds.max.x, bounds.min.y, bounds.min.z));
     gus.points[3] = go.transform.TransformPoint(new Vector3(bounds.min.x, bounds.min.y, bounds.min.z));
     gus.points[7] = go.transform.TransformPoint(new Vector3(bounds.min.x, bounds.max.y, bounds.min.z));
     gus.points[4] = go.transform.TransformPoint(new Vector3(bounds.min.x, bounds.max.y, bounds.max.z));
     gus.points[5] = go.transform.TransformPoint(new Vector3(bounds.max.x, bounds.max.y, bounds.max.z));
     gus.points[6] = go.transform.TransformPoint(new Vector3(bounds.max.x, bounds.max.y, bounds.min.z));
 }
        public static void GetGUO(this GraphUpdateScene gus, ref GraphUpdateObject guo)
        {
            if (gus.points == null || gus.points.Length == 0)
            {
                var polygonCollider = gus.GetComponent <PolygonCollider2D>();
                if (polygonCollider != null)
                {
                    var       points2D = polygonCollider.points;
                    Vector3[] pts      = new Vector3[points2D.Length];
                    for (int i = 0; i < pts.Length; i++)
                    {
                        var p = points2D[i] + polygonCollider.offset;
                        pts[i] = new Vector3(p.x, 0, p.y);
                    }

                    var mat   = gus.transform.localToWorldMatrix * Matrix4x4.TRS(Vector3.zero, Quaternion.Euler(-90, 0, 0), Vector3.one);
                    var shape = new GraphUpdateShape(gus.points, gus.convex, mat, gus.minBoundsHeight);
                    if (guo == null)
                    {
                        guo = new GraphUpdateObject();
                    }
                    guo.bounds = shape.GetBounds();
                    guo.shape  = shape;
                }
                else
                {
                    var bounds = gus.GetBounds();
                    if (bounds.center == Vector3.zero && bounds.size == Vector3.zero)
                    {
                        Debug.LogError("Cannot apply GraphUpdateScene, no points defined and no renderer or collider attached", gus);
                    }

                    if (guo == null)
                    {
                        guo = new GraphUpdateObject(bounds);
                    }
                    else
                    {
                        guo.bounds = bounds;
                    }
                    guo.shape = null;
                }
            }
            else
            {
                GraphUpdateShape shape;

                // Used for compatibility with older versions
                var worldPoints = new Vector3[gus.points.Length];
                for (int i = 0; i < gus.points.Length; i++)
                {
                    worldPoints[i] = gus.transform.TransformPoint(gus.points[i]);
                }
                shape = new GraphUpdateShape(worldPoints, gus.convex, Matrix4x4.identity, gus.minBoundsHeight);

                if (guo == null)
                {
                    guo = new GraphUpdateObject();
                }
                guo.bounds = shape.GetBounds();
                guo.shape  = shape;
            }

            guo.nnConstraint          = NNConstraint.None;
            guo.modifyWalkability     = gus.modifyWalkability;
            guo.setWalkability        = gus.setWalkability;
            guo.addPenalty            = gus.penaltyDelta;
            guo.updatePhysics         = gus.updatePhysics;
            guo.updateErosion         = gus.updateErosion;
            guo.resetPenaltyOnPhysics = gus.resetPenaltyOnPhysics;

            guo.modifyTag = gus.modifyTag;
            guo.setTag    = gus.setTag;
        }