Archive for the 'Source Code' Category

Full Source: Papervision Isometric View & Pathfinding with as3isolib and A* (A Star)

This week’s example / tutorial for papervision2.com is on how to mix Papervision with as3isolib and A* for a nice 3D isometric pathfinding experience.

View Tutorial Here

Full Source: Loading images via XML into a Papervision Coverflow

Papervision Coverflow

As a simple addition to the original coverflow post, showing the same example but with loading images via XML.

Click here to view the updated Papervision Coverflow Tutorial.

Simple Papervision Coverflow : Papervision2.com

Papervision Coverflow

There are plenty of AS3 Coverflow examples out there. Most of the examples and source I found had more options than I needed and all the extra code that comes with those features. Or you have to pay for them. I had a hard time finding a stripped down version to use as my coverflow base. Today I set out to create such a version.

Feel free to post any other coverflow examples that you have come across.

You can find the full sourceand some explanation on papervision2.com.

View the tutorial here.

Papervision Explode Image / Rebuild : Papervision2.com

I wanted to share another Papervision tutorial I wrote over at Papervision2.com. This is to replace my original post in 2008 about an exploding image. Full source included. Check it out.

Simple Papervision Carousel : Papervision2.com

Over at papervision2.com I just finished a posting for a very simple Papervision 3D Carousel. Check it out. Enjoy..

Charlie

Papervision Custom Primitive Source code for use with cool particle field effect.

Papervision Particle field

Carlos Lunetta was kind enough to send me his custom primitive and vertex particle classes for this blog entry. These are similar to the ones that he used for the Terra TV website.  In this example that concept is rather simple. You create a primitive object (Plane, Cube, Sphere) and run a for loop on the verticles.

As shown here:

Actionscript:
  1. for (var i:int = 0; i <disc.geometry.vertices.length; i++)
  2. {
  3. var discVert:Vertex3D = disc.geometry.vertices[i];
  4. }

Link to my example

Source Code

Now that you are looping through the verticles of this primitive you are now privy to information such as the x y and z locations of each point. So what can we do with that.....

One thing we can do is attach particles to it.. such as

Actionscript:
  1. for (var i:int = 0; i <disc.geometry.vertices.length; i++)
  2. {
  3. var discVert:Vertex3D = disc.geometry.vertices[i];
  4.  
  5. var vertexParticle:VertexParticle = new VertexParticle(particalMaterial, 5, discVert);
  6.  
  7. vertexParticle.x = discVert.x;
  8. vertexParticle.y = discVert.y;
  9. vertexParticle.z = discVert.z;
  10.  
  11. myParticles.addParticle(vertexParticle);
  12. }

Doing this lends itself to some neat particle field effects such as the one shown above. Of course you can mess around with the settings to optimize it for your needs.

Here is how you put it all together:

Actionscript:
  1. package com.cs54.papervision
  2. {
  3.  
  4. import flash.events.Event;
  5.  
  6. import org.papervision3d.core.geom.Particles;
  7. import org.papervision3d.core.geom.renderables.Particle;
  8. import org.papervision3d.core.geom.renderables.Vertex3D;
  9. import org.papervision3d.core.geom.Vertices3D;
  10. import org.papervision3d.materials.ColorMaterial;
  11. import org.papervision3d.materials.special.MovieAssetParticleMaterial;
  12. import org.papervision3d.materials.special.ParticleMaterial;
  13. import org.papervision3d.materials.utils.MaterialsList;
  14. import org.papervision3d.materials.WireframeMaterial;
  15. import org.papervision3d.objects.DisplayObject3D;
  16. import org.papervision3d.objects.primitives.Cone;
  17. import org.papervision3d.objects.primitives.Plane;
  18.  
  19. import src.primitives.Dome;
  20. import src.primitives.Disc;
  21. import src.primitives.VertexParticle;
  22.  
  23. public class PV3DScene extends PaperBase
  24. {
  25.  
  26. protected var displayContainer    :DisplayObject3D;
  27. protected var particle            :Particle
  28. protected var myParticles        :Particles;
  29. protected var domeParticles        :Particles;
  30. protected var partMat            :ParticleMaterial;
  31. protected var colorMaterial        :ColorMaterial;
  32. protected var disc                :Disc;
  33. protected var particalMaterial    :ParticleMaterial;
  34. protected var particalMaterial2    :ParticleMaterial;
  35. protected var container            :DisplayObject3D;
  36. protected var domeContainer        :DisplayObject3D;
  37. protected var dome                :Dome;
  38.  
  39. override public function init(vpWidth:Number = 800, vpHeight:Number = 600):void
  40. {
  41. super.init(vpWidth, vpHeight);
  42. default_camera.y = 800;
  43. default_camera.rotationX = 20;
  44. }
  45.  
  46. override protected function createChildren3D():void
  47. {
  48. super.createChildren3D();
  49.  
  50. container = new DisplayObject3D();
  51. domeContainer = new DisplayObject3D();
  52.  
  53. default_scene.addChild(container);
  54. default_scene.addChild(domeContainer);
  55.  
  56. default_camera.zoom                 = 50;
  57. default_camera.z                     = -3000
  58. colorMaterial                         = new ColorMaterial(0xFF0000, 1, true);
  59. colorMaterial.doubleSided             = true;
  60. particalMaterial                     = new ParticleMaterial(0xFF00CC, 100, ParticleMaterial.SHAPE_CIRCLE);
  61. particalMaterial2                     = new ParticleMaterial(0x000000, 100, ParticleMaterial.SHAPE_CIRCLE);
  62. myParticles                         = new Particles();
  63. domeParticles                        = new Particles();
  64. disc                                 = new Disc(colorMaterial, 500,15,2,3,this);
  65. dome                                = new Dome(colorMaterial, 500, 100, 10, 5, 2);
  66.  
  67. for (var i:int = 0; i <disc.geometry.vertices.length; i++)
  68. {
  69. var discVert:Vertex3D = disc.geometry.vertices[i];
  70.  
  71. var vertexParticle:VertexParticle = new VertexParticle(particalMaterial2, 5, discVert);
  72.  
  73. vertexParticle.x = discVert.x;
  74. vertexParticle.y = discVert.y;
  75. vertexParticle.z = discVert.z;
  76.  
  77. myParticles.addParticle(vertexParticle);
  78. }
  79.  
  80. for (var j:int = 0; j <dome.geometry.vertices.length; j++)
  81. {
  82. var domeVert:Vertex3D = dome.geometry.vertices[j];
  83.  
  84. var domeVertexParticle:VertexParticle = new VertexParticle(particalMaterial, 5, domeVert);
  85.  
  86. domeVertexParticle.x = domeVert.x;
  87. domeVertexParticle.y = domeVert.y;
  88. domeVertexParticle.z = domeVert.z;
  89.  
  90. domeParticles.addParticle(domeVertexParticle);
  91. }
  92.  
  93. container.addChild(myParticles)
  94. domeContainer.addChild(domeParticles)
  95.  
  96. domeContainer.y = -10
  97. default_scene.addChild(container)
  98. default_scene.addChild(domeContainer);
  99. }
  100.  
  101. override protected function renderEnterFrame(ThisEvent:Event):void
  102. {
  103. super.renderEnterFrame(ThisEvent);
  104.  
  105. if (default_camera.z <0)
  106. {
  107. default_camera.z += (0 - default_camera.z)*.01
  108. }
  109.  
  110. default_camera.y += (50 - default_camera.y)*.01;
  111.  
  112. container.yaw(1);
  113. domeContainer.yaw(1);
  114. }
  115.  
  116. }
  117.  
  118. }

Paperbase.as

Actionscript:
  1. package  com.cs54.papervision
  2. {
  3. import flash.display.Sprite;
  4. import flash.events.Event;
  5. import org.papervision3d.view.Viewport3D;
  6. import org.papervision3d.cameras.*;
  7. import org.papervision3d.scenes.Scene3D;
  8. import org.papervision3d.render.BasicRenderEngine;
  9.  
  10. public class PaperBase extends Sprite
  11. {
  12.  
  13. public var viewport:Viewport3D;
  14. public var renderer:BasicRenderEngine;
  15. public var default_scene:Scene3D;
  16. public var default_camera:Camera3D;
  17.  
  18. public function init(vpWidth:Number = 800, vpHeight:Number = 600):void
  19. {
  20. initPapervision(vpWidth, vpHeight);
  21. createChildren3D();
  22. createChildren2D();
  23. size();
  24. initEvents();
  25. }
  26. protected function initPapervision(vpWidth:Number, vpHeight:Number):void
  27. {
  28. viewport = new Viewport3D(vpWidth, vpHeight);
  29. addChild(viewport);
  30.  
  31. renderer = new BasicRenderEngine();
  32.  
  33. default_scene = new Scene3D();
  34.  
  35. default_camera = new Camera3D();
  36. }
  37. protected function createChildren3D():void
  38. {
  39. // This function should hold all of the stages needed
  40. // to initialise everything used for papervision.
  41. // Models, materials, cameras etc.
  42. }
  43. protected function createChildren2D():void
  44. {
  45. // This function should create all of the 2d items
  46. // that will be overlayed on your papervision project.
  47. // User interfaces, Heads up displays etc.
  48. }
  49. protected function size():void
  50. {
  51.  
  52. }
  53. protected function initEvents():void
  54. {
  55. addEventListener(Event.ENTER_FRAME, renderEnterFrame);
  56. }
  57. protected function processFrame():void
  58. {
  59. // Process any movement or animation here.
  60. }
  61. protected function renderEnterFrame( ThisEvent:Event ):void
  62. {
  63. //trace("RENDERING")
  64. //We need to render the scene and update anything here.
  65.  
  66. renderer.renderScene(default_scene, default_camera, viewport);
  67. processFrame();
  68. }
  69. }
  70. }

Here are Carlos Lunetta's classes for the Dome,Disc and VertexParticle

Dome.as

Actionscript:
  1. package src.primitives
  2. {
  3. import org.papervision3d.core.geom.*;
  4. import org.papervision3d.core.geom.renderables.Vertex3D;
  5. import org.papervision3d.core.proto.*;
  6. /**
  7. * ...
  8. * @author clunetta
  9. */
  10. public class Dome extends TriangleMesh3D
  11. {
  12. private var segments:uint;
  13. private var sides:uint;
  14. private var radius:Number
  15. private var height:Number;
  16. private var sideIncrement:Number;
  17. private var currentIncrement:Number = 0;
  18.  
  19. public function Dome (material:MaterialObject3D=null, radius:Number=100, height:Number = 100, segments:uint=0, sides:uint=3, sideIncrement:Number=0, initObject:Object=null )
  20. {
  21. super(material, new Array(), new Array());
  22.  
  23. this.segments         = 1 + segments;
  24. this.sides             = sides;
  25. this.radius         = radius;
  26. this.sideIncrement     = sideIncrement;
  27. this.height         = 2 * height;
  28.  
  29. buildDome();
  30. }
  31.  
  32. private function buildDome():void
  33. {
  34. var matInstance:MaterialObject3D = material;
  35.  
  36. var i:Number, j:Number, k:Number;
  37.  
  38. var aVertice:Array = this.geometry.vertices;
  39.  
  40. var oVtx:Vertex3D;
  41. oVtx = new Vertex3D(0,height,0);
  42. aVertice.push(oVtx);
  43.  
  44. for (j = 1; j <segments; j++) {
  45.  
  46. //var hRds:Number = (segments-j)* (height/segments)
  47. var fZ:Numberheight * Math.sin((segments-(j-1))(0.5*Math.PI) / segments);
  48.  
  49. //var fZ:Number = (height)*Math.sin((j-1)*(0.5*Math.PI/segments));
  50. currentIncrement += sideIncrement;
  51. var curSides:uint = uint(Math.floor(sides + currentIncrement));
  52.  
  53. for (i = 0; i <curSides; i++) {
  54. var fRds:Number = j * radius / segments;
  55. var fX:Number = fRds*Math.sin(i*(2*Math.PI/curSides));
  56. var fY:Number = fRds*Math.cos(i*(2*Math.PI/curSides));
  57. oVtx = new Vertex3D(fY,fZ,fX);
  58. aVertice.push(oVtx);
  59. }
  60. }
  61.  
  62. this.geometry.ready = true;
  63. }
  64.  
  65. }
  66.  
  67. }

Disc.as

Actionscript:
  1. package src.primitives
  2. {
  3. import org.papervision3d.core.geom.*;
  4. import org.papervision3d.core.geom.renderables.Vertex3D;
  5. import org.papervision3d.core.proto.*;
  6. /**
  7. * ...
  8. * @author clunetta
  9. */
  10. public class Disc extends TriangleMesh3D
  11. {
  12. private var segments :uint;
  13. private var sides :uint;
  14. private var radius :Number;
  15. private var sideIncrement:Number;
  16. private var currentIncrement:Number = 0;
  17.  
  18. public function Disc (material:MaterialObject3D=null, radius:Number=100, segments:uint=0, sides:uint=3, sideIncrement:Number=0, initObject:Object=null )
  19. {
  20. super(material, new Array(), new Array());
  21. this.segments = 1+ segments;
  22. this.sides = sides;
  23. this.radius = radius;
  24. this.sideIncrement = sideIncrement;
  25.  
  26. buildDisc();
  27. }
  28.  
  29. private function buildDisc():void
  30. {
  31. var matInstance:MaterialObject3D = material;
  32.  
  33. var i:Number, j:Number, k:Number;
  34.  
  35. var aVertice:Array = this.geometry.vertices;
  36.  
  37. var fZ:Number = 0;
  38. var oVtx:Vertex3D;
  39. oVtx = new Vertex3D(0,0,0);
  40. aVertice.push(oVtx);
  41. for (j = 0; j <segments; j++) {
  42. currentIncrement += sideIncrement;
  43. var curSides:uint = uint(Math.floor(sides + currentIncrement));
  44. for (i = 0; i <curSides; i++) {
  45. var fRds:Number = (j + 1) * radius / segments;
  46. var fX:Number = fRds*Math.sin(i*(2*Math.PI/curSides));
  47. var fY:Number = fRds*Math.cos(i*(2*Math.PI/curSides));
  48. oVtx = new Vertex3D(fY,fZ,fX);
  49. aVertice.push(oVtx);
  50. }
  51.  
  52. }
  53.  
  54. this.geometry.ready = true;
  55. }
  56.  
  57. }
  58.  
  59. }

VertexParticle.as

Actionscript:
  1. package src.primitives
  2. {
  3. import org.papervision3d.core.geom.renderables.Particle;
  4. import org.papervision3d.core.geom.renderables.Vertex3D;
  5. import org.papervision3d.materials.special.ParticleMaterial;
  6.  
  7. /**
  8. * ...
  9. * @author clunetta
  10. */
  11. public class VertexParticle extends Particle
  12. {
  13.  
  14. public function VertexParticle(material:ParticleMaterial, size:Number, vertex:Vertex3D)
  15. {
  16. super(material, size);
  17. super.vertex3D = vertex;
  18. }
  19.  
  20. }
  21.  
  22. }

Source Code

Papervision with 3D Physics WOW engine (Getting started)

Papervision with WOW Engine

Today I decided to create this simple example that used Papervision and the 3D physics WOW Engine. It is actually quite simple. Basically all that happens is you setup your objects for papervision (5 minutes) - setup your objects for the wow engine (5 mintues) then when you render - you just attach the movements of the WOW objects to your Papervision objects. (5 more minutes or less and you're DONE)

So taking out 3D and Papervision all together here is what is happening. Let's say you setup an enterframe and then assign a movieclips x and y positions to your mouse x and y position. SAME Exact thing happening here. Except you are attaching the x y and z properties of a papervision object to a wow object. Pretty simple! I'll take corrections if I am wrong since I have spent all of 30 minutes playing around with it.

Here is a quick sample of what goes on when rendering:

Actionscript:
  1. sphere.x    = wowSphere.px;
  2. sphere.y    = -wowSphere.py;
  3. sphere.z    = wowSphere.pz;

Not too hard right.

Here is the ActionScript code for creating a simple sphere boucing on the stage. Source code is below. (Basically a modified example shown on the WOW site)

Main.as

Actionscript:
  1. package
  2. {
  3. import com.cs54.papervision.PV3DSceneWOW;
  4. import flash.display.Sprite;
  5.  
  6. public class Main extends Sprite
  7. {
  8. protected var papervisionSceneWOW:PV3DSceneWOW
  9.  
  10. public function Main():void
  11. {
  12. papervisionSceneWOW = new PV3DSceneWOW();
  13. papervisionSceneWOW.init(stage.stageWidth, stage.stageHeight)
  14. addChild(papervisionSceneWOW);
  15. }
  16. }
  17. }

PV3DSceneWOW.as:

Actionscript:
  1. package com.cs54.papervision
  2. {
  3.  
  4. import flash.events.Event;
  5. import fr.seraf.wow.core.data.WVector;
  6. import fr.seraf.wow.core.WOWEngine;
  7. import fr.seraf.wow.primitive.WBox;
  8. import fr.seraf.wow.primitive.WOWPlane;
  9. import fr.seraf.wow.primitive.WSphere;
  10. import org.papervision3d.materials.ColorMaterial;
  11. import org.papervision3d.materials.utils.MaterialsList;
  12. import org.papervision3d.materials.WireframeMaterial;
  13. import org.papervision3d.objects.DisplayObject3D;
  14. import org.papervision3d.objects.primitives.Plane;
  15. import org.papervision3d.objects.primitives.Sphere;
  16.  
  17. public class PV3DSceneWOW extends PaperBase
  18. {
  19.  
  20. protected var displayContainer    :DisplayObject3D;
  21. protected var holder            :DisplayObject3D
  22. protected var sphere            :Sphere;
  23. protected var wireframeMat        :WireframeMaterial;
  24. protected var wow                :WOWEngine
  25. protected var wowSphere            :WSphere
  26. override public function init(vpWidth:Number = 800, vpHeight:Number = 600):void
  27. {
  28.  
  29. super.init(vpWidth, vpHeight);
  30. default_camera.y = 300;
  31. }
  32.  
  33. override protected function createChildren3D():void
  34. {
  35. super.createChildren3D();
  36.  
  37. wireframeMat                 = new WireframeMaterial(0, 100, .2);
  38. wireframeMat.doubleSided     = true;
  39. sphere                         = new Sphere(wireframeMat, 100);
  40. default_camera.zoom         = 40;
  41.  
  42. default_scene.addChild(sphere);
  43.  
  44. setupWow();
  45. createGround();
  46. createWOWSphere();
  47. }
  48.  
  49. public function setupWow(): void
  50. {
  51. //this is the physics engine
  52. wow=new WOWEngine(.3);
  53. wow.collisionResponseMode = wow.STANDARD;
  54.  
  55. //setup the gravity
  56. wow.addMasslessForce(new WVector(0,50,0));
  57. }
  58.  
  59. public function createGround(): void
  60. {
  61. //we create a ground on the physics engine
  62. var ground:WOWPlane             = new WOWPlane(0,0,0);
  63. ground.elasticity                =.35;
  64. ground.friction                    =2;
  65.  
  66. wow.addParticle(ground);
  67.  
  68. // we create the ground on the render engine so we start by setup the material...
  69. var material:WireframeMaterial    = new WireframeMaterial(0,100,1)
  70. material.doubleSided            = true
  71.  
  72. var plane:Plane                 = new Plane(material , 1000, 1000, 5, 5 );
  73. plane.rotationX                    =-90
  74.  
  75. default_scene.addChild(plane);
  76. }
  77.  
  78. public function createWOWSphere(): void
  79. {
  80. wowSphere = new WSphere(0,-900,100,100,false,0.1);
  81. wowSphere.elasticity = 0;
  82.  
  83. wowSphere.friction=3320;
  84. wow.addParticle(wowSphere);
  85.  
  86. sphere.x    =-wowSphere.px;
  87. sphere.y    =-wowSphere.py;
  88. sphere.z    =-wowSphere.pz;
  89. }
  90.  
  91. override protected function processFrame():void
  92. {
  93. super.processFrame();
  94.  
  95. wow.step()
  96.  
  97. sphere.x    = wowSphere.px;
  98. sphere.y    = -wowSphere.py;
  99. sphere.z    = wowSphere.pz;
  100.  
  101. }
  102. override protected function renderEnterFrame(ThisEvent:Event):void
  103. {
  104. super.renderEnterFrame(ThisEvent);
  105.  
  106. }
  107.  
  108. }
  109.  
  110. }

The paper base that I reuse:
Based off of this example

Actionscript:
  1. package  com.cs54.papervision
  2. {
  3. import flash.display.Sprite;
  4. import flash.events.Event;
  5. import org.papervision3d.view.Viewport3D;
  6. import org.papervision3d.cameras.*;
  7. import org.papervision3d.scenes.Scene3D;
  8. import org.papervision3d.render.BasicRenderEngine;
  9.  
  10. public class PaperBase extends Sprite
  11. {
  12.  
  13. public var viewport:Viewport3D;
  14. public var renderer:BasicRenderEngine;
  15. public var default_scene:Scene3D;
  16. public var default_camera:Camera3D;
  17.  
  18. public function init(vpWidth:Number = 800, vpHeight:Number = 600):void
  19. {
  20. initPapervision(vpWidth, vpHeight);
  21. createChildren3D();
  22. createChildren2D();
  23. size();
  24. initEvents();
  25. }
  26. protected function initPapervision(vpWidth:Number, vpHeight:Number):void
  27. {
  28. viewport = new Viewport3D(vpWidth, vpHeight);
  29. addChild(viewport);
  30.  
  31. renderer = new BasicRenderEngine();
  32.  
  33. default_scene = new Scene3D();
  34.  
  35. default_camera = new Camera3D();
  36. }
  37. protected function createChildren3D():void
  38. {
  39. // This function should hold all of the stages needed
  40. // to initialise everything used for papervision.
  41. // Models, materials, cameras etc.
  42. }
  43. protected function createChildren2D():void
  44. {
  45. // This function should create all of the 2d items
  46. // that will be overlayed on your papervision project.
  47. // User interfaces, Heads up displays etc.
  48. }
  49. protected function size():void
  50. {
  51.  
  52. }
  53. protected function initEvents():void
  54. {
  55. addEventListener(Event.ENTER_FRAME, renderEnterFrame);
  56. }
  57. protected function processFrame():void
  58. {
  59. // Process any movement or animation here.
  60. }
  61. protected function renderEnterFrame( ThisEvent:Event ):void
  62. {
  63. //trace("RENDERING")
  64. //We need to render the scene and update anything here.
  65.  
  66. renderer.renderScene(default_scene, default_camera, viewport);
  67. processFrame();
  68. }
  69. }
  70. }

SOURCE CODE

Don't forget to download the latest Papervision Great White Branch.

Papervision Double Sided Plane (err sorta) :/

Papervision double sided plane

As far as I know there are 2 options when creating a double sided plane. Double sided meaning a different material for each side.

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 I 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. }

