Mesh Creator

I came across the Mesh Creator project for Unity a while back while I was trying to create a game that my niece designed (Rainbow Unicorn…. no ponycorn’s here).  Scanning in marker drawings, inserting a transparent background, and then creating custom meshed objects was super simple.  To create the meshes in Unity, you drag in the assets (downloadable from the link) into your project, add a script to an empty game object, give it a texture, specify what type of mesh you want, and hit go.

I liked it so much that I used it again when making a second game that my niece designed, a Christmas-based game that currently resides in the same iOS app as Rainbow Unicorn.  I made a few improvements along the way to the Mesh Creator script, but didn’t really keep track of what I had changed.  I eventually contacted the creator of the Mesh Creator, Jonathan Cecil, and offered to share my improvements with the project…. weeks later I still need to learn how to use Git so I can actually share.   😛

A random Mu Studios game appears

We are using 2D tiles to create the environment and characters, think Planet Cute pixel dimensions but different art, for our first major development project.  Kevin was able to get a basic framework together quickly based on work he had done in earlier prototyping.  Once we saw things moving across the screen it was great, then we noticed the frame rates were…not good, especially if we hoped to submit to the AppStore at some point in the future.  We suspected that the problem was each 101×171 texture in the scene had at least some amount of transparency.

After my worlds collided and I told Kevin about the Mesh Creator script I had been working with, I started to plan and make some changes so it could speed up the development process.  I saw the following problems and potential solutions as ways to keep it simple for us and our artist:

  1. The Mesh Creator script requires that you have specific folders already created and saves the resulting meshes and materials in those locations.  We already have a folder structure that we use and are used to, so I wanted to be able to pass the script a few strings on where to place things.
  2. The script only handles one texture at a time.  This would have been a huge bottleneck as well unless I did something about it.  Working on previous games and prototypes had shown us that the art, just like the code, is not ‘done’ once the initial version has been created.
  3. The script would create game objects in the currently open scene.  I didn’t feel like creating new prefabs for every gameObject that was created…. so I knew that the scripts should create prefabs in a user specified location as well.

Luckily I had worked through some of Jonathan’s code previously while working on the Rainbow Unicorn games, but there was still quite a bit of catch up.  I was unfamiliar with some of the techniques used but was able to code through them without ruining anything.

After working through quite a few Unity crashes (it’s really picky when you are working with saving things via the Editor classes), I had completed the aforementioned tasks and was able to convert folders full of textures into separate folders with meshes and materials.  It was great, but I quickly discovered that we were still seeing poor performance due to transparencies AND the Planet Cute characters were missing large chunks of their torso.  After some further investigation I found two more problems:

  1. The Mesh Creator script will create any parts of the converted mesh that you want, but if you want just the front face of a mesh it bypasses the conversion portion and creates a simple 2-triangle rectangular mesh.  I hadn’t noticed this with the Rainbow Unicorn games because those games required extruded meshes to simplify how I was handling collisions.  The current Mu game only needs the front mesh to work correctly.
  2. The Mesh Creator script only checks to see if each pixel has an alpha value equal to 1.  The Planet Cute images had alpha values that varied slightly and this was why part of the characters’ bodies were being lopped off.  The initial images we had from our artist also had varying alpha values and resulted in completely unusable meshes.

I decided to tackle the 2-tri mesh issue first.  After attempting to modify his routines for each case, I scrapped it all and started over by copying his main routine and cutting out everything I didn’t need.  Doing so meant that I left the redundancy in the code, but it is structured similarly to the other mesh cases and it works well. The only thing that I found annoying in this modification was that it uses ArrayLists instead of the much friendlier List.

Then came the alpha values in the SimpleSurfaceEdge script.  This was my second attempt at it… I had previously attempted to convert over to a user specified alphaCutoff value, but it did not go well.   I swapped out the many == and != comparisons that were in the code for >= and <.  Not too difficult.  It would run perfectly for images that were alpha binary (1 or 0), but it would still cause Unity to either crash or stop responding completely for other values.

After quite some time (largely due to restarting Unity every time that it failed….. ya, lots of Unity-fail in this process), I tracked the problem down to the script’s MakeOutsideEdge function.  What this function did is collapse collinear lines into a single line and make a complete edge around each ‘island’ of alpha’d-in values.  These edges would then be sent to the triangulation routine which would mesh each ‘island’.  As it always seems to be, a ‘while’ loop was causing the repeated hangs and crashes.  I looked into the issue further and found that the routine wasn’t adding subsequent lines to islands of alpha’d values because it had an incomplete, and disconnected, set of lines.  After a few hours of frustrating debugging, I described the problem to Kevin and asked him to take a look.

Kevin’s first question: Are you sure there isn’t a break or continue statement somewhere?

Me: There isn’t one, I can check again but I’ve been staring at it and I haven’t seen one.

Kevin: Looks like there is a continue statement right up top.

Me:  😐

It took about 3 seconds to fix the problem.  The fix was to add similar logic that I had applied to the == and != checks to the ‘continue’ statement.  It works great now and hopefully adds value to the Mesh Creator project.  Once I get the chance to clean up the code a little more I will figure out Git and add it to the project.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s