/src/org/papervision3d/view/layer/ViewportLayer.as

https://github.com/ieseltemple/EZFLAR · ActionScript · 309 lines · 210 code · 77 blank · 22 comment · 33 complexity · f146a221b3bcf567ceefc602152d077f MD5 · raw file

  1. package org.papervision3d.view.layer {
  2. import flash.display.Graphics;
  3. import flash.display.Sprite;
  4. import flash.utils.Dictionary;
  5. import org.papervision3d.core.ns.pv3dview;
  6. import org.papervision3d.core.render.command.RenderableListItem;
  7. import org.papervision3d.objects.DisplayObject3D;
  8. import org.papervision3d.view.Viewport3D;
  9. import org.papervision3d.view.layer.util.ViewportLayerSortMode;
  10. /**
  11. * @Author Ralph Hauwert
  12. */
  13. public class ViewportLayer extends Sprite
  14. {
  15. use namespace pv3dview;
  16. public var childLayers :Array;
  17. public var layers :Dictionary = new Dictionary(true);
  18. protected var viewport :Viewport3D;
  19. public var displayObject3D :DisplayObject3D;
  20. public var displayObjects :Dictionary = new Dictionary(true);
  21. public var layerIndex :Number;
  22. public var forceDepth :Boolean = false;
  23. public var screenDepth :Number = 0;
  24. public var weight :Number = 0;
  25. public var sortMode :String = ViewportLayerSortMode.Z_SORT;
  26. public var dynamicLayer :Boolean = false;
  27. public var graphicsChannel :Graphics;
  28. public function ViewportLayer(viewport:Viewport3D, do3d:DisplayObject3D, isDynamic:Boolean = false)
  29. {
  30. super();
  31. this.viewport = viewport;
  32. this.displayObject3D = do3d;
  33. this.dynamicLayer = isDynamic;
  34. this.graphicsChannel = this.graphics;
  35. if(isDynamic){
  36. this.filters = do3d.filters;
  37. this.blendMode = do3d.blendMode;
  38. this.alpha = do3d.alpha;
  39. }
  40. if(do3d)
  41. addDisplayObject3D(do3d);
  42. init();
  43. }
  44. public function addDisplayObject3D(do3d:DisplayObject3D, recurse:Boolean = true):void{
  45. if(!do3d) return;
  46. displayObjects[do3d] = do3d;
  47. dispatchEvent(new ViewportLayerEvent(ViewportLayerEvent.CHILD_ADDED, do3d, this));
  48. if(recurse)
  49. do3d.addChildrenToLayer(do3d, this);
  50. }
  51. public function removeDisplayObject3D(do3d:DisplayObject3D):void{
  52. displayObjects[do3d] = null;
  53. dispatchEvent(new ViewportLayerEvent(ViewportLayerEvent.CHILD_REMOVED, do3d, this));
  54. }
  55. public function hasDisplayObject3D(do3d:DisplayObject3D):Boolean{
  56. return (displayObjects[do3d] != null);
  57. }
  58. protected function init():void
  59. {
  60. childLayers = new Array();
  61. }
  62. public function getChildLayer(do3d:DisplayObject3D, createNew:Boolean = true, recurse:Boolean = false):ViewportLayer{
  63. do3d = do3d.parentContainer?do3d.parentContainer:do3d;
  64. /* var index:Number = childLayerIndex(do3d);
  65. if(index > -1)
  66. return childLayers[index];
  67. for each(var vpl:ViewportLayer in childLayers){
  68. var tmpLayer:ViewportLayer = vpl.getChildLayer(do3d, false);
  69. if(tmpLayer)
  70. return tmpLayer;
  71. }
  72. */
  73. if(layers[do3d]){
  74. return layers[do3d];
  75. }
  76. //no layer found = return a new one
  77. if(createNew)
  78. return getChildLayerFor(do3d, recurse);
  79. else
  80. return null;
  81. }
  82. protected function getChildLayerFor(displayObject3D:DisplayObject3D, recurse:Boolean = false):ViewportLayer
  83. {
  84. if(displayObject3D){
  85. var vpl:ViewportLayer = new ViewportLayer(viewport,displayObject3D, displayObject3D.useOwnContainer);
  86. addLayer(vpl);
  87. if(recurse)
  88. displayObject3D.addChildrenToLayer(displayObject3D, vpl);
  89. return vpl;
  90. }else{
  91. trace("Needs to be a do3d");
  92. }
  93. return null;
  94. }
  95. public function childLayerIndex(do3d:DisplayObject3D):Number{
  96. do3d = do3d.parentContainer?do3d.parentContainer:do3d;
  97. for(var i:int=0;i<childLayers.length;i++){
  98. if(childLayers[i].hasDisplayObject3D(do3d)){
  99. return i;
  100. }
  101. }
  102. return -1;
  103. }
  104. public function addLayer(vpl:ViewportLayer):void{
  105. var do3d:DisplayObject3D;
  106. childLayers.push(vpl);
  107. addChild(vpl);
  108. vpl.addEventListener(ViewportLayerEvent.CHILD_ADDED, onChildAdded);
  109. vpl.addEventListener(ViewportLayerEvent.CHILD_REMOVED, onChildRemoved);
  110. for each(do3d in vpl.displayObjects){
  111. linkChild(do3d, vpl);
  112. }
  113. for each(var v:ViewportLayer in vpl.layers){
  114. for each(do3d in v.displayObjects){
  115. linkChild(do3d, v);
  116. }
  117. }
  118. }
  119. private function linkChild(do3d:DisplayObject3D, vpl:ViewportLayer, e:ViewportLayerEvent = null):void{
  120. layers[do3d] = vpl;
  121. dispatchEvent(new ViewportLayerEvent(ViewportLayerEvent.CHILD_ADDED, do3d, vpl));
  122. }
  123. private function unlinkChild(do3d:DisplayObject3D, e:ViewportLayerEvent = null):void{
  124. layers[do3d ] = null;
  125. dispatchEvent(new ViewportLayerEvent(ViewportLayerEvent.CHILD_REMOVED, do3d));
  126. }
  127. private function onChildAdded(e:ViewportLayerEvent):void{
  128. if(e.do3d){
  129. linkChild(e.do3d, e.layer, e);
  130. }
  131. }
  132. private function onChildRemoved(e:ViewportLayerEvent):void{
  133. if(e.do3d){
  134. unlinkChild(e.do3d, e);
  135. }
  136. }
  137. public function updateBeforeRender():void{
  138. clear();
  139. for each(var vpl:ViewportLayer in childLayers){
  140. vpl.updateBeforeRender();
  141. }
  142. }
  143. public function updateAfterRender():void{
  144. for each(var vpl:ViewportLayer in childLayers){
  145. vpl.updateAfterRender();
  146. }
  147. }
  148. public function removeLayer(vpl:ViewportLayer):void{
  149. var index:int = getChildIndex(vpl);
  150. if(index >-1){
  151. removeLayerAt(index);
  152. }else{
  153. trace("Layer not found for removal.");
  154. }
  155. }
  156. public function removeLayerAt(index:Number):void{
  157. for each(var do3d:DisplayObject3D in childLayers[index].displayObjects){
  158. unlinkChild(do3d);
  159. }
  160. removeChild(childLayers[index]);
  161. childLayers.splice(index, 1);
  162. }
  163. public function getLayerObjects(ar:Array = null):Array{
  164. if(!ar)
  165. ar = new Array();
  166. for each(var do3d:DisplayObject3D in this.displayObjects){
  167. if(do3d){
  168. ar.push(do3d);
  169. }
  170. }
  171. for each(var vpl:ViewportLayer in childLayers){
  172. vpl.getLayerObjects(ar);
  173. }
  174. return ar;
  175. }
  176. public function clear():void
  177. {
  178. /* var vpl:ViewportLayer;
  179. for each(vpl in childLayers){
  180. vpl.clear();
  181. } */
  182. graphicsChannel.clear();
  183. reset();
  184. }
  185. protected function reset():void{
  186. if( !forceDepth)
  187. screenDepth = 0;
  188. this.weight = 0;
  189. }
  190. public function sortChildLayers():void{
  191. if(sortMode == ViewportLayerSortMode.Z_SORT){
  192. childLayers.sortOn("screenDepth", Array.DESCENDING | Array.NUMERIC);
  193. }else{
  194. childLayers.sortOn("layerIndex", Array.NUMERIC);
  195. }
  196. orderLayers();
  197. }
  198. protected function orderLayers():void{
  199. //trace("---------", childLayers.length);
  200. for(var i:int = 0;i<childLayers.length;i++){
  201. this.setChildIndex(childLayers[i], i);
  202. childLayers[i].sortChildLayers();
  203. }
  204. }
  205. public function processRenderItem(rc:RenderableListItem):void{
  206. if(!forceDepth){
  207. this.screenDepth += rc.screenDepth;
  208. this.weight++;
  209. }
  210. }
  211. public function updateInfo():void{
  212. //this.screenDepth /= this.weight;
  213. for each(var vpl:ViewportLayer in childLayers){
  214. vpl.updateInfo();
  215. if(!forceDepth){
  216. this.weight += vpl.weight;
  217. this.screenDepth += (vpl.screenDepth*vpl.weight);
  218. }
  219. }
  220. if(!forceDepth)
  221. this.screenDepth /= this.weight;
  222. }
  223. public function removeAllLayers():void{
  224. for(var i:int=childLayers.length-1;i>=0;i--){
  225. removeLayerAt(i);
  226. }
  227. }
  228. }
  229. }