-->

Having looked at the different types of texture mapped and coloured materials with different lighting aspects, I want to discuss briefly a couple of more interactive and dynamic materials. These don’t necessarily add realism to a scene but can be very useful for 3D website development to provide a richer user experience. To do this we use the MovieMaterial and VideoMaterial. If you’re interested here is an equivalent tutorial for movie materials in Papervision3D.

Previous articles summary :

This article will be looking at two materials available in Away3D: the MovieMaterial for rendering Flash movies onto a surface and VideoMaterial to show Flash video streams on an object (using .flv files). As well as being able to show another Flash animation, mouse events are mapped to the MovieMaterial allowing it to be interactive, even in a 3D environment.

This article is taking advantage of the very latest types of materials available with Away3D: to be able to compile the examples and develop your own you may need to use the repository (SVN) version of Away3D. If you don’t have away3d.materials.VideoMaterial available in your Away3D source then this is the case. Here’s an article on downloading and installing Away3D from SNV if you need help.

To illustrate these different materials we’re going to create three simple planes rotated and translated to form three sides of a cube. Two of these will be rendered using MovieMaterials and the third with a Flash video stream using a VideoMaterial. One of the MovieMaterial planes will be interactive and I’ll show how a external Flash movie can be embedded in the compiled animation.

So let’s look at the code. For this example we will actually have three ActionScript classes: the main Away3D class and two additional classes to be used for the individual MovieMaterial cube faces. The latter two will be shown at the end of the article just for completeness.

