As promised, I’m hereby posting the second part of the series about using Avatars with XNA GS on the XBox 360.
If you remember, on my first article I showed how to draw an animated avatar on screen taking into account transitions between two animations.
In this part, I will talk about one factor that will help you improve a little bit the eye-candy in your game when using avatars: shadows.
As you may know, the Avatar’s rendering system does not allow us to use a custom shader effect to render an avatar on screen; instead, we must only use the built-in system to accomplish that task.
On one side, this simplify things but on the other, it limits our possibilities a little bit.
Casting shadows is an example of this limitation. As I will show you in a minute or two, a “cheap” workaround can can used, for simple games.
The technique I’ll use is known as “Flat Shadows” (the XNA Framework includes all we need for it). It is a rather basic substitute for “real” shadows, but it will do the trick just fine for projects that don’t require “picky” shadow effects.
We will be using the project I had included last time as a starting point and mainly focus on the code to add and or modify.
1. Fields to add:
What’s new? The ‘shadow transforms’ array will store the matrices we need to flatten the model based on a reference plane, which we also define.
2. The constructor:
Nothing fancy here. Just create the array that will hold the collection of matrices to flatten the model.
3. Initializing the game:
We just create the reference plane with a normal facing up and without moving along that normal (so it’s a XZ plane where the Y coordinate is zero, initially).
4. Loading content:
We just modify the line that places the avatar in the world.
5. Updating the game:
We will only add this line:
So the light will rotate to show the effect.
6. Drawing the avatar:
Here’s where most changes occur; the game ...:
- ... draws the avatar as on my previous example,
- ... changes the values of the lights that affect the avatar,
- ... adjusts the depth-bias to avoid any eventual z-fight,
- ... rotates the light and adjusts the plane altitude before flattening the model,
- ... updates the position of the flattened model using the static method of the matrix struct, which is named in the code as “CreateShadow”,
- ... draws the fake shadow, and finally ...
- ... restores the values of the lights and position of the avatar’s model meshes.
7: Changes to transitions code: none.
If everything goes fine you will see something like this:
Things to notice, though:
- Shadows will be drawn even when there’s no elements to cast shadows on (see how part of the shadow is rendered beyond the “floor” for a short period of time),
- You will have to modify the location of the shadow when the position of the avatar changes its height (i.e.: if jumping),
- The model’s meshes are flattened onto a reference plane, so it will only work for objects on that specific plane (like, in my example, a floor), and
- Thus, there’s no self-shadowing.
A more precise approach would be extending this example by trying to use the stencil buffer and depth data, as explained at the end of this thread.
Well, this is it for today. You can find the source code for this example here.
> Link to Spanish version.