- Home /
Custom Mesh Normals Problem
I am creating a very simple mesh procedurally but I am having problems with setting the normals so I feel like I am miss understanding something. At the moment I am simply making a plane:
List<Vector3> vertices = new List<Vector3> {
new Vector3(-width/2,0,-length/2),new Vector3(width/2,0,-length/2),new Vector3(-width/2,0,length/2),new Vector3(width/2,0,length/2)
};
List<Vector2> UV_MaterialDisplay = new List<Vector2> {
new Vector2 (0.1f, 0.1f),
new Vector2 (0.9f, 0.1f),
new Vector2 (0.1f, 0.9f),
new Vector2 (0.9f, 0.9f)
};
triangles.AddRange(new int[]{0, 3, 1});
triangles.AddRange(new int[]{0, 2, 3});
List<Vector3> normals = new List<Vector3>();
for (int i = 0; i < vertices.Count; i++) {
normals.Add(new Vector3 (0.0f, -1.0f, 0.0f));
}
Mesh mesh = new Mesh ();
transform.GetComponent<MeshFilter> ();
if(!transform.GetComponent<MeshFilter> () || !transform.GetComponent<MeshRenderer> () ) //If you havent got any meshrenderer or filter
{
transform.gameObject.AddComponent<MeshFilter>();
transform.gameObject.AddComponent<MeshRenderer>();
}
GetComponent<MeshFilter> ().mesh = mesh;
mesh.name = "Thing";
mesh.vertices = vertices.ToArray();
mesh.SetNormals (normals);
mesh.triangles = triangles.ToArray();
mesh.uv = UV_MaterialDisplay.ToArray();
This is correct and draws how I want it to draw except for I can only ever see it from 'above', when i want to see it from below instead, despite having set the normal to be -1.0f for all vertices. I get the same regardless of whether I set it to be -1.0f or 1.0f.
Answer by FlaSh-G · Aug 06, 2017 at 12:39 AM
There's two things called "normal" here. One is the information about which side of the face is "front", influencing which side backface culling is culling. The other is the vector that fakes the normal direction of a vertex for light calculation. That's the vector that, for example, makes the difference between these two spheres:
You are setting the latter value, while it's the first thing you are looking for.
To change a face's normal, you have to change the order of vertices forming the face in the triangles array. Clockwise means front face, counterclockwise means backface: