Detecting corners in a grid

My problem is a little bit complex so bear with me as I explain it. I’m build a perlin noise-based procedural generation algorithm that goes through the following steps:

  1. Generates a list of perlin noise values.
  2. Flattens those values out into values for tiles.
  3. Instantiates a tile based on the value.

The generated terrain looks something like this:


The problem I can’t solve is that I want it to instantiate tiles that slope down to reach lower tiles at the edges of tile regions, areas like where the red tiles meet the green and green meets the yellow. In short, I would like to be able to do the following:

  1. Loop through the list of tile values.
  2. Determine if the tiles that border the tile are lower than it and where they are in location to the tile in question.
  3. Based on the location of those tiles, determine if the tile is on an edge or a convex or concave corner.

I know this is a bit of complicated request, but I’ve tried to think of a way to do this without any luck. If someone knows of a way to do this, I would appreciate it.

I assume you have a two dimensional array of height values, generated in step 2 of your algorithm. If run time is not an issue, you can run another scan after the initial array is generated and manually check whether each tile neighbours lower tile using an if statement that defines the conditions you described, and create a second array using the enum I described below.

A more time efficient solution would be to run a compute shader using a kernel that describes each slope direction, but that’s not very simple, and I would advise trying to brute force the issue before trying that.

enum SlopeDirection {
  flat,
  up,
  upLeft,
  upRight,
  etc.
}