Tuesday, August 23, 2011

An easy way to create 1x1 textures for XNA.

I've noticed on more than a few occasions while reviewing XNA code for demos and game engines that people often need small textures in order to render diagnostic information. More often than not, they will create a small bitmap file and feed it through the content pipeline. It's no big deal, but there is a slightly easier way that is also better performing - just create a 1x1 texture directly, without using the file system.

Here's a function that does just that:

public static Texture2D CreateSolidTexture(GraphicsDevice graphics, Color color)
	var tex = new Texture2D(graphics, 1, 1, falseSurfaceFormat.Color);
	tex.SetData<Color>(new Color[] { color });
	return tex;
As a bonus, anyone using your demo, engine or what have you will have a slightly easier time using your code because they won't have to deal with migrating the sample content into their code space.

Tuesday, August 16, 2011

Notes about our new content pipeline features

The video below represents a personal milestone for me as it demonstrates the results of something I've been wanting to do for a very long time. 

Shortly after finishing Star Ninja, I decided the next game was going to be 3D. While I have done quite a bit of 3D graphics in the past, that was some time ago and I never really had a chance to work closely with HLSL much less all the many things that go into creating content that uses it. While trying to use the more conventional ways of getting the content I wanted in game, I found the available solutions were not compatible with XNA, not flexible enough or simply not affordable. Clearly, it was time to tackle that problem and get our own system for this because it's an important problem to solve.

I have since created a content pipeline that allows me to author content in Softimage, including HLSL code, and automatically process the data into a form that is usable by the game engine with minimal effort. Essentially, I can create a model, animate it, create (or reuse) HLSL shaders, hit export and it's pretty much ready to use in game. Creating content easily is an essential  feature of any modern pipeline and while this took a lot of work to figure out I am glad to have it because I will probably use this for quite some time. Some of the major features include:
  • Automatic HLSL semantic and vertex stream support.  
  • Support for multiple UV sets, vertex color and weight map data streams, which can be used as vertex streams or raw data for general game logic. 
  • Instanced model support. 
  • Skinned meshes.
  • Multiple animations.
  • Mesh optimization / vertex reduction.

The next game will of course use all these features so it is pretty neat to see the work result in something that  will nearly eliminate the error prone manual effort previously involved with getting 3D content into the game. Being able to author content that makes use of arbitrary HLSL code that doesn't require code changes to use in the game engine should also help me raise the visual fidelity of what I create by quite a lot.

While creating this system, I realized that I'm working in an uncommon place - the intersection of code and art. Most people do one or the other. I know I'm not a great artist, but by being able to enhance my limited artistic abilities with good tech I think I'll be able to create higher quality content than I would be able to do otherwise. For instance, if I want to create an asset with some special shader effect, I can just open the shader for that specific model and modify it; the exporter will propagate those changes and the run time will use the newly available data without any code changes required. This includes situations where the shader changes require a changes in textures or vertex stream for per vertex colors, weights. Pretty handy!

I'm really excited about the next project and what this new pipeline will do to help.