SOLVED ds_grid median filtering / terrain smothing

Hello, Game Maker Community!
I ran into problem with smooth terrain generation. Initially a had something like on the image below.

ezgif.com-resize (1).gif

This terrain was generated with using ds_grid_set_region and ds_grid_set_disk in random positions of ds_grid
I should say, that I've tried using Perlin's noise algorithm but find it quite complicated for this problem.

So that I did...
As an Aseprite (programme for pixel art) user I knew that there's a feature called Median filter.

1592111023585.png

That it does is removing noise from the image.

1592111349973.png

I though it would be perfect if the same effect will be applied on my grid. Unfoturnatelly, I didn't find anything about game maker and median filtering, so tried myself
and came up with somthing like this:

GML:
/// ds_grid_median_filter(index, width, height);
/*
  Author: danielpancake
  Date: 13.06.20
*/

var median_list = ds_list_create();

var index = argument0;

var w = argument1;
var h = argument2;

var grid_width = ds_grid_width(index);
var grid_height = ds_grid_height(index);

for (var i = 0; i < grid_width - 1; i++) {
    for (var j = 0; j < grid_height - 1; j++) {
 
        for (var ii = -floor(w / 2); ii < ceil(w / 2); ii++) {
            for (var jj = -floor(h / 2); jj < ceil(h / 2); jj++) {
                ds_list_add(median_list, index[# clamp(i + ii, 0, grid_width - 1), clamp(j + jj, 0, grid_height - 1)]);
            }
        }
     
        ds_list_sort(median_list, true);
        index[# i, j] = median_list[| ds_list_size(median_list) / 2];
        ds_list_clear(median_list);
    }
}

ds_list_destroy(median_list);

And the result was...

ezgif.com-resize.gif

...impressive!
 
Last edited:
Top