AS3 - Dynamic 3D API

OK, I have decided to release a nice 3D API which I made a few days ago… It’s not perfect and you are likely to come across display issues regarding z-sorting, but if you design your Objects according to certain guidelines, you shouldn’t encounter any major problem. So it’s still rather flexible.

What it does is take an array of coordinates and makes a textured 3D object that rotates according to the mouse position; it would look nice on a loading screen as a play-around thing… Or if you want to alter the rotation part of the script, go ahead.

Here’s an example of what I made using the API: http://jongro.phpnet.us/space.html

The API consists of an “Object3D” class which has exactly 3 parameters; the first is the stage of your movie (this is to detect the position of the mouse in regards to the stage), the second is an array of building-blocks - I will explain this later, and the third is a BitmapData object that will be used as the texture for your object.

Now everything should make sense except for the “Array of building-blocks” and this does get a bit complicated… What the second parameter is, is what I like to call a 3D object structure; it’s an array of components which contain other, lesser components which contain other components; so there are 3 levels; The top-level of every 3D object is the 3D object itself, second is the component level; which consists of simple 3D shapes like pyramids, prisms, and cubes… Prisms and cubes tend to display the best, but working with pyramids is quite feasible.

Now this might get confusing, but I’ll give you tips on how to work this later on:

In order to break down these sections, I have made the 2nd parameter a multi-dimensional array; this does make the code more complicated, but it adds a great deal more flexibility to your design… Remember that there are 3 levels; the top level array is just a single new Array() (or []) which you pass as the 2nd parameter to Object3D… Inside that array though, are other arrays and these are actually 3D building-blocks like pyramids or prisms… Now inside each of these building-block arrays, you have to define more Arrays each of which defines a plane containing 4 Objects which are 3D points (x,y,z). Ok, so that’s the structure.

Let me give you a simple example:

// define the planes
var plane1:Array = [{_x:10,_y:20,_z:30},{_x:10,_y:20,_z:30},{_x:10,_y:20,_z:30},{_x:10,_y:20,_z:30}]
var plane2:Array = [{_x:50,_y:20,_z:50},{_x:30,_y:40,_z:30},{_x:60,_y:50,_z:70},{_x:30,_y:40,_z:36}]
var plane3:Array = …

// define the building-blocks
var buildingblock1:Array = [plane1,plane2,plane3,plane4,plane5,plane6]
var buildingblock2:Array = [plane7]

// define the 3D object
my3DArray = new Array([buildingblock1],[buildingblock2])

^ Now while it’s probably easier to see the structure, It’s not the easiest way to write it, if you open the attached files, in “Main.as” you will find an example of how I like to write it.

So, ultimately, 3D points define your 3D shape, but they are grouped into sections so that the code can manipulate the “z” of each building-block separately, and the “z” of each plane within each of these building-blocks also. The idea is that it will not only help the system sort out all the shapes more effectively, but it might also aid you in structuring your 3D object in a way that makes things more readable.

Now again, I highly recommend that you look inside “Main.as” to see what I’m talking about.

Now, there are some things you need to know; EVERY plane needs to have EXACTLY 4 points, so, how do you make a triangular plane then? Well it’s simple; just make it so that the plane has two “conjoined” points.

Another issue which you might encounter is depth-inaccuracies; the current algorithm sorts everything according to average z-indices and in some cases, overlapping shapes could be problematic when it comes to having the right depth; not to worry; there is a work around! There is something I call “Abstract Planes” basically, they are written just like normal planes and are added to a building block to alter their “weight” so that the z-sorting works better; all you do is make a plane which has all its points with a value of 0 except for one coordinate; x, y, or z, so you pick the most appropriate coordinate (the one that is directed away from the origin) , give it a value and the remaining coordinates will be 0; this will insure that the plane isn’t drawn on the screen while it can have a significant effect on the z-index of a building-block.

You can see an example of an abstract plane in my “Main.as” document class…

So yeah, that’s it. Enjoy!

P.S. yes I know it’s quick, messy, and isn’t as fast as possible; feel free to make improvements.

Also, It would be fun if you posted some object you made with it in case you decide to try it out; I’d like to see what you can do with it :slight_smile:

[EDIT]I have removed the source files as I am considering exploiting an upgraded version of this API and I don’t want to cause confusion, you are free to keep it if you’ve already downloaded it. Cheers.[/EDIT]