SEToolbox.Interop.Asteroids.MyVoxelContentCell.SetAllVoxelContents C# (CSharp) Method

SetAllVoxelContents() public method

public SetAllVoxelContents ( byte buffer, BoundingBoxD &bounding ) : void
buffer byte
bounding BoundingBoxD
return void
        public void SetAllVoxelContents(byte[] buffer, out BoundingBoxD bounding)
        {
            // quantize the buffer and compute sum
            this._voxelContentSum = 0;
            for (var i = 0; i < buffer.Length; i++)
            {
                buffer[i] = MyVoxelContentCellContent.QuantizedValue(buffer[i]);
                this._voxelContentSum += buffer[i];
            }

            bounding = new BoundingBoxD(Vector3I.MaxValue, Vector3I.MinValue);

            // mixed-->empty/full: deallocate
            // empty/full-->mixed: allocate
            // mixed: fill with values from buffer
            if (_voxelContentSum == 0)
            {
                if (this.CellType == MyVoxelCellType.MIXED) this.Deallocate();
                this.CellType = MyVoxelCellType.EMPTY;
            }
            else if (_voxelContentSum == MyVoxelConstants.VOXEL_CELL_CONTENT_SUM_TOTAL)
            {
                if (this.CellType == MyVoxelCellType.MIXED) this.Deallocate();
                this.CellType = MyVoxelCellType.FULL;
            }
            else
            {
                if (this.CellType == MyVoxelCellType.FULL || this.CellType == MyVoxelCellType.EMPTY)
                {
                    this._cellContent = new MyVoxelContentCellContent();
                }
                if (this._cellContent != null)
                {
                    this._cellContent.SetAddVoxelContents(buffer, ref bounding);
                }
                this.CellType = MyVoxelCellType.MIXED;
            }
        }

Usage Example

Exemplo n.º 1
0
        private void LoadUncompressedV2(BinaryReader reader, bool loadMaterial, int materialBaseCount)
        {
            var materialIndexDict = new Dictionary<byte, byte>();
            for (byte i = 0; i < materialBaseCount; i++)
            {
                var idx = reader.ReadByte();
                var materialName = reader.ReadString();
                var resourceIdx = SpaceEngineersCore.Resources.GetMaterialIndex(materialName);
                materialIndexDict.Add(idx, resourceIdx);
            }

            var cellsCount = Size / _cellSize;

            for (var x = 0; x < cellsCount.X; x++)
            {
                for (var y = 0; y < cellsCount.Y; y++)
                {
                    for (var z = 0; z < cellsCount.Z; z++)
                    {
                        var cellType = (MyVoxelCellType)reader.ReadByte();

                        //  Cell's are FULL by default, therefore we don't need to change them
                        if (cellType != MyVoxelCellType.FULL)
                        {
                            var cellCoord = new Vector3I(x, y, z);
                            var newCell = new MyVoxelContentCell();
                            _voxelContentCells[cellCoord.X][cellCoord.Y][cellCoord.Z] = newCell;

                            if (cellType == MyVoxelCellType.EMPTY)
                            {
                                newCell.SetToEmpty();
                            }
                            else if (cellType == MyVoxelCellType.MIXED)
                            {
                                BoundingBoxD box;
                                newCell.SetAllVoxelContents(reader.ReadBytes(_cellSize.X * _cellSize.Y * _cellSize.Z), out box);
                                _boundingContent.Min = Vector3D.Min(_boundingContent.Min, new Vector3D((x << MyVoxelConstants.VOXEL_DATA_CELL_SIZE_IN_VOXELS_BITS) + box.Min.X, (y << MyVoxelConstants.VOXEL_DATA_CELL_SIZE_IN_VOXELS_BITS) + box.Min.Y, (z << MyVoxelConstants.VOXEL_DATA_CELL_SIZE_IN_VOXELS_BITS) + box.Min.Z));
                                _boundingContent.Max = Vector3D.Max(_boundingContent.Max, new Vector3D((x << MyVoxelConstants.VOXEL_DATA_CELL_SIZE_IN_VOXELS_BITS) + box.Max.X, (y << MyVoxelConstants.VOXEL_DATA_CELL_SIZE_IN_VOXELS_BITS) + box.Max.Y, (z << MyVoxelConstants.VOXEL_DATA_CELL_SIZE_IN_VOXELS_BITS) + box.Max.Z));
                            }
                            // ignore else condition
                        }
                        else
                        {
                            _boundingContent.Min = Vector3D.Min(_boundingContent.Min, new Vector3D(x << MyVoxelConstants.VOXEL_DATA_CELL_SIZE_IN_VOXELS_BITS, y << MyVoxelConstants.VOXEL_DATA_CELL_SIZE_IN_VOXELS_BITS, z << MyVoxelConstants.VOXEL_DATA_CELL_SIZE_IN_VOXELS_BITS));
                            _boundingContent.Max = Vector3D.Max(_boundingContent.Max, new Vector3D((x + 1 << MyVoxelConstants.VOXEL_DATA_CELL_SIZE_IN_VOXELS_BITS) - 1, (y + 1 << MyVoxelConstants.VOXEL_DATA_CELL_SIZE_IN_VOXELS_BITS) - 1, (z + 1 << MyVoxelConstants.VOXEL_DATA_CELL_SIZE_IN_VOXELS_BITS) - 1));
                        }
                    }
                }
            }

            if (reader.PeekChar() == -1 || !loadMaterial)
            {
                return;
            }

            // Read materials.
            const byte indestructibleContent = 0xff;
            for (var x = 0; x < cellsCount.X; x++)
            {
                for (var y = 0; y < cellsCount.Y; y++)
                {
                    for (var z = 0; z < cellsCount.Z; z++)
                    {
                        var matCell = _voxelMaterialCells[x][y][z];

                        var materialCount = reader.ReadByte();

                        if (materialCount == (byte)MyVoxelCellType.FULL)
                        {
                            var materialIndex = reader.ReadByte();
                            matCell.Reset(materialIndexDict[materialIndex], indestructibleContent);
                        }
                        else
                        {
                            Vector3I voxelCoordInCell;
                            for (voxelCoordInCell.X = 0; voxelCoordInCell.X < MyVoxelConstants.VOXEL_DATA_CELL_SIZE_IN_VOXELS; voxelCoordInCell.X++)
                            {
                                for (voxelCoordInCell.Y = 0; voxelCoordInCell.Y < MyVoxelConstants.VOXEL_DATA_CELL_SIZE_IN_VOXELS; voxelCoordInCell.Y++)
                                {
                                    for (voxelCoordInCell.Z = 0; voxelCoordInCell.Z < MyVoxelConstants.VOXEL_DATA_CELL_SIZE_IN_VOXELS; voxelCoordInCell.Z++)
                                    {
                                        var materialIndex = reader.ReadByte();
                                        materialCount = reader.ReadByte();
                                        matCell.SetMaterialAndIndestructibleContent(materialIndexDict[materialIndex], indestructibleContent, ref voxelCoordInCell);
                                    }
                                }
                            }
                            matCell.CalcAverageCellMaterial();
                        }
                    }
                }
            }
        }