共用方式為


Rendering and Animating Avatars (Xbox 360)

Note

Avatar-related APIs are exposed on Windows to ease cross-platform development between Windows and Xbox 360. By design, all avatar-related methods return default values, and rendering methods do not draw anything on the screen.

Complete Sample

The code in this topic shows you the technique for rendering and animating a gamer's avatar. Use IAvatarAnimation if you want to integrate custom avatar animations with standard animations. You can download a complete code sample for this topic, including full source code and any additional supporting files required by the sample.

Download AnimatedAvatar_Sample.zip

To render a random model with a standard animation

  1. Initialize the gamer services dispatcher for your application.

    Components.Add(new GamerServicesComponent(this));
    

    For more information, see Initializing and Updating Gamer Services.

  2. Declare three new members in your main game class to store the various objects needed to render and animate the random avatar.

    AvatarDescription avatarDesc;
    AvatarRenderer avatarRenderer;
    AvatarAnimation avatarAnimation;
    
  3. Create and initialize the avatar data to a random set with a call to CreateRandom. Use the Initialize method of your Game class after the existing call to Initialize.

    avatarDesc = AvatarDescription.CreateRandom();
    
  4. Create a new avatar renderer with the standard loading effect for the recently created random avatar. Use the Initialize method of your Game class.

    avatarRenderer = new AvatarRenderer(avatarDesc, true);
    
  5. Create a new AvatarAnimation object, and load any standard animation from the list supported by AvatarAnimationPreset. Use the Initialize method of your Game class.

    Because the avatar gender is unknown at the time of rendering, for this example use the gender-neutral clap animation.

    Note

    Gender-specific animations work with any avatar, but each is designed with the proportions of that gender in mind. This prevents the avatar model from clipping through itself during the animation cycle.

    avatarAnimation = new AvatarAnimation(AvatarAnimationPreset.Clap);
    
  6. Initialize the avatar's transformational matrices (world, projection, and view), using the World, Projection, and View properties of the avatar object. Use the Initialize method of your Game class.

    avatarRenderer.World = 
        Matrix.CreateRotationY(MathHelper.ToRadians(180.0f));
    avatarRenderer.Projection = 
        Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45.0f),
        GraphicsDevice.Viewport.AspectRatio, .01f, 200.0f);
    avatarRenderer.View = 
        Matrix.CreateLookAt(new Vector3(0, 1, 3), new Vector3(0, 1, 0),
        Vector3.Up);
    

    Note

    The values used in this example are for demonstration purposes only. Although they provide a good location and orientation for the camera (in terms of this sample), your application will require different values, which are determined by your application's needs.

  7. Make any necessary modifications to the current values of the avatar's ambient light and directional light properties by using the similarly-named properties of the avatar object (for example, AvatarRenderer.AmbientLightColor).

  8. Update the current animation of the avatar.

    The recommended place for this code is within the Update method of your application.

    if (avatarRenderer.State == AvatarRendererState.Ready)
    {
        avatarAnimation.Update(gameTime.ElapsedGameTime, true);
    }
    
  9. In the Draw method of your game, render the avatar by calling Draw.

    avatarRenderer.Draw(avatarAnimation.BoneTransforms,
        avatarAnimation.Expression);
    

    When you render the avatar, keep in mind that various parts (for example, glasses) of the avatar model can be transparent. Transparent parts are rendered with the depth buffer disabled. For this reason, first render all opaque objects (except the avatar) in the scene, and then render the avatar and any other transparent objects sorted by depth (from back to front).

    In addition, avatar rendering ignores the current state of the FillMode property. It is always treated as if FillMode.Solid is the current value.

See Also

Avatar Support Overview (Xbox 360)
AvatarAnimation Class
IAvatarAnimation Interface
AvatarDescription Class
AvatarRenderer Class