- Home /
Generating Normals
I've generated a mesh, and now I want to generate its normals for a smooth shading effect. The problem is that it doesn't seem to be working as I'd expect, it's still very blocky. The below is the model with a simple diffuse shader:

And my current code:
 public Vector3[] CalculateNormals(Vector3[] vertices, int[] triangles)
     {
         Vector3[] normals = new Vector3[vertices.Length];
         for(int i = 0; i < triangles.Length; i = i + 3)
         {
             Vector3[] storedPoints = new Vector3[3];
             storedPoints[0] = vertices[triangles[i]];
             storedPoints[1] = vertices[triangles[i + 1]];
             storedPoints[2] = vertices[triangles[i + 2]];
             Vector3 normal = Vector3.Cross(storedPoints[1] - storedPoints[0], storedPoints[2] - storedPoints[0]);
 
             for(int j = 0; j < 3; ++j)
             {
                 Vector3 a = storedPoints[(j+1) % 3] - storedPoints[j];
                 Vector3 b = storedPoints[(j+2) % 3] - storedPoints[j];
                 float weight = Mathf.Acos(Vector3.Dot(a, b) / (Vector3.Distance(Vector3.zero, a) * Vector3.Distance(Vector3.zero, b)));
                 normals[triangles[i + j]] += weight * normal;
             }
         }
 
         foreach(Vector3 normal in normals)
         {
             normal.Normalize();
         }
         return normals;
     }
I've been really tearing my hair out over this for the past week or so, any help would be immensely appreciated!
Answer by Eric5h5 · Mar 16, 2014 at 11:26 AM
You can make sure the vertices are shared for all the quads, and use Mesh.RecalculateNormals, which will do it for you.
$$anonymous$$esh.RecalculateNormals results in blocky shading does it not? Or is that only the case if vertices are not shared?
Thanks :)
It's not even possible to have blocky shading if the vertices are shared.
Your answer
 
 
             Follow this Question
Related Questions
How can I "generate a mesh" for a projected image or decal? 0 Answers
Changes to a mesh via code don't render until I interact with them through manual editor tools. 1 Answer
How to set up UV for lightmaps for generated meshes? 4 Answers
Procedurally Generated Cube Mesh 3 Answers
Mesh visible in editor but not build 1 Answer
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                