PageRenderTime 950ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/src/org/papervision3d/view/Viewport3D.as

http://github.com/tcha-tcho/EZFLAR
ActionScript | 347 lines | 275 code | 66 blank | 6 comment | 28 complexity | 9f6a1cfe48521a27b0b1885771d81206 MD5 | raw file
  1. package org.papervision3d.view {
  2. import flash.display.Sprite;
  3. import flash.events.Event;
  4. import flash.geom.Point;
  5. import flash.geom.Rectangle;
  6. import flash.utils.Dictionary;
  7. import org.papervision3d.core.culling.DefaultLineCuller;
  8. import org.papervision3d.core.culling.DefaultParticleCuller;
  9. import org.papervision3d.core.culling.DefaultTriangleCuller;
  10. import org.papervision3d.core.culling.ILineCuller;
  11. import org.papervision3d.core.culling.IParticleCuller;
  12. import org.papervision3d.core.culling.ITriangleCuller;
  13. import org.papervision3d.core.culling.RectangleLineCuller;
  14. import org.papervision3d.core.culling.RectangleParticleCuller;
  15. import org.papervision3d.core.culling.RectangleTriangleCuller;
  16. import org.papervision3d.core.culling.ViewportObjectFilter;
  17. import org.papervision3d.core.render.IRenderEngine;
  18. import org.papervision3d.core.render.command.IRenderListItem;
  19. import org.papervision3d.core.render.command.RenderableListItem;
  20. import org.papervision3d.core.render.data.RenderHitData;
  21. import org.papervision3d.core.render.data.RenderSessionData;
  22. import org.papervision3d.core.utils.InteractiveSceneManager;
  23. import org.papervision3d.core.view.IViewport3D;
  24. import org.papervision3d.objects.DisplayObject3D;
  25. import org.papervision3d.view.layer.ViewportBaseLayer;
  26. import org.papervision3d.view.layer.ViewportLayer;
  27. /**
  28. * @Author Ralph Hauwert
  29. */
  30. /* Changed to protected methods on 11/27/2007 by John */
  31. /* Added LineCulling on 22 May 08 by Seb Lee-Delisle */
  32. public class Viewport3D extends Sprite implements IViewport3D
  33. {
  34. //use namespace org.papervision3d.core.ns.pv3dview;
  35. protected var _width:Number;
  36. protected var _hWidth:Number;
  37. protected var _height:Number;
  38. protected var _hHeight:Number;
  39. protected var _autoClipping:Boolean;
  40. protected var _autoCulling:Boolean;
  41. protected var _autoScaleToStage:Boolean;
  42. protected var _interactive:Boolean;
  43. protected var _lastRenderer:IRenderEngine;
  44. protected var _viewportObjectFilter:ViewportObjectFilter;
  45. protected var _containerSprite:ViewportBaseLayer;
  46. protected var _layerInstances:Dictionary;
  47. public var sizeRectangle:Rectangle;
  48. public var cullingRectangle:Rectangle;
  49. public var triangleCuller:ITriangleCuller;
  50. public var particleCuller:IParticleCuller;
  51. public var lineCuller : ILineCuller;
  52. public var lastRenderList:Array;
  53. public var interactiveSceneManager:InteractiveSceneManager;
  54. protected var renderHitData:RenderHitData;
  55. public function Viewport3D(viewportWidth:Number = 640, viewportHeight:Number = 480, autoScaleToStage:Boolean = false, interactive:Boolean = false, autoClipping:Boolean = true, autoCulling:Boolean = true)
  56. {
  57. super();
  58. init();
  59. this.interactive = interactive;
  60. this.viewportWidth = viewportWidth;
  61. this.viewportHeight = viewportHeight;
  62. this.autoClipping = autoClipping;
  63. this.autoCulling = autoCulling;
  64. this.autoScaleToStage = autoScaleToStage;
  65. this._layerInstances = new Dictionary(true);
  66. }
  67. public function destroy():void
  68. {
  69. if(interactiveSceneManager){
  70. interactiveSceneManager.destroy();
  71. interactiveSceneManager = null;
  72. }
  73. lastRenderList = null;
  74. }
  75. protected function init():void
  76. {
  77. this.renderHitData = new RenderHitData();
  78. lastRenderList = new Array();
  79. sizeRectangle = new Rectangle();
  80. cullingRectangle = new Rectangle();
  81. _containerSprite = new ViewportBaseLayer(this);
  82. addChild(_containerSprite);
  83. addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
  84. addEventListener(Event.REMOVED_FROM_STAGE, onRemovedFromStage);
  85. }
  86. public function hitTestMouse():RenderHitData
  87. {
  88. var p:Point = new Point(containerSprite.mouseX, containerSprite.mouseY);
  89. return hitTestPoint2D(p);
  90. }
  91. public function hitTestPoint2D(point:Point):RenderHitData
  92. {
  93. renderHitData.clear();
  94. if(interactive){
  95. var rli:RenderableListItem;
  96. var rhd:RenderHitData = renderHitData;
  97. var rc:IRenderListItem;
  98. for(var i:uint = lastRenderList.length; rc = lastRenderList[--i]; )
  99. {
  100. if(rc is RenderableListItem)
  101. {
  102. rli = rc as RenderableListItem;
  103. rhd = rli.hitTestPoint2D(point, rhd);
  104. if(rhd.hasHit)
  105. {
  106. return rhd;
  107. }
  108. }
  109. }
  110. }
  111. return renderHitData;
  112. }
  113. public function getChildLayer(do3d:DisplayObject3D, createNew:Boolean=true, recurse:Boolean = true):ViewportLayer{
  114. return containerSprite.getChildLayer(do3d, createNew, recurse);
  115. }
  116. public function accessLayerFor(rc:RenderableListItem, setInstance:Boolean = false):ViewportLayer{
  117. var do3d:DisplayObject3D;
  118. if(rc.renderableInstance){
  119. do3d = rc.renderableInstance.instance;
  120. do3d = do3d.parentContainer?do3d.parentContainer:do3d;
  121. if(containerSprite.layers[do3d]){
  122. if(setInstance){
  123. do3d.container = containerSprite.layers[do3d];
  124. }
  125. return containerSprite.layers[do3d];
  126. }else if(do3d.useOwnContainer){
  127. return containerSprite.getChildLayer(do3d, true, true);
  128. }
  129. }
  130. return containerSprite;
  131. }
  132. protected function onAddedToStage(event:Event):void
  133. {
  134. stage.addEventListener(Event.RESIZE, onStageResize);
  135. onStageResize();
  136. }
  137. protected function onRemovedFromStage(event:Event):void
  138. {
  139. stage.removeEventListener(Event.RESIZE, onStageResize);
  140. }
  141. protected function onStageResize(event:Event = null):void
  142. {
  143. if(_autoScaleToStage)
  144. {
  145. viewportWidth = stage.stageWidth;
  146. viewportHeight = stage.stageHeight;
  147. }
  148. }
  149. public function set viewportWidth(width:Number):void
  150. {
  151. _width = width;
  152. _hWidth = width/2;
  153. containerSprite.x = _hWidth;
  154. cullingRectangle.x = -_hWidth;
  155. cullingRectangle.width = width;
  156. sizeRectangle.width = width;
  157. if(_autoClipping){
  158. scrollRect = sizeRectangle;
  159. }
  160. }
  161. public function get viewportWidth():Number
  162. {
  163. return _width;
  164. }
  165. public function set viewportHeight(height:Number):void
  166. {
  167. _height = height;
  168. _hHeight = height/2;
  169. containerSprite.y = _hHeight;
  170. cullingRectangle.y = -_hHeight;
  171. cullingRectangle.height = height;
  172. sizeRectangle.height = height;
  173. if(_autoClipping){
  174. scrollRect = sizeRectangle;
  175. }
  176. }
  177. public function get viewportHeight():Number
  178. {
  179. return _height;
  180. }
  181. public function get containerSprite():ViewportLayer
  182. {
  183. return _containerSprite;
  184. }
  185. public function set autoClipping(clip:Boolean):void
  186. {
  187. if(clip){
  188. scrollRect = sizeRectangle;
  189. }else{
  190. scrollRect = null;
  191. }
  192. _autoClipping = clip;
  193. }
  194. public function get autoClipping():Boolean
  195. {
  196. return _autoClipping;
  197. }
  198. public function set autoCulling(culling:Boolean):void
  199. {
  200. if(culling){
  201. triangleCuller = new RectangleTriangleCuller(cullingRectangle);
  202. particleCuller = new RectangleParticleCuller(cullingRectangle);
  203. lineCuller = new RectangleLineCuller(cullingRectangle);
  204. }else if(!culling){
  205. triangleCuller = new DefaultTriangleCuller();
  206. particleCuller = new DefaultParticleCuller();
  207. lineCuller = new DefaultLineCuller();
  208. }
  209. _autoCulling = culling;
  210. }
  211. public function get autoCulling():Boolean
  212. {
  213. return _autoCulling;
  214. }
  215. public function set autoScaleToStage(scale:Boolean):void
  216. {
  217. _autoScaleToStage = scale;
  218. if(scale && stage != null){
  219. onStageResize();
  220. }
  221. }
  222. public function get autoScaleToStage():Boolean
  223. {
  224. return _autoScaleToStage;
  225. }
  226. public function set interactive(b:Boolean):void
  227. {
  228. if(b != _interactive){
  229. if(_interactive && interactiveSceneManager){
  230. interactiveSceneManager.destroy();
  231. interactiveSceneManager = null;
  232. }
  233. _interactive = b;
  234. if(b){
  235. interactiveSceneManager = new InteractiveSceneManager(this);
  236. }
  237. }
  238. }
  239. public function get interactive():Boolean
  240. {
  241. return _interactive;
  242. }
  243. public function updateBeforeRender(renderSessionData:RenderSessionData):void
  244. {
  245. lastRenderList.length = 0;
  246. if(renderSessionData.renderLayers){
  247. for each(var vpl:ViewportLayer in renderSessionData.renderLayers){
  248. vpl.updateBeforeRender();
  249. }
  250. }else{
  251. _containerSprite.updateBeforeRender();
  252. }
  253. _layerInstances = new Dictionary(true);
  254. }
  255. public function updateAfterRender(renderSessionData:RenderSessionData):void
  256. {
  257. if(interactive){
  258. interactiveSceneManager.updateRenderHitData();
  259. }
  260. if(renderSessionData.renderLayers){
  261. for each(var vpl:ViewportLayer in renderSessionData.renderLayers) {
  262. vpl.updateInfo();
  263. vpl.sortChildLayers();
  264. vpl.updateAfterRender();
  265. }
  266. }else{
  267. containerSprite.updateInfo();
  268. containerSprite.updateAfterRender();
  269. }
  270. containerSprite.sortChildLayers();
  271. }
  272. public function set viewportObjectFilter(vof:ViewportObjectFilter):void
  273. {
  274. _viewportObjectFilter = vof;
  275. }
  276. public function get viewportObjectFilter():ViewportObjectFilter
  277. {
  278. return _viewportObjectFilter;
  279. }
  280. }
  281. }