How to identify a straight line in mesh

207494-スクリーンショット-2023-05-24-140954.png
The mesh has the shape shown in the attached photo.

Is it possible to distinguish the straight lines that make up the edges of the blue line shape from the straight lines that do not make up the edges of the red line shape in the mesh data?

@ManatoMiyata - Are you asking if it’s possible to differentiate between straight edge lines and angled lines in a mesh? Specifically, you’re interested in detecting edge lines that are either aligned with or perpendicular to the ground in the world scene. Is that correct?

If so, you could try first Calculate the normals of each face, every mesh consists of a number of faces. The orientation of a face in 3D space can be calculated by finding the normal of the face. After that you can Determine the direction of the edges, This can be done by subtracting the vertices of an edge. If the direction of an edge is (0, 1, 0), (0, -1, 0), (1, 0, 0), (-1, 0, 0), (0, 0, 1), or (0, 0, -1), the edge can be considered as straight (aligned with or perpendicular to the ground).

Note that the orientation is given in the World Coordinate System, so if your mesh is rotated, you will have to apply the inverse rotation to get the correct direction.

Please be aware that due to the nature of floating point numbers and their precision limits, you may want to avoid exact comparisons and instead use some small tolerance. Also, this approach assumes that your meshes are manifold and have consistent winding. You can try something like this (warning, untested)

Mesh mesh = GetComponent<MeshFilter>().mesh;
Vector3[] vertices = mesh.vertices;
int[] triangles = mesh.triangles;

for (int i = 0; i < triangles.Length; i += 3)
{
    Vector3 v1 = vertices[triangles[i + 1]] - vertices[triangles*];*

Vector3 v2 = vertices[triangles[i + 2]] - vertices[triangles*];*
Vector3 normal = Vector3.Cross(v1, v2).normalized;

// Assuming ground plane is Y = 0.
if (Mathf.Abs(normal.y) > 0.999f)
{
Debug.Log(“Face is aligned with the ground plane.”);
}
}
This code will print a log message for each face of the mesh that is aligned with the ground plane.
Remember, this example assumes the ground plane is Y = 0. If your ground plane is different, you will need to adjust the code accordingly.
let me know if this gets you closer to an answer.
=============EDIT============= based on @andrew-lukasik input
In case you question is about finding ‘hard edges’ as @andrew-lukasik suggested, then that involves calculating the angle between the normals of adjacent triangles. Something like this.( You can adapt it to your requirements.)
public class EdgeDetector : MonoBehaviour
{
public float threshold = 60f; // define your specific angle here

void Start()
{
Mesh mesh = GetComponent().mesh;
Vector3[] vertices = mesh.vertices;
int[] triangles = mesh.triangles;
List hardEdges = new List();

for (int i = 0; i < triangles.Length; i += 3)
{
Vector3 normal1 = mesh.normals[triangles*];*
Vector3 normal2 = mesh.normals[triangles[i + 1]];
Vector3 normal3 = mesh.normals[triangles[i + 2]];

if (Vector3.Angle(normal1, normal2) > threshold || Vector3.Angle(normal1, normal3) > threshold || Vector3.Angle(normal2, normal3) > threshold)
{
hardEdges.Add(i / 3); // store the triangle number
}
}

// Now you have a list of triangles with hard edges
}
}
This script will examine each triangle in your mesh and calculate the angles between the normals of their vertices. If any angle exceeds your specified threshold, it’s considered a hard edge and the triangle’s index is stored in the hardEdges list.
It’s important to note that this code considers a hard edge to be an edge with an angle greater than the threshold between ANY of the triangle’s vertices. Depending on your specific needs, you might want to modify this to only consider edges between specific pairs of vertices.
Also, you might want to run this code in an editor script or on a lower-frequency update loop, as it can be computationally heavy for large meshes.