Michael Jackson Pearl Jam Stevie Ray Vaughan Ozzy Osbourne Finntroll MP3 list A Fine Frenzy Capone Phil Collins Shakira Pink Floyd Keane

Tag Archive for 'Add new tag'

Advanced Interactivity

advancedinteractivity1

download-source

This is just another example of how to do some advanced interactivity in Papervision 3D. This tutorial is just a slightly updated version of the #10 : Advanced Interactivity tutorial from a while back.

Here is what has been updated:
1) Removed InteractiveScene3DEvent and replaced it with standard MouseEvents
2) Updated to use papervision's BasicView.as class

For a full description of what is going on please refer back to this link: #10 : Advanced Interactivity

Actionscript:
  1. package
  2. {
  3.     import flash.display.Sprite;
  4.     import flash.events.Event;
  5.     import flash.events.MouseEvent;
  6.     import flash.text.TextField;
  7.     import org.papervision3d.materials.ColorMaterial;
  8.     import org.papervision3d.materials.MovieMaterial;
  9.     import org.papervision3d.materials.utils.MaterialsList;
  10.     import org.papervision3d.objects.primitives.Cube;
  11.     import org.papervision3d.view.BasicView;
  12.    
  13.     /**
  14.      * ...
  15.      * @author Charlie Schulze, charlie[at]woveninteractive[dot]com
  16.      */
  17.    
  18.     public class Main extends BasicView
  19.     {
  20.         protected var cube:Cube;
  21.         protected var interactiveMats:Array;
  22.         protected var materialsList:MaterialsList;
  23.         protected var targetrotationX:Number;
  24.         protected var targetrotationY:Number;
  25.         protected var targetrotationZ:Number;
  26.         protected var tweening:Boolean;
  27.        
  28.         public function Main():void
  29.         {
  30.             super();
  31.             init();
  32.         }
  33.         protected function init():void
  34.         {
  35.             createChildren();
  36.             startRendering();
  37.         }
  38.         protected function createChildren():void
  39.         {
  40.             //Set the viewport to interactive
  41.             viewport.interactive = true;
  42.            
  43.             //Create Materials:
  44.             materialsList       = new MaterialsList();
  45.             interactiveMats         = ["front", "back", "left", "right", "bottom", "top"];
  46.             var colorsArray:Array   = [0x76b6f8, 0x4291e1, 0x1f73c8, 0xe77111, 0xe8914c, 0xfad2b2];
  47.            
  48.             for (var i:int = 0; i <interactiveMats.length; i++)
  49.             {
  50.                 //Create a color box so we can use our MouseEvents
  51.                 var colorBox:Sprite = new Sprite();
  52.                 colorBox.graphics.beginFill(colorsArray[i]);
  53.                 colorBox.graphics.drawRect(0, 0, 100, 100);
  54.                 colorBox.graphics.endFill();
  55.                 colorBox.name = interactiveMats[i];
  56.                
  57.                 //Add a textField for reference
  58.                 var textField:TextField = new TextField()
  59.                 colorBox.addChild(textField)
  60.                 textField.text = interactiveMats[i];
  61.                
  62.                 //Add a MouseEvent to the Sprite
  63.                 colorBox.mouseChildren = false;
  64.                 colorBox.addEventListener(MouseEvent.CLICK, onMovieMatClicked);
  65.                
  66.                 //Create the MovieMat
  67.                 var movieMat:MovieMaterial   = new MovieMaterial(colorBox, true, true);
  68.                 movieMat.interactive          = true;
  69.                 movieMat.smooth                 = true;
  70.                 materialsList.addMaterial(movieMat, interactiveMats[i]);
  71.             }
  72.            
  73.             //Create Cube
  74.             cube    = new Cube(materialsList, 100, 100, 100);
  75.            
  76.             //Add cube to the scene
  77.             scene.addChild(cube);
  78.         }
  79.        
  80.         protected function onMovieMatClicked(evt:MouseEvent):void
  81.         {
  82.             if (tweening)
  83.             {
  84.                 // Let it rotate again
  85.                 tweening = false;
  86.             }
  87.             else
  88.             {
  89.                 switch(evt.target.name) {
  90.                     case "front":
  91.                         targetrotationX = 0;
  92.                         targetrotationY = 180;
  93.                         targetrotationZ = 0;
  94.                         tweening = true;
  95.                     break;
  96.                     case "back":
  97.                         targetrotationX = 0;
  98.                         targetrotationY = 0;
  99.                         targetrotationZ = 0;
  100.                         tweening = true;
  101.                     break;
  102.                     case "left":
  103.                         targetrotationX = 0;
  104.                         targetrotationY = -90;
  105.                         targetrotationZ = 0;
  106.                         tweening = true;
  107.                     break;
  108.                     case "right":
  109.                         targetrotationX = 0;
  110.                         targetrotationY = 90;
  111.                         targetrotationZ = 0;
  112.                         tweening = true;
  113.                     break;
  114.                     case "top":
  115.                         targetrotationX = -90;
  116.                         targetrotationY = 0;
  117.                         targetrotationZ = 0;
  118.                         tweening = true;
  119.                     break;
  120.                     case "bottom":
  121.                         targetrotationX = 90;
  122.                         targetrotationY = 0;
  123.                         targetrotationZ = 180;
  124.                         tweening = true;
  125.                     break;
  126.                 }
  127.             }
  128.         }
  129.        
  130.         override protected function onRenderTick(event:Event = null):void
  131.         {
  132.             super.onRenderTick(event);
  133.            
  134.             if (tweening) {
  135.                 // If a face has been clicked
  136.                 if (camera.zoom <230) {
  137.                     // If the camera isn't zoomed enough then zoom in a bit more:
  138.                     camera.zoom += Math.sqrt(230-camera.zoom)/5;
  139.                 }
  140.                 // Test each rotation and rotate it towards the target rotation:
  141.                 // X axis:
  142.                 if (cube.rotationX <targetrotationX)
  143.                 {
  144.                     cube.rotationX += Math.sqrt(targetrotationX-cube.rotationX);
  145.                     cube.rotationX = Math.round(cube.rotationX);
  146.                 }
  147.                 else if (cube.rotationX> targetrotationX)
  148.                 {
  149.                     cube.rotationX -= Math.sqrt(cube.rotationX-targetrotationX);
  150.                     cube.rotationX = Math.round(cube.rotationX);
  151.                 }
  152.                
  153.                 // Y axis:
  154.                 if (cube.rotationY <targetrotationY)
  155.                 {
  156.                     cube.rotationY += Math.sqrt(targetrotationY-cube.rotationY);
  157.                     cube.rotationY = Math.round(cube.rotationY);
  158.                 }
  159.                 else if (cube.rotationY> targetrotationY)
  160.                 {
  161.                     cube.rotationY -= Math.sqrt(cube.rotationY-targetrotationY);
  162.                     cube.rotationY = Math.round(cube.rotationY);
  163.                 }
  164.                
  165.                 // Z axis:
  166.                 if (cube.rotationZ <targetrotationZ)
  167.                 {
  168.                     cube.rotationZ += Math.sqrt(targetrotationZ-cube.rotationZ);
  169.                     cube.rotationZ = Math.round(cube.rotationZ);
  170.                 }
  171.                 else if (cube.rotationZ> targetrotationZ)
  172.                 {
  173.                     cube.rotationZ -= Math.sqrt(cube.rotationZ-targetrotationZ);
  174.                     cube.rotationZ = Math.round(cube.rotationZ);
  175.                 }
  176.             }
  177.             else
  178.             {
  179.                 // If the camera is zoomed in, it shouldn't be now
  180.                 if (camera.zoom> 200)
  181.                 {
  182.                     // So zoom out a bit.
  183.                     camera.zoom -= Math.sqrt(camera.zoom-2)/5;
  184.                 }
  185.                 // Rotate the cube a bit:
  186.                 cube.rotationX += 2;
  187.                 cube.rotationY += 2;
  188.                 // Make sure that we dont "wind up" the rotation
  189.                 if (cube.rotationX>= 360) cube.rotationX = 0;
  190.                 if (cube.rotationY>= 360) cube.rotationY = 0;
  191.             }
  192.         }      
  193.     }
  194. }

