public void PrepareItemsPhysics(MyEnvironmentItemsSpawnData spawnData)
 {
     spawnData.SectorRootShape = new HkStaticCompoundShape(HkReferencePolicy.None);
     spawnData.EnvironmentItems.PrepareItemsPhysics(spawnData.SectorRootShape, ref spawnData.AabbWorld, spawnData.SubtypeToShapes);
 }
        /// <summary>
        /// Ends spawning - finishes preparetion of items data.
        /// </summary>
        public static void EndSpawn(MyEnvironmentItemsSpawnData spawnData,bool updateGraphics = true,bool updatePhysics = true)
        {
            if (updatePhysics)
            {
                ProfilerShort.Begin("prepare physics");
                spawnData.EnvironmentItems.PrepareItemsPhysics(spawnData);
                spawnData.SubtypeToShapes.Clear();
                ProfilerShort.End();

                ProfilerShort.Begin("remove reference");
                foreach (var pair in spawnData.SubtypeToShapes)
                {
                    pair.Value.RemoveReference();
                }
                spawnData.SubtypeToShapes.Clear();
                
                ProfilerShort.End();
            }

            if (updateGraphics)
            {
                spawnData.EnvironmentItems.PrepareItemsGraphics();
            }
           
            ProfilerShort.Begin("prunning");
            
			spawnData.EnvironmentItems.UpdateGamePruningStructure();
            ProfilerShort.End();
        }
 public void ClosePhysics(MyEnvironmentItemsSpawnData data)
 {
     Physics.Close();
     Physics = null;
 }
        /// <summary>
        /// Spawn environment item with the definition subtype on world position.
        /// </summary>
        public static bool SpawnItem(MyEnvironmentItemsSpawnData spawnData, MyEnvironmentItemDefinition itemDefinition, Vector3D position, Vector3D up, bool silentOverlaps = true)
        {
            if (!MyFakes.ENABLE_ENVIRONMENT_ITEMS)
                return true;

            Debug.Assert(spawnData != null && spawnData.EnvironmentItems != null);
            //Debug.Assert(itemDefinition != null);
            if (spawnData == null || spawnData.EnvironmentItems == null || itemDefinition == null)
            {
                return false;
            }

            Vector3D forward = MyUtils.GetRandomPerpendicularVector(ref up);
            MatrixD worldMatrix = MatrixD.CreateWorld(position, forward, up);


            return spawnData.EnvironmentItems.AddItem(itemDefinition, ref worldMatrix, ref spawnData.AabbWorld, silentOverlaps);
        }
        /// <summary>
        /// Spawn Environment Items instance (e.g. forest) object which can be then used for spawning individual items (e.g. trees).
        /// </summary>
        public static MyEnvironmentItemsSpawnData BeginSpawn(MyEnvironmentItemsDefinition itemsDefinition)
        {
            var builder = MyObjectBuilderSerializer.CreateNewObject(itemsDefinition.Id.TypeId, itemsDefinition.Id.SubtypeName) as MyObjectBuilder_EnvironmentItems;
            builder.PersistentFlags |= MyPersistentEntityFlags2.Enabled | MyPersistentEntityFlags2.InScene | MyPersistentEntityFlags2.CastShadows;
            var envItems = MyEntities.CreateFromObjectBuilderAndAdd(builder) as MyEnvironmentItems;

            MyEnvironmentItemsSpawnData spawnData = new MyEnvironmentItemsSpawnData();
            spawnData.EnvironmentItems = envItems;
            return spawnData;
        }
        /// <summary>
        /// Spawn Environment Items instance (e.g. forest) object which can be then used for spawning individual items (e.g. trees).
        /// </summary>
        public static MyEnvironmentItemsSpawnData BeginSpawn(MyEnvironmentItemsDefinition itemsDefinition, bool addToScene = true, long withEntityId = 0)
        {
            var builder = MyObjectBuilderSerializer.CreateNewObject(itemsDefinition.Id.TypeId, itemsDefinition.Id.SubtypeName) as MyObjectBuilder_EnvironmentItems;
            builder.EntityId = withEntityId;
            builder.PersistentFlags |= MyPersistentEntityFlags2.Enabled | (addToScene ? MyPersistentEntityFlags2.InScene : 0)| MyPersistentEntityFlags2.CastShadows;

            MyEnvironmentItems envItems;
            
            if(addToScene)
                envItems = MyEntities.CreateFromObjectBuilderAndAdd(builder) as MyEnvironmentItems;
            else
                envItems = MyEntities.CreateFromObjectBuilder(builder) as MyEnvironmentItems;


            MyEnvironmentItemsSpawnData spawnData = new MyEnvironmentItemsSpawnData();
            spawnData.EnvironmentItems = envItems;
            return spawnData;
        }
        /// <summary>
        /// Ends spawning - finishes preparetion of items data.
        /// </summary>
        public static void EndSpawn(MyEnvironmentItemsSpawnData spawnData,bool updateGraphics = true)
        {
            spawnData.EnvironmentItems.PrepareItemsPhysics(spawnData.SectorRootShape, ref spawnData.AabbWorld);

            if (updateGraphics)
            {
                spawnData.EnvironmentItems.PrepareItemsGraphics();
            }

            foreach (var pair in spawnData.SubtypeToShapes)
            {
                pair.Value.RemoveReference();
            }
            spawnData.SubtypeToShapes.Clear();
        }
        /// <summary>
        /// Ends spawning - finishes preparetion of items data.
        /// </summary>
        public static void EndSpawn(MyEnvironmentItemsSpawnData spawnData)
        {
            spawnData.EnvironmentItems.PrepareItems(spawnData.SectorRootShape, ref spawnData.AabbWorld);

            foreach (var pair in spawnData.SubtypeToShapes)
            {
                pair.Value.RemoveReference();
            }
            spawnData.SubtypeToShapes.Clear();
        }