unit – Get the project plan of Click mesh and calculate its area

Here is the excerpt from my script that calculates the area and facial area of a mesh on OnMouseDown Event.

                private void OnMouseDown ()
{
Mesh selectedMesh = this.gameObject.GetComponent().mesh;

float surfaceArea = CalculateSurfaceArea (selectedMesh);
float areaFromXFace = CalculateFacingArea (selectedMesh, new Vector3 (1, 0, 0));
float areaFromYFace = CalculateFacingArea (selectedMesh, new Vector3 (0, 1, 0));
float areaFromZFace = CalculateFacingArea (selectedMesh, new Vector3 (0, 0, 1));
float areaFromClickedFace = ProjectionPlaneClickedArea (selectedMesh);

string area = " n Surface:" + surfaceArea;
areas + = " n Area of ​​the X-axis face:" + areaFromXFace;
areas + = " n Area of ​​the Y-axis face:" + areaFromYFace;
areas + = " n Zone of the Z-axis face:" + areaFromZFace;
areas + = " n Area of ​​the selected face:" + areaFromClickedFace;
Debug.Log ("Zones:" + zones);

}

float ProjectionPlaneClickedArea (Mesh selcectedMesh)
{

Camera c = Camera.main;
Ray ray = c.ScreenPointToRay (Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast (ray, out hit, 100f))
{
Debug.DrawRay (ray.origin, ray.direction * 100, Color.yellow);
Debug.Log ("normal:" + hit.normal);
Debug.Log ("Normal / Direction Zone:" + CalculateFacingArea (selcectedMesh, hit.normal));
return CalculateFacingArea (selcectedMesh, hit.normal);
}
return 0f;
}

float CalculateSurfaceArea (mesh)
{
// Limitation: note that if your mesh has double-sided faces or internal faces, they can skew this result.
var triangles = mesh.triangles;
var vertices = mesh.vertices;

double sum = 0.0;

for (int i = 0; i <triangles.Length; i + = 3) // iterates on the triangles of the mesh
{
Vector3 corner = vertices[triangles[i]]; // for each triangle, we calculate vectors representing both edges
Vector3 a = highs[triangles[i + 1]]- corner;
Vector3 b = highs[triangles[i + 2]]- corner;

sum + = Vector3.Cross (a, b) .magnitude; // half of the crossed product of these edges gives us the area of ​​the triangle
}

return (float) (sum / 2.0);
}

/// 
     /// only calculates the mesh surface in a particular direction, you can try this version instead.
/// Specify the direction in which you want to display the mesh. The projected area must then be calculated.
/// the faces turned at least partially in this direction.
/// 
    /// 
     
     
     
     /// 
     
     
     
     /// 
      
      
      
      float CalculateFacingArea (mesh, Vector3 direction)
{
direction = direction.normalized;
var triangles = mesh.triangles;
var vertices = mesh.vertices;

double sum = 0.0;

for (int i = 0; i < triangles.Length; i += 3)    //iterate over the triangles of the mesh
         {
             //"Subtracting each 2nd & 3rd vertex" just gives the two edges of the triangle radiating from the initial corner. 
             Vector3 corner  = vertices[triangles[i]];
             Vector3 a       = vertices[triangles[i + 1]] - corner; 
             Vector3 b       = vertices[triangles[i + 2]] - corner;

            //The cross product divided by two gives the area of the triangle.
            //The dot product is what handles projecting this area onto the plane of interest. 
            float projection = Vector3.Dot(Vector3.Cross(b, a), direction);

            if (projection > 0f) // add only the projected triangles
sum + = projection;
}
return (float) (sum / 2.0);
}

I am able to calculate the surface of the face by CalculateFacingArea method, it takes a mesh and the direction that works well, as if I want to calculate the area of ​​x direction, I can use it

float areaFromXFace = CalculateFacingArea (selectedMesh, new Vector3 (1, 0, 0));

BUT I need to provide the face / direction information dynamically, what I do in this function ProjectionPlaneClickedArea using a raycast. It's about getting normal at the points of attraction that I use as the direction and passing them to the function. It works well with some of the meshes but not with all the meshes, for example below the image:

enter the description of the image here

As you can see, if I click on this object from the front, then it is necessary to calculate the surface of the Y side but it calculates with the X direction. The normal value of the click point is (1.0, 0.0, 0.1).

Although it works correctly if the axes of my clicked object look like this:

enter the description of the image here

In this case, x is facing and working properly. I have this value of normal (1.0, 0.0, 0.0).

I am neither an expert in geometry / mathematics nor a modeller, so I can not identify the normal problem of its object or its computation problem.

Remember that I do not want to model all objects in the same direction / axis, where I can get the hard-coded direction. I want to calculate the direction dynamically but correctly.