Load and Animate an MD2 Model in Away3D

The Backstory

Nearly every 3D Flash project I’ve recently developed has needed at least one 3D model for something. So my next logical Away3D adventure was exploring model loading.

The Description

Just looking over the list of loaders in the away3d.loaders package is impressive.  Seeing all those supported types right out of the box just blew me away.

I’ve found that the MD2 format, while super out-of-date, is one of the best choices for model formats in Flash 3D engines. It has two main advantages over other formats: simplicity and super compact files.

MD2 is a super common file format and most 3D modeling software can export it without any additional plug-ins or converters.

The Classes

There is only one class you’re gonna need to use an MD2 model: MD2. It has loading logic and events built-in as well as a publicly accessible parser for embedded data. After loading or parsing it returns an Object3D so you can throw it in a scene, skin it, animate it or whatever.

The Setup

You’re gonna need two things: a 3D model in the MD2 file format and a material. For this tutorial I’m using Boba Fett from my weird AR video. Don’t worry, he’s provided with the example code later in the post.

The How-To

First things first, you need to decide whether you want to dynamically load your model and material at runtime, or embed them.  I’m going the embed route because it’s easier for demonstration purposes.

There are two steps for displaying an MD2 model:

  1. Parse the file
  2. Apply the material
  3. Add the model to the scene

Pretty freakin basic. Here’s what it looks like in code:

var bobaFett:Mesh = Md2.parse(Cast.bytearray(new MD2Tutorial.bobaFettModel()));
bobaFett.material = new BitmapMaterial(Cast.bitmap(new MD2Tutorial.bobaFettMaterial()), {smooth: true});
this.view.scene.addChild(bobaFett);

As far as animation goes, well, that’s a different beast and I’m only going to touch on it slightly.

Basically, MD2 animation is stored as an array of frames. Each frame contains a copy of the model with it’s verticies moved. So, animation is achieved by swapping the rendered verticies with the ones in a particular frame. Each frame also has a name in this format: prefix + frame number. This name is used to access and identify multiple animations across a series of frames.

To play animation in an MD2 file you need to have prior knowledge of what animations are provided with that model.  Boba Fett has # different animations with these prefixes:

  • stand
  • run
  • attack
  • pain1
  • pain2
  • pain3
  • jump
  • flip
  • salute
  • taunt
  • wave
  • point
  • crstand
  • crwalk
  • crattak
  • crpain
  • crdeath
  • death1
  • death2
  • death3

So, armed with that knowledge, all we have to do is one line of code to start playing one of those animations.

bobaFett.play(new AnimationSequence("run", true, true, 8));

Pretty simple (except for my trainwreck of an explanation). If you combine the loading and animation you get code that should look similar to this:

var bobaFett:Mesh = Md2.parse(Cast.bytearray(new MD2Tutorial.bobaFettModel()));
bobaFett.material = new BitmapMaterial(Cast.bitmap(new MD2Tutorial.bobaFettMaterial()), {smooth: true});
bobaFett.play(new AnimationSequence("run", true, true, 8));
this.view.scene.addChild(bobaFett);

The Example

Here’s a quick example using the code from above.

The Code

Download the code for this example here. Keep in mind that this was built using Flash 10, so if you’re using Eclipse or Flex Builder then you’ll have to change some stuff in your project before it will compile.

Posted in Away3D | Tagged , , , | 7 Comments

Create Terrain in Away3D

The Backstory

After nerding out over skyboxes and continuing my exploration of Away3D I stumbled upon the Elevation class. After a little research I learned that it’s a super fast and easy way for making terrains! More nerding out and reminiscing over the old counterstrike days followed.

The Description

Terrain is just that, terrain. It’s what you walk on. It’s the river that you ford, the mountains you climb, the road you travel and the hills that you roll (over).

The Classes

There are two classes that you’ll use to make your very own terrain: Elevation and SkinExtrude. Elevation generates verticies from a heightmap (more on this later). And SkinExtrude just displays it all.

The Setup

To make this thing work you’re going to need two images: a heightmap and a material. The material is just that, the material that gets rendered (in this case it gets applied to the SkinExtrude). The heightmap is a bit different, all it does is describe different heights using a single color channel, 0 being the lowest and 255 being the tallest.

