I'm working on an annotation system that allows a user to click on a mesh to attach a pin with a label hanging off it.

It must be possible to pin an annotation on an object that's being morphed with keyframe morph targets, so that the pin will move with the surface of the mesh.

It's mainly pretty simple, where I do a ray-intersect to find the pin position on the mesh, for the shape of the mesh at the current time within the morph targets.

Then I have, for that time instance:

  1. the 3D coordinates of the pin
  2. the triangle (ie. the indices and vertex positions of the triangle's vertices) that the pin is within
  3. the time instant

Now, if I had the 3D coordinates of the pin at each of the morph's targets, then I can simply tween the pin position, to animate it in synch with the morph.

However, the challenge I is to somehow compute the position of the pin at each of the morph targets, from these three pieces of information.

Intuitively, it seems to be a matter of somehow finding some sort of coefficient (or set of coefficients) that indicates the pin's position relative to the vertices of its enclosing triangle, at the instant within the morph sequence that we attach the pin.

Then, for each other target within the morph sequence, given the morphed positions of the triangle vertices, which could be anything, somehow use the coefficient(s) to find the updated pin position, relative to the triangle's current vertex positions.

Anybody got any ideas? Math pointers etc?



You have the position of the pin and a triangle index. Compute the barycentric coordinates of the point on the triangle, with the triangle vertices in the tweened position. These barycentric coordinates can then be used to compute the position of the pin in any other tweened position by taking the triangle vertices in that new position and computing the point as:

p_transformed = tri.p0 + u * (tri.p1 - tri.p0) + v * (tri.p2 - tri.p0)

where tri is the transformed triangle, u,v are the calculated barycentric coordinates, and p_transformed is the tweened pin position. Some raycast algorithms will directly give you the barycentric (also called UV!!) coordinates, but if not you will need to calculate them from the position and (transformed) vertices using this answer.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.