/// <summary> /// sets height to global terrain /// maps given local coordinates on global /// </summary> /// <param name="x"></param> /// <param name="z"></param> public void SetLocalValue(int x, int z, float height, bool overwrite, GlobalCoordinates gc) { if (!overwrite && IsDefined(x, z, gc)) return; gc.SetValue(x + (int)center.x - terrainWidth / 2, z + (int)center.z - terrainHeight / 2, height, overwrite); }
/// <summary> /// sets filter value /// all filters should operate on global space /// gc = global filter space /// </summary> public void SetGlobalValue(int x, int z, float value, bool overwrite, GlobalCoordinates gc) { gc.SetValue(x, z, value, overwrite); }
/// <summary> /// apply filter to given heightmap /// </summary> public void FilterMapInRegion(GlobalCoordinates map, Area region, float epsilon) { int x_min = region.botLeft.x; int z_min = region.botLeft.z; int x_max = region.topRight.x; int z_max = region.topRight.z; int area = 1; GlobalCoordinates mapTmp = new GlobalCoordinates(100); for (int x = x_min; x < x_max; x++) { for (int z = z_min; z < z_max; z++) { if (map.IsDefined(x, z)) { float average = 0; int neighboursCount = 0; for (int _x = x - area; _x <= x + area; _x++) { for (int _z = z - area; _z <= z + area; _z++) { if (map.IsDefined(_x, _z)) { average += map.GetValue(_x, _z, 0); neighboursCount++; } } } if (neighboursCount != 0) average /= neighboursCount; else average = 666; if (average != 666) { float height = map.GetValue(x, z); if (height < average - epsilon) fg.SetGlobalValue(x, z, (average + epsilon), true, mapTmp); else if (height > average + epsilon) fg.SetGlobalValue(x, z, (average - epsilon), true, mapTmp); } } } } for (int x = x_min; x < x_max; x++) { for (int z = z_min; z < z_max; z++) { if(mapTmp.IsDefined(x, z)) map.SetValue(x, z, mapTmp.GetValue(x, z)); } } }