Skip to content

Commit

Permalink
Optimised the conversion with a palette
Browse files Browse the repository at this point in the history
  • Loading branch information
Zarbuz committed May 12, 2020
1 parent 86c1247 commit f26926a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
20 changes: 16 additions & 4 deletions SchematicToVoxCore/Schematics/PaletteSchematicConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,24 @@ public Schematic ConvertSchematic(Schematic schematic)
{
Console.WriteLine("[LOG] Started to convert all colors of blocks to match the palette");
Schematic newSchematic = new Schematic(schematic.Width, schematic.Height, schematic.Length, new HashSet<Block>());
foreach (Block block in schematic.Blocks)
List<uint> colors = schematic.Colors;
Dictionary<uint, int> paletteDictionary = new Dictionary<uint, int>();
foreach (uint color in colors)
{
Color color = block.Color.UIntToColor();
int index = ColorComparison.CompareColorAll(_colors, color);
newSchematic.Blocks.Add(new Block(block.X, block.Y, block.Z, _colors[index].ColorToUInt(), index));
int index = ColorComparison.CompareColorAll(_colors, color.UIntToColor());
paletteDictionary[color] = index;
}

using (ProgressBar progressbar = new ProgressBar())
{
int i = 0;
foreach (Block block in schematic.Blocks)
{
newSchematic.Blocks.Add(new Block(block.X, block.Y, block.Z, _colors[paletteDictionary[block.Color]].ColorToUInt(), paletteDictionary[block.Color]));
progressbar.Report(i++ / (float)schematic.Blocks.Count);
}
}

Console.WriteLine("[LOG] Done.");
return newSchematic;
}
Expand Down
18 changes: 16 additions & 2 deletions SchematicToVoxCore/Schematics/Schematic.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Collections.Generic;
using System.Drawing;
using System.Linq;

namespace FileToVox.Schematics
{
Expand Down Expand Up @@ -26,7 +28,19 @@ public Schematic(ushort width, ushort height, ushort length, HashSet<Block> bloc
{
Blocks = blocks;
}



public List<uint> Colors
{
get
{
List<uint> colors = new List<uint>();
foreach (Block block in Blocks.Where(block => !colors.Contains(block.Color)))
{
colors.Add(block.Color);
}

return colors;
}
}
}
}

0 comments on commit f26926a

Please sign in to comment.