If you want to generate it then you have to write a script to procedurally generate a mesh based on some kind of algorithm. Poly world is a good asset on the asset store, check it out.
something like that would be accomplished by making a mesh with each triangle “separate” from all others, so that the vertices of each face have normals that match the face normal. it isn’t possible to do with a mesh that shares vertices between faces, because each vertex can only have one normal. the lighting is based on the vertex normal.
i don’t know how unity handles terrain objects, or if you could split the vertices to keep separate normals. you may end up having to implement some custom terrain thing yourself in order to generate a mesh with split vertices/normals.
It’s just vertex colors. In addition to supplying vertices, you also supply an array of RGBA colors, one for each vertex, so that means you need to repeat the same color 3 times, for 3 vertices of each triangle. It’ll then look flat shaded because the whole triangle is the same color. The hardware will still interpolate color between the vertices but it will look like it’s all the same color.
@imaginaryhuman_1
But how I can get the colors from my terrain?
I can’t find something useful on the API. Just something about alphamaps. But I don’t know how to handle it.
Vector3[] vertices = mesh.vertices;
Color[] colors = new Color[vertices.Length];
for (int i = 0; i < vertices.Length; i = i + 3)
{
if (i + 2 < vertices.Length)
{
Color32 color = new Color32((byte) (50 * Random.Range(0.8f, 1.2f)), (byte) (200 * Random.Range(0.8f, 1.2f)),(byte) (50 * Random.Range(0.8f, 1.2f)) ,255);
colors[i] = color;
colors[i + 1] = color;
colors[i + 2] = color;
}
}
mesh.colors = colors;
And I have a little shader.
But now I don’t know how to get the colors from my terrain.
I have the idea to iterate through the alpha maps and check wich texture is set and get the color from it but I am not sure how to iterate through because vertices.Length is not equal to d.alphamapHeight * td.alphamapWidth (20.000 : 200.000)
Has anyone an idea how to deal with it or a solution for my problem describing one post earlier?
Looks like the colors at each of the 3 corners of a triangle are NOT the same, because there is some gauraud shading showing. They don’t look flat. I thought you wanted the whole triangle to be the same color? maybe your Triangles array is sharing some vertices? The triangles array must be set as well as the vertices array, so that it doesn’t share vertices, each triangle comprising three distinct vertex indexes.
To get colors from your texture you need to make the texture readable and then use like GetPixel() or something to read the pixel values, and feed them into your mesh.
Thank you very much guy!!
I used this code to make an unshared vertex mesh.
int[] sourceIndices = mesh.GetTriangles(0);
Vector3[] sourceVerts = mesh.vertices;
Vector2[] sourceUVs = mesh.uv;
int[] newIndices = new int[sourceIndices.Length];
Vector3[] newVertices = new Vector3[sourceIndices.Length];
Vector2[] newUVs = new Vector2[sourceIndices.Length];
// Create a unique vertex for every index in the original Mesh:
for(int i = 0; i < sourceIndices.Length; i++)
{
newIndices[i] = i;
newVertices[i] = sourceVerts[sourceIndices[i]];
newUVs[i] = sourceUVs[sourceIndices[i]];
}
Mesh unsharedVertexMesh = new Mesh();
unsharedVertexMesh.vertices = newVertices;
unsharedVertexMesh.uv = newUVs;
unsharedVertexMesh.SetTriangles(newIndices, 0);
With getting the color from my terrain I think you don’t understand my problem.
Width x length of my terrain is = 262144
The colors and me vertecies length is only = 24576
So I don’t know how to loop to my terrain because I don’t know on which xy-coordinate I have to set my color for example at position 100.
Does anyone have a solution for it here?
I think what I can do ist to read the value when I’m creating the mesh from the terrain.
Then I have the exact vertex point on my terrain and mesh and can save the color in a seperate array.
Is this possible?