PageRenderTime 442ms CodeModel.GetById 161ms app.highlight 156ms RepoModel.GetById 116ms app.codeStats 0ms

/src/away3d/containers/View3D.as

http://github.com/away3d/away3d-core-fp11
ActionScript | 1007 lines | 686 code | 187 blank | 134 comment | 95 complexity | 51a20d32f2d0a0cc8c67d410ddbdbdb7 MD5 | raw file
   1package away3d.containers
   2{
   3	
   4	import away3d.*;
   5	import away3d.cameras.*;
   6	import away3d.core.managers.*;
   7	import away3d.core.pick.*;
   8	import away3d.core.render.*;
   9	import away3d.core.traverse.*;
  10	import away3d.events.*;
  11	import away3d.textures.*;
  12	
  13	import flash.display.*;
  14	import flash.display3D.*;
  15	import flash.display3D.textures.*;
  16	import flash.events.*;
  17	import flash.geom.*;
  18	import flash.net.*;
  19	import flash.ui.*;
  20	import flash.utils.*;
  21	
  22	use namespace arcane;
  23	
  24	public class View3D extends Sprite
  25	{
  26		private var _width:Number = 0;
  27		private var _height:Number = 0;
  28		private var _localTLPos:Point = new Point();
  29		private var _localBRPos:Point = new Point();
  30		private var _globalPos:Point = new Point();
  31		private var _globalWidth:Number = 0;
  32		private var _globalHeight:Number = 0;
  33		private var _globalPosDirty:Boolean;
  34		protected var _scene:Scene3D;
  35		protected var _camera:Camera3D;
  36		protected var _entityCollector:EntityCollector;
  37		
  38		protected var _aspectRatio:Number;
  39		private var _time:Number = 0;
  40		private var _deltaTime:uint;
  41		private var _backgroundColor:uint = 0x000000;
  42		private var _backgroundAlpha:Number = 1;
  43		
  44		protected var _mouse3DManager:Mouse3DManager;
  45		
  46		protected var _touch3DManager:Touch3DManager;
  47		
  48		protected var _renderer:RendererBase;
  49		private var _depthRenderer:DepthRenderer;
  50		private var _addedToStage:Boolean;
  51		
  52		private var _forceSoftware:Boolean;
  53		
  54		protected var _filter3DRenderer:Filter3DRenderer;
  55		protected var _requireDepthRender:Boolean;
  56		protected var _depthRender:Texture;
  57		private var _depthTextureInvalid:Boolean = true;
  58		
  59		private var _hitField:Sprite;
  60		protected var _parentIsStage:Boolean;
  61		
  62		private var _background:Texture2DBase;
  63		protected var _stage3DProxy:Stage3DProxy;
  64		protected var _backBufferInvalid:Boolean = true;
  65		private var _antiAlias:uint;
  66		
  67		protected var _rttBufferManager:RTTBufferManager;
  68		
  69		private var _rightClickMenuEnabled:Boolean = true;
  70		private var _sourceURL:String;
  71		private var _menu0:ContextMenuItem;
  72		private var _menu1:ContextMenuItem;
  73		private var _ViewContextMenu:ContextMenu;
  74		protected var _shareContext:Boolean = false;
  75		protected var _scissorRect:Rectangle;
  76		private var _scissorRectDirty:Boolean = true;
  77		private var _viewportDirty:Boolean = true;
  78		
  79		private var _depthPrepass:Boolean;
  80		private var _profile:String;
  81		private var _layeredView:Boolean = false;
  82		
  83		private function viewSource(e:ContextMenuEvent):void
  84		{
  85			var request:URLRequest = new URLRequest(_sourceURL);
  86			try {
  87				navigateToURL(request, "_blank");
  88			} catch (error:Error) {
  89				
  90			}
  91		}
  92		
  93		public function get depthPrepass():Boolean
  94		{
  95			return _depthPrepass;
  96		}
  97		
  98		public function set depthPrepass(value:Boolean):void
  99		{
 100			_depthPrepass = value;
 101		}
 102		
 103		private function visitWebsite(e:ContextMenuEvent):void
 104		{
 105			var url:String = Away3D.WEBSITE_URL;
 106			var request:URLRequest = new URLRequest(url);
 107			try {
 108				navigateToURL(request);
 109			} catch (error:Error) {
 110				
 111			}
 112		}
 113		
 114		private function initRightClickMenu():void
 115		{
 116			_menu0 = new ContextMenuItem("Away3D.com\tv" + Away3D.MAJOR_VERSION + "." + Away3D.MINOR_VERSION + "." + Away3D.REVISION, true, true, true);
 117			_menu1 = new ContextMenuItem("View Source", true, true, true);
 118			_menu0.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, visitWebsite);
 119			_menu1.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, viewSource);
 120			_ViewContextMenu = new ContextMenu();
 121			
 122			updateRightClickMenu();
 123		}
 124		
 125		private function updateRightClickMenu():void
 126		{
 127			if (_rightClickMenuEnabled)
 128				_ViewContextMenu.customItems = _sourceURL? [_menu0, _menu1] : [_menu0];
 129			else
 130				_ViewContextMenu.customItems = [];
 131			
 132			contextMenu = _ViewContextMenu;
 133		}
 134		
 135		public function View3D(scene:Scene3D = null, camera:Camera3D = null, renderer:RendererBase = null, forceSoftware:Boolean = false, profile:String = "baseline")
 136		{
 137			super();
 138			_profile = profile;
 139			_scene = scene || new Scene3D();
 140			_scene.addEventListener(Scene3DEvent.PARTITION_CHANGED, onScenePartitionChanged);
 141			_camera = camera || new Camera3D();
 142			_renderer = renderer || new DefaultRenderer();
 143			_depthRenderer = new DepthRenderer();
 144			_forceSoftware = forceSoftware;
 145			
 146			// todo: entity collector should be defined by renderer
 147			_entityCollector = _renderer.createEntityCollector();
 148			_entityCollector.camera = _camera;
 149			
 150			_scissorRect = new Rectangle();
 151			
 152			initHitField();
 153			
 154			_mouse3DManager = new Mouse3DManager();
 155			_mouse3DManager.enableMouseListeners(this);
 156			
 157			_touch3DManager = new Touch3DManager();
 158			_touch3DManager.view = this;
 159			_touch3DManager.enableTouchListeners(this);
 160			
 161			addEventListener(Event.ADDED_TO_STAGE, onAddedToStage, false, 0, true);
 162			addEventListener(Event.ADDED, onAdded, false, 0, true);
 163			
 164			_camera.addEventListener(CameraEvent.LENS_CHANGED, onLensChanged);
 165			
 166			_camera.partition = _scene.partition;
 167			
 168			initRightClickMenu();
 169		}
 170		
 171		private function onScenePartitionChanged(event:Scene3DEvent):void
 172		{
 173			if (_camera)
 174				_camera.partition = scene.partition;
 175		}
 176		
 177		public function get rightClickMenuEnabled():Boolean
 178		{
 179			return _rightClickMenuEnabled;
 180		}
 181		
 182		public function set rightClickMenuEnabled(val:Boolean):void
 183		{
 184			_rightClickMenuEnabled = val;
 185			
 186			updateRightClickMenu();
 187		}
 188		
 189		public function get stage3DProxy():Stage3DProxy
 190		{
 191			return _stage3DProxy;
 192		}
 193		
 194		public function set stage3DProxy(stage3DProxy:Stage3DProxy):void
 195		{
 196			if (_stage3DProxy) {
 197				_stage3DProxy.removeEventListener(Stage3DEvent.VIEWPORT_UPDATED, onViewportUpdated);
 198				_stage3DProxy.removeEventListener(Stage3DEvent.CONTEXT3D_RECREATED, onContext3DRecreated);
 199			}
 200
 201			_stage3DProxy = stage3DProxy;
 202			
 203			_stage3DProxy.addEventListener(Stage3DEvent.VIEWPORT_UPDATED, onViewportUpdated);
 204			_stage3DProxy.addEventListener(Stage3DEvent.CONTEXT3D_RECREATED, onContext3DRecreated);
 205
 206			_renderer.stage3DProxy = _depthRenderer.stage3DProxy = _stage3DProxy;
 207			
 208			_globalPosDirty = true;
 209			_backBufferInvalid = true;
 210		}
 211
 212		private function onContext3DRecreated(event:Stage3DEvent):void {
 213			_depthTextureInvalid = true;
 214		}
 215		
 216		/**
 217		 * Forces mouse-move related events even when the mouse hasn't moved. This allows mouseOver and mouseOut events
 218		 * etc to be triggered due to changes in the scene graph. Defaults to false.
 219		 */
 220		public function get forceMouseMove():Boolean
 221		{
 222			return _mouse3DManager.forceMouseMove;
 223		}
 224		
 225		public function set forceMouseMove(value:Boolean):void
 226		{
 227			_mouse3DManager.forceMouseMove = value;
 228			_touch3DManager.forceTouchMove = value;
 229		}
 230		
 231		public function get background():Texture2DBase
 232		{
 233			return _background;
 234		}
 235		
 236		public function set background(value:Texture2DBase):void
 237		{
 238			_background = value;
 239			_renderer.background = _background;
 240		}
 241		
 242		/**
 243		 * Used in a sharedContext. When true, clears the depth buffer prior to rendering this particular
 244		 * view to avoid depth sorting with lower layers. When false, the depth buffer is not cleared
 245		 * from the previous (lower) view's render so objects in this view may be occluded by the lower
 246		 * layer. Defaults to false.
 247		 */
 248		public function get layeredView():Boolean
 249		{
 250			return _layeredView;
 251		}
 252		
 253		public function set layeredView(value:Boolean):void
 254		{
 255			_layeredView = value;
 256		}
 257		
 258		private function initHitField():void
 259		{
 260			_hitField = new Sprite();
 261			_hitField.alpha = 0;
 262			_hitField.doubleClickEnabled = true;
 263			_hitField.graphics.beginFill(0x000000);
 264			_hitField.graphics.drawRect(0, 0, 100, 100);
 265			addChild(_hitField);
 266		}
 267		
 268		/**
 269		 * Not supported. Use filters3d instead.
 270		 */
 271		override public function get filters():Array
 272		{
 273			throw new Error("filters is not supported in View3D. Use filters3d instead.");
 274			return super.filters;
 275		}
 276		
 277		/**
 278		 * Not supported. Use filters3d instead.
 279		 */
 280		override public function set filters(value:Array):void
 281		{
 282			throw new Error("filters is not supported in View3D. Use filters3d instead.");
 283		}
 284		
 285		public function get filters3d():Array
 286		{
 287			return _filter3DRenderer? _filter3DRenderer.filters : null;
 288		}
 289		
 290		public function set filters3d(value:Array):void
 291		{
 292			if (value && value.length == 0)
 293				value = null;
 294			
 295			if (_filter3DRenderer && !value) {
 296				_filter3DRenderer.dispose();
 297				_filter3DRenderer = null;
 298			} else if (!_filter3DRenderer && value) {
 299				_filter3DRenderer = new Filter3DRenderer(stage3DProxy);
 300				_filter3DRenderer.filters = value;
 301			}
 302			
 303			if (_filter3DRenderer) {
 304				_filter3DRenderer.filters = value;
 305				_requireDepthRender = _filter3DRenderer.requireDepthRender;
 306			} else {
 307				_requireDepthRender = false;
 308				if (_depthRender) {
 309					_depthRender.dispose();
 310					_depthRender = null;
 311				}
 312			}
 313		}
 314		
 315		/**
 316		 * The renderer used to draw the scene.
 317		 */
 318		public function get renderer():RendererBase
 319		{
 320			return _renderer;
 321		}
 322		
 323		public function set renderer(value:RendererBase):void
 324		{
 325			_renderer.dispose();
 326			_renderer = value;
 327			_entityCollector = _renderer.createEntityCollector();
 328			_entityCollector.camera = _camera;
 329			_renderer.stage3DProxy = _stage3DProxy;
 330			_renderer.antiAlias = _antiAlias;
 331			_renderer.backgroundR = ((_backgroundColor >> 16) & 0xff)/0xff;
 332			_renderer.backgroundG = ((_backgroundColor >> 8) & 0xff)/0xff;
 333			_renderer.backgroundB = (_backgroundColor & 0xff)/0xff;
 334			_renderer.backgroundAlpha = _backgroundAlpha;
 335			_renderer.viewWidth = _globalWidth;
 336			_renderer.viewHeight = _globalHeight;
 337			
 338			_backBufferInvalid = true;
 339		}
 340		
 341		/**
 342		 * The background color of the screen. This value is only used when clearAll is set to true.
 343		 */
 344		public function get backgroundColor():uint
 345		{
 346			return _backgroundColor;
 347		}
 348		
 349		public function set backgroundColor(value:uint):void
 350		{
 351			_backgroundColor = value;
 352			_renderer.backgroundR = ((value >> 16) & 0xff)/0xff;
 353			_renderer.backgroundG = ((value >> 8) & 0xff)/0xff;
 354			_renderer.backgroundB = (value & 0xff)/0xff;
 355		}
 356		
 357		public function get backgroundAlpha():Number
 358		{
 359			return _backgroundAlpha;
 360		}
 361		
 362		public function set backgroundAlpha(value:Number):void
 363		{
 364			if (value > 1)
 365				value = 1;
 366			else if (value < 0)
 367				value = 0;
 368			
 369			_renderer.backgroundAlpha = value;
 370			_backgroundAlpha = value;
 371		}
 372		
 373		/**
 374		 * The camera that's used to render the scene for this viewport
 375		 */
 376		public function get camera():Camera3D
 377		{
 378			return _camera;
 379		}
 380		
 381		/**
 382		 * Set camera that's used to render the scene for this viewport
 383		 */
 384		public function set camera(camera:Camera3D):void
 385		{
 386			_camera.removeEventListener(CameraEvent.LENS_CHANGED, onLensChanged);
 387			
 388			_camera = camera;
 389			_entityCollector.camera = _camera;
 390			
 391			if (_scene)
 392				_camera.partition = _scene.partition;
 393			
 394			_camera.addEventListener(CameraEvent.LENS_CHANGED, onLensChanged);
 395			
 396			_scissorRectDirty = true;
 397			_viewportDirty = true;
 398		}
 399		
 400		/**
 401		 * The scene that's used to render for this viewport
 402		 */
 403		public function get scene():Scene3D
 404		{
 405			return _scene;
 406		}
 407		
 408		/**
 409		 * Set the scene that's used to render for this viewport
 410		 */
 411		public function set scene(scene:Scene3D):void
 412		{
 413			_scene.removeEventListener(Scene3DEvent.PARTITION_CHANGED, onScenePartitionChanged);
 414			_scene = scene;
 415			_scene.addEventListener(Scene3DEvent.PARTITION_CHANGED, onScenePartitionChanged);
 416			
 417			if (_camera)
 418				_camera.partition = _scene.partition;
 419		}
 420		
 421		// todo: probably temporary:
 422		/**
 423		 * The amount of milliseconds the last render call took
 424		 */
 425		public function get deltaTime():uint
 426		{
 427			return _deltaTime;
 428		}
 429		
 430		/**
 431		 * The width of the viewport. When software rendering is used, this is limited by the
 432		 * platform to 2048 pixels.
 433		 */
 434		override public function get width():Number
 435		{
 436			return _width;
 437		}
 438		
 439		override public function set width(value:Number):void
 440		{
 441			// Backbuffer limitation in software mode. See comment in updateBackBuffer()
 442			if (_stage3DProxy && _stage3DProxy.usesSoftwareRendering && value > 2048)
 443				value = 2048;
 444			
 445			if (_width == value)
 446				return;
 447			
 448			_hitField.width = value;
 449			_width = value;
 450			
 451			_localBRPos.x = value + _localTLPos.x;
 452			_globalWidth = parent? parent.localToGlobal(_localBRPos).x - _globalPos.x : value;
 453		
 454			if (_rttBufferManager)
 455				_rttBufferManager.viewWidth = _globalWidth;
 456			
 457			_aspectRatio = _globalWidth/_globalHeight;
 458			_camera.lens.aspectRatio = _aspectRatio;
 459			_depthTextureInvalid = true;
 460			
 461			_renderer.viewWidth = _globalWidth;
 462			
 463			_scissorRect.width = _globalWidth;
 464			
 465			_backBufferInvalid = true;
 466			_scissorRectDirty = true;
 467		}
 468		
 469		/**
 470		 * The height of the viewport. When software rendering is used, this is limited by the
 471		 * platform to 2048 pixels.
 472		 */
 473		override public function get height():Number
 474		{
 475			return _height;
 476		}
 477		
 478		override public function set height(value:Number):void
 479		{
 480			// Backbuffer limitation in software mode. See comment in updateBackBuffer()
 481			if (_stage3DProxy && _stage3DProxy.usesSoftwareRendering && value > 2048)
 482				value = 2048;
 483			
 484			if (_height == value)
 485				return;
 486			
 487			_hitField.height = value;
 488			_height = value;
 489			
 490			_localBRPos.y = value + _localTLPos.y;
 491			_globalHeight = parent? parent.localToGlobal(_localBRPos).y - _globalPos.y : value;
 492			
 493			if (_rttBufferManager)
 494				_rttBufferManager.viewHeight = _globalHeight;
 495			
 496			_aspectRatio = _globalWidth/_globalHeight;
 497			_camera.lens.aspectRatio = _aspectRatio;
 498			_depthTextureInvalid = true;
 499			
 500			_renderer.viewHeight = _globalHeight;
 501			
 502			_scissorRect.height = _globalHeight;
 503			
 504			_backBufferInvalid = true;
 505			_scissorRectDirty = true;
 506		}
 507		
 508		override public function set x(value:Number):void
 509		{
 510			if (x == value)
 511				return;
 512			
 513			_localTLPos.x = super.x = value;
 514			
 515			_globalPos.x = parent? parent.localToGlobal(_localTLPos).x : value;
 516			_globalPosDirty = true;
 517		}
 518		
 519		override public function set y(value:Number):void
 520		{
 521			if (y == value)
 522				return;
 523			
 524			_localTLPos.y = super.y = value;
 525			
 526			_globalPos.y = parent? parent.localToGlobal(_localTLPos).y : value;
 527			_globalPosDirty = true;
 528		}
 529		
 530		override public function set visible(value:Boolean):void
 531		{
 532			super.visible = value;
 533			
 534			if (_stage3DProxy && !_shareContext)
 535				_stage3DProxy.visible = value;
 536		}
 537		
 538		/**
 539		 * The amount of anti-aliasing to be used.
 540		 */
 541		public function get antiAlias():uint
 542		{
 543			return _antiAlias;
 544		}
 545		
 546		public function set antiAlias(value:uint):void
 547		{
 548			_antiAlias = value;
 549			_renderer.antiAlias = value;
 550			
 551			_backBufferInvalid = true;
 552		}
 553		
 554		/**
 555		 * The amount of faces that were pushed through the render pipeline on the last frame render.
 556		 */
 557		public function get renderedFacesCount():uint
 558		{
 559			return _entityCollector.numTriangles;
 560		}
 561		
 562		/**
 563		 * Defers control of Context3D clear() and present() calls to Stage3DProxy, enabling multiple Stage3D frameworks
 564		 * to share the same Context3D object.
 565		 */
 566		public function get shareContext():Boolean
 567		{
 568			return _shareContext;
 569		}
 570		
 571		public function set shareContext(value:Boolean):void
 572		{
 573			if (_shareContext == value)
 574				return;
 575			
 576			_shareContext = value;
 577			_globalPosDirty = true;
 578		}
 579		
 580		/**
 581		 * Updates the backbuffer dimensions.
 582		 */
 583		protected function updateBackBuffer():void
 584		{
 585			// No reason trying to configure back buffer if there is no context available.
 586			// Doing this anyway (and relying on _stage3DProxy to cache width/height for 
 587			// context does get available) means usesSoftwareRendering won't be reliable.
 588			if (_stage3DProxy.context3D && !_shareContext) {
 589				if (_globalWidth && _globalHeight) {
 590					// Backbuffers are limited to 2048x2048 in software mode and
 591					// trying to configure the backbuffer to be bigger than that
 592					// will throw an error. Capping the value is a graceful way of
 593					// avoiding runtime exceptions for developers who are unable
 594					// to test their Away3D implementation on screens that are 
 595					// large enough for this error to ever occur.
 596					if (_stage3DProxy.usesSoftwareRendering) {
 597						// Even though these checks where already made in the width
 598						// and height setters, at that point we couldn't be sure that
 599						// the context had even been retrieved and the software flag
 600						// thus be reliable. Make checks again.
 601						if (_globalWidth > 2048)
 602							_globalWidth = 2048;
 603						if (_globalHeight > 2048)
 604							_globalHeight = 2048;
 605					}
 606					
 607					_stage3DProxy.configureBackBuffer(_globalWidth, _globalHeight, _antiAlias);
 608					_backBufferInvalid = false;
 609				} else {
 610					var stageBR:Point = new Point(stage.x + stage.stageWidth, stage.y + stage.stageHeight);
 611					width = parent? parent.globalToLocal(stageBR).x - _localTLPos.x : stage.stageWidth;
 612					height = parent? parent.globalToLocal(stageBR).y - _localTLPos.y : stage.stageHeight;
 613				}
 614			}
 615		}
 616		
 617		/**
 618		 * Defines a source url string that can be accessed though a View Source option in the right-click menu.
 619		 *
 620		 * Requires the stats panel to be enabled.
 621		 *
 622		 * @param    url        The url to the source files.
 623		 */
 624		public function addSourceURL(url:String):void
 625		{
 626			_sourceURL = url;
 627			
 628			updateRightClickMenu();
 629		}
 630		
 631		/**
 632		 * Renders the view.
 633		 */
 634		public function render():void
 635		{
 636			//if context3D has Disposed by the OS,don't render at this frame
 637			if (!stage3DProxy.recoverFromDisposal()) {
 638				_backBufferInvalid = true;
 639				return;
 640			}
 641			
 642			// reset or update render settings
 643			if (_backBufferInvalid)
 644				updateBackBuffer();
 645			
 646			if (_shareContext && _layeredView)
 647				stage3DProxy.clearDepthBuffer();
 648			
 649			if (!_parentIsStage) {
 650				var globalPos:Point = parent.localToGlobal(_localTLPos);
 651				if (_globalPos.x != globalPos.x || _globalPos.y != globalPos.y) {
 652					_globalPos = globalPos;
 653					_globalPosDirty = true;
 654				}
 655			}
 656			
 657			if (_globalPosDirty)
 658				updateGlobalPos();
 659			
 660			updateTime();
 661			
 662			updateViewSizeData();
 663			
 664			_entityCollector.clear();
 665			
 666			// collect stuff to render
 667			_scene.traversePartitions(_entityCollector);
 668			
 669			// update picking
 670			_mouse3DManager.updateCollider(this);
 671			_touch3DManager.updateCollider();
 672			
 673			if (_requireDepthRender)
 674				renderSceneDepthToTexture(_entityCollector);
 675			
 676			// todo: perform depth prepass after light update and before final render
 677			if (_depthPrepass)
 678				renderDepthPrepass(_entityCollector);
 679			
 680			_renderer.clearOnRender = !_depthPrepass;
 681			
 682			if (_filter3DRenderer && _stage3DProxy._context3D) {
 683				_renderer.render(_entityCollector, _filter3DRenderer.getMainInputTexture(_stage3DProxy), _rttBufferManager.renderToTextureRect);
 684				_filter3DRenderer.render(_stage3DProxy, camera, _depthRender);
 685			} else {
 686				_renderer.shareContext = _shareContext;
 687				if (_shareContext)
 688					_renderer.render(_entityCollector, null, _scissorRect);
 689				else
 690					_renderer.render(_entityCollector);
 691				
 692			}
 693			
 694			if (!_shareContext) {
 695				stage3DProxy.present();
 696				
 697				// fire collected mouse events
 698				_mouse3DManager.fireMouseEvents();
 699				_touch3DManager.fireTouchEvents();
 700			}
 701			
 702			// clean up data for this render
 703			_entityCollector.cleanUp();
 704			
 705			// register that a view has been rendered
 706			stage3DProxy.bufferClear = false;
 707		}
 708		
 709		protected function updateGlobalPos():void
 710		{
 711			_globalPosDirty = false;
 712			
 713			if (!_stage3DProxy)
 714				return;
 715			
 716			if (_shareContext) {
 717				_scissorRect.x = _globalPos.x - _stage3DProxy.x;
 718				_scissorRect.y = _globalPos.y - _stage3DProxy.y;
 719			} else {
 720				_scissorRect.x = 0;
 721				_scissorRect.y = 0;
 722				_stage3DProxy.x = _globalPos.x;
 723				_stage3DProxy.y = _globalPos.y;
 724			}
 725			
 726			_scissorRectDirty = true;
 727		}
 728		
 729		protected function updateTime():void
 730		{
 731			var time:Number = getTimer();
 732			if (_time == 0)
 733				_time = time;
 734			_deltaTime = time - _time;
 735			_time = time;
 736		}
 737		
 738		protected function updateViewSizeData():void
 739		{
 740			_camera.lens.aspectRatio = _aspectRatio;
 741			
 742			if (_scissorRectDirty) {
 743				_scissorRectDirty = false;
 744				_camera.lens.updateScissorRect(_scissorRect.x, _scissorRect.y, _scissorRect.width, _scissorRect.height);
 745			}
 746			
 747			if (_viewportDirty) {
 748				_viewportDirty = false;
 749				_camera.lens.updateViewport(_stage3DProxy.viewPort.x, _stage3DProxy.viewPort.y, _stage3DProxy.viewPort.width, _stage3DProxy.viewPort.height);
 750			}
 751			
 752			if (_filter3DRenderer || _renderer.renderToTexture) {
 753				_renderer.textureRatioX = _rttBufferManager.textureRatioX;
 754				_renderer.textureRatioY = _rttBufferManager.textureRatioY;
 755			} else {
 756				_renderer.textureRatioX = 1;
 757				_renderer.textureRatioY = 1;
 758			}
 759		}
 760		
 761		protected function renderDepthPrepass(entityCollector:EntityCollector):void
 762		{
 763			_depthRenderer.disableColor = true;
 764			if (_filter3DRenderer || _renderer.renderToTexture) {
 765				_depthRenderer.textureRatioX = _rttBufferManager.textureRatioX;
 766				_depthRenderer.textureRatioY = _rttBufferManager.textureRatioY;
 767				_depthRenderer.render(entityCollector, _filter3DRenderer.getMainInputTexture(_stage3DProxy), _rttBufferManager.renderToTextureRect);
 768			} else {
 769				_depthRenderer.textureRatioX = 1;
 770				_depthRenderer.textureRatioY = 1;
 771				_depthRenderer.render(entityCollector);
 772			}
 773			_depthRenderer.disableColor = false;
 774		}
 775		
 776		protected function renderSceneDepthToTexture(entityCollector:EntityCollector):void
 777		{
 778			if (_depthTextureInvalid || !_depthRender)
 779				initDepthTexture(_stage3DProxy._context3D);
 780			_depthRenderer.textureRatioX = _rttBufferManager.textureRatioX;
 781			_depthRenderer.textureRatioY = _rttBufferManager.textureRatioY;
 782			_depthRenderer.render(entityCollector, _depthRender);
 783		}
 784		
 785		private function initDepthTexture(context:Context3D):void
 786		{
 787			_depthTextureInvalid = false;
 788			
 789			if (_depthRender)
 790				_depthRender.dispose();
 791			
 792			_depthRender = context.createTexture(_rttBufferManager.textureWidth, _rttBufferManager.textureHeight, Context3DTextureFormat.BGRA, true);
 793		}
 794		
 795		/**
 796		 * Disposes all memory occupied by the view. This will also dispose the renderer.
 797		 */
 798		public function dispose():void
 799		{
 800			_stage3DProxy.removeEventListener(Stage3DEvent.VIEWPORT_UPDATED, onViewportUpdated);
 801			_stage3DProxy.removeEventListener(Stage3DEvent.CONTEXT3D_RECREATED, onContext3DRecreated);
 802			if (!shareContext)
 803				_stage3DProxy.dispose();
 804			_renderer.dispose();
 805			
 806			if (_depthRender)
 807				_depthRender.dispose();
 808			
 809			if (_rttBufferManager)
 810				_rttBufferManager.dispose();
 811			
 812			_mouse3DManager.disableMouseListeners(this);
 813			_mouse3DManager.dispose();
 814			
 815			_touch3DManager.disableTouchListeners(this);
 816			_touch3DManager.dispose();
 817			
 818			_rttBufferManager = null;
 819			_depthRender = null;
 820			_mouse3DManager = null;
 821			_touch3DManager = null;
 822			_depthRenderer = null;
 823			_stage3DProxy = null;
 824			_renderer = null;
 825			_entityCollector = null;
 826		}
 827		
 828		/**
 829		 * Calculates the projected position in screen space of the given scene position.
 830		 *
 831		 * @param point3d the position vector of the point to be projected.
 832		 * @return The absolute screen position of the given scene coordinates.
 833		 */
 834		public function project(point3d:Vector3D):Vector3D
 835		{
 836			var v:Vector3D = _camera.project(point3d);
 837			
 838			v.x = (v.x + 1.0)*_globalWidth/2.0;
 839			v.y = (v.y + 1.0)*_globalHeight/2.0;
 840			
 841			return v;
 842		}
 843		
 844		/**
 845		 * Calculates the scene position of the given screen coordinates.
 846		 *
 847		 * eg. unproject(view.mouseX, view.mouseY, 500) returns the scene position of the mouse 500 units into the screen.
 848		 *
 849		 * @param sX The absolute x coordinate in 2D relative to View3D, representing the screenX coordinate.
 850		 * @param sY The absolute y coordinate in 2D relative to View3D, representing the screenY coordinate.
 851		 * @param sZ The distance into the screen, representing the screenZ coordinate.
 852		 * @param v the destination Vector3D object
 853		 * @return The scene position of the given screen coordinates.
 854		 */
 855		public function unproject(sX:Number, sY:Number, sZ:Number, v:Vector3D = null):Vector3D
 856		{
 857			return _camera.unproject(((sX - _globalPos.x)*2 - _globalWidth)/_stage3DProxy.width, ((sY - _globalPos.y)*2 - _globalHeight)/_stage3DProxy.height, sZ, v);
 858		}
 859		
 860		/**
 861		 * Calculates the ray in scene space from the camera to the given screen coordinates.
 862		 *
 863		 * eg. getRay(view.mouseX, view.mouseY, 500) returns the ray from the camera to a position under the mouse, 500 units into the screen.
 864		 *
 865		 * @param sX The absolute x coordinate in 2D relative to View3D, representing the screenX coordinate.
 866		 * @param sY The absolute y coordinate in 2D relative to View3D, representing the screenY coordinate.
 867		 * @param sZ The distance into the screen, representing the screenZ coordinate.
 868		 * @return The ray from the camera to the scene space position of the given screen coordinates.
 869		 */
 870		public function getRay(sX:Number, sY:Number, sZ:Number):Vector3D
 871		{
 872			return _camera.getRay(((sX - _globalPos.x)*2 - _globalWidth)/_globalWidth, ((sY - _globalPos.y)*2 - _globalHeight)/_globalHeight, sZ);
 873		}
 874		
 875		public function get mousePicker():IPicker
 876		{
 877			return _mouse3DManager.mousePicker;
 878		}
 879		
 880		public function set mousePicker(value:IPicker):void
 881		{
 882			_mouse3DManager.mousePicker = value;
 883		}
 884		
 885		public function get touchPicker():IPicker
 886		{
 887			return _touch3DManager.touchPicker;
 888		}
 889		
 890		public function set touchPicker(value:IPicker):void
 891		{
 892			_touch3DManager.touchPicker = value;
 893		}
 894		
 895		/**
 896		 * The EntityCollector object that will collect all potentially visible entities in the partition tree.
 897		 *
 898		 * @see away3d.core.traverse.EntityCollector
 899		 * @private
 900		 */
 901		arcane function get entityCollector():EntityCollector
 902		{
 903			return _entityCollector;
 904		}
 905		
 906		private function onLensChanged(event:CameraEvent):void
 907		{
 908			_scissorRectDirty = true;
 909			_viewportDirty = true;
 910		}
 911		
 912		/**
 913		 * When added to the stage, retrieve a Stage3D instance
 914		 */
 915		private function onAddedToStage(event:Event):void
 916		{
 917			if (_addedToStage)
 918				return;
 919			
 920			_addedToStage = true;
 921			
 922			if (!_stage3DProxy) {
 923				_stage3DProxy = Stage3DManager.getInstance(stage).getFreeStage3DProxy(_forceSoftware, _profile);
 924				_stage3DProxy.addEventListener(Stage3DEvent.VIEWPORT_UPDATED, onViewportUpdated);
 925				_stage3DProxy.addEventListener(Stage3DEvent.CONTEXT3D_RECREATED, onContext3DRecreated);
 926			}
 927			
 928			_globalPosDirty = true;
 929			
 930			_rttBufferManager = RTTBufferManager.getInstance(_stage3DProxy);
 931			
 932			_renderer.stage3DProxy = _depthRenderer.stage3DProxy = _stage3DProxy;
 933			
 934			//default wiidth/height to stageWidth/stageHeight
 935			var stageBR:Point = new Point(stage.x + stage.stageWidth, stage.y + stage.stageHeight);
 936			if (_globalWidth == 0)
 937				width = parent? parent.globalToLocal(stageBR).x - _localTLPos.x : stage.stageWidth;
 938			else
 939				_rttBufferManager.viewWidth = _globalWidth;
 940			if (_globalWidth == 0)
 941				height = parent? parent.globalToLocal(stageBR).y - _localTLPos.y : stage.stageHeight;
 942			else
 943				_rttBufferManager.viewHeight = _globalHeight;
 944			
 945			if (_shareContext)
 946				_mouse3DManager.addViewLayer(this);
 947		}
 948		
 949		private function onAdded(event:Event):void
 950		{
 951			_parentIsStage = (parent == stage);
 952			
 953			_globalPos = parent.localToGlobal(_localTLPos);
 954			_globalPosDirty = true;
 955		}
 956		
 957		private function onViewportUpdated(event:Stage3DEvent):void
 958		{
 959			if (_shareContext) {
 960				_scissorRect.x = _globalPos.x - _stage3DProxy.x;
 961				_scissorRect.y = _globalPos.y - _stage3DProxy.y;
 962				_scissorRect.width = _globalWidth;
 963				_scissorRect.height = _globalHeight;
 964				_scissorRectDirty = true;
 965			}
 966			
 967			_viewportDirty = true;
 968		}
 969		
 970		// dead ends:
 971		override public function set z(value:Number):void
 972		{
 973		}
 974		
 975		override public function set scaleZ(value:Number):void
 976		{
 977		}
 978		
 979		override public function set rotation(value:Number):void
 980		{
 981		}
 982		
 983		override public function set rotationX(value:Number):void
 984		{
 985		}
 986		
 987		override public function set rotationY(value:Number):void
 988		{
 989		}
 990		
 991		override public function set rotationZ(value:Number):void
 992		{
 993		}
 994		
 995		override public function set transform(value:Transform):void
 996		{
 997		}
 998		
 999		override public function set scaleX(value:Number):void
1000		{
1001		}
1002		
1003		override public function set scaleY(value:Number):void
1004		{
1005		}
1006	}
1007}