Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
0
Question by turtsmcgurts · Feb 14, 2014 at 03:34 AM · meshvertexfacespecific

How to get the vertices of a mesh face?

Can anyone point me into the right direction of how to get the vertices of a specific face of a mesh, or another way of getting the corner positions. Thanks.

Comment
Add comment · Show 1
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 robertbu · Feb 14, 2014 at 05:25 AM 0
Share

A raycast will give you the index of the first point in the triangle hit (RaycastHit.triangleIndex). Assu$$anonymous$$g your faces are built out of quads, you can calculate the fourth point from the other three.

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by MakeCodeNow · Feb 14, 2014 at 04:23 AM

If you have the index of the face, then use Mesh.triangles to get the indices into the Mesh.vertices array.

Comment
Add comment · Show 2 · 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 turtsmcgurts · Feb 14, 2014 at 04:56 AM 0
Share

Using that works properly for half of the face, but splits off on the other half.

http://imgur.com/a/QJ$$anonymous$$7L#0

In the first screenshot you'll notice that I am ai$$anonymous$$g to the bottom right triangle and it works perfectly, highlights just that face. However, in the second screenshot you see me ai$$anonymous$$g to the top left and things get a bit wonky. Any idea as to why this is happening, or how to force it to only get the selected face's vertices?

Here's the relevant code...

         Vector3 p0 = vertices[triangles[hit.triangleIndex * 3 + 0]];
         Vector3 p1 = vertices[triangles[hit.triangleIndex * 3 + 1]];
         Vector3 p2 = vertices[triangles[hit.triangleIndex * 3 + 2]];
         Vector3 p3 = vertices[triangles[hit.triangleIndex * 3 + 3]];
         Vector3 p4 = vertices[triangles[hit.triangleIndex * 3 + 4]];
         Vector3 p5 = vertices[triangles[hit.triangleIndex * 3 + 5]];

         Transform hitTransform = hit.collider.transform;
         p0 = hitTransform.TransformPoint(p0);
         p1 = hitTransform.TransformPoint(p1);
         p2 = hitTransform.TransformPoint(p2);
         p3 = hitTransform.TransformPoint(p3);
         p4 = hitTransform.TransformPoint(p4);
         p5 = hitTransform.TransformPoint(p5);

         Debug.DrawLine(p0, p1, Color.green);
         Debug.DrawLine(p1, p2, Color.green);
         Debug.DrawLine(p2, p0, Color.green);
         Debug.DrawLine(p3, p4, Color.green);
         Debug.DrawLine(p4, p5, Color.green);
         Debug.DrawLine(p5, p3, Color.green);
avatar image MakeCodeNow · Feb 14, 2014 at 05:15 AM 0
Share

In unity everything is triangulated, so you can't get quads, just tris. That means that in your code p0, p1, and p2 are correct, but p3, p4, and p5 are not necessarily what you think they are. They could be on another side of the mesh entirely.

avatar image
-1

Answer by wolvz38 · Sep 01, 2014 at 05:50 AM

I recently found a good way of doing this for cubes (sides are all the same). Script Reference: RaycastHit.triangleIndex

 Transform hitTransform = hit.collider.transform;
 
 int t = hit.triangleIndex * 3;
 p0 = vertices[triangles[t]];
 p1 = vertices[triangles[t+1]];
 p2 = vertices[triangles[t+2]];
 
 if (p1.x != p0.x) {
     p3 = p2;
     p3.x = p0.x;
 }
 
 if (p1.y != p0.y) {
     p3 = p2;
     p3.y = p0.y;
 }
 
 if (p1.z != p0.z) {
     p3 = p2;
     p3.z = p0.z;
 }
 
 p0 = hitTransform.TransformPoint(p0);
 p1 = hitTransform.TransformPoint(p1);
 p2 = hitTransform.TransformPoint(p2);
 p3 = hitTransform.TransformPoint(p3);


Hope this helps anyone who has been struggling to figure this out :)

Comment
Add comment · 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

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.

Follow this Question

Answers Answers and Comments

21 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

Related Questions

How make visible the back face of a Mesh 4 Answers

Mesh breaks apart when using vertex displacement with shadergraph. 3 Answers

UV Mapping - by vertex or face? Weird banding 2 Answers

Mirroring a mesh? 1 Answer

Create a Wireframe Shader with Shader Forge 1 Answer


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