• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by ManatoMiyata · 4 days ago · mesh

How to identify a straight line in mesh

alt text 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?

スクリーンショット-2023-05-24-140954.png (52.0 kB)
Comment

People who like this

0 Show 0
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image

Answer by SteenPetersen · 4 days ago

@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[i]];
     Vector3 v2 = vertices[triangles[i + 2]] - vertices[triangles[i]];
     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<MeshFilter>().mesh;
         Vector3[] vertices = mesh.vertices;
         int[] triangles = mesh.triangles;
         List<int> hardEdges = new List<int>();
 
         for (int i = 0; i < triangles.Length; i += 3)
         {
             Vector3 normal1 = mesh.normals[triangles[i]];
             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.

Comment
nihirum

People who like this

1 Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image andrew-lukasik · 4 days ago 1
Share

I think he meant "how to find hard edges in a mesh" i.e. edges between two neighboring triangles, where angle between their normal is > some specific angle.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Update about the future of Unity Answers

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta later in June. Please note, we are aiming to set Unity Answers to read-only mode on the 31st of May in order to prepare for the final data migration.

For more information, please read our full announcement.

Follow this Question

Answers Answers and Comments

176 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I change vertices in mesh in unity? 1 Answer

Mesh manipulation at runtime - existing tools / libraries? 0 Answers

How to get access to default sphere mesh? 2 Answers

How to Render a Mesh Always, Ignoring Z-Distance 1 Answer

Terrain Mesh - Poly Facing... 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges