Search Unity

Doing 2D animation with 3d package?

Discussion in 'iOS and tvOS' started by hexdump, Jan 17, 2011.

  1. MikaMobile

    MikaMobile

    Joined:
    Jan 29, 2009
    Posts:
    845
    If the fact that the mesh has too many contiguous polys is the issue, maybe try breaking that leg up into separate polys that just happen to share edges? It should probably look exactly the same, I don't think you'll see any seams. Might be worth a try before writing any crazy scripts to manually handle it.
     
  2. rogerimp

    rogerimp

    Joined:
    Jun 21, 2010
    Posts:
    17
    Thanks for the suggestion! So hey, reporting back, I tried a couple different approaches.

    * Reordering source polygons to match unity's stripping but keeping them in body-part order. I did this for a few parts, importing the post-optimized poly order using a unity script and C4D's structure manager. In particular, I ensured that that leg in the screenshot was exported in exactly the order that Unity optimized it into. Unfortunately, Unity still reordered the foot up in between the strips of the leg and it ended up looking just the same. I bailed on this approach.

    * Adding "spacer" polygons in the poly list between mesh parts. These were 0-size tris that connected vertices 0 0 and 0. I hoped that maybe padding them in would be a hint for Unity, but it was a pretty long shot and indeed, their mesh optimizer is smarter than that. Had no effect.

    * Connected the body part meshes into a single mesh when they were well separated, and used bones to pull them together. I thought that if the polys weren't overlapping so much in the unbound pose, Unity might not reorder them so vigorously. This didn't work for me, but I still think that something like this might work eventually, given enough trial and error.

    * Finally, I tried the guns-blazing approach and exported the poly list from C4D and used a Unity script to access the mesh's triangles, completely overriding them at runtime with the original, untouched polys. It's not ideal, but so far this works. If I can extend this to run in the editor instead, so that I can create fixed-up versions en masse after import, then I guess I'll be happy with how it works. Additionally, I have yet to make sure this works with the skinned version of the mesh. Hopefully it will - I was just in a hurry to post now since I'm excited I got anything at all working!
     
  3. rogerimp

    rogerimp

    Joined:
    Jun 21, 2010
    Posts:
    17
  4. bryantdrewjones

    bryantdrewjones

    Joined:
    Aug 29, 2011
    Posts:
    147
    Fantastic thread :) It's certainly a lot to take in, especially for someone like me who's never opened a 3D editor before :)

    I have a simple, naive question that I'm hoping someone can answer. How do you size your planes with respect to your texture size? For example, if the head of my character is 60x80 pixels, what size should my plane be in world units to make sure my character's head is pixel perfect?

    Obviously it's all relative to the position of the camera, but I'm looking for any kind of logic to help guide me.

    Thanks for the great discussion so far :)
     
  5. bigdaddio

    bigdaddio

    Joined:
    May 18, 2009
    Posts:
    220
    Making the planes pixel perfect is a pain, but through trial and error I believe I have found the formula. I set the camera to orthographic projection then set the size to 50% of the target height. Actually I use 50% of the target height/10, it is just what I am used to from another 2D game engine I used to use on another platform :)

    Example: If targeting retina display (and you should, regular display automatically works) make the orthographic camera 480 (in my case 48). the top left of the screen is -360, 480 - the bottom right is 360, -480. Assuming of course your camera is 0,0. set your objects to the correct pixel size, say you have a 64x64 sprite it's scale would be 64, 64, 1.

    Anyway that works for me. It looks like a 2D engine. as an aside I would also suggest that if you want a pixelated look you make each pixel 4x4 at least. That retina display just smooths out any 1x1 pixels, even the 3Gs display doesn't look retro enough at 1x1 pixels.
     
    theANMATOR2b likes this.
  6. ElBueno

    ElBueno

    Joined:
    Jul 4, 2011
    Posts:
    19
    Has anyone figured out something similar for Blender? This technique doesn't seem to work for it. Ah what I would give for Maya!

    I would give thousands of dollars! Never mind!

    So, Blender...
     
  7. MetaMythril

    MetaMythril

    Joined:
    May 5, 2010
    Posts:
    150
    The best way I've found to do this in Blender is to start with your first plane object, go into edit mode and set selection to face mode. From there, select the one face that makes up your plane object and then duplicate that face. The order in which you duplicate is the order of depth, first --> last = back --> front. So arrange your pieces where the one furthest back would be first and then as you duplicate, layer on top accordingly. Easier said than done of course but it seems like the only method that worked for me. Though I've had an issue where one of my pieces moved to the front for some unexplained reason. I'll have to figure out how that happened, that or roll back to my last save and try not to have that happen again.

    Hope that helps.
     
  8. ElBueno

    ElBueno

    Joined:
    Jul 4, 2011
    Posts:
    19
    Would you believe it, I figured out a trick that works in Blender not an hour after posting my reply. Here are the steps:

    (Note: Strictly speaking, you don't have to do any texturing during this process, but you won't be able to test the results otherwise.)
    - Create your planes. Texture them however you like and arrange them on the z-axis. I like to arrange each "layer" of body parts in increments of 0.1 BU on the z-axis.
    - Do a quick test render to make sure the parts are arranged correctly. Save and import the file into Unity, too, and check it there.
    - In Object Mode, select each plane and press Ctrl-J (Join). This merges the parts all into one mesh.
    - Switch to Edit Mode. Individually set each plane in the mesh to z = 0.0
    - Exit Edit Mode, save, and return to Unity. The body parts should all move to the same plane as Unity reimports the file, and they will all render in the correct order.

    Hope this helps someone! :p

    Edit: I just missed you, GravitySpec. Thanks for the help!
     
  9. MetaMythril

    MetaMythril

    Joined:
    May 5, 2010
    Posts:
    150
    Sweet! Thanks for sharing. I'll have to try this method out as it seems a lot less prone to error like my current method.
     
  10. ValenUA

    ValenUA

    Joined:
    Nov 14, 2010
    Posts:
    20
    Really nice thread, tnx for sharing that info. But I have a question, how do you switch texture part on the polygon to other texture part on the same atlas? I mean you assign a different material or just use material.SetTextureOffset, both methods seams to add some additional draw calls to unskined character. Or am I missing something?
     
  11. bryantdrewjones

    bryantdrewjones

    Joined:
    Aug 29, 2011
    Posts:
    147
    SetTextureOffset will indeed add another draw call because you're basically creating a new instance of the material. There are two alternatives. The first is to set the UV offset on the *shared* material, so all instances of the material are offset by the same amount (you probably don't want this). The second is to recreate the UVs on the polygon itself. We created a pretty simple script that does just that (we were using it for spritesheet animation, but the idea is the same):

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class UVAnimation : MonoBehaviour
    6. {
    7.     public Vector2[] uvOffsets; // Each offset represents a frame of animation
    8.    
    9.     private Vector2[] originalUVs = new Vector2[4];
    10.     private Mesh mesh;
    11.    
    12.     // Use this for initialization
    13.     void Awake ()
    14.     {
    15.         mesh = ((MeshFilter)gameObject.GetComponent<MeshFilter>()).mesh;
    16.         originalUVs = mesh.uv;
    17.         SetFrame(0);
    18.     }
    19.    
    20.     public void SetFrame(int frame)
    21.     {  
    22.         Vector2[] newUVs = new Vector2[4];
    23.        
    24.         for(int i = 0; i < 4; ++i)
    25.         {
    26.             newUVs[i] = originalUVs[i] + uvOffsets[frame];
    27.         }
    28.        
    29.         mesh.uv = newUVs;
    30.     }
    31. }
    32.  
    This code also assumes that your mesh is a plane, which is why we hardcode the array to a size of 4. I'm sure it would be pretty straightforward to accommodate a variable number of UVs per mesh.

    Hope that helps! :)
     
    theANMATOR2b likes this.
  12. ValenUA

    ValenUA

    Joined:
    Nov 14, 2010
    Posts:
    20
    Wow, thats explains a lot really :) Thank you very much. Also will this thing work for frame by frame animation of muzzle flashes from the gun, witch is 10 frames total? And how resource consuming it will be for 5-10 muzzle flashes animations playing in the same time?
     
  13. bryantdrewjones

    bryantdrewjones

    Joined:
    Aug 29, 2011
    Posts:
    147
    I'm not sure how resource intensive that would be...I guess it depends on the size of your textures. But if you attach a slightly modified version of this script to each of your gun objects, you should have no problem getting them to play their muzzle flash animations. Just change the UVs on your polygons each time you want to advance the frame of animation.
     
  14. GabrielEmilio

    GabrielEmilio

    Joined:
    Jan 4, 2012
    Posts:
    3
    Great thread.
    I'm currently working on a 2D game on unity and we are using the cut out technique.
    Everything is pretty clear but I'm having trouble when i combine the planes and then export to Unity.
    When I don't combine the planes and bring everything into unity, everything works fine, expect it uses lots of draw calls,
    But when I combine my parts into one mesh and bring it into Unity, the animation doesn't work. The model is there, but static.
    Any thaughts on what i might be doing wrong?
     
  15. ValenUA

    ValenUA

    Joined:
    Nov 14, 2010
    Posts:
    20
    What 3D package do you use?

    I have one big 2048x2048 atlas for everything, and each part of character or other stuff took about 32x32 or 64x64 part of it.
     
  16. bryantdrewjones

    bryantdrewjones

    Joined:
    Aug 29, 2011
    Posts:
    147
    Are you using bones or are you animating free floating planes? I'm guessing you're doing the latter. Once you combine a mesh, you'll lose the animations on the individual planes. Try applying bones to your combined mesh and animating those instead of the planes. Your model should then import into Unity with a SkinnedMeshRenderer and the corresponding animations.

    EDIT: Gah, I forgot to ask: are you using texture atlases? If you combine your textures into a single atlas you shouldn't see an increase in draw calls with or without the combined mesh. Maybe try that before you try skinning your mesh.
     
    Last edited: Jan 4, 2012
  17. Krobill

    Krobill

    Joined:
    Nov 10, 2008
    Posts:
    282
    Using SetTextureOffset is a bad idea since it will force you to use individual materials for each body part of every character using this atlas. First way of doing this is modifying the uv of each vertex of the desired quad polygons so they target a new section of the atlas/spritesheet. The second option is to have every possible texture on different gameObjects parented to the same body part gameObject and enable / disable the correct GO to display whatever you want. Second solution is only practical for unskinned character of course.
     
  18. ValenUA

    ValenUA

    Joined:
    Nov 14, 2010
    Posts:
    20
    But second solutions seams to add a lot of additional polygons. And frame by frame animation with this method would be pretty big pain in the ass i think.
     
  19. Krobill

    Krobill

    Joined:
    Nov 10, 2008
    Posts:
    282
    Unrendered polygons since the gameObjects are not enabled. Now if you talk about fluid animations with lots of keyframes for each parts, yeah you're better off with swapping uvs..
     
  20. ValenUA

    ValenUA

    Joined:
    Nov 14, 2010
    Posts:
    20
  21. GabrielEmilio

    GabrielEmilio

    Joined:
    Jan 4, 2012
    Posts:
    3

    I'm Using Maya



    Thanks for the reply.
    I'm using bones, but I'm not skin binding them to the planes, I'm just parenting them. As for the textures, I'm using atlases.
     
  22. nocanwin

    nocanwin

    Joined:
    May 7, 2009
    Posts:
    176
    if you're parenting them then you're not going to be able to combine the planes. You want to combine the planes then skin that mesh to the the bones.
     
  23. GabrielEmilio

    GabrielEmilio

    Joined:
    Jan 4, 2012
    Posts:
    3
    Ok thanks!
    Does it have to be a smooth bind, or can it be a rigid bind?
     
  24. Lotho

    Lotho

    Joined:
    Feb 19, 2012
    Posts:
    9
    Hey guys, im struggling with this too. Ive combined the planes then tried applying multiple bones at once (i assume thats correct) but it warps the entire model when I alter the bones. Is this something that skin weights affect if not done properly?

    Ok, after just messing around for 10min with the paint weight tool I worked it out, ooops.
     
    Last edited: Feb 19, 2012
  25. Deleted User

    Deleted User

    Guest

    We have realized .FBX exporter ( with bones animations) directly from Umotion 2D !
    In these days we are testing it with great results.
     
    Last edited by a moderator: Mar 1, 2012
  26. monkeypit

    monkeypit

    Joined:
    Jan 4, 2012
    Posts:
    38
    I have a question about swapping textures out for sometime like an attack ( example: a different facial gesture ). Are you guys switching them out at run time using a script in Unity or just animating UV coordinates in the 3d package? or do both work?
     
  27. MikaMobile

    MikaMobile

    Joined:
    Jan 29, 2009
    Posts:
    845
    I don't think animating UV coordinates for a single polygon within a more complex mesh is possible - I could be wrong, but the only way I've animated UV's is by changing the offset of the whole material to switch to a different "frame" within a sprite sheet, which might not be ideal for just changing a facial expression and nothing else.

    I've done texture swaps for "2D" characters in various ways - in OMG pirates, we swapped the entire texture based on an animation curve. In Battleheart, we just had two different versions of an enemy's head that were part of the same skeleton, and the one that wasn't being used at the moment was just scaled down to 0 in the animation (as long as we never cross-faded, you'd never see the transition). In Zombieville 2, we ended up doing a complete model swap for zombies who were frozen, on fire, had their head exploded, etc. I wouldn't say any of these are necessarily the "best" way to do it, it sorta depends on the specifics of your project. The Battleheart method is probably the simplest, and is adequate for a character which only needs a couple different faces or something simple.
     
    theANMATOR2b likes this.
  28. monkeypit

    monkeypit

    Joined:
    Jan 4, 2012
    Posts:
    38

    I'm still in the middle of testing out animations for our game and I'm using Max to handle those. So far I've been able to animate the UV coordinates successfully with in the program itself. I have yet to prove it works in Unity though, but I'll let you know if it does. At least from a Max point of view.

    So do you have two different heads attached to two separate bones and just scale them in and out during the animation? If I'm correct in understanding, I've actually tried that for the eyes on our characters to have them blink, but that's a great idea for swapping out parts as well. I think I recall you mentioning that earlier in the thread. Thanks for reiterating it for me. :)
    I'm trying to re-use the game art as much as possible and create an interesting looking game with my friends. How do you personally break up your atlas'? I'm still trying to work out the best solution for this.

    Being that I'm the only artist on my team, and this is my first phone project to boot, its been a fun struggle and this thread has been very helpful.
     
    theANMATOR2b likes this.
  29. MetaMythril

    MetaMythril

    Joined:
    May 5, 2010
    Posts:
    150
    Interesting, did you have two bones (one for each head), split off from the main body and scaled down the bone of the unneeded head in the animation or did you have two faces (quads) tied to one bone and scaled those down instead of the bone?

    EDIT: heh, just noticed that monkeypit asked the same question. Sorry for the double question. :p

     
    Last edited: Apr 2, 2012
  30. Pollypally

    Pollypally

    Joined:
    Apr 21, 2012
    Posts:
    4
    Sorry double post somehow. Guess this forum doesn't like my mobile..
     
    Last edited: Apr 21, 2012
  31. Pollypally

    Pollypally

    Joined:
    Apr 21, 2012
    Posts:
    4
    Big thanks to Mika, aka Pirate, and everyone else in this thread. It's been a big help to working out how I will make my game idea. Previously I have only done basic things in C++ but it sounds like Unity is the way to go. Do I need Unity Pro for this or will the free version do? I have 0 experience with Unity and Maya and my 3DsMax xp is next to nothing.

    Mika I purchased your games on Android for reference as your videos on your website are really poor quality so I'm not sure if that worked in your favor or not :p

    BTW did you end up making that Demo/Tutorial?

    Even if it's paid I'd be grateful for something to get me started.

    BTW BTW I'm assuming these same processes work for Android and PC as well as iOS?
     
    Last edited: Apr 21, 2012
  32. MetaMythril

    MetaMythril

    Joined:
    May 5, 2010
    Posts:
    150
    I'm surprised this hasn't been posted here yet. This Kickstarter project looks really promising for making 2D art for Unity and I would suggest others check it out. I'll be looking to help them build support for Unity. :)

    Spriter:
    http://www.kickstarter.com/projects/539087245/spriter

    UPDATE: They have 7 days left and are already 179% funded. Additional funding will go toward developing more advanced features. Really looking forward to this! Spread the word if you feel the same way! :D
     
    Last edited: Apr 21, 2012
  33. Matz

    Matz

    Joined:
    Jul 3, 2012
    Posts:
    12
    Btw guys how do you combine the skinned meshes in one model, and then export it to unity.

    Because when i do that, my model wont show up in UNITY.