Away3D now includes some advanced materials that make use of Pixel Bender. This article looks at the PhongMultiPassMaterial class.
Away3D has always had a number of materials available, with some of the more advanced ones replicating the multipass materials that have been seen since the time of Quake 2. However these materials have been limited in their quality, and have issues with concave objects. Starting with Flash 10 materials can now be created with Pixel Bender; a technology that allows the Flash runtime to make use of the underlying GPU hardware that is present on almost all PC’s. With the latest release of Away3D 3.4.2 a number of new materials have been included that make use of Pixel Bender to create the sort of effects that are present in modern 3D applications and game. This article will look at one of these materials: PhongMultiPassMaterial.
ResourceManager.as
Unlike most materials, the PhongMultiPassMaterial requires a reference to the mesh that it will be applied to. This means that we need to defer the creation of the material until we have created the mesh. For this we create a new function called startupResourceManagerEx, which will be called by the ApplicationManager once it has created the mesh. [code]
Creating the material itself doesn’t require anything special. You just pass in the base texture, the normal map, the mesh and optionally the specular map and usual init object. [code]
ApplicationManager.as
Here we create a new instance of the Lantern class, which is just a model that has been hard coded into an ActionScript class. [code]
The first mesh contained in the Lantern class is then used in the call to the ResourceManager startupResourceManagerEx function, and then added to the scene, textured by assigning the new PhongMultiPassMaterial object to its material property, positioned, and added to the scene via a MeshObject. Again there is nothing special about this procedure – the PhongMultiPassMaterial is assigned just like any other material. [code]
A new HoverCamera is created. The HoverCamera3D class is covered in this article here. [code]
In order for the PhongMultiPassMaterial to show up it needs to be lit. We add 3 lights to the scene: two point lights and an ambient light to give the model a base light level. [code]
During the enterFrame function the two point lights are circled around the mesh, and the hover camera is updated against the sliders on the main GUI. [code]
The end result is a model that is individually lit at the pixel level by all three lights, complete with bump mapping.
While Pixel Bender is a logical and powerful technology, it does require learning a new language, and the shaders themselves can be quite involved. Thankfully Away3D, through materials like PhongMultiPassMaterial, allows developers to harness the power of Pixel Bender without having to know anything about the underlying technology. Materials and lights are created like normal, and Away3D takes care of the rest.
Go back to Away3D Tutorials
Away3D Programming Tutorials - Animated Textures
See how to use video files as textures in Away3D with this tutorial