Marc Hughes


Home
Blog
Twitter
LinkedIn
GitHub
about
I am a developer from a bit west of Boston.

Lazy day, playing with papervision

16 Dec 2007

Snowed in today, so I took a few hours to play with some stuff I've been meaning to look into. I've been doing so much Flex/AIR work lately my fun side has been lacking.



First up was PaperVision3d.  Nothing spectacular, just playing with bitmaps on planes and moving the camera around. But that also gave me a chance to explore Tweener, which might become my tween engine of choice for AS3 until GO comes out. I rather liked using it.



http://rogue-development.com/experiments/HomePageMovie.swf



So , like I said it's nothing spectacular but if you want the source, it's listed below. One thing I still don't know how to do is determine how far away to put the camera from a 3d object so it will fill the screen.


package {
import caurina.transitions.Tweener;

import flash.display.Bitmap;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageQuality;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.events.MouseEvent;

import org.papervision3d.cameras.FreeCamera3D;
import org.papervision3d.materials.BitmapMaterial;
import org.papervision3d.objects.Plane;
import org.papervision3d.scenes.Scene3D;


[SWF(backgroundColor="#000000", frameRate="60")]
public class HomePageMovie extends Sprite
{
private var paperCanvas:Sprite;
private var camera:FreeCamera3D;
private var scene:Scene3D;

[Embed(source="screenshot1.png")]
private var ss1:Class;
[Embed(source="screenshot2.png")]
private var ss2:Class;
[Embed(source="screenshot3.png")]
private var ss3:Class;
[Embed(source="screenshot4.png")]
private var ss4:Class;
[Embed(source="screenshot5.png")]
private var ss5:Class;
[Embed(source="screenshot6.png")]
private var ss6:Class;
[Embed(source="screenshot7.png")]
private var ss7:Class;
[Embed(source="screenshot8.png")]
private var ss8:Class;

private var screenshotBitmaps:Array = [ss1,ss2,ss3,ss4,ss5,ss6,ss7,ss8];
private var screenshotPlanes:Array = [];

[Embed(source="background.png")]
private var backgroundImage:Class;

private var backgroundPlane:Plane;

public function HomePageMovie()
{
paperCanvas = new Sprite();
addChild(paperCanvas);

scene = new Scene3D( paperCanvas );

stage.addEventListener(Event.RESIZE, onStageResize);
stage.quality = StageQuality.MEDIUM;
stage.scaleMode = StageScaleMode.NOSCALE;
stage.align = StageAlign.TOP
LEFT;


camera = new FreeCamera3D();
//camera = new Camera3D();
camera.x = 0;
camera.y = 0;
camera.z = -200;
camera.zoom = 2;
camera.focus = 200;
//var stars:Stars = new Stars(new ColorMaterial(0xffffff) , paperCanvas);
//scene.addChild(stars);
var depth:Number = 0;

stage.addEventListener(MouseEvent.CLICK, onClick );

for each (var c:Class in screenshotBitmaps )
{
var bm:Bitmap = new c();
var mat:BitmapMaterial = new BitmapMaterial(bm.bitmapData);
mat.doubleSided = true;

var p:Plane = new Plane(mat ,0,0,4,4);
p.extra = {width:bm.width, height:bm.height};
p.x = Math.random() * 6000;
p.y = Math.random() * 6000;
p.z = Math.random() * 6000;
scene.addChild( p );
screenshotPlanes.push( p );
}



this.addEventListener( Event.ENTER_FRAME, loop3D );
relayout();

}

protected function onClick(e:MouseEvent):void
{
return;
if( stage.displayState == "fullScreen" )
{
stage.displayState = "normal"
}
else
{
stage.displayState = "fullScreen";
}

}

private function relayout() : void
{

screenshotPlanes.push( screenshotPlanes.shift() );
var i:Number = 0;

var p:Plane = screenshotPlanes[0];
Tweener.addTween( camera, { x:p.x , y:p.y , time:4 ,transition:"easeInOutCubic"} );
Tweener.addTween( camera, { z:Math.min(camera.z-1500,p.z-2500), time:2 ,transition:"easeOutQuart"} );

var d:Number = stage.stageWidth/p.extra.width;
Tweener.addTween( camera, { z:p.z - 200 * d, time:2, delay:2 ,transition:"easeInOutQuart"} );
Tweener.addTween( this, { onComplete:relayout, delay:6} );
}



private function loop3D(event:Event):void
{
scene.renderCamera( camera );
}



private function onStageResize(event:Event):void
{
paperCanvas.x = stage.stageWidth/2;
paperCanvas.y = stage.stageHeight/2;
}
}
}