#Why is the lighting on my Mesh so weird?
1 messages ยท Page 1 of 1 (latest)
I am currently learning how to generate meshes in code.
I made a cube mesh and it works fine.
But the lighting on my mesh is pretty odd.
My code looks like this:
using UnityEngine;
[RequireComponent(typeof(MeshFilter))]
public class CubeMeshGenerator : MonoBehaviour
{
Vector3[] vertices;
int[] triangles;
MeshFilter meshFilter;
Mesh mesh;
void Start()
{
meshFilter = GetComponent<MeshFilter>();
GenerateCubeMesh();
UpdateMesh();
}
void GenerateCubeMesh()
{
vertices = new Vector3[]
{
new Vector3(0, 0, 0),
new Vector3(1, 0, 0),
new Vector3(1, 1, 0),
new Vector3(0, 1, 0),
new Vector3(0, 1, 1),
new Vector3(1, 1, 1),
new Vector3(1, 0, 1),
new Vector3(0, 0, 1)
};
triangles = new int[]
{
//front
0, 3, 2,
2, 1, 0,
//back
7, 6, 5,
4, 7, 5,
//right
1, 2, 6,
6, 2, 5,
//left
0, 7, 3,
3, 7, 4,
//top
3, 4, 2,
2, 4, 5,
//bottom
0, 1, 7,
1, 6, 7
};
mesh = new Mesh();
}
void UpdateMesh()
{
if (meshFilter != null)
{
mesh.vertices = vertices;
mesh.triangles = triangles;
meshFilter.mesh = mesh;
mesh.RecalculateNormals();
}
}
}```
I think there is something wrong with the way i am using mesh.RecalculateNormals()
can someone here see whats wrong? ๐
if you need more info please tell me! ๐
thanks in advance
UPDATE: Solved.
The problem was that the different faces of the cube share the vertices, which smoothes the normals.