download-source

Post to Twitter Post to Delicious Delicious Post to Digg Digg This Post Post to Facebook Facebook Post to StumbleUpon Stumble This Post

Basic Texturing (v2)

basic-texturing
download-source

Even if you had a chance to look at Getting started using BasicView.as this tutorial provides another example of basic texturing in Papervision.

In Papervision for the most part you have objects (planes, cubes, cones, 3D Models, etc) and then you have the textures or materials that you add to these objects. Think of it as getting a brand new iPhone and then buying a case or skin for that iPhone. We are essentially doing the same thing in principle - we have our object a cone (iPhone) and a material a bitmapMaterial (iPhone skin).

Actionscript:
  1. package 
  2. {
  3.     import flash.events.Event;
  4.     import org.papervision3d.view.BasicView;
  5.     import org.papervision3d.objects.primitives.Cone;
  6.     import org.papervision3d.materials.BitmapFileMaterial;
  7.    
  8.     public class Main extends BasicView
  9.     {
  10.         protected var cone:Cone
  11.         protected var bitmapMaterial:BitmapFileMaterial;
  12.        
  13.         public function Main()
  14.         {
  15.             super();
  16.             createChildren();
  17.             startRendering();
  18.         }
  19.         public function createChildren():void
  20.         {
  21.             //Create a new 3D object
  22.             bitmapMaterial = new BitmapFileMaterial("images/ourtex.jpg")
  23.             cone = new Cone(bitmapMaterial, 20, 200);
  24.            
  25.             //Set some properties
  26.             cone.scale = 4;
  27.             cone.pitch( -10);
  28.            
  29.             //Add to scene
  30.             scene.addChild(cone);
  31.         }
  32.        
  33.         override protected function onRenderTick(event:Event = null):void
  34.         {
  35.             super.onRenderTick(event);
  36.  
  37.             //Rotate
  38.             cone.yaw(3);
  39.         }
  40.     }
  41. }

