Finding Vertex Locations in an Animated Mesh

Dear Sirs:

I have an animated mesh, it’s the spherical shape in the photo. I am writing a script in C# to get a game object to stick to the side of that sphere. To do this I am using a linecast which returns the info I need in RaycastHit.

To access the mesh I use:

	Mesh mesh;
	mesh = meshCollider.GetComponent<MeshFilter>().mesh;

Then I use hit.triangleIndex to access the specific triangle.

My problem is that my Mesh Filter does not move with my visible, animated mesh. So my object does not move with the animation.

The Question is this: how do I get the information of where the animated triangle is? It should have the same triangle number as the hit returns, so I need a way to access that mesh instead of the Mesh Filter.

Any ideas or help is appreciated.

Thank you!

I believe your problem is similar to mine. Funny that we posted at nearly the same time:

http://forum.unity3d.com/viewtopic.php?t=65620

As I posted, I think you have to apply the transformation of each bone as given by the boneweights to each vertice, modified by the bindpose. Or something. I have a feeling that this value should be modified based off the boneweight to each bone, but I’m unsure how to do the matrix math.

I used this interesting script for a sphere which had a crumple mesh doforming it procedurally:

// Attach this script to a camera and it will
// draw a debug line triangle triangle
// at the triangle where you place the mouse.

var sphere:Transform;

function Update () {
// Only if we hit something, do we continue
var hit : RaycastHit;
if (!Physics.Raycast (camera.ScreenPointToRay(Input.mousePosition), hit))
return;
Debug.Log(hit.collider.name);
// Just in case, also make sure the collider also has a renderer
// material and texture
var meshCollider = hit.collider as MeshCollider;
if (meshCollider == null || meshCollider.sharedMesh == null)
return;

var mesh : Mesh = meshCollider.GetComponent(MeshFilter).mesh;
var vertices = mesh.vertices;
var triangles = mesh.triangles;

// Extract local space vertices that were hit
var p0 = vertices[triangles[hit.triangleIndex * 3 + 0]];
var p1 = vertices[triangles[hit.triangleIndex * 3 + 1]];
var p2 = vertices[triangles[hit.triangleIndex * 3 + 2]];

// Transform local space vertices to world space
var hitTransform : Transform = hit.collider.transform;
p0 = hitTransform.TransformPoint(p0);
p1 = hitTransform.TransformPoint(p1);
p2 = hitTransform.TransformPoint(p2);

// Display with Debug.DrawLine
Debug.DrawLine(p0, p1);
Debug.DrawLine(p1, p2);
Debug.DrawLine(p2, p0);
var centerPoint = (p0 + p1 + p2) /3;
sphere.position = centerPoint;
}

It worked great! The target sphere would ride along right in the middle of the moving triangle, and the debug lines swaying properly, but it doesn’t work for meshes deformed by bone animations.

By the way, the crumple mesh script that deforms the sphere is available here:http://unity3d.com/support/resources/example-projects/procedural-examples.html). It’s a very interesting project and shows ways to access triangles and vertices individually, which is intense!

I’m still very stumped. I’m not sure why I can get the triangle I’m hitting, but can’t reference the same triangle by number in the mesh that is being deformed by bones. The computer obviously knows exactly where it is, otherwise it wouldn’t be able to render it! So there must be some trick…

Hmmm…
:roll:

it might know it but for performance reason its not kept in a place where you can access it by cpu at least not without some significant impact.

also access to it is useless cause it changes that fast that the data by the time you got them are not correct anymore

Interesting idea… thanks

check out mesh smoother in wiki will show how to manipulate vertices in script