- Home /
Mesh Normal
So, i am working on a script what reads terrain data values and makes them into a mesh, i can do it fine with voxels, but when it comes to just pretty much printing the heightmap into a mesh, it has these weird " normal " issues, does this look right? is it actually the normals causing this? or could it be something else?
The flat parts are fine, its the slopes which seem to have normal issues.
Here is my script i am using :
void renderExternalHeightmap()
{
Generator noise;
RidgeNoise temperature = new RidgeNoise(83573);
PinkNoise noiseMod = new PinkNoise(32562356);
temperature.Lacunarity = 0.9f;
temperature.Frequency = 0.01f;
temperature.Offset = 1.2f; // sharpness
temperature.OctaveCount = 8;
noiseMod.Lacunarity = 0.9f;
noiseMod.Frequency = 0.01f;
noiseMod.Persistence = 1.2f; // sharpness
noiseMod.OctaveCount = 8;
noise = noiseMod;
int X, Y, Z;
X = BaseClass.WORLD_CHUNK_DIMENSION_X * BaseClass.WORLDCHUNK_LOADED_LIMIT_X;
Y = BaseClass.WORLD_CHUNK_DIMENSION_Y * BaseClass.WORLDCHUNK_LOADED_LIMIT_Y;
Z = BaseClass.WORLD_CHUNK_DIMENSION_Z * BaseClass.WORLDCHUNK_LOADED_LIMIT_Z;
List<int> triangles = new List<int>();
List<Vector3> vertices = new List<Vector3>();
List<Vector2> uvs = new List<Vector2>();
int index = 0; // index used for triangle work around
for (int i = 0; i < 50; i++)
{
for (int j = 0; j < 1; j++)
{
for (int k = 0; k < 50; k++)
{
vertices.Add(new Vector3(i - 0.5f, Mathf.FloorToInt(noise.GetValue(i - 0.5f, 0.5f, k + 0.5f) * temperature.GetValue(i - 0.5f, 0, k + 0.5f)), k + 0.5f));
vertices.Add(new Vector3(i + 0.5f, Mathf.FloorToInt(noise.GetValue(i + 0.5f, 0.5f, k + 0.5f) * temperature.GetValue(i + 0.5f, 0, k + 0.5f)), k + 0.5f));
vertices.Add(new Vector3(i + 0.5f, Mathf.FloorToInt(noise.GetValue(i + 0.5f, 0.5f, k - 0.5f) * temperature.GetValue(i + 0.5f, 0, k - 0.5f)), k - 0.5f));
vertices.Add(new Vector3(i - 0.5f, Mathf.FloorToInt(noise.GetValue(i - 0.5f, 0.5f, k - 0.5f) * temperature.GetValue(i - 0.5f, 0, k - 0.5f)), k - 0.5f));
triangles.Add(index + 0);
triangles.Add(index + 1);
triangles.Add(index + 2);
triangles.Add(index + 0);
triangles.Add(index + 2);
triangles.Add(index + 3);
index += 4;
}
}
}
Mesh mesh = GetComponent<MeshFilter>().mesh;
Vector4[] tangents = new Vector4[vertices.Count];
for (int i = 0; i < vertices.Count; i++)
{
uvs.Add(new Vector2(vertices[i].x, vertices[i].z));
}
mesh.Clear();
mesh.vertices = vertices.ToArray();
mesh.triangles = triangles.ToArray();
mesh.uv = uvs.ToArray();
mesh.tangents = tangents;
mesh.RecalculateNormals();
mesh.Optimize();
}
Answer by DarkPixel · Oct 08, 2013 at 12:04 AM
First, I think some of your plane are not coplanar. I'm not 100% sure, if you can add a wireframe screenshot, it would help.
Second, If you check the documentation, RecalculateNormals use the shared vertices to calculate the normal. RecalculateNormals
In your case, each plane have 4 unique vertex. Even if the position is the same, you duplicate the vertex. So each vertices on the plane have the plane normal
I think what you want is a smooth shading. You should build all you vertex first, then build the face and reuse the vertices instead of creating unique vertex for each face.
And here's an algorithm to manually generate smooth normal: OpenGL Normal Smoothing