public IEnumerator DisplayWalkPath() { //change layer an update graphs so the current space does not count this.gameObject.layer = LayerMask.NameToLayer("SelectedUnit"); AstarPath.active.UpdateGraphs(new GraphUpdateObject(this.GetComponent <BoxCollider2D>().bounds)); // find path ConstantPath constPath = ConstantPath.Construct(this.transform.position, (this.Movement.GetDistance() + 1) * 1000, null); this.WalkSeeker.StartPath(constPath); yield return(StartCoroutine(constPath.WaitForPath())); // display tiles List <Vector3> walkTilePositions = new List <Vector3>(); List <GraphNode> nodes = constPath.allNodes; for (int i = nodes.Count - 1; i >= 0; i--) { GraphNode node = nodes[i]; Vector3 position = (Vector3)node.position; position.x = Mathf.Round(position.x); position.y = Mathf.Round(position.y); position.z = 0; this.walkTiles.Add(position, Instantiate(this.walkTile, position, Quaternion.identity, this.transform)); } StartCoroutine(this.DisplayAttackPath()); }
/** Starts a path specified by PathTypesDemo::activeDemo */ public void DemoPath() { Path p = null; if (activeDemo == 0) { p = new Path (start.position,end.position, OnPathComplete); } else if (activeDemo == 1) { MultiTargetPath mp = new MultiTargetPath (multipoints.ToArray (), end.position, null, OnPathComplete); p = mp; } else if (activeDemo == 2) { RandomPath rp = new RandomPath (start.position,searchLength, OnPathComplete); rp.spread = spread; rp.aimStrength = aimStrength; rp.aim = end.position; rp.replaceChance = replaceChance; p = rp; } else if (activeDemo == 3) { FleePath fp = new FleePath (start.position, end.position, searchLength, OnPathComplete); fp.fleeStrength = aimStrength; fp.replaceChance = replaceChance; fp.spread = spread; p = fp; } else if (activeDemo == 4) { ConstantPath constPath = new ConstantPath (start.position, searchLength, OnPathComplete); p = constPath; } if (p != null) AstarPath.StartPath (p); }
/// <summary> /// This method generates all the nodes available for the pathfinding (no visual overlays). /// </summary> private void GeneratePossibleMoves() { GraphNodes.Clear(); ConstantPath path = ConstantPath.Construct(selected.transform.position, selected.unitData.stats.Ap * 750); path.traversalProvider = selected.traversalProvider; AstarPath.StartPath(path); path.BlockUntilCalculated(); foreach (GraphNode node in path.allNodes) { if (node != path.startNode) { GameObject go = Instantiate(nodePrefab, (Vector3)node.position, Quaternion.identity); possibleMoves.Add(go); go.GetComponent <AStarNode>().node = node; node.position.z = 0; GraphNodes.Add((Vector3)node.position); } } }
public IEnumerator DisplayAttackPath() { foreach (Vector3 pos in this.walkTiles.Keys.ToArray()) { // find path ConstantPath constPath = ConstantPath.Construct(pos, (this.Attack.GetRange() + 1) * 1000, null); this.AttackSeeker.StartPath(constPath); yield return(StartCoroutine(constPath.WaitForPath())); // display tiles List <GraphNode> nodes = constPath.allNodes; for (int i = nodes.Count - 1; i >= 0; i--) { GraphNode node = nodes[i]; Vector3 position = (Vector3)node.position; position.x = Mathf.Round(position.x); position.y = Mathf.Round(position.y); position.z = 0; if (!this.walkTiles.ContainsKey(position) && !this.attackTiles.ContainsKey(position)) { this.attackTiles.Add(position, Instantiate(this.attackTile, position, Quaternion.identity, this.transform)); } } } }
void GeneratePossibleMoves(TurnBasedAI unit) { var path = ConstantPath.Construct(unit.transform.position, unit.movementPoints * 1000 + 1); path.traversalProvider = unit.traversalProvider; // Schedule the path for calculation AstarPath.StartPath(path); // Force the path request to complete immediately // This assumes the graph is small enough that // this will not cause any lag path.BlockUntilCalculated(); foreach (var node in path.allNodes) { if (node != path.startNode) { // Create a new node prefab to indicate a node that can be reached // NOTE: If you are going to use this in a real game, you might want to // use an object pool to avoid instantiating new GameObjects all the time var go = GameObject.Instantiate(nodePrefab, (Vector3)node.position, Quaternion.identity) as GameObject; possibleMoves.Add(go); go.GetComponent <Astar3DButton>().node = node; } } }
public IEnumerator CalculateConstantPath() { ConstantPath constPath = ConstantPath.Construct(end.position, searchLength, OnPathComplete); AstarPath.StartPath(constPath); lastPath = constPath; yield return(constPath.WaitForPath()); }
public void OnConstantPathComplete(Path p) { ConstantPath constPath = p as ConstantPath; List <GraphNode> nodes = constPath.allNodes; MoveableNodes = nodes; TakeCover(); }
public IEnumerator CalculateConstantPath() { ConstantPath constPath = ConstantPath.Construct(this.end.position, this.searchLength, new OnPathDelegate(this.OnPathComplete)); AstarPath.StartPath(constPath, false); this.lastPath = constPath; yield return constPath.WaitForPath(); yield break; }
/** Starts a path specified by PathTypesDemo.activeDemo */ public void DemoPath() { Path p = null; if (activeDemo == 0) { p = ABPath.Construct(start.position, end.position, OnPathComplete); } else if (activeDemo == 1) { MultiTargetPath mp = MultiTargetPath.Construct(multipoints.ToArray(), end.position, null, OnPathComplete); p = mp; } else if (activeDemo == 2) { RandomPath rp = RandomPath.Construct(start.position, searchLength, OnPathComplete); rp.spread = spread; rp.aimStrength = aimStrength; rp.aim = end.position; rp.replaceChance = replaceChance; p = rp; } else if (activeDemo == 3) { FleePath fp = FleePath.Construct(start.position, end.position, searchLength, OnPathComplete); fp.aimStrength = aimStrength; fp.replaceChance = replaceChance; fp.spread = spread; p = fp; } else if (activeDemo == 4) { ConstantPath constPath = ConstantPath.Construct(end.position, searchLength, OnPathComplete); p = constPath; } else if (activeDemo == 5) { FloodPath fp = FloodPath.Construct(end.position, null); lastFlood = fp; p = fp; } else if (activeDemo == 6 && lastFlood != null) { FloodPathTracer fp = FloodPathTracer.Construct(end.position, lastFlood, OnPathComplete); p = fp; } if (p != null) { AstarPath.StartPath(p); lastPath = p; } }
public IEnumerator Constant() { ConstantPath constPath = ConstantPath.Construct(CoverTarget.transform.position, 5 * 3000, OnConstantPathComplete); AstarPath.StartPath(constPath); yield return(constPath.WaitForPath()); Debug.Log(constPath.pathID + " " + constPath.allNodes.Count); }
public IEnumerator Constant() { ConstantPath constPath = ConstantPath.Construct(end.position, searchLength, OnPathComplete); AstarPath.StartPath(constPath); lastPath = constPath; yield return(constPath.WaitForPath()); Debug.Log(constPath.pathID + " " + constPath.allNodes.Count); }
public IEnumerator Constant() { mAstarPath.astarData.gridGraph.GetNearest(transform.position).node.Walkable = true; ConstantPath constPath = ConstantPath.Construct((transform.position - new Vector3(0, 1, 0)), (MoveSpeed / 3) * 3000, OnConstantPathComplete); AstarPath.StartPath(constPath); yield return(constPath.WaitForPath()); Debug.Log(constPath.pathID + " " + constPath.allNodes.Count); }
public static Vector3 RandomPointConstantPath(Vector3 origin, int searchLength) { ConstantPath path = ConstantPath.Construct(origin, searchLength); AstarPath.StartPath(path); path.BlockUntilCalculated(); var randomPoint = PathUtilities.GetPointsOnNodes(path.allNodes, 1)[0]; return(randomPoint); }
//计算常量路径 public ConstantPath StartConstantPath(Vector3 start, int maxGScore, ITraversalProvider traversalProvider, OnPathDelegate callback) { var path = ConstantPath.Construct(start, maxGScore + 1, callback); path.traversalProvider = traversalProvider; AstarPath.StartPath(path); if (callback != null) { } else { path.BlockUntilCalculated(); } return(path); }
// Token: 0x06002A30 RID: 10800 RVA: 0x001C7418 File Offset: 0x001C5618 private void GeneratePossibleMoves(TurnBasedAI unit) { ConstantPath constantPath = ConstantPath.Construct(unit.transform.position, unit.movementPoints * 1000 + 1, null); constantPath.traversalProvider = unit.traversalProvider; AstarPath.StartPath(constantPath, false); constantPath.BlockUntilCalculated(); foreach (GraphNode graphNode in constantPath.allNodes) { if (graphNode != constantPath.startNode) { GameObject gameObject = UnityEngine.Object.Instantiate <GameObject>(this.nodePrefab, (Vector3)graphNode.position, Quaternion.identity); this.possibleMoves.Add(gameObject); gameObject.GetComponent <Astar3DButton>().node = graphNode; } } }
public void GeneratePossibleMoves(Unit.Unit unit) { ConstantPath path = null; GameObject nodePrefab = null; if (UnitStateManager.currentState == State.SelectAttackTarget) { path = ConstantPath.Construct(unit.transform.position, unit.AttackRangePoints * 1000 + 1); nodePrefab = nodePrefabs[1]; } else if (UnitStateManager.currentState == State.SelectMoveTarget) { path = ConstantPath.Construct(unit.transform.position, unit.MovementPoints * 1000 + 1); nodePrefab = nodePrefabs[0]; path.traversalProvider = unit.TraversalProvider; } // Schedule the path for calculation AstarPath.StartPath(path); // Force the path request to complete immediately // This assumes the graph is small enough that // this will not cause any lag if (path == null) { return; } path.BlockUntilCalculated(); foreach (var node in path.allNodes) { if (node != path.startNode) { // Create a new node prefab to indicate a node that can be reached // NOTE: If you are going to use this in a real game, you might want to // use an object pool to avoid instantiating new GameObjects all the time var go = Instantiate(nodePrefab, (Vector3)node.position, Quaternion.identity); possibleMoves.Add(go); go.GetComponent <MoveReservable>().Node = node; } } OnPathHasBeenGenerated?.Invoke(true); }
/** Starts a path specified by PathTypesDemo::activeDemo */ public void DemoPath() { Path p = null; if (activeDemo == 0) { p = new Path(start.position, end.position, OnPathComplete); } else if (activeDemo == 1) { MultiTargetPath mp = new MultiTargetPath(multipoints.ToArray(), end.position, null, OnPathComplete); p = mp; } else if (activeDemo == 2) { RandomPath rp = new RandomPath(start.position, searchLength, OnPathComplete); rp.spread = spread; rp.aimStrength = aimStrength; rp.aim = end.position; rp.replaceChance = replaceChance; p = rp; } else if (activeDemo == 3) { FleePath fp = new FleePath(start.position, end.position, searchLength, OnPathComplete); fp.fleeStrength = aimStrength; fp.replaceChance = replaceChance; fp.spread = spread; p = fp; } else if (activeDemo == 4) { ConstantPath constPath = new ConstantPath(start.position, searchLength, OnPathComplete); p = constPath; } if (p != null) { AstarPath.StartPath(p); } }
//计算常量路径 public ConstantPath StartConstantPath(Vector3 start, int movePoint, OnPathDelegate callback, ITraversalProvider traversalProvider = null) { var path = ConstantPath.Construct(start, Mathf.Clamp(movePoint, 1, 1000) * 1000, callback); path.traversalProvider = CommonTraversalProvider; if (traversalProvider != null) { path.traversalProvider = traversalProvider; } AstarPath.StartPath(path); if (callback != null) { } else { path.BlockUntilCalculated(); } return(path); }
// Token: 0x060029EE RID: 10734 RVA: 0x001C234B File Offset: 0x001C054B 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; }
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>(); 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); }
/** * Constructs an Operator based on a string. With the exception of possible * superfluous parentheses and whitespace, the resulting operator is such * that getFromString(s).toString() == s. The input string must respect the * following BNF grammar (which corresponds to LTL + first-order; * parentheses are important): * <ol> * <li>string := atom | binary_op | unary_op | quantified * <li>binary_op := (string) bin_operator (string) * <li>unary_op := un_operator (string) * <li>quantified := [qualif_var] (string) | <qualif_var> (string) * <li>bin_operator := & | -> | the "pipe" character | U | = | < | * > | - * <li>un_operator := ! | G | X | F * <li>qualif_var := atom qualif (there is a whitespace character between * atom and qualif) * <li>atom := any literal composed of alphanumerical characters, with the * exception of reserved sequences such as operators * <li>qualif:= any literal composed of alphanumerical characters, with the * exception of reserved sequences such as operators * </ol> * * @param s * The input string * @param domains * A map from a set of paths to a set of values (atoms), providing * finite domains for quantifiers. This parameter is facultative; * domains need to be provided only when quantifiers need to be * expanded into explicit conjunctions/disjunctions of values, or * when messages must be generated (instead of monitored). * * @return An Operator equivalent to the string passed as an argument, null * if the string does not correspond to a valid Operator. */ public static Operator parseFromString(string s, IDictionary<string, HashSet<Constant>> domains) { bool flag = false; int quantRight = 0, parLeft = 0; Operator o = null, o2 = null; FOQuantifier foq = null; UnaryOperator uo = null; BinaryOperator bo = null; Atom a = null; // First removes/converts extra whitespace s = LTLStringParser.formatString(s); string sTrim = s.Trim(); if (sTrim.Length == 0) { return null; } // Handles first-order quantifiers flag = false; if (sTrim[0] == '[') { foq = new FOForAll(); quantRight = sTrim.IndexOf("]"); flag = true; } if (sTrim[0] == '<') { foq = new FOExists(); quantRight = sTrim.IndexOf(">"); flag = true; } if (flag) { Regex r = new Regex("^(\\w+)\\s*(.*)$"); MatchCollection m = r.Matches(sTrim.Substring(1, quantRight - 1)); if (m.Count > 0) { Match m2 = m[0]; GroupCollection g = m2.Groups; Atom qvar = new Atom(g[1].ToString()); string qualifier = g[2].ToString(); foq.setQuantifiedVariable(qvar); foq.setQualifier(qualifier); if (domains != null) { // If a domain is provided, attaches it to the quantifier HashSet<Constant> dom = new HashSet<Constant>(); foreach (KeyValuePair<string, HashSet<Constant>> con in domains) { if (con.Key == qualifier) { dom = con.Value; } } foq.setDomain(dom); } } //foq.setQualifiedVariable(sTrim.substring(1, quantRight)); parLeft = sTrim.IndexOf("(", (quantRight + 1)); //parRight = sTrim.indexOf(")", parLeft); o = parseFromString(sTrim.Substring((parLeft + 1), sTrim.Length - 1 - (parLeft + 1)), domains); foq.setOperand(o); return foq; } // Handles unary operators flag = false; if (sTrim.Substring(0, 1) == "!") { uo = new OperatorNot(); flag = true; } if (sTrim.Substring(0, 1) == "F") { uo = new OperatorF(); flag = true; } if (sTrim.Substring(0, 1) == "G") { uo = new OperatorG(); flag = true; } if (sTrim.Substring(0, 1) == "X") { uo = new OperatorX(); flag = true; } if (sTrim.Length >= 2) { // This is for CTL, nothing to do here } if (flag) { parLeft = sTrim.IndexOf("("); o = parseFromString(sTrim.Substring((parLeft + 1), sTrim.Length - 1 - (parLeft + 1)), domains); uo.setOperand(o); return uo; } // Handles binary operators flag = false; if (sTrim[0] == '(') { // At this point in the method, if first char is a "(", // the formula is necessarily a binary operator int parNum = 0; string sLeft = "", sRight = ""; string binaryOp = ""; Regex r = new Regex("(\\(|\\))"); MatchCollection m = r.Matches(sTrim); int mend = -1; for (int i = 0; i < m.Count; i++) { Match m2 = m[i]; mend = m[i].Groups[0].Index + m[i].Groups[0].Length; GroupCollection g = m2.Groups; if (g[0].ToString().Equals("(")) { parNum++; } else { parNum--; } if (parNum == 0) { sLeft = sTrim.Substring(1, mend - 1 - 1); break; } } parLeft = sTrim.IndexOf("(", mend); int left_var = mend + 1; binaryOp = sTrim.Substring(left_var, parLeft - left_var - 1).Trim(); sRight = sTrim.Substring(parLeft + 1, sTrim.Length - 1 - (parLeft + 1)).Trim(); o = parseFromString(sLeft, domains); o2 = parseFromString(sRight, domains); if (binaryOp == "&") { bo = new OperatorAnd(); flag = true; } if (binaryOp == "|") { bo = new OperatorOr(); flag = true; } if (binaryOp == "=") { bo = new OperatorEquals(o, o2); flag = true; } if (binaryOp == "->") { bo = new OperatorImplies(o, o2); flag = true; } if (binaryOp == "-") { bo = new OperatorMinus(o, o2); flag = true; } if (binaryOp == "<") { bo = new OperatorSmallerThan(o, o2); flag = true; } if (binaryOp == "<=") { bo = new OperatorSmallerThan(o, o2, true); flag = true; } if (binaryOp == ">") { bo = new OperatorGreaterThan(o, o2); flag = true; } if (binaryOp == ">=") { bo = new OperatorGreaterThan(o, o2, true); flag = true; } if (binaryOp == "U") { bo = new OperatorU(o, o2); flag = true; } if (binaryOp == "V") { bo = new OperatorV(o, o2); flag = true; } if (flag) { return bo; } } else { // At this point, the only case left is that of a single atom // (either a constant or a variable) if (sTrim[0] == '{') { // Constants are surrounded by braces a = new Constant(sTrim.Substring(1, sTrim.Length - 1 - 1)); } else if (sTrim[0] == '/') { // XPaths are surrounded by forward slashes a = new ConstantPath(sTrim.Substring(1, sTrim.Length - 1 - 1)); } else { // Otherwise, we have an atom a = new Atom(sTrim); } return a; } // Down there, none of the previous cases has fired: return o, which is // null System.Diagnostics.Debug.Assert (false); return o; }
/** 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; } //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); } 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); } }
public void OnPathComplete(Path p) { if (this.lastRender == null) { return; } if (p.error) { this.ClearPrevious(); return; } if (p.GetType() == typeof(MultiTargetPath)) { List <GameObject> list = new List <GameObject>(this.lastRender); this.lastRender.Clear(); MultiTargetPath multiTargetPath = p as MultiTargetPath; for (int i = 0; i < multiTargetPath.vectorPaths.Length; i++) { if (multiTargetPath.vectorPaths[i] != null) { List <Vector3> list2 = multiTargetPath.vectorPaths[i]; GameObject gameObject; if (list.Count > i && list[i].GetComponent <LineRenderer>() != null) { gameObject = list[i]; list.RemoveAt(i); } else { gameObject = new GameObject("LineRenderer_" + i, new Type[] { typeof(LineRenderer) }); } LineRenderer component = gameObject.GetComponent <LineRenderer>(); component.sharedMaterial = this.lineMat; for (int j = 0; j < list2.Count; j++) { component.SetPosition(j, list2[j] + this.pathOffset); } this.lastRender.Add(gameObject); } } for (int k = 0; k < list.Count; k++) { Object.Destroy(list[k]); } } else if (p.GetType() == typeof(ConstantPath)) { this.ClearPrevious(); ConstantPath constantPath = p as ConstantPath; List <GraphNode> allNodes = constantPath.allNodes; Mesh mesh = new Mesh(); List <Vector3> list3 = new List <Vector3>(); bool flag = false; for (int l = allNodes.Count - 1; l >= 0; l--) { Vector3 vector = (Vector3)allNodes[l].position + this.pathOffset; if (list3.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(vector, Vector3.up, Color.blue); } else { GridGraph gridGraph = AstarData.GetGraph(allNodes[l]) as GridGraph; float num = 1f; if (gridGraph != null) { num = gridGraph.nodeSize; } list3.Add(vector + new Vector3(-0.5f, 0f, -0.5f) * num); list3.Add(vector + new Vector3(0.5f, 0f, -0.5f) * num); list3.Add(vector + new Vector3(-0.5f, 0f, 0.5f) * num); list3.Add(vector + new Vector3(0.5f, 0f, 0.5f) * num); } } Vector3[] array = list3.ToArray(); int[] array2 = new int[3 * array.Length / 2]; int m = 0; int num2 = 0; while (m < array.Length) { array2[num2] = m; array2[num2 + 1] = m + 1; array2[num2 + 2] = m + 2; array2[num2 + 3] = m + 1; array2[num2 + 4] = m + 3; array2[num2 + 5] = m + 2; num2 += 6; m += 4; } Vector2[] array3 = new Vector2[array.Length]; for (int n = 0; n < array3.Length; n += 4) { array3[n] = new Vector2(0f, 0f); array3[n + 1] = new Vector2(1f, 0f); array3[n + 2] = new Vector2(0f, 1f); array3[n + 3] = new Vector2(1f, 1f); } mesh.vertices = array; mesh.triangles = array2; mesh.uv = array3; mesh.RecalculateNormals(); GameObject gameObject2 = new GameObject("Mesh", new Type[] { typeof(MeshRenderer), typeof(MeshFilter) }); MeshFilter component2 = gameObject2.GetComponent <MeshFilter>(); component2.mesh = mesh; MeshRenderer component3 = gameObject2.GetComponent <MeshRenderer>(); component3.material = this.squareMat; this.lastRender.Add(gameObject2); } else { this.ClearPrevious(); GameObject gameObject3 = new GameObject("LineRenderer", new Type[] { typeof(LineRenderer) }); LineRenderer component4 = gameObject3.GetComponent <LineRenderer>(); component4.sharedMaterial = this.lineMat; for (int num3 = 0; num3 < p.vectorPath.Count; num3++) { component4.SetPosition(num3, p.vectorPath[num3] + this.pathOffset); } this.lastRender.Add(gameObject3); } }
public void OnConstantPathComplete(Path p) { ConstantPath constPath = p as ConstantPath; List <GraphNode> nodes = constPath.allNodes; MoveableNodes = nodes; Mesh mesh = new Mesh(); List <Vector3> verts = new List <Vector3> (); bool drawRaysInstead = false; 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(transform.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; } //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; RenderedGrid.Add(go); }