• 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 /
avatar image
Question by Cmushi · Oct 29, 2016 at 06:22 PM · uvmesh color

Mesh Colors Problem

I am reading the pixels of the image below in order to create a mesh out of it:

alt text

I used a multi-color marc$$anonymous$$ng square algorithm and managed to generate a mesh:

alt text

But, as you can see, the colors of the mesh are not being displayed correctly. The following is the class that contains the code that reads a 2d array of colors that contains the rgb code of every pixel of the small image, and creates a mesh out of it:

 using UnityEngine;
 using System.Collections.Generic;
 
 public static class MeshGenerator 
 {
     public static MeshData GenerateMesh(Color[,] importedMap, float squareSize)
     {
         MeshData meshData = new MeshData (importedMap, squareSize);
 
         for (int x = 0; x < meshData.squareGrid.squares.GetLength(0); x++)
         {
             for (int y = 0; y < meshData.squareGrid.squares.GetLength(1); y++)
             {
                 meshData.TriangulateSquare(meshData.squareGrid.squares[x,y]);
             }
         }
 
         return meshData;
     }
 }
 
 public class MeshData
 {
     public SquareGrid squareGrid;
     List<Vector3> meshVertices;
     List<int> meshTriangles;
     List<Color> meshColors;
     List<Vector2> meshUVs;
 
     public MeshData(Color[,] map, float squareSize)
     {
         squareGrid = new SquareGrid(map, squareSize);
         meshVertices = new List<Vector3>();
         meshTriangles = new List<int>();
         meshColors = new List<Color>();
         meshUVs = new List<Vector2> ();
     }
 
     public Mesh CreateMesh()
     {
         Mesh mesh = new Mesh ();
         mesh.vertices = meshVertices.ToArray();
         mesh.triangles = meshTriangles.ToArray();
         mesh.colors = meshColors.ToArray();
         mesh.uv = meshUVs.ToArray ();
         mesh.RecalculateNormals ();
         return mesh;
     }
 
     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 to fix
         else if ((!square.topLeftColor.Equals(square.topRightColor)) && (square.topRightColor.Equals(square.bottomRightColor)) && (square.topRightColor.Equals(square.bottomLeftColor)))
         {
             MeshFromPoints(square.centreTop, square.centreLeft, square.topLeft);
             MeshFromPoints(square.centreLeft, square.centreTop, square.topRight, square.bottomRight, square.bottomLeft);
         }
         // 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 to fix
         else if ((square.topLeftColor.Equals(square.topRightColor)) && (!square.topLeftColor.Equals(square.bottomRightColor)) && (square.topLeftColor.Equals(square.bottomLeftColor)))
         {
             MeshFromPoints(square.centreBottom, square.bottomLeft, square.topLeft, square.topRight, square.centreRight);
             MeshFromPoints(square.centreRight, square.bottomRight, square.centreBottom);
         }
         // 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);
         }
         // 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);
             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, square.middle);
             MeshFromPoints(square.centreBottom, square.bottomLeft, square.centreLeft, square.middle);
         }
         // 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);
         }
         // 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);
             MeshFromPoints(square.centreTop, square.topRight, square.centreRight, square.middle);
             MeshFromPoints(square.centreRight, square.bottomRight, square.centreBottom, square.middle);
         }
         // 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.middle, square.centreLeft);
             MeshFromPoints(square.centreBottom, square.bottomLeft, square.centreLeft, square.middle);
             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.middle, square.centreLeft);
             MeshFromPoints(square.centreTop, square.topRight, square.centreRight, square.middle);
             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.middle, square.centreLeft);
             MeshFromPoints(square.centreTop, square.topRight, square.centreRight, square.middle);
             MeshFromPoints(square.centreRight, square.bottomRight, square.centreBottom, square.middle);
             MeshFromPoints(square.centreBottom, square.bottomLeft, square.centreLeft, square.middle);
         }
         else
         {
             Debug.Log("Error: Triangulation error");
         }
     }
 
     void MeshFromPoints(params 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);
                 meshColors.Add(points[i].color);
                 meshUVs.Add(points[i].position);
             }
         }
         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 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, middle;
         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;
             middle = bottomLeft.across;
             topLeftColor = topLeft.color;
             topRightColor = topRight.color;
             bottomRightColor = bottomRight.color;
             bottomLeftColor = bottomLeft.color;
         }
     }
 
     public class Node
     {
         public Vector3 position;
         public int vertexIndex = -1;
         public Color color;
 
         public Node (Vector3 _pos, Color _color)
         {
             position = _pos;
             color = _color;
         }
     }
 
     public class ControlNode : Node
     {
         public Node above, right, across;
 
         public ControlNode(Vector3 _pos, Color _color, float squareSize) : base(_pos, _color)
         {
             above = new Node(position + Vector3.forward * squareSize / 2f, color);
             right = new Node(position + Vector3.right * squareSize / 2f, color);
             across = new Node(position + Vector3.forward * squareSize / 2f + Vector3.right * squareSize / 2f, color);
         }
     }
 }

Can someone identify what I did wrong?

testprov4.png (941 B)
mesh.png (144.3 kB)
Comment

People who like this

0 Show 7
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
avatar image Sergio7888 · Oct 30, 2016 at 02:02 AM 0
Share

Mesh.colors is to set Vertex color, to set the mesh color you need use a Material in a MeshRenderer.

avatar image Cmushi Sergio7888 · Oct 30, 2016 at 06:39 PM 0
Share

I have a material already assigned. Is there a particular material that I should be using?

avatar image Eric5h5 Cmushi · Oct 30, 2016 at 06:46 PM 0
Share

A material with a shader that uses vertex colors.

Show more comments
Show more comments

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Assigning UV Map to model at runtime 0 Answers

Reimporting during play changes UVs/Tiling? 0 Answers

How to assign UV coordinates to arbitrary mesh. 1 Answer

Custom skybox Shader uv issue, skybox texture stretching along one axis. 0 Answers

Blender Model Texture not seamless in Unity 0 Answers


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