package {   import away3d.cameras.HoverCamera3D;   import away3d.containers.ObjectContainer3D;   import away3d.containers.Scene3D;   import away3d.containers.View3D;   import away3d.core.base.Object3D;   import away3d.core.render.Renderer;   import away3d.events.MouseEvent3D;   import away3d.materials.MovieMaterial;   import away3d.materials.VideoMaterial;   import away3d.primitives.Plane;     import caurina.transitions.Tweener;     import flash.display.Sprite;   import flash.display.StageAlign;   import flash.display.StageScaleMode;   import flash.events.Event;   import flash.filters.BlurFilter;     [SWF(backgroundColor="#222222")]     public class Example007 extends Sprite {     private var videoURL:String = "http://www.tartiflop.com/away3d/FirstSteps/AmIWrong.flv";     [Embed(source="/../assets/DrawTool.swf")]     private var DrawToolEmbedded:Class;     private var scene:Scene3D;     private var camera:HoverCamera3D;     private var view:View3D;     private var planeGroup:ObjectContainer3D;         private var doRotation:Boolean = true;         public function Example007() {             // set up the stage       stage.align = StageAlign.TOP_LEFT;       stage.scaleMode = StageScaleMode.NO_SCALE;       // Add resize event listener       stage.addEventListener(Event.RESIZE, onResize);             // 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:200});       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 of the scene     */     private function createScene():void {       // Video material using a flash streaming video URL       var frontMaterial:VideoMaterial = new VideoMaterial({file:videoURL});             // Movie material from an embedded flash animation       var topMaterial:MovieMaterial = new MovieMaterial(new DrawToolEmbedded(), {lockW:320, lockH:240, interactive:true, smooth:true, precision:5});             // Movie material from another class       var leftMaterial:MovieMaterial = new MovieMaterial(new Pong(), {smooth:true, precision:5});       // Create three planes with different material, blurred by default       // and position them them to create tree sides of a cube       var topPlane:Plane = new Plane({material:topMaterial, width:100, height:100, segmentsW:2, segmentsH:2, ownCanvas:true});       topPlane.rotationY = -180;       topPlane.y = 50;       topPlane.filters.push(new BlurFilter(8, 8));             var leftPlane:Plane = new Plane({material:leftMaterial, width:100, height:100, segmentsW:2, segmentsH:2, ownCanvas:true});       leftPlane.rotationZ = -90;       leftPlane.rotationY = -90;       leftPlane.x = 50;       leftPlane.filters.push(new BlurFilter(8, 8));             var frontPlane:Plane = new Plane({material:frontMaterial, width:100, height:100, segmentsW:2, segmentsH:2, ownCanvas:true});       frontPlane.rotationX = -90;       frontPlane.rotationZ = 180;       frontPlane.z = 50;       frontPlane.filters.push(new BlurFilter(8, 8));             // 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);                   // Add mouse listeners to each plane for mouse down, over and out events       topPlane.addOnMouseDown(onMouseClickOnObject);       leftPlane.addOnMouseDown(onMouseClickOnObject);       frontPlane.addOnMouseDown(onMouseClickOnObject);       topPlane.addOnMouseOver(onMouseOverObject);       leftPlane.addOnMouseOver(onMouseOverObject);       frontPlane.addOnMouseOver(onMouseOverObject);       topPlane.addOnMouseOut(onMouseLeavesObject);       leftPlane.addOnMouseOut(onMouseLeavesObject);       frontPlane.addOnMouseOut(onMouseLeavesObject);           }         /**     * Frame-enter event handler     */     private function loop(event:Event):void {             // update camera position       updateCamera();       camera.hover();             // Render the 3D scene       view.render();     }     /**     * Update the camera position from mouse positions     */     private function updateCamera():void {       if (doRotation) {         camera.targetpanangle = (stage.stageWidth - stage.mouseX) / stage.stageWidth * 90;         camera.targettiltangle = (stage.stageHeight - stage.mouseY) / stage.stageHeight * 70       }     }         /**     * Event listener for mouse click on plane. Makes the camera look     * directly at the plane and move closer to it.     */     private function onMouseClickOnObject(event:MouseEvent3D):void {       var object:Object3D = event.object;             doRotation = false;       // Calculate angles necessary for camera            var theta:Number = Math.atan2(object.x, object.z);       var len:Number = Math.sqrt(object.x*object.x + object.z*object.z);       var phi:Number = Math.atan2(object.y, len);       // rotate camera position       camera.targetpanangle = theta * 180 / Math.PI;       camera.targettiltangle = phi * 180 / Math.PI;       // move camera towards plane       Tweener.addTween(camera, {distance:150, time:0.5, transition:"easeOutSine"});     }        /**     * Event listener for mouse over plane. Removes the blur filter.     */        private function onMouseOverObject(event:MouseEvent3D):void {       var object:Object3D = event.object;             object.filters = new Array();     }            /**     * Event listener for mouse out of plane. Adds blur filter and moves     * camera away from plane if it isn't already.     */     private function onMouseLeavesObject(event:MouseEvent3D):void {       var object:Object3D = event.object;       object.filters.push(new BlurFilter(8, 8));       Tweener.addTween(camera, {distance:200, time:0.5, transition:"easeOutSine"});       doRotation = true;     }     /**     * Resize the scene when the stage resizes     */      private function onResize(event:Event):void {       view.x = stage.stageWidth / 2;       view.y = stage.stageHeight / 2;     }   }   }

This (along with the other ActionScript classes shown below) produces a cube that rotates as the mouse moves. One face shows a Flash video stream (Etienne de Crécy : Am I Wrong), another is an interactive drawable surface and the third a simple Pong simulation. Each face is blurred until the mouse enters it. Clicking on a face moves the camera directly above it and moves towards it. Moving the mouse outside of a face blurs the face again and the cube regains its original size. You can see the finished result by clicking on the image below.

As usual, the code for Example007 follows the same style as shown in the previous articles of this series. Lets look at what has changed.

The constructor and initialisation of Away3D elements is virtually identical to before so not worth looking at here. Lets move onto the scene creation (createScene())and see how we use these new materials. To start off with the materials are created.

      // Video material using a flash streaming video URL       var frontMaterial:VideoMaterial = new VideoMaterial({file:videoURL});             // Movie material from an embedded flash animation       var topMaterial:MovieMaterial = new MovieMaterial(new DrawToolEmbedded(), {lockW:320, lockH:240, interactive:true, smooth:true, precision:5});             // Movie material from another class       var leftMaterial:MovieMaterial = new MovieMaterial(new Pong(), {smooth:true, precision:5});

As you can see, the VideoMaterial is very easy to create - simply pass the URL of the Flash video stream in the initialisation parameters and its ready! Compared to Papervision3D this is much easier (in fact, the internet connection and creation of the video stream is encapsulated in the VideoMaterial class). If you want the video to loop you can set the loop parameter to true, also sent in the initialisation parameters array. If you want to pause/play the video then you can access the netStream object directly from the material to control the playback.

The MovieMaterial is similarly easy to create. The constructor takes a Sprite object which will be mapped to the surface. I show two cases here: one where the object is an instantiation of a class in the same project, another where we embed a previously compiled Flash movie. Other than the smooth and precision parameters as we discussed in the texture mapping tutorial, we can indicate that the material should be interactive by specifying the interactive parameter to be true. Similarly we can force a size of the Sprite by giving the lockW and lockH parameters being the width and height - note that these have no relation to the dimensions of the object to which the material is mapped.

These materials are used just like all the other materials presented in this series of articles: simply create an object and pass the material to it.

      // Create three planes with different material, blurred by default       // and position them them to create tree sides of a cube       var topPlane:Plane = new Plane({material:topMaterial, width:100, height:100, segmentsW:2, segmentsH:2, ownCanvas:true});       topPlane.rotationY = -180;       topPlane.y = 50;       topPlane.filters.push(new BlurFilter(8, 8));             var leftPlane:Plane = new Plane({material:leftMaterial, width:100, height:100, segmentsW:2, segmentsH:2, ownCanvas:true});       leftPlane.rotationZ = -90;       leftPlane.rotationY = -90;       leftPlane.x = 50;       leftPlane.filters.push(new BlurFilter(8, 8));             var frontPlane:Plane = new Plane({material:frontMaterial, width:100, height:100, segmentsW:2, segmentsH:2, ownCanvas:true});       frontPlane.rotationX = -90;       frontPlane.rotationZ = 180;       frontPlane.z = 50;       frontPlane.filters.push(new BlurFilter(8, 8));             // 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);

As you can see, three planes are created, as shown in previous articles, and a different material passed to each one. These planes are then rotated and translated to form three faces of a cube. I also added a BlurFilter just to show how adding effects to Away3D objects is very simple as well.

Moving onto the mouse event listeners, for this example I don’t have a stage mouse listener to rotate the scene, only listeners for the MouseEvent3D events. Here, for each face, I add a mouse down, mouse over and mouse up listener that are triggered only when the mouse interacts with a specific 3D object.

      // Add mouse listeners to each plane for mouse down, over and out events       topPlane.addOnMouseDown(onMouseClickOnObject);       leftPlane.addOnMouseDown(onMouseClickOnObject);       frontPlane.addOnMouseDown(onMouseClickOnObject);       topPlane.addOnMouseOver(onMouseOverObject);       leftPlane.addOnMouseOver(onMouseOverObject);       frontPlane.addOnMouseOver(onMouseOverObject);       topPlane.addOnMouseOut(onMouseLeavesObject);       leftPlane.addOnMouseOut(onMouseLeavesObject);       frontPlane.addOnMouseOut(onMouseLeavesObject);

Looking first at the mouse down listener, the objective is to make the camera look directly at a cube face.

    /**     * Event listener for mouse click on plane. Makes the camera look     * directly at the plane and move closer to it.     */     private function onMouseClickOnObject(event:MouseEvent3D):void {       var object:Object3D = event.object;             doRotation = false;       // Calculate angles necessary for camera            var theta:Number = Math.atan2(object.x, object.z);       var len:Number = Math.sqrt(object.x*object.x + object.z*object.z);       var phi:Number = Math.atan2(object.y, len);       // rotate camera position       camera.targetpanangle = theta * 180 / Math.PI;       camera.targettiltangle = phi * 180 / Math.PI;       // move camera towards plane       Tweener.addTween(camera, {distance:150, time:0.5, transition:"easeOutSine"});     }

From the object location we can calculate a camera tilt and pan angle. Using the properties targetpanangle and targettiltangle the camera moves gently towards the desired position. To move the camera towards the object I’ve added a Tweener call (as we looked at in scene interaction). In this function we also turn off the automatic camera movement (the camera follows the mouse otherwise as shown below).

Moving on to the mouse over event listener, here we simply remove the BlurFilter making the face come into focus.

    /**     * Event listener for mouse over plane. Removes the blur filter.     */        private function onMouseOverObject(event:MouseEvent3D):void {       var object:Object3D = event.object;             object.filters = new Array();     }

Finally for the listeners, the mouse out event listener adds the BlurFilter, ensures that the camera moves freely with the mouse movement again and executes another Tweener call to take the camera back to its original distance.

    /**     * Event listener for mouse out of plane. Adds blur filter and moves     * camera away from plane if it isn't already.     */     private function onMouseLeavesObject(event:MouseEvent3D):void {       var object:Object3D = event.object;       object.filters.push(new BlurFilter(8, 8));       Tweener.addTween(camera, {distance:200, time:0.5, transition:"easeOutSine"});       doRotation = true;     }

That leaves us with just the event loop and the camera update function. You’ll notice that the update loop is very similar to before - there is no rotation applied to the scene objects this time but we do update the camera position.

To move the camera automatically, the relative mouse position on the screen is simply converted into pan and tilt angles and applied to the camera target angles as shown below.

    /**     * Update the camera position from mouse positions     */     private function updateCamera():void {       if (doRotation) {         camera.targetpanangle = (stage.stageWidth - stage.mouseX) / stage.stageWidth * 90;         camera.targettiltangle = (stage.stageHeight - stage.mouseY) / stage.stageHeight * 70       }     }

Note that if the user has clicked on a cube face then the doRotation boolean is false allowing the user to interact more easily with the different movie or video stream material.

And that’s all there is to it! As you’ll see, mouse events are mapped effectively to the embedded Flash animations, even in 3D, and the streaming of video is very clean and very simple to implement. Hopefully this has provided a useful introduction to these types of materials, don’t hesitate to look into the code itself to understand them more. As always comments, questions and suggestions are very welcome too!

As promised, just for completeness, you’ll find the source for the drawing tool movie and the automated Pong game below - going into the detail of these is out of the scope of this article !

DrawTool.as :

package {   import flash.display.Sprite;   import flash.events.MouseEvent;   import flash.text.TextField;   import flash.text.TextFieldAutoSize;   import flash.text.TextFormat;     public class DrawTool extends Sprite {     private var isDrawing:Boolean = false;     private var sprite:Sprite;     public function DrawTool() {       // create a drawing surface       sprite = new Sprite();       sprite.graphics.beginFill(0xEEEEEE);       sprite.graphics.moveTo(0, 0);       sprite.graphics.lineTo(320, 0);       sprite.graphics.lineTo(320, 240);       sprite.graphics.lineTo(0, 240);       sprite.graphics.endFill();       addChild(sprite);             // create text and format       var textFormat:TextFormat = new TextFormat();       textFormat.size = 30;       textFormat.font = "Arial";             var text:TextField = new TextField();       text.x = 50;       text.y = 100;       text.textColor = 0x222222;       text.text = "click to draw!";       text.setTextFormat(textFormat);       text.autoSize = TextFieldAutoSize.LEFT;       text.selectable = false;       addChild(text);             // listen to mouse events       this.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);       this.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);       this.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);           }         /**     * Event listener for mouse down event. Starts drawing circles.     */     private function onMouseDown(event:MouseEvent):void {       isDrawing = true;       drawCircle(this.mouseX, this.mouseY);     }         /**     * Event listener for mouse up event. Stops drawing circles.     */     private function onMouseUp(event:MouseEvent):void {       isDrawing = false;     }         /**     * Event listener for mouse move event. Draws a circle.     */     private function onMouseMove(event:MouseEvent):void {       if (isDrawing) {         drawCircle(this.mouseX, this.mouseY);       }     }     /**     * Function to draw a circle.     */     private function drawCircle(x:int, y:int):void {       sprite.graphics.beginFill(Math.random() * 0xFFFFFF, 0.5);       sprite.graphics.drawCircle(x, y, 5);       sprite.graphics.endFill();     }       } }

Pong.as :

package {   import flash.display.Sprite;   import flash.events.Event;     [SWF(backgroundColor="#000000")]   /**   * Simple computerised Pong copy.   */   public class Pong extends Sprite {     private static const COURT_WIDTH:Number = 320;     private static const COURT_HEIGHT:Number = 240;     private static const BALL_WIDTH:Number = 5;     private static const BAT_WIDTH:Number = 5;     private static const BAT_HEIGHT:Number = 30;     private static const COLOUR:Number = 0xDDDDDD;     private var player1:Sprite;     private var player2:Sprite;     private var ball:Sprite;         private var ballSpeedX:Number;     private var ballSpeedY:Number;     private var activePlayer:int;     private var playerIsMoving:Boolean = false;     private var playerSpeed:Number;     private var playerDestination:Number;     public function Pong() {       createCourt();       addPlayer1();       addPlayer2();       addBall();             this.addEventListener(Event.ENTER_FRAME, onFrameEnter);     }     /**     * Creates the court with net     */     private function createCourt():void {       var background:Sprite = new Sprite();       background.graphics.beginFill(0x000000);       background.graphics.moveTo(0, 0);       background.graphics.lineTo(COURT_WIDTH, 0);       background.graphics.lineTo(COURT_WIDTH, COURT_HEIGHT);       background.graphics.lineTo(0, COURT_HEIGHT);       background.graphics.endFill();       addChild(background);       var net:Sprite = new Sprite();       var nPoints:Number = 32;       var pointHeight:Number = (COURT_HEIGHT / nPoints);       var drawHeight:Number = pointHeight * 0.6;       var drawWidth:Number = drawHeight / 2;       // Create dashed net       for (var i:Number = 0; i < nPoints; i++) {         var x:Number = COURT_WIDTH / 2 - drawWidth / 2;         var y:Number = i*pointHeight;         net.graphics.beginFill(COLOUR);         net.graphics.moveTo(x, y);         net.graphics.lineTo(x+drawWidth, y);         net.graphics.lineTo(x+drawWidth, y+drawHeight);         net.graphics.lineTo(x, y+drawHeight);         net.graphics.endFill();       }                  addChild(net);     }     /**     * Add left-hand player     */     private function addPlayer1():void {       player1 = new Sprite();       createBat(player1);       player1.x = 20;       player1.y = (COURT_HEIGHT / 2) - (BAT_HEIGHT / 2);             addChild(player1);     }         /**     * Add right-hand player     */     private function addPlayer2():void {       player2 = new Sprite();       createBat(player2);       player2.x = COURT_WIDTH - 20 - BAT_WIDTH;       player2.y = (COURT_HEIGHT / 2) - (BAT_HEIGHT / 2);             addChild(player2);     }         /**     * Create a bat     */     private function createBat(player:Sprite):void {       player.graphics.beginFill(COLOUR);       player.graphics.moveTo(0, 0);       player.graphics.lineTo(BAT_WIDTH, 0);       player.graphics.lineTo(BAT_WIDTH, BAT_HEIGHT);       player.graphics.lineTo(0, BAT_HEIGHT);       player.graphics.endFill();     }         /**     * Add ball to scene and initialise speeds     */     private function addBall():void {       ball = new Sprite();             ball.graphics.beginFill(COLOUR);       ball.graphics.moveTo(0, 0);       ball.graphics.lineTo(BALL_WIDTH, 0);       ball.graphics.lineTo(BALL_WIDTH, BALL_WIDTH);       ball.graphics.lineTo(0, BALL_WIDTH);       ball.graphics.endFill();             ball.x = 0;       ball.y = 20;       addChild(ball);             ballSpeedX = 6;       ballSpeedY = 5;       activePlayer = 1;     }         /**     * Called at every frame     */     private function onFrameEnter(event:Event):void {       // update ball position       updateBall();             // update player position       updateActivePlayer();             // detect hits       hitTest();     }         /**     * Updates the ball position taking into account court dimensions     */     private function updateBall():void {       ball.x = ball.x + ballSpeedX;       ball.y = ball.y + ballSpeedY;             // Detect if ball escapes a player       if (ball.x > COURT_WIDTH - BALL_WIDTH) {         ball.x = 0;         ball.y = COURT_HEIGHT / 2;                 ballSpeedY = Math.random() * 10;       } else if (ball.x < 0) {         ball.x = COURT_WIDTH - BALL_WIDTH;         ball.y = COURT_HEIGHT / 2;                 ballSpeedY = -Math.random() * 10;       }       // Detect wall hits: invert ball y-direction and initiate player position calculation       if (ball.y < 0) {         ball.y = 0;         ballSpeedY = -ballSpeedY;         playerIsMoving = false;       } else if (ball.y > COURT_HEIGHT - BALL_WIDTH) {         ball.y = COURT_HEIGHT - BALL_WIDTH;         ballSpeedY = -ballSpeedY;         playerIsMoving = false;       }     }     /**     * Updates player position or calculates new position     */     private function updateActivePlayer():void {       var player:Sprite;       var calculatedTime:Number;             // calculate time for ball to reach player       if (activePlayer == 0) {         player = player1;         calculatedTime = Math.abs(player.x + BAT_WIDTH - ball.x) / Math.abs(ballSpeedX);       } else {         player = player2;         calculatedTime = Math.abs(player.x - (ball.x + BALL_WIDTH)) / Math.abs(ballSpeedX);       }             if (playerIsMoving) {         // update player position         player.y = player.y + playerSpeed;                 // ensure that player does not leave the court         if (player.y > COURT_HEIGHT - BAT_HEIGHT) {           player.y = COURT_HEIGHT - BAT_HEIGHT;           playerIsMoving = false;         } else if (player.y < 0) {           player.y = 0;           playerIsMoving = false;         }                 // determine if player is more or less in position         if (Math.abs(player.y - playerDestination) < BAT_HEIGHT / 4) {           playerIsMoving = false;         }               } else {         // calculate expected ball position         var calculatedY:Number = ball.y + ballSpeedY * calculatedTime;         var random:Boolean = false;                 if (calculatedY < 0 || calculatedY > COURT_HEIGHT) {           calculatedY = Math.random() * COURT_HEIGHT;           random = true;         }                 // calculate desired player position with a random element         playerDestination = calculatedY - (BAT_HEIGHT / 2) + (0.4 * (Math.random() - 0.5) * BAT_HEIGHT);         if (playerDestination < 0) {           playerDestination = 0;         } else if (playerDestination > COURT_HEIGHT - BAT_HEIGHT) {           playerDestination = COURT_HEIGHT - BAT_HEIGHT;         }                 // Calculate a random speed for player         if (player.y < playerDestination) {           playerSpeed = 5 + Math.random() * 10;                   } else {           playerSpeed = -5 + Math.random() * -10;         }                 // increase speed if player can't work out ball position         if (random) {           playerSpeed = playerSpeed * 2;         }                 // only move player if really necessary (+ stupidity estimate)         if (Math.abs(playerDestination - player.y) > BAT_HEIGHT / 2) {           playerIsMoving = true;         }       }           }         /**     * Determine if ball hits a bat     */     private function hitTest():void {       var player:Sprite;       var check:Boolean = false;             // check to see if ball is at same x position as bat       if (activePlayer == 0) {         player = player1;         if (ball.x > player1.x && ball.x < player1.x + BAT_WIDTH) {           check = true;         }       } else {         player = player2;         if (ball.x + BALL_WIDTH > player2.x && ball.x + BALL_WIDTH < player2.x + BAT_WIDTH) {           check = true;         }       }       if (check) {         // verify y position         if ((ball.y + BALL_WIDTH <= player.y + BAT_HEIGHT) && (ball.y >= player.y)) {                     // hit, change player           activePlayer = 1 - activePlayer;                     // reverse ball direction           ballSpeedX = -ballSpeedX;           // calculate new ball speed in y                    var batPosition:Number = (ball.y + (BALL_WIDTH / 2)) - (player.y + (BAT_HEIGHT / 2));           ballSpeedY = batPosition / BAT_HEIGHT * 10 * (1 + Math.random() * 0.1);         }       }     }       } }

Having obtained an interactive, texture mapped scene I’d now like to a bit more realism into it by adding a light source and shading the objects. The price of course for having a more realistic scene is at the cost of reduced performance as you’ll find many more calculations are necessary to render the scene. You can find the equivalent article for Papervision3D in my post on First steps in Papervision3D : Part 4 - lighting and shading, however I find that there is quite a difference for Away3D, at least compared to the other tutorials in this series.

Previous articles summary :

Lighting in Away3D can be split into three basic components:

  • the light source(s) defining the properties of the light(s)
  • shaders providing algorithms that determine how a light source interacts with a material
  • a material type combining different shaders along with base colours or bitmaps

The first difference I notice compared to Papervision3D is that we can specify more than one light source. This of course increases the cpu load but in certain cases may produce a more realistic rendering of a scene.

There are three main types of light sources:

  • AmbientLight3D which has no position and provides a uniform ambient light to all objects
  • DirectionalLight3D which provides light from a specified position but with a strength that does not diminish with distance
  • PointLight3D providing light from a specified position that weakens in intensity with distance

In terms of shaders we have the following posibilities:

  • AmbientShader which provides ambient shading to a triangle face
  • DiffusePhongShader which, depending on the angle between the light source and a triangle face, produces a diffuse shading pattern
  • SpecularPhongShader which produces specular shading on triangles to imitate a surface that is reflecting light, depending on the observer’s position and the light position
  • EnviroShader allowing for environment mapping, producing a reflection from a surrounding environment on an object, independent of a light source. Requires bitmap data for the environment
  • DiffuseDot3Shader to increase scene reality by adding more small scale structure through the use of normal mapping to an object. Requires bitmap data for the normal map

The above shaders, with the exception of the EnviroShader assume that the light source is a DirectionalLight3D.

We then have a choice of CompositeMaterials that uses a combination of different shaders and base materials as shown in the following list:

  • PhongBitmapMaterial used to create ambient, diffuse and specular lighting on a texture-mapped material
    Base material : BitmapMaterial
    Shaders : AmbientShader, DiffusePhongShader and SpecularPhongShader
  • PhongMovieMaterial used to create ambient, diffuse and specular lighting on a material based on another Flash movie
    Base material : MovieMaterial
    Shaders : AmbientShader, DiffusePhongShader and SpecularPhongShader
  • PhongColorMaterial used to create ambient, diffuse and specular lighting on a simple coloured material
    Base material : ColorMaterial
    Shaders : AmbientShader, DiffusePhongShader and SpecularPhongShader
  • EnviroBitmapMaterial used to create environmental lighting on a texture-mapped material
    Base material : BitmapMaterial
    Shaders : EnviroShader
  • Dot3BitmapMaterial used to create ambient and normal-mapped lighting on a texture-mapped material
    Base material : BitmapMaterial
    Shaders : AmbientShader and DiffuseDot3Shader,
  • Dot3MovieMaterial used to create ambient and normal-mapped lighting on a material based on another Flash movie
    Base material : MovieMaterial
    Shaders : AmbientShader and DiffuseDot3Shader

The above materials all produce smoothly shaded objects which naturally has a price in terms of performance. If we want to have objects that are more simply shaded then we can use a CenterLightingMaterial. This type of material produces the equivalent of flat-shaded materials in Papervision3D. We have in this category two choices:

  • ShadingColorMaterial for simple flat shading of a ColorMaterial
  • WhiteShadingBitmapMaterial for simple flat shading of a BitmapMaterial, but assuming that the light source is always white (independent of colour given to light source).

In the example code for this article I’m going to look at two of these materials: the PhongBitmapMaterial and the WhiteShadingBitmapMaterial. These two materials are very simple to use and provide a lot of control over the lighting effects on the material. One advantage here, personally speaking, over Papervision3D is the ability to mix ambient, diffuse and specular lighting all together which is necessary to produce real Phong reflection. Papervision3D provides materials for ambient plus diffuse and ambient plus specular but not all three together. This obviously is quicker to render but is less realistic when drawing shiny materials for example. Mixing all three is still possible but requires more effort as I showed in my post on phong reflection with Papervision3D.

For the light source I’m simply going to use a DirectionalLight3D which, as I mentioned before, correctly renders these materials. Another aspect that I like with Away3D is that the light source itself can be given a number of characteristics. Specifically we can give it a colour and define relative strengths of ambient, diffuse and specular shading, none of which are possible with Papervision3D. Being able to specify a light colour allows us to create new effects with materials of different colours. Specifying the ambient level directly with the light source, for example, also eliminates the need to specify the ambient light with every material created, as is the case with Papervision3D.

Away3D also allows us to create more than one light source and have a material rendered appropriately, which is not possible in Papervision3D. This, again, is obviously a drain on computational resources, but, in certain cases, this can open up new possibilities for rendering a more realistic scene.

I guess what I like most is that, conceptually, Away3D allows us to disassociate the light source from the material meaning that we can create a specific material without thinking about the environment and lighting in which it will be placed.

Anyway, enough of the theory and personal opinions, lets move onto the code. I’m using exactly the same scene as for the previous example only this time adding a DirectionalLight3D and replacing the BitmapMaterial with PhongBitmapMaterial and WhiteShadingBitmapMaterial.

package {   import away3d.cameras.Camera3D;   import away3d.containers.ObjectContainer3D;   import away3d.containers.Scene3D;   import away3d.containers.View3D;   import away3d.core.base.Object3D;   import away3d.core.math.Number3D;   import away3d.core.render.Renderer;   import away3d.core.utils.Cast;   import away3d.events.MouseEvent3D;   import away3d.lights.DirectionalLight3D;   import away3d.materials.PhongBitmapMaterial;   import away3d.materials.WhiteShadingBitmapMaterial;   import away3d.primitives.Cube;   import away3d.primitives.Cylinder;   import away3d.primitives.Sphere;   import away3d.primitives.Torus;     import caurina.transitions.Tweener;     import flash.display.Bitmap;   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 Example005 extends Sprite {     [Embed(source="/../assets/earth.jpg")] private var EarthImage:Class;     private var earthBitmap:Bitmap = new EarthImage();     [Embed(source="/../assets/away3D.png")] private var Away3DImage:Class;     private var away3DBitmap:Bitmap = new Away3DImage();     [Embed(source="/../assets/checker.jpg")] private var CheckerImage:Class;     private var checkerBitmap:Bitmap = new CheckerImage();     private static const ORBITAL_RADIUS:Number = 150;     private static const CAMERA_ORBIT:Number = 600;     private var scene:Scene3D;     private var camera:Camera3D;     private var view:View3D;       private var group:ObjectContainer3D;     private var sphere:Sphere;     private var cube:Cube;     private var centerCube:Cube;     private var cylinder:Cylinder;     private var torus:Torus;       private var doRotation:Boolean = false;     private var lastMouseX:int;     private var lastMouseY:int;     private var cameraPitch:Number = 60;     private var cameraYaw:Number = -60;         public function Example005() {             // 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 Papervision3D       init3D();             // Create the 3D objects       createScene();             // Initialise frame-enter loop       this.addEventListener(Event.ENTER_FRAME, loop);     }     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 Camera3D({zoom:25, focus:30});       setCameraPosition();             // Create a new view that encapsulates the scene and the camera       view = new View3D({scene:scene, camera:camera, renderer:Renderer.CORRECT_Z_ORDER});       // center the viewport to the middle of the stage       view.x = stage.stageWidth / 2;       view.y = stage.stageHeight / 2;             //view.renderer = Renderer.CORRECT_Z_ORDER;             addChild(view);     }     private function createScene():void {       // 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 = 100;       light.z = 500;       light.y = 500;       scene.addChild(light);       // Create an object container to group the objects on the scene       group = new ObjectContainer3D();       scene.addChild(group);           // Create a new sphere object using a very shiny phong-shaded bitmap material representing the earth       var earthMaterial:PhongBitmapMaterial = new PhongBitmapMaterial(Cast.bitmap(earthBitmap));       earthMaterial.shininess = 100;       sphere = new Sphere({material:earthMaterial, radius:50, segmentsW:10, segmentsH:10});       sphere.x = ORBITAL_RADIUS;       sphere.ownCanvas = true;       group.addChild(sphere);       // Create a new cube object using a tiled, phong-shaded bitmap material       var tiledAway3DMaterial:PhongBitmapMaterial = new PhongBitmapMaterial(Cast.bitmap(away3DBitmap), {repeat:true, scaleX:.5, scaleY:.5});       cube = new Cube({material:tiledAway3DMaterial, width:75, height:75, depth:75});       cube.z = -ORBITAL_RADIUS;       cube.ownCanvas = true;       group.addChild(cube);         // Create a cylinder mapping the earth data again       cylinder = new Cylinder({material:earthMaterial, radius:25, height:100, segmentsW:16});       cylinder.x = -ORBITAL_RADIUS;       cylinder.ownCanvas = true;       group.addChild(cylinder);         // Create a torus object and use a checkered, flat-shaded (from white light) bitmap material       var checkerBitmapMaterial:WhiteShadingBitmapMaterial = new WhiteShadingBitmapMaterial(Cast.bitmap(checkerBitmap));       torus = new Torus({material:checkerBitmapMaterial, radius:40, tube:10, segmentsT:8, segmentsR:16});       torus.z = ORBITAL_RADIUS;       torus.ownCanvas = true;       group.addChild(torus);         // Create a new cube object using a smoothed, precise, phong-shaded, mat (not shiny) bitmap material       var away3DMaterial:PhongBitmapMaterial = new PhongBitmapMaterial(Cast.bitmap(away3DBitmap), {smooth:true, precision:2});       away3DMaterial.shininess = 0;       centerCube = new Cube({material:away3DMaterial, width:75, height:75, depth:75});       centerCube.ownCanvas = true;       group.addChild(centerCube);       // add mouse listeners to all the 3D objects       sphere.addOnMouseDown(onMouseDownOnObject);       cube.addOnMouseDown(onMouseDownOnObject);       cylinder.addOnMouseDown(onMouseDownOnObject);       torus.addOnMouseDown(onMouseDownOnObject);       centerCube.addOnMouseDown(onMouseDownOnObject);     }         private function loop(event:Event):void {             // rotate the group of objects       group.yaw(2);           // rotate the objects       sphere.yaw(-4);       cube.yaw(-4);       cylinder.yaw(-4);       torus.yaw(-4);       // update the camera position       updateCamera();       // Render the 3D scene       view.render();     }     // updates the camera position     private function updateCamera():void {             // 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         setCameraPosition();       }           }     // sets the camera position given pitch and yaw angles     private function setCameraPosition():void {       camera.y = CAMERA_ORBIT * Math.cos(cameraPitch * Math.PI / 180);       camera.x = CAMERA_ORBIT * Math.sin(cameraPitch * Math.PI / 180) * Math.cos(cameraYaw * Math.PI / 180);       camera.z = CAMERA_ORBIT * Math.sin(cameraPitch * Math.PI / 180) * Math.sin(cameraYaw * Math.PI / 180);             // keep the camera looking at the origin       camera.lookAt(new Number3D(0, 0, 0));     }     // 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 3D object     private function onMouseDownOnObject(event:MouseEvent3D):void {       var object:Object3D = event.object;       Tweener.addTween(object, {y:200, time:1, transition:"easeOutSine", onComplete:function():void {goBack(object);} });     }         // called when a tween created in onMouseDownOnObject has terminated     private function goBack(object:Object3D):void {       Tweener.addTween(object, {y:0, time:2, transition:"easeOutBounce"});     }         // called when the window is resized     private function onResize(event:Event):void {       view.x = stage.stageWidth / 2;       view.y = stage.stageHeight / 2;     }   }     }

When compiled you should get the same result as below (click on the image). As before, the scene is interactive so you can click on the background and move the mouse to move the camera and click on an object to make it bounce.

Since the code is so similar to the previous example I’m going to concentrate solely on the function createScene - this is the only function that has been modified.

In this example we use a simple white light source and see four different types of materials rendered: one that is very shiny, another that has a mat texture, one that takes default lighting parameters (being quite shiny) and another that is flat shaded.

Starting with the light source, we use a DirectionLight3D for which we specify its position and lighting properties.

      // 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 = 100;       light.z = 500;       light.y = 500;       scene.addChild(light);

As you can see the constructor takes an array of initialisation parameters in which we set the colour to white and give it ambient, diffuse and specular factors (being between 0 and 1).

The first object that we create is the rotating globe. Here I wanted the object to appear to be very shiny so modify its shininess.

      // Create a new sphere object using a very shiny phong-shaded bitmap material representing the earth       var earthMaterial:PhongBitmapMaterial = new PhongBitmapMaterial(Cast.bitmap(earthBitmap));       earthMaterial.shininess = 100;       sphere = new Sphere({material:earthMaterial, radius:50, segmentsW:10, segmentsH:10});       sphere.x = ORBITAL_RADIUS;       sphere.ownCanvas = true;       group.addChild(sphere);

First of all, the PhongBitmapMaterial takes bitmap data for the texture mapping, as is the case for an ordinary, unshaded BitmapMaterial. Next we modify the shininess property (by default this is set at 20). This property, on the rendered scene, in effect modifies the extent of the specular lighting. The bright white dot that we see on the globe is reduced by increasing this property as if the light source is reflected off a more concentrated region of the material.

An important point to note is the line

      sphere.ownCanvas = true;

Without this line the scene is not correctly rendered as objects remain partially visible when they pass behind others.

Moving on to the tiled material object, I wanted to show that the initialisation parameters that we send to a material are still taken into account for the base material. The PhongBitmapMaterial is composed of a TransformBitmapMaterial (as well as the shaders) as we discussed in the previous example. The initialisation parameters {repeat:true, scaleX:.5, scaleY:.5} are passed directly to this element when it is constructed and therefore the texture map is still tiled. This object uses the default shading parameters of the PhongBitmapMaterial and is therefore appears less shiny than the globe.

The cylinder uses the same material as the globe just to illustrate that we can use the same material more than once.

Next, for the torus, we implement flat shading by using a WhiteShadingBitmapMaterial. As its name indicates this assumes a white light source. You can test this by experimenting with the light source colour. You’ll notice that all the other objects correctly reflect the change in light colour but this object remains white and red. This material is obviously less complex in terms of necessary calculations and therefore provides better rendering performance. However, we see that smoothly shaded materials, as with the other objects, allow us to reduce the number of rendered triangles to produce more rounded objects.

Finally, the cube in the center is rendered with shininess set to zero. Only diffuse and ambient lighting remain in this case. This is useful for rendering mat materials but unfortunately has no performance benefits. We can see however that there is a difference in the visible result for the two cubes.

So thats it for this tutorial! I hope this has given a useful overview of lighting and shading in Away3D. Of course I’ve not gone into detail for all the different types of lights and shaders - you’ll discover a lot by experimenting with this example, for example by changing the materials, the type of lighting, its properties and its position. I’ve purposely ignored the Dot3BitmapMaterial and EnviroBitmapMaterial for this post because I felt that it was important to look at simple lighting and shading first - they should appear in the next article. But for now, at least, I hope this provides a useful starting point - comments and suggestions, as always, are very welcome!

Next article:

-->
Monday, November 17th, 2008

First steps in Away3D : Part 4 - Scene interaction

Having created a scene last time with texture mapped objects, I’d now like to add some interaction so that we can rotate the camera around the scene and make the 3D objects react to mouse events. As you’ll see this is very straightforward with Away3D as each triangle drawn on the screen individually detects mouse events. For an equivalent tutorial in Papervision3D, check out First steps in Papervision3D : Part 5.

Previous articles summary :

For the scene interaction we’ll be looking at two different types of events. Firstly, the standard flash MouseEvent that we’ll use to rotate the camera when the user clicks on the stage and moves the mouse. Secondly we’ll look at the Away3D-specific event: the MouseEvent3D. This event is created for similar mouse actions on 3D objects but more importantly it contains the associated 3D object. To add MouseEvent3D listeners, all Object3D objects have the following functions for different mouse interactions:

Another new feature for this tutorial is to add object animation using an external library: Tweener.

Without having to calculate and specify the change in positions (for example) of objects within the code, a tween does all the hard work for us. A tween works on a particular property of an object and at each frame of an animation modifies this property in a particular manner. You’ll find more information on tweens on the web, for example at wikipedia.

Tweener is an opensource library, compatible with Actionscript2 and Actionscript3 as well as JavaScript and HaXE. To install it in your Eclipse/Flex Builder 3 environment simply create a new Flex Library Project, download the sources from the Tweener homepage and extract them into the src directory. The source should be automatically compiled and you can either link your own ActionScript projects to this project or directly used the compiled library. If you need help on compiling new flex library projects in eclipse then you should find useful information on my post for installing and compiling Away3D in eclipse.

Lets take a look at the code. I’m following directly from the example in the last post but am simply adding the new mouse listeners.

package {   import away3d.cameras.Camera3D;   import away3d.containers.ObjectContainer3D;   import away3d.containers.Scene3D;   import away3d.containers.View3D;   import away3d.core.base.Object3D;   import away3d.core.math.Number3D;   import away3d.core.utils.Cast;   import away3d.events.MouseEvent3D;   import away3d.materials.BitmapMaterial;   import away3d.materials.TransformBitmapMaterial;   import away3d.primitives.Cube;   import away3d.primitives.Cylinder;   import away3d.primitives.Sphere;   import away3d.primitives.Torus;     import caurina.transitions.Tweener;     import flash.display.Bitmap;   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 Example004 extends Sprite {     [Embed(source="/../assets/earth.jpg")] private var EarthImage:Class;     private var earthBitmap:Bitmap = new EarthImage();     [Embed(source="/../assets/away3D.png")] private var Away3DImage:Class;     private var away3DBitmap:Bitmap = new Away3DImage();     [Embed(source="/../assets/checker.jpg")] private var CheckerImage:Class;     private var checkerBitmap:Bitmap = new CheckerImage();     private static const ORBITAL_RADIUS:Number = 150;     private static const CAMERA_ORBIT:Number = 600;     private var scene:Scene3D;     private var camera:Camera3D;     private var view:View3D;       private var group:ObjectContainer3D;     private var sphere:Sphere;     private var cube:Cube;     private var centerCube:Cube;     private var cylinder:Cylinder;     private var torus:Torus;       private var doRotation:Boolean = false;     private var lastMouseX:int;     private var lastMouseY:int;     private var cameraPitch:Number = 60;     private var cameraYaw:Number = -60;         public function Example004() {             // 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 Papervision3D       init3D();             // Create the 3D objects       createScene();             // Initialise frame-enter loop       this.addEventListener(Event.ENTER_FRAME, loop);     }     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 Camera3D({zoom:25, focus:30});       setCameraPosition();             // Create a new view that encapsulates the scene and the camera       view = new View3D({scene:scene, camera:camera});       // center the viewport to the middle of the stage       view.x = stage.stageWidth / 2;       view.y = stage.stageHeight / 2;       addChild(view);     }     private function createScene():void {       // Create an object container to group the objects on the scene       group = new ObjectContainer3D();       scene.addChild(group);           // Create a new sphere object using a bitmap material representing the earth       var earthMaterial:BitmapMaterial = new BitmapMaterial(Cast.bitmap(earthBitmap));       sphere = new Sphere({material:earthMaterial, radius:50, segmentsW:10, segmentsH:10});       sphere.x = ORBITAL_RADIUS;       group.addChild(sphere);       // Create a new cube object using a tiled bitmap material       var tiledAway3DMaterial:TransformBitmapMaterial = new TransformBitmapMaterial(Cast.bitmap(away3DBitmap), {repeat:true, scaleX:.5, scaleY:.5});       cube = new Cube({material:tiledAway3DMaterial, width:75, height:75, depth:75});       cube.z = -ORBITAL_RADIUS;       group.addChild(cube);         // Create a cylinder mapping the earth data again       cylinder = new Cylinder({material:earthMaterial, radius:25, height:100, segmentsW:16});       cylinder.x = -ORBITAL_RADIUS;       group.addChild(cylinder);         // Create a torus object and use a checkered bitmap material       var checkerBitmapMaterial:BitmapMaterial = new BitmapMaterial(Cast.bitmap(checkerBitmap));       torus = new Torus({material:checkerBitmapMaterial, radius:40, tube:10, segmentsT:8, segmentsR:16});       torus.z = ORBITAL_RADIUS;       group.addChild(torus);         // Create a new cube object using a smoothed, precise bitmap material       var away3DMaterial:BitmapMaterial = new BitmapMaterial(Cast.bitmap(away3DBitmap), {smooth:true, precision:2});       centerCube = new Cube({material:away3DMaterial, width:75, height:75, depth:75});       group.addChild(centerCube);       // add mouse listeners to all the 3D objects       sphere.addOnMouseDown(onMouseDownOnObject);       cube.addOnMouseDown(onMouseDownOnObject);       cylinder.addOnMouseDown(onMouseDownOnObject);       torus.addOnMouseDown(onMouseDownOnObject);       centerCube.addOnMouseDown(onMouseDownOnObject);     }         private function loop(event:Event):void {             // rotate the group of objects       group.yaw(2);           // rotate the objects       sphere.yaw(-4);       cube.yaw(-4);       cylinder.yaw(-4);       torus.yaw(-4);       // update the camera position       updateCamera();       // Render the 3D scene       view.render();     }     // updates the camera position     private function updateCamera():void {             // 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         setCameraPosition();       }           }     // sets the camera position given pitch and yaw angles     private function setCameraPosition():void {       camera.y = CAMERA_ORBIT * Math.cos(cameraPitch * Math.PI / 180);       camera.x = CAMERA_ORBIT * Math.sin(cameraPitch * Math.PI / 180) * Math.cos(cameraYaw * Math.PI / 180);       camera.z = CAMERA_ORBIT * Math.sin(cameraPitch * Math.PI / 180) * Math.sin(cameraYaw * Math.PI / 180);             // keep the camera looking at the origin       camera.lookAt(new Number3D(0, 0, 0));     }     // 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 3D object     private function onMouseDownOnObject(event:MouseEvent3D):void {       var object:Object3D = event.object;       Tweener.addTween(object, {y:200, time:1, transition:"easeOutSine", onComplete:function():void {goBack(object);} });     }         // called when a tween created in onMouseDownOnObject has terminated     private function goBack(object:Object3D):void {       Tweener.addTween(object, {y:0, time:2, transition:"easeOutBounce"});     }         // called when the window is resized     private function onResize(event:Event):void {       view.x = stage.stageWidth / 2;       view.y = stage.stageHeight / 2;     }   }     }

The example embeds the same images as the last time so if you need them you can find them in my last post. Don’t forget that you’ll need to link to the Tweener library for this to compile correctly. The resulting Flash movie should be as below - click on the image below to load it. Now when you click on the background and move the mouse you should be able to rotate the scene (effectively moving the camera) and clicking on each object should make them bounce.

First of all, lets have a look how the we add the standard Flash MouseEvent to rotate the camera.

Starting with the constructor we add two listeners.

      // Listen to mouse up and down events on the stage       stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);       stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);

When a mouse click is detected on the stage the function onMouseDown is called. When the button is released onMouseUp is called. These functions indicate when the camera movement should start and stop.

    // 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;     }

To start the camera movement we set the boolean doRotation to true and to stop it we set it to false. The amount of rotation depends on how far the mouse moves during a single frame so we store the position of the mouse at every frame as well. The camera is then updated during the loop function

    private function loop(event:Event):void {             // rotate the group of objects       group.yaw(2);           // rotate the objects       sphere.yaw(-4);       cube.yaw(-4);       cylinder.yaw(-4);       torus.yaw(-4);       // update the camera position       updateCamera();       // Render the 3D scene       view.render();     }

Other than the call to updateCamera the rendering remains identical to the last example. The camera position is then calculated as follows.

    // updates the camera position     private function updateCamera():void {             // 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         setCameraPosition();       }           }

From the change in mouse position we calculate (arbitrarily) a change in angle from the y axis (pitch) and similarly around the y axis (yaw). The pitch is kept within limits to avoid the camera from going over the poles and hence changing the up direction of the camera. Once we’ve calculated the new values of pitch and yaw for the camera position we can convert these angles into x, y and z positions. This is done in setCameraPosition.

    // sets the camera position given pitch and yaw angles     private function setCameraPosition():void {       camera.y = CAMERA_ORBIT * Math.cos(cameraPitch * Math.PI / 180);       camera.x = CAMERA_ORBIT * Math.sin(cameraPitch * Math.PI / 180) * Math.cos(cameraYaw * Math.PI / 180);       camera.z = CAMERA_ORBIT * Math.sin(cameraPitch * Math.PI / 180) * Math.sin(cameraYaw * Math.PI / 180);             // keep the camera looking at the origin       camera.lookAt(new Number3D(0, 0, 0));     }

Note that we keep the camera at all times looking at the origin.

Moving on to the interaction with the 3D objects using the MouseEvent3D event, for this example I’m just going to look at the events when the mouse is clicked over an object. You’ll see that adding the listeners is very simple: at the end of the createScene a listener is added to each object.

      // add mouse listeners to all the 3D objects       sphere.addOnMouseDown(onMouseDownOnObject);       cube.addOnMouseDown(onMouseDownOnObject);       cylinder.addOnMouseDown(onMouseDownOnObject);       torus.addOnMouseDown(onMouseDownOnObject);       centerCube.addOnMouseDown(onMouseDownOnObject);

The rest of createScene is identical to the previous example. Now, whenever a user clicks on these objects, the function onMouseDownOnObject is called.

    // called when mouse down on a 3D object     private function onMouseDownOnObject(event:MouseEvent3D):void {       var object:Object3D = event.object;       Tweener.addTween(object, {y:200, time:1, transition:"easeOutSine", onComplete:function():void {goBack(object);} });     }

The listener is called with the MouseEvent3D passed as an argument. From this we can easily obtain the Object3D for which the event occurred. We then invoke Tweener to perform an animation on the object.

Tweener contains static methods that allow us to modify object properties. In this case we simply modify the y position of the object so that it goes to a value of 200 in 1 second using the easeOutSine transition function. I’d recommend looking at the Tweener documentation on the different transition functions. The onComplete argument allows us to put the sphere back to its original position when the tween has completed.

    // called when a tween created in onMouseDownOnObject has terminated     private function goBack(object:Object3D):void {       Tweener.addTween(object, {y:0, time:2, transition:"easeOutBounce"});     }

This then uses another tween with an easeOutBounce transition to move the sphere back to the x-z plane.

And that’s all there is to it! With Tweener you can modify a number of different properties in parallel. For example you can change y and scale the object at the same time. The interface provided by Away3D is in any case very easy to use. Making a comparison to Papervision3D I find it a bit easier: with Papervision3D we need to say that the say that each individual material is interactive, as well as the View itself. This is surely to produce some performance optimisation. Other than this the interaction with objects is very similar for both engines and, I hope you’ll find, is straightforward to implement!

Next article:

Saturday, November 15th, 2008

First steps in Away3D : Part 2 - Animation

Following from my previous post, I’d like to make the scene a bit more interesting by adding some animation. As you’ll see, not many modifications to the code are necessary. As with Part 1, I’m basing this example on a previous Papervision3D example that you can find in First steps in Papervision3D : Part 3.

Previous articles summary :

Using the same objects as before (a Sphere and LineSegments displaying the x, y and z axes), my objective is to rotate all of them about the origin and individually rotate the sphere. Rotation in Away3D is very easy to achieve. These objects inherit from a base class called Object3D as do all 3D object displayed in a Scene in Away3D. This class provides a number of useful functions to rotate, translate and scale an object. The simplest way to rotate an object is to use the pitch, yaw and roll functions which rotate an object about its local x, y and z axes respectively.

So, lets dive right in and take a look at the code. As I mentioned before, this is based on the previous example and very little modifications have been made. As before I’m using eclipse with the Flex Builder 3 plugin to compile the examples: take a look at my previous post if you’re new to eclipse and want to see how to set up the projects. Otherwise, create a new ActionScript class, call it Example002 and cut and paste the following code.

package {   import away3d.cameras.Camera3D;   import away3d.containers.ObjectContainer3D;   import away3d.containers.Scene3D;   import away3d.containers.View3D;   import away3d.core.base.Vertex;   import away3d.core.math.Number3D;   import away3d.materials.WireColorMaterial;   import away3d.materials.WireframeMaterial;   import away3d.primitives.LineSegment;   import away3d.primitives.Sphere;     import flash.display.Sprite;   import flash.display.StageAlign;   import flash.display.StageScaleMode;   import flash.events.Event;     [SWF(backgroundColor="#000000")]     public class Example002 extends Sprite {     private var scene:Scene3D;     private var camera:Camera3D;     private var view:View3D;       private var group:ObjectContainer3D;     private var sphere:Sphere;         public function Example002() {             // set up the stage       stage.align = StageAlign.TOP_LEFT;       stage.scaleMode = StageScaleMode.NO_SCALE;       // Add resize event listener       stage.addEventListener(Event.RESIZE, onResize);             // Initialise Papervision3D       init3D();             // Create the 3D objects       createScene();             // Initialise frame-enter loop       this.addEventListener(Event.ENTER_FRAME, loop);     }     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 Camera3D({zoom:15, focus:30, x:100, y:300, z:-200});       camera.lookAt(new Number3D(0, 0, 0));             // Create a new view that encapsulates the scene and the camera       view = new View3D({scene:scene, camera:camera});       // center the viewport to the middle of the stage       view.x = stage.stageWidth / 2;       view.y = stage.stageHeight / 2;       addChild(view);     }     private function createScene():void {       // Create an object container to group the objects on the scene       group = new ObjectContainer3D();       scene.addChild(group);             // Create a new sphere object using a wirecolor material       var sphereMaterial:WireColorMaterial = new WireColorMaterial(0x5500FF, {wirecolor:0xFF9900});       sphere = new Sphere({material:sphereMaterial, radius:50, segmentsW:10, segmentsH:10});       // Position the sphere and add it to the group       sphere.x = -100;       group.addChild(sphere);         // Create a origin vertex       var origin:Vertex = new Vertex(0, 0, 0);       // Create the red-coloured x-axis with a width of 2 and add it to the group       var xAxis:LineSegment = new LineSegment({material:new WireframeMaterial(0xFF0000, {width:2})});       xAxis.start = origin;       xAxis.end = new Vertex(100, 0, 0);       group.addChild(xAxis);           // Create the green-coloured y-axis with a width of 2 and add it to the group       var yAxis:LineSegment = new LineSegment({material:new WireframeMaterial(0x00FF00, {width:2})});       yAxis.start = origin;       yAxis.end = new Vertex(0, 100, 0);       group.addChild(yAxis);           // Create the blue-coloured z-axis with a width of 2 and add it to the group       var zAxis:LineSegment = new LineSegment({material:new WireframeMaterial(0x0000FF, {width:2})});       zAxis.start = origin;       zAxis.end = new Vertex(0, 0, 100);       group.addChild(zAxis);       }         private function loop(event:Event):void {             // rotate the group of objects       group.yaw(5);           // rotate the sphere       sphere.yaw(-10);           // Render the 3D scene       view.render();     }     private function onResize(event:Event):void {       view.x = stage.stageWidth / 2;       view.y = stage.stageHeight / 2;     }   } }

This should produce a scene that is rotated about the y-axis with a sphere that rotates in the opposite direction, also about its y-axis. Click on the image below to see the final result.

As always, lets take a look at the code in more detail. Since there are so many similarities with the previous example I won’t go into detail for everything, just what is new.

Starting with the constructor, you’ll notice that I’ve added a resize listener that calls the method onResize.

    private function onResize(event:Event):void {       view.x = stage.stageWidth / 2;       view.y = stage.stageHeight / 2;     }   } }

This is used to ensure that if a user resizes the browser or flash player that the View is automatically resized to take up the full stage area. Its not really much of a 3D element of the scene but its important not to forget it!

The rest of the code takes the same form as before: initialise the 3D elements, create the scene and re-render the scene at every new frame. As you’ll see in init3D I’ve modified the camera slightly.

      // Create a new camera, passing some initialisation parameters       camera = new Camera3D({zoom:15, focus:30, x:100, y:300, z:-200});       camera.lookAt(new Number3D(0, 0, 0));

The camera is now in a different position, but more importantly I’ve added a call to camera.lookAt. Previously the camera was looking directly along the z-axis: now I’ve told it to look at the origin. You can similarly call the functions tilt and pan which (as it says in the code comments for the Camera3D class) is like someone nodding and shaking their head.

Moving on to the scene creation in createScene, I’ve introduced a new element used to group the objects: an ObjectContainer3D.

      // Create an object container to group the objects on the scene       group = new ObjectContainer3D();       scene.addChild(group);

This object is not itself visible on the scene but allows us to add children to it and translate, rotate and scale these children all together. So, rather than adding each individual object to the scene, they are now added to the group as is, for example, the sphere.

      // Create a new sphere object using a wirecolor material       var sphereMaterial:WireColorMaterial = new WireColorMaterial(0x5500FF, {wirecolor:0xFF9900});       sphere = new Sphere({material:sphereMaterial, radius:50, segmentsW:10, segmentsH:10});       // Position the sphere and add it to the group       sphere.x = -100;       group.addChild(sphere);  

The lines are similarly added to the group. So that’s the only difference to the scene creation itself (other than the sphere now being a different colour). Now we come to adding the animation.

To add animation we simply need to modify object parameters at each frame and then re-render the scene. As I mentioned before I simple rotate the ObjectContainer3D and the Sphere itself and this is done in the loop function that is called at the start of every new frame.

    private function loop(event:Event):void {             // rotate the group of objects       group.yaw(5);           // rotate the sphere       sphere.yaw(-10);           // Render the 3D scene       view.render();     }

As you can see there’s really not much to it: simple rotate the group about its y axis, and the same for the sphere itself. The call the view.render then updates what we see on the screen.

And that’s it! To get a feel for the animation, try changing the yaw method to roll or pitch, or try adding some translation. I’d recommend having a look at the Away3D code to see what else is available - you’ll find that there are a lot of possibilities! Anyway, I hope this has been a useful step in making more interesting 3D scenes in Away3D!

Next article:

-->
Monday, September 29th, 2008

First steps in Papervision3D : Part 9 - Importing and working with 3D objects

Previous articles summary :

Back again for another article in the First Steps series… its been a while since the last one mainly because I’ve moved from Blogger to Wordpress and that’s been quite a hassle. But, hopefully, now that its up and running it won’t be so long before the next one… fingers crossed!

In this article (which again is probably going to be quite a long one) I’m looking at how to import 3D objects that have been created using specialised applications such as 3DS Max, Maya, Blender and Google Sketchup to name a few.

To improve the exchange of digital assets between these different applications, various file formats exists that allow 3D objects from one application to be used in another. One particular format that is readable by Papervision3D, is the Collaborative Design Activity format or COLLADA which saves the information of a 3D scene as XML.

The Collada format specifications are maintained by the Khronos Group and embedded within the file are details on geometry, shaders and effects, physics, animation and kinematics. The specifications are supported by a number of companies including 3DS Max, Maya and Blender. The files themselves have the .dae extension, standing for Digital Asset Exchange.

Papervision3D contains a extensive package to parse and import Collada files yet has a simple interface making it easy to use complex 3D models created in specialised applications. Worth noting are a couple of sites that contain Collada files created by different communities that can be downloaded and used fairly freely. These are

As I’ve mentioned from the beginning of this series, these posts represent also what I’ve learned over the last few weeks and are by no means expert tutorials. I’ve therefore made use of a number of sites in understanding how to import objects into Papervision3D and I’m sure you’ll find useful information on them as well.

Since there are a number of points to look at I decided it was best to split this post into two parts. In the first part of this post I’ll illustrate a selection of different ways of importing objects into Papervision3D based on the above examples and also from exploring the source code of Paperivison3D itself. In the second part I’m going to look at how to add interactivity and shading with Collada objects. I’ll also present some problems that exist with shading in the current version of Papervision3D - well, it is beta after all!

Lets have a look at the source for the first example. Here, my objective is simply to import 3D objects into a scene using a number of different sources. These include

The last one is making use of a special class within Papervision3D dedicated to file with the .kmz file extension (Google Earth). These files are actually .zip files that contain texture maps and Collada data.

Papervision3D also has (as far as I can tell) two main ways of importing Collada data: one is using the Collada class, the other is using the DAE class, both of which are in the org.papervision3d.objects.parsers package. Again, as far as I can tell, the DAE class appears to be more mature making extensive use of the org.ascollada package and I’ve had more success over the last few days using this rather than the Collada class.

So here’s the source code, importing four objects using different sources and different importing methods.

package {     import flash.events.Event;   import flash.events.MouseEvent;     import org.papervision3d.events.FileLoadEvent;   import org.papervision3d.lights.PointLight3D;   import org.papervision3d.materials.BitmapMaterial;   import org.papervision3d.materials.MovieMaterial;   import org.papervision3d.materials.utils.MaterialsList;   import org.papervision3d.objects.DisplayObject3D;   import org.papervision3d.objects.parsers.Collada;   import org.papervision3d.objects.parsers.DAE;   import org.papervision3d.objects.parsers.KMZ;   import org.papervision3d.view.BasicView;   public class Example009a extends BasicView {       [Embed(source="/../assets/cow.dae", mimeType="application/octet-stream")] private var CowDAE:Class;     [Embed(source="/../assets/Cow.png")] private var CowBitmapImage:Class;     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;             public function Example009a() {       super(0, 0, true, true);             // 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);       // Start rendering the scene       startRendering();     }         private function init3D():void {       // position the camera       camera.z = -700;       camera.fov = 60;       camera.orbit(cameraPitch, cameraYaw);     }         private function createScene():void {       // create new Collada from URL, using original materials and scaled to 50%       var cow:Collada = new Collada("http://www.tartiflop.com/pv3d/FirstSteps/collada/cow.dae", null, 0.5);       cow.moveDown(100);       cow.moveBackward(200);       cow.yaw(90);       scene.addChild(cow);       // create a texture mapped material from embedded png       var cowMaterial:BitmapMaterial = new MovieMaterial(new CowBitmapImage(), true);             // add the texture map to a material list corresponding to the material symbols in the dae       var cowMaterials:MaterialsList = new MaterialsList();       cowMaterials.addMaterial(cowMaterial, "mat0");       // create a new Collada, specifying the materials we want to use       var cow2:Collada = new Collada(new XML(new CowDAE()), cowMaterials);       cow2.moveRight(300);       cow2.moveDown(100);       scene.addChild(cow2);           // create a new DAE that is animated and perform actions once it is loaded       var seymour:DAE = new DAE(true);       seymour.addEventListener(FileLoadEvent.LOAD_COMPLETE, function onLoad(event:Event):void {         seymour.scale = 20;         seymour.moveForward(200);         seymour.moveDown(100);         scene.addChild(seymour);       });             // load the DAE from a specific URL       seymour.load("http://www.tartiflop.com/pv3d/FirstSteps/collada/Seymour.dae");             // create a new 3D object from a 3D google earth object file and perform actions when loaded       var kmz:KMZ = new KMZ();       kmz.addEventListener(FileLoadEvent.LOAD_COMPLETE, function onLoad(event:Event):void {         kmz.scale = 20;         kmz.moveLeft(300);         kmz.moveDown(100);         scene.addChild(kmz);       });             // load kmz from a specific URL       kmz.load("http://www.tartiflop.com/pv3d/FirstSteps/collada/thing.kmz");           }     override protected function onRenderTick(event:Event=null):void {       // update camera position       updateCamera();           // call the renderer       super.onRenderTick(event);     }         private function updateCamera():void {             // 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);       }           }     // 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;     }       } }

This produces the following flash animation (click on the image below). Note that it can take some time for the models to be loaded into flash, but in the end you should see four models including two cows, an animated Space Boy (coming from the public directory of the Collada test model bank at Khronos) and a rather crappy thing I made in Google Sketchup! You can rotate the scene by clicking and moving the mouse at the same time.

So, lets take a closer look at the code… As with other examples I’m using the same standard BasicView derived class - the main difference from previous ones coming in the createScene function.

The first cow object is created by obtaining all the data for the Collada object from an external URL. Contained in the .dae file is information on the texture maps so we don’t need to specify anything other than the location of the Collada file itself to create a fully rendered object.

      // create new Collada from URL, using original materials and scaled to 50%       var cow:Collada = new Collada("http://www.tartiflop.com/pv3d/FirstSteps/collada/cow.dae", null, 0.5);       cow.moveDown(100);       cow.moveBackward(200);       cow.yaw(90);       scene.addChild(cow);

Here I’m using the Collada class which provides a very simple interface to import Collada data. The first argument is of course the location of the .dae file (you can just as easily use a file on the local file system). The second argument is to specify a different material for the object: in this case I’ll use the file referenced by the Collada file itself. The third parameter relates to the scaling: in this case 50%. The resulting object is then translated, rotated and added to the scene.

We can similarly import Collada data by embedding the data in the Flash animation (as we’ve seen in previous examples in this series). We have to, however, embed both the Collada data and the texture map data for it to be correctly rendered. You’ll find the embedded files at the beginning of the class definition.

    [Embed(source="/../assets/cow.dae", mimeType="application/octet-stream")] private var CowDAE:Class;     [Embed(source="/../assets/Cow.png")] private var CowBitmapImage:Class;

The .dae file format is not recognised by Flash which is why we need to explicitly give the mimeType. As you’ll see below we can use the DAE data directly as an XML document. The Collada object is then created as follows.

      // create a texture mapped material from embedded png       var cowMaterial:BitmapMaterial = new MovieMaterial(new CowBitmapImage(), true);             // add the texture map to a material list corresponding to the material symbols in the dae       var cowMaterials:MaterialsList = new MaterialsList();       cowMaterials.addMaterial(cowMaterial, "mat0");       // create a new Collada, specifying the materials we want to use       var cow2:Collada = new Collada(new XML(new CowDAE()), cowMaterials);       cow2.moveRight(300);       cow2.moveDown(100);       scene.addChild(cow2);

We specifically create a material for the 3D object: I’m using a MovieMaterial which in this case is created with a simple bitmap image. Since a Collada object can have a number of different textured materials we need to provide it with a MaterialList. The names associated with the materials have to relate to details within the Collada file… after some investigation I found that the material was referenced by the name mat0. The Collada object is then created by passing data encapsulated in an XML format and this time specifying the material list used in association with the data. As with the first object, we then translate it and add it to the scene.

The third object is an animated Collada object. This time I’m using the DAE class which has more success in importing the data. The interface remains very similar to the Collada class however we need to specifically load the data.

      // create a new DAE that is animated and perform actions once it is loaded       var seymour:DAE = new DAE(true);       seymour.addEventListener(FileLoadEvent.LOAD_COMPLETE, function onLoad(event:Event):void {         seymour.scale = 20;         seymour.moveForward(200);         seymour.moveDown(100);         scene.addChild(seymour);       });             // load the DAE from a specific URL       seymour.load("http://www.tartiflop.com/pv3d/FirstSteps/collada/Seymour.dae");

The first argument to the DAE constructor specifies whether we want the DAE to be animated or not: in this case we do. We then add a listener which is triggered when the data is fully loaded. This allows us to add it to the scene and modify its size and position when we are sure that the data is coherent. The data is loaded by specifying a URL to the Collada file.

Finally to show how to import data from a different source, I’ve included an example that I made using Google Sketchup and exported as a Google Earth object (.kmz file extension). And yes, I know, its not very pretty…

      // create a new 3D object from a 3D google earth object file and perform actions when loaded       var kmz:KMZ = new KMZ();       kmz.addEventListener(FileLoadEvent.LOAD_COMPLETE, function onLoad(event:Event):void {         kmz.scale = 20;         kmz.moveLeft(300);         kmz.moveDown(100);         scene.addChild(kmz);       });             // load kmz from a specific URL       kmz.load("http://www.tartiflop.com/pv3d/FirstSteps/collada/thing.kmz");

Papervision3D provides us with a class KMZ specifically to import this type of data. In fact, embedded in this file is a .dae Collada file (and the class has a reference to a DAE object as well) so the import mechanism remains the same… it is however another handy tool. As you can see we use the same technique as with the DAE import.

So, there we are: four different methods of importing Collada data. Now for the more advanced part of this post: adding shading to the objects.

In principal this should follow from the previous post on texture mapping with lighting where a ShadedMaterial is used to skin an object (as we did above with cow2 when we specified a list of materials for the DAE). However, at the time of writing this article, this is not working correctly: the main problem being black lines appearing on face edges. As you can see from the list of bugs at the home of Papervision3D, an issue has been raised explaining the problem. You can also see on the Papervision3D mailing list that this is a recurring problem (here and here for example).

This said, all is not lost! Thanks to those talented people involved in the Papervision3D project, specifically in this case to Andy Zupko, a work-around does exist! Also, I’d like to give a thanks to the Papervision3D newsgroup because the list is very active and you can find a lot of very good information on it… like this fix.

So, here’s the second example source code for this post. Here I’m showing the two different methods for rendering a shaded Collada object: the first (not fully working in the current release of Papervision3D) using a ShadedMaterial and the second using Andy’s work-around which involves blending two identical models: one with a simple shaded material, the other with a texture-mapped material.

package {     import flash.display.BlendMode;   import flash.events.Event;   import flash.events.MouseEvent;   import flash.text.TextField;   import flash.text.TextFieldAutoSize;   import flash.text.TextFormat;   import flash.utils.getTimer;     import org.papervision3d.events.FileLoadEvent;   import org.papervision3d.events.InteractiveScene3DEvent;   import org.papervision3d.lights.PointLight3D;   import org.papervision3d.materials.BitmapMaterial;   import org.papervision3d.materials.MovieMaterial;   import org.papervision3d.materials.shadematerials.GouraudMaterial;   import org.papervision3d.materials.shaders.GouraudShader;   import org.papervision3d.materials.shaders.ShadedMaterial;   import org.papervision3d.materials.utils.MaterialsList;   import org.papervision3d.objects.DisplayObject3D;   import org.papervision3d.objects.parsers.DAE;   import org.papervision3d.view.BasicView;   public class Example009b extends BasicView {       [Embed(source="/../assets/cow.dae", mimeType="application/octet-stream")] private var CowDAE:Class;     [Embed(source="/../assets/Cow.png")] private var CowBitmapImage:Class;     private var light:PointLight3D;         private var shadedMaterialCow:DAE;     private var gouraudCow:DAE;     private var texturedCow:DAE;     private var allCows:DisplayObject3D;         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 fpsText:TextField;     private var textFormat:TextFormat;       private var frames:Number = 0;     private var lastTimeMS:Number = 0;       private var doSimple:Boolean = false;       public function Example009b() {       super(0, 0, true, true);             // Initialise Papervision3D       init3D();             // Create the 3D objects       createScene();       // create the frame rate counter label       createFPSLabel();       // Listen to mouse up and down events on the stage       stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);       stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);       // Start rendering the scene       startRendering();     }         private function init3D():void {       // position the camera       camera.z = -500;       camera.fov = 60;       camera.orbit(cameraPitch, cameraYaw);     }         private function createFPSLabel():void {       // create text and format to display current fps       textFormat = new TextFormat();       textFormat.size = 20;       textFormat.font = "Arial";             fpsText = new TextField();       fpsText.x = 50;       fpsText.y = 50;       fpsText.textColor = 0xFFFFFF;       fpsText.text = "";       fpsText.setTextFormat(textFormat);       fpsText.autoSize = TextFieldAutoSize.LEFT;             stage.addChild(fpsText);     }     private function createScene():void {       // Specify a point light source and its location       light = new PointLight3D(true);       light.x = 500;       light.y = 300;       light.z = -500;       scene.addChild(light);       // create a display object to group all created cows       allCows = new DisplayObject3D();       scene.addChild(allCows);       // create a cow with a shaded material       createSimpleShadedDAE();             // create a shaded cow by blending two different rendered objects       createNiceShadedDAE();     }     private function createSimpleShadedDAE():void {       // create BitmapMaterial from texture map       var cowBitmapMaterial:BitmapMaterial = new MovieMaterial(new CowBitmapImage(), true);         // create a ShadedMaterial using a Gouraud shader       var shader:GouraudShader = new GouraudShader(light, 0xFFFFFF, 0x333333);       var shadedMaterial:ShadedMaterial = new ShadedMaterial(cowBitmapMaterial, shader);       shadedMaterial.interactive = true;             // Material list linked to material symbol name in dae       var mainMaterials:MaterialsList = new MaterialsList();       mainMaterials.addMaterial(shadedMaterial, "mat0");       // create a new dae and perform actions when loaded       shadedMaterialCow = new DAE(false);       shadedMaterialCow.addEventListener(FileLoadEvent.LOAD_COMPLETE, function onLoad(event:Event):void {         shadedMaterialCow.moveDown(100);         shadedMaterialCow.scale = 100;                 // add cow to scene when loaded         allCows.addChild(shadedMaterialCow);                 // recursively add event listeners to dae and all children         addEventListeners(shadedMaterialCow, InteractiveScene3DEvent.OBJECT_CLICK, toggleRendering);       });             // load the dae from the embedded structure and replace the materials       shadedMaterialCow.load(new XML(new CowDAE()), mainMaterials);     }         private function createNiceShadedDAE():void {       // create a simple texture mapped material for the embedded png       var cowBitmapMaterial:BitmapMaterial = new MovieMaterial(new CowBitmapImage(), true);       cowBitmapMaterial.interactive = true;             // add the material to a material list corresponding to the dae       var bitmapMaterials:MaterialsList = new MaterialsList();       bitmapMaterials.addMaterial(cowBitmapMaterial, "mat0");       // create a new dae and perform actions when loaded       texturedCow = new DAE(false);       texturedCow.addEventListener(FileLoadEvent.LOAD_COMPLETE, function onLoad(event:Event):void {         texturedCow.moveDown(100);         texturedCow.scale = 100;         // set the dae to initially not be visible         texturedCow.visible = false;                 // add to the scene         allCows.addChild(texturedCow);                 // listen to events (applies to all children of dae as well)         addEventListeners(texturedCow, InteractiveScene3DEvent.OBJECT_CLICK, toggleRendering);               });       // load the dae from the embedded structure and replace the materials       texturedCow.load(new XML(new CowDAE()), bitmapMaterials);       // create a simple Gouraud shaded material and add to list corresponding to dae       var gouraudMaterial:GouraudMaterial = new GouraudMaterial(light, 0xFFFFFF, 0x333333);       var shadedMaterials:MaterialsList = new MaterialsList();       shadedMaterials.addMaterial(gouraudMaterial, "mat0");       // create a new dae and perform actions when loaded       gouraudCow = new DAE(false);       gouraudCow.addEventListener(FileLoadEvent.LOAD_COMPLETE, function onLoad(event:Event):void {         gouraudCow.scale = 100;         gouraudCow.moveDown(100);         // set the dae to initially not be visible         gouraudCow.visible = false;           // add to the scene         allCows.addChild(gouraudCow);         // change the rendering so that it is blended with other rendered objects         viewport.getChildLayer(gouraudCow).blendMode = BlendMode.MULTIPLY;        });             // load the dae from the embedded structure and replace the materials       gouraudCow.load(new XML(new CowDAE()), shadedMaterials);     }     // used to ensure that all children in a dae listen to events     private function addEventListeners(displayObject:DisplayObject3D, eventType:String, listener:Function):void {       // add listener to DisplayObect       displayObject.addEventListener(eventType, listener);             // add listener to all contained childred       for each(var child:DisplayObject3D in displayObject.children) {         addEventListeners(child, eventType, listener);       }     }         // toggles between the two rendering techniques     private function toggleRendering(event:InteractiveScene3DEvent):void {       texturedCow.visible = !texturedCow.visible;       gouraudCow.visible = !gouraudCow.visible;       shadedMaterialCow.visible = !shadedMaterialCow.visible;     }     override protected function onRenderTick(event:Event=null):void {             // rotate the scene       allCows.yaw(-1);             // calculate the frame rate       calculateFrameRate();       // update the camera position       updateCamera();           // call the renderer       super.onRenderTick(event);     }         private function calculateFrameRate():void {       // calculate the time elapsed since the last calculation            var currentTimeMS:Number = getTimer();       var elapsedTimeMS:Number = currentTimeMS - lastTimeMS;       // if a second has elapsed then calculate the fps       if (elapsedTimeMS >= 1000) {         fpsText.text = frames.toString() + " fps";         fpsText.setTextFormat(textFormat);                 // reset the counter         lastTimeMS = currentTimeMS;         frames = 0;             } else {         // increment the counter         frames++;       }           }         private function updateCamera():void {             // 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);       }           }     // 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;     }       } }

The resulting Flash animation can be seen below by clicking on the image. You can rotate the scene by clicking and moving the mouse. You can also switch between the two rendering techniques by clicking on the cow. To show that there is not a huge difference in performance between the two techniques, a frame-rate meter is shown in the top-left hand corner.

So again, lets take a closer look at the code. Starting with the ShadedMaterial version that produces the problems. This is created in the createSimpleShadedDAE function.

    private function createSimpleShadedDAE():void {       // create BitmapMaterial from texture map       var cowBitmapMaterial:BitmapMaterial = new MovieMaterial(new CowBitmapImage(), true);         // create a ShadedMaterial using a Gouraud shader       var shader:GouraudShader = new GouraudShader(light, 0xFFFFFF, 0x333333);       var shadedMaterial:ShadedMaterial = new ShadedMaterial(cowBitmapMaterial, shader);       shadedMaterial.interactive = true;             // Material list linked to material symbol name in dae       var mainMaterials:MaterialsList = new MaterialsList();       mainMaterials.addMaterial(shadedMaterial, "mat0");       // create a new dae and perform actions when loaded       shadedMaterialCow = new DAE(false);       shadedMaterialCow.addEventListener(FileLoadEvent.LOAD_COMPLETE, function onLoad(event:Event):void {         shadedMaterialCow.moveDown(100);         shadedMaterialCow.scale = 100;                 // add cow to scene when loaded         allCows.addChild(shadedMaterialCow);                 // recursively add event listeners to dae and all children         addEventListeners(shadedMaterialCow, InteractiveScene3DEvent.OBJECT_CLICK, toggleRendering);       });             // load the dae from the embedded structure and replace the materials       shadedMaterialCow.load(new XML(new CowDAE()), mainMaterials);     }

As we’ve seen in previous posts, to create a shaded bitmap material we combine a BitmapMaterial with a Shader. Here we use the embedded bitmap data of the texture map combined with a simple Gouraud shader. These are combined in a ShadedMaterial which is then added to the material list as we did above for the first part of this post. I’m using a DAE object to load the Collada data which is then combined with material data.

What you’ll notice however is that the interactivity is added in a different manner from previous posts. For Collada objects we need to add the event listener to all of the children of the DAE as well as the DAE itself - see this post on the Papervision3D newsgroup for details. To perform this, I’ve added (as mentioned in the post) a small routine to recursively add the listener to all children.

    // used to ensure that all children in a dae listen to events     private function addEventListeners(displayObject:DisplayObject3D, eventType:String, listener:Function):void {       // add listener to DisplayObect       displayObject.addEventListener(eventType, listener);             // add listener to all contained childred       for each(var child:DisplayObject3D in displayObject.children) {         addEventListeners(child, eventType, listener);       }     }

So, as you can see from the Flash animation, this method doesn’t work - yet! So, now for the fix provided by Andy Zupko. You’ll find his original post on shadow casting very interesting. The idea is that we take advantage of the Flash architecture to blend 2D objects together. To do this we render the DAE twice: once with a texture map but no shading and another time with no texture map and simple shading. Each render produces a 2D image (what is seen on the screen). These images can be superimposed and blended so that the resulting image is a shaded texture map.

The overhead of drawing the same 3D object twice seems to be fairly small (as you can see from the fps counter in the demo). This is not really surprising since the ShadedMaterial method also does two passes: each triangle is initially rendered with a texture map and then again with a shader.

Lets look at the code in createNiceShadedDAE

    private function createNiceShadedDAE():void {       // create a simple texture mapped material for the embedded png       var cowBitmapMaterial:BitmapMaterial = new MovieMaterial(new CowBitmapImage(), true);       cowBitmapMaterial.interactive = true;             // add the material to a material list corresponding to the dae       var bitmapMaterials:MaterialsList = new MaterialsList();       bitmapMaterials.addMaterial(cowBitmapMaterial, "mat0");       // create a new dae and perform actions when loaded       texturedCow = new DAE(false);       texturedCow.addEventListener(FileLoadEvent.LOAD_COMPLETE, function onLoad(event:Event):void {         texturedCow.moveDown(100);         texturedCow.scale = 100;         // set the dae to initially not be visible         texturedCow.visible = false;                 // add to the scene         allCows.addChild(texturedCow);                 // listen to events (applies to all children of dae as well)         addEventListeners(texturedCow, InteractiveScene3DEvent.OBJECT_CLICK, toggleRendering);               });       // load the dae from the embedded structure and replace the materials       texturedCow.load(new XML(new CowDAE()), bitmapMaterials);       // create a simple Gouraud shaded material and add to list corresponding to dae       var gouraudMaterial:GouraudMaterial = new GouraudMaterial(light, 0xFFFFFF, 0x333333);       var shadedMaterials:MaterialsList = new MaterialsList();       shadedMaterials.addMaterial(gouraudMaterial, "mat0");       // create a new dae and perform actions when loaded       gouraudCow = new DAE(false);       gouraudCow.addEventListener(FileLoadEvent.LOAD_COMPLETE, function onLoad(event:Event):void {         gouraudCow.scale = 100;         gouraudCow.moveDown(100);         // set the dae to initially not be visible         gouraudCow.visible = false;           // add to the scene         allCows.addChild(gouraudCow);         // change the rendering so that it is blended with other rendered objects         viewport.getChildLayer(gouraudCow).blendMode = BlendMode.MULTIPLY;        });             // load the dae from the embedded structure and replace the materials       gouraudCow.load(new XML(new CowDAE()), shadedMaterials);     }

As you can see two DAEs are created: one as before with the embedded texture map data and another using a Gouraud shaded material. They are both initially set with visible = false. This means that they are not visible on screen and no rendering of them occurs. The Papervision3D event listeners are added to the first one (recursively for all DAE children too).

To ensure that the two are blended we just need to add the line viewport.getChildLayer(gouraudCow).blendMode = BlendMode.MULTIPLY to the Gouraud shaded DAE. The layer contains the 2D end result (or Sprite) of the render process for the DisplayObject3D (as it is seen on the screen) so by specifying BlendMode.MULTIPLY we are directly modifying the Flash characteristics of the Sprite. As you can see from the example this works very well which little, or no, change to the fps. As a little bit of further reading, I’d recommend another post by Andy Zupko where he gives more details on what can be achieved by modifying the layer characteristics and how this can create some greate effects.

Finally just to illustrate how the two models are switched, lets have a look at the event handler

    // toggles between the two rendering techniques     private function toggleRendering(event:InteractiveScene3DEvent):void {       texturedCow.visible = !texturedCow.visible;       gouraudCow.visible = !gouraudCow.visible;       shadedMaterialCow.visible = !shadedMaterialCow.visible;     }

Very simply, we set the different DAE models to visible = true or visible = false depending on which rendering technique we want to use. This is a very efficient way of adding and removing objects from a scene.

So just before ending this post, a couple of points about this technique. Firstly, this works fine for simple Gouraud, Flat, Cell or Phong shading, however how can bump mapping be included? Bump mapping requires a ShadedMaterial (see Part 7) but as we’ve seen this type of material doesn’t fully work yet for Collada objects. Secondly, if the Collada object is animated there needs to be synchronisation between the two rendered models: each frame has to be matched identically but the animation starts as soon as the model is loaded and each model can load in a different time. How can this synchronisation be achieved?

If you have comments or suggestions on these last couple of points, or indeed on any part of this post, then please don’t hesitate to add to the discussion below - I’d be very happy to hear of solutions and experiences!

Saturday, September 6th, 2008

First steps in Papervision3D : Part 8 - Movie materials

Previous articles summary :

This article continues investigating the wide range of materials available in Papervision3D and probably represents the last one of this theme. Whereas the previous examples tried to improve the realism of a 3D scene, this article takes a look at the more dynamic materials available with Papervision3D.

First, apologies for the length of this entry : actually, the more you look at what is available in Papervision3D the more you realise how much it offers! The aim here is to look at MovieMaterials. These offer the ability of having interactive and dynamic surfaces on 3D objects either with Flash movies or Flash video streams. Summarizing this to a few lines probably wouldn’t do it justice so I’ve tried to illustrate here some of the more exciting features offered… and even in doing so am probably still missing a lot!

Anyway, this article introduces two new kinds of materials: MovieMaterial and VideoStreamMaterial (which inherits from the former).

The MovieMaterial allows us to create a material using a pre-existing Flash movie (embedded in a Papervision3D movie) or simply from any MovieClip / Sprite inheriting class instance. Papervision3D provides mapping functions that allow us to interact with these Flash movies with mouse clicks and movements even in a 3D environment.

The VideoStreamMaterial, as its name implies, allows us to stream flash video streams (flv files) onto a 3D object.

The example shown in this article includes these three possibilities including: a flash video stream from a given URL, an embedded standard (non-3D) Flash movie and an example showing a Papervision3D scene being animated as a material in another Papervision3D scene… did I mention before that this article might be quite long?!

So, what we essentially have here are three Actionscript classes: the main 3D scene, a non-3D Sprite-inheriting class and another, secondary Papervision3D scene. I’m only going to discuss the first one here but I’ll include the source for the others at the end.

The main source code is shown below. The example shows the three movie materials projected onto a specific face of three projectors (Cube instances), all rotating about the y-axis. The projectors can be double-clicked to enlarge them, stop them from rotating and provide a more simple means of interacting with them. Double-clicking again puts them back with the others. The whole scene can be rotated by clicking on the background and moving the mouse. The code, as warned, is a little longer than hoped for, but we’ll look at each part in more details afterwards and really there’s nothing very complicated there. I’m using the Tweener library again to provide smoother visual effects (see Part 3 - animation - for more details).

package {     import caurina.transitions.Tweener;     import flash.display.MovieClip;   import flash.events.Event;   import flash.events.MouseEvent;   import flash.media.Video;   import flash.net.NetConnection;   import flash.net.NetStream;     import org.papervision3d.core.proto.MaterialObject3D;   import org.papervision3d.events.InteractiveScene3DEvent;   import org.papervision3d.lights.PointLight3D;   import org.papervision3d.materials.MovieMaterial;   import org.papervision3d.materials.VideoStreamMaterial;   import org.papervision3d.materials.shadematerials.FlatShadeMaterial;   import org.papervision3d.materials.utils.MaterialsList;   import org.papervision3d.objects.DisplayObject3D;   import org.papervision3d.objects.primitives.Cube;   import org.papervision3d.view.BasicView;   [SWF(backgroundColor="#222222")]   public class Example008 extends BasicView {       private static const ORBITAL_RADIUS:Number = 400;     [Embed(source="/../assets/DrawTool.swf")]     private var DrawTool:Class;     private var exampleMovie:MovieClip;     private var videoURL:String = "http://www.tartiflop.com/pv3d/FirstSteps/Radiohead_HOC.flv";     private var video:Video;     private var stream:NetStream;     private var connection:NetConnection;     private var objectGroup:DisplayObject3D;     private var light:PointLight3D;     private var currentActiveObject:DisplayObject3D = null;         private var projectors:Array = new Array();         private var doRotation:Boolean = false;     private var canRotate:Boolean = true;     private var lastMouseX:int;     private var lastMouseY:int;     private var cameraPitch:Number = 60;     private var cameraYaw:Number = -60;         public function Example008() {       super(0, 0, true, true);       // Initialise Papervision3D       init3D();       // create video stream for VideoStreamMaterial       createVideoStream();       // 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);       // Start rendering the scene       startRendering();     }         private function init3D():void {       // position the camera       camera.z = -1000;       camera.fov = 60;       camera.orbit(cameraPitch, cameraYaw);     }     private function createVideoStream():void {       // Create a NetConnection. 2-way connection not necessary: connect to null       connection = new NetConnection();       connection.connect(null);       // Create a new NetStream to obtain the flv stream. Ignore client messages so use a simple Object       stream = new NetStream(connection);       stream.client = new Object();             // create a new video player       video = new Video();             // start streaming the video from the given URL and play it on the video player       stream.play(videoURL);       video.attachNetStream(stream);     }     private function createScene():void {       // Specify a point light source and its location       light = new PointLight3D();       light.x = 400;       light.y = 1000;       light.z = -400;       // Create a 3D object to group the projectors       objectGroup = new DisplayObject3D();       // Create a new video stream material with precise rendering.       var videoMaterial:VideoStreamMaterial = new VideoStreamMaterial(video, stream, true);       addProjector(videoMaterial);                 // Create a new flash movie material from an actionscript class (not transparent, animated and precise rendering)       var movieMaterial1:MovieMaterial = new MovieMaterial(new Example006b(), false, true, true);       addProjector(movieMaterial1);       // Create a new flash movie material from an embedded flash movie (not transparent, animated and precise rendering)       var movieMaterial2:MovieMaterial = new MovieMaterial(new DrawTool(), false, true, true);       addProjector(movieMaterial2);           // add the object group and light       scene.addChild(objectGroup);       scene.addChild(light);       // set up the projector positions in the scene       organiseProjectors();     }         private function addProjector(material:MovieMaterial):void {       // materials are smooth rendred, interactive and resize to the 3D object.       material.smooth = true;       material.interactive = true;       material.allowAutoResize = true;       // simple flat shaded material as default for the projector       var flatShadedMaterial:MaterialObject3D = new FlatShadeMaterial(light, 0x554D33, 0x1A120C);       flatShadedMaterial.interactive = true;             // Material list with MovieMaterial used on the front, the rest being flat shaded       var materialList:MaterialsList = new MaterialsList({"all":flatShadedMaterial, "front":material});       // create a new interactive projector       var projector:Cube = new Cube(materialList, 320, 10, 240);       projector.addEventListener(InteractiveScene3DEvent.OBJECT_DOUBLE_CLICK, onMouseDoubleClickOnObject);       projector.addEventListener(InteractiveScene3DEvent.OBJECT_OVER, onMouseOverObject);       projector.addEventListener(InteractiveScene3DEvent.OBJECT_OUT, onMouseOutObject);       // add the projector to the scene, being part of the object group       objectGroup.addChild(projector);             // store projector in an array       projectors.push(projector);     }         private function organiseProjectors():void {       // calculate angle between projectors       var theta:Number = 360 / projectors.length;             // set up each projector so that they are distributed in a circle and facing outwards       for (var i:int = 0; i < projectors.length; i++) {         var projector:Cube = projectors[i];                 // specifc angle for projector         var angle:Number = i * theta - 180;         var angleRadians:Number = angle * 2 * Math.PI / 360.;         // position of projector         var x:Number = Math.sin(angleRadians) * ORBITAL_RADIUS;         var z:Number = Math.cos(angleRadians) * ORBITAL_RADIUS;         // create tween to position, rotate and scale projector smoothly over 1 second         Tweener.addTween(projector, {x:x, y:-150, z:z, rotationY:angle, scale:0.8, time:1, transition:"linear" });       }     }         override protected function onRenderTick(event:Event=null):void {       // rotate the object group: angle kept between 0 and 360 degrees       objectGroup.rotationY += 1;       if (objectGroup.rotationY > 360) {         objectGroup.rotationY -= 360;       }             // if an object is active (double clicked) rotate it in the opposite direction       // to the group so that it is stationary       if (currentActiveObject != null) {         currentActiveObject.rotationY -=1;         if (currentActiveObject.rotationY < 0) {           currentActiveObject.rotationY += 360;         }       }             // If the mouse button has been clicked then update the camera position            if (doRotation && canRotate) {         // 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 double clicked on a projector     private function onMouseDoubleClickOnObject(event:InteractiveScene3DEvent):void {       var object:DisplayObject3D = event.displayObject3D;             // determine if the object is to be activated (placed in center) or deactivated       if (object == currentActiveObject) {         deactivate(object);       } else {         activate(object);       }     }         // disable camera rotation when mouse is over a projector     private function onMouseOverObject(event:InteractiveScene3DEvent):void {       canRotate = false;     }         // re-enable camera rotation when mouse is out of a projector     private function onMouseOutObject(event:InteractiveScene3DEvent):void {       canRotate = true;     }         // places a projector in the center     private function activate(object:DisplayObject3D):void {       // remove projector from rotating projectors array       projectors.splice(projectors.indexOf(object), 1);             // if a projector is active already, put it back in the array of rotating projectors       if (currentActiveObject != null) {         projectors.push(currentActiveObject);       }             // create a tween to place selected projector in the center       Tweener.addTween(object, {y:100, x:0, z:0, rotationY:-objectGroup.rotationY, scale:2, time:1, transition:"linear" });       currentActiveObject = object;       // re-organise the other projectors       organiseProjectors();          }         // puts an activated projector back into the main pack of rotating projectors     private function deactivate(object:DisplayObject3D):void {       // put the projector back into the rotating projectors array       projectors.push(currentActiveObject);       currentActiveObject = null;            // re-organise all projectors       organiseProjectors();          }       } }

All of this provides the following Flash movie. As mentioned above, double-click on an object to activate it (this actually just means that the object is magnified and stops spinning - it doesn’t change any of the object characteristics). Double-click on an activated one to deactivate it (put it back with the others). Two projectors allow for user interactions at any point in time: you can draw on one and rotate the 3D scene on the other. The final projector streams House Of Cards by Radiohead (another Paperivision3D example?!). The whole scene can be rotated by clicking on the background and moving the mouse. Click on the image below to see it all in action.

So, as with the other articles in this series lets take a look at how the scene is constructed step-by-step. As usual, the code is organised in more of less the same way as previous examples. The main difference comes from creating and attaching a video stream and modifying the animation and object interaction.

Let’s start with the constructor. The only difference here is the initialisation of the video stream. If you take a look at the source code for the VideoStreamMaterial you’ll see that it takes two objects: a Video and a NetStream. These are pure Actionscript objects necessary for streaming the data and displaying it. The Flex language reference for NetConnection came in handy here to see what these objects do and how to create them. A slightly cut-down method is used here but it remains in principal the same.

    private function createVideoStream():void {       // Create a NetConnection. 2-way connection not necessary: connect to null       connection = new NetConnection();       connection.connect(null);       // Create a new NetStream to obtain the flv stream. Ignore client messages so use a simple Object       stream = new NetStream(connection);       stream.client = new Object();             // create a new video player       video = new Video();             // start streaming the video from the given URL and play it on the video player       stream.play(localVideoURL);       video.attachNetStream(stream);     }

As you can see in the example shown in the Flex livedocs, there are ways to listen to events occurring during the streaming but for this example I’ve just done a minimum to restrict the length of the code a bit.

Next we come to the scene creation. This again is based on previous examples so we have a light source, an object group to simplify the rotation of a number of objects and then the individual 3D objects, each one with a different MovieMaterial.

    private function createScene():void {       // Specify a point light source and its location       light = new PointLight3D();       light.x = 400;       light.y = 1000;       light.z = -400;       // Create a 3D object to group the projectors       objectGroup = new DisplayObject3D();       // Create a new video stream material with precise rendering.       var videoMaterial:VideoStreamMaterial = new VideoStreamMaterial(video, stream, true);       addProjector(videoMaterial);                 // Create a new flash movie material from an actionscript class (not transparent, animated and precise rendering)       var movieMaterial1:MovieMaterial = new MovieMaterial(new Example006b(), false, true, true);       addProjector(movieMaterial1);       // Create a new flash movie material from an embedded flash movie (not transparent, animated and precise rendering)       var movieMaterial2:MovieMaterial = new MovieMaterial(new DrawTool(), false, true, true);       addProjector(movieMaterial2);           // add the object group and light       scene.addChild(objectGroup);       scene.addChild(light);       // set up the projector positions in the scene       organiseProjectors();     }

As you can see the three MovieMaterials (VideoStreamMaterial inherits from this) are simple to create. Firstly the VideoStreamMaterial takes the Video and NetStream we created just before and I’ve chosen precise rendering to minimise perspective distortions. The other two MovieMaterials take in one case a Actionscript object and an embedded Flash movie in the other (see the start of the class definition to see the embedding, which is identical to how we embedded images in previous examples). The three boolean values are associated with transparent, animated and precise rendering arguments. So since the Flash movie objects are animated we need to specify true for the animated argument to ensure that the scenes are updated.

The scene is then populated with the object group (containing the 3D objects) and the light. The positioning of the 3D objects is delegated to the organiseProjectors function which we’ll come to shortly.

In this example I’m using the Cube primitive. Each one has a specific face (the “front”) showing the MovieMaterial and since each one has essentially the same characteristics I’ve factorised the code to initialise each one identically.

    private function addProjector(material:MovieMaterial):void {       // materials are smooth rendred, interactive and resize to the 3D object.       material.smooth = true;       material.interactive = true;       material.allowAutoResize = true;       // simple flat shaded material as default for the projector       var flatShadedMaterial:MaterialObject3D = new FlatShadeMaterial(light, 0x554D33, 0x1A120C);       flatShadedMaterial.interactive = true;             // Material list with MovieMaterial used on the front, the rest being flat shaded       var materialList:MaterialsList = new MaterialsList({"all":flatShadedMaterial, "front":material});       // create a new interactive projector       var projector:Cube = new Cube(materialList, 320, 10, 240);       projector.addEventListener(InteractiveScene3DEvent.OBJECT_DOUBLE_CLICK, onMouseDoubleClickOnObject);       projector.addEventListener(InteractiveScene3DEvent.OBJECT_OVER, onMouseOverObject);       projector.addEventListener(InteractiveScene3DEvent.OBJECT_OUT, onMouseOutObject);       // add the projector to the scene, being part of the object group       objectGroup.addChild(projector);             // store projector in an array       projectors.push(projector);     }

Each MovieMaterial is smoothed (to appear less pixelated), made interactive (so that the 3D object responds to mouse events) and auto-resized so that they resize automatically to the cube dimensions. The other five faces of the cube are covered in a simple flat-shaded material (as seen in Part 4) which is also interactive. The cube is then constructed with a MaterialList containing these two different materials. Event listeners are then added to the cube so that it responds to double-click events (to activate and deactivate it) and mouse over and out events which, as we’ll see later, are used to restrict the stage mouse listeners for rotating the scene (essentially they stop the scene from rotating when a user is interacting with one of the 3D objets).

The new cube is then added to the object group (so that it is rendered) and stored in an Array to allow us to access it later.

As you see in the example, the non-activated projectors are spaced evenly in a circle, facing outwards (the rotation comes simply from a rotation of the object group, handled separately). The function organiseProjectors performs the necessary calculations and animation.

    private function organiseProjectors():void {       // calculate angle between projectors       var theta:Number = 360 / projectors.length;             // set up each projector so that they are distributed in a circle and facing outwards       for (var i:int = 0; i < projectors.length; i++) {         var projector:Cube = projectors[i];                 // specifc angle for projector         var angle:Number = i * theta - 180;         var angleRadians:Number = angle * 2 * Math.PI / 360.;         // position of projector         var x:Number = Math.sin(angleRadians) * ORBITAL_RADIUS;         var z:Number = Math.cos(angleRadians) * ORBITAL_RADIUS;         // create tween to position, rotate and scale projector smoothly over 1 second         Tweener.addTween(projector, {x:x, y:-150, z:z, rotationY:angle, scale:0.8, time:1, transition:"linear" });       }     }

This function quite simply calculates the angle between each projector (Cube instance) and positions them in the x-z plane accordingly. To smoothly position each of them I’ve used a linear tween to modify the x, y, z, rotationY and scale properties of each of them, taking one second to animate. Thanks to Tweener this is very simple to perform!

Next we come to the onRenderTick function which updates the scene at every movie frame. This is essentially the same as for previous examples in the series

    override protected function onRenderTick(event:Event=null):void {       // rotate the object group: angle kept between 0 and 360 degrees       objectGroup.rotationY += 1;       if (objectGroup.rotationY > 360) {         objectGroup.rotationY -= 360;       }             // if an object is active (double clicked) rotate it in the opposite direction       // to the group so that it is stationary       if (currentActiveObject != null) {         currentActiveObject.rotationY -=1;         if (currentActiveObject.rotationY < 0) {           currentActiveObject.rotationY += 360;         }       }             // If the mouse button has been clicked then update the camera position            if (doRotation && canRotate) {         // 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);     }

The object group is rotated as in previous examples. This time I’m using the rotationY property rather than the function yaw to have a better control of the angle of rotation. The activated object is spun in the opposite direction at the same time so that it is effectively stationary.

The camera rotation is essentially the same as before except that we use the canRotate boolean value to restrict the rotation when the mouse is over a 3D object.

The rest of the code is essentially to handle the mouse events. The onMouseDown, onMouseUp are the same as before to initiate and stop the scene rotation. onMouseOverObject and onMouseOutObject add to this by limiting the rotation when the user is interacting with a 3D object.

To activate and deactivate a projector the double-click event on a 3D object is used.

    // called when mouse double clicked on a projector     private function onMouseDoubleClickOnObject(event:InteractiveScene3DEvent):void {       var object:DisplayObject3D = event.displayObject3D;             // determine if the object is to be activated (placed in center) or deactivated       if (object == currentActiveObject) {         deactivate(object);       } else {         activate(object);       }     }

Simply, if the object clicked is the current active object then we deactivate it. If not we activate it.

    // places a projector in the center     private function activate(object:DisplayObject3D):void {       // remove projector from rotating projectors array       projectors.splice(projectors.indexOf(object), 1);             // if a projector is active already, put it back in the array of rotating projectors       if (currentActiveObject != null) {         projectors.push(currentActiveObject);       }             // create a tween to place selected projector in the center       Tweener.addTween(object, {y:100, x:0, z:0, rotationY:-objectGroup.rotationY, scale:2, time:1, transition:"linear" });       currentActiveObject = object;       // re-organise the other projectors       organiseProjectors();          }

When activating a projector, it is removed from the Array of spinning projectors. If another projector is already activated then we put it back into this group. A simple linear tween is then used to reposition the newly activated projector in the center and to rescale it so that it is bigger than the others. We then recall organiseProjectors to reposition the remaining projectors around a circle.

Deactivating a projector simply involves putting it back into the Array and repositioning all of them around a circle.

    // puts an activated projector back into the main pack of rotating projectors     private function deactivate(object:DisplayObject3D):void {       // put the projector back into the rotating projectors array       projectors.push(currentActiveObject);       currentActiveObject = null;            // re-organise all projectors       organiseProjectors();          }

So that’s all there is to it! Really Papervision3D and Tweener do all the complicated work to display and animated the 3D scene: all that’s new here is the creation of the movie materials. Once again I hope this shows that Papervision3D is really very simple to use. Looking at the Papervision3D source code really helps a lot to understand how the materials are created and you’ll see that I haven’t covered everything but hopefully this gives a good starting point in creating your own 3D scenes with movie materials!

Just for completeness I’ve included below the source code for the animated movies used for the MovieMaterials. One is a simple 2D, standard Flash animation that reacts to mouse events. The other is based on a previous Papervision3D example shown in this series (from Part 6) but without the InteractiveScene3DEvent handlers. I found it really amazing that one 3D scene can be used as a material in another 3D scene - good work Papervision3D!

Here’s drawTool.as…

package {   import flash.display.Sprite;   import flash.events.MouseEvent;   import flash.text.TextField;   import flash.text.TextFieldAutoSize;   import flash.text.TextFormat;     public class DrawTool extends Sprite {     private var isDrawing:Boolean = false;     public function DrawTool() {       // create a drawing surface       graphics.beginFill(0xEEEEEE);       graphics.moveTo(0, 0);       graphics.lineTo(320, 0);       graphics.lineTo(320, 240);       graphics.lineTo(0, 240);       graphics.endFill();             // create text and format       var textFormat:TextFormat = new TextFormat();       textFormat.size = 30;       textFormat.font = "Arial";             var text:TextField = new TextField();       text.x = 50;       text.y = 100;       text.textColor = 0x222222;       text.text = "click to draw!";       text.setTextFormat(textFormat);       text.autoSize = TextFieldAutoSize.LEFT;       text.selectable = false;       addChild(text);             // listen to mouse events       this.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);       this.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);       this.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);           }         // start drawing circles     private function onMouseDown(event:MouseEvent):void {       isDrawing = true;       drawCircle(event.stageX, event.stageY);     }         // stop drawing circles     private function onMouseUp(event:MouseEvent):void {       isDrawing = false;     }         // draw a circle     private function onMouseMove(event:MouseEvent):void {       if (isDrawing) {         drawCircle(event.stageX, event.stageY);       }     }     // circle drawing function     private function drawCircle(x:int, y:int):void {       graphics.beginFill(Math.random() * 0xFFFFFF, 0.5);       graphics.drawCircle(x, y, 5);       graphics.endFill();     }       } }

… and finally Example006b.as :

package {     import flash.display.Bitmap;   import flash.display.Sprite;   import flash.events.Event;   import flash.events.MouseEvent;     import org.papervision3d.materials.BitmapMaterial;   import org.papervision3d.materials.utils.MaterialsList;   import org.papervision3d.objects.DisplayObject3D;   import org.papervision3d.objects.primitives.Cube;   import org.papervision3d.objects.primitives.Sphere;   import org.papervision3d.view.BasicView;   [SWF(backgroundColor="#FFFFFF")]   public class Example006b extends BasicView {       [Embed(source="/../assets/pv3d.png")] private var PV3D:Class;     private static const ORBITAL_RADIUS:Number = 100;       private var bitmap:Bitmap = new PV3D();     private var cube1:Cube;     private var cube2:Cube;     private var sphere1:Sphere;     private var sphere2:Sphere;     private var objectGroup:DisplayObject3D;         private var doRotation:Boolean = false;     private var lastMouseX:int;     private var lastMouseY:int;     private var cameraPitch:Number = 60;     private var cameraYaw:Number = -60;         public function Example006b() {       var background:Sprite = new Sprite();       background.graphics.beginFill(0x000000);       background.graphics.moveTo(0, 0);       background.graphics.lineTo(320, 0);       background.graphics.lineTo(320, 240);       background.graphics.lineTo(0, 240);       background.graphics.endFill();       addChild(background);             super(320, 240, true, false);       // Initialise Papervision3D       init3D();             // Create the 3D objects       createScene();       // Listen to mouse up and down events on the stage       background.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);       background.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);       background.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);       // Start rendering the scene       startRendering();     }         private function init3D():void {       // position the camera       camera.z = -500;       camera.orbit(cameraPitch, cameraYaw);     }     private function createScene():void {       // create interactive bitmap material       var bitmapMaterial:BitmapMaterial = new BitmapMaterial(bitmap.bitmapData, false);       // create an interactive tiled bitmap material (bitmap tiled as 2 x 2)       var tiledBitmapMaterial:BitmapMaterial = new BitmapMaterial(bitmap.bitmapData, false);       tiledBitmapMaterial.tiled = true;       tiledBitmapMaterial.maxU = 2;       tiledBitmapMaterial.maxV = 2;             // create cube with simple bitmap material       cube1 = new Cube(getBitmapMaterials(bitmapMaterial), 50, 50, 50);       cube1.x = ORBITAL_RADIUS;       // create cube with tiled bitmap material       cube2 = new Cube(getBitmapMaterials(tiledBitmapMaterial), 50, 50, 50);       cube2.x = -ORBITAL_RADIUS;         // create sphere with simple bitmap material       sphere1 = new Sphere(bitmapMaterial, 25, 10, 10);       sphere1.z = ORBITAL_RADIUS;       // create sphere with tiled bitmap material       sphere2 = new Sphere(tiledBitmapMaterial, 25, 10, 10);       sphere2.z = -ORBITAL_RADIUS;       // Create a 3D object to group the spheres       objectGroup = new DisplayObject3D();       objectGroup.addChild(cube1);       objectGroup.addChild(cube2);       objectGroup.addChild(sphere1);       objectGroup.addChild(sphere2);       // Add the light and spheres to the scene       scene.addChild(objectGroup);     }         private function getBitmapMaterials(bitmapMaterial:BitmapMaterial):MaterialsList {       // create list of materials for all faces of the cube,       // all with the same bitmap material       var materials:MaterialsList = new MaterialsList();       materials.addMaterial(bitmapMaterial, "all");             return materials;     }         override protected function onRenderTick(event:Event=null):void {       // rotate the objects       cube1.yaw(-3);       cube2.yaw(-3);       sphere1.yaw(-3);       sphere2.yaw(-3);             // rotate the group of objects       objectGroup.yaw(1);       // call the renderer       super.onRenderTick(event);     }     // called when mouse down on stage     public function onMouseDown(event:MouseEvent):void {       doRotation = true;       lastMouseX = event.stageX;       lastMouseY = event.stageY;     }     // called when mouse up on stage     public function onMouseUp(event:MouseEvent):void {       doRotation = false;     }         // called when the mouse moves over the stage     public function onMouseMove(event:MouseEvent):void {       // 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 = (event.stageY - lastMouseY) / 2;         var dYaw:Number = (event.stageX - 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 = event.stageX;         lastMouseY = event.stageY;                 // reposition the camera         camera.orbit(cameraPitch, cameraYaw);       }           }       } }

Next article:

Sunday, August 3rd, 2008

First steps in Papervision3D : Part 5 - scene interaction

Previous articles summary :

In this post, rather than adding more 3D rendering to the scene, I’d like to illustrate how Papervision3D provides us with a very simple interface so that users can interact with objects that we’ve created. As someone coming from an OpenGL background this is real magic ! Each triangle that we draw in a scene can listen to mouse events and call a specified function for example when we click on it or move the mouse over it.

The code sample that I show below is based on the previous post : four spheres with different shaded materials rotating about the origin. I’m now adding two different types of event listeners : a listener for standard Flash mouse events allowing the user can move around the scene by changing the camera position and a listener for Papervision3D InteractiveScene3DEvent events. The first of these is added to the stage, the second to individual DisplayObject3D objects.

To illustrate the interactions with the scene objects, I’m adding a third new element to code : tweens. Without having to calculate and specify the change in positions (for example) of objects within the code, a tween does all the hard work for us. A tween works on a particular property of an object and at each frame of an animation modifies this property in a particular manner. I’m not going to go into much detail here as its not the purpose of this post but you’ll find a lot of information on the web - here’s wikipedia’s definition for what its worth. The tweens I use here come from the opensource library Tweener which provides tweens for Actionscript2 and Actionscript3 as well as JavaScript and HaXE. To install it in your Eclipse/Flex Builder 3 environment follow the same guidelines that I gave with installing Papervision3D but rather than importing the source using SVN, simply download the zip file of the source from the Tweener homepage and extract it into the src folder of a new Tweener project. When its compiled link your own Actionscript project to the Tweener project.

Here’s the full code for the interactive scene and including the imports of the new Tweener library.

package {
 
  import caurina.transitions.Tweener;
 
  import flash.display.StageAlign;
  import flash.display.StageScaleMode;
  import flash.events.Event;
  import flash.events.MouseEvent;
 
  import org.papervision3d.core.proto.MaterialObject3D;
  import org.papervision3d.events.InteractiveScene3DEvent;
  import org.papervision3d.lights.PointLight3D;
  import org.papervision3d.materials.shadematerials.CellMaterial;
  import org.papervision3d.materials.shadematerials.FlatShadeMaterial;
  import org.papervision3d.materials.shadematerials.GouraudMaterial;
  import org.papervision3d.materials.shadematerials.PhongMaterial;
  import org.papervision3d.objects.DisplayObject3D;
  import org.papervision3d.objects.primitives.Sphere;
  import org.papervision3d.view.BasicView;

  public class Example005 extends BasicView {
 
    private static const ORBITAL_RADIUS:Number = 200;
   
    private var sphere1:Sphere;
    private var sphere2:Sphere;
    private var sphere3:Sphere;
    private var sphere4:Sphere;
    private var sphereGroup:DisplayObject3D;
    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;
   
    public function Example005() {
      // specify that we want the scene to be interactive
      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);
     
      // Start rendering the scene
      startRendering();
    }
   
    private function init3D():void {
      // position the camera
      camera.z = -500;
      camera.orbit(cameraPitch, cameraYaw);
    }

    private function createScene():void {
      // Specify a point light source and its location
      light = new PointLight3D(true);
      light.x = 400;
      light.y = 1000;
      light.z = -400;

      // Create a new material (flat shaded) and make it interactive
      var flatShadedMaterial:MaterialObject3D = new FlatShadeMaterial(light, 0x6654FF, 0x060433);
      flatShadedMaterial.interactive = true;
      sphere1 = new Sphere(flatShadedMaterial, 50, 10, 10);
      sphere1.x = -ORBITAL_RADIUS;

      // Create a new material (flat shaded) and make it interactive
      var gouraudMaterial:MaterialObject3D = new GouraudMaterial(light, 0x6654FF, 0x060433);
      gouraudMaterial.interactive = true;
      sphere2 = new Sphere(gouraudMaterial, 50, 10, 10);
      sphere2.x =  ORBITAL_RADIUS;

      // Create a new material (flat shaded) and make it interactive
      var phongMaterial:MaterialObject3D = new PhongMaterial(light, 0x6654FF, 0x060433, 150);
      phongMaterial.interactive = true;
      sphere3 = new Sphere(phongMaterial, 50, 10, 10);
      sphere3.z = -ORBITAL_RADIUS;

      // Create a new material (flat shaded) and make it interactive
      var cellMaterial:MaterialObject3D = new CellMaterial(light, 0x6654FF, 0x060433, 5);
      cellMaterial.interactive = true;
      sphere4 = new Sphere(cellMaterial, 50, 10, 10);
      sphere4.z =  ORBITAL_RADIUS;

      // Create a 3D object to group the spheres
      sphereGroup = new DisplayObject3D();
      sphereGroup.addChild(sphere1);
      sphereGroup.addChild(sphere2);
      sphereGroup.addChild(sphere3);
      sphereGroup.addChild(sphere4);

      // Add a listener to each of the spheres to listen to InteractiveScene3DEvent events
      sphere1.addEventListener(InteractiveScene3DEvent.OBJECT_CLICK, onMouseDownOnSphere);
      sphere2.addEventListener(InteractiveScene3DEvent.OBJECT_CLICK, onMouseDownOnSphere);
      sphere3.addEventListener(InteractiveScene3DEvent.OBJECT_CLICK, onMouseDownOnSphere);
      sphere4.addEventListener(InteractiveScene3DEvent.OBJECT_CLICK, onMouseDownOnSphere);

      // Add the light and spheres to the scene
      scene.addChild(sphereGroup);
      scene.addChild(light);
    }
   
    override protected function onRenderTick(event:Event=null):void {
      // rotate the spheres
      sphere1.yaw(-8);
      sphere2.yaw(-8);
      sphere3.yaw(-8);
      sphere4.yaw(-8);
     
      // rotate the group of spheres
      sphereGroup.yaw(3);

      // 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 onMouseDownOnSphere(event:InteractiveScene3DEvent):void {
      var object:DisplayObject3D = event.displayObject3D;
      Tweener.addTween(object, {y:200, time:1, transition:"easeOutSine", onComplete:function():void {goBack(object);} });
    }
   
    // called when a tween created in onMouseDownOnSphere has terminated
    private function goBack(object:DisplayObject3D):void {
      Tweener.addTween(object, {y:0, time:2, transition:"easeOutBounce"});
    }
  }
}

This produces the following Flash animation (click on image below) where we see the four spinning spheres rotating about the origin. You can rotate the scene by clicking and moving the mouse. You can interact with the spheres by clicking on them to make them jump.


Now lets look at the two different types of interaction that occur with this scene.

Stage interaction and modifying the camera position
The objective is to look at the scene from different angles by clicking the mouse button and moving the mouse. The interaction with the mouse is initialised by listening to the standard Flash event listeners. In the constructor we add the following lines :

      // Listen to mouse up and down events on the stage
      stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
      stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);

When a mouse click occurs on the stage, calls are made to onMouseDown and onMouseUp. These methods modify a boolean value to indicate that the user wants to translate the camera and initialises the position of the mouse, necessary to determine the amount of movement that occurs during a frame.

    // 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;
    }

The movement of the camera occurs by implementing code in the onRenderTick function which is called systematically at the beginning of every frame.

      // 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);
      }

The amount of horizontal and vertical movement of the mouse since the last frame (or the mouse down event was captured) are converted into angular displacements. These displacements are converted into a change in camera position. Rather than specifying and x, y and z values for the camera we are able to take advantage of its orbit function. The orbit takes two angles : yaw (or angle in the x-z plane) and pitch angle from the y axis which we limit to be between 0 and 180 degrees.

The orbit occurs around any given 3D object - in this case the origin as default - and has a particular distance from the object. This distance was initialised during the init3D method when we specified camera.z = -500. All subsequent orbital positions therefore have a distance of 500 from the origin. If you look into the code of the function orbit, you’ll see that these angles are subsequently converted into positional values for the camera.

After updating the camera position, the call to super.onRenderTick performs the necessary actions to render the scene at the modified viewing position.

Object interaction and tweening
The second type of interaction that we use takes advantage of Papervision3D’s power of detecting which 3D object is beneath the mouse. We need to explicitly indicate to Papervision3D that we want to have an interactive scene when we create the viewport and for each interactive material that we create, the interactions themselves are handled by adding listeners to the 3D objects.

First of all, to specify that we want an interactive scene we modify the call to the constructor of BasicView which is done by passing the value of true to the interactive scene argument (the second boolean shown below, the first being to indicate that we want the viewport to resize to the stage).

    public function Example005() {
      // specify that we want the scene to be interactive
      super(0, 0, true, true);

Secondly, each object that we want to be interactive must have an interactive material. This is done by setting the variable interactive to true for the materials that we have created. For example, the flat shaded sphere is as such :

      // Create a new material (flat shaded) and make it interactive
      var flatShadedMaterial:MaterialObject3D = new FlatShadeMaterial(light, 0x6654FF, 0x060433);
      flatShadedMaterial.interactive = true;
      sphere1 = new Sphere(flatShadedMaterial, 50, 10, 10);

Once the 3D objects have been created we add listeners to listen to Papervision3D interactive scene events, in this case when the user clicks on a sphere.

      // Add a listener to each of the spheres to listen to InteractiveScene3DEvent events
      sphere1.addEventListener(InteractiveScene3DEvent.OBJECT_CLICK, onMouseDownOnSphere);
      sphere2.addEventListener(InteractiveScene3DEvent.OBJECT_CLICK, onMouseDownOnSphere);
      sphere3.addEventListener(InteractiveScene3DEvent.OBJECT_CLICK, onMouseDownOnSphere);
      sphere4.addEventListener(InteractiveScene3DEvent.OBJECT_CLICK, onMouseDownOnSphere);

All of these objects use the same function to handle the event.

    // called when mouse down on a sphere
    private function onMouseDownOnSphere(event:InteractiveScene3DEvent):void {
      var object:DisplayObject3D = event.displayObject3D;
      Tweener.addTween(object, {y:200, time:1, transition:"easeOutSine", onComplete:function():void {goBack(object);} });
    }

The object that has been clicked on is sent as part of the InteractiveScene3DEvent data. Here we then see the implementation of Tweener.

Tweener contains static methods that allow us to modify object properties. In this case we simply modify the y position of the object so that it goes to a value of 200 in 1 second using the easeOutSine transition function. I’d recommend looking at the Tweener documentation on the different transition functions. The onComplete argument allows us to put the sphere back to its original position when the tween has completed.

    // called when a tween created in onMouseDownOnSphere has terminated
    private function goBack(object:DisplayObject3D):void {
      Tweener.addTween(object, {y:0, time:2, transition:"easeOutBounce"});
    }
  }

This then uses another tween with an easeOutBounce transition to move the sphere back to the x-z plane.

As you can see, adding user interactions to the scene is very easy. Papervision3D provides a very simple interface to modify both camera position and produce interactions with individual objects in the scene. What I’ve shown here is just is a simple introduction to object interaction : there are different types of interactions for example when an object is added to a scene, the mouse moves over the object or the object is moved. Look inside the source of InteractiveScene3DEvent to get an idea of all the possibilities. Hopefully this at least has given a taster of what can be produced. As always comments and feedback are very welcome !

Next article:

Saturday, August 2nd, 2008

First steps in Papervision3D : Part 4 - lighting and shading

Previous articles summary :

I’d like to illustrate in this post how we can add another essential ingredient to the scene : lighting. Lighting provides a much more realistic rendering of 3D objects and with Papervision3D is very simple to add. The example that I give here shows how lighting is added to a specific material. Papervision3D allows us to specify how the light interacts with the material and provides different qualities of rendering.

First its useful to have a brief summary of the commonly used terms used to describe lighting and shading in 3D graphics.

Typically objects are made up of a combination of all these lighting characteristics. Now, as I’ve said since the beginning, I’m a beginner in Papervision3D with an OpenGL background and am merely illustrating the progress I’ve personally made over the last few weeks. But, as far as I can tell, the basic shading models available combine ambient lighting with either diffuse or specular and all three is not possible. I’d be very happy if someone could correct me on this… In any case the shading available, produces some beautiful results as we can see in these examples :

You can also find here a useful article on textures, shading and materials which illustrates many techniques possible.

In this post I’d like to show simply how we can add a light source to a scene and implement the four basic types of shading on different materials. These are :

The code to illustrate these shading models is as follows :

package {
 
  import flash.display.StageAlign;
  import flash.display.StageScaleMode;
  import flash.events.Event;
 
  import org.papervision3d.core.proto.MaterialObject3D;
  import org.papervision3d.lights.PointLight3D;
  import org.papervision3d.materials.shadematerials.CellMaterial;
  import org.papervision3d.materials.shadematerials.FlatShadeMaterial;
  import org.papervision3d.materials.shadematerials.GouraudMaterial;
  import org.papervision3d.materials.shadematerials.PhongMaterial;
  import org.papervision3d.objects.DisplayObject3D;
  import org.papervision3d.objects.primitives.Sphere;
  import org.papervision3d.view.BasicView;

  public class Example004 extends BasicView {
 
    private static const ORBITAL_RADIUS:Number = 200;
   
    private var sphere1:Sphere;
    private var sphere2:Sphere;
    private var sphere3:Sphere;
    private var sphere4:Sphere;
    private var sphereGroup:DisplayObject3D;
   
    public function Example004() {
      super(0, 0, true, false);
     
      // set up the stage
      stage.align = StageAlign.TOP_LEFT;
      stage.scaleMode = StageScaleMode.NO_SCALE;

      // Initialise Papervision3D
      init3D();
     
      // Create the 3D objects
      createScene();
     
      // Start rendering the scene
      startRendering();
    }
   
    private function init3D():void {

      // position the camera
      camera.x = -200;
      camera.y =  200;
      camera.z = -500;
    }

    private function createScene():void {

      // Specify a point light source and its location
      var light:PointLight3D = new PointLight3D(true);
      light.x = 400;
      light.y = 1000;
      light.z = -400;

      // Create a new material (flat shaded) and apply it to a sphere
      var flatShadedMaterial:MaterialObject3D = new FlatShadeMaterial(light, 0x6654FF, 0x060433);
      sphere1 = new Sphere(flatShadedMaterial, 50, 10, 10);
      sphere1.x = -ORBITAL_RADIUS;

      // Create a new material (Gouraud shaded) and apply it to a sphere
      var gouraudMaterial:MaterialObject3D = new GouraudMaterial(light, 0x6654FF, 0x060433);
      sphere2 = new Sphere(gouraudMaterial, 50, 10, 10);
      sphere2.x =  ORBITAL_RADIUS;

      // Create a new material (Phong shaded) and apply it to a sphere
      var phongMaterial:MaterialObject3D = new PhongMaterial(light, 0x6654FF, 0x060433, 150);
      sphere3 = new Sphere(phongMaterial, 50, 10, 10);
      sphere3.z = -ORBITAL_RADIUS;

      // Create a new material (cell shaded) and apply it to a sphere
      var cellMaterial:MaterialObject3D = new CellMaterial(light, 0x6654FF, 0x060433, 5);
      sphere4 = new Sphere(cellMaterial, 50, 10, 10);
      sphere4.z =  ORBITAL_RADIUS;

      // Create a 3D object to group the spheres
      sphereGroup = new DisplayObject3D();
      sphereGroup.addChild(sphere1);
      sphereGroup.addChild(sphere2);
      sphereGroup.addChild(sphere3);
        sphereGroup.addChild(sphere4);

      // Add the light and spheres to the scene
      scene.addChild(sphereGroup);
      scene.addChild(light);
    }
   
    override protected function onRenderTick(event:Event=null):void {

      // rotate the spheres
      sphere1.yaw(-8);
      sphere2.yaw(-8);
      sphere3.yaw(-8);
      sphere4.yaw(-8);
     
      // rotate the group of spheres
      sphereGroup.yaw(3);
     
      // call the renderer
      super.onRenderTick(event);
    }

  }
}

This results in four rotating spheres, each one shaded differently and all rotating about the center of the display (click on image to launch animation).


This example uses the same code as in the previous post as a starting block. The initialisation of the 3D is identical : the viewport and the camera position are the same as before. The scene creation is however different to create the four spheres and include a light source.

Adding a light source is very simple in Papervision3D : all we need to do is create a point light source and give it a position.

      // Specify a point light source and its location
      var light:PointLight3D = new PointLight3D(true);
      light.x = 400;
      light.y = 1000;
      light.z = -400;

The value of true in the constructor simply indicates to Papervision3D that we want to artificially render an object representing the light source itself. In this example its not really necessary, but its useful as a debugging tool.

To use the light source we need to create materials that provide shading. The calculation of the colours that we see on the screen are made by these materials rather than the light itself. We can assume that the light source is white light : colours are specified with the materials.

      // Create a new material (flat shaded) and apply it to a sphere
      var flatShadedMaterial:MaterialObject3D = new FlatShadeMaterial(light, 0x6654FF, 0x060433);
      sphere1 = new Sphere(flatShadedMaterial, 50, 10, 10);
      sphere1.x = -ORBITAL_RADIUS;

      // Create a new material (Gouraud shaded) and apply it to a sphere
      var gouraudMaterial:MaterialObject3D = new GouraudMaterial(light, 0x6654FF, 0x060433);
      sphere2 = new Sphere(gouraudMaterial, 50, 10, 10);
      sphere2.x =  ORBITAL_RADIUS;

      // Create a new material (Phong shaded) and apply it to a sphere
      var phongMaterial:MaterialObject3D = new PhongMaterial(light, 0x6654FF, 0x060433, 150);
      sphere3 = new Sphere(phongMaterial, 50, 10, 10);
      sphere3.z = -ORBITAL_RADIUS;

      // Create a new material (cell shaded) and apply it to a sphere
      var cellMaterial:MaterialObject3D = new CellMaterial(light, 0x6654FF, 0x060433, 5);
      sphere4 = new Sphere(cellMaterial, 50, 10, 10);
      sphere4.z =  ORBITAL_RADIUS;

This code creates the four identical spheres positioned on the x-z plane, each one with a different material (being one of the four main shaded material types mentioned above). As you can see, each material takes a light as the first parameter. The two colours are then diffuse and ambient for FlatShaded and Gouraud, specular and ambient for Phong respectively. For the Cell shaded material the two colours are the two extremes which are then split into the n different values. The Phong material takes an additional parameter specifying the level of specular reflection being between 0 and 255 - higher numbers result in more point-like reflections.

Finally I’d like to mention the use of a DisplayObjet3D to group the spheres together :

      // Create a 3D object to group the spheres
      sphereGroup = new DisplayObject3D();
      sphereGroup.addChild(sphere1);
      sphereGroup.addChild(sphere2);
      sphereGroup.addChild(sphere3);
        sphereGroup.addChild(sphere4);

      // Add the light and spheres to the scene
      scene.addChild(sphereGroup);
      scene.addChild(light);

This object acts as a layer that can be manipulated as with any other 3D object. However this manipulation performs an action on all the child objects together as we’ll see when we look at the code for the animation. This object, as well as the light, are then added to the main scene to be rendered.

To animate the scene I wanted all the spheres to rotate about the origin. Rather than calculating the coordinates of each sphere (as I did in my last post), the sphereGroup object allows me to rotate all the spheres together : Papervision3D does all the hard work in calculating their coordinates.

      // rotate the spheres
      sphere1.yaw(-8);
      sphere2.yaw(-8);
      sphere3.yaw(-8);
      sphere4.yaw(-8);
     
      // rotate the group of spheres
      sphereGroup.yaw(3);

As with the previous example, each sphere is individually rotated on its axis. The rotation of all four about the origin now just takes a single line : sphereGroup.yaw(3) : much simpler to code than before !

So there you are ! This is really just a simple example of basic lighting and shading. You’ll find more complex examples on the web, for example including techniques such as bump mapping, to create more realistic scene lighting. Anyway, as always, I hope this has given a few insights into lighting with Papervision3D and please send any comments or suggestions as this article really shows only the tip of the iceberg !

Next article:

Thursday, July 31st, 2008

First steps in Papervision3D : Part 3 - animation

Previous articles summary :

Now that we are able to render simple 3D scenes, I’d like to add some animation. In the previous parts we saw that the initialisation of Papervision3D and the creation of the scene objects occurred in the constructor of our example classes. To add animation we need to update the scene at regular intervals which is done either by listening to the Event.FRAME_ENTER event as shown in Example001 or overloading the onRenderTick function of BasicView as shown in Example002 (which is in effect an encapsulation of the first option).

This example follows the code shown in Example002, so I’m using the BasicView.

package {
 
  import flash.display.StageAlign;
  import flash.display.StageScaleMode;
  import flash.events.Event;
 
  import org.papervision3d.core.geom.Lines3D;
  import org.papervision3d.core.geom.renderables.Line3D;
  import org.papervision3d.core.geom.renderables.Vertex3D;
  import org.papervision3d.core.proto.MaterialObject3D;
  import org.papervision3d.materials.WireframeMaterial;
  import org.papervision3d.materials.special.LineMaterial;
  import org.papervision3d.objects.primitives.Sphere;
  import org.papervision3d.view.BasicView;

  public class Example003 extends BasicView {
 
    private static const ORBITAL_RADIUS:Number = 200;
   
    private var sphere:Sphere;
    private var theta:Number = 0;
   
    public function Example003() {
      super(0, 0, true, false);
     
      // set up the stage
      stage.align = StageAlign.TOP_LEFT;
      stage.scaleMode = StageScaleMode.NO_SCALE;

      // Initialise Papervision3D
      init3D();
     
      // Create the 3D objects
      createScene();
     
      // Start rendering the scene
      startRendering();
    }
   
    private function init3D():void {

      // position the camera
      camera.x = -200;
      camera.y =  200;
      camera.z = -500;
    }

    private function createScene():void {

      // First object : a sphere
     
      // Create a new material for the sphere : simple white wireframe
      var sphereMaterial:MaterialObject3D = new WireframeMaterial(0xFFFFFF);

      // Create a new sphere object using wireframe material, radius 50 with
      //   10 horizontal and vertical segments
      sphere = new Sphere(sphereMaterial, 50, 10, 10);

      // Position the sphere (default = [0, 0, 0])
      sphere.x = -ORBITAL_RADIUS;

      // Second object : x-, y- and z-axis
     
      // Create a default line material and a Lines3D object (container for Line3D objects)
      var defaultMaterial:LineMaterial = new LineMaterial(0xFFFFFF);
      var axes:Lines3D = new Lines3D(defaultMaterial);

      // Create a different colour line material for each axis
      var xAxisMaterial:LineMaterial = new LineMaterial(0xFF0000);
      var yAxisMaterial:LineMaterial = new LineMaterial(0x00FF00);
      var zAxisMaterial:LineMaterial = new LineMaterial(0x0000FF);

      // Create a origin vertex
      var origin:Vertex3D = new Vertex3D(0, 0, 0);

      // Create a new line (length 100) for each axis using the different materials and a width of 2.
      var xAxis:Line3D = new Line3D(axes, xAxisMaterial, 2, origin, new Vertex3D(100, 0, 0));
      var yAxis:Line3D = new Line3D(axes, yAxisMaterial, 2, origin, new Vertex3D(0, 100, 0));
      var zAxis:Line3D = new Line3D(axes, zAxisMaterial, 2, origin, new Vertex3D(0, 0, 100));
     
      // Add lines to the Lines3D container
      axes.addLine(xAxis);
      axes.addLine(yAxis);
      axes.addLine(zAxis);

      // Add the sphere and the lines to the scene
      scene.addChild(sphere);
      scene.addChild(axes);
    }
   
    override protected function onRenderTick(event:Event=null):void {

      // rotate the sphere
      sphere.yaw(-4);
     
      // change the position of the sphere
      theta += 3;
      var x:Number = - Math.cos(theta * Math.PI / 180) * ORBITAL_RADIUS;
      var z:Number =   Math.sin(theta * Math.PI / 180) * ORBITAL_RADIUS;
      sphere.x = x;
      sphere.z = z;
     
      // call the renderer
      super.onRenderTick(event);
    }

  }
}

As you can see below, we see as before the sphere and the axes, but we have added animation so that the sphere rotates on its z-axis and orbits the origin (click on image to launch animation).


Compared to Example002 shown in Part 2, there are very few differences in the construction of the scene. The initialisation of the 3D is identical except for the position of the camera. The observer is now positioned above the horizontal plane looking down at the scene (the camera is still targeted on the origin). The construction of the 3D objects is also virtually identical : the only difference being the initial position of the sphere.

The real difference comes for overloading the onRenderTick function which is called at every FRAME_ENTER event :

    override protected function onRenderTick(event:Event=null):void {

      // rotate the sphere
      sphere.yaw(-4);
     
      // change the position of the sphere
      theta += 3;
      var x:Number = - Math.cos(theta * Math.PI / 180) * ORBITAL_RADIUS;
      var z:Number =   Math.sin(theta * Math.PI / 180) * ORBITAL_RADIUS;
      sphere.x = x;
      sphere.z = z;
     
      // call the renderer
      super.onRenderTick(event);
    }

As you can see, all that is necessary is to update the rotation and position of the sphere. The yaw function rotates a 3D object around its z-axis. A calculation is performed to convert an angle theta, that we increase at every frame, into x and z coordinates of the sphere. Note that we must call to super.onRenderTick(event) to display the updated scene.

A lot more can be performed at every frame - for example we could reposition the camera as well - but hopefully this gives a simple example showing how easy animation can be in Papervision3D!

Next article: