internal void AddInstanceLod(InstancingId id, int index, MyRenderableProxy[][] newProxies, ulong[][] newSortingKeys, BoundingBoxD aabb, Vector3D position)
        {
            ProfilerShort.Begin("AddInstanceLod");

            if (!SetDelta(id, index, -0.1f, (float)(MyEnvironment.CameraPosition - position).LengthSquared()))
            {
                MyInstanceLodId key = new MyInstanceLodId {
                    Id = id, InstanceIndex = index
                };

                MySingleInstance instance = MySingleInstance.Allocate();

                Array.Resize(ref instance.Lods, newProxies.Length);
                instance.Position = position;

                for (int lodIndex = 0; lodIndex < newProxies.Length; ++lodIndex)
                {
                    MyUtils.Init(ref instance.Lods[lodIndex]);
                    MySingleInstanceLod lod = instance.Lods[lodIndex];

                    if (lod.RenderableProxies != null && lod.RenderableProxies.Length == newProxies[lodIndex].Length)
                    {
                        Array.Copy(newProxies[lodIndex], lod.RenderableProxies, lod.RenderableProxies.Length);
                    }
                    else
                    {
                        lod.RenderableProxies = newProxies[lodIndex];
                    }

                    lod.SortingKeys = newSortingKeys[lodIndex];

                    if (lodIndex < newProxies.Length - 1)
                    {
                        int lodTransitionProxiesCount = newProxies[lodIndex].Length + newProxies[lodIndex + 1].Length;

                        Array.Resize(ref lod.RenderableProxiesForLodTransition, lodTransitionProxiesCount);
                        Array.Copy(newProxies[lodIndex], lod.RenderableProxiesForLodTransition, newProxies[lodIndex].Length);
                        Array.Copy(newProxies[lodIndex + 1], 0, lod.RenderableProxiesForLodTransition, newProxies[lodIndex].Length, newProxies[lodIndex + 1].Length);

                        int sortingKeysLength = newSortingKeys[lodIndex].Length + newSortingKeys[lodIndex + 1].Length;
                        Array.Resize(ref lod.SortingKeysForLodTransition, sortingKeysLength);
                        Array.Copy(newSortingKeys[lodIndex], lod.SortingKeysForLodTransition, newSortingKeys.Length);
                        Array.Copy(newSortingKeys[lodIndex + 1], 0, lod.SortingKeysForLodTransition, newSortingKeys.Length, newSortingKeys[lodIndex + 1].Length);
                    }
                }

                instance.CurrentLod = 0;

                instance.CullProxy = MyObjectPoolManager.Allocate <MyCullProxy>();

                instance.BtreeProxy = MyScene.DynamicRenderablesDBVH.AddProxy(ref aabb, instance.CullProxy, 0);
                m_instances.Add(key, instance);

                instance.IsDirty = true;
            }
            ProfilerShort.End();
        }
        internal void AddInstanceLod(InstancingId id, int index, MyRenderableProxy[][] newProxies, ulong[][] newSortingKeys, BoundingBoxD aabb)
        {
            VRageRender.MyRenderProxy.GetRenderProfiler().StartProfilingBlock("AddInstanceLod");
            if (!SetDelta(id, index, -0.1f))
            {
                MyInstanceLodId key = new MyInstanceLodId {
                    Id = id, InstanceIndex = index
                };

                MySingleInstance instance = new MySingleInstance();
                instance.Lods = new MySingleInstanceLod[newProxies.Length];

                for (int i = 0; i < newProxies.Length; i++)
                {
                    MySingleInstanceLod lod = new MySingleInstanceLod();
                    lod.RenderableProxies = newProxies[i];
                    lod.SortingKeys       = newSortingKeys[i];

                    if (i < newProxies.Length - 1)
                    {
                        lod.RenderableProxiesForLodTransition = new MyRenderableProxy[newProxies[i].Length + newProxies[i + 1].Length];
                        for (int j = 0; j < newProxies[i].Length; j++)
                        {
                            lod.RenderableProxiesForLodTransition[j] = newProxies[i][j];
                        }
                        for (int j = 0; j < newProxies[i + 1].Length; j++)
                        {
                            lod.RenderableProxiesForLodTransition[j + newProxies[i].Length] = newProxies[i + 1][j];
                        }

                        lod.SortingKeysForLodTransition = new ulong[newSortingKeys[i].Length + newSortingKeys[i + 1].Length];
                        for (int j = 0; j < newSortingKeys[i].Length; j++)
                        {
                            lod.SortingKeysForLodTransition[j] = newSortingKeys[i][j];
                        }
                        for (int j = 0; j < newSortingKeys[i + 1].Length; j++)
                        {
                            lod.SortingKeysForLodTransition[j + newSortingKeys[i].Length] = newSortingKeys[i + 1][j];
                        }
                    }

                    instance.Lods[i] = lod;
                }

                instance.CurrentLod = 0;

                instance.CullProxy = MyProxiesFactory.CreateCullProxy();

                instance.BtreeProxy = MyScene.RenderablesDBVH.AddProxy(ref aabb, instance.CullProxy, 0);
                m_instances.Add(key, instance);

                instance.IsDirty = true;
            }
            VRageRender.MyRenderProxy.GetRenderProfiler().EndProfilingBlock();
        }
        private void RemoveInstance(MySingleInstance instance)
        {
            Debug.Assert(instance != null, "RemoveInstance called for null instance!");
            if (instance == null)
            {
                return;
            }

            Debug.Assert(instance.BtreeProxy != -1 && instance.CullProxy != null);
            if (instance.BtreeProxy != -1)
            {
                MyScene.DynamicRenderablesDBVH.RemoveProxy(instance.BtreeProxy);
                instance.BtreeProxy = -1;
            }

            if (instance.CullProxy != null)
            {
                MyObjectPoolManager.Deallocate(instance.CullProxy);
                instance.CullProxy = null;
            }
        }
        internal void AddInstanceLod(InstancingId id, int index, MyRenderableProxy[][] newProxies, ulong[][] newSortingKeys, BoundingBoxD aabb)
        {
            VRageRender.MyRenderProxy.GetRenderProfiler().StartProfilingBlock("AddInstanceLod");
            if (!SetDelta(id, index, -0.1f))
            {
                MyInstanceLodId key = new MyInstanceLodId { Id = id, InstanceIndex = index };

                MySingleInstance instance = new MySingleInstance();
                instance.Lods = new MySingleInstanceLod[newProxies.Length];

                for (int i = 0; i < newProxies.Length; i++)
                {
                    MySingleInstanceLod lod = new MySingleInstanceLod();
                    lod.RenderableProxies = newProxies[i];
                    lod.SortingKeys = newSortingKeys[i];

                    if (i < newProxies.Length - 1)
                    {
                        lod.RenderableProxiesForLodTransition = new MyRenderableProxy[newProxies[i].Length + newProxies[i + 1].Length];
                        for (int j = 0; j < newProxies[i].Length; j++)
                        {
                            lod.RenderableProxiesForLodTransition[j] = newProxies[i][j];
                        }
                        for (int j = 0; j < newProxies[i + 1].Length; j++)
                        {
                            lod.RenderableProxiesForLodTransition[j + newProxies[i].Length] = newProxies[i + 1][j];
                        }

                        lod.SortingKeysForLodTransition = new ulong[newSortingKeys[i].Length + newSortingKeys[i + 1].Length];
                        for (int j = 0; j < newSortingKeys[i].Length; j++)
                        {
                            lod.SortingKeysForLodTransition[j] = newSortingKeys[i][j];
                        }
                        for (int j = 0; j < newSortingKeys[i + 1].Length; j++)
                        {
                            lod.SortingKeysForLodTransition[j + newSortingKeys[i].Length] = newSortingKeys[i + 1][j];
                        }
                    }

                    instance.Lods[i] = lod;
                }
                
                instance.CurrentLod = 0;

                instance.CullProxy = MyProxiesFactory.CreateCullProxy();

                instance.BtreeProxy = MyScene.RenderablesDBVH.AddProxy(ref aabb, instance.CullProxy, 0);
                m_instances.Add(key, instance);

                instance.IsDirty = true;
            }
            VRageRender.MyRenderProxy.GetRenderProfiler().EndProfilingBlock();
        }
 internal static void Release(MySingleInstance instance)
 {
     instance.ReleaseLodProxies();
     m_objectPool.Deallocate(instance);
 }
 private void RemoveAndDeallocateInstance(MySingleInstance instance)
 {
     RemoveInstance(instance);
     MySingleInstance.Deallocate(instance);
 }
 private void RemoveAndReleaseInstance(MySingleInstance instance)
 {
     RemoveInstance(instance);
     MySingleInstance.Release(instance);
 }