// Set voxel content for the whole cell. public void SetAllVoxelContents(byte[] buffer) { // quantize the buffer and compute sum m_voxelContentSum = 0; for (int i = 0; i < buffer.Length; i++) { buffer[i] = MyCellStorage.Quantizer.QuantizeValue(buffer[i]); m_voxelContentSum += buffer[i]; } // mixed-->empty/full: deallocate // empty/full-->mixed: allocate // mixed: fill with values from buffer if (m_voxelContentSum == 0) { if (CellType == MyVoxelRangeType.MIXED) { Deallocate(); } CellType = MyVoxelRangeType.EMPTY; } else if (m_voxelContentSum == MyVoxelConstants.DATA_CELL_CONTENT_SUM_TOTAL) { if (CellType == MyVoxelRangeType.MIXED) { Deallocate(); } CellType = MyVoxelRangeType.FULL; } else { if (CellType == MyVoxelRangeType.FULL || CellType == MyVoxelRangeType.EMPTY) { m_cellContent = MyVoxelContentCellContents.Allocate(); } if (m_cellContent != null) { m_cellContent.SetAddVoxelContents(buffer); } CellType = MyVoxelRangeType.MIXED; } }
// Voxel at specified coordinate 'x, y, z' sets to value 'content'. Coordinates are relative to voxel cell // IMPORTANT: Do not call this method directly! Always call it through MyVoxelMap.SetVoxelContent() public void SetVoxelContent(byte content, ref Vector3I voxelCoordInCell) { content = MyCellStorage.Quantizer.QuantizeValue(content); if (CellType == MyVoxelRangeType.FULL) { if (content == MyVoxelConstants.VOXEL_CONTENT_FULL) { // Nothing is changing return; } else { m_voxelContentSum -= (MyVoxelConstants.VOXEL_CONTENT_FULL - content); CheckCellType(); // If this cell is mixed, we change voxel's value in the cell content array, but first allocate the array if (CellType == MyVoxelRangeType.MIXED) { m_cellContent = MyVoxelContentCellContents.Allocate(); if (m_cellContent != null) { m_cellContent.Reset(MyVoxelConstants.VOXEL_CONTENT_FULL); m_cellContent.SetVoxelContent(content, ref voxelCoordInCell); } } } } else if (CellType == MyVoxelRangeType.EMPTY) { if (content == MyVoxelConstants.VOXEL_CONTENT_EMPTY) { // Nothing is changing return; } else { m_voxelContentSum += content; CheckCellType(); // If this cell is mixed, we change voxel's value in the cell content array, but first allocate the array if (CellType == MyVoxelRangeType.MIXED) { m_cellContent = MyVoxelContentCellContents.Allocate(); if (m_cellContent != null) { m_cellContent.Reset(MyVoxelConstants.VOXEL_CONTENT_EMPTY); m_cellContent.SetVoxelContent(content, ref voxelCoordInCell); } } } } else if (CellType == MyVoxelRangeType.MIXED) { if (m_cellContent == null) { return; } // Check for previous content value not only for optimisation, but because we need to know how much it changed // for calculating whole cell content summary. byte previousContent = m_cellContent.GetVoxelContent(ref voxelCoordInCell); if (previousContent == content) { // New value is same as current, so nothing needs to be changed return; } m_voxelContentSum -= previousContent - content; CheckCellType(); // If this cell is still mixed, we change voxel's value in the cell content array if (CellType == MyVoxelRangeType.MIXED) { m_cellContent.SetVoxelContent(content, ref voxelCoordInCell); } } else { throw new InvalidBranchException(); } }