Top

Modelling for XNA with Blender Part I

March 10, 2008

blender_big.jpgIn this series, we will look at using Blender to build models and assets for use in XNA games. In part I, we build a cube, apply a texture to it, and export it to fbx format.

Note: the Blender/XNA series is intended for coders who have every intention of hiring/finding an artist to build their assets, but would like to know enough about the basics to whip up some crude, textured, and animated models to use in their tests. If you would like to learn Blender in more detail, or you are an artist looking for a richer how-to document for Blender, I recommend the Introduction to Character Animation tutorial which can be found on the BlenderWiki. This series of articles covers only the bare essentials, and may be used as a companion to the previously mentioned tutorial.

When modeling for XNA you’ve got a lot of options. Since XNA readily accepts models in the fbx format, pretty much any 3D modeling package you can find is ready, right out of the box, to be used to build models for your XNA games.

With that said, you’ve got four classes of packages to choose from:

  • Top dollar software (Maya, 3DS Max)
  • “Free” versions of top dollar software (XSI Mod Tool)
  • Low budget software (Wings3D, trueSpace)
  • Free software (Blender)

Most of us with limited budgets are going to wipe the top dollar offerings right off the plate. For me, not only is the cost prohibitive, but I’m not even an artist — I’m a coder. Why would I spend thousands on software that there isn’t a chance in hell I’m ever going to figure out how to use, just so I can whip up some half-way decent models to use in my unit tests until I can pay a real artist to build me some good ones?

The “free” (note the quotes) offerings like Maya PE and Mod Tool, unfortunately, come with some severe limitations. Maya PE is not going to work because you can’t export, and Mod Tool comes with the caveat that you can’t use it for commercial works. What if your game starts turning out really good, as in, I can charge for this? You’re going to run afoul of the license.

For me, personally, the low budget offerings would be fine, except for one small factor: there is a full-featured, awesome piece of open source software that blows them out of the water, and with a total price of free. No cost, no restrictions on what you can do with your creations. And a positively huge community of helpful users. C’mon, there really isn’t any better alternative. That software, of course, is Blender.

Now, you might have heard some bad things about Blender. Actually, you’ve probably only heard one bad thing among all the good things people have to say about it. And that one thing is, the interface stinks! I’m going to be the odd one out. I’ve used a lot of 3D modeling software in my time,and I haven’t been able to figure out any of it. It simply doesn’t make sense to me. Blender, however, immediately made sense to me. I don’t know why, but something just clicked when I was using it the first time.
Again, I’m a programmer, not an artist, but wow are these programs complicated. When I’m building something for a game, all I want to do is build up my model, texture it, maybe add some bones to it and animate it. Right? So why is it so hard to accomplish such a simple task? The answer is that most modeling software wasn’t designed for you to build animated models for XNA. It was designed to do everything, it just so happens to be capable of building animated models for XNA. Your task is to navigate through all the options you don’t need to get to the stuff you do. Luckily, Blender seems to make finding all that other stuff kind of complicated, which is great for me, because I don’t know what any of it is. Everything I need is right in front of me, which cuts down on my anxiety. That’s a really good thing.

We’re going to go through four stages of modeling using Blender for XNA. We’ll look at:

  1. How to build a textured cube
  2. How to build a UV textured model
  3. How to add bones to a model
  4. How to animate a model

That’s pretty much all you need to know to use Blender for your XNA project. In fact, you only need 1 & 2 if you just want to build furniture, vehicles, weapons, and so on for your game. We really only need 3 and 4 to build more complicated stuff like players and NPCs.

So let’s get started. Begin by downloading the latest version of Python at http://www.python.org, and then Blender at http://www.blender.org/download/get-blender/ and install them in that order.

Note: I’ll be using Blender 2.45 for this demo. Blender seems to have this weird habit of changing things pretty dramatically between minor releases, so get your learn on while the interface still looks like I’m about to describe!

blender1.jpgNow fire up Blender. You’re presented with a default scene, which contains a cube. This is the start state for Blender, and you should see it whenever you start the program. A word of caution here: Blender’s interface was designed for efficiency, and what that means is that you will be doing almost everything using keyboard shortcuts. Good luck remembering them all!

Even though we’re going to just render a cube here, let’s delete the cube and recreate it. Hit A to select everything in the scene, and then DEL. Now hit SPACE and select Add->Mesh->Cube. We’ve got our cube back. You might have noticed a little cross hair in the middle of the window before the cube appeared. That’s the cursor, and it defines where the center of the new mesh is going to go. You could have moved that around if you wanted, but we want it in the center anyways.

So how come it doesn’t look like a cube? Because we’re looking at it square on. If you want to change your viewing perspective, hold down your MIDDLE MOUSE BUTTON and wiggle the mouse around. We’re now changing the perspective, so we can look at the cube from an angle. Looks like a cube now, doesn’t it?

So now we want to texture this thing. Let’s use the same texture we used in the Silent Hill Cube render example. You should see it off to the left. It’s a 128×128 square jpg. It could be a different size, but for a small cube like this, smaller is better. Just make sure whatever you use is a power of 2 in dimensions, i.e. either 32×32, 64×64, 128×128, 256×256, etc.

