Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
1
Question by Nikolpix · Aug 06, 2018 at 04:10 PM · scripting problemverticesmeshcollidertriangles

Mesh triangles and vertices, searching for adjacent triangle

I made a 3D model using 3ds Max. It has 160 vertices, 316 triangles and 4 smoothing groups. I set it as a mesh collider for game object in Unity. When I'm spherecasting against this object and hitting an edge I want to know the angle between 2 triangles adjacent to this edge. I know if the edge was hit by using my own function that calculates barycentric coordinates (built-in property doesn't work for spherecast for some reason). By using "triangleIndex" hit property and barycentric coordinates, I'm getting the vertex indices of the edge we hit. The only thing to do is to find 2 triangles that has these 2 vertices in common, right? The first triangle is already found and its index written in "triangleIndex" property. My problem is that I just can't find the second one. Analysing triangles[] and vertices[] arrays in debug mode I found out that mesh has 328 vertices and 948 triangles! That's way much more then model actually has. I heard it can be because of smoothing groups, but model without smoothing groups shows the same numbers. Also I searched how many triangles are connected with, for example, the first vertex. It turned out there are only 2 triangles! How can it be? My model is closed and it has no holes. So the question is how the hell these triangles and vertices are numbered and how to find adjacent triangle knowing 2 vertices. Or how to make mesh collider with correct structure.

Comment
Add comment
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
2
Best Answer

Answer by Bunny83 · Aug 06, 2018 at 06:05 PM

Well you have several seperate issues here. First of all don't trust any vertex / triangle information you get from modelling tools. They work with logical vertices to make editing easy.


The next thing is vertices don't need to be shared. In fact if the same vertex that is used by two trianges but has any vertex attributes that differ (position, normal, uv, color, tangent, ....), the vertex has to be duplicated. You can't have two or more normals, positions, colors, ... for the same vertex. So If a vertex is actually shared between two triangles, you can detect them through the triangles indices. However if the vertices are splitted they are essentially two seperate vertices. If you need to find adjacent triangles you first have to find other vertices with the same position. Once you have all vertices for a certain position you can check the triangles array where those are used.


For example an ordinary cube mesh has 8 "logical" vertices but actually requires 24 vertices. This has several reasons. The most important is that each "face" has a different surface normal. Since a cube face is flat, all 4 corners of a face need to have the same normal. Since always 3 "faces" (quads) meet at each of the 8 corners each corner need to be splitted into 3 seperate vertices (3 * 8 == 24). The same number can be aquired by having seperate 4 vertices per face. Since a cube has 6 faces 4*6==24. Apart from the normals you often need to split vertices when you want to unwrap the mesh (create UV coordinates). In theory a sphere mesh could use fully shared vertices since at each point 4 quads meet and have the same normal. However when you want any meaningful UV mapping you need at least 1 UV "seam". At those seems you will have duplicated vertices.


There has been several other questions that revolve around shared vertices. The code in my answers might be useful. One here, another one here, and another one

Comment
Add comment · Show 7 · 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 Nikolpix · Aug 06, 2018 at 06:45 PM 0
Share

Thank you for your answer! I got the vertices duplication thing. But why triangles have to be duplicated too? And back to my problem. The only way to find the angle between triangles is to search over the whole array of vertices for ones with the same position, and then search through the whole array of triangles? Can "==" operator be used then? Is it correct to compare float values?

avatar image Bunny83 Nikolpix · Aug 06, 2018 at 07:15 PM 0
Share

About your last question, that depends entirely on the mesh. The == operator could be used if the positions of the two vertices are exactly the same. However the slightest difference and it won't work. It's common to compare them approximately to a certain threshold.. Just subtract the two positions from each other. Take the squared magnitude of the resulting vector. If it's close to 0 (or equal) they could be considered the same.


Triangles aren't really duplicated. Your mesh contains exactly 316 triangles. $$anonymous$$eep in $$anonymous$$d that the triangles array contains vertex indices. You need 3 to define one triangle. 3*316 == 948 indices.


What exact angle do you want to know? When you have smooth normals (no hard edges) using barycentric interpolation should give you the proper normal at any given point.


$$anonymous$$eep in $$anonymous$$d that when you use a mesh as collider for a dynamic object (rigidbody) the collider need to be marked as convex. However this could mean that the actual mesh will look completely different since PhysX will calculate the convex hull of your mesh. In the past the convex hull was limited to 255 polygons. I'm not sure if this still applies as the documentation doesn't mention that limit anywhere. Note that static colliders don't need to be convex and can have more that 255 polys.

avatar image Nikolpix Bunny83 · Aug 06, 2018 at 07:59 PM 0
Share

I'm using it for my custom character controller. It follows the rotation of the surface when moving. But it follows even when I don't need this, for example stairs (or any sharp corner of floor). So I want to make condition when character should follow rotation and when it doesn't. If hit the edge > check angle between faces making this edge > if angle is greater than certain value then do not rotate.

Show more comments

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

157 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

Related Questions

Quick way to get touching triangles? 0 Answers

How to find connected mesh triangles? 2 Answers

Problem creating submeshes based on raycast. 0 Answers

Does mesh.triangles also return a copy of the triangle array? 1 Answer

Setting triangles failing 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