|Read more about this book|
(For more resources on Away3D 3.6, see here.)
To demonstrate the basic materials available in Away3D, we will create a new demo called MaterialsDemo.
Some primitives show off a material better than others. To accommodate this, we will apply the various materials to the sphere, torus, cube, and plane primitive 3D objects in this demo. All primitives extend the Mesh class, which makes it the logical choice for the type of the variable that will reference instances of all four primitives.
The Cast class provides a number of handy functions that deal with the casting of objects between types.
As we saw previously, those materials that can be illuminated support point or directional light sources (and sometimes both). To show off materials that can be illuminated, one of these types of lights will be added to the scene.
In order to load textures from external image files, we need to import the TextureLoadQueue and TextureLoader classes.
The various material classes demonstrated by the MaterialsDemo class are imported from the away3d.materials package.
These materials will all be applied to a number of primitive types, which are all imported from the away3d.primitives package.
The CubFaces class defines a number of constants that identify each of the six sides of a cube.
The following Flash classes are used when loading textures from external image files, to handle events, to display a textfield on the screen, and to define a position or vector within the scene.
The MaterialsDemo class extends the Away3DTemplate class (download code Ch:1).
public class MaterialsDemo extends Away3DTemplate
One of the ways to manage resources that was discussed in the Resource management section was to embed them. Here, we see how an external JPG image file, referenced by the source parameter, has been embedded using the Embed keyword. Embedding an image file in this way means that instantiating the EarthDiffuse class will result in a Bitmap object populated with the image data contained in the earth_diffuse.jpg file.
[Embed(source = "earth_diffuse.jpg")]protected var EarthDiffuse:Class;
A number of additional images have been embedded in the same way.
[Embed(source = "earth_normal.jpg")]protected var EarthNormal:Class;
[Embed(source = "earth_specular.jpg")]protected var EarthSpecular:Class;
[Embed(source = "checkerboard.jpg")]protected var Checkerboard:Class;
[Embed(source = "bricks.jpg")]protected var Bricks:Class;
[Embed(source = "marble.jpg")]protected var Marble:Class;
[Embed(source = "water.jpg")]protected var Water:Class;
[Embed(source = "waternormal.jpg")]protected var WaterNormal:Class;
[Embed(source = "spheremap.gif")]protected var SphereMap:Class;
[Embed(source = "skyleft.jpg")]protected var Skyleft:Class;
[Embed(source = "skyfront.jpg")]protected var Skyfront:Class;
[Embed(source = "skyright.jpg")]protected var Skyright:Class;
[Embed(source = "skyback.jpg")]protected var Skyback:Class;
[Embed(source = "skyup.jpg")]protected var Skyup:Class;
[Embed(source = "skydown.jpg")]protected var Skydown:Class;
Here we are embedding three SWF files. These are embedded just like the preceding images.
[Embed(source = "Butterfly.swf")]protected var Butterfly:Class;
[Embed(source = "InteractiveTexture.swf")]private var InteractiveTexture:Class;
[Embed(source = "Bear.swf")]private var Bear:Class;
A TextField object is used to display the name of the current material on the screen.
protected var materialText:TextField;
The currentPrimitive property is used to reference the primitive to which we will apply the various materials.
protected var currentPrimitive:Mesh;
The directionalLight and pointLight properties each reference a light that is added to the scene to illuminate certain materials.
protected var directionalLight:DirectionalLight3D;
protected var pointLight:PointLight3D;
The bounce property is set to true when we want the sphere to bounce along the Z-axis. This bouncing motion will be used to show off the effect of the DepthBitmapMaterial class.
protected var bounce:Boolean;
The frameCount property maintains a count of the frames that have been rendered while bounce property is set to true.
protected var frameCount:int;
The constructor calls the Away3DTemplate constructor, which will initialize the Away3D engine.
public function MaterialsDemo()
The removePrimitive() function removes the current primitive 3D object from the scene, in preparation for a new primitive to be created.
protected function removePrimitive():void
if (currentPrimitive != null)
currentPrimitive = null;
The initSphere() function first removes the existing primitive from the scene by calling the removePrimitive() function, and then creates a new sphere primitive and adds it to the scene. Optionally, it can set the bounce property to true, which indicates that the primitive should bounce along the Z-axis.
protected function initSphere(bounce:Boolean = false):void
currentPrimitive = new Sphere();
this.bounce = bounce;
The initTorus(), initCube(), and initPlane() functions all work like the initSphere() function to add a specific type of primitive to the scene. These functions all set the bounce property to false, as none of the materials that will be applied to these primitives gain anything by having the primitive bounce within the scene.
protected function initTorus():void
currentPrimitive = new Torus();
this.bounce = false;
protected function initCube():void
currentPrimitive = new Cube(
this.bounce = false;
protected function initPlane():void
currentPrimitive = new Plane(
this.bounce = false;
The removeLights() function will remove any lights that have been added to the scene in preparation for a new light to be created.
protected function removeLights():void
if (directionalLight != null)
directionalLight = null;
if (pointLight != null)
pointLight = null;
The initPointLight() and initDirectionalLight() functions each remove any existing lights in the scene by calling the removeLights() function, and then add their specific type of light to the scene.
protected function initPointLight():void
pointLight = new PointLight3D(
protected function initDirectionalLight():void
directionalLight = new DirectionalLight3D(
The direction that the light is pointing is set to (0, 0, 0) by default, which effectively means the light is not pointing anywhere. If you have a directional light that is not being reflected off the surface of a lit material, leaving the direction property to this default value may be the cause. Here we override the default to make the light point back to the origin.
direction: new Vector3D(-1, -1, 0)
The initScene() function has been overridden to call the applyWireColorMaterial() function, which will display a sphere with the WireColorMaterial material applied to it. We also set the position of the camera back to the origin.
protected override function initScene():void
this.camera.z = 0;