As you can see from the code above we have a cone and a bitmap texture.

download-source

Post to Twitter Post to Delicious Delicious Post to Digg Digg This Post Post to Facebook Facebook Post to StumbleUpon Stumble This Post

Getting started using BasicView.as

basicview-image

download-source

In a previous tutorial (Creating a Papervision Base Template) we created a base Papervision template that we could re-use. Now however Papervision includes a form of this base template called BasicView.as

With just a few lines of code you're up and running. Here is the very simple / basic use of BasicView.as:

Actionscript:
  1. package
  2. {
  3.     import flash.events.Event;
  4.     import org.papervision3d.materials.ColorMaterial;
  5.     import org.papervision3d.objects.primitives.Plane;
  6.     import org.papervision3d.view.BasicView;
  7.    
  8.     /**
  9.      * ...
  10.      * @author Charlie Schulze, charlie[at]woveninteractive[dot]com
  11.      */
  12.    
  13.     public class Main extends BasicView
  14.     {
  15.         protected var plane:Plane;
  16.         protected var colorMaterial:ColorMaterial;
  17.        
  18.         public function Main():void
  19.         {
  20.             super();
  21.            
  22.             //Create one material and make it doublesided
  23.             colorMaterial = new ColorMaterial(0xFF0000, .5);
  24.             colorMaterial.doubleSided = true;
  25.            
  26.             //Create a plane using the colorMaterial
  27.             plane = new Plane(colorMaterial, 100, 100);
  28.             scene.addChild(plane);
  29.            
  30.             //Start the rendering
  31.             startRendering();
  32.         }
  33.        
  34.         override protected function onRenderTick(event:Event = null):void
  35.         {
  36.             super.onRenderTick(event);
  37.            
  38.             //Rotate the plane
  39.             plane.yaw(2);
  40.         }
  41.     }
  42. }

This very simple example you will just see a single light red plane rotating.

That is all you need to do to get started in Papervision. Test it out. Don't forget to add your 3D objects to the scene.

Actionscript:
  1. scene.addChild(plane);

Here is what the BasicView.as class looks like:

Actionscript:
  1. package org.papervision3d.view
  2. {
  3.     import org.papervision3d.cameras.SpringCamera3D;   
  4.     import org.papervision3d.cameras.Camera3D;
  5.     import org.papervision3d.cameras.CameraType;
  6.     import org.papervision3d.cameras.DebugCamera3D;
  7.     import org.papervision3d.core.view.IView;
  8.     import org.papervision3d.objects.DisplayObject3D;
  9.     import org.papervision3d.render.BasicRenderEngine;
  10.     import org.papervision3d.scenes.Scene3D;
  11.  
  12.     /**
  13.      * <p>
  14.      * BasicView provides a simple template for quickly setting up
  15.      * basic Papervision3D projects by creating a viewport, scene,
  16.      * camera, and renderer for you. Because BasicView is a subclass of
  17.      * Sprite, it can be added to any DisplayObject.
  18.      *
  19.      * </p>
  20.      *
  21.      * <p>
  22.      * <p>
  23.      * Example:
  24.      * </p>
  25.      * <pre><code>
  26.      * var width:Number = 640;
  27.      * var heigth:Number = 480;
  28.      * var scaleToStage:Boolean = true;
  29.      * var interactive:Boolean = true;
  30.      * var cameraType:String = Camera3D.TYPE;
  31.      *
  32.      * var myBasicView:BasicView = new BasicView(width, height, scaleToStage, interactive, cameraType);
  33.      * myDisplayObject.addChild(myBasicView);
  34.      * </code></pre>
  35.      * </p>
  36.      * @author Ralph Hauwert
  37.      */
  38.     public class BasicView extends AbstractView implements IView
  39.     {
  40.         /**
  41.          * @param viewportWidth  Width of the viewport
  42.          * @param viewportHeight    Height of the viewport
  43.          * @param scaleToStage    Whether you viewport should scale with the stage
  44.          * @param interactive      Whether your scene should be interactive
  45.          * @param cameraType        A String for the type of camera. @see org.papervision3d.cameras.CameraType
  46.          *
  47.          */ 
  48.         public function BasicView(viewportWidth:Number = 640, viewportHeight:Number = 480, scaleToStage:Boolean = true, interactive:Boolean = false, cameraType:String = "Target")
  49.         {
  50.             super();
  51.            
  52.             scene = new Scene3D();
  53.             viewport = new Viewport3D(viewportWidth, viewportHeight, scaleToStage, interactive);
  54.             addChild(viewport);
  55.             renderer = new BasicRenderEngine();
  56.            
  57.             switch(cameraType)
  58.             {
  59.                 case CameraType.DEBUG:
  60.                     _camera = new DebugCamera3D(viewport);
  61.                     break;
  62.                 case CameraType.TARGET:
  63.                     _camera = new Camera3D(60);
  64.                     _camera.target = DisplayObject3D.ZERO;
  65.                     break;
  66.                 case CameraType.SPRING:
  67.                     _camera = new SpringCamera3D();
  68.                     _camera.target = DisplayObject3D.ZERO;   
  69.                     break;     
  70.                 case CameraType.FREE:
  71.                 default:
  72.                     _camera = new Camera3D(60);
  73.                     break;
  74.             }
  75.            
  76.             cameraAsCamera3D.update(viewport.sizeRectangle);
  77.         }
  78.        
  79.         /**
  80.          * Exposes the camera as a <code>Camera3D</code>
  81.          */
  82.         public function get cameraAsCamera3D():Camera3D
  83.         {
  84.                 return _camera as Camera3D;
  85.         }
  86.        
  87.         /**
  88.          * Exposes the camera as a <code>DebugCamera3D</code>
  89.          */
  90.         public function get cameraAsDebugCamera3D():DebugCamera3D
  91.         {
  92.                 return _camera as DebugCamera3D;
  93.         }
  94.     }
  95. }

