Ejemplo n.º 1
0
        public IEnumerator DemoConstantPath()
        {
            ConstantPath constPath = ConstantPath.Construct(end.position, searchLength, null);

            AstarPath.StartPath(constPath);
            lastPath = constPath;
            // Wait for the path to be calculated
            yield return(StartCoroutine(constPath.WaitForPath()));

            ClearPrevious();

            // The following code will build a mesh with a square for each node visited
            List <GraphNode> nodes = constPath.allNodes;

            Mesh mesh = new Mesh();

            List <Vector3> verts = new List <Vector3>();


                        #if FALSE
            // Just some debugging code which selects random points on the nodes
            List <Vector3> pts = Pathfinding.PathUtilities.GetPointsOnNodes(nodes, 20, 0);
            Vector3        avg = Vector3.zero;
            for (int i = 0; i < pts.Count; i++)
            {
                Debug.DrawRay(pts[i], Vector3.up * 5, Color.red, 3);
                avg += pts[i];
            }

            if (pts.Count > 0)
            {
                avg /= pts.Count;
            }

            for (int i = 0; i < pts.Count; i++)
            {
                pts[i] -= avg;
            }

            Pathfinding.PathUtilities.GetPointsAroundPoint(start.position, AstarPath.active.data.graphs[0] as IRaycastableGraph, pts, 0, 1);

            for (int i = 0; i < pts.Count; i++)
            {
                Debug.DrawRay(pts[i], Vector3.up * 5, Color.blue, 3);
            }
                        #endif

            bool drawRaysInstead = false;
            // This will loop through the nodes from furthest away to nearest, not really necessary... but why not :D
            for (int i = nodes.Count - 1; i >= 0; i--)
            {
                Vector3 pos = (Vector3)nodes[i].position + pathOffset;
                if (verts.Count == 65000 && !drawRaysInstead)
                {
                    Debug.LogError("Too many nodes, rendering a mesh would throw 65K vertex error. Using Debug.DrawRay instead for the rest of the nodes");
                    drawRaysInstead = true;
                }

                if (drawRaysInstead)
                {
                    Debug.DrawRay(pos, Vector3.up, Color.blue);
                    continue;
                }

                // Add vertices in a square

                GridGraph gg    = AstarData.GetGraph(nodes[i]) as GridGraph;
                float     scale = 1F;

                if (gg != null)
                {
                    scale = gg.nodeSize;
                }

                verts.Add(pos + new Vector3(-0.5F, 0, -0.5F) * scale);
                verts.Add(pos + new Vector3(0.5F, 0, -0.5F) * scale);
                verts.Add(pos + new Vector3(-0.5F, 0, 0.5F) * scale);
                verts.Add(pos + new Vector3(0.5F, 0, 0.5F) * scale);
            }

            // Build triangles for the squares
            Vector3[] vs   = verts.ToArray();
            int[]     tris = new int[(3 * vs.Length) / 2];
            for (int i = 0, j = 0; i < vs.Length; j += 6, i += 4)
            {
                tris[j + 0] = i;
                tris[j + 1] = i + 1;
                tris[j + 2] = i + 2;

                tris[j + 3] = i + 1;
                tris[j + 4] = i + 3;
                tris[j + 5] = i + 2;
            }

            Vector2[] uv = new Vector2[vs.Length];
            // Set up some basic UV
            for (int i = 0; i < uv.Length; i += 4)
            {
                uv[i]     = new Vector2(0, 0);
                uv[i + 1] = new Vector2(1, 0);
                uv[i + 2] = new Vector2(0, 1);
                uv[i + 3] = new Vector2(1, 1);
            }

            mesh.vertices  = vs;
            mesh.triangles = tris;
            mesh.uv        = uv;
            mesh.RecalculateNormals();

            GameObject go = new GameObject("Mesh", typeof(MeshRenderer), typeof(MeshFilter));
            MeshFilter fi = go.GetComponent <MeshFilter>();
            fi.mesh = mesh;
            MeshRenderer re = go.GetComponent <MeshRenderer>();
            re.material = squareMat;

            lastRender.Add(go);
        }
Ejemplo n.º 2
0
        // Token: 0x06002A44 RID: 10820 RVA: 0x001C7F37 File Offset: 0x001C6137
        public IEnumerator DemoConstantPath()
        {
            ConstantPath constPath = ConstantPath.Construct(this.end.position, this.searchLength, null);

            AstarPath.StartPath(constPath, false);
            this.lastPath = constPath;
            yield return(base.StartCoroutine(constPath.WaitForPath()));

            this.ClearPrevious();
            List <GraphNode> allNodes = constPath.allNodes;
            Mesh             mesh     = new Mesh();
            List <Vector3>   list     = new List <Vector3>();
            bool             flag     = false;

            for (int i = allNodes.Count - 1; i >= 0; i--)
            {
                Vector3 a = (Vector3)allNodes[i].position + this.pathOffset;
                if (list.Count == 65000 && !flag)
                {
                    Debug.LogError("Too many nodes, rendering a mesh would throw 65K vertex error. Using Debug.DrawRay instead for the rest of the nodes");
                    flag = true;
                }
                if (flag)
                {
                    Debug.DrawRay(a, Vector3.up, Color.blue);
                }
                else
                {
                    GridGraph gridGraph = AstarData.GetGraph(allNodes[i]) as GridGraph;
                    float     d         = 1f;
                    if (gridGraph != null)
                    {
                        d = gridGraph.nodeSize;
                    }
                    list.Add(a + new Vector3(-0.5f, 0f, -0.5f) * d);
                    list.Add(a + new Vector3(0.5f, 0f, -0.5f) * d);
                    list.Add(a + new Vector3(-0.5f, 0f, 0.5f) * d);
                    list.Add(a + new Vector3(0.5f, 0f, 0.5f) * d);
                }
            }
            Vector3[] array  = list.ToArray();
            int[]     array2 = new int[3 * array.Length / 2];
            int       j      = 0;
            int       num    = 0;

            while (j < array.Length)
            {
                array2[num]     = j;
                array2[num + 1] = j + 1;
                array2[num + 2] = j + 2;
                array2[num + 3] = j + 1;
                array2[num + 4] = j + 3;
                array2[num + 5] = j + 2;
                num            += 6;
                j += 4;
            }
            Vector2[] array3 = new Vector2[array.Length];
            for (int k = 0; k < array3.Length; k += 4)
            {
                array3[k]     = new Vector2(0f, 0f);
                array3[k + 1] = new Vector2(1f, 0f);
                array3[k + 2] = new Vector2(0f, 1f);
                array3[k + 3] = new Vector2(1f, 1f);
            }
            mesh.vertices  = array;
            mesh.triangles = array2;
            mesh.uv        = array3;
            mesh.RecalculateNormals();
            GameObject gameObject = new GameObject("Mesh", new Type[]
            {
                typeof(MeshRenderer),
                typeof(MeshFilter)
            });

            gameObject.GetComponent <MeshFilter>().mesh       = mesh;
            gameObject.GetComponent <MeshRenderer>().material = this.squareMat;
            this.lastRender.Add(gameObject);
            yield break;
        }