- Home /
Mesh Colors Problem
I am reading the pixels of the image below in order to create a mesh out of it:
I used a multi-color marching square algorithm and managed to generate a mesh:
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?
$$anonymous$$esh.colors is to set Vertex color, to set the mesh color you need use a $$anonymous$$aterial in a $$anonymous$$eshRenderer.
I have a material already assigned. Is there a particular material that I should be using?
Your answer
Follow this Question
Related Questions
Assigning UV Map to model at runtime 0 Answers
Dynamic Texture Tiling 1 Answer
Generating UVs for a scripted mesh 4 Answers
Procedural Mesh UVing 2 Answers
RaycastHit.textureCoord --- does the reverse exist? 2 Answers