Followers 0

# Barycentric UV Help

## 9 posts in this topic

I have a function like this from recast, its a function that tests if a triangle gets intersected by a ray:

static bool intersectSegmentTriangle(const float* sp, const float* sq,
const float* a, const float* b, const float* c,
float &t)
{
float v, w;
float ab[3], ac[3], qp[3], ap[3], norm[3], e[3];
rcVsub(ab, b, a);
rcVsub(ac, c, a);
rcVsub(qp, sp, sq);

// Compute triangle normal. Can be precalculated or cached if
// intersecting multiple segments against the same triangle
rcVcross(norm, ab, ac);

// Compute denominator d. If d <= 0, segment is parallel to or points
// away from triangle, so exit early
float d = rcVdot(qp, norm);
if (d <= 0.0f) return false;

// Compute intersection t value of pq with plane of triangle. A ray
// intersects iff 0 <= t. Segment intersects iff 0 <= t <= 1. Delay
// dividing by d until intersection has been found to pierce triangle
rcVsub(ap, sp, a);
t = rcVdot(ap, norm);
if (t < 0.0f) return false;
if (t > d) return false; // For segment; exclude this code line for a ray test

// Compute barycentric coordinate components and test if within bounds
rcVcross(e, qp, ap);
v = rcVdot(ac, e);
if (v < 0.0f || v > d) return false;
w = -rcVdot(ab, e);
if (w < 0.0f || v + w > d) return false;

// Segment/ray intersects triangle. Perform delayed division
t /= d;

return true;
}

The trouble I am having is that I now have attached some uv coords to the vertices, how can I find out the interpolated uv coord from 3 uv coords of those 3 vertices that go into the function. I need to do something with that t value to get the point in the triangle where the ray hits and then use that to calculate my desired uv coord to output. What I want to achieve is to be able to raycast a triangle and get a uv coord at the point where the ray intersects.

0

##### Share on other sites

You have your three barycentric coordinates already: v, w, and u = 1 - v - w. At this point, most data can be linearly interpolated from the vertices of the triangle, using the barycentric coordinates. So, if your barycentric coordinates are (v, w, 1 - v - w), and your vertices are (v1, v2, v3), with uv coordinates v1.uv, v2.uv, v3.uv, then:

point.uv = v * v1.uv + w * v2.uv + (1 - v - w) * v3.uv

You can quickly check it is valid because the barycentric coordinates add up to 1. You can also do the same for the vertex normals to obtain an interpolated triangle normal.

1

##### Share on other sites

Thanks for the help I am trying that line of code you showed me, however after debugging I noticed that v and w are giving me very strange values:

v:  1213134.3

w: 1669297.8

Now the raycast itself is actually working so I am really confused? what values should v and w normally be?

0

##### Share on other sites
Thanks for the help I am trying that line of code you showed me, however after debugging I noticed that v and w are giving me very strange values:

v:  1213134.3

w: 1669297.8

Now the raycast itself is actually working so I am really confused? what values should v and w normally be?

If the ray is inside the triangle, they should all be greater than zero, and v + w cannot exceed 1. If the ray does not intersect the triangle, they can take any value. The barycentric coordinates really define a coordinate space over the plane in which the triangle lies, so you can have arbitrarily small or large barycentric coordinates, that map to some 3D point way outside the triangle.

1

##### Share on other sites

Ok so if those values are correct, how am I supposed to get my uv coordinate? it comes out with crazy numbers with that formula you showed me.

0

##### Share on other sites
Ok so if those values are correct, how am I supposed to get my uv coordinate? it comes out with crazy numbers with that formula you showed me.

If your barycentric coordinates come out wrong while the raycast test is working, it means you are not actually calculating barycentric coordinates but something else. I think you're doing some precomputation here - what value does "d" have usually? What do you get if you divide v and w by d at the end?

1

##### Share on other sites

I actually did not write that function, its in a library called recast (for navmesh generation).

I have modified it like this and left some comments can you have a quick look please:

static bool intersectSegmentTriangle(const float* sp, const float* sq,
const float* a, const float* b, const float* c,
float &t, bool GetUV,
const float* ta, const float* tb, const float* tc)
{
float v, w;
float ab[3], ac[3], qp[3], ap[3], norm[3], e[3];
rcVsub(ab, b, a);
rcVsub(ac, c, a);
rcVsub(qp, sp, sq);

// Compute triangle normal. Can be precalculated or cached if
// intersecting multiple segments against the same triangle
rcVcross(norm, ab, ac);

// Compute denominator d. If d <= 0, segment is parallel to or points
// away from triangle, so exit early
float d = rcVdot(qp, norm);
if (d <= 0.0f) return false;

// Compute intersection t value of pq with plane of triangle. A ray
// intersects iff 0 <= t. Segment intersects iff 0 <= t <= 1. Delay
// dividing by d until intersection has been found to pierce triangle
rcVsub(ap, sp, a);
t = rcVdot(ap, norm);
if (t < 0.0f) return false;
if (t > d) return false; // For segment; exclude this code line for a ray test

// Compute barycentric coordinate components and test if within bounds
rcVcross(e, qp, ap);
v = rcVdot(ac, e);
if (v < 0.0f || v > d) return false;
w = -rcVdot(ab, e);
if (w < 0.0f || v + w > d) return false;

// Segment/ray intersects triangle. Perform delayed division
t /= d; //d ends up 3676338.0 here

//It is about to return true so it means its a triangle hit but the v + w end up as 1315396.8 + 1609470.5 = 3676338.0

if(GetUV)
{
Vector2 v1 = Vector2(ta[0], ta[1]);
Vector2 v2 = Vector2(tb[0], tb[1]);
Vector2 v3 = Vector2(tc[0], tc[1]);

Vector2 UV = v * v1 + w * v2 + (1 - v - w) * v3;
}

return true;
}
0

##### Share on other sites

Ah, all good then, the code is just saving some divisions by doing them at the end, your barycentric coordinates are just v / d and w / d (and 1 - v / d - w / d). Try using those and see if they are within the required bounds, then try to apply the formula (make sure to associate the correct uv coordinates with the correct barycentric coordinate, otherwise it'll be off). As far as I can see this should be correct now.

If you care about performance, you can compute 1 / d first, then you just have to multiply a few times, which is faster.

1

##### Share on other sites
then try to apply the formula (make sure to associate the correct uv coordinates with the correct barycentric coordinate, otherwise it'll be off)

Sorry what do you mean by that? any sample code would be apreciated. I just added w /= d and v /= d before the if(GetUV) and it produces numbers below 1 now how a uv should be.

0

##### Share on other sites
then try to apply the formula (make sure to associate the correct uv coordinates with the correct barycentric coordinate, otherwise it'll be off)

Sorry what do you mean by that? any sample code would be apreciated. I just added w /= d and v /= d before the if(GetUV) and it produces numbers below 1 now how a uv should be.

Now you just use the formula in my first post again, but with the new, corrected v and w coordinates (divided by d, as you just did):

v /= d;
w /= d;
...
point.uv = v * v1.uv + w * v2.uv + (1 - v - w) * v3.uv;

This should work and give you the proper uv coordinates for the intersection point. You may need to shuffle around v1, v2, etc.. depending on how you defined v and w (which vertex are they based on?) but there's only six possible arrangements, try them and see which one looks correct (the bad ones will look like the texture is rotated, I think).

Edited by Bacterius
1

## Create an account

Register a new account

Followers 0

• 9
• 18
• 11
• 28
• 14