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
1
Question by Egons · Mar 17, 2011 at 12:00 AM · mesh3dtriangles

Fetching triangles from a 3D Mesh

A question derived from http://answers.unity3d.com/questions/46662/how-would-one-calculate-a-3d-mesh-volume-in-unity

As @yoyo commented there - "Get point i for triangle j with p = mesh.vertices[mesh.triangles[i + 3 * j]] -- i is 0, 1, 2 for the three corners of the triangle, and j goes from 0 to mesh.triangles.length/3 - 1."

Basicly, this is what I have made so far for triangle fetching, but it looks like the function isn't working as it should:

// function to gather all the triangles on mesh private function GatherTriangles(mesh : Mesh, triangle : int){ var p : Vector3 = Vector3.zero; for(var i = 0; i < 3; i++){ p = mesh.vertices[mesh.triangles[i + 3 * triangle]]; } return p; }

// Start function, executes gathering, counts results and stuff function Start () { var mesh : Mesh = GetComponent(MeshFilter).mesh; for(var n = 0; n < mesh.triangles.length; n++){ trianglePoints.Push(GatherTriangles(mesh, j)); j = (mesh.triangles.length / 3) - 1; } print(trianglePoints); // print #1 print(trianglePoints.length); // print #2 }

Result:

print #1: (0.0, 0.0, 0.3),(0.0, 0.0, 0.4),(0.0, 0.0, 0.4) and continues so on, no values are changed anymore.
print #2: 12060

Used on Standard Assets -> Construction Worker Mesh, has 4020 tris (4020 * 3 = 12060, so there is some kind of relation between all that hassle there).

Actually, have no idea what is causing this mess, and I have no idea what should be the format for p.

Hope you guys can help me, thanks in advance!

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 yoyo · Mar 17, 2011 at 05:08 AM 0
Share

GatherTriangles returns a single Vector3 -- it takes three such points to make a triangle. And when you call GatherTriangles, you keep passing in the same value of j.

2 Replies

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

Answer by Statement · Mar 17, 2011 at 12:35 AM

Given you have a mesh, you can get each individual corner of the triangles like this:

for (int i = 0; i < mesh.triangles.Length; i += 3)
{
    Vector3 p1 = mesh.vertices[mesh.triangles[i + 0]];
    Vector3 p2 = mesh.vertices[mesh.triangles[i + 1]];
    Vector3 p3 = mesh.vertices[mesh.triangles[i + 2]];
}

See also my answer in your previous topic, it contain full source code for your problem.

The triangles property contain the indices to the vertices. So we go through each triplet of indices to look up the vertex index. We then use the vertex index to find the actual vertex.

Just to clarify what is returned from the triangles and vertices, consider this small example:

// Get the first vertex index which is the first vertex in the first triangle. int vertexIndex = mesh.triangles[0];

// Then get the vertex from a vertex index. Vector3 vertex = mesh.vertices[vertexIndex];

Comment
Add comment · Show 5 · 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 sanjayaddagudi · Jan 15, 2021 at 12:28 PM 0
Share

I have a doubt regarding the triangles of a mesh. is the order of vertices is correct because in my mesh when I try to form a triangle mesh with the first three vertices, the vertex0 and vertex2 have the same value. How can I get the triangles?

avatar image Bunny83 sanjayaddagudi · Jan 15, 2021 at 01:07 PM 0
Share

The triangles are defined in the triangle array, not the vertices array. The vertices in the vertices array can be in any order. The triangle array contains indices which define which vertices form a triangle. The index values in the triangle array are in order. So always 3 consecutive index values form one triangle. For example the triangle array could contain the values 0,5,7,3,2,4, ...

That means the first triangle is made up of the vertices at index 0, 5 and 7. The second triangle is made up of the vertices 3, 2 and 4 in that order.

avatar image sanjayaddagudi Bunny83 · Jan 15, 2021 at 02:52 PM 0
Share

Thank you @Bunny83 for the clarification. I want to know the indices of the triangles. I have 2112 vertices with 200 different vector values? how can I get the triangle indices and how can know the face of the vertex like which vertex is facing which side??

Show more comments
avatar image
0

Answer by DaveA · Mar 17, 2011 at 12:25 AM

One thing off the bat is your gather func will always return just the last vertex in the given triangle

Comment
Add comment · 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 Egons · Mar 17, 2011 at 12:33 AM 0
Share

I'm aware of that, but previously I used a different approach for it, and results didn't change. I'm more interested in how to populate the correct coordinates for those triangle vertices.

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

2 People are following this question.

avatar image avatar image

Related Questions

How do i create a Mesh in the middle of a Catmull spline? 0 Answers

How does the Triangles work on Mesh? 1 Answer

Determining where a mesh triangle faces 1 Answer

Connecting flatshaded vertices 0 Answers

Triangles on my mesh don't show 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