There’s a ton of different ways to obtain these images: there a programs made to generate random terrain, you could use actual USGS data, you could snag them from some game mod, etc. For this demo I just went into Photoshop.

First I created my base image:

  1. Create a new layer
  2. Apply Filter > Render > Clouds
  3. Apply Filter > Artistic > Watercolor

That gives me something like this:

base

Now I need to make it look like terrain, so I just created a Gradient Map layer and chose a gradient (pretty much at random). This gives me my material:

terrain

As for the heightmap, I just took my base image, created a layer over it filled with pure red (#ff0000) and set that layers’ Blend Mode to Multiply. So, here’s my heightmap:

heightmap

Pretty simple, and if you take some time on the material you could get some really good looking terrain.

The How-To

With our material and heightmap in-hand all we need is some code to put them into action:

// grab the BitmapData from the embedded images
var heightmap:BitmapData = Cast.bitmap(new TerrainTutorial.jpgHeightmap());
var terrain:BitmapData = Cast.bitmap(new TerrainTutorial.jpgTerrain());
 
// create the material to be applied to the terrain
var material:BitmapMaterial = new BitmapMaterial(terrain, {smooth: true});
 
// create an Elevation instance
var elevation:Elevation = new Elevation();
 
// generate verticies based on the embedded heightmap image
var verticies:Array = elevation.generate(heightmap, "r", 12, 12, 5, 5, 1);
 
// create the actual terrain with the generated verticies and apply the material to it
this.extrude = new SkinExtrude(verticies, {coverall: true, material: material});
 
// SkinExtrudes are facing the wrong way by default, so we rotate it
this.extrude.rotationX = 90;
 
// add it to the scene
this.view.scene.addChild(this.extrude);

Not so bad. The place you’ll want to pay attention to is the elevation.generate() function call. Here’s a rundown of it’s parameters:

  1. sourceBmd: The heightmap bitmap data
  2. channel: The color channel in the heightmap to calculate the heights from
  3. subdivisionX: How many pixels wide a subdivision (face) is
  4. subdivisionY: How many pixels tall a subdivision is
  5. scalingX: Width scale factor
  6. scalingY: Height scale factor
  7. elevate: Elevation scale factor

The subdivisionX and subdivisionY are what to pay attention to. The smaller the number, the more accurate the terrain but the more faces there will be. When working with terrains you will constantly balance accuracy and face count.

The Example

Here’s an example using the material and heightmap from the how-to.

The Code

Download the code for this example here. Keep in mind that this was built using Flash 10, so if you’re using Eclipse or Flex Builder then you’ll have to change some stuff in your project before it will compile.

Posted in Away3D | Tagged , , , , | 1 Comment

Create a Skybox in Away3D

The Backstory

I’ve recently been looking into alternatives to Papervision3D so I decided to see what all the Away3D hooplah was about. Turns out that it’s pretty freakin sweet and as I was pouring over the API docs I came across a pair of classes called Skybox and Skybox6. Since I was huge into CounterStrike mapping back-in-the-day, this triggered my inner super nerd.

Since the Skybox example on the Away3D site really didn’t do a good job explaining how to use it, I’m going to detail it for you.

The Description

If you play 3d shooters, a skybox is used for the sky (duh) and distance scenery/environment. It’s literally just a cube with seamless skin on the inside faces. The cube also moves with the camera, so it always stays way off in the distance.

The Classes

Away3D comes with two classes to make the implementation of skyboxes super easy: Skybox and Skybox6. The difference is that Skybox takes 6 individual images (for the 6 faces of a cube) while Skybox6 takes one image comprised of the 6 images for each face.

Since the Skybox class is super straight-forward, just give it 6 square images and add it to the scene, I’m gonna concentrate on Skybox6.

The Setup

Skybox6 takes one image that is used for the 6 faces of the skybox cube, this image is affectionately referred to as a 3×2 texture (three faces wide by two faces tall). What Away3D doesn’t explain is how to setup this 3×2 image. Well, it looks something like this:

skymapDebug

  • Top-left: Front face
  • Top-Middle: Right face
  • Top-Right: Back face
  • Bottom-Left: :Left face
  • Bottom-Middle: Up face
  • Bottom-Right: Down face

Just keep in mind that since a Skybox is just a skinned cube each face is square, so each sub-image in your 3×2 texture must also be square. In my example each face is 512×512, so my 3×2 texture is 1536×1024. Kinda big but it gets the job done.

The How-To

Now that you have a texture, lets jump into some code:

var skyboxMaterial:BitmapMaterial = new BitmapMaterial(Cast.bitmap(new SkyBoxTutorial.jpgSkymap()));
var skybox:Skybox6 = new Skybox6(skyboxMaterial);
this.view.scene.addChild(skybox);

Boom, done. You could even condense that into one line of code if you wanted to.

The Example

Here’s a quick example I whipped together using that exact code from the how-to along with a wireframe plane to show how the skybox looks relative to other objects.

The Code

Download the code for this example here. Keep in mind that this was built using Flash 10, so if you’re using Eclipse or Flex Builder then you’ll have to change some stuff in your project before it will compile.

Posted in Away3D | Tagged , , , | 1 Comment

AR music video starring Boba Fett

What would happen if Boba Fett starred in an AR app? A badass music video!

Actually, I slapped Boba Fett into this simple AR app as part of a presentation I did recently over FLARToolkit. Turns out it was a hit, big enough to warrant it’s own music video.  You can read a summary of the presentation on the Phenomblue blog.

FLAR Boba Fett Demo from Phenomblue on Vimeo.

Posted in Augmented Reality | Tagged , , , | 1 Comment

Business Cards: Augmented Reality Style

Another article sent to me from @k_to_the_t is about a sweet idea, putting AR markers on business cards. This should give the dusty-old business card market a technological kick in the pants. I wanna give this dude a high-five.

AR Business Card from James Alliban on Vimeo.

Posted in Augmented Reality | Tagged , | 1 Comment

The future-past techno of Minority Report

I was introduced to another thought provoking article by way of @k_to_the_t, but this time it’s about hardware (as opposed to my usual M.O. of software). Go ahead and check em out:

Basically, Nokia filed a patent for a glove that detects when the skin in your hand stretches, which is pretty cool. Engadget then went on to say that they think it would be used for Augmented Reality applications. Sure, that sounds perfectly plausible. However, I see something wrong with this and not just because it conjures up images of the overused Minority Report analogy.

Notice that he’s wearing special gloves to use that crazy contraption!

The thing is, you don’t need special skin watching gloves to do stuff like that. In fact, that sort of technology already exists… in a form. Infrared cameras and sensors (a.k.a. WiiMotes) are where it’s at. There have been huges advances in the past few years of people using WiiMotes and projectors to create multi-input devices.

If you haven’t heard of it WiiFlash is hugely popular to achieve these sorts of interactions. In fact, the dude who started in all, Johnny Lee, demonstrates all this and a ton of other sweet stuff you can do with a single WiiMote.

So, you could wait for Nokia to make a weird glove for user input (now it reminds me of a Nintendo Power Glove), or you can make your own using a WiiMote and some cheap RadioShack parts.

Posted in Augmented Reality | Tagged , , , , , , , , | 1 Comment

The New Frontier of Augmented Reality

Augmented Reality concepts are starting to get really cool. @k_to_the_t pointed out this article about about a concept called Augmented ID from The Astonishing Tribe. It’s so cool I’m not even going to try to explain anything about it, just watch the demo:

With Augmented ID and TwitARound, it looks like things are moving towards AR social networking.

Could AR be the next social media frontier? If it’s deployed on mobile devices, then hell yes.

It’s obvious that AR is moving into mobile in a huge way. Out of all the demos I’ve seen, the best ones are deployed on phones, the majority being iPhones. Right now, in the iPhone SDK, the video API is too limited so all those demos can’t be made into apps deployable on the App Store. So Apple needs to get their ass in gear and change the SDK so we can conqure the App Store with AR goodness. It just so happens that people are already petitioning for this, check it out.

Posted in Augmented Reality | Tagged , , , , , | 1 Comment

What’s that? AR without markers?

There’s some libraries out there getting some attention that don’t require you to print-out a marker to experience augmented reality. Here’s a video of me on phenomblue.tv giving a quick demo of one of these sweet, sweet libraries:

AR Without Markers from Phenomblue on Vimeo.

Posted in Augmented Reality | Tagged , | 8 Comments