internal static void AddShadowRenderObject(MyRenderObject renderObject, bool rebalance = true)
        {
            if (renderObject.ShadowProxyData != MyElement.PROXY_UNASSIGNED)
                RemoveShadowRenderObject(renderObject);

            if (renderObject.ShadowProxyData == MyElement.PROXY_UNASSIGNED && renderObject.CastShadows)
            {
                var aabb = renderObject.WorldAABB;
                renderObject.SetDirty();

                renderObject.ShadowProxyData = m_shadowPrunningStructure.AddProxy(ref aabb, renderObject, 0, rebalance);
            }
        }
        internal static void MoveRenderObject(MyRenderObject renderObject)
        {

            if (renderObject.ParentCullObject != null)
            {
                renderObject.ParentCullObject.MoveRenderObject(renderObject);
                return;
            }

            var aabb = renderObject.WorldAABB;

            if (renderObject is MyManualCullableRenderObject)
            {
                m_manualCullingStructure.MoveProxy(renderObject.ProxyData, ref aabb, Vector3D.Zero);
            }
            else
                if (renderObject is MyCullableRenderObject)
                {
                    m_cullingStructure.MoveProxy(renderObject.ProxyData, ref aabb, Vector3D.Zero);
                }
                else
                {

                    if (renderObject.CullObject != null)
                    {
                        //Cannot use move because cullobject aabb then does not fit
                        //renderObject.CullObject.CulledObjects.MoveProxy(renderObject.ProxyData, ref aabb, Vector3.Zero);
                        RemoveRenderObject(renderObject, false);

                        renderObject.SetDirty();
                        renderObject.ProxyData = m_prunningStructure.AddProxy(ref aabb, renderObject, 0, true);
                        renderObject.CullObject = null;
                    }
                    else
                    {
                        m_prunningStructure.MoveProxy(renderObject.ProxyData, ref aabb, Vector3D.Zero);
                    }

                    if (renderObject.ShadowProxyData != MyElement.PROXY_UNASSIGNED)
                    {
                        m_shadowPrunningStructure.MoveProxy(renderObject.ShadowProxyData, ref aabb, Vector3D.Zero);
                    }
                }

            if (renderObject is IMyBackgroundDrawableRenderObject)
            {
                m_farObjectsPrunningStructure.MoveProxy((renderObject as IMyBackgroundDrawableRenderObject).BackgroundProxyData, ref aabb, Vector3D.Zero);
            }
        }
        internal static void AddRenderObject(MyRenderObject renderObject, bool rebalance = true)
        {
            if (renderObject is MyManualCullableRenderObject)
            {
                var boundingBox = renderObject.WorldAABB;
                renderObject.ProxyData = m_manualCullingStructure.AddProxy(ref boundingBox, renderObject, 0, rebalance);

                MyManualCullableRenderObject cullableObject = renderObject as MyManualCullableRenderObject;
                System.Diagnostics.Debug.Assert(cullableObject.GetQuery(MyOcclusionQueryID.MAIN_RENDER).OcclusionQuery == null);
                cullableObject.LoadContent();
                cullableObject.RenderCounter = m_renderObjectIncrementalCounter % OCCLUSION_INTERVAL;

                AddShadowRenderObject(renderObject, rebalance);
                return;
            }
         
            if (renderObject.NearFlag && !m_nearObjects.Contains(renderObject))
            {
                m_nearObjects.Add(renderObject);
            }
            else if (renderObject.ProxyData == MyElement.PROXY_UNASSIGNED)
            {
                var aabb = renderObject.WorldAABB;
                renderObject.SetDirty();

                if (renderObject is MyCullableRenderObject)
                {
                    MyCullableRenderObject cullableObject = renderObject as MyCullableRenderObject;

                    renderObject.ProxyData = m_cullingStructure.AddProxy(ref aabb, renderObject, 0);

                    //Move all existing included proxies to cull objects
                    m_prunningStructure.OverlapAllBoundingBox(ref aabb, m_renderObjectListForDraw);

                    foreach (MyRenderObject ro in m_renderObjectListForDraw)
                    {
                        System.Diagnostics.Debug.Assert(!(ro is MyCullableRenderObject));
                        Debug.Assert(!ro.NearFlag);

                        var roAABB = ro.WorldAABB;

                        if (ro.CullObject == null && aabb.Contains(roAABB) == VRageMath.ContainmentType.Contains)
                        {
                            RemoveRenderObject(ro, false);
                            ro.ProxyData = cullableObject.CulledObjects.AddProxy(ref roAABB, ro, 0);
                            cullableObject.EntitiesContained++;
                            ro.CullObject = cullableObject;
                        }
                    }

                    System.Diagnostics.Debug.Assert(cullableObject.GetQuery(MyOcclusionQueryID.MAIN_RENDER).OcclusionQuery == null);
                    cullableObject.LoadContent();
                    cullableObject.RenderCounter = m_renderObjectIncrementalCounter % OCCLUSION_INTERVAL;
                    m_renderObjectIncrementalCounter++;
                }
                else
                {
                    GetRenderProfiler().StartProfilingBlock("Overlap");
                    //find potential cull objects and move render object to it if it is fully included
                    m_cullingStructure.OverlapAllBoundingBox(ref aabb, m_cullObjectListForDraw);
                    GetRenderProfiler().EndProfilingBlock();
                    bool contained = false;
                    MyCullableRenderObject mostSuitableCO = null;
                    double minVolume = double.MaxValue;
                    foreach (MyCullableRenderObject co in m_cullObjectListForDraw)
                    {
                        if (co.WorldAABB.Contains(aabb) == VRageMath.ContainmentType.Contains)
                        {
                            var volume = co.WorldAABB.Volume;
                            if (volume < minVolume)
                            {
                                minVolume = volume;
                                mostSuitableCO = co;
                            }
                        }
                    }

                    if (renderObject is MyRenderAtmosphere)
                    {
                        renderObject.ProxyData = m_atmospherePurunnigStructure.AddProxy(ref aabb, renderObject, 0, rebalance);
                    }
                    else
                    {
                        if (mostSuitableCO != null)
                        {
                            GetRenderProfiler().StartProfilingBlock("AddProxy");
                            renderObject.ProxyData = mostSuitableCO.CulledObjects.AddProxy(ref aabb, renderObject, 0, rebalance);
                            GetRenderProfiler().EndProfilingBlock();
                            mostSuitableCO.EntitiesContained++;
                            renderObject.CullObject = mostSuitableCO;
                            contained = true;
                        }

                        if (!contained)
                        {
                            renderObject.ProxyData = m_prunningStructure.AddProxy(ref aabb, renderObject, 0, rebalance);
                            renderObject.CullObject = null;
                        }

                        if (renderObject.CastShadows)
                        {
                            AddShadowRenderObject(renderObject, rebalance);
                        }
                    }
                    if (renderObject is IMyBackgroundDrawableRenderObject)
                    {
                        (renderObject as IMyBackgroundDrawableRenderObject).BackgroundProxyData =  m_farObjectsPrunningStructure.AddProxy(ref aabb, renderObject, 0, rebalance);
                    }                 
                }
            }
        }