Hello I’ve a simple problem but can not get it to fix it.
I have a plane in my scene where I want to display the normals of the vertexes and the normal of the center of the triangle. The normals on the vertices are displayed correctly but somehow I miscalcualting the normal of the triangles.
Here is a image. The yellow lines indicates the normal of each vertice which points to the right direction.
The blue Line indiciates the normal of the triangle from the center but somehow the lines are pointing into the wrong direction (I guess). The start Position is correct (triangleCenter) but the direction of the normal seems wrong. Any idea what I am doing wrong here? The blue lines should point from the center of the triangle in the same direction as the normals of the vertices.
In the code below the direction from the center of the triangle is drawn
faceNormalWP = (n1wp + n2wp + n3wp) / 3;
[...]
Debug.DrawLine(centerWP, centerWP + faceNormalWP.normalized, Color.blue, 999);
Even if I try to calculate the normal via the cross product I get the same result.
faceNormalWP = transform.TransformPoint(Vector3.Cross(n2 - n1, n3 - n1)).normalized;
private void ShowVerticeNormals()
{
Vector3 p1, p2, p3, n1, n2, n3, center, centerWP, faceNormal, faceNormalWP, n1wp,n2wp,n3wp;
for (int j = 0; j < triangles.Length; j += 3)
{
p1 = vertices[triangles[j]];
p2 = vertices[triangles[j + 1]];
p3 = vertices[triangles[j + 2]];
// p1 = transform.TransformPoint(vertices[triangles[j]]);
// p2 = transform.TransformPoint(vertices[triangles[j + 1]]);
// p3 = transform.TransformPoint(vertices[triangles[j + 2]]);
center = (p1 + p2 + p3) / 3;
centerWP = transform.TransformPoint((p1 + p2 + p3) / 3);
n1 = normals[triangles[j]];
n2 = normals[triangles[j + 1]];
n3 = normals[triangles[j + 2]];
n1wp = transform.TransformPoint(n1);
n2wp = transform.TransformPoint(n2);
n3wp = transform.TransformPoint(n3);
faceNormal = ((n1 + n2 + n3) / 3);
faceNormalWP = (n1wp + n2wp + n3wp) / 3;
// faceNormalWP = transform.TransformPoint(Vector3.Cross(n2 - n1, n3 - n1)).normalized;
// Debug.DrawLine(centerWP, centerWP + faceNormalWP * 0.4f, Color.blue, 999);
// Debug.DrawLine(transform.TransformPoint(p1), transform.TransformPoint(p1 + n1 * 3.4f), Color.blue, 999);
Debug.DrawLine(transform.TransformPoint(p1), transform.TransformPoint(p1 + n1), Color.yellow, 999);
Debug.DrawLine(transform.TransformPoint(p2), transform.TransformPoint(p2 + n2), Color.yellow, 999);
Debug.DrawLine(transform.TransformPoint(p3), transform.TransformPoint(p3 + n3), Color.yellow, 999);
// Debug.DrawLine(centerWP, centerWP + transform.TransformPoint(n1).normalized, Color.blue, 999);
// Debug.DrawLine(centerWP, centerWP + transform.TransformPoint(n2).normalized, Color.blue, 999);
// Debug.DrawLine(centerWP, centerWP + transform.TransformPoint(n3).normalized, Color.blue, 999);
Debug.DrawLine(centerWP, centerWP + faceNormalWP.normalized, Color.blue, 999);
Debug.DrawLine(transform.position, transform.position + transform.up, Color.cyan, 999);
surfaceNormals[j / 3] = faceNormal;
}