Since BasicView.as extends AbstractView.as you can see a few more of the methods available to you just by extending BasicView.as

Actionscript:
  1. package org.papervision3d.view
  2. {
  3.     import flash.display.Sprite;
  4.     import flash.events.Event;
  5.    
  6.     import org.papervision3d.core.proto.CameraObject3D;
  7.     import org.papervision3d.core.view.IView;
  8.     import org.papervision3d.render.BasicRenderEngine;
  9.     import org.papervision3d.scenes.Scene3D;
  10.    
  11.     /**
  12.      * @Author Ralph Hauwert
  13.      */
  14.     public class AbstractView extends Sprite implements IView
  15.     {
  16.         protected var _camera:CameraObject3D;
  17.         protected var _height:Number;
  18.         protected var _width:Number;
  19.        
  20.         public var scene:Scene3D;
  21.         public var viewport:Viewport3D;
  22.         public var renderer:BasicRenderEngine;
  23.        
  24.         public function AbstractView()
  25.         {
  26.             super();
  27.         }
  28.        
  29.         public function startRendering():void
  30.         {
  31.             addEventListener(Event.ENTER_FRAME, onRenderTick);
  32.             viewport.containerSprite.cacheAsBitmap = false;
  33.         }
  34.        
  35.         public function stopRendering(reRender:Boolean = false, cacheAsBitmap:Boolean = false):void
  36.         {
  37.             removeEventListener(Event.ENTER_FRAME, onRenderTick);
  38.             if(reRender){
  39.                 onRenderTick()
  40.             }
  41.             if(cacheAsBitmap){
  42.                 viewport.containerSprite.cacheAsBitmap = true;
  43.             }else{
  44.                 viewport.containerSprite.cacheAsBitmap = false;
  45.             }
  46.         }
  47.        
  48.         public function singleRender():void
  49.         {
  50.             onRenderTick();
  51.         }
  52.        
  53.         protected function onRenderTick(event:Event = null):void
  54.         {
  55.             renderer.renderScene(scene, _camera, viewport);
  56.         }
  57.        
  58.         public function get camera():CameraObject3D
  59.         {
  60.             return _camera;
  61.         }
  62.        
  63.         public function set viewportWidth(width:Number):void
  64.         {
  65.             _width = width;
  66.             viewport.width = width;
  67.         }
  68.        
  69.         public function get viewportWidth():Number
  70.         {
  71.             return _width;
  72.         }
  73.        
  74.         public function set viewportHeight(height:Number):void
  75.         {
  76.             _height = height;
  77.             viewport.height = height;
  78.         }
  79.        
  80.         public function get viewportHeight():Number
  81.         {
  82.             return _height;
  83.         }
  84.        
  85.     }
  86. }

download-source

Post to Twitter Post to Delicious Delicious Post to Digg Digg This Post Post to Facebook Facebook Post to StumbleUpon Stumble This Post

12. Animation using Tweener

**Update
This tutorial has been updated using TweenLite and the most recent papervision BasicView class.
view-update

Hi,

In this tutorial we're going to use Tweener to move a cube around. This will teach you basics of animation, and how to use Tweener (which is awesome).

The example that I've produced is this:

So, in this example our solid red cube is trying to stay inside our green wireframe cube. Every time the red cube gets itself positioned completley inside the green cube, the green cube moves.

Tweener is the package which is smoothly moving the red cube around. The Tweener package is also firing the event to move the green cube when the tween is complete.

1: To start off, you'll need Tweener. You can download it here if you don't already have it: http://code.google.com/p/tweener/

Once you've got tweener, either add the source directory containing the "caurina" folder to your project's classpaths, or add the caurina folder into your current project.

