Skip to content

Commit

Permalink
[Heightmap] Improved the excavate option
Browse files Browse the repository at this point in the history
  • Loading branch information
Zarbuz committed Jan 28, 2019
1 parent 1d3f53f commit 59cbfce
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 26 deletions.
8 changes: 6 additions & 2 deletions SchematicToVox/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class Program
private static bool _verbose = false;
private static bool _excavate = false;
private static bool _color = false;
private static bool _top = false;

private static int _ignore_min_y = -1;
private static int _ignore_max_y = 256;
Expand All @@ -41,7 +42,8 @@ static void Main(string[] args)
{ "e|excavate", "delete all blocks which doesn't have at lease one face connected with air", v => _excavate = v != null },
{ "s|scale=", "increase the scale of each block", (int v) => _scale = v },
{ "hm|heightmap=", "create voxels terrain from heightmap", (int v)=> _heightmap = v },
{ "c|color", "enable color when generating heightmap", v => _color = v != null }
{ "c|color", "enable color when generating heightmap", v => _color = v != null },
{ "t|top", "create voxels only for top", v => _top = v != null }
};

try
Expand Down Expand Up @@ -105,6 +107,8 @@ private static void DisplayArguments()
Console.WriteLine("[INFO] Enabled option: color");
if (_heightmap != 1)
Console.WriteLine("[INFO] Enabled option: heightmap (value=" + _heightmap + ")");
if (_top)
Console.WriteLine("[INFO] Enabled option: top");
if (_scale > 1)
Console.WriteLine("[INFO] Specified increase size: " + _scale);
Console.WriteLine("[INFO] Way: " + _direction);
Expand Down Expand Up @@ -138,7 +142,7 @@ private static void ProcessSchematicFile()

private static void ProcessImageFile()
{
var schematic = SchematicWriter.WriteSchematic(_inputFile, _heightmap, _excavate, _color);
var schematic = SchematicWriter.WriteSchematic(_inputFile, _heightmap, _excavate, _color, _top);
VoxWriter writer = new VoxWriter();
writer.WriteModel(_outputDir + ".vox", schematic, _direction, _scale);
}
Expand Down
2 changes: 1 addition & 1 deletion SchematicToVox/SchematicToVox.csproj.user
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|AnyCPU'">
<StartArguments>--i 25_spheres.schematic --o spheres</StartArguments>
<StartArguments>--i heightmaps/JS-Classic-2019-01-28-13-41-32-Color.png --o vox/heightmap --hm 100 --c --e</StartArguments>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|AnyCPU'">
<StartArguments>
Expand Down
71 changes: 48 additions & 23 deletions SchematicToVox/Schematics/SchematicWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@ public static class SchematicWriter
private static bool _excavate;
private static int _heightmap;
private static bool _color;
private static bool _top;


public static Schematic WriteSchematic(string path, int heightmap, bool excavate, bool color)
public static Schematic WriteSchematic(string path, int heightmap, bool excavate, bool color, bool top)
{
_excavate = excavate;
_heightmap = heightmap;
_color = color;
_top = top;

return WriteSchematicFromImage(path);
}
Expand Down Expand Up @@ -60,31 +61,29 @@ private static Schematic WriteSchematicFromImage(string path)
int y = i / schematic.Width;
var color = bitmap.GetPixel(x, y);
var colorGray = grayScale.GetPixel(x, y);
var finalColor = (_color) ? color : colorGray;
if (color.A != 0)
{
if (_heightmap != 1)
{
int intensity = colorGray.R + colorGray.G + colorGray.B;
float position = intensity / (float)765;
int height = (int)(position * _heightmap);
int height = GetHeight(colorGray);

if (_excavate)
{
if (CheckCornerPixels(bitmap, color, x, y))
GenerateFromMinNeighbor(ref schematic, ref global, grayScale, finalColor, x, y);
}
else
{
if (_top)
{
AddMultipleBlocks(ref schematic, ref global, height, x, y, color, colorGray);
Block block = new Block(x, height - 1, y, finalColor);
AddBlock(ref schematic, ref global, block);
}
else
{
Block block = (_color) ? new Block(x, height - 1, y, color) :
new Block(x, height - 1, y, colorGray);
AddBlock(ref schematic, ref global, block);
AddMultipleBlocks(ref schematic, ref global, 0, height, x, y, finalColor);
}
}
else
{
AddMultipleBlocks(ref schematic, ref global, height, x, y, color, colorGray);
}
}
else
{
Expand Down Expand Up @@ -134,12 +133,11 @@ private static Bitmap MakeGrayscale3(Bitmap original)
return newBitmap;
}

private static void AddMultipleBlocks(ref Schematic schematic, ref int global, int height, int x, int y, Color color, Color colorGray)
private static void AddMultipleBlocks(ref Schematic schematic, ref int global, int minZ, int maxZ, int x, int y, Color color)
{
for (int z = 0; z < height; z++)
for (int z = minZ; z < maxZ; z++)
{
Block block = (_color) ? new Block(x, z, y, color) :
new Block(x, z, y, colorGray);
Block block = new Block(x, z, y, color);
AddBlock(ref schematic, ref global, block);
}
}
Expand All @@ -158,22 +156,49 @@ private static void AddBlock(ref Schematic schematic, ref int global, Block bloc
}
}

private static bool CheckCornerPixels(Bitmap bitmap, Color color, int x, int y)
private static int GetHeight(Color color)
{
int intensity = color.R + color.G + color.B;
float position = intensity / (float)765;
return (int)(position * _heightmap);
}

private static void GenerateFromMinNeighbor(ref Schematic schematic, ref int global, Bitmap bitmap, Color color, int x, int y)
{
bool createAll = true;
int height = GetHeight(color);

if (x - 1 > 0 && x + 1 < bitmap.Width && y - 1 > 0 && y + 1 < bitmap.Height)
{
var colorLeft = bitmap.GetPixel(x - 1, y);
var colorTop = bitmap.GetPixel(x, y - 1);
var colorRight = bitmap.GetPixel(x + 1, y);
var colorBottom = bitmap.GetPixel(x, y + 1);

if (color == colorLeft && color == colorTop && color == colorRight && color == colorBottom)
int heightLeft = GetHeight(colorLeft);
int heightTop = GetHeight(colorTop);
int heightRight = GetHeight(colorRight);
int heightBottom = GetHeight(colorBottom);

var list = new List<int>
{
heightLeft, heightTop, heightRight, heightBottom
};

int min = list.Min();
if (min < height)
AddMultipleBlocks(ref schematic, ref global, list.Min(), height, x, y, color);
else
{
createAll = false;
Block block = new Block(x, height - 1, y, color);
AddBlock(ref schematic, ref global, block);
}

}
else
{
AddMultipleBlocks(ref schematic, ref global, 0, height, x, y, color);

}
return createAll;
}
}
}

0 comments on commit 59cbfce

Please sign in to comment.