Pathfinding.SimpleSmoothModifier.SmoothOffsetSimple C# (CSharp) Method

SmoothOffsetSimple() public method

public SmoothOffsetSimple ( List path ) : List
path List
return List
		public List<Vector3> SmoothOffsetSimple (List<Vector3> path) {
			
			if (path.Count <= 2 || iterations <= 0) {
				return path;
			}
			
			if (iterations > 12) {
				Debug.LogWarning ("A very high iteration count was passed, won't let this one through");
				return path;
			}
			
			int maxLength = (path.Count-2)*(int)Mathf.Pow(2,iterations)+2;
			
			List<Vector3> subdivided = ListPool<Vector3>.Claim (maxLength);
			//new Vector3[(path.Length-2)*(int)Mathf.Pow(2,iterations)+2];
			List<Vector3> subdivided2 = ListPool<Vector3>.Claim (maxLength);
			//new Vector3[(path.Length-2)*(int)Mathf.Pow(2,iterations)+2];
			
			for (int i=0;i<maxLength;i++) { subdivided.Add (Vector3.zero); subdivided2.Add (Vector3.zero); }
			
			for (int i=0;i<path.Count;i++) {
				subdivided[i] = path[i];
			}
			
			for (int iteration=0;iteration < iterations; iteration++) {
				int currentPathLength = (path.Count-2)*(int)Mathf.Pow(2,iteration)+2;
				
				//Switch the arrays
				List<Vector3> tmp = subdivided;
				subdivided = subdivided2;
				subdivided2 = tmp;
				
				float nextMultiplier = 1F;
				
				for (int i=0;i<currentPathLength-1;i++) {
					Vector3 current = subdivided2[i];
					Vector3 next = subdivided2[i+1];
					
					Vector3 normal = Vector3.Cross (next-current,Vector3.up);
					normal = normal.normalized;
					
					//This didn't work very well, made the path jaggy
					/*Vector3 dir = next-current;
					dir *= strength*0.5F;
					current += dir;
					next -= dir;*/
					
					bool firstRight = false;
					bool secondRight = false;
					bool setFirst = false;
					bool setSecond = false;
					if (i != 0 && !Polygon.IsColinear (current,next, subdivided2[i-1])) {
						setFirst = true;
						firstRight = Polygon.Left (current,next, subdivided2[i-1]);
					}
					if (i < currentPathLength-1 && !Polygon.IsColinear (current,next, subdivided2[i+2])) {
						setSecond = true;
						secondRight = Polygon.Left (current,next,subdivided2[i+2]);
					}
					
					if (setFirst) {
						subdivided[i*2] = current + (firstRight ? normal*offset*nextMultiplier : -normal*offset*nextMultiplier);
					} else {
						subdivided[i*2] = current;
					}
					
					//Didn't work very well
					/*if (setFirst && setSecond) {
						if (firstRight != secondRight) {
							nextMultiplier = 0.5F;
						} else {
							nextMultiplier = 1F;
						}
					}*/
					
					if (setSecond) {
						subdivided[i*2+1] = next  + (secondRight ? normal*offset*nextMultiplier : -normal*offset*nextMultiplier);
					} else {
						subdivided[i*2+1] = next;
					}
				}
				
				subdivided[(path.Count-2)*(int)Mathf.Pow(2,iteration+1)+2-1] = subdivided2[currentPathLength-1];
			}
			
	#if ASTARDEBUG
			for (int i=0;i<subdivided.Count-1;i++) {
				Debug.DrawLine (subdivided[i],subdivided[i+1],Color.red);
			}
	#endif
			
			ListPool<Vector3>.Release (subdivided2);
			
			return subdivided;
		}