# Multi-color Marching Square

I’m using the marching square algorithm from the procedural cave tutorial ([1]) to create a world map mesh which uses a bitmap to determine the provinces position and shape ([like this][2]). The algorithm in the tutorial can only support 2 states, black which represents the walls and white which represents the ground. I modified the algorithm to allow the algorithm to support more than 2 states by storing the color of each pixel with the nodes and replacing the configuration switch with an if statements that check the color of the nodes and determine the triangles and colors of the mesh.

``````using UnityEngine;
using System.Collections.Generic;

public class MeshGenerator : MonoBehaviour
{
public SquareGrid squareGrid;
List<Vector3> meshVertices;
List<int> meshTriangles;
List<Color> meshColors;

public void GenerateMesh(Color[,] map, float squareSize)
{
squareGrid = new SquareGrid(map, squareSize);
meshVertices = new List<Vector3>();
meshTriangles = new List<int>();
meshColors = new List<Color>();

for (int x = 0; x < squareGrid.squares.GetLength(0); x++)
{
for (int y = 0; y < squareGrid.squares.GetLength(1); y++)
{
TriangulateSquare(squareGrid.squares[x,y]);
}
}
Mesh mesh = new Mesh();
GetComponent<MeshFilter>().mesh = mesh;

mesh.vertices = meshVertices.ToArray();
mesh.triangles = meshTriangles.ToArray();
mesh.colors = meshColors.ToArray();
mesh.RecalculateNormals();
}

public void TriangulateSquare(Square square)
{
// ABCD
if((square.topLeftColor.Equals(square.topRightColor)) && (square.topLeftColor.Equals(square.bottomRightColor)) && (square.topLeftColor.Equals(square.bottomLeftColor)))
{
MeshFromPoints(square.topLeft, square.topRight, square.bottomRight, square.bottomLeft);
}
// BCD A
else if ((!square.topLeftColor.Equals(square.topRightColor)) && (square.topRightColor.Equals(square.bottomRightColor)) && (square.topRightColor.Equals(square.bottomLeftColor)))
{
MeshFromPoints(square.topLeft, square.centreTop, square.centreLeft);
MeshFromPoints(square.centreTop, square.topRight, square.bottomRight, square.bottomLeft, square.centreLeft);
}
// ACD B
else if ((!square.topLeftColor.Equals(square.topRightColor)) && (square.topLeftColor.Equals(square.bottomRightColor)) && (square.bottomRightColor.Equals(square.bottomLeftColor)))
{
MeshFromPoints(square.centreTop, square.topRight, square.centreRight);
MeshFromPoints(square.topLeft, square.centreTop, square.centreRight, square.bottomRight, square.bottomLeft);
}
// ABD C
else if ((square.topLeftColor.Equals(square.topRightColor)) && (!square.topLeftColor.Equals(square.bottomRightColor)) && (square.topLeftColor.Equals(square.bottomLeftColor)))
{
MeshFromPoints(square.centreRight, square.bottomRight, square.centreBottom);
MeshFromPoints(square.topLeft, square.topRight, square.centreRight, square.centreBottom, square.bottomLeft);
}
// ABC D
else if ((square.topLeftColor.Equals(square.topRightColor)) && (square.topLeftColor.Equals(square.bottomRightColor)) && (!square.topLeftColor.Equals(square.bottomLeftColor)))
{
MeshFromPoints(square.centreBottom, square.bottomLeft, square.centreLeft);
MeshFromPoints(square.topLeft, square.topRight, square.bottomRight, square.centreBottom, square.centreLeft);
}
// AB CD
else if ((square.topLeftColor.Equals(square.topRightColor)) && (!square.topLeftColor.Equals(square.bottomRightColor)) && (square.bottomRightColor.Equals(square.bottomLeftColor)))
{
MeshFromPoints(square.topLeft, square.topRight, square.centreRight, square.centreLeft);
MeshFromPoints(square.centreRight, square.bottomRight, square.bottomLeft, square.centreLeft);
}
// AC BD
else if ((square.topLeftColor.Equals(square.bottomRightColor)) && (!square.topLeftColor.Equals(square.topRightColor)) && (square.topRightColor.Equals(square.bottomLeftColor)))
{
MeshFromPoints(square.topLeft, square.centreTop, square.centreRight, square.bottomRight, square.centreBottom, square.centreLeft);
MeshFromPoints(square.centreTop, square.topRight, square.centreRight);
MeshFromPoints(square.centreBottom, square.bottomLeft, square.centreLeft);

}
else if ((square.topLeftColor.Equals(square.bottomLeftColor)) && (!square.topLeftColor.Equals(square.topRightColor)) && (square.topRightColor.Equals(square.bottomRightColor)))
{
MeshFromPoints(square.topLeft, square.centreTop, square.centreBottom, square.bottomLeft);
MeshFromPoints(square.centreTop, square.topRight, square.bottomRight, square.centreBottom);
}
// AB C D
else if ((square.topLeftColor.Equals(square.topRightColor)) && (!square.topLeftColor.Equals(square.bottomRightColor)) && (!square.bottomRightColor.Equals(square.bottomLeftColor)))
{
MeshFromPoints(square.topLeft, square.topRight, square.centreRight, square.centreLeft);
MeshFromPoints(square.centreRight, square.bottomRight, square.centreBottom);
MeshFromPoints(square.centreBottom, square.bottomLeft, square.centreLeft);
}
// AC B D
else if ((square.topLeftColor.Equals(square.bottomRightColor)) && (!square.topLeftColor.Equals(square.topRightColor)) && (!square.topRightColor.Equals(square.bottomLeftColor)))
{
MeshFromPoints(square.topLeft, square.centreTop, square.centreRight, square.bottomRight, square.centreBottom, square.centreLeft);
MeshFromPoints(square.centreTop, square.topRight, square.centreRight);
MeshFromPoints(square.centreBottom, square.bottomLeft, square.centreLeft);
}
else if ((square.topLeftColor.Equals(square.bottomLeftColor)) && (!square.topLeftColor.Equals(square.topRightColor)) && (!square.topRightColor.Equals(square.bottomRightColor)))
{
MeshFromPoints(square.topLeft, square.centreTop, square.centreBottom, square.bottomLeft);
MeshFromPoints(square.centreTop, square.topRight, square.centreRight);
MeshFromPoints(square.centreRight, square.bottomRight, square.centreBottom);
}
// A D BC
else if ((!square.topLeftColor.Equals(square.bottomLeftColor)) && (!square.topLeftColor.Equals(square.topRightColor)) && (square.topRightColor.Equals(square.bottomRightColor)))
{
MeshFromPoints(square.topLeft, square.centreTop, square.centreLeft);
MeshFromPoints(square.centreBottom, square.bottomLeft, square.centreLeft);
MeshFromPoints(square.centreTop, square.topRight, square.bottomRight, square.centreBottom);
}
// A C BD
else if ((!square.topLeftColor.Equals(square.bottomRightColor)) && (!square.topLeftColor.Equals(square.topRightColor)) && (square.topRightColor.Equals(square.bottomLeftColor)))
{
MeshFromPoints(square.topLeft, square.centreTop, square.centreLeft);
MeshFromPoints(square.centreRight, square.bottomRight, square.centreBottom);
MeshFromPoints(square.centreTop, square.topRight, square.centreRight, square.centreBottom, square.bottomLeft, square.centreLeft);
}
// A B CD
else if ((!square.topLeftColor.Equals(square.topRightColor)) && (!square.topLeftColor.Equals(square.bottomRightColor)) && (square.bottomRightColor.Equals(square.bottomLeftColor)))
{
MeshFromPoints(square.topLeft, square.centreTop, square.centreLeft);
MeshFromPoints(square.centreTop, square.topRight, square.centreRight);
MeshFromPoints(square.centreRight, square.bottomRight, square.bottomLeft, square.centreLeft);
}
// A B C D
else if ((!square.topLeftColor.Equals(square.topRightColor)) && (!square.topLeftColor.Equals(square.bottomRightColor)) && (!square.topLeftColor.Equals(square.bottomLeftColor)))
{
MeshFromPoints(square.topLeft, square.centreTop, square.centreLeft);
MeshFromPoints(square.centreTop, square.topRight, square.centreRight);
MeshFromPoints(square.centreRight, square.bottomRight, square.centreBottom);
MeshFromPoints(square.centreBottom, square.bottomLeft, square.centreLeft);
}
else
{
Debug.Log("Error: Triangulation error");
}
}

void MeshFromPoints(params Node[] points)
{
AssignVertices(points);

if (points.Length >= 3)
CreateTriangle(points[0], points[1], points[2]);
if (points.Length >= 4)
CreateTriangle(points[0], points[2], points[3]);
if (points.Length >= 5)
CreateTriangle(points[0], points[3], points[4]);
if (points.Length >= 6)
CreateTriangle(points[0], points[4], points[5]);
}

void AssignVertices(Node[] points)
{
for (int i = 0; i < points.Length; i++)
{
if (points*.vertexIndex == -1)*
``````

{
points*.vertexIndex = meshVertices.Count;*
}
}
}

void CreateTriangle(Node a, Node b, Node c)
{
}

public class SquareGrid
{
public Square[,] squares;

public SquareGrid(Color[,] map, float squareSize)
{
int nodeCountX = map.GetLength(0);
int nodeCountY = map.GetLength(1);
float mapWidth = nodeCountX * squareSize;
float mapHeight = nodeCountY * squareSize;
ControlNode[,] controlNodes = new ControlNode[nodeCountX, nodeCountY];

for (int x = 0; x < nodeCountX; x++)
{
for (int y = 0; y < nodeCountY; y++)
{
Vector3 pos = new Vector3(-mapWidth / 2 + x * squareSize + squareSize / 2, 0, -mapHeight / 2 + y * squareSize + squareSize / 2);
controlNodes[x, y] = new ControlNode(pos, map[x, y], squareSize);
}
}
squares = new Square[nodeCountX - 1, nodeCountY - 1];
for (int x = 0; x < nodeCountX - 1; x++)
{
for (int y = 0; y < nodeCountY - 1; y++)
{
squares[x, y] = new Square(controlNodes[x, y + 1], controlNodes[x + 1, y + 1], controlNodes[x + 1, y], controlNodes[x, y]);
}
}
}
}

public class Square
{
public ControlNode topLeft, topRight, bottomRight, bottomLeft;
public Node centreTop, centreRight, centreBottom, centreLeft;
public Color topLeftColor, topRightColor, bottomRightColor, bottomLeftColor;

public Square (ControlNode _topLeft, ControlNode _topRight, ControlNode _bottomRight, ControlNode _bottomLeft)
{
topLeft = _topLeft;
topRight = _topRight;
bottomRight = _bottomRight;
bottomLeft = _bottomLeft;
centreTop = topLeft.right;
centreRight = bottomRight.above;
centreBottom = bottomLeft.right;
centreLeft = bottomLeft.above;
topLeftColor = topLeft.color;
topRightColor = topRight.color;
bottomRightColor = bottomRight.color;
bottomLeftColor = bottomLeft.color;
}
}

public class Node
{
public Vector3 position;
public int vertexIndex = -1;

public Node (Vector3 _pos)
{
position = _pos;
}
}

public class ControlNode : Node
{
public Color color;
public Node above, right;

public ControlNode(Vector3 _pos, Color _color, float squareSize) : base(_pos)
{
color = _color;
_ above = new Node(position + Vector3.forward * squareSize / 2f);
right = new Node(position + Vector3.right * squareSize / 2f);
}
}
}
using UnityEngine;_

