Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- public class MidPointHeight
- {
- public int Size { get; set; }
- public float FallOff { get; set; }
- public bool Wrap { get; set; }
- public int cSize { get; private set; }
- public float cDeviance { get; private set; }
- public static float cRange { get; set; }
- private Random rand;
- public float[,] heights { get; private set; }
- public MidPointHeight(int Seed, bool Wrap, float Range)
- {
- this.FallOff = 0.495f;
- this.rand = new Random(Seed != -1 ? Seed : (int)DateTime.UtcNow.Ticks);
- this.Wrap = Wrap;
- this.cSize = 2;
- this.cDeviance = 1.0f;
- cRange = Range;
- this.heights = new float[cSize, cSize];
- //init minium
- if (this.Wrap)
- {
- this.heights[0, 0] = this.rand.GetHeight();
- this.heights[0, 1] = this.heights[0, 0];
- this.heights[1, 0] = this.heights[0, 0];
- this.heights[1, 1] = this.heights[0, 0];
- }
- else
- {
- this.heights[0, 0] = this.rand.GetHeight();
- this.heights[0, 1] = this.rand.GetHeight();
- this.heights[1, 0] = this.rand.GetHeight();
- this.heights[1, 1] = this.rand.GetHeight();
- }
- }
- public void MakeStep()
- {
- this.heights = this.DiamondStep(this.heights);
- this.SquareStep(this.heights);
- }
- private float[,] DiamondStep(float[,] data)
- {
- this.cDeviance *= this.FallOff;
- int oldSize = cSize;
- cSize = cSize * 2 - 1;
- float[,] result = new float[cSize, cSize];
- for (int x = 0, nx = 1; x < oldSize - 1; x++, nx += 2)
- for (int y = 0, ny = 1; y < oldSize - 1; y++, ny += 2)
- {
- float hlt = data[x, y];
- float hrt = data[x + 1, y];
- float hlb = data[x, y + 1];
- float hrb = data[x + 1, y + 1];
- float add = 0f;
- if (y % 2 == 0)
- if (x % 2 == 0) { add += hrb + hlt; } else { add += hlb + hrt; }
- else
- if (x % 2 == 0) { add += hrt + hlb; } else { add += hlt + hrb; }
- float mid = (hlt + hrt + hlb + hrb + add) / 6.0f;
- float offset = (this.rand.GetHeight() * this.cDeviance);
- result[nx, ny] = (mid + offset);
- //set existing heights
- result[nx - 1, ny - 1] = hlt;
- result[nx + 1, ny - 1] = hrt;
- result[nx - 1, ny + 1] = hlb;
- result[nx + 1, ny + 1] = hrb;
- }
- return result;
- }
- private void SquareStep(float[,] data)
- {
- for (int y = 0; y < cSize; y++)
- for (int x = y % 2 == 0 ? 1 : 0; x < cSize; x += 2)
- {
- if (data[x, y] != 0)
- continue;
- float l = (x - 1 < 0) ? float.NaN : data[x - 1, y];
- float r = (x + 1 >= cSize) ? float.NaN : data[x + 1, y];
- float t = (y - 1 < 0) ? float.NaN : data[x, y - 1];
- float b = (y + 1 >= cSize) ? float.NaN : data[x, y + 1];
- if (float.IsNaN(l)) { if (this.Wrap) { data[x, y] = this.GetMissingPoint(cSize - 1, y, data, (r + t + b)); continue; } else l = r; }
- if (float.IsNaN(r)) { if (this.Wrap) { data[x, y] = this.GetMissingPoint(0, y, data, (l + t + b)); continue; } else r = l; }
- if (float.IsNaN(t)) { if (this.Wrap) { data[x, y] = this.GetMissingPoint(x, cSize - 1, data, (l + r + b)); continue; } else t = b; }
- if (float.IsNaN(b)) { if (this.Wrap) { data[x, y] = this.GetMissingPoint(x, 0, data, (l + r + t)); continue; } else b = t; }
- float mid = x % 2 == 0 ? (l + r + t * 2f + b * 2f) / 6.0f : (l * 2f + r * 2f + t + b) / 6.0f;
- float offset = this.rand.GetHeight() * this.cDeviance;
- data[x, y] = (mid + offset);
- }
- }
- private float GetMissingPoint(int x, int y, float[,] data, float height)
- {
- float l = x - 1 < 0 ? float.NaN : data[x - 1, y];
- float r = x + 1 >= cSize ? float.NaN : data[x + 1, y];
- float t = y - 1 < 0 ? float.NaN : data[x, y - 1];
- float b = y + 1 >= cSize ? float.NaN : data[x, y + 1];
- if (float.IsNaN(l)) l = 0;
- if (float.IsNaN(r)) r = 0;
- if (float.IsNaN(b)) b = 0;
- if (float.IsNaN(t)) t = 0;
- float mid = (l + r + t + b + height) / 6.0f;
- float res = mid + (this.rand.GetHeight() * this.cDeviance);
- data[x, y] = res;
- return res;
- }
- public static float Clamp(float value, float min, float max)
- {
- return value > max ? max : value < min ? min : value;
- }
- }
- public static class MyExtensions
- {
- public static float GetHeight(this Random rnd)
- {
- return (float)(rnd.NextDouble() * MidPointHeight.cRange - (MidPointHeight.cRange / 2.0f));
- }
- }
- public class MeshData
- {
- public int Size { get; set; }
- public float[,] Heights { get; set; }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement