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 code-blep · Jun 15, 2014 at 06:47 PM · meshforwardtrianglein front

How do I calculate positions directly in front of a mesh triangle?

Hi,

I am trying to raycast from the centre of a mesh triangle in the direction that the triangle is facing.

I am able to get the middle(average) position of the triangle as the starting point of the raycast, but can't seem get the last bit, and no matter what I try, I just can't get it. Below is cleaned up code showing the triangle info and average position.

     //Triangle Average positions
     for (var a : int = 0; a < triangles.Count; a++)
     {
         var averagePosition = ((triangles[a].tri0 + triangles[a].tri1 + triangles[a].tri2) / 3);
         trianglesAveragePosition.Add(averagePosition);
     }


Any ideas out there? Thanks!

Comment
Add comment · Show 2
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 ProtoTerminator · Jun 15, 2014 at 07:06 PM 0
Share

if you can get the direction of the normal on the triangle, you should be able to raycast in that direction. how to obtain the normal, however, I don't know.

avatar image code-blep · Jun 15, 2014 at 07:15 PM 0
Share

Yes, that's one of the approaches I tried but for some reason could never get it right. I tried mesh.normals (although that can be bad apparently if the mesh is smoothed), and even calculating the dot product of the triangle myself.

1 Reply

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

Answer by robertbu · Jun 15, 2014 at 08:02 PM

Given three unique vertices, you can construct two vectors and take the cross produce. So if a, b, and c are three vertices, then it will be something like:

 var v1 = b - a;
 var v2 = c - b;
 var normal = Vector3.Cross(v1, v2);

Note if a, b, & c are taken directly from the mesh, they will be in local coordinates, so you will have to do a 'transform.TransformDirection()' to get the world direction of the normal. In addition, I don't remember the direction of the winding of the vertices in Unity triangles, so you may have to reverse the order of the parameters in the Vector3.Cross().

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 code-blep · Jun 15, 2014 at 10:06 PM 0
Share

Brilliant, thanks robertbu. I looked up cross and learnt the left hand rule as well, very useful!

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

22 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

Related Questions

Find all triangles inside given bounds 1 Answer

Weird triangle indexing problem 1 Answer

Trouble recalculating 3D mesh's triangles after deleting verts 2 Answers

Change color of mesh triangle based on Y position in world space 1 Answer

Odd Duplicate when selecting Triangle from Mesh (with Barycentric Selection.) 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