Papervision Collada – Spongebob SquarePants

Spongebob SquarePants

View Collada Spongebob in Papervision

I had some time on my hands this weekend and decided to mess around with loading Collada models with Papervision. I snagged a few models from Google Sketchup and went to town. Also tried to work in some OOP instead of just having one huge papervision file. I really need to clean up the source but rest assured the full source will be posted soon.

Basic lesson is that you can load a Collada file with just actionscript (packaged as a zip or just the dae file)

Actionscript:
  1. //Load zipped dae file
  2. protected function loadKMZ():void
  3. {
  4.     kmz = new KMZ();
  5.     kmz.load("model/spongebob_squarepants.zip");
  6.     kmz.addEventListener(FileLoadEvent.LOAD_ERROR, onKmzError);
  7.     kmz.addEventListener(FileLoadEvent.LOAD_COMPLETE, onKmzLoaded);
  8.    
  9.     //Scale if needed
  10.     kmz.scale = 20;
  11.     container.addChild(kmz);
  12. }
  13.  
  14. //load the dae file
  15. protected function loadDAE():void
  16. {
  17.     dae = new DAE();
  18.     dae.load("model/spongebob_squarepants.dae");
  19.     dae.addEventListener(FileLoadEvent.LOAD_ERROR, onDaeError);
  20.     dae.addEventListener(FileLoadEvent.LOAD_COMPLETE, onDaeLoaded);
  21.     container.addChild(dae);
  22. }

