public static void Serpentine(MatrixWorld heights, Spline spline, float segLength, int iterations, SerpentineFactors factors) { DebugGizmos.Clear("Serp"); factors.Normalize(); float DistFn(Vector3 n1, Vector3 n2) => Mathf.Sqrt((n1.x - n2.x) * (n1.x - n2.x) + (n1.z - n2.z) * (n1.z - n2.z)); for (int i = 0; i < iterations; i++) { tmpIteration = i; tmpHighlightIteration = iterations - 1; spline.SubdivideDist(segLength, DistFn); Floor(heights, spline); CoordRect rect = heights.rect; Coord rectMin = heights.rect.offset; Coord rectMax = heights.rect.offset + heights.rect.size; float pixelSize = heights.PixelSize.x; Vector3[] newNodes = new Vector3[spline.nodes.Length]; for (int n = 1; n < spline.nodes.Length - 1; n++) { //if (n>2) { newNodes[n]=spline.nodes[n]; continue; } tmpNode = n; float weight = GetNodeWeight(spline.nodes[n - 1], spline.nodes[n], spline.nodes[n + 1], factors, heights.worldSize.y); Vector3 moveVector = GetMoveVector(heights, spline.nodes[n - 1], spline.nodes[n], spline.nodes[n + 1], weight, 1, heights.PixelSize.x, factors); newNodes[n] = spline.nodes[n] + moveVector * 3f; //*heights.PixelSize*0.2f;// * (2f/iterations); if (tmpHighlightIteration == i) { DebugGizmos.DrawRay("Serp", newNodes[n], moveVector, Color.yellow, additive: true); } } newNodes[0] = spline.nodes[0]; newNodes[newNodes.Length - 1] = spline.nodes[spline.nodes.Length - 1]; spline.nodes = newNodes; spline.Weld(segLength / 2, DistFn); } }
public static void SerpentineUnordered(MatrixWorld heights, Spline spline, float segLength, int iterations, SerpentineFactors factors) { DebugGizmos.Clear("Serp"); factors.Normalize(); float DistFn(Vector3 n1, Vector3 n2) => Mathf.Sqrt((n1.x - n2.x) * (n1.x - n2.x) + (n1.z - n2.z) * (n1.z - n2.z)); CoordRect rect = heights.rect; Coord rectMin = heights.rect.offset; Coord rectMax = heights.rect.offset + heights.rect.size; float pixelSize = heights.PixelSize.x; for (int i = 0; i < iterations; i++) { spline.SubdivideDist(segLength, DistFn); Floor(heights, spline); tmpIteration = i; tmpHighlightIteration = iterations - 1; //finding node with lowest rating float lowestRating = float.MaxValue; int lowestNum = 0; for (int n = 1; n < spline.nodes.Length - 1; n++) { float rating = GetNodeWeight(spline.nodes[n - 1], spline.nodes[n], spline.nodes[n + 1], factors, heights.worldSize.y); if (rating < lowestRating) { lowestRating = rating; lowestNum = n; } } tmpNode = lowestNum; float weight = GetNodeWeight(spline.nodes[lowestNum - 1], spline.nodes[lowestNum], spline.nodes[lowestNum + 1], factors, heights.worldSize.y); Vector3 moveVector = GetMoveVector(heights, spline.nodes[lowestNum - 1], spline.nodes[lowestNum], spline.nodes[lowestNum + 1], weight, 1, heights.PixelSize.x, factors); spline.nodes[lowestNum] += moveVector; //*heights.PixelSize*0.2f;// * (2f/iterations); spline.Weld(segLength / 2, DistFn); } }