- Home /
For raycast hits, what is a "barycentric coordinate"?
I haven't seen a good explanation for this yet. It's defined elsewhere as the "center of mass" for a triangle, pyramid, or for any dimensional "simplex".
So in Unity, is it just the very center point of the raycast hit triangle?
Answer by Bunny83 · Jul 02, 2019 at 04:47 PM
No, it is the hit point described in the barycentric coordinate system. A value of (1,0,0) would be the first corner of the triangle. A value of (0,1,0) would be the second corner of the triangle and (0,0,1) the third. As you might know if you read the barycentric coordinate system wiki page the 3 components of a barycentric coordinate will always add up to 1.0. So a value of (0.33, 0.33, 0.33) would be the center of the triangle (not that the 0.33 is actually 1/3 so 0.3 recurring).
The great thing about barycentric coordinates is that you can use them to easily interpolate any vertex attribute at a given point. For example if you have the position of v0, v1 and v2 of the triangle that was hit, you can calculate the actual hitpoint inside the triangle be just doing
Vector3 p = bary.x*v0 + bary.y*v2 + bary.z*v3;
However this does also work for normals, colors or texture coordinate. That's actually how Unity calculates all the other information you get in the RaycastHit struct. If you want to work with barycentric coordinates yourself, I created this helper struct (Barycentric).
Your answer
Follow this Question
Related Questions
Can't find a clone object with RayCast? [Solved] 2 Answers
Raycast script help? 1 Answer
Raycast hit in OnDrawGizmos but not in Update 1 Answer
Vehicle is vibrating on the edge of the ramp/surface when I get the normal of the surface 0 Answers
Help finding out which side of a cube a raycast hits 1 Answer