- Home /
How to create accurate mesh from texture (like 2D sprites)
Unity Pro automatically creates an accurate mesh for 2d sprites that basically fits the non-clear pixels.
Look at this example: http://cdn1.raywenderlich.com/wp-content/uploads/2014/01/sprite_mesh.png
Now Unitys 2d sprites have some overhead for me so I don't want to use them directly. But I really want to know how I can create these accurate meshes. Is there already a script or algorithm out there?
(Please no discussions if I should use 2d sprites directly or not, thanks)
I believe what you are looking at is a polygon collider, not a mesh in the image. For performance, I suggest you do what I always do. Setup a quick sample scene using the shaders you believe you will be using. Drop the FPS script from Unity Wiki in the project. Then write a script that loads the system based on a keystroke or a touch. Then run the app on the target platform. So for example each touch might add 100 (animated?) sprites. See where the FPS drops and after that how quick after it drops does it become unusable as you continue to add. Then if you want to take it a step further, compare that performance with one using Quads ins$$anonymous$$d.
Nope, I'm looking for creating a mesh that's more accurate than a simple square since checking for alpha is expensive for the GPU and having a more accurate mesh reduces these checks.
Answer by Mefistofel · Jul 21, 2014 at 08:34 AM
There is a lot of algorithms for finding contour of figure. It can be http://en.wikipedia.org/wiki/Marching_squares with optimisations. Optimisation of contour(like mesh optimisation) - complicated theme. http://research.microsoft.com/pubs/68244/meshopt.pdf If for you it's a one-time solution, it is better to use square meshes or optimize meshes by hands in 3d editor.
Answer by Asse83 · Jul 22, 2014 at 08:19 AM
Thanks for the links, very interesting! I'll check them out later.
My current solution is to simply create a Sprite with Sprite.Create() and my texture, create a GameObject, add a SpriteRenderer to it and assign the Sprite. Then you can read the mesh data by calling UnityEditor.Sprites.DataUtility.GetSpriteMesh() and so on. Everything is done in code and I destroy the GameObject after reading the data.
Plain but it works...
Of course you'll need the pro version for this feature.
hi,could you provide a detail demo about this resolve, i drop into it,thx