I've done a lot of research on marching squares, including:

But I just can't seem to get my head around it for some reason.

A few questions I have just while going into the algorithm:

  • Are the "blending"/"marching square" tiles the same size as the original tile?

  • Do the "blending" tiles overlay the tiles POST the original render/draw? (I assume there would need to be a transparent portion of the "blending" tile for this to work)

  • The second displaced grid is just completely confusing to me, is that the location where the "blending" tiles would be placed? I thought the "blend" would just be placed at the intersection (so that 1 tile "invades" the other).

Additionally, I was confused about how this would work if there were multiple terrain types.

For example, If I were to have 3 terrain types and they were laid out like this:

0 2

1 2

Where the overlap is 2 -> 1 -> 0. I believe the way I would code this is so that the 1 overlaps the 0, and then the 2 overlaps the 1 & 0. So, if I had 128 x 128 pixel tiles, I would need 32x32 blending tiles for this to work.

The "2" overlap would also end up overlapping 2 of the 32x32 "1" tiles, but I suppose I could just not draw those if they look weird..

The other option I thought of was to simply ignore either the "0" or "1" tile, and have them be converted so that only 2 different tiles could ever sit next to each other..

  • 1
    \$\begingroup\$ Your question about grid offsets sounds like it might be helped by checking out this talk from Oskar Stålberg, where he explains the benefits of thinking about your tiles using the "dual grid". \$\endgroup\$
    – DMGregory
    Feb 3, 2022 at 23:51
  • \$\begingroup\$ Please let me get more details: 1) Do you understand how Marching Cubes or Marching Squares works, in general? 2) Do you need a C++ code? 3) Are you looking for the 1D outlines that encompass the regions with measure 3, measure 2, measure 1, etc.? 4) Are you looking for the 2D triangles that encompass the regions with measure 3, measure 2, measure 1, etc.? I have an existing code, but it doesn't quite do what you want: github.com/sjhalayka/Marching-Squares \$\endgroup\$ Feb 4, 2022 at 1:08
  • \$\begingroup\$ @shawn_halayka 1. I'm not sure if I understand, that is why i asked those 4 questions. I haven't looked into Marching Cubes, I'm trying to do 2D terrain blending with several different tiles, so only require Marching Squares. If you do have existing code that does 2D terrain blending using Marching Squares (Important it would need to be 3+ adjacent tile types, not 2), that would be helpful \$\endgroup\$
    – sohnda
    Feb 4, 2022 at 1:38

1 Answer 1


I made a simple live demo in JavaScript, where you can draw with 3 different materials using layered marching squares: water, soil and grass


Feel free to make a copy of the code and use your own tiles or whatever. If you need to export/import raw data of the map, there is a test version that can handle this:


The source can also be found here: Map tile terrain transitions with 3-4 different types (don't get, how to link the files together here with code snippets … ;)

  • 1
    \$\begingroup\$ This answer would be better if the body of the text outlined the method you used to solve this problem. Right now, if the page you're linking to ever changes or becomes unreachable from certain readers' locations, the answer won't give any clues about how to do this. \$\endgroup\$
    – DMGregory
    Feb 15, 2023 at 15:45
  • \$\begingroup\$ If you'd like help formatting code and other content in your answers, visit the help center and check out the page on formatting posts with Markdown and HTML. \$\endgroup\$
    – DMGregory
    Feb 15, 2023 at 15:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .