Monday, January 25, 2010

Simple Stereo 3d Anaglpyh example


Download FLA

import flash.display.*;
import flash.events.*;
import flash.geom.*;
import flash.utils.*;

// Picture is a movie clip symbol in the Library
var loader:Picture = new Picture();
var loader2:Picture = new Picture();
var scene:Sprite = new Sprite();

// for drawing scene from left eye view
var left:Bitmap = new Bitmap();
// for drawing scene from right eye view
var right:Bitmap = new Bitmap();
// background for correct blending
var bg:Sprite = new Sprite();

var leftData:BitmapData = new BitmapData(stage.stageWidth, stage.stageHeight);
var rightData:BitmapData = new BitmapData(stage.stageWidth, stage.stageHeight);

var distance:Number = 4; // left-right eye distance


init();
prepareStereo();

function prepareStereo():void
{
left.blendMode = BlendMode.SCREEN;
// right eye = green and blue channel
right.transform.colorTransform = new ColorTransform(0, 1, 1, 1);
// left eye = red channel
left.transform.colorTransform = new ColorTransform(1, 0, 0, 1);
left.bitmapData = leftData;
right.bitmapData = rightData;
setBackground();
this.addChild(right);
this.addChild(left);
// set the UI textbox and label to the top
this.setChildIndex(ui_mc, this.numChildren-1);
}
function setBackground(color:int = 0):void{
bg.graphics.beginFill(color);
bg.graphics.drawRect(-2000, -2000, 4000, 4000);
}
function init():void{
loader.x = 200;
loader.y = 200;
loader2.y = 200;
loader2.z = 300;
stage.addEventListener(MouseEvent.MOUSE_MOVE, move);
this.addEventListener(Event.ENTER_FRAME, update);
scene.addChild(bg);

scene.addChild(loader2);
scene.addChild(loader);
ui_mc.distance_txt.text = String(distance);
}
function update(e:Event):void{
// clear screen
rightData.fillRect(rightData.rect, 0xFFFFFF);
leftData.fillRect(rightData.rect, 0xFFFFFF);
distance = Number(ui_mc.distance_txt.text);
loader.x -= distance / 2; // move scene to the left for right eye
loader2.x -= distance / 2;
rightData.draw(scene);
loader.x += distance; // move scene to the right for left eye
loader2.x += distance;
leftData.draw(scene);
loader.x -= distance / 2; // move scene back to original position
loader2.x -= distance / 2;
}
function move(e:MouseEvent):void
{
loader.rotationY = (mouseY*2/stage.stageHeight - 1) * 60;
loader2.rotationY = (1-mouseY*2/stage.stageHeight) * 60;
loader.x = stage.stageWidth - mouseX;
loader2.x = mouseX;
}

No comments: