Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Cmushi · Oct 10, 2016 at 04:47 PM · mesh color

Multi-color Marching Square

I'm using the marching square algorithm from the procedural cave tutorial (link) to create a world map mesh which uses a bitmap to determine the provinces position and shape (like this). 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);
             meshColors.Add(square.topLeftColor);
         }
         // 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);
             meshColors.Add(square.topLeftColor);
             MeshFromPoints(square.centreTop, square.topRight, square.bottomRight, square.bottomLeft, square.centreLeft);
             meshColors.Add(square.topRightColor);
         }
         // 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);
             meshColors.Add(square.topRightColor);
             MeshFromPoints(square.topLeft, square.centreTop, square.centreRight, square.bottomRight, square.bottomLeft);
             meshColors.Add(square.topLeftColor);
         }
         // 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);
             meshColors.Add(square.bottomRightColor);
             MeshFromPoints(square.topLeft, square.topRight, square.centreRight, square.centreBottom, square.bottomLeft);
             meshColors.Add(square.topLeftColor);
         }
         // 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);
             meshColors.Add(square.bottomLeftColor);
             MeshFromPoints(square.topLeft, square.topRight, square.bottomRight, square.centreBottom, square.centreLeft);
             meshColors.Add(square.topLeftColor);
         }
         // 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);
             meshColors.Add(square.topLeftColor);
             MeshFromPoints(square.centreRight, square.bottomRight, square.bottomLeft, square.centreLeft);
             meshColors.Add(square.bottomRightColor);
         }
         // 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);
             meshColors.Add(square.topLeftColor);
             MeshFromPoints(square.centreTop, square.topRight, square.centreRight);
             meshColors.Add(square.topRightColor);
             MeshFromPoints(square.centreBottom, square.bottomLeft, square.centreLeft);
             meshColors.Add(square.bottomLeftColor);
 
         }
         // AD BC
         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);
             meshColors.Add(square.topLeftColor);
             MeshFromPoints(square.centreTop, square.topRight, square.bottomRight, square.centreBottom);
             meshColors.Add(square.topRightColor);
         }
         // 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);
             meshColors.Add(square.topLeftColor);
             MeshFromPoints(square.centreRight, square.bottomRight, square.centreBottom);
             meshColors.Add(square.bottomRightColor);
             MeshFromPoints(square.centreBottom, square.bottomLeft, square.centreLeft);
             meshColors.Add(square.bottomLeftColor);
         }
         // 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);
             meshColors.Add(square.topLeftColor);
             MeshFromPoints(square.centreTop, square.topRight, square.centreRight);
             meshColors.Add(square.topRightColor);
             MeshFromPoints(square.centreBottom, square.bottomLeft, square.centreLeft);
             meshColors.Add(square.bottomLeftColor);
         }
         // AD B C
         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);
             meshColors.Add(square.topLeftColor);
             MeshFromPoints(square.centreTop, square.topRight, square.centreRight);
             meshColors.Add(square.topRightColor);
             MeshFromPoints(square.centreRight, square.bottomRight, square.centreBottom);
             meshColors.Add(square.bottomRightColor);
         }
         // 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);
             meshColors.Add(square.topLeftColor);
             MeshFromPoints(square.centreBottom, square.bottomLeft, square.centreLeft);
             meshColors.Add(square.bottomLeftColor);
             MeshFromPoints(square.centreTop, square.topRight, square.bottomRight, square.centreBottom);
             meshColors.Add(square.topRightColor);
         }
         // 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);
             meshColors.Add(square.topLeftColor);
             MeshFromPoints(square.centreRight, square.bottomRight, square.centreBottom);
             meshColors.Add(square.bottomRightColor);
             MeshFromPoints(square.centreTop, square.topRight, square.centreRight, square.centreBottom, square.bottomLeft, square.centreLeft);
             meshColors.Add(square.topRightColor);
         }
         // 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);
             meshColors.Add(square.topLeftColor);
             MeshFromPoints(square.centreTop, square.topRight, square.centreRight);
             meshColors.Add(square.topRightColor);
             MeshFromPoints(square.centreRight, square.bottomRight, square.bottomLeft, square.centreLeft);
             meshColors.Add(square.bottomRightColor);
         }
         // 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);
             meshColors.Add(square.topLeftColor);
             MeshFromPoints(square.centreTop, square.topRight, square.centreRight);
             meshColors.Add(square.topRightColor);
             MeshFromPoints(square.centreRight, square.bottomRight, square.centreBottom);
             meshColors.Add(square.bottomRightColor);
             MeshFromPoints(square.centreBottom, square.bottomLeft, square.centreLeft);
             meshColors.Add(square.bottomLeftColor);
         }
         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[i].vertexIndex == -1)
             {
                 points[i].vertexIndex = meshVertices.Count;
                 meshVertices.Add(points[i].position);
             }
         }
     }
 
     void CreateTriangle(Node a, Node b, Node c)
     {
         meshTriangles.Add(a.vertexIndex);
         meshTriangles.Add(b.vertexIndex);
         meshTriangles.Add(c.vertexIndex);
     }
 
     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>();
         meshGenerator.GenerateMesh(mapArray, 1);
     }
 
     public void ImageToArray()
     {
         mapImage = (Texture2D)Resources.Load(mapImagePath, typeof(Texture2D));
         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)

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
â–¼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Cmushi · Oct 11, 2016 at 05:43 PM

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
â–¼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

75 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Change a single vertex color after mesh is created? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges