/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

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