Exemplo n.º 1
0
        /** Get the path back */
        public void OnPathComplete (Path p) {
            //To prevent it from creating new GameObjects when the application is quitting when using multithreading.
            if(lastRender == null) return;
		
            if (p.error) {
                ClearPrevious ();
                return;
            }
		
		
            if (p.GetType () == typeof (MultiTargetPath)) {
			
                List<GameObject> unused = new List<GameObject> (lastRender);
                lastRender.Clear ();
			
                MultiTargetPath mp = p as MultiTargetPath;
			
                for (int i=0;i<mp.vectorPaths.Length;i++) {
                    if (mp.vectorPaths[i] == null) continue;
				
                    List<Vector3> vpath = mp.vectorPaths[i];
				
                    GameObject ob = null;
                    if (unused.Count > i && unused[i].GetComponent<LineRenderer>() != null) {
                        ob = unused[i];
                        unused.RemoveAt (i);
                    } else {
                        ob = new GameObject ("LineRenderer_"+i,typeof(LineRenderer));
                    }
				
                    LineRenderer lr = ob.GetComponent<LineRenderer>();
                    lr.sharedMaterial = lineMat;
                    lr.SetWidth (lineWidth,lineWidth);
				
                    lr.SetVertexCount (vpath.Count);
                    for (int j=0;j<vpath.Count;j++) {
                        lr.SetPosition (j,vpath[j] + pathOffset);
                    }
				
                    lastRender.Add (ob);
                }
			
                for (int i=0;i<unused.Count;i++) {
                    Destroy (unused[i]);
                }
			
            } else if (p.GetType () == typeof (ConstantPath)) {
			
                ClearPrevious ();
                //The following code will build a mesh with a square for each node visited
			
                ConstantPath constPath = p as ConstantPath;
                List<GraphNode> nodes = constPath.allNodes;
			
                Mesh mesh = new Mesh ();

                List<Vector3> verts = new List<Vector3>();
			
                bool drawRaysInstead = 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.astarData.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);
			}*/
			
                //This will loop through the nodes from furthest away to nearest, not really necessary... but why not :D
                //Note that the reverse does not, as common sense would suggest, loop through from the closest to the furthest away
                //since is might contain duplicates and only the node duplicate placed at the highest index is guarenteed to be ordered correctly.
                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;
                    }
				
                    //Enqueue 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);
			
            } else {
			
                ClearPrevious ();
			
                GameObject ob = new GameObject ("LineRenderer",typeof(LineRenderer));
                LineRenderer lr = ob.GetComponent<LineRenderer>();
                lr.sharedMaterial = lineMat;
                lr.SetWidth (lineWidth,lineWidth);
			
                lr.SetVertexCount (p.vectorPath.Count);
                for (int i=0;i<p.vectorPath.Count;i++) {
                    lr.SetPosition (i,p.vectorPath[i] + pathOffset);
                }
			
                lastRender.Add (ob);
            }
        }