Free your Unity 3D

Why is a free version of Unity 3D such a big deal?

First off, Unity 3D kicks ass.

Secondly, the ability to build and deploy high-quality 3D applications is available to everyone for the low, low price of $0.

Thirdly, the SDK is already mature and full of great features. The 3rd party, open source libraries for Flash can’t come close to competing with it.

Fourthly, you’re bringing OpenGL and DirectX to the web browser. That beats the hell out of Flash and its slow, software only rendering.

Oh, did I mention it’s free? Flash isn’t even free, and a ton of those who use it have a pirated copy. Although, one of the few things that Flash has on Unity 3D is it’s market saturation. And there’s really no way around that.

All-in-all, I think that offering a free version of Unity 3D is gonna change the game for 3D web apps. Flash’s sluggishness has been the bottleneck of a flourishing 3D web experience. With a little luck, and community support, shit’s gonna get real.

  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • email
  • Ping.fm
  • Reddit
  • StumbleUpon
  • TwitThis
Posted in General | 3 Comments

Color Programming in Flash

When I first thought about trying to wrap code around color values in Actionscript, years ago, I was super intimidated. I conjured up ideas that it was a world full of complex algorithms and crack-brained theories. That’s really about half true.

If your goal is some crazy results then you’re probably going to be dealing with some crazy algorithms. But otherwise the basics of it are pretty straightforward.

Note that I will not be talking about pixel bender in this post.

Color Theory

I’m not really going to talk about color theory at all. Basicaly because I don’t understand enough of it (yet) to talk in any real depth. Just not that color theory is a huge topic and thankfully you’ll only have to deal with a very tiny little part of it.

I have found that the Colour Space Conversions article can come in handy as it explains just about everything you might want to know about color.

Color in Memory

The foundation of color programming is understanding how a color is represented in code.

A color is made of 3 or more channels, each channel being 8 bits in size representing values from 0 to 255. In memory a color is stored as a single unsigned integer (uint) by arranging the bits of each channel side-by-side. So an RGB color would have a total of 24 bits (3 channels) and look something like this:

Color Channels in Memory

No, it’s not a power meter. As you can see the 8 red channel bits are the furthest to the left, then the 8 green bits and finally the 8 blue bits. And no, the bits themselves aren’t red, green or blue, they just represent those colors.

Since the bits are stored side-by-side then the color also has a number value. If all bits in a color are off (zero) then the number value is 0 (which is used to represent black). If all bits are on (one) then the number value depends on how many channels the color has (i.e. 3 channels = 16777215). This max value is used to represent white.

There are two different color spaces in Flash/Flex: RGB and ARGB

RGB is just like the example above with a red channel, green channel and blue channel. This is called a 24 bit color and is pretty standard.

ARGB is like RGB but has an alpha channel. This is called a 32 bit color (because of the extra channel).

Color in Syntax

The most common way for defining a color in code is by using the hex syntax:

// 24 bit colors
var black24:uint = 0x000000;
var white24:uint = 0xffffff;
 
// 32 bit colors
var black32:uint = 0x00000000;
var white32:uint = 0xffffffff;

