Mobile
 

XNA Game Studio 3.0 : Creating Fake 3-D - Creating Shadows Using Transparent Colors

11/29/2011 4:48:11 PM
Lots of the graphics in games are faked. Rather than make something 3-D, a game programmer makes something that looks 3-D but turns out to be much easier to program. In this section, you make some 3-D text, but without using any complicated rendering or models (although you can do this kind of thing if required). You use only two principles:
  • Things that are 3-D have shadows.

  • Things that have the light shining directly on them look the brightest.

This means that you need to draw your text in three stages. First, you draw the shadows, then the "sides" of the text, and finally the top layer of the text. This seems like a lot of work, but, as Figure 1 shows, I think it’s worth the effort.

Figure 1. 3-D text that "jumps" out of the screen


1. Creating Shadows Using Transparent Colors

The first part of the text that you want to draw is the shadow at the back. You draw your picture from the back forwards and use the fact that each time you draw, you add to what’s already there. You use another feature of XNA drawing: colors that cause things to be drawn slightly transparent (that is, with part of the background showing through). By drawing transparent colors on top of each other, you can get a nice blurry effect, as done in the following code:

Color nowColor = new Color(0,0,0,20);
for (layer = 0; layer < 10 ; layer++)
{
spriteBatch.DrawString(font, nowString, nowVector, nowColor);
nowVector.X++;
nowVector.Y++;
}

This code is very similar to the previous code that draws the 3-D text except that it creates the value for nowColor in a slightly different way. The Color is constructed from four values rather than three:

Color nowColor = new Color(0,0,0,20);

The first three values give the intensity of red, green, and blue, which you’ve set to 0 because you’re drawing in black. The fourth gives the transparency of the color. In graphical terms, this is often called the alpha channel value. The bigger the number, the less the background shows through. Just like your color intensity values, the transparency value can range from 0 (completely transparent) to 255 (solid color). If you don’t give a transparency value, the Color is created as solid.

A value of 20 means that a lot of the background shows through the color that you draw. Figure 2 shows the display produced by drawing 10 times using a transparent black value. Note that because each of the drawing positions is slightly different, you get a blurring effect.

Figure 2. Creating a shadow using transparent colors


This works rather well in that the text is nicely blurred around the edges, as a shadow would be. Now you know one way video games achieve blur. They can do it by repeatedly drawing the same scene in slightly different positions.

The next part of the drawing process uses the same technique you’ve used before, except that you use slightly different colors. The complete drawing method is as follows:

protected override void Draw(GameTime gameTime)
{
graphics.GraphicsDevice.Clear(Color.CornflowerBlue);

DateTime nowDateTime = DateTime.Now;
string nowString = nowDateTime.ToLongTimeString();
Vector2 nowVector = new Vector2(50, 400);
int layer;

spriteBatch.Begin();

// Draw the shadow
Color nowColor = new Color(0, 0, 0, 20);
for (layer = 0; layer < 10; layer++)
{
spriteBatch.DrawString(font, nowString, nowVector, nowColor);
nowVector.X++;
nowVector.Y++;
}

// Draw the solid part of the characters
nowColor = Color.Gray;
for (layer = 0; layer < 5; layer++)
{
spriteBatch.DrawString(font, nowString, nowVector, nowColor);
nowVector.X++;
nowVector.Y++;
}

// Draw the top of the characters
spriteBatch.DrawString(font, nowString, nowVector, Color.White);
spriteBatch.End();

base.Draw(gameTime);
}


This produces the display shown in Figure 2.


2. Drawing Images with Transparency

Something else that’s useful is that if you draw an image using a color that has a transparency value, the image is drawn transparently. This is how game programmers get pictures to fade slowly onto the screen. The image is repeatedly drawn with different levels of transparency to make it slowly appear over a background.

 
Others
 
- Android Application Development : ViewGroups (part 2) - ListView, ListActivity, ScrollView & TabHost
- Android Application Development : ViewGroups (part 1) - Gallery and GridView
- Java ME on Symbian OS : MIDP 2.0 Game API Core Concepts
- Java ME on Symbian OS : Building a Simple MIDP Game
- jQuery 1.3 : Compound effects & Creating custom animations
- jQuery 1.3 : Effects and speed
- Mobile Web Apps : Quick Wins (part 2) - Form Field Attributes
- Mobile Web Apps : Quick Wins (part 1) - Nifty Links
- iPhone Developer : Using Creative Popping Options
- iPhone Developer : Navigating Between View Controllers
- iOS SDK : Basic SQLite Database Manipulation (part 3) - SQLite Binding, Inserting, Updating, and Deleting
- iOS SDK : Basic SQLite Database Manipulation (part 2) - Select
- iOS SDK : Basic SQLite Database Manipulation (part 1) - Opening the Database, Statements, Preparing Statements, and Executing Statements
- The Anatomy of a Mobile Site : PRIMARY SITE CONTENT (part 3) - Forms
- The Anatomy of a Mobile Site : PRIMARY SITE CONTENT (part 2) - Embedding Images and Media
- The Anatomy of a Mobile Site : PRIMARY SITE CONTENT (part 1) - Text, Typography& Pagination
- iPad Does Not Show Up in iTunes & Synchronization Problems
- iPad Troubleshooting : Re-register with Your iTunes Account
- XNA Game Studio 3.0 : Making a Prettier Clock with 3-D Text (part 2)
- XNA Game Studio 3.0 : Making a Prettier Clock with 3-D Text (part 1)
 
 
Most View
 
- Adobe Flash Professional CS5 : Manipulating Symbols in 3D Space (part 1) - Controlling the camera view: Perspective and vanishing point
- Adobe Flash Professional CS5 : Manipulating Symbols in 3D Space (part 2) - Transforming symbols with the 3D Rotation tool
- Mobile Web Apps : Loading Pages (part 3) - Going Backwards
- Microsoft Dynamics AX 2009 : Design and Implementation Patterns (part 1) - Class-Level Patterns
- Introducing the iPhone SDK (part 5) - Programming Paradigms
- Beginning Android 3 : Set Up the Emulator
- Microsoft Excel 2010 : Analyzing Worksheet Data - Adding Data Validation to a Worksheet
- Microsoft Dynamic CRM 2011 : Resolving a Service Request Case
- Accessing PowerPoint on the Web and Mobile Devices (part 1) - Setting Up SkyDrive
- Microsoft Excel 2010 : Using Print Preview