Question by
HeavyBro · Jan 04, 2021 at 10:17 PM ·
vertex color
How to smooth vertex colors?
Hello, this may sound like a stupid question, but I'm trying to solve the problem like for hours. I want to make vertex colors smooth between them. I tried it via code, but I only get this (See picture). I don't have any knowledges in shaders and I guess it's the only way to solve this. Maybe it is possible with C#. Isn't it? Just in case here's my code:
for (int x = 0; x < (int)Size.x; x++)
{
for (int y = 0; y < (int)Size.y; y++)
{
int index = x * (int)Size.y + y;
Debug.Log(index + " " + vertices.Length);
vertices[index * verticesCount + 0] = new Vector3(meshSize * x, meshSize * y);
vertices[index * verticesCount + 1] = new Vector3(meshSize * x, meshSize * (y + 1));
vertices[index * verticesCount + 2] = new Vector3(meshSize * (x + 1), meshSize * (y + 1));
vertices[index * verticesCount + 3] = new Vector3(meshSize * (x + 1), meshSize * y);
uv[index * verticesCount + 0] = new Vector2(0, 0);
uv[index * verticesCount + 1] = new Vector2(0, 1);
uv[index * verticesCount + 2] = new Vector2(1, 1);
uv[index * verticesCount + 3] = new Vector2(1, 0);
triangles[index * trianglesCount + 0] = index * 4 + 0;
triangles[index * trianglesCount + 1] = index * 4 + 1;
triangles[index * trianglesCount + 2] = index * 4 + 2;
triangles[index * trianglesCount + 3] = index * 4 + 0;
triangles[index * trianglesCount + 4] = index * 4 + 2;
triangles[index * trianglesCount + 5] = index * 4 + 3;
if (x == 1 && y == 1) // Center
{ // Ignore all
colors[index * verticesCount + 0] = newCanvas[position.x, position.y].color;
colors[index * verticesCount + 1] = newCanvas[position.x, position.y].color;
colors[index * verticesCount + 2] = newCanvas[position.x, position.y].color;
colors[index * verticesCount + 3] = newCanvas[position.x, position.y].color;
continue;
}
if (x == 0 && y == 0) // Bottom Left (2)
{
colors[index * verticesCount + 0] = Color.Lerp(newCanvas[position.x, position.y].color, newCanvas[position.x - 1, position.y - 1].color, 0.5f);
colors[index * verticesCount + 1] = Color.Lerp(newCanvas[position.x, position.y].color, newCanvas[position.x + 1, position.y].color, 0.5f);
colors[index * verticesCount + 2] = newCanvas[position.x, position.y].color; // Ignore
colors[index * verticesCount + 3] = Color.Lerp(newCanvas[position.x, position.y].color, newCanvas[position.x, position.y - 1].color, 0.5f);
continue;
}
if (x == 0 && y == 1) // Middle Left (2,3)
{
colors[index * verticesCount + 0] = Color.Lerp(newCanvas[position.x, position.y].color, newCanvas[position.x - 1, position.y].color, 0.5f);
colors[index * verticesCount + 1] = Color.Lerp(newCanvas[position.x, position.y].color, newCanvas[position.x - 1, position.y].color, 0.5f);
colors[index * verticesCount + 2] = newCanvas[position.x, position.y].color; // Ignore
colors[index * verticesCount + 3] = newCanvas[position.x, position.y].color; // Ignore
continue;
}
if (x == 2 && y == 1) // Middle Right (1,0)
{
colors[index * verticesCount + 0] = newCanvas[position.x, position.y].color; // Ignore
colors[index * verticesCount + 1] = newCanvas[position.x, position.y].color; // Ignore
colors[index * verticesCount + 2] = Color.Lerp(newCanvas[position.x, position.y].color, newCanvas[position.x + 1, position.y].color, 0.5f);
colors[index * verticesCount + 3] = Color.Lerp(newCanvas[position.x, position.y].color, newCanvas[position.x + 1, position.y].color, 0.5f);
continue;
}
if (x == 1 && y == 0) // Bottom Middle (1,2)
{
colors[index * verticesCount + 0] = Color.Lerp(newCanvas[position.x, position.y].color, newCanvas[position.x, position.y - 1].color, 0.5f);
colors[index * verticesCount + 1] = newCanvas[position.x, position.y].color; // Ignore
colors[index * verticesCount + 2] = newCanvas[position.x, position.y].color; // Ignore
colors[index * verticesCount + 3] = Color.Lerp(newCanvas[position.x, position.y].color, newCanvas[position.x, position.y - 1].color, 0.5f);
continue;
}
if (x == 1 && y == 2) // Top Middle (0,3)
{
colors[index * verticesCount + 0] = newCanvas[position.x, position.y].color; // Ignore
colors[index * verticesCount + 1] = Color.Lerp(newCanvas[position.x, position.y].color, newCanvas[position.x, position.y + 1].color, 0.5f);
colors[index * verticesCount + 2] = Color.Lerp(newCanvas[position.x, position.y].color, newCanvas[position.x, position.y + 1].color, 0.5f);
colors[index * verticesCount + 3] = newCanvas[position.x, position.y].color; // Ignore
continue;
}
if (x == 0 && y == 2) // Top Left (3)
{
colors[index * verticesCount + 0] = Color.Lerp(newCanvas[position.x, position.y].color, newCanvas[position.x - 1, position.y].color, 0.5f);
colors[index * verticesCount + 1] = Color.Lerp(newCanvas[position.x, position.y].color, newCanvas[position.x - 1, position.y + 1].color, 0.5f);
colors[index * verticesCount + 2] = Color.Lerp(newCanvas[position.x, position.y].color, newCanvas[position.x, position.y + 1].color, 0.5f);
colors[index * verticesCount + 3] = newCanvas[position.x, position.y].color; // Ignore
continue;
}
if (x == 2 && y == 0) // Bottom Right (1)
{
colors[index * verticesCount + 0] = Color.Lerp(newCanvas[position.x, position.y].color, newCanvas[position.x, position.y - 1].color, 0.5f);
colors[index * verticesCount + 1] = newCanvas[position.x, position.y].color; // Ignore
colors[index * verticesCount + 2] = Color.Lerp(newCanvas[position.x, position.y].color, newCanvas[position.x + 1, position.y].color, 0.5f);
colors[index * verticesCount + 3] = Color.Lerp(newCanvas[position.x, position.y].color, newCanvas[position.x + 1, position.y - 1].color, 0.5f);
continue;
}
if (x == 2 && y == 2) // Top Right (0)
{
colors[index * verticesCount + 0] = newCanvas[position.x, position.y].color; // Ignore
colors[index * verticesCount + 1] = Color.Lerp(newCanvas[position.x, position.y].color, newCanvas[position.x, position.y + 1].color, 0.5f);
colors[index * verticesCount + 2] = Color.Lerp(newCanvas[position.x, position.y].color, newCanvas[position.x + 1, position.y + 1].color, 0.5f);
colors[index * verticesCount + 3] = Color.Lerp(newCanvas[position.x, position.y].color, newCanvas[position.x + 1, position.y].color, 0.5f);
continue;
}
}
}
problem.png
(36.9 kB)
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613022555im_/https://answers.unity.com/themes/thub/images/avi.jpg)