Panorama Viewer


*FIXED!!*

Here's a really simple example, all it does is texture a sphere and rotate it depending on what the camera is doing:

Click Here (1.3Mb download so I didn't embed this one)

Source Code (Uses the Base Class):

Actionscript:
  1. /**
  2. * ...
  3. * @author Luke Mitchell
  4. * @version 1
  5. */
  6.  
  7. package  {
  8.     import org.papervision3d.materials.BitmapFileMaterial;
  9.     import flash.events.MouseEvent;
  10.     import org.papervision3d.materials.ColorMaterial;
  11.     import org.papervision3d.materials.utils.MaterialsList;
  12.     import org.papervision3d.objects.primitives.Cube;
  13.     
  14.     public class Main extends PaperBase {
  15.       
  16.        public var materials:MaterialsList = new MaterialsList(
  17.        {
  18.          front: new BitmapFileMaterial("http://papervision2.com/wp-content/pano/s1.gif"),
  19.          back: new BitmapFileMaterial("http://papervision2.com/wp-content/pano/s3.gif"),
  20.          left: new BitmapFileMaterial("http://papervision2.com/wp-content/pano/s4.gif"),
  21.          right: new BitmapFileMaterial("http://papervision2.com/wp-content/pano/s2.gif"),
  22.          top: new ColorMaterial(0x000000),
  23.          bottom: new ColorMaterial(0x000000)
  24.        });
  25.        public var cubemap:Cube = new Cube(materials, 2000, 2000, 900, 5, 5, 5,Cube.ALL);
  26.       
  27.        public function Main() {
  28.          init(600,300);
  29.          cubemap.x = 0;
  30.          cubemap.y = 0;
  31.          cubemap.z = 0;
  32.          
  33.          default_scene.addChild(cubemap);
  34.          default_camera.z = 0;
  35.          default_camera.x = 0;
  36.          default_camera.y = 0;
  37.          default_camera.lookAt(cubemap);
  38.          
  39.          default_camera.zoom = 5;
  40.          stage.addEventListener(MouseEvent.MOUSE_WHEEL, mWheel);
  41.        }
  42.       
  43.        public function mWheel(e:MouseEvent):void {
  44.          default_camera.zoom += e.delta / 5;
  45.          if (default_camera.zoom <5) {
  46.               default_camera.zoom = 5;
  47.          }else if (default_camera.zoom> 20) {
  48.               default_camera.zoom = 20;
  49.          }
  50.        }
  51.       
  52.        override protected function processFrame():void {
  53.          cubemap.yaw( -((stage.mouseX - (stage.width / 2)) / stage.width)*4);
  54.          default_camera.rotationX = -((stage.mouseY - (stage.height / 2)) / stage.height) * (Math.sqrt(default_camera.zoom*6));
  55.        }
  56.     }
  57. }

You can use this for pretty much any panoramic image, just change the image url and tweak it a bit :)

-Luke



Physics in Papervision


Hi Everyone!

Today I've been experimenting with Verlet physics, and using Papervision to render the results. Here's what I've come up with:

It's a little bit buggy, but pretty good for a quick papervision project!

I had to modify the DisplayObject3D class a bit, It's a tiny modification which just allows you to store an acceleration and "previous position" in any object. I added the variables because it was the easiest and quickest way to store this information.. And my physics formula needs them.

If you want to play with it, you'll have to download my modified DisplayObject3D class by clicking here

Just replace your current file (located in Greatwhite/src/org/papervision3d/objects) with my one. It won't affect the performance or anything on projects which also use this class (I've only added two or three lines).

Here is the code (Uses the base class and modified DisplayObject3D):

Actionscript:
  1. package  {
  2.     
  3.     import flash.display.StageQuality;
  4.     import org.papervision3d.core.geom.renderables.Vertex3D;
  5.     import org.papervision3d.lights.PointLight3D;
  6.     import org.papervision3d.materials.shadematerials.FlatShadeMaterial;
  7.     import org.papervision3d.materials.shadematerials.PhongMaterial;
  8.     import org.papervision3d.objects.DisplayObject3D;
  9.     import org.papervision3d.objects.primitives.Plane;
  10.     import org.papervision3d.objects.primitives.Sphere;
  11.  
  12.     public class Main extends PaperBase {
  13.       
  14.        public var light:PointLight3D = new PointLight3D(true);
  15.        public var physBox:DisplayObject3D = new DisplayObject3D(); //We'll keep all of the physical objects in here..
  16.        public var sidemat:FlatShadeMaterial = new FlatShadeMaterial(light, 0xBBBBBB, 0x000000);
  17.        public var floor:Plane = new Plane(sidemat, 1100, 1100, 10, 10);
  18.       
  19.        public function Main() {
  20.          stage.quality = StageQuality.LOW; //Set the stage quality to low (for speed)
  21.          init();
  22.        }
  23.       
  24.        override protected function init3d():void {
  25.          //Position the floor
  26.          floor.y = -100;
  27.          floor.pitch(90);
  28.          default_scene.addChild(floor);
  29.          
  30.          //Position the camera
  31.          default_camera.y = 300;
  32.          default_camera.z = 1500;
  33.          default_camera.lookAt(floor);
  34.          
  35.          //Position the light
  36.          light.x = 0;
  37.          light.y = 300;
  38.          light.z = 500;
  39.          
  40.          //Create 10 Spheres, and put them in our Physics Box (Anything you add to physbox will be simulated, only spheres look right)
  41.          for (var x:Number = 0; x <10; x++) {
  42.               var sph:Sphere = new Sphere(new PhongMaterial(light, 0xBBBBBB, Math.round(Math.random() * 10000), 20), 100, 10, 8);
  43.               sph.x = (Math.random() * 40) - 20; // Position it slightly randomly
  44.               sph.z = (Math.random() * 40) - 20;
  45.               sph.y = (300 * x); // Put it 300 px above the previous one
  46.               sph.PreviousPosition = new Vertex3D(sph.x, sph.y, sph.z); // This stops the verlet from going crazy
  47.               physBox.addChild(sph); // Add it to the physbox
  48.          }
  49.          default_scene.addChild(physBox); // Add the physbox to the scene
  50.        }
  51.       
  52.        override protected function processFrame():void {
  53.       
  54.          default_camera.moveLeft(20); //This will rotate our camera around the scene,
  55.          default_camera.lookAt(floor);//whilst keeping it looking at the middle.
  56.          
  57.          for (var item:String in physBox.children) { // This loop is run for every sphere in our physBox.
  58.               var citem:DisplayObject3D = physBox.getChildByName(item); // Get the sphere.
  59.               var lastposTmp:Vertex3D = getPos(citem); // Temporarily store it's position.
  60.               // This line of code is known as "Verlet". Wikipedia it for more info.
  61.               setPos(citem, vertexAdd(getPos(citem), vertexAdd(vertexSubtract(lastposTmp, citem.PreviousPosition), citem.Acceleration)));
  62.               citem.PreviousPosition = lastposTmp; // Update the previous position..
  63.               citem.Acceleration = new Vertex3D(0, -8.3, 0); // Apply Gravity
  64.               constrain(citem); // This code checks if the sphere has collided with the environment.
  65.              
  66.               for (var item2:String in physBox.children) { //Check to see if it's hit any other spheres.
  67.                   if (item != item2) {
  68.                      var pitem:DisplayObject3D = physBox.getChildByName(item2);
  69.                      dohittest(pitem, citem); // Check
  70.                   }
  71.               }
  72.          }
  73.        }
  74.       
  75.        private function dohittest(o1:DisplayObject3D, o2:DisplayObject3D):void {
  76.          if (o1.hitTestObject(o2)) { // IF the two objects have collided, then
  77.               var xmag:Number = (o1.x - o2.x)/2.1; // Get the distance
  78.               var ymag:Number = (o1.y - o2.y)/2.1;
  79.               var zmag:Number = (o1.z - o2.z)/2.1;
  80.               // Move the objects away from eachother
  81.               setPos(o1, vertexAdd(new Vertex3D(xmag, ymag, zmag), getPos(o1)));
  82.               setPos(o2, vertexSubtract(getPos(o2),new Vertex3D(xmag, ymag, zmag)));
  83.          }
  84.        }
  85.       
  86.        private function constrain(obj:DisplayObject3D):void {
  87.          if (obj.x <500 &amp;&amp; obj.x> -500 &amp;&amp; obj.z <500 &amp;&amp; obj.z> -500){ // If the object is over the floor
  88.               if (obj.y <0) { // If it's hit the floor
  89.                   // Move it out of the floor.
  90.                   if((obj.y - obj.PreviousPosition.y) <-10){
  91.                      obj.y = (obj.PreviousPosition.y * 1.2);
  92.                      if (obj.y <0) {
  93.                        obj.y = 0;
  94.                      }
  95.                   }else {
  96.                      obj.y = 0;
  97.                   }
  98.               }
  99.          }
  100.          // If the object is way off the screen, put it back at the top.
  101.          if (obj.y <-10000) {
  102.               obj.y = 300 + (Math.random() * 5000);
  103.               obj.x = (Math.random() * 20) - 40;
  104.               obj.z = (Math.random() * 20) - 40;
  105.               obj.PreviousPosition = new Vertex3D(obj.x, obj.y, obj.z);
  106.          }
  107.        }
  108.       
  109.        // --- These functions are used to do general addition and stuff ---
  110.       
  111.        private function vertexSubtract(v1:Vertex3D, v2:Vertex3D):Vertex3D {
  112.          return new Vertex3D(v1.x - v2.x, v1.y - v2.y, v1.z - v2.z);
  113.        }
  114.       
  115.        private function vertexAdd(v1:Vertex3D, v2:Vertex3D):Vertex3D {
  116.          return new Vertex3D(v1.x + v2.x, v1.y + v2.y, v1.z + v2.z);
  117.        }
  118.       
  119.        private function getPos(object:DisplayObject3D):Vertex3D {
  120.          return new Vertex3D(object.x, object.y, object.z);
  121.        }
  122.       
  123.        private function setPos(object:DisplayObject3D, point:Vertex3D):void {
  124.          object.x = point.x;
  125.          object.y = point.y;
  126.          object.z = point.z;
  127.        }
  128.        // --- ---
  129.     }  
  130. }

