I have been following this tutorial from catlikecoding on a mesh generator and currently have this result.
I’m a beginner at all of this but i thought it would automatically smooth shade if i use RecalculateNormals() but i’m guessing it doesn’t work well because it’s all seperate vertices.
Would it make sense to create a seperate list of shared vertices and somehow find the normals per vertice cluster and average them on all? I can’t find a good way to approach that problem, so any hint would help greatly!
Here is what i have so far on the mesh generation
[NonSerialized] List<Vector3> vertices;
[NonSerialized] List<int> triangles;
[NonSerialized] List<Vector3> normals;
void Awake () {
GetComponent<MeshFilter>().mesh = hexMesh = new Mesh();
hexMesh.name = "Hex Mesh";
}
public void Clear () {
hexMesh.Clear();
vertices = ListPool<Vector3>.Get();
triangles = ListPool<int>.Get();
normals = ListPool<Vector3>.Get();
}
public void Apply () {
hexMesh.SetVertices(vertices);
ListPool<Vector3>.Add(vertices);
hexMesh.SetTriangles(triangles, 0);
hexMesh.SetNormals(normals);
ListPool<int>.Add(triangles);
ListPool<Vector3>.Add(normals);
hexMesh.RecalculateNormals();
}
public void AddTriangle (Vector3 v1, Vector3 v2, Vector3 v3) {
int vertexIndex = vertices.Count;
vertices.Add(HexMetrics.Perturb(v1));
vertices.Add(HexMetrics.Perturb(v2));
vertices.Add(HexMetrics.Perturb(v3));
triangles.Add(vertexIndex);
triangles.Add(vertexIndex + 1);
triangles.Add(vertexIndex + 2);
}
public void AddQuad (Vector3 v1, Vector3 v2, Vector3 v3, Vector3 v4) {
int vertexIndex = vertices.Count;
vertices.Add(HexMetrics.Perturb(v1));
vertices.Add(HexMetrics.Perturb(v2));
vertices.Add(HexMetrics.Perturb(v3));
vertices.Add(HexMetrics.Perturb(v4));
triangles.Add(vertexIndex);
triangles.Add(vertexIndex + 2);
triangles.Add(vertexIndex + 1);
triangles.Add(vertexIndex + 1);
triangles.Add(vertexIndex + 2);
triangles.Add(vertexIndex + 3);
}