Papervision Popup Book : The source – Just the elements on the stage

Sorry for putting this up so late. This code is only used to arrange the items on the stage. Much work and re-working will need to be done to even start to achieve the effect of the  ecodazoo website.  Anyways, perhaps this will get someones brain moving in the right direction and help lead us in the path to re-creating this effect fully. As soon as I get some free time I will post a version that is a true popup book in papervision.

If anyone gets a chance sooner to do some cool things with the code please shoot me an email or a blog post and I would love to share it, with myself and with everyone else. :-D

Download : FLA - AS & SOURCE

Don't forget the other code you will need to compile:

1) Papervision 2.0

2) Tweener

Actionscript:
  1. package {
  2. import caurina.transitions.Tweener;
  3. import org.papervision3d.cameras.FreeCamera3D;
  4. import org.papervision3d.core.data.qTree.QuadTree;
  5. import org.papervision3d.objects.DisplayObject3D;
  6. import com.foomonger.utils.Later;
  7. import flash.display.*;
  8. import flash.events.Event;
  9. import flash.events.MouseEvent;
  10. import org.papervision3d.cameras.Camera3D;
  11. import org.papervision3d.materials.ColorMaterial;
  12. import org.papervision3d.materials.MovieMaterial;
  13. import org.papervision3d.objects.primitives.Cube;
  14. import org.papervision3d.objects.primitives.Plane;
  15. import org.papervision3d.render.BasicRenderEngine;
  16. import org.papervision3d.scenes.Scene3D;
  17. import org.papervision3d.view.Viewport3D;
  18. public class Main extends Sprite
  19. {
  20. private var __camera                :FreeCamera3D;
  21. private var __scene  :Scene3D;
  22. private var __viewport    :Viewport3D;
  23. private var __renderer    :BasicRenderEngine;
  24. private var __width  :Number;
  25. private var __height                :Number;
  26. private var __background            :Shape;
  27. protected var __wallLeft            :D isplayObject;
  28. protected var __wallRight       :D isplayObject;
  29. protected var __wallBack            :D isplayObject;
  30. protected var __floor         :DisplayObject;
  31. protected var __tree                :D isplayObject;
  32. protected var __monster :DisplayObject;
  33. protected var __moveMatWallLeft  :MovieMaterial;
  34. protected var __moveMatRightWall    :MovieMaterial;
  35. protected var __moveMatWallBack  :MovieMaterial;
  36. protected var __moveMatFloor        :MovieMaterial;
  37. protected var __moveMatTree   :MovieMaterial;
  38. protected var __moveMatMonster  :MovieMaterial;
  39. protected var __plane_wallLeft  :P lane;
  40. protected var __plane_rightWall  :P lane;
  41. protected var __plane_wallBack  :P lane;
  42. protected var __floorPlane    :Plane;
  43. protected var __treePlane       :P lane;
  44. protected var __monsterPlane        :P lane;
  45. protected var __rotation            :Number;
  46. public function Main():void
  47. {
  48. init();
  49. }
  50. protected function init():void
  51. {
  52. __rotation = 0;
  53. setDefaultWidthHeight();
  54. createChildren();
  55. initPapervision();
  56. initMaterials();
  57. initObjects();
  58. initEvents();
  59. }
  60. protected function setDefaultWidthHeight():void
  61. {
  62. __width = 1000;
  63. __height = 600;
  64. }
  65. protected function initPapervision():void
  66. {
  67. __scene = new Scene3D();
  68. __camera = new FreeCamera3D();
  69. __camera.zoom = 11;
  70. __camera.z = -2000;
  71. __camera.y = 20;
  72. __viewport = new Viewport3D(__width,__height,false,true,true);
  73. __renderer = new BasicRenderEngine();
  74. addChild(__viewport);
  75. }
  76. protected function initMaterials():void
  77. {
  78. __moveMatRightWall = new MovieMaterial(__wallRight, true)
  79. __moveMatWallLeft = new MovieMaterial(__wallLeft, true);
  80. __moveMatWallBack = new MovieMaterial(__wallBack)
  81. __moveMatFloor = new MovieMaterial(__floor);
  82. __moveMatTree = new MovieMaterial(__tree, true );
  83. __moveMatMonster = new MovieMaterial(__monster, true);
  84. __moveMatRightWall.doubleSided = true;
  85. __moveMatWallLeft.doubleSided = true;
  86. __moveMatWallBack.doubleSided = true;
  87. __moveMatTree.doubleSided = true;
  88. __moveMatFloor.doubleSided = true;
  89. __moveMatTree.doubleSided = true;
  90. __moveMatMonster.doubleSided = true;
  91. }
  92. protected function initObjects():void
  93. {
  94. __monsterPlane   = new Plane(__moveMatMonster, 184, 237, 4, 4);
  95. __plane_rightWall   = new Plane(__moveMatRightWall, 386, 239, 4,4)
  96. __plane_wallBack    = new Plane(__moveMatWallBack, 734, 240, 4,4);
  97. __plane_wallLeft    = new Plane(__moveMatWallLeft,386,239,4,4)
  98. __floorPlane       = new Plane(__moveMatFloor, 800, 800, 14,14);
  99. __treePlane   = new Plane(__moveMatTree, 79, 112, 4, 4);
  100. __scene.addChild(__floorPlane);
  101. __scene.addChild(__plane_wallBack);
  102. __scene.addChild(__treePlane);
  103. __scene.addChild(__plane_wallLeft);
  104. __scene.addChild(__plane_rightWall);
  105. __scene.addChild(__monsterPlane);
  106. __floorPlane.y = -120;
  107. __treePlane.y = -69;
  108. __floorPlane.pitch(90);
  109. __monsterPlane.z = -300;
  110. __monsterPlane.x = 150;
  111. __monsterPlane.y = -10;
  112. __plane_wallBack.z += 340
  113. __treePlane.z = 300;
  114. __treePlane.x = -220;
  115. __plane_wallLeft.x = -155;
  116. __plane_rightWall.x = 155;
  117. __plane_wallLeft.rotationY = -35;
  118. __plane_rightWall.rotationY = 35
  119. }
  120. protected function initEvents():void
  121. {
  122. this.addEventListener(Event.ENTER_FRAME,render);
  123. }
  124. protected function render(e:Event):void
  125. {
  126. __renderer.renderScene(__scene, __camera, __viewport, true);
  127. var moveXAmount:Number = (mouseX - 1000 / 2) * 4
  128. var moveYAmount:Number = (mouseY - 600 / 2) * 2
  129. if (moveYAmount <0)
  130. {
  131. moveYAmount = 0;
  132. }
  133. __camera.x = moveXAmount;
  134. __camera.y = moveYAmount;
  135. __camera.lookAt(__floorPlane)
  136. }
  137. protected function createChildren():void
  138. {
  139. //Movieclips with Export for AS in Library
  140. __wallBack   = new Wall_back();
  141. __wallLeft   = new Wall_left();
  142. __wallRight  = new Wall_right();
  143. __floor   = new Floor();
  144. __tree  = new Tree();
  145. __monster    = new Monster();
  146. }
  147. }
  148. }


Follow papervision2 on Twitter

RSS Feed