Speaking

360Flex Milan is HERE!

Domani inizia la prima 360 Conference europea, l’eccitazione √® alle stelle…Stasera ci sar√† il ricevimento degli speakers in quel di Milano, finalmente stringer√≤ la mano a Tom e John…e da domani YEAH, 3 giornate ricche di sessions!Ricordo che la prima giornata (domani, luned√¨ 7) √® GRATUITA, mentre la sera ci sar√† un “evento nell’evento” aperto a tutti organizzato da Actionscript.it in cui si discuteranno i temi caldi della giornata 🙂
Vi lascio con questo esempio in papervision e vi aspetto alla mia session marted√¨ 😀

Questo il codice sorgente:

package{
	import flash.display.BlendMode;
	import flash.display.Sprite;
	import flash.events.AsyncErrorEvent;
	import flash.events.Event;
	import flash.events.NetStatusEvent;
	import flash.events.SecurityErrorEvent;
	import flash.geom.ColorTransform;
	import flash.media.Video;
	import flash.net.NetConnection;
	import flash.net.NetStream;
	import org.papervision3d.cameras.Camera3D;
	import org.papervision3d.core.effects.BitmapFireEffect;
	import org.papervision3d.core.effects.utils.BitmapDrawCommand;
	import org.papervision3d.core.geom.renderables.Vertex3D;
	import org.papervision3d.core.layers.BitmapEffectLayer;
	import org.papervision3d.materials.BitmapMaterial;
	import org.papervision3d.materials.VideoStreamMaterial;
	import org.papervision3d.objects.primitives.Plane;
	import org.papervision3d.render.BasicRenderEngine;
	import org.papervision3d.scenes.Scene3D;
	import org.papervision3d.view.Viewport3D;
	[SWF(width="400",height="400",frameRate="24",backgroundColor="0x111111")]
	public class Flag3D extends Sprite	{
		[Embed (source="assets/360.png")] private var FlagAsset:Class;
		private var _scene:Scene3D;
		private var _cam:Camera3D;
		private var _view:Viewport3D;
		private var _render:BasicRenderEngine;
		private var _bfx:BitmapEffectLayer;
		private var _p:Plane;
		private var _bgp:Plane;
		private var _sine:Number=0;
		private var _srcs:Array = [
		//"http://www.youtube.com/get_video?video_id=Qk1qSpM63u4&t=OEgsToPDskIHja8NsMXSksfwHJKkwY2t",
		"http://www.flashfuck.it/360/get_video.flv"
		];
		private var _conn:NetConnection;
		private var _video:Video;
		private var _stream:NetStream;

		public function Flag3D()
		{
			super();
			stage.scaleMode = "noScale";
			stage.align = "TL";
			initVideo();
			init3D();
		}

		private function initVideo():void
		{
			_video = new Video(320,240);
			connection();
			trace("init video")
		}

		private function init3D():void
		{
			trace("init 3d")
			_view = new Viewport3D(0,0,true,true,true,true);
			_view.addEventListener(Event.ADDED_TO_STAGE,onViewPortReady);
			_cam = new Camera3D();
			_cam.focus=100;
			_cam.zoom=11;
			_cam.z=-1000;
			_scene = new Scene3D();
			_render = new BasicRenderEngine();
			addChild(_view);
		}

		private function onViewPortReady(e:Event):void
		{
			addElements();
			setupEffects();
		}

		private function setupEffects():void
		{
			_bfx = new BitmapEffectLayer(_view.width,_view.height);
			_bfx.addEffect( new BitmapFireEffect(1,1,1,1) );
			_bfx.drawCommand = new BitmapDrawCommand(null,new ColorTransform(1,1,1,1),BlendMode.HARDLIGHT,true);
			_bfx.drawLayer.blendMode = BlendMode.OVERLAY;
			_view.addRenderLayer(_bfx);
			_p.material.renderLayer = _bfx;
		}

		private function addElements():void
		{
			trace("add plane");
			_p = createFlag();
			_scene.addChild(_p);
			_bgp = createBG();
			_scene.addChild(_bgp);
			startRendering();
		}

		public function startRendering():void
		{
			this.addEventListener(Event.ENTER_FRAME,render);
		}

		public function stopRendering():void
		{
			this.removeEventListener(Event.ENTER_FRAME,render);
		}

		private function createFlag():Plane
		{
			trace("plane");
			var p:Plane = new Plane(createBitmapMaterial(),200,200,5,5);
			return p;
		}

		private function createBG():Plane
		{
			trace("plane");
			var p:Plane = new Plane(createVideoMaterial(),3200,2400,5,5);
			p.z = 1800;
			return p;
		}

		private function createBitmapMaterial():BitmapMaterial
		{
			var m:BitmapMaterial = new BitmapMaterial(new FlagAsset().bitmapData);
			m.doubleSided=true;
			m.smooth=true;
			return m;
		}

		private function createVideoMaterial():VideoStreamMaterial
		{
			var m:VideoStreamMaterial = new VideoStreamMaterial(_video,_stream);
			m.smooth=true;
			m.doubleSided=true;
			return m;
		}

		private function render(e:Event=null):void
		{
			_cam.hover(1,_view.containerSprite.mouseX/400,_view.containerSprite.mouseY/400);
			_p.yaw(.2);
			_render.renderScene(_scene,_cam,_view);
			_sine++;
			tweenPlaneMesh();
		}

		private function tweenPlaneMesh():void
		{
			var _deltaZ:Number= 30;
			var count:int=0;
			//plane waving:)
			for each(var v:Vertex3D in _p.geometry.vertices){
				var _vz:Number = Math.sin((count+_sine)/10)*_deltaZ;
				v.z = _vz;
				count++;
			}
		}
		private function connection():void
		{
			_conn = new NetConnection();
			_conn.addEventListener(NetStatusEvent.NET_STATUS,netHandler);
			_conn.addEventListener(SecurityErrorEvent.SECURITY_ERROR,crossdomainHandler);
			_conn.connect(null);
		}
		private function netHandler(e:NetStatusEvent):void
		{
			switch(e.info.code){
			case "NetConnection.Connect.Success":
                    startVideo();
                break;
                case "NetStream.Play.StreamNotFound":
                    trace("404!");
           		break;
			}
		}

		private function crossdomainHandler(e:SecurityErrorEvent):void
		{
			trace("security error: "+e);
		}

		private function startVideo():void
		{
			_stream = createStream();
			_video.attachNetStream(_stream);
			_stream.play(_srcs[0]);
		}

		private function createStream():NetStream
		{
			var stream:NetStream = new NetStream(_conn);
			stream.addEventListener(AsyncErrorEvent.ASYNC_ERROR,manageAsync);
			stream.addEventListener(NetStatusEvent.NET_STATUS,netHandler);
			return stream;
		}

		private function manageAsync(e:AsyncErrorEvent):void
		{
			//nada de nada :D
		}
	}
}

Download Source

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s