Browse Source

added optimization for empty nodes and static empty bounding box

Ronen 8 years ago
parent
commit
095b05514a

+ 6 - 0
MonoGameSceneGraph/Source/Nodes/CullingNode.cs

@@ -101,6 +101,12 @@ namespace MonoGameSceneGraph
         /// <param name="node">The child node that updated.</param>
         public override void OnChildWorldMatrixChange(Node node)
         {
+            // if node is empty do nothing, its not interesting
+            if (node.Empty)
+            {
+                return;
+            }
+
             // mark bounding box as needing update
             _isBoundingBoxDirty = true;
 

+ 20 - 1
MonoGameSceneGraph/Source/Nodes/Node.cs

@@ -44,6 +44,11 @@ namespace MonoGameSceneGraph
         /// </summary>
         public object UserData;
 
+        /// <summary>
+        /// Const return value for null bounding box.
+        /// </summary>
+        private static readonly BoundingBox EmptyBoundingBox = new BoundingBox();
+
         /// <summary>
         /// The order in which we apply transformations when building the matrix for this node.
         /// </summary>
@@ -485,6 +490,14 @@ namespace MonoGameSceneGraph
         {
         }
 
+        /// <summary>
+        /// Return true if this node is empty.
+        /// </summary>
+        public bool Empty
+        {
+            get { return _childEntities.Count == 0 && _childNodes.Count == 0; }
+        }
+
         /// <summary>
         /// Get bounding box of this node and all its child nodes.
         /// </summary>
@@ -492,6 +505,12 @@ namespace MonoGameSceneGraph
         /// <returns>Bounding box of the node and its children.</returns>
         public virtual BoundingBox GetBoundingBox(bool includeChildNodes = true)
         {
+            // if empty skip
+            if (Empty)
+            {
+                return EmptyBoundingBox;
+            }
+
             // make sure transformations are up-to-date
             UpdateTransformations();
 
@@ -540,7 +559,7 @@ namespace MonoGameSceneGraph
             // nothing in this node?
             if (corners.Count == 0)
             {
-                return new BoundingBox();
+                return EmptyBoundingBox;
             }
 
             // return final bounding box