Just like a hex code in HTML except the hash (#) is replaces by a 0x. Notice that a 32 bit color has two extra values for the alpha channel.

Since a color is also an integer value you could use integers to define your colors:

// 24 bit colors
var black24:uint = 0;
var white24:uint = 16777215;
 
// 32 bit colors
var black32:uint = 0;
var white32:uint = 4294967295;

Color in Code

Now that we know how colors are stored, lets talk about how to tear a color apart to extract it’s different channels. Since the channels are stored side-by-side in an unsigned integer we have to do some bitwise operations to get their values.

If you don’t know what bitwise operations are I would highly advise looking into them. They’re kinda confusing at first but they can be a huge boost to your code.

Here’s the code for grabbing channel values from a color:

// for 32 bit colors
var alpha32:int = color32 >> 24;
var red32:int = color32 >> 16 & 0xff;
var green32:int = color32 >> 8 & 0xff;
var blue32:int = color32 & 0xff
 
// for 24 bit colors
var red24:int = color24 >> 16;
var green24:int = color24 >> 8 & 0xff;
var blue24:int = color24 & 0xff;

The method is this:

  1. Shift the bits to the right by the size of a channel (8) multiplied by number of preceeding channels
  2. Bitwise AND by value 255 (binary 1111, hex 0xff)  to clear any channel bits to the left of desired channel

The resulting channel values will be between 0 and 255.

Now that we can get channel values, how do we stitch them back together to form a cohesive color? More bitwise action!

// for 32 bit colors
var color32:uint = alpha32 << 24 | red32 << 16 | green32 << 8 | blue32;
 
// for 24 bit colors
var color24:uint = red24 << 16 | green24 << 8 | blue24;

The method:

  1. Shift the channel value to the left by the size of a channel (8) multiplied by the number of preceeding channels
  2. Bitwise OR each shifted channel value in order (RGB or ARGB);

Conclusion

That’s about it for the basics. There’s a bajillion things you can do once you know how to tear apart and restitch colors.

  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • email
  • Ping.fm
  • Reddit
  • StumbleUpon
  • TwitThis
Posted in Actionscript | Tagged , , , | Leave a comment

Create an Over-The-Shoulder Camera in Away3D

The Backstory

This tutorial is an accident. No really, a total accident. I was putting together a totally different tutorial when I thought it would be cool to throw Boba Fett in it and explore stuff. So, that unfinished tutorial gave rise to this tutorial.

The Description

I’m not actually sure if it’s called an Over-The-Shoulder camera, but if you’ve ever played any modern 3D adventure game (i.e. Tomb Raider, World of Warcraft, Fable, etc) your camera is usually following behind your character and looking over their shoulder.

I’m sure there are multiple ways to implement an Over-The-Shoulder camera and although this method is far from perfect it is pretty simple.

The Setup

You don’t need anything special. There are really only two basics necessary, a subject (I’m using Boba Fett) and a trusty Camera3D.

The How-To

Please Note: The code snippets here won’t compile if they’re pasted in an actionscript file. You’ll need to refer to the full source code so see how everything fits together.

The first thing you’ll need to do is create Boba. If you’re not sure how to do this go see my MD2 tutorial.

Next we want our camera to follow Boba when ever he moves around. Solutions for this can be complex or surprisingly simple. I like simple, so I just threw Boba and my camera in an ObjectContainer3D.

this.bobaGroup = new ObjectContainer3D();
this.bobaGroup.addChild(this.bobaModel);
this.bobaGroup.addChild(this.view.camera);
this.view.scene.addChild(this.bobaGroup);

So, instead of interacting with the Boba model, you interact with the group and the camera will always be along for the ride.

Now we just need to adjust the cameras position within the group, so we add this line after the camera is added to the group:

this.view.camera.position = new Number3D(75, 300, -1000);

At this point we now have a camera that will always follow Boba Fett around and it positioned behind his right shoulder. Now all we have to do is add in some logic for moving him around:

protected function move():void
{
	if(this.keys[87]) this.bobaGroup.moveForward(10);	// w
	if(this.keys[65]) this.bobaGroup.moveLeft(10);		// a
	if(this.keys[83]) this.bobaGroup.moveBackward(10);	// s
	if(this.keys[68]) this.bobaGroup.moveRight(10);		// d
}
 
private function onEnterFrame(e:Event):void
{
	this.move();
	this.view.render();
}

Now he should move move forward, backward, left and right and the camera should be following along. Albeit, he looks a little odd without any animations but that’s an entirely different beast so we’re not going to worry about it in this tutorial.

The tricky part is using the mouse to look around. We basically have to treat the camera like a TargetCamera3D but have it look at a point in space rather than an actual model.

protected function look():void
{
	var amt:Number;
	var percent:Number;
 
	// left-right camera movement
	if(this.stage.mouseX != this.lastMouseX)
	{
		amt = this.stage.mouseX - this.lastMouseX;
		percent = amt / this.stage.stageWidth;
		this.bobaGroup.rotationY += percent * 360;
		this.lastMouseX = this.stage.mouseX;
	}
 
	// up-down camera movement
	if(this.stage.mouseY != this.lastMouseY)
	{
		amt = this.stage.mouseY - this.lastMouseY;
		this.view.camera.y += amt * 2;
		this.lastMouseY = this.stage.mouseY;
		this.view.camera.lookAt(new Number3D(75, 250));
	}
}
 
private function onEnterFrame(e:Event):void
{
	if(this.mouseDown) this.look();
	this.move();
 
	this.view.render();
}

The camera only moves if it’s vertical movement, horizontal rotation is applied to the group (which is used for turning). The trick to making this work is the lookAt call after moving the camera vertically, this ensures that the camrea is always looking at the right spot in space.

The Example

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.

  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • email
  • Ping.fm
  • Reddit
  • StumbleUpon
  • TwitThis
Posted in Away3D | Tagged , , | 4 Comments