Tag Archive for 'AS3'

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

Controlling a Papervision Cube

Papervision 3D Cube Example

View Example

The goal for this example was to create a simple way to display a cube, then rotate it one side at a time. The image is just from Google Images which will be all 6 sides of the cube. This again like the exploding image example illustrates some control over your elements. You can easily modify this example to include mouse events that you can use trigger a view each side of the cube.

Actionscript:
  1. package
  2. {
  3. import caurina.transitions.Tweener;
  4.  
  5. import com.foomonger.utils.Later;
  6.  
  7. import flash.display.*;
  8.  
  9. import flash.events.Event;
  10.  
  11. import org.papervision3d.cameras.Camera3D;
  12. import org.papervision3d.materials.ColorMaterial;
  13. import org.papervision3d.materials.MovieMaterial;
  14. import org.papervision3d.materials.utils.MaterialsList;
  15. import org.papervision3d.objects.primitives.Cone;
  16. import org.papervision3d.objects.primitives.Cube;
  17. import org.papervision3d.objects.primitives.Plane;
  18. import org.papervision3d.render.BasicRenderEngine;
  19. import org.papervision3d.scenes.Scene3D;
  20. import org.papervision3d.view.Viewport3D;
  21.  
  22. public class PapervisionControlledCube extends Sprite
  23. {
  24. private var __camera:Camera3D;
  25. private var __scene:Scene3D;
  26. private var __viewport:Viewport3D;
  27. private var __renderer:BasicRenderEngine;
  28.  
  29. private var __cube:Cube;
  30. private var __matList:MaterialsList;
  31.  
  32. private var __width:Number;
  33. private var __height:Number;
  34.  
  35. private var __background:Shape;
  36.  
  37. //Only needed if you are linking the image from FlexBuilder
  38. //[Embed(source="assets/brick.png")]
  39. //public var Brick:Class;
  40.  
  41. protected var __brick:DisplayObject;
  42.  
  43. protected var __brickClip:MovieClip;
  44.  
  45. protected var __rotation:Number;
  46.  
  47. public function PapervisionControlledCube():void
  48. {
  49.  
  50. init();
  51.  
  52. }
  53. protected function init():void
  54. {
  55. __rotation = 0;
  56.  
  57. setDefaultWidthHeight();
  58.  
  59. //Only needed for flexbuilder
  60. //setupStage();
  61. //--------------------------
  62.  
  63. createChildren();
  64. initPapervision();
  65. initMaterials();
  66. initObjects();
  67. initEvents();
  68. }
  69. protected function setDefaultWidthHeight():void
  70. {
  71. __width = 500;
  72. __height = 250;
  73. }
  74.  
  75. protected function initPapervision():void
  76. {
  77. __scene = new Scene3D();
  78. __camera = new Camera3D();
  79.  
  80. __camera.zoom = 11;
  81. __camera.z = -5000;
  82. __camera.y = 200;
  83.  
  84. __viewport = new Viewport3D(__width,__height,false,true,true);
  85. __renderer = new BasicRenderEngine();
  86. addChild(__viewport);
  87. }
  88.  
  89. protected function initMaterials():void
  90. {
  91. __matList = new MaterialsList();
  92.  
  93. var movieMat:MovieMaterial = new MovieMaterial(__brickClip);
  94.  
  95. __matList.addMaterial( movieMat, "front" );
  96. __matList.addMaterial( movieMat, "back" );
  97. __matList.addMaterial( movieMat, "left" );
  98. __matList.addMaterial( movieMat, "right" );
  99. __matList.addMaterial( movieMat, "top" );
  100. __matList.addMaterial( movieMat, "bottom" );
  101.  
  102. }
  103.  
  104. protected function initObjects():void
  105. {
  106. __cube = new Cube(__matList,250,130,130);
  107. __scene.addChild(__cube);
  108.  
  109. Later.call(this,zoomIn,1000,true);
  110.  
  111. }
  112. protected function zoomIn(e:Event = null):void
  113. {
  114. Tweener.addTween(__camera,{z:-1000,transition:"easeInOutQuint",time:1,onComplete:rotateCube})
  115. }
  116.  
  117. //-----------------------------------------------------------------------
  118. //Rotate Cube
  119. //-----------------------------------------------------------------------
  120.  
  121. protected function rotateCube(e:Event = null):void
  122. {
  123.  
  124. __rotation += 90;
  125. Tweener.addTween(__cube,{rotationY:__rotation,delay:.6,transition:"easeInOutQuint",time:1,onComplete:rotateCube})
  126. }
  127.  
  128. protected function initEvents():void
  129. {
  130. this.addEventListener(Event.ENTER_FRAME,render);
  131. }
  132.  
  133. protected function render(e:Event):void
  134. {
  135. __renderer.renderScene(__scene,__camera,__viewport,true);
  136. }
  137.  
  138. protected function createChildren():void
  139. {
  140. __brick = new Brick();
  141. __brickClip = new MovieClip();
  142. __brickClip.addChild(__brick);
  143.  
  144. }
  145. //Only used in flexbuilder
  146. private function setupStage():void
  147. {
  148. stage.stageWidth         = __width;
  149. stage.stageHeight         = __height;
  150. stage.align             = "cc";
  151. stage.scaleMode         = "noScale";
  152. stage.stageFocusRect     = true;
  153.  
  154. }
  155. }
  156. }

