I’m trying to create simple dynamic drop shadows for 2D sprites.
I don’t have much experience with the “new” 2D aspects of Unity, but when I did this for a game in Unity 4.2 (in which 2D graphics were billboard textures on mesh quads), the approach was relatively straightforward - I duplicated the mesh, flipped the UV coordinates along the x-axis and modified the vertex positions through a scale/shear matrix, passed the modified mesh.vertices and mesh.uv back and applied a transparent shader - which gave exactly the result I wanted as below:
My problem is that I can’t seem to repeat the same process when working with sprites…
Unlike Mesh.vertices, Sprite.vertices is readonly, and it seems the only way to assign new vertices is to call Sprite.OverrideGeometry(). When I pass in the modified vertex array to OverrideGeometry, I get an “Invalid vertex array. Some vertices are outside the Sprite rectangle” error, but I don’t understand this - I think the “Sprite rectangle” refers to the location of the sprite in the texture atlas, whereas I’m trying to shear and scale the mesh onto which the sprite is applied, not the rectangle from which the sprite texture is read.
After bashing my head for a bit, I considered keeping the original object as a sprite, but making a mesh copy to act as the shadow so that I could keep my old method of shearing. However, the process of converting from a sprite to a quad is proving to be a bit cumbersome; I can do it and it works fine if the source sprite was rectangular. However, for tightly packed sprites I can’t work out how I can convert the sprite’s UV coordinates (which Unity calculates automatically) to the shadow mesh.
If anyone can enlighten me as to how to shear/scale a sprite’s mesh (or alternative methods to create the drop shadow above) I’d be very grateful! Otherwise, I think I’m going to scrap sprites altogether and go back to working with 2D axis-aligned quads in 3D space! (and don’t even get me started on the “sorting order” issues I’ve been having!)
Thanks.