First tests with Papervision3D

cube experiment

Playing around with Papervision3D. It’s pretty cool, and i can see that it will grow quickly beyond its showing off stage. Real world applications are popping up left and right. While still far from getting to that point, every programmer has to suffer through the learning curves of understanding the class hierarchy and worse, 3 dimensional space. This piece above is my first success at applying video as a material onto a cube face. There is actually two cubes out there, you just have to “pan” around for the other. Anyway, in my learning, the following are noteworthy:

  • “primitives”- the built in display objects in Papervision. These are (to my knowledge)-Cube, Cylinder, Cone, Plane (like a sheet of paper), Paperplane, & sphere- the cube is the learning curve, as it needs a materialsList- huh? yeah, essentially, an associative array of predefined materials used to cover the cube. The simplest is to define at least one material, and assign it to all sides of the cube:  materialsListName({all:yourmaterial})- the “all” in that anonymous object refers to all sides…you could do front, back , left , right ,top, and bottom if you want different materials on given sides.
  • “material” is essentially what makes a surface on a papervision display object.- you can go simple with wireframes or colors, to more complex MovieAssetMaterial- NOTE: MoveiAssetMaterial vs MovieMaterial…the asset refers to the class name in your library, while the other is useful for a MovieClip instance. The big winner in the MoveMaterial is that you can load dynamic images as material.
  • “camera”- two common kinds- the standard (Camera3D) and the FreeCamera (spelling?). The only difference i see here is that the standard camera is a lot easier to control, as it maintains a “focal point” to the viewport object….the huh?
  • “viewport”-ok, its the 3d space- that’s how i understand it- And don’t forget to add it to the stage! After all, it is a DisplayObject
  • finally, don’t forget…all this is useless without a Scene3D and a BasicRenderEngine.
So, now that we addressed the basics to creating some papervision, let’s look at the order of events.
  1. declare your class- simplest thing (for getting the concepts) is to whack this thing into you DocumentClass. That’s gonna be the engine of this “application”
  2. you have two options now….the traditional, build from the floor up…or the premade class called BasicView. The demo above uses basicview, but if you are just starting, you should read step 3
  3. Good for you…keep reading….Again, to keep it easy, let’s declare some global (but private) variables. These will be your Viewport3D, Scene3D, BasicRenderEngine, Camera3D, and your primitive object of choice- call these whatever is easiest for you to remember.
  4. lay these all out in your init function… or the constructor of your class.
  5. begin the build out…to save us all time, here is my ugly, un-optomized source…remember, these are layman’s terms
Good luck out there…. remember, it’s hard to get it right the first few times, but it will come.

source:

package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.events.MouseEvent;
import flash.media.SoundTransform;
import flash.media.Video;
import flash.net.NetConnection;
import flash.net.NetStream;
 
import org.papervision3d.cameras.FreeCamera3D;
import org.papervision3d.materials.ColorMaterial;
import org.papervision3d.materials.VideoStreamMaterial;
import org.papervision3d.materials.utils.MaterialsList;
import org.papervision3d.objects.primitives.Cube;
import org.papervision3d.view.BasicView;
 
public class Inside extends Sprite
{
private var bv:BasicView;
private var dir:String="none"
private var zoomable:Boolean
private var drag:Boolean=false
public function Inside()
{
super();
bv= new BasicView(0,0,true,true,"FREECAMERA3D")
addChild(bv)
var mt:ColorMaterial= new ColorMaterial()
 
//add vid
var NC:NetConnection=new NetConnection()
NC.connect(null)
var NS:NetStream= new NetStream(NC);
var ns_client:Object= new Object();
ns_client.onMetaData=metaDataReceived
NS.client=ns_client;
var video:Video= new Video(300,240)
video.attachNetStream(NS);
var channel:SoundTransform=new SoundTransform()
channel.volume=0
NS.soundTransform=channel;
NS.play("media/vid.flv")
var vid:VideoStreamMaterial=new VideoStreamMaterial(video,NS)
 
//create cube
 
var ml:MaterialsList= new MaterialsList({all:mt,back:vid})
var cube:Cube= new Cube(ml)
cube.name="c"
 
var cube2:Cube= new Cube(ml)
cube2.x=5000
cube2.name="c2"
 
bv.scene.addChild(cube)
bv.scene.addChild(cube2)
 
stage.addEventListener(KeyboardEvent.KEY_DOWN,direction);
stage.addEventListener(KeyboardEvent.KEY_UP,stopdir)
stage.addEventListener(MouseEvent.MOUSE_DOWN,zoom)
stage.addEventListener(MouseEvent.MOUSE_UP,endZoom)
this.addEventListener(Event.ENTER_FRAME,rend)
}
private function zoom(me:MouseEvent):void
{
zoomable=true
drag=true
}
private function endZoom(me:MouseEvent):void
{
zoomable=false;
drag=false
}
private function direction(ke:KeyboardEvent):void
{
trace(ke.keyCode)
var code=ke.keyCode;
switch(code)
{
case 37:
dir="left"
break;
 
case 38:
dir="up"
break;
 
case 39:
dir="right"
break;
 
case 40:
dir="down"
break;
 
case 87:// A key
dir="zoomin"
break;
 
case 88:// S key
dir="zoomout"
break;
}
}
private function stopdir(ke:KeyboardEvent):void
{
dir="none"
}
private function rend(e:Event)
{
var cx=(mouseX-stage.stageWidth/2)/stage.stageWidth;
var cy=(mouseY-stage.stageHeight/2)/stage.stageHeight;
var c=bv.scene.getChildByName("c")
var c2=bv.scene.getChildByName("c2")
if(drag)
{
c.rotationX+=cy*10
c.rotationY-=cx*10
 
c2.rotationX+=cy*10
c2.rotationY-=cx*10
}
var cam:FreeCamera3D = bv.camera as FreeCamera3D
if(dir=="left")
{
cam.rotationY+=10
}
if(dir=="right")
{
cam.rotationY-=10
}
if(dir=="up")
{
cam.rotationX-=10
}
if(dir=="down")
{
cam.rotationX+=10
}
if(dir=="zoomin")
{
cam.zoom++;
}
if(dir=="zoomout")
{
cam.zoom--;
}
if(dir=="none")
{
cam.rotationX=cam.rotationX;
cam.rotationY=cam.rotationY;
cam.rotationZ=cam.rotationZ;
}
if(zoomable)
{
 
}
bv.renderer.renderScene(bv.scene,bv.camera,bv.viewport)
}
private function metaDataReceived(meta:Object):void
{
trace(meta)
}
}
}

Tags: , ,

Leave a Reply