SEToolbox.Models.Asteroids.AsteroidByteFiller.FillAsteroid C# (CSharp) Метод

FillAsteroid() публичный Метод

public FillAsteroid ( MyVoxelMap asteroid, IMyVoxelFillProperties fillProperties ) : void
asteroid SEToolbox.Interop.Asteroids.MyVoxelMap
fillProperties IMyVoxelFillProperties
Результат void
        public void FillAsteroid(MyVoxelMap asteroid, IMyVoxelFillProperties fillProperties)
        {
            var properties = (AsteroidByteFillProperties)fillProperties;

            IList<byte> baseAssets;
            Dictionary<byte, long> materialVoxelCells;

            asteroid.CalculateMaterialCellAssets(out baseAssets, out materialVoxelCells);

            var distribution = new List<double> { Double.NaN };
            var materialSelection = new List<byte> { SpaceEngineersCore.Resources.GetMaterialIndex(properties.MainMaterial.Value) };

            if (properties.SecondPercent > 0)
            {
                distribution.Add((double)properties.SecondPercent / 100);
                materialSelection.Add(SpaceEngineersCore.Resources.GetMaterialIndex(properties.SecondMaterial.Value));
            }
            if (properties.ThirdPercent > 0)
            {
                distribution.Add((double)properties.ThirdPercent / 100);
                materialSelection.Add(SpaceEngineersCore.Resources.GetMaterialIndex(properties.ThirdMaterial.Value));
            }
            if (properties.FourthPercent > 0)
            {
                distribution.Add((double)properties.FourthPercent / 100);
                materialSelection.Add(SpaceEngineersCore.Resources.GetMaterialIndex(properties.FourthMaterial.Value));
            }
            if (properties.FifthPercent > 0)
            {
                distribution.Add((double)properties.FifthPercent / 100);
                materialSelection.Add(SpaceEngineersCore.Resources.GetMaterialIndex(properties.FifthMaterial.Value));
            }
            if (properties.SixthPercent > 0)
            {
                distribution.Add((double)properties.SixthPercent / 100);
                materialSelection.Add(SpaceEngineersCore.Resources.GetMaterialIndex(properties.SixthMaterial.Value));
            }
            if (properties.SeventhPercent > 0)
            {
                distribution.Add((double)properties.SeventhPercent / 100);
                materialSelection.Add(SpaceEngineersCore.Resources.GetMaterialIndex(properties.SeventhMaterial.Value));
            }

            var newDistributiuon = new List<byte>();
            int count;
            for (var i = 1; i < distribution.Count(); i++)
            {
                count = (int)Math.Floor(distribution[i] * baseAssets.Count); // Round down.
                for (var j = 0; j < count; j++)
                {
                    newDistributiuon.Add(materialSelection[i]);
                }
            }
            count = baseAssets.Count - newDistributiuon.Count;
            for (var j = 0; j < count; j++)
            {
                newDistributiuon.Add(materialSelection[0]);
            }

            newDistributiuon.Shuffle();
            asteroid.SetMaterialAssets(newDistributiuon);
            //asteroid.ForceVoxelFaceMaterial(_dataModel.BaseMaterial.DisplayName);
        }
    }

Usage Example

        public void BuildEntities(out string[] sourceVoxelFiles, out MyObjectBuilder_EntityBase[] sourceEntities)
        {
            var entities = new List<MyObjectBuilder_EntityBase>();
            var sourceFiles = new List<string>();

            MainViewModel.ResetProgress(0, VoxelCollection.Count);

            foreach (var voxelDesign in VoxelCollection)
            {
                MainViewModel.Progress++;
                if (string.IsNullOrEmpty(voxelDesign.VoxelFile.SourceFilename) || !MyVoxelMap.IsVoxelMapFile(voxelDesign.VoxelFile.SourceFilename))
                    continue;


                var asteroid = new MyVoxelMap();
                string tempSourcefilename = null;

                switch (AsteroidFillType)
                {
                    case Support.AsteroidFillType.None:
                        asteroid.Load(voxelDesign.VoxelFile.SourceFilename, voxelDesign.MainMaterial.Value, false);
                        tempSourcefilename = voxelDesign.VoxelFile.SourceFilename;
                        break;

                    case AsteroidFillType.ByteFiller:
                        asteroid.Load(voxelDesign.VoxelFile.SourceFilename, voxelDesign.MainMaterial.Value, false);
                        var filler = new AsteroidByteFiller();
                        filler.FillAsteroid(asteroid, voxelDesign);
                        tempSourcefilename = TempfileUtil.NewFilename(MyVoxelMap.V2FileExtension);
                        asteroid.Save(tempSourcefilename);
                        break;
                }


                // automatically number all files, and check for duplicate filenames.
                var filename = MainViewModel.CreateUniqueVoxelStorageName(voxelDesign.VoxelFile.Name + MyVoxelMap.V2FileExtension, entities.ToArray());

                var radius = RandomUtil.GetDouble(MinimumRange, MaximumRange);
                var longitude = RandomUtil.GetDouble(0, 2 * Math.PI);
                var latitude = RandomUtil.GetDouble(-Math.PI / 2, (Math.PI / 2) + double.Epsilon);

                // Test data. Place asteroids items into a circle.
                //radius = 500;
                //longitude = Math.PI * 2 * ((double)voxelDesign.Index / VoxelCollection.Count);
                //latitude = 0;

                var x = radius * Math.Cos(latitude) * Math.Cos(longitude);
                var z = radius * Math.Cos(latitude) * Math.Sin(longitude);
                var y = radius * Math.Sin(latitude);

                var center = new Vector3D(CenterPositionX, CenterPositionY, CenterPositionZ);
                var position = center + new Vector3D(x, y, z) - asteroid.BoundingContent.Center;
                var entity = new MyObjectBuilder_VoxelMap(position, filename)
                {
                    EntityId = SpaceEngineersApi.GenerateEntityId(IDType.ASTEROID),
                    PersistentFlags = MyPersistentEntityFlags2.CastShadows | MyPersistentEntityFlags2.InScene,
                    StorageName = Path.GetFileNameWithoutExtension(filename),
                    PositionAndOrientation = new MyPositionAndOrientation
                    {
                        Position = position,
                        Forward = Vector3.Forward, // Asteroids currently don't have any orientation.
                        Up = Vector3.Up
                    }
                };

                entities.Add(entity);
                sourceFiles.Add(tempSourcefilename);
            }

            sourceVoxelFiles = sourceFiles.ToArray();
            sourceEntities = entities.ToArray();
        }