- Home /
Deform sprite using SkinnedMeshRenderer
Hi!
I have a 3d SkinnedMeshRenderer, it's animated in blender and works well. Also, I have a 2d sprite that will work as a mask to be drawn in front of animated object but not in the rest of the scene. So, to achieve this, I've created some SpriteMask components inside the animated GameObject matching the size and texture used by the animated object itself. The problem is when the object plays an animation, the sprite mask does not follow the deformation of the mesh.
I've added a script inside the sprite mask to try to sprite match the vertices used by the SkinnedMeshRenderer like this. This code executes every Update:
Mesh m = new Mesh();
skinnedMeshRenderer.BakeMesh(m);
Vector2[] vertices = Array.ConvertAll(m.vertices, i => (Vector2)i);
ushort[] triangles = Array.ConvertAll(m.triangles, i => (ushort)i);
mask.sprite.OverrideGeometry(vertices, triangles);
But this code creates an exception: Invalid vertex array. Some vertices are outside of the Sprite rectangle.
I have both FBX and Sprite marked as Read/Write enabled. What I'm missing?
Thank you!!!