Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 unityBerserker · May 21, 2017 at 09:13 PM · raycastraycasthitcoordinate-system

Deep understand of barycentric coordinates from Raycasthit structure

Hi, I try to understand how work barycentric coordinates in deep. I know how it is definied: https://en.wikipedia.org/wiki/Barycentric_coordinate_system

In RaycastHit structure we have barycentricCoordinate field. I don't know how can I use it in practice. I do some research about barycentic cordinates:

http://answers.unity3d.com/questions/383804/calculate-uv-coordinates-of-3d-point-on-plane-of-m.html

http://answers.unity3d.com/questions/1105729/find-uv-coordinates-of-mesh-without-a-raycast.html

https://www.scratchapixel.com/lessons/3d-basic-rendering/ray-tracing-rendering-a-triangle/barycentric-coordinates

Where and why I should use barycentric coordinates? Why don't use uv instead of barycentic coordinates?

I also try to understand example script from documentation:

https://docs.unity3d.com/ScriptReference/RaycastHit-barycentricCoordinate.html

I don't understand why we multiply triangleIndex by 3?

 Vector3 n1 = normals[triangles[hit.triangleIndex * 3 + 1]];

Also I don't understand idea behind this line of code:

 Vector3 interpolatedNormal = n0 * baryCenter.x + n1 * baryCenter.y + n2 * baryCenter.z;

Why don't use Raycasthit.normal?

Summary:

Where I must use barycentric coordinates? Are other variables from Raycasthit not enought to do everything?

What are pros and cons of that space in practice?

Sorry for chaos in my question but after few hours of searching explanation I don't know how to define question.

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 RobAnthem · May 21, 2017 at 10:09 PM 0
Share

While I'm sure you will still want an answer, because like most programmers, problems haunt us haha and we must seek solutions.

In my experience, if you have to ask something like "when do I use barycentric coordinates?" The most logical answer is, if you have to ask, you likely don't need to use them. If this is just for learning purposes, all the power to you, but remember one of the golden rules of program$$anonymous$$g "Do not attempt to solve problems that do not yet exist".

However, as far as I know, a barycentric coordinate in this case, would be use to locate the actual point on the mesh, inside a specific poly. The reason it is referencing triangles is because meshes are comprised of polys which are triangular and comprised of vertices.

avatar image unityBerserker RobAnthem · May 22, 2017 at 05:19 PM 0
Share

It stupid rule. $$anonymous$$any times I spend few hours to solve problem with miserable result and much later I learn something useful what solve my problem with one line of code. Actually I learning how work every variable and method in unity. $$anonymous$$aybe it stupid, but it giving me deep understanding of engine (and I can waste lot of time to do thing that I will never use :D).

1 Reply

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

Answer by bobisgod234 · May 22, 2017 at 06:17 AM

Where and why I should use barycentric coordinates? Why don't use uv instead of barycentic coordinates?

If you only want the UV coord of a raycast, than just use UV from the raycast. Barycentric coordinates can be used to interpolate arbitrary per-vertex data across a triangle.

I don't understand why we multiply triangleIndex by 3?

the triangle array is an array of indexes that make up a triangle, with each index representing a vertex. There are 3 vertices in a triangle, so you multiply triangle index by 3.

e.g. 0,1,2,1,2,3,1,3,4 would be a valid list of indexes for a mesh, representing 3 triangles. The first triangle would be composed of vertices 0,1,2, the second triangle 1,2,3, and the third triangle 1,3,4. If you wanted to access the indexes for the second triangle (triangleIndex = 1), that triangle would start at the third position of the array, so you multiply it by 3. The third triangle (triangleIndex = 2) would start at the 6th index (1,3,4), and so on.

Why don't use Raycasthit.normal?

This is merely an example on how to use barycentric coordinates, not an example on how you should get the normal of a hit.

Where I must use barycentric coordinates?

Where you need them. If you do not find you need them to solve a particular problem, then you don't have to use them.

Are other variables from Raycasthit not enought to do everything?

Nope. Boneweights and tangents are two things I can think of off the top of my head not provided by Raycasthit. Barycentric coordinates are a great way to calculate values on triangles of other custom per-vertex data that you have as e.g. part of a script.

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

93 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

Related Questions

Problem With Raycasthit Angle 0 Answers

Something fishy with RaycastHit 1 Answer

How can I can I cast a ray from a gameobject? 1 Answer

Raycast Not Working 3 Answers

Find gameobjects in a crosshair? 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