Where should you save your texture? You should save it to your XNA project’s Content folder. I like to make a subfolder called Textures and stick all my textures in there. You don’t have to, but it’s important that XNA knows where to find your texture when you build your project.

Now back to Blender. How do we wrap this texture around our cube? Well, we need to do what is called UV texturing. But we’re going to do a really, really simple version of it. In part II of this series, we’ll be tearing our model apart and actually mapping coordinates of our texture to our vertices, but since all we have here is a cube, we just want to apply the texture to each face of the cube.

blender2.jpgTo do this, we’re going to work in two separate windows. We need to split the Blender window into two separate windows. To do this, click the MIDDLE MOUSE BUTTON where the modeling window meets the menu bar at the top. A menu will pop up, so select “Split Area” and move the vertical divider to the middle of the window. LEFT CLICK to set the split. Great, now we’ve got two windows that look the same.

Underneath the right view window, you should see a little drop down that says Edit Mode. This is where you choose what mode this window is in. We actually want to be in Object Mode, so select Object Mode from this drop down.

Now we’re ready to texture. Hit F5 to bring up the shading window. You’ll see it at the bottom of the screen.

If you aren’t in the Materials buttons list, click the Materials icon (its a red ball). Click Add New. You just added a material to your mesh.

Now look on the right, and you’ll see a panel with a tab that says, Texture and one that says Map Input. In the Texture tab, click Add New. You’ve just added a texture to your material. You’ll notice that an entry named Tex appears in the list on the left.

Now select the Map Input tab. We’re going to tell Blender how to map this texture to our cube. Click the UV button. Awesome, that’s done.

Review what you just did: you created a mesh, added a material to it, added a texture to your material, and told Blender to map the texture to your mesh using UV.

Ok, now go back up to your view windows, where your cube is sitting. In the left window, change the mode to UV Face Select. We’re going to tell blender which of the faces to do the UV mapping on. We want to do it on all of them, so make sure every face is selected. The quick way to do this is to hit A.

blender3.jpgNow in the right window, change the Window Type (not the mode, the window type) to UV/Image Editor. (See the image at the left for what the window type selector looks like). The window should change to a flat square. This is your texture — well, it will be! We need to tell Blender where our texture is. So select Image->Open from the menu (the menu on this window), and navigate to your texture. Open it, and the square should now display your texture. You should also see that your cube on the left has received its texture!

blender4.jpgAgain, let’s review. After we got our cube all set up to be textured, we imported a texture and applied it to every face of our model.

Well, we sure seem to be done, don’t we? All we need to do is export our model to fbx format. On the main menu, select File->Export->Autodesk FBX. Save the fbx file to our content directory.

Are we done? Well, not quite. We still have to make sure that our XNA project can handle all of this. Add the FBX file to your content pipeline using the Add->Existing Item option in Visual Studio. Now we need to do one last thing. We need to make sure our FBX file really points to the texture. Guess what? It doesn’t. Nothing is ever easy, right?

Open the fbx file in any text editor (use vi you apes!) Search for the string RelativeFilename. There should be two instances of this line in the file, which look something like this:

  1. RelativeFilename: "fbx/mytexture.jpg"

Not good! We don’t even have a fbx folder. We need to change this line ever so slightly:

  1. RelativeFilename: "../Textures/mytexture.jpg"

And we need to do that in both places. That’s it! Now render your cube as you would any model in XNA, and you should see it all nice and textured.

Next time, we’ll look at building a more complicated mesh, and applying a more complicated texture to it. For now, here’s a neat-o video of our textured cube:

Comments

4 Responses to “Modelling for XNA with Blender Part I”

  1. GameDevKicks.com on March 11th, 2008 9:16 am

    Modelling for XNA with Blender Part I…

    You’ve been kicked (a good thing) - Trackback from GameDevKicks.com…

  2. Taylor on March 12th, 2008 9:52 pm

    Awesome tutorial man! that was way easier than some of the stuff i’ve come across.

    My only problem is i’m pretty weak on the code side… I can’t seem to get my models to show up in my apps, and the only code example I’ve found so far was from XNA 1.0 .. could you possibly add or send me a sample of code that works so i could study it?

    Thanks for the tutorial man!!

  3. stromdotcom on March 12th, 2008 11:27 pm

    Taylor- I’ve got a brief overview of the XNA Model class up here at Stromcode, but if that’s not enough to get you going, I’d recommend looking at the Getting Started info pack at creators.xna.com. The very first tutorial walks you through building your first project, copying a model and texture over to your Content folder, and displaying it in code.

    From there, it gets more complicated, but I’ll be covering pretty much everything you need to know here over the next couple weeks.

  4. Building an XNA Skybox with Blender : Stromcode on March 30th, 2008 5:47 pm

    […] the basics of UV texturing and exporting in Blender.  If you aren’t up to speed, check out  part one and part two of my XNA/Blender […]

Got something to say?





Bottom