Tuesday, January 31, 2012

Skinned Meshes (clothes)

To make a cloth scarf for my character, I modeled a mesh in Blender, and added an armature with a single bone.
In Unity, once an armature is added to a mesh, it is considered a skinned mesh and "Skinned Mesh Renderer" is automatically added as a component to a prefab set in the scene.
I then added a "Skinned Cloth" component to the armature's root bone in the prefab, and painted "max Distance" on the vertices.

Example:
Create the scarf in Blender, then added an Armature and one tiny Bone where the scarf will attach to the character. You only needed one Vertex Group and one Bone for this example.

Before you even start, be sure that your scarf has enough geometry to deform! This means that your mesh should not be made of one square. The mesh works best when there are enough "hinges". Imagine a wooden snake. If the snake is only cut from one piece of wood, it will be stiff like a pencil, but if you create hinges in the wood, then the wooden snake will be able to bend at the hinges. See the image below. My scarf has a nice amount of hinges.

1. Model your scarf, then create your Armature in Object Mode and add one Bone to it.

2. In Object Mode, select your mesh > select your Armature > CTRL+P > create empty vertex groups.

3. Select your mesh and go to Edit Mode > Properties Panel > Object Data (the upside down triangle symbol) >Vertex Groups

4.Check that Weight is set to 1 (the highest weight) and be sure that you have a vertex group with the same name as your bone. This assigns the vertex group to the bone with a matching name.

5. Select all vertices on your scarf mesh > be sure that the vertex group is highlighted on the list > click Assign. It will look like nothing happened, but now when you click the Select and Deselect buttons, the assigned vertices will toggle on and off - this shows which verts are being controlled by the highlighted group

6. Export your model being sure to include the armature in whatever format you choose. (I used FBX).

Click for larger image

7. Import to Unity.

8. Create prefab of the scarf to be used in scene > Drag it into the scene.

9. In the Hierarchy, select your scarf and navigate until you get to the bone you created in Blender. Your Skinned Mesh Renderer will be on this bone NOT on the mesh itself. See image below.

10. After selecting the bone, add the Skinned Cloth component. On the top menu bar select, Component > Physics > Skinned Cloth. Your scene should now look like the one below. You can copy my settings but I suggest reading the documentation and experimenting.

Click for larger image
YOU'RE ALMOST DONE!!!

11. Click the paint brush button under the Skinned Cloth Component. This will allow you to paint weights on each of the vertices on the scarf. I only used the Max Distance parameter. The points at the tail end of the scarf are at 5 (long stretch), the ones in the middle are between 3 - 0.2 , and the verts at the neck have 0 (no stretch). This tool actually seems to be buggy, but with some tweaking, you'll get it right.
 
Click for larger image
TRY IT OUT!!!

Find a way to attach your cloth to your character and watch it move! Have fun! And let me know if you still run into problems.

7 comments:

  1. Hello,
    I found your blog because I was searching for a way to animate a scarf with unity and blender, and that is actually what you did! I tried the component and I wanted to know if you could post your results or something like how you get it done... because I really get weird/ugly stuff and that could help.
    Thank you :)

    ReplyDelete
    Replies
    1. Here is an example of it working: http://staging.worpd.com/99-problems/

      The scarf on her neck is a skinned cloth. Her hair has one too. When she jumps, her hair and scarf deform.

      Would you post an example of your problem for me so I can better help you? In the meantime, I'm happy to post a short walk-through.

      Delete
    2. I added a more in-depth explanation above, so I hope this helps. If you have more questions, feel free to ask.

      Delete
    3. Thank you so much for those all, that help me a lot. I still have some questions:

      - My scarf mesh has lots more vertices than yours, I was wondering if the stretching depends on the number of vertices.

      - Also is it important to have only 1 bone, having could be useless?

      -Why there is no collision with your character but the scarf always stays behind?

      Thank you so much for your help!

      Delete
    4. - The number of verts determines the places where your scarf can bend, painting the max distance on a vert determines the amount of stretch.

      - You can have more than one bone, but you'll be making things more complicated than they need to be. Reason being that the Skinned Mesh Renderer is only placed in the root bone of the armature, and you can only place the Skinned Cloth component where there is a Skinned Mesh Renderer. There is only one Skinned Mesh Renderer per Armature, not per bone. Simple answer - you don't need the extra bones in this case.

      - If you look at my settings under Skinned Cloth in the picture, I have "Use Gravity" turned on. This causes the scarf to always fall and not float around in space. Because I have the verts near the neck set to not move a lot, the scarf stays behind my character.
      I also have gravity enabled in my game. You can see your global gravity setting by going to Edit > Project Settings > Physics. My Y value is set to -50.

      Delete
  2. Hi latorril, sorry for taking so long...
    Finally, I gave up with the SkinnedCloth.
    It was expensive in time and hard to get the result I wanted.
    I had to get a scarf working with collision and that I could limit also on axis. For that I used Configurablejoint, It works well now. thank you once more for your help, I really learnt a lot.

    Just to know, did you design a real game with that little kid? And on what are you working right now?

    Thx again!

    ReplyDelete
  3. Hey S.B. I barely saw your reply (1 month later)

    I'm really glad I could help, and I am planning to finish a game about the kid character in the future. At the moment, I've been sharpening my modeling skills, and working on side projects: My 3D Projects

    I hope you've been making good progress with your work! Keep it up :D

    ReplyDelete