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.