/src/away3d/containers/Scene3D.as

http://github.com/away3d/away3d-core-fp11 · ActionScript · 179 lines · 92 code · 23 blank · 64 comment · 3 complexity · 094c58435e10e77580e4a2d40e587dbc MD5 · raw file

  1. package away3d.containers
  2. {
  3. import away3d.arcane;
  4. import away3d.core.partition.NodeBase;
  5. import away3d.core.partition.Partition3D;
  6. import away3d.core.traverse.PartitionTraverser;
  7. import away3d.entities.Entity;
  8. import away3d.events.Scene3DEvent;
  9. import flash.events.EventDispatcher;
  10. use namespace arcane;
  11. /**
  12. * The Scene3D class represents an independent 3D scene in which 3D objects can be created and manipulated.
  13. * Multiple Scene3D instances can be created in the same SWF file.
  14. *
  15. * Scene management happens through the scene graph, which is exposed using addChild and removeChild methods.
  16. * Internally, the Scene3D object also manages any space partition objects that have been assigned to objects in
  17. * the scene graph, of which there is at least 1.
  18. */
  19. public class Scene3D extends EventDispatcher
  20. {
  21. arcane var _sceneGraphRoot:ObjectContainer3D;
  22. private var _partitions:Vector.<Partition3D>;
  23. /**
  24. * Creates a new Scene3D object.
  25. */
  26. public function Scene3D()
  27. {
  28. _partitions = new Vector.<Partition3D>();
  29. _sceneGraphRoot = new ObjectContainer3D();
  30. _sceneGraphRoot.scene = this;
  31. _sceneGraphRoot._isRoot = true;
  32. _sceneGraphRoot.partition = new Partition3D(new NodeBase());
  33. }
  34. /**
  35. * Sends a PartitionTraverser object down the scene partitions
  36. * @param traverser The traverser which will pass through the partitions.
  37. *
  38. * @see away3d.core.traverse.PartitionTraverser
  39. * @see away3d.core.traverse.EntityCollector
  40. */
  41. public function traversePartitions(traverser:PartitionTraverser):void
  42. {
  43. var i:uint;
  44. var len:uint = _partitions.length;
  45. traverser.scene = this;
  46. while (i < len)
  47. _partitions[i++].traverse(traverser);
  48. }
  49. /**
  50. * The root partition to be used by the Scene3D.
  51. */
  52. public function get partition():Partition3D
  53. {
  54. return _sceneGraphRoot.partition;
  55. }
  56. public function set partition(value:Partition3D):void
  57. {
  58. _sceneGraphRoot.partition = value;
  59. dispatchEvent(new Scene3DEvent(Scene3DEvent.PARTITION_CHANGED, _sceneGraphRoot));
  60. }
  61. public function contains(child:ObjectContainer3D):Boolean
  62. {
  63. return _sceneGraphRoot.contains(child);
  64. }
  65. /**
  66. * Adds a child to the scene's root.
  67. * @param child The child to be added to the scene
  68. * @return A reference to the added child.
  69. */
  70. public function addChild(child:ObjectContainer3D):ObjectContainer3D
  71. {
  72. return _sceneGraphRoot.addChild(child);
  73. }
  74. /**
  75. * Removes a child from the scene's root.
  76. * @param child The child to be removed from the scene.
  77. */
  78. public function removeChild(child:ObjectContainer3D):void
  79. {
  80. _sceneGraphRoot.removeChild(child);
  81. }
  82. /**
  83. * Removes a child from the scene's root.
  84. * @param index Index of child to be removed from the scene.
  85. */
  86. public function removeChildAt(index:uint):void
  87. {
  88. _sceneGraphRoot.removeChildAt(index);
  89. }
  90. /**
  91. * Retrieves the child with the given index
  92. * @param index The index for the child to be retrieved.
  93. * @return The child with the given index
  94. */
  95. public function getChildAt(index:uint):ObjectContainer3D
  96. {
  97. return _sceneGraphRoot.getChildAt(index);
  98. }
  99. /**
  100. * The amount of children directly contained by the scene.
  101. */
  102. public function get numChildren():uint
  103. {
  104. return _sceneGraphRoot.numChildren;
  105. }
  106. /**
  107. * When an entity is added to the scene, or to one of its children, add it to the partition tree.
  108. * @private
  109. */
  110. arcane function registerEntity(entity:Entity):void
  111. {
  112. var partition:Partition3D = entity.implicitPartition;
  113. addPartitionUnique(partition);
  114. partition.markForUpdate(entity);
  115. }
  116. /**
  117. * When an entity is removed from the scene, or from one of its children, remove it from its former partition tree.
  118. * @private
  119. */
  120. arcane function unregisterEntity(entity:Entity):void
  121. {
  122. entity.implicitPartition.removeEntity(entity);
  123. }
  124. /**
  125. * When an entity has moved or changed size, update its position in its partition tree.
  126. */
  127. arcane function invalidateEntityBounds(entity:Entity):void
  128. {
  129. entity.implicitPartition.markForUpdate(entity);
  130. }
  131. /**
  132. * When a partition is assigned to an object somewhere in the scene graph, add the partition to the list if it isn't in there yet
  133. */
  134. arcane function registerPartition(entity:Entity):void
  135. {
  136. addPartitionUnique(entity.implicitPartition);
  137. }
  138. /**
  139. * When a partition is removed from an object somewhere in the scene graph, remove the partition from the list
  140. */
  141. arcane function unregisterPartition(entity:Entity):void
  142. {
  143. // todo: wait... is this even correct?
  144. // shouldn't we check the number of children in implicitPartition and remove partition if 0?
  145. entity.implicitPartition.removeEntity(entity);
  146. }
  147. /**
  148. * Add a partition if it's not in the list
  149. */
  150. protected function addPartitionUnique(partition:Partition3D):void
  151. {
  152. if (_partitions.indexOf(partition) == -1)
  153. _partitions.push(partition);
  154. }
  155. }
  156. }