Nat King Cole Elton John Artist list Cobra Starship Used Free MP3 Whitney Houston Cat Stevens Phil Collins Ozzy Osbourne MP3 search Bob Dylan

Tag Archive for 'Tutorial'Page 2 of 2

Basic Mouse Interaction (v2)

basic-mouse-interaction

download-source

In the previous tutorial we showed you how to load a 3D model into Papervision. Now we are just going to show you how to rotate that model based off of your mouse movements.

The process is very simple. Since our render function is like the engine of our animations that is where we will add the code for the rotation. Later we'll cover animating objects via TweenLite / TweenMax.

Actionscript:
  1. override protected function onRenderTick(event:Event = null):void
  2. {
  3.     super.onRenderTick(event);
  4.     cow.rotationY = stage.mouseX / sceneWidth * 360
  5. }

When you first start moving objects in Papervision you may notice that rotationY and rotationX do not act as you might think they would. In flash you would think of something on a Y axis as going up and down and X as left and right. With Papervision RotationY rotates and object on its Y axis (left /right) and RotationX rotates an object on it's X axis (top / bottom).

Here is the full code:

Actionscript:
  1. package 
  2. {
  3.     import flash.events.Event;
  4.     import org.papervision3d.materials.BitmapFileMaterial;
  5.     import org.papervision3d.materials.utils.MaterialsList;
  6.     import org.papervision3d.objects.DisplayObject3D;
  7.     import org.papervision3d.objects.parsers.Collada;
  8.     import org.papervision3d.view.BasicView;
  9.  
  10.     public class Main extends BasicView
  11.     {
  12.         protected var cow:DisplayObject3D;
  13.         protected var materialList:MaterialsList;
  14.         protected var bitmapFileMaterial:BitmapFileMaterial;
  15.        
  16.         protected var sceneWidth:Number;
  17.         protected var sceneHeight:Number;
  18.        
  19.         public function Main()
  20.         {
  21.             super();
  22.             init();
  23.             createChildren();
  24.             startRendering();
  25.         }
  26.         protected function init():void
  27.         {
  28.             sceneWidth = stage.stageWidth
  29.         }
  30.         public function createChildren():void
  31.         {
  32.             //Setup the materials manually (sometimes the dae handles this without issue)
  33.             materialList       = new MaterialsList();
  34.             bitmapFileMaterial  = new BitmapFileMaterial("daeModel/Cow.png");
  35.             materialList.addMaterial(bitmapFileMaterial,"all");
  36.            
  37.             //Create the new Collada Object with materialList
  38.             cow = new Collada("daeModel/cow.dae",materialList);
  39.  
  40.             ///Set some properties
  41.             cow.moveDown(150);
  42.             cow.scale = 3;
  43.             cow.pitch( -10);
  44.  
  45.             //Add to scene
  46.             scene.addChild(cow);
  47.         }
  48.        
  49.         override protected function onRenderTick(event:Event = null):void
  50.         {
  51.             super.onRenderTick(event);
  52.             cow.rotationY = stage.mouseX / sceneWidth * 360
  53.         }
  54.     }
  55. }

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

Papervision 3D Controlling Top and Bottom Cylinder Materials

custom-materials-cylinder
While working on a project this morning I came across the need to have a different material applied to a primitive cylinder. After digging through the Cylinder source code I came across these lines

Actionscript:
  1. if (fBottomFace) aFace.push( new Triangle3D(this, [aP1, aP3, aP2], matInstance, [aP1uv, aP3uv, aP2uv]) );
  2.  
  3. //and
  4.  
  5. if (fTopFace) aFace.push( new Triangle3D(this, [aP1, aP2, aP3], matInstance, [aP1uv, aP2uv, aP3uv]));

Since matInstance is just a material it can easily be replaced. I was in a hurry so I decided to just directly modified the class; not the greatest idea but it worked.

Actionscript:
  1. if (j == 0) {
  2.     if(bottomMat)
  3.     {
  4.         if (fBottomFace) aFace.push( new Triangle3D(this, [aP1, aP3, aP2], bottomMat, [aP1uv, aP3uv, aP2uv]) );
  5.     }
  6.     else
  7.     {
  8.         if (fBottomFace) aFace.push( new Triangle3D(this, [aP1, aP3, aP2], matInstance, [aP1uv, aP3uv, aP2uv]) );
  9.     }
  10.    
  11. }
  12. else
  13. {
  14.    
  15.     if(topMat)
  16.     {
  17.         if (fTopFace) aFace.push( new Triangle3D(this, [aP1, aP2, aP3], topMat, [aP1uv, aP2uv, aP3uv]));
  18.    
  19.     }
  20.     else
  21.     {
  22.         if (fTopFace) aFace.push( new Triangle3D(this, [aP1, aP2, aP3], matInstance, [aP1uv, aP2uv, aP3uv]));
  23.     }
  24. }

Here is the new way to create the Cylinder:

Actionscript:
  1. var cyl:Cylinder = new Cylinder(new ColorMaterial(0x000000),200,900,5,20,200,true,true,new ColorMaterial(0x8ad51f),new ColorMaterial(0x00FF00));

Here is the full modified Papervision Cylinder.as class:

Actionscript:
  1. package org.papervision3d.objects.primitives {
  2.     import org.papervision3d.Papervision3D;
  3.     import org.papervision3d.core.geom.*;
  4.     import org.papervision3d.core.geom.renderables.Triangle3D;
  5.     import org.papervision3d.core.geom.renderables.Vertex3D;
  6.     import org.papervision3d.core.math.NumberUV;
  7.     import org.papervision3d.core.proto.*; 
  8.  
  9.     /**
  10.     * The Cylinder class lets you create and display Cylinders.
  11.     * <p/>
  12.     * The Cylinder is divided in vertical and horizontal segment, the smallest combination is two vertical and three horizontal segments.
  13.     */
  14.     public class Cylinder extends TriangleMesh3D
  15.     {
  16.         /**
  17.         * Number of segments horizontally. Defaults to 8.
  18.         */
  19.         public var segmentsW :Number;
  20.    
  21.         /**
  22.         * Number of segments vertically. Defaults to 6.
  23.         */
  24.         public var segmentsH :Number;
  25.    
  26.         /**
  27.         * Default radius of Cylinder if not defined.
  28.         */
  29.         static public const DEFAULT_RADIUS :Number = 100;
  30.    
  31.         /**
  32.         * Default height if not defined.
  33.         */
  34.         static public const DEFAULT_HEIGHT :Number = 100;
  35.    
  36.         /**
  37.         * Default scale of Cylinder texture if not defined.
  38.         */
  39.         static public const DEFAULT_SCALE :Number = 1;
  40.    
  41.         /**
  42.         * Default value of gridX if not defined.
  43.         */
  44.         static public const DEFAULT_SEGMENTSW :Number = 8;
  45.    
  46.         /**
  47.         * Default value of gridY if not defined.
  48.         */
  49.         static public const DEFAULT_SEGMENTSH :Number = 6;
  50.    
  51.         /**
  52.         * Minimum value of gridX.
  53.         */
  54.         static public const MIN_SEGMENTSW :Number = 3;
  55.    
  56.         /**
  57.         * Minimum value of gridY.
  58.         */
  59.         static public const MIN_SEGMENTSH :Number = 1;
  60.    
  61.         public var topFace
  62.         // ___________________________________________________________________________________________________
  63.         //                                                                                               N E W
  64.         // NN  NN EEEEEE WW    WW
  65.         // NNN NN EE     WW WW WW
  66.         // NNNNNN EEEE   WWWWWWWW
  67.         // NN NNN EE     WWW  WWW
  68.         // NN  NN EEEEEE WW    WW
  69.    
  70.         /**
  71.         * Create a new Cylinder object.
  72.         * <p/>
  73.         * @param    material    A MaterialObject3D object that contains the material properties of the object.
  74.         * <p/>
  75.         * @param    radius    [optional] - Desired radius.
  76.         * <p/>
  77.         * @param    segmentsW   [optional] - Number of segments horizontally. Defaults to 8.
  78.         * <p/>
  79.         * @param    segmentsH   [optional] - Number of segments vertically. Defaults to 6.
  80.         * <p/>
  81.         * @param    topRadius   [optional] - An optional parameter for con- or diverging cylinders.
  82.         * <p/>
  83.         * @param    topFace  [optional] - An optional parameter specifying if the top face of the cylinder should be drawn.
  84.         * <p/>
  85.         * @param    bottomFace  [optional] - An optional parameter specifying if the bottom face of the cylinder should be drawn.
  86.         * <p/>
  87.         */
  88.         public function Cylinder( material:MaterialObject3D=null, radius:Number=100, height:Number=100, segmentsW:int=8, segmentsH:int=6, topRadius:Number=-1, topFace:Boolean=true, bottomFace:Boolean=true,topMat:MaterialObject3D = null ,bottomMat:MaterialObject3D = null)
  89.         {
  90.             super( material, new Array(), new Array(), null );
  91.    
  92.             this.segmentsW = Math.max( MIN_SEGMENTSW, segmentsW || DEFAULT_SEGMENTSW); // Defaults to 8
  93.             this.segmentsH = Math.max( MIN_SEGMENTSH, segmentsH || DEFAULT_SEGMENTSH); // Defaults to 6
  94.             if (radius==0) radius = DEFAULT_RADIUS; // Defaults to 100
  95.             if (height==0) height = DEFAULT_HEIGHT; // Defaults to 100
  96.             if (topRadius==-1) topRadius = radius;
  97.    
  98.             var scale :Number = DEFAULT_SCALE;
  99.    
  100.             buildCylinder( radius, height, topRadius, topFace, bottomFace,topMat,bottomMat );
  101.         }
  102.    
  103.         private function buildCylinder( fRadius:Number, fHeight:Number, fTopRadius:Number, fTopFace:Boolean, fBottomFace:Boolean ,topMat:MaterialObject3D,bottomMat:MaterialObject3D):void
  104.         {
  105.             var matInstance:MaterialObject3D = material;
  106.            
  107.             var i:Number, j:Number, k:Number;
  108.    
  109.             var iHor:Number = Math.max(MIN_SEGMENTSW, this.segmentsW);
  110.             var iVer:Number = Math.max(MIN_SEGMENTSH, this.segmentsH);
  111.             var aVertice:Array = this.geometry.vertices;
  112.             var aFace:Array = this.geometry.faces;
  113.             var aVtc:Array = new Array();
  114.             for (j=0;j<(iVer+1);j++) { // vertical
  115.                 var fRad1:Number = Number(j/iVer);
  116.                 var fZ:Number = fHeight*(j/(iVer+0))-fHeight/2;//-fRadius*Math.cos(fRad1*Math.PI);
  117.                 var fRds:Number = fTopRadius+(fRadius-fTopRadius)*(1-j/(iVer));//*Math.sin(fRad1*Math.PI);
  118.                 var aRow:Array = new Array();
  119.                 var oVtx:Vertex3D;
  120.                 for (i=0;i<iHor;i++) { // horizontal
  121.                     var fRad2:Number = Number(2*i/iHor);
  122.                     var fX:Number = fRds*Math.sin(fRad2*Math.PI);
  123.                     var fY:Number = fRds*Math.cos(fRad2*Math.PI);
  124.                     //if (!((j==0||j==iVer)&&i>0)) { // top||bottom = 1 vertex
  125.                     oVtx = new Vertex3D(fY,fZ,fX);
  126.                     aVertice.push(oVtx);
  127.                     //}
  128.                     aRow.push(oVtx);
  129.                 }
  130.                 aVtc.push(aRow);
  131.             }
  132.             var iVerNum:int = aVtc.length;
  133.    
  134.             var aP4uv:NumberUV, aP1uv:NumberUV, aP2uv:NumberUV, aP3uv:NumberUV;
  135.             var aP1:Vertex3D, aP2:Vertex3D, aP3:Vertex3D, aP4:Vertex3D;
  136.    
  137.             for (j=0;j<iVerNum;j++) {
  138.                 var iHorNum:int = aVtc[j].length;
  139.                 for (i=0;i<iHorNum;i++) {
  140.                     if (j>0&&i>=0) {
  141.                         // select vertices
  142.                         var bEnd:Boolean = i==(iHorNum-0);
  143.                         aP1 = aVtc[j][bEnd?0:i];
  144.                         aP2 = aVtc[j][(i==0?iHorNum:i)-1];
  145.                         aP3 = aVtc[j-1][(i==0?iHorNum:i)-1];
  146.                         aP4 = aVtc[j-1][bEnd?0:i];
  147.                         // uv
  148.                         var fJ0:Number = j    / iVerNum;
  149.                         var fJ1:Number = (j-1)  / iVerNum;
  150.                         var fI0:Number = (i+1)  / iHorNum;
  151.                         var fI1:Number = i    / iHorNum;
  152.                         aP4uv = new NumberUV(fI0,fJ1);
  153.                         aP1uv = new NumberUV(fI0,fJ0);
  154.                         aP2uv = new NumberUV(fI1,fJ0);
  155.                         aP3uv = new NumberUV(fI1,fJ1);
  156.                         // 2 faces
  157.                         aFace.push( new Triangle3D(this, [aP1,aP2,aP3], matInstance, [aP1uv,aP2uv,aP3uv]) );
  158.                         aFace.push( new Triangle3D(this, [aP1,aP3,aP4], matInstance, [aP1uv,aP3uv,aP4uv]) );
  159.                     }
  160.                 }
  161.                 if (j==0||j==(iVerNum-1)) {
  162.                     for (i=0;i<(iHorNum-2);i++) {
  163.                         // uv
  164.                         var iI:int = Math.floor(i/2);
  165.                         aP1 = aVtc[j][iI];
  166.                         aP2 = (i%2==0)? (aVtc[j][iHorNum-2-iI]) : (aVtc[j][iI+1]);
  167.                         aP3 = (i%2==0)? (aVtc[j][iHorNum-1-iI]) : (aVtc[j][iHorNum-2-iI]);
  168.    
  169.                         var bTop:Boolean = j==0;
  170.                         aP1uv = new NumberUV( (bTop?1:0)+(bTop?-1:1)*(aP1.x/fRadius/2+.5), aP1.z/fRadius/2+.5 );
  171.                         aP2uv = new NumberUV( (bTop?1:0)+(bTop?-1:1)*(aP2.x/fRadius/2+.5), aP2.z/fRadius/2+.5 );
  172.                         aP3uv = new NumberUV( (bTop?1:0)+(bTop?-1:1)*(aP3.x/fRadius/2+.5), aP3.z/fRadius/2+.5 );
  173.    
  174.                         // face
  175.                         if (j == 0) {
  176.                             if(bottomMat)
  177.                             {
  178.                                 if (fBottomFace) aFace.push( new Triangle3D(this, [aP1, aP3, aP2], bottomMat, [aP1uv, aP3uv, aP2uv]) );
  179.                             }
  180.                             else
  181.                             {
  182.                                 if (fBottomFace) aFace.push( new Triangle3D(this, [aP1, aP3, aP2], matInstance, [aP1uv, aP3uv, aP2uv]) );
  183.                             }
  184.                            
  185.                         }
  186.                         else {
  187.                            
  188.                             if(topMat)
  189.                             {
  190.                                 if (fTopFace) aFace.push( new Triangle3D(this, [aP1, aP2, aP3], topMat, [aP1uv, aP2uv, aP3uv]));
  191.                            
  192.                             }
  193.                             else
  194.                             {
  195.                                 if (fTopFace) aFace.push( new Triangle3D(this, [aP1, aP2, aP3], matInstance, [aP1uv, aP2uv, aP3uv]));
  196.                             }
  197.                            
  198.                            
  199.                         }
  200.                     }
  201.                 }
  202.             }
  203.             this.geometry.ready = true;
  204.            
  205.             if(Papervision3D.useRIGHTHANDED)
  206.                 this.geometry.flipFaces();
  207.         }
  208.     }
  209. }

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

Papervision Double Sided Plane

Papervision double sided plane

Thanks to Luke I am the new proud owner of Papervision2.com. He is keeping busy and just ran out of time. I know he is sad to see it go, but glad that someone can take over and continue helping the Flash / Papervision community.

To introduce myself, I decided to take a tutorial from my site and add it to this new site.

The ole, doublesided plane. Lets get started.

Here are your options as we see it to create a double-sided plane.

Option 1:
Create two planes. Plane 1 with a z sorting of 1 and the other with 0 and the rotationY of one of those planes set to 180. You could then contain this in one display container.

Version 2:
Create one cube with a depth of 0. Then you just apply a material to the front and back. OK OK OK so this is not really a Plane... but I like this approach because it allows me to more easily use things like Bend Modifier.

If anyone has a better or more improved approach we would love to hear about it.

Here is what the code for Option 1:

Actionscript:
  1. package com.cs54.ui
  2. {
  3. import org.papervision3d.objects.primitives.Plane;
  4. import org.papervision3d.materials.MovieMaterial;
  5. import org.papervision3d.materials.utils.MaterialsList;
  6. import org.papervision3d.objects.DisplayObject3D;
  7. import com.everydayflash.pv3d.modifiers.Bend;
  8. import flash.display.DisplayObject;public class Tree2Planes extends DisplayObject3D
  9. {
  10. [Embed(source="/assets/tree-side-1.png")]
  11. public var TreeFront        :Class;[Embed(source="/assets/tree-side-2.png")]
  12. public var TreeBack            :Class;
  13.  
  14. protected var treeFront        :DisplayObject;
  15. protected var treeBack        :DisplayObject;
  16.  
  17. protected var treeFrontMat    :MovieMaterial;
  18. protected var treeBackMat    :MovieMaterial;
  19.  
  20. protected var treeMatList    :MaterialsList;
  21.  
  22. protected var _height        :Number = 175;
  23. protected var _width        :Number = 191;
  24. protected var _container    :DisplayObject3D;
  25.  
  26. protected var plane1        :Plane;
  27. protected var plane2        :Plane;
  28.  
  29. public function Tree2Planes():void
  30. {
  31. treeMatList     = new MaterialsList();
  32. treeFront        = new TreeFront();
  33. treeBack         = new TreeBack();
  34.  
  35. //Tree Materials
  36. treeFrontMat     = new MovieMaterial(treeFront, true);
  37. treeBackMat     = new MovieMaterial(treeBack, true);
  38.  
  39. treeFrontMat.allowAutoResize = false;
  40.  
  41. plane1        = new Plane(treeFrontMat,_width,_height);
  42. plane2        = new Plane(treeBackMat,_width,_height);
  43.  
  44. plane1.z = 1;
  45. plane1.rotationY = 180;
  46.  
  47. _container = new DisplayObject3D();
  48.  
  49. addChild(_container);
  50. _container.addChild(plane1);
  51. _container.addChild(plane2);
  52.  
  53. }
  54.  
  55. public function get height():Number
  56. {
  57. return _height;
  58. }
  59. public function get width():Number
  60. {
  61. return _width;
  62. }
  63.  
  64. }
  65.  
  66. }

Here is what the code for Option 2:

Actionscript:
  1. package com.cs54.ui
  2. {
  3. import org.papervision3d.materials.MovieMaterial;
  4. import org.papervision3d.materials.utils.MaterialsList;
  5. import org.papervision3d.objects.DisplayObject3D;
  6. import org.papervision3d.objects.primitives.Cube;
  7. import com.everydayflash.pv3d.modifiers.Bend;
  8. import flash.display.DisplayObject;
  9.  
  10. public class Tree extends DisplayObject3D
  11. {
  12. [Embed(source="/assets/tree-side-1.png")]
  13. public var TreeFront        :Class;
  14.  
  15. [Embed(source="/assets/tree-side-2.png")]
  16. public var TreeBack            :Class;
  17.  
  18. protected var treeFront        :DisplayObject;
  19. protected var treeBack        :DisplayObject;
  20.  
  21. protected var treeFrontMat    :MovieMaterial;
  22. protected var treeBackMat    :MovieMaterial;
  23.  
  24. protected var treeMatList    :MaterialsList;
  25.  
  26. protected var treeCube        :Cube;
  27. protected var _height        :Number = 175;
  28. protected var _width        :Number = 191;
  29. protected var _container    :DisplayObject3D;
  30.  
  31. public function Tree():void
  32. {
  33.  
  34. treeMatList     = new MaterialsList();
  35. treeFront        = new TreeFront();
  36. treeBack         = new TreeBack();
  37.  
  38. //Tree Materials
  39. treeFrontMat     = new MovieMaterial(treeFront, true);
  40. treeBackMat     = new MovieMaterial(treeBack, true);
  41.  
  42. treeFrontMat.allowAutoResize = false;
  43.  
  44. treeFrontMat.animated = true;
  45.  
  46. treeMatList.addMaterial( treeFrontMat, "front" );
  47. treeMatList.addMaterial( treeBackMat, "back" );
  48.  
  49. treeCube     = new Cube(treeMatList, _width, 0, _height, 6, 6, 6);
  50.  
  51. var bend:Bend = new Bend(treeCube);
  52.  
  53. bend.quickBend(1, .1);
  54.  
  55. addChild(treeCube);
  56.  
  57. }
  58.  
  59. public function get height():Number
  60. {
  61. return _height;
  62. }
  63. public function get width():Number
  64. {
  65. return _width;
  66. }
  67.  
  68. }
  69.  
  70. }

More to come in the weeks ahead. New papervision tutorials, full source, and support will all be provided.

Thanks for your continued support.

Charles Schulze,
CS54, LLC

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