Up until now, we have only drawn cubes. It's about time we looked at some other forms. In this recipe, we look at how you can use Away3D's built-in shapes to create different visualizations. We will replace the cubes from the previous recipe with cylinders.
We will adapt the previous recipe. So create a copy of it and move it to a new package or rename it. Finally, set it as the document class.
First let's make the Graph3D
class a little more generic:
Now we can put our new drawing function inside the main program (although in a real program, you may want to keep it in the Graph3D
class):
Main
class:private var _lightPicker:LightPickerBase;
_lightPicker = new StaticLightPicker([light]); _graph = new Graph3D(_lightPicker); _graph.data = _data; _graph.drawFunction = drawCylinder; _graph.createGraph(); _graph.x = -350; _graph.y = -250; _view.scene.addChild(_graph);
drawCylinder
function:private function drawCylinder(x:Number, y:Number, z:Number):void { var cylinderGeometry:CylinderGeometry = new CylinderGeometry(20, 20, y); var cylinderMaterial:ColorMaterial = new ColorMaterial(0xff9933); cylinderMaterial.lightPicker = _lightPicker; var cylinder:Mesh = new Mesh(cylinderGeometry, cylinderMaterial); cylinder.x = x; cylinder.y = y/2; cylinder.z = z; _graph.addChild(cylinder); }
If you run the program now, you should see the same graph, but now drawn with cylinders instead of cubes.
ActionScript allows you to store pointers to functions inside variables. We use this to make the drawing function customizable. We can create whatever function we want to draw the data points, but it only has to accept three parameters.
Next we build up a mesh based on a cylinder geometry. This piece of code is exactly the same as drawing a cube, only now we create a different geometry that takes different parameters.
The parameters for the cylinder geometry are the radius at the top and bottom, and the height. Most of the additional parameters that the cylinder geometry takes will be of little use when creating graphs.
This short recipe showed only one different shape that you can use. The shape of the graph elements can be adjusted in a number of ways.
Apart from the cube and cylinder, Away3D also has a ConeGeometry
shape that will be of use for this type of graph. If you draw different types of graphs, you may also find a use for CapsuleGeometry
and SphereGeometry
.
If you're not happy with the existing shapes, it is possible to define your own geometry and draw whatever you desire.
We'll take a closer look at this in the Graphing a function in three dimensions recipe of this chapter; however, keep in mind that this is a very advanced topic and should not be attempted by first-time 3D developers. You're often better off creating a shape in the 3D editor and loading that one.
Away3D comes with a number of parsers that can load 3D models from 3D editors, such as 3D Studio Max and Wavefront. This is, by far, the easiest way to get a custom model into your application.
An example can be found at http://www.allforthecode.co.uk/aftc/forum/user/modules/forum/article.php?index=6&subindex=8&aid=328.
18.191.14.196