-->

Back again after a break longer than predicted! In this tutorial I’ll show how we can add more stunning visual effects to objects by applying normal maps (to give the impression that an object is composed of many more triangles from the rendered shading) and environment maps (for objects to appear shiny and reflecting the surrounding environment). An equivalent tutorial for Papervision3D can be found at my post on texture mapping with lighting, bump mapping and environment mapping.

Previous articles summary :

Normal maps - or Dot3 bump maps - can be seen as something of an evolution from standard bump maps. Bump maps, as used in Papervision3D, contain a single value describing a displacement perpendicular to a surface. Normal maps, however, are more detailed because they contain three axis elements, effectively describing the normal to a specific point on a surface, replacing the triangle normal entirely. Whereas a bump map would be rendered the same for any point of view, a normal map is rendered differently depending on the viewer’s position: hence giving a much better impression of realism.

Normal maps are mapped in much the same way as a texture map, using the same uv values as for a texture. The bitmap data used in a normal map is split into the three components red, green and blue, each one giving a representation of the normal vector components in x, y and z respectively. So, normal values between -1 and 1 are discretised to an integer value between 0 and 255.

Normal maps greatly increase the performance of rendering a 3D object since the normals are given directly rather than requiring a calculation from triangles. So, a complex model requiring thousands of triangles can be reduced to, say, a few hundred (a low-poly model) with a normal map being derived from the original model. You can check out wikipedia for more information on normal mapping. For an example of a low-poly model using a normal map generated from a more complex one, check out this demo of normal mapping at Away3D.

Environment mapping, or reflection mapping, is a technique used to map a surrounding environment onto an object giving the impression that the object has a mirrored surface. The technique is much more efficient than ray tracing and, even though it is not exact, still produces realistic effects. Again, wikipedia contains some useful information on environment mapping.

In this tutorial I’m going to show normal mapping on a cube and a sphere and environment mapping on a sphere. Following from the last post on Lighting and Shading, we’re going to be using another couple of CompositeMaterials: namely the Dot3BitmapMaterial and the EnviroBitmapMaterial.

It should be noted that while normal mapping can produce a more realistic effect than bump mapping, it can be more complex to implement. Since a normal map replaces the normal to a surface we cannot use the same map on an object that has many faces (unless the normal map has been created specifically for example as shown in the Away3D demo above). This is a problem for example for Primitives. A Cube for example has six faces with six discrete principal normals - if we use the same normal map for each surface then each face will have the same normal data and hence be rendered identically, rather than each face being independent. Currently we therefore have to provide six independent normal maps.

My method for this example however is to create a cube using six individual planes using the same normal map and rotating and translating them: the normal map data is then rendered correctly for each face.

I also had a problem retrieving normal maps from the web for use in this tutorial. To help me I created a small utility that takes bump (displacement) map data and converts this into normal map data for either a plane or a sphere. You can try this out yourselves by checking out my article on displacement map to normal map conversion. With this utility you are able to specify the direction of the displacement (for a plane) or the direction against which the angle phi is calculated for a sphere.

So, lets get on with the code! Below you’ll see the next example in this series, producing three rotating texture-mapped objects each one rendered with either an environment map or normal map.

package {   import away3d.cameras.HoverCamera3D;   import away3d.containers.ObjectContainer3D;   import away3d.containers.Scene3D;   import away3d.containers.View3D;   import away3d.core.render.Renderer;   import away3d.core.utils.Cast;   import away3d.lights.DirectionalLight3D;   import away3d.materials.Dot3BitmapMaterial;   import away3d.materials.EnviroBitmapMaterial;   import away3d.primitives.Plane;   import away3d.primitives.Sphere;     import flash.display.Bitmap;   import flash.display.BitmapData;   import flash.display.Sprite;   import flash.display.StageAlign;   import flash.display.StageScaleMode;   import flash.events.Event;   import flash.events.MouseEvent;     [SWF(backgroundColor="#000000")]     public class Example006 extends Sprite {     [Embed(source="/../assets/away3D.png")] private var Away3DImage:Class;     private var away3DBitmap:Bitmap = new Away3DImage();     [Embed(source="/../assets/normalMap.png")] private var NormalImage:Class;     private var normalBitmap:Bitmap = new NormalImage();     [Embed(source="/../assets/asteroidNormal.png")] private var SphereNormalImage:Class;     private var sphereNormalBitmap:Bitmap = new SphereNormalImage();     [Embed(source="/../assets/checker.jpg")] private var CheckerImage:Class;     private var checkerBitmap:Bitmap = new CheckerImage();     [Embed(source="/../assets/envMap.png")] private var EnvironmentImage:Class;     private var envBitmap:Bitmap = new EnvironmentImage();     private static const ORBITAL_RADIUS:Number = 150;     private static const CAMERA_ORBIT:Number = 600;     private var scene:Scene3D;     private var camera:HoverCamera3D;     private var view:View3D;       private var planeGroup:ObjectContainer3D;     private var normalSphere:Sphere;     private var envSphere:Sphere;     private var doRotation:Boolean = false;     private var lastMouseX:int;     private var lastMouseY:int;     private var lastPanAngle:Number = 60;     private var lastTiltAngle:Number = -60;         public function Example006() {             // set up the stage       stage.align = StageAlign.TOP_LEFT;       stage.scaleMode = StageScaleMode.NO_SCALE;       // Add resize event listener       stage.addEventListener(Event.RESIZE, onResize);             // Listen to mouse up and down events on the stage       stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);       stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);       // Initialise Away3D       init3D();             // Create the 3D objects       createScene();             // Initialise frame-enter loop       this.addEventListener(Event.ENTER_FRAME, loop);     }     /**     * Initialise all 3D components.     */     private function init3D():void {       // Create a new scene where all the 3D object will be rendered       scene = new Scene3D();             // Create a new camera, passing some initialisation parameters       camera = new HoverCamera3D({zoom:25, focus:30, distance:600});       camera.targetpanangle = camera.panangle = -10;       camera.targettiltangle = camera.tiltangle = 20;       camera.yfactor = 1;             // Create a new view that encapsulates the scene and the camera       view = new View3D({scene:scene, camera:camera});       // center the view to the middle of the stage       view.x = stage.stageWidth / 2;       view.y = stage.stageHeight / 2;             // ensure that the z-order is calculated correctly       view.renderer = Renderer.CORRECT_Z_ORDER;             addChild(view);     }     /**     * Create the objects and lighting of the scene     */     private function createScene():void {       // Create 3 different materials: two normal mapped ones (planar and spherical) and an       // environment mapped one.       var normalMapMaterial:Dot3BitmapMaterial = new Dot3BitmapMaterial(Cast.bitmap(away3DBitmap), Cast.bitmap(normalBitmap), {smooth:true, precision:5});       var envMapMaterial:EnviroBitmapMaterial = new EnviroBitmapMaterial(Cast.bitmap(checkerBitmap), Cast.bitmap(envBitmap), {smooth:true, precision:5});       var sphereNormalMapMaterial:Dot3BitmapMaterial = new Dot3BitmapMaterial(new BitmapData(1, 1, false, 0x666666), Cast.bitmap(sphereNormalBitmap), {smooth:true, precision:5});       // create a new directional white light source with specific ambient, diffuse and specular parameters       var light:DirectionalLight3D = new DirectionalLight3D({color:0xFFFFFF, ambient:0.25, diffuse:0.75, specular:0.9});       light.x = 10000;       light.z = 50000;       light.y = 50000;       scene.addChild(light);       // Create six with the same (normal mapped) material and position them them to create a cube       var topPlane:Plane = new Plane({material:normalMapMaterial, width:100, height:100, segmentsW:2, segmentsH:2, ownCanvas:true});       topPlane.y = 50;       var leftPlane:Plane = new Plane({material:normalMapMaterial, width:100, height:100, segmentsW:2, segmentsH:2, ownCanvas:true});       leftPlane.rotationZ = 90;       leftPlane.x = -50;       var frontPlane:Plane = new Plane({material:normalMapMaterial, width:100, height:100, segmentsW:2, segmentsH:2, ownCanvas:true});       frontPlane.rotationX = 90;       frontPlane.z = -50;       var bottomPlane:Plane = new Plane({material:normalMapMaterial, width:100, height:100, segmentsW:2, segmentsH:2, ownCanvas:true});       bottomPlane.rotationX = 180;       bottomPlane.y = -50;       var rightPlane:Plane = new Plane({material:normalMapMaterial, width:100, height:100, segmentsW:2, segmentsH:2, ownCanvas:true});       rightPlane.rotationZ = -90;       rightPlane.x = 50;       var backPlane:Plane = new Plane({material:normalMapMaterial, width:100, height:100, segmentsW:2, segmentsH:2, ownCanvas:true});       backPlane.rotationX = -90;       backPlane.z = 50;             // Create an object container to group the sides of the cube       planeGroup = new ObjectContainer3D();       scene.addChild(planeGroup);       planeGroup.addChild(topPlane);       planeGroup.addChild(leftPlane);       planeGroup.addChild(frontPlane);       planeGroup.addChild(bottomPlane);       planeGroup.addChild(rightPlane);       planeGroup.addChild(backPlane);       planeGroup.x = -100;       planeGroup.z = 100;       // Create a sphere with normal-mapped material       normalSphere = new Sphere({material:sphereNormalMapMaterial, radius:65, segmentsW:10, segmentsH:10, ownCanvas:true});       normalSphere.x = 100;       normalSphere.z = 100;       scene.addChild(normalSphere);       // Create a sphere with environment-mapped material       envSphere = new Sphere({material:envMapMaterial, radius:65, segmentsW:10, segmentsH:10, ownCanvas:true});       envSphere.z = -90;       scene.addChild(envSphere);     }         /**     * Frame-enter event handler     */     private function loop(event:Event):void {             // rotate the objects       planeGroup.rotationY += 2;       normalSphere.rotationY += 2;       envSphere.rotationY += 2;       // update camera position       updateCamera();       camera.hover();       // Render the 3D scene       view.render();     }     /**     * Update the camera position from mouse movements     */     private function updateCamera():void {       if (doRotation) {         camera.targetpanangle = 0.5 * (stage.mouseX - lastMouseX) + lastPanAngle;         camera.targettiltangle = 0.5 * (stage.mouseY - lastMouseY) + lastTiltAngle;       }     }         /**     * Mouse down listener for camera rotation     */     private function onMouseDown(event:MouseEvent):void {       lastPanAngle = camera.targetpanangle;       lastTiltAngle = camera.targettiltangle;       lastMouseX = stage.mouseX;       lastMouseY = stage.mouseY;       doRotation = true;       stage.addEventListener(Event.MOUSE_LEAVE, onStageMouseLeave);     }         /**     * Mouse up listener for camera rotation     */     private function onMouseUp(event:MouseEvent):void {       doRotation = false;       stage.removeEventListener(Event.MOUSE_LEAVE, onStageMouseLeave);     }     /**     * Mouse stage leave listener for camera rotation     */     private function onStageMouseLeave(event:Event):void {       doRotation = false;       stage.removeEventListener(Event.MOUSE_LEAVE, onStageMouseLeave);     }         /**     * Resize the scene when the stage resizes     */      private function onResize(event:Event):void {       view.x = stage.stageWidth / 2;       view.y = stage.stageHeight / 2;     }   }     }

If you want to use the same images as in this example then away3D.png and checker.jpg you’ll find in the previous post on texture mapping in Away3D. The new images are shown below.

The first two images (the normal maps) were created using the utility described above. The first one from a bump map I used for a Papervision3D tutorial on bump mapping, the second one comes from a bump map I found on the internet at gamedev.net. The environment map comes from and old OpenGL example I discovered on my hard disk… if you search for “opengl sphere map” in Google you’ll find plenty of instances!

Once its all compiled you should see three rotating objects with either normal and environment mapped data. Click on the image below to see the Flash movie. You can click anywhere and move the mouse to rotate the scene.

So, lets go into more detail on each part of the code. There’s nothing too complicated here but a few changes from the last time…

The first change comes from the initialisation of the 3D basics of the scene, specifically with the camera.

    private function init3D():void {       // Create a new scene where all the 3D object will be rendered       scene = new Scene3D();             // Create a new camera, passing some initialisation parameters       camera = new HoverCamera3D({zoom:25, focus:30, distance:600});       camera.targetpanangle = camera.panangle = -10;       camera.targettiltangle = camera.tiltangle = 20;       camera.yfactor = 1;             // Create a new view that encapsulates the scene and the camera       view = new View3D({scene:scene, camera:camera});       // center the view to the middle of the stage       view.x = stage.stageWidth / 2;       view.y = stage.stageHeight / 2;             // ensure that the z-order is calculated correctly       view.renderer = Renderer.CORRECT_Z_ORDER;             addChild(view);     }

I’ve now chosen to use a HoverCamera3D. This simplifies the movement and positioning of the camera. You’ll remember that previously I used the basic Camera3D class and had to calculate specific values of x, y and z as well as redirect the camera towards the origin. Much of this is included in the HoverCamera3D class. By specifying targettiltangle and targetpanangle the camera will calculate a trajectory necessary to move the camera to a new position and keep it focused on a particular target. To maintain the orbital radius of the camera we set the yfactor to 1 otherwise a more elliptical orbit is calculated.

You’ll see towards the end of the code how we perform an interaction with the camera. If we look at the loop function, called on every frame-enter event we can see how the camera position is updated.

    private function loop(event:Event):void {             // rotate the objects       planeGroup.rotationY += 2;       normalSphere.rotationY += 2;       envSphere.rotationY += 2;       // update camera position       updateCamera();       camera.hover();       // Render the 3D scene       view.render();     }

The object rotation is self evident so we’ll ignore that. The updateCamera function recalculates the camera position from mouse position. More importantly the camera.hover() call makes the camera move gradually towards the target tilt and pan angles giving a smoother animation of the camera than in previous examples.

For completeness, the updateCamera function and the mouseDown event listener are shown below.

    private function updateCamera():void {       if (doRotation) {         camera.targetpanangle = 0.5 * (stage.mouseX - lastMouseX) + lastPanAngle;         camera.targettiltangle = 0.5 * (stage.mouseY - lastMouseY) + lastTiltAngle;       }     }         private function onMouseDown(event:MouseEvent):void {       lastPanAngle = camera.targetpanangle;       lastTiltAngle = camera.targettiltangle;       lastMouseX = stage.mouseX;       lastMouseY = stage.mouseY;       doRotation = true;       stage.addEventListener(Event.MOUSE_LEAVE, onStageMouseLeave);     }

As you can see, the target angles are updated in the updateCamera function, taking into account the current mouse position. The onMouseDown function performs the necessary initialisation of pan and tilt angles. It also adds an event listener to detect when the mouse leaves the stage: this is useful because if the mouse button is released outside the stage, the onMouseUp listener is not called and we can obtain a state in which the scene is continuously rotated even if the mouse button is up.

So, on to more interesting elements of the code: normal mapped and environment mapped materials!

All the interesting stuff happens in createScene where the materials and objects are created.

    private function createScene():void {       // Create 3 different materials: two normal mapped ones (planar and spherical) and an       // environment mapped one.       var normalMapMaterial:Dot3BitmapMaterial = new Dot3BitmapMaterial(Cast.bitmap(away3DBitmap), Cast.bitmap(normalBitmap), {smooth:true, precision:5});       var envMapMaterial:EnviroBitmapMaterial = new EnviroBitmapMaterial(Cast.bitmap(checkerBitmap), Cast.bitmap(envBitmap), {smooth:true, precision:5});       var sphereNormalMapMaterial:Dot3BitmapMaterial = new Dot3BitmapMaterial(new BitmapData(1, 1, false, 0x666666), Cast.bitmap(sphereNormalBitmap), {smooth:true, precision:5});       // create a new directional white light source with specific ambient, diffuse and specular parameters       var light:DirectionalLight3D = new DirectionalLight3D({color:0xFFFFFF, ambient:0.25, diffuse:0.75, specular:0.9});       light.x = 10000;       light.z = 50000;       light.y = 50000;       scene.addChild(light);       // Create six with the same (normal mapped) material and position them them to create a cube       var topPlane:Plane = new Plane({material:normalMapMaterial, width:100, height:100, segmentsW:2, segmentsH:2, ownCanvas:true});       topPlane.y = 50;       var leftPlane:Plane = new Plane({material:normalMapMaterial, width:100, height:100, segmentsW:2, segmentsH:2, ownCanvas:true});       leftPlane.rotationZ = 90;       leftPlane.x = -50;       var frontPlane:Plane = new Plane({material:normalMapMaterial, width:100, height:100, segmentsW:2, segmentsH:2, ownCanvas:true});       frontPlane.rotationX = 90;       frontPlane.z = -50;       var bottomPlane:Plane = new Plane({material:normalMapMaterial, width:100, height:100, segmentsW:2, segmentsH:2, ownCanvas:true});       bottomPlane.rotationX = 180;       bottomPlane.y = -50;       var rightPlane:Plane = new Plane({material:normalMapMaterial, width:100, height:100, segmentsW:2, segmentsH:2, ownCanvas:true});       rightPlane.rotationZ = -90;       rightPlane.x = 50;       var backPlane:Plane = new Plane({material:normalMapMaterial, width:100, height:100, segmentsW:2, segmentsH:2, ownCanvas:true});       backPlane.rotationX = -90;       backPlane.z = 50;             // Create an object container to group the sides of the cube       planeGroup = new ObjectContainer3D();       scene.addChild(planeGroup);       planeGroup.addChild(topPlane);       planeGroup.addChild(leftPlane);       planeGroup.addChild(frontPlane);       planeGroup.addChild(bottomPlane);       planeGroup.addChild(rightPlane);       planeGroup.addChild(backPlane);       planeGroup.x = -100;       planeGroup.z = 100;       // Create a sphere with normal-mapped material       normalSphere = new Sphere({material:sphereNormalMapMaterial, radius:65, segmentsW:10, segmentsH:10, ownCanvas:true});       normalSphere.x = 100;       normalSphere.z = 100;       scene.addChild(normalSphere);       // Create a sphere with environment-mapped material       envSphere = new Sphere({material:envMapMaterial, radius:65, segmentsW:10, segmentsH:10, ownCanvas:true});       envSphere.z = -90;       scene.addChild(envSphere);     }

To start off with the three different materials are created. Firstly a Dot3BitmapMaterial taking the Away3D texture and a random normal map. I’ve chosen to smooth the material and have increased the precision of rendering to 5 pixels. The second material uses the EnviroBitmapMaterial using the checkered texture and the OpenGl sphere-map for the environment data, again smoothed as before. Incidentally, the shininess of the material can be modified by specifying the reflectiveness parameter in the initialisation parameters, taking a value between 0 and 1 (for most reflective). The final Dot3BitmapMaterial material, rather than using a bitmap file, creates a plain grey bitmap. The normal map comes from the asteroid spherical normal map.

We add a DirectionalLight3D to the scene to provide uniform lighting, independent of light distance. It should be noted that the Dot3BitmapMaterial will not currently work with a PointLight3D. Also, the EnviroBitmapMaterial is independent of the source: the environment map provides the equivalent shading.

For the cube we create six Planes, each using the same material. These are then rotated and translated to form a cube. They are finally added to a ObjectContainer3D so that we can rotate all of them together in the loop function. Note as well that, as before, each object needs to have its own canvas which is specified within the initialisation parameters.

The two spheres are then created: one using an environment map, the other with normal mapped data. These are then positioned and added to the scene, again with independent canvases.

And that’s all there is to it! As you’ll see, the normal map produces fantastic results and really adds a feeling of depth to the object surface. The environment map really gives the impression that the object is very shiny. These effects are very simple to produce in Away3D but be aware too that added realism comes at a cost - the frame rate is bound to be slower than without these effects - so be warned!

Anyway, I hope this has been a useful addition to basic lighting and shading. As always, let me know if you have any comments or questions!

Next article:

Previous articles summary :

This article is essentially a continuation of the previous one, building on the texture mapped materials. As I said in my last post, the aim now is to add lighting to add more realism and provide depth to the scene - much like we did in Part 4.

Previously for the shading we used specific materials to account for the different shading methods such as GouraudMaterial or PhongMaterial (as you will find in the package org.papervision3d.materials.shadematerials of the pv3d source). These however are based on coloured materials rather than textured materials.

To mix both texture maps and shading we need to create a Shader and add this with the BitmapMaterial (like we created in Part 6) together in a ShadedMaterial. Papervision3D does all the magic necessary to mix the two to create a shaded bitmap material.

As you can see if you look in the org.papervision3d.materials.shaders package in the pv3d source there are a number of different shaders available - some of which we recognise from Part 4. The list includes FlatShader, GouraudShader, PhongShader, CellShader and EnvMapShader.

The last one of the above allows us to add an environment map to the material which essentially is like adding lighting from a surrounding environment and makes the material look like it is highly reflective. Take a look at this wikipedia page on reflection mapping for more details.

Another concept that is important in producing more realistic scenes is bump mapping. This is basically an optimised way of modifying the lighting of a surface so that it appears as if the surface has bumps in it. It can make a big difference to a rendered object in terms of realism without having to specify many object vertices. Again, wikipedia has lots of information on bump mapping. With Papervision3D we can add bump maps to enhance both the Phong and environment shaders.

So, on with the code… As usual, I’m modifying the code from the previous article so there’s not a huge change. However to account for the different number of shaders, rather than show all of them at once I’m only showing one sphere at a time so that you get a better idea of what each shader does. To change the shader I’ve modified the scene interaction so that clicking on the sphere changes the material dynamically - another great feature of Papervision3D! Anyway here’s the code:

package {     import flash.display.Bitmap;   import flash.display.StageAlign;   import flash.display.StageScaleMode;   import flash.events.Event;   import flash.events.MouseEvent;   import flash.text.TextField;   import flash.text.TextFieldAutoSize;   import flash.text.TextFormat;     import org.papervision3d.events.InteractiveScene3DEvent;   import org.papervision3d.lights.PointLight3D;   import org.papervision3d.materials.BitmapMaterial;   import org.papervision3d.materials.shaders.CellShader;   import org.papervision3d.materials.shaders.EnvMapShader;   import org.papervision3d.materials.shaders.FlatShader;   import org.papervision3d.materials.shaders.GouraudShader;   import org.papervision3d.materials.shaders.PhongShader;   import org.papervision3d.materials.shaders.ShadedMaterial;   import org.papervision3d.materials.shaders.Shader;   import org.papervision3d.objects.DisplayObject3D;   import org.papervision3d.objects.primitives.Sphere;   import org.papervision3d.view.BasicView;   public class Example007 extends BasicView {       [Embed(source="/../assets/pv3d.png")] private var Pv3dBitmapImage:Class;     [Embed(source="/../assets/randomBump.png")] private var BumpImage:Class;     [Embed(source="/../assets/mountains.png")] private var EnvImage:Class;     private var pv3dBitmap:Bitmap = new Pv3dBitmapImage();     private var bumpMap:Bitmap = new BumpImage();     private var envMap:Bitmap = new EnvImage();     private var bitmapMaterial:BitmapMaterial;         private var sphere:Sphere;     private var light:PointLight3D;         private var doRotation:Boolean = false;     private var lastMouseX:int;     private var lastMouseY:int;     private var cameraPitch:Number = 60;     private var cameraYaw:Number = -60;         private var shaders:Array = ["flat", "cell", "gouraud", "phong", "phongBump", "env", "envBump"];     private var shaderIndex:int = 0;         private var shaderText:TextField;     private var textFormat:TextFormat;     public function Example007() {       super(0, 0, true, true);             // set up the stage       stage.align = StageAlign.TOP_LEFT;       stage.scaleMode = StageScaleMode.NO_SCALE;       // Initialise Papervision3D       init3D();             // Create the 3D objects       createScene();       // Listen to mouse up and down events on the stage       stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);       stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);       stage.addChild(shaderText);       // Start rendering the scene       startRendering();     }         private function init3D():void {       // position the camera       camera.z = -500;       camera.orbit(60, -60);     }     private function createScene():void {       // create text and format to display current shader type       textFormat = new TextFormat();       textFormat.size = 20;       textFormat.font = "Arial";             shaderText = new TextField();       shaderText.x = 50;       shaderText.y = 50;       shaderText.textColor = 0xFFFFFF;       shaderText.text = "flat";       shaderText.setTextFormat(textFormat);       shaderText.autoSize = TextFieldAutoSize.LEFT;       // Specify a point light source and its location       light = new PointLight3D(true);       light.x = 500;       light.y = 500;       light.z = -200;       // create bitmap material with smoothing       bitmapMaterial = new BitmapMaterial(pv3dBitmap.bitmapData, false);       bitmapMaterial.smooth = true;         // create sphere       sphere = new Sphere(getShadedBitmapMaterial(bitmapMaterial, "flat"), 150, 20, 20);       // Add a listener to the spheres to listen to InteractiveScene3DEvent events       sphere.addEventListener(InteractiveScene3DEvent.OBJECT_CLICK, onMouseDownOnObject);       // Add the light and sphere to the scene       scene.addChild(sphere);       scene.addChild(light);     }         private function getShadedBitmapMaterial(bitmapMaterial:BitmapMaterial, shaderType:String):ShadedMaterial {       var shader:Shader;             if (shaderType == "flat") {         // create new flat shader         shader = new FlatShader(light, 0xFFFFFF, 0x333333);       } else if (shaderType == "cell") {         // create new cell shader with 5 colour levels         shader = new CellShader(light, 0xFFFFFF, 0x333333, 5);       } else if (shaderType == "gouraud") {         // create new gouraud shader         shader = new GouraudShader(light, 0xFFFFFF, 0x333333);       } else if (shaderType == "phong") {         // create new phong shader         shader = new PhongShader(light, 0xFFFFFF, 0x333333, 50);       } else if (shaderType == "phongBump") {         // create new phong shader with bump map         shader = new PhongShader(light, 0xFFFFFF, 0x333333, 50, bumpMap.bitmapData);       } else if (shaderType == "env") {         // create new environment map shader         shader = new EnvMapShader(light, envMap.bitmapData, envMap.bitmapData, 0x333333);       } else if (shaderType == "envBump") {         // create new environment map shader with bump map         shader = new EnvMapShader(light, envMap.bitmapData, envMap.bitmapData, 0x333333, bumpMap.bitmapData);       }       // create new shaded material by combining the bitmap material with shader       var shadedMaterial:ShadedMaterial = new ShadedMaterial(bitmapMaterial, shader);       shadedMaterial.interactive = true;             return shadedMaterial;     }         override protected function onRenderTick(event:Event=null):void {       // rotate the sphere       sphere.yaw(-1);             // If the mouse button has been clicked then update the camera position            if (doRotation) {                 // convert the change in mouse position into a change in camera angle         var dPitch:Number = (mouseY - lastMouseY) / 2;         var dYaw:Number = (mouseX - lastMouseX) / 2;                 // update the camera angles         cameraPitch -= dPitch;         cameraYaw -= dYaw;         // limit the pitch of the camera         if (cameraPitch <= 0) {           cameraPitch = 0.1;         } else if (cameraPitch >= 180) {           cameraPitch = 179.9;         }               // reset the last mouse position         lastMouseX = mouseX;         lastMouseY = mouseY;                 // reposition the camera         camera.orbit(cameraPitch, cameraYaw);       }             // call the renderer       super.onRenderTick(event);     }     // called when mouse down on stage     private function onMouseDown(event:MouseEvent):void {       doRotation = true;       lastMouseX = event.stageX;       lastMouseY = event.stageY;     }     // called when mouse up on stage     private function onMouseUp(event:MouseEvent):void {       doRotation = false;     }         // called when mouse down on a sphere     private function onMouseDownOnObject(event:InteractiveScene3DEvent):void {       var object:DisplayObject3D = event.displayObject3D;             // calculate index of next shader       shaderIndex++;       if (shaderIndex == shaders.length) {         shaderIndex = 0;       }             // dynamically modify the material of the object and update text       object.material = getShadedBitmapMaterial(bitmapMaterial, shaders[shaderIndex]);       shaderText.text = shaders[shaderIndex];       shaderText.setTextFormat(textFormat);     }   } }

Click on the image below to see this in action. Clicking on the sphere will change the material - in this case change the shader associated with the textured material - and moving the mouse while clicking anywhere in the window will rotate the camera around the sphere.

Compared to the previous posts the construction, initialisation of 3D and scene renderer remain virually the same so I won’t go into details here. You’ll see that, as with the texture map, the bump map and environment map are embedded in the flash animation and come from image files. These are then converted into Bitmap objects.

    [Embed(source="/../assets/pv3d.png")] private var Pv3dBitmapImage:Class;     [Embed(source="/../assets/randomBump.png")] private var BumpImage:Class;     [Embed(source="/../assets/mountains.png")] private var EnvImage:Class;     private var pv3dBitmap:Bitmap = new Pv3dBitmapImage();     private var bumpMap:Bitmap = new BumpImage();     private var envMap:Bitmap = new EnvImage();

For sake of completeness, the images I use are below. The pv3d symbol comes directly from the papervision3d developer site, the bump map I made myself just by adding random noise to a blank image and blurring it (thanks to Gimp) and the environment image comes from my neighbourhood Alps.

Texture map image:

Bump map:

Environment map:

Remark: Actually I had to invert the environment map image for this demo to have it appear the right way up when rendered. If anyone can tell me why I’d be happy to know…

The main modification to the code compared to the previous post occurs in the createScene method. Firstly a simple Text object is created to display the current shader type - nothing really interesting here!

      // create text and format to display current shader type       textFormat = new TextFormat();       textFormat.size = 20;       textFormat.font = "Arial";             shaderText = new TextField();       shaderText.x = 50;       shaderText.y = 50;       shaderText.textColor = 0xFFFFFF;       shaderText.text = "flat";       shaderText.setTextFormat(textFormat);       shaderText.autoSize = TextFieldAutoSize.LEFT;

We then put the light back in that we removed in the last article - each shader is associated with a single light source.

      // Specify a point light source and its location       light = new PointLight3D(true);       light.x = 500;       light.y = 500;       light.z = -200;

As with Part 6, we create a BitmapMaterial using the texture map data. As before I’m using normal rather than accurate perspective (the second parameter of the constructor set to false) to improve the performance. I am however choosing to smooth the material which produces a much nicer looking texture map. This is of course at the cost of performance…

      // create bitmap material with smoothing       bitmapMaterial = new BitmapMaterial(pv3dBitmap.bitmapData, false);       bitmapMaterial.smooth = true;

The sphere is then created using a call to getShadedBitmapMaterial to enhance the bitmap material with a shader - to start off with, just a simple flat shader - and an event listener added to allow us to change the material.

      // create sphere       sphere = new Sphere(getShadedBitmapMaterial(bitmapMaterial, "flat"), 150, 20, 20);       // Add a listener to the spheres to listen to InteractiveScene3DEvent events       sphere.addEventListener(InteractiveScene3DEvent.OBJECT_CLICK, onMouseDownOnObject);

So how do we add a shader to a BitmapMaterial? The different types of shaders used in this demo are created in the getShadedBitmapMaterial method

    private function getShadedBitmapMaterial(bitmapMaterial:BitmapMaterial, shaderType:String):ShadedMaterial {       var shader:Shader;             if (shaderType == "flat") {         // create new flat shader         shader = new FlatShader(light, 0xFFFFFF, 0x333333);       } else if (shaderType == "cell") {         // create new cell shader with 5 colour levels         shader = new CellShader(light, 0xFFFFFF, 0x333333, 5);       } else if (shaderType == "gouraud") {         // create new gouraud shader         shader = new GouraudShader(light, 0xFFFFFF, 0x333333);       } else if (shaderType == "phong") {         // create new phong shader         shader = new PhongShader(light, 0xFFFFFF, 0x333333, 50);       } else if (shaderType == "phongBump") {         // create new phong shader with bump map         shader = new PhongShader(light, 0xFFFFFF, 0x333333, 50, bumpMap.bitmapData);       } else if (shaderType == "env") {         // create new environment map shader         shader = new EnvMapShader(light, envMap.bitmapData, envMap.bitmapData, 0x333333);       } else if (shaderType == "envBump") {         // create new environment map shader with bump map         shader = new EnvMapShader(light, envMap.bitmapData, envMap.bitmapData, 0x333333, bumpMap.bitmapData);       }       // create new shaded material by combining the bitmap material with shader       var shadedMaterial:ShadedMaterial = new ShadedMaterial(bitmapMaterial, shader);       shadedMaterial.interactive = true;             return shadedMaterial;     }

As you can see, the simple shaders (”flat”, “gouraud”, “cell” and “phong”) are created exactly the same as for the ShadeMaterials we introduced in Part 4 using essentially a light colour and an ambient colour. The new types of shaders here come from using the bump map and environment map.

The PhongShader allows us to add bump map data. This is added very simply by using the bump map image data we loaded before. As you can see with the demo this changes a lot the appearance of the sphere - we really get the impression that it has a bumpy surface.

The EnvMapShader is also very simple to use. It takes a light source as for all the other shaders then two texture maps for the environment - a front and back. Here for simplicity I just used the same image for both. It then takes an ambient colour for when it is not facing the light source. To add a bump map we simple add as well the bitmap data of the bump map image.

Finally, what we create is a ShadedMaterial: this simply takes the material texture map and the shader and combines the two.

The only other major difference from previous articles is the dynamic changing of object materials. This is performed when a mouse click event is detected on the sphere.

    // called when mouse down on a sphere     private function onMouseDownOnObject(event:InteractiveScene3DEvent):void {       var object:DisplayObject3D = event.displayObject3D;             // calculate index of next shader       shaderIndex++;       if (shaderIndex == shaders.length) {         shaderIndex = 0;       }             // dynamically modify the material of the object and update text       object.material = getShadedBitmapMaterial(bitmapMaterial, shaders[shaderIndex]);       shaderText.text = shaders[shaderIndex];       shaderText.setTextFormat(textFormat);     }

Dynamically changing the material of an object is a new feature in Papervision3D and is very simple to implement: simply change the value of the material member of the object! In the above method we just cycle through the array of shader types and update the text associated… almost too easy!

And that’s all there is too it! I’ve not really gone into the mechanics behind bump mapping and environment mapping because I’m sure you can find explanations much better that I could give elsewhere on the web. But hopefully this has given some insight into how just a few simple lines can totally change the appearance of a rendered object. Comments and suggestions, as always, are very welcome!

Next article: