static void DiamondStep(float[,] arr, int range, int x, int z, int reach, MathLib.UCL_Random rnd) { //Debug.LogWarning("diamondStep x:" + x +",z:" + z); int count = 0; float avg = 0.0f; if (x - reach >= 0) { avg += arr[x - reach, z]; count++; } if (x + reach < range) { avg += arr[x + reach, z]; count++; } if (z - reach >= 0) { avg += arr[x, z - reach]; count++; } if (z + reach < range) { avg += arr[x, z + reach]; count++; } float val = reach / (float)range; avg += rnd.Range(-val, val); avg /= count; arr[x, z] = avg; }
static void SquareStep(float[,] arr, int range, int x, int z, int reach, MathLib.UCL_Random rnd) { int count = 0; float avg = 0.0f; if (x - reach >= 0 && z - reach >= 0) { avg += arr[x - reach, z - reach]; count++; } if (x - reach >= 0 && z + reach < range) { avg += arr[x - reach, z + reach]; count++; } if (x + reach < range && z - reach >= 0) { avg += arr[x + reach, z - reach]; count++; } if (x + reach < range && z + reach < range) { avg += arr[x + reach, z + reach]; count++; } float val = reach / (float)range; avg += rnd.Range(-val, val); avg /= count; arr[x, z] = avg; }
static void DiamondSquare(float[,] arr, int range, int sx, int sz, int size, MathLib.UCL_Random rnd) { int half = size / 2; if (half < 1) { return; } //square steps for (int z = half; z < range; z += size) { for (int x = half; x < range; x += size) { SquareStep(arr, range, sx, sz, x, z, half, rnd); } } // diamond steps int col = 0; for (int x = 0; x < range; x += half) { col++; //If this is an odd column. if (col % 2 == 1) { for (int z = half; z < range; z += size) { DiamondStep(arr, range, sx, sz, x, z, half, rnd); } } else { for (int z = 0; z < range; z += size) { DiamondStep(arr, range, sx, sz, x, z, half, rnd); } } } //return; DiamondSquare(arr, range, sx, sz, size / 2, rnd); return; }
static void SquareStep(float[,] arr, int range, int sx, int sz, int x, int z, int reach, MathLib.UCL_Random rnd) { //Debug.LogWarning("SquareStep sx:" + sx+ ",sz:" + sz + ",x:" + x + ",z:" + z); int count = 0; float avg = 0.0f; if (x - reach >= 0 && z - reach >= 0) { avg += arr[x - reach + sx, z - reach + sz]; count++; } if (x - reach >= 0 && z + reach < range) { avg += arr[x - reach + sx, z + reach + sz]; count++; } if (x + reach < range && z - reach >= 0) { avg += arr[x + reach + sx, z - reach + sz]; count++; } if (x + reach < range && z + reach < range) { avg += arr[x + reach + sx, z + reach + sz]; count++; } float val = reach / (float)range; avg += rnd.Range(-val, val); avg /= count; arr[x + sx, z + sz] = avg; }
public static void DiamondSquare(float[,] arr, int sx, int sz, int range, MathLib.UCL_Random rnd) { DiamondSquare(arr, range, sx, sz, range - 1, rnd); }
public static void DiamondSquare(float[,] arr, int step, MathLib.UCL_Random rnd) { DiamondSquare(arr, step, step - 1, rnd); }
public static void DiamondSquare(float[,] arr, MathLib.UCL_Random rnd) { DiamondSquare(arr, arr.GetLength(0), arr.GetLength(0) - 1, rnd); }