private double[,] MockImageEffect(HeightMap h) { var result = h.Clone(); h.ForEach((r, c) => { result[r, c] = 1 - result[r, c]; }); return(result.Data); }
private double[,] MockImageEffect(HeightMap h, double intensity) { var result = h.Clone(); h.ForEach((r, c) => { result[r, c] = result[r, c] * intensity; }); return(result.Data); }
private double[,] MockImageEffect(HeightMap h, double brightness) { var result = h.Clone(); h.ForEach((r, c) => { result[r, c] = result[r, c] + brightness; }); return(result.Data); }
private static HeightMap MockBlendMode(HeightMap a, HeightMap b) { var result = a.Clone(); result.ForEach((r, c) => { result[r, c] += b.IsInRange(r, c) ? b[r, c] : 0; }); return(result); }
private double[,] MockImageEffect(HeightMap h, double gamma) { var result = h.Clone(); double gammaCorrection = 1 / gamma; h.ForEach((r, c) => { result[r, c] = Math.Pow(result[r, c], gammaCorrection); }); return(result.Data); }
private HeightMap MockImageEffect(HeightMap h) { var result = h.Clone(); h.ForEach((r, c) => { result[r, c] += 0.1d; }); return(result); }
/// <summary> /// Change the brightness of the given heightmap /// </summary> public HeightMap Calculate(HeightMap heightMap) { var result = heightMap.Clone(); result.ForEach((r, c) => { result[r, c] = result[r, c] + Brightness; }); return(result); }
/// <summary> /// Change the intensity of the given heightmap /// </summary> public HeightMap Calculate(HeightMap heightMap) { var result = heightMap.Clone(); result.ForEach((r, c) => { result[r, c] = result[r, c] * Intensity; }); return(result); }
/// <summary> /// Perform the blending. /// </summary> public HeightMap Blend(HeightMap left, HeightMap right) { var result = left.Clone(); result.ForEach((r, c) => { result[r, c] *= right.IsInRange(r, c) ? right[r, c] : 0d; }); return(result); }
/// <summary> /// Invert given heightmap /// </summary> public HeightMap Calculate(HeightMap heightMap) { var inverted = heightMap.Clone(); inverted.ForEach((r, c) => { inverted[r, c] = 1 - inverted[r, c]; }); return(inverted); }
private double[,] MockImageEffect(HeightMap h, double brightness) { const double High = 259d; const double Low = 255d; double F = (High * (brightness + Low)) / (Low * (High - brightness)); var result = h.Clone(); h.ForEach((r, c) => { result[r, c] = F * (result[r, c] - 0.5d) + 0.5d; }); return(result.Data); }
/// <summary> /// Perform the blending. /// </summary> public HeightMap Blend(HeightMap left, HeightMap right) { var result = left.Clone(); result.ForEach((r, c) => { var a = result[r, c]; var b = right.IsInRange(r, c) ? right[r, c] : 1d; result[r, c] = Math.Min(a, b); }); return(result); }
/// <summary> /// Change the contrast of the given heightmap /// </summary> public HeightMap Calculate(HeightMap heightMap) { var result = heightMap.Clone(); double F = (High * (Contrast + Low)) / (Low * (High - Contrast)); result.ForEach((r, c) => { result[r, c] = F * (result[r, c] - 0.5d) + 0.5d; }); return(result); }
/// <summary> /// Change the gamma of the given heightmap /// </summary> public HeightMap Calculate(HeightMap heightMap) { var result = heightMap.Clone(); double gammaCorrection = 1 / Gamma; result.ForEach((r, c) => { result[r, c] = Math.Pow(result[r, c], gammaCorrection); }); return(result); }
/// <summary> /// Perform the blending. /// </summary> public HeightMap Blend(HeightMap left, HeightMap right) { var result = left.Clone(); result.ForEach((r, c) => { var a = result[r, c]; var b = right.IsInRange(r, c) ? right[r, c] : 0d; var ab = a - b; var ba = b - a; result[r, c] = ba > 0 ? ba : ab; }); return(result); }
public void Clone_ChangingClonePropertiesDoesNotChangeParent() { // Arrange var source = new double[, ] { { 1d, 2d }, { 3d, 4d } }; var map = new HeightMap(source); var clone = map.Clone(); // Act clone.SetValue(1, 1, 99d); // Assert Assert.NotEqual(map.Data[1, 1], clone.Data[1, 1]); Assert.Equal(99d, clone.Data[1, 1]); }
public void Clone_ReturnsHeightMapWithExactlySameProperties() { // Arrange var source = new double[, ] { { 1d, 2d }, { 3d, 4d } }; var expected = new HeightMap(source); // Act var actual = expected.Clone(); // Assert Assert.Equal(expected.Height, actual.Height); Assert.Equal(expected.Width, actual.Width); Assert.Equal(expected.MinHeight, actual.MinHeight); Assert.Equal(expected.MaxHeight, actual.MaxHeight); Assert.Equal(expected.Data, actual.Data); }
/// <summary> /// Perform the blending. /// </summary> public HeightMap Blend(HeightMap left, HeightMap right) { var result = left.Clone(); result.ForEach((r, c) => { var a = result[r, c]; var b = right.IsInRange(r, c) ? right[r, c] : 0d; if (a < 0.5) { result[r, c] = 2 * a * b; } else { result[r, c] = 1 - (2 * (1 - a) * (1 - b)); } }); return(result); }
public static HeightMap Erode(HeightMap map, int erosions, float inertia = 0.3f, float gravity = -9.81f, float minSlope = 0.01f, float capacity = 8, float maxSteps = 500, float evaporation = 0.02f, float erosion = 0.7f, float deposition = 0.2f, int radius = 2, float minSedimentCapacity = 0.01f, float smoothFactor = 2) { HeightMap simulator = map.Clone(); HeightMap changes = new HeightMap(map.size); for (int d = 0; d < erosions; d++) { Vector2 pos = new Vector2(Random.Range(0, map.size), Random.Range(0, map.size)); Vector2 dir = grad(map, pos.x, pos.y); float vel = 1; float water = 1; float sediment = 0; for (int s = 0; s < maxSteps; s++) { int x = Mathf.FloorToInt(pos.x); int y = Mathf.FloorToInt(pos.y); float u = pos.x % 1.0f; float v = pos.y % 1.0f; float currHeight = fracHeight(simulator, pos.x, pos.y); Vector2 gradient = grad(simulator, pos.x, pos.y); dir = new Vector2(dir.x * inertia - gradient.x * (1 - inertia), dir.y * inertia - gradient.y * (1 - inertia)); if (dir.x == 0 && dir.y == 0) { dir = new Vector2(Random.Range(0, 1), Random.Range(0, 1)); } dir.Normalize(); Vector2 newPos = pos + dir; //Debug.Log("currently at " + pos.x + ", " + pos.y + ", height " + currHeight); //Debug.Log("Direction is " + dir.x + ", " + dir.y); //Debug.Log("new pos is " + newPos.x + ", " + newPos.y); if (simulator.OutOfBounds(Mathf.FloorToInt(newPos.x), Mathf.FloorToInt(newPos.y)) || simulator.OutOfBounds(Mathf.CeilToInt(newPos.x), Mathf.CeilToInt(newPos.y))) { break; } float diff = fracHeight(simulator, newPos.x, newPos.y) - fracHeight(simulator, pos.x, pos.y); float sedimentCapacity = Mathf.Max(-diff * vel * water * capacity, minSedimentCapacity); //Debug.Log("Diff " + diff + ", capacity " + sedimentCapacity); if (diff > 0 || sediment > sedimentCapacity) { float amount = diff > 0 ? Mathf.Min(sediment, diff) : (sediment - sedimentCapacity) * deposition; //Debug.Log("amount " + amount); //Debug.Log("diff " + diff + ", sediment " + sediment + ", capacity " + sedimentCapacity); changes[x, y] += amount * (1 - u) * (1 - v); simulator[x, y] += amount * (1 - u) * (1 - v); // simulator.Set(x, y, simulator.Get(x, y) + amount * (1-u) * (1-v)); // changes.Set(x, y, changes.Get(x, y) + amount * (1-u) * (1-v)); if (x + 1 < map.size) { changes[x + 1, y] += amount * u * (1 - v); simulator[x + 1, y] += amount * u * (1 - v); //simulator.Set(x+1, y, simulator.Get(x+1, y) + amount * u * (1-v)); // changes.Set(x+1, y, changes.Get(x+1, y) + amount * u * (1-v)); } if (y + 1 < map.size) { changes[x, y + 1] += amount * (1 - u) * v; simulator[x, y + 1] += amount * (1 - u) * v; // changes.Set(x, y+1, changes.Get(x, y+1) + amount * (1-u) * v); // simulator.Set(x, y+1, simulator.Get(x, y+1) + amount * (1-u) * v); if (x + 1 < map.size) { changes[x + 1, y + 1] += amount * u * v; simulator[x + 1, y + 1] += amount * u * v; // changes.Set(x+1, y+1, changes.Get(x+1, y+1) + amount * u * v); // simulator.Set(x+1, y+1, simulator.Get(x+1, y+1) + amount * u * v); } } sediment -= amount; } else { float amount = Mathf.Min((sedimentCapacity - sediment) * erosion, -diff); changes[x, y] -= amount * (1 - u) * (1 - v); simulator[x, y] -= amount * (1 - u) * (1 - v); // simulator.Set(x, y, simulator.Get(x, y) - amount * (1-u) * (1-v)); // changes.Set(x, y, changes.Get(x, y) - amount * (1-u) * (1-v)); if (x + 1 < map.size) { changes[x + 1, y] -= amount * u * (1 - v); simulator[x + 1, y] -= amount * u * (1 - v); // simulator.Set(x+1, y, simulator.Get(x+1, y) - amount * u * (1-v)); // changes.Set(x+1, y, changes.Get(x+1, y) - amount * u * (1-v)); } if (y + 1 < map.size) { changes[x, y + 1] -= amount * (1 - u) * v; // changes.Set(x, y+1, changes.Get(x, y+1) - amount * (1-u) * v); simulator[x, y + 1] -= amount * (1 - u) * v; //simulator.Set(x, y+1, simulator.Get(x, y+1) - amount * (1-u) * v); if (x + 1 < map.size) { changes[x + 1, y + 1] -= amount * u * v; //changes.Set(x+1, y+1, changes.Get(x+1, y+1) - amount * u * v); simulator[x + 1, y + 1] -= amount * u * v; //simulator.Set(x+1, y+1, simulator.Get(x+1, y+1) - amount * u * v); } } sediment += amount; } vel = Mathf.Sqrt(Mathf.Max(Mathf.Pow(vel, 2) + diff * gravity, 0)); if (vel == 0) { break; } water *= (1 - evaporation); pos = newPos; } } for (int y = 0; y < map.size; y++) { for (int x = 0; x < map.size; x++) { changes[x, y] /= smoothFactor; //Debug.Log(changes.Get(x, y)); } } HeightMap eroded = map + changes; return(simulator); }