I hope it's commented enough :D

Have fun!



Playing with filters


I've been playing with applying flash filters to the viewport object and wanted to share a few examples of what can be done!

This is NOT the effects branch of papervision, this is simply applying filters onto the viewport object, really simple and fast, check it out:

Cel Shading!!

"Dazed" Effect:

Shadow:

How cool is that?! Fast too.

Cel-Shading Code:

Actionscript:
  1. package  {
  2.     
  3.     import org.papervision3d.objects.DisplayObject3D;
  4.     import PaperBase;
  5.     import org.papervision3d.objects.parsers.Collada;
  6.     import org.papervision3d.materials.BitmapFileMaterial;
  7.     import flash.filters.*;
  8.     
  9.     public class Main extends PaperBase {
  10.       
  11.        public var cow:DisplayObject3D;
  12.        public var filter:GlowFilter = new GlowFilter (0x000000,1,5,5,50,1,false,false);
  13.       
  14.        public function Main() {
  15.          init(300, 200);
  16.          viewport.filters = [filter];
  17.        }
  18.       
  19.        override protected function init3d():void {
  20.          cow = new Collada("http://papervision2.com/wp-content/downloads/dae/cow.dae");
  21.          cow.moveDown(100);
  22.          cow.scale = 3;
  23.          cow.pitch( -30);
  24.          default_scene.addChild(cow);
  25.        }
  26.       
  27.        override protected function processFrame():void {
  28.          cow.yaw(5);
  29.        }
  30.       
  31.     }
  32.     
  33. }

Try changing the line:
public var filter:GlowFilter = new GlowFilter (0x000000,1,5,5,50,1,false,false);

To one of the following:
public var filter:BlurFilter = new BlurFilter();
or

public var filter:BevelFilter = new BevelFilter();
or any other filters you can think of!

Have fun :)


Papervision 2 is proudly powered by WordPress and themed by Mukka-mu