- Home /
How do I go about texturing a flat-shaded generated mesh?
So far I've come up with code that generates the mesh without sharing vertices for different triangles, in order to achieve a flat-shaded style. The problem comes when I try to texture it using height map values as guides.
At the moment the outcome looks like this:
and as seen from above:
I want to remove the possibility of only some vertices in a triangle to be coloured. I want either all three, or none, but I have no idea of how to achieve this.
The other problem I'm having is that some blue appears on the top and right of the mesh which I suppose is an issue with overlapping.
Any help would be greatly appreciated!
My code is as follows:
private void GenerateChunk(int width, int height, float vSpacing)
{
Mesh mesh = new Mesh();
mesh.name = "newMesh";
List<List<Vector3>> vertices = new List<List<Vector3>>();
List<int> triangles = new List<int>();
List<Vector2> uvs = new List<Vector2>();
float vertexSpacing = vSpacing;
float[,] heightMap = new float[width * width, height * height];
int count = 0;
for (float z = 0; z < height; z++)
{
vertices.Add(new List<Vector3>());
for (float x = 0; x < width; x++)
{
float intensity = 50.0f;
for (int f = 0; f < 6; f++)
{
float xData = 0;
float zData = 0;
float yData = 0;
switch (f)
{
case 0: { xData = x * vertexSpacing; zData = z * vertexSpacing; break; }
case 1: { xData = (x * vertexSpacing) + vertexSpacing; zData = z * vertexSpacing; break; }
case 2: { xData = x * vertexSpacing; zData = (z * vertexSpacing) - vertexSpacing; break; }
case 3: { xData = x * vertexSpacing; zData = (z * vertexSpacing) - vertexSpacing; break; }
case 4: { xData = (x * vertexSpacing) + vertexSpacing; zData = (z * vertexSpacing) - vertexSpacing; break; }
case 5: { xData = (x * vertexSpacing) + vertexSpacing; zData = z * vertexSpacing; break; }
}
float normalGround = TerrainGen.GroundNoise.FractalNoise2D(xData, zData, 4, TerrainGen.GroundFrequency, 1.2f) + 0.1f;
float highGround = Mathf.Max(0.0f, TerrainGen.MountainNoise.FractalNoise2D(xData, zData, 6, TerrainGen.MountainFrequency, 5.4f));
heightMap[(int)z, (int)x] = (normalGround + highGround);
yData = (normalGround + highGround) * intensity;
Vector3 point = new Vector3(xData, yData, zData);
vertices[(int)z].Add(point);
}
// uv test
Vector2 uvVect = new Vector2(x / width, z / height);
Vector2 uvVect2 = new Vector2((x + 1) / width, z / height);
Vector2 uvVect3 = new Vector2(x / width, (z + 1) / height);
uvs.Add(uvVect);
uvs.Add(uvVect2);
uvs.Add(uvVect3);
Vector2 uvVect4 = new Vector2((x + 1) / width, z / height);
Vector2 uvVect5 = new Vector2((x + 1) / width, (z + 1) / height);
Vector2 uvVect6 = new Vector2(x / width, (z + 1) / height);
uvs.Add(uvVect4);
uvs.Add(uvVect5);
uvs.Add(uvVect6);
// uv test
triangles.Add(count);
triangles.Add(count + 1);
triangles.Add(count + 2);
triangles.Add(count + 5);
triangles.Add(count + 4);
triangles.Add(count + 3);
count += 6;
}
}
Vector3[] finalVertices = new Vector3[width * height * 6];
int k = 0;
foreach (List<Vector3> vList in vertices)
{
foreach (Vector3 v3 in vList)
{
finalVertices[k] = v3;
k++;
}
}
mesh.vertices = finalVertices;
mesh.uv = uvs.ToArray();
mesh.triangles = triangles.ToArray();
mesh.RecalculateNormals();
GameObject gameObject = new GameObject();
MeshFilter meshFilter = (MeshFilter)gameObject.AddComponent(typeof(MeshFilter));
meshFilter.mesh = mesh;
MeshRenderer meshRenderer = (MeshRenderer)gameObject.AddComponent(typeof(MeshRenderer));
meshRenderer.material.shader = Shader.Find("Diffuse");
// Test painting
Texture2D texture = new Texture2D(width, height);
for (int tx = 0; tx < width; tx++)
{
for (int ty = 0; ty < height; ty++)
{
Debug.Log(heightMap[ty, tx]);
if (heightMap[ty, tx] < 0.6f)
{
texture.SetPixel(tx, ty, Color.blue);
}
else if (heightMap[ty, tx] < 1.5f && heightMap[ty, tx] > 0.6f)
{
texture.SetPixel(tx, ty, Color.green);
}
}
}
texture.Apply();
// Test painting
meshRenderer.material.mainTexture = texture;
meshRenderer.material.color = Color.white;
Debug.Log("Tris: " + mesh.triangles.Length / 3);
Debug.Log("Verts: " + mesh.vertices.Length);
}
}
Answer by Reselence · Jun 30, 2014 at 07:19 PM
Got it working by using mesh.colors32 instead of creating a texture. It's much simpler this way and does what I need it to do.
Outcome:
@Reselence How do you mesh.color32 work? I tried it but I see no change. Its pure white. I think it might be because of the "Diffuse" shader. How is ur terrain showing color?
Your answer
Follow this Question
Related Questions
"Baking" to 2d UV Map? 1 Answer
Discrete divisions on terrain? 0 Answers
How to texture walls in procedurally generated mesh? 1 Answer
Assigning UV Map to model at runtime 0 Answers