- Home /
How to calculate normal direction for shared vertex position
Hi all, I've tried searching for the answer and this is as close as I can get to the answer but can't figure this one out. Please see the screenshot below:
The debug lines are the vertex normals in the direction the normal points. Where there are more than one line coming out of a single point, this is where more than one of the triangle vertices share a Vector3 position. So in the example screenshot, there are 6 triangles that share the central vertex position but each have different normals, and I need to figure out how to calculate a single normal direction.
This is so you can calculate an extruded position for the vector for further information.
I am using https://docs.unity3d.com/Manual/ComputingNormalPerpendicularVector.html as my starting point, here is some code in brief:
var side1 = SelectedTriangles[j].P1 - SelectedTriangles[j].P0;
var side2 = SelectedTriangles[j].P2 - SelectedTriangles[j].P0;
var perp = Vector3.Cross(side1, side2);
Debug.DrawLine(SelectedTriangles[j].P0, SelectedTriangles[j].P0 + (perp * 50), Color.yellow, 10, false);
var side3 = SelectedTriangles[j].P0 - SelectedTriangles[j].P1;
var side4 = SelectedTriangles[j].P2 - SelectedTriangles[j].P1;
var perp2 = Vector3.Cross(side3, side4);
Debug.DrawLine(SelectedTriangles[j].P1, SelectedTriangles[j].P1 + (perp2 * 50), Color.yellow, 10, false);
var side5 = SelectedTriangles[j].P0 - SelectedTriangles[j].P2;
var side6 = SelectedTriangles[j].P1 - SelectedTriangles[j].P2;
var perp3 = Vector3.Cross(side5, side6);
Debug.DrawLine(SelectedTriangles[j].P2, SelectedTriangles[j].P2 + (perp3 * 50), Color.yellow, 10, false);
The P0 / P1 / P2 refer to position for each vertex that makes up a triangle. Any ideas please?
Answer by Bunny83 · Jan 03, 2017 at 11:59 PM
Well, first of all you should normalize each normal vector of each triangle. Then just take the arithmetic mean of them. That is, add up all normals and divide by the number of vectors. So in case of 7 normals it would be something like
nAvr = (n0 + n1 + n2 + n3 + n4 + n5 + n6) / 7;
This will give you the average of all normals. However the resulting vector won't be a normalized vector, so you need to normalize the result again. Since normalizing will adjust the length of the vector to be 1.0f you could omit the division by the number of vectors. So this would be enough:
nAvr = (n0 + n1 + n2 + n3 + n4 + n5 + n6).normalized;
However keep in mind if you actually use a shared vertex that the shading at that point will be equal for all triangles that use that vertex and you get a smooth transition to the other vertices.
Thanks for this @Bunny83, will give it a try after work. So as long as I get all of the vertex normals that are in the same location, sum them then normalize the sum, I can use this single value for the extrusion normal value I should be ok?
Your answer
Follow this Question
Related Questions
Problem drawing a mesh with Graphics.DrawMeshNow 1 Answer
How to set mesh normals per face? 2 Answers
Holes in procedural mesh 0 Answers
How to make sure two meshes have the same vertex count 0 Answers
How to edit a mesh from a script 1 Answer