Ejemplo n.º 1
0
        //  Merges specified materials (from file) into our actual voxel map - overwriting materials only.
        //  We are using a regular voxel map to define areas where we want to set a specified material. Empty voxels are ignored and 
        //  only mixed/full voxels are used to tell us that that voxel will contain new material - 'materialToSet'.
        //  If we are seting indestructible material, voxel content values from merged voxel map will be used to define indestructible content.
        //  Parameter 'voxelPosition' - place where we will place merged voxel map withing actual voxel map. It's in voxel coords.
        //  IMPORTANT: THIS METHOD WILL WORK ONLY IF WE PLACE THE MAP THAT WE TRY TO MERGE FROM IN VOXEL COORDINATES THAT ARE MULTIPLY OF DATA CELL SIZE
        //  This method is used to load small material areas, overwriting actual material only if value from file is 1. Zeros are ignored (it's empty space).
        //  This method is quite fast, even on large maps - 512x512x512, so we can do more overwrites.
        //  Parameter 'materialToSet' tells us what material to set at places which are full in file. Empty are ignored - so stay as they were before this method was called.
        //  IMPORTANT: THIS MERGE MATERIAL CAN BE CALLED ONLY AFTER ALL VOXEL CONTENTS ARE LOADED. THAT'S BECAUSE WE NEED TO KNOW THEM FOR MIN CONTENT / INDESTRUCTIBLE CONTENT.
        //  Voxel map we are trying to merge into existing voxel map can be bigger or outside of area of existing voxel map. This method will just ignore those parts.
        // mk:TODO move to Data storage, hide behind interface and make sure this does not overwrite empty area material (in target voxel map)
        protected override void MergeVoxelMaterialsInternal(MyMwcVoxelFilesEnum voxelFile, Vector3I voxelPosition, MyVoxelMaterialDefinition materialToSet)
        {
            Profiler.Begin("MyVoxelMap.MergeVoxelMaterials");

            using (var fileStream = File.OpenRead(MyVoxelFiles.Get(voxelFile).GetVoxFilePath()))
            using (var gzip = new GZipStream(fileStream, CompressionMode.Decompress))
            {
                var storage = gzip.ReadString();
                Debug.Assert(storage == "Cell");

                //  Version of a VOX file
                int fileVersion = gzip.Read7BitEncodedInt();

                //  Not supported VOX file version
                Debug.Assert(fileVersion == CURRENT_FILE_VERSION);

                //  Size of this voxel map (in voxels)
                int sizeX = gzip.ReadInt32();
                int sizeY = gzip.ReadInt32();
                int sizeZ = gzip.ReadInt32();

                //  Size of data cell in voxels, doesn't have to be same as current size specified by our constants.
                int cellSizeX = gzip.ReadInt32();
                int cellSizeY = gzip.ReadInt32();
                int cellSizeZ = gzip.ReadInt32();

                int cellsCountX = sizeX / cellSizeX;
                int cellsCountY = sizeY / cellSizeY;
                int cellsCountZ = sizeZ / cellSizeZ;

                //  This method will work only if we place the map that we try to merge from in voxel coordinates that are multiply of data cell size
                Debug.Assert((voxelPosition.X & MyVoxelConstants.DATA_CELL_SIZE_IN_VOXELS_MASK) == 0);
                Debug.Assert((voxelPosition.Y & MyVoxelConstants.DATA_CELL_SIZE_IN_VOXELS_MASK) == 0);
                Debug.Assert((voxelPosition.Z & MyVoxelConstants.DATA_CELL_SIZE_IN_VOXELS_MASK) == 0);
                Vector3I cellFullForVoxelPosition;
                MyCellStorage.ComputeCellCoord(ref voxelPosition, out cellFullForVoxelPosition);

                Vector3I cellCoord;
                for (cellCoord.X = 0; cellCoord.X < cellsCountX; cellCoord.X++)
                {
                    for (cellCoord.Y = 0; cellCoord.Y < cellsCountY; cellCoord.Y++)
                    {
                        for (cellCoord.Z = 0; cellCoord.Z < cellsCountZ; cellCoord.Z++)
                        {
                            MyVoxelRangeType cellType = (MyVoxelRangeType)gzip.ReadByteNoAlloc();

                            //  We can do "continue" here, becase we need to read this file properly, even if we will ignore that data
                            var tmp = new Vector3I(
                                    cellFullForVoxelPosition.X + cellCoord.X,
                                    cellFullForVoxelPosition.Y + cellCoord.Y,
                                    cellFullForVoxelPosition.Z + cellCoord.Z);
                            bool isDataCellInVoxelMap = IsValidCellCoord(ref tmp);

                            if (cellType == MyVoxelRangeType.EMPTY)
                            {
                                //  If merged cell is empty, there is nothing to overwrite, so we can skip this cell
                                continue;
                            }
                            else if (cellType == MyVoxelRangeType.FULL)
                            {
                                //  If merged cell is full, than we reset whole material cell to 'materialToSet'
                                if (isDataCellInVoxelMap)
                                {

                                    var coord = cellFullForVoxelPosition + cellCoord;
                                    ResetCellMaterial(ref coord, materialToSet);
                                }
                            }
                            else
                            {
                                //Vector3I cellCoordInVoxels = GetVoxelCoordinatesOfDataCell(ref cellCoord);
                                Vector3I cellCoordInVoxels;
                                MyCellStorage.ComputeVoxelCoordOfCell(ref cellCoord, out cellCoordInVoxels);

                                Vector3I voxelCoordRelative;
                                voxelCoordRelative.X = voxelPosition.X + cellCoordInVoxels.X;
                                voxelCoordRelative.Y = voxelPosition.Y + cellCoordInVoxels.Y;
                                voxelCoordRelative.Z = voxelPosition.Z + cellCoordInVoxels.Z;

                                Vector3I voxelCoordInCell;
                                for (voxelCoordInCell.X = 0; voxelCoordInCell.X < cellSizeX; voxelCoordInCell.X++)
                                {
                                    for (voxelCoordInCell.Y = 0; voxelCoordInCell.Y < cellSizeY; voxelCoordInCell.Y++)
                                    {
                                        for (voxelCoordInCell.Z = 0; voxelCoordInCell.Z < cellSizeZ; voxelCoordInCell.Z++)
                                        {
                                            byte voxelFromFile = gzip.ReadByteNoAlloc();

                                            if (isDataCellInVoxelMap)
                                            {
                                                //  Ignore empty voxels, but use mixed/full for seting the material
                                                if (voxelFromFile > MyVoxelConstants.VOXEL_CONTENT_EMPTY)
                                                {
                                                    Vector3I voxelCoord = new Vector3I(
                                                        voxelCoordRelative.X + voxelCoordInCell.X,
                                                        voxelCoordRelative.Y + voxelCoordInCell.Y,
                                                        voxelCoordRelative.Z + voxelCoordInCell.Z);

                                                    //  Actual voxel content
                                                    byte voxelContent = GetContent(ref voxelCoord);

                                                    SetMaterial(materialToSet, ref voxelCoord);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            Profiler.End();
        }