Thursday, November 5, 2015

Tutorial - Importing a Seamless nCloth Animation into Unreal Engine 4

~ ~ ~ ~ ~ ~ ~ ~

How I Did What I Did

~ ~ ~ ~ ~ ~ ~ ~

 

Seeing as that all of my previous posts are solely made up of my successes and fails whilst trying to get this to work, I've decided to write an easy-to-follow tutorial that explains all of the steps that I took to get to my end product. Here we go!

Part 1: Step 1: Clothimation


 1. Open up Autodesk Maya and switch to the FX toolkit

2. Change the length of the timeline to a large number like 1000.
This will help us when creating the animation. 

3. Create a plane and set the subdivision width and height to a high number.
 I set mine to 40 and would suggest not setting it to anything under 25.
Keep in mind that lower subdivision values will speed up the process once you get to Part 2.

4. Create a piece of geometry that the nCloth will attach to. It doesn't matter what it is.

5. Move and scale the  plane and the solid component to the desired location and size.
6. Select the plane and go to nCloth> Create nCloth.
I chose to make a flag and flagpole. I added textures to make it look good.

7. Select the vertices that are touching the solid component, then shift-select the solid component.
8. Go to nConstraint> Point to surface.
9. Edit the Gravity and Wind component on the nucleus and the Dynamic
Properties of the nCloth to get your desired effect.
The base nCloth animation.

Part 1: Step 2: Gotta cache 'em all


1. Start playing the animation. You will notice that the nCloth falls down initially,
but then gets picked up by the wind, eventually resulting an a stable animation.
This is where we will start the loop. Move the time slider to the start of that stable animation.

Examples of bad and good starting points

2. Select the nCloth and go to Fields/Solvers>Initial State>Set for Selected.
3.  Change the length of the timeline to the desired animation length plus 50.
Mine's set to 550. I will explain why we do this later.

4. Move the time slider to frame one and select the nCloth.

5. Go to nCache>Create new Cache>nObject>Option Box.

6. Change the Cache directory to a memorable location.
You can also change the Cache name if you wish.

7. Leave all other settings default and press create. Wait until the cache is done being made.

8. Select the nCloth and find the nClothShape node in the Attribute Editor.
Uncheck the 'Enable' box.

Part 1: Step 3: Hit New Trax


1. The next part will be done in the Trax editor. Open it by going to
Windows>Animation Editors>Trax Editor

2. Select the nCloth and go to nCache>Attach Cache.
 Find the cache you previously made and click open.
 In the Trax Editor, you should see two nCaches.

3. Click on the top nCache and drag it to the left until there is a 50 in the bottom right corner.
This means that the last 50 frames of the nCache animation is
being played in the first 50 frames of the timeline.

The nCaches are offset correctly. (50 frames)

4. Change the timeline length to the initial desired timeline length, cutting out the last 50 frames.
This means that the first frame and the last frame are both the 500th frame
of the cached animation, making it loop. We're not done yet, however. Those extra 50 frames will allow us to transition nicely.

5. Select either the first or the second nCache and you will see the cacheBlend node available in the Attribute Editor. Move the time slider to the first frame.

6. Set the first nCache's input weight to 1 and the second nCache's input weight to 0.
Right click on both of their names and set keys.

7. Move the time slider to 50 and change the first nCache's input weight to 0 and the second nCache's input weight to 1. Once again, set keys by right clicking on both of their names.
These nCaches are blended seamlessly.

The animation should loop seamlessly! Good job, now let's move into more uncharted territory.

 
The final looping animation.

Part 2: Step 1: The Bone Script

1. In this part, we'll add joints to every vertex on out mesh. Sounds spooky, right?
It actually works surprisingly well. The first step is to copy the following script:
string $sel[] = `ls -sl`;
float $selPos[3] = `xform -ws -q -t $sel`;
PolySelectConvert 3;
string $verts[] = `ls -sl -fl`;
select -clear;
//string $jParent = `joint -name "jParent" -p $selPos[0] $selPos[1] $selPos[2]`;

for ($v in $verts)
{
float $pos[3] = `xform -ws -q -t $v`;
//select $jParent;
joint -p $pos[0] $pos[1] $pos[2];
}

SelectAllJoints;
group -n BoneGroup; xform -os -piv 0 0 0;

float $rMin = `playbackOptions -q -min`;
float $rMax = `playbackOptions -q -max`;
int $frame;
for ($frame = $rMin; $frame <= $rMax; $frame++)
{
int $vjCounter = 0;
currentTime -e $frame;
for ($v in $verts)
{
// --get it's position..
float $vpos[3] = `xform -q -ws -t $v`;
// --get the name of the appropriate joint incremented by 1.
string $jName = "|BoneGroup|joint" + ++$vjCounter;
// --set the pos of the joint to the pos of the vertex..
move $vpos[0] $vpos[1] $vpos[2] $jName;
// --set keyframe
setKeyframe -at translate -t $frame $jName;
}
}

2. This is a MEL script first created in 2003.
To use it, open up the script editor by going to Windows>General Editors> Script Editor.

3. Making sure that the language is MEL, paste the script
into the bottom window and hit the blue play arrow.

The script inside of the Script Editor

 4. Now this is the scary part. Maya itself will be unresponsive for quite a long time while it calculates placing hundreds of joints that follow every vertex.
Wait it out. Maya hasn't crashed. This particular calculation took me 35 minutes,
and I'm on a pretty speedy computer.
 If Maya actually froze, you'll have to manually delete some edge loops to make it easier on Maya

Part 2: Step 2: Love the Skin You're In

1. Once that's done, we'll want to remove all influence the nCloth has on the shape.
 Select the cloth, move the time slider to the first frame, and go to Edit>Delete All by Type>History.

2. Next, we'll export it as an obj, which will remove all unnecessary information. Select the cloth and go to File>Export Selection. The file name is up to you, but make sure the file type is obj.

3. Delete the nCloth. You won't need it anymore. Go to File>Import, find the obj you exported, 
then click import. You'll want to fix any thing wrong with the textures now if you have any.

4. Open up the rigging toolkit. Select all of the joints, then the imported cloth, 
then go to Skin>Bind Skin>Option Box. Set 'Bind to:' to Selected joints and click Bind skin.

If your flag is waving and looking a-ok, then you've successfully rigged a waving flag!
 In the next part, we'll import it to Unreal Engine 4.

The final rigged animation

Part 3: Step 1: Fbxport 'em

1. The first step here is to export all of the models you will want in the game engine.
First, click on the solid component that the flag is attached to.
Go to Modify>Freeze Transformations, then Modify>Reset Transformations.

2. With the solid component selected, go to File>Export Selection. Change the file type to fbx.
Make sure smoothing groups is on and the fbx file format is FBX 2014/2015.
Title the file, find a place to put it, and click Export Selection.

2. Now, select the flag's geometry and go to File>Export Selection.
Use the same settings as you did in the last step.
Title the file, find a place to put it, and click Export Selection.

Part 3: Step 2: Coming to an Unrealization

1. From now on, We will use the program Unreal Engine 4.
 If you're using another game engine, I'm sorry,
 but you'll have to look elsewhere on how to import animations.
Start by opening UE4.

2.Make a new project with no starter content.
 Call it whatever you like and click Create Project.

3. Drag the fbx files from an open window into the content folder in UE4.
For the cloth component, be sure to import the mesh, skeleton, and animations.
For the solid component, only import the mesh.

  4. Double click on the cloth's material and turn on two sided.

5. Drag both the cloth _anim file and the solid component into the viewport.
And you're done! Hit play to see that amazing piece of cloth you made flap in the wind!
I hope this helped you in whatever project you will use it in. So long for now.
The final animation in UE4



Tuesday, November 3, 2015

Part 5 - The Redemtion

So it turns out that if I move the vertices of the already-smoothbinded cloth in Maya, I can make it look pretty much the same as the original. Additionally, knowing this is a big boon to my toolkit. This means I can get a flat - looking cloth animation and move it into any shape I want without breaking anything. Realistic cloth in any shape!

I forgot to mention on my last update that the bones, though insanely huge in number, actually didn't bring down the fps by any noticeable amount. It ran at a smooth 70 fps. This is a huge relief as I thought having a couple of these cloth simulations in UE4 would surely make it run at the speed of a morbidly obese elderly man.

Here is the final imported version of the cloth:



Part 4 - The Importation

The first import didn't go as well as I'd hoped...

 
Turns out that the nCloth node actually destroys the animation if I try to export it. The exact error in unreal was, "". The non-nCloth cloth turned out as I hoped, but, again, there isn't any depth to it. It just sort of stretches around instead of waving.



To be honest, it isn't too bad, but if I want to have the same animation I made in Maya, I'll need to try something else.

Part 3 - Putting bones on Vertices

In this part, I had to put bones on each vertex of the cloth simulation. to do this, I used an old MEL script I found (website link below). When I started the script, Maya froze for about 10 minutes.
I gave up, force quit it and tried again.

 I eventually had to wait approx. 20 minutes before Maya unfroze, but to my surprise, it actually worked! One thing that worries me is that it made 644 bones. Not sure how UE4 will handle that...

I exported a version of it with and without the nCloth modifier in the outliner. Without it, the animation is kind of flat. I'm not sure how well UE4 takes kindly to strangers.

Part 2 - Seamless Cloth

This part took a bit for me to fully understand because I had to create nCaches and use the Trax editor, neither of which I have touched before. Basically, I created an nCache which pretty much baked the animation of the cloth into a cache.

Next, I opened two instances of the nCache in the trax editor offset them, making sure the beginning frame of the animation was at both 0 and 188 on the timeline. (please note that I messed up a bit here. I wanted to create a 200 frame long animation so I mistakenly made an nCache that was 200 frames long. since I needed a blend period to make it look seamless, I could only output 188 frames) I then used a cacheblend to blend between the two caches.


The result is a seamless cloth animation.

 
(Maya subsequently crashed and I had to re-do the last step. Animation is now 195 frames)

Part 1 - Ncloth simulation complete

So nothing groundbreaking yet... but the first step of getting this to work is done. There's a nice Ncloth awning animation in Maya - next step is to make it seamlessly loop.

Overview of what I'm trying to do.

When I first decided that I'd be using cloth in my UE4 Capstone, I assumed that using PhysX cloth would be the way to go. However, when I thought about it more, I realized that I wouldn't be able to open the project on my AMD graphics card at home. I'd have to create my entire capstone in the lab.

The other options I have are to use Havok cloth, AMD's version of PhysX cloth, or bake an Ncloth simulation onto bones in Maya

The first option sounds good, until you consider a little problem: Havok can't work in UE4.
The second option is much more reasonable. I could work at both
home and in the lab, and still have cloth that looks reasonably good.

 Some issues with this are: I'd have to create a seamlessly looping animation from an unpredictable and nearly impossible to realistically hand animate mesh of bones, and would have to deal with the fact that thousands of bones moving every frame will drastically decrease the frame rate in-engine. Although this will eventually be a pre-rendered video, moving around the viewport and baking lighting at any quality will take some time.


Fortunately, there are some documented tutorials I have found on how to loop Ncloth in Maya:
https://www.youtube.com/watch?v=0LFWv7rNufI
And how to apply bones to a moving piece of cloth:
https://facepunch.com/showthread.php?t=1251298

I will post another update when I've made some progress...