Download Source

Other classes used:

1) Papervision 2.0

2) Foomonger's Code (Call Later)

3) Tweener

Papervision 2.0 – Getting started from a simple starting example

At first when trying to create my first few Papervision projects it seemed a bit overwhelming. As you work with it more and more you notice that there are really only a few elements for creating a simple Papervision project. Then you can forget about the code and just think about what cool things you can do.

For any Papervision 2.0 project you need:

Actionscript:
  1. //As always your imports
  2. import flash.display.*;
  3. import flash.events.Event;
  4. import org.papervision3d.scenes.Scene3D;
  5. import org.papervision3d.view.Viewport3D;
  6. import org.papervision3d.cameras.Camera3D;
  7. import org.papervision3d.materials.ColorMaterial;
  8. import org.papervision3d.objects.primitives.Plane;
  9. import org.papervision3d.render.BasicRenderEngine;
  10.  
  11. //1) A Scene
  12. var scene:Scene3D = new Scene3D()
  13.  
  14. //2) A Camera
  15. var camera = new Camera3D()
  16. //Setting the camera zoom to 11 will make sure your objects are rendered at 100%
  17. camera.zoom = 11;
  18.  
  19. //3) A view area "viewport" - think of this as your canvas or stage for your 3D objects
  20. var viewport:Viewport3D = new Viewport3D(500,300,false,true,true)
  21.  
  22. //4) A render engine
  23. var renderer:BasicRenderEngine = new BasicRenderEngine();
  24.  
  25. //5) add the viewport to the stage
  26. addChild(viewport);
  27.  
  28. //6) Something in your scene such as a Plane, Cube, Sphere, etc - in this case a plane with a black color material
  29. var plane:Plane = new Plane(new ColorMaterial(0x000000),100,100)
  30. //add your object to your scene
  31. scene.addChild(plane)
  32.  
  33. //7) Create a loop for rendering your animation
  34. this.addEventListener(Event.ENTER_FRAME,render);
  35. function render(e:Event):void
  36. {
  37. //Add some simple animation
  38. plane.rotationY += 2; //Could also be plane.yaw(2);
  39. //Render the scene
  40. renderer.renderScene(scene,camera,viewport,true);
  41. }

Don't forget to download the latest Papervision code

The great thing is that if you build this once into a nice clean class, you can just keep re-using that class as your papervision starting place. Then you can do what you do best, create and have fun!

Start with this and add a cube or different materials and animations. Then toss in some Tweener code to get some cool movement effects. In a few days you'll have some amazing things.

Papervision 2.0 – Exploding and Rebuilding an Image

[kml_flashembed movie="image-explode.swf" height="300" width="550" /]

I took some time last week to have some fun with Papervision. The goal was to just take an image (any image) and have it explode into pieces and then rebuild it. While searching around for code or tutorials I have only found a few where the items on the stage go back into a controlled state. So over the next few weeks I am going to try to create a few examples of things that could be used IN a website rather than having the Papervision BE the website. I am not sure that makes sense. While I love experiments I like to have things I can use on everyday websites. This example I probably be using when I launch my new site. It's just something to break away from the ole fade in and out motion.

If you want to test this code on your own computer you will need to download a few things first. :)

1) Papervision 2.0

2) Foomonger's Code (Call Later)

3) Tweener

4) The Source

Actionscript:
  1. package
  2. {
  3. import flash.display.*;
  4. import flash.events.*;
  5. import flash.geom.Matrix;
  6. import flash.geom.Point;
  7.  
  8. import caurina.transitions.*
  9.  
  10. import com.foomonger.utils.Later;
  11.  
  12. import org.papervision3d.cameras.FreeCamera3D;
  13. import org.papervision3d.materials.BitmapFileMaterial;
  14. import org.papervision3d.materials.MovieMaterial;
  15. import org.papervision3d.objects.DisplayObject3D;
  16. import org.papervision3d.objects.primitives.Plane;
  17. import org.papervision3d.render.BasicRenderEngine;
  18. import org.papervision3d.scenes.Scene3D;
  19. import org.papervision3d.view.Viewport3D;
  20.  
  21. public class PVImageExplode extends MovieClip
  22. {
  23. private var viewport        :Viewport3D;
  24. private var scene            :Scene3D;
  25. private var camera            :FreeCamera3D;
  26.  
  27. private var block            :BitmapFileMaterial;
  28. private var movieBlock        :MovieMaterial;
  29.  
  30. private var blocksArray    :Array;
  31. private var planeArray        :Array;
  32. private var clips_array        :Array;
  33.  
  34. private var renderer        :BasicRenderEngine;
  35.  
  36. private var __holder        :D isplayObject3D;
  37. private var __startCamZ        :Number;
  38.  
  39. private var __homePage        :MovieClip;
  40. private var __grass            :D isplayObject;
  41. private var __grassHolder    :MovieClip;
  42.  
  43. private var __col            :Number;
  44. private var __rows            :Number;
  45. private var __blockWidth    :Number;
  46. private var __blockHeight    :Number;
  47.  
  48. private var __yawAmount        :Number = 0;
  49.  
  50. public function PVImageExplode()
  51. {
  52. init()
  53. }
  54.  
  55. private function init():void
  56. {
  57. createChildren();
  58. size();
  59.  
  60. initPapervision();
  61. initMaterials();
  62. initObjects();
  63. initEvents();
  64. }
  65.  
  66. //-----------------------------------------------------------------------
  67. //Adds image to the stage to be then broke apart
  68. //-----------------------------------------------------------------------
  69.  
  70. protected function createChildren():void
  71. {
  72.  
  73. __grassHolder            = new MovieClip();
  74. __grass                = new GrassItem();
  75. __grassHolder.visible    = false;
  76. __grassHolder.addChild(__grass)
  77.  
  78. addChild(__grassHolder);
  79.  
  80. }
  81.  
  82. protected function size():void
  83. {
  84. __col            = 4;
  85. __rows            = 10;
  86.  
  87. }
  88.  
  89. //-----------------------------------------------------------------------
  90. //Papervision init setup
  91. //-----------------------------------------------------------------------
  92.  
  93. private function initPapervision():void
  94. {
  95. viewport                        = new Viewport3D(1000,600,false,true,false);
  96. addChild(viewport);
  97.  
  98. scene                            = new Scene3D();
  99. camera                            = new FreeCamera3D();
  100. renderer                        = new BasicRenderEngine();
  101.  
  102. camera.zoom = 11;
  103. __startCamZ = -6000
  104.  
  105. }
  106.  
  107. //-----------------------------------------------------------------------
  108. //Papervision materials setup
  109. //-----------------------------------------------------------------------
  110.  
  111. private function initMaterials():void
  112. {
  113. blocksArray = [];
  114.  
  115. clips_array            = new Array();
  116.  
  117. var __w:int            =Math.ceil(__grassHolder.width / __col);
  118. var __h:int            =Math.ceil(__grassHolder.height / __rows);
  119.  
  120. __blockWidth    = __w;
  121. __blockHeight    = __h;
  122.  
  123. var matrix            :Matrix;
  124. var bitmap_data        :BitmapData;
  125. var point            :P oint;
  126. var clip            :MovieClip;
  127. var columns            :Number             = __col;
  128. var cellWidth        :Number             = __blockWidth;
  129. var cellHeight        :Number             = __blockHeight;
  130. var cellX            :Number
  131. var cellY            :Number
  132. var bx                :Number
  133. var by                :Number
  134. var bitmap            :Bitmap
  135.  
  136. for (var i:int=0; i <(__rows * __col); i++)
  137. {
  138.  
  139. cellX        = i % columns;
  140. cellY        = Math.floor(i / columns);
  141.  
  142. bitmap_data            = new BitmapData(__w,__h,true,0x00FFFFFF);
  143. matrix                = __grassHolder.transform.matrix;
  144.  
  145. bx = (cellX * cellWidth);
  146. by = cellY * cellHeight;
  147.  
  148. matrix.translate(-__w * cellX, -__h * cellY);
  149.  
  150. bitmap_data.draw(__grassHolder,matrix);
  151. clip = new MovieClip();
  152. clips_array.push(clip);
  153. clip.id = i;
  154. addChild(clip);
  155.  
  156. point = new Point(__grassHolder.x + __w * cellX, __grassHolder.y + __h * cellY);
  157.  
  158. bitmap =new Bitmap(bitmap_data);
  159. clip.addChild(bitmap);
  160.  
  161. clip.visible = false;
  162. clip.x = point.x;
  163. clip.y = point.y;
  164. }
  165.  
  166. for(var k:uint = 0;k<clips_array.length;k++)
  167. {
  168. movieBlock = new MovieMaterial(clips_array[k],true);
  169. movieBlock.doubleSided = true;
  170. blocksArray.push(movieBlock);
  171. }
  172.  
  173. }
  174.  
  175. //-----------------------------------------------------------------------
  176. //Papervision objects setup (planes etc)
  177. //-----------------------------------------------------------------------
  178.  
  179. private function initObjects():void
  180. {
  181.  
  182. planeArray = [];
  183.  
  184. __holder        = new DisplayObject3D();
  185. __holder.x        = -420;
  186. __holder.y        = 280
  187.  
  188. scene.addChild(__holder);
  189.  
  190. for(var k:uint = 0;k<blocksArray.length;k++)
  191. {
  192. var plane:Plane = new Plane(blocksArray[k],__blockWidth,__blockHeight,3,3);
  193.  
  194. var columns:Number        = __col;
  195. var cellWidth:Number    = __blockWidth;
  196. var cellHeight:Number    = -__blockHeight;
  197. var cellX:Number        = k % columns;
  198. var cellY:Number        = Math.floor(k / columns);
  199.  
  200. plane.x = (cellX * cellWidth);
  201. plane.y = cellY * cellHeight;
  202.  
  203. planeArray.push({plane:plane,pX:plane.x,pY:plane.y,pZ:plane.z,pR:plane.rotationY})
  204.  
  205. __holder.addChild(plane);
  206. }
  207. Later.call(this, explode, 500, true);
  208. }
  209.  
  210. //-----------------------------------------------------------------------
  211. //Start enterframe render event
  212. //-----------------------------------------------------------------------
  213.  
  214. public function initEvents():void
  215. {
  216. this.addEventListener(Event.ENTER_FRAME,renderEvent);
  217. }
  218.  
  219. //-----------------------------------------------------------------------
  220. //Killin it
  221. //-----------------------------------------------------------------------
  222.  
  223. public function killEvents():void
  224. {
  225. this.removeEventListener(Event.ENTER_FRAME,renderEvent);
  226. }
  227.  
  228. //-----------------------------------------------------------------------
  229. //Render the stage
  230. //-----------------------------------------------------------------------
  231.  
  232. protected function renderEvent(e:Event):void
  233. {
  234. for(var i:uint = 0; i<planeArray.length;i++)
  235. {
  236. var plane = planeArray[i].plane;
  237.  
  238. plane.yaw(__yawAmount);
  239.  
  240. }
  241.  
  242. renderer.renderScene(scene,camera,viewport);
  243. }
  244.  
  245. protected function resumeYaw():void
  246. {
  247. __yawAmount = 10;
  248. }
  249.  
  250. //-----------------------------------------------------------------------
  251. //The explosive magic (could be on a button click instead of later calls)
  252. //-----------------------------------------------------------------------
  253.  
  254. protected function explode(e:Event = null):void
  255. {
  256. for(var i:uint = 0; i<planeArray.length;i++)
  257. {
  258. var plane = planeArray[i].plane;
  259. var __x:Number = Math.random() * 3000 - 1000;
  260. var __y:Number = Math.random() * 1000 - 500;
  261. var __z:Number = Math.random() * 1000 - 500;
  262. var __rY:Number = Math.random()* 720;
  263.  
  264. Tweener.addTween(plane,{x:__x,y:__y,z:__z,rotationY:__rY,transition:"easeInOutQuint",time:1})
  265. }
  266.  
  267. Tweener.addTween(camera,{z:__startCamZ,x:1000,y:-400,transition:"easeInOutQuint",time:1,onComplete:resumeYaw})
  268.  
  269. Later.call(this, putBackTogether, 5000, true);
  270. }
  271.  
  272. //-----------------------------------------------------------------------
  273. //The Humpty Dumpty
  274. //-----------------------------------------------------------------------
  275.  
  276. protected function putBackTogether(e:Event = null):void
  277. {
  278. Tweener.addTween(camera,{z:-1000,x:0,y:0,transition:"easeOutQuint",time:.3})
  279. __yawAmount = 0;
  280. for(var i:uint = 0; i<planeArray.length;i++)
  281. {
  282. var plane:Plane = planeArray[i].plane;
  283. var __x:Number = planeArray[i].pX
  284. var __y:Number = planeArray[i].pY
  285. var __z:Number = planeArray[i].pZ
  286. var __rY:Number = planeArray[i].pR
  287.  
  288. Tweener.addTween(plane,{x:__x,y:__y,z:__z,rotationY:__rY,delay:.3,transition:"easeInOutQuint",time:1})
  289. }
  290. Later.call(this, explode, 5000, true);
  291. }
  292.  
  293. }
  294. }


Follow papervision2 on Twitter

RSS Feed