public class MapGenerator : MonoBehaviour
{
private Texture2D mapImage;
private string mapImagePath = “Map/testprov”;
private Color[,] mapArray;

void Start()
{
ImageToArray();
MeshGenerator meshGenerator = GetComponent();
meshGenerator.GenerateMesh(mapArray, 1);
}

public void ImageToArray()
{
if (mapImage == null)
{
Debug.Log(“Error: mapImage is null”);
return;
}
mapArray = new Color[mapImage.width, mapImage.height];
for (int x = 0; x < mapArray.GetLength(0); x++)
{
for (int y = 0; y < mapArray.GetLength(1); y++)
{
mapArray[x, y] = mapImage.GetPixel(x, y);
}
}
mapImage = null;
}
}

When I click ‘Play’, the following error is displayed.
- Mesh.colors is out of bounds. The supplied array needs to be the same size as the Mesh.vertices array.
- UnityEngine.Mesh:set_colors(Color[])
- MeshGenerator:GenerateMesh(Color[,], Single) (at Assets/MeshGenerator.cs:31)
- MapGenerator:Start() (at Assets/MapGenerator.cs:13)
*[1]: [Unity] Procedural Cave Generation (E01. Cellular Automata) - YouTube
*[2]: http://imgur.com/i2O00Nb*_

I managed to get rid of the errors by moving the variable color from ‘ControlNode’ to ‘Node’ and moved ‘mesh.addColors’ from the if statements to ‘assignVertices’ method.