2: Import tweener - Add this line to your imports:

import caurina.transitions.Tweener;

3: Add a Tween. You can add tweens to any parameter of any object. The Tweener package will gradually change any parameter in your code to a new value over time, so only numeric values will work nicely. Anyway, you can read a load more about tweener at the google code page above.

To add a tween, we use the code

Tweener.addTween(object, {parameter1: value, parameter2: value2, time: X, 
transition:"transition", onComplete: functionname});

This may look a little bit confusing at first, but it's really simple. Let's show you a real example..

Tweener.addTween(cube, { x: 1000, z: 500, time: 2, onComplete: randomize } );

The line above will add a tween. This tween will change the values "x" and "y" on the object "cube" to 1000 and 500 respectively. It will smoothly change the values over a period of 2 seconds and once the tween is complete and the cube.x is 1000 and cube.y is 500, it will trigger the "randomize" function.

Experiment with more ways to tween your objects. You can apply tweens to anything, including cameras, lights, rotations, sizes and camera zooms. Any numerical value you can think of.

Here is my source code (Uses latest version of my base class):

Actionscript:
  1. package 
  2. {
  3.      import org.papervision3d.lights.PointLight3D;
  4.      import org.papervision3d.materials.shadematerials.FlatShadeMaterial;
  5.      import org.papervision3d.materials.utils.MaterialsList;
  6.      import org.papervision3d.materials.WireframeMaterial;
  7.      import org.papervision3d.objects.primitives.Cube;
  8.      import org.papervision3d.objects.primitives.Plane;
  9.  
  10.      import caurina.transitions.Tweener;
  11.  
  12.      public class Main extends PaperBase
  13.      {
  14.           private var cube:Cube;
  15.           private var cube2:Cube;
  16.           private var mat:FlatShadeMaterial = new FlatShadeMaterial(new PointLight3D(), 0xFFFFFF, 0xFF0000);
  17.           private var mat2:WireframeMaterial = new WireframeMaterial(0x00FF00);
  18.           private var plane:Plane = new Plane(null, 2000, 2000, 10, 10);
  19.  
  20.           public function Main()
  21.           {
  22.                init(600, 300);
  23.           }
  24.  
  25.           override protected function init3d():void
  26.           {
  27.                cube = new Cube(new MaterialsList( { all: mat } ), 100, 100, 100);
  28.                cube.y = 0;
  29.                cube2 = new Cube(new MaterialsList( { all: mat2 } ), 100, 100, 100);
  30.                cube2.y = 0;
  31.                cube2.x = 1000;
  32.                cube2.z = 1000;
  33.                plane.material.lineColor = 0x777777;
  34.                plane.material.doubleSided = true;
  35.                plane.pitch(90);
  36.                plane.y = -50;
  37.                default_scene.addChild(plane);
  38.                default_scene.addChild(cube);
  39.                default_scene.addChild(cube2);
  40.                Tweener.addTween(cube, { x:1000, z:1000, time:2, onComplete: randomize } );
  41.                default_camera.x = 0;
  42.                default_camera.z = 1000;
  43.                default_camera.y = 1000;
  44.           }
  45.  
  46.           public function randomize():void
  47.           {
  48.                var xp:Number = (Math.random() * 2000) - 1000;
  49.                var yp:Number = (Math.random() * 2000) - 1000;
  50.                Tweener.addTween(cube, { x:xp, z:yp, time:2, onComplete: randomize } );
  51.                cube2.x = xp;
  52.                cube2.z = yp;
  53.           }
  54.  
  55.           override protected function processFrame():void
  56.           {
  57.                default_camera.lookAt(cube);
  58.           }
  59.      }
  60. }

Post to Twitter Post to Delicious Delicious Post to Digg Digg This Post Post to Facebook Facebook Post to StumbleUpon Stumble This Post


Follow WovenCharlie on Twitter

Flash and the City banner
2010 Flash And The City Speaker

RSS Feed