- Home /
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
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.
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.
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).
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.
Your answer
Follow this Question
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