Since the announcement of the 3.1 features a number of people have had questions about the new Avatar API’s. I wanted to take a few minutes to preview some of Avatar API’s to help developers better understand what to expect in the 3.1 release.
There are three main classes that are used to render and animate an avatar.
Each avatar has a description that describes how the avatar looks and what clothing the avatar has on. This description tells the system what models, textures, colors, and scales to use when rendering an avatar. This description is mostly opaque to the developer but does allow for the developer to get the height and body type of the avatar. The height is in meters and the body type allows the developer to know if the avatar is female or male. The AvatarDescription object can be created in three ways. The first way is to read the Avatar property on any of the SingedInGamers. The second is to use the static methods on AvatarDescription to create random descriptions. The third is to send an AvatarDescription over the network or save it to a file by looking up a byte array from the AvatarDescription, transferring those bytes, and then creating a new AvatarDescription from the byte array. I will create a future post to talk more about the third method.
The AvatarRenderer is used to draw an avatar to the screen. You create a new AvatarRenderer by passing in an AvatarDescription object. To draw the avatar to the screen you set the World, View, and Projection matrix properties on the AvatarRenderer object and then call the Draw method. The draw method takes two parameters. The first is an IList<Matrix> called bones. This is a list of the bone transforms to use when rendering the avatar. The transforms are in local bone space relative to their parent. The avatar has a total of 71 bones. The second parameter is an AvatarExpression. Avatars use textures for the mouth, eyes, and eyebrows. The AvatarExpression is a structure that holds enum values that specify the current state of the eyes, eyebrows, and mouth. Now you may be asking where you get the list of bone matrices and the avatar expression. The easiest way is to use the AvatarAnimaion class.
There are 31 built in animations that developers can use in their games. Most of these animations are used in the dash. An AvatarAnimation object is created by passing an enum value from AvatarAnimationPreset. Once created the developer updates the animations current time. After setting the current time in the animation the developer can access the bones and expression needed by the AvatarRednerer.Draw call by using the BoneTransforms and Expression properties.
Now let’s check out how to code this up.
In the games initialize method load the description, renderer, and animation.
AvatarDescription avatarDescription = AvatarDescription.CreateRandom();
AvatarRenderer avatarRenderer = new AvatarRenderer(avatarDescription);
AvatarAnimation avatarAnimation = new AvatarAnimation(AvatarAnimationPreset.Clap);
Now in the games update method update the animation.
Finally in the draw method set the world, view, and projection matrix and call the draw method.
avatarRenderer.World = Matrix.CreateRotationY(MathHelper.ToRadians(180.0f));
avatarRenderer.Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45.0f),
avatarRenderer.View = Matrix.CreateLookAt(new Vector3(0, 1, 3),
new Vector3(0, 1, 0),
All this talk about how to render an avatar without a picture would be wrong. So below is one of the first screen shots taken while working on this feature.