Sunday, March 17, 2019

Pedestrian Animation Update

This is a quick update. I made some improvements to 3DWorld's pedestrian walking animations that were introduced in the previous post. I've added a knee joint to go with the hip joint on each leg. This looks much more natural than any of the strange animations from last time. I'm still applying the exact same animation code to all three models.


It's not perfect though. Some of the models have their legs spaced out too far for a casual walking animation. Also, none of their arms move. Real people swing their arms at least somewhat while walking, and also move the other parts of their bodies a bit. Still, it's good enough for now. I've gotten the basic animation down. That's 90% of the way there with 10% of the work.

I'll continue to add new models of people, improved walking, and new animations in the future. In fact, I've already added one new model of a man since recording this video. The model looks good and has high quality textures. However, the animations don't quite work because the area between the bottom of his shirt and the top of his pants stretch in an unnatural way. I'll get back to it later.

Once again, the code can be found here in my 3DWorld project on GitHub.

Sunday, March 10, 2019

Procedural City: Animating Pedestrian Models

I finally got around to experimenting with adding animations to the 3D models of people in my procedural city. I ended up doing something pretty simple that at least looks better than no animation at all. Before I get into the details of what I tried, let me discuss the problem statement in more detail.

I've decided on the following constraints to pedestrian animation in 3DWorld, listed starting with the most difficult:
  • Must use free models found online (since I don't have the time/patience/skills to create them myself).
  • Must be done without buying or installing any fancy 3D content creation/animation tools.
  • No art skills required.
  • The same animation system must work with multiple models from different sources in different formats.
  • Must scale to a few hundred animated models of people with ~10k-50k triangles each.
  • Something that can be done in a few weeks or less.
  • Should look better (more realistic) than no animation at all. I sure hope this is the case!
That seems like a real challenge for creating high quality animations. I suppose the standard flow would be to join a 3D model website where I can download models of people with animation data. Then either use a third party model/animation loading library, or write this part myself. Then implement the animation system in a special shader with a lot of CPU side code support. That would be a lot of work and could take more than a month of late night programming. Is there an easier system that I can use to get started, one that works with the models I already have?

Let me throw in some simplifications/trade-offs that should make this problem easier, at the cost of reduced quality and realism.
  • I only need one simple walking animation.
That's a start, but it doesn't help much. How about:
  • Only the legs need to move. We'll just leave their arms sticking out to the sides for now.
 Okay, that's a bit easier, but still seems like a lot of work. Let's do something silly to really simplify things:
  • Only the hip joints are animated.
Yeah, I know, it's not going to be the greatest walking animation. But hopefully it will be better than no animation where people appear to slide on the ground or ride on invisible roller skates. What's the worst that can happen? Take a look and see - but don't judge it until you see the end. I started with some simple test animations.



The video shows me cycling through the various animations, which are named in white onscreen text. In case you didn't catch it, the animation names are: "The Slide", "The Bunny Hop", "The Flip", "The Twirl", "Marching", "Walk Like an Alien", and "Walking". The vertex shader code for all of these can be found here in my 3DWorld GitHub project. Sure, all of the animations look at least somewhat silly. That's the point, right now I'm just experimenting. I'll bet you laughed at the alien walk animation; I watched the video three times so far and laughed every time! That alien walk was actually one of my first attempts at rotating the legs at the hip where I was incorrectly scaling the hip location by the height of the model.

[Bonus: The pedestrians are actually crossing the roads safely and not getting hit by cars.]

The final walking animation doesn't look too bad. In fact, it looks almost normal when viewing pedestrians from a distance. It looks like someone walking very stiff-legged without bending their knees up close. I feel it's definitely better than no animation at all. I'm sure I can work to improve it, but I'm surprised at how good it looks already with so little work. All of this took only a few hours this weekend, and at least half the time was spent adjusting constants and transforms in the shaders. That's definitely time well spent. I didn't have to download new models, install 3D modeling tools, add new dependencies, parse a new file format, or implement a new shader flow. I'm not sure why I was so worried about this originally.

How did I implement this animation feature? There are in fact no animation files, no bone weights, no transform matrices. I'm passing the static model vertices that are loaded from disk straight to OpenGL for rendering. All of the magic is in the vertex shader, which applies transforms to the individual mesh vertices using a procedural algorithm. I suppose this is how procedural animation works.

The various body parts can be identified from the sign and magnitude of the (x,y,z) coordinates of each vertex. X = left/right, Y = up/down, and Z = front/back. The sign of the x-value will tell you which side (left or right) of the body you're on. Normalized y-values near 0.0 are the feet, and values near 1.0 are the head. This allows the shader to, for example, alternatively rotate the left and right legs about the hip by looking the the x and y values of each vertex. The hip joint is located around y=0.4 in all three models.

Each pedestrian has an animation time value. This time is reset to zero when stopped so that their body has a neutral (default) position. As each person walks, their animation time increases based on the product of velocity and elapsed realtime, producing a smooth animation timeline while in motion. The faster the person is moving, the faster the animation plays. A combination of fract() (fractional component) and sin() are used to turn linear time into a periodic walking cycle.

There's one thing wrong in the video. Can you spot it? The rotations I'm applying should also rotate the vertex normals. Without this, the lighting on the models is incorrect. I need to go back and fix that. Do I simply rotate the normals using the same matrix? [Update: It's fixed, but I didn't record a new video yet. Apparently you do just apply the same matrix to rotate the normals.]

I'll continue to work on improving pedestrian animation. It should be straightforward to add additional joints in this way. The knee joint is probably next to add. This a very slow and tedious way to put together animations, but it does require minimal code, and it works the same with all the models I have. Once I have enough models, it will probably be less work to add animations this way that it would be to animate each individual model using a real animation tool.