package com.lessrain.projects.luis.pv3dWhite.video3d
{
import flash.display.BitmapData;
import flash.display.Sprite;
import flash.events.AsyncErrorEvent;
import flash.events.Event;
import flash.geom.Point;
import flash.geom.Rectangle;
import flash.media.Video;
import flash.net.NetConnection;
import flash.net.NetStream;
import flash.system.Security;
import org.papervision3d.cameras.Camera3D;
import org.papervision3d.materials.BitmapMaterial;
import org.papervision3d.objects.DisplayObject3D;
import org.papervision3d.objects.primitives.Plane;
import org.papervision3d.render.BasicRenderEngine;
import org.papervision3d.scenes.Scene3D;
import org.papervision3d.view.Viewport3D;
/**
* @author Luis Martinez, Less Rain (luis@lessrain.com)
*/
public class Video3D
{
private var _target : Sprite;
private var _container : Sprite;
private var _viewport : Viewport3D;
private var _renderer : BasicRenderEngine;
private var _scene3D : Scene3D;
private var _camera : Camera3D;
private var _rootNode : DisplayObject3D;
private var _connection : NetConnection;
private var _stream : NetStream;
private var _video : Video;
private var _videoWidth : Number = 320;
private var _videoHeight : Number = 240;
private var _videoTexture : BitmapData;
private var _planeTexture1 : BitmapData;
private var _planeTexture2 : BitmapData;
private var _srcRect1 : Rectangle;
private var _srcRect2 : Rectangle;
private var _basePoint : Point = new Point(0, 0);
public function Video3D()
{
Security.loadPolicyFile("http://www.yourdomain.com/cross-domain.xml");
}
public function initialize() : void
{
_srcRect1 = new Rectangle(0, 0, _videoWidth>> 1, _videoHeight);
_srcRect2 = new Rectangle(_videoWidth>> 1, 0, _videoWidth>> 1, _videoHeight);
_videoTexture = new BitmapData(_videoWidth, _videoHeight);
_planeTexture1 = new BitmapData(_videoWidth>> 1, _videoHeight);
_planeTexture2 = new BitmapData(_videoWidth>> 1, _videoHeight);
initialize3D();
loadVideo();
createPlanes();
_container.addEventListener(Event.ENTER_FRAME, loop3D);
}
private function initialize3D() : void
{
_container = new Sprite();
_target.addChild(_container);
_viewport = new Viewport3D(990, 600, false, false);
_container.addChild(_viewport);
_renderer = new BasicRenderEngine();
_scene3D = new Scene3D();
_camera = new Camera3D();
_camera.zoom = 8;
_camera.focus = 200;
_rootNode = new DisplayObject3D();
_scene3D.addChild(_rootNode);
}
private function loadVideo() : void
{
var customClient : Object = new Object();
customClient["onCuePoint"] = cuePointHandler;
customClient["onMetaData"] = metaDataHandler;
_connection = new NetConnection();
_connection.connect(null);
_stream = new NetStream(_connection);
_stream.checkPolicyFile = true;
_stream.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler);
_stream.client = customClient;
_video = new Video(_videoWidth, _videoHeight);
_video.attachNetStream(_stream);
_stream.play('http://www.blog.lessrain.com/wp-content/upload/papervideo/myvideo.flv');
}
private function cuePointHandler(infoObject_ : Object) : void
{
}
private function metaDataHandler(infoObject_ : Object) : void
{
}
private function asyncErrorHandler(event : AsyncErrorEvent) : void
{
}
private function createPlanes() : void
{
var material1 : BitmapMaterial = new BitmapMaterial(_planeTexture1);
material1.oneSide = false;
var plane1 : DisplayObject3D = _rootNode.addChild(new Plane(material1, _videoWidth>> 1, _videoHeight, 4, 4), "plane1");
plane1.x = -_videoWidth>> 2;
var material2 : BitmapMaterial = new BitmapMaterial(_planeTexture2);
material2.oneSide = false;
var plane2 : DisplayObject3D = _rootNode.addChild(new Plane(material2, _videoWidth>> 1, _videoHeight, 4, 4), "plane2");
plane2.z = _videoWidth>> 2;
plane2.rotationY = -90;
}
private function loop3D(event_ : Event) : void
{
_rootNode.rotationY += ((-_container.mouseX * 0.5) - _rootNode.rotationY) / 10;
_rootNode.rotationX += ((-_container.mouseY * 0.5) - _rootNode.rotationX) / 10;
_videoTexture.draw(_video);
_planeTexture1.copyPixels(_videoTexture, _srcRect1, _basePoint);
_planeTexture2.copyPixels(_videoTexture, _srcRect2, _basePoint);
_renderer.renderScene(_scene3D, _camera, _viewport);
}
public function get target() : Sprite
{
return _target;
}
public function set target(target_ : Sprite) : void
{